NavigationTests.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. using Xunit.Abstractions;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. using Console = Terminal.Gui.FakeConsole;
  4. namespace Terminal.Gui.ViewTests;
  5. public class NavigationTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public NavigationTests (ITestOutputHelper output) { _output = output; }
  9. [Fact]
  10. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  11. {
  12. var r = new View ();
  13. var v1 = new View { CanFocus = true };
  14. var v2 = new View { CanFocus = true };
  15. var v3 = new View { CanFocus = true };
  16. r.Add (v1, v2, v3);
  17. r.BringSubviewForward (v1);
  18. Assert.True (r.Subviews.IndexOf (v1) == 1);
  19. Assert.True (r.Subviews.IndexOf (v2) == 0);
  20. Assert.True (r.Subviews.IndexOf (v3) == 2);
  21. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  22. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  23. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  24. r.Dispose ();
  25. }
  26. [Fact]
  27. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  28. {
  29. var r = new View ();
  30. var v1 = new View { CanFocus = true };
  31. var v2 = new View { CanFocus = true };
  32. var v3 = new View { CanFocus = true };
  33. r.Add (v1, v2, v3);
  34. r.BringSubviewToFront (v1);
  35. Assert.True (r.Subviews.IndexOf (v1) == 2);
  36. Assert.True (r.Subviews.IndexOf (v2) == 0);
  37. Assert.True (r.Subviews.IndexOf (v3) == 1);
  38. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  39. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  40. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  41. r.Dispose ();
  42. }
  43. [Fact]
  44. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  45. {
  46. Application.Init (new FakeDriver ());
  47. Toplevel t = Application.Top;
  48. var w = new Window ();
  49. var f = new FrameView ();
  50. var v1 = new View { CanFocus = true };
  51. var v2 = new View { CanFocus = true };
  52. f.Add (v1, v2);
  53. w.Add (f);
  54. t.Add (w);
  55. t.Ready += (s, e) =>
  56. {
  57. Assert.True (t.CanFocus);
  58. Assert.True (w.CanFocus);
  59. Assert.True (f.CanFocus);
  60. Assert.True (v1.CanFocus);
  61. Assert.True (v2.CanFocus);
  62. w.CanFocus = false;
  63. Assert.False (w.CanFocus);
  64. Assert.False (f.CanFocus);
  65. Assert.False (v1.CanFocus);
  66. Assert.False (v2.CanFocus);
  67. };
  68. Application.Iteration += (s, a) => Application.RequestStop ();
  69. Application.Run ();
  70. Application.Shutdown ();
  71. }
  72. [Fact]
  73. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  74. {
  75. Application.Init (new FakeDriver ());
  76. Toplevel t = Application.Top;
  77. var w = new Window ();
  78. var f = new FrameView ();
  79. var v1 = new View ();
  80. var v2 = new View { CanFocus = true };
  81. f.Add (v1, v2);
  82. w.Add (f);
  83. t.Add (w);
  84. t.Ready += (s, e) =>
  85. {
  86. Assert.True (t.CanFocus);
  87. Assert.True (w.CanFocus);
  88. Assert.True (f.CanFocus);
  89. Assert.False (v1.CanFocus);
  90. Assert.True (v2.CanFocus);
  91. w.CanFocus = false;
  92. Assert.False (w.CanFocus);
  93. Assert.False (f.CanFocus);
  94. Assert.False (v1.CanFocus);
  95. Assert.False (v2.CanFocus);
  96. w.CanFocus = true;
  97. Assert.True (w.CanFocus);
  98. Assert.True (f.CanFocus);
  99. Assert.False (v1.CanFocus);
  100. Assert.True (v2.CanFocus);
  101. };
  102. Application.Iteration += (s, a) => Application.RequestStop ();
  103. Application.Run ();
  104. Application.Shutdown ();
  105. }
  106. [Fact]
  107. [AutoInitShutdown]
  108. public void CanFocus_Faced_With_Container ()
  109. {
  110. var t = new Toplevel ();
  111. var w = new Window ();
  112. var f = new FrameView ();
  113. var v = new View { CanFocus = true };
  114. f.Add (v);
  115. w.Add (f);
  116. t.Add (w);
  117. Assert.True (t.CanFocus);
  118. Assert.True (w.CanFocus);
  119. Assert.True (f.CanFocus);
  120. Assert.True (v.CanFocus);
  121. f.CanFocus = false;
  122. Assert.False (f.CanFocus);
  123. Assert.True (v.CanFocus);
  124. v.CanFocus = false;
  125. Assert.False (f.CanFocus);
  126. Assert.False (v.CanFocus);
  127. v.CanFocus = true;
  128. Assert.False (f.CanFocus);
  129. Assert.True (v.CanFocus);
  130. t.Dispose ();
  131. }
  132. [Fact]
  133. public void CanFocus_Faced_With_Container_After_Run ()
  134. {
  135. Application.Init (new FakeDriver ());
  136. Toplevel t = Application.Top;
  137. var w = new Window ();
  138. var f = new FrameView ();
  139. var v = new View { CanFocus = true };
  140. f.Add (v);
  141. w.Add (f);
  142. t.Add (w);
  143. t.Ready += (s, e) =>
  144. {
  145. Assert.True (t.CanFocus);
  146. Assert.True (w.CanFocus);
  147. Assert.True (f.CanFocus);
  148. Assert.True (v.CanFocus);
  149. f.CanFocus = false;
  150. Assert.False (f.CanFocus);
  151. Assert.False (v.CanFocus);
  152. v.CanFocus = false;
  153. Assert.False (f.CanFocus);
  154. Assert.False (v.CanFocus);
  155. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  156. Assert.False (f.CanFocus);
  157. Assert.False (v.CanFocus);
  158. f.CanFocus = true;
  159. Assert.True (f.CanFocus);
  160. Assert.True (v.CanFocus);
  161. };
  162. Application.Iteration += (s, a) => Application.RequestStop ();
  163. Application.Run ();
  164. Application.Shutdown ();
  165. }
  166. [Fact]
  167. public void CanFocus_Faced_With_Container_Before_Run ()
  168. {
  169. Application.Init (new FakeDriver ());
  170. Toplevel t = Application.Top;
  171. var w = new Window ();
  172. var f = new FrameView ();
  173. var v = new View { CanFocus = true };
  174. f.Add (v);
  175. w.Add (f);
  176. t.Add (w);
  177. Assert.True (t.CanFocus);
  178. Assert.True (w.CanFocus);
  179. Assert.True (f.CanFocus);
  180. Assert.True (v.CanFocus);
  181. f.CanFocus = false;
  182. Assert.False (f.CanFocus);
  183. Assert.True (v.CanFocus);
  184. v.CanFocus = false;
  185. Assert.False (f.CanFocus);
  186. Assert.False (v.CanFocus);
  187. v.CanFocus = true;
  188. Assert.False (f.CanFocus);
  189. Assert.True (v.CanFocus);
  190. Application.Iteration += (s, a) => Application.RequestStop ();
  191. Application.Run ();
  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.True (v2.TabStop);
  206. v1.CanFocus = true;
  207. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  208. Assert.Equal (1, v1.TabIndex);
  209. Assert.True (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.True (v3.TabStop);
  219. v2.CanFocus = false;
  220. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  221. Assert.Equal (1, v1.TabIndex);
  222. Assert.True (v1.TabStop);
  223. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  224. Assert.Equal (-1, v2.TabIndex);
  225. Assert.False (v2.TabStop);
  226. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  227. Assert.Equal (2, v3.TabIndex);
  228. Assert.True (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. Application.Top.Add (win);
  239. Application.Begin (Application.Top);
  240. Assert.True (view.CanFocus);
  241. Assert.True (view.HasFocus);
  242. view.CanFocus = false;
  243. Assert.False (view.CanFocus);
  244. Assert.False (view.HasFocus);
  245. Assert.Null (Application.Current.Focused);
  246. Assert.Null (Application.Current.MostFocused);
  247. }
  248. [Fact]
  249. [AutoInitShutdown]
  250. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  251. {
  252. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  253. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  254. win1.Add (view1);
  255. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  256. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  257. win2.Add (view2);
  258. Application.Top.Add (win1, win2);
  259. Application.Begin (Application.Top);
  260. Assert.True (view1.CanFocus);
  261. Assert.True (view1.HasFocus);
  262. Assert.True (view2.CanFocus);
  263. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  264. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab));
  265. Assert.True (view1.CanFocus);
  266. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  267. Assert.True (view2.CanFocus);
  268. Assert.True (view2.HasFocus);
  269. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab));
  270. Assert.True (view1.CanFocus);
  271. Assert.True (view1.HasFocus);
  272. Assert.True (view2.CanFocus);
  273. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  274. view1.CanFocus = false;
  275. Assert.False (view1.CanFocus);
  276. Assert.False (view1.HasFocus);
  277. Assert.True (view2.CanFocus);
  278. Assert.True (view2.HasFocus);
  279. Assert.Equal (win2, Application.Current.Focused);
  280. Assert.Equal (view2, Application.Current.MostFocused);
  281. }
  282. [Fact]
  283. [AutoInitShutdown]
  284. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  285. {
  286. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  287. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  288. win1.Add (view1);
  289. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  290. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  291. win2.Add (view2);
  292. Application.Top.Add (win1, win2);
  293. Application.Begin (Application.Top);
  294. Assert.True (view1.CanFocus);
  295. Assert.True (view1.HasFocus);
  296. Assert.True (view2.CanFocus);
  297. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  298. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  299. Assert.True (view1.CanFocus);
  300. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  301. Assert.True (view2.CanFocus);
  302. Assert.True (view2.HasFocus);
  303. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  304. Assert.True (view1.CanFocus);
  305. Assert.True (view1.HasFocus);
  306. Assert.True (view2.CanFocus);
  307. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  308. win1.CanFocus = false;
  309. Assert.False (view1.CanFocus);
  310. Assert.False (view1.HasFocus);
  311. Assert.False (win1.CanFocus);
  312. Assert.False (win1.HasFocus);
  313. Assert.True (view2.CanFocus);
  314. Assert.True (view2.HasFocus);
  315. Assert.Equal (win2, Application.Current.Focused);
  316. Assert.Equal (view2, Application.Current.MostFocused);
  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. Application.Top.Add (win1, win2);
  337. Application.Begin (Application.Top);
  338. Assert.True (view1.CanFocus);
  339. Assert.True (view1.HasFocus);
  340. Assert.True (view2.CanFocus);
  341. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  342. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  343. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  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.Top.NewKeyDownEvent (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. }
  361. [Fact]
  362. [AutoInitShutdown]
  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. Application.Top.Add (view);
  369. view.NewKeyDownEvent (Key.Space);
  370. Assert.True (wasClicked);
  371. view.OnMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  372. Assert.False (wasClicked);
  373. Assert.True (view.Enabled);
  374. Assert.True (view.CanFocus);
  375. Assert.True (view.HasFocus);
  376. view.Enabled = false;
  377. view.NewKeyDownEvent (Key.Space);
  378. Assert.False (wasClicked);
  379. view.OnMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  380. Assert.False (wasClicked);
  381. Assert.False (view.Enabled);
  382. Assert.True (view.CanFocus);
  383. Assert.False (view.HasFocus);
  384. view.SetFocus ();
  385. Assert.False (view.HasFocus);
  386. }
  387. [Fact]
  388. [AutoInitShutdown]
  389. public void Enabled_Sets_Also_Sets_Subviews ()
  390. {
  391. var wasClicked = false;
  392. var button = new Button { Text = "Click Me" };
  393. button.IsDefault = true;
  394. button.Accept += (s, e) => wasClicked = !wasClicked;
  395. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  396. win.Add (button);
  397. Application.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.OnMouseEvent (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.OnMouseEvent (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 ();
  431. Assert.True (button.HasFocus);
  432. Assert.True (win.HasFocus);
  433. Application.RequestStop ();
  434. };
  435. Application.Run ();
  436. Assert.Equal (1, iterations);
  437. }
  438. [Fact]
  439. public void FocusNearestView_Ensure_Focus_Ordered ()
  440. {
  441. var top = new Toplevel ();
  442. var win = new Window ();
  443. var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
  444. win.Add (winSubview);
  445. top.Add (win);
  446. var frm = new FrameView ();
  447. var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
  448. frm.Add (frmSubview);
  449. top.Add (frm);
  450. top.NewKeyDownEvent (Key.Tab);
  451. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  452. top.NewKeyDownEvent (Key.Tab);
  453. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  454. top.NewKeyDownEvent (Key.Tab);
  455. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  456. top.NewKeyDownEvent (Key.Tab.WithShift);
  457. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  458. top.NewKeyDownEvent (Key.Tab.WithShift);
  459. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  460. top.Dispose ();
  461. }
  462. [Fact]
  463. [AutoInitShutdown]
  464. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  465. {
  466. Toplevel top1 = Application.Top;
  467. var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  468. var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
  469. var view2 = new View
  470. {
  471. Id = "view2",
  472. Y = 1,
  473. Width = 10,
  474. Height = 5,
  475. CanFocus = true
  476. };
  477. View view3 = null;
  478. var removed = false;
  479. view2.Enter += (s, e) =>
  480. {
  481. if (!removed)
  482. {
  483. removed = true;
  484. view3 = new View { Id = "view3", Y = 1, Width = 10, Height = 5 };
  485. Application.Current.Add (view3);
  486. Application.Current.BringSubviewToFront (view3);
  487. Assert.False (view3.HasFocus);
  488. }
  489. };
  490. view2.Leave += (s, e) =>
  491. {
  492. Application.Current.Remove (view3);
  493. view3.Dispose ();
  494. view3 = null;
  495. };
  496. top2.Add (view2);
  497. top1.Add (view1, top2);
  498. Application.Begin (top1);
  499. Assert.True (top1.HasFocus);
  500. Assert.True (view1.HasFocus);
  501. Assert.False (view2.HasFocus);
  502. Assert.False (removed);
  503. Assert.Null (view3);
  504. Assert.True (top1.NewKeyDownEvent (Key.Tab.WithCtrl));
  505. Assert.True (top1.HasFocus);
  506. Assert.False (view1.HasFocus);
  507. Assert.True (view2.HasFocus);
  508. Assert.True (removed);
  509. Assert.NotNull (view3);
  510. Exception exception =
  511. Record.Exception (() => top1.NewKeyDownEvent (Key.Tab.WithCtrl));
  512. Assert.Null (exception);
  513. Assert.True (removed);
  514. Assert.Null (view3);
  515. }
  516. [Fact]
  517. [AutoInitShutdown]
  518. public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  519. {
  520. var sbQuiting = false;
  521. var tfQuiting = false;
  522. var topQuiting = false;
  523. var sb = new StatusBar (
  524. new StatusItem []
  525. {
  526. new (
  527. KeyCode.CtrlMask | KeyCode.Q,
  528. "~^Q~ Quit",
  529. () => sbQuiting = true
  530. )
  531. }
  532. );
  533. var tf = new TextField ();
  534. tf.KeyDown += Tf_KeyPressed;
  535. void Tf_KeyPressed (object sender, Key obj)
  536. {
  537. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  538. {
  539. obj.Handled = tfQuiting = true;
  540. }
  541. }
  542. var win = new Window ();
  543. win.Add (sb, tf);
  544. Toplevel top = Application.Top;
  545. top.KeyDown += Top_KeyPress;
  546. void Top_KeyPress (object sender, Key obj)
  547. {
  548. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  549. {
  550. obj.Handled = topQuiting = true;
  551. }
  552. }
  553. top.Add (win);
  554. Application.Begin (top);
  555. Assert.False (sbQuiting);
  556. Assert.False (tfQuiting);
  557. Assert.False (topQuiting);
  558. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  559. Assert.False (sbQuiting);
  560. Assert.True (tfQuiting);
  561. Assert.False (topQuiting);
  562. #if BROKE_WITH_2927
  563. tf.KeyPressed -= Tf_KeyPress;
  564. tfQuiting = false;
  565. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  566. Application.MainLoop.RunIteration ();
  567. Assert.True (sbQuiting);
  568. Assert.False (tfQuiting);
  569. Assert.False (topQuiting);
  570. sb.RemoveItem (0);
  571. sbQuiting = false;
  572. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  573. Application.MainLoop.RunIteration ();
  574. Assert.False (sbQuiting);
  575. Assert.False (tfQuiting);
  576. // This test is now invalid because `win` is focused, so it will receive the keypress
  577. Assert.True (topQuiting);
  578. #endif
  579. }
  580. [Fact]
  581. [AutoInitShutdown]
  582. public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  583. {
  584. var sbQuiting = false;
  585. var tfQuiting = false;
  586. var sb = new StatusBar (
  587. new StatusItem []
  588. {
  589. new (
  590. KeyCode.CtrlMask | KeyCode.Q,
  591. "~^Q~ Quit",
  592. () => sbQuiting = true
  593. )
  594. }
  595. );
  596. var tf = new TextField ();
  597. tf.KeyDown += Tf_KeyPressed;
  598. void Tf_KeyPressed (object sender, Key obj)
  599. {
  600. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  601. {
  602. obj.Handled = tfQuiting = true;
  603. }
  604. }
  605. var win = new Window ();
  606. win.Add (sb, tf);
  607. Toplevel top = Application.Top;
  608. top.Add (win);
  609. Application.Begin (top);
  610. Assert.False (sbQuiting);
  611. Assert.False (tfQuiting);
  612. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  613. Assert.False (sbQuiting);
  614. Assert.True (tfQuiting);
  615. tf.KeyDown -= Tf_KeyPressed;
  616. tfQuiting = false;
  617. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  618. Application.MainLoop.RunIteration ();
  619. #if BROKE_WITH_2927
  620. Assert.True (sbQuiting);
  621. Assert.False (tfQuiting);
  622. #endif
  623. }
  624. [Fact]
  625. public void Navigation_With_Null_Focused_View ()
  626. {
  627. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  628. Application.Init (new FakeDriver ());
  629. Application.Top.Ready += (s, e) => { Assert.Null (Application.Top.Focused); };
  630. // Keyboard navigation with tab
  631. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  632. Application.Iteration += (s, a) => Application.RequestStop ();
  633. Application.Run ();
  634. Application.Shutdown ();
  635. }
  636. [Fact]
  637. [AutoInitShutdown]
  638. public void Remove_Does_Not_Change_Focus ()
  639. {
  640. Assert.True (Application.Top.CanFocus);
  641. Assert.False (Application.Top.HasFocus);
  642. var container = new View { Width = 10, Height = 10 };
  643. var leave = false;
  644. container.Leave += (s, e) => leave = true;
  645. Assert.False (container.CanFocus);
  646. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  647. container.Add (child);
  648. Assert.True (container.CanFocus);
  649. Assert.False (container.HasFocus);
  650. Assert.True (child.CanFocus);
  651. Assert.False (child.HasFocus);
  652. Application.Top.Add (container);
  653. Application.Begin (Application.Top);
  654. Assert.True (Application.Top.CanFocus);
  655. Assert.True (Application.Top.HasFocus);
  656. Assert.True (container.CanFocus);
  657. Assert.True (container.HasFocus);
  658. Assert.True (child.CanFocus);
  659. Assert.True (child.HasFocus);
  660. container.Remove (child);
  661. child.Dispose ();
  662. child = null;
  663. Assert.True (Application.Top.HasFocus);
  664. Assert.True (container.CanFocus);
  665. Assert.True (container.HasFocus);
  666. Assert.Null (child);
  667. Assert.False (leave);
  668. }
  669. [Fact]
  670. [AutoInitShutdown]
  671. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  672. {
  673. Toplevel top = Application.Current;
  674. top.BorderStyle = LineStyle.Single;
  675. var view = new View
  676. {
  677. X = 3,
  678. Y = 2,
  679. Width = 10,
  680. Height = 1,
  681. Text = "0123456789"
  682. };
  683. top.Add (view);
  684. Application.Begin (top);
  685. Assert.Equal (Application.Current, top);
  686. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  687. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  688. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  689. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  690. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  691. Assert.Equal (new Rectangle (0, 0, 20, 10), top.Frame);
  692. _ = TestHelpers.AssertDriverContentsWithFrameAre (
  693. @"
  694. ┌──────────────────┐
  695. │ │
  696. │ │
  697. │ 0123456789 │
  698. │ │
  699. │ │
  700. │ │
  701. │ │
  702. │ │
  703. └──────────────────┘",
  704. _output
  705. );
  706. // top
  707. Assert.Equal (Point.Empty, top.ScreenToFrame (0, 0));
  708. top.Margin.BoundsToScreen (0, 0, out int col, out int row);
  709. Assert.Equal (0, col);
  710. Assert.Equal (0, row);
  711. top.Border.BoundsToScreen (0, 0, out col, out row);
  712. Assert.Equal (0, col);
  713. Assert.Equal (0, row);
  714. top.Padding.BoundsToScreen (0, 0, out col, out row);
  715. Assert.Equal (0, col);
  716. Assert.Equal (0, row);
  717. top.BoundsToScreen (0, 0, out col, out row);
  718. Assert.Equal (1, col);
  719. Assert.Equal (1, row);
  720. top.BoundsToScreen (-1, -1, out col, out row);
  721. Assert.Equal (0, col);
  722. Assert.Equal (0, row);
  723. var found = View.FindDeepestView (top, 0, 0);
  724. Assert.Equal (top, found);
  725. Assert.Equal (0, found.Frame.X);
  726. Assert.Equal (0, found.Frame.Y);
  727. Assert.Equal (new Point (3, 2), top.ScreenToFrame (3, 2));
  728. top.BoundsToScreen (3, 2, out col, out row);
  729. Assert.Equal (4, col);
  730. Assert.Equal (3, row);
  731. found = View.FindDeepestView (top, col, row);
  732. Assert.Equal (view, found);
  733. //Assert.Equal (0, found.FrameToScreen ().X);
  734. //Assert.Equal (0, found.FrameToScreen ().Y);
  735. found = View.FindDeepestView (top, 3, 2);
  736. Assert.Equal (top, found);
  737. //Assert.Equal (3, found.FrameToScreen ().X);
  738. //Assert.Equal (2, found.FrameToScreen ().Y);
  739. Assert.Equal (new Point (13, 2), top.ScreenToFrame (13, 2));
  740. top.BoundsToScreen (12, 2, out col, out row);
  741. Assert.Equal (13, col);
  742. Assert.Equal (3, row);
  743. found = View.FindDeepestView (top, col, row);
  744. Assert.Equal (view, found);
  745. //Assert.Equal (9, found.FrameToScreen ().X);
  746. //Assert.Equal (0, found.FrameToScreen ().Y);
  747. top.BoundsToScreen (13, 2, out col, out row);
  748. Assert.Equal (14, col);
  749. Assert.Equal (3, row);
  750. found = View.FindDeepestView (top, 13, 2);
  751. Assert.Equal (top, found);
  752. //Assert.Equal (13, found.FrameToScreen ().X);
  753. //Assert.Equal (2, found.FrameToScreen ().Y);
  754. Assert.Equal (new Point (14, 3), top.ScreenToFrame (14, 3));
  755. top.BoundsToScreen (14, 3, out col, out row);
  756. Assert.Equal (15, col);
  757. Assert.Equal (4, row);
  758. found = View.FindDeepestView (top, 14, 3);
  759. Assert.Equal (top, found);
  760. //Assert.Equal (14, found.FrameToScreen ().X);
  761. //Assert.Equal (3, found.FrameToScreen ().Y);
  762. // view
  763. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (0, 0));
  764. view.Margin.BoundsToScreen (-3, -2, out col, out row);
  765. Assert.Equal (1, col);
  766. Assert.Equal (1, row);
  767. view.Border.BoundsToScreen (-3, -2, out col, out row);
  768. Assert.Equal (1, col);
  769. Assert.Equal (1, row);
  770. view.Padding.BoundsToScreen (-3, -2, out col, out row);
  771. Assert.Equal (1, col);
  772. Assert.Equal (1, row);
  773. view.BoundsToScreen (-3, -2, out col, out row);
  774. Assert.Equal (1, col);
  775. Assert.Equal (1, row);
  776. view.BoundsToScreen (-4, -3, out col, out row);
  777. Assert.Equal (0, col);
  778. Assert.Equal (0, row);
  779. found = View.FindDeepestView (top, 0, 0);
  780. Assert.Equal (top, found);
  781. //Assert.Equal (0, found.FrameToScreen ().X);
  782. //Assert.Equal (0, found.FrameToScreen ().Y);
  783. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (3, 2));
  784. view.BoundsToScreen (0, 0, out col, out row);
  785. Assert.Equal (4, col);
  786. Assert.Equal (3, row);
  787. found = View.FindDeepestView (top, 4, 3);
  788. Assert.Equal (view, found);
  789. //Assert.Equal (0, found.FrameToScreen ().X);
  790. //Assert.Equal (0, found.FrameToScreen ().Y);
  791. Assert.Equal (new Point (9, -1), view.ScreenToFrame (13, 2));
  792. view.BoundsToScreen (10, 0, out col, out row);
  793. Assert.Equal (14, col);
  794. Assert.Equal (3, row);
  795. found = View.FindDeepestView (top, 14, 3);
  796. Assert.Equal (top, found);
  797. //Assert.Equal (14, found.FrameToScreen ().X);
  798. //Assert.Equal (3, found.FrameToScreen ().Y);
  799. Assert.Equal (new Point (10, 0), view.ScreenToFrame (14, 3));
  800. view.BoundsToScreen (11, 1, out col, out row);
  801. Assert.Equal (15, col);
  802. Assert.Equal (4, row);
  803. found = View.FindDeepestView (top, 15, 4);
  804. Assert.Equal (top, found);
  805. //Assert.Equal (15, found.FrameToScreen ().X);
  806. //Assert.Equal (4, found.FrameToScreen ().Y);
  807. }
  808. [Fact]
  809. [AutoInitShutdown]
  810. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  811. {
  812. var top = new Toplevel
  813. {
  814. X = 3,
  815. Y = 2,
  816. Width = 20,
  817. Height = 10,
  818. BorderStyle = LineStyle.Single
  819. };
  820. var view = new View
  821. {
  822. X = 3,
  823. Y = 2,
  824. Width = 10,
  825. Height = 1,
  826. Text = "0123456789"
  827. };
  828. top.Add (view);
  829. Application.Begin (top);
  830. Assert.Equal (Application.Current, top);
  831. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  832. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  833. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  834. ((FakeDriver)Application.Driver).SetBufferSize (30, 20);
  835. Assert.Equal (new Rectangle (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  836. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  837. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  838. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  839. @"
  840. ┌──────────────────┐
  841. │ │
  842. │ │
  843. │ 0123456789 │
  844. │ │
  845. │ │
  846. │ │
  847. │ │
  848. │ │
  849. └──────────────────┘",
  850. _output
  851. );
  852. // mean the output started at col 3 and line 2
  853. // which result with a width of 23 and a height of 10 on the output
  854. Assert.Equal (new Rectangle (3, 2, 23, 10), frame);
  855. // top
  856. Assert.Equal (new Point (-3, -2), top.ScreenToFrame (0, 0));
  857. top.Margin.BoundsToScreen (-3, -2, out int col, out int row);
  858. Assert.Equal (0, col);
  859. Assert.Equal (0, row);
  860. top.Border.BoundsToScreen (-3, -2, out col, out row);
  861. Assert.Equal (0, col);
  862. Assert.Equal (0, row);
  863. top.Padding.BoundsToScreen (-3, -2, out col, out row);
  864. Assert.Equal (0, col);
  865. Assert.Equal (0, row);
  866. top.BoundsToScreen (-3, -2, out col, out row);
  867. Assert.Equal (1, col);
  868. Assert.Equal (1, row);
  869. top.BoundsToScreen (-4, -3, out col, out row);
  870. Assert.Equal (0, col);
  871. Assert.Equal (0, row);
  872. var found = View.FindDeepestView (top, -4, -3);
  873. Assert.Null (found);
  874. //Assert.Equal (0, found.FrameToScreen ().X);
  875. //Assert.Equal (0, found.FrameToScreen ().Y);
  876. Assert.Equal (Point.Empty, top.ScreenToFrame (3, 2));
  877. top.BoundsToScreen (0, 0, out col, out row);
  878. Assert.Equal (4, col);
  879. Assert.Equal (3, row);
  880. Assert.Equal (top, View.FindDeepestView (top, 3, 2));
  881. //Assert.Equal (0, found.FrameToScreen ().X);
  882. //Assert.Equal (0, found.FrameToScreen ().Y);
  883. Assert.Equal (new Point (10, 0), top.ScreenToFrame (13, 2));
  884. top.BoundsToScreen (10, 0, out col, out row);
  885. Assert.Equal (14, col);
  886. Assert.Equal (3, row);
  887. Assert.Equal (top, View.FindDeepestView (top, 13, 2));
  888. //Assert.Equal (10, found.FrameToScreen ().X);
  889. //Assert.Equal (0, found.FrameToScreen ().Y);
  890. Assert.Equal (new Point (11, 1), top.ScreenToFrame (14, 3));
  891. top.BoundsToScreen (11, 1, out col, out row);
  892. Assert.Equal (15, col);
  893. Assert.Equal (4, row);
  894. Assert.Equal (top, View.FindDeepestView (top, 14, 3));
  895. //Assert.Equal (11, found.FrameToScreen ().X);
  896. //Assert.Equal (1, found.FrameToScreen ().Y);
  897. // view
  898. Assert.Equal (new Point (-7, -5), view.ScreenToFrame (0, 0));
  899. view.Margin.BoundsToScreen (-6, -4, out col, out row);
  900. Assert.Equal (1, col);
  901. Assert.Equal (1, row);
  902. view.Border.BoundsToScreen (-6, -4, out col, out row);
  903. Assert.Equal (1, col);
  904. Assert.Equal (1, row);
  905. view.Padding.BoundsToScreen (-6, -4, out col, out row);
  906. Assert.Equal (1, col);
  907. Assert.Equal (1, row);
  908. view.BoundsToScreen (-6, -4, out col, out row);
  909. Assert.Equal (1, col);
  910. Assert.Equal (1, row);
  911. Assert.Null (View.FindDeepestView (top, 1, 1));
  912. //Assert.Equal (0, found.FrameToScreen ().X);
  913. //Assert.Equal (0, found.FrameToScreen ().Y);
  914. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (3, 2));
  915. view.BoundsToScreen (-3, -2, out col, out row);
  916. Assert.Equal (4, col);
  917. Assert.Equal (3, row);
  918. Assert.Equal (top, View.FindDeepestView (top, 4, 3));
  919. //Assert.Equal (1, found.FrameToScreen ().X);
  920. //Assert.Equal (1, found.FrameToScreen ().Y);
  921. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (6, 4));
  922. view.BoundsToScreen (0, 0, out col, out row);
  923. Assert.Equal (7, col);
  924. Assert.Equal (5, row);
  925. Assert.Equal (view, View.FindDeepestView (top, 7, 5));
  926. //Assert.Equal (0, found.FrameToScreen ().X);
  927. //Assert.Equal (0, found.FrameToScreen ().Y);
  928. Assert.Equal (new Point (6, -1), view.ScreenToFrame (13, 4));
  929. view.BoundsToScreen (7, 0, out col, out row);
  930. Assert.Equal (14, col);
  931. Assert.Equal (5, row);
  932. Assert.Equal (view, View.FindDeepestView (top, 14, 5));
  933. //Assert.Equal (7, found.FrameToScreen ().X);
  934. //Assert.Equal (0, found.FrameToScreen ().Y);
  935. Assert.Equal (new Point (7, -2), view.ScreenToFrame (14, 3));
  936. view.BoundsToScreen (8, -1, out col, out row);
  937. Assert.Equal (15, col);
  938. Assert.Equal (4, row);
  939. Assert.Equal (top, View.FindDeepestView (top, 15, 4));
  940. //Assert.Equal (12, found.FrameToScreen ().X);
  941. //Assert.Equal (2, found.FrameToScreen ().Y);
  942. Assert.Equal (new Point (16, -2), view.ScreenToFrame (23, 3));
  943. view.BoundsToScreen (17, -1, out col, out row);
  944. Assert.Equal (24, col);
  945. Assert.Equal (4, row);
  946. Assert.Null (View.FindDeepestView (top, 24, 4));
  947. //Assert.Equal (0, found.FrameToScreen ().X);
  948. //Assert.Equal (0, found.FrameToScreen ().Y);
  949. }
  950. [Fact]
  951. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  952. {
  953. var r = new View ();
  954. var v1 = new View { CanFocus = true };
  955. var v2 = new View { CanFocus = true };
  956. var v3 = new View { CanFocus = true };
  957. r.Add (v1, v2, v3);
  958. r.SendSubviewBackwards (v3);
  959. Assert.True (r.Subviews.IndexOf (v1) == 0);
  960. Assert.True (r.Subviews.IndexOf (v2) == 2);
  961. Assert.True (r.Subviews.IndexOf (v3) == 1);
  962. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  963. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  964. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  965. r.Dispose ();
  966. }
  967. [Fact]
  968. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  969. {
  970. var r = new View ();
  971. var v1 = new View { CanFocus = true };
  972. var v2 = new View { CanFocus = true };
  973. var v3 = new View { CanFocus = true };
  974. r.Add (v1, v2, v3);
  975. r.SendSubviewToBack (v3);
  976. Assert.True (r.Subviews.IndexOf (v1) == 1);
  977. Assert.True (r.Subviews.IndexOf (v2) == 2);
  978. Assert.True (r.Subviews.IndexOf (v3) == 0);
  979. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  980. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  981. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  982. r.Dispose ();
  983. }
  984. [Fact]
  985. [AutoInitShutdown]
  986. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  987. {
  988. Assert.True (Application.Top.CanFocus);
  989. Assert.False (Application.Top.HasFocus);
  990. Exception exception = Record.Exception (Application.Top.SetFocus);
  991. Assert.Null (exception);
  992. Assert.True (Application.Top.CanFocus);
  993. Assert.True (Application.Top.HasFocus);
  994. }
  995. [Fact]
  996. [AutoInitShutdown]
  997. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  998. {
  999. var view1Leave = false;
  1000. var subView1Leave = false;
  1001. var subView1subView1Leave = false;
  1002. Toplevel top = Application.Top;
  1003. var view1 = new View { CanFocus = true };
  1004. var subView1 = new View { CanFocus = true };
  1005. var subView1subView1 = new View { CanFocus = true };
  1006. view1.Leave += (s, e) => { view1Leave = true; };
  1007. subView1.Leave += (s, e) =>
  1008. {
  1009. subView1.Remove (subView1subView1);
  1010. subView1Leave = true;
  1011. };
  1012. view1.Add (subView1);
  1013. subView1subView1.Leave += (s, e) =>
  1014. {
  1015. // This is never invoked
  1016. subView1subView1Leave = true;
  1017. };
  1018. subView1.Add (subView1subView1);
  1019. var view2 = new View { CanFocus = true };
  1020. top.Add (view1, view2);
  1021. RunState rs = Application.Begin (top);
  1022. view2.SetFocus ();
  1023. Assert.True (view1Leave);
  1024. Assert.True (subView1Leave);
  1025. Assert.False (subView1subView1Leave);
  1026. Application.End (rs);
  1027. subView1subView1.Dispose ();
  1028. }
  1029. [Fact]
  1030. public void Subviews_TabIndexes_AreEqual ()
  1031. {
  1032. var r = new View ();
  1033. var v1 = new View { CanFocus = true };
  1034. var v2 = new View { CanFocus = true };
  1035. var v3 = new View { CanFocus = true };
  1036. r.Add (v1, v2, v3);
  1037. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1038. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1039. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1040. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1041. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1042. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  1043. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  1044. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  1045. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  1046. r.Dispose ();
  1047. }
  1048. [Fact]
  1049. public void TabIndex_Set_CanFocus_False ()
  1050. {
  1051. var r = new View ();
  1052. var v1 = new View { CanFocus = true };
  1053. var v2 = new View { CanFocus = true };
  1054. var v3 = new View { CanFocus = true };
  1055. r.Add (v1, v2, v3);
  1056. v1.CanFocus = false;
  1057. v1.TabIndex = 0;
  1058. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1059. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1060. Assert.Equal (-1, v1.TabIndex);
  1061. r.Dispose ();
  1062. }
  1063. [Fact]
  1064. public void TabIndex_Set_CanFocus_False_To_True ()
  1065. {
  1066. var r = new View ();
  1067. var v1 = new View ();
  1068. var v2 = new View { CanFocus = true };
  1069. var v3 = new View { CanFocus = true };
  1070. r.Add (v1, v2, v3);
  1071. v1.CanFocus = true;
  1072. v1.TabIndex = 1;
  1073. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1074. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1075. r.Dispose ();
  1076. }
  1077. [Fact]
  1078. public void TabIndex_Set_CanFocus_HigherValues ()
  1079. {
  1080. var r = new View ();
  1081. var v1 = new View { CanFocus = true };
  1082. var v2 = new View { CanFocus = true };
  1083. var v3 = new View { CanFocus = true };
  1084. r.Add (v1, v2, v3);
  1085. v1.TabIndex = 3;
  1086. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1087. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1088. r.Dispose ();
  1089. }
  1090. [Fact]
  1091. public void TabIndex_Set_CanFocus_LowerValues ()
  1092. {
  1093. var r = new View ();
  1094. var v1 = new View { CanFocus = true };
  1095. var v2 = new View { CanFocus = true };
  1096. var v3 = new View { CanFocus = true };
  1097. r.Add (v1, v2, v3);
  1098. v1.TabIndex = -1;
  1099. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1100. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1101. r.Dispose ();
  1102. }
  1103. [Fact]
  1104. public void TabIndex_Set_CanFocus_ValidValues ()
  1105. {
  1106. var r = new View ();
  1107. var v1 = new View { CanFocus = true };
  1108. var v2 = new View { CanFocus = true };
  1109. var v3 = new View { CanFocus = true };
  1110. r.Add (v1, v2, v3);
  1111. v1.TabIndex = 1;
  1112. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1113. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1114. v1.TabIndex = 2;
  1115. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1116. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1117. r.Dispose ();
  1118. }
  1119. [Fact]
  1120. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  1121. {
  1122. var r = new View ();
  1123. var v1 = new View { CanFocus = true, TabStop = false };
  1124. var v2 = new View { CanFocus = true, TabStop = false };
  1125. var v3 = new View { CanFocus = true, TabStop = false };
  1126. r.Add (v1, v2, v3);
  1127. r.FocusNext ();
  1128. Assert.False (v1.HasFocus);
  1129. Assert.False (v2.HasFocus);
  1130. Assert.False (v3.HasFocus);
  1131. v1.TabStop = true;
  1132. r.FocusNext ();
  1133. Assert.True (v1.HasFocus);
  1134. Assert.False (v2.HasFocus);
  1135. Assert.False (v3.HasFocus);
  1136. v2.TabStop = true;
  1137. r.FocusNext ();
  1138. Assert.False (v1.HasFocus);
  1139. Assert.True (v2.HasFocus);
  1140. Assert.False (v3.HasFocus);
  1141. v3.TabStop = true;
  1142. r.FocusNext ();
  1143. Assert.False (v1.HasFocus);
  1144. Assert.False (v2.HasFocus);
  1145. Assert.True (v3.HasFocus);
  1146. r.Dispose ();
  1147. }
  1148. [Fact]
  1149. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  1150. {
  1151. var r = new View ();
  1152. var v1 = new View ();
  1153. var v2 = new View ();
  1154. var v3 = new View ();
  1155. r.Add (v1, v2, v3);
  1156. r.FocusNext ();
  1157. Assert.False (v1.HasFocus);
  1158. Assert.False (v2.HasFocus);
  1159. Assert.False (v3.HasFocus);
  1160. v1.CanFocus = true;
  1161. r.FocusNext ();
  1162. Assert.True (v1.HasFocus);
  1163. Assert.False (v2.HasFocus);
  1164. Assert.False (v3.HasFocus);
  1165. v2.CanFocus = true;
  1166. r.FocusNext ();
  1167. Assert.False (v1.HasFocus);
  1168. Assert.True (v2.HasFocus);
  1169. Assert.False (v3.HasFocus);
  1170. v3.CanFocus = true;
  1171. r.FocusNext ();
  1172. Assert.False (v1.HasFocus);
  1173. Assert.False (v2.HasFocus);
  1174. Assert.True (v3.HasFocus);
  1175. r.Dispose ();
  1176. }
  1177. [Fact]
  1178. public void TabStop_And_CanFocus_Are_All_True ()
  1179. {
  1180. var r = new View ();
  1181. var v1 = new View { CanFocus = true };
  1182. var v2 = new View { CanFocus = true };
  1183. var v3 = new View { CanFocus = true };
  1184. r.Add (v1, v2, v3);
  1185. r.FocusNext ();
  1186. Assert.True (v1.HasFocus);
  1187. Assert.False (v2.HasFocus);
  1188. Assert.False (v3.HasFocus);
  1189. r.FocusNext ();
  1190. Assert.False (v1.HasFocus);
  1191. Assert.True (v2.HasFocus);
  1192. Assert.False (v3.HasFocus);
  1193. r.FocusNext ();
  1194. Assert.False (v1.HasFocus);
  1195. Assert.False (v2.HasFocus);
  1196. Assert.True (v3.HasFocus);
  1197. r.Dispose ();
  1198. }
  1199. [Fact]
  1200. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  1201. {
  1202. var r = new View ();
  1203. var v1 = new View { CanFocus = true, TabStop = false };
  1204. var v2 = new View { CanFocus = false, TabStop = true };
  1205. var v3 = new View { CanFocus = false, TabStop = false };
  1206. r.Add (v1, v2, v3);
  1207. r.FocusNext ();
  1208. Assert.False (v1.HasFocus);
  1209. Assert.False (v2.HasFocus);
  1210. Assert.False (v3.HasFocus);
  1211. r.FocusNext ();
  1212. Assert.False (v1.HasFocus);
  1213. Assert.False (v2.HasFocus);
  1214. Assert.False (v3.HasFocus);
  1215. r.FocusNext ();
  1216. Assert.False (v1.HasFocus);
  1217. Assert.False (v2.HasFocus);
  1218. Assert.False (v3.HasFocus);
  1219. r.Dispose ();
  1220. }
  1221. [Fact]
  1222. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  1223. {
  1224. var r = new View ();
  1225. var v1 = new View { CanFocus = true, TabStop = false };
  1226. var v2 = new View { CanFocus = true, TabStop = false };
  1227. var v3 = new View { CanFocus = true, TabStop = false };
  1228. r.Add (v1, v2, v3);
  1229. r.FocusNext ();
  1230. Assert.False (v1.HasFocus);
  1231. Assert.False (v2.HasFocus);
  1232. Assert.False (v3.HasFocus);
  1233. r.FocusNext ();
  1234. Assert.False (v1.HasFocus);
  1235. Assert.False (v2.HasFocus);
  1236. Assert.False (v3.HasFocus);
  1237. r.FocusNext ();
  1238. Assert.False (v1.HasFocus);
  1239. Assert.False (v2.HasFocus);
  1240. Assert.False (v3.HasFocus);
  1241. r.Dispose ();
  1242. }
  1243. [Fact]
  1244. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  1245. {
  1246. var r = new View ();
  1247. var v1 = new View ();
  1248. var v2 = new View ();
  1249. var v3 = new View ();
  1250. r.Add (v1, v2, v3);
  1251. r.FocusNext ();
  1252. Assert.False (v1.HasFocus);
  1253. Assert.False (v2.HasFocus);
  1254. Assert.False (v3.HasFocus);
  1255. r.FocusNext ();
  1256. Assert.False (v1.HasFocus);
  1257. Assert.False (v2.HasFocus);
  1258. Assert.False (v3.HasFocus);
  1259. r.FocusNext ();
  1260. Assert.False (v1.HasFocus);
  1261. Assert.False (v2.HasFocus);
  1262. Assert.False (v3.HasFocus);
  1263. r.Dispose ();
  1264. }
  1265. [Fact]
  1266. [AutoInitShutdown]
  1267. public void WindowDispose_CanFocusProblem ()
  1268. {
  1269. // Arrange
  1270. Application.Init ();
  1271. using var top = new Toplevel ();
  1272. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  1273. using var window = new Window ();
  1274. top.Add (window);
  1275. window.Add (view);
  1276. // Act
  1277. RunState rs = Application.Begin (top);
  1278. Application.End (rs);
  1279. Application.Shutdown ();
  1280. // Assert does Not throw NullReferenceException
  1281. top.SetFocus ();
  1282. }
  1283. }