NavigationTests.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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.FocusDeepest (null, NavigationDirection.Forward);
  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_With_Null_Superview_Does_Not_Throw_Exception ()
  1252. {
  1253. var view = new View ()
  1254. {
  1255. CanFocus = true
  1256. };
  1257. Assert.True (view.CanFocus);
  1258. Assert.False (view.HasFocus);
  1259. Exception exception = Record.Exception (() => view.SetFocus());
  1260. Assert.Null (exception);
  1261. Assert.True (view.CanFocus);
  1262. Assert.True (view.HasFocus);
  1263. }
  1264. [Fact]
  1265. [AutoInitShutdown]
  1266. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  1267. {
  1268. var view1Leave = false;
  1269. var subView1Leave = false;
  1270. var subView1subView1Leave = false;
  1271. Toplevel top = new ();
  1272. var view1 = new View { CanFocus = true };
  1273. var subView1 = new View { CanFocus = true };
  1274. var subView1subView1 = new View { CanFocus = true };
  1275. view1.Leave += (s, e) => { view1Leave = true; };
  1276. subView1.Leave += (s, e) =>
  1277. {
  1278. subView1.Remove (subView1subView1);
  1279. subView1Leave = true;
  1280. };
  1281. view1.Add (subView1);
  1282. subView1subView1.Leave += (s, e) =>
  1283. {
  1284. // This is never invoked
  1285. subView1subView1Leave = true;
  1286. };
  1287. subView1.Add (subView1subView1);
  1288. var view2 = new View { CanFocus = true };
  1289. top.Add (view1, view2);
  1290. RunState rs = Application.Begin (top);
  1291. view2.SetFocus ();
  1292. Assert.True (view1Leave);
  1293. Assert.True (subView1Leave);
  1294. Assert.False (subView1subView1Leave);
  1295. Application.End (rs);
  1296. subView1subView1.Dispose ();
  1297. top.Dispose ();
  1298. }
  1299. [Fact]
  1300. public void Subviews_TabIndexes_AreEqual ()
  1301. {
  1302. var r = new View ();
  1303. var v1 = new View { CanFocus = true };
  1304. var v2 = new View { CanFocus = true };
  1305. var v3 = new View { CanFocus = true };
  1306. r.Add (v1, v2, v3);
  1307. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1308. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1309. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1310. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1311. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1312. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  1313. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  1314. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  1315. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  1316. r.Dispose ();
  1317. }
  1318. [Fact]
  1319. public void TabIndex_Invert_Order ()
  1320. {
  1321. var r = new View ();
  1322. var v1 = new View { Id = "1", CanFocus = true };
  1323. var v2 = new View { Id = "2", CanFocus = true };
  1324. var v3 = new View { Id = "3", CanFocus = true };
  1325. r.Add (v1, v2, v3);
  1326. v1.TabIndex = 2;
  1327. v2.TabIndex = 1;
  1328. v3.TabIndex = 0;
  1329. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1330. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1331. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1332. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1333. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1334. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1335. }
  1336. [Fact]
  1337. public void TabIndex_Invert_Order_Added_One_By_One_Does_Not_Do_What_Is_Expected ()
  1338. {
  1339. var r = new View ();
  1340. var v1 = new View { Id = "1", CanFocus = true };
  1341. r.Add (v1);
  1342. v1.TabIndex = 2;
  1343. var v2 = new View { Id = "2", CanFocus = true };
  1344. r.Add (v2);
  1345. v2.TabIndex = 1;
  1346. var v3 = new View { Id = "3", CanFocus = true };
  1347. r.Add (v3);
  1348. v3.TabIndex = 0;
  1349. Assert.False (r.TabIndexes.IndexOf (v1) == 2);
  1350. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1351. Assert.False (r.TabIndexes.IndexOf (v2) == 1);
  1352. Assert.True (r.TabIndexes.IndexOf (v2) == 2);
  1353. // Only the last is in the expected index
  1354. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1355. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1356. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1357. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1358. }
  1359. [Fact]
  1360. public void TabIndex_Invert_Order_Mixed ()
  1361. {
  1362. var r = new View ();
  1363. var vl1 = new View { Id = "vl1" };
  1364. var v1 = new View { Id = "v1", CanFocus = true };
  1365. var vl2 = new View { Id = "vl2" };
  1366. var v2 = new View { Id = "v2", CanFocus = true };
  1367. var vl3 = new View { Id = "vl3" };
  1368. var v3 = new View { Id = "v3", CanFocus = true };
  1369. r.Add (vl1, v1, vl2, v2, vl3, v3);
  1370. v1.TabIndex = 2;
  1371. v2.TabIndex = 1;
  1372. v3.TabIndex = 0;
  1373. Assert.True (r.TabIndexes.IndexOf (v1) == 4);
  1374. Assert.True (r.TabIndexes.IndexOf (v2) == 2);
  1375. Assert.True (r.TabIndexes.IndexOf (v3) == 0);
  1376. Assert.True (r.Subviews.IndexOf (v1) == 1);
  1377. Assert.True (r.Subviews.IndexOf (v2) == 3);
  1378. Assert.True (r.Subviews.IndexOf (v3) == 5);
  1379. }
  1380. [Fact]
  1381. public void TabIndex_Set_CanFocus_False ()
  1382. {
  1383. var r = new View ();
  1384. var v1 = new View { CanFocus = true };
  1385. var v2 = new View { CanFocus = true };
  1386. var v3 = new View { CanFocus = true };
  1387. r.Add (v1, v2, v3);
  1388. v1.CanFocus = false;
  1389. v1.TabIndex = 0;
  1390. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1391. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1392. Assert.NotEqual (-1, v1.TabIndex);
  1393. r.Dispose ();
  1394. }
  1395. [Fact]
  1396. public void TabIndex_Set_CanFocus_False_To_True ()
  1397. {
  1398. var r = new View ();
  1399. var v1 = new View ();
  1400. var v2 = new View { CanFocus = true };
  1401. var v3 = new View { CanFocus = true };
  1402. r.Add (v1, v2, v3);
  1403. v1.CanFocus = true;
  1404. v1.TabIndex = 1;
  1405. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1406. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1407. r.Dispose ();
  1408. }
  1409. [Fact]
  1410. public void TabIndex_Set_CanFocus_HigherValues ()
  1411. {
  1412. var r = new View ();
  1413. var v1 = new View { CanFocus = true };
  1414. var v2 = new View { CanFocus = true };
  1415. var v3 = new View { CanFocus = true };
  1416. r.Add (v1, v2, v3);
  1417. v1.TabIndex = 3;
  1418. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1419. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1420. r.Dispose ();
  1421. }
  1422. [Fact]
  1423. public void TabIndex_Set_CanFocus_LowerValues ()
  1424. {
  1425. var r = new View ();
  1426. var v1 = new View { CanFocus = true };
  1427. var v2 = new View { CanFocus = true };
  1428. var v3 = new View { CanFocus = true };
  1429. r.Add (v1, v2, v3);
  1430. //v1.TabIndex = -1;
  1431. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1432. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1433. r.Dispose ();
  1434. }
  1435. [Fact]
  1436. public void TabIndex_Set_CanFocus_ValidValues ()
  1437. {
  1438. var r = new View ();
  1439. var v1 = new View { CanFocus = true };
  1440. var v2 = new View { CanFocus = true };
  1441. var v3 = new View { CanFocus = true };
  1442. r.Add (v1, v2, v3);
  1443. v1.TabIndex = 1;
  1444. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1445. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1446. v1.TabIndex = 2;
  1447. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1448. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1449. r.Dispose ();
  1450. }
  1451. [Fact]
  1452. public void TabStop_And_CanFocus_Are_All_True ()
  1453. {
  1454. var r = new View ();
  1455. var v1 = new View { CanFocus = true };
  1456. var v2 = new View { CanFocus = true };
  1457. var v3 = new View { CanFocus = true };
  1458. r.Add (v1, v2, v3);
  1459. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1460. Assert.True (v1.HasFocus);
  1461. Assert.False (v2.HasFocus);
  1462. Assert.False (v3.HasFocus);
  1463. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1464. Assert.False (v1.HasFocus);
  1465. Assert.True (v2.HasFocus);
  1466. Assert.False (v3.HasFocus);
  1467. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1468. Assert.False (v1.HasFocus);
  1469. Assert.False (v2.HasFocus);
  1470. Assert.True (v3.HasFocus);
  1471. r.Dispose ();
  1472. }
  1473. [Theory]
  1474. [CombinatorialData]
  1475. public void TabStop_And_CanFocus_Are_Decoupled (bool canFocus, TabBehavior tabStop)
  1476. {
  1477. var view = new View { CanFocus = canFocus, TabStop = tabStop };
  1478. Assert.Equal (canFocus, view.CanFocus);
  1479. Assert.Equal (tabStop, view.TabStop);
  1480. }
  1481. [Fact]
  1482. public void TabStop_And_CanFocus_Mixed ()
  1483. {
  1484. var r = new View ();
  1485. var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1486. var v2 = new View { CanFocus = false, TabStop = TabBehavior.TabStop };
  1487. var v3 = new View { CanFocus = false, TabStop = TabBehavior.NoStop };
  1488. r.Add (v1, v2, v3);
  1489. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1490. Assert.False (v1.HasFocus);
  1491. Assert.False (v2.HasFocus);
  1492. Assert.False (v3.HasFocus);
  1493. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1494. Assert.False (v1.HasFocus);
  1495. Assert.False (v2.HasFocus);
  1496. Assert.False (v3.HasFocus);
  1497. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1498. Assert.False (v1.HasFocus);
  1499. Assert.False (v2.HasFocus);
  1500. Assert.False (v3.HasFocus);
  1501. r.Dispose ();
  1502. }
  1503. [Theory]
  1504. [CombinatorialData]
  1505. public void TabStop_Change_CanFocus_Works ([CombinatorialValues (TabBehavior.NoStop, TabBehavior.TabStop, TabBehavior.TabGroup)] TabBehavior behavior)
  1506. {
  1507. var r = new View ();
  1508. var v1 = new View ();
  1509. var v2 = new View ();
  1510. var v3 = new View ();
  1511. Assert.False (v1.CanFocus);
  1512. Assert.False (v2.CanFocus);
  1513. Assert.False (v3.CanFocus);
  1514. r.Add (v1, v2, v3);
  1515. r.AdvanceFocus (NavigationDirection.Forward, behavior);
  1516. Assert.False (v1.HasFocus);
  1517. Assert.False (v2.HasFocus);
  1518. Assert.False (v3.HasFocus);
  1519. v1.CanFocus = true;
  1520. v1.TabStop = behavior;
  1521. r.AdvanceFocus (NavigationDirection.Forward, behavior);
  1522. Assert.True (v1.HasFocus);
  1523. Assert.False (v2.HasFocus);
  1524. Assert.False (v3.HasFocus);
  1525. v2.CanFocus = true;
  1526. v2.TabStop = behavior;
  1527. r.AdvanceFocus (NavigationDirection.Forward, behavior);
  1528. Assert.False (v1.HasFocus);
  1529. Assert.True (v2.HasFocus);
  1530. Assert.False (v3.HasFocus);
  1531. v3.CanFocus = true;
  1532. v3.TabStop = behavior;
  1533. r.AdvanceFocus (NavigationDirection.Forward, behavior);
  1534. Assert.False (v1.HasFocus);
  1535. Assert.False (v2.HasFocus);
  1536. Assert.True (v3.HasFocus);
  1537. r.Dispose ();
  1538. }
  1539. [Fact]
  1540. public void TabStop_NoStop_And_CanFocus_True_No_Focus ()
  1541. {
  1542. var r = new View ();
  1543. var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1544. var v2 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1545. var v3 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1546. r.Add (v1, v2, v3);
  1547. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1548. Assert.False (v1.HasFocus);
  1549. Assert.False (v2.HasFocus);
  1550. Assert.False (v3.HasFocus);
  1551. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1552. Assert.False (v1.HasFocus);
  1553. Assert.False (v2.HasFocus);
  1554. Assert.False (v3.HasFocus);
  1555. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1556. Assert.False (v1.HasFocus);
  1557. Assert.False (v2.HasFocus);
  1558. Assert.False (v3.HasFocus);
  1559. r.Dispose ();
  1560. }
  1561. [Fact]
  1562. public void TabStop_NoStop_Change_Enables_Stop ()
  1563. {
  1564. var r = new View ();
  1565. var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1566. var v2 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1567. var v3 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1568. r.Add (v1, v2, v3);
  1569. v1.TabStop = TabBehavior.TabStop;
  1570. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1571. Assert.True (v1.HasFocus);
  1572. Assert.False (v2.HasFocus);
  1573. Assert.False (v3.HasFocus);
  1574. v2.TabStop = TabBehavior.TabStop;
  1575. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1576. Assert.False (v1.HasFocus);
  1577. Assert.True (v2.HasFocus);
  1578. Assert.False (v3.HasFocus);
  1579. v3.TabStop = TabBehavior.TabStop;
  1580. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1581. Assert.False (v1.HasFocus);
  1582. Assert.False (v2.HasFocus);
  1583. Assert.True (v3.HasFocus);
  1584. r.Dispose ();
  1585. }
  1586. [Fact]
  1587. public void TabStop_NoStop_Prevents_Stop ()
  1588. {
  1589. var r = new View ();
  1590. var v1 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1591. var v2 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1592. var v3 = new View { CanFocus = true, TabStop = TabBehavior.NoStop };
  1593. r.Add (v1, v2, v3);
  1594. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1595. Assert.False (v1.HasFocus);
  1596. Assert.False (v2.HasFocus);
  1597. Assert.False (v3.HasFocus);
  1598. }
  1599. [Fact]
  1600. public void TabStop_Null_And_CanFocus_False_No_Advance ()
  1601. {
  1602. var r = new View ();
  1603. var v1 = new View ();
  1604. var v2 = new View ();
  1605. var v3 = new View ();
  1606. Assert.False (v1.CanFocus);
  1607. Assert.Null (v1.TabStop);
  1608. r.Add (v1, v2, v3);
  1609. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1610. Assert.False (v1.HasFocus);
  1611. Assert.False (v2.HasFocus);
  1612. Assert.False (v3.HasFocus);
  1613. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1614. Assert.False (v1.HasFocus);
  1615. Assert.False (v2.HasFocus);
  1616. Assert.False (v3.HasFocus);
  1617. r.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1618. Assert.False (v1.HasFocus);
  1619. Assert.False (v2.HasFocus);
  1620. Assert.False (v3.HasFocus);
  1621. r.Dispose ();
  1622. }
  1623. [Fact (Skip = "Causes crash on Ubuntu in Github Action. Bogus test anyway.")]
  1624. public void WindowDispose_CanFocusProblem ()
  1625. {
  1626. // Arrange
  1627. Application.Init ();
  1628. using var top = new Toplevel ();
  1629. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  1630. using var window = new Window ();
  1631. top.Add (window);
  1632. window.Add (view);
  1633. // Act
  1634. RunState rs = Application.Begin (top);
  1635. Application.End (rs);
  1636. top.Dispose ();
  1637. Application.Shutdown ();
  1638. // Assert does Not throw NullReferenceException
  1639. top.SetFocus ();
  1640. }
  1641. }