diff --git a/src/Irihi.Avalonia.Shared.Public/Helpers/AvaloniaPropertyExtension.cs b/src/Irihi.Avalonia.Shared.Public/Helpers/AvaloniaPropertyExtension.cs index d177b24..ca0cf9f 100644 --- a/src/Irihi.Avalonia.Shared.Public/Helpers/AvaloniaPropertyExtension.cs +++ b/src/Irihi.Avalonia.Shared.Public/Helpers/AvaloniaPropertyExtension.cs @@ -24,7 +24,6 @@ private static void OnPropertyChanged(TControl control, Avaloni where TControl: Control where TArgs: RoutedEventArgs, new() { - if (args.Sender != control) return; PseudolassesExtensions.Set(control.Classes, pseudoClass, args.NewValue.Value); if (routedEvent is not null) { diff --git a/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs new file mode 100644 index 0000000..f29252b --- /dev/null +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs @@ -0,0 +1,46 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Irihi.Avalonia.Shared.Helpers; + +namespace Irihi.Avalonia.Shared.UnitTest.Helpers; + +public class AffectsPseudoClassTests +{ + private class Sample : Control + { + public static readonly StyledProperty TestProperty = AvaloniaProperty.Register( + nameof(Test)); + + public bool Test + { + get => GetValue(TestProperty); + set => SetValue(TestProperty, value); + } + + public static readonly RoutedEvent TestChangedEvent = + RoutedEvent.Register(nameof(TestChanged), RoutingStrategies.Bubble); + + public event EventHandler TestChanged + { + add => AddHandler(TestChangedEvent, value); + remove => RemoveHandler(TestChangedEvent, value); + } + + static Sample() + { + TestProperty.AffectsPseudoClass(":test", TestChangedEvent); + } + } + + [Fact] + public void TestAffectsPseudoClass() + { + Sample sample = new(); + bool flag = false; + sample.TestChanged += (_, _) => flag = true; + sample.Test = true; + Assert.Contains(sample.Classes, x => x == ":test"); + Assert.True(flag); + } +} \ No newline at end of file diff --git a/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/RoutedEventExtensionTest.cs b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/RoutedEventExtensionTest.cs index 7bf4a85..86d9bc8 100644 --- a/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/RoutedEventExtensionTest.cs +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/RoutedEventExtensionTest.cs @@ -50,6 +50,20 @@ void Handler(object? sender, RoutedEventArgs args) Assert.Equal(2, count); } + [Fact] + public void EventHandler_Add_With_Strategy_Success() + { + var button = new Button(); + int count = 0; + void Handler(object? sender, RoutedEventArgs args) + { + count++; + } + Button.ClickEvent.AddHandler(Handler, RoutingStrategies.Bubble, false, button); + button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent}); + Assert.Equal(1, count); + } + [Fact] public void EventHandler_Remove_Without_SideEffect() { @@ -121,4 +135,24 @@ void Handler(object? sender, RoutedEventArgs args) button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent}); Assert.Equal(2, count); } + + [Fact] + public void EventHandler_AddDisposable_With_Strategy_Success() + { + var button = new Button(); + var button2 = new Button(); + int count = 0; + void Handler(object? sender, RoutedEventArgs args) + { + count++; + } + + var _ = Button.ClickEvent.AddDisposableHandler(Handler, + RoutingStrategies.Bubble, + false, + button, button2); + button.RaiseEvent(new RoutedEventArgs(){ Source = button, RoutedEvent = Button.ClickEvent}); + button2.RaiseEvent(new RoutedEventArgs(){ Source = button2, RoutedEvent = Button.ClickEvent}); + Assert.Equal(2, count); + } } \ No newline at end of file diff --git a/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems b/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems index 53ee4cd..266d4fe 100644 --- a/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Irihi.Avalonia.Shared.UnitTest.Public.projitems @@ -10,6 +10,7 @@ +