KeyBindingsTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.ViewTests;
  5. /// <summary>
  6. /// Tests for View.KeyBindings
  7. /// </summary>
  8. public class KeyBindingsTests ()
  9. {
  10. [Fact]
  11. [AutoInitShutdown]
  12. public void Focused_HotKey_Application_All_Work ()
  13. {
  14. var view = new ScopedKeyBindingView ();
  15. var keyWasHandled = false;
  16. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  17. var top = new Toplevel ();
  18. top.Add (view);
  19. Application.Begin (top);
  20. Application.RaiseKeyDownEvent (Key.A);
  21. Assert.False (keyWasHandled);
  22. Assert.True (view.ApplicationCommand);
  23. keyWasHandled = false;
  24. Application.RaiseKeyDownEvent (Key.H);
  25. Assert.True (view.HotKeyCommand);
  26. Assert.False (keyWasHandled);
  27. keyWasHandled = false;
  28. Assert.False (view.HasFocus);
  29. Application.RaiseKeyDownEvent (Key.F);
  30. Assert.False (keyWasHandled);
  31. Assert.False (view.FocusedCommand);
  32. keyWasHandled = false;
  33. view.CanFocus = true;
  34. view.SetFocus ();
  35. Assert.True (view.HasFocus);
  36. Application.RaiseKeyDownEvent (Key.F);
  37. Assert.True (view.FocusedCommand);
  38. Assert.False (keyWasHandled); // Command was invoked, but wasn't handled
  39. Assert.True (view.ApplicationCommand);
  40. Assert.True (view.HotKeyCommand);
  41. top.Dispose ();
  42. }
  43. [Fact]
  44. [AutoInitShutdown]
  45. public void KeyBinding_Negative ()
  46. {
  47. var view = new ScopedKeyBindingView ();
  48. var keyWasHandled = false;
  49. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  50. var top = new Toplevel ();
  51. top.Add (view);
  52. Application.Begin (top);
  53. Application.RaiseKeyDownEvent (Key.Z);
  54. Assert.False (keyWasHandled);
  55. Assert.False (view.ApplicationCommand);
  56. Assert.False (view.HotKeyCommand);
  57. Assert.False (view.FocusedCommand);
  58. keyWasHandled = false;
  59. Assert.False (view.HasFocus);
  60. Application.RaiseKeyDownEvent (Key.F);
  61. Assert.False (keyWasHandled);
  62. Assert.False (view.ApplicationCommand);
  63. Assert.False (view.HotKeyCommand);
  64. Assert.False (view.FocusedCommand);
  65. top.Dispose ();
  66. }
  67. [Fact]
  68. [AutoInitShutdown]
  69. public void HotKey_KeyBinding ()
  70. {
  71. var view = new ScopedKeyBindingView ();
  72. var keyWasHandled = false;
  73. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  74. var top = new Toplevel ();
  75. top.Add (view);
  76. Application.Begin (top);
  77. keyWasHandled = false;
  78. Application.RaiseKeyDownEvent (Key.H);
  79. Assert.True (view.HotKeyCommand);
  80. Assert.False (keyWasHandled);
  81. view.HotKey = KeyCode.Z;
  82. keyWasHandled = false;
  83. view.HotKeyCommand = false;
  84. Application.RaiseKeyDownEvent (Key.H); // old hot key
  85. Assert.False (keyWasHandled);
  86. Assert.False (view.HotKeyCommand);
  87. Application.RaiseKeyDownEvent (Key.Z); // new hot key
  88. Assert.True (view.HotKeyCommand);
  89. Assert.False (keyWasHandled);
  90. top.Dispose ();
  91. }
  92. [Fact]
  93. [AutoInitShutdown]
  94. public void HotKey_KeyBinding_Negative ()
  95. {
  96. var view = new ScopedKeyBindingView ();
  97. var keyWasHandled = false;
  98. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  99. var top = new Toplevel ();
  100. top.Add (view);
  101. Application.Begin (top);
  102. Application.RaiseKeyDownEvent (Key.Z);
  103. Assert.False (keyWasHandled);
  104. Assert.False (view.HotKeyCommand);
  105. keyWasHandled = false;
  106. Application.RaiseKeyDownEvent (Key.F);
  107. Assert.False (view.HotKeyCommand);
  108. top.Dispose ();
  109. }
  110. [Fact]
  111. [AutoInitShutdown]
  112. public void HotKey_Enabled_False_Does_Not_Invoke ()
  113. {
  114. var view = new ScopedKeyBindingView ();
  115. var keyWasHandled = false;
  116. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  117. var top = new Toplevel ();
  118. top.Add (view);
  119. Application.Begin (top);
  120. Application.RaiseKeyDownEvent (Key.Z);
  121. Assert.False (keyWasHandled);
  122. Assert.False (view.HotKeyCommand);
  123. keyWasHandled = false;
  124. view.Enabled = false;
  125. Application.RaiseKeyDownEvent (Key.F);
  126. Assert.False (view.HotKeyCommand);
  127. top.Dispose ();
  128. }
  129. [Fact]
  130. public void HotKey_Raises_HotKeyCommand ()
  131. {
  132. var hotKeyRaised = false;
  133. var acceptRaised = false;
  134. var selectRaised = false;
  135. Application.TopRunnable = new Toplevel ();
  136. var view = new View
  137. {
  138. CanFocus = true,
  139. HotKeySpecifier = new Rune ('_'),
  140. Title = "_Test"
  141. };
  142. Application.TopRunnable.Add (view);
  143. view.HandlingHotKey += (s, e) => hotKeyRaised = true;
  144. view.Accepting += (s, e) => acceptRaised = true;
  145. view.Selecting += (s, e) => selectRaised = true;
  146. Assert.Equal (KeyCode.T, view.HotKey);
  147. Assert.True (Application.RaiseKeyDownEvent (Key.T));
  148. Assert.True (hotKeyRaised);
  149. Assert.False (acceptRaised);
  150. Assert.False (selectRaised);
  151. hotKeyRaised = false;
  152. Assert.True (Application.RaiseKeyDownEvent (Key.T.WithAlt));
  153. Assert.True (hotKeyRaised);
  154. Assert.False (acceptRaised);
  155. Assert.False (selectRaised);
  156. hotKeyRaised = false;
  157. view.HotKey = KeyCode.E;
  158. Assert.True (Application.RaiseKeyDownEvent (Key.E.WithAlt));
  159. Assert.True (hotKeyRaised);
  160. Assert.False (acceptRaised);
  161. Assert.False (selectRaised);
  162. Application.TopRunnable.Dispose ();
  163. Application.ResetState (true);
  164. }
  165. // tests that test KeyBindingScope.Focus and KeyBindingScope.HotKey (tests for KeyBindingScope.Application are in Application/KeyboardTests.cs)
  166. public class ScopedKeyBindingView : View
  167. {
  168. public ScopedKeyBindingView ()
  169. {
  170. AddCommand (Command.Save, () => ApplicationCommand = true);
  171. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  172. AddCommand (Command.Left, () => FocusedCommand = true);
  173. Application.KeyBindings.Add (Key.A, this, Command.Save);
  174. HotKey = KeyCode.H;
  175. KeyBindings.Add (Key.F, Command.Left);
  176. }
  177. public bool ApplicationCommand { get; set; }
  178. public bool FocusedCommand { get; set; }
  179. public bool HotKeyCommand { get; set; }
  180. }
  181. }