ViewTests.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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. }
  1114. }