KeyboardTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ApplicationTests;
  3. /// <summary>
  4. /// Application tests for keyboard support.
  5. /// </summary>
  6. public class KeyboardTests
  7. {
  8. public KeyboardTests (ITestOutputHelper output)
  9. {
  10. _output = output;
  11. #if DEBUG_IDISPOSABLE
  12. View.Instances.Clear ();
  13. RunState.Instances.Clear ();
  14. #endif
  15. }
  16. private readonly ITestOutputHelper _output;
  17. private object _timeoutLock;
  18. [Fact (Skip = "No longer valid test.")]
  19. [AutoInitShutdown]
  20. public void EnsuresTopOnFront_CanFocus_False_By_Keyboard ()
  21. {
  22. Toplevel top = new ();
  23. var win = new Window
  24. {
  25. Title = "win",
  26. X = 0,
  27. Y = 0,
  28. Width = 20,
  29. Height = 10
  30. };
  31. var tf = new TextField { Width = 10 };
  32. win.Add (tf);
  33. var win2 = new Window
  34. {
  35. Title = "win2",
  36. X = 22,
  37. Y = 0,
  38. Width = 20,
  39. Height = 10
  40. };
  41. var tf2 = new TextField { Width = 10 };
  42. win2.Add (tf2);
  43. top.Add (win, win2);
  44. Application.Begin (top);
  45. Assert.True (win.CanFocus);
  46. Assert.True (win.HasFocus);
  47. Assert.True (win2.CanFocus);
  48. Assert.False (win2.HasFocus);
  49. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  50. win.CanFocus = false;
  51. Assert.False (win.CanFocus);
  52. Assert.False (win.HasFocus);
  53. Assert.True (win2.CanFocus);
  54. Assert.True (win2.HasFocus);
  55. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  56. Application.RaiseKeyDownEvent (Key.F6);
  57. Assert.True (win2.CanFocus);
  58. Assert.False (win.HasFocus);
  59. Assert.True (win2.CanFocus);
  60. Assert.True (win2.HasFocus);
  61. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  62. Application.RaiseKeyDownEvent (Key.F6);
  63. Assert.False (win.CanFocus);
  64. Assert.False (win.HasFocus);
  65. Assert.True (win2.CanFocus);
  66. Assert.True (win2.HasFocus);
  67. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  68. top.Dispose ();
  69. }
  70. [Fact (Skip = "No longer valid test.")]
  71. [AutoInitShutdown]
  72. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard ()
  73. {
  74. Toplevel top = new ();
  75. var win = new Window
  76. {
  77. Title = "win",
  78. X = 0,
  79. Y = 0,
  80. Width = 20,
  81. Height = 10
  82. };
  83. var tf = new TextField { Width = 10 };
  84. win.Add (tf);
  85. var win2 = new Window
  86. {
  87. Title = "win2",
  88. X = 22,
  89. Y = 0,
  90. Width = 20,
  91. Height = 10
  92. };
  93. var tf2 = new TextField { Width = 10 };
  94. win2.Add (tf2);
  95. top.Add (win, win2);
  96. Application.Begin (top);
  97. Assert.True (win.CanFocus);
  98. Assert.True (win.HasFocus);
  99. Assert.True (win2.CanFocus);
  100. Assert.False (win2.HasFocus);
  101. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  102. Application.RaiseKeyDownEvent (Key.F6);
  103. Assert.True (win.CanFocus);
  104. Assert.False (win.HasFocus);
  105. Assert.True (win2.CanFocus);
  106. Assert.True (win2.HasFocus);
  107. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  108. Application.RaiseKeyDownEvent (Key.F6);
  109. Assert.True (win.CanFocus);
  110. Assert.True (win.HasFocus);
  111. Assert.True (win2.CanFocus);
  112. Assert.False (win2.HasFocus);
  113. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  114. top.Dispose ();
  115. }
  116. [Fact]
  117. [AutoInitShutdown]
  118. public void KeyBindings_Add_Adds ()
  119. {
  120. Application.KeyBindings.Add (Key.A, Command.Accept);
  121. Application.KeyBindings.Add (Key.B, Command.Accept);
  122. Assert.True (Application.KeyBindings.TryGet (Key.A, out KeyBinding binding));
  123. Assert.Null (binding.Target);
  124. Assert.True (Application.KeyBindings.TryGet (Key.B, out binding));
  125. Assert.Null (binding.Target);
  126. }
  127. [Fact]
  128. [AutoInitShutdown]
  129. public void KeyBindings_Remove_Removes ()
  130. {
  131. Application.KeyBindings.Add (Key.A, Command.Accept);
  132. Assert.True (Application.KeyBindings.TryGet (Key.A, out _));
  133. Application.KeyBindings.Remove (Key.A);
  134. Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
  135. }
  136. [Fact]
  137. public void KeyBindings_OnKeyDown ()
  138. {
  139. Application.Top = new Toplevel ();
  140. var view = new ScopedKeyBindingView ();
  141. var keyWasHandled = false;
  142. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  143. Application.Top.Add (view);
  144. Application.RaiseKeyDownEvent (Key.A);
  145. Assert.False (keyWasHandled);
  146. Assert.True (view.ApplicationCommand);
  147. keyWasHandled = false;
  148. view.ApplicationCommand = false;
  149. Application.KeyBindings.Remove (KeyCode.A);
  150. Application.RaiseKeyDownEvent (Key.A); // old
  151. Assert.False (keyWasHandled);
  152. Assert.False (view.ApplicationCommand);
  153. Application.KeyBindings.Add (Key.A.WithCtrl, view, Command.Save);
  154. Application.RaiseKeyDownEvent (Key.A); // old
  155. Assert.False (keyWasHandled);
  156. Assert.False (view.ApplicationCommand);
  157. Application.RaiseKeyDownEvent (Key.A.WithCtrl); // new
  158. Assert.False (keyWasHandled);
  159. Assert.True (view.ApplicationCommand);
  160. keyWasHandled = false;
  161. Application.RaiseKeyDownEvent (Key.H);
  162. Assert.False (keyWasHandled);
  163. Assert.True (view.HotKeyCommand);
  164. keyWasHandled = false;
  165. Assert.False (view.HasFocus);
  166. Application.RaiseKeyDownEvent (Key.F);
  167. Assert.False (keyWasHandled);
  168. Assert.True (view.ApplicationCommand);
  169. Assert.True (view.HotKeyCommand);
  170. Assert.False (view.FocusedCommand);
  171. Application.Top.Dispose ();
  172. Application.ResetState (true);
  173. }
  174. [Fact]
  175. [AutoInitShutdown]
  176. public void KeyBindings_OnKeyDown_Negative ()
  177. {
  178. var view = new ScopedKeyBindingView ();
  179. var keyWasHandled = false;
  180. view.KeyDownNotHandled += (s, e) => keyWasHandled = true;
  181. var top = new Toplevel ();
  182. top.Add (view);
  183. Application.Begin (top);
  184. Application.RaiseKeyDownEvent (Key.A.WithCtrl);
  185. Assert.False (keyWasHandled);
  186. Assert.False (view.ApplicationCommand);
  187. Assert.False (view.HotKeyCommand);
  188. Assert.False (view.FocusedCommand);
  189. keyWasHandled = false;
  190. Assert.False (view.HasFocus);
  191. Application.RaiseKeyDownEvent (Key.Z);
  192. Assert.False (keyWasHandled);
  193. Assert.False (view.ApplicationCommand);
  194. Assert.False (view.HotKeyCommand);
  195. Assert.False (view.FocusedCommand);
  196. top.Dispose ();
  197. }
  198. [Fact]
  199. public void KeyUp_Event ()
  200. {
  201. Application.Init (new FakeDriver ());
  202. // Setup some fake keypresses (This)
  203. var input = "Tests";
  204. Key originalQuitKey = Application.QuitKey;
  205. Application.QuitKey = Key.Q.WithCtrl;
  206. // Put a control-q in at the end
  207. FakeConsole.MockKeyPresses.Push (new ('Q', ConsoleKey.Q, false, false, true));
  208. foreach (char c in input.Reverse ())
  209. {
  210. if (char.IsLetter (c))
  211. {
  212. FakeConsole.MockKeyPresses.Push (
  213. new (
  214. c,
  215. (ConsoleKey)char.ToUpper (c),
  216. char.IsUpper (c),
  217. false,
  218. false
  219. )
  220. );
  221. }
  222. else
  223. {
  224. FakeConsole.MockKeyPresses.Push (
  225. new (
  226. c,
  227. (ConsoleKey)c,
  228. false,
  229. false,
  230. false
  231. )
  232. );
  233. }
  234. }
  235. int stackSize = FakeConsole.MockKeyPresses.Count;
  236. var iterations = 0;
  237. Application.Iteration += (s, a) =>
  238. {
  239. iterations++;
  240. // Stop if we run out of control...
  241. if (iterations > 10)
  242. {
  243. Application.RequestStop ();
  244. }
  245. };
  246. var keyUps = 0;
  247. var output = string.Empty;
  248. var top = new Toplevel ();
  249. top.KeyUp += (sender, args) =>
  250. {
  251. if (args.KeyCode != (KeyCode.CtrlMask | KeyCode.Q))
  252. {
  253. output += args.AsRune;
  254. }
  255. keyUps++;
  256. };
  257. Application.Run (top);
  258. Application.QuitKey = originalQuitKey;
  259. // Input string should match output
  260. Assert.Equal (input, output);
  261. // # of key up events should match stack size
  262. //Assert.Equal (stackSize, keyUps);
  263. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  264. // it must be literal (Linux only).
  265. Assert.Equal (6, keyUps);
  266. // # of key up events should match # of iterations
  267. Assert.Equal (stackSize, iterations);
  268. top.Dispose ();
  269. Application.Shutdown ();
  270. Assert.Null (Application.Top);
  271. Assert.Null (Application.MainLoop);
  272. Assert.Null (Application.Driver);
  273. }
  274. [Fact]
  275. public void NextTabGroupKey_Moves_Focus_To_TabStop_In_Next_TabGroup ()
  276. {
  277. // Arrange
  278. Application.Navigation = new ();
  279. var top = new Toplevel ();
  280. var view1 = new View
  281. {
  282. Id = "view1",
  283. CanFocus = true,
  284. TabStop = TabBehavior.TabGroup
  285. };
  286. var subView1 = new View
  287. {
  288. Id = "subView1",
  289. CanFocus = true,
  290. TabStop = TabBehavior.TabStop
  291. };
  292. view1.Add (subView1);
  293. var view2 = new View
  294. {
  295. Id = "view2",
  296. CanFocus = true,
  297. TabStop = TabBehavior.TabGroup
  298. };
  299. var subView2 = new View
  300. {
  301. Id = "subView2",
  302. CanFocus = true,
  303. TabStop = TabBehavior.TabStop
  304. };
  305. view2.Add (subView2);
  306. top.Add (view1, view2);
  307. Application.Top = top;
  308. view1.SetFocus ();
  309. Assert.True (view1.HasFocus);
  310. Assert.True (subView1.HasFocus);
  311. // Act
  312. Application.RaiseKeyDownEvent (Application.NextTabGroupKey);
  313. // Assert
  314. Assert.True (view2.HasFocus);
  315. Assert.True (subView2.HasFocus);
  316. top.Dispose ();
  317. Application.Navigation = null;
  318. }
  319. [Fact]
  320. public void NextTabGroupKey_PrevTabGroupKey_Tests ()
  321. {
  322. Application.Init (new FakeDriver ());
  323. Toplevel top = new (); // TabGroup
  324. var w1 = new Window (); // TabGroup
  325. var v1 = new TextField (); // TabStop
  326. var v2 = new TextView (); // TabStop
  327. w1.Add (v1, v2);
  328. var w2 = new Window (); // TabGroup
  329. var v3 = new CheckBox (); // TabStop
  330. var v4 = new Button (); // TabStop
  331. w2.Add (v3, v4);
  332. top.Add (w1, w2);
  333. Application.Iteration += (s, a) =>
  334. {
  335. Assert.True (v1.HasFocus);
  336. // Across TabGroups
  337. Application.RaiseKeyDownEvent (Key.F6);
  338. Assert.True (v3.HasFocus);
  339. Application.RaiseKeyDownEvent (Key.F6);
  340. Assert.True (v1.HasFocus);
  341. Application.RaiseKeyDownEvent (Key.F6.WithShift);
  342. Assert.True (v3.HasFocus);
  343. Application.RaiseKeyDownEvent (Key.F6.WithShift);
  344. Assert.True (v1.HasFocus);
  345. // Restore?
  346. Application.RaiseKeyDownEvent (Key.Tab);
  347. Assert.True (v2.HasFocus);
  348. Application.RaiseKeyDownEvent (Key.F6);
  349. Assert.True (v3.HasFocus);
  350. Application.RaiseKeyDownEvent (Key.F6);
  351. Assert.True (v1.HasFocus);
  352. Application.RequestStop ();
  353. };
  354. Application.Run (top);
  355. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  356. Application.NextTabGroupKey = Key.PageDown.WithCtrl;
  357. Application.PrevTabGroupKey = Key.PageUp.WithCtrl;
  358. Application.QuitKey = Key.Q.WithCtrl;
  359. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, Application.NextTabGroupKey.KeyCode);
  360. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, Application.PrevTabGroupKey.KeyCode);
  361. Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
  362. top.Dispose ();
  363. // Shutdown must be called to safely clean up Application if Init has been called
  364. Application.Shutdown ();
  365. }
  366. [Fact]
  367. public void NextTabKey_Moves_Focus_To_Next_TabStop ()
  368. {
  369. // Arrange
  370. Application.Navigation = new ();
  371. var top = new Toplevel ();
  372. var view1 = new View { Id = "view1", CanFocus = true };
  373. var view2 = new View { Id = "view2", CanFocus = true };
  374. top.Add (view1, view2);
  375. Application.Top = top;
  376. view1.SetFocus ();
  377. // Act
  378. Application.RaiseKeyDownEvent (Application.NextTabKey);
  379. // Assert
  380. Assert.True (view2.HasFocus);
  381. top.Dispose ();
  382. Application.Navigation = null;
  383. }
  384. [Fact]
  385. public void PrevTabGroupKey_Moves_Focus_To_TabStop_In_Prev_TabGroup ()
  386. {
  387. // Arrange
  388. Application.Navigation = new ();
  389. var top = new Toplevel ();
  390. var view1 = new View
  391. {
  392. Id = "view1",
  393. CanFocus = true,
  394. TabStop = TabBehavior.TabGroup
  395. };
  396. var subView1 = new View
  397. {
  398. Id = "subView1",
  399. CanFocus = true,
  400. TabStop = TabBehavior.TabStop
  401. };
  402. view1.Add (subView1);
  403. var view2 = new View
  404. {
  405. Id = "view2",
  406. CanFocus = true,
  407. TabStop = TabBehavior.TabGroup
  408. };
  409. var subView2 = new View
  410. {
  411. Id = "subView2",
  412. CanFocus = true,
  413. TabStop = TabBehavior.TabStop
  414. };
  415. view2.Add (subView2);
  416. top.Add (view1, view2);
  417. Application.Top = top;
  418. view1.SetFocus ();
  419. Assert.True (view1.HasFocus);
  420. Assert.True (subView1.HasFocus);
  421. // Act
  422. Application.RaiseKeyDownEvent (Application.PrevTabGroupKey);
  423. // Assert
  424. Assert.True (view2.HasFocus);
  425. Assert.True (subView2.HasFocus);
  426. top.Dispose ();
  427. Application.Navigation = null;
  428. }
  429. [Fact]
  430. public void PrevTabKey_Moves_Focus_To_Prev_TabStop ()
  431. {
  432. // Arrange
  433. Application.Navigation = new ();
  434. var top = new Toplevel ();
  435. var view1 = new View { Id = "view1", CanFocus = true };
  436. var view2 = new View { Id = "view2", CanFocus = true };
  437. top.Add (view1, view2);
  438. Application.Top = top;
  439. view1.SetFocus ();
  440. // Act
  441. Application.RaiseKeyDownEvent (Application.NextTabKey);
  442. // Assert
  443. Assert.True (view2.HasFocus);
  444. top.Dispose ();
  445. Application.Navigation = null;
  446. }
  447. [Fact]
  448. public void QuitKey_Default_Is_Esc ()
  449. {
  450. Application.ResetState (true);
  451. // Before Init
  452. Assert.Equal (Key.Esc, Application.QuitKey);
  453. Application.Init (new FakeDriver ());
  454. // After Init
  455. Assert.Equal (Key.Esc, Application.QuitKey);
  456. Application.Shutdown ();
  457. }
  458. [Fact]
  459. [AutoInitShutdown]
  460. public void QuitKey_Getter_Setter ()
  461. {
  462. Toplevel top = new ();
  463. var isQuiting = false;
  464. top.Closing += (s, e) =>
  465. {
  466. isQuiting = true;
  467. e.Cancel = true;
  468. };
  469. Application.Begin (top);
  470. top.Running = true;
  471. Key prevKey = Application.QuitKey;
  472. Application.RaiseKeyDownEvent (Application.QuitKey);
  473. Assert.True (isQuiting);
  474. isQuiting = false;
  475. Application.RaiseKeyDownEvent (Application.QuitKey);
  476. Assert.True (isQuiting);
  477. isQuiting = false;
  478. Application.QuitKey = Key.C.WithCtrl;
  479. Application.RaiseKeyDownEvent (prevKey); // Should not quit
  480. Assert.False (isQuiting);
  481. Application.RaiseKeyDownEvent (Key.Q.WithCtrl); // Should not quit
  482. Assert.False (isQuiting);
  483. Application.RaiseKeyDownEvent (Application.QuitKey);
  484. Assert.True (isQuiting);
  485. // Reset the QuitKey to avoid throws errors on another tests
  486. Application.QuitKey = prevKey;
  487. top.Dispose ();
  488. }
  489. [Fact]
  490. public void QuitKey_Quits ()
  491. {
  492. Assert.Null (_timeoutLock);
  493. _timeoutLock = new ();
  494. uint abortTime = 500;
  495. var initialized = false;
  496. var iteration = 0;
  497. var shutdown = false;
  498. object timeout = null;
  499. Application.InitializedChanged += OnApplicationOnInitializedChanged;
  500. Application.Init (new FakeDriver ());
  501. Assert.True (initialized);
  502. Assert.False (shutdown);
  503. _output.WriteLine ("Application.Run<Toplevel> ().Dispose ()..");
  504. Application.Run<Toplevel> ().Dispose ();
  505. _output.WriteLine ("Back from Application.Run<Toplevel> ().Dispose ()");
  506. Assert.True (initialized);
  507. Assert.False (shutdown);
  508. Assert.Equal (1, iteration);
  509. Application.Shutdown ();
  510. Application.InitializedChanged -= OnApplicationOnInitializedChanged;
  511. lock (_timeoutLock)
  512. {
  513. if (timeout is { })
  514. {
  515. Application.RemoveTimeout (timeout);
  516. timeout = null;
  517. }
  518. }
  519. Assert.True (initialized);
  520. Assert.True (shutdown);
  521. #if DEBUG_IDISPOSABLE
  522. Assert.Empty (View.Instances);
  523. #endif
  524. lock (_timeoutLock)
  525. {
  526. _timeoutLock = null;
  527. }
  528. return;
  529. void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
  530. {
  531. _output.WriteLine ("OnApplicationOnInitializedChanged: {0}", a.CurrentValue);
  532. if (a.CurrentValue)
  533. {
  534. Application.Iteration += OnApplicationOnIteration;
  535. initialized = true;
  536. lock (_timeoutLock)
  537. {
  538. timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
  539. }
  540. }
  541. else
  542. {
  543. Application.Iteration -= OnApplicationOnIteration;
  544. shutdown = true;
  545. }
  546. }
  547. bool ForceCloseCallback ()
  548. {
  549. lock (_timeoutLock)
  550. {
  551. _output.WriteLine ($"ForceCloseCallback. iteration: {iteration}");
  552. if (timeout is { })
  553. {
  554. timeout = null;
  555. }
  556. }
  557. Application.ResetState (true);
  558. Assert.Fail ($"Failed to Quit with {Application.QuitKey} after {abortTime}ms. Force quit.");
  559. return false;
  560. }
  561. void OnApplicationOnIteration (object s, IterationEventArgs a)
  562. {
  563. _output.WriteLine ("Iteration: {0}", iteration);
  564. iteration++;
  565. Assert.True (iteration < 2, "Too many iterations, something is wrong.");
  566. if (Application.Initialized)
  567. {
  568. _output.WriteLine (" Pressing QuitKey");
  569. Application.RaiseKeyDownEvent (Application.QuitKey);
  570. }
  571. }
  572. }
  573. // Test View for testing Application key Bindings
  574. public class ScopedKeyBindingView : View
  575. {
  576. public ScopedKeyBindingView ()
  577. {
  578. AddCommand (Command.Save, () => ApplicationCommand = true);
  579. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  580. AddCommand (Command.Left, () => FocusedCommand = true);
  581. Application.KeyBindings.Add (Key.A, this, Command.Save);
  582. HotKey = KeyCode.H;
  583. KeyBindings.Add (Key.F, Command.Left);
  584. }
  585. public bool ApplicationCommand { get; set; }
  586. public bool FocusedCommand { get; set; }
  587. public bool HotKeyCommand { get; set; }
  588. }
  589. }