KeyboardDeviceTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Input.Raw;
  4. using Avalonia.UnitTests;
  5. using Moq;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Input
  8. {
  9. public class KeyboardDeviceTests
  10. {
  11. [Fact]
  12. public void Keypresses_Should_Be_Sent_To_Root_If_No_Focused_Element()
  13. {
  14. var target = new KeyboardDevice();
  15. var root = new Mock<IInputRoot>();
  16. target.ProcessRawEvent(
  17. new RawKeyEventArgs(
  18. target,
  19. 0,
  20. root.Object,
  21. RawKeyEventType.KeyDown,
  22. Key.A,
  23. RawInputModifiers.None,
  24. PhysicalKey.A,
  25. "a"));
  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. PhysicalKey.A,
  49. "a"));
  50. Assert.Equal(1, raised);
  51. }
  52. [Fact]
  53. public void TextInput_Should_Be_Sent_To_Root_If_No_Focused_Element()
  54. {
  55. var target = new KeyboardDevice();
  56. var root = new Mock<IInputRoot>();
  57. target.ProcessRawEvent(
  58. new RawTextInputEventArgs(
  59. target,
  60. 0,
  61. root.Object,
  62. "Foo"));
  63. root.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
  64. }
  65. [Fact]
  66. public void TextInput_Should_Be_Sent_To_Focused_Element()
  67. {
  68. var target = new KeyboardDevice();
  69. var focused = new Control();
  70. var root = new TestRoot();
  71. var raised = 0;
  72. target.SetFocusedElement(
  73. focused,
  74. NavigationMethod.Unspecified,
  75. KeyModifiers.None);
  76. focused.TextInput += (s, e) => ++raised;
  77. target.ProcessRawEvent(
  78. new RawTextInputEventArgs(
  79. target,
  80. 0,
  81. root,
  82. "Foo"));
  83. Assert.Equal(1, raised);
  84. }
  85. [Fact]
  86. public void Can_Change_KeyBindings_In_Keybinding_Event_Handler()
  87. {
  88. var target = new KeyboardDevice();
  89. var button = new Button();
  90. var root = new TestRoot(button);
  91. var raised = 0;
  92. button.KeyBindings.Add(new KeyBinding
  93. {
  94. Gesture = new KeyGesture(Key.O, KeyModifiers.Control),
  95. Command = new Utilities.DelegateCommand(() =>
  96. {
  97. button.KeyBindings.Clear();
  98. ++raised;
  99. }),
  100. });
  101. target.SetFocusedElement(button, NavigationMethod.Pointer, 0);
  102. target.ProcessRawEvent(
  103. new RawKeyEventArgs(
  104. target,
  105. 0,
  106. root,
  107. RawKeyEventType.KeyDown,
  108. Key.O,
  109. RawInputModifiers.Control,
  110. PhysicalKey.O,
  111. "o"));
  112. Assert.Equal(1, raised);
  113. }
  114. [Fact]
  115. public void Control_Focus_Should_Be_Set_Before_FocusedElement_Raises_PropertyChanged()
  116. {
  117. var target = new KeyboardDevice();
  118. var focused = new Control();
  119. var root = new TestRoot();
  120. var gotFocusRaised = 0;
  121. var propertyChangedRaised = 0;
  122. focused.GotFocus += (s, e) => ++gotFocusRaised;
  123. target.PropertyChanged += (s, e) =>
  124. {
  125. if (e.PropertyName == nameof(target.FocusedElement))
  126. {
  127. Assert.Equal(1, gotFocusRaised);
  128. ++propertyChangedRaised;
  129. }
  130. };
  131. target.SetFocusedElement(
  132. focused,
  133. NavigationMethod.Unspecified,
  134. KeyModifiers.None);
  135. Assert.Equal(1, propertyChangedRaised);
  136. }
  137. [Fact]
  138. public void Cancelled_Focus_Change_Should_Not_Send_Got_Focus_Event()
  139. {
  140. var target = new KeyboardDevice();
  141. var focused = new Control();
  142. var root = new TestRoot();
  143. bool focusCancelled = false;
  144. focused.GettingFocus += (s, e) =>
  145. {
  146. focusCancelled = e.TryCancel();
  147. };
  148. focused.GotFocus += (s, e) =>
  149. {
  150. focusCancelled = false;
  151. };
  152. target.SetFocusedElement(
  153. focused,
  154. NavigationMethod.Unspecified,
  155. KeyModifiers.None);
  156. Assert.True(focusCancelled);
  157. }
  158. [Fact]
  159. public void Redirected_Focus_Should_Change_Focused_Element()
  160. {
  161. var target = new KeyboardDevice();
  162. var first = new Control();
  163. var second = new Control();
  164. var stack = new StackPanel();
  165. stack.Children.AddRange(new[] { first, second });
  166. var root = new TestRoot(stack);
  167. first.GettingFocus += (s, e) =>
  168. {
  169. e.TrySetNewFocusedElement(second);
  170. };
  171. target.SetFocusedElement(
  172. first,
  173. NavigationMethod.Unspecified,
  174. KeyModifiers.None);
  175. Assert.True(second.IsFocused);
  176. }
  177. }
  178. }