NavigationTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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.Top = 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.RaiseKeyDownEvent (key);
  48. if (view.HasFocus)
  49. {
  50. // Try once more (HexView)
  51. Application.RaiseKeyDownEvent (key);
  52. }
  53. break;
  54. default:
  55. Application.RaiseKeyDownEvent (Key.Tab);
  56. break;
  57. }
  58. if (!view.HasFocus)
  59. {
  60. left = true;
  61. _output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
  62. view.SetFocus ();
  63. }
  64. else
  65. {
  66. _output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
  67. }
  68. }
  69. top.Dispose ();
  70. Application.ResetState ();
  71. Assert.True (left);
  72. }
  73. [Theory]
  74. [MemberData (nameof (AllViewTypes))]
  75. [SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
  76. public void AllViews_HasFocus_Changed_Event (Type viewType)
  77. {
  78. View view = CreateInstanceIfNotGeneric (viewType);
  79. if (view == null)
  80. {
  81. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  82. return;
  83. }
  84. if (!view.CanFocus)
  85. {
  86. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  87. return;
  88. }
  89. if (view is Toplevel && ((Toplevel)view).Modal)
  90. {
  91. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  92. return;
  93. }
  94. Toplevel top = new ();
  95. Application.Top = top;
  96. Application.Navigation = new ApplicationNavigation ();
  97. View otherView = new ()
  98. {
  99. Id = "otherView",
  100. CanFocus = true,
  101. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  102. };
  103. var hasFocusTrue = 0;
  104. var hasFocusFalse = 0;
  105. view.HasFocusChanged += (s, e) =>
  106. {
  107. if (e.NewValue)
  108. {
  109. hasFocusTrue++;
  110. }
  111. else
  112. {
  113. hasFocusFalse++;
  114. }
  115. };
  116. top.Add (view, otherView);
  117. Assert.False (view.HasFocus);
  118. Assert.False (otherView.HasFocus);
  119. // Ensure the view is Visible
  120. view.Visible = true;
  121. Application.Top.SetFocus ();
  122. Assert.True (Application.Top!.HasFocus);
  123. Assert.True (top.HasFocus);
  124. // Start with the focus on our test view
  125. Assert.True (view.HasFocus);
  126. Assert.Equal (1, hasFocusTrue);
  127. Assert.Equal (0, hasFocusFalse);
  128. // Use keyboard to navigate to next view (otherView).
  129. var tries = 0;
  130. while (view.HasFocus)
  131. {
  132. if (++tries > 10)
  133. {
  134. Assert.Fail ($"{view} is not leaving.");
  135. }
  136. switch (view.TabStop)
  137. {
  138. case null:
  139. case TabBehavior.NoStop:
  140. case TabBehavior.TabStop:
  141. if (Application.RaiseKeyDownEvent (Key.Tab))
  142. {
  143. if (view.HasFocus)
  144. {
  145. // Try another nav key (e.g. for TextView that eats Tab)
  146. Application.RaiseKeyDownEvent (Key.CursorDown);
  147. }
  148. };
  149. break;
  150. case TabBehavior.TabGroup:
  151. Application.RaiseKeyDownEvent (Key.F6);
  152. break;
  153. default:
  154. throw new ArgumentOutOfRangeException ();
  155. }
  156. }
  157. Assert.Equal (1, hasFocusTrue);
  158. Assert.Equal (1, hasFocusFalse);
  159. Assert.False (view.HasFocus);
  160. Assert.True (otherView.HasFocus);
  161. // Now navigate back to our test view
  162. switch (view.TabStop)
  163. {
  164. case TabBehavior.NoStop:
  165. view.SetFocus ();
  166. break;
  167. case TabBehavior.TabStop:
  168. Application.RaiseKeyDownEvent (Key.Tab);
  169. break;
  170. case TabBehavior.TabGroup:
  171. if (!Application.RaiseKeyDownEvent (Key.F6))
  172. {
  173. view.SetFocus ();
  174. }
  175. break;
  176. case null:
  177. Application.RaiseKeyDownEvent (Key.Tab);
  178. break;
  179. default:
  180. throw new ArgumentOutOfRangeException ();
  181. }
  182. Assert.Equal (2, hasFocusTrue);
  183. Assert.Equal (1, hasFocusFalse);
  184. Assert.True (view.HasFocus);
  185. Assert.False (otherView.HasFocus);
  186. // Cache state because Shutdown has side effects.
  187. // Also ensures other tests can continue running if there's a fail
  188. bool otherViewHasFocus = otherView.HasFocus;
  189. bool viewHasFocus = view.HasFocus;
  190. int enterCount = hasFocusTrue;
  191. int leaveCount = hasFocusFalse;
  192. top.Dispose ();
  193. Assert.False (otherViewHasFocus);
  194. Assert.True (viewHasFocus);
  195. Assert.Equal (2, enterCount);
  196. Assert.Equal (1, leaveCount);
  197. Application.ResetState ();
  198. }
  199. [Theory]
  200. [MemberData (nameof (AllViewTypes))]
  201. [SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
  202. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  203. {
  204. View view = CreateInstanceIfNotGeneric (viewType);
  205. if (view == null)
  206. {
  207. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  208. return;
  209. }
  210. if (!view.CanFocus)
  211. {
  212. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  213. return;
  214. }
  215. if (view is Toplevel && ((Toplevel)view).Modal)
  216. {
  217. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  218. return;
  219. }
  220. Toplevel top = new ();
  221. Application.Top = top;
  222. Application.Navigation = new ApplicationNavigation ();
  223. View otherView = new ()
  224. {
  225. CanFocus = true
  226. };
  227. view.Visible = false;
  228. var hasFocusChangingCount = 0;
  229. var hasFocusChangedCount = 0;
  230. view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
  231. view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
  232. top.Add (view, otherView);
  233. // Start with the focus on our test view
  234. view.SetFocus ();
  235. Assert.Equal (0, hasFocusChangingCount);
  236. Assert.Equal (0, hasFocusChangedCount);
  237. Application.RaiseKeyDownEvent (Key.Tab);
  238. Assert.Equal (0, hasFocusChangingCount);
  239. Assert.Equal (0, hasFocusChangedCount);
  240. Application.RaiseKeyDownEvent (Key.F6);
  241. Assert.Equal (0, hasFocusChangingCount);
  242. Assert.Equal (0, hasFocusChangedCount);
  243. top.Dispose ();
  244. Application.ResetState ();
  245. }
  246. // View.Focused & View.MostFocused tests
  247. // View.Focused - No subviews
  248. [Fact]
  249. public void Focused_NoSubviews ()
  250. {
  251. var view = new View ();
  252. Assert.Null (view.Focused);
  253. view.CanFocus = true;
  254. view.SetFocus ();
  255. }
  256. [Fact]
  257. public void GetMostFocused_NoSubviews_Returns_Null ()
  258. {
  259. var view = new View ();
  260. Assert.Null (view.Focused);
  261. view.CanFocus = true;
  262. Assert.False (view.HasFocus);
  263. view.SetFocus ();
  264. Assert.True (view.HasFocus);
  265. Assert.Null (view.MostFocused);
  266. }
  267. [Fact]
  268. public void GetMostFocused_Returns_Most ()
  269. {
  270. var view = new View ()
  271. {
  272. Id = "view",
  273. CanFocus = true
  274. };
  275. var subview = new View ()
  276. {
  277. Id = "subview",
  278. CanFocus = true
  279. };
  280. view.Add (subview);
  281. view.SetFocus ();
  282. Assert.True (view.HasFocus);
  283. Assert.True (subview.HasFocus);
  284. Assert.Equal (subview, view.MostFocused);
  285. var subview2 = new View ()
  286. {
  287. Id = "subview2",
  288. CanFocus = true
  289. };
  290. view.Add (subview2);
  291. Assert.Equal (subview2, view.MostFocused);
  292. }
  293. [Fact]
  294. [SetupFakeDriver]
  295. public void Navigation_With_Null_Focused_View ()
  296. {
  297. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  298. Application.Init (new FakeDriver ());
  299. var top = new Toplevel ();
  300. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  301. // Keyboard navigation with tab
  302. FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
  303. Application.Iteration += (s, a) => Application.RequestStop ();
  304. Application.Run (top);
  305. top.Dispose ();
  306. Application.Shutdown ();
  307. }
  308. [Fact]
  309. [AutoInitShutdown]
  310. public void Application_Begin_FocusesDeepest ()
  311. {
  312. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  313. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  314. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  315. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  316. win2.Add (view2);
  317. win1.Add (view1, win2);
  318. Application.Begin (win1);
  319. Assert.True (win1.HasFocus);
  320. Assert.True (view1.HasFocus);
  321. Assert.False (win2.HasFocus);
  322. Assert.False (view2.HasFocus);
  323. win1.Dispose ();
  324. }
  325. }