DimTests.cs 39 KB

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