NavigationTests.cs 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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. }