AllViewsNavigationTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. if (view is IDesignable designable)
  23. {
  24. designable.EnableForDesign ();
  25. }
  26. IApplication app = Application.Create ();
  27. app.Begin (new Runnable<bool> () { CanFocus = true });
  28. View otherView = new ()
  29. {
  30. Id = "otherView",
  31. CanFocus = true,
  32. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  33. };
  34. app.TopRunnableView!.Add (view, otherView);
  35. // Start with the focus on our test view
  36. view.SetFocus ();
  37. Key [] navKeys = [Key.Tab, Key.Tab.WithShift, Key.CursorUp, Key.CursorDown, Key.CursorLeft, Key.CursorRight];
  38. if (view.TabStop == TabBehavior.TabGroup)
  39. {
  40. navKeys = [Key.F6, Key.F6.WithShift];
  41. }
  42. var left = false;
  43. foreach (Key key in navKeys)
  44. {
  45. switch (view.TabStop)
  46. {
  47. case TabBehavior.TabStop:
  48. case TabBehavior.NoStop:
  49. case TabBehavior.TabGroup:
  50. app.Keyboard.RaiseKeyDownEvent (key);
  51. if (view.HasFocus)
  52. {
  53. // Try once more (HexView)
  54. app.Keyboard.RaiseKeyDownEvent (key);
  55. }
  56. break;
  57. default:
  58. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  59. break;
  60. }
  61. if (!view.HasFocus)
  62. {
  63. left = true;
  64. output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
  65. break;
  66. }
  67. output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
  68. }
  69. Assert.True (left);
  70. }
  71. [Theory]
  72. [MemberData (nameof (AllViewTypes))]
  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 IRunnable)
  87. {
  88. output.WriteLine ($"Ignoring {viewType} - It's an IRunnable");
  89. return;
  90. }
  91. if (view is IDesignable designable)
  92. {
  93. designable.EnableForDesign ();
  94. }
  95. IApplication app = Application.Create ();
  96. app.Begin (new Runnable<bool> () { CanFocus = true });
  97. View otherView = new ()
  98. {
  99. Id = "otherView",
  100. CanFocus = true,
  101. TabStop = view.TabStop == TabBehavior.NoStop ? TabBehavior.TabStop : view.TabStop
  102. };
  103. var hasFocusTrue = 0;
  104. var hasFocusFalse = 0;
  105. // Ensure the view is Visible
  106. view.Visible = true;
  107. view.HasFocus = false;
  108. view.HasFocusChanged += (s, e) =>
  109. {
  110. if (e.NewValue)
  111. {
  112. hasFocusTrue++;
  113. }
  114. else
  115. {
  116. hasFocusFalse++;
  117. }
  118. };
  119. Assert.Equal (0, hasFocusTrue);
  120. Assert.Equal (0, hasFocusFalse);
  121. app.TopRunnableView!.Add (view, otherView);
  122. Assert.False (view.HasFocus);
  123. Assert.True (otherView.HasFocus);
  124. Assert.Equal (1, hasFocusTrue);
  125. Assert.Equal (1, hasFocusFalse);
  126. // Start with the focus on our test view
  127. view.SetFocus ();
  128. Assert.True (view.HasFocus);
  129. Assert.Equal (2, hasFocusTrue);
  130. Assert.Equal (1, hasFocusFalse);
  131. // Use keyboard to navigate to next view (otherView).
  132. var tries = 0;
  133. while (view.HasFocus)
  134. {
  135. if (++tries > 10)
  136. {
  137. Assert.Fail ($"{view} is not leaving.");
  138. }
  139. switch (view.TabStop)
  140. {
  141. case null:
  142. case TabBehavior.NoStop:
  143. case TabBehavior.TabStop:
  144. if (app.Keyboard.RaiseKeyDownEvent (Key.Tab))
  145. {
  146. if (view.HasFocus)
  147. {
  148. // Try another nav key (e.g. for TextView that eats Tab)
  149. app.Keyboard.RaiseKeyDownEvent (Key.CursorDown);
  150. }
  151. };
  152. break;
  153. case TabBehavior.TabGroup:
  154. app.Keyboard.RaiseKeyDownEvent (Key.F6);
  155. break;
  156. default:
  157. throw new ArgumentOutOfRangeException ();
  158. }
  159. }
  160. Assert.Equal (2, hasFocusTrue);
  161. Assert.Equal (2, hasFocusFalse);
  162. Assert.False (view.HasFocus);
  163. Assert.True (otherView.HasFocus);
  164. // Now navigate back to our test view
  165. switch (view.TabStop)
  166. {
  167. case TabBehavior.NoStop:
  168. view.SetFocus ();
  169. break;
  170. case TabBehavior.TabStop:
  171. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  172. break;
  173. case TabBehavior.TabGroup:
  174. if (!app.Keyboard.RaiseKeyDownEvent (Key.F6))
  175. {
  176. view.SetFocus ();
  177. }
  178. break;
  179. case null:
  180. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  181. break;
  182. default:
  183. throw new ArgumentOutOfRangeException ();
  184. }
  185. Assert.Equal (3, hasFocusTrue);
  186. Assert.Equal (2, hasFocusFalse);
  187. Assert.True (view.HasFocus);
  188. Assert.False (otherView.HasFocus);
  189. // Cache state because Shutdown has side effects.
  190. // Also ensures other tests can continue running if there's a fail
  191. bool otherViewHasFocus = otherView.HasFocus;
  192. bool viewHasFocus = view.HasFocus;
  193. int enterCount = hasFocusTrue;
  194. int leaveCount = hasFocusFalse;
  195. Assert.False (otherViewHasFocus);
  196. Assert.True (viewHasFocus);
  197. Assert.Equal (3, enterCount);
  198. Assert.Equal (2, leaveCount);
  199. }
  200. [Theory]
  201. [MemberData (nameof (AllViewTypes))]
  202. public void AllViews_Visible_False_No_HasFocus_Events (Type viewType)
  203. {
  204. View? view = CreateInstanceIfNotGeneric (viewType);
  205. if (view == null)
  206. {
  207. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  208. return;
  209. }
  210. if (!view.CanFocus)
  211. {
  212. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  213. return;
  214. }
  215. if (view is IRunnable)
  216. {
  217. output.WriteLine ($"Ignoring {viewType} - It's an IRunnable");
  218. return;
  219. }
  220. IApplication? app = Application.Create ();
  221. app.Begin (new Runnable<bool> () { CanFocus = true });
  222. View otherView = new ()
  223. {
  224. CanFocus = true
  225. };
  226. view.Visible = false;
  227. var hasFocusChangingCount = 0;
  228. var hasFocusChangedCount = 0;
  229. view.HasFocusChanging += (s, e) => hasFocusChangingCount++;
  230. view.HasFocusChanged += (s, e) => hasFocusChangedCount++;
  231. app.TopRunnableView!.Add (view, otherView);
  232. // Start with the focus on our test view
  233. view.SetFocus ();
  234. Assert.Equal (0, hasFocusChangingCount);
  235. Assert.Equal (0, hasFocusChangedCount);
  236. app.Keyboard.RaiseKeyDownEvent (Key.Tab);
  237. Assert.Equal (0, hasFocusChangingCount);
  238. Assert.Equal (0, hasFocusChangedCount);
  239. app.Keyboard.RaiseKeyDownEvent (Key.F6);
  240. Assert.Equal (0, hasFocusChangingCount);
  241. Assert.Equal (0, hasFocusChangedCount);
  242. }
  243. [Fact]
  244. public void Application_Begin_FocusesDeepest ()
  245. {
  246. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  247. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  248. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  249. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  250. win2.Add (view2);
  251. win1.Add (view1, win2);
  252. IApplication app = Application.Create ();
  253. app.Begin (win1);
  254. Assert.True (win1.HasFocus);
  255. Assert.True (view1.HasFocus);
  256. Assert.False (win2.HasFocus);
  257. Assert.False (view2.HasFocus);
  258. }
  259. // View.Focused & View.MostFocused tests
  260. // View.Focused - No subviews
  261. [Fact]
  262. public void Focused_NoSubViews ()
  263. {
  264. var view = new View ();
  265. Assert.Null (view.Focused);
  266. view.CanFocus = true;
  267. view.SetFocus ();
  268. }
  269. [Fact]
  270. public void GetMostFocused_NoSubViews_Returns_Null ()
  271. {
  272. var view = new View ();
  273. Assert.Null (view.Focused);
  274. view.CanFocus = true;
  275. Assert.False (view.HasFocus);
  276. view.SetFocus ();
  277. Assert.True (view.HasFocus);
  278. Assert.Null (view.MostFocused);
  279. }
  280. [Fact]
  281. public void GetMostFocused_Returns_Most ()
  282. {
  283. var view = new View
  284. {
  285. Id = "view",
  286. CanFocus = true
  287. };
  288. var subview = new View
  289. {
  290. Id = "subview",
  291. CanFocus = true
  292. };
  293. view.Add (subview);
  294. view.SetFocus ();
  295. Assert.True (view.HasFocus);
  296. Assert.True (subview.HasFocus);
  297. Assert.Equal (subview, view.MostFocused);
  298. var subview2 = new View
  299. {
  300. Id = "subview2",
  301. CanFocus = true
  302. };
  303. view.Add (subview2);
  304. Assert.Equal (subview2, view.MostFocused);
  305. }
  306. }