DimTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Threading;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.ViewTests;
  11. public class DimTests {
  12. readonly ITestOutputHelper _output;
  13. public DimTests (ITestOutputHelper output)
  14. {
  15. _output = output;
  16. Console.OutputEncoding = Encoding.Default;
  17. // Change current culture
  18. var culture = CultureInfo.CreateSpecificCulture ("en-US");
  19. Thread.CurrentThread.CurrentCulture = culture;
  20. Thread.CurrentThread.CurrentUICulture = culture;
  21. }
  22. [Fact]
  23. public void New_Works ()
  24. {
  25. var dim = new Dim ();
  26. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  27. }
  28. [Fact]
  29. public void Sized_SetsValue ()
  30. {
  31. var dim = Dim.Sized (0);
  32. Assert.Equal ("Absolute(0)", dim.ToString ());
  33. var testVal = 5;
  34. dim = Dim.Sized (testVal);
  35. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  36. testVal = -1;
  37. dim = Dim.Sized (testVal);
  38. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  39. }
  40. [Fact]
  41. public void Sized_Equals ()
  42. {
  43. var n1 = 0;
  44. var n2 = 0;
  45. var dim1 = Dim.Sized (n1);
  46. var dim2 = Dim.Sized (n2);
  47. Assert.Equal (dim1, dim2);
  48. n1 = n2 = 1;
  49. dim1 = Dim.Sized (n1);
  50. dim2 = Dim.Sized (n2);
  51. Assert.Equal (dim1, dim2);
  52. n1 = n2 = -1;
  53. dim1 = Dim.Sized (n1);
  54. dim2 = Dim.Sized (n2);
  55. Assert.Equal (dim1, dim2);
  56. n1 = 0;
  57. n2 = 1;
  58. dim1 = Dim.Sized (n1);
  59. dim2 = Dim.Sized (n2);
  60. Assert.NotEqual (dim1, dim2);
  61. }
  62. [Fact]
  63. public void Width_Set_To_Null_Throws ()
  64. {
  65. var dim = Dim.Width (null);
  66. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  67. }
  68. [Fact]
  69. [TestRespondersDisposed]
  70. public void SetsValue ()
  71. {
  72. var testVal = Rect.Empty;
  73. var testValView = new View (testVal);
  74. var dim = Dim.Width (testValView);
  75. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  76. testValView.Dispose ();
  77. testVal = new Rect (1, 2, 3, 4);
  78. testValView = new View (testVal);
  79. dim = Dim.Width (testValView);
  80. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  81. testValView.Dispose ();
  82. }
  83. [Fact]
  84. [TestRespondersDisposed]
  85. public void Width_Equals ()
  86. {
  87. var testRect1 = Rect.Empty;
  88. var view1 = new View (testRect1);
  89. var testRect2 = Rect.Empty;
  90. var view2 = new View (testRect2);
  91. var dim1 = Dim.Width (view1);
  92. var dim2 = Dim.Width (view1);
  93. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  94. Assert.Equal (dim1, dim2);
  95. dim2 = Dim.Width (view2);
  96. Assert.NotEqual (dim1, dim2);
  97. testRect1 = new Rect (0, 1, 2, 3);
  98. view1 = new View (testRect1);
  99. testRect2 = new Rect (0, 1, 2, 3);
  100. dim1 = Dim.Width (view1);
  101. dim2 = Dim.Width (view1);
  102. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  103. Assert.Equal (dim1, dim2);
  104. testRect1 = new Rect (0, -1, 2, 3);
  105. view1 = new View (testRect1);
  106. testRect2 = new Rect (0, -1, 2, 3);
  107. dim1 = Dim.Width (view1);
  108. dim2 = Dim.Width (view1);
  109. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  110. Assert.Equal (dim1, dim2);
  111. testRect1 = new Rect (0, -1, 2, 3);
  112. view1 = new View (testRect1);
  113. testRect2 = Rect.Empty;
  114. view2 = new View (testRect2);
  115. dim1 = Dim.Width (view1);
  116. dim2 = Dim.Width (view2);
  117. Assert.NotEqual (dim1, dim2);
  118. #if DEBUG_IDISPOSABLE
  119. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  120. Responder.Instances.Clear ();
  121. Assert.Empty (Responder.Instances);
  122. #endif
  123. }
  124. [Fact]
  125. public void Height_Set_To_Null_Throws ()
  126. {
  127. var dim = Dim.Height (null);
  128. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  129. }
  130. [Fact]
  131. [TestRespondersDisposed]
  132. public void Height_SetsValue ()
  133. {
  134. var testVal = Rect.Empty;
  135. var testValview = new View (testVal);
  136. var dim = Dim.Height (testValview);
  137. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  138. testValview.Dispose ();
  139. testVal = new Rect (1, 2, 3, 4);
  140. testValview = new View (testVal);
  141. dim = Dim.Height (testValview);
  142. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  143. testValview.Dispose ();
  144. }
  145. // TODO: Other Dim.Height tests (e.g. Equal?)
  146. [Fact]
  147. public void Fill_SetsValue ()
  148. {
  149. var testMargin = 0;
  150. var dim = Dim.Fill ();
  151. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  152. testMargin = 0;
  153. dim = Dim.Fill (testMargin);
  154. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  155. testMargin = 5;
  156. dim = Dim.Fill (testMargin);
  157. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  158. }
  159. [Fact]
  160. public void Fill_Equal ()
  161. {
  162. var margin1 = 0;
  163. var margin2 = 0;
  164. var dim1 = Dim.Fill (margin1);
  165. var dim2 = Dim.Fill (margin2);
  166. Assert.Equal (dim1, dim2);
  167. }
  168. [Fact]
  169. public void Percent_SetsValue ()
  170. {
  171. float f = 0;
  172. var dim = Dim.Percent (f);
  173. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  174. f = 0.5F;
  175. dim = Dim.Percent (f);
  176. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  177. f = 100;
  178. dim = Dim.Percent (f);
  179. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  180. }
  181. [Fact]
  182. public void Percent_Equals ()
  183. {
  184. float n1 = 0;
  185. float n2 = 0;
  186. var dim1 = Dim.Percent (n1);
  187. var dim2 = Dim.Percent (n2);
  188. Assert.Equal (dim1, dim2);
  189. n1 = n2 = 1;
  190. dim1 = Dim.Percent (n1);
  191. dim2 = Dim.Percent (n2);
  192. Assert.Equal (dim1, dim2);
  193. n1 = n2 = 0.5f;
  194. dim1 = Dim.Percent (n1);
  195. dim2 = Dim.Percent (n2);
  196. Assert.Equal (dim1, dim2);
  197. n1 = n2 = 100f;
  198. dim1 = Dim.Percent (n1);
  199. dim2 = Dim.Percent (n2);
  200. Assert.Equal (dim1, dim2);
  201. n1 = n2 = 0.3f;
  202. dim1 = Dim.Percent (n1, true);
  203. dim2 = Dim.Percent (n2, true);
  204. Assert.Equal (dim1, dim2);
  205. n1 = n2 = 0.3f;
  206. dim1 = Dim.Percent (n1);
  207. dim2 = Dim.Percent (n2, true);
  208. Assert.NotEqual (dim1, dim2);
  209. n1 = 0;
  210. n2 = 1;
  211. dim1 = Dim.Percent (n1);
  212. dim2 = Dim.Percent (n2);
  213. Assert.NotEqual (dim1, dim2);
  214. n1 = 0.5f;
  215. n2 = 1.5f;
  216. dim1 = Dim.Percent (n1);
  217. dim2 = Dim.Percent (n2);
  218. Assert.NotEqual (dim1, dim2);
  219. }
  220. [Fact]
  221. public void Percent_Invalid_Throws ()
  222. {
  223. var dim = Dim.Percent (0);
  224. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  225. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  226. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  227. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  228. }
  229. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  230. // TODO: A new test that calls SetRelativeLayout directly is needed.
  231. [Fact]
  232. [TestRespondersDisposed]
  233. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  234. {
  235. var t = new View ("top") { Width = 80, Height = 25 };
  236. var w = new Window (new Rect (1, 2, 4, 5)) { Title = "w" };
  237. t.Add (w);
  238. t.LayoutSubviews ();
  239. Assert.Equal (3, w.Width = 3);
  240. Assert.Equal (4, w.Height = 4);
  241. t.Dispose ();
  242. }
  243. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  244. // TODO: A new test that calls SetRelativeLayout directly is needed.
  245. [Fact]
  246. [TestRespondersDisposed]
  247. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  248. {
  249. var t = new View ("top") { Width = 80, Height = 25 };
  250. var w = new Window {
  251. Width = Dim.Fill (),
  252. Height = Dim.Sized (10)
  253. };
  254. var v = new View ("v") {
  255. Width = Dim.Width (w) - 2,
  256. Height = Dim.Percent (10)
  257. };
  258. w.Add (v);
  259. t.Add (w);
  260. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  261. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  262. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  263. t.LayoutSubviews ();
  264. Assert.Equal (2, v.Width = 2);
  265. Assert.Equal (2, v.Height = 2);
  266. // Force v to be LayoutStyle.Absolute;
  267. v.Frame = new Rect (0, 1, 3, 4);
  268. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  269. t.LayoutSubviews ();
  270. Assert.Equal (2, v.Width = 2);
  271. Assert.Equal (2, v.Height = 2);
  272. t.Dispose ();
  273. }
  274. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  275. // TODO: A new test that calls SetRelativeLayout directly is needed.
  276. [Fact]
  277. [AutoInitShutdown]
  278. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  279. {
  280. // Testing with the Button because it properly handles the Dim class.
  281. var t = Application.Top;
  282. var w = new Window {
  283. Width = 100,
  284. Height = 100
  285. };
  286. var f1 = new FrameView ("f1") {
  287. X = 0,
  288. Y = 0,
  289. Width = Dim.Percent (50),
  290. Height = 5
  291. };
  292. var f2 = new FrameView ("f2") {
  293. X = Pos.Right (f1),
  294. Y = 0,
  295. Width = Dim.Fill (),
  296. Height = 5
  297. };
  298. var v1 = new Button ("v1") {
  299. AutoSize = false,
  300. X = Pos.X (f1) + 2,
  301. Y = Pos.Bottom (f1) + 2,
  302. Width = Dim.Width (f1) - 2,
  303. Height = Dim.Fill () - 2,
  304. ValidatePosDim = true
  305. };
  306. var v2 = new Button ("v2") {
  307. AutoSize = false,
  308. X = Pos.X (f2) + 2,
  309. Y = Pos.Bottom (f2) + 2,
  310. Width = Dim.Width (f2) - 2,
  311. Height = Dim.Fill () - 2,
  312. ValidatePosDim = true
  313. };
  314. var v3 = new Button ("v3") {
  315. AutoSize = false,
  316. Width = Dim.Percent (10),
  317. Height = Dim.Percent (10),
  318. ValidatePosDim = true
  319. };
  320. var v4 = new Button ("v4") {
  321. AutoSize = false,
  322. Width = Dim.Sized (50),
  323. Height = Dim.Sized (50),
  324. ValidatePosDim = true
  325. };
  326. var v5 = new Button ("v5") {
  327. AutoSize = false,
  328. Width = Dim.Width (v1) - Dim.Width (v3),
  329. Height = Dim.Height (v1) - Dim.Height (v3),
  330. ValidatePosDim = true
  331. };
  332. var v6 = new Button ("v6") {
  333. AutoSize = false,
  334. X = Pos.X (f2),
  335. Y = Pos.Bottom (f2) + 2,
  336. Width = Dim.Percent (20, true),
  337. Height = Dim.Percent (20, true),
  338. ValidatePosDim = true
  339. };
  340. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  341. t.Add (w);
  342. t.Ready += (s, e) => {
  343. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  344. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  345. Assert.Equal (100, w.Frame.Width);
  346. Assert.Equal (100, w.Frame.Height);
  347. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  348. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  349. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  350. Assert.Equal (5, f1.Frame.Height);
  351. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  352. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  353. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  354. Assert.Equal (5, f2.Frame.Height);
  355. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
  356. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  357. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  358. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  359. Assert.Equal ("Combine(View(Width,FrameView(f2)(49,0,49,5))-Absolute(2))", v2.Width.ToString ());
  360. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  361. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  362. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  363. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  364. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  365. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  366. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  367. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  368. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  369. Assert.Equal (50, v4.Frame.Width);
  370. Assert.Equal (50, v4.Frame.Height);
  371. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))", v5.Width.ToString ());
  372. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,47,89))-View(Height,Button(v3)(0,0,9,9)))", v5.Height.ToString ());
  373. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  374. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  375. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  376. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  377. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  378. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  379. w.Width = 200;
  380. Assert.True (t.LayoutNeeded);
  381. w.Height = 200;
  382. t.LayoutSubviews ();
  383. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  384. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  385. Assert.Equal (200, w.Frame.Width);
  386. Assert.Equal (200, w.Frame.Height);
  387. f1.Text = "Frame1";
  388. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  389. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  390. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  391. Assert.Equal (5, f1.Frame.Height);
  392. f2.Text = "Frame2";
  393. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  394. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  395. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  396. Assert.Equal (5, f2.Frame.Height);
  397. v1.Text = "Button1";
  398. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
  399. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  400. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  401. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  402. v2.Text = "Button2";
  403. Assert.Equal ("Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
  404. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  405. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  406. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  407. v3.Text = "Button3";
  408. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  409. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  410. 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
  411. Assert.Equal (19, v3.Frame.Height); // 199*10%=19
  412. v4.Text = "Button4";
  413. v4.AutoSize = false;
  414. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  415. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  416. Assert.Equal (50, v4.Frame.Width);
  417. Assert.Equal (50, v4.Frame.Height);
  418. v4.AutoSize = true;
  419. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  420. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  421. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  422. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  423. v5.Text = "Button5";
  424. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))", v5.Width.ToString ());
  425. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))", v5.Height.ToString ());
  426. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  427. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  428. v6.Text = "Button6";
  429. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  430. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  431. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  432. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  433. };
  434. Application.Iteration += (s, a) => Application.RequestStop ();
  435. Application.Run ();
  436. }
  437. // See #2461
  438. //[Fact]
  439. //public void Dim_Referencing_SuperView_Throws ()
  440. //{
  441. // var super = new View ("super") {
  442. // Width = 10,
  443. // Height = 10
  444. // };
  445. // var view = new View ("view") {
  446. // Width = Dim.Width (super), // this is not allowed
  447. // Height = Dim.Height (super), // this is not allowed
  448. // };
  449. // super.Add (view);
  450. // super.BeginInit ();
  451. // super.EndInit ();
  452. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  453. //}
  454. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  455. // TODO: A new test that calls SetRelativeLayout directly is needed.
  456. /// <summary>
  457. /// This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461
  458. /// </summary>
  459. [Fact]
  460. [TestRespondersDisposed]
  461. public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  462. {
  463. var t = new View { Width = 80, Height = 25 };
  464. var w = new Window {
  465. Width = Dim.Width (t) - 2, // 78
  466. Height = Dim.Height (t) - 2 // 23
  467. };
  468. var f = new FrameView ();
  469. var v1 = new View {
  470. Width = Dim.Width (w) - 2, // 76
  471. Height = Dim.Height (w) - 2 // 21
  472. };
  473. var v2 = new View {
  474. Width = Dim.Width (v1) - 2, // 74
  475. Height = Dim.Height (v1) - 2 // 19
  476. };
  477. f.Add (v1, v2);
  478. w.Add (f);
  479. t.Add (w);
  480. t.BeginInit ();
  481. t.EndInit ();
  482. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  483. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  484. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  485. Assert.Equal (80, t.Frame.Width);
  486. Assert.Equal (25, t.Frame.Height);
  487. Assert.Equal (78, w.Frame.Width);
  488. Assert.Equal (23, w.Frame.Height);
  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. t.Dispose ();
  496. }
  497. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  498. // TODO: A new test that calls SetRelativeLayout directly is needed.
  499. [Fact]
  500. [TestRespondersDisposed]
  501. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  502. {
  503. var t = new View ("top") { Width = 80, Height = 25 };
  504. var w = new Window {
  505. Width = Dim.Width (t) - 2, // 78
  506. Height = Dim.Height (t) - 2 // 23
  507. };
  508. var f = new FrameView ();
  509. var v1 = new View {
  510. Width = Dim.Width (w) - 2, // 76
  511. Height = Dim.Height (w) - 2 // 21
  512. };
  513. var v2 = new View {
  514. Width = Dim.Width (v1) - 2, // 74
  515. Height = Dim.Height (v1) - 2 // 19
  516. };
  517. f.Add (v1, v2);
  518. w.Add (f);
  519. t.Add (w);
  520. t.BeginInit ();
  521. t.EndInit ();
  522. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  523. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  524. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  525. var exception = Record.Exception (t.LayoutSubviews);
  526. Assert.Null (exception);
  527. Assert.Equal (80, t.Frame.Width);
  528. Assert.Equal (25, t.Frame.Height);
  529. Assert.Equal (78, w.Frame.Width);
  530. Assert.Equal (23, w.Frame.Height);
  531. Assert.Equal (6, f.Frame.Width);
  532. Assert.Equal (6, f.Frame.Height);
  533. Assert.Equal (76, v1.Frame.Width);
  534. Assert.Equal (21, v1.Frame.Height);
  535. Assert.Equal (74, v2.Frame.Width);
  536. Assert.Equal (19, v2.Frame.Height);
  537. t.Dispose ();
  538. }
  539. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  540. // TODO: A new test that calls SetRelativeLayout directly is needed.
  541. [Fact]
  542. [TestRespondersDisposed]
  543. public void PosCombine_View_Not_Added_Throws ()
  544. {
  545. var t = new View { Width = 80, Height = 50 };
  546. var super = new View {
  547. Width = Dim.Width (t) - 2,
  548. Height = Dim.Height (t) - 2
  549. };
  550. t.Add (super);
  551. var sub = new View ();
  552. super.Add (sub);
  553. var v1 = new View {
  554. Width = Dim.Width (super) - 2,
  555. Height = Dim.Height (super) - 2
  556. };
  557. var v2 = new View {
  558. Width = Dim.Width (v1) - 2,
  559. Height = Dim.Height (v1) - 2
  560. };
  561. sub.Add (v1);
  562. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  563. sub.Width = Dim.Fill () - Dim.Width (v2);
  564. sub.Height = Dim.Fill () - Dim.Height (v2);
  565. t.BeginInit ();
  566. t.EndInit ();
  567. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  568. t.Dispose ();
  569. v2.Dispose ();
  570. }
  571. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  572. // A new test that does not depend on Application is needed.
  573. [Fact]
  574. [AutoInitShutdown]
  575. public void Dim_Add_Operator ()
  576. {
  577. var top = Application.Top;
  578. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  579. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  580. var count = 0;
  581. field.KeyDown += (s, k) => {
  582. if (k.KeyCode == KeyCode.Enter) {
  583. field.Text = $"Label {count}";
  584. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  585. view.Add (label);
  586. Assert.Equal ($"Label {count}", label.Text);
  587. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  588. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  589. view.Height += 1;
  590. count++;
  591. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  592. }
  593. };
  594. Application.Iteration += (s, a) => {
  595. while (count < 20) {
  596. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  597. }
  598. Application.RequestStop ();
  599. };
  600. var win = new Window ();
  601. win.Add (view);
  602. win.Add (field);
  603. top.Add (win);
  604. Application.Run (top);
  605. Assert.Equal (20, count);
  606. }
  607. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  608. // TODO: A new test that calls SetRelativeLayout directly is needed.
  609. [Fact]
  610. [AutoInitShutdown]
  611. public void Dim_Subtract_Operator ()
  612. {
  613. var top = Application.Top;
  614. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  615. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  616. var count = 20;
  617. var listLabels = new List<Label> ();
  618. for (int i = 0; i < count; i++) {
  619. field.Text = $"Label {i}";
  620. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  621. view.Add (label);
  622. Assert.Equal ($"Label {i}", label.Text);
  623. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  624. listLabels.Add (label);
  625. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  626. view.Height += 1;
  627. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  628. }
  629. field.KeyDown += (s, k) => {
  630. if (k.KeyCode == KeyCode.Enter) {
  631. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  632. view.Remove (listLabels [count - 1]);
  633. listLabels [count - 1].Dispose ();
  634. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  635. view.Height -= 1;
  636. count--;
  637. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  638. }
  639. };
  640. Application.Iteration += (s, a) => {
  641. while (count > 0) {
  642. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  643. }
  644. Application.RequestStop ();
  645. };
  646. var win = new Window ();
  647. win.Add (view);
  648. win.Add (field);
  649. top.Add (win);
  650. Application.Run (top);
  651. Assert.Equal (0, count);
  652. }
  653. [Fact]
  654. [TestRespondersDisposed]
  655. public void Internal_Tests ()
  656. {
  657. var dimFactor = new Dim.DimFactor (0.10F);
  658. Assert.Equal (10, dimFactor.Anchor (100));
  659. var dimAbsolute = new Dim.DimAbsolute (10);
  660. Assert.Equal (10, dimAbsolute.Anchor (0));
  661. var dimFill = new Dim.DimFill (1);
  662. Assert.Equal (99, dimFill.Anchor (100));
  663. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  664. Assert.Equal (dimCombine._left, dimFactor);
  665. Assert.Equal (dimCombine._right, dimAbsolute);
  666. Assert.Equal (20, dimCombine.Anchor (100));
  667. var view = new View (new Rect (20, 10, 20, 1));
  668. var dimViewHeight = new Dim.DimView (view, 0);
  669. Assert.Equal (1, dimViewHeight.Anchor (0));
  670. var dimViewWidth = new Dim.DimView (view, 1);
  671. Assert.Equal (20, dimViewWidth.Anchor (0));
  672. view.Dispose ();
  673. }
  674. [Fact]
  675. public void Function_SetsValue ()
  676. {
  677. var text = "Test";
  678. var dim = Dim.Function (() => text.Length);
  679. Assert.Equal ("DimFunc(4)", dim.ToString ());
  680. text = "New Test";
  681. Assert.Equal ("DimFunc(8)", dim.ToString ());
  682. text = "";
  683. Assert.Equal ("DimFunc(0)", dim.ToString ());
  684. }
  685. [Fact]
  686. public void Function_Equal ()
  687. {
  688. var f1 = () => 0;
  689. var f2 = () => 0;
  690. var dim1 = Dim.Function (f1);
  691. var dim2 = Dim.Function (f2);
  692. Assert.Equal (dim1, dim2);
  693. f2 = () => 1;
  694. dim2 = Dim.Function (f2);
  695. Assert.NotEqual (dim1, dim2);
  696. }
  697. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  698. // TODO: A new test that calls SetRelativeLayout directly is needed.
  699. [Theory]
  700. [AutoInitShutdown]
  701. [InlineData (0, true)]
  702. [InlineData (0, false)]
  703. [InlineData (50, true)]
  704. [InlineData (50, false)]
  705. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  706. {
  707. var container = new View {
  708. Width = 100,
  709. Height = 100
  710. };
  711. var label = new Label {
  712. X = testHorizontal ? startingDistance : 0,
  713. Y = testHorizontal ? 0 : startingDistance,
  714. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  715. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  716. };
  717. container.Add (label);
  718. Application.Top.Add (container);
  719. Application.Top.BeginInit ();
  720. Application.Top.EndInit ();
  721. Application.Top.LayoutSubviews ();
  722. Assert.Equal (100, container.Frame.Width);
  723. Assert.Equal (100, container.Frame.Height);
  724. if (testHorizontal) {
  725. Assert.Equal (51, label.Frame.Width);
  726. Assert.Equal (1, label.Frame.Height);
  727. } else {
  728. Assert.Equal (1, label.Frame.Width);
  729. Assert.Equal (51, label.Frame.Height);
  730. }
  731. }
  732. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  733. // TODO: A new test that calls SetRelativeLayout directly is needed.
  734. [Fact]
  735. [TestRespondersDisposed]
  736. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  737. {
  738. var super = new View ("super") {
  739. Width = 10,
  740. Height = 10
  741. };
  742. var view = new View ("view") {
  743. Width = Dim.Width (super), // this is allowed
  744. Height = Dim.Height (super) // this is allowed
  745. };
  746. super.Add (view);
  747. super.BeginInit ();
  748. super.EndInit ();
  749. var exception = Record.Exception (super.LayoutSubviews);
  750. Assert.Null (exception);
  751. super.Dispose ();
  752. }
  753. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  754. // TODO: A new test that calls SetRelativeLayout directly is needed.
  755. [Fact]
  756. [TestRespondersDisposed]
  757. public void Dim_SyperView_Referencing_SubView_Throws ()
  758. {
  759. var super = new View ("super") {
  760. Width = 10,
  761. Height = 10
  762. };
  763. var view2 = new View ("view2") {
  764. Width = 10,
  765. Height = 10
  766. };
  767. var view = new View ("view") {
  768. Width = Dim.Width (view2), // this is not allowed
  769. Height = Dim.Height (view2) // this is not allowed
  770. };
  771. view.Add (view2);
  772. super.Add (view);
  773. super.BeginInit ();
  774. super.EndInit ();
  775. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  776. super.Dispose ();
  777. }
  778. }