CanFocusTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class CanFocusTests () : TestsAllViews
  4. {
  5. [Fact]
  6. public void CanFocus_False_Prevents_SubSubView_HasFocus ()
  7. {
  8. var view = new View { };
  9. var subView = new View { };
  10. var subSubView = new View { CanFocus = true };
  11. subView.Add (subSubView);
  12. view.Add (subView);
  13. Assert.False (view.CanFocus);
  14. Assert.False (subView.CanFocus);
  15. Assert.True (subSubView.CanFocus);
  16. view.SetFocus ();
  17. Assert.False (view.HasFocus);
  18. subView.SetFocus ();
  19. Assert.False (subView.HasFocus);
  20. subSubView.SetFocus ();
  21. Assert.False (subSubView.HasFocus);
  22. }
  23. [Fact]
  24. public void CanFocus_False_Prevents_SubView_HasFocus ()
  25. {
  26. var view = new View { };
  27. var subView = new View { CanFocus = true };
  28. var subSubView = new View { };
  29. subView.Add (subSubView);
  30. view.Add (subView);
  31. Assert.False (view.CanFocus);
  32. Assert.True (subView.CanFocus);
  33. Assert.False (subSubView.CanFocus);
  34. view.SetFocus ();
  35. Assert.False (view.HasFocus);
  36. subView.SetFocus ();
  37. Assert.False (subView.HasFocus);
  38. subSubView.SetFocus ();
  39. Assert.False (subSubView.HasFocus);
  40. }
  41. [Fact]
  42. public void CanFocus_Set_True_No_SuperView_Doesnt_Set_HasFocus ()
  43. {
  44. var view = new View { };
  45. // Act
  46. view.CanFocus = true;
  47. Assert.False (view.HasFocus);
  48. }
  49. [Fact]
  50. public void CanFocus_Set_True_Sets_HasFocus_To_True ()
  51. {
  52. var view = new View { };
  53. var subView = new View { };
  54. view.Add (subView);
  55. Assert.False (view.CanFocus);
  56. Assert.False (subView.CanFocus);
  57. view.SetFocus ();
  58. Assert.False (view.HasFocus);
  59. Assert.False (subView.HasFocus);
  60. view.CanFocus = true;
  61. view.SetFocus ();
  62. Assert.True (view.HasFocus);
  63. // Act
  64. subView.CanFocus = true;
  65. Assert.True (subView.HasFocus);
  66. }
  67. [Fact]
  68. public void CanFocus_Set_SubView_True_Sets_HasFocus_To_True ()
  69. {
  70. var view = new View
  71. {
  72. CanFocus = true
  73. };
  74. var subView = new View
  75. {
  76. CanFocus = false
  77. };
  78. var subSubView = new View
  79. {
  80. CanFocus = true
  81. };
  82. subView.Add (subSubView);
  83. view.Add (subView);
  84. view.SetFocus ();
  85. Assert.True (view.HasFocus);
  86. Assert.False (subView.HasFocus);
  87. Assert.False (subSubView.HasFocus);
  88. // Act
  89. subView.CanFocus = true;
  90. Assert.True (subView.HasFocus);
  91. Assert.True (subSubView.HasFocus);
  92. }
  93. [Fact]
  94. public void CanFocus_Set_SubView_True_Does_Not_Change_Focus_If_SuperView_Focused_Is_True ()
  95. {
  96. var top = new View
  97. {
  98. Id = "top",
  99. CanFocus = true
  100. };
  101. var subView = new View
  102. {
  103. Id = "subView",
  104. CanFocus = true
  105. };
  106. var subSubView = new View
  107. {
  108. Id = "subSubView",
  109. CanFocus = true
  110. };
  111. subView.Add (subSubView);
  112. var subView2 = new View
  113. {
  114. Id = "subView2",
  115. CanFocus = false
  116. };
  117. top.Add (subView, subView2);
  118. top.SetFocus ();
  119. Assert.True (top.HasFocus);
  120. Assert.Equal (subView, top.Focused);
  121. Assert.True (subView.HasFocus);
  122. Assert.True (subSubView.HasFocus);
  123. // Act
  124. subView2.CanFocus = true;
  125. Assert.False (subView2.HasFocus);
  126. Assert.True (subView.HasFocus);
  127. Assert.True (subSubView.HasFocus);
  128. }
  129. [Fact]
  130. public void CanFocus_Set_False_Sets_HasFocus_To_False ()
  131. {
  132. var view = new View { CanFocus = true };
  133. var view2 = new View { CanFocus = true };
  134. view2.Add (view);
  135. Assert.True (view.CanFocus);
  136. view.SetFocus ();
  137. Assert.True (view.HasFocus);
  138. view.CanFocus = false;
  139. Assert.False (view.CanFocus);
  140. Assert.False (view.HasFocus);
  141. }
  142. // TODO: Figure out what this test is supposed to be testing
  143. [Fact]
  144. public void CanFocus_Faced_With_Container ()
  145. {
  146. var t = new Toplevel ();
  147. var w = new Window ();
  148. var f = new FrameView ();
  149. var v = new View { CanFocus = true };
  150. f.Add (v);
  151. w.Add (f);
  152. t.Add (w);
  153. Assert.True (t.CanFocus);
  154. Assert.True (w.CanFocus);
  155. Assert.True (f.CanFocus);
  156. Assert.True (v.CanFocus);
  157. f.CanFocus = false;
  158. Assert.False (f.CanFocus);
  159. Assert.True (v.CanFocus);
  160. v.CanFocus = false;
  161. Assert.False (f.CanFocus);
  162. Assert.False (v.CanFocus);
  163. v.CanFocus = true;
  164. Assert.False (f.CanFocus);
  165. Assert.True (v.CanFocus);
  166. }
  167. // TODO: Figure out what this test is supposed to be testing
  168. [Fact]
  169. public void CanFocus_Faced_With_Container_Before_Run ()
  170. {
  171. Application.Init (new FakeDriver ());
  172. Toplevel t = new ();
  173. var w = new Window ();
  174. var f = new FrameView ();
  175. var v = new View { CanFocus = true };
  176. f.Add (v);
  177. w.Add (f);
  178. t.Add (w);
  179. Assert.True (t.CanFocus);
  180. Assert.True (w.CanFocus);
  181. Assert.True (f.CanFocus);
  182. Assert.True (v.CanFocus);
  183. f.CanFocus = false;
  184. Assert.False (f.CanFocus);
  185. Assert.True (v.CanFocus);
  186. v.CanFocus = false;
  187. Assert.False (f.CanFocus);
  188. Assert.False (v.CanFocus);
  189. v.CanFocus = true;
  190. Assert.False (f.CanFocus);
  191. Assert.True (v.CanFocus);
  192. Application.Iteration += (s, a) => Application.RequestStop ();
  193. Application.Run (t);
  194. t.Dispose ();
  195. Application.Shutdown ();
  196. }
  197. //[Fact]
  198. //public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  199. //{
  200. // var r = new View ();
  201. // var v1 = new View { Text = "1" };
  202. // var v2 = new View { Text = "2" };
  203. // var v3 = new View { Text = "3" };
  204. // r.Add (v1, v2, v3);
  205. // v2.CanFocus = true;
  206. // Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  207. // Assert.Equal (0, v2.TabIndex);
  208. // Assert.Equal (TabBehavior.TabStop, v2.TabStop);
  209. // v1.CanFocus = true;
  210. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  211. // Assert.Equal (1, v1.TabIndex);
  212. // Assert.Equal (TabBehavior.TabStop, v1.TabStop);
  213. // v1.TabIndex = 2;
  214. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  215. // Assert.Equal (1, v1.TabIndex);
  216. // v3.CanFocus = true;
  217. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  218. // Assert.Equal (1, v1.TabIndex);
  219. // Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  220. // Assert.Equal (2, v3.TabIndex);
  221. // Assert.Equal (TabBehavior.TabStop, v3.TabStop);
  222. // v2.CanFocus = false;
  223. // Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  224. // Assert.Equal (1, v1.TabIndex);
  225. // Assert.Equal (TabBehavior.TabStop, v1.TabStop);
  226. // Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex); // TabIndex is not changed
  227. // Assert.NotEqual (-1, v2.TabIndex);
  228. // Assert.Equal (TabBehavior.TabStop, v2.TabStop); // TabStop is not changed
  229. // Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  230. // Assert.Equal (2, v3.TabIndex);
  231. // Assert.Equal (TabBehavior.TabStop, v3.TabStop);
  232. // r.Dispose ();
  233. //}
  234. [Fact]
  235. public void CanFocus_True_Focuses ()
  236. {
  237. View view = new ()
  238. {
  239. Id = "view"
  240. };
  241. View superView = new ()
  242. {
  243. Id = "superView",
  244. CanFocus = true
  245. };
  246. superView.Add (view);
  247. superView.SetFocus ();
  248. Assert.True (superView.HasFocus);
  249. Assert.NotEqual (view, superView.Focused);
  250. view.CanFocus = true;
  251. Assert.True (superView.HasFocus);
  252. Assert.Equal (view, superView.Focused);
  253. Assert.True (view.HasFocus);
  254. view.CanFocus = false;
  255. Assert.True (superView.HasFocus);
  256. Assert.NotEqual (view, superView.Focused);
  257. Assert.False (view.HasFocus);
  258. }
  259. [Fact]
  260. public void CanFocus_Set_True_Get_AdvanceFocus_Works ()
  261. {
  262. Label label = new () { Text = "label" };
  263. View view = new () { Text = "view", CanFocus = true };
  264. Application.Navigation = new ();
  265. Application.Current = new ();
  266. Application.Current.Add (label, view);
  267. Application.Current.SetFocus ();
  268. Assert.Equal (view, Application.Navigation.GetFocused());
  269. Assert.False (label.CanFocus);
  270. Assert.False (label.HasFocus);
  271. Assert.True (view.CanFocus);
  272. Assert.True (view.HasFocus);
  273. Assert.False (Application.Navigation.AdvanceFocus (NavigationDirection.Forward, null));
  274. Assert.False (label.HasFocus);
  275. Assert.True (view.HasFocus);
  276. // Set label CanFocus to true
  277. label.CanFocus = true;
  278. Assert.False (label.HasFocus);
  279. Assert.True (view.HasFocus);
  280. // label can now be focused, so AdvanceFocus should move to it.
  281. Assert.True (Application.Navigation.AdvanceFocus (NavigationDirection.Forward, null));
  282. Assert.True (label.HasFocus);
  283. Assert.False (view.HasFocus);
  284. // Move back to view
  285. view.SetFocus ();
  286. Assert.False (label.HasFocus);
  287. Assert.True (view.HasFocus);
  288. Assert.True (Application.OnKeyDown (Key.Tab));
  289. Assert.True (label.HasFocus);
  290. Assert.False (view.HasFocus);
  291. Application.Current.Dispose ();
  292. Application.ResetState ();
  293. }
  294. #if V2_NEW_FOCUS_IMPL // Bogus test - depends on auto CanFocus behavior
  295. [Fact]
  296. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  297. {
  298. Application.Init (new FakeDriver ());
  299. Toplevel t = new ();
  300. var w = new Window ();
  301. var f = new FrameView ();
  302. var v1 = new View { CanFocus = true };
  303. var v2 = new View { CanFocus = true };
  304. f.Add (v1, v2);
  305. w.Add (f);
  306. t.Add (w);
  307. t.Ready += (s, e) =>
  308. {
  309. Assert.True (t.CanFocus);
  310. Assert.True (w.CanFocus);
  311. Assert.True (f.CanFocus);
  312. Assert.True (v1.CanFocus);
  313. Assert.True (v2.CanFocus);
  314. w.CanFocus = false;
  315. Assert.False (w.CanFocus);
  316. Assert.False (f.CanFocus);
  317. Assert.False (v1.CanFocus);
  318. Assert.False (v2.CanFocus);
  319. };
  320. Application.Iteration += (s, a) => Application.RequestStop ();
  321. Application.Run (t);
  322. t.Dispose ();
  323. Application.Shutdown ();
  324. }
  325. #endif
  326. #if V2_NEW_FOCUS_IMPL // Bogus test - depends on auto CanFocus behavior
  327. [Fact]
  328. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  329. {
  330. Application.Init (new FakeDriver ());
  331. Toplevel t = new ();
  332. var w = new Window ();
  333. var f = new FrameView ();
  334. var v1 = new View ();
  335. var v2 = new View { CanFocus = true };
  336. f.Add (v1, v2);
  337. w.Add (f);
  338. t.Add (w);
  339. t.Ready += (s, e) =>
  340. {
  341. Assert.True (t.CanFocus);
  342. Assert.True (w.CanFocus);
  343. Assert.True (f.CanFocus);
  344. Assert.False (v1.CanFocus);
  345. Assert.True (v2.CanFocus);
  346. w.CanFocus = false;
  347. Assert.False (w.CanFocus);
  348. Assert.False (f.CanFocus);
  349. Assert.False (v1.CanFocus);
  350. Assert.False (v2.CanFocus);
  351. w.CanFocus = true;
  352. Assert.True (w.CanFocus);
  353. Assert.True (f.CanFocus);
  354. Assert.False (v1.CanFocus);
  355. Assert.True (v2.CanFocus);
  356. };
  357. Application.Iteration += (s, a) => Application.RequestStop ();
  358. Application.Run (t);
  359. t.Dispose ();
  360. Application.Shutdown ();
  361. }
  362. #endif
  363. #if V2_NEW_FOCUS_IMPL // Bogus test - depends on auto CanFocus behavior
  364. [Fact]
  365. public void CanFocus_Faced_With_Container_After_Run ()
  366. {
  367. Application.Init (new FakeDriver ());
  368. Toplevel t = new ();
  369. var w = new Window ();
  370. var f = new FrameView ();
  371. var v = new View { CanFocus = true };
  372. f.Add (v);
  373. w.Add (f);
  374. t.Add (w);
  375. t.Ready += (s, e) =>
  376. {
  377. Assert.True (t.CanFocus);
  378. Assert.True (w.CanFocus);
  379. Assert.True (f.CanFocus);
  380. Assert.True (v.CanFocus);
  381. f.CanFocus = false;
  382. Assert.False (f.CanFocus);
  383. Assert.False (v.CanFocus);
  384. v.CanFocus = false;
  385. Assert.False (f.CanFocus);
  386. Assert.False (v.CanFocus);
  387. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  388. Assert.False (f.CanFocus);
  389. Assert.False (v.CanFocus);
  390. f.CanFocus = true;
  391. Assert.True (f.CanFocus);
  392. Assert.True (v.CanFocus);
  393. };
  394. Application.Iteration += (s, a) => Application.RequestStop ();
  395. Application.Run (t);
  396. t.Dispose ();
  397. Application.Shutdown ();
  398. }
  399. #endif
  400. #if V2_NEW_FOCUS_IMPL
  401. [Fact]
  402. [AutoInitShutdown]
  403. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  404. {
  405. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  406. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  407. win1.Add (view1);
  408. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  409. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  410. win2.Add (view2);
  411. var top = new Toplevel ();
  412. top.Add (win1, win2);
  413. Application.Begin (top);
  414. Assert.True (view1.CanFocus);
  415. Assert.True (view1.HasFocus);
  416. Assert.True (view2.CanFocus);
  417. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  418. Assert.True (Application.OnKeyDown (Key.F6));
  419. Assert.True (view1.CanFocus);
  420. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  421. Assert.True (view2.CanFocus);
  422. Assert.True (view2.HasFocus);
  423. Assert.True (Application.OnKeyDown (Key.F6));
  424. Assert.True (view1.CanFocus);
  425. Assert.True (view1.HasFocus);
  426. Assert.True (view2.CanFocus);
  427. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  428. view1.CanFocus = false;
  429. Assert.False (view1.CanFocus);
  430. Assert.False (view1.HasFocus);
  431. Assert.True (view2.CanFocus);
  432. Assert.True (view2.HasFocus);
  433. Assert.Equal (win2, Application.Current.GetFocused ());
  434. Assert.Equal (view2, Application.Current.GetMostFocused ());
  435. top.Dispose ();
  436. }
  437. [Fact]
  438. [AutoInitShutdown]
  439. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  440. {
  441. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  442. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  443. win1.Add (view1);
  444. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  445. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  446. win2.Add (view2);
  447. var top = new Toplevel ();
  448. top.Add (win1, win2);
  449. Application.Begin (top);
  450. Assert.True (view1.CanFocus);
  451. Assert.True (view1.HasFocus);
  452. Assert.True (view2.CanFocus);
  453. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  454. Assert.True (Application.OnKeyDown (Key.F6));
  455. Assert.True (view1.CanFocus);
  456. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  457. Assert.True (view2.CanFocus);
  458. Assert.True (view2.HasFocus);
  459. Assert.True (Application.OnKeyDown (Key.F6));
  460. Assert.True (view1.CanFocus);
  461. Assert.True (view1.HasFocus);
  462. Assert.True (view2.CanFocus);
  463. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  464. win1.CanFocus = false;
  465. Assert.False (view1.CanFocus);
  466. Assert.False (view1.HasFocus);
  467. Assert.False (win1.CanFocus);
  468. Assert.False (win1.HasFocus);
  469. Assert.True (view2.CanFocus);
  470. Assert.True (view2.HasFocus);
  471. Assert.Equal (win2, Application.Current.GetFocused ());
  472. Assert.Equal (view2, Application.Current.GetMostFocused ());
  473. top.Dispose ();
  474. }
  475. [Fact]
  476. [AutoInitShutdown]
  477. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  478. {
  479. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  480. var view12 = new View
  481. {
  482. Id = "view12",
  483. Y = 5,
  484. Width = 10,
  485. Height = 1,
  486. CanFocus = true
  487. };
  488. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  489. win1.Add (view1, view12);
  490. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  491. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  492. win2.Add (view2);
  493. var top = new Toplevel ();
  494. top.Add (win1, win2);
  495. Application.Begin (top);
  496. Assert.True (view1.CanFocus);
  497. Assert.True (view1.HasFocus);
  498. Assert.True (view2.CanFocus);
  499. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  500. Assert.True (Application.OnKeyDown (Key.F6)); // move to win2
  501. Assert.True (view1.CanFocus);
  502. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  503. Assert.True (view2.CanFocus);
  504. Assert.True (view2.HasFocus);
  505. Assert.True (Application.OnKeyDown (Key.F6));
  506. Assert.True (view1.CanFocus);
  507. Assert.True (view1.HasFocus);
  508. Assert.True (view2.CanFocus);
  509. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  510. view1.CanFocus = false;
  511. Assert.False (view1.CanFocus);
  512. Assert.False (view1.HasFocus);
  513. Assert.True (view2.CanFocus);
  514. Assert.False (view2.HasFocus);
  515. Assert.Equal (win1, Application.Current.GetFocused ());
  516. Assert.Equal (view12, Application.Current.GetMostFocused ());
  517. top.Dispose ();
  518. }
  519. #endif
  520. [Fact (Skip = "Causes crash on Ubuntu in Github Action. Bogus test anyway.")]
  521. public void WindowDispose_CanFocusProblem ()
  522. {
  523. // Arrange
  524. Application.Init ();
  525. using var top = new Toplevel ();
  526. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  527. using var window = new Window ();
  528. top.Add (window);
  529. window.Add (view);
  530. // Act
  531. RunState rs = Application.Begin (top);
  532. Application.End (rs);
  533. top.Dispose ();
  534. Application.Shutdown ();
  535. // Assert does Not throw NullReferenceException
  536. top.SetFocus ();
  537. }
  538. }