ViewKeyBindingTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class ViewKeyBindingTests (ITestOutputHelper output)
  4. {
  5. private readonly ITestOutputHelper _output = output;
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void Focus_KeyBinding ()
  9. {
  10. var view = new ScopedKeyBindingView ();
  11. var keyWasHandled = false;
  12. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  13. var top = new Toplevel ();
  14. top.Add (view);
  15. Application.Begin (top);
  16. Application.RaiseKeyDownEvent (Key.A);
  17. Assert.False (keyWasHandled);
  18. Assert.True (view.ApplicationCommand);
  19. keyWasHandled = false;
  20. Application.RaiseKeyDownEvent (Key.H);
  21. Assert.True (view.HotKeyCommand);
  22. Assert.False (keyWasHandled);
  23. keyWasHandled = false;
  24. Assert.False (view.HasFocus);
  25. Application.RaiseKeyDownEvent (Key.F);
  26. Assert.False (keyWasHandled);
  27. Assert.False (view.FocusedCommand);
  28. keyWasHandled = false;
  29. view.CanFocus = true;
  30. view.SetFocus ();
  31. Assert.True (view.HasFocus);
  32. Application.RaiseKeyDownEvent (Key.F);
  33. Assert.True (view.FocusedCommand);
  34. Assert.False (keyWasHandled); // Command was invoked, but wasn't handled
  35. Assert.True (view.ApplicationCommand);
  36. Assert.True (view.HotKeyCommand);
  37. top.Dispose ();
  38. }
  39. [Fact]
  40. [AutoInitShutdown]
  41. public void Focus_KeyBinding_Negative ()
  42. {
  43. var view = new ScopedKeyBindingView ();
  44. var keyWasHandled = false;
  45. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  46. var top = new Toplevel ();
  47. top.Add (view);
  48. Application.Begin (top);
  49. Application.RaiseKeyDownEvent (Key.Z);
  50. Assert.False (keyWasHandled);
  51. Assert.False (view.ApplicationCommand);
  52. Assert.False (view.HotKeyCommand);
  53. Assert.False (view.FocusedCommand);
  54. keyWasHandled = false;
  55. Assert.False (view.HasFocus);
  56. Application.RaiseKeyDownEvent (Key.F);
  57. Assert.False (keyWasHandled);
  58. Assert.False (view.ApplicationCommand);
  59. Assert.False (view.HotKeyCommand);
  60. Assert.False (view.FocusedCommand);
  61. top.Dispose ();
  62. }
  63. [Fact]
  64. [AutoInitShutdown]
  65. public void HotKey_KeyBinding ()
  66. {
  67. var view = new ScopedKeyBindingView ();
  68. var keyWasHandled = false;
  69. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  70. var top = new Toplevel ();
  71. top.Add (view);
  72. Application.Begin (top);
  73. keyWasHandled = false;
  74. Application.RaiseKeyDownEvent (Key.H);
  75. Assert.True (view.HotKeyCommand);
  76. Assert.False (keyWasHandled);
  77. view.HotKey = KeyCode.Z;
  78. keyWasHandled = false;
  79. view.HotKeyCommand = false;
  80. Application.RaiseKeyDownEvent (Key.H); // old hot key
  81. Assert.False (keyWasHandled);
  82. Assert.False (view.HotKeyCommand);
  83. Application.RaiseKeyDownEvent (Key.Z); // new hot key
  84. Assert.True (view.HotKeyCommand);
  85. Assert.False (keyWasHandled);
  86. top.Dispose ();
  87. }
  88. [Fact]
  89. [AutoInitShutdown]
  90. public void HotKey_KeyBinding_Negative ()
  91. {
  92. var view = new ScopedKeyBindingView ();
  93. var keyWasHandled = false;
  94. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  95. var top = new Toplevel ();
  96. top.Add (view);
  97. Application.Begin (top);
  98. Application.RaiseKeyDownEvent (Key.Z);
  99. Assert.False (keyWasHandled);
  100. Assert.False (view.HotKeyCommand);
  101. keyWasHandled = false;
  102. Application.RaiseKeyDownEvent (Key.F);
  103. Assert.False (view.HotKeyCommand);
  104. top.Dispose ();
  105. }
  106. // tests that test KeyBindingScope.Focus and KeyBindingScope.HotKey (tests for KeyBindingScope.Application are in Application/KeyboardTests.cs)
  107. public class ScopedKeyBindingView : View
  108. {
  109. public ScopedKeyBindingView ()
  110. {
  111. AddCommand (Command.Save, () => ApplicationCommand = true);
  112. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  113. AddCommand (Command.Left, () => FocusedCommand = true);
  114. Application.KeyBindings.Add (Key.A, this, Command.Save);
  115. HotKey = KeyCode.H;
  116. KeyBindings.Add (Key.F, KeyBindingScope.Focused, Command.Left);
  117. }
  118. public bool ApplicationCommand { get; set; }
  119. public bool FocusedCommand { get; set; }
  120. public bool HotKeyCommand { get; set; }
  121. }
  122. }