DimTests.cs 27 KB

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