ViewTests.cs 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alais Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui {
  11. public class ViewTests {
  12. [Fact]
  13. public void New_Initializes ()
  14. {
  15. // Parameterless
  16. var r = new View ();
  17. Assert.NotNull (r);
  18. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  19. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  20. Assert.False (r.CanFocus);
  21. Assert.False (r.HasFocus);
  22. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  23. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  24. Assert.Null (r.Focused);
  25. Assert.Null (r.ColorScheme);
  26. Assert.Equal (Dim.Sized (0), r.Width);
  27. Assert.Equal (Dim.Sized (0), r.Height);
  28. // BUGBUG: Pos needs eqality implemented
  29. //Assert.Equal (Pos.At (0), r.X);
  30. //Assert.Equal (Pos.At (0), r.Y);
  31. Assert.False (r.IsCurrentTop);
  32. Assert.Empty (r.Id);
  33. Assert.Empty (r.Subviews);
  34. Assert.False (r.WantContinuousButtonPressed);
  35. Assert.False (r.WantMousePositionReports);
  36. Assert.Null (r.GetEnumerator ().Current);
  37. Assert.Null (r.SuperView);
  38. Assert.Null (r.MostFocused);
  39. // Empty Rect
  40. r = new View (Rect.Empty);
  41. Assert.NotNull (r);
  42. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  43. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  44. Assert.False (r.CanFocus);
  45. Assert.False (r.HasFocus);
  46. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  47. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  48. Assert.Null (r.Focused);
  49. Assert.Null (r.ColorScheme);
  50. Assert.Null (r.Height);
  51. Assert.Null (r.Height);
  52. Assert.Null (r.X);
  53. Assert.Null (r.Y);
  54. Assert.False (r.IsCurrentTop);
  55. Assert.Empty (r.Id);
  56. Assert.Empty (r.Subviews);
  57. Assert.False (r.WantContinuousButtonPressed);
  58. Assert.False (r.WantMousePositionReports);
  59. Assert.Null (r.GetEnumerator ().Current);
  60. Assert.Null (r.SuperView);
  61. Assert.Null (r.MostFocused);
  62. // Rect with values
  63. r = new View (new Rect (1, 2, 3, 4));
  64. Assert.NotNull (r);
  65. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  66. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  67. Assert.False (r.CanFocus);
  68. Assert.False (r.HasFocus);
  69. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  70. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  71. Assert.Null (r.Focused);
  72. Assert.Null (r.ColorScheme);
  73. Assert.Null (r.Height);
  74. Assert.Null (r.Height);
  75. Assert.Null (r.X);
  76. Assert.Null (r.Y);
  77. Assert.False (r.IsCurrentTop);
  78. Assert.Empty (r.Id);
  79. Assert.Empty (r.Subviews);
  80. Assert.False (r.WantContinuousButtonPressed);
  81. Assert.False (r.WantMousePositionReports);
  82. Assert.Null (r.GetEnumerator ().Current);
  83. Assert.Null (r.SuperView);
  84. Assert.Null (r.MostFocused);
  85. }
  86. [Fact]
  87. public void New_Methods_Return_False ()
  88. {
  89. var r = new View ();
  90. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  91. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  92. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  93. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  94. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  95. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  96. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  97. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  98. Assert.False (r.OnEnter (new View ()));
  99. Assert.False (r.OnLeave (new View ()));
  100. // TODO: Add more
  101. }
  102. [Fact]
  103. public void TopologicalSort_Missing_Add ()
  104. {
  105. var root = new View ();
  106. var sub1 = new View ();
  107. root.Add (sub1);
  108. var sub2 = new View ();
  109. sub1.Width = Dim.Width (sub2);
  110. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  111. sub2.Width = Dim.Width (sub1);
  112. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  113. }
  114. [Fact]
  115. public void TopologicalSort_Recursive_Ref ()
  116. {
  117. var root = new View ();
  118. var sub1 = new View ();
  119. root.Add (sub1);
  120. var sub2 = new View ();
  121. root.Add (sub2);
  122. sub2.Width = Dim.Width (sub2);
  123. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  124. }
  125. [Fact]
  126. public void Added_Removed ()
  127. {
  128. var v = new View (new Rect (0, 0, 10, 24));
  129. var t = new View ();
  130. v.Added += (View e) => {
  131. Assert.True (v.SuperView == e);
  132. };
  133. v.Removed += (View e) => {
  134. Assert.True (v.SuperView == null);
  135. };
  136. t.Add (v);
  137. Assert.True (t.Subviews.Count == 1);
  138. t.Remove (v);
  139. Assert.True (t.Subviews.Count == 0);
  140. }
  141. [Fact]
  142. public void Subviews_TabIndexes_AreEqual ()
  143. {
  144. var r = new View ();
  145. var v1 = new View () { CanFocus = true };
  146. var v2 = new View () { CanFocus = true };
  147. var v3 = new View () { CanFocus = true };
  148. r.Add (v1, v2, v3);
  149. Assert.True (r.Subviews.IndexOf (v1) == 0);
  150. Assert.True (r.Subviews.IndexOf (v2) == 1);
  151. Assert.True (r.Subviews.IndexOf (v3) == 2);
  152. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  153. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  154. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  155. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  156. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  157. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  158. }
  159. [Fact]
  160. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  161. {
  162. var r = new View ();
  163. var v1 = new View () { CanFocus = true };
  164. var v2 = new View () { CanFocus = true };
  165. var v3 = new View () { CanFocus = true };
  166. r.Add (v1, v2, v3);
  167. r.BringSubviewToFront (v1);
  168. Assert.True (r.Subviews.IndexOf (v1) == 2);
  169. Assert.True (r.Subviews.IndexOf (v2) == 0);
  170. Assert.True (r.Subviews.IndexOf (v3) == 1);
  171. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  172. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  173. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  174. }
  175. [Fact]
  176. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  177. {
  178. var r = new View ();
  179. var v1 = new View () { CanFocus = true };
  180. var v2 = new View () { CanFocus = true };
  181. var v3 = new View () { CanFocus = true };
  182. r.Add (v1, v2, v3);
  183. r.BringSubviewForward (v1);
  184. Assert.True (r.Subviews.IndexOf (v1) == 1);
  185. Assert.True (r.Subviews.IndexOf (v2) == 0);
  186. Assert.True (r.Subviews.IndexOf (v3) == 2);
  187. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  188. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  189. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  190. }
  191. [Fact]
  192. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  193. {
  194. var r = new View ();
  195. var v1 = new View () { CanFocus = true };
  196. var v2 = new View () { CanFocus = true };
  197. var v3 = new View () { CanFocus = true };
  198. r.Add (v1, v2, v3);
  199. r.SendSubviewToBack (v3);
  200. Assert.True (r.Subviews.IndexOf (v1) == 1);
  201. Assert.True (r.Subviews.IndexOf (v2) == 2);
  202. Assert.True (r.Subviews.IndexOf (v3) == 0);
  203. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  204. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  205. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  206. }
  207. [Fact]
  208. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  209. {
  210. var r = new View ();
  211. var v1 = new View () { CanFocus = true };
  212. var v2 = new View () { CanFocus = true };
  213. var v3 = new View () { CanFocus = true };
  214. r.Add (v1, v2, v3);
  215. r.SendSubviewBackwards (v3);
  216. Assert.True (r.Subviews.IndexOf (v1) == 0);
  217. Assert.True (r.Subviews.IndexOf (v2) == 2);
  218. Assert.True (r.Subviews.IndexOf (v3) == 1);
  219. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  220. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  221. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  222. }
  223. [Fact]
  224. public void TabIndex_Set_CanFocus_ValidValues ()
  225. {
  226. var r = new View ();
  227. var v1 = new View () { CanFocus = true };
  228. var v2 = new View () { CanFocus = true };
  229. var v3 = new View () { CanFocus = true };
  230. r.Add (v1, v2, v3);
  231. v1.TabIndex = 1;
  232. Assert.True (r.Subviews.IndexOf (v1) == 0);
  233. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  234. v1.TabIndex = 2;
  235. Assert.True (r.Subviews.IndexOf (v1) == 0);
  236. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  237. }
  238. [Fact]
  239. public void TabIndex_Set_CanFocus_HigherValues ()
  240. {
  241. var r = new View ();
  242. var v1 = new View () { CanFocus = true };
  243. var v2 = new View () { CanFocus = true };
  244. var v3 = new View () { CanFocus = true };
  245. r.Add (v1, v2, v3);
  246. v1.TabIndex = 3;
  247. Assert.True (r.Subviews.IndexOf (v1) == 0);
  248. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  249. }
  250. [Fact]
  251. public void TabIndex_Set_CanFocus_LowerValues ()
  252. {
  253. var r = new View ();
  254. var v1 = new View () { CanFocus = true };
  255. var v2 = new View () { CanFocus = true };
  256. var v3 = new View () { CanFocus = true };
  257. r.Add (v1, v2, v3);
  258. v1.TabIndex = -1;
  259. Assert.True (r.Subviews.IndexOf (v1) == 0);
  260. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  261. }
  262. [Fact]
  263. public void TabIndex_Set_CanFocus_False ()
  264. {
  265. var r = new View ();
  266. var v1 = new View () { CanFocus = true };
  267. var v2 = new View () { CanFocus = true };
  268. var v3 = new View () { CanFocus = true };
  269. r.Add (v1, v2, v3);
  270. v1.CanFocus = false;
  271. v1.TabIndex = 0;
  272. Assert.True (r.Subviews.IndexOf (v1) == 0);
  273. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  274. Assert.Equal (-1, v1.TabIndex);
  275. }
  276. [Fact]
  277. public void TabIndex_Set_CanFocus_False_To_True ()
  278. {
  279. var r = new View ();
  280. var v1 = new View ();
  281. var v2 = new View () { CanFocus = true };
  282. var v3 = new View () { CanFocus = true };
  283. r.Add (v1, v2, v3);
  284. v1.CanFocus = true;
  285. v1.TabIndex = 1;
  286. Assert.True (r.Subviews.IndexOf (v1) == 0);
  287. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  288. }
  289. [Fact]
  290. public void TabStop_And_CanFocus_Are_All_True ()
  291. {
  292. var r = new View ();
  293. var v1 = new View () { CanFocus = true };
  294. var v2 = new View () { CanFocus = true };
  295. var v3 = new View () { CanFocus = true };
  296. r.Add (v1, v2, v3);
  297. r.FocusNext ();
  298. Assert.True (v1.HasFocus);
  299. Assert.False (v2.HasFocus);
  300. Assert.False (v3.HasFocus);
  301. r.FocusNext ();
  302. Assert.False (v1.HasFocus);
  303. Assert.True (v2.HasFocus);
  304. Assert.False (v3.HasFocus);
  305. r.FocusNext ();
  306. Assert.False (v1.HasFocus);
  307. Assert.False (v2.HasFocus);
  308. Assert.True (v3.HasFocus);
  309. }
  310. [Fact]
  311. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  312. {
  313. var r = new View ();
  314. var v1 = new View ();
  315. var v2 = new View ();
  316. var v3 = new View ();
  317. r.Add (v1, v2, v3);
  318. r.FocusNext ();
  319. Assert.False (v1.HasFocus);
  320. Assert.False (v2.HasFocus);
  321. Assert.False (v3.HasFocus);
  322. r.FocusNext ();
  323. Assert.False (v1.HasFocus);
  324. Assert.False (v2.HasFocus);
  325. Assert.False (v3.HasFocus);
  326. r.FocusNext ();
  327. Assert.False (v1.HasFocus);
  328. Assert.False (v2.HasFocus);
  329. Assert.False (v3.HasFocus);
  330. }
  331. [Fact]
  332. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  333. {
  334. var r = new View ();
  335. var v1 = new View () { CanFocus = true, TabStop = false };
  336. var v2 = new View () { CanFocus = true, TabStop = false };
  337. var v3 = new View () { CanFocus = true, TabStop = false };
  338. r.Add (v1, v2, v3);
  339. r.FocusNext ();
  340. Assert.False (v1.HasFocus);
  341. Assert.False (v2.HasFocus);
  342. Assert.False (v3.HasFocus);
  343. r.FocusNext ();
  344. Assert.False (v1.HasFocus);
  345. Assert.False (v2.HasFocus);
  346. Assert.False (v3.HasFocus);
  347. r.FocusNext ();
  348. Assert.False (v1.HasFocus);
  349. Assert.False (v2.HasFocus);
  350. Assert.False (v3.HasFocus);
  351. }
  352. [Fact]
  353. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  354. {
  355. var r = new View ();
  356. var v1 = new View () { CanFocus = true, TabStop = false };
  357. var v2 = new View () { CanFocus = false, TabStop = true };
  358. var v3 = new View () { CanFocus = false, TabStop = false };
  359. r.Add (v1, v2, v3);
  360. r.FocusNext ();
  361. Assert.False (v1.HasFocus);
  362. Assert.False (v2.HasFocus);
  363. Assert.False (v3.HasFocus);
  364. r.FocusNext ();
  365. Assert.False (v1.HasFocus);
  366. Assert.False (v2.HasFocus);
  367. Assert.False (v3.HasFocus);
  368. r.FocusNext ();
  369. Assert.False (v1.HasFocus);
  370. Assert.False (v2.HasFocus);
  371. Assert.False (v3.HasFocus);
  372. }
  373. [Fact]
  374. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  375. {
  376. var r = new View ();
  377. var v1 = new View ();
  378. var v2 = new View ();
  379. var v3 = new View ();
  380. r.Add (v1, v2, v3);
  381. r.FocusNext ();
  382. Assert.False (v1.HasFocus);
  383. Assert.False (v2.HasFocus);
  384. Assert.False (v3.HasFocus);
  385. v1.CanFocus = true;
  386. r.FocusNext ();
  387. Assert.True (v1.HasFocus);
  388. Assert.False (v2.HasFocus);
  389. Assert.False (v3.HasFocus);
  390. v2.CanFocus = true;
  391. r.FocusNext ();
  392. Assert.False (v1.HasFocus);
  393. Assert.True (v2.HasFocus);
  394. Assert.False (v3.HasFocus);
  395. v3.CanFocus = true;
  396. r.FocusNext ();
  397. Assert.False (v1.HasFocus);
  398. Assert.False (v2.HasFocus);
  399. Assert.True (v3.HasFocus);
  400. }
  401. [Fact]
  402. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  403. {
  404. var r = new View ();
  405. var v1 = new View () { CanFocus = true, TabStop = false };
  406. var v2 = new View () { CanFocus = true, TabStop = false };
  407. var v3 = new View () { CanFocus = true, TabStop = false };
  408. r.Add (v1, v2, v3);
  409. r.FocusNext ();
  410. Assert.False (v1.HasFocus);
  411. Assert.False (v2.HasFocus);
  412. Assert.False (v3.HasFocus);
  413. v1.TabStop = true;
  414. r.FocusNext ();
  415. Assert.True (v1.HasFocus);
  416. Assert.False (v2.HasFocus);
  417. Assert.False (v3.HasFocus);
  418. v2.TabStop = true;
  419. r.FocusNext ();
  420. Assert.False (v1.HasFocus);
  421. Assert.True (v2.HasFocus);
  422. Assert.False (v3.HasFocus);
  423. v3.TabStop = true;
  424. r.FocusNext ();
  425. Assert.False (v1.HasFocus);
  426. Assert.False (v2.HasFocus);
  427. Assert.True (v3.HasFocus);
  428. }
  429. [Fact]
  430. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  431. {
  432. var r = new View ();
  433. var v1 = new View ("1");
  434. var v2 = new View ("2");
  435. var v3 = new View ("3");
  436. r.Add (v1, v2, v3);
  437. v2.CanFocus = true;
  438. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  439. Assert.Equal (0, v2.TabIndex);
  440. Assert.True (v2.TabStop);
  441. v1.CanFocus = true;
  442. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  443. Assert.Equal (1, v1.TabIndex);
  444. Assert.True (v1.TabStop);
  445. v1.TabIndex = 2;
  446. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  447. Assert.Equal (1, v1.TabIndex);
  448. v3.CanFocus = true;
  449. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  450. Assert.Equal (1, v1.TabIndex);
  451. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  452. Assert.Equal (2, v3.TabIndex);
  453. Assert.True (v3.TabStop);
  454. v2.CanFocus = false;
  455. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  456. Assert.Equal (1, v1.TabIndex);
  457. Assert.True (v1.TabStop);
  458. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  459. Assert.Equal (-1, v2.TabIndex);
  460. Assert.False (v2.TabStop);
  461. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  462. Assert.Equal (2, v3.TabIndex);
  463. Assert.True (v3.TabStop);
  464. }
  465. [Fact]
  466. public void Initialized_Event_Comparing_With_Added_Event ()
  467. {
  468. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  469. var t = new Toplevel () { Id = "0", };
  470. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  471. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  472. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  473. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  474. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  475. w.Added += (e) => {
  476. Assert.Equal (e.Frame.Width, w.Frame.Width);
  477. Assert.Equal (e.Frame.Height, w.Frame.Height);
  478. };
  479. v1.Added += (e) => {
  480. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  481. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  482. };
  483. v2.Added += (e) => {
  484. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  485. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  486. };
  487. sv1.Added += (e) => {
  488. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  489. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  490. };
  491. t.Initialized += (s, e) => {
  492. tc++;
  493. Assert.Equal (1, tc);
  494. Assert.Equal (1, wc);
  495. Assert.Equal (1, v1c);
  496. Assert.Equal (1, v2c);
  497. Assert.Equal (1, sv1c);
  498. Assert.True (t.CanFocus);
  499. Assert.True (w.CanFocus);
  500. Assert.False (v1.CanFocus);
  501. Assert.False (v2.CanFocus);
  502. Assert.False (sv1.CanFocus);
  503. Application.Refresh ();
  504. };
  505. w.Initialized += (s, e) => {
  506. wc++;
  507. Assert.Equal (t.Frame.Width, w.Frame.Width);
  508. Assert.Equal (t.Frame.Height, w.Frame.Height);
  509. };
  510. v1.Initialized += (s, e) => {
  511. v1c++;
  512. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  513. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  514. };
  515. v2.Initialized += (s, e) => {
  516. v2c++;
  517. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  518. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  519. };
  520. sv1.Initialized += (s, e) => {
  521. sv1c++;
  522. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  523. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  524. Assert.False (sv1.CanFocus);
  525. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  526. Assert.False (sv1.CanFocus);
  527. };
  528. v1.Add (sv1);
  529. w.Add (v1, v2);
  530. t.Add (w);
  531. Application.Iteration = () => {
  532. Application.Refresh ();
  533. t.Running = false;
  534. };
  535. Application.Run (t);
  536. Application.Shutdown ();
  537. Assert.Equal (1, tc);
  538. Assert.Equal (1, wc);
  539. Assert.Equal (1, v1c);
  540. Assert.Equal (1, v2c);
  541. Assert.Equal (1, sv1c);
  542. Assert.True (t.CanFocus);
  543. Assert.True (w.CanFocus);
  544. Assert.False (v1.CanFocus);
  545. Assert.False (v2.CanFocus);
  546. Assert.False (sv1.CanFocus);
  547. v1.CanFocus = true;
  548. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  549. }
  550. [Fact]
  551. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  552. {
  553. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  554. var t = new Toplevel () { Id = "0", };
  555. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  556. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  557. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  558. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  559. t.Initialized += (s, e) => {
  560. tc++;
  561. Assert.Equal (1, tc);
  562. Assert.Equal (1, wc);
  563. Assert.Equal (1, v1c);
  564. Assert.Equal (1, v2c);
  565. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  566. Assert.True (t.CanFocus);
  567. Assert.True (w.CanFocus);
  568. Assert.False (v1.CanFocus);
  569. Assert.False (v2.CanFocus);
  570. Application.Refresh ();
  571. };
  572. w.Initialized += (s, e) => {
  573. wc++;
  574. Assert.Equal (t.Frame.Width, w.Frame.Width);
  575. Assert.Equal (t.Frame.Height, w.Frame.Height);
  576. };
  577. v1.Initialized += (s, e) => {
  578. v1c++;
  579. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  580. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  581. };
  582. v2.Initialized += (s, e) => {
  583. v2c++;
  584. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  585. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  586. };
  587. w.Add (v1, v2);
  588. t.Add (w);
  589. Application.Iteration = () => {
  590. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  591. sv1.Initialized += (s, e) => {
  592. sv1c++;
  593. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  594. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  595. Assert.False (sv1.CanFocus);
  596. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  597. Assert.False (sv1.CanFocus);
  598. };
  599. v1.Add (sv1);
  600. Application.Refresh ();
  601. t.Running = false;
  602. };
  603. Application.Run (t);
  604. Application.Shutdown ();
  605. Assert.Equal (1, tc);
  606. Assert.Equal (1, wc);
  607. Assert.Equal (1, v1c);
  608. Assert.Equal (1, v2c);
  609. Assert.Equal (1, sv1c);
  610. Assert.True (t.CanFocus);
  611. Assert.True (w.CanFocus);
  612. Assert.False (v1.CanFocus);
  613. Assert.False (v2.CanFocus);
  614. }
  615. [Fact]
  616. public void CanFocus_Faced_With_Container ()
  617. {
  618. var t = new Toplevel ();
  619. var w = new Window ();
  620. var f = new FrameView ();
  621. var v = new View () { CanFocus = true };
  622. f.Add (v);
  623. w.Add (f);
  624. t.Add (w);
  625. Assert.True (t.CanFocus);
  626. Assert.True (w.CanFocus);
  627. Assert.True (f.CanFocus);
  628. Assert.True (v.CanFocus);
  629. f.CanFocus = false;
  630. Assert.False (f.CanFocus);
  631. Assert.True (v.CanFocus);
  632. v.CanFocus = false;
  633. Assert.False (f.CanFocus);
  634. Assert.False (v.CanFocus);
  635. v.CanFocus = true;
  636. Assert.False (f.CanFocus);
  637. Assert.True (v.CanFocus);
  638. }
  639. [Fact]
  640. public void CanFocus_Faced_With_Container_Before_Run ()
  641. {
  642. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  643. var t = Application.Top;
  644. var w = new Window ("w");
  645. var f = new FrameView ("f");
  646. var v = new View ("v") { CanFocus = true };
  647. f.Add (v);
  648. w.Add (f);
  649. t.Add (w);
  650. Assert.True (t.CanFocus);
  651. Assert.True (w.CanFocus);
  652. Assert.True (f.CanFocus);
  653. Assert.True (v.CanFocus);
  654. f.CanFocus = false;
  655. Assert.False (f.CanFocus);
  656. Assert.True (v.CanFocus);
  657. v.CanFocus = false;
  658. Assert.False (f.CanFocus);
  659. Assert.False (v.CanFocus);
  660. v.CanFocus = true;
  661. Assert.False (f.CanFocus);
  662. Assert.True (v.CanFocus);
  663. Application.Iteration += () => Application.RequestStop ();
  664. Application.Run ();
  665. Application.Shutdown ();
  666. }
  667. [Fact]
  668. public void CanFocus_Faced_With_Container_After_Run ()
  669. {
  670. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  671. var t = Application.Top;
  672. var w = new Window ("w");
  673. var f = new FrameView ("f");
  674. var v = new View ("v") { CanFocus = true };
  675. f.Add (v);
  676. w.Add (f);
  677. t.Add (w);
  678. t.Ready += () => {
  679. Assert.True (t.CanFocus);
  680. Assert.True (w.CanFocus);
  681. Assert.True (f.CanFocus);
  682. Assert.True (v.CanFocus);
  683. f.CanFocus = false;
  684. Assert.False (f.CanFocus);
  685. Assert.False (v.CanFocus);
  686. v.CanFocus = false;
  687. Assert.False (f.CanFocus);
  688. Assert.False (v.CanFocus);
  689. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  690. Assert.False (f.CanFocus);
  691. Assert.False (v.CanFocus);
  692. f.CanFocus = true;
  693. Assert.True (f.CanFocus);
  694. Assert.True (v.CanFocus);
  695. };
  696. Application.Iteration += () => Application.RequestStop ();
  697. Application.Run ();
  698. Application.Shutdown ();
  699. }
  700. [Fact]
  701. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  702. {
  703. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  704. var t = Application.Top;
  705. var w = new Window ("w");
  706. var f = new FrameView ("f");
  707. var v1 = new View ("v1") { CanFocus = true };
  708. var v2 = new View ("v2") { CanFocus = true };
  709. f.Add (v1, v2);
  710. w.Add (f);
  711. t.Add (w);
  712. t.Ready += () => {
  713. Assert.True (t.CanFocus);
  714. Assert.True (w.CanFocus);
  715. Assert.True (f.CanFocus);
  716. Assert.True (v1.CanFocus);
  717. Assert.True (v2.CanFocus);
  718. w.CanFocus = false;
  719. Assert.True (w.CanFocus);
  720. Assert.False (f.CanFocus);
  721. Assert.False (v1.CanFocus);
  722. Assert.False (v2.CanFocus);
  723. };
  724. Application.Iteration += () => Application.RequestStop ();
  725. Application.Run ();
  726. Application.Shutdown ();
  727. }
  728. [Fact]
  729. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  730. {
  731. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  732. var t = Application.Top;
  733. var w = new Window ("w");
  734. var f = new FrameView ("f");
  735. var v1 = new View ("v1");
  736. var v2 = new View ("v2") { CanFocus = true };
  737. f.Add (v1, v2);
  738. w.Add (f);
  739. t.Add (w);
  740. t.Ready += () => {
  741. Assert.True (t.CanFocus);
  742. Assert.True (w.CanFocus);
  743. Assert.True (f.CanFocus);
  744. Assert.False (v1.CanFocus);
  745. Assert.True (v2.CanFocus);
  746. w.CanFocus = false;
  747. Assert.True (w.CanFocus);
  748. Assert.False (f.CanFocus);
  749. Assert.False (v1.CanFocus);
  750. Assert.False (v2.CanFocus);
  751. w.CanFocus = true;
  752. Assert.True (w.CanFocus);
  753. Assert.True (f.CanFocus);
  754. Assert.False (v1.CanFocus);
  755. Assert.True (v2.CanFocus);
  756. };
  757. Application.Iteration += () => Application.RequestStop ();
  758. Application.Run ();
  759. Application.Shutdown ();
  760. }
  761. [Fact]
  762. public void Navigation_With_Null_Focused_View ()
  763. {
  764. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  765. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  766. Application.Top.Ready += () => {
  767. Assert.Null (Application.Top.Focused);
  768. };
  769. // Keyboard navigation with tab
  770. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  771. Application.Iteration += () => Application.RequestStop ();
  772. Application.Run ();
  773. Application.Shutdown ();
  774. }
  775. [Fact]
  776. public void Multi_Thread_Toplevels ()
  777. {
  778. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  779. var t = Application.Top;
  780. var w = new Window ();
  781. t.Add (w);
  782. int count = 0, count1 = 0, count2 = 0;
  783. bool log = false, log1 = false, log2 = false;
  784. bool fromTopStillKnowFirstIsRunning = false;
  785. bool fromTopStillKnowSecondIsRunning = false;
  786. bool fromFirstStillKnowSecondIsRunning = false;
  787. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  788. count++;
  789. if (count1 == 5) {
  790. log1 = true;
  791. }
  792. if (count1 > 13 && count < 15) {
  793. fromTopStillKnowFirstIsRunning = true;
  794. }
  795. if (count2 > 6 && count2 < 8) {
  796. fromTopStillKnowSecondIsRunning = true;
  797. }
  798. if (count == 30) {
  799. Assert.Equal (30, count);
  800. Assert.Equal (20, count1);
  801. Assert.Equal (10, count2);
  802. Assert.True (log);
  803. Assert.True (log1);
  804. Assert.True (log2);
  805. Assert.True (fromTopStillKnowFirstIsRunning);
  806. Assert.True (fromTopStillKnowSecondIsRunning);
  807. Assert.True (fromFirstStillKnowSecondIsRunning);
  808. Application.RequestStop ();
  809. return false;
  810. }
  811. return true;
  812. });
  813. t.Ready += FirstDialogToplevel;
  814. void FirstDialogToplevel ()
  815. {
  816. var od = new OpenDialog();
  817. od.Ready += SecoundDialogToplevel;
  818. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  819. count1++;
  820. if (count2 == 5) {
  821. log2 = true;
  822. }
  823. if (count2 > 3 && count2 < 5) {
  824. fromFirstStillKnowSecondIsRunning = true;
  825. }
  826. if (count1 == 20) {
  827. Assert.Equal (20, count1);
  828. Application.RequestStop ();
  829. return false;
  830. }
  831. return true;
  832. });
  833. Application.Run (od);
  834. }
  835. void SecoundDialogToplevel ()
  836. {
  837. var d = new Dialog ();
  838. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  839. count2++;
  840. if (count < 30) {
  841. log = true;
  842. }
  843. if (count2 == 10) {
  844. Assert.Equal (10, count2);
  845. Application.RequestStop ();
  846. return false;
  847. }
  848. return true;
  849. });
  850. Application.Run (d);
  851. }
  852. Application.Run ();
  853. Application.Shutdown ();
  854. }
  855. }
  856. }