DimTests.cs 34 KB

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