NavigationTests.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using NStack;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.ViewTests {
  8. public class NavigationTests {
  9. readonly ITestOutputHelper output;
  10. public NavigationTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact]
  15. public void FocusNearestView_Ensure_Focus_Ordered ()
  16. {
  17. var top = new Toplevel ();
  18. var win = new Window ();
  19. var winSubview = new View ("WindowSubview") {
  20. CanFocus = true
  21. };
  22. win.Add (winSubview);
  23. top.Add (win);
  24. var frm = new FrameView ();
  25. var frmSubview = new View ("FrameSubview") {
  26. CanFocus = true
  27. };
  28. frm.Add (frmSubview);
  29. top.Add (frm);
  30. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  31. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  32. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  33. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  34. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  35. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  36. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  37. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  38. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  39. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  40. }
  41. [Fact]
  42. public void Subviews_TabIndexes_AreEqual ()
  43. {
  44. var r = new View ();
  45. var v1 = new View () { CanFocus = true };
  46. var v2 = new View () { CanFocus = true };
  47. var v3 = new View () { CanFocus = true };
  48. r.Add (v1, v2, v3);
  49. Assert.True (r.Subviews.IndexOf (v1) == 0);
  50. Assert.True (r.Subviews.IndexOf (v2) == 1);
  51. Assert.True (r.Subviews.IndexOf (v3) == 2);
  52. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  53. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  54. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  55. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  56. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  57. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  58. }
  59. [Fact]
  60. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  61. {
  62. var r = new View ();
  63. var v1 = new View () { CanFocus = true };
  64. var v2 = new View () { CanFocus = true };
  65. var v3 = new View () { CanFocus = true };
  66. r.Add (v1, v2, v3);
  67. r.BringSubviewToFront (v1);
  68. Assert.True (r.Subviews.IndexOf (v1) == 2);
  69. Assert.True (r.Subviews.IndexOf (v2) == 0);
  70. Assert.True (r.Subviews.IndexOf (v3) == 1);
  71. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  72. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  73. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  74. }
  75. [Fact]
  76. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  77. {
  78. var r = new View ();
  79. var v1 = new View () { CanFocus = true };
  80. var v2 = new View () { CanFocus = true };
  81. var v3 = new View () { CanFocus = true };
  82. r.Add (v1, v2, v3);
  83. r.BringSubviewForward (v1);
  84. Assert.True (r.Subviews.IndexOf (v1) == 1);
  85. Assert.True (r.Subviews.IndexOf (v2) == 0);
  86. Assert.True (r.Subviews.IndexOf (v3) == 2);
  87. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  88. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  89. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  90. }
  91. [Fact]
  92. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  93. {
  94. var r = new View ();
  95. var v1 = new View () { CanFocus = true };
  96. var v2 = new View () { CanFocus = true };
  97. var v3 = new View () { CanFocus = true };
  98. r.Add (v1, v2, v3);
  99. r.SendSubviewToBack (v3);
  100. Assert.True (r.Subviews.IndexOf (v1) == 1);
  101. Assert.True (r.Subviews.IndexOf (v2) == 2);
  102. Assert.True (r.Subviews.IndexOf (v3) == 0);
  103. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  104. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  105. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  106. }
  107. [Fact]
  108. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  109. {
  110. var r = new View ();
  111. var v1 = new View () { CanFocus = true };
  112. var v2 = new View () { CanFocus = true };
  113. var v3 = new View () { CanFocus = true };
  114. r.Add (v1, v2, v3);
  115. r.SendSubviewBackwards (v3);
  116. Assert.True (r.Subviews.IndexOf (v1) == 0);
  117. Assert.True (r.Subviews.IndexOf (v2) == 2);
  118. Assert.True (r.Subviews.IndexOf (v3) == 1);
  119. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  120. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  121. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  122. }
  123. [Fact]
  124. public void TabIndex_Set_CanFocus_ValidValues ()
  125. {
  126. var r = new View ();
  127. var v1 = new View () { CanFocus = true };
  128. var v2 = new View () { CanFocus = true };
  129. var v3 = new View () { CanFocus = true };
  130. r.Add (v1, v2, v3);
  131. v1.TabIndex = 1;
  132. Assert.True (r.Subviews.IndexOf (v1) == 0);
  133. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  134. v1.TabIndex = 2;
  135. Assert.True (r.Subviews.IndexOf (v1) == 0);
  136. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  137. }
  138. [Fact]
  139. public void TabIndex_Set_CanFocus_HigherValues ()
  140. {
  141. var r = new View ();
  142. var v1 = new View () { CanFocus = true };
  143. var v2 = new View () { CanFocus = true };
  144. var v3 = new View () { CanFocus = true };
  145. r.Add (v1, v2, v3);
  146. v1.TabIndex = 3;
  147. Assert.True (r.Subviews.IndexOf (v1) == 0);
  148. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  149. }
  150. [Fact]
  151. public void TabIndex_Set_CanFocus_LowerValues ()
  152. {
  153. var r = new View ();
  154. var v1 = new View () { CanFocus = true };
  155. var v2 = new View () { CanFocus = true };
  156. var v3 = new View () { CanFocus = true };
  157. r.Add (v1, v2, v3);
  158. v1.TabIndex = -1;
  159. Assert.True (r.Subviews.IndexOf (v1) == 0);
  160. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  161. }
  162. [Fact]
  163. public void TabIndex_Set_CanFocus_False ()
  164. {
  165. var r = new View ();
  166. var v1 = new View () { CanFocus = true };
  167. var v2 = new View () { CanFocus = true };
  168. var v3 = new View () { CanFocus = true };
  169. r.Add (v1, v2, v3);
  170. v1.CanFocus = false;
  171. v1.TabIndex = 0;
  172. Assert.True (r.Subviews.IndexOf (v1) == 0);
  173. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  174. Assert.Equal (-1, v1.TabIndex);
  175. }
  176. [Fact]
  177. public void TabIndex_Set_CanFocus_False_To_True ()
  178. {
  179. var r = new View ();
  180. var v1 = new View ();
  181. var v2 = new View () { CanFocus = true };
  182. var v3 = new View () { CanFocus = true };
  183. r.Add (v1, v2, v3);
  184. v1.CanFocus = true;
  185. v1.TabIndex = 1;
  186. Assert.True (r.Subviews.IndexOf (v1) == 0);
  187. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  188. }
  189. [Fact]
  190. public void TabStop_And_CanFocus_Are_All_True ()
  191. {
  192. var r = new View ();
  193. var v1 = new View () { CanFocus = true };
  194. var v2 = new View () { CanFocus = true };
  195. var v3 = new View () { CanFocus = true };
  196. r.Add (v1, v2, v3);
  197. r.FocusNext ();
  198. Assert.True (v1.HasFocus);
  199. Assert.False (v2.HasFocus);
  200. Assert.False (v3.HasFocus);
  201. r.FocusNext ();
  202. Assert.False (v1.HasFocus);
  203. Assert.True (v2.HasFocus);
  204. Assert.False (v3.HasFocus);
  205. r.FocusNext ();
  206. Assert.False (v1.HasFocus);
  207. Assert.False (v2.HasFocus);
  208. Assert.True (v3.HasFocus);
  209. }
  210. [Fact]
  211. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  212. {
  213. var r = new View ();
  214. var v1 = new View ();
  215. var v2 = new View ();
  216. var v3 = new View ();
  217. r.Add (v1, v2, v3);
  218. r.FocusNext ();
  219. Assert.False (v1.HasFocus);
  220. Assert.False (v2.HasFocus);
  221. Assert.False (v3.HasFocus);
  222. r.FocusNext ();
  223. Assert.False (v1.HasFocus);
  224. Assert.False (v2.HasFocus);
  225. Assert.False (v3.HasFocus);
  226. r.FocusNext ();
  227. Assert.False (v1.HasFocus);
  228. Assert.False (v2.HasFocus);
  229. Assert.False (v3.HasFocus);
  230. }
  231. [Fact]
  232. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  233. {
  234. var r = new View ();
  235. var v1 = new View () { CanFocus = true, TabStop = false };
  236. var v2 = new View () { CanFocus = true, TabStop = false };
  237. var v3 = new View () { CanFocus = true, TabStop = false };
  238. r.Add (v1, v2, v3);
  239. r.FocusNext ();
  240. Assert.False (v1.HasFocus);
  241. Assert.False (v2.HasFocus);
  242. Assert.False (v3.HasFocus);
  243. r.FocusNext ();
  244. Assert.False (v1.HasFocus);
  245. Assert.False (v2.HasFocus);
  246. Assert.False (v3.HasFocus);
  247. r.FocusNext ();
  248. Assert.False (v1.HasFocus);
  249. Assert.False (v2.HasFocus);
  250. Assert.False (v3.HasFocus);
  251. }
  252. [Fact]
  253. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  254. {
  255. var r = new View ();
  256. var v1 = new View () { CanFocus = true, TabStop = false };
  257. var v2 = new View () { CanFocus = false, TabStop = true };
  258. var v3 = new View () { CanFocus = false, TabStop = false };
  259. r.Add (v1, v2, v3);
  260. r.FocusNext ();
  261. Assert.False (v1.HasFocus);
  262. Assert.False (v2.HasFocus);
  263. Assert.False (v3.HasFocus);
  264. r.FocusNext ();
  265. Assert.False (v1.HasFocus);
  266. Assert.False (v2.HasFocus);
  267. Assert.False (v3.HasFocus);
  268. r.FocusNext ();
  269. Assert.False (v1.HasFocus);
  270. Assert.False (v2.HasFocus);
  271. Assert.False (v3.HasFocus);
  272. }
  273. [Fact]
  274. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  275. {
  276. var r = new View ();
  277. var v1 = new View ();
  278. var v2 = new View ();
  279. var v3 = new View ();
  280. r.Add (v1, v2, v3);
  281. r.FocusNext ();
  282. Assert.False (v1.HasFocus);
  283. Assert.False (v2.HasFocus);
  284. Assert.False (v3.HasFocus);
  285. v1.CanFocus = true;
  286. r.FocusNext ();
  287. Assert.True (v1.HasFocus);
  288. Assert.False (v2.HasFocus);
  289. Assert.False (v3.HasFocus);
  290. v2.CanFocus = true;
  291. r.FocusNext ();
  292. Assert.False (v1.HasFocus);
  293. Assert.True (v2.HasFocus);
  294. Assert.False (v3.HasFocus);
  295. v3.CanFocus = true;
  296. r.FocusNext ();
  297. Assert.False (v1.HasFocus);
  298. Assert.False (v2.HasFocus);
  299. Assert.True (v3.HasFocus);
  300. }
  301. [Fact]
  302. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  303. {
  304. var r = new View ();
  305. var v1 = new View () { CanFocus = true, TabStop = false };
  306. var v2 = new View () { CanFocus = true, TabStop = false };
  307. var v3 = new View () { CanFocus = true, TabStop = false };
  308. r.Add (v1, v2, v3);
  309. r.FocusNext ();
  310. Assert.False (v1.HasFocus);
  311. Assert.False (v2.HasFocus);
  312. Assert.False (v3.HasFocus);
  313. v1.TabStop = true;
  314. r.FocusNext ();
  315. Assert.True (v1.HasFocus);
  316. Assert.False (v2.HasFocus);
  317. Assert.False (v3.HasFocus);
  318. v2.TabStop = true;
  319. r.FocusNext ();
  320. Assert.False (v1.HasFocus);
  321. Assert.True (v2.HasFocus);
  322. Assert.False (v3.HasFocus);
  323. v3.TabStop = true;
  324. r.FocusNext ();
  325. Assert.False (v1.HasFocus);
  326. Assert.False (v2.HasFocus);
  327. Assert.True (v3.HasFocus);
  328. }
  329. [Fact]
  330. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  331. {
  332. var r = new View ();
  333. var v1 = new View ("1");
  334. var v2 = new View ("2");
  335. var v3 = new View ("3");
  336. r.Add (v1, v2, v3);
  337. v2.CanFocus = true;
  338. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  339. Assert.Equal (0, v2.TabIndex);
  340. Assert.True (v2.TabStop);
  341. v1.CanFocus = true;
  342. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  343. Assert.Equal (1, v1.TabIndex);
  344. Assert.True (v1.TabStop);
  345. v1.TabIndex = 2;
  346. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  347. Assert.Equal (1, v1.TabIndex);
  348. v3.CanFocus = true;
  349. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  350. Assert.Equal (1, v1.TabIndex);
  351. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  352. Assert.Equal (2, v3.TabIndex);
  353. Assert.True (v3.TabStop);
  354. v2.CanFocus = false;
  355. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  356. Assert.Equal (1, v1.TabIndex);
  357. Assert.True (v1.TabStop);
  358. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  359. Assert.Equal (-1, v2.TabIndex);
  360. Assert.False (v2.TabStop);
  361. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  362. Assert.Equal (2, v3.TabIndex);
  363. Assert.True (v3.TabStop);
  364. }
  365. [Fact]
  366. [AutoInitShutdown]
  367. public void CanFocus_Faced_With_Container ()
  368. {
  369. var t = new Toplevel ();
  370. var w = new Window ();
  371. var f = new FrameView ();
  372. var v = new View () { CanFocus = true };
  373. f.Add (v);
  374. w.Add (f);
  375. t.Add (w);
  376. Assert.True (t.CanFocus);
  377. Assert.True (w.CanFocus);
  378. Assert.True (f.CanFocus);
  379. Assert.True (v.CanFocus);
  380. f.CanFocus = false;
  381. Assert.False (f.CanFocus);
  382. Assert.True (v.CanFocus);
  383. v.CanFocus = false;
  384. Assert.False (f.CanFocus);
  385. Assert.False (v.CanFocus);
  386. v.CanFocus = true;
  387. Assert.False (f.CanFocus);
  388. Assert.True (v.CanFocus);
  389. }
  390. [Fact]
  391. public void CanFocus_Faced_With_Container_Before_Run ()
  392. {
  393. Application.Init (new FakeDriver ());
  394. var t = Application.Top;
  395. var w = new Window ("w");
  396. var f = new FrameView ("f");
  397. var v = new View ("v") { CanFocus = true };
  398. f.Add (v);
  399. w.Add (f);
  400. t.Add (w);
  401. Assert.True (t.CanFocus);
  402. Assert.True (w.CanFocus);
  403. Assert.True (f.CanFocus);
  404. Assert.True (v.CanFocus);
  405. f.CanFocus = false;
  406. Assert.False (f.CanFocus);
  407. Assert.True (v.CanFocus);
  408. v.CanFocus = false;
  409. Assert.False (f.CanFocus);
  410. Assert.False (v.CanFocus);
  411. v.CanFocus = true;
  412. Assert.False (f.CanFocus);
  413. Assert.True (v.CanFocus);
  414. Application.Iteration += () => Application.RequestStop ();
  415. Application.Run ();
  416. Application.Shutdown ();
  417. }
  418. [Fact]
  419. public void CanFocus_Faced_With_Container_After_Run ()
  420. {
  421. Application.Init (new FakeDriver ());
  422. var t = Application.Top;
  423. var w = new Window ("w");
  424. var f = new FrameView ("f");
  425. var v = new View ("v") { CanFocus = true };
  426. f.Add (v);
  427. w.Add (f);
  428. t.Add (w);
  429. t.Ready += (s, e) => {
  430. Assert.True (t.CanFocus);
  431. Assert.True (w.CanFocus);
  432. Assert.True (f.CanFocus);
  433. Assert.True (v.CanFocus);
  434. f.CanFocus = false;
  435. Assert.False (f.CanFocus);
  436. Assert.False (v.CanFocus);
  437. v.CanFocus = false;
  438. Assert.False (f.CanFocus);
  439. Assert.False (v.CanFocus);
  440. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  441. Assert.False (f.CanFocus);
  442. Assert.False (v.CanFocus);
  443. f.CanFocus = true;
  444. Assert.True (f.CanFocus);
  445. Assert.True (v.CanFocus);
  446. };
  447. Application.Iteration += () => Application.RequestStop ();
  448. Application.Run ();
  449. Application.Shutdown ();
  450. }
  451. [Fact]
  452. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  453. {
  454. Application.Init (new FakeDriver ());
  455. var t = Application.Top;
  456. var w = new Window ("w");
  457. var f = new FrameView ("f");
  458. var v1 = new View ("v1") { CanFocus = true };
  459. var v2 = new View ("v2") { CanFocus = true };
  460. f.Add (v1, v2);
  461. w.Add (f);
  462. t.Add (w);
  463. t.Ready += (s, e) => {
  464. Assert.True (t.CanFocus);
  465. Assert.True (w.CanFocus);
  466. Assert.True (f.CanFocus);
  467. Assert.True (v1.CanFocus);
  468. Assert.True (v2.CanFocus);
  469. w.CanFocus = false;
  470. Assert.False (w.CanFocus);
  471. Assert.False (f.CanFocus);
  472. Assert.False (v1.CanFocus);
  473. Assert.False (v2.CanFocus);
  474. };
  475. Application.Iteration += () => Application.RequestStop ();
  476. Application.Run ();
  477. Application.Shutdown ();
  478. }
  479. [Fact]
  480. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  481. {
  482. Application.Init (new FakeDriver ());
  483. var t = Application.Top;
  484. var w = new Window ("w");
  485. var f = new FrameView ("f");
  486. var v1 = new View ("v1");
  487. var v2 = new View ("v2") { CanFocus = true };
  488. f.Add (v1, v2);
  489. w.Add (f);
  490. t.Add (w);
  491. t.Ready += (s, e) => {
  492. Assert.True (t.CanFocus);
  493. Assert.True (w.CanFocus);
  494. Assert.True (f.CanFocus);
  495. Assert.False (v1.CanFocus);
  496. Assert.True (v2.CanFocus);
  497. w.CanFocus = false;
  498. Assert.False (w.CanFocus);
  499. Assert.False (f.CanFocus);
  500. Assert.False (v1.CanFocus);
  501. Assert.False (v2.CanFocus);
  502. w.CanFocus = true;
  503. Assert.True (w.CanFocus);
  504. Assert.True (f.CanFocus);
  505. Assert.False (v1.CanFocus);
  506. Assert.True (v2.CanFocus);
  507. };
  508. Application.Iteration += () => Application.RequestStop ();
  509. Application.Run ();
  510. Application.Shutdown ();
  511. }
  512. [Fact]
  513. public void Navigation_With_Null_Focused_View ()
  514. {
  515. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  516. Application.Init (new FakeDriver ());
  517. Application.Top.Ready += (s, e) => {
  518. Assert.Null (Application.Top.Focused);
  519. };
  520. // Keyboard navigation with tab
  521. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  522. Application.Iteration += () => Application.RequestStop ();
  523. Application.Run ();
  524. Application.Shutdown ();
  525. }
  526. [Fact]
  527. [AutoInitShutdown]
  528. public void Enabled_False_Sets_HasFocus_To_False ()
  529. {
  530. var wasClicked = false;
  531. var view = new Button ("Click Me");
  532. view.Clicked += (s, e) => wasClicked = !wasClicked;
  533. Application.Top.Add (view);
  534. view.ProcessKey (new KeyEvent (Key.Enter, null));
  535. Assert.True (wasClicked);
  536. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  537. Assert.False (wasClicked);
  538. Assert.True (view.Enabled);
  539. Assert.True (view.CanFocus);
  540. Assert.True (view.HasFocus);
  541. view.Enabled = false;
  542. view.ProcessKey (new KeyEvent (Key.Enter, null));
  543. Assert.False (wasClicked);
  544. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  545. Assert.False (wasClicked);
  546. Assert.False (view.Enabled);
  547. Assert.True (view.CanFocus);
  548. Assert.False (view.HasFocus);
  549. view.SetFocus ();
  550. Assert.False (view.HasFocus);
  551. }
  552. [Fact]
  553. [AutoInitShutdown]
  554. public void Enabled_Sets_Also_Sets_Subviews ()
  555. {
  556. var wasClicked = false;
  557. var button = new Button ("Click Me");
  558. button.Clicked += (s, e) => wasClicked = !wasClicked;
  559. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  560. win.Add (button);
  561. Application.Top.Add (win);
  562. var iterations = 0;
  563. Application.Iteration += () => {
  564. iterations++;
  565. button.ProcessKey (new KeyEvent (Key.Enter, null));
  566. Assert.True (wasClicked);
  567. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  568. Assert.False (wasClicked);
  569. Assert.True (button.Enabled);
  570. Assert.True (button.CanFocus);
  571. Assert.True (button.HasFocus);
  572. Assert.True (win.Enabled);
  573. Assert.True (win.CanFocus);
  574. Assert.True (win.HasFocus);
  575. win.Enabled = false;
  576. button.ProcessKey (new KeyEvent (Key.Enter, null));
  577. Assert.False (wasClicked);
  578. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  579. Assert.False (wasClicked);
  580. Assert.False (button.Enabled);
  581. Assert.True (button.CanFocus);
  582. Assert.False (button.HasFocus);
  583. Assert.False (win.Enabled);
  584. Assert.True (win.CanFocus);
  585. Assert.False (win.HasFocus);
  586. button.SetFocus ();
  587. Assert.False (button.HasFocus);
  588. Assert.False (win.HasFocus);
  589. win.SetFocus ();
  590. Assert.False (button.HasFocus);
  591. Assert.False (win.HasFocus);
  592. win.Enabled = true;
  593. win.FocusFirst ();
  594. Assert.True (button.HasFocus);
  595. Assert.True (win.HasFocus);
  596. Application.RequestStop ();
  597. };
  598. Application.Run ();
  599. Assert.Equal (1, iterations);
  600. }
  601. [Fact]
  602. [AutoInitShutdown]
  603. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  604. {
  605. var view = new View () { CanFocus = true };
  606. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  607. win.Add (view);
  608. Application.Top.Add (win);
  609. Application.Begin (Application.Top);
  610. Assert.True (view.CanFocus);
  611. Assert.True (view.HasFocus);
  612. view.CanFocus = false;
  613. Assert.False (view.CanFocus);
  614. Assert.False (view.HasFocus);
  615. Assert.Null (Application.Current.Focused);
  616. Assert.Null (Application.Current.MostFocused);
  617. }
  618. [Fact]
  619. [AutoInitShutdown]
  620. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  621. {
  622. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  623. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  624. win1.Add (view1);
  625. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  626. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  627. win2.Add (view2);
  628. Application.Top.Add (win1, win2);
  629. Application.Begin (Application.Top);
  630. Assert.True (view1.CanFocus);
  631. Assert.True (view1.HasFocus);
  632. Assert.True (view2.CanFocus);
  633. Assert.False (view2.HasFocus);
  634. view1.CanFocus = false;
  635. Assert.False (view1.CanFocus);
  636. Assert.False (view1.HasFocus);
  637. Assert.Equal (win2, Application.Current.Focused);
  638. Assert.Equal (view2, Application.Current.MostFocused);
  639. }
  640. [Fact]
  641. [AutoInitShutdown]
  642. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  643. {
  644. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  645. var view12 = new View () { Id = "view12", Y = 5, Width = 10, Height = 1, CanFocus = true };
  646. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  647. win1.Add (view1, view12);
  648. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  649. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  650. win2.Add (view2);
  651. Application.Top.Add (win1, win2);
  652. Application.Begin (Application.Top);
  653. Assert.True (view1.CanFocus);
  654. Assert.True (view1.HasFocus);
  655. Assert.True (view2.CanFocus);
  656. Assert.False (view2.HasFocus);
  657. view1.CanFocus = false;
  658. Assert.False (view1.CanFocus);
  659. Assert.False (view1.HasFocus);
  660. Assert.Equal (win1, Application.Current.Focused);
  661. Assert.Equal (view12, Application.Current.MostFocused);
  662. }
  663. [Fact]
  664. [AutoInitShutdown]
  665. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  666. {
  667. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  668. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  669. win1.Add (view1);
  670. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  671. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  672. win2.Add (view2);
  673. Application.Top.Add (win1, win2);
  674. Application.Begin (Application.Top);
  675. Assert.True (view1.CanFocus);
  676. Assert.True (view1.HasFocus);
  677. Assert.True (view2.CanFocus);
  678. Assert.False (view2.HasFocus);
  679. win1.CanFocus = false;
  680. Assert.False (view1.CanFocus);
  681. Assert.False (view1.HasFocus);
  682. Assert.False (win1.CanFocus);
  683. Assert.False (win1.HasFocus);
  684. Assert.Equal (win2, Application.Current.Focused);
  685. Assert.Equal (view2, Application.Current.MostFocused);
  686. }
  687. [Fact]
  688. [AutoInitShutdown]
  689. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  690. {
  691. var sbQuiting = false;
  692. var tfQuiting = false;
  693. var topQuiting = false;
  694. var sb = new StatusBar (new StatusItem [] {
  695. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  696. });
  697. var tf = new TextField ();
  698. tf.KeyPress += Tf_KeyPress;
  699. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  700. {
  701. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  702. obj.Handled = tfQuiting = true;
  703. }
  704. }
  705. var win = new Window ();
  706. win.Add (sb, tf);
  707. var top = Application.Top;
  708. top.KeyPress += Top_KeyPress;
  709. void Top_KeyPress (object sender, KeyEventEventArgs obj)
  710. {
  711. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  712. obj.Handled = topQuiting = true;
  713. }
  714. }
  715. top.Add (win);
  716. Application.Begin (top);
  717. Assert.False (sbQuiting);
  718. Assert.False (tfQuiting);
  719. Assert.False (topQuiting);
  720. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  721. Assert.False (sbQuiting);
  722. Assert.True (tfQuiting);
  723. Assert.False (topQuiting);
  724. tf.KeyPress -= Tf_KeyPress;
  725. tfQuiting = false;
  726. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  727. Application.MainLoop.MainIteration ();
  728. Assert.True (sbQuiting);
  729. Assert.False (tfQuiting);
  730. Assert.False (topQuiting);
  731. sb.RemoveItem (0);
  732. sbQuiting = false;
  733. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  734. Application.MainLoop.MainIteration ();
  735. Assert.False (sbQuiting);
  736. Assert.False (tfQuiting);
  737. Assert.True (topQuiting);
  738. }
  739. [Fact]
  740. [AutoInitShutdown]
  741. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  742. {
  743. var sbQuiting = false;
  744. var tfQuiting = false;
  745. var sb = new StatusBar (new StatusItem [] {
  746. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  747. });
  748. var tf = new TextField ();
  749. tf.KeyPress += Tf_KeyPress;
  750. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  751. {
  752. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  753. obj.Handled = tfQuiting = true;
  754. }
  755. }
  756. var win = new Window ();
  757. win.Add (sb, tf);
  758. var top = Application.Top;
  759. top.Add (win);
  760. Application.Begin (top);
  761. Assert.False (sbQuiting);
  762. Assert.False (tfQuiting);
  763. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  764. Assert.False (sbQuiting);
  765. Assert.True (tfQuiting);
  766. tf.KeyPress -= Tf_KeyPress;
  767. tfQuiting = false;
  768. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  769. Application.MainLoop.MainIteration ();
  770. Assert.True (sbQuiting);
  771. Assert.False (tfQuiting);
  772. }
  773. [Fact]
  774. [AutoInitShutdown]
  775. public void WindowDispose_CanFocusProblem ()
  776. {
  777. // Arrange
  778. Application.Init ();
  779. using var top = Toplevel.Create ();
  780. using var view = new View (
  781. x: 0,
  782. y: 1,
  783. text: nameof (WindowDispose_CanFocusProblem));
  784. using var window = new Window ();
  785. top.Add (window);
  786. window.Add (view);
  787. // Act
  788. Application.Begin (top);
  789. Application.Shutdown ();
  790. // Assert does Not throw NullReferenceException
  791. top.SetFocus ();
  792. }
  793. [Fact, AutoInitShutdown]
  794. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  795. {
  796. var view1Leave = false;
  797. var subView1Leave = false;
  798. var subView1subView1Leave = false;
  799. var top = Application.Top;
  800. var view1 = new View { CanFocus = true };
  801. var subView1 = new View { CanFocus = true };
  802. var subView1subView1 = new View { CanFocus = true };
  803. view1.Leave += (s, e) => {
  804. view1Leave = true;
  805. };
  806. subView1.Leave += (s, e) => {
  807. subView1.Remove (subView1subView1);
  808. subView1Leave = true;
  809. };
  810. view1.Add (subView1);
  811. subView1subView1.Leave += (s, e) => {
  812. // This is never invoked
  813. subView1subView1Leave = true;
  814. };
  815. subView1.Add (subView1subView1);
  816. var view2 = new View { CanFocus = true };
  817. top.Add (view1, view2);
  818. Application.Begin (top);
  819. view2.SetFocus ();
  820. Assert.True (view1Leave);
  821. Assert.True (subView1Leave);
  822. Assert.False (subView1subView1Leave);
  823. }
  824. [Fact, AutoInitShutdown]
  825. public void Remove_Does_Not_Change_Focus ()
  826. {
  827. Assert.True (Application.Top.CanFocus);
  828. Assert.False (Application.Top.HasFocus);
  829. var container = new View () { Width = 10, Height = 10 };
  830. var leave = false;
  831. container.Leave += (s, e) => leave = true;
  832. Assert.False (container.CanFocus);
  833. var child = new View () { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  834. container.Add (child);
  835. Assert.True (container.CanFocus);
  836. Assert.False (container.HasFocus);
  837. Assert.True (child.CanFocus);
  838. Assert.False (child.HasFocus);
  839. Application.Top.Add (container);
  840. Application.Begin (Application.Top);
  841. Assert.True (Application.Top.CanFocus);
  842. Assert.True (Application.Top.HasFocus);
  843. Assert.True (container.CanFocus);
  844. Assert.True (container.HasFocus);
  845. Assert.True (child.CanFocus);
  846. Assert.True (child.HasFocus);
  847. container.Remove (child);
  848. child.Dispose ();
  849. child = null;
  850. Assert.True (Application.Top.HasFocus);
  851. Assert.True (container.CanFocus);
  852. Assert.True (container.HasFocus);
  853. Assert.Null (child);
  854. Assert.False (leave);
  855. }
  856. [Fact, AutoInitShutdown]
  857. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  858. {
  859. Assert.True (Application.Top.CanFocus);
  860. Assert.False (Application.Top.HasFocus);
  861. var exception = Record.Exception (Application.Top.SetFocus);
  862. Assert.Null (exception);
  863. Assert.True (Application.Top.CanFocus);
  864. Assert.True (Application.Top.HasFocus);
  865. }
  866. [Fact, AutoInitShutdown]
  867. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  868. {
  869. var top1 = Application.Top;
  870. var view1 = new View () { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  871. var top2 = new Toplevel () { Id = "top2", Y = 1, Width = 10, Height = 5 };
  872. var view2 = new View () { Id = "view2", Y = 1, Width = 10, Height = 5, CanFocus = true };
  873. View view3 = null;
  874. var removed = false;
  875. view2.Enter += (s, e) => {
  876. if (!removed) {
  877. removed = true;
  878. view3 = new View () { Id = "view3", Y = 1, Width = 10, Height = 5 };
  879. Application.Current.Add (view3);
  880. Application.Current.BringSubviewToFront (view3);
  881. Assert.False (view3.HasFocus);
  882. }
  883. };
  884. view2.Leave += (s, e) => {
  885. Application.Current.Remove (view3);
  886. view3.Dispose ();
  887. view3 = null;
  888. };
  889. top2.Add (view2);
  890. top1.Add (view1, top2);
  891. Application.Begin (top1);
  892. Assert.True (top1.HasFocus);
  893. Assert.True (view1.HasFocus);
  894. Assert.False (view2.HasFocus);
  895. Assert.False (removed);
  896. Assert.Null (view3);
  897. Assert.True (top1.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers { Ctrl = true })));
  898. Assert.True (top1.HasFocus);
  899. Assert.False (view1.HasFocus);
  900. Assert.True (view2.HasFocus);
  901. Assert.True (removed);
  902. Assert.NotNull (view3);
  903. var exception = Record.Exception (() => top1.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers { Ctrl = true })));
  904. Assert.Null (exception);
  905. Assert.True (removed);
  906. Assert.Null (view3);
  907. }
  908. }
  909. }