Skip to content

Commit

Permalink
test: Automatic Routed Event Handler Genration
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Apr 9, 2024
1 parent 7abb947 commit 9adf6f2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@
<Link>PlatformFactAttribute.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Update="xunit.runner.console" Version="2.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Update="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<Import Project="..\..\build\BuildTargets.targets" />
</Project>
44 changes: 44 additions & 0 deletions tests/Avalonia.Markup.Xaml.UnitTests/Xaml/EventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public void Attached_Event_Is_Assigned()
Assert.True(target.WasTapped);
}

[Fact]
public void Attached_Event_Is_Assigned_Generic()
{
var xaml = @"<Panel xmlns='https://github.com/avaloniaui'><Grid DoubleTapped='OnTapped'><Button Name='target'/></Grid></Panel>";
var host = new MyPanel();

AvaloniaRuntimeXamlLoader.Load(xaml, rootInstance: host);

var target = host.FindControl<Button>("target");

Assert.NotNull(target);

target.RaiseEvent(new TappedEventArgs(Gestures.DoubleTappedEvent, default));

Assert.True(host.WasTapped);
}

[Fact]
public void Exception_Is_Thrown_If_Event_Not_Found()
{
Expand All @@ -48,6 +65,25 @@ public void Exception_Is_Thrown_If_Event_Not_Found()
XamlTestHelpers.AssertThrowsXamlException(() => AvaloniaRuntimeXamlLoader.Load(xaml, rootInstance: target));
}



[Fact]
public void Attached_Event_Routed_Event_Handler()
{
var xaml = @"<Panel xmlns='https://github.com/avaloniaui' Button.Click='OnClick'><Button Name='target'/></Panel>";
var host = new MyPanel();

AvaloniaRuntimeXamlLoader.Load(xaml, rootInstance: host);

var target = host.FindControl<Button>("target");
target.RaiseEvent(new RoutedEventArgs
{
RoutedEvent = Button.ClickEvent,
});

Assert.True(host.WasClicked);
}

public class MyButton : Button
{
public bool WasClicked { get; private set; }
Expand All @@ -56,5 +92,13 @@ public class MyButton : Button
public void OnClick(object sender, RoutedEventArgs e) => WasClicked = true;
public void OnTapped(object sender, RoutedEventArgs e) => WasTapped = true;
}

public class MyPanel : Panel
{
public bool WasClicked { get; private set; }
public bool WasTapped { get; private set; }
public void OnClick(object sender, RoutedEventArgs e) => WasClicked = true;
public void OnTapped(object sender, RoutedEventArgs e) => WasTapped = true;
}
}
}

0 comments on commit 9adf6f2

Please sign in to comment.