HotKeyTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. public class HotKeyTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public HotKeyTests (ITestOutputHelper output) { _output = output; }
  8. [Theory]
  9. [InlineData (KeyCode.A)]
  10. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  11. [InlineData (KeyCode.D1)]
  12. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
  13. [InlineData ((KeyCode)'х')] // Cyrillic x
  14. [InlineData ((KeyCode)'你')] // Chinese ni
  15. public void AddKeyBindingsForHotKey_Sets (KeyCode key)
  16. {
  17. var view = new View ();
  18. view.HotKey = KeyCode.Z;
  19. Assert.Equal (string.Empty, view.Title);
  20. Assert.Equal (KeyCode.Z, view.HotKey);
  21. view.AddKeyBindingsForHotKey (KeyCode.Null, key);
  22. // Verify key bindings were set
  23. // As passed
  24. Command [] commands = view.KeyBindings.GetCommands (key);
  25. Assert.Contains (Command.HotKey, commands);
  26. commands = view.KeyBindings.GetCommands (key | KeyCode.AltMask);
  27. Assert.Contains (Command.HotKey, commands);
  28. KeyCode baseKey = key & ~KeyCode.ShiftMask;
  29. // If A...Z, with and without shift
  30. if (baseKey is >= KeyCode.A and <= KeyCode.Z)
  31. {
  32. commands = view.KeyBindings.GetCommands (key | KeyCode.ShiftMask);
  33. Assert.Contains (Command.HotKey, commands);
  34. commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask);
  35. Assert.Contains (Command.HotKey, commands);
  36. commands = view.KeyBindings.GetCommands (key | KeyCode.AltMask);
  37. Assert.Contains (Command.HotKey, commands);
  38. commands = view.KeyBindings.GetCommands ((key & ~KeyCode.ShiftMask) | KeyCode.AltMask);
  39. Assert.Contains (Command.HotKey, commands);
  40. }
  41. else
  42. {
  43. // Non A..Z keys should not have shift bindings
  44. if (key.HasFlag (KeyCode.ShiftMask))
  45. {
  46. commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask);
  47. Assert.Empty (commands);
  48. }
  49. else
  50. {
  51. commands = view.KeyBindings.GetCommands (key | KeyCode.ShiftMask);
  52. Assert.Empty (commands);
  53. }
  54. }
  55. }
  56. [Fact]
  57. public void Defaults ()
  58. {
  59. var view = new View ();
  60. Assert.Equal (string.Empty, view.Title);
  61. Assert.Equal (KeyCode.Null, view.HotKey);
  62. // Verify key bindings were set
  63. Command [] commands = view.KeyBindings.GetCommands (KeyCode.Null);
  64. Assert.Empty (commands);
  65. }
  66. [Theory]
  67. [InlineData (KeyCode.Null, true)] // non-shift
  68. [InlineData (KeyCode.ShiftMask, true)]
  69. [InlineData (KeyCode.AltMask, true)]
  70. [InlineData (KeyCode.ShiftMask | KeyCode.AltMask, true)]
  71. [InlineData (KeyCode.CtrlMask, false)]
  72. [InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask, false)]
  73. public void NewKeyDownEvent_Runs_Default_HotKey_Command (KeyCode mask, bool expected)
  74. {
  75. var view = new View { HotKeySpecifier = (Rune)'^', Title = "^Test" };
  76. view.CanFocus = true;
  77. Assert.False (view.HasFocus);
  78. view.NewKeyDownEvent (KeyCode.T | mask);
  79. Assert.Equal (expected, view.HasFocus);
  80. }
  81. [Fact]
  82. public void NewKeyDownEvent_Ignores_Focus_KeyBindings_SuperView ()
  83. {
  84. var view = new View ();
  85. view.KeyBindings.Add (Key.A, Command.HotKey); // implies KeyBindingScope.Focused - so this should not be invoked
  86. view.InvokingKeyBindings += (s, e) => { Assert.Fail (); };
  87. var superView = new View ();
  88. superView.Add (view);
  89. var ke = Key.A;
  90. superView.NewKeyDownEvent (ke);
  91. }
  92. [Fact]
  93. public void NewKeyDownEvent_Honors_HotKey_KeyBindings_SuperView ()
  94. {
  95. var view = new View ();
  96. view.KeyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey);
  97. bool invoked = false;
  98. view.InvokingKeyBindings += (s, e) => { invoked = true; };
  99. var superView = new View ();
  100. superView.Add (view);
  101. var ke = Key.A;
  102. superView.NewKeyDownEvent (ke);
  103. Assert.True (invoked);
  104. }
  105. [Fact]
  106. public void NewKeyDownEvent_InNewKeyDownEvent_Invokes_HotKey_Command_With_SuperView ()
  107. {
  108. var superView = new View ()
  109. {
  110. CanFocus = true
  111. };
  112. var view1 = new View
  113. {
  114. HotKeySpecifier = (Rune)'^',
  115. Title = "view^1",
  116. CanFocus = true
  117. };
  118. var view2 = new View
  119. {
  120. HotKeySpecifier = (Rune)'^',
  121. Title = "view^2",
  122. CanFocus = true
  123. };
  124. superView.Add (view1, view2);
  125. superView.SetFocus ();
  126. Assert.True (view1.HasFocus);
  127. var ke = Key.D2;
  128. superView.NewKeyDownEvent (ke);
  129. Assert.True (view2.HasFocus);
  130. }
  131. [Fact]
  132. public void Set_RemovesOldKeyBindings ()
  133. {
  134. var view = new View ();
  135. view.HotKey = KeyCode.A;
  136. Assert.Equal (string.Empty, view.Title);
  137. Assert.Equal (KeyCode.A, view.HotKey);
  138. // Verify key bindings were set
  139. Command [] commands = view.KeyBindings.GetCommands (KeyCode.A);
  140. Assert.Contains (Command.HotKey, commands);
  141. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
  142. Assert.Contains (Command.HotKey, commands);
  143. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
  144. Assert.Contains (Command.HotKey, commands);
  145. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
  146. Assert.Contains (Command.HotKey, commands);
  147. // Now set again
  148. view.HotKey = KeyCode.B;
  149. Assert.Equal (string.Empty, view.Title);
  150. Assert.Equal (KeyCode.B, view.HotKey);
  151. commands = view.KeyBindings.GetCommands (KeyCode.A);
  152. Assert.DoesNotContain (Command.HotKey, commands);
  153. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
  154. Assert.DoesNotContain (Command.HotKey, commands);
  155. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
  156. Assert.DoesNotContain (Command.HotKey, commands);
  157. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
  158. Assert.DoesNotContain (Command.HotKey, commands);
  159. }
  160. [Theory]
  161. [InlineData (KeyCode.A)]
  162. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  163. [InlineData (KeyCode.D1)]
  164. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)]
  165. [InlineData ((KeyCode)'!')]
  166. [InlineData ((KeyCode)'х')] // Cyrillic x
  167. [InlineData ((KeyCode)'你')] // Chinese ni
  168. [InlineData ((KeyCode)'ö')] // German o umlaut
  169. [InlineData (KeyCode.Null)]
  170. public void Set_Sets_WithValidKey (KeyCode key)
  171. {
  172. var view = new View ();
  173. view.HotKey = key;
  174. Assert.Equal (key, view.HotKey);
  175. }
  176. [Theory]
  177. [InlineData (KeyCode.A)]
  178. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  179. [InlineData (KeyCode.D1)]
  180. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
  181. [InlineData ((KeyCode)'х')] // Cyrillic x
  182. [InlineData ((KeyCode)'你')] // Chinese ni
  183. [InlineData ((KeyCode)'ö')] // German o umlaut
  184. public void Set_SetsKeyBindings (KeyCode key)
  185. {
  186. var view = new View ();
  187. view.HotKey = key;
  188. Assert.Equal (string.Empty, view.Title);
  189. Assert.Equal (key, view.HotKey);
  190. // Verify key bindings were set
  191. // As passed
  192. Command [] commands = view.KeyBindings.GetCommands (view.HotKey);
  193. Assert.Contains (Command.HotKey, commands);
  194. Key baseKey = view.HotKey.NoShift;
  195. // If A...Z, with and without shift
  196. if (baseKey.IsKeyCodeAtoZ)
  197. {
  198. commands = view.KeyBindings.GetCommands (view.HotKey.WithShift);
  199. Assert.Contains (Command.HotKey, commands);
  200. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift);
  201. Assert.Contains (Command.HotKey, commands);
  202. commands = view.KeyBindings.GetCommands (view.HotKey.WithAlt);
  203. Assert.Contains (Command.HotKey, commands);
  204. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift.WithAlt);
  205. Assert.Contains (Command.HotKey, commands);
  206. }
  207. else
  208. {
  209. // Non A..Z keys should not have shift bindings
  210. if (view.HotKey.IsShift)
  211. {
  212. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift);
  213. Assert.Empty (commands);
  214. }
  215. else
  216. {
  217. commands = view.KeyBindings.GetCommands (view.HotKey.WithShift);
  218. Assert.Empty (commands);
  219. }
  220. }
  221. }
  222. [Fact]
  223. public void Set_Throws_If_Modifiers_Are_Included ()
  224. {
  225. var view = new View ();
  226. // A..Z must be naked (Alt is assumed)
  227. view.HotKey = Key.A.WithAlt;
  228. Assert.Throws<ArgumentException> (() => view.HotKey = Key.A.WithCtrl);
  229. Assert.Throws<ArgumentException> (
  230. () =>
  231. view.HotKey =
  232. KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask
  233. );
  234. // All others must not have Ctrl (Alt is assumed)
  235. view.HotKey = Key.D1.WithAlt;
  236. Assert.Throws<ArgumentException> (() => view.HotKey = Key.D1.WithCtrl);
  237. Assert.Throws<ArgumentException> (
  238. () =>
  239. view.HotKey =
  240. KeyCode.D1 | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask
  241. );
  242. // Shift is ok (e.g. this is '!')
  243. view.HotKey = Key.D1.WithShift;
  244. }
  245. [Theory]
  246. [InlineData (KeyCode.Delete)]
  247. [InlineData (KeyCode.Backspace)]
  248. [InlineData (KeyCode.Tab)]
  249. [InlineData (KeyCode.Enter)]
  250. [InlineData (KeyCode.Esc)]
  251. [InlineData (KeyCode.Space)]
  252. [InlineData (KeyCode.CursorLeft)]
  253. [InlineData (KeyCode.F1)]
  254. [InlineData (KeyCode.Null | KeyCode.ShiftMask)]
  255. public void Set_Throws_With_Invalid_Key (KeyCode key)
  256. {
  257. var view = new View ();
  258. Assert.Throws<ArgumentException> (() => view.HotKey = key);
  259. }
  260. [Theory]
  261. [InlineData ("Test", KeyCode.Null)]
  262. [InlineData ("^Test", KeyCode.T)]
  263. [InlineData ("T^est", KeyCode.E)]
  264. [InlineData ("Te^st", KeyCode.S)]
  265. [InlineData ("Tes^t", KeyCode.T)]
  266. [InlineData ("other", KeyCode.Null)]
  267. [InlineData ("oTher", KeyCode.Null)]
  268. [InlineData ("^Öther", (KeyCode)'Ö')]
  269. [InlineData ("^öther", (KeyCode)'ö')]
  270. // BUGBUG: '!' should be supported. Line 968 of TextFormatter filters on char.IsLetterOrDigit
  271. //[InlineData ("Test^!", (Key)'!')]
  272. public void Title_Change_Sets_HotKey (string title, KeyCode expectedHotKey)
  273. {
  274. var view = new View { HotKeySpecifier = new Rune ('^'), Title = "^Hello" };
  275. Assert.Equal (KeyCode.H, view.HotKey);
  276. view.Title = title;
  277. Assert.Equal (expectedHotKey, view.HotKey);
  278. }
  279. [Theory]
  280. [InlineData ("^Test")]
  281. public void Title_Empty_Sets_HotKey_To_Null (string title)
  282. {
  283. var view = new View { HotKeySpecifier = (Rune)'^', Title = title };
  284. Assert.Equal (title, view.Title);
  285. Assert.Equal (KeyCode.T, view.HotKey);
  286. view.Title = string.Empty;
  287. Assert.Equal ("", view.Title);
  288. Assert.Equal (KeyCode.Null, view.HotKey);
  289. }
  290. [Fact]
  291. public void HotKey_Raises_HotKeyCommand ()
  292. {
  293. var hotKeyRaised = false;
  294. var acceptRaised = false;
  295. var selectRaised = false;
  296. Application.Top = new Toplevel ();
  297. var view = new View
  298. {
  299. CanFocus = true,
  300. HotKeySpecifier = new Rune ('_'),
  301. Title = "_Test"
  302. };
  303. Application.Top.Add (view);
  304. view.HandlingHotKey += (s, e) => hotKeyRaised = true;
  305. view.Accepting += (s, e) => acceptRaised = true;
  306. view.Selecting += (s, e) => selectRaised = true;
  307. Assert.Equal (KeyCode.T, view.HotKey);
  308. Assert.True (Application.OnKeyDown (Key.T));
  309. Assert.True (hotKeyRaised);
  310. Assert.False (acceptRaised);
  311. Assert.False (selectRaised);
  312. hotKeyRaised = false;
  313. Assert.True (Application.OnKeyDown (Key.T.WithAlt));
  314. Assert.True (hotKeyRaised);
  315. Assert.False (acceptRaised);
  316. Assert.False (selectRaised);
  317. hotKeyRaised = false;
  318. view.HotKey = KeyCode.E;
  319. Assert.True (Application.OnKeyDown (Key.E.WithAlt));
  320. Assert.True (hotKeyRaised);
  321. Assert.False (acceptRaised);
  322. Assert.False (selectRaised);
  323. Application.Top.Dispose ();
  324. Application.ResetState (true);
  325. }
  326. }