From ebf4a35d90c9e80a6f834791b4c172f4d2b8dbda Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sat, 6 Apr 2024 22:12:03 +0800 Subject: [PATCH 1/2] feat: add ut for coverage. --- .../Helpers/AvaloniaPropertyExtension.cs | 1 - .../Helpers/AffectsPseudoClassTests.cs | 27 +++++++++++++++ .../Helpers/RoutedEventExtensionTest.cs | 34 +++++++++++++++++++ ....Avalonia.Shared.UnitTest.Public.projitems | 1 + 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs 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..9368c16 --- /dev/null +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs @@ -0,0 +1,27 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Irihi.Avalonia.Shared.Helpers; + +namespace Irihi.Avalonia.Shared.UnitTest.Helpers; + +public class AffectsPseudoClassTests +{ + [Fact] + public void TestAffectsPseudoClass() + { + // Arrange + var button = new Button(); + var property = AvaloniaProperty.Register("TestProperty"); + bool flag = false; + button.Click += (sender, e) => flag = true; + + // Act + property.AffectsPseudoClass(pseudoClass: "test", Button.ClickEvent); + property.SetValue(true, button); + + // Assert + Assert.Contains("test", button.Classes); + 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 @@ + From 8154424c8baf24928e20d6b75802d0943df21741 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Sat, 6 Apr 2024 22:22:14 +0800 Subject: [PATCH 2/2] test: remove warning in test. --- .../Helpers/AffectsPseudoClassTests.cs | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs index 9368c16..f29252b 100644 --- a/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs +++ b/test/Irihi.Avalonia.Shared.UnitTest.Public/Helpers/AffectsPseudoClassTests.cs @@ -7,21 +7,40 @@ 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() { - // Arrange - var button = new Button(); - var property = AvaloniaProperty.Register("TestProperty"); + Sample sample = new(); bool flag = false; - button.Click += (sender, e) => flag = true; - - // Act - property.AffectsPseudoClass(pseudoClass: "test", Button.ClickEvent); - property.SetValue(true, button); - - // Assert - Assert.Contains("test", button.Classes); + sample.TestChanged += (_, _) => flag = true; + sample.Test = true; + Assert.Contains(sample.Classes, x => x == ":test"); Assert.True(flag); } } \ No newline at end of file