DimTests.cs 38 KB

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