NavigationTests.cs 64 KB

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