DimTests.cs 34 KB

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