NavigationTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. if (!Application.OnKeyDown (Key.F6))
  165. {
  166. view.SetFocus ();
  167. }
  168. break;
  169. case null:
  170. Application.OnKeyDown (Key.Tab);
  171. break;
  172. default:
  173. throw new ArgumentOutOfRangeException ();
  174. }
  175. Assert.Equal (2, hasFocusTrue);
  176. Assert.Equal (1, hasFocusFalse);
  177. Assert.True (view.HasFocus);
  178. Assert.False (otherView.HasFocus);
  179. // Cache state because Shutdown has side effects.
  180. // Also ensures other tests can continue running if there's a fail
  181. bool otherViewHasFocus = otherView.HasFocus;
  182. bool viewHasFocus = view.HasFocus;
  183. int enterCount = hasFocusTrue;
  184. int leaveCount = hasFocusFalse;
  185. top.Dispose ();
  186. Assert.False (otherViewHasFocus);
  187. Assert.True (viewHasFocus);
  188. Assert.Equal (2, enterCount);
  189. Assert.Equal (1, leaveCount);
  190. Application.ResetState ();
  191. }
  192. [Theory]
  193. [MemberData (nameof (AllViewTypes))]
  194. [SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
  195. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  196. {
  197. View view = CreateInstanceIfNotGeneric (viewType);
  198. if (view == null)
  199. {
  200. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  201. return;
  202. }
  203. if (!view.CanFocus)
  204. {
  205. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  206. return;
  207. }
  208. if (view is Toplevel && ((Toplevel)view).Modal)
  209. {
  210. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  211. return;
  212. }
  213. Toplevel top = new ();
  214. Application.Current = top;
  215. Application.Navigation = new ApplicationNavigation ();
  216. View otherView = new ()
  217. {
  218. CanFocus = true
  219. };
  220. view.Visible = false;
  221. var hasFocusChangingCount = 0;
  222. var hasFocusChangedCount = 0;
  223. view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
  224. view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
  225. top.Add (view, otherView);
  226. // Start with the focus on our test view
  227. view.SetFocus ();
  228. Assert.Equal (0, hasFocusChangingCount);
  229. Assert.Equal (0, hasFocusChangedCount);
  230. Application.OnKeyDown (Key.Tab);
  231. Assert.Equal (0, hasFocusChangingCount);
  232. Assert.Equal (0, hasFocusChangedCount);
  233. Application.OnKeyDown (Key.F6);
  234. Assert.Equal (0, hasFocusChangingCount);
  235. Assert.Equal (0, hasFocusChangedCount);
  236. top.Dispose ();
  237. Application.ResetState ();
  238. }
  239. // View.Focused & View.MostFocused tests
  240. // View.Focused - No subviews
  241. [Fact]
  242. public void Focused_NoSubviews ()
  243. {
  244. var view = new View ();
  245. Assert.Null (view.Focused);
  246. view.CanFocus = true;
  247. view.SetFocus ();
  248. }
  249. [Fact]
  250. public void GetMostFocused_NoSubviews_Returns_Null ()
  251. {
  252. var view = new View ();
  253. Assert.Null (view.Focused);
  254. view.CanFocus = true;
  255. Assert.False (view.HasFocus);
  256. view.SetFocus ();
  257. Assert.True (view.HasFocus);
  258. Assert.Null (view.MostFocused);
  259. }
  260. [Fact]
  261. public void GetMostFocused_Returns_Most ()
  262. {
  263. var view = new View ()
  264. {
  265. Id = "view",
  266. CanFocus = true
  267. };
  268. var subview = new View ()
  269. {
  270. Id = "subview",
  271. CanFocus = true
  272. };
  273. view.Add (subview);
  274. view.SetFocus ();
  275. Assert.True (view.HasFocus);
  276. Assert.True (subview.HasFocus);
  277. Assert.Equal (subview, view.MostFocused);
  278. var subview2 = new View ()
  279. {
  280. Id = "subview2",
  281. CanFocus = true
  282. };
  283. view.Add (subview2);
  284. Assert.Equal (subview2, view.MostFocused);
  285. }
  286. // [Fact]
  287. // [AutoInitShutdown]
  288. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  289. // {
  290. // var sbQuiting = false;
  291. // var tfQuiting = false;
  292. // var topQuiting = false;
  293. // var sb = new StatusBar (
  294. // new Shortcut []
  295. // {
  296. // new (
  297. // KeyCode.CtrlMask | KeyCode.Q,
  298. // "Quit",
  299. // () => sbQuiting = true
  300. // )
  301. // }
  302. // );
  303. // var tf = new TextField ();
  304. // tf.KeyDown += Tf_KeyPressed;
  305. // void Tf_KeyPressed (object sender, Key obj)
  306. // {
  307. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  308. // {
  309. // obj.Handled = tfQuiting = true;
  310. // }
  311. // }
  312. // var win = new Window ();
  313. // win.Add (sb, tf);
  314. // Toplevel top = new ();
  315. // top.KeyDown += Top_KeyPress;
  316. // void Top_KeyPress (object sender, Key obj)
  317. // {
  318. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  319. // {
  320. // obj.Handled = topQuiting = true;
  321. // }
  322. // }
  323. // top.Add (win);
  324. // Application.Begin (top);
  325. // Assert.False (sbQuiting);
  326. // Assert.False (tfQuiting);
  327. // Assert.False (topQuiting);
  328. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  329. // Assert.False (sbQuiting);
  330. // Assert.True (tfQuiting);
  331. // Assert.False (topQuiting);
  332. //#if BROKE_WITH_2927
  333. // tf.KeyPressed -= Tf_KeyPress;
  334. // tfQuiting = false;
  335. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  336. // Application.MainLoop.RunIteration ();
  337. // Assert.True (sbQuiting);
  338. // Assert.False (tfQuiting);
  339. // Assert.False (topQuiting);
  340. // sb.RemoveItem (0);
  341. // sbQuiting = false;
  342. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  343. // Application.MainLoop.RunIteration ();
  344. // Assert.False (sbQuiting);
  345. // Assert.False (tfQuiting);
  346. //// This test is now invalid because `win` is focused, so it will receive the keypress
  347. // Assert.True (topQuiting);
  348. //#endif
  349. // top.Dispose ();
  350. // }
  351. // [Fact]
  352. // [AutoInitShutdown]
  353. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  354. // {
  355. // var sbQuiting = false;
  356. // var tfQuiting = false;
  357. // var sb = new StatusBar (
  358. // new Shortcut []
  359. // {
  360. // new (
  361. // KeyCode.CtrlMask | KeyCode.Q,
  362. // "~^Q~ Quit",
  363. // () => sbQuiting = true
  364. // )
  365. // }
  366. // );
  367. // var tf = new TextField ();
  368. // tf.KeyDown += Tf_KeyPressed;
  369. // void Tf_KeyPressed (object sender, Key obj)
  370. // {
  371. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  372. // {
  373. // obj.Handled = tfQuiting = true;
  374. // }
  375. // }
  376. // var win = new Window ();
  377. // win.Add (sb, tf);
  378. // Toplevel top = new ();
  379. // top.Add (win);
  380. // Application.Begin (top);
  381. // Assert.False (sbQuiting);
  382. // Assert.False (tfQuiting);
  383. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  384. // Assert.False (sbQuiting);
  385. // Assert.True (tfQuiting);
  386. // tf.KeyDown -= Tf_KeyPressed;
  387. // tfQuiting = false;
  388. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  389. // Application.MainLoop.RunIteration ();
  390. //#if BROKE_WITH_2927
  391. // Assert.True (sbQuiting);
  392. // Assert.False (tfQuiting);
  393. //#endif
  394. // top.Dispose ();
  395. // }
  396. [Fact]
  397. [SetupFakeDriver]
  398. public void Navigation_With_Null_Focused_View ()
  399. {
  400. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  401. Application.Init (new FakeDriver ());
  402. var top = new Toplevel ();
  403. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  404. // Keyboard navigation with tab
  405. FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
  406. Application.Iteration += (s, a) => Application.RequestStop ();
  407. Application.Run (top);
  408. top.Dispose ();
  409. Application.Shutdown ();
  410. }
  411. [Fact]
  412. [AutoInitShutdown]
  413. public void Application_Begin_FocusesDeepest ()
  414. {
  415. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  416. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  417. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  418. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  419. win2.Add (view2);
  420. win1.Add (view1, win2);
  421. Application.Begin (win1);
  422. Assert.True (win1.HasFocus);
  423. Assert.True (view1.HasFocus);
  424. Assert.False (win2.HasFocus);
  425. Assert.False (view2.HasFocus);
  426. win1.Dispose ();
  427. }
  428. #if V2_NEW_FOCUS_IMPL // bogus test - Depends on auto setting of CanFocus
  429. [Fact]
  430. [AutoInitShutdown]
  431. public void Remove_Does_Not_Change_Focus ()
  432. {
  433. var top = new Toplevel ();
  434. Assert.True (top.CanFocus);
  435. Assert.False (top.HasFocus);
  436. var container = new View { Width = 10, Height = 10 };
  437. var leave = false;
  438. container.Leave += (s, e) => leave = true;
  439. Assert.False (container.CanFocus);
  440. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  441. container.Add (child);
  442. Assert.True (container.CanFocus);
  443. Assert.False (container.HasFocus);
  444. Assert.True (child.CanFocus);
  445. Assert.False (child.HasFocus);
  446. top.Add (container);
  447. Application.Begin (top);
  448. Assert.True (top.CanFocus);
  449. Assert.True (top.HasFocus);
  450. Assert.True (container.CanFocus);
  451. Assert.True (container.HasFocus);
  452. Assert.True (child.CanFocus);
  453. Assert.True (child.HasFocus);
  454. container.Remove (child);
  455. child.Dispose ();
  456. child = null;
  457. Assert.True (top.HasFocus);
  458. Assert.True (container.CanFocus);
  459. Assert.True (container.HasFocus);
  460. Assert.Null (child);
  461. Assert.False (leave);
  462. top.Dispose ();
  463. }
  464. #endif
  465. }