NavigationTests.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class NavigationTests (ITestOutputHelper output)
  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 (top.NewKeyDownEvent (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 (top.NewKeyDownEvent (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 (top.NewKeyDownEvent (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 (top.NewKeyDownEvent (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 (top.NewKeyDownEvent (Key.Tab.WithCtrl));
  350. Assert.True (top.NewKeyDownEvent (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 (top.NewKeyDownEvent (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. public void FocusNearestView_Ensure_Focus_Ordered ()
  448. {
  449. var top = new Toplevel ();
  450. var win = new Window ();
  451. var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
  452. win.Add (winSubview);
  453. top.Add (win);
  454. var frm = new FrameView ();
  455. var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
  456. frm.Add (frmSubview);
  457. top.Add (frm);
  458. top.NewKeyDownEvent (Key.Tab);
  459. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  460. top.NewKeyDownEvent (Key.Tab);
  461. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  462. top.NewKeyDownEvent (Key.Tab);
  463. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  464. top.NewKeyDownEvent (Key.Tab.WithShift);
  465. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  466. top.NewKeyDownEvent (Key.Tab.WithShift);
  467. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  468. top.Dispose ();
  469. }
  470. [Fact]
  471. [AutoInitShutdown]
  472. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  473. {
  474. Toplevel top1 = new ();
  475. var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  476. var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
  477. var view2 = new View
  478. {
  479. Id = "view2",
  480. Y = 1,
  481. Width = 10,
  482. Height = 5,
  483. CanFocus = true
  484. };
  485. View view3 = null;
  486. var removed = false;
  487. view2.Enter += (s, e) =>
  488. {
  489. if (!removed)
  490. {
  491. removed = true;
  492. view3 = new View { Id = "view3", Y = 1, Width = 10, Height = 5 };
  493. Application.Current.Add (view3);
  494. Application.Current.BringSubviewToFront (view3);
  495. Assert.False (view3.HasFocus);
  496. }
  497. };
  498. view2.Leave += (s, e) =>
  499. {
  500. Application.Current.Remove (view3);
  501. view3.Dispose ();
  502. view3 = null;
  503. };
  504. top2.Add (view2);
  505. top1.Add (view1, top2);
  506. Application.Begin (top1);
  507. Assert.True (top1.HasFocus);
  508. Assert.True (view1.HasFocus);
  509. Assert.False (view2.HasFocus);
  510. Assert.False (removed);
  511. Assert.Null (view3);
  512. Assert.True (top1.NewKeyDownEvent (Key.Tab.WithCtrl));
  513. Assert.True (top1.HasFocus);
  514. Assert.False (view1.HasFocus);
  515. Assert.True (view2.HasFocus);
  516. Assert.True (removed);
  517. Assert.NotNull (view3);
  518. Exception exception =
  519. Record.Exception (() => top1.NewKeyDownEvent (Key.Tab.WithCtrl));
  520. Assert.Null (exception);
  521. Assert.True (removed);
  522. Assert.Null (view3);
  523. top1.Dispose ();
  524. }
  525. #if V2_STATUSBAR
  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 StatusItem []
  535. {
  536. new (
  537. KeyCode.CtrlMask | KeyCode.Q,
  538. "~^Q~ 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 StatusItem []
  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. #endif
  637. [Fact]
  638. [SetupFakeDriver]
  639. public void Navigation_With_Null_Focused_View ()
  640. {
  641. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  642. Application.Init (new FakeDriver ());
  643. var top = new Toplevel ();
  644. top.Ready += (s, e) => { Assert.Null (top.Focused); };
  645. // Keyboard navigation with tab
  646. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  647. Application.Iteration += (s, a) => Application.RequestStop ();
  648. Application.Run (top);
  649. top.Dispose ();
  650. Application.Shutdown ();
  651. }
  652. [Fact]
  653. [AutoInitShutdown]
  654. public void Remove_Does_Not_Change_Focus ()
  655. {
  656. var top = new Toplevel ();
  657. Assert.True (top.CanFocus);
  658. Assert.False (top.HasFocus);
  659. var container = new View { Width = 10, Height = 10 };
  660. var leave = false;
  661. container.Leave += (s, e) => leave = true;
  662. Assert.False (container.CanFocus);
  663. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  664. container.Add (child);
  665. Assert.True (container.CanFocus);
  666. Assert.False (container.HasFocus);
  667. Assert.True (child.CanFocus);
  668. Assert.False (child.HasFocus);
  669. top.Add (container);
  670. Application.Begin (top);
  671. Assert.True (top.CanFocus);
  672. Assert.True (top.HasFocus);
  673. Assert.True (container.CanFocus);
  674. Assert.True (container.HasFocus);
  675. Assert.True (child.CanFocus);
  676. Assert.True (child.HasFocus);
  677. container.Remove (child);
  678. child.Dispose ();
  679. child = null;
  680. Assert.True (top.HasFocus);
  681. Assert.True (container.CanFocus);
  682. Assert.True (container.HasFocus);
  683. Assert.Null (child);
  684. Assert.False (leave);
  685. top.Dispose ();
  686. }
  687. [Fact]
  688. [AutoInitShutdown]
  689. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  690. {
  691. Toplevel top = new ();
  692. top.BorderStyle = LineStyle.Single;
  693. var view = new View
  694. {
  695. X = 3,
  696. Y = 2,
  697. Width = 10,
  698. Height = 1,
  699. Text = "0123456789"
  700. };
  701. top.Add (view);
  702. Application.Begin (top);
  703. Assert.Equal (Application.Current, top);
  704. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  705. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  706. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  707. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  708. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  709. Assert.Equal (new Rectangle (0, 0, 20, 10), top.Frame);
  710. _ = TestHelpers.AssertDriverContentsWithFrameAre (
  711. @"
  712. ┌──────────────────┐
  713. │ │
  714. │ │
  715. │ 0123456789 │
  716. │ │
  717. │ │
  718. │ │
  719. │ │
  720. │ │
  721. └──────────────────┘",
  722. output
  723. );
  724. // top
  725. Assert.Equal (Point.Empty, top.ScreenToFrame (new (0, 0)));
  726. var screen = top.Margin.ViewportToScreen (new Point (0, 0));
  727. Assert.Equal (0, screen.X);
  728. Assert.Equal (0, screen.Y);
  729. screen = top.Border.ViewportToScreen (new Point (0, 0));
  730. Assert.Equal (0, screen.X);
  731. Assert.Equal (0, screen.Y);
  732. screen = top.Padding.ViewportToScreen (new Point (0, 0));
  733. Assert.Equal (1, screen.X);
  734. Assert.Equal (1, screen.Y);
  735. screen = top.ViewportToScreen (new Point (0, 0));
  736. Assert.Equal (1, screen.X);
  737. Assert.Equal (1, screen.Y);
  738. screen = top.ViewportToScreen (new Point (-1, -1));
  739. Assert.Equal (0, screen.X);
  740. Assert.Equal (0, screen.Y);
  741. var found = View.FindDeepestView (top, new (0, 0));
  742. Assert.Equal (top.Border, found);
  743. Assert.Equal (0, found.Frame.X);
  744. Assert.Equal (0, found.Frame.Y);
  745. Assert.Equal (new Point (3, 2), top.ScreenToFrame (new (3, 2)));
  746. screen = top.ViewportToScreen (new Point (3, 2));
  747. Assert.Equal (4, screen.X);
  748. Assert.Equal (3, screen.Y);
  749. found = View.FindDeepestView (top, new (screen.X, screen.Y));
  750. Assert.Equal (view, found);
  751. //Assert.Equal (0, found.FrameToScreen ().X);
  752. //Assert.Equal (0, found.FrameToScreen ().Y);
  753. found = View.FindDeepestView (top, new (3, 2));
  754. Assert.Equal (top, found);
  755. //Assert.Equal (3, found.FrameToScreen ().X);
  756. //Assert.Equal (2, found.FrameToScreen ().Y);
  757. Assert.Equal (new Point (13, 2), top.ScreenToFrame (new (13, 2)));
  758. screen = top.ViewportToScreen (new Point (12, 2));
  759. Assert.Equal (13, screen.X);
  760. Assert.Equal (3, screen.Y);
  761. found = View.FindDeepestView (top, new (screen.X, screen.Y));
  762. Assert.Equal (view, found);
  763. //Assert.Equal (9, found.FrameToScreen ().X);
  764. //Assert.Equal (0, found.FrameToScreen ().Y);
  765. screen = top.ViewportToScreen (new Point (13, 2));
  766. Assert.Equal (14, screen.X);
  767. Assert.Equal (3, screen.Y);
  768. found = View.FindDeepestView (top, new (13, 2));
  769. Assert.Equal (top, found);
  770. //Assert.Equal (13, found.FrameToScreen ().X);
  771. //Assert.Equal (2, found.FrameToScreen ().Y);
  772. Assert.Equal (new Point (14, 3), top.ScreenToFrame (new (14, 3)));
  773. screen = top.ViewportToScreen (new Point (14, 3));
  774. Assert.Equal (15, screen.X);
  775. Assert.Equal (4, screen.Y);
  776. found = View.FindDeepestView (top, new (14, 3));
  777. Assert.Equal (top, found);
  778. //Assert.Equal (14, found.FrameToScreen ().X);
  779. //Assert.Equal (3, found.FrameToScreen ().Y);
  780. // view
  781. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (new (0, 0)));
  782. screen = view.Margin.ViewportToScreen (new Point (-3, -2));
  783. Assert.Equal (1, screen.X);
  784. Assert.Equal (1, screen.Y);
  785. screen = view.Border.ViewportToScreen (new Point (-3, -2));
  786. Assert.Equal (1, screen.X);
  787. Assert.Equal (1, screen.Y);
  788. screen = view.Padding.ViewportToScreen (new Point (-3, -2));
  789. Assert.Equal (1, screen.X);
  790. Assert.Equal (1, screen.Y);
  791. screen = view.ViewportToScreen (new Point (-3, -2));
  792. Assert.Equal (1, screen.X);
  793. Assert.Equal (1, screen.Y);
  794. screen = view.ViewportToScreen (new Point (-4, -3));
  795. Assert.Equal (0, screen.X);
  796. Assert.Equal (0, screen.Y);
  797. found = View.FindDeepestView (top, new (0, 0));
  798. Assert.Equal (top.Border, found);
  799. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (new (3, 2)));
  800. screen = view.ViewportToScreen (new Point (0, 0));
  801. Assert.Equal (4, screen.X);
  802. Assert.Equal (3, screen.Y);
  803. found = View.FindDeepestView (top, new (4, 3));
  804. Assert.Equal (view, found);
  805. Assert.Equal (new Point (9, -1), view.ScreenToFrame (new (13, 2)));
  806. screen = view.ViewportToScreen (new Point (10, 0));
  807. Assert.Equal (14, screen.X);
  808. Assert.Equal (3, screen.Y);
  809. found = View.FindDeepestView (top, new (14, 3));
  810. Assert.Equal (top, found);
  811. Assert.Equal (new Point (10, 0), view.ScreenToFrame (new (14, 3)));
  812. screen = view.ViewportToScreen (new Point (11, 1));
  813. Assert.Equal (15, screen.X);
  814. Assert.Equal (4, screen.Y);
  815. found = View.FindDeepestView (top, new (15, 4));
  816. Assert.Equal (top, found);
  817. top.Dispose ();
  818. }
  819. [Fact]
  820. [AutoInitShutdown]
  821. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  822. {
  823. var top = new Toplevel
  824. {
  825. X = 3,
  826. Y = 2,
  827. Width = 20,
  828. Height = 10,
  829. BorderStyle = LineStyle.Single
  830. };
  831. var view = new View
  832. {
  833. X = 3,
  834. Y = 2,
  835. Width = 10,
  836. Height = 1,
  837. Text = "0123456789"
  838. };
  839. top.Add (view);
  840. Application.Begin (top);
  841. Assert.Equal (Application.Current, top);
  842. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  843. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  844. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  845. ((FakeDriver)Application.Driver).SetBufferSize (30, 20);
  846. Assert.Equal (new Rectangle (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  847. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  848. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  849. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  850. @"
  851. ┌──────────────────┐
  852. │ │
  853. │ │
  854. │ 0123456789 │
  855. │ │
  856. │ │
  857. │ │
  858. │ │
  859. │ │
  860. └──────────────────┘",
  861. output
  862. );
  863. // mean the output started at col 3 and line 2
  864. // which result with a width of 23 and a height of 10 on the output
  865. Assert.Equal (new Rectangle (3, 2, 23, 10), frame);
  866. // top
  867. Assert.Equal (new Point (-3, -2), top.ScreenToFrame (new (0, 0)));
  868. var screen = top.Margin.ViewportToScreen (new Point (-3, -2));
  869. Assert.Equal (0, screen.X);
  870. Assert.Equal (0, screen.Y);
  871. screen = top.Border.ViewportToScreen (new Point (-3, -2));
  872. Assert.Equal (0, screen.X);
  873. Assert.Equal (0, screen.Y);
  874. screen = top.Padding.ViewportToScreen (new Point (-3, -2));
  875. Assert.Equal (1, screen.X);
  876. Assert.Equal (1, screen.Y);
  877. screen = top.ViewportToScreen (new Point (-3, -2));
  878. Assert.Equal (1, screen.X);
  879. Assert.Equal (1, screen.Y);
  880. screen = top.ViewportToScreen (new Point (-4, -3));
  881. Assert.Equal (0, screen.X);
  882. Assert.Equal (0, screen.Y);
  883. var found = View.FindDeepestView (top, new (-4, -3));
  884. Assert.Null (found);
  885. Assert.Equal (Point.Empty, top.ScreenToFrame (new (3, 2)));
  886. screen = top.ViewportToScreen (new Point (0, 0));
  887. Assert.Equal (4, screen.X);
  888. Assert.Equal (3, screen.Y);
  889. Assert.Equal (top.Border, View.FindDeepestView (top, new (3, 2)));
  890. //Assert.Equal (0, found.FrameToScreen ().X);
  891. //Assert.Equal (0, found.FrameToScreen ().Y);
  892. Assert.Equal (new Point (10, 0), top.ScreenToFrame (new (13, 2)));
  893. screen = top.ViewportToScreen (new Point (10, 0));
  894. Assert.Equal (14, screen.X);
  895. Assert.Equal (3, screen.Y);
  896. Assert.Equal (top.Border, View.FindDeepestView (top, new (13, 2)));
  897. //Assert.Equal (10, found.FrameToScreen ().X);
  898. //Assert.Equal (0, found.FrameToScreen ().Y);
  899. Assert.Equal (new Point (11, 1), top.ScreenToFrame (new (14, 3)));
  900. screen = top.ViewportToScreen (new Point (11, 1));
  901. Assert.Equal (15, screen.X);
  902. Assert.Equal (4, screen.Y);
  903. Assert.Equal (top, View.FindDeepestView (top, new (14, 3)));
  904. // view
  905. Assert.Equal (new Point (-7, -5), view.ScreenToFrame (new (0, 0)));
  906. screen = view.Margin.ViewportToScreen (new Point (-6, -4));
  907. Assert.Equal (1, screen.X);
  908. Assert.Equal (1, screen.Y);
  909. screen = view.Border.ViewportToScreen (new Point (-6, -4));
  910. Assert.Equal (1, screen.X);
  911. Assert.Equal (1, screen.Y);
  912. screen = view.Padding.ViewportToScreen (new Point (-6, -4));
  913. Assert.Equal (1, screen.X);
  914. Assert.Equal (1, screen.Y);
  915. screen = view.ViewportToScreen (new Point (-6, -4));
  916. Assert.Equal (1, screen.X);
  917. Assert.Equal (1, screen.Y);
  918. Assert.Null (View.FindDeepestView (top, new (1, 1)));
  919. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (new (3, 2)));
  920. screen = view.ViewportToScreen (new Point (-3, -2));
  921. Assert.Equal (4, screen.X);
  922. Assert.Equal (3, screen.Y);
  923. Assert.Equal (top, View.FindDeepestView (top, new (4, 3)));
  924. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (new (6, 4)));
  925. screen = view.ViewportToScreen (new Point (0, 0));
  926. Assert.Equal (7, screen.X);
  927. Assert.Equal (5, screen.Y);
  928. Assert.Equal (view, View.FindDeepestView (top, new (7, 5)));
  929. Assert.Equal (new Point (6, -1), view.ScreenToFrame (new (13, 4)));
  930. screen = view.ViewportToScreen (new Point (7, 0));
  931. Assert.Equal (14, screen.X);
  932. Assert.Equal (5, screen.Y);
  933. Assert.Equal (view, View.FindDeepestView (top, new (14, 5)));
  934. Assert.Equal (new Point (7, -2), view.ScreenToFrame (new (14, 3)));
  935. screen = view.ViewportToScreen (new Point (8, -1));
  936. Assert.Equal (15, screen.X);
  937. Assert.Equal (4, screen.Y);
  938. Assert.Equal (top, View.FindDeepestView (top, new (15, 4)));
  939. Assert.Equal (new Point (16, -2), view.ScreenToFrame (new (23, 3)));
  940. screen = view.ViewportToScreen (new Point (17, -1));
  941. Assert.Equal (24, screen.X);
  942. Assert.Equal (4, screen.Y);
  943. Assert.Null (View.FindDeepestView (top, new (24, 4)));
  944. top.Dispose ();
  945. }
  946. [Fact]
  947. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  948. {
  949. var r = new View ();
  950. var v1 = new View { CanFocus = true };
  951. var v2 = new View { CanFocus = true };
  952. var v3 = new View { CanFocus = true };
  953. r.Add (v1, v2, v3);
  954. r.SendSubviewBackwards (v3);
  955. Assert.True (r.Subviews.IndexOf (v1) == 0);
  956. Assert.True (r.Subviews.IndexOf (v2) == 2);
  957. Assert.True (r.Subviews.IndexOf (v3) == 1);
  958. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  959. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  960. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  961. r.Dispose ();
  962. }
  963. [Fact]
  964. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  965. {
  966. var r = new View ();
  967. var v1 = new View { CanFocus = true };
  968. var v2 = new View { CanFocus = true };
  969. var v3 = new View { CanFocus = true };
  970. r.Add (v1, v2, v3);
  971. r.SendSubviewToBack (v3);
  972. Assert.True (r.Subviews.IndexOf (v1) == 1);
  973. Assert.True (r.Subviews.IndexOf (v2) == 2);
  974. Assert.True (r.Subviews.IndexOf (v3) == 0);
  975. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  976. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  977. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  978. r.Dispose ();
  979. }
  980. [Fact]
  981. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  982. {
  983. var top = new Toplevel ();
  984. Assert.True (top.CanFocus);
  985. Assert.False (top.HasFocus);
  986. Exception exception = Record.Exception (top.SetFocus);
  987. Assert.Null (exception);
  988. Assert.True (top.CanFocus);
  989. Assert.True (top.HasFocus);
  990. }
  991. [Fact]
  992. [AutoInitShutdown]
  993. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  994. {
  995. var view1Leave = false;
  996. var subView1Leave = false;
  997. var subView1subView1Leave = false;
  998. Toplevel top = new ();
  999. var view1 = new View { CanFocus = true };
  1000. var subView1 = new View { CanFocus = true };
  1001. var subView1subView1 = new View { CanFocus = true };
  1002. view1.Leave += (s, e) => { view1Leave = true; };
  1003. subView1.Leave += (s, e) =>
  1004. {
  1005. subView1.Remove (subView1subView1);
  1006. subView1Leave = true;
  1007. };
  1008. view1.Add (subView1);
  1009. subView1subView1.Leave += (s, e) =>
  1010. {
  1011. // This is never invoked
  1012. subView1subView1Leave = true;
  1013. };
  1014. subView1.Add (subView1subView1);
  1015. var view2 = new View { CanFocus = true };
  1016. top.Add (view1, view2);
  1017. RunState rs = Application.Begin (top);
  1018. view2.SetFocus ();
  1019. Assert.True (view1Leave);
  1020. Assert.True (subView1Leave);
  1021. Assert.False (subView1subView1Leave);
  1022. Application.End (rs);
  1023. subView1subView1.Dispose ();
  1024. top.Dispose ();
  1025. }
  1026. [Fact]
  1027. public void Subviews_TabIndexes_AreEqual ()
  1028. {
  1029. var r = new View ();
  1030. var v1 = new View { CanFocus = true };
  1031. var v2 = new View { CanFocus = true };
  1032. var v3 = new View { CanFocus = true };
  1033. r.Add (v1, v2, v3);
  1034. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1035. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1036. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1037. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1038. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1039. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  1040. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  1041. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  1042. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  1043. r.Dispose ();
  1044. }
  1045. [Fact]
  1046. public void TabIndex_Set_CanFocus_False ()
  1047. {
  1048. var r = new View ();
  1049. var v1 = new View { CanFocus = true };
  1050. var v2 = new View { CanFocus = true };
  1051. var v3 = new View { CanFocus = true };
  1052. r.Add (v1, v2, v3);
  1053. v1.CanFocus = false;
  1054. v1.TabIndex = 0;
  1055. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1056. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1057. Assert.Equal (-1, v1.TabIndex);
  1058. r.Dispose ();
  1059. }
  1060. [Fact]
  1061. public void TabIndex_Set_CanFocus_False_To_True ()
  1062. {
  1063. var r = new View ();
  1064. var v1 = new View ();
  1065. var v2 = new View { CanFocus = true };
  1066. var v3 = new View { CanFocus = true };
  1067. r.Add (v1, v2, v3);
  1068. v1.CanFocus = true;
  1069. v1.TabIndex = 1;
  1070. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1071. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1072. r.Dispose ();
  1073. }
  1074. [Fact]
  1075. public void TabIndex_Set_CanFocus_HigherValues ()
  1076. {
  1077. var r = new View ();
  1078. var v1 = new View { CanFocus = true };
  1079. var v2 = new View { CanFocus = true };
  1080. var v3 = new View { CanFocus = true };
  1081. r.Add (v1, v2, v3);
  1082. v1.TabIndex = 3;
  1083. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1084. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1085. r.Dispose ();
  1086. }
  1087. [Fact]
  1088. public void TabIndex_Set_CanFocus_LowerValues ()
  1089. {
  1090. var r = new View ();
  1091. var v1 = new View { CanFocus = true };
  1092. var v2 = new View { CanFocus = true };
  1093. var v3 = new View { CanFocus = true };
  1094. r.Add (v1, v2, v3);
  1095. v1.TabIndex = -1;
  1096. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1097. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1098. r.Dispose ();
  1099. }
  1100. [Fact]
  1101. public void TabIndex_Set_CanFocus_ValidValues ()
  1102. {
  1103. var r = new View ();
  1104. var v1 = new View { CanFocus = true };
  1105. var v2 = new View { CanFocus = true };
  1106. var v3 = new View { CanFocus = true };
  1107. r.Add (v1, v2, v3);
  1108. v1.TabIndex = 1;
  1109. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1110. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1111. v1.TabIndex = 2;
  1112. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1113. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1114. r.Dispose ();
  1115. }
  1116. [Fact]
  1117. public void TabIndex_Invert_Order ()
  1118. {
  1119. var r = new View ();
  1120. var v1 = new View () { Id = "1", CanFocus = true };
  1121. var v2 = new View () { Id = "2", CanFocus = true };
  1122. var v3 = new View () { Id = "3", CanFocus = true };
  1123. r.Add (v1, v2, v3);
  1124. v1.TabIndex = 2;
  1125. v2.TabIndex = 1;
  1126. v3.TabIndex = 0;
  1127. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1128. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1129. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1130. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1131. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1132. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1133. }
  1134. [Fact]
  1135. public void TabIndex_Invert_Order_Added_One_By_One_Does_Not_Do_What_Is_Expected ()
  1136. {
  1137. var r = new View ();
  1138. var v1 = new View () { Id = "1", CanFocus = true };
  1139. r.Add (v1);
  1140. v1.TabIndex = 2;
  1141. var v2 = new View () { Id = "2", CanFocus = true };
  1142. r.Add (v2);
  1143. v2.TabIndex = 1;
  1144. var v3 = new View () { Id = "3", CanFocus = true };
  1145. r.Add (v3);
  1146. v3.TabIndex = 0;
  1147. Assert.False (r.TabIndexes.IndexOf (v1) == 2);
  1148. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1149. Assert.False (r.TabIndexes.IndexOf (v2) == 1);
  1150. Assert.True (r.TabIndexes.IndexOf (v2) == 2);
  1151. // Only the last is in the expected index
  1152. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1153. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1154. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1155. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1156. }
  1157. [Fact]
  1158. public void TabIndex_Invert_Order_Mixed ()
  1159. {
  1160. var r = new View ();
  1161. var vl1 = new View () { Id = "vl1" };
  1162. var v1 = new View () { Id = "v1", CanFocus = true };
  1163. var vl2 = new View () { Id = "vl2" };
  1164. var v2 = new View () { Id = "v2", CanFocus = true };
  1165. var vl3 = new View () { Id = "vl3" };
  1166. var v3 = new View () { Id = "v3", CanFocus = true };
  1167. r.Add (vl1, v1, vl2, v2, vl3, v3);
  1168. v1.TabIndex = 2;
  1169. v2.TabIndex = 1;
  1170. v3.TabIndex = 0;
  1171. Assert.True (r.TabIndexes.IndexOf (v1) == 4);
  1172. Assert.True (r.TabIndexes.IndexOf (v2) == 2);
  1173. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1174. Assert.True (r.Subviews.IndexOf (v1) == 1);
  1175. Assert.True (r.Subviews.IndexOf (v2) == 3);
  1176. Assert.True (r.Subviews.IndexOf (v3) == 5);
  1177. }
  1178. [Fact]
  1179. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  1180. {
  1181. var r = new View ();
  1182. var v1 = new View { CanFocus = true, TabStop = false };
  1183. var v2 = new View { CanFocus = true, TabStop = false };
  1184. var v3 = new View { CanFocus = true, TabStop = false };
  1185. r.Add (v1, v2, v3);
  1186. r.FocusNext ();
  1187. Assert.False (v1.HasFocus);
  1188. Assert.False (v2.HasFocus);
  1189. Assert.False (v3.HasFocus);
  1190. v1.TabStop = true;
  1191. r.FocusNext ();
  1192. Assert.True (v1.HasFocus);
  1193. Assert.False (v2.HasFocus);
  1194. Assert.False (v3.HasFocus);
  1195. v2.TabStop = true;
  1196. r.FocusNext ();
  1197. Assert.False (v1.HasFocus);
  1198. Assert.True (v2.HasFocus);
  1199. Assert.False (v3.HasFocus);
  1200. v3.TabStop = true;
  1201. r.FocusNext ();
  1202. Assert.False (v1.HasFocus);
  1203. Assert.False (v2.HasFocus);
  1204. Assert.True (v3.HasFocus);
  1205. r.Dispose ();
  1206. }
  1207. [Fact]
  1208. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  1209. {
  1210. var r = new View ();
  1211. var v1 = new View ();
  1212. var v2 = new View ();
  1213. var v3 = new View ();
  1214. r.Add (v1, v2, v3);
  1215. r.FocusNext ();
  1216. Assert.False (v1.HasFocus);
  1217. Assert.False (v2.HasFocus);
  1218. Assert.False (v3.HasFocus);
  1219. v1.CanFocus = true;
  1220. r.FocusNext ();
  1221. Assert.True (v1.HasFocus);
  1222. Assert.False (v2.HasFocus);
  1223. Assert.False (v3.HasFocus);
  1224. v2.CanFocus = true;
  1225. r.FocusNext ();
  1226. Assert.False (v1.HasFocus);
  1227. Assert.True (v2.HasFocus);
  1228. Assert.False (v3.HasFocus);
  1229. v3.CanFocus = true;
  1230. r.FocusNext ();
  1231. Assert.False (v1.HasFocus);
  1232. Assert.False (v2.HasFocus);
  1233. Assert.True (v3.HasFocus);
  1234. r.Dispose ();
  1235. }
  1236. [Fact]
  1237. public void TabStop_And_CanFocus_Are_All_True ()
  1238. {
  1239. var r = new View ();
  1240. var v1 = new View { CanFocus = true };
  1241. var v2 = new View { CanFocus = true };
  1242. var v3 = new View { CanFocus = true };
  1243. r.Add (v1, v2, v3);
  1244. r.FocusNext ();
  1245. Assert.True (v1.HasFocus);
  1246. Assert.False (v2.HasFocus);
  1247. Assert.False (v3.HasFocus);
  1248. r.FocusNext ();
  1249. Assert.False (v1.HasFocus);
  1250. Assert.True (v2.HasFocus);
  1251. Assert.False (v3.HasFocus);
  1252. r.FocusNext ();
  1253. Assert.False (v1.HasFocus);
  1254. Assert.False (v2.HasFocus);
  1255. Assert.True (v3.HasFocus);
  1256. r.Dispose ();
  1257. }
  1258. [Fact]
  1259. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  1260. {
  1261. var r = new View ();
  1262. var v1 = new View { CanFocus = true, TabStop = false };
  1263. var v2 = new View { CanFocus = false, TabStop = true };
  1264. var v3 = new View { CanFocus = false, TabStop = false };
  1265. r.Add (v1, v2, v3);
  1266. r.FocusNext ();
  1267. Assert.False (v1.HasFocus);
  1268. Assert.False (v2.HasFocus);
  1269. Assert.False (v3.HasFocus);
  1270. r.FocusNext ();
  1271. Assert.False (v1.HasFocus);
  1272. Assert.False (v2.HasFocus);
  1273. Assert.False (v3.HasFocus);
  1274. r.FocusNext ();
  1275. Assert.False (v1.HasFocus);
  1276. Assert.False (v2.HasFocus);
  1277. Assert.False (v3.HasFocus);
  1278. r.Dispose ();
  1279. }
  1280. [Fact]
  1281. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  1282. {
  1283. var r = new View ();
  1284. var v1 = new View { CanFocus = true, TabStop = false };
  1285. var v2 = new View { CanFocus = true, TabStop = false };
  1286. var v3 = new View { CanFocus = true, TabStop = false };
  1287. r.Add (v1, v2, v3);
  1288. r.FocusNext ();
  1289. Assert.False (v1.HasFocus);
  1290. Assert.False (v2.HasFocus);
  1291. Assert.False (v3.HasFocus);
  1292. r.FocusNext ();
  1293. Assert.False (v1.HasFocus);
  1294. Assert.False (v2.HasFocus);
  1295. Assert.False (v3.HasFocus);
  1296. r.FocusNext ();
  1297. Assert.False (v1.HasFocus);
  1298. Assert.False (v2.HasFocus);
  1299. Assert.False (v3.HasFocus);
  1300. r.Dispose ();
  1301. }
  1302. [Fact]
  1303. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  1304. {
  1305. var r = new View ();
  1306. var v1 = new View ();
  1307. var v2 = new View ();
  1308. var v3 = new View ();
  1309. r.Add (v1, v2, v3);
  1310. r.FocusNext ();
  1311. Assert.False (v1.HasFocus);
  1312. Assert.False (v2.HasFocus);
  1313. Assert.False (v3.HasFocus);
  1314. r.FocusNext ();
  1315. Assert.False (v1.HasFocus);
  1316. Assert.False (v2.HasFocus);
  1317. Assert.False (v3.HasFocus);
  1318. r.FocusNext ();
  1319. Assert.False (v1.HasFocus);
  1320. Assert.False (v2.HasFocus);
  1321. Assert.False (v3.HasFocus);
  1322. r.Dispose ();
  1323. }
  1324. [Fact]
  1325. [AutoInitShutdown]
  1326. public void WindowDispose_CanFocusProblem ()
  1327. {
  1328. // Arrange
  1329. Application.Init ();
  1330. using var top = new Toplevel ();
  1331. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  1332. using var window = new Window ();
  1333. top.Add (window);
  1334. window.Add (view);
  1335. // Act
  1336. RunState rs = Application.Begin (top);
  1337. Application.End (rs);
  1338. top.Dispose ();
  1339. Application.Shutdown ();
  1340. // Assert does Not throw NullReferenceException
  1341. top.SetFocus ();
  1342. }
  1343. // View.Focused & View.MostFocused tests
  1344. // View.Focused - No subviews
  1345. [Fact, Trait("BUGBUG", "Fix in Issue #3444")]
  1346. public void Focused_NoSubviews ()
  1347. {
  1348. var view = new View ();
  1349. Assert.Null (view.Focused);
  1350. view.CanFocus = true;
  1351. view.SetFocus ();
  1352. Assert.True (view.HasFocus);
  1353. Assert.Null (view.Focused); // BUGBUG: Should be view
  1354. }
  1355. // View.MostFocused - No subviews
  1356. [Fact, Trait ("BUGBUG", "Fix in Issue #3444")]
  1357. public void Most_Focused_NoSubviews ()
  1358. {
  1359. var view = new View ();
  1360. Assert.Null (view.Focused);
  1361. view.CanFocus = true;
  1362. view.SetFocus ();
  1363. Assert.True (view.HasFocus);
  1364. Assert.Null (view.MostFocused); // BUGBUG: Should be view
  1365. }
  1366. }