CanFocusTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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.Current = new ();
  265. Application.Current.Add (label, view);
  266. Application.Current.SetFocus ();
  267. Assert.Equal (view, Application.Current.MostFocused);
  268. Assert.False (label.CanFocus);
  269. Assert.False (label.HasFocus);
  270. Assert.True (view.CanFocus);
  271. Assert.True (view.HasFocus);
  272. Assert.False (Application.Current.AdvanceFocus (NavigationDirection.Forward, null));
  273. Assert.False (label.HasFocus);
  274. Assert.True (view.HasFocus);
  275. // Set label CanFocus to true
  276. label.CanFocus = true;
  277. Assert.False (label.HasFocus);
  278. Assert.True (view.HasFocus);
  279. // label can now be focused, so AdvanceFocus should move to it.
  280. Assert.True (Application.Current.AdvanceFocus (NavigationDirection.Forward, null));
  281. Assert.True (label.HasFocus);
  282. Assert.False (view.HasFocus);
  283. // Move back to view
  284. view.SetFocus ();
  285. Assert.False (label.HasFocus);
  286. Assert.True (view.HasFocus);
  287. Assert.True (Application.OnKeyDown (Key.Tab));
  288. Assert.True (label.HasFocus);
  289. Assert.False (view.HasFocus);
  290. Application.Current.Dispose ();
  291. Application.ResetState ();
  292. }
  293. #if V2_NEW_FOCUS_IMPL // Bogus test - depends on auto CanFocus behavior
  294. [Fact]
  295. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  296. {
  297. Application.Init (new FakeDriver ());
  298. Toplevel t = new ();
  299. var w = new Window ();
  300. var f = new FrameView ();
  301. var v1 = new View { CanFocus = true };
  302. var v2 = new View { CanFocus = true };
  303. f.Add (v1, v2);
  304. w.Add (f);
  305. t.Add (w);
  306. t.Ready += (s, e) =>
  307. {
  308. Assert.True (t.CanFocus);
  309. Assert.True (w.CanFocus);
  310. Assert.True (f.CanFocus);
  311. Assert.True (v1.CanFocus);
  312. Assert.True (v2.CanFocus);
  313. w.CanFocus = false;
  314. Assert.False (w.CanFocus);
  315. Assert.False (f.CanFocus);
  316. Assert.False (v1.CanFocus);
  317. Assert.False (v2.CanFocus);
  318. };
  319. Application.Iteration += (s, a) => Application.RequestStop ();
  320. Application.Run (t);
  321. t.Dispose ();
  322. Application.Shutdown ();
  323. }
  324. #endif
  325. #if V2_NEW_FOCUS_IMPL // Bogus test - depends on auto CanFocus behavior
  326. [Fact]
  327. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  328. {
  329. Application.Init (new FakeDriver ());
  330. Toplevel t = new ();
  331. var w = new Window ();
  332. var f = new FrameView ();
  333. var v1 = new View ();
  334. var v2 = new View { CanFocus = true };
  335. f.Add (v1, v2);
  336. w.Add (f);
  337. t.Add (w);
  338. t.Ready += (s, e) =>
  339. {
  340. Assert.True (t.CanFocus);
  341. Assert.True (w.CanFocus);
  342. Assert.True (f.CanFocus);
  343. Assert.False (v1.CanFocus);
  344. Assert.True (v2.CanFocus);
  345. w.CanFocus = false;
  346. Assert.False (w.CanFocus);
  347. Assert.False (f.CanFocus);
  348. Assert.False (v1.CanFocus);
  349. Assert.False (v2.CanFocus);
  350. w.CanFocus = true;
  351. Assert.True (w.CanFocus);
  352. Assert.True (f.CanFocus);
  353. Assert.False (v1.CanFocus);
  354. Assert.True (v2.CanFocus);
  355. };
  356. Application.Iteration += (s, a) => Application.RequestStop ();
  357. Application.Run (t);
  358. t.Dispose ();
  359. Application.Shutdown ();
  360. }
  361. #endif
  362. #if V2_NEW_FOCUS_IMPL // Bogus test - depends on auto CanFocus behavior
  363. [Fact]
  364. public void CanFocus_Faced_With_Container_After_Run ()
  365. {
  366. Application.Init (new FakeDriver ());
  367. Toplevel t = new ();
  368. var w = new Window ();
  369. var f = new FrameView ();
  370. var v = new View { CanFocus = true };
  371. f.Add (v);
  372. w.Add (f);
  373. t.Add (w);
  374. t.Ready += (s, e) =>
  375. {
  376. Assert.True (t.CanFocus);
  377. Assert.True (w.CanFocus);
  378. Assert.True (f.CanFocus);
  379. Assert.True (v.CanFocus);
  380. f.CanFocus = false;
  381. Assert.False (f.CanFocus);
  382. Assert.False (v.CanFocus);
  383. v.CanFocus = false;
  384. Assert.False (f.CanFocus);
  385. Assert.False (v.CanFocus);
  386. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  387. Assert.False (f.CanFocus);
  388. Assert.False (v.CanFocus);
  389. f.CanFocus = true;
  390. Assert.True (f.CanFocus);
  391. Assert.True (v.CanFocus);
  392. };
  393. Application.Iteration += (s, a) => Application.RequestStop ();
  394. Application.Run (t);
  395. t.Dispose ();
  396. Application.Shutdown ();
  397. }
  398. #endif
  399. #if V2_NEW_FOCUS_IMPL
  400. [Fact]
  401. [AutoInitShutdown]
  402. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  403. {
  404. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  405. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  406. win1.Add (view1);
  407. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  408. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  409. win2.Add (view2);
  410. var top = new Toplevel ();
  411. top.Add (win1, win2);
  412. Application.Begin (top);
  413. Assert.True (view1.CanFocus);
  414. Assert.True (view1.HasFocus);
  415. Assert.True (view2.CanFocus);
  416. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  417. Assert.True (Application.OnKeyDown (Key.F6));
  418. Assert.True (view1.CanFocus);
  419. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  420. Assert.True (view2.CanFocus);
  421. Assert.True (view2.HasFocus);
  422. Assert.True (Application.OnKeyDown (Key.F6));
  423. Assert.True (view1.CanFocus);
  424. Assert.True (view1.HasFocus);
  425. Assert.True (view2.CanFocus);
  426. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  427. view1.CanFocus = false;
  428. Assert.False (view1.CanFocus);
  429. Assert.False (view1.HasFocus);
  430. Assert.True (view2.CanFocus);
  431. Assert.True (view2.HasFocus);
  432. Assert.Equal (win2, Application.Current.GetFocused ());
  433. Assert.Equal (view2, Application.Current.GetMostFocused ());
  434. top.Dispose ();
  435. }
  436. [Fact]
  437. [AutoInitShutdown]
  438. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  439. {
  440. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  441. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  442. win1.Add (view1);
  443. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  444. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  445. win2.Add (view2);
  446. var top = new Toplevel ();
  447. top.Add (win1, win2);
  448. Application.Begin (top);
  449. Assert.True (view1.CanFocus);
  450. Assert.True (view1.HasFocus);
  451. Assert.True (view2.CanFocus);
  452. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  453. Assert.True (Application.OnKeyDown (Key.F6));
  454. Assert.True (view1.CanFocus);
  455. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  456. Assert.True (view2.CanFocus);
  457. Assert.True (view2.HasFocus);
  458. Assert.True (Application.OnKeyDown (Key.F6));
  459. Assert.True (view1.CanFocus);
  460. Assert.True (view1.HasFocus);
  461. Assert.True (view2.CanFocus);
  462. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  463. win1.CanFocus = false;
  464. Assert.False (view1.CanFocus);
  465. Assert.False (view1.HasFocus);
  466. Assert.False (win1.CanFocus);
  467. Assert.False (win1.HasFocus);
  468. Assert.True (view2.CanFocus);
  469. Assert.True (view2.HasFocus);
  470. Assert.Equal (win2, Application.Current.GetFocused ());
  471. Assert.Equal (view2, Application.Current.GetMostFocused ());
  472. top.Dispose ();
  473. }
  474. [Fact]
  475. [AutoInitShutdown]
  476. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  477. {
  478. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  479. var view12 = new View
  480. {
  481. Id = "view12",
  482. Y = 5,
  483. Width = 10,
  484. Height = 1,
  485. CanFocus = true
  486. };
  487. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  488. win1.Add (view1, view12);
  489. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  490. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  491. win2.Add (view2);
  492. var top = new Toplevel ();
  493. top.Add (win1, win2);
  494. Application.Begin (top);
  495. Assert.True (view1.CanFocus);
  496. Assert.True (view1.HasFocus);
  497. Assert.True (view2.CanFocus);
  498. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  499. Assert.True (Application.OnKeyDown (Key.F6)); // move to win2
  500. Assert.True (view1.CanFocus);
  501. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  502. Assert.True (view2.CanFocus);
  503. Assert.True (view2.HasFocus);
  504. Assert.True (Application.OnKeyDown (Key.F6));
  505. Assert.True (view1.CanFocus);
  506. Assert.True (view1.HasFocus);
  507. Assert.True (view2.CanFocus);
  508. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  509. view1.CanFocus = false;
  510. Assert.False (view1.CanFocus);
  511. Assert.False (view1.HasFocus);
  512. Assert.True (view2.CanFocus);
  513. Assert.False (view2.HasFocus);
  514. Assert.Equal (win1, Application.Current.GetFocused ());
  515. Assert.Equal (view12, Application.Current.GetMostFocused ());
  516. top.Dispose ();
  517. }
  518. #endif
  519. [Fact (Skip = "Causes crash on Ubuntu in Github Action. Bogus test anyway.")]
  520. public void WindowDispose_CanFocusProblem ()
  521. {
  522. // Arrange
  523. Application.Init ();
  524. using var top = new Toplevel ();
  525. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  526. using var window = new Window ();
  527. top.Add (window);
  528. window.Add (view);
  529. // Act
  530. RunState rs = Application.Begin (top);
  531. Application.End (rs);
  532. top.Dispose ();
  533. Application.Shutdown ();
  534. // Assert does Not throw NullReferenceException
  535. top.SetFocus ();
  536. }
  537. }