AllViewsNavigationTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #nullable enable
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace ViewBaseTests.Navigation;
  5. public class AllViewsNavigationTests (ITestOutputHelper output) : TestsAllViews
  6. {
  7. [Theory]
  8. [MemberData (nameof (AllViewTypes))]
  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. IApplication app = Application.Create ();
  23. app.Begin (new Runnable<bool> () { CanFocus = true });
  24. View otherView = new ()
  25. {
  26. Id = "otherView",
  27. CanFocus = true,
  28. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  29. };
  30. app.TopRunnableView!.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. app.Keyboard.RaiseKeyDownEvent (key);
  47. if (view.HasFocus)
  48. {
  49. // Try once more (HexView)
  50. app.Keyboard.RaiseKeyDownEvent (key);
  51. }
  52. break;
  53. default:
  54. app.Keyboard.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. Assert.True (left);
  66. }
  67. [Theory]
  68. [MemberData (nameof (AllViewTypes))]
  69. public void AllViews_HasFocus_Changed_Event (Type viewType)
  70. {
  71. View? view = CreateInstanceIfNotGeneric (viewType);
  72. if (view == null)
  73. {
  74. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  75. return;
  76. }
  77. if (!view.CanFocus)
  78. {
  79. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  80. return;
  81. }
  82. if (view is IRunnable)
  83. {
  84. output.WriteLine ($"Ignoring {viewType} - It's an IRunnable");
  85. return;
  86. }
  87. IApplication app = Application.Create ();
  88. app.Begin (new Runnable<bool> () { CanFocus = true });
  89. View otherView = new ()
  90. {
  91. Id = "otherView",
  92. CanFocus = true,
  93. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  94. };
  95. var hasFocusTrue = 0;
  96. var hasFocusFalse = 0;
  97. // Ensure the view is Visible
  98. view.Visible = true;
  99. view.HasFocus = false;
  100. view.HasFocusChanged += (s, e) =>
  101. {
  102. if (e.NewValue)
  103. {
  104. hasFocusTrue++;
  105. }
  106. else
  107. {
  108. hasFocusFalse++;
  109. }
  110. };
  111. Assert.Equal (0, hasFocusTrue);
  112. Assert.Equal (0, hasFocusFalse);
  113. app.TopRunnableView!.Add (view, otherView);
  114. Assert.False (view.HasFocus);
  115. Assert.True (otherView.HasFocus);
  116. Assert.Equal (1, hasFocusTrue);
  117. Assert.Equal (1, hasFocusFalse);
  118. // Start with the focus on our test view
  119. view.SetFocus ();
  120. Assert.True (view.HasFocus);
  121. Assert.Equal (2, hasFocusTrue);
  122. Assert.Equal (1, 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 (app.Keyboard.RaiseKeyDownEvent (Key.Tab))
  137. {
  138. if (view.HasFocus)
  139. {
  140. // Try another nav key (e.g. for TextView that eats Tab)
  141. app.Keyboard.RaiseKeyDownEvent (Key.CursorDown);
  142. }
  143. };
  144. break;
  145. case TabBehavior.TabGroup:
  146. app.Keyboard.RaiseKeyDownEvent (Key.F6);
  147. break;
  148. default:
  149. throw new ArgumentOutOfRangeException ();
  150. }
  151. }
  152. Assert.Equal (2, hasFocusTrue);
  153. Assert.Equal (2, 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. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  164. break;
  165. case TabBehavior.TabGroup:
  166. if (!app.Keyboard.RaiseKeyDownEvent (Key.F6))
  167. {
  168. view.SetFocus ();
  169. }
  170. break;
  171. case null:
  172. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  173. break;
  174. default:
  175. throw new ArgumentOutOfRangeException ();
  176. }
  177. Assert.Equal (3, hasFocusTrue);
  178. Assert.Equal (2, 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. Assert.False (otherViewHasFocus);
  188. Assert.True (viewHasFocus);
  189. Assert.Equal (3, enterCount);
  190. Assert.Equal (2, leaveCount);
  191. }
  192. [Theory]
  193. [MemberData (nameof (AllViewTypes))]
  194. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  195. {
  196. View? view = CreateInstanceIfNotGeneric (viewType);
  197. if (view == null)
  198. {
  199. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  200. return;
  201. }
  202. if (!view.CanFocus)
  203. {
  204. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  205. return;
  206. }
  207. if (view is IRunnable)
  208. {
  209. output.WriteLine ($"Ignoring {viewType} - It's an IRunnable");
  210. return;
  211. }
  212. IApplication? app = Application.Create ();
  213. app.Begin (new Runnable<bool> () { CanFocus = true });
  214. View otherView = new ()
  215. {
  216. CanFocus = true
  217. };
  218. view.Visible = false;
  219. var hasFocusChangingCount = 0;
  220. var hasFocusChangedCount = 0;
  221. view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
  222. view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
  223. app.TopRunnableView!.Add (view, otherView);
  224. // Start with the focus on our test view
  225. view.SetFocus ();
  226. Assert.Equal (0, hasFocusChangingCount);
  227. Assert.Equal (0, hasFocusChangedCount);
  228. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  229. Assert.Equal (0, hasFocusChangingCount);
  230. Assert.Equal (0, hasFocusChangedCount);
  231. app.Keyboard.RaiseKeyDownEvent (Key.F6);
  232. Assert.Equal (0, hasFocusChangingCount);
  233. Assert.Equal (0, hasFocusChangedCount);
  234. }
  235. [Fact]
  236. public void Application_Begin_FocusesDeepest ()
  237. {
  238. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  239. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  240. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  241. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  242. win2.Add (view2);
  243. win1.Add (view1, win2);
  244. IApplication app = Application.Create ();
  245. app.Begin (win1);
  246. Assert.True (win1.HasFocus);
  247. Assert.True (view1.HasFocus);
  248. Assert.False (win2.HasFocus);
  249. Assert.False (view2.HasFocus);
  250. }
  251. // View.Focused & View.MostFocused tests
  252. // View.Focused - No subviews
  253. [Fact]
  254. public void Focused_NoSubViews ()
  255. {
  256. var view = new View ();
  257. Assert.Null (view.Focused);
  258. view.CanFocus = true;
  259. view.SetFocus ();
  260. }
  261. [Fact]
  262. public void GetMostFocused_NoSubViews_Returns_Null ()
  263. {
  264. var view = new View ();
  265. Assert.Null (view.Focused);
  266. view.CanFocus = true;
  267. Assert.False (view.HasFocus);
  268. view.SetFocus ();
  269. Assert.True (view.HasFocus);
  270. Assert.Null (view.MostFocused);
  271. }
  272. [Fact]
  273. public void GetMostFocused_Returns_Most ()
  274. {
  275. var view = new View
  276. {
  277. Id = "view",
  278. CanFocus = true
  279. };
  280. var subview = new View
  281. {
  282. Id = "subview",
  283. CanFocus = true
  284. };
  285. view.Add (subview);
  286. view.SetFocus ();
  287. Assert.True (view.HasFocus);
  288. Assert.True (subview.HasFocus);
  289. Assert.Equal (subview, view.MostFocused);
  290. var subview2 = new View
  291. {
  292. Id = "subview2",
  293. CanFocus = true
  294. };
  295. view.Add (subview2);
  296. Assert.Equal (subview2, view.MostFocused);
  297. }
  298. }