DimTests.cs 35 KB

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