NavigationTests.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class NavigationTests (ITestOutputHelper output) : TestsAllViews
  4. {
  5. [Fact]
  6. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  7. {
  8. var r = new View ();
  9. var v1 = new View { CanFocus = true };
  10. var v2 = new View { CanFocus = true };
  11. var v3 = new View { CanFocus = true };
  12. r.Add (v1, v2, v3);
  13. r.BringSubviewForward (v1);
  14. Assert.True (r.Subviews.IndexOf (v1) == 1);
  15. Assert.True (r.Subviews.IndexOf (v2) == 0);
  16. Assert.True (r.Subviews.IndexOf (v3) == 2);
  17. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  18. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  19. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  20. r.Dispose ();
  21. }
  22. [Fact]
  23. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  24. {
  25. var r = new View ();
  26. var v1 = new View { CanFocus = true };
  27. var v2 = new View { CanFocus = true };
  28. var v3 = new View { CanFocus = true };
  29. r.Add (v1, v2, v3);
  30. r.BringSubviewToFront (v1);
  31. Assert.True (r.Subviews.IndexOf (v1) == 2);
  32. Assert.True (r.Subviews.IndexOf (v2) == 0);
  33. Assert.True (r.Subviews.IndexOf (v3) == 1);
  34. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  35. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  36. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  37. r.Dispose ();
  38. }
  39. [Fact]
  40. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  41. {
  42. Application.Init (new FakeDriver ());
  43. Toplevel t = new ();
  44. var w = new Window ();
  45. var f = new FrameView ();
  46. var v1 = new View { CanFocus = true };
  47. var v2 = new View { CanFocus = true };
  48. f.Add (v1, v2);
  49. w.Add (f);
  50. t.Add (w);
  51. t.Ready += (s, e) =>
  52. {
  53. Assert.True (t.CanFocus);
  54. Assert.True (w.CanFocus);
  55. Assert.True (f.CanFocus);
  56. Assert.True (v1.CanFocus);
  57. Assert.True (v2.CanFocus);
  58. w.CanFocus = false;
  59. Assert.False (w.CanFocus);
  60. Assert.False (f.CanFocus);
  61. Assert.False (v1.CanFocus);
  62. Assert.False (v2.CanFocus);
  63. };
  64. Application.Iteration += (s, a) => Application.RequestStop ();
  65. Application.Run (t);
  66. t.Dispose ();
  67. Application.Shutdown ();
  68. }
  69. [Fact]
  70. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  71. {
  72. Application.Init (new FakeDriver ());
  73. Toplevel t = new ();
  74. var w = new Window ();
  75. var f = new FrameView ();
  76. var v1 = new View ();
  77. var v2 = new View { CanFocus = true };
  78. f.Add (v1, v2);
  79. w.Add (f);
  80. t.Add (w);
  81. t.Ready += (s, e) =>
  82. {
  83. Assert.True (t.CanFocus);
  84. Assert.True (w.CanFocus);
  85. Assert.True (f.CanFocus);
  86. Assert.False (v1.CanFocus);
  87. Assert.True (v2.CanFocus);
  88. w.CanFocus = false;
  89. Assert.False (w.CanFocus);
  90. Assert.False (f.CanFocus);
  91. Assert.False (v1.CanFocus);
  92. Assert.False (v2.CanFocus);
  93. w.CanFocus = true;
  94. Assert.True (w.CanFocus);
  95. Assert.True (f.CanFocus);
  96. Assert.False (v1.CanFocus);
  97. Assert.True (v2.CanFocus);
  98. };
  99. Application.Iteration += (s, a) => Application.RequestStop ();
  100. Application.Run (t);
  101. t.Dispose ();
  102. Application.Shutdown ();
  103. }
  104. [Fact]
  105. [AutoInitShutdown]
  106. public void CanFocus_Faced_With_Container ()
  107. {
  108. var t = new Toplevel ();
  109. var w = new Window ();
  110. var f = new FrameView ();
  111. var v = new View { CanFocus = true };
  112. f.Add (v);
  113. w.Add (f);
  114. t.Add (w);
  115. Assert.True (t.CanFocus);
  116. Assert.True (w.CanFocus);
  117. Assert.True (f.CanFocus);
  118. Assert.True (v.CanFocus);
  119. f.CanFocus = false;
  120. Assert.False (f.CanFocus);
  121. Assert.True (v.CanFocus);
  122. v.CanFocus = false;
  123. Assert.False (f.CanFocus);
  124. Assert.False (v.CanFocus);
  125. v.CanFocus = true;
  126. Assert.False (f.CanFocus);
  127. Assert.True (v.CanFocus);
  128. t.Dispose ();
  129. }
  130. [Fact]
  131. public void CanFocus_Faced_With_Container_After_Run ()
  132. {
  133. Application.Init (new FakeDriver ());
  134. Toplevel t = new ();
  135. var w = new Window ();
  136. var f = new FrameView ();
  137. var v = new View { CanFocus = true };
  138. f.Add (v);
  139. w.Add (f);
  140. t.Add (w);
  141. t.Ready += (s, e) =>
  142. {
  143. Assert.True (t.CanFocus);
  144. Assert.True (w.CanFocus);
  145. Assert.True (f.CanFocus);
  146. Assert.True (v.CanFocus);
  147. f.CanFocus = false;
  148. Assert.False (f.CanFocus);
  149. Assert.False (v.CanFocus);
  150. v.CanFocus = false;
  151. Assert.False (f.CanFocus);
  152. Assert.False (v.CanFocus);
  153. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  154. Assert.False (f.CanFocus);
  155. Assert.False (v.CanFocus);
  156. f.CanFocus = true;
  157. Assert.True (f.CanFocus);
  158. Assert.True (v.CanFocus);
  159. };
  160. Application.Iteration += (s, a) => Application.RequestStop ();
  161. Application.Run (t);
  162. t.Dispose ();
  163. Application.Shutdown ();
  164. }
  165. [Fact]
  166. public void CanFocus_Faced_With_Container_Before_Run ()
  167. {
  168. Application.Init (new FakeDriver ());
  169. Toplevel t = new ();
  170. var w = new Window ();
  171. var f = new FrameView ();
  172. var v = new View { CanFocus = true };
  173. f.Add (v);
  174. w.Add (f);
  175. t.Add (w);
  176. Assert.True (t.CanFocus);
  177. Assert.True (w.CanFocus);
  178. Assert.True (f.CanFocus);
  179. Assert.True (v.CanFocus);
  180. f.CanFocus = false;
  181. Assert.False (f.CanFocus);
  182. Assert.True (v.CanFocus);
  183. v.CanFocus = false;
  184. Assert.False (f.CanFocus);
  185. Assert.False (v.CanFocus);
  186. v.CanFocus = true;
  187. Assert.False (f.CanFocus);
  188. Assert.True (v.CanFocus);
  189. Application.Iteration += (s, a) => Application.RequestStop ();
  190. Application.Run (t);
  191. t.Dispose ();
  192. Application.Shutdown ();
  193. }
  194. [Fact]
  195. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  196. {
  197. var r = new View ();
  198. var v1 = new View { Text = "1" };
  199. var v2 = new View { Text = "2" };
  200. var v3 = new View { Text = "3" };
  201. r.Add (v1, v2, v3);
  202. v2.CanFocus = true;
  203. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  204. Assert.Equal (0, v2.TabIndex);
  205. Assert.True (v2.TabStop);
  206. v1.CanFocus = true;
  207. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  208. Assert.Equal (1, v1.TabIndex);
  209. Assert.True (v1.TabStop);
  210. v1.TabIndex = 2;
  211. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  212. Assert.Equal (1, v1.TabIndex);
  213. v3.CanFocus = true;
  214. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  215. Assert.Equal (1, v1.TabIndex);
  216. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  217. Assert.Equal (2, v3.TabIndex);
  218. Assert.True (v3.TabStop);
  219. v2.CanFocus = false;
  220. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  221. Assert.Equal (1, v1.TabIndex);
  222. Assert.True (v1.TabStop);
  223. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  224. Assert.Equal (-1, v2.TabIndex);
  225. Assert.False (v2.TabStop);
  226. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  227. Assert.Equal (2, v3.TabIndex);
  228. Assert.True (v3.TabStop);
  229. r.Dispose ();
  230. }
  231. [Fact]
  232. [AutoInitShutdown]
  233. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  234. {
  235. var view = new View { CanFocus = true };
  236. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  237. win.Add (view);
  238. var top = new Toplevel ();
  239. top.Add (win);
  240. Application.Begin (top);
  241. Assert.True (view.CanFocus);
  242. Assert.True (view.HasFocus);
  243. view.CanFocus = false;
  244. Assert.False (view.CanFocus);
  245. Assert.False (view.HasFocus);
  246. Assert.Null (Application.Current.Focused);
  247. Assert.Null (Application.Current.MostFocused);
  248. top.Dispose ();
  249. }
  250. [Fact]
  251. [AutoInitShutdown]
  252. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  253. {
  254. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  255. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  256. win1.Add (view1);
  257. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  258. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  259. win2.Add (view2);
  260. var top = new Toplevel ();
  261. top.Add (win1, win2);
  262. Application.Begin (top);
  263. Assert.True (view1.CanFocus);
  264. Assert.True (view1.HasFocus);
  265. Assert.True (view2.CanFocus);
  266. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  267. Assert.True (Application.OnKeyDown (Key.Tab));
  268. Assert.True (view1.CanFocus);
  269. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  270. Assert.True (view2.CanFocus);
  271. Assert.True (view2.HasFocus);
  272. Assert.True (Application.OnKeyDown (Key.Tab));
  273. Assert.True (view1.CanFocus);
  274. Assert.True (view1.HasFocus);
  275. Assert.True (view2.CanFocus);
  276. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  277. view1.CanFocus = false;
  278. Assert.False (view1.CanFocus);
  279. Assert.False (view1.HasFocus);
  280. Assert.True (view2.CanFocus);
  281. Assert.True (view2.HasFocus);
  282. Assert.Equal (win2, Application.Current.Focused);
  283. Assert.Equal (view2, Application.Current.MostFocused);
  284. top.Dispose ();
  285. }
  286. [Fact]
  287. [AutoInitShutdown]
  288. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  289. {
  290. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  291. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  292. win1.Add (view1);
  293. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  294. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  295. win2.Add (view2);
  296. var top = new Toplevel ();
  297. top.Add (win1, win2);
  298. Application.Begin (top);
  299. Assert.True (view1.CanFocus);
  300. Assert.True (view1.HasFocus);
  301. Assert.True (view2.CanFocus);
  302. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  303. Assert.True (Application.OnKeyDown (Key.Tab.WithCtrl));
  304. Assert.True (view1.CanFocus);
  305. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  306. Assert.True (view2.CanFocus);
  307. Assert.True (view2.HasFocus);
  308. Assert.True (Application.OnKeyDown (Key.Tab.WithCtrl));
  309. Assert.True (view1.CanFocus);
  310. Assert.True (view1.HasFocus);
  311. Assert.True (view2.CanFocus);
  312. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  313. win1.CanFocus = false;
  314. Assert.False (view1.CanFocus);
  315. Assert.False (view1.HasFocus);
  316. Assert.False (win1.CanFocus);
  317. Assert.False (win1.HasFocus);
  318. Assert.True (view2.CanFocus);
  319. Assert.True (view2.HasFocus);
  320. Assert.Equal (win2, Application.Current.Focused);
  321. Assert.Equal (view2, Application.Current.MostFocused);
  322. top.Dispose ();
  323. }
  324. [Fact]
  325. [AutoInitShutdown]
  326. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  327. {
  328. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  329. var view12 = new View
  330. {
  331. Id = "view12",
  332. Y = 5,
  333. Width = 10,
  334. Height = 1,
  335. CanFocus = true
  336. };
  337. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  338. win1.Add (view1, view12);
  339. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  340. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  341. win2.Add (view2);
  342. var top = new Toplevel ();
  343. top.Add (win1, win2);
  344. Application.Begin (top);
  345. Assert.True (view1.CanFocus);
  346. Assert.True (view1.HasFocus);
  347. Assert.True (view2.CanFocus);
  348. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  349. Assert.True (Application.OnKeyDown (Key.Tab.WithCtrl));
  350. Assert.True (Application.OnKeyDown (Key.Tab.WithCtrl));
  351. Assert.True (view1.CanFocus);
  352. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  353. Assert.True (view2.CanFocus);
  354. Assert.True (view2.HasFocus);
  355. Assert.True (Application.OnKeyDown (Key.Tab.WithCtrl));
  356. Assert.True (view1.CanFocus);
  357. Assert.True (view1.HasFocus);
  358. Assert.True (view2.CanFocus);
  359. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  360. view1.CanFocus = false;
  361. Assert.False (view1.CanFocus);
  362. Assert.False (view1.HasFocus);
  363. Assert.True (view2.CanFocus);
  364. Assert.False (view2.HasFocus);
  365. Assert.Equal (win1, Application.Current.Focused);
  366. Assert.Equal (view12, Application.Current.MostFocused);
  367. top.Dispose ();
  368. }
  369. [Fact]
  370. public void Enabled_False_Sets_HasFocus_To_False ()
  371. {
  372. var wasClicked = false;
  373. var view = new Button { Text = "Click Me" };
  374. view.Accept += (s, e) => wasClicked = !wasClicked;
  375. view.NewKeyDownEvent (Key.Space);
  376. Assert.True (wasClicked);
  377. view.NewMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  378. Assert.False (wasClicked);
  379. Assert.True (view.Enabled);
  380. Assert.True (view.CanFocus);
  381. Assert.True (view.HasFocus);
  382. view.Enabled = false;
  383. view.NewKeyDownEvent (Key.Space);
  384. Assert.False (wasClicked);
  385. view.NewMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  386. Assert.False (wasClicked);
  387. Assert.False (view.Enabled);
  388. Assert.True (view.CanFocus);
  389. Assert.False (view.HasFocus);
  390. view.SetFocus ();
  391. Assert.False (view.HasFocus);
  392. }
  393. [Fact]
  394. [AutoInitShutdown]
  395. public void Enabled_Sets_Also_Sets_Subviews ()
  396. {
  397. var wasClicked = false;
  398. var button = new Button { Text = "Click Me" };
  399. button.IsDefault = true;
  400. button.Accept += (s, e) => wasClicked = !wasClicked;
  401. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  402. win.Add (button);
  403. var top = new Toplevel ();
  404. top.Add (win);
  405. var iterations = 0;
  406. Application.Iteration += (s, a) =>
  407. {
  408. iterations++;
  409. win.NewKeyDownEvent (Key.Enter);
  410. Assert.True (wasClicked);
  411. button.NewMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  412. Assert.False (wasClicked);
  413. Assert.True (button.Enabled);
  414. Assert.True (button.CanFocus);
  415. Assert.True (button.HasFocus);
  416. Assert.True (win.Enabled);
  417. Assert.True (win.CanFocus);
  418. Assert.True (win.HasFocus);
  419. win.Enabled = false;
  420. button.NewKeyDownEvent (Key.Enter);
  421. Assert.False (wasClicked);
  422. button.NewMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  423. Assert.False (wasClicked);
  424. Assert.False (button.Enabled);
  425. Assert.True (button.CanFocus);
  426. Assert.False (button.HasFocus);
  427. Assert.False (win.Enabled);
  428. Assert.True (win.CanFocus);
  429. Assert.False (win.HasFocus);
  430. button.SetFocus ();
  431. Assert.False (button.HasFocus);
  432. Assert.False (win.HasFocus);
  433. win.SetFocus ();
  434. Assert.False (button.HasFocus);
  435. Assert.False (win.HasFocus);
  436. win.Enabled = true;
  437. win.FocusFirst ();
  438. Assert.True (button.HasFocus);
  439. Assert.True (win.HasFocus);
  440. Application.RequestStop ();
  441. };
  442. Application.Run (top);
  443. Assert.Equal (1, iterations);
  444. top.Dispose ();
  445. }
  446. [Fact]
  447. [AutoInitShutdown]
  448. public void FocusNearestView_Ensure_Focus_Ordered ()
  449. {
  450. var top = new Toplevel ();
  451. var win = new Window ();
  452. var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
  453. win.Add (winSubview);
  454. top.Add (win);
  455. var frm = new FrameView ();
  456. var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
  457. frm.Add (frmSubview);
  458. top.Add (frm);
  459. Application.Begin (top);
  460. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  461. Application.OnKeyDown (Key.Tab);
  462. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  463. Application.OnKeyDown (Key.Tab);
  464. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  465. Application.OnKeyDown (Key.Tab.WithShift);
  466. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  467. Application.OnKeyDown (Key.Tab.WithShift);
  468. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  469. top.Dispose ();
  470. }
  471. [Fact]
  472. [AutoInitShutdown]
  473. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  474. {
  475. Toplevel top1 = new ();
  476. var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  477. var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
  478. var view2 = new View
  479. {
  480. Id = "view2",
  481. Y = 1,
  482. Width = 10,
  483. Height = 5,
  484. CanFocus = true
  485. };
  486. View view3 = null;
  487. var removed = false;
  488. view2.Enter += (s, e) =>
  489. {
  490. if (!removed)
  491. {
  492. removed = true;
  493. view3 = new View { Id = "view3", Y = 1, Width = 10, Height = 5 };
  494. Application.Current.Add (view3);
  495. Application.Current.BringSubviewToFront (view3);
  496. Assert.False (view3.HasFocus);
  497. }
  498. };
  499. view2.Leave += (s, e) =>
  500. {
  501. Application.Current.Remove (view3);
  502. view3.Dispose ();
  503. view3 = null;
  504. };
  505. top2.Add (view2);
  506. top1.Add (view1, top2);
  507. Application.Begin (top1);
  508. Assert.True (top1.HasFocus);
  509. Assert.True (view1.HasFocus);
  510. Assert.False (view2.HasFocus);
  511. Assert.False (removed);
  512. Assert.Null (view3);
  513. Assert.True (Application.OnKeyDown (Key.Tab.WithCtrl));
  514. Assert.True (top1.HasFocus);
  515. Assert.False (view1.HasFocus);
  516. Assert.True (view2.HasFocus);
  517. Assert.True (removed);
  518. Assert.NotNull (view3);
  519. Exception exception =
  520. Record.Exception (() => Application.OnKeyDown (Key.Tab.WithCtrl));
  521. Assert.Null (exception);
  522. Assert.True (removed);
  523. Assert.Null (view3);
  524. top1.Dispose ();
  525. }
  526. // [Fact]
  527. // [AutoInitShutdown]
  528. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  529. // {
  530. // var sbQuiting = false;
  531. // var tfQuiting = false;
  532. // var topQuiting = false;
  533. // var sb = new StatusBar (
  534. // new Shortcut []
  535. // {
  536. // new (
  537. // KeyCode.CtrlMask | KeyCode.Q,
  538. // "Quit",
  539. // () => sbQuiting = true
  540. // )
  541. // }
  542. // );
  543. // var tf = new TextField ();
  544. // tf.KeyDown += Tf_KeyPressed;
  545. // void Tf_KeyPressed (object sender, Key obj)
  546. // {
  547. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  548. // {
  549. // obj.Handled = tfQuiting = true;
  550. // }
  551. // }
  552. // var win = new Window ();
  553. // win.Add (sb, tf);
  554. // Toplevel top = new ();
  555. // top.KeyDown += Top_KeyPress;
  556. // void Top_KeyPress (object sender, Key obj)
  557. // {
  558. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  559. // {
  560. // obj.Handled = topQuiting = true;
  561. // }
  562. // }
  563. // top.Add (win);
  564. // Application.Begin (top);
  565. // Assert.False (sbQuiting);
  566. // Assert.False (tfQuiting);
  567. // Assert.False (topQuiting);
  568. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  569. // Assert.False (sbQuiting);
  570. // Assert.True (tfQuiting);
  571. // Assert.False (topQuiting);
  572. //#if BROKE_WITH_2927
  573. // tf.KeyPressed -= Tf_KeyPress;
  574. // tfQuiting = false;
  575. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  576. // Application.MainLoop.RunIteration ();
  577. // Assert.True (sbQuiting);
  578. // Assert.False (tfQuiting);
  579. // Assert.False (topQuiting);
  580. // sb.RemoveItem (0);
  581. // sbQuiting = false;
  582. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  583. // Application.MainLoop.RunIteration ();
  584. // Assert.False (sbQuiting);
  585. // Assert.False (tfQuiting);
  586. //// This test is now invalid because `win` is focused, so it will receive the keypress
  587. // Assert.True (topQuiting);
  588. //#endif
  589. // top.Dispose ();
  590. // }
  591. // [Fact]
  592. // [AutoInitShutdown]
  593. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  594. // {
  595. // var sbQuiting = false;
  596. // var tfQuiting = false;
  597. // var sb = new StatusBar (
  598. // new Shortcut []
  599. // {
  600. // new (
  601. // KeyCode.CtrlMask | KeyCode.Q,
  602. // "~^Q~ Quit",
  603. // () => sbQuiting = true
  604. // )
  605. // }
  606. // );
  607. // var tf = new TextField ();
  608. // tf.KeyDown += Tf_KeyPressed;
  609. // void Tf_KeyPressed (object sender, Key obj)
  610. // {
  611. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  612. // {
  613. // obj.Handled = tfQuiting = true;
  614. // }
  615. // }
  616. // var win = new Window ();
  617. // win.Add (sb, tf);
  618. // Toplevel top = new ();
  619. // top.Add (win);
  620. // Application.Begin (top);
  621. // Assert.False (sbQuiting);
  622. // Assert.False (tfQuiting);
  623. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  624. // Assert.False (sbQuiting);
  625. // Assert.True (tfQuiting);
  626. // tf.KeyDown -= Tf_KeyPressed;
  627. // tfQuiting = false;
  628. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  629. // Application.MainLoop.RunIteration ();
  630. //#if BROKE_WITH_2927
  631. // Assert.True (sbQuiting);
  632. // Assert.False (tfQuiting);
  633. //#endif
  634. // top.Dispose ();
  635. // }
  636. [Fact]
  637. [SetupFakeDriver]
  638. public void Navigation_With_Null_Focused_View ()
  639. {
  640. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  641. Application.Init (new FakeDriver ());
  642. var top = new Toplevel ();
  643. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  644. // Keyboard navigation with tab
  645. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  646. Application.Iteration += (s, a) => Application.RequestStop ();
  647. Application.Run (top);
  648. top.Dispose ();
  649. Application.Shutdown ();
  650. }
  651. [Fact]
  652. [AutoInitShutdown]
  653. public void Remove_Does_Not_Change_Focus ()
  654. {
  655. var top = new Toplevel ();
  656. Assert.True (top.CanFocus);
  657. Assert.False (top.HasFocus);
  658. var container = new View { Width = 10, Height = 10 };
  659. var leave = false;
  660. container.Leave += (s, e) => leave = true;
  661. Assert.False (container.CanFocus);
  662. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  663. container.Add (child);
  664. Assert.True (container.CanFocus);
  665. Assert.False (container.HasFocus);
  666. Assert.True (child.CanFocus);
  667. Assert.False (child.HasFocus);
  668. top.Add (container);
  669. Application.Begin (top);
  670. Assert.True (top.CanFocus);
  671. Assert.True (top.HasFocus);
  672. Assert.True (container.CanFocus);
  673. Assert.True (container.HasFocus);
  674. Assert.True (child.CanFocus);
  675. Assert.True (child.HasFocus);
  676. container.Remove (child);
  677. child.Dispose ();
  678. child = null;
  679. Assert.True (top.HasFocus);
  680. Assert.True (container.CanFocus);
  681. Assert.True (container.HasFocus);
  682. Assert.Null (child);
  683. Assert.False (leave);
  684. top.Dispose ();
  685. }
  686. [Fact]
  687. [AutoInitShutdown]
  688. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  689. {
  690. Toplevel top = new ();
  691. top.BorderStyle = LineStyle.Single;
  692. var view = new View
  693. {
  694. X = 3,
  695. Y = 2,
  696. Width = 10,
  697. Height = 1,
  698. Text = "0123456789"
  699. };
  700. top.Add (view);
  701. Application.Begin (top);
  702. Assert.Equal (Application.Current, top);
  703. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  704. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  705. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  706. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  707. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  708. Assert.Equal (new Rectangle (0, 0, 20, 10), top.Frame);
  709. _ = TestHelpers.AssertDriverContentsWithFrameAre (
  710. @"
  711. ┌──────────────────┐
  712. │ │
  713. │ │
  714. │ 0123456789 │
  715. │ │
  716. │ │
  717. │ │
  718. │ │
  719. │ │
  720. └──────────────────┘",
  721. output
  722. );
  723. // top
  724. Assert.Equal (Point.Empty, top.ScreenToFrame (new (0, 0)));
  725. var screen = top.Margin.ViewportToScreen (new Point (0, 0));
  726. Assert.Equal (0, screen.X);
  727. Assert.Equal (0, screen.Y);
  728. screen = top.Border.ViewportToScreen (new Point (0, 0));
  729. Assert.Equal (0, screen.X);
  730. Assert.Equal (0, screen.Y);
  731. screen = top.Padding.ViewportToScreen (new Point (0, 0));
  732. Assert.Equal (1, screen.X);
  733. Assert.Equal (1, screen.Y);
  734. screen = top.ViewportToScreen (new Point (0, 0));
  735. Assert.Equal (1, screen.X);
  736. Assert.Equal (1, screen.Y);
  737. screen = top.ViewportToScreen (new Point (-1, -1));
  738. Assert.Equal (0, screen.X);
  739. Assert.Equal (0, screen.Y);
  740. var found = View.FindDeepestView (top, new (0, 0));
  741. Assert.Equal (top.Border, found);
  742. Assert.Equal (0, found.Frame.X);
  743. Assert.Equal (0, found.Frame.Y);
  744. Assert.Equal (new Point (3, 2), top.ScreenToFrame (new (3, 2)));
  745. screen = top.ViewportToScreen (new Point (3, 2));
  746. Assert.Equal (4, screen.X);
  747. Assert.Equal (3, screen.Y);
  748. found = View.FindDeepestView (top, new (screen.X, screen.Y));
  749. Assert.Equal (view, found);
  750. //Assert.Equal (0, found.FrameToScreen ().X);
  751. //Assert.Equal (0, found.FrameToScreen ().Y);
  752. found = View.FindDeepestView (top, new (3, 2));
  753. Assert.Equal (top, found);
  754. //Assert.Equal (3, found.FrameToScreen ().X);
  755. //Assert.Equal (2, found.FrameToScreen ().Y);
  756. Assert.Equal (new Point (13, 2), top.ScreenToFrame (new (13, 2)));
  757. screen = top.ViewportToScreen (new Point (12, 2));
  758. Assert.Equal (13, screen.X);
  759. Assert.Equal (3, screen.Y);
  760. found = View.FindDeepestView (top, new (screen.X, screen.Y));
  761. Assert.Equal (view, found);
  762. //Assert.Equal (9, found.FrameToScreen ().X);
  763. //Assert.Equal (0, found.FrameToScreen ().Y);
  764. screen = top.ViewportToScreen (new Point (13, 2));
  765. Assert.Equal (14, screen.X);
  766. Assert.Equal (3, screen.Y);
  767. found = View.FindDeepestView (top, new (13, 2));
  768. Assert.Equal (top, found);
  769. //Assert.Equal (13, found.FrameToScreen ().X);
  770. //Assert.Equal (2, found.FrameToScreen ().Y);
  771. Assert.Equal (new Point (14, 3), top.ScreenToFrame (new (14, 3)));
  772. screen = top.ViewportToScreen (new Point (14, 3));
  773. Assert.Equal (15, screen.X);
  774. Assert.Equal (4, screen.Y);
  775. found = View.FindDeepestView (top, new (14, 3));
  776. Assert.Equal (top, found);
  777. //Assert.Equal (14, found.FrameToScreen ().X);
  778. //Assert.Equal (3, found.FrameToScreen ().Y);
  779. // view
  780. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (new (0, 0)));
  781. screen = view.Margin.ViewportToScreen (new Point (-3, -2));
  782. Assert.Equal (1, screen.X);
  783. Assert.Equal (1, screen.Y);
  784. screen = view.Border.ViewportToScreen (new Point (-3, -2));
  785. Assert.Equal (1, screen.X);
  786. Assert.Equal (1, screen.Y);
  787. screen = view.Padding.ViewportToScreen (new Point (-3, -2));
  788. Assert.Equal (1, screen.X);
  789. Assert.Equal (1, screen.Y);
  790. screen = view.ViewportToScreen (new Point (-3, -2));
  791. Assert.Equal (1, screen.X);
  792. Assert.Equal (1, screen.Y);
  793. screen = view.ViewportToScreen (new Point (-4, -3));
  794. Assert.Equal (0, screen.X);
  795. Assert.Equal (0, screen.Y);
  796. found = View.FindDeepestView (top, new (0, 0));
  797. Assert.Equal (top.Border, found);
  798. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (new (3, 2)));
  799. screen = view.ViewportToScreen (new Point (0, 0));
  800. Assert.Equal (4, screen.X);
  801. Assert.Equal (3, screen.Y);
  802. found = View.FindDeepestView (top, new (4, 3));
  803. Assert.Equal (view, found);
  804. Assert.Equal (new Point (9, -1), view.ScreenToFrame (new (13, 2)));
  805. screen = view.ViewportToScreen (new Point (10, 0));
  806. Assert.Equal (14, screen.X);
  807. Assert.Equal (3, screen.Y);
  808. found = View.FindDeepestView (top, new (14, 3));
  809. Assert.Equal (top, found);
  810. Assert.Equal (new Point (10, 0), view.ScreenToFrame (new (14, 3)));
  811. screen = view.ViewportToScreen (new Point (11, 1));
  812. Assert.Equal (15, screen.X);
  813. Assert.Equal (4, screen.Y);
  814. found = View.FindDeepestView (top, new (15, 4));
  815. Assert.Equal (top, found);
  816. top.Dispose ();
  817. }
  818. [Fact]
  819. [AutoInitShutdown]
  820. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  821. {
  822. var top = new Toplevel
  823. {
  824. X = 3,
  825. Y = 2,
  826. Width = 20,
  827. Height = 10,
  828. BorderStyle = LineStyle.Single
  829. };
  830. var view = new View
  831. {
  832. X = 3,
  833. Y = 2,
  834. Width = 10,
  835. Height = 1,
  836. Text = "0123456789"
  837. };
  838. top.Add (view);
  839. Application.Begin (top);
  840. Assert.Equal (Application.Current, top);
  841. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  842. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  843. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  844. ((FakeDriver)Application.Driver!).SetBufferSize (30, 20);
  845. Assert.Equal (new Rectangle (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  846. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  847. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  848. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  849. @"
  850. ┌──────────────────┐
  851. │ │
  852. │ │
  853. │ 0123456789 │
  854. │ │
  855. │ │
  856. │ │
  857. │ │
  858. │ │
  859. └──────────────────┘",
  860. output
  861. );
  862. // mean the output started at col 3 and line 2
  863. // which result with a width of 23 and a height of 10 on the output
  864. Assert.Equal (new Rectangle (3, 2, 23, 10), frame);
  865. // top
  866. Assert.Equal (new Point (-3, -2), top.ScreenToFrame (new (0, 0)));
  867. var screen = top.Margin.ViewportToScreen (new Point (-3, -2));
  868. Assert.Equal (0, screen.X);
  869. Assert.Equal (0, screen.Y);
  870. screen = top.Border.ViewportToScreen (new Point (-3, -2));
  871. Assert.Equal (0, screen.X);
  872. Assert.Equal (0, screen.Y);
  873. screen = top.Padding.ViewportToScreen (new Point (-3, -2));
  874. Assert.Equal (1, screen.X);
  875. Assert.Equal (1, screen.Y);
  876. screen = top.ViewportToScreen (new Point (-3, -2));
  877. Assert.Equal (1, screen.X);
  878. Assert.Equal (1, screen.Y);
  879. screen = top.ViewportToScreen (new Point (-4, -3));
  880. Assert.Equal (0, screen.X);
  881. Assert.Equal (0, screen.Y);
  882. var found = View.FindDeepestView (top, new (-4, -3));
  883. Assert.Null (found);
  884. Assert.Equal (Point.Empty, top.ScreenToFrame (new (3, 2)));
  885. screen = top.ViewportToScreen (new Point (0, 0));
  886. Assert.Equal (4, screen.X);
  887. Assert.Equal (3, screen.Y);
  888. Assert.Equal (top.Border, View.FindDeepestView (top, new (3, 2)));
  889. //Assert.Equal (0, found.FrameToScreen ().X);
  890. //Assert.Equal (0, found.FrameToScreen ().Y);
  891. Assert.Equal (new Point (10, 0), top.ScreenToFrame (new (13, 2)));
  892. screen = top.ViewportToScreen (new Point (10, 0));
  893. Assert.Equal (14, screen.X);
  894. Assert.Equal (3, screen.Y);
  895. Assert.Equal (top.Border, View.FindDeepestView (top, new (13, 2)));
  896. //Assert.Equal (10, found.FrameToScreen ().X);
  897. //Assert.Equal (0, found.FrameToScreen ().Y);
  898. Assert.Equal (new Point (11, 1), top.ScreenToFrame (new (14, 3)));
  899. screen = top.ViewportToScreen (new Point (11, 1));
  900. Assert.Equal (15, screen.X);
  901. Assert.Equal (4, screen.Y);
  902. Assert.Equal (top, View.FindDeepestView (top, new (14, 3)));
  903. // view
  904. Assert.Equal (new Point (-7, -5), view.ScreenToFrame (new (0, 0)));
  905. screen = view.Margin.ViewportToScreen (new Point (-6, -4));
  906. Assert.Equal (1, screen.X);
  907. Assert.Equal (1, screen.Y);
  908. screen = view.Border.ViewportToScreen (new Point (-6, -4));
  909. Assert.Equal (1, screen.X);
  910. Assert.Equal (1, screen.Y);
  911. screen = view.Padding.ViewportToScreen (new Point (-6, -4));
  912. Assert.Equal (1, screen.X);
  913. Assert.Equal (1, screen.Y);
  914. screen = view.ViewportToScreen (new Point (-6, -4));
  915. Assert.Equal (1, screen.X);
  916. Assert.Equal (1, screen.Y);
  917. Assert.Null (View.FindDeepestView (top, new (1, 1)));
  918. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (new (3, 2)));
  919. screen = view.ViewportToScreen (new Point (-3, -2));
  920. Assert.Equal (4, screen.X);
  921. Assert.Equal (3, screen.Y);
  922. Assert.Equal (top, View.FindDeepestView (top, new (4, 3)));
  923. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (new (6, 4)));
  924. screen = view.ViewportToScreen (new Point (0, 0));
  925. Assert.Equal (7, screen.X);
  926. Assert.Equal (5, screen.Y);
  927. Assert.Equal (view, View.FindDeepestView (top, new (7, 5)));
  928. Assert.Equal (new Point (6, -1), view.ScreenToFrame (new (13, 4)));
  929. screen = view.ViewportToScreen (new Point (7, 0));
  930. Assert.Equal (14, screen.X);
  931. Assert.Equal (5, screen.Y);
  932. Assert.Equal (view, View.FindDeepestView (top, new (14, 5)));
  933. Assert.Equal (new Point (7, -2), view.ScreenToFrame (new (14, 3)));
  934. screen = view.ViewportToScreen (new Point (8, -1));
  935. Assert.Equal (15, screen.X);
  936. Assert.Equal (4, screen.Y);
  937. Assert.Equal (top, View.FindDeepestView (top, new (15, 4)));
  938. Assert.Equal (new Point (16, -2), view.ScreenToFrame (new (23, 3)));
  939. screen = view.ViewportToScreen (new Point (17, -1));
  940. Assert.Equal (24, screen.X);
  941. Assert.Equal (4, screen.Y);
  942. Assert.Null (View.FindDeepestView (top, new (24, 4)));
  943. top.Dispose ();
  944. }
  945. [Fact]
  946. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  947. {
  948. var r = new View ();
  949. var v1 = new View { CanFocus = true };
  950. var v2 = new View { CanFocus = true };
  951. var v3 = new View { CanFocus = true };
  952. r.Add (v1, v2, v3);
  953. r.SendSubviewBackwards (v3);
  954. Assert.True (r.Subviews.IndexOf (v1) == 0);
  955. Assert.True (r.Subviews.IndexOf (v2) == 2);
  956. Assert.True (r.Subviews.IndexOf (v3) == 1);
  957. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  958. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  959. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  960. r.Dispose ();
  961. }
  962. [Fact]
  963. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  964. {
  965. var r = new View ();
  966. var v1 = new View { CanFocus = true };
  967. var v2 = new View { CanFocus = true };
  968. var v3 = new View { CanFocus = true };
  969. r.Add (v1, v2, v3);
  970. r.SendSubviewToBack (v3);
  971. Assert.True (r.Subviews.IndexOf (v1) == 1);
  972. Assert.True (r.Subviews.IndexOf (v2) == 2);
  973. Assert.True (r.Subviews.IndexOf (v3) == 0);
  974. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  975. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  976. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  977. r.Dispose ();
  978. }
  979. [Fact]
  980. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  981. {
  982. var top = new Toplevel ();
  983. Assert.True (top.CanFocus);
  984. Assert.False (top.HasFocus);
  985. Exception exception = Record.Exception (top.SetFocus);
  986. Assert.Null (exception);
  987. Assert.True (top.CanFocus);
  988. Assert.True (top.HasFocus);
  989. }
  990. [Fact]
  991. [AutoInitShutdown]
  992. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  993. {
  994. var view1Leave = false;
  995. var subView1Leave = false;
  996. var subView1subView1Leave = false;
  997. Toplevel top = new ();
  998. var view1 = new View { CanFocus = true };
  999. var subView1 = new View { CanFocus = true };
  1000. var subView1subView1 = new View { CanFocus = true };
  1001. view1.Leave += (s, e) => { view1Leave = true; };
  1002. subView1.Leave += (s, e) =>
  1003. {
  1004. subView1.Remove (subView1subView1);
  1005. subView1Leave = true;
  1006. };
  1007. view1.Add (subView1);
  1008. subView1subView1.Leave += (s, e) =>
  1009. {
  1010. // This is never invoked
  1011. subView1subView1Leave = true;
  1012. };
  1013. subView1.Add (subView1subView1);
  1014. var view2 = new View { CanFocus = true };
  1015. top.Add (view1, view2);
  1016. RunState rs = Application.Begin (top);
  1017. view2.SetFocus ();
  1018. Assert.True (view1Leave);
  1019. Assert.True (subView1Leave);
  1020. Assert.False (subView1subView1Leave);
  1021. Application.End (rs);
  1022. subView1subView1.Dispose ();
  1023. top.Dispose ();
  1024. }
  1025. [Fact]
  1026. public void Subviews_TabIndexes_AreEqual ()
  1027. {
  1028. var r = new View ();
  1029. var v1 = new View { CanFocus = true };
  1030. var v2 = new View { CanFocus = true };
  1031. var v3 = new View { CanFocus = true };
  1032. r.Add (v1, v2, v3);
  1033. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1034. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1035. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1036. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1037. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1038. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  1039. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  1040. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  1041. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  1042. r.Dispose ();
  1043. }
  1044. [Fact]
  1045. public void TabIndex_Set_CanFocus_False ()
  1046. {
  1047. var r = new View ();
  1048. var v1 = new View { CanFocus = true };
  1049. var v2 = new View { CanFocus = true };
  1050. var v3 = new View { CanFocus = true };
  1051. r.Add (v1, v2, v3);
  1052. v1.CanFocus = false;
  1053. v1.TabIndex = 0;
  1054. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1055. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1056. Assert.Equal (-1, v1.TabIndex);
  1057. r.Dispose ();
  1058. }
  1059. [Fact]
  1060. public void TabIndex_Set_CanFocus_False_To_True ()
  1061. {
  1062. var r = new View ();
  1063. var v1 = new View ();
  1064. var v2 = new View { CanFocus = true };
  1065. var v3 = new View { CanFocus = true };
  1066. r.Add (v1, v2, v3);
  1067. v1.CanFocus = true;
  1068. v1.TabIndex = 1;
  1069. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1070. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1071. r.Dispose ();
  1072. }
  1073. [Fact]
  1074. public void TabIndex_Set_CanFocus_HigherValues ()
  1075. {
  1076. var r = new View ();
  1077. var v1 = new View { CanFocus = true };
  1078. var v2 = new View { CanFocus = true };
  1079. var v3 = new View { CanFocus = true };
  1080. r.Add (v1, v2, v3);
  1081. v1.TabIndex = 3;
  1082. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1083. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1084. r.Dispose ();
  1085. }
  1086. [Fact]
  1087. public void TabIndex_Set_CanFocus_LowerValues ()
  1088. {
  1089. var r = new View ();
  1090. var v1 = new View { CanFocus = true };
  1091. var v2 = new View { CanFocus = true };
  1092. var v3 = new View { CanFocus = true };
  1093. r.Add (v1, v2, v3);
  1094. v1.TabIndex = -1;
  1095. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1096. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1097. r.Dispose ();
  1098. }
  1099. [Fact]
  1100. public void TabIndex_Set_CanFocus_ValidValues ()
  1101. {
  1102. var r = new View ();
  1103. var v1 = new View { CanFocus = true };
  1104. var v2 = new View { CanFocus = true };
  1105. var v3 = new View { CanFocus = true };
  1106. r.Add (v1, v2, v3);
  1107. v1.TabIndex = 1;
  1108. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1109. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1110. v1.TabIndex = 2;
  1111. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1112. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1113. r.Dispose ();
  1114. }
  1115. [Fact]
  1116. public void TabIndex_Invert_Order ()
  1117. {
  1118. var r = new View ();
  1119. var v1 = new View () { Id = "1", CanFocus = true };
  1120. var v2 = new View () { Id = "2", CanFocus = true };
  1121. var v3 = new View () { Id = "3", CanFocus = true };
  1122. r.Add (v1, v2, v3);
  1123. v1.TabIndex = 2;
  1124. v2.TabIndex = 1;
  1125. v3.TabIndex = 0;
  1126. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1127. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1128. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1129. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1130. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1131. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1132. }
  1133. [Fact]
  1134. public void TabIndex_Invert_Order_Added_One_By_One_Does_Not_Do_What_Is_Expected ()
  1135. {
  1136. var r = new View ();
  1137. var v1 = new View () { Id = "1", CanFocus = true };
  1138. r.Add (v1);
  1139. v1.TabIndex = 2;
  1140. var v2 = new View () { Id = "2", CanFocus = true };
  1141. r.Add (v2);
  1142. v2.TabIndex = 1;
  1143. var v3 = new View () { Id = "3", CanFocus = true };
  1144. r.Add (v3);
  1145. v3.TabIndex = 0;
  1146. Assert.False (r.TabIndexes.IndexOf (v1) == 2);
  1147. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1148. Assert.False (r.TabIndexes.IndexOf (v2) == 1);
  1149. Assert.True (r.TabIndexes.IndexOf (v2) == 2);
  1150. // Only the last is in the expected index
  1151. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1152. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1153. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1154. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1155. }
  1156. [Fact]
  1157. public void TabIndex_Invert_Order_Mixed ()
  1158. {
  1159. var r = new View ();
  1160. var vl1 = new View () { Id = "vl1" };
  1161. var v1 = new View () { Id = "v1", CanFocus = true };
  1162. var vl2 = new View () { Id = "vl2" };
  1163. var v2 = new View () { Id = "v2", CanFocus = true };
  1164. var vl3 = new View () { Id = "vl3" };
  1165. var v3 = new View () { Id = "v3", CanFocus = true };
  1166. r.Add (vl1, v1, vl2, v2, vl3, v3);
  1167. v1.TabIndex = 2;
  1168. v2.TabIndex = 1;
  1169. v3.TabIndex = 0;
  1170. Assert.True (r.TabIndexes.IndexOf (v1) == 4);
  1171. Assert.True (r.TabIndexes.IndexOf (v2) == 2);
  1172. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1173. Assert.True (r.Subviews.IndexOf (v1) == 1);
  1174. Assert.True (r.Subviews.IndexOf (v2) == 3);
  1175. Assert.True (r.Subviews.IndexOf (v3) == 5);
  1176. }
  1177. [Fact]
  1178. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  1179. {
  1180. var r = new View ();
  1181. var v1 = new View { CanFocus = true, TabStop = false };
  1182. var v2 = new View { CanFocus = true, TabStop = false };
  1183. var v3 = new View { CanFocus = true, TabStop = false };
  1184. r.Add (v1, v2, v3);
  1185. r.FocusNext ();
  1186. Assert.False (v1.HasFocus);
  1187. Assert.False (v2.HasFocus);
  1188. Assert.False (v3.HasFocus);
  1189. v1.TabStop = true;
  1190. r.FocusNext ();
  1191. Assert.True (v1.HasFocus);
  1192. Assert.False (v2.HasFocus);
  1193. Assert.False (v3.HasFocus);
  1194. v2.TabStop = true;
  1195. r.FocusNext ();
  1196. Assert.False (v1.HasFocus);
  1197. Assert.True (v2.HasFocus);
  1198. Assert.False (v3.HasFocus);
  1199. v3.TabStop = true;
  1200. r.FocusNext ();
  1201. Assert.False (v1.HasFocus);
  1202. Assert.False (v2.HasFocus);
  1203. Assert.True (v3.HasFocus);
  1204. r.Dispose ();
  1205. }
  1206. [Fact]
  1207. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  1208. {
  1209. var r = new View ();
  1210. var v1 = new View ();
  1211. var v2 = new View ();
  1212. var v3 = new View ();
  1213. r.Add (v1, v2, v3);
  1214. r.FocusNext ();
  1215. Assert.False (v1.HasFocus);
  1216. Assert.False (v2.HasFocus);
  1217. Assert.False (v3.HasFocus);
  1218. v1.CanFocus = true;
  1219. r.FocusNext ();
  1220. Assert.True (v1.HasFocus);
  1221. Assert.False (v2.HasFocus);
  1222. Assert.False (v3.HasFocus);
  1223. v2.CanFocus = true;
  1224. r.FocusNext ();
  1225. Assert.False (v1.HasFocus);
  1226. Assert.True (v2.HasFocus);
  1227. Assert.False (v3.HasFocus);
  1228. v3.CanFocus = true;
  1229. r.FocusNext ();
  1230. Assert.False (v1.HasFocus);
  1231. Assert.False (v2.HasFocus);
  1232. Assert.True (v3.HasFocus);
  1233. r.Dispose ();
  1234. }
  1235. [Fact]
  1236. public void TabStop_And_CanFocus_Are_All_True ()
  1237. {
  1238. var r = new View ();
  1239. var v1 = new View { CanFocus = true };
  1240. var v2 = new View { CanFocus = true };
  1241. var v3 = new View { CanFocus = true };
  1242. r.Add (v1, v2, v3);
  1243. r.FocusNext ();
  1244. Assert.True (v1.HasFocus);
  1245. Assert.False (v2.HasFocus);
  1246. Assert.False (v3.HasFocus);
  1247. r.FocusNext ();
  1248. Assert.False (v1.HasFocus);
  1249. Assert.True (v2.HasFocus);
  1250. Assert.False (v3.HasFocus);
  1251. r.FocusNext ();
  1252. Assert.False (v1.HasFocus);
  1253. Assert.False (v2.HasFocus);
  1254. Assert.True (v3.HasFocus);
  1255. r.Dispose ();
  1256. }
  1257. [Fact]
  1258. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  1259. {
  1260. var r = new View ();
  1261. var v1 = new View { CanFocus = true, TabStop = false };
  1262. var v2 = new View { CanFocus = false, TabStop = true };
  1263. var v3 = new View { CanFocus = false, TabStop = false };
  1264. r.Add (v1, v2, v3);
  1265. r.FocusNext ();
  1266. Assert.False (v1.HasFocus);
  1267. Assert.False (v2.HasFocus);
  1268. Assert.False (v3.HasFocus);
  1269. r.FocusNext ();
  1270. Assert.False (v1.HasFocus);
  1271. Assert.False (v2.HasFocus);
  1272. Assert.False (v3.HasFocus);
  1273. r.FocusNext ();
  1274. Assert.False (v1.HasFocus);
  1275. Assert.False (v2.HasFocus);
  1276. Assert.False (v3.HasFocus);
  1277. r.Dispose ();
  1278. }
  1279. [Fact]
  1280. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  1281. {
  1282. var r = new View ();
  1283. var v1 = new View { CanFocus = true, TabStop = false };
  1284. var v2 = new View { CanFocus = true, TabStop = false };
  1285. var v3 = new View { CanFocus = true, TabStop = false };
  1286. r.Add (v1, v2, v3);
  1287. r.FocusNext ();
  1288. Assert.False (v1.HasFocus);
  1289. Assert.False (v2.HasFocus);
  1290. Assert.False (v3.HasFocus);
  1291. r.FocusNext ();
  1292. Assert.False (v1.HasFocus);
  1293. Assert.False (v2.HasFocus);
  1294. Assert.False (v3.HasFocus);
  1295. r.FocusNext ();
  1296. Assert.False (v1.HasFocus);
  1297. Assert.False (v2.HasFocus);
  1298. Assert.False (v3.HasFocus);
  1299. r.Dispose ();
  1300. }
  1301. [Fact]
  1302. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  1303. {
  1304. var r = new View ();
  1305. var v1 = new View ();
  1306. var v2 = new View ();
  1307. var v3 = new View ();
  1308. r.Add (v1, v2, v3);
  1309. r.FocusNext ();
  1310. Assert.False (v1.HasFocus);
  1311. Assert.False (v2.HasFocus);
  1312. Assert.False (v3.HasFocus);
  1313. r.FocusNext ();
  1314. Assert.False (v1.HasFocus);
  1315. Assert.False (v2.HasFocus);
  1316. Assert.False (v3.HasFocus);
  1317. r.FocusNext ();
  1318. Assert.False (v1.HasFocus);
  1319. Assert.False (v2.HasFocus);
  1320. Assert.False (v3.HasFocus);
  1321. r.Dispose ();
  1322. }
  1323. [Fact (Skip="Causes crash on Ubuntu in Github Action. Bogus test anyway.")]
  1324. public void WindowDispose_CanFocusProblem ()
  1325. {
  1326. // Arrange
  1327. Application.Init ();
  1328. using var top = new Toplevel ();
  1329. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  1330. using var window = new Window ();
  1331. top.Add (window);
  1332. window.Add (view);
  1333. // Act
  1334. RunState rs = Application.Begin (top);
  1335. Application.End (rs);
  1336. top.Dispose ();
  1337. Application.Shutdown ();
  1338. // Assert does Not throw NullReferenceException
  1339. top.SetFocus ();
  1340. }
  1341. // View.Focused & View.MostFocused tests
  1342. // View.Focused - No subviews
  1343. [Fact, Trait("BUGBUG", "Fix in Issue #3444")]
  1344. public void Focused_NoSubviews ()
  1345. {
  1346. var view = new View ();
  1347. Assert.Null (view.Focused);
  1348. view.CanFocus = true;
  1349. view.SetFocus ();
  1350. Assert.True (view.HasFocus);
  1351. Assert.Null (view.Focused); // BUGBUG: Should be view
  1352. }
  1353. // View.MostFocused - No subviews
  1354. [Fact, Trait ("BUGBUG", "Fix in Issue #3444")]
  1355. public void Most_Focused_NoSubviews ()
  1356. {
  1357. var view = new View ();
  1358. Assert.Null (view.Focused);
  1359. view.CanFocus = true;
  1360. view.SetFocus ();
  1361. Assert.True (view.HasFocus);
  1362. Assert.Null (view.MostFocused); // BUGBUG: Should be view
  1363. }
  1364. [Theory]
  1365. [MemberData (nameof (AllViewTypes))]
  1366. public void AllViews_Enter_Leave_Events (Type viewType)
  1367. {
  1368. var view = CreateInstanceIfNotGeneric (viewType);
  1369. if (view == null)
  1370. {
  1371. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  1372. return;
  1373. }
  1374. if (!view.CanFocus)
  1375. {
  1376. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  1377. return;
  1378. }
  1379. if (view is Toplevel && ((Toplevel)view).Modal)
  1380. {
  1381. output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  1382. return;
  1383. }
  1384. Application.Init (new FakeDriver ());
  1385. Toplevel top = new ()
  1386. {
  1387. Height = 10,
  1388. Width = 10
  1389. };
  1390. View otherView = new ()
  1391. {
  1392. X = 0, Y = 0,
  1393. Height = 1,
  1394. Width = 1,
  1395. CanFocus = true,
  1396. };
  1397. view.X = Pos.Right (otherView);
  1398. view.Y = 0;
  1399. view.Width = 10;
  1400. view.Height = 1;
  1401. var nEnter = 0;
  1402. var nLeave = 0;
  1403. view.Enter += (s, e) => nEnter++;
  1404. view.Leave += (s, e) => nLeave++;
  1405. top.Add (view, otherView);
  1406. Application.Begin (top);
  1407. // Start with the focus on our test view
  1408. view.SetFocus ();
  1409. Assert.Equal (1, nEnter);
  1410. Assert.Equal (0, nLeave);
  1411. // Use keyboard to navigate to next view (otherView).
  1412. if (view is TextView)
  1413. {
  1414. Application.OnKeyDown (Key.Tab.WithCtrl);
  1415. }
  1416. else if (view is DatePicker)
  1417. {
  1418. for (var i = 0; i < 4; i++)
  1419. {
  1420. Application.OnKeyDown (Key.Tab.WithCtrl);
  1421. }
  1422. }
  1423. else
  1424. {
  1425. Application.OnKeyDown (Key.Tab);
  1426. }
  1427. Assert.Equal (1, nEnter);
  1428. Assert.Equal (1, nLeave);
  1429. Application.OnKeyDown (Key.Tab);
  1430. Assert.Equal (2, nEnter);
  1431. Assert.Equal (1, nLeave);
  1432. top.Dispose ();
  1433. Application.Shutdown ();
  1434. }
  1435. [Theory]
  1436. [MemberData (nameof (AllViewTypes))]
  1437. public void AllViews_Enter_Leave_Events_Visible_False (Type viewType)
  1438. {
  1439. var view = CreateInstanceIfNotGeneric (viewType);
  1440. if (view == null)
  1441. {
  1442. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  1443. return;
  1444. }
  1445. if (!view.CanFocus)
  1446. {
  1447. output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  1448. return;
  1449. }
  1450. if (view is Toplevel && ((Toplevel)view).Modal)
  1451. {
  1452. output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  1453. return;
  1454. }
  1455. Application.Init (new FakeDriver ());
  1456. Toplevel top = new ()
  1457. {
  1458. Height = 10,
  1459. Width = 10
  1460. };
  1461. View otherView = new ()
  1462. {
  1463. X = 0, Y = 0,
  1464. Height = 1,
  1465. Width = 1,
  1466. CanFocus = true,
  1467. };
  1468. view.Visible = false;
  1469. view.X = Pos.Right (otherView);
  1470. view.Y = 0;
  1471. view.Width = 10;
  1472. view.Height = 1;
  1473. var nEnter = 0;
  1474. var nLeave = 0;
  1475. view.Enter += (s, e) => nEnter++;
  1476. view.Leave += (s, e) => nLeave++;
  1477. top.Add (view, otherView);
  1478. Application.Begin (top);
  1479. // Start with the focus on our test view
  1480. view.SetFocus ();
  1481. Assert.Equal (0, nEnter);
  1482. Assert.Equal (0, nLeave);
  1483. // Use keyboard to navigate to next view (otherView).
  1484. if (view is TextView)
  1485. {
  1486. Application.OnKeyDown (Key.Tab.WithCtrl);
  1487. }
  1488. else if (view is DatePicker)
  1489. {
  1490. for (var i = 0; i < 4; i++)
  1491. {
  1492. Application.OnKeyDown (Key.Tab.WithCtrl);
  1493. }
  1494. }
  1495. else
  1496. {
  1497. Application.OnKeyDown (Key.Tab);
  1498. }
  1499. Assert.Equal (0, nEnter);
  1500. Assert.Equal (0, nLeave);
  1501. top.NewKeyDownEvent (Key.Tab);
  1502. Assert.Equal (0, nEnter);
  1503. Assert.Equal (0, nLeave);
  1504. top.Dispose ();
  1505. Application.Shutdown ();
  1506. }
  1507. }