ViewTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 (0, wc);
  495. Assert.Equal (0, v1c);
  496. Assert.Equal (0, v2c);
  497. Assert.Equal (0, 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. sv1.CanFocus = true;
  526. Assert.True (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.True (sv1.CanFocus);
  547. }
  548. [Fact]
  549. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  550. {
  551. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  552. var t = new Toplevel () { Id = "0", };
  553. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  554. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  555. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  556. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  557. t.Initialized += (s, e) => {
  558. tc++;
  559. Assert.Equal (1, tc);
  560. Assert.Equal (0, wc);
  561. Assert.Equal (0, v1c);
  562. Assert.Equal (0, v2c);
  563. Assert.Equal (0, sv1c);
  564. Assert.True (t.CanFocus);
  565. Assert.True (w.CanFocus);
  566. Assert.False (v1.CanFocus);
  567. Assert.False (v2.CanFocus);
  568. Application.Refresh ();
  569. };
  570. w.Initialized += (s, e) => {
  571. wc++;
  572. Assert.Equal (t.Frame.Width, w.Frame.Width);
  573. Assert.Equal (t.Frame.Height, w.Frame.Height);
  574. };
  575. v1.Initialized += (s, e) => {
  576. v1c++;
  577. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  578. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  579. };
  580. v2.Initialized += (s, e) => {
  581. v2c++;
  582. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  583. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  584. };
  585. w.Add (v1, v2);
  586. t.Add (w);
  587. Application.Iteration = () => {
  588. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  589. sv1.Initialized += (s, e) => {
  590. sv1c++;
  591. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  592. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  593. Assert.False (sv1.CanFocus);
  594. sv1.CanFocus = true;
  595. Assert.True (sv1.CanFocus);
  596. };
  597. v1.Add (sv1);
  598. Application.Refresh ();
  599. t.Running = false;
  600. };
  601. Application.Run (t);
  602. Application.Shutdown ();
  603. Assert.Equal (1, tc);
  604. Assert.Equal (1, wc);
  605. Assert.Equal (1, v1c);
  606. Assert.Equal (1, v2c);
  607. Assert.Equal (1, sv1c);
  608. Assert.True (t.CanFocus);
  609. Assert.True (w.CanFocus);
  610. Assert.False (v1.CanFocus);
  611. Assert.False (v2.CanFocus);
  612. }
  613. [Fact]
  614. public void CanFocus_Faced_With_Container ()
  615. {
  616. var t = new Toplevel ();
  617. var w = new Window ();
  618. var f = new FrameView ();
  619. var v = new View () { CanFocus = true };
  620. f.Add (v);
  621. w.Add (f);
  622. t.Add (w);
  623. Assert.True (t.CanFocus);
  624. Assert.True (w.CanFocus);
  625. Assert.True (f.CanFocus);
  626. Assert.True (v.CanFocus);
  627. f.CanFocus = false;
  628. Assert.False (f.CanFocus);
  629. Assert.True (v.CanFocus);
  630. v.CanFocus = false;
  631. Assert.False (f.CanFocus);
  632. Assert.False (v.CanFocus);
  633. v.CanFocus = true;
  634. Assert.False (f.CanFocus);
  635. Assert.True (v.CanFocus);
  636. }
  637. [Fact]
  638. public void CanFocus_Faced_With_Container_Before_Run ()
  639. {
  640. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  641. var t = Application.Top;
  642. var w = new Window ("w");
  643. var f = new FrameView ("f");
  644. var v = new View ("v") { CanFocus = true };
  645. f.Add (v);
  646. w.Add (f);
  647. t.Add (w);
  648. Assert.True (t.CanFocus);
  649. Assert.True (w.CanFocus);
  650. Assert.True (f.CanFocus);
  651. Assert.True (v.CanFocus);
  652. f.CanFocus = false;
  653. Assert.False (f.CanFocus);
  654. Assert.True (v.CanFocus);
  655. v.CanFocus = false;
  656. Assert.False (f.CanFocus);
  657. Assert.False (v.CanFocus);
  658. v.CanFocus = true;
  659. Assert.False (f.CanFocus);
  660. Assert.True (v.CanFocus);
  661. Application.Iteration += () => Application.RequestStop ();
  662. Application.Run ();
  663. Application.Shutdown ();
  664. }
  665. [Fact]
  666. public void CanFocus_Faced_With_Container_After_Run ()
  667. {
  668. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  669. var t = Application.Top;
  670. var w = new Window ("w");
  671. var f = new FrameView ("f");
  672. var v = new View ("v") { CanFocus = true };
  673. f.Add (v);
  674. w.Add (f);
  675. t.Add (w);
  676. t.Ready += () => {
  677. Assert.True (t.CanFocus);
  678. Assert.True (w.CanFocus);
  679. Assert.True (f.CanFocus);
  680. Assert.True (v.CanFocus);
  681. f.CanFocus = false;
  682. Assert.False (f.CanFocus);
  683. Assert.False (v.CanFocus);
  684. v.CanFocus = false;
  685. Assert.False (f.CanFocus);
  686. Assert.False (v.CanFocus);
  687. v.CanFocus = true;
  688. Assert.True (f.CanFocus);
  689. Assert.True (v.CanFocus);
  690. };
  691. Application.Iteration += () => Application.RequestStop ();
  692. Application.Run ();
  693. Application.Shutdown ();
  694. }
  695. [Fact]
  696. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  697. {
  698. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  699. var t = Application.Top;
  700. var w = new Window ("w");
  701. var f = new FrameView ("f");
  702. var v1 = new View ("v1") { CanFocus = true };
  703. var v2 = new View ("v2") { CanFocus = true };
  704. f.Add (v1, v2);
  705. w.Add (f);
  706. t.Add (w);
  707. t.Ready += () => {
  708. Assert.True (t.CanFocus);
  709. Assert.True (w.CanFocus);
  710. Assert.True (f.CanFocus);
  711. Assert.True (v1.CanFocus);
  712. Assert.True (v2.CanFocus);
  713. w.CanFocus = false;
  714. Assert.True (w.CanFocus);
  715. Assert.False (f.CanFocus);
  716. Assert.False (v1.CanFocus);
  717. Assert.False (v2.CanFocus);
  718. };
  719. Application.Iteration += () => Application.RequestStop ();
  720. Application.Run ();
  721. Application.Shutdown ();
  722. }
  723. [Fact]
  724. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  725. {
  726. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  727. var t = Application.Top;
  728. var w = new Window ("w");
  729. var f = new FrameView ("f");
  730. var v1 = new View ("v1");
  731. var v2 = new View ("v2") { CanFocus = true };
  732. f.Add (v1, v2);
  733. w.Add (f);
  734. t.Add (w);
  735. t.Ready += () => {
  736. Assert.True (t.CanFocus);
  737. Assert.True (w.CanFocus);
  738. Assert.True (f.CanFocus);
  739. Assert.False (v1.CanFocus);
  740. Assert.True (v2.CanFocus);
  741. w.CanFocus = false;
  742. Assert.True (w.CanFocus);
  743. Assert.False (f.CanFocus);
  744. Assert.False (v1.CanFocus);
  745. Assert.False (v2.CanFocus);
  746. w.CanFocus = true;
  747. Assert.True (w.CanFocus);
  748. Assert.True (f.CanFocus);
  749. Assert.False (v1.CanFocus);
  750. Assert.True (v2.CanFocus);
  751. };
  752. Application.Iteration += () => Application.RequestStop ();
  753. Application.Run ();
  754. Application.Shutdown ();
  755. }
  756. }
  757. }