DimTests.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using Terminal.Gui;
  9. using Terminal.Gui.Views;
  10. using Xunit;
  11. using Xunit.Abstractions;
  12. // Alias Console to MockConsole so we don't accidentally use Console
  13. using Console = Terminal.Gui.FakeConsole;
  14. namespace Terminal.Gui.Core {
  15. public class DimTests {
  16. readonly ITestOutputHelper output;
  17. public DimTests (ITestOutputHelper output)
  18. {
  19. this.output = output;
  20. Console.OutputEncoding = System.Text.Encoding.Default;
  21. // Change current culture
  22. CultureInfo culture = CultureInfo.CreateSpecificCulture ("en-US");
  23. Thread.CurrentThread.CurrentCulture = culture;
  24. Thread.CurrentThread.CurrentUICulture = culture;
  25. }
  26. [Fact]
  27. public void New_Works ()
  28. {
  29. var dim = new Dim ();
  30. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  31. }
  32. [Fact]
  33. public void Sized_SetsValue ()
  34. {
  35. var dim = Dim.Sized (0);
  36. Assert.Equal ("Dim.Absolute(0)", dim.ToString ());
  37. int testVal = 5;
  38. dim = Dim.Sized (testVal);
  39. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  40. testVal = -1;
  41. dim = Dim.Sized (testVal);
  42. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  43. }
  44. [Fact]
  45. public void Sized_Equals ()
  46. {
  47. int n1 = 0;
  48. int n2 = 0;
  49. var dim1 = Dim.Sized (n1);
  50. var dim2 = Dim.Sized (n2);
  51. Assert.Equal (dim1, dim2);
  52. n1 = n2 = 1;
  53. dim1 = Dim.Sized (n1);
  54. dim2 = Dim.Sized (n2);
  55. Assert.Equal (dim1, dim2);
  56. n1 = n2 = -1;
  57. dim1 = Dim.Sized (n1);
  58. dim2 = Dim.Sized (n2);
  59. Assert.Equal (dim1, dim2);
  60. n1 = 0;
  61. n2 = 1;
  62. dim1 = Dim.Sized (n1);
  63. dim2 = Dim.Sized (n2);
  64. Assert.NotEqual (dim1, dim2);
  65. }
  66. [Fact]
  67. public void Width_SetsValue ()
  68. {
  69. var dim = Dim.Width (null);
  70. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  71. var testVal = Rect.Empty;
  72. testVal = Rect.Empty;
  73. dim = Dim.Width (new View (testVal));
  74. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  75. testVal = new Rect (1, 2, 3, 4);
  76. dim = Dim.Width (new View (testVal));
  77. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  78. }
  79. [Fact]
  80. public void Width_Equals ()
  81. {
  82. var testRect1 = Rect.Empty;
  83. var view1 = new View (testRect1);
  84. var testRect2 = Rect.Empty;
  85. var view2 = new View (testRect2);
  86. var dim1 = Dim.Width (view1);
  87. var dim2 = Dim.Width (view1);
  88. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  89. Assert.Equal (dim1, dim2);
  90. dim2 = Dim.Width (view2);
  91. Assert.NotEqual (dim1, dim2);
  92. testRect1 = new Rect (0, 1, 2, 3);
  93. view1 = new View (testRect1);
  94. testRect2 = new Rect (0, 1, 2, 3);
  95. dim1 = Dim.Width (view1);
  96. dim2 = Dim.Width (view1);
  97. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  98. Assert.Equal (dim1, dim2);
  99. Assert.Throws<ArgumentException> (() => new Rect (0, -1, -2, -3));
  100. testRect1 = new Rect (0, -1, 2, 3);
  101. view1 = new View (testRect1);
  102. testRect2 = new Rect (0, -1, 2, 3);
  103. dim1 = Dim.Width (view1);
  104. dim2 = Dim.Width (view1);
  105. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  106. Assert.Equal (dim1, dim2);
  107. testRect1 = new Rect (0, -1, 2, 3);
  108. view1 = new View (testRect1);
  109. testRect2 = Rect.Empty;
  110. view2 = new View (testRect2);
  111. dim1 = Dim.Width (view1);
  112. dim2 = Dim.Width (view2);
  113. Assert.NotEqual (dim1, dim2);
  114. }
  115. [Fact]
  116. public void Height_SetsValue ()
  117. {
  118. var dim = Dim.Height (null);
  119. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  120. var testVal = Rect.Empty;
  121. testVal = Rect.Empty;
  122. dim = Dim.Height (new View (testVal));
  123. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  124. testVal = new Rect (1, 2, 3, 4);
  125. dim = Dim.Height (new View (testVal));
  126. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  127. }
  128. // TODO: Other Dim.Height tests (e.g. Equal?)
  129. [Fact]
  130. public void Fill_SetsValue ()
  131. {
  132. var testMargin = 0;
  133. var dim = Dim.Fill ();
  134. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  135. testMargin = 0;
  136. dim = Dim.Fill (testMargin);
  137. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  138. testMargin = 5;
  139. dim = Dim.Fill (testMargin);
  140. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  141. }
  142. [Fact]
  143. public void Fill_Equal ()
  144. {
  145. var margin1 = 0;
  146. var margin2 = 0;
  147. var dim1 = Dim.Fill (margin1);
  148. var dim2 = Dim.Fill (margin2);
  149. Assert.Equal (dim1, dim2);
  150. }
  151. [Fact]
  152. public void Percent_SetsValue ()
  153. {
  154. float f = 0;
  155. var dim = Dim.Percent (f);
  156. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  157. f = 0.5F;
  158. dim = Dim.Percent (f);
  159. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  160. f = 100;
  161. dim = Dim.Percent (f);
  162. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  163. }
  164. [Fact]
  165. public void Percent_Equals ()
  166. {
  167. float n1 = 0;
  168. float n2 = 0;
  169. var dim1 = Dim.Percent (n1);
  170. var dim2 = Dim.Percent (n2);
  171. Assert.Equal (dim1, dim2);
  172. n1 = n2 = 1;
  173. dim1 = Dim.Percent (n1);
  174. dim2 = Dim.Percent (n2);
  175. Assert.Equal (dim1, dim2);
  176. n1 = n2 = 0.5f;
  177. dim1 = Dim.Percent (n1);
  178. dim2 = Dim.Percent (n2);
  179. Assert.Equal (dim1, dim2);
  180. n1 = n2 = 100f;
  181. dim1 = Dim.Percent (n1);
  182. dim2 = Dim.Percent (n2);
  183. Assert.Equal (dim1, dim2);
  184. n1 = n2 = 0.3f;
  185. dim1 = Dim.Percent (n1, true);
  186. dim2 = Dim.Percent (n2, true);
  187. Assert.Equal (dim1, dim2);
  188. n1 = n2 = 0.3f;
  189. dim1 = Dim.Percent (n1);
  190. dim2 = Dim.Percent (n2, true);
  191. Assert.NotEqual (dim1, dim2);
  192. n1 = 0;
  193. n2 = 1;
  194. dim1 = Dim.Percent (n1);
  195. dim2 = Dim.Percent (n2);
  196. Assert.NotEqual (dim1, dim2);
  197. n1 = 0.5f;
  198. n2 = 1.5f;
  199. dim1 = Dim.Percent (n1);
  200. dim2 = Dim.Percent (n2);
  201. Assert.NotEqual (dim1, dim2);
  202. }
  203. [Fact]
  204. public void Percent_ThrowsOnIvalid ()
  205. {
  206. var dim = Dim.Percent (0);
  207. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  208. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  209. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  210. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  211. }
  212. [Fact]
  213. public void ForceValidatePosDim_True_Dim_Validation_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type ()
  214. {
  215. Application.Init (new FakeDriver ());
  216. var t = Application.Top;
  217. var w = new Window ("w") {
  218. Width = Dim.Fill (0),
  219. Height = Dim.Sized (10)
  220. };
  221. var v = new View ("v") {
  222. Width = Dim.Width (w) - 2,
  223. Height = Dim.Percent (10),
  224. ForceValidatePosDim = true
  225. };
  226. w.Add (v);
  227. t.Add (w);
  228. t.Ready += () => {
  229. Assert.Equal (2, w.Width = 2);
  230. Assert.Equal (2, w.Height = 2);
  231. Assert.Throws<ArgumentException> (() => v.Width = 2);
  232. Assert.Throws<ArgumentException> (() => v.Height = 2);
  233. v.ForceValidatePosDim = false;
  234. var exception = Record.Exception (() => v.Width = 2);
  235. Assert.Null (exception);
  236. Assert.Equal (2, v.Width);
  237. exception = Record.Exception (() => v.Height = 2);
  238. Assert.Null (exception);
  239. Assert.Equal (2, v.Height);
  240. };
  241. Application.Iteration += () => Application.RequestStop ();
  242. Application.Run ();
  243. Application.Shutdown ();
  244. }
  245. [Fact]
  246. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  247. {
  248. Application.Init (new FakeDriver ());
  249. var t = Application.Top;
  250. var w = new Window (new Rect (1, 2, 4, 5), "w");
  251. t.Add (w);
  252. t.Ready += () => {
  253. Assert.Equal (3, w.Width = 3);
  254. Assert.Equal (4, w.Height = 4);
  255. };
  256. Application.Iteration += () => Application.RequestStop ();
  257. Application.Run ();
  258. Application.Shutdown ();
  259. }
  260. [Fact]
  261. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  262. {
  263. Application.Init (new FakeDriver ());
  264. var t = Application.Top;
  265. var w = new Window ("w") {
  266. Width = Dim.Fill (0),
  267. Height = Dim.Sized (10)
  268. };
  269. var v = new View ("v") {
  270. Width = Dim.Width (w) - 2,
  271. Height = Dim.Percent (10)
  272. };
  273. w.Add (v);
  274. t.Add (w);
  275. t.Ready += () => {
  276. v.LayoutStyle = LayoutStyle.Absolute;
  277. Assert.Equal (2, v.Width = 2);
  278. Assert.Equal (2, v.Height = 2);
  279. };
  280. Application.Iteration += () => Application.RequestStop ();
  281. Application.Run ();
  282. Application.Shutdown ();
  283. }
  284. [Fact]
  285. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  286. {
  287. // Testing with the Button because it properly handles the Dim class.
  288. Application.Init (new FakeDriver ());
  289. var t = Application.Top;
  290. var w = new Window ("w") {
  291. Width = 100,
  292. Height = 100
  293. };
  294. var f1 = new FrameView ("f1") {
  295. X = 0,
  296. Y = 0,
  297. Width = Dim.Percent (50),
  298. Height = 5
  299. };
  300. var f2 = new FrameView ("f2") {
  301. X = Pos.Right (f1),
  302. Y = 0,
  303. Width = Dim.Fill (),
  304. Height = 5
  305. };
  306. var v1 = new Button ("v1") {
  307. AutoSize = false,
  308. X = Pos.X (f1) + 2,
  309. Y = Pos.Bottom (f1) + 2,
  310. Width = Dim.Width (f1) - 2,
  311. Height = Dim.Fill () - 2,
  312. ForceValidatePosDim = true
  313. };
  314. var v2 = new Button ("v2") {
  315. AutoSize = false,
  316. X = Pos.X (f2) + 2,
  317. Y = Pos.Bottom (f2) + 2,
  318. Width = Dim.Width (f2) - 2,
  319. Height = Dim.Fill () - 2,
  320. ForceValidatePosDim = true
  321. };
  322. var v3 = new Button ("v3") {
  323. AutoSize = false,
  324. Width = Dim.Percent (10),
  325. Height = Dim.Percent (10),
  326. ForceValidatePosDim = true
  327. };
  328. var v4 = new Button ("v4") {
  329. AutoSize = false,
  330. Width = Dim.Sized (50),
  331. Height = Dim.Sized (50),
  332. ForceValidatePosDim = true
  333. };
  334. var v5 = new Button ("v5") {
  335. AutoSize = false,
  336. Width = Dim.Width (v1) - Dim.Width (v3),
  337. Height = Dim.Height (v1) - Dim.Height (v3),
  338. ForceValidatePosDim = true
  339. };
  340. var v6 = new Button ("v6") {
  341. AutoSize = false,
  342. X = Pos.X (f2),
  343. Y = Pos.Bottom (f2) + 2,
  344. Width = Dim.Percent (20, true),
  345. Height = Dim.Percent (20, true),
  346. ForceValidatePosDim = true
  347. };
  348. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  349. t.Add (w);
  350. t.Ready += () => {
  351. Assert.Equal ("Dim.Absolute(100)", w.Width.ToString ());
  352. Assert.Equal ("Dim.Absolute(100)", w.Height.ToString ());
  353. Assert.Equal (100, w.Frame.Width);
  354. Assert.Equal (100, w.Frame.Height);
  355. Assert.Equal ("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString ());
  356. Assert.Equal ("Dim.Absolute(5)", f1.Height.ToString ());
  357. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  358. Assert.Equal (5, f1.Frame.Height);
  359. Assert.Equal ("Dim.Fill(margin=0)", f2.Width.ToString ());
  360. Assert.Equal ("Dim.Absolute(5)", f2.Height.ToString ());
  361. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  362. Assert.Equal (5, f2.Frame.Height);
  363. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v1.Width.ToString ());
  364. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v1.Height.ToString ());
  365. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  366. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  367. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=49,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v2.Width.ToString ());
  368. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v2.Height.ToString ());
  369. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  370. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  371. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString ());
  372. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Height.ToString ());
  373. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  374. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  375. Assert.Equal ("Dim.Absolute(50)", v4.Width.ToString ());
  376. Assert.Equal ("Dim.Absolute(50)", v4.Height.ToString ());
  377. Assert.Equal (50, v4.Frame.Width);
  378. Assert.Equal (50, v4.Frame.Height);
  379. Assert.Equal ("Dim.Combine(DimView(side=Width, target=Button()({X=2,Y=7,Width=47,Height=89}))-DimView(side=Width, target=Button()({X=0,Y=0,Width=9,Height=9})))", v5.Width.ToString ());
  380. Assert.Equal ("Dim.Combine(DimView(side=Height, target=Button()({X=2,Y=7,Width=47,Height=89}))-DimView(side=Height, target=Button()({X=0,Y=0,Width=9,Height=9})))", v5.Height.ToString ());
  381. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  382. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  383. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString ());
  384. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Height.ToString ());
  385. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  386. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  387. w.Width = 200;
  388. w.Height = 200;
  389. t.LayoutSubviews ();
  390. Assert.Equal ("Dim.Absolute(200)", w.Width.ToString ());
  391. Assert.Equal ("Dim.Absolute(200)", w.Height.ToString ());
  392. Assert.Equal (200, w.Frame.Width);
  393. Assert.Equal (200, w.Frame.Height);
  394. f1.Text = "Frame1";
  395. Assert.Equal ("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString ());
  396. Assert.Equal ("Dim.Absolute(5)", f1.Height.ToString ());
  397. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  398. Assert.Equal (5, f1.Frame.Height);
  399. f2.Text = "Frame2";
  400. Assert.Equal ("Dim.Fill(margin=0)", f2.Width.ToString ());
  401. Assert.Equal ("Dim.Absolute(5)", f2.Height.ToString ());
  402. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  403. Assert.Equal (5, f2.Frame.Height);
  404. v1.Text = "Button1";
  405. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v1.Width.ToString ());
  406. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v1.Height.ToString ());
  407. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  408. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  409. v2.Text = "Button2";
  410. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=99,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v2.Width.ToString ());
  411. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v2.Height.ToString ());
  412. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  413. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  414. v3.Text = "Button3";
  415. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString ());
  416. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Height.ToString ());
  417. Assert.Equal (19, v3.Frame.Width); // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  418. Assert.Equal (19, v3.Frame.Height); // 199*10%=19
  419. v4.Text = "Button4";
  420. v4.AutoSize = false;
  421. Assert.Equal ("Dim.Absolute(50)", v4.Width.ToString ());
  422. Assert.Equal ("Dim.Absolute(50)", v4.Height.ToString ());
  423. Assert.Equal (50, v4.Frame.Width);
  424. Assert.Equal (50, v4.Frame.Height);
  425. v4.AutoSize = true;
  426. Assert.Equal ("Dim.Absolute(11)", v4.Width.ToString ());
  427. Assert.Equal ("Dim.Absolute(1)", v4.Height.ToString ());
  428. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  429. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  430. v5.Text = "Button5";
  431. Assert.Equal ("Dim.Combine(DimView(side=Width, target=Button()({X=2,Y=7,Width=97,Height=189}))-DimView(side=Width, target=Button()({X=0,Y=0,Width=19,Height=19})))", v5.Width.ToString ());
  432. Assert.Equal ("Dim.Combine(DimView(side=Height, target=Button()({X=2,Y=7,Width=97,Height=189}))-DimView(side=Height, target=Button()({X=0,Y=0,Width=19,Height=19})))", v5.Height.ToString ());
  433. Assert.Equal (78, v5.Frame.Width); // 97-19=78
  434. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  435. v6.Text = "Button6";
  436. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString ());
  437. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Height.ToString ());
  438. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  439. Assert.Equal (38, v6.Frame.Height); // 198-7*20=38
  440. };
  441. Application.Iteration += () => Application.RequestStop ();
  442. Application.Run ();
  443. Application.Shutdown ();
  444. }
  445. // DONE: Test operators
  446. [Fact]
  447. public void DimCombine_Do_Not_Throws ()
  448. {
  449. Application.Init (new FakeDriver ());
  450. var t = Application.Top;
  451. var w = new Window ("w") {
  452. Width = Dim.Width (t) - 2,
  453. Height = Dim.Height (t) - 2
  454. };
  455. var f = new FrameView ("f");
  456. var v1 = new View ("v1") {
  457. Width = Dim.Width (w) - 2,
  458. Height = Dim.Height (w) - 2
  459. };
  460. var v2 = new View ("v2") {
  461. Width = Dim.Width (v1) - 2,
  462. Height = Dim.Height (v1) - 2
  463. };
  464. f.Add (v1, v2);
  465. w.Add (f);
  466. t.Add (w);
  467. f.Width = Dim.Width (t) - Dim.Width (v2);
  468. f.Height = Dim.Height (t) - Dim.Height (v2);
  469. t.Ready += () => {
  470. Assert.Equal (80, t.Frame.Width);
  471. Assert.Equal (25, t.Frame.Height);
  472. Assert.Equal (78, w.Frame.Width);
  473. Assert.Equal (23, w.Frame.Height);
  474. Assert.Equal (6, f.Frame.Width);
  475. Assert.Equal (6, f.Frame.Height);
  476. Assert.Equal (76, v1.Frame.Width);
  477. Assert.Equal (21, v1.Frame.Height);
  478. Assert.Equal (74, v2.Frame.Width);
  479. Assert.Equal (19, v2.Frame.Height);
  480. };
  481. Application.Iteration += () => Application.RequestStop ();
  482. Application.Run ();
  483. Application.Shutdown ();
  484. }
  485. [Fact]
  486. public void PosCombine_Will_Throws ()
  487. {
  488. Application.Init (new FakeDriver ());
  489. var t = Application.Top;
  490. var w = new Window ("w") {
  491. Width = Dim.Width (t) - 2,
  492. Height = Dim.Height (t) - 2
  493. };
  494. var f = new FrameView ("f");
  495. var v1 = new View ("v1") {
  496. Width = Dim.Width (w) - 2,
  497. Height = Dim.Height (w) - 2
  498. };
  499. var v2 = new View ("v2") {
  500. Width = Dim.Width (v1) - 2,
  501. Height = Dim.Height (v1) - 2
  502. };
  503. f.Add (v1); // v2 not added
  504. w.Add (f);
  505. t.Add (w);
  506. f.Width = Dim.Width (t) - Dim.Width (v2);
  507. f.Height = Dim.Height (t) - Dim.Height (v2);
  508. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  509. Application.Shutdown ();
  510. }
  511. [Fact]
  512. public void Dim_Add_Operator ()
  513. {
  514. Application.Init (new FakeDriver ());
  515. var top = Application.Top;
  516. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  517. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  518. var count = 0;
  519. field.KeyDown += (k) => {
  520. if (k.KeyEvent.Key == Key.Enter) {
  521. field.Text = $"Label {count}";
  522. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  523. view.Add (label);
  524. Assert.Equal ($"Label {count}", label.Text);
  525. Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
  526. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  527. view.Height += 1;
  528. count++;
  529. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  530. }
  531. };
  532. Application.Iteration += () => {
  533. while (count < 20) {
  534. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  535. }
  536. Application.RequestStop ();
  537. };
  538. var win = new Window ();
  539. win.Add (view);
  540. win.Add (field);
  541. top.Add (win);
  542. Application.Run (top);
  543. Assert.Equal (20, count);
  544. // Shutdown must be called to safely clean up Application if Init has been called
  545. Application.Shutdown ();
  546. }
  547. private string [] expecteds = new string [21] {
  548. @"
  549. ┌────────────────────┐
  550. │View with long text │
  551. │ │
  552. └────────────────────┘",
  553. @"
  554. ┌────────────────────┐
  555. │View with long text │
  556. │Label 0 │
  557. │Label 0 │
  558. └────────────────────┘",
  559. @"
  560. ┌────────────────────┐
  561. │View with long text │
  562. │Label 0 │
  563. │Label 1 │
  564. │Label 1 │
  565. └────────────────────┘",
  566. @"
  567. ┌────────────────────┐
  568. │View with long text │
  569. │Label 0 │
  570. │Label 1 │
  571. │Label 2 │
  572. │Label 2 │
  573. └────────────────────┘",
  574. @"
  575. ┌────────────────────┐
  576. │View with long text │
  577. │Label 0 │
  578. │Label 1 │
  579. │Label 2 │
  580. │Label 3 │
  581. │Label 3 │
  582. └────────────────────┘",
  583. @"
  584. ┌────────────────────┐
  585. │View with long text │
  586. │Label 0 │
  587. │Label 1 │
  588. │Label 2 │
  589. │Label 3 │
  590. │Label 4 │
  591. │Label 4 │
  592. └────────────────────┘",
  593. @"
  594. ┌────────────────────┐
  595. │View with long text │
  596. │Label 0 │
  597. │Label 1 │
  598. │Label 2 │
  599. │Label 3 │
  600. │Label 4 │
  601. │Label 5 │
  602. │Label 5 │
  603. └────────────────────┘",
  604. @"
  605. ┌────────────────────┐
  606. │View with long text │
  607. │Label 0 │
  608. │Label 1 │
  609. │Label 2 │
  610. │Label 3 │
  611. │Label 4 │
  612. │Label 5 │
  613. │Label 6 │
  614. │Label 6 │
  615. └────────────────────┘",
  616. @"
  617. ┌────────────────────┐
  618. │View with long text │
  619. │Label 0 │
  620. │Label 1 │
  621. │Label 2 │
  622. │Label 3 │
  623. │Label 4 │
  624. │Label 5 │
  625. │Label 6 │
  626. │Label 7 │
  627. │Label 7 │
  628. └────────────────────┘",
  629. @"
  630. ┌────────────────────┐
  631. │View with long text │
  632. │Label 0 │
  633. │Label 1 │
  634. │Label 2 │
  635. │Label 3 │
  636. │Label 4 │
  637. │Label 5 │
  638. │Label 6 │
  639. │Label 7 │
  640. │Label 8 │
  641. │Label 8 │
  642. └────────────────────┘",
  643. @"
  644. ┌────────────────────┐
  645. │View with long text │
  646. │Label 0 │
  647. │Label 1 │
  648. │Label 2 │
  649. │Label 3 │
  650. │Label 4 │
  651. │Label 5 │
  652. │Label 6 │
  653. │Label 7 │
  654. │Label 8 │
  655. │Label 9 │
  656. │Label 9 │
  657. └────────────────────┘",
  658. @"
  659. ┌────────────────────┐
  660. │View with long text │
  661. │Label 0 │
  662. │Label 1 │
  663. │Label 2 │
  664. │Label 3 │
  665. │Label 4 │
  666. │Label 5 │
  667. │Label 6 │
  668. │Label 7 │
  669. │Label 8 │
  670. │Label 9 │
  671. │Label 10 │
  672. │Label 10 │
  673. └────────────────────┘",
  674. @"
  675. ┌────────────────────┐
  676. │View with long text │
  677. │Label 0 │
  678. │Label 1 │
  679. │Label 2 │
  680. │Label 3 │
  681. │Label 4 │
  682. │Label 5 │
  683. │Label 6 │
  684. │Label 7 │
  685. │Label 8 │
  686. │Label 9 │
  687. │Label 10 │
  688. │Label 11 │
  689. │Label 11 │
  690. └────────────────────┘",
  691. @"
  692. ┌────────────────────┐
  693. │View with long text │
  694. │Label 0 │
  695. │Label 1 │
  696. │Label 2 │
  697. │Label 3 │
  698. │Label 4 │
  699. │Label 5 │
  700. │Label 6 │
  701. │Label 7 │
  702. │Label 8 │
  703. │Label 9 │
  704. │Label 10 │
  705. │Label 11 │
  706. │Label 12 │
  707. │Label 12 │
  708. └────────────────────┘",
  709. @"
  710. ┌────────────────────┐
  711. │View with long text │
  712. │Label 0 │
  713. │Label 1 │
  714. │Label 2 │
  715. │Label 3 │
  716. │Label 4 │
  717. │Label 5 │
  718. │Label 6 │
  719. │Label 7 │
  720. │Label 8 │
  721. │Label 9 │
  722. │Label 10 │
  723. │Label 11 │
  724. │Label 12 │
  725. │Label 13 │
  726. │Label 13 │
  727. └────────────────────┘",
  728. @"
  729. ┌────────────────────┐
  730. │View with long text │
  731. │Label 0 │
  732. │Label 1 │
  733. │Label 2 │
  734. │Label 3 │
  735. │Label 4 │
  736. │Label 5 │
  737. │Label 6 │
  738. │Label 7 │
  739. │Label 8 │
  740. │Label 9 │
  741. │Label 10 │
  742. │Label 11 │
  743. │Label 12 │
  744. │Label 13 │
  745. │Label 14 │
  746. │Label 14 │
  747. └────────────────────┘",
  748. @"
  749. ┌────────────────────┐
  750. │View with long text │
  751. │Label 0 │
  752. │Label 1 │
  753. │Label 2 │
  754. │Label 3 │
  755. │Label 4 │
  756. │Label 5 │
  757. │Label 6 │
  758. │Label 7 │
  759. │Label 8 │
  760. │Label 9 │
  761. │Label 10 │
  762. │Label 11 │
  763. │Label 12 │
  764. │Label 13 │
  765. │Label 14 │
  766. │Label 15 │
  767. │Label 15 │
  768. └────────────────────┘",
  769. @"
  770. ┌────────────────────┐
  771. │View with long text │
  772. │Label 0 │
  773. │Label 1 │
  774. │Label 2 │
  775. │Label 3 │
  776. │Label 4 │
  777. │Label 5 │
  778. │Label 6 │
  779. │Label 7 │
  780. │Label 8 │
  781. │Label 9 │
  782. │Label 10 │
  783. │Label 11 │
  784. │Label 12 │
  785. │Label 13 │
  786. │Label 14 │
  787. │Label 15 │
  788. │Label 16 │
  789. │Label 16 │
  790. └────────────────────┘",
  791. @"
  792. ┌────────────────────┐
  793. │View with long text │
  794. │Label 0 │
  795. │Label 1 │
  796. │Label 2 │
  797. │Label 3 │
  798. │Label 4 │
  799. │Label 5 │
  800. │Label 6 │
  801. │Label 7 │
  802. │Label 8 │
  803. │Label 9 │
  804. │Label 10 │
  805. │Label 11 │
  806. │Label 12 │
  807. │Label 13 │
  808. │Label 14 │
  809. │Label 15 │
  810. │Label 16 │
  811. │Label 17 │
  812. │Label 17 │
  813. └────────────────────┘",
  814. @"
  815. ┌────────────────────┐
  816. │View with long text │
  817. │Label 0 │
  818. │Label 1 │
  819. │Label 2 │
  820. │Label 3 │
  821. │Label 4 │
  822. │Label 5 │
  823. │Label 6 │
  824. │Label 7 │
  825. │Label 8 │
  826. │Label 9 │
  827. │Label 10 │
  828. │Label 11 │
  829. │Label 12 │
  830. │Label 13 │
  831. │Label 14 │
  832. │Label 15 │
  833. │Label 16 │
  834. │Label 17 │
  835. │Label 18 │
  836. │Label 18 │
  837. └────────────────────┘",
  838. @"
  839. ┌────────────────────┐
  840. │View with long text │
  841. │Label 0 │
  842. │Label 1 │
  843. │Label 2 │
  844. │Label 3 │
  845. │Label 4 │
  846. │Label 5 │
  847. │Label 6 │
  848. │Label 7 │
  849. │Label 8 │
  850. │Label 9 │
  851. │Label 10 │
  852. │Label 11 │
  853. │Label 12 │
  854. │Label 13 │
  855. │Label 14 │
  856. │Label 15 │
  857. │Label 16 │
  858. │Label 17 │
  859. │Label 18 │
  860. │Label 19 │
  861. │Label 19 │
  862. └────────────────────┘",
  863. };
  864. [Fact]
  865. public void Dim_Add_Operator_With_Text ()
  866. {
  867. Application.Init (new FakeDriver ());
  868. var top = Application.Top;
  869. // Although view height is zero the text it's draw due the SetMinWidthHeight method
  870. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 0 };
  871. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  872. var count = 0;
  873. var listLabels = new List<Label> ();
  874. field.KeyDown += (k) => {
  875. if (k.KeyEvent.Key == Key.Enter) {
  876. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  877. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  878. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  879. if (count < 20) {
  880. field.Text = $"Label {count}";
  881. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  882. view.Add (label);
  883. Assert.Equal ($"Label {count}", label.Text);
  884. Assert.Equal ($"Pos.Absolute({count + 1})", label.Y.ToString ());
  885. listLabels.Add (label);
  886. if (count == 0) {
  887. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  888. view.Height += 2;
  889. } else {
  890. Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
  891. view.Height += 1;
  892. }
  893. count++;
  894. }
  895. Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
  896. }
  897. };
  898. Application.Iteration += () => {
  899. while (count < 21) {
  900. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  901. if (count == 20) {
  902. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  903. break;
  904. }
  905. }
  906. Application.RequestStop ();
  907. };
  908. var win = new Window ();
  909. win.Add (view);
  910. win.Add (field);
  911. top.Add (win);
  912. Application.Run (top);
  913. Assert.Equal (20, count);
  914. Assert.Equal (count, listLabels.Count);
  915. // Shutdown must be called to safely clean up Application if Init has been called
  916. Application.Shutdown ();
  917. }
  918. [Fact]
  919. public void Dim_Subtract_Operator ()
  920. {
  921. Application.Init (new FakeDriver ());
  922. var top = Application.Top;
  923. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  924. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  925. var count = 20;
  926. var listLabels = new List<Label> ();
  927. for (int i = 0; i < count; i++) {
  928. field.Text = $"Label {i}";
  929. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  930. view.Add (label);
  931. Assert.Equal ($"Label {i}", label.Text);
  932. Assert.Equal ($"Pos.Absolute({i})", label.Y.ToString ());
  933. listLabels.Add (label);
  934. Assert.Equal ($"Dim.Absolute({i})", view.Height.ToString ());
  935. view.Height += 1;
  936. Assert.Equal ($"Dim.Absolute({i + 1})", view.Height.ToString ());
  937. }
  938. field.KeyDown += (k) => {
  939. if (k.KeyEvent.Key == Key.Enter) {
  940. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  941. view.Remove (listLabels [count - 1]);
  942. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  943. view.Height -= 1;
  944. count--;
  945. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  946. }
  947. };
  948. Application.Iteration += () => {
  949. while (count > 0) {
  950. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  951. }
  952. Application.RequestStop ();
  953. };
  954. var win = new Window ();
  955. win.Add (view);
  956. win.Add (field);
  957. top.Add (win);
  958. Application.Run (top);
  959. Assert.Equal (0, count);
  960. // Shutdown must be called to safely clean up Application if Init has been called
  961. Application.Shutdown ();
  962. }
  963. [Fact]
  964. public void Dim_Subtract_Operator_With_Text ()
  965. {
  966. Application.Init (new FakeDriver ());
  967. var top = Application.Top;
  968. // Although view height is zero the text it's draw due the SetMinWidthHeight method
  969. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 0 };
  970. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  971. var count = 20;
  972. var listLabels = new List<Label> ();
  973. for (int i = 0; i < count; i++) {
  974. field.Text = $"Label {i}";
  975. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  976. view.Add (label);
  977. Assert.Equal ($"Label {i}", label.Text);
  978. Assert.Equal ($"Pos.Absolute({i + 1})", label.Y.ToString ());
  979. listLabels.Add (label);
  980. if (i == 0) {
  981. Assert.Equal ($"Dim.Absolute({i})", view.Height.ToString ());
  982. view.Height += 2;
  983. Assert.Equal ($"Dim.Absolute({i + 2})", view.Height.ToString ());
  984. } else {
  985. Assert.Equal ($"Dim.Absolute({i + 1})", view.Height.ToString ());
  986. view.Height += 1;
  987. Assert.Equal ($"Dim.Absolute({i + 2})", view.Height.ToString ());
  988. }
  989. }
  990. field.KeyDown += (k) => {
  991. if (k.KeyEvent.Key == Key.Enter) {
  992. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  993. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  994. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  995. if (count > 0) {
  996. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  997. view.Remove (listLabels [count - 1]);
  998. listLabels.RemoveAt (count - 1);
  999. Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
  1000. view.Height -= 1;
  1001. count--;
  1002. if (listLabels.Count > 0)
  1003. field.Text = listLabels [count - 1].Text;
  1004. else
  1005. field.Text = NStack.ustring.Empty;
  1006. }
  1007. Assert.Equal ($"Dim.Absolute({count + 1})", view.Height.ToString ());
  1008. }
  1009. };
  1010. Application.Iteration += () => {
  1011. while (count > -1) {
  1012. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1013. if (count == 0) {
  1014. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1015. break;
  1016. }
  1017. }
  1018. Application.RequestStop ();
  1019. };
  1020. var win = new Window ();
  1021. win.Add (view);
  1022. win.Add (field);
  1023. top.Add (win);
  1024. Application.Run (top);
  1025. Assert.Equal (0, count);
  1026. Assert.Equal (count, listLabels.Count);
  1027. // Shutdown must be called to safely clean up Application if Init has been called
  1028. Application.Shutdown ();
  1029. }
  1030. [Fact]
  1031. public void Internal_Tests ()
  1032. {
  1033. var dimFactor = new Dim.DimFactor (0.10F);
  1034. Assert.Equal (10, dimFactor.Anchor (100));
  1035. var dimAbsolute = new Dim.DimAbsolute (10);
  1036. Assert.Equal (10, dimAbsolute.Anchor (0));
  1037. var dimFill = new Dim.DimFill (1);
  1038. Assert.Equal (99, dimFill.Anchor (100));
  1039. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  1040. Assert.Equal (dimCombine.left, dimFactor);
  1041. Assert.Equal (dimCombine.right, dimAbsolute);
  1042. Assert.Equal (20, dimCombine.Anchor (100));
  1043. var view = new View (new Rect (20, 10, 20, 1));
  1044. var dimViewHeight = new Dim.DimView (view, 0);
  1045. Assert.Equal (1, dimViewHeight.Anchor (0));
  1046. var dimViewWidth = new Dim.DimView (view, 1);
  1047. Assert.Equal (20, dimViewWidth.Anchor (0));
  1048. }
  1049. [Fact]
  1050. public void Function_SetsValue ()
  1051. {
  1052. var text = "Test";
  1053. var dim = Dim.Function (() => text.Length);
  1054. Assert.Equal ("Dim.DimFunc(4)", dim.ToString ());
  1055. text = "New Test";
  1056. Assert.Equal ("Dim.DimFunc(8)", dim.ToString ());
  1057. text = "";
  1058. Assert.Equal ("Dim.DimFunc(0)", dim.ToString ());
  1059. }
  1060. [Fact]
  1061. public void Function_Equal ()
  1062. {
  1063. var f1 = () => 0;
  1064. var f2 = () => 0;
  1065. var dim1 = Dim.Function (f1);
  1066. var dim2 = Dim.Function (f2);
  1067. Assert.Equal (dim1, dim2);
  1068. f2 = () => 1;
  1069. dim2 = Dim.Function (f2);
  1070. Assert.NotEqual (dim1, dim2);
  1071. }
  1072. [Theory, AutoInitShutdown]
  1073. [InlineData (0, true)]
  1074. [InlineData (0, false)]
  1075. [InlineData (50, true)]
  1076. [InlineData (50, false)]
  1077. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1078. {
  1079. var container = new View {
  1080. Width = 100,
  1081. Height = 100,
  1082. };
  1083. var label = new Label {
  1084. X = testHorizontal ? startingDistance : 0,
  1085. Y = testHorizontal ? 0 : startingDistance,
  1086. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1087. Height = testHorizontal ? 1 : Dim.Percent (50) + 1,
  1088. };
  1089. container.Add (label);
  1090. Application.Top.Add (container);
  1091. Application.Top.LayoutSubviews ();
  1092. Assert.Equal (100, container.Frame.Width);
  1093. Assert.Equal (100, container.Frame.Height);
  1094. if (testHorizontal) {
  1095. Assert.Equal (51, label.Frame.Width);
  1096. Assert.Equal (1, label.Frame.Height);
  1097. } else {
  1098. Assert.Equal (1, label.Frame.Width);
  1099. Assert.Equal (51, label.Frame.Height);
  1100. }
  1101. }
  1102. }
  1103. }