NavigationTests.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.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. Assert.True (left);
  68. }
  69. [Theory]
  70. [MemberData (nameof (AllViewTypes))]
  71. [SetupFakeApplication] // SetupFakeDriver resets app state; helps to avoid test pollution
  72. public void AllViews_HasFocus_Changed_Event (Type viewType)
  73. {
  74. View view = CreateInstanceIfNotGeneric (viewType);
  75. if (view == null)
  76. {
  77. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  78. return;
  79. }
  80. if (!view.CanFocus)
  81. {
  82. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  83. return;
  84. }
  85. if (view is Toplevel && ((Toplevel)view).Modal)
  86. {
  87. output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  88. return;
  89. }
  90. Toplevel top = new ();
  91. Application.Top = top;
  92. Application.Navigation = new ();
  93. View otherView = new ()
  94. {
  95. Id = "otherView",
  96. CanFocus = true,
  97. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  98. };
  99. var hasFocusTrue = 0;
  100. var hasFocusFalse = 0;
  101. view.HasFocusChanged += (s, e) =>
  102. {
  103. if (e.NewValue)
  104. {
  105. hasFocusTrue++;
  106. }
  107. else
  108. {
  109. hasFocusFalse++;
  110. }
  111. };
  112. top.Add (view, otherView);
  113. Assert.False (view.HasFocus);
  114. Assert.False (otherView.HasFocus);
  115. // Ensure the view is Visible
  116. view.Visible = true;
  117. Application.Top.SetFocus ();
  118. Assert.True (Application.Top!.HasFocus);
  119. Assert.True (top.HasFocus);
  120. // Start with the focus on our test view
  121. Assert.True (view.HasFocus);
  122. Assert.Equal (1, hasFocusTrue);
  123. Assert.Equal (0, hasFocusFalse);
  124. // Use keyboard to navigate to next view (otherView).
  125. var tries = 0;
  126. while (view.HasFocus)
  127. {
  128. if (++tries > 10)
  129. {
  130. Assert.Fail ($"{view} is not leaving.");
  131. }
  132. switch (view.TabStop)
  133. {
  134. case null:
  135. case TabBehavior.NoStop:
  136. case TabBehavior.TabStop:
  137. if (Application.RaiseKeyDownEvent (Key.Tab))
  138. {
  139. if (view.HasFocus)
  140. {
  141. // Try another nav key (e.g. for TextView that eats Tab)
  142. Application.RaiseKeyDownEvent (Key.CursorDown);
  143. }
  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. }
  195. [Theory]
  196. [MemberData (nameof (AllViewTypes))]
  197. [SetupFakeApplication] // SetupFakeDriver resets app state; helps to avoid test pollution
  198. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  199. {
  200. View view = CreateInstanceIfNotGeneric (viewType);
  201. if (view == null)
  202. {
  203. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  204. return;
  205. }
  206. if (!view.CanFocus)
  207. {
  208. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  209. return;
  210. }
  211. if (view is Toplevel && ((Toplevel)view).Modal)
  212. {
  213. output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  214. return;
  215. }
  216. Toplevel top = new ();
  217. Application.Top = top;
  218. Application.Navigation = new ();
  219. View otherView = new ()
  220. {
  221. CanFocus = true
  222. };
  223. view.Visible = false;
  224. var hasFocusChangingCount = 0;
  225. var hasFocusChangedCount = 0;
  226. view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
  227. view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
  228. top.Add (view, otherView);
  229. // Start with the focus on our test view
  230. view.SetFocus ();
  231. Assert.Equal (0, hasFocusChangingCount);
  232. Assert.Equal (0, hasFocusChangedCount);
  233. Application.RaiseKeyDownEvent (Key.Tab);
  234. Assert.Equal (0, hasFocusChangingCount);
  235. Assert.Equal (0, hasFocusChangedCount);
  236. Application.RaiseKeyDownEvent (Key.F6);
  237. Assert.Equal (0, hasFocusChangingCount);
  238. Assert.Equal (0, hasFocusChangedCount);
  239. top.Dispose ();
  240. }
  241. [Fact]
  242. [AutoInitShutdown]
  243. public void Application_Begin_FocusesDeepest ()
  244. {
  245. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  246. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  247. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  248. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  249. win2.Add (view2);
  250. win1.Add (view1, win2);
  251. Application.Begin (win1);
  252. Assert.True (win1.HasFocus);
  253. Assert.True (view1.HasFocus);
  254. Assert.False (win2.HasFocus);
  255. Assert.False (view2.HasFocus);
  256. win1.Dispose ();
  257. }
  258. }