NavigationTests.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace UnitTests.ViewTests;
  4. public class NavigationTests (ITestOutputHelper output) : TestsAllViews
  5. {
  6. [Theory]
  7. [MemberData (nameof (AllViewTypes))]
  8. [SetupFakeApplication] // 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.TopRunnable = top;
  24. View otherView = new ()
  25. {
  26. Id = "otherView",
  27. CanFocus = true,
  28. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  29. };
  30. top.Add (view, otherView);
  31. // Start with the focus on our test view
  32. view.SetFocus ();
  33. Key [] navKeys = [Key.Tab, Key.Tab.WithShift, Key.CursorUp, Key.CursorDown, Key.CursorLeft, Key.CursorRight];
  34. if (view.TabStop == TabBehavior.TabGroup)
  35. {
  36. navKeys = new [] { Key.F6, Key.F6.WithShift };
  37. }
  38. var left = false;
  39. foreach (Key key in navKeys)
  40. {
  41. switch (view.TabStop)
  42. {
  43. case TabBehavior.TabStop:
  44. case TabBehavior.NoStop:
  45. case TabBehavior.TabGroup:
  46. Application.RaiseKeyDownEvent (key);
  47. if (view.HasFocus)
  48. {
  49. // Try once more (HexView)
  50. Application.RaiseKeyDownEvent (key);
  51. }
  52. break;
  53. default:
  54. Application.RaiseKeyDownEvent (Key.Tab);
  55. break;
  56. }
  57. if (!view.HasFocus)
  58. {
  59. left = true;
  60. output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
  61. break;
  62. }
  63. output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
  64. }
  65. top.Dispose ();
  66. Assert.True (left);
  67. }
  68. [Theory]
  69. [MemberData (nameof (AllViewTypes))]
  70. [SetupFakeApplication] // 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.TopRunnable = top;
  91. View otherView = new ()
  92. {
  93. Id = "otherView",
  94. CanFocus = true,
  95. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  96. };
  97. var hasFocusTrue = 0;
  98. var hasFocusFalse = 0;
  99. view.HasFocusChanged += (s, e) =>
  100. {
  101. if (e.NewValue)
  102. {
  103. hasFocusTrue++;
  104. }
  105. else
  106. {
  107. hasFocusFalse++;
  108. }
  109. };
  110. top.Add (view, otherView);
  111. Assert.False (view.HasFocus);
  112. Assert.False (otherView.HasFocus);
  113. // Ensure the view is Visible
  114. view.Visible = true;
  115. Application.TopRunnable.SetFocus ();
  116. Assert.True (Application.TopRunnable!.HasFocus);
  117. Assert.True (top.HasFocus);
  118. // Start with the focus on our test view
  119. Assert.True (view.HasFocus);
  120. Assert.Equal (1, hasFocusTrue);
  121. Assert.Equal (0, hasFocusFalse);
  122. // Use keyboard to navigate to next view (otherView).
  123. var tries = 0;
  124. while (view.HasFocus)
  125. {
  126. if (++tries > 10)
  127. {
  128. Assert.Fail ($"{view} is not leaving.");
  129. }
  130. switch (view.TabStop)
  131. {
  132. case null:
  133. case TabBehavior.NoStop:
  134. case TabBehavior.TabStop:
  135. if (Application.RaiseKeyDownEvent (Key.Tab))
  136. {
  137. if (view.HasFocus)
  138. {
  139. // Try another nav key (e.g. for TextView that eats Tab)
  140. Application.RaiseKeyDownEvent (Key.CursorDown);
  141. }
  142. }
  143. ;
  144. break;
  145. case TabBehavior.TabGroup:
  146. Application.RaiseKeyDownEvent (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.RaiseKeyDownEvent (Key.Tab);
  164. break;
  165. case TabBehavior.TabGroup:
  166. if (!Application.RaiseKeyDownEvent (Key.F6))
  167. {
  168. view.SetFocus ();
  169. }
  170. break;
  171. case null:
  172. Application.RaiseKeyDownEvent (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. }
  193. [Theory]
  194. [MemberData (nameof (AllViewTypes))]
  195. [SetupFakeApplication] // SetupFakeDriver resets app state; helps to avoid test pollution
  196. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  197. {
  198. View view = CreateInstanceIfNotGeneric (viewType);
  199. if (view == null)
  200. {
  201. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  202. return;
  203. }
  204. if (!view.CanFocus)
  205. {
  206. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  207. return;
  208. }
  209. if (view is Toplevel && ((Toplevel)view).Modal)
  210. {
  211. output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  212. return;
  213. }
  214. Toplevel top = new ();
  215. Application.TopRunnable = top;
  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.RaiseKeyDownEvent (Key.Tab);
  231. Assert.Equal (0, hasFocusChangingCount);
  232. Assert.Equal (0, hasFocusChangedCount);
  233. Application.RaiseKeyDownEvent (Key.F6);
  234. Assert.Equal (0, hasFocusChangingCount);
  235. Assert.Equal (0, hasFocusChangedCount);
  236. top.Dispose ();
  237. }
  238. [Fact]
  239. [AutoInitShutdown]
  240. public void Application_Begin_FocusesDeepest ()
  241. {
  242. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  243. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  244. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  245. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  246. win2.Add (view2);
  247. win1.Add (view1, win2);
  248. Application.Begin (win1);
  249. Assert.True (win1.HasFocus);
  250. Assert.True (view1.HasFocus);
  251. Assert.False (win2.HasFocus);
  252. Assert.False (view2.HasFocus);
  253. win1.Dispose ();
  254. }
  255. }