DimTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. // BUGBUG: v2 - f references t here; t is f's super-superview. This is supported!
  476. // BUGBUG: v2 - f references v2 here; v2 is f's subview. This is not supported!
  477. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  478. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  479. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  480. Assert.Equal (80, t.Frame.Width);
  481. Assert.Equal (25, t.Frame.Height);
  482. Assert.Equal (78, w.Frame.Width);
  483. Assert.Equal (23, w.Frame.Height);
  484. // BUGBUG: v2 - this no longer works - see above
  485. //Assert.Equal (6, f.Frame.Width);
  486. //Assert.Equal (6, f.Frame.Height);
  487. //Assert.Equal (76, v1.Frame.Width);
  488. //Assert.Equal (21, v1.Frame.Height);
  489. //Assert.Equal (74, v2.Frame.Width);
  490. //Assert.Equal (19, v2.Frame.Height);
  491. t.Dispose ();
  492. }
  493. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  494. // TODO: A new test that calls SetRelativeLayout directly is needed.
  495. [Fact] [TestRespondersDisposed]
  496. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  497. {
  498. var t = new View ("top") { Width = 80, Height = 25 };
  499. var w = new Window {
  500. Width = Dim.Width (t) - 2, // 78
  501. Height = Dim.Height (t) - 2 // 23
  502. };
  503. var f = new FrameView ();
  504. var v1 = new View {
  505. Width = Dim.Width (w) - 2, // 76
  506. Height = Dim.Height (w) - 2 // 21
  507. };
  508. var v2 = new View {
  509. Width = Dim.Width (v1) - 2, // 74
  510. Height = Dim.Height (v1) - 2 // 19
  511. };
  512. f.Add (v1, v2);
  513. w.Add (f);
  514. t.Add (w);
  515. t.BeginInit ();
  516. t.EndInit ();
  517. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  518. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  519. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  520. var exception = Record.Exception (t.LayoutSubviews);
  521. Assert.Null (exception);
  522. Assert.Equal (80, t.Frame.Width);
  523. Assert.Equal (25, t.Frame.Height);
  524. Assert.Equal (78, w.Frame.Width);
  525. Assert.Equal (23, w.Frame.Height);
  526. Assert.Equal (6, f.Frame.Width);
  527. Assert.Equal (6, f.Frame.Height);
  528. Assert.Equal (76, v1.Frame.Width);
  529. Assert.Equal (21, v1.Frame.Height);
  530. Assert.Equal (74, v2.Frame.Width);
  531. Assert.Equal (19, v2.Frame.Height);
  532. t.Dispose ();
  533. }
  534. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  535. // TODO: A new test that calls SetRelativeLayout directly is needed.
  536. [Fact] [TestRespondersDisposed]
  537. public void PosCombine_View_Not_Added_Throws ()
  538. {
  539. var t = new View { Width = 80, Height = 50 };
  540. // BUGBUG: v2 - super should not reference it's superview (t)
  541. var super = new View {
  542. Width = Dim.Width (t) - 2,
  543. Height = Dim.Height (t) - 2
  544. };
  545. t.Add (super);
  546. var sub = new View ();
  547. super.Add (sub);
  548. var v1 = new View {
  549. Width = Dim.Width (super) - 2,
  550. Height = Dim.Height (super) - 2
  551. };
  552. var v2 = new View {
  553. Width = Dim.Width (v1) - 2,
  554. Height = Dim.Height (v1) - 2
  555. };
  556. sub.Add (v1);
  557. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  558. sub.Width = Dim.Fill () - Dim.Width (v2);
  559. sub.Height = Dim.Fill () - Dim.Height (v2);
  560. t.BeginInit ();
  561. t.EndInit ();
  562. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  563. t.Dispose ();
  564. v2.Dispose ();
  565. }
  566. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  567. // A new test that does not depend on Application is needed.
  568. [Fact] [AutoInitShutdown]
  569. public void Dim_Add_Operator ()
  570. {
  571. var top = Application.Top;
  572. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  573. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  574. var count = 0;
  575. field.KeyDown += (s, k) => {
  576. if (k.KeyCode == KeyCode.Enter) {
  577. field.Text = $"Label {count}";
  578. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  579. view.Add (label);
  580. Assert.Equal ($"Label {count}", label.Text);
  581. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  582. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  583. view.Height += 1;
  584. count++;
  585. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  586. }
  587. };
  588. Application.Iteration += (s, a) => {
  589. while (count < 20) {
  590. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  591. }
  592. Application.RequestStop ();
  593. };
  594. var win = new Window ();
  595. win.Add (view);
  596. win.Add (field);
  597. top.Add (win);
  598. Application.Run (top);
  599. Assert.Equal (20, count);
  600. }
  601. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  602. // TODO: A new test that calls SetRelativeLayout directly is needed.
  603. [Fact] [AutoInitShutdown]
  604. public void Dim_Subtract_Operator ()
  605. {
  606. var top = Application.Top;
  607. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  608. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  609. var count = 20;
  610. var listLabels = new List<Label> ();
  611. for (var i = 0; i < count; i++) {
  612. field.Text = $"Label {i}";
  613. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  614. view.Add (label);
  615. Assert.Equal ($"Label {i}", label.Text);
  616. // BUGBUG: Bogus test; views have not been initialized yet
  617. //Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  618. listLabels.Add (label);
  619. // BUGBUG: Bogus test; views have not been initialized yet
  620. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  621. view.Height += 1;
  622. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  623. }
  624. field.KeyDown += (s, k) => {
  625. if (k.KeyCode == KeyCode.Enter) {
  626. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  627. view.Remove (listLabels [count - 1]);
  628. listLabels [count - 1].Dispose ();
  629. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  630. view.Height -= 1;
  631. count--;
  632. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  633. }
  634. };
  635. Application.Iteration += (s, a) => {
  636. while (count > 0) {
  637. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  638. }
  639. Application.RequestStop ();
  640. };
  641. var win = new Window ();
  642. win.Add (view);
  643. win.Add (field);
  644. top.Add (win);
  645. Application.Run (top);
  646. Assert.Equal (0, count);
  647. }
  648. [Fact] [TestRespondersDisposed]
  649. public void Internal_Tests ()
  650. {
  651. var dimFactor = new Dim.DimFactor (0.10F);
  652. Assert.Equal (10, dimFactor.Anchor (100));
  653. var dimAbsolute = new Dim.DimAbsolute (10);
  654. Assert.Equal (10, dimAbsolute.Anchor (0));
  655. var dimFill = new Dim.DimFill (1);
  656. Assert.Equal (99, dimFill.Anchor (100));
  657. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  658. Assert.Equal (dimCombine._left, dimFactor);
  659. Assert.Equal (dimCombine._right, dimAbsolute);
  660. Assert.Equal (20, dimCombine.Anchor (100));
  661. var view = new View (new Rect (20, 10, 20, 1));
  662. var dimViewHeight = new Dim.DimView (view, 0);
  663. Assert.Equal (1, dimViewHeight.Anchor (0));
  664. var dimViewWidth = new Dim.DimView (view, 1);
  665. Assert.Equal (20, dimViewWidth.Anchor (0));
  666. view.Dispose ();
  667. }
  668. [Fact]
  669. public void Function_SetsValue ()
  670. {
  671. var text = "Test";
  672. var dim = Dim.Function (() => text.Length);
  673. Assert.Equal ("DimFunc(4)", dim.ToString ());
  674. text = "New Test";
  675. Assert.Equal ("DimFunc(8)", dim.ToString ());
  676. text = "";
  677. Assert.Equal ("DimFunc(0)", dim.ToString ());
  678. }
  679. [Fact]
  680. public void Function_Equal ()
  681. {
  682. var f1 = () => 0;
  683. var f2 = () => 0;
  684. var dim1 = Dim.Function (f1);
  685. var dim2 = Dim.Function (f2);
  686. Assert.Equal (dim1, dim2);
  687. f2 = () => 1;
  688. dim2 = Dim.Function (f2);
  689. Assert.NotEqual (dim1, dim2);
  690. }
  691. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  692. // TODO: A new test that calls SetRelativeLayout directly is needed.
  693. [Theory] [AutoInitShutdown]
  694. [InlineData (0, true)]
  695. [InlineData (0, false)]
  696. [InlineData (50, true)]
  697. [InlineData (50, false)]
  698. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  699. {
  700. var container = new View {
  701. Width = 100,
  702. Height = 100
  703. };
  704. var label = new Label {
  705. X = testHorizontal ? startingDistance : 0,
  706. Y = testHorizontal ? 0 : startingDistance,
  707. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  708. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  709. };
  710. container.Add (label);
  711. Application.Top.Add (container);
  712. Application.Top.BeginInit ();
  713. Application.Top.EndInit ();
  714. Application.Top.LayoutSubviews ();
  715. Assert.Equal (100, container.Frame.Width);
  716. Assert.Equal (100, container.Frame.Height);
  717. if (testHorizontal) {
  718. Assert.Equal (51, label.Frame.Width);
  719. Assert.Equal (1, label.Frame.Height);
  720. } else {
  721. Assert.Equal (1, label.Frame.Width);
  722. Assert.Equal (51, label.Frame.Height);
  723. }
  724. }
  725. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  726. // TODO: A new test that calls SetRelativeLayout directly is needed.
  727. [Fact] [TestRespondersDisposed]
  728. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  729. {
  730. var super = new View ("super") {
  731. Width = 10,
  732. Height = 10
  733. };
  734. var view = new View ("view") {
  735. Width = Dim.Width (super), // this is allowed
  736. Height = Dim.Height (super) // this is allowed
  737. };
  738. super.Add (view);
  739. super.BeginInit ();
  740. super.EndInit ();
  741. var exception = Record.Exception (super.LayoutSubviews);
  742. Assert.Null (exception);
  743. super.Dispose ();
  744. }
  745. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  746. // TODO: A new test that calls SetRelativeLayout directly is needed.
  747. [Fact] [TestRespondersDisposed]
  748. public void Dim_SyperView_Referencing_SubView_Throws ()
  749. {
  750. var super = new View ("super") {
  751. Width = 10,
  752. Height = 10
  753. };
  754. var view2 = new View ("view2") {
  755. Width = 10,
  756. Height = 10
  757. };
  758. var view = new View ("view") {
  759. Width = Dim.Width (view2), // this is not allowed
  760. Height = Dim.Height (view2) // this is not allowed
  761. };
  762. view.Add (view2);
  763. super.Add (view);
  764. super.BeginInit ();
  765. super.EndInit ();
  766. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  767. super.Dispose ();
  768. }
  769. }