KeyboardDeviceTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Windows.Input;
  3. using Avalonia.Controls;
  4. using Avalonia.Input.Raw;
  5. using Avalonia.Interactivity;
  6. using Avalonia.UnitTests;
  7. using Moq;
  8. using Xunit;
  9. namespace Avalonia.Input.UnitTests
  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 Mock<IInputElement>();
  33. var root = Mock.Of<IInputRoot>();
  34. target.SetFocusedElement(
  35. focused.Object,
  36. NavigationMethod.Unspecified,
  37. KeyModifiers.None);
  38. target.ProcessRawEvent(
  39. new RawKeyEventArgs(
  40. target,
  41. 0,
  42. root,
  43. RawKeyEventType.KeyDown,
  44. Key.A,
  45. RawInputModifiers.None));
  46. focused.Verify(x => x.RaiseEvent(It.IsAny<KeyEventArgs>()));
  47. }
  48. [Fact]
  49. public void TextInput_Should_Be_Sent_To_Root_If_No_Focused_Element()
  50. {
  51. var target = new KeyboardDevice();
  52. var root = new Mock<IInputRoot>();
  53. target.ProcessRawEvent(
  54. new RawTextInputEventArgs(
  55. target,
  56. 0,
  57. root.Object,
  58. "Foo"));
  59. root.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
  60. }
  61. [Fact]
  62. public void TextInput_Should_Be_Sent_To_Focused_Element()
  63. {
  64. var target = new KeyboardDevice();
  65. var focused = new Mock<IInputElement>();
  66. var root = Mock.Of<IInputRoot>();
  67. target.SetFocusedElement(
  68. focused.Object,
  69. NavigationMethod.Unspecified,
  70. KeyModifiers.None);
  71. target.ProcessRawEvent(
  72. new RawTextInputEventArgs(
  73. target,
  74. 0,
  75. root,
  76. "Foo"));
  77. focused.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
  78. }
  79. [Fact]
  80. public void Can_Change_KeyBindings_In_Keybinding_Event_Handler()
  81. {
  82. var target = new KeyboardDevice();
  83. var button = new Button();
  84. var root = new TestRoot(button);
  85. button.KeyBindings.Add(new KeyBinding
  86. {
  87. Gesture = new KeyGesture(Key.O, KeyModifiers.Control),
  88. Command = new DelegateCommand(() => button.KeyBindings.Clear()),
  89. });
  90. target.SetFocusedElement(button, NavigationMethod.Pointer, 0);
  91. target.ProcessRawEvent(
  92. new RawKeyEventArgs(
  93. target,
  94. 0,
  95. root,
  96. RawKeyEventType.KeyDown,
  97. Key.O,
  98. RawInputModifiers.Control));
  99. }
  100. private class DelegateCommand : ICommand
  101. {
  102. private readonly Action _action;
  103. public DelegateCommand(Action action) => _action = action;
  104. public event EventHandler CanExecuteChanged;
  105. public bool CanExecute(object parameter) => true;
  106. public void Execute(object parameter) => _action();
  107. }
  108. }
  109. }