NavigationTests.cs 11 KB

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