NavigationTests.cs 60 KB

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