NavigationTests.cs 11 KB

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