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_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 supported!
  480. // BUGBUG: v2 - f references v2 here; v2 is f's subview. This is not supported!
  481. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  482. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  483. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  484. Assert.Equal (80, t.Frame.Width);
  485. Assert.Equal (25, t.Frame.Height);
  486. Assert.Equal (78, w.Frame.Width);
  487. Assert.Equal (23, w.Frame.Height);
  488. // BUGBUG: v2 - this no longer works - see above
  489. //Assert.Equal (6, f.Frame.Width);
  490. //Assert.Equal (6, f.Frame.Height);
  491. //Assert.Equal (76, v1.Frame.Width);
  492. //Assert.Equal (21, v1.Frame.Height);
  493. //Assert.Equal (74, v2.Frame.Width);
  494. //Assert.Equal (19, v2.Frame.Height);
  495. }
  496. [Fact]
  497. public void PosCombine_View_Not_Added_Throws ()
  498. {
  499. var t = new View ("t") { Width = 80, Height = 50 };
  500. // BUGBUG: v2 - super should not reference it's superview (t)
  501. var super = new View ("super") {
  502. Width = Dim.Width (t) - 2,
  503. Height = Dim.Height (t) - 2
  504. };
  505. t.Add (super);
  506. var sub = new View ("sub");
  507. super.Add (sub);
  508. var v1 = new View ("v1") {
  509. Width = Dim.Width (super) - 2,
  510. Height = Dim.Height (super) - 2
  511. };
  512. var v2 = new View ("v2") {
  513. Width = Dim.Width (v1) - 2,
  514. Height = Dim.Height (v1) - 2
  515. };
  516. sub.Add (v1);
  517. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  518. sub.Width = Dim.Fill () - Dim.Width (v2);
  519. sub.Height = Dim.Fill () - Dim.Height (v2);
  520. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  521. }
  522. [Fact, AutoInitShutdown]
  523. public void Dim_Add_Operator ()
  524. {
  525. var top = Application.Top;
  526. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  527. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  528. var count = 0;
  529. field.KeyDown += (s, k) => {
  530. if (k.KeyEvent.Key == Key.Enter) {
  531. field.Text = $"Label {count}";
  532. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  533. view.Add (label);
  534. Assert.Equal ($"Label {count}", label.Text);
  535. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  536. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  537. view.Height += 1;
  538. count++;
  539. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  540. }
  541. };
  542. Application.Iteration += () => {
  543. while (count < 20) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  544. Application.RequestStop ();
  545. };
  546. var win = new Window ();
  547. win.Add (view);
  548. win.Add (field);
  549. top.Add (win);
  550. Application.Run (top);
  551. Assert.Equal (20, count);
  552. }
  553. private string [] expecteds = new string [21] {
  554. @"
  555. ┌────────────────────┐
  556. │View with long text │
  557. │ │
  558. └────────────────────┘",
  559. @"
  560. ┌────────────────────┐
  561. │View with long text │
  562. │Label 0 │
  563. │Label 0 │
  564. └────────────────────┘",
  565. @"
  566. ┌────────────────────┐
  567. │View with long text │
  568. │Label 0 │
  569. │Label 1 │
  570. │Label 1 │
  571. └────────────────────┘",
  572. @"
  573. ┌────────────────────┐
  574. │View with long text │
  575. │Label 0 │
  576. │Label 1 │
  577. │Label 2 │
  578. │Label 2 │
  579. └────────────────────┘",
  580. @"
  581. ┌────────────────────┐
  582. │View with long text │
  583. │Label 0 │
  584. │Label 1 │
  585. │Label 2 │
  586. │Label 3 │
  587. │Label 3 │
  588. └────────────────────┘",
  589. @"
  590. ┌────────────────────┐
  591. │View with long text │
  592. │Label 0 │
  593. │Label 1 │
  594. │Label 2 │
  595. │Label 3 │
  596. │Label 4 │
  597. │Label 4 │
  598. └────────────────────┘",
  599. @"
  600. ┌────────────────────┐
  601. │View with long text │
  602. │Label 0 │
  603. │Label 1 │
  604. │Label 2 │
  605. │Label 3 │
  606. │Label 4 │
  607. │Label 5 │
  608. │Label 5 │
  609. └────────────────────┘",
  610. @"
  611. ┌────────────────────┐
  612. │View with long text │
  613. │Label 0 │
  614. │Label 1 │
  615. │Label 2 │
  616. │Label 3 │
  617. │Label 4 │
  618. │Label 5 │
  619. │Label 6 │
  620. │Label 6 │
  621. └────────────────────┘",
  622. @"
  623. ┌────────────────────┐
  624. │View with long text │
  625. │Label 0 │
  626. │Label 1 │
  627. │Label 2 │
  628. │Label 3 │
  629. │Label 4 │
  630. │Label 5 │
  631. │Label 6 │
  632. │Label 7 │
  633. │Label 7 │
  634. └────────────────────┘",
  635. @"
  636. ┌────────────────────┐
  637. │View with long text │
  638. │Label 0 │
  639. │Label 1 │
  640. │Label 2 │
  641. │Label 3 │
  642. │Label 4 │
  643. │Label 5 │
  644. │Label 6 │
  645. │Label 7 │
  646. │Label 8 │
  647. │Label 8 │
  648. └────────────────────┘",
  649. @"
  650. ┌────────────────────┐
  651. │View with long text │
  652. │Label 0 │
  653. │Label 1 │
  654. │Label 2 │
  655. │Label 3 │
  656. │Label 4 │
  657. │Label 5 │
  658. │Label 6 │
  659. │Label 7 │
  660. │Label 8 │
  661. │Label 9 │
  662. │Label 9 │
  663. └────────────────────┘",
  664. @"
  665. ┌────────────────────┐
  666. │View with long text │
  667. │Label 0 │
  668. │Label 1 │
  669. │Label 2 │
  670. │Label 3 │
  671. │Label 4 │
  672. │Label 5 │
  673. │Label 6 │
  674. │Label 7 │
  675. │Label 8 │
  676. │Label 9 │
  677. │Label 10 │
  678. │Label 10 │
  679. └────────────────────┘",
  680. @"
  681. ┌────────────────────┐
  682. │View with long text │
  683. │Label 0 │
  684. │Label 1 │
  685. │Label 2 │
  686. │Label 3 │
  687. │Label 4 │
  688. │Label 5 │
  689. │Label 6 │
  690. │Label 7 │
  691. │Label 8 │
  692. │Label 9 │
  693. │Label 10 │
  694. │Label 11 │
  695. │Label 11 │
  696. └────────────────────┘",
  697. @"
  698. ┌────────────────────┐
  699. │View with long text │
  700. │Label 0 │
  701. │Label 1 │
  702. │Label 2 │
  703. │Label 3 │
  704. │Label 4 │
  705. │Label 5 │
  706. │Label 6 │
  707. │Label 7 │
  708. │Label 8 │
  709. │Label 9 │
  710. │Label 10 │
  711. │Label 11 │
  712. │Label 12 │
  713. │Label 12 │
  714. └────────────────────┘",
  715. @"
  716. ┌────────────────────┐
  717. │View with long text │
  718. │Label 0 │
  719. │Label 1 │
  720. │Label 2 │
  721. │Label 3 │
  722. │Label 4 │
  723. │Label 5 │
  724. │Label 6 │
  725. │Label 7 │
  726. │Label 8 │
  727. │Label 9 │
  728. │Label 10 │
  729. │Label 11 │
  730. │Label 12 │
  731. │Label 13 │
  732. │Label 13 │
  733. └────────────────────┘",
  734. @"
  735. ┌────────────────────┐
  736. │View with long text │
  737. │Label 0 │
  738. │Label 1 │
  739. │Label 2 │
  740. │Label 3 │
  741. │Label 4 │
  742. │Label 5 │
  743. │Label 6 │
  744. │Label 7 │
  745. │Label 8 │
  746. │Label 9 │
  747. │Label 10 │
  748. │Label 11 │
  749. │Label 12 │
  750. │Label 13 │
  751. │Label 14 │
  752. │Label 14 │
  753. └────────────────────┘",
  754. @"
  755. ┌────────────────────┐
  756. │View with long text │
  757. │Label 0 │
  758. │Label 1 │
  759. │Label 2 │
  760. │Label 3 │
  761. │Label 4 │
  762. │Label 5 │
  763. │Label 6 │
  764. │Label 7 │
  765. │Label 8 │
  766. │Label 9 │
  767. │Label 10 │
  768. │Label 11 │
  769. │Label 12 │
  770. │Label 13 │
  771. │Label 14 │
  772. │Label 15 │
  773. │Label 15 │
  774. └────────────────────┘",
  775. @"
  776. ┌────────────────────┐
  777. │View with long text │
  778. │Label 0 │
  779. │Label 1 │
  780. │Label 2 │
  781. │Label 3 │
  782. │Label 4 │
  783. │Label 5 │
  784. │Label 6 │
  785. │Label 7 │
  786. │Label 8 │
  787. │Label 9 │
  788. │Label 10 │
  789. │Label 11 │
  790. │Label 12 │
  791. │Label 13 │
  792. │Label 14 │
  793. │Label 15 │
  794. │Label 16 │
  795. │Label 16 │
  796. └────────────────────┘",
  797. @"
  798. ┌────────────────────┐
  799. │View with long text │
  800. │Label 0 │
  801. │Label 1 │
  802. │Label 2 │
  803. │Label 3 │
  804. │Label 4 │
  805. │Label 5 │
  806. │Label 6 │
  807. │Label 7 │
  808. │Label 8 │
  809. │Label 9 │
  810. │Label 10 │
  811. │Label 11 │
  812. │Label 12 │
  813. │Label 13 │
  814. │Label 14 │
  815. │Label 15 │
  816. │Label 16 │
  817. │Label 17 │
  818. │Label 17 │
  819. └────────────────────┘",
  820. @"
  821. ┌────────────────────┐
  822. │View with long text │
  823. │Label 0 │
  824. │Label 1 │
  825. │Label 2 │
  826. │Label 3 │
  827. │Label 4 │
  828. │Label 5 │
  829. │Label 6 │
  830. │Label 7 │
  831. │Label 8 │
  832. │Label 9 │
  833. │Label 10 │
  834. │Label 11 │
  835. │Label 12 │
  836. │Label 13 │
  837. │Label 14 │
  838. │Label 15 │
  839. │Label 16 │
  840. │Label 17 │
  841. │Label 18 │
  842. │Label 18 │
  843. └────────────────────┘",
  844. @"
  845. ┌────────────────────┐
  846. │View with long text │
  847. │Label 0 │
  848. │Label 1 │
  849. │Label 2 │
  850. │Label 3 │
  851. │Label 4 │
  852. │Label 5 │
  853. │Label 6 │
  854. │Label 7 │
  855. │Label 8 │
  856. │Label 9 │
  857. │Label 10 │
  858. │Label 11 │
  859. │Label 12 │
  860. │Label 13 │
  861. │Label 14 │
  862. │Label 15 │
  863. │Label 16 │
  864. │Label 17 │
  865. │Label 18 │
  866. │Label 19 │
  867. │Label 19 │
  868. └────────────────────┘",
  869. };
  870. [Fact, AutoInitShutdown]
  871. public void Dim_Add_Operator_With_Text ()
  872. {
  873. var top = Application.Top;
  874. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  875. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  876. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  877. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  878. var count = 0;
  879. var listLabels = new List<Label> ();
  880. field.KeyDown += (s, k) => {
  881. if (k.KeyEvent.Key == Key.Enter) {
  882. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  883. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  884. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  885. if (count < 20) {
  886. field.Text = $"Label {count}";
  887. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  888. view.Add (label);
  889. Assert.Equal ($"Label {count}", label.Text);
  890. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  891. listLabels.Add (label);
  892. //if (count == 0) {
  893. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  894. // view.Height += 2;
  895. //} else {
  896. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  897. view.Height += 1;
  898. //}
  899. count++;
  900. }
  901. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  902. }
  903. };
  904. Application.Iteration += () => {
  905. while (count < 21) {
  906. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  907. if (count == 20) {
  908. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  909. break;
  910. }
  911. }
  912. Application.RequestStop ();
  913. };
  914. var win = new Window ();
  915. win.Add (view);
  916. win.Add (field);
  917. top.Add (win);
  918. Application.Run (top);
  919. Assert.Equal (20, count);
  920. Assert.Equal (count, listLabels.Count);
  921. }
  922. [Fact, AutoInitShutdown]
  923. public void Dim_Subtract_Operator ()
  924. {
  925. var top = Application.Top;
  926. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  927. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  928. var count = 20;
  929. var listLabels = new List<Label> ();
  930. for (int i = 0; i < count; i++) {
  931. field.Text = $"Label {i}";
  932. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  933. view.Add (label);
  934. Assert.Equal ($"Label {i}", label.Text);
  935. // BUGBUG: Bogus test; views have not been initialized yet
  936. //Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  937. listLabels.Add (label);
  938. // BUGBUG: Bogus test; views have not been initialized yet
  939. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  940. view.Height += 1;
  941. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  942. }
  943. field.KeyDown += (s, k) => {
  944. if (k.KeyEvent.Key == Key.Enter) {
  945. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  946. view.Remove (listLabels [count - 1]);
  947. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  948. view.Height -= 1;
  949. count--;
  950. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  951. }
  952. };
  953. Application.Iteration += () => {
  954. while (count > 0) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  955. Application.RequestStop ();
  956. };
  957. var win = new Window ();
  958. win.Add (view);
  959. win.Add (field);
  960. top.Add (win);
  961. Application.Run (top);
  962. Assert.Equal (0, count);
  963. }
  964. [Fact, AutoInitShutdown]
  965. public void Dim_Subtract_Operator_With_Text ()
  966. {
  967. var top = Application.Top;
  968. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  969. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  970. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  971. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  972. var count = 20;
  973. var listLabels = new List<Label> ();
  974. for (int i = 0; i < count; i++) {
  975. field.Text = $"Label {i}";
  976. // BUGBUG: v2 - view has not been initialied yet; view.Bounds is indeterminate
  977. var label = new Label (field.Text) { X = 0, Y = i + 1, Width = 10 };
  978. view.Add (label);
  979. Assert.Equal ($"Label {i}", label.Text);
  980. // BUGBUG: Bogus test; views have not been initialized yet
  981. //Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  982. listLabels.Add (label);
  983. //if (i == 0) {
  984. // BUGBUG: Bogus test; views have not been initialized yet
  985. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  986. //view.Height += 2;
  987. // BUGBUG: Bogus test; views have not been initialized yet
  988. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  989. //} else {
  990. // BUGBUG: Bogus test; views have not been initialized yet
  991. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  992. view.Height += 1;
  993. // BUGBUG: Bogus test; views have not been initialized yet
  994. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  995. //}
  996. }
  997. field.KeyDown += (s, k) => {
  998. if (k.KeyEvent.Key == Key.Enter) {
  999. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1000. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  1001. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1002. if (count > 0) {
  1003. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  1004. view.Remove (listLabels [count - 1]);
  1005. listLabels.RemoveAt (count - 1);
  1006. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1007. view.Height -= 1;
  1008. count--;
  1009. if (listLabels.Count > 0)
  1010. field.Text = listLabels [count - 1].Text;
  1011. else
  1012. field.Text = NStack.ustring.Empty;
  1013. }
  1014. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1015. }
  1016. };
  1017. Application.Iteration += () => {
  1018. while (count > -1) {
  1019. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1020. if (count == 0) {
  1021. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1022. break;
  1023. }
  1024. }
  1025. Application.RequestStop ();
  1026. };
  1027. var win = new Window ();
  1028. win.Add (view);
  1029. win.Add (field);
  1030. top.Add (win);
  1031. Application.Run (top);
  1032. Assert.Equal (0, count);
  1033. Assert.Equal (count, listLabels.Count);
  1034. }
  1035. [Fact]
  1036. public void Internal_Tests ()
  1037. {
  1038. var dimFactor = new Dim.DimFactor (0.10F);
  1039. Assert.Equal (10, dimFactor.Anchor (100));
  1040. var dimAbsolute = new Dim.DimAbsolute (10);
  1041. Assert.Equal (10, dimAbsolute.Anchor (0));
  1042. var dimFill = new Dim.DimFill (1);
  1043. Assert.Equal (99, dimFill.Anchor (100));
  1044. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  1045. Assert.Equal (dimCombine.left, dimFactor);
  1046. Assert.Equal (dimCombine.right, dimAbsolute);
  1047. Assert.Equal (20, dimCombine.Anchor (100));
  1048. var view = new View (new Rect (20, 10, 20, 1));
  1049. var dimViewHeight = new Dim.DimView (view, 0);
  1050. Assert.Equal (1, dimViewHeight.Anchor (0));
  1051. var dimViewWidth = new Dim.DimView (view, 1);
  1052. Assert.Equal (20, dimViewWidth.Anchor (0));
  1053. }
  1054. [Fact]
  1055. public void Function_SetsValue ()
  1056. {
  1057. var text = "Test";
  1058. var dim = Dim.Function (() => text.Length);
  1059. Assert.Equal ("DimFunc(4)", dim.ToString ());
  1060. text = "New Test";
  1061. Assert.Equal ("DimFunc(8)", dim.ToString ());
  1062. text = "";
  1063. Assert.Equal ("DimFunc(0)", dim.ToString ());
  1064. }
  1065. [Fact]
  1066. public void Function_Equal ()
  1067. {
  1068. var f1 = () => 0;
  1069. var f2 = () => 0;
  1070. var dim1 = Dim.Function (f1);
  1071. var dim2 = Dim.Function (f2);
  1072. Assert.Equal (dim1, dim2);
  1073. f2 = () => 1;
  1074. dim2 = Dim.Function (f2);
  1075. Assert.NotEqual (dim1, dim2);
  1076. }
  1077. [Theory, AutoInitShutdown]
  1078. [InlineData (0, true)]
  1079. [InlineData (0, false)]
  1080. [InlineData (50, true)]
  1081. [InlineData (50, false)]
  1082. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1083. {
  1084. var container = new View {
  1085. Width = 100,
  1086. Height = 100,
  1087. };
  1088. var label = new Label {
  1089. X = testHorizontal ? startingDistance : 0,
  1090. Y = testHorizontal ? 0 : startingDistance,
  1091. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1092. Height = testHorizontal ? 1 : Dim.Percent (50) + 1,
  1093. };
  1094. container.Add (label);
  1095. Application.Top.Add (container);
  1096. Application.Top.LayoutSubviews ();
  1097. Assert.Equal (100, container.Frame.Width);
  1098. Assert.Equal (100, container.Frame.Height);
  1099. if (testHorizontal) {
  1100. Assert.Equal (51, label.Frame.Width);
  1101. Assert.Equal (1, label.Frame.Height);
  1102. } else {
  1103. Assert.Equal (1, label.Frame.Width);
  1104. Assert.Equal (51, label.Frame.Height);
  1105. }
  1106. }
  1107. [Fact]
  1108. public void Dim_Referencing_SuperView_Does_Not_Throws ()
  1109. {
  1110. var super = new View ("super") {
  1111. Width = 10,
  1112. Height = 10
  1113. };
  1114. var view = new View ("view") {
  1115. Width = Dim.Width (super), // this is allowed
  1116. Height = Dim.Height (super), // this is allowed
  1117. };
  1118. super.Add (view);
  1119. super.BeginInit ();
  1120. super.EndInit ();
  1121. var exception = Record.Exception (super.LayoutSubviews);
  1122. Assert.Null (exception);
  1123. }
  1124. [Fact]
  1125. public void Dim_SyperView_Referencing_SubView_Throws ()
  1126. {
  1127. var super = new View ("super") {
  1128. Width = 10,
  1129. Height = 10
  1130. };
  1131. var view2 = new View ("view2") {
  1132. Width = 10,
  1133. Height = 10,
  1134. };
  1135. var view = new View ("view") {
  1136. Width = Dim.Width (view2), // this is not allowed
  1137. Height = Dim.Height (view2), // this is not allowed
  1138. };
  1139. view.Add (view2);
  1140. super.Add (view);
  1141. super.BeginInit ();
  1142. super.EndInit ();
  1143. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  1144. }
  1145. }
  1146. }