NavigationTests.cs 18 KB

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