KeyboardDeviceTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Windows.Input;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Input.Raw;
  6. using Avalonia.UnitTests;
  7. using Moq;
  8. using Xunit;
  9. namespace Avalonia.Base.UnitTests.Input
  10. {
  11. public class KeyboardDeviceTests
  12. {
  13. [Fact]
  14. public void Keypresses_Should_Be_Sent_To_Root_If_No_Focused_Element()
  15. {
  16. var target = new KeyboardDevice();
  17. var root = new Mock<IInputRoot>();
  18. target.ProcessRawEvent(
  19. new RawKeyEventArgs(
  20. target,
  21. 0,
  22. root.Object,
  23. RawKeyEventType.KeyDown,
  24. Key.A,
  25. RawInputModifiers.None));
  26. root.Verify(x => x.RaiseEvent(It.IsAny<KeyEventArgs>()));
  27. }
  28. [Fact]
  29. public void Keypresses_Should_Be_Sent_To_Focused_Element()
  30. {
  31. var target = new KeyboardDevice();
  32. var focused = new Control();
  33. var root = new TestRoot();
  34. var raised = 0;
  35. target.SetFocusedElement(
  36. focused,
  37. NavigationMethod.Unspecified,
  38. KeyModifiers.None);
  39. focused.KeyDown += (s, e) => ++raised;
  40. target.ProcessRawEvent(
  41. new RawKeyEventArgs(
  42. target,
  43. 0,
  44. root,
  45. RawKeyEventType.KeyDown,
  46. Key.A,
  47. RawInputModifiers.None));
  48. Assert.Equal(1, raised);
  49. }
  50. [Fact]
  51. public void TextInput_Should_Be_Sent_To_Root_If_No_Focused_Element()
  52. {
  53. var target = new KeyboardDevice();
  54. var root = new Mock<IInputRoot>();
  55. target.ProcessRawEvent(
  56. new RawTextInputEventArgs(
  57. target,
  58. 0,
  59. root.Object,
  60. "Foo"));
  61. root.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
  62. }
  63. [Fact]
  64. public void TextInput_Should_Be_Sent_To_Focused_Element()
  65. {
  66. var target = new KeyboardDevice();
  67. var focused = new Control();
  68. var root = new TestRoot();
  69. var raised = 0;
  70. target.SetFocusedElement(
  71. focused,
  72. NavigationMethod.Unspecified,
  73. KeyModifiers.None);
  74. focused.TextInput += (s, e) => ++raised;
  75. target.ProcessRawEvent(
  76. new RawTextInputEventArgs(
  77. target,
  78. 0,
  79. root,
  80. "Foo"));
  81. Assert.Equal(1, raised);
  82. }
  83. [Fact]
  84. public void Can_Change_KeyBindings_In_Keybinding_Event_Handler()
  85. {
  86. var target = new KeyboardDevice();
  87. var button = new Button();
  88. var root = new TestRoot(button);
  89. var raised = 0;
  90. button.KeyBindings.Add(new KeyBinding
  91. {
  92. Gesture = new KeyGesture(Key.O, KeyModifiers.Control),
  93. Command = new DelegateCommand(() =>
  94. {
  95. button.KeyBindings.Clear();
  96. ++raised;
  97. }),
  98. });
  99. target.SetFocusedElement(button, NavigationMethod.Pointer, 0);
  100. target.ProcessRawEvent(
  101. new RawKeyEventArgs(
  102. target,
  103. 0,
  104. root,
  105. RawKeyEventType.KeyDown,
  106. Key.O,
  107. RawInputModifiers.Control));
  108. Assert.Equal(1, raised);
  109. }
  110. private class DelegateCommand : ICommand
  111. {
  112. private readonly Action _action;
  113. public DelegateCommand(Action action) => _action = action;
  114. public event EventHandler CanExecuteChanged { add { } remove { } }
  115. public bool CanExecute(object parameter) => true;
  116. public void Execute(object parameter) => _action();
  117. }
  118. [Fact]
  119. public void Control_Focus_Should_Be_Set_Before_FocusedElement_Raises_PropertyChanged()
  120. {
  121. var target = new KeyboardDevice();
  122. var focused = new Control();
  123. var root = new TestRoot();
  124. var gotFocusRaised = 0;
  125. var propertyChangedRaised = 0;
  126. focused.GotFocus += (s, e) => ++gotFocusRaised;
  127. target.PropertyChanged += (s, e) =>
  128. {
  129. if (e.PropertyName == nameof(target.FocusedElement))
  130. {
  131. Assert.Equal(1, gotFocusRaised);
  132. ++propertyChangedRaised;
  133. }
  134. };
  135. target.SetFocusedElement(
  136. focused,
  137. NavigationMethod.Unspecified,
  138. KeyModifiers.None);
  139. Assert.Equal(1, propertyChangedRaised);
  140. }
  141. }
  142. }