NavigationTests.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. Rectangle screen = top.Margin.ViewportToScreen (new (0, 0, 0, 0));
  709. Assert.Equal (0, screen.X);
  710. Assert.Equal (0, screen.Y);
  711. screen = top.Border.ViewportToScreen (new (0, 0, 0, 0));
  712. Assert.Equal (0, screen.X);
  713. Assert.Equal (0, screen.Y);
  714. screen = top.Padding.ViewportToScreen (new (0, 0, 0, 0));
  715. Assert.Equal (1, screen.X);
  716. Assert.Equal (1, screen.Y);
  717. screen = top.ViewportToScreen (new (0, 0, 0, 0));
  718. Assert.Equal (1, screen.X);
  719. Assert.Equal (1, screen.Y);
  720. screen = top.ViewportToScreen (new (-1, -1, 0, 0));
  721. Assert.Equal (0, screen.X);
  722. Assert.Equal (0, screen.Y);
  723. var found = View.FindDeepestView (top, 0, 0);
  724. Assert.Equal (top.Border, 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. screen = top.ViewportToScreen (new (3, 2, 0, 0));
  729. Assert.Equal (4, screen.X);
  730. Assert.Equal (3, screen.Y);
  731. found = View.FindDeepestView (top, screen.X, screen.Y);
  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. screen = top.ViewportToScreen (new (12, 2, 0, 0));
  741. Assert.Equal (13, screen.X);
  742. Assert.Equal (3, screen.Y);
  743. found = View.FindDeepestView (top, screen.X, screen.Y);
  744. Assert.Equal (view, found);
  745. //Assert.Equal (9, found.FrameToScreen ().X);
  746. //Assert.Equal (0, found.FrameToScreen ().Y);
  747. screen = top.ViewportToScreen (new (13, 2, 0, 0));
  748. Assert.Equal (14, screen.X);
  749. Assert.Equal (3, screen.Y);
  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. screen = top.ViewportToScreen (new (14, 3, 0, 0));
  756. Assert.Equal (15, screen.X);
  757. Assert.Equal (4, screen.Y);
  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. screen = view.Margin.ViewportToScreen (new (-3, -2, 0, 0));
  765. Assert.Equal (1, screen.X);
  766. Assert.Equal (1, screen.Y);
  767. screen = view.Border.ViewportToScreen (new (-3, -2, 0, 0));
  768. Assert.Equal (1, screen.X);
  769. Assert.Equal (1, screen.Y);
  770. screen = view.Padding.ViewportToScreen (new (-3, -2, 0, 0));
  771. Assert.Equal (1, screen.X);
  772. Assert.Equal (1, screen.Y);
  773. screen = view.ViewportToScreen (new (-3, -2, 0, 0));
  774. Assert.Equal (1, screen.X);
  775. Assert.Equal (1, screen.Y);
  776. screen = view.ViewportToScreen (new (-4, -3, 0, 0));
  777. Assert.Equal (0, screen.X);
  778. Assert.Equal (0, screen.Y);
  779. found = View.FindDeepestView (top, 0, 0);
  780. Assert.Equal (top.Border, found);
  781. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (3, 2));
  782. screen = view.ViewportToScreen (new (0, 0, 0, 0));
  783. Assert.Equal (4, screen.X);
  784. Assert.Equal (3, screen.Y);
  785. found = View.FindDeepestView (top, 4, 3);
  786. Assert.Equal (view, found);
  787. Assert.Equal (new Point (9, -1), view.ScreenToFrame (13, 2));
  788. screen = view.ViewportToScreen (new (10, 0, 0, 0));
  789. Assert.Equal (14, screen.X);
  790. Assert.Equal (3, screen.Y);
  791. found = View.FindDeepestView (top, 14, 3);
  792. Assert.Equal (top, found);
  793. Assert.Equal (new Point (10, 0), view.ScreenToFrame (14, 3));
  794. screen = view.ViewportToScreen (new (11, 1, 0, 0));
  795. Assert.Equal (15, screen.X);
  796. Assert.Equal (4, screen.Y);
  797. found = View.FindDeepestView (top, 15, 4);
  798. Assert.Equal (top, found);
  799. }
  800. [Fact]
  801. [AutoInitShutdown]
  802. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  803. {
  804. var top = new Toplevel
  805. {
  806. X = 3,
  807. Y = 2,
  808. Width = 20,
  809. Height = 10,
  810. BorderStyle = LineStyle.Single
  811. };
  812. var view = new View
  813. {
  814. X = 3,
  815. Y = 2,
  816. Width = 10,
  817. Height = 1,
  818. Text = "0123456789"
  819. };
  820. top.Add (view);
  821. Application.Begin (top);
  822. Assert.Equal (Application.Current, top);
  823. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  824. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  825. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  826. ((FakeDriver)Application.Driver).SetBufferSize (30, 20);
  827. Assert.Equal (new Rectangle (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  828. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  829. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  830. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  831. @"
  832. ┌──────────────────┐
  833. │ │
  834. │ │
  835. │ 0123456789 │
  836. │ │
  837. │ │
  838. │ │
  839. │ │
  840. │ │
  841. └──────────────────┘",
  842. _output
  843. );
  844. // mean the output started at col 3 and line 2
  845. // which result with a width of 23 and a height of 10 on the output
  846. Assert.Equal (new Rectangle (3, 2, 23, 10), frame);
  847. // top
  848. Assert.Equal (new Point (-3, -2), top.ScreenToFrame (0, 0));
  849. Rectangle screen = top.Margin.ViewportToScreen (new (-3, -2, 0, 0));
  850. Assert.Equal (0, screen.X);
  851. Assert.Equal (0, screen.Y);
  852. screen = top.Border.ViewportToScreen (new (-3, -2, 0, 0));
  853. Assert.Equal (0, screen.X);
  854. Assert.Equal (0, screen.Y);
  855. screen = top.Padding.ViewportToScreen (new (-3, -2, 0, 0));
  856. Assert.Equal (1, screen.X);
  857. Assert.Equal (1, screen.Y);
  858. screen = top.ViewportToScreen (new (-3, -2, 0, 0));
  859. Assert.Equal (1, screen.X);
  860. Assert.Equal (1, screen.Y);
  861. screen = top.ViewportToScreen (new (-4, -3, 0, 0));
  862. Assert.Equal (0, screen.X);
  863. Assert.Equal (0, screen.Y);
  864. var found = View.FindDeepestView (top, -4, -3);
  865. Assert.Null (found);
  866. Assert.Equal (Point.Empty, top.ScreenToFrame (3, 2));
  867. screen = top.ViewportToScreen (new (0, 0, 0, 0));
  868. Assert.Equal (4, screen.X);
  869. Assert.Equal (3, screen.Y);
  870. Assert.Equal (top.Border, View.FindDeepestView (top, 3, 2));
  871. //Assert.Equal (0, found.FrameToScreen ().X);
  872. //Assert.Equal (0, found.FrameToScreen ().Y);
  873. Assert.Equal (new Point (10, 0), top.ScreenToFrame (13, 2));
  874. screen = top.ViewportToScreen (new (10, 0, 0, 0));
  875. Assert.Equal (14, screen.X);
  876. Assert.Equal (3, screen.Y);
  877. Assert.Equal (top.Border, View.FindDeepestView (top, 13, 2));
  878. //Assert.Equal (10, found.FrameToScreen ().X);
  879. //Assert.Equal (0, found.FrameToScreen ().Y);
  880. Assert.Equal (new Point (11, 1), top.ScreenToFrame (14, 3));
  881. screen = top.ViewportToScreen (new (11, 1, 0, 0));
  882. Assert.Equal (15, screen.X);
  883. Assert.Equal (4, screen.Y);
  884. Assert.Equal (top, View.FindDeepestView (top, 14, 3));
  885. // view
  886. Assert.Equal (new Point (-7, -5), view.ScreenToFrame (0, 0));
  887. screen = view.Margin.ViewportToScreen (new (-6, -4, 0, 0));
  888. Assert.Equal (1, screen.X);
  889. Assert.Equal (1, screen.Y);
  890. screen = view.Border.ViewportToScreen (new (-6, -4, 0, 0));
  891. Assert.Equal (1, screen.X);
  892. Assert.Equal (1, screen.Y);
  893. screen = view.Padding.ViewportToScreen (new (-6, -4, 0, 0));
  894. Assert.Equal (1, screen.X);
  895. Assert.Equal (1, screen.Y);
  896. screen = view.ViewportToScreen (new (-6, -4, 0, 0));
  897. Assert.Equal (1, screen.X);
  898. Assert.Equal (1, screen.Y);
  899. Assert.Null (View.FindDeepestView (top, 1, 1));
  900. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (3, 2));
  901. screen = view.ViewportToScreen (new (-3, -2, 0, 0));
  902. Assert.Equal (4, screen.X);
  903. Assert.Equal (3, screen.Y);
  904. Assert.Equal (top, View.FindDeepestView (top, 4, 3));
  905. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (6, 4));
  906. screen = view.ViewportToScreen (new (0, 0, 0, 0));
  907. Assert.Equal (7, screen.X);
  908. Assert.Equal (5, screen.Y);
  909. Assert.Equal (view, View.FindDeepestView (top, 7, 5));
  910. Assert.Equal (new Point (6, -1), view.ScreenToFrame (13, 4));
  911. screen = view.ViewportToScreen (new (7, 0, 0, 0));
  912. Assert.Equal (14, screen.X);
  913. Assert.Equal (5, screen.Y);
  914. Assert.Equal (view, View.FindDeepestView (top, 14, 5));
  915. Assert.Equal (new Point (7, -2), view.ScreenToFrame (14, 3));
  916. screen = view.ViewportToScreen (new (8, -1, 0, 0));
  917. Assert.Equal (15, screen.X);
  918. Assert.Equal (4, screen.Y);
  919. Assert.Equal (top, View.FindDeepestView (top, 15, 4));
  920. Assert.Equal (new Point (16, -2), view.ScreenToFrame (23, 3));
  921. screen = view.ViewportToScreen (new (17, -1, 0, 0));
  922. Assert.Equal (24, screen.X);
  923. Assert.Equal (4, screen.Y);
  924. Assert.Null (View.FindDeepestView (top, 24, 4));
  925. }
  926. [Fact]
  927. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  928. {
  929. var r = new View ();
  930. var v1 = new View { CanFocus = true };
  931. var v2 = new View { CanFocus = true };
  932. var v3 = new View { CanFocus = true };
  933. r.Add (v1, v2, v3);
  934. r.SendSubviewBackwards (v3);
  935. Assert.True (r.Subviews.IndexOf (v1) == 0);
  936. Assert.True (r.Subviews.IndexOf (v2) == 2);
  937. Assert.True (r.Subviews.IndexOf (v3) == 1);
  938. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  939. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  940. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  941. r.Dispose ();
  942. }
  943. [Fact]
  944. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  945. {
  946. var r = new View ();
  947. var v1 = new View { CanFocus = true };
  948. var v2 = new View { CanFocus = true };
  949. var v3 = new View { CanFocus = true };
  950. r.Add (v1, v2, v3);
  951. r.SendSubviewToBack (v3);
  952. Assert.True (r.Subviews.IndexOf (v1) == 1);
  953. Assert.True (r.Subviews.IndexOf (v2) == 2);
  954. Assert.True (r.Subviews.IndexOf (v3) == 0);
  955. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  956. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  957. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  958. r.Dispose ();
  959. }
  960. [Fact]
  961. [AutoInitShutdown]
  962. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  963. {
  964. Assert.True (Application.Top.CanFocus);
  965. Assert.False (Application.Top.HasFocus);
  966. Exception exception = Record.Exception (Application.Top.SetFocus);
  967. Assert.Null (exception);
  968. Assert.True (Application.Top.CanFocus);
  969. Assert.True (Application.Top.HasFocus);
  970. }
  971. [Fact]
  972. [AutoInitShutdown]
  973. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  974. {
  975. var view1Leave = false;
  976. var subView1Leave = false;
  977. var subView1subView1Leave = false;
  978. Toplevel top = Application.Top;
  979. var view1 = new View { CanFocus = true };
  980. var subView1 = new View { CanFocus = true };
  981. var subView1subView1 = new View { CanFocus = true };
  982. view1.Leave += (s, e) => { view1Leave = true; };
  983. subView1.Leave += (s, e) =>
  984. {
  985. subView1.Remove (subView1subView1);
  986. subView1Leave = true;
  987. };
  988. view1.Add (subView1);
  989. subView1subView1.Leave += (s, e) =>
  990. {
  991. // This is never invoked
  992. subView1subView1Leave = true;
  993. };
  994. subView1.Add (subView1subView1);
  995. var view2 = new View { CanFocus = true };
  996. top.Add (view1, view2);
  997. RunState rs = Application.Begin (top);
  998. view2.SetFocus ();
  999. Assert.True (view1Leave);
  1000. Assert.True (subView1Leave);
  1001. Assert.False (subView1subView1Leave);
  1002. Application.End (rs);
  1003. subView1subView1.Dispose ();
  1004. }
  1005. [Fact]
  1006. public void Subviews_TabIndexes_AreEqual ()
  1007. {
  1008. var r = new View ();
  1009. var v1 = new View { CanFocus = true };
  1010. var v2 = new View { CanFocus = true };
  1011. var v3 = new View { CanFocus = true };
  1012. r.Add (v1, v2, v3);
  1013. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1014. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1015. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1016. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1017. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1018. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  1019. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  1020. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  1021. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  1022. r.Dispose ();
  1023. }
  1024. [Fact]
  1025. public void TabIndex_Set_CanFocus_False ()
  1026. {
  1027. var r = new View ();
  1028. var v1 = new View { CanFocus = true };
  1029. var v2 = new View { CanFocus = true };
  1030. var v3 = new View { CanFocus = true };
  1031. r.Add (v1, v2, v3);
  1032. v1.CanFocus = false;
  1033. v1.TabIndex = 0;
  1034. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1035. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1036. Assert.Equal (-1, v1.TabIndex);
  1037. r.Dispose ();
  1038. }
  1039. [Fact]
  1040. public void TabIndex_Set_CanFocus_False_To_True ()
  1041. {
  1042. var r = new View ();
  1043. var v1 = new View ();
  1044. var v2 = new View { CanFocus = true };
  1045. var v3 = new View { CanFocus = true };
  1046. r.Add (v1, v2, v3);
  1047. v1.CanFocus = true;
  1048. v1.TabIndex = 1;
  1049. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1050. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1051. r.Dispose ();
  1052. }
  1053. [Fact]
  1054. public void TabIndex_Set_CanFocus_HigherValues ()
  1055. {
  1056. var r = new View ();
  1057. var v1 = new View { CanFocus = true };
  1058. var v2 = new View { CanFocus = true };
  1059. var v3 = new View { CanFocus = true };
  1060. r.Add (v1, v2, v3);
  1061. v1.TabIndex = 3;
  1062. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1063. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1064. r.Dispose ();
  1065. }
  1066. [Fact]
  1067. public void TabIndex_Set_CanFocus_LowerValues ()
  1068. {
  1069. var r = new View ();
  1070. var v1 = new View { CanFocus = true };
  1071. var v2 = new View { CanFocus = true };
  1072. var v3 = new View { CanFocus = true };
  1073. r.Add (v1, v2, v3);
  1074. v1.TabIndex = -1;
  1075. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1076. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1077. r.Dispose ();
  1078. }
  1079. [Fact]
  1080. public void TabIndex_Set_CanFocus_ValidValues ()
  1081. {
  1082. var r = new View ();
  1083. var v1 = new View { CanFocus = true };
  1084. var v2 = new View { CanFocus = true };
  1085. var v3 = new View { CanFocus = true };
  1086. r.Add (v1, v2, v3);
  1087. v1.TabIndex = 1;
  1088. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1089. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1090. v1.TabIndex = 2;
  1091. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1092. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1093. r.Dispose ();
  1094. }
  1095. [Fact]
  1096. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  1097. {
  1098. var r = new View ();
  1099. var v1 = new View { CanFocus = true, TabStop = false };
  1100. var v2 = new View { CanFocus = true, TabStop = false };
  1101. var v3 = new View { CanFocus = true, TabStop = false };
  1102. r.Add (v1, v2, v3);
  1103. r.FocusNext ();
  1104. Assert.False (v1.HasFocus);
  1105. Assert.False (v2.HasFocus);
  1106. Assert.False (v3.HasFocus);
  1107. v1.TabStop = true;
  1108. r.FocusNext ();
  1109. Assert.True (v1.HasFocus);
  1110. Assert.False (v2.HasFocus);
  1111. Assert.False (v3.HasFocus);
  1112. v2.TabStop = true;
  1113. r.FocusNext ();
  1114. Assert.False (v1.HasFocus);
  1115. Assert.True (v2.HasFocus);
  1116. Assert.False (v3.HasFocus);
  1117. v3.TabStop = true;
  1118. r.FocusNext ();
  1119. Assert.False (v1.HasFocus);
  1120. Assert.False (v2.HasFocus);
  1121. Assert.True (v3.HasFocus);
  1122. r.Dispose ();
  1123. }
  1124. [Fact]
  1125. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  1126. {
  1127. var r = new View ();
  1128. var v1 = new View ();
  1129. var v2 = new View ();
  1130. var v3 = new View ();
  1131. r.Add (v1, v2, v3);
  1132. r.FocusNext ();
  1133. Assert.False (v1.HasFocus);
  1134. Assert.False (v2.HasFocus);
  1135. Assert.False (v3.HasFocus);
  1136. v1.CanFocus = true;
  1137. r.FocusNext ();
  1138. Assert.True (v1.HasFocus);
  1139. Assert.False (v2.HasFocus);
  1140. Assert.False (v3.HasFocus);
  1141. v2.CanFocus = true;
  1142. r.FocusNext ();
  1143. Assert.False (v1.HasFocus);
  1144. Assert.True (v2.HasFocus);
  1145. Assert.False (v3.HasFocus);
  1146. v3.CanFocus = true;
  1147. r.FocusNext ();
  1148. Assert.False (v1.HasFocus);
  1149. Assert.False (v2.HasFocus);
  1150. Assert.True (v3.HasFocus);
  1151. r.Dispose ();
  1152. }
  1153. [Fact]
  1154. public void TabStop_And_CanFocus_Are_All_True ()
  1155. {
  1156. var r = new View ();
  1157. var v1 = new View { CanFocus = true };
  1158. var v2 = new View { CanFocus = true };
  1159. var v3 = new View { CanFocus = true };
  1160. r.Add (v1, v2, v3);
  1161. r.FocusNext ();
  1162. Assert.True (v1.HasFocus);
  1163. Assert.False (v2.HasFocus);
  1164. Assert.False (v3.HasFocus);
  1165. r.FocusNext ();
  1166. Assert.False (v1.HasFocus);
  1167. Assert.True (v2.HasFocus);
  1168. Assert.False (v3.HasFocus);
  1169. r.FocusNext ();
  1170. Assert.False (v1.HasFocus);
  1171. Assert.False (v2.HasFocus);
  1172. Assert.True (v3.HasFocus);
  1173. r.Dispose ();
  1174. }
  1175. [Fact]
  1176. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  1177. {
  1178. var r = new View ();
  1179. var v1 = new View { CanFocus = true, TabStop = false };
  1180. var v2 = new View { CanFocus = false, TabStop = true };
  1181. var v3 = new View { CanFocus = false, TabStop = false };
  1182. r.Add (v1, v2, v3);
  1183. r.FocusNext ();
  1184. Assert.False (v1.HasFocus);
  1185. Assert.False (v2.HasFocus);
  1186. Assert.False (v3.HasFocus);
  1187. r.FocusNext ();
  1188. Assert.False (v1.HasFocus);
  1189. Assert.False (v2.HasFocus);
  1190. Assert.False (v3.HasFocus);
  1191. r.FocusNext ();
  1192. Assert.False (v1.HasFocus);
  1193. Assert.False (v2.HasFocus);
  1194. Assert.False (v3.HasFocus);
  1195. r.Dispose ();
  1196. }
  1197. [Fact]
  1198. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  1199. {
  1200. var r = new View ();
  1201. var v1 = new View { CanFocus = true, TabStop = false };
  1202. var v2 = new View { CanFocus = true, TabStop = false };
  1203. var v3 = new View { CanFocus = true, TabStop = false };
  1204. r.Add (v1, v2, v3);
  1205. r.FocusNext ();
  1206. Assert.False (v1.HasFocus);
  1207. Assert.False (v2.HasFocus);
  1208. Assert.False (v3.HasFocus);
  1209. r.FocusNext ();
  1210. Assert.False (v1.HasFocus);
  1211. Assert.False (v2.HasFocus);
  1212. Assert.False (v3.HasFocus);
  1213. r.FocusNext ();
  1214. Assert.False (v1.HasFocus);
  1215. Assert.False (v2.HasFocus);
  1216. Assert.False (v3.HasFocus);
  1217. r.Dispose ();
  1218. }
  1219. [Fact]
  1220. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  1221. {
  1222. var r = new View ();
  1223. var v1 = new View ();
  1224. var v2 = new View ();
  1225. var v3 = new View ();
  1226. r.Add (v1, v2, v3);
  1227. r.FocusNext ();
  1228. Assert.False (v1.HasFocus);
  1229. Assert.False (v2.HasFocus);
  1230. Assert.False (v3.HasFocus);
  1231. r.FocusNext ();
  1232. Assert.False (v1.HasFocus);
  1233. Assert.False (v2.HasFocus);
  1234. Assert.False (v3.HasFocus);
  1235. r.FocusNext ();
  1236. Assert.False (v1.HasFocus);
  1237. Assert.False (v2.HasFocus);
  1238. Assert.False (v3.HasFocus);
  1239. r.Dispose ();
  1240. }
  1241. [Fact]
  1242. [AutoInitShutdown]
  1243. public void WindowDispose_CanFocusProblem ()
  1244. {
  1245. // Arrange
  1246. Application.Init ();
  1247. using var top = new Toplevel ();
  1248. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  1249. using var window = new Window ();
  1250. top.Add (window);
  1251. window.Add (view);
  1252. // Act
  1253. RunState rs = Application.Begin (top);
  1254. Application.End (rs);
  1255. Application.Shutdown ();
  1256. // Assert does Not throw NullReferenceException
  1257. top.SetFocus ();
  1258. }
  1259. }