NavigationTests.cs 19 KB

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