KeyboardTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ApplicationTests;
  4. /// <summary>
  5. /// Application tests for keyboard support.
  6. /// </summary>
  7. public class KeyboardTests
  8. {
  9. public KeyboardTests (ITestOutputHelper output)
  10. {
  11. _output = output;
  12. #if DEBUG_IDISPOSABLE
  13. View.Instances.Clear ();
  14. RunState.Instances.Clear ();
  15. #endif
  16. }
  17. private readonly ITestOutputHelper _output;
  18. private object _timeoutLock;
  19. [Fact]
  20. [AutoInitShutdown]
  21. public void KeyBindings_Add_Adds ()
  22. {
  23. Application.KeyBindings.Add (Key.A, Command.Accept);
  24. Application.KeyBindings.Add (Key.B, Command.Accept);
  25. Assert.True (Application.KeyBindings.TryGet (Key.A, out KeyBinding binding));
  26. Assert.Null (binding.Target);
  27. Assert.True (Application.KeyBindings.TryGet (Key.B, out binding));
  28. Assert.Null (binding.Target);
  29. }
  30. [Fact]
  31. [AutoInitShutdown]
  32. public void KeyBindings_Remove_Removes ()
  33. {
  34. Application.KeyBindings.Add (Key.A, Command.Accept);
  35. Assert.True (Application.KeyBindings.TryGet (Key.A, out _));
  36. Application.KeyBindings.Remove (Key.A);
  37. Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
  38. }
  39. [Fact]
  40. public void KeyBindings_OnKeyDown ()
  41. {
  42. Application.Top = new ();
  43. var view = new ScopedKeyBindingView ();
  44. var keyWasHandled = false;
  45. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  46. Application.Top.Add (view);
  47. Application.RaiseKeyDownEvent (Key.A);
  48. Assert.False (keyWasHandled);
  49. Assert.True (view.ApplicationCommand);
  50. keyWasHandled = false;
  51. view.ApplicationCommand = false;
  52. Application.KeyBindings.Remove (KeyCode.A);
  53. Application.RaiseKeyDownEvent (Key.A); // old
  54. Assert.False (keyWasHandled);
  55. Assert.False (view.ApplicationCommand);
  56. Application.KeyBindings.Add (Key.A.WithCtrl, view, Command.Save);
  57. Application.RaiseKeyDownEvent (Key.A); // old
  58. Assert.False (keyWasHandled);
  59. Assert.False (view.ApplicationCommand);
  60. Application.RaiseKeyDownEvent (Key.A.WithCtrl); // new
  61. Assert.False (keyWasHandled);
  62. Assert.True (view.ApplicationCommand);
  63. keyWasHandled = false;
  64. Application.RaiseKeyDownEvent (Key.H);
  65. Assert.False (keyWasHandled);
  66. Assert.True (view.HotKeyCommand);
  67. keyWasHandled = false;
  68. Assert.False (view.HasFocus);
  69. Application.RaiseKeyDownEvent (Key.F);
  70. Assert.False (keyWasHandled);
  71. Assert.True (view.ApplicationCommand);
  72. Assert.True (view.HotKeyCommand);
  73. Assert.False (view.FocusedCommand);
  74. Application.Top.Dispose ();
  75. Application.ResetState (true);
  76. }
  77. [Fact]
  78. [AutoInitShutdown]
  79. public void KeyBindings_OnKeyDown_Negative ()
  80. {
  81. var view = new ScopedKeyBindingView ();
  82. var keyWasHandled = false;
  83. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  84. var top = new Toplevel ();
  85. top.Add (view);
  86. Application.Begin (top);
  87. Application.RaiseKeyDownEvent (Key.A.WithCtrl);
  88. Assert.False (keyWasHandled);
  89. Assert.False (view.ApplicationCommand);
  90. Assert.False (view.HotKeyCommand);
  91. Assert.False (view.FocusedCommand);
  92. keyWasHandled = false;
  93. Assert.False (view.HasFocus);
  94. Application.RaiseKeyDownEvent (Key.Z);
  95. Assert.False (keyWasHandled);
  96. Assert.False (view.ApplicationCommand);
  97. Assert.False (view.HotKeyCommand);
  98. Assert.False (view.FocusedCommand);
  99. top.Dispose ();
  100. }
  101. [Fact]
  102. public void NextTabGroupKey_Moves_Focus_To_TabStop_In_Next_TabGroup ()
  103. {
  104. // Arrange
  105. Application.Navigation = new ();
  106. var top = new Toplevel ();
  107. var view1 = new View
  108. {
  109. Id = "view1",
  110. CanFocus = true,
  111. TabStop = TabBehavior.TabGroup
  112. };
  113. var subView1 = new View
  114. {
  115. Id = "subView1",
  116. CanFocus = true,
  117. TabStop = TabBehavior.TabStop
  118. };
  119. view1.Add (subView1);
  120. var view2 = new View
  121. {
  122. Id = "view2",
  123. CanFocus = true,
  124. TabStop = TabBehavior.TabGroup
  125. };
  126. var subView2 = new View
  127. {
  128. Id = "subView2",
  129. CanFocus = true,
  130. TabStop = TabBehavior.TabStop
  131. };
  132. view2.Add (subView2);
  133. top.Add (view1, view2);
  134. Application.Top = top;
  135. view1.SetFocus ();
  136. Assert.True (view1.HasFocus);
  137. Assert.True (subView1.HasFocus);
  138. // Act
  139. Application.RaiseKeyDownEvent (Application.NextTabGroupKey);
  140. // Assert
  141. Assert.True (view2.HasFocus);
  142. Assert.True (subView2.HasFocus);
  143. top.Dispose ();
  144. Application.Navigation = null;
  145. }
  146. [Fact]
  147. public void NextTabGroupKey_PrevTabGroupKey_Tests ()
  148. {
  149. Application.Init (new FakeDriver ());
  150. Toplevel top = new (); // TabGroup
  151. var w1 = new Window (); // TabGroup
  152. var v1 = new TextField (); // TabStop
  153. var v2 = new TextView (); // TabStop
  154. w1.Add (v1, v2);
  155. var w2 = new Window (); // TabGroup
  156. var v3 = new CheckBox (); // TabStop
  157. var v4 = new Button (); // TabStop
  158. w2.Add (v3, v4);
  159. top.Add (w1, w2);
  160. Application.Iteration += (s, a) =>
  161. {
  162. Assert.True (v1.HasFocus);
  163. // Across TabGroups
  164. Application.RaiseKeyDownEvent (Key.F6);
  165. Assert.True (v3.HasFocus);
  166. Application.RaiseKeyDownEvent (Key.F6);
  167. Assert.True (v1.HasFocus);
  168. Application.RaiseKeyDownEvent (Key.F6.WithShift);
  169. Assert.True (v3.HasFocus);
  170. Application.RaiseKeyDownEvent (Key.F6.WithShift);
  171. Assert.True (v1.HasFocus);
  172. // Restore?
  173. Application.RaiseKeyDownEvent (Key.Tab);
  174. Assert.True (v2.HasFocus);
  175. Application.RaiseKeyDownEvent (Key.F6);
  176. Assert.True (v3.HasFocus);
  177. Application.RaiseKeyDownEvent (Key.F6);
  178. Assert.True (v1.HasFocus);
  179. Application.RequestStop ();
  180. };
  181. Application.Run (top);
  182. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  183. Application.NextTabGroupKey = Key.PageDown.WithCtrl;
  184. Application.PrevTabGroupKey = Key.PageUp.WithCtrl;
  185. Application.QuitKey = Key.Q.WithCtrl;
  186. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.NextTabGroupKey.KeyCode);
  187. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.PrevTabGroupKey.KeyCode);
  188. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  189. top.Dispose ();
  190. // Shutdown must be called to safely clean up Application if Init has been called
  191. Application.Shutdown ();
  192. }
  193. [Fact]
  194. public void NextTabKey_Moves_Focus_To_Next_TabStop ()
  195. {
  196. // Arrange
  197. Application.Navigation = new ();
  198. var top = new Toplevel ();
  199. var view1 = new View { Id = "view1", CanFocus = true };
  200. var view2 = new View { Id = "view2", CanFocus = true };
  201. top.Add (view1, view2);
  202. Application.Top = top;
  203. view1.SetFocus ();
  204. // Act
  205. Application.RaiseKeyDownEvent (Application.NextTabKey);
  206. // Assert
  207. Assert.True (view2.HasFocus);
  208. top.Dispose ();
  209. Application.Navigation = null;
  210. }
  211. [Fact]
  212. public void PrevTabGroupKey_Moves_Focus_To_TabStop_In_Prev_TabGroup ()
  213. {
  214. // Arrange
  215. Application.Navigation = new ();
  216. var top = new Toplevel ();
  217. var view1 = new View
  218. {
  219. Id = "view1",
  220. CanFocus = true,
  221. TabStop = TabBehavior.TabGroup
  222. };
  223. var subView1 = new View
  224. {
  225. Id = "subView1",
  226. CanFocus = true,
  227. TabStop = TabBehavior.TabStop
  228. };
  229. view1.Add (subView1);
  230. var view2 = new View
  231. {
  232. Id = "view2",
  233. CanFocus = true,
  234. TabStop = TabBehavior.TabGroup
  235. };
  236. var subView2 = new View
  237. {
  238. Id = "subView2",
  239. CanFocus = true,
  240. TabStop = TabBehavior.TabStop
  241. };
  242. view2.Add (subView2);
  243. top.Add (view1, view2);
  244. Application.Top = top;
  245. view1.SetFocus ();
  246. Assert.True (view1.HasFocus);
  247. Assert.True (subView1.HasFocus);
  248. // Act
  249. Application.RaiseKeyDownEvent (Application.PrevTabGroupKey);
  250. // Assert
  251. Assert.True (view2.HasFocus);
  252. Assert.True (subView2.HasFocus);
  253. top.Dispose ();
  254. Application.Navigation = null;
  255. }
  256. [Fact]
  257. public void PrevTabKey_Moves_Focus_To_Prev_TabStop ()
  258. {
  259. // Arrange
  260. Application.Navigation = new ();
  261. var top = new Toplevel ();
  262. var view1 = new View { Id = "view1", CanFocus = true };
  263. var view2 = new View { Id = "view2", CanFocus = true };
  264. top.Add (view1, view2);
  265. Application.Top = top;
  266. view1.SetFocus ();
  267. // Act
  268. Application.RaiseKeyDownEvent (Application.NextTabKey);
  269. // Assert
  270. Assert.True (view2.HasFocus);
  271. top.Dispose ();
  272. Application.Navigation = null;
  273. }
  274. [Fact]
  275. public void QuitKey_Default_Is_Esc ()
  276. {
  277. Application.ResetState (true);
  278. // Before Init
  279. Assert.Equal (Key.Esc, Application.QuitKey);
  280. Application.Init (new FakeDriver ());
  281. // After Init
  282. Assert.Equal (Key.Esc, Application.QuitKey);
  283. Application.Shutdown ();
  284. }
  285. [Fact]
  286. [AutoInitShutdown]
  287. public void QuitKey_Getter_Setter ()
  288. {
  289. Toplevel top = new ();
  290. var isQuiting = false;
  291. top.Closing += (s, e) =>
  292. {
  293. isQuiting = true;
  294. e.Cancel = true;
  295. };
  296. Application.Begin (top);
  297. top.Running = true;
  298. Key prevKey = Application.QuitKey;
  299. Application.RaiseKeyDownEvent (Application.QuitKey);
  300. Assert.True (isQuiting);
  301. isQuiting = false;
  302. Application.RaiseKeyDownEvent (Application.QuitKey);
  303. Assert.True (isQuiting);
  304. isQuiting = false;
  305. Application.QuitKey = Key.C.WithCtrl;
  306. Application.RaiseKeyDownEvent (prevKey); // Should not quit
  307. Assert.False (isQuiting);
  308. Application.RaiseKeyDownEvent (Key.Q.WithCtrl); // Should not quit
  309. Assert.False (isQuiting);
  310. Application.RaiseKeyDownEvent (Application.QuitKey);
  311. Assert.True (isQuiting);
  312. // Reset the QuitKey to avoid throws errors on another tests
  313. Application.QuitKey = prevKey;
  314. top.Dispose ();
  315. }
  316. [Fact]
  317. public void QuitKey_Quits ()
  318. {
  319. Assert.Null (_timeoutLock);
  320. _timeoutLock = new ();
  321. uint abortTime = 500;
  322. var initialized = false;
  323. var iteration = 0;
  324. var shutdown = false;
  325. object timeout = null;
  326. Application.InitializedChanged += OnApplicationOnInitializedChanged;
  327. Application.Init (new FakeDriver ());
  328. Assert.True (initialized);
  329. Assert.False (shutdown);
  330. _output.WriteLine ("Application.Run<Toplevel> ().Dispose ()..");
  331. Application.Run<Toplevel> ().Dispose ();
  332. _output.WriteLine ("Back from Application.Run<Toplevel> ().Dispose ()");
  333. Assert.True (initialized);
  334. Assert.False (shutdown);
  335. Assert.Equal (1, iteration);
  336. Application.Shutdown ();
  337. Application.InitializedChanged -= OnApplicationOnInitializedChanged;
  338. lock (_timeoutLock)
  339. {
  340. if (timeout is { })
  341. {
  342. Application.RemoveTimeout (timeout);
  343. timeout = null;
  344. }
  345. }
  346. Assert.True (initialized);
  347. Assert.True (shutdown);
  348. #if DEBUG_IDISPOSABLE
  349. Assert.Empty (View.Instances);
  350. #endif
  351. lock (_timeoutLock)
  352. {
  353. _timeoutLock = null;
  354. }
  355. return;
  356. void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
  357. {
  358. _output.WriteLine ("OnApplicationOnInitializedChanged: {0}", a.CurrentValue);
  359. if (a.CurrentValue)
  360. {
  361. Application.Iteration += OnApplicationOnIteration;
  362. initialized = true;
  363. lock (_timeoutLock)
  364. {
  365. timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  366. }
  367. }
  368. else
  369. {
  370. Application.Iteration -= OnApplicationOnIteration;
  371. shutdown = true;
  372. }
  373. }
  374. bool ForceCloseCallback ()
  375. {
  376. lock (_timeoutLock)
  377. {
  378. _output.WriteLine ($"ForceCloseCallback. iteration: {iteration}");
  379. if (timeout is { })
  380. {
  381. timeout = null;
  382. }
  383. }
  384. Application.ResetState (true);
  385. Assert.Fail ($"Failed to Quit with {Application.QuitKey} after {abortTime}ms. Force quit.");
  386. return false;
  387. }
  388. void OnApplicationOnIteration (object s, IterationEventArgs a)
  389. {
  390. _output.WriteLine ("Iteration: {0}", iteration);
  391. iteration++;
  392. Assert.True (iteration < 2, "Too many iterations, something is wrong.");
  393. if (Application.Initialized)
  394. {
  395. _output.WriteLine (" Pressing QuitKey");
  396. Application.RaiseKeyDownEvent (Application.QuitKey);
  397. }
  398. }
  399. }
  400. // Test View for testing Application key Bindings
  401. public class ScopedKeyBindingView : View
  402. {
  403. public ScopedKeyBindingView ()
  404. {
  405. AddCommand (Command.Save, () => ApplicationCommand = true);
  406. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  407. AddCommand (Command.Left, () => FocusedCommand = true);
  408. Application.KeyBindings.Add (Key.A, this, Command.Save);
  409. HotKey = KeyCode.H;
  410. KeyBindings.Add (Key.F, Command.Left);
  411. }
  412. public bool ApplicationCommand { get; set; }
  413. public bool FocusedCommand { get; set; }
  414. public bool HotKeyCommand { get; set; }
  415. }
  416. }