KeyboardDeviceTests.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Avalonia.Input.Raw;
  2. using Avalonia.Interactivity;
  3. using Moq;
  4. using Xunit;
  5. namespace Avalonia.Input.UnitTests
  6. {
  7. public class KeyboardDeviceTests
  8. {
  9. [Fact]
  10. public void Keypresses_Should_Be_Sent_To_Root_If_No_Focused_Element()
  11. {
  12. var target = new KeyboardDevice();
  13. var root = new Mock<IInputRoot>();
  14. target.ProcessRawEvent(
  15. new RawKeyEventArgs(
  16. target,
  17. 0,
  18. root.Object,
  19. RawKeyEventType.KeyDown,
  20. Key.A,
  21. RawInputModifiers.None));
  22. root.Verify(x => x.RaiseEvent(It.IsAny<KeyEventArgs>()));
  23. }
  24. [Fact]
  25. public void Keypresses_Should_Be_Sent_To_Focused_Element()
  26. {
  27. var target = new KeyboardDevice();
  28. var focused = new Mock<IInputElement>();
  29. var root = Mock.Of<IInputRoot>();
  30. target.SetFocusedElement(
  31. focused.Object,
  32. NavigationMethod.Unspecified,
  33. InputModifiers.None);
  34. target.ProcessRawEvent(
  35. new RawKeyEventArgs(
  36. target,
  37. 0,
  38. root,
  39. RawKeyEventType.KeyDown,
  40. Key.A,
  41. RawInputModifiers.None));
  42. focused.Verify(x => x.RaiseEvent(It.IsAny<KeyEventArgs>()));
  43. }
  44. [Fact]
  45. public void TextInput_Should_Be_Sent_To_Root_If_No_Focused_Element()
  46. {
  47. var target = new KeyboardDevice();
  48. var root = new Mock<IInputRoot>();
  49. target.ProcessRawEvent(
  50. new RawTextInputEventArgs(
  51. target,
  52. 0,
  53. root.Object,
  54. "Foo"));
  55. root.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
  56. }
  57. [Fact]
  58. public void TextInput_Should_Be_Sent_To_Focused_Element()
  59. {
  60. var target = new KeyboardDevice();
  61. var focused = new Mock<IInputElement>();
  62. var root = Mock.Of<IInputRoot>();
  63. target.SetFocusedElement(
  64. focused.Object,
  65. NavigationMethod.Unspecified,
  66. InputModifiers.None);
  67. target.ProcessRawEvent(
  68. new RawTextInputEventArgs(
  69. target,
  70. 0,
  71. root,
  72. "Foo"));
  73. focused.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
  74. }
  75. }
  76. }