NavigationTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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.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.Top = 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.Top.SetFocus ();
  115. Assert.True (Application.Top!.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.Top = 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. [SetupFakeDriver]
  288. public void Navigation_With_Null_Focused_View ()
  289. {
  290. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  291. Application.Init (new FakeDriver ());
  292. var top = new Toplevel ();
  293. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  294. // Keyboard navigation with tab
  295. FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
  296. Application.Iteration += (s, a) => Application.RequestStop ();
  297. Application.Run (top);
  298. top.Dispose ();
  299. Application.Shutdown ();
  300. }
  301. [Fact]
  302. [AutoInitShutdown]
  303. public void Application_Begin_FocusesDeepest ()
  304. {
  305. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  306. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  307. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  308. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  309. win2.Add (view2);
  310. win1.Add (view1, win2);
  311. Application.Begin (win1);
  312. Assert.True (win1.HasFocus);
  313. Assert.True (view1.HasFocus);
  314. Assert.False (win2.HasFocus);
  315. Assert.False (view2.HasFocus);
  316. win1.Dispose ();
  317. }
  318. }