NavigationTests.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using UnitTests;
  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 ();
  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 ();
  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. ;
  147. break;
  148. case TabBehavior.TabGroup:
  149. Application.RaiseKeyDownEvent (Key.F6);
  150. break;
  151. default:
  152. throw new ArgumentOutOfRangeException ();
  153. }
  154. }
  155. Assert.Equal (1, hasFocusTrue);
  156. Assert.Equal (1, hasFocusFalse);
  157. Assert.False (view.HasFocus);
  158. Assert.True (otherView.HasFocus);
  159. // Now navigate back to our test view
  160. switch (view.TabStop)
  161. {
  162. case TabBehavior.NoStop:
  163. view.SetFocus ();
  164. break;
  165. case TabBehavior.TabStop:
  166. Application.RaiseKeyDownEvent (Key.Tab);
  167. break;
  168. case TabBehavior.TabGroup:
  169. if (!Application.RaiseKeyDownEvent (Key.F6))
  170. {
  171. view.SetFocus ();
  172. }
  173. break;
  174. case null:
  175. Application.RaiseKeyDownEvent (Key.Tab);
  176. break;
  177. default:
  178. throw new ArgumentOutOfRangeException ();
  179. }
  180. Assert.Equal (2, hasFocusTrue);
  181. Assert.Equal (1, hasFocusFalse);
  182. Assert.True (view.HasFocus);
  183. Assert.False (otherView.HasFocus);
  184. // Cache state because Shutdown has side effects.
  185. // Also ensures other tests can continue running if there's a fail
  186. bool otherViewHasFocus = otherView.HasFocus;
  187. bool viewHasFocus = view.HasFocus;
  188. int enterCount = hasFocusTrue;
  189. int leaveCount = hasFocusFalse;
  190. top.Dispose ();
  191. Assert.False (otherViewHasFocus);
  192. Assert.True (viewHasFocus);
  193. Assert.Equal (2, enterCount);
  194. Assert.Equal (1, leaveCount);
  195. Application.ResetState ();
  196. }
  197. [Theory]
  198. [MemberData (nameof (AllViewTypes))]
  199. [SetupFakeDriver] // SetupFakeDriver resets app state; helps to avoid test pollution
  200. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  201. {
  202. View view = CreateInstanceIfNotGeneric (viewType);
  203. if (view == null)
  204. {
  205. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  206. return;
  207. }
  208. if (!view.CanFocus)
  209. {
  210. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  211. return;
  212. }
  213. if (view is Toplevel && ((Toplevel)view).Modal)
  214. {
  215. output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  216. return;
  217. }
  218. Toplevel top = new ();
  219. Application.Top = top;
  220. Application.Navigation = new ();
  221. View otherView = new ()
  222. {
  223. CanFocus = true
  224. };
  225. view.Visible = false;
  226. var hasFocusChangingCount = 0;
  227. var hasFocusChangedCount = 0;
  228. view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
  229. view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
  230. top.Add (view, otherView);
  231. // Start with the focus on our test view
  232. view.SetFocus ();
  233. Assert.Equal (0, hasFocusChangingCount);
  234. Assert.Equal (0, hasFocusChangedCount);
  235. Application.RaiseKeyDownEvent (Key.Tab);
  236. Assert.Equal (0, hasFocusChangingCount);
  237. Assert.Equal (0, hasFocusChangedCount);
  238. Application.RaiseKeyDownEvent (Key.F6);
  239. Assert.Equal (0, hasFocusChangingCount);
  240. Assert.Equal (0, hasFocusChangedCount);
  241. top.Dispose ();
  242. Application.ResetState ();
  243. }
  244. [Fact]
  245. [SetupFakeDriver]
  246. public void Navigation_With_Null_Focused_View ()
  247. {
  248. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  249. Application.Init (new FakeDriver ());
  250. var top = new Toplevel ();
  251. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  252. // Keyboard navigation with tab
  253. FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
  254. Application.Iteration += (s, a) => Application.RequestStop ();
  255. Application.Run (top);
  256. top.Dispose ();
  257. Application.Shutdown ();
  258. }
  259. [Fact]
  260. [AutoInitShutdown]
  261. public void Application_Begin_FocusesDeepest ()
  262. {
  263. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  264. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  265. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  266. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  267. win2.Add (view2);
  268. win1.Add (view1, win2);
  269. Application.Begin (win1);
  270. Assert.True (win1.HasFocus);
  271. Assert.True (view1.HasFocus);
  272. Assert.False (win2.HasFocus);
  273. Assert.False (view2.HasFocus);
  274. win1.Dispose ();
  275. }
  276. }