KeyBindingsTests.cs 4.5 KB

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