ViewTests.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.Views {
  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. // FIXED: Pos needs equality 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.SuperView);
  37. Assert.Null (r.MostFocused);
  38. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  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.NotNull (r.Width); // All view Dim are initialized now,
  51. Assert.NotNull (r.Height); // avoiding Dim errors.
  52. Assert.NotNull (r.X); // All view Pos are initialized now,
  53. Assert.NotNull (r.Y); // avoiding Pos errors.
  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.SuperView);
  60. Assert.Null (r.MostFocused);
  61. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  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.NotNull (r.Width);
  74. Assert.NotNull (r.Height);
  75. Assert.NotNull (r.X);
  76. Assert.NotNull (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.SuperView);
  83. Assert.Null (r.MostFocused);
  84. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  85. // Initializes a view with a vertical direction
  86. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  87. Assert.NotNull (r);
  88. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  89. Assert.Equal ("View()({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  90. Assert.False (r.CanFocus);
  91. Assert.False (r.HasFocus);
  92. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  93. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  94. Assert.Null (r.Focused);
  95. Assert.Null (r.ColorScheme);
  96. Assert.NotNull (r.Width); // All view Dim are initialized now,
  97. Assert.NotNull (r.Height); // avoiding Dim errors.
  98. Assert.NotNull (r.X); // All view Pos are initialized now,
  99. Assert.NotNull (r.Y); // avoiding Pos errors.
  100. Assert.False (r.IsCurrentTop);
  101. Assert.Empty (r.Id);
  102. Assert.Empty (r.Subviews);
  103. Assert.False (r.WantContinuousButtonPressed);
  104. Assert.False (r.WantMousePositionReports);
  105. Assert.Null (r.SuperView);
  106. Assert.Null (r.MostFocused);
  107. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  108. }
  109. [Fact]
  110. public void New_Methods_Return_False ()
  111. {
  112. var r = new View ();
  113. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  114. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  115. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  116. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  119. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  120. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  121. Assert.False (r.OnEnter (new View ()));
  122. Assert.False (r.OnLeave (new View ()));
  123. // TODO: Add more
  124. }
  125. [Fact]
  126. public void TopologicalSort_Missing_Add ()
  127. {
  128. var root = new View ();
  129. var sub1 = new View ();
  130. root.Add (sub1);
  131. var sub2 = new View ();
  132. sub1.Width = Dim.Width (sub2);
  133. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  134. sub2.Width = Dim.Width (sub1);
  135. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  136. }
  137. [Fact]
  138. public void TopologicalSort_Recursive_Ref ()
  139. {
  140. var root = new View ();
  141. var sub1 = new View ();
  142. root.Add (sub1);
  143. var sub2 = new View ();
  144. root.Add (sub2);
  145. sub2.Width = Dim.Width (sub2);
  146. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  147. }
  148. [Fact]
  149. public void Added_Removed ()
  150. {
  151. var v = new View (new Rect (0, 0, 10, 24));
  152. var t = new View ();
  153. v.Added += (View e) => {
  154. Assert.True (v.SuperView == e);
  155. };
  156. v.Removed += (View e) => {
  157. Assert.True (v.SuperView == null);
  158. };
  159. t.Add (v);
  160. Assert.True (t.Subviews.Count == 1);
  161. t.Remove (v);
  162. Assert.True (t.Subviews.Count == 0);
  163. }
  164. [Fact]
  165. public void Subviews_TabIndexes_AreEqual ()
  166. {
  167. var r = new View ();
  168. var v1 = new View () { CanFocus = true };
  169. var v2 = new View () { CanFocus = true };
  170. var v3 = new View () { CanFocus = true };
  171. r.Add (v1, v2, v3);
  172. Assert.True (r.Subviews.IndexOf (v1) == 0);
  173. Assert.True (r.Subviews.IndexOf (v2) == 1);
  174. Assert.True (r.Subviews.IndexOf (v3) == 2);
  175. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  176. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  177. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  178. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  179. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  180. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  181. }
  182. [Fact]
  183. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  184. {
  185. var r = new View ();
  186. var v1 = new View () { CanFocus = true };
  187. var v2 = new View () { CanFocus = true };
  188. var v3 = new View () { CanFocus = true };
  189. r.Add (v1, v2, v3);
  190. r.BringSubviewToFront (v1);
  191. Assert.True (r.Subviews.IndexOf (v1) == 2);
  192. Assert.True (r.Subviews.IndexOf (v2) == 0);
  193. Assert.True (r.Subviews.IndexOf (v3) == 1);
  194. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  195. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  196. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  197. }
  198. [Fact]
  199. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  200. {
  201. var r = new View ();
  202. var v1 = new View () { CanFocus = true };
  203. var v2 = new View () { CanFocus = true };
  204. var v3 = new View () { CanFocus = true };
  205. r.Add (v1, v2, v3);
  206. r.BringSubviewForward (v1);
  207. Assert.True (r.Subviews.IndexOf (v1) == 1);
  208. Assert.True (r.Subviews.IndexOf (v2) == 0);
  209. Assert.True (r.Subviews.IndexOf (v3) == 2);
  210. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  211. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  212. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  213. }
  214. [Fact]
  215. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  216. {
  217. var r = new View ();
  218. var v1 = new View () { CanFocus = true };
  219. var v2 = new View () { CanFocus = true };
  220. var v3 = new View () { CanFocus = true };
  221. r.Add (v1, v2, v3);
  222. r.SendSubviewToBack (v3);
  223. Assert.True (r.Subviews.IndexOf (v1) == 1);
  224. Assert.True (r.Subviews.IndexOf (v2) == 2);
  225. Assert.True (r.Subviews.IndexOf (v3) == 0);
  226. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  227. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  228. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  229. }
  230. [Fact]
  231. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  232. {
  233. var r = new View ();
  234. var v1 = new View () { CanFocus = true };
  235. var v2 = new View () { CanFocus = true };
  236. var v3 = new View () { CanFocus = true };
  237. r.Add (v1, v2, v3);
  238. r.SendSubviewBackwards (v3);
  239. Assert.True (r.Subviews.IndexOf (v1) == 0);
  240. Assert.True (r.Subviews.IndexOf (v2) == 2);
  241. Assert.True (r.Subviews.IndexOf (v3) == 1);
  242. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  243. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  244. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  245. }
  246. [Fact]
  247. public void TabIndex_Set_CanFocus_ValidValues ()
  248. {
  249. var r = new View ();
  250. var v1 = new View () { CanFocus = true };
  251. var v2 = new View () { CanFocus = true };
  252. var v3 = new View () { CanFocus = true };
  253. r.Add (v1, v2, v3);
  254. v1.TabIndex = 1;
  255. Assert.True (r.Subviews.IndexOf (v1) == 0);
  256. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  257. v1.TabIndex = 2;
  258. Assert.True (r.Subviews.IndexOf (v1) == 0);
  259. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  260. }
  261. [Fact]
  262. public void TabIndex_Set_CanFocus_HigherValues ()
  263. {
  264. var r = new View ();
  265. var v1 = new View () { CanFocus = true };
  266. var v2 = new View () { CanFocus = true };
  267. var v3 = new View () { CanFocus = true };
  268. r.Add (v1, v2, v3);
  269. v1.TabIndex = 3;
  270. Assert.True (r.Subviews.IndexOf (v1) == 0);
  271. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  272. }
  273. [Fact]
  274. public void TabIndex_Set_CanFocus_LowerValues ()
  275. {
  276. var r = new View ();
  277. var v1 = new View () { CanFocus = true };
  278. var v2 = new View () { CanFocus = true };
  279. var v3 = new View () { CanFocus = true };
  280. r.Add (v1, v2, v3);
  281. v1.TabIndex = -1;
  282. Assert.True (r.Subviews.IndexOf (v1) == 0);
  283. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  284. }
  285. [Fact]
  286. public void TabIndex_Set_CanFocus_False ()
  287. {
  288. var r = new View ();
  289. var v1 = new View () { CanFocus = true };
  290. var v2 = new View () { CanFocus = true };
  291. var v3 = new View () { CanFocus = true };
  292. r.Add (v1, v2, v3);
  293. v1.CanFocus = false;
  294. v1.TabIndex = 0;
  295. Assert.True (r.Subviews.IndexOf (v1) == 0);
  296. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  297. Assert.Equal (-1, v1.TabIndex);
  298. }
  299. [Fact]
  300. public void TabIndex_Set_CanFocus_False_To_True ()
  301. {
  302. var r = new View ();
  303. var v1 = new View ();
  304. var v2 = new View () { CanFocus = true };
  305. var v3 = new View () { CanFocus = true };
  306. r.Add (v1, v2, v3);
  307. v1.CanFocus = true;
  308. v1.TabIndex = 1;
  309. Assert.True (r.Subviews.IndexOf (v1) == 0);
  310. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  311. }
  312. [Fact]
  313. public void TabStop_And_CanFocus_Are_All_True ()
  314. {
  315. var r = new View ();
  316. var v1 = new View () { CanFocus = true };
  317. var v2 = new View () { CanFocus = true };
  318. var v3 = new View () { CanFocus = true };
  319. r.Add (v1, v2, v3);
  320. r.FocusNext ();
  321. Assert.True (v1.HasFocus);
  322. Assert.False (v2.HasFocus);
  323. Assert.False (v3.HasFocus);
  324. r.FocusNext ();
  325. Assert.False (v1.HasFocus);
  326. Assert.True (v2.HasFocus);
  327. Assert.False (v3.HasFocus);
  328. r.FocusNext ();
  329. Assert.False (v1.HasFocus);
  330. Assert.False (v2.HasFocus);
  331. Assert.True (v3.HasFocus);
  332. }
  333. [Fact]
  334. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  335. {
  336. var r = new View ();
  337. var v1 = new View ();
  338. var v2 = new View ();
  339. var v3 = new View ();
  340. r.Add (v1, v2, v3);
  341. r.FocusNext ();
  342. Assert.False (v1.HasFocus);
  343. Assert.False (v2.HasFocus);
  344. Assert.False (v3.HasFocus);
  345. r.FocusNext ();
  346. Assert.False (v1.HasFocus);
  347. Assert.False (v2.HasFocus);
  348. Assert.False (v3.HasFocus);
  349. r.FocusNext ();
  350. Assert.False (v1.HasFocus);
  351. Assert.False (v2.HasFocus);
  352. Assert.False (v3.HasFocus);
  353. }
  354. [Fact]
  355. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  356. {
  357. var r = new View ();
  358. var v1 = new View () { CanFocus = true, TabStop = false };
  359. var v2 = new View () { CanFocus = true, TabStop = false };
  360. var v3 = new View () { CanFocus = true, TabStop = false };
  361. r.Add (v1, v2, v3);
  362. r.FocusNext ();
  363. Assert.False (v1.HasFocus);
  364. Assert.False (v2.HasFocus);
  365. Assert.False (v3.HasFocus);
  366. r.FocusNext ();
  367. Assert.False (v1.HasFocus);
  368. Assert.False (v2.HasFocus);
  369. Assert.False (v3.HasFocus);
  370. r.FocusNext ();
  371. Assert.False (v1.HasFocus);
  372. Assert.False (v2.HasFocus);
  373. Assert.False (v3.HasFocus);
  374. }
  375. [Fact]
  376. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  377. {
  378. var r = new View ();
  379. var v1 = new View () { CanFocus = true, TabStop = false };
  380. var v2 = new View () { CanFocus = false, TabStop = true };
  381. var v3 = new View () { CanFocus = false, TabStop = false };
  382. r.Add (v1, v2, v3);
  383. r.FocusNext ();
  384. Assert.False (v1.HasFocus);
  385. Assert.False (v2.HasFocus);
  386. Assert.False (v3.HasFocus);
  387. r.FocusNext ();
  388. Assert.False (v1.HasFocus);
  389. Assert.False (v2.HasFocus);
  390. Assert.False (v3.HasFocus);
  391. r.FocusNext ();
  392. Assert.False (v1.HasFocus);
  393. Assert.False (v2.HasFocus);
  394. Assert.False (v3.HasFocus);
  395. }
  396. [Fact]
  397. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  398. {
  399. var r = new View ();
  400. var v1 = new View ();
  401. var v2 = new View ();
  402. var v3 = new View ();
  403. r.Add (v1, v2, v3);
  404. r.FocusNext ();
  405. Assert.False (v1.HasFocus);
  406. Assert.False (v2.HasFocus);
  407. Assert.False (v3.HasFocus);
  408. v1.CanFocus = true;
  409. r.FocusNext ();
  410. Assert.True (v1.HasFocus);
  411. Assert.False (v2.HasFocus);
  412. Assert.False (v3.HasFocus);
  413. v2.CanFocus = true;
  414. r.FocusNext ();
  415. Assert.False (v1.HasFocus);
  416. Assert.True (v2.HasFocus);
  417. Assert.False (v3.HasFocus);
  418. v3.CanFocus = true;
  419. r.FocusNext ();
  420. Assert.False (v1.HasFocus);
  421. Assert.False (v2.HasFocus);
  422. Assert.True (v3.HasFocus);
  423. }
  424. [Fact]
  425. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  426. {
  427. var r = new View ();
  428. var v1 = new View () { CanFocus = true, TabStop = false };
  429. var v2 = new View () { CanFocus = true, TabStop = false };
  430. var v3 = new View () { CanFocus = true, TabStop = false };
  431. r.Add (v1, v2, v3);
  432. r.FocusNext ();
  433. Assert.False (v1.HasFocus);
  434. Assert.False (v2.HasFocus);
  435. Assert.False (v3.HasFocus);
  436. v1.TabStop = true;
  437. r.FocusNext ();
  438. Assert.True (v1.HasFocus);
  439. Assert.False (v2.HasFocus);
  440. Assert.False (v3.HasFocus);
  441. v2.TabStop = true;
  442. r.FocusNext ();
  443. Assert.False (v1.HasFocus);
  444. Assert.True (v2.HasFocus);
  445. Assert.False (v3.HasFocus);
  446. v3.TabStop = true;
  447. r.FocusNext ();
  448. Assert.False (v1.HasFocus);
  449. Assert.False (v2.HasFocus);
  450. Assert.True (v3.HasFocus);
  451. }
  452. [Fact]
  453. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  454. {
  455. var r = new View ();
  456. var v1 = new View ("1");
  457. var v2 = new View ("2");
  458. var v3 = new View ("3");
  459. r.Add (v1, v2, v3);
  460. v2.CanFocus = true;
  461. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  462. Assert.Equal (0, v2.TabIndex);
  463. Assert.True (v2.TabStop);
  464. v1.CanFocus = true;
  465. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  466. Assert.Equal (1, v1.TabIndex);
  467. Assert.True (v1.TabStop);
  468. v1.TabIndex = 2;
  469. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  470. Assert.Equal (1, v1.TabIndex);
  471. v3.CanFocus = true;
  472. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  473. Assert.Equal (1, v1.TabIndex);
  474. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  475. Assert.Equal (2, v3.TabIndex);
  476. Assert.True (v3.TabStop);
  477. v2.CanFocus = false;
  478. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  479. Assert.Equal (1, v1.TabIndex);
  480. Assert.True (v1.TabStop);
  481. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  482. Assert.Equal (-1, v2.TabIndex);
  483. Assert.False (v2.TabStop);
  484. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  485. Assert.Equal (2, v3.TabIndex);
  486. Assert.True (v3.TabStop);
  487. }
  488. [Fact]
  489. public void Initialized_Event_Comparing_With_Added_Event ()
  490. {
  491. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  492. var t = new Toplevel () { Id = "0", };
  493. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  494. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  495. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  496. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  497. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  498. w.Added += (e) => {
  499. Assert.Equal (e.Frame.Width, w.Frame.Width);
  500. Assert.Equal (e.Frame.Height, w.Frame.Height);
  501. };
  502. v1.Added += (e) => {
  503. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  504. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  505. };
  506. v2.Added += (e) => {
  507. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  508. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  509. };
  510. sv1.Added += (e) => {
  511. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  512. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  513. };
  514. t.Initialized += (s, e) => {
  515. tc++;
  516. Assert.Equal (1, tc);
  517. Assert.Equal (1, wc);
  518. Assert.Equal (1, v1c);
  519. Assert.Equal (1, v2c);
  520. Assert.Equal (1, sv1c);
  521. Assert.True (t.CanFocus);
  522. Assert.True (w.CanFocus);
  523. Assert.False (v1.CanFocus);
  524. Assert.False (v2.CanFocus);
  525. Assert.False (sv1.CanFocus);
  526. Application.Refresh ();
  527. };
  528. w.Initialized += (s, e) => {
  529. wc++;
  530. Assert.Equal (t.Frame.Width, w.Frame.Width);
  531. Assert.Equal (t.Frame.Height, w.Frame.Height);
  532. };
  533. v1.Initialized += (s, e) => {
  534. v1c++;
  535. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  536. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  537. };
  538. v2.Initialized += (s, e) => {
  539. v2c++;
  540. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  541. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  542. };
  543. sv1.Initialized += (s, e) => {
  544. sv1c++;
  545. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  546. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  547. Assert.False (sv1.CanFocus);
  548. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  549. Assert.False (sv1.CanFocus);
  550. };
  551. v1.Add (sv1);
  552. w.Add (v1, v2);
  553. t.Add (w);
  554. Application.Iteration = () => {
  555. Application.Refresh ();
  556. t.Running = false;
  557. };
  558. Application.Run (t);
  559. Application.Shutdown ();
  560. Assert.Equal (1, tc);
  561. Assert.Equal (1, wc);
  562. Assert.Equal (1, v1c);
  563. Assert.Equal (1, v2c);
  564. Assert.Equal (1, sv1c);
  565. Assert.True (t.CanFocus);
  566. Assert.True (w.CanFocus);
  567. Assert.False (v1.CanFocus);
  568. Assert.False (v2.CanFocus);
  569. Assert.False (sv1.CanFocus);
  570. v1.CanFocus = true;
  571. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  572. }
  573. [Fact]
  574. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  575. {
  576. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  577. var t = new Toplevel () { Id = "0", };
  578. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  579. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  580. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  581. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  582. t.Initialized += (s, e) => {
  583. tc++;
  584. Assert.Equal (1, tc);
  585. Assert.Equal (1, wc);
  586. Assert.Equal (1, v1c);
  587. Assert.Equal (1, v2c);
  588. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  589. Assert.True (t.CanFocus);
  590. Assert.True (w.CanFocus);
  591. Assert.False (v1.CanFocus);
  592. Assert.False (v2.CanFocus);
  593. Application.Refresh ();
  594. };
  595. w.Initialized += (s, e) => {
  596. wc++;
  597. Assert.Equal (t.Frame.Width, w.Frame.Width);
  598. Assert.Equal (t.Frame.Height, w.Frame.Height);
  599. };
  600. v1.Initialized += (s, e) => {
  601. v1c++;
  602. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  603. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  604. };
  605. v2.Initialized += (s, e) => {
  606. v2c++;
  607. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  608. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  609. };
  610. w.Add (v1, v2);
  611. t.Add (w);
  612. Application.Iteration = () => {
  613. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  614. sv1.Initialized += (s, e) => {
  615. sv1c++;
  616. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  617. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  618. Assert.False (sv1.CanFocus);
  619. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  620. Assert.False (sv1.CanFocus);
  621. };
  622. v1.Add (sv1);
  623. Application.Refresh ();
  624. t.Running = false;
  625. };
  626. Application.Run (t);
  627. Application.Shutdown ();
  628. Assert.Equal (1, tc);
  629. Assert.Equal (1, wc);
  630. Assert.Equal (1, v1c);
  631. Assert.Equal (1, v2c);
  632. Assert.Equal (1, sv1c);
  633. Assert.True (t.CanFocus);
  634. Assert.True (w.CanFocus);
  635. Assert.False (v1.CanFocus);
  636. Assert.False (v2.CanFocus);
  637. }
  638. [Fact]
  639. public void CanFocus_Faced_With_Container ()
  640. {
  641. var t = new Toplevel ();
  642. var w = new Window ();
  643. var f = new FrameView ();
  644. var v = new View () { 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. }
  662. [Fact]
  663. public void CanFocus_Faced_With_Container_Before_Run ()
  664. {
  665. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  666. var t = Application.Top;
  667. var w = new Window ("w");
  668. var f = new FrameView ("f");
  669. var v = new View ("v") { CanFocus = true };
  670. f.Add (v);
  671. w.Add (f);
  672. t.Add (w);
  673. Assert.True (t.CanFocus);
  674. Assert.True (w.CanFocus);
  675. Assert.True (f.CanFocus);
  676. Assert.True (v.CanFocus);
  677. f.CanFocus = false;
  678. Assert.False (f.CanFocus);
  679. Assert.True (v.CanFocus);
  680. v.CanFocus = false;
  681. Assert.False (f.CanFocus);
  682. Assert.False (v.CanFocus);
  683. v.CanFocus = true;
  684. Assert.False (f.CanFocus);
  685. Assert.True (v.CanFocus);
  686. Application.Iteration += () => Application.RequestStop ();
  687. Application.Run ();
  688. Application.Shutdown ();
  689. }
  690. [Fact]
  691. public void CanFocus_Faced_With_Container_After_Run ()
  692. {
  693. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  694. var t = Application.Top;
  695. var w = new Window ("w");
  696. var f = new FrameView ("f");
  697. var v = new View ("v") { CanFocus = true };
  698. f.Add (v);
  699. w.Add (f);
  700. t.Add (w);
  701. t.Ready += () => {
  702. Assert.True (t.CanFocus);
  703. Assert.True (w.CanFocus);
  704. Assert.True (f.CanFocus);
  705. Assert.True (v.CanFocus);
  706. f.CanFocus = false;
  707. Assert.False (f.CanFocus);
  708. Assert.False (v.CanFocus);
  709. v.CanFocus = false;
  710. Assert.False (f.CanFocus);
  711. Assert.False (v.CanFocus);
  712. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  713. Assert.False (f.CanFocus);
  714. Assert.False (v.CanFocus);
  715. f.CanFocus = true;
  716. Assert.True (f.CanFocus);
  717. Assert.True (v.CanFocus);
  718. };
  719. Application.Iteration += () => Application.RequestStop ();
  720. Application.Run ();
  721. Application.Shutdown ();
  722. }
  723. [Fact]
  724. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  725. {
  726. Application.Init (new FakeDriver (), new FakeMainLoop (() => 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") { CanFocus = true };
  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.True (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. };
  747. Application.Iteration += () => Application.RequestStop ();
  748. Application.Run ();
  749. Application.Shutdown ();
  750. }
  751. [Fact]
  752. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  753. {
  754. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  755. var t = Application.Top;
  756. var w = new Window ("w");
  757. var f = new FrameView ("f");
  758. var v1 = new View ("v1");
  759. var v2 = new View ("v2") { CanFocus = true };
  760. f.Add (v1, v2);
  761. w.Add (f);
  762. t.Add (w);
  763. t.Ready += () => {
  764. Assert.True (t.CanFocus);
  765. Assert.True (w.CanFocus);
  766. Assert.True (f.CanFocus);
  767. Assert.False (v1.CanFocus);
  768. Assert.True (v2.CanFocus);
  769. w.CanFocus = false;
  770. Assert.True (w.CanFocus);
  771. Assert.False (f.CanFocus);
  772. Assert.False (v1.CanFocus);
  773. Assert.False (v2.CanFocus);
  774. w.CanFocus = true;
  775. Assert.True (w.CanFocus);
  776. Assert.True (f.CanFocus);
  777. Assert.False (v1.CanFocus);
  778. Assert.True (v2.CanFocus);
  779. };
  780. Application.Iteration += () => Application.RequestStop ();
  781. Application.Run ();
  782. Application.Shutdown ();
  783. }
  784. [Fact]
  785. public void Navigation_With_Null_Focused_View ()
  786. {
  787. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  788. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  789. Application.Top.Ready += () => {
  790. Assert.Null (Application.Top.Focused);
  791. };
  792. // Keyboard navigation with tab
  793. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  794. Application.Iteration += () => Application.RequestStop ();
  795. Application.Run ();
  796. Application.Shutdown ();
  797. }
  798. [Fact]
  799. public void Multi_Thread_Toplevels ()
  800. {
  801. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  802. var t = Application.Top;
  803. var w = new Window ();
  804. t.Add (w);
  805. int count = 0, count1 = 0, count2 = 0;
  806. bool log = false, log1 = false, log2 = false;
  807. bool fromTopStillKnowFirstIsRunning = false;
  808. bool fromTopStillKnowSecondIsRunning = false;
  809. bool fromFirstStillKnowSecondIsRunning = false;
  810. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  811. count++;
  812. if (count1 == 5) {
  813. log1 = true;
  814. }
  815. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  816. fromTopStillKnowFirstIsRunning = true;
  817. }
  818. if (count1 == 7 && count2 == 7 && count == 8) {
  819. fromTopStillKnowSecondIsRunning = true;
  820. }
  821. if (count == 30) {
  822. Assert.Equal (30, count);
  823. Assert.Equal (20, count1);
  824. Assert.Equal (10, count2);
  825. Assert.True (log);
  826. Assert.True (log1);
  827. Assert.True (log2);
  828. Assert.True (fromTopStillKnowFirstIsRunning);
  829. Assert.True (fromTopStillKnowSecondIsRunning);
  830. Assert.True (fromFirstStillKnowSecondIsRunning);
  831. Application.RequestStop ();
  832. return false;
  833. }
  834. return true;
  835. });
  836. t.Ready += FirstDialogToplevel;
  837. void FirstDialogToplevel ()
  838. {
  839. var od = new OpenDialog ();
  840. od.Ready += SecoundDialogToplevel;
  841. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  842. count1++;
  843. if (count2 == 5) {
  844. log2 = true;
  845. }
  846. if (count2 == 4 && count1 == 5 && count == 5) {
  847. fromFirstStillKnowSecondIsRunning = true;
  848. }
  849. if (count1 == 20) {
  850. Assert.Equal (20, count1);
  851. Application.RequestStop ();
  852. return false;
  853. }
  854. return true;
  855. });
  856. Application.Run (od);
  857. }
  858. void SecoundDialogToplevel ()
  859. {
  860. var d = new Dialog ();
  861. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  862. count2++;
  863. if (count < 30) {
  864. log = true;
  865. }
  866. if (count2 == 10) {
  867. Assert.Equal (10, count2);
  868. Application.RequestStop ();
  869. return false;
  870. }
  871. return true;
  872. });
  873. Application.Run (d);
  874. }
  875. Application.Run ();
  876. Application.Shutdown ();
  877. }
  878. [Fact]
  879. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  880. {
  881. // Object Initializer
  882. var view = new View () {
  883. X = 1,
  884. Y = 2,
  885. Width = 3,
  886. Height = 4
  887. };
  888. Assert.Equal (1, view.X);
  889. Assert.Equal (2, view.Y);
  890. Assert.Equal (3, view.Width);
  891. Assert.Equal (4, view.Height);
  892. Assert.False (view.Frame.IsEmpty);
  893. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  894. Assert.False (view.Bounds.IsEmpty);
  895. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  896. view.LayoutSubviews ();
  897. Assert.Equal (1, view.X);
  898. Assert.Equal (2, view.Y);
  899. Assert.Equal (3, view.Width);
  900. Assert.Equal (4, view.Height);
  901. Assert.False (view.Frame.IsEmpty);
  902. Assert.False (view.Bounds.IsEmpty);
  903. // Default Constructor
  904. view = new View ();
  905. Assert.Equal (0, view.X);
  906. Assert.Equal (0, view.Y);
  907. Assert.Equal (0, view.Width);
  908. Assert.Equal (0, view.Height);
  909. Assert.True (view.Frame.IsEmpty);
  910. Assert.True (view.Bounds.IsEmpty);
  911. // Constructor
  912. view = new View (1, 2, "");
  913. Assert.NotNull (view.X);
  914. Assert.NotNull (view.Y);
  915. Assert.NotNull (view.Width);
  916. Assert.NotNull (view.Height);
  917. Assert.False (view.Frame.IsEmpty);
  918. Assert.True (view.Bounds.IsEmpty);
  919. // Default Constructor and post assignment equivalent to Object Initializer
  920. view = new View ();
  921. view.X = 1;
  922. view.Y = 2;
  923. view.Width = 3;
  924. view.Height = 4;
  925. Assert.Equal (1, view.X);
  926. Assert.Equal (2, view.Y);
  927. Assert.Equal (3, view.Width);
  928. Assert.Equal (4, view.Height);
  929. Assert.False (view.Frame.IsEmpty);
  930. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  931. Assert.False (view.Bounds.IsEmpty);
  932. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  933. }
  934. [Fact]
  935. public void FocusNearestView_Ensure_Focus_Ordered ()
  936. {
  937. var top = new Toplevel ();
  938. var win = new Window ();
  939. var winSubview = new View ("WindowSubview") {
  940. CanFocus = true
  941. };
  942. win.Add (winSubview);
  943. top.Add (win);
  944. var frm = new FrameView ();
  945. var frmSubview = new View ("FrameSubview") {
  946. CanFocus = true
  947. };
  948. frm.Add (frmSubview);
  949. top.Add (frm);
  950. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  951. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  952. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  953. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  954. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  955. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  956. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  957. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  958. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  959. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  960. }
  961. [Fact]
  962. public void KeyPress_Handled_To_True_Prevents_Changes ()
  963. {
  964. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  965. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  966. var top = Application.Top;
  967. var text = new TextField ("");
  968. text.KeyPress += (e) => {
  969. e.Handled = true;
  970. Assert.True (e.Handled);
  971. Assert.Equal (Key.N, e.KeyEvent.Key);
  972. };
  973. top.Add (text);
  974. Application.Iteration += () => {
  975. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  976. Assert.Equal ("", text.Text);
  977. Application.RequestStop ();
  978. };
  979. Application.Run ();
  980. // Shutdown must be called to safely clean up Application if Init has been called
  981. Application.Shutdown ();
  982. }
  983. [Fact]
  984. public void SetWidth_CanSetWidth ()
  985. {
  986. var top = new View () {
  987. X = 0,
  988. Y = 0,
  989. Width = 80,
  990. };
  991. var v = new View () {
  992. Width = Dim.Fill ()
  993. };
  994. top.Add (v);
  995. Assert.False (v.SetWidth (70, out int rWidth));
  996. Assert.Equal (70, rWidth);
  997. v.Width = Dim.Fill (1);
  998. Assert.False (v.SetWidth (70, out rWidth));
  999. Assert.Equal (69, rWidth);
  1000. v.Width = null;
  1001. Assert.True (v.SetWidth (70, out rWidth));
  1002. Assert.Equal (70, rWidth);
  1003. v.IsInitialized = true;
  1004. v.Width = Dim.Fill (1);
  1005. Assert.Throws<ArgumentException> (() => v.Width = 75);
  1006. v.LayoutStyle = LayoutStyle.Absolute;
  1007. v.Width = 75;
  1008. Assert.True (v.SetWidth (60, out rWidth));
  1009. Assert.Equal (60, rWidth);
  1010. }
  1011. [Fact]
  1012. public void SetHeight_CanSetHeight ()
  1013. {
  1014. var top = new View () {
  1015. X = 0,
  1016. Y = 0,
  1017. Height = 20
  1018. };
  1019. var v = new View () {
  1020. Height = Dim.Fill ()
  1021. };
  1022. top.Add (v);
  1023. Assert.False (v.SetHeight (10, out int rHeight));
  1024. Assert.Equal (10, rHeight);
  1025. v.Height = Dim.Fill (1);
  1026. Assert.False (v.SetHeight (10, out rHeight));
  1027. Assert.Equal (9, rHeight);
  1028. v.Height = null;
  1029. Assert.True (v.SetHeight (10, out rHeight));
  1030. Assert.Equal (10, rHeight);
  1031. v.IsInitialized = true;
  1032. v.Height = Dim.Fill (1);
  1033. Assert.Throws<ArgumentException> (() => v.Height = 15);
  1034. v.LayoutStyle = LayoutStyle.Absolute;
  1035. v.Height = 15;
  1036. Assert.True (v.SetHeight (5, out rHeight));
  1037. Assert.Equal (5, rHeight);
  1038. }
  1039. [Fact]
  1040. public void GetCurrentWidth_CanSetWidth ()
  1041. {
  1042. var top = new View () {
  1043. X = 0,
  1044. Y = 0,
  1045. Width = 80,
  1046. };
  1047. var v = new View () {
  1048. Width = Dim.Fill ()
  1049. };
  1050. top.Add (v);
  1051. Assert.False (v.GetCurrentWidth (out int cWidth));
  1052. Assert.Equal (80, cWidth);
  1053. v.Width = Dim.Fill (1);
  1054. Assert.False (v.GetCurrentWidth (out cWidth));
  1055. Assert.Equal (79, cWidth);
  1056. }
  1057. [Fact]
  1058. public void GetCurrentHeight_CanSetHeight ()
  1059. {
  1060. var top = new View () {
  1061. X = 0,
  1062. Y = 0,
  1063. Height = 20
  1064. };
  1065. var v = new View () {
  1066. Height = Dim.Fill ()
  1067. };
  1068. top.Add (v);
  1069. Assert.False (v.GetCurrentHeight (out int cHeight));
  1070. Assert.Equal (20, cHeight);
  1071. v.Height = Dim.Fill (1);
  1072. Assert.False (v.GetCurrentHeight (out cHeight));
  1073. Assert.Equal (19, cHeight);
  1074. }
  1075. [Fact]
  1076. public void AutoSize_False_ResizeView_Is_Always_False ()
  1077. {
  1078. var label = new Label () { AutoSize = false };
  1079. label.Text = "New text";
  1080. Assert.False (label.AutoSize);
  1081. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  1082. }
  1083. [Fact]
  1084. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  1085. {
  1086. var label = new Label ();
  1087. label.Text = "New text";
  1088. Assert.True (label.AutoSize);
  1089. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  1090. }
  1091. [Fact]
  1092. public void AutoSize_True_ResizeView_With_Dim_Fill ()
  1093. {
  1094. var win = new Window (new Rect (0, 0, 30, 80), "");
  1095. var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () };
  1096. win.Add (label);
  1097. label.Text = "New text\nNew line";
  1098. win.LayoutSubviews ();
  1099. Assert.True (label.AutoSize);
  1100. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  1101. }
  1102. [Fact]
  1103. public void AutoSize_True_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute ()
  1104. {
  1105. var win = new Window (new Rect (0, 0, 30, 80), "");
  1106. var label = new Label () { Width = Dim.Fill () };
  1107. win.Add (label);
  1108. label.Text = "New text\nNew line";
  1109. win.LayoutSubviews ();
  1110. Assert.True (label.AutoSize);
  1111. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  1112. }
  1113. [Theory]
  1114. [InlineData (1)]
  1115. [InlineData (2)]
  1116. [InlineData (3)]
  1117. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  1118. {
  1119. var driver = new FakeDriver ();
  1120. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  1121. try {
  1122. // Create a label with a short text
  1123. Label lbl;
  1124. var text = "test";
  1125. if (choice == 1) {
  1126. // An object initializer should call the default constructor.
  1127. lbl = new Label { Text = text };
  1128. } else if (choice == 2) {
  1129. // Calling the default constructor followed by the object initializer.
  1130. lbl = new Label () { Text = text };
  1131. } else {
  1132. // Calling the Text constructor.
  1133. lbl = new Label (text);
  1134. }
  1135. lbl.ColorScheme = new ColorScheme ();
  1136. lbl.Redraw (lbl.Bounds);
  1137. // should have the initial text
  1138. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  1139. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  1140. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  1141. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  1142. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  1143. } finally {
  1144. Application.Shutdown ();
  1145. }
  1146. }
  1147. [Fact]
  1148. [AutoInitShutdown]
  1149. public void Internal_Tests ()
  1150. {
  1151. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1152. Enum.GetValues (typeof (View.Direction)));
  1153. var rect = new Rect (1, 1, 10, 1);
  1154. var view = new View (rect);
  1155. var top = Application.Top;
  1156. top.Add (view);
  1157. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1158. view.FocusDirection = View.Direction.Backward;
  1159. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1160. Assert.Empty (view.InternalSubviews);
  1161. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view.NeedDisplay);
  1162. Assert.True (view.LayoutNeeded);
  1163. Assert.False (view.ChildNeedsDisplay);
  1164. Assert.False (view.addingView);
  1165. view.addingView = true;
  1166. Assert.True (view.addingView);
  1167. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1168. Assert.Equal (1, rcol);
  1169. Assert.Equal (1, rrow);
  1170. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1171. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1172. view.Width = Dim.Fill ();
  1173. view.Height = Dim.Fill ();
  1174. Assert.Equal (10, view.Bounds.Width);
  1175. Assert.Equal (1, view.Bounds.Height);
  1176. view.SetRelativeLayout (top.Bounds);
  1177. Assert.Equal (79, view.Bounds.Width);
  1178. Assert.Equal (24, view.Bounds.Height);
  1179. bool layoutStarted = false;
  1180. view.LayoutStarted += (_) => { layoutStarted = true; };
  1181. view.OnLayoutStarted (null);
  1182. Assert.True (layoutStarted);
  1183. view.LayoutComplete += (_) => { layoutStarted = false; };
  1184. view.OnLayoutComplete (null);
  1185. Assert.False (layoutStarted);
  1186. }
  1187. }
  1188. }