NavigationTests.cs 64 KB

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