EventTests.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Interactivity;
  6. using Xunit;
  7. namespace Avalonia.Markup.Xaml.UnitTests.Xaml
  8. {
  9. public class EventTests : XamlTestBase
  10. {
  11. [Fact]
  12. public void Event_Is_Assigned()
  13. {
  14. var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='OnClick'/>";
  15. var loader = new AvaloniaXamlLoader();
  16. var target = new MyButton();
  17. loader.Load(xaml, rootInstance: target);
  18. target.RaiseEvent(new RoutedEventArgs
  19. {
  20. RoutedEvent = Button.ClickEvent,
  21. });
  22. Assert.True(target.WasClicked);
  23. }
  24. [Fact]
  25. public void Attached_Event_Is_Assigned()
  26. {
  27. var xaml = @"<Button xmlns='https://github.com/avaloniaui' Gestures.Tapped='OnTapped'/>";
  28. var loader = new AvaloniaXamlLoader();
  29. var target = new MyButton();
  30. loader.Load(xaml, rootInstance: target);
  31. target.RaiseEvent(new RoutedEventArgs
  32. {
  33. RoutedEvent = Gestures.TappedEvent,
  34. });
  35. Assert.True(target.WasTapped);
  36. }
  37. [Fact]
  38. public void Exception_Is_Thrown_If_Event_Not_Found()
  39. {
  40. var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>";
  41. var loader = new AvaloniaXamlLoader();
  42. var target = new MyButton();
  43. XamlTestHelpers.AssertThrowsXamlException(() => loader.Load(xaml, rootInstance: target));
  44. }
  45. public class MyButton : Button
  46. {
  47. public bool WasClicked { get; private set; }
  48. public bool WasTapped { get; private set; }
  49. public void OnClick(object sender, RoutedEventArgs e) => WasClicked = true;
  50. public void OnTapped(object sender, RoutedEventArgs e) => WasTapped = true;
  51. }
  52. }
  53. }