NavigationTests.cs 11 KB

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