Skip to content

Commit

Permalink
Merge pull request #12 from irihitech/test
Browse files Browse the repository at this point in the history
Add UT for coverage.
  • Loading branch information
rabbitism authored Apr 6, 2024
2 parents 60c4563 + 8154424 commit 4a228d3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ private static void OnPropertyChanged<TControl, TArgs>(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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<bool> TestProperty = AvaloniaProperty.Register<Sample, bool>(
nameof(Test));

public bool Test
{
get => GetValue(TestProperty);
set => SetValue(TestProperty, value);
}

public static readonly RoutedEvent<RoutedEventArgs> TestChangedEvent =
RoutedEvent.Register<Sample, RoutedEventArgs>(nameof(TestChanged), RoutingStrategies.Bubble);

public event EventHandler<RoutedEventArgs> TestChanged
{
add => AddHandler(TestChangedEvent, value);
remove => RemoveHandler(TestChangedEvent, value);
}

static Sample()
{
TestProperty.AffectsPseudoClass<Sample, RoutedEventArgs>(":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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)GlobalUsings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AffectsPseudoClassTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\AvaloniaPropertyExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\BindingExtensionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ObservableExtensionTest.cs" />
Expand Down

0 comments on commit 4a228d3

Please sign in to comment.