NavigationTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. using Xunit.Abstractions;
  2. using static System.Net.Mime.MediaTypeNames;
  3. namespace Terminal.Gui.ViewTests;
  4. public class NavigationTests (ITestOutputHelper _output) : TestsAllViews
  5. {
  6. [Theory]
  7. [MemberData (nameof (AllViewTypes))]
  8. public void AllViews_AtLeastOneNavKey_Leaves (Type viewType)
  9. {
  10. View view = CreateInstanceIfNotGeneric (viewType);
  11. if (view == null)
  12. {
  13. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  14. return;
  15. }
  16. if (!view.CanFocus)
  17. {
  18. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  19. return;
  20. }
  21. Application.Init (new FakeDriver ());
  22. Toplevel top = new ();
  23. View otherView = new ()
  24. {
  25. Id = "otherView",
  26. CanFocus = true,
  27. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  28. };
  29. top.Add (view, otherView);
  30. Application.Begin (top);
  31. // Start with the focus on our test view
  32. view.SetFocus ();
  33. Key [] navKeys = { Key.Tab, Key.Tab.WithShift, Key.CursorUp, Key.CursorDown, Key.CursorLeft, Key.CursorRight };
  34. if (view.TabStop == TabBehavior.TabGroup)
  35. {
  36. navKeys = new [] { Key.F6, Key.F6.WithShift };
  37. }
  38. var left = false;
  39. foreach (Key key in navKeys)
  40. {
  41. switch (view.TabStop)
  42. {
  43. case TabBehavior.TabStop:
  44. case TabBehavior.NoStop:
  45. case TabBehavior.TabGroup:
  46. Application.OnKeyDown (key);
  47. break;
  48. default:
  49. Application.OnKeyDown (Key.Tab);
  50. break;
  51. }
  52. if (!view.HasFocus)
  53. {
  54. left = true;
  55. _output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
  56. view.SetFocus ();
  57. }
  58. else
  59. {
  60. _output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
  61. }
  62. }
  63. top.Dispose ();
  64. Application.Shutdown ();
  65. Assert.True (left);
  66. }
  67. [Theory]
  68. [MemberData (nameof (AllViewTypes))]
  69. public void AllViews_Enter_Leave_Events (Type viewType)
  70. {
  71. View view = CreateInstanceIfNotGeneric (viewType);
  72. if (view == null)
  73. {
  74. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  75. return;
  76. }
  77. if (!view.CanFocus)
  78. {
  79. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  80. return;
  81. }
  82. if (view is Toplevel && ((Toplevel)view).Modal)
  83. {
  84. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  85. return;
  86. }
  87. Application.Init (new FakeDriver ());
  88. Toplevel top = new ()
  89. {
  90. Height = 10,
  91. Width = 10
  92. };
  93. View otherView = new ()
  94. {
  95. Id = "otherView",
  96. X = 0, Y = 0,
  97. Height = 1,
  98. Width = 1,
  99. CanFocus = true,
  100. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  101. };
  102. view.X = Pos.Right (otherView);
  103. view.Y = 0;
  104. view.Width = 10;
  105. view.Height = 1;
  106. var nEnter = 0;
  107. var nLeave = 0;
  108. view.HasFocusChanging += (s, e) => nEnter++;
  109. view.HasFocusChanged += (s, e) => nLeave++;
  110. top.Add (view, otherView);
  111. Assert.False (view.HasFocus);
  112. Assert.False (otherView.HasFocus);
  113. Application.Begin (top);
  114. Assert.True (Application.Current!.HasFocus);
  115. Assert.True (top.HasFocus);
  116. // Start with the focus on our test view
  117. Assert.True (view.HasFocus);
  118. Assert.Equal (1, nEnter);
  119. Assert.Equal (0, nLeave);
  120. // Use keyboard to navigate to next view (otherView).
  121. var tries = 0;
  122. while (view.HasFocus)
  123. {
  124. if (++tries > 10)
  125. {
  126. Assert.Fail ($"{view} is not leaving.");
  127. }
  128. switch (view.TabStop)
  129. {
  130. case null:
  131. case TabBehavior.NoStop:
  132. case TabBehavior.TabStop:
  133. if (Application.OnKeyDown (Key.Tab))
  134. {
  135. if (view.HasFocus)
  136. {
  137. // Try another nav key (e.g. for TextView that eats Tab)
  138. Application.OnKeyDown (Key.CursorDown);
  139. }
  140. };
  141. break;
  142. case TabBehavior.TabGroup:
  143. Application.OnKeyDown (Key.F6);
  144. break;
  145. default:
  146. throw new ArgumentOutOfRangeException ();
  147. }
  148. }
  149. Assert.Equal (1, nEnter);
  150. Assert.Equal (1, nLeave);
  151. Assert.False (view.HasFocus);
  152. Assert.True (otherView.HasFocus);
  153. // Now navigate back to our test view
  154. switch (view.TabStop)
  155. {
  156. case TabBehavior.NoStop:
  157. view.SetFocus ();
  158. break;
  159. case TabBehavior.TabStop:
  160. Application.OnKeyDown (Key.Tab);
  161. break;
  162. case TabBehavior.TabGroup:
  163. Application.OnKeyDown (Key.F6);
  164. break;
  165. case null:
  166. Application.OnKeyDown (Key.Tab);
  167. break;
  168. default:
  169. throw new ArgumentOutOfRangeException ();
  170. }
  171. Assert.Equal (2, nEnter);
  172. Assert.Equal (1, nLeave);
  173. Assert.True (view.HasFocus);
  174. Assert.False (otherView.HasFocus);
  175. // Cache state because Shutdown has side effects.
  176. // Also ensures other tests can continue running if there's a fail
  177. bool otherViewHasFocus = otherView.HasFocus;
  178. bool viewHasFocus = view.HasFocus;
  179. int enterCount = nEnter;
  180. int leaveCount = nLeave;
  181. top.Dispose ();
  182. Application.Shutdown ();
  183. Assert.False (otherViewHasFocus);
  184. Assert.True (viewHasFocus);
  185. Assert.Equal (2, enterCount);
  186. Assert.Equal (1, leaveCount);
  187. }
  188. [Theory]
  189. [MemberData (nameof (AllViewTypes))]
  190. public void AllViews_Enter_Leave_Events_Visible_False (Type viewType)
  191. {
  192. View view = CreateInstanceIfNotGeneric (viewType);
  193. if (view == null)
  194. {
  195. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  196. return;
  197. }
  198. if (!view.CanFocus)
  199. {
  200. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  201. return;
  202. }
  203. if (view is Toplevel && ((Toplevel)view).Modal)
  204. {
  205. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  206. return;
  207. }
  208. Application.Init (new FakeDriver ());
  209. Toplevel top = new ()
  210. {
  211. Height = 10,
  212. Width = 10
  213. };
  214. View otherView = new ()
  215. {
  216. X = 0, Y = 0,
  217. Height = 1,
  218. Width = 1,
  219. CanFocus = true
  220. };
  221. view.Visible = false;
  222. view.X = Pos.Right (otherView);
  223. view.Y = 0;
  224. view.Width = 10;
  225. view.Height = 1;
  226. var nEnter = 0;
  227. var nLeave = 0;
  228. view.HasFocusChanging += (s, e) => nEnter++;
  229. view.HasFocusChanged += (s, e) => nLeave++;
  230. top.Add (view, otherView);
  231. Application.Begin (top);
  232. // Start with the focus on our test view
  233. view.SetFocus ();
  234. Assert.Equal (0, nEnter);
  235. Assert.Equal (0, nLeave);
  236. // Use keyboard to navigate to next view (otherView).
  237. if (view is TextView)
  238. {
  239. Application.OnKeyDown (Key.F6);
  240. }
  241. else if (view is DatePicker)
  242. {
  243. for (var i = 0; i < 4; i++)
  244. {
  245. Application.OnKeyDown (Key.F6);
  246. }
  247. }
  248. else
  249. {
  250. Application.OnKeyDown (Key.Tab);
  251. }
  252. Assert.Equal (0, nEnter);
  253. Assert.Equal (0, nLeave);
  254. top.NewKeyDownEvent (Key.Tab);
  255. Assert.Equal (0, nEnter);
  256. Assert.Equal (0, nLeave);
  257. top.Dispose ();
  258. Application.Shutdown ();
  259. }
  260. [Fact]
  261. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  262. {
  263. var r = new View ();
  264. var v1 = new View { CanFocus = true };
  265. var v2 = new View { CanFocus = true };
  266. var v3 = new View { CanFocus = true };
  267. r.Add (v1, v2, v3);
  268. r.BringSubviewForward (v1);
  269. Assert.True (r.Subviews.IndexOf (v1) == 1);
  270. Assert.True (r.Subviews.IndexOf (v2) == 0);
  271. Assert.True (r.Subviews.IndexOf (v3) == 2);
  272. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  273. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  274. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  275. r.Dispose ();
  276. }
  277. [Fact]
  278. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  279. {
  280. var r = new View ();
  281. var v1 = new View { CanFocus = true };
  282. var v2 = new View { CanFocus = true };
  283. var v3 = new View { CanFocus = true };
  284. r.Add (v1, v2, v3);
  285. r.BringSubviewToFront (v1);
  286. Assert.True (r.Subviews.IndexOf (v1) == 2);
  287. Assert.True (r.Subviews.IndexOf (v2) == 0);
  288. Assert.True (r.Subviews.IndexOf (v3) == 1);
  289. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  290. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  291. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  292. r.Dispose ();
  293. }
  294. // View.Focused & View.MostFocused tests
  295. // View.Focused - No subviews
  296. [Fact]
  297. [Trait ("BUGBUG", "Fix in Issue #3444")]
  298. public void Focused_NoSubviews ()
  299. {
  300. var view = new View ();
  301. Assert.Null (view.Focused);
  302. view.CanFocus = true;
  303. view.SetFocus ();
  304. Assert.True (view.HasFocus);
  305. Assert.Null (view.Focused); // BUGBUG: Should be view
  306. }
  307. [Fact]
  308. public void FocusNearestView_Ensure_Focus_Ordered ()
  309. {
  310. Application.Top = Application.Current = new Toplevel ();
  311. var win = new Window ();
  312. var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
  313. win.Add (winSubview);
  314. Application.Current.Add (win);
  315. var frm = new FrameView ();
  316. var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
  317. frm.Add (frmSubview);
  318. Application.Current.Add (frm);
  319. Application.Current.SetFocus ();
  320. Assert.Equal (winSubview, Application.Current.MostFocused);
  321. Application.OnKeyDown (Key.Tab); // Move to the next TabStop. There is none. So we should stay.
  322. Assert.Equal (winSubview, Application.Current.MostFocused);
  323. Application.OnKeyDown (Key.F6);
  324. Assert.Equal (frmSubview, Application.Current.MostFocused);
  325. Application.OnKeyDown (Key.Tab);
  326. Assert.Equal (frmSubview, Application.Current.MostFocused);
  327. Application.OnKeyDown (Key.F6);
  328. Assert.Equal (winSubview, Application.Current.MostFocused);
  329. Application.OnKeyDown (Key.F6.WithShift);
  330. Assert.Equal (frmSubview, Application.Current.MostFocused);
  331. Application.OnKeyDown (Key.F6.WithShift);
  332. Assert.Equal (winSubview, Application.Current.MostFocused);
  333. Application.Current.Dispose ();
  334. }
  335. [Fact]
  336. [AutoInitShutdown]
  337. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  338. {
  339. Toplevel top1 = new ();
  340. var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  341. var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
  342. var view2 = new View
  343. {
  344. Id = "view2",
  345. Y = 1,
  346. Width = 10,
  347. Height = 5,
  348. CanFocus = true
  349. };
  350. View view3 = null;
  351. var removed = false;
  352. view2.HasFocusChanging += (s, e) =>
  353. {
  354. if (!removed)
  355. {
  356. removed = true;
  357. view3 = new () { Id = "view3", Y = 1, Width = 10, Height = 5 };
  358. Application.Current.Add (view3);
  359. Application.Current.BringSubviewToFront (view3);
  360. Assert.False (view3.HasFocus);
  361. }
  362. };
  363. view2.HasFocusChanged += (s, e) =>
  364. {
  365. Application.Current.Remove (view3);
  366. view3.Dispose ();
  367. view3 = null;
  368. };
  369. top2.Add (view2);
  370. top1.Add (view1, top2);
  371. Application.Begin (top1);
  372. Assert.True (top1.HasFocus);
  373. Assert.True (view1.HasFocus);
  374. Assert.False (view2.HasFocus);
  375. Assert.False (removed);
  376. Assert.Null (view3);
  377. Assert.True (Application.OnKeyDown (Key.F6));
  378. Assert.True (top1.HasFocus);
  379. Assert.False (view1.HasFocus);
  380. Assert.True (view2.HasFocus);
  381. Assert.True (removed);
  382. Assert.NotNull (view3);
  383. Exception exception = Record.Exception (() => Application.OnKeyDown (Key.F6));
  384. Assert.Null (exception);
  385. Assert.True (removed);
  386. //Assert.Null (view3);
  387. top1.Dispose ();
  388. }
  389. [Fact]
  390. public void GetMostFocused_NoSubviews_Returns_Null ()
  391. {
  392. var view = new View ();
  393. Assert.Null (view.Focused);
  394. view.CanFocus = true;
  395. Assert.False (view.HasFocus);
  396. view.SetFocus ();
  397. Assert.True (view.HasFocus);
  398. Assert.Null (view.MostFocused);
  399. }
  400. [Fact]
  401. public void GetMostFocused_Returns_Most ()
  402. {
  403. var view = new View ()
  404. {
  405. Id = "view",
  406. CanFocus = true
  407. };
  408. var subview = new View ()
  409. {
  410. Id = "subview",
  411. CanFocus = true
  412. };
  413. view.Add (subview);
  414. view.SetFocus ();
  415. Assert.True (view.HasFocus);
  416. Assert.True (subview.HasFocus);
  417. Assert.Equal (subview, view.MostFocused);
  418. var subview2 = new View ()
  419. {
  420. Id = "subview2",
  421. CanFocus = true
  422. };
  423. view.Add (subview2);
  424. Assert.Equal (subview2, view.MostFocused);
  425. }
  426. // [Fact]
  427. // [AutoInitShutdown]
  428. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  429. // {
  430. // var sbQuiting = false;
  431. // var tfQuiting = false;
  432. // var topQuiting = false;
  433. // var sb = new StatusBar (
  434. // new Shortcut []
  435. // {
  436. // new (
  437. // KeyCode.CtrlMask | KeyCode.Q,
  438. // "Quit",
  439. // () => sbQuiting = true
  440. // )
  441. // }
  442. // );
  443. // var tf = new TextField ();
  444. // tf.KeyDown += Tf_KeyPressed;
  445. // void Tf_KeyPressed (object sender, Key obj)
  446. // {
  447. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  448. // {
  449. // obj.Handled = tfQuiting = true;
  450. // }
  451. // }
  452. // var win = new Window ();
  453. // win.Add (sb, tf);
  454. // Toplevel top = new ();
  455. // top.KeyDown += Top_KeyPress;
  456. // void Top_KeyPress (object sender, Key obj)
  457. // {
  458. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  459. // {
  460. // obj.Handled = topQuiting = true;
  461. // }
  462. // }
  463. // top.Add (win);
  464. // Application.Begin (top);
  465. // Assert.False (sbQuiting);
  466. // Assert.False (tfQuiting);
  467. // Assert.False (topQuiting);
  468. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  469. // Assert.False (sbQuiting);
  470. // Assert.True (tfQuiting);
  471. // Assert.False (topQuiting);
  472. //#if BROKE_WITH_2927
  473. // tf.KeyPressed -= Tf_KeyPress;
  474. // tfQuiting = false;
  475. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  476. // Application.MainLoop.RunIteration ();
  477. // Assert.True (sbQuiting);
  478. // Assert.False (tfQuiting);
  479. // Assert.False (topQuiting);
  480. // sb.RemoveItem (0);
  481. // sbQuiting = false;
  482. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  483. // Application.MainLoop.RunIteration ();
  484. // Assert.False (sbQuiting);
  485. // Assert.False (tfQuiting);
  486. //// This test is now invalid because `win` is focused, so it will receive the keypress
  487. // Assert.True (topQuiting);
  488. //#endif
  489. // top.Dispose ();
  490. // }
  491. // [Fact]
  492. // [AutoInitShutdown]
  493. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  494. // {
  495. // var sbQuiting = false;
  496. // var tfQuiting = false;
  497. // var sb = new StatusBar (
  498. // new Shortcut []
  499. // {
  500. // new (
  501. // KeyCode.CtrlMask | KeyCode.Q,
  502. // "~^Q~ Quit",
  503. // () => sbQuiting = true
  504. // )
  505. // }
  506. // );
  507. // var tf = new TextField ();
  508. // tf.KeyDown += Tf_KeyPressed;
  509. // void Tf_KeyPressed (object sender, Key obj)
  510. // {
  511. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  512. // {
  513. // obj.Handled = tfQuiting = true;
  514. // }
  515. // }
  516. // var win = new Window ();
  517. // win.Add (sb, tf);
  518. // Toplevel top = new ();
  519. // top.Add (win);
  520. // Application.Begin (top);
  521. // Assert.False (sbQuiting);
  522. // Assert.False (tfQuiting);
  523. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  524. // Assert.False (sbQuiting);
  525. // Assert.True (tfQuiting);
  526. // tf.KeyDown -= Tf_KeyPressed;
  527. // tfQuiting = false;
  528. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  529. // Application.MainLoop.RunIteration ();
  530. //#if BROKE_WITH_2927
  531. // Assert.True (sbQuiting);
  532. // Assert.False (tfQuiting);
  533. //#endif
  534. // top.Dispose ();
  535. // }
  536. [Fact]
  537. [SetupFakeDriver]
  538. public void Navigation_With_Null_Focused_View ()
  539. {
  540. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  541. Application.Init (new FakeDriver ());
  542. var top = new Toplevel ();
  543. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  544. // Keyboard navigation with tab
  545. FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
  546. Application.Iteration += (s, a) => Application.RequestStop ();
  547. Application.Run (top);
  548. top.Dispose ();
  549. Application.Shutdown ();
  550. }
  551. #if V2_NEW_FOCUS_IMPL // bogus test - Depends on auto setting of CanFocus
  552. [Fact]
  553. [AutoInitShutdown]
  554. public void Remove_Does_Not_Change_Focus ()
  555. {
  556. var top = new Toplevel ();
  557. Assert.True (top.CanFocus);
  558. Assert.False (top.HasFocus);
  559. var container = new View { Width = 10, Height = 10 };
  560. var leave = false;
  561. container.Leave += (s, e) => leave = true;
  562. Assert.False (container.CanFocus);
  563. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  564. container.Add (child);
  565. Assert.True (container.CanFocus);
  566. Assert.False (container.HasFocus);
  567. Assert.True (child.CanFocus);
  568. Assert.False (child.HasFocus);
  569. top.Add (container);
  570. Application.Begin (top);
  571. Assert.True (top.CanFocus);
  572. Assert.True (top.HasFocus);
  573. Assert.True (container.CanFocus);
  574. Assert.True (container.HasFocus);
  575. Assert.True (child.CanFocus);
  576. Assert.True (child.HasFocus);
  577. container.Remove (child);
  578. child.Dispose ();
  579. child = null;
  580. Assert.True (top.HasFocus);
  581. Assert.True (container.CanFocus);
  582. Assert.True (container.HasFocus);
  583. Assert.Null (child);
  584. Assert.False (leave);
  585. top.Dispose ();
  586. }
  587. #endif
  588. }