DimTests.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 = Application.Top;
  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 (new Key (KeyCode.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 = Application.Top;
  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 (new Key (KeyCode.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 Rect (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. Application.Top.Add (container);
  325. Application.Top.BeginInit ();
  326. Application.Top.EndInit ();
  327. Application.Top.LayoutSubviews ();
  328. Assert.Equal (100, container.Frame.Width);
  329. Assert.Equal (100, container.Frame.Height);
  330. if (testHorizontal)
  331. {
  332. Assert.Equal (51, label.Frame.Width);
  333. Assert.Equal (1, label.Frame.Height);
  334. }
  335. else
  336. {
  337. Assert.Equal (1, label.Frame.Width);
  338. Assert.Equal (51, label.Frame.Height);
  339. }
  340. }
  341. [Fact]
  342. public void Fill_Equal ()
  343. {
  344. var margin1 = 0;
  345. var margin2 = 0;
  346. Dim dim1 = Dim.Fill (margin1);
  347. Dim dim2 = Dim.Fill (margin2);
  348. Assert.Equal (dim1, dim2);
  349. }
  350. // TODO: Other Dim.Height tests (e.g. Equal?)
  351. [Fact]
  352. public void Fill_SetsValue ()
  353. {
  354. var testMargin = 0;
  355. Dim dim = Dim.Fill ();
  356. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  357. testMargin = 0;
  358. dim = Dim.Fill (testMargin);
  359. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  360. testMargin = 5;
  361. dim = Dim.Fill (testMargin);
  362. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  363. }
  364. [Fact]
  365. public void Function_Equal ()
  366. {
  367. Func<int> f1 = () => 0;
  368. Func<int> f2 = () => 0;
  369. Dim dim1 = Dim.Function (f1);
  370. Dim dim2 = Dim.Function (f2);
  371. Assert.Equal (dim1, dim2);
  372. f2 = () => 1;
  373. dim2 = Dim.Function (f2);
  374. Assert.NotEqual (dim1, dim2);
  375. }
  376. [Fact]
  377. public void Function_SetsValue ()
  378. {
  379. var text = "Test";
  380. Dim dim = Dim.Function (() => text.Length);
  381. Assert.Equal ("DimFunc(4)", dim.ToString ());
  382. text = "New Test";
  383. Assert.Equal ("DimFunc(8)", dim.ToString ());
  384. text = "";
  385. Assert.Equal ("DimFunc(0)", dim.ToString ());
  386. }
  387. [Fact]
  388. public void Height_Set_To_Null_Throws ()
  389. {
  390. Dim dim = Dim.Height (null);
  391. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  392. }
  393. [Fact]
  394. [TestRespondersDisposed]
  395. public void Height_SetsValue ()
  396. {
  397. var testVal = Rect.Empty;
  398. var testValview = new View { Frame = testVal };
  399. Dim dim = Dim.Height (testValview);
  400. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  401. testValview.Dispose ();
  402. testVal = new Rect (1, 2, 3, 4);
  403. testValview = new View { Frame = testVal };
  404. dim = Dim.Height (testValview);
  405. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  406. testValview.Dispose ();
  407. }
  408. [Fact]
  409. [TestRespondersDisposed]
  410. public void Internal_Tests ()
  411. {
  412. var dimFactor = new Dim.DimFactor (0.10F);
  413. Assert.Equal (10, dimFactor.Anchor (100));
  414. var dimAbsolute = new Dim.DimAbsolute (10);
  415. Assert.Equal (10, dimAbsolute.Anchor (0));
  416. var dimFill = new Dim.DimFill (1);
  417. Assert.Equal (99, dimFill.Anchor (100));
  418. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  419. Assert.Equal (dimCombine._left, dimFactor);
  420. Assert.Equal (dimCombine._right, dimAbsolute);
  421. Assert.Equal (20, dimCombine.Anchor (100));
  422. var view = new View { Frame = new Rect (20, 10, 20, 1) };
  423. var dimViewHeight = new Dim.DimView (view, 0);
  424. Assert.Equal (1, dimViewHeight.Anchor (0));
  425. var dimViewWidth = new Dim.DimView (view, 1);
  426. Assert.Equal (20, dimViewWidth.Anchor (0));
  427. view.Dispose ();
  428. }
  429. [Fact]
  430. public void New_Works ()
  431. {
  432. var dim = new Dim ();
  433. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  434. }
  435. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  436. // TODO: A new test that calls SetRelativeLayout directly is needed.
  437. [Fact]
  438. [AutoInitShutdown]
  439. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  440. {
  441. // Testing with the Button because it properly handles the Dim class.
  442. Toplevel t = Application.Top;
  443. var w = new Window { Width = 100, Height = 100 };
  444. var f1 = new FrameView
  445. {
  446. X = 0,
  447. Y = 0,
  448. Width = Dim.Percent (50),
  449. Height = 5,
  450. Title = "f1"
  451. };
  452. var f2 = new FrameView
  453. {
  454. X = Pos.Right (f1),
  455. Y = 0,
  456. Width = Dim.Fill (),
  457. Height = 5,
  458. Title = "f2"
  459. };
  460. var v1 = new Button
  461. {
  462. AutoSize = false,
  463. X = Pos.X (f1) + 2,
  464. Y = Pos.Bottom (f1) + 2,
  465. Width = Dim.Width (f1) - 2,
  466. Height = Dim.Fill () - 2,
  467. ValidatePosDim = true,
  468. Text = "v1"
  469. };
  470. var v2 = new Button
  471. {
  472. AutoSize = false,
  473. X = Pos.X (f2) + 2,
  474. Y = Pos.Bottom (f2) + 2,
  475. Width = Dim.Width (f2) - 2,
  476. Height = Dim.Fill () - 2,
  477. ValidatePosDim = true,
  478. Text = "v2"
  479. };
  480. var v3 = new Button
  481. {
  482. AutoSize = false,
  483. Width = Dim.Percent (10),
  484. Height = Dim.Percent (10),
  485. ValidatePosDim = true,
  486. Text = "v3"
  487. };
  488. var v4 = new Button
  489. {
  490. AutoSize = false,
  491. Width = Dim.Sized (50),
  492. Height = Dim.Sized (50),
  493. ValidatePosDim = true,
  494. Text = "v4"
  495. };
  496. var v5 = new Button
  497. {
  498. AutoSize = false,
  499. Width = Dim.Width (v1) - Dim.Width (v3),
  500. Height = Dim.Height (v1) - Dim.Height (v3),
  501. ValidatePosDim = true,
  502. Text = "v5"
  503. };
  504. var v6 = new Button
  505. {
  506. AutoSize = false,
  507. X = Pos.X (f2),
  508. Y = Pos.Bottom (f2) + 2,
  509. Width = Dim.Percent (20, true),
  510. Height = Dim.Percent (20, true),
  511. ValidatePosDim = true,
  512. Text = "v6"
  513. };
  514. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  515. t.Add (w);
  516. t.Ready += (s, e) =>
  517. {
  518. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  519. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  520. Assert.Equal (100, w.Frame.Width);
  521. Assert.Equal (100, w.Frame.Height);
  522. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  523. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  524. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  525. Assert.Equal (5, f1.Frame.Height);
  526. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  527. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  528. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  529. Assert.Equal (5, f2.Frame.Height);
  530. #if DEBUG
  531. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
  532. #else
  533. Assert.Equal ("Combine(View(Width,FrameView()(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
  534. #endif
  535. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  536. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  537. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  538. #if DEBUG
  539. Assert.Equal (
  540. "Combine(View(Width,FrameView(f2)(49,0,49,5))-Absolute(2))",
  541. v2.Width.ToString ()
  542. #else
  543. Assert.Equal (
  544. "Combine(View(Width,FrameView()(49,0,49,5))-Absolute(2))",
  545. v2.Width.ToString ()
  546. #endif
  547. );
  548. #if DEBUG
  549. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  550. #else
  551. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  552. #endif
  553. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  554. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  555. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  556. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  557. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  558. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  559. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  560. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  561. Assert.Equal (50, v4.Frame.Width);
  562. Assert.Equal (50, v4.Frame.Height);
  563. #if DEBUG
  564. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))", v5.Width.ToString ());
  565. #else
  566. Assert.Equal ("Combine(View(Height,Button()(2,7,47,89))-View(Height,Button()(0,0,9,9)))", v5.Height.ToString ( ));
  567. #endif
  568. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  569. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  570. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  571. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  572. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  573. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  574. w.Width = 200;
  575. Assert.True (t.LayoutNeeded);
  576. w.Height = 200;
  577. t.LayoutSubviews ();
  578. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  579. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  580. Assert.Equal (200, w.Frame.Width);
  581. Assert.Equal (200, w.Frame.Height);
  582. f1.Text = "Frame1";
  583. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  584. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  585. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  586. Assert.Equal (5, f1.Frame.Height);
  587. f2.Text = "Frame2";
  588. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  589. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  590. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  591. Assert.Equal (5, f2.Frame.Height);
  592. v1.Text = "Button1";
  593. #if DEBUG
  594. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
  595. #else
  596. Assert.Equal ("Combine(View(Width,FrameView()(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
  597. #endif
  598. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  599. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  600. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  601. v2.Text = "Button2";
  602. #if DEBUG
  603. Assert.Equal ( "Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
  604. #else
  605. Assert.Equal ( "Combine(View(Width,FrameView()(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
  606. #endif
  607. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  608. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  609. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  610. v3.Text = "Button3";
  611. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  612. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  613. // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  614. Assert.Equal (19, v3.Frame.Width );
  615. // 199*10%=19
  616. Assert.Equal (19, v3.Frame.Height);
  617. v4.Text = "Button4";
  618. v4.AutoSize = false;
  619. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  620. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  621. Assert.Equal (50, v4.Frame.Width);
  622. Assert.Equal (50, v4.Frame.Height);
  623. v4.AutoSize = true;
  624. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  625. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  626. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  627. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  628. v5.Text = "Button5";
  629. #if DEBUG
  630. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))", v5.Width.ToString ());
  631. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))", v5.Height.ToString ());
  632. #else
  633. Assert.Equal ("Combine(View(Width,Button()(2,7,97,189))-View(Width,Button()(0,0,19,19)))", v5.Width.ToString ());
  634. Assert.Equal ("Combine(View(Height,Button()(2,7,97,189))-View(Height,Button()(0,0,19,19)))", v5.Height.ToString ());
  635. #endif
  636. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  637. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  638. v6.Text = "Button6";
  639. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  640. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  641. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  642. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  643. };
  644. Application.Iteration += (s, a) => Application.RequestStop ();
  645. Application.Run ();
  646. }
  647. [Fact]
  648. public void Percent_Equals ()
  649. {
  650. float n1 = 0;
  651. float n2 = 0;
  652. Dim dim1 = Dim.Percent (n1);
  653. Dim dim2 = Dim.Percent (n2);
  654. Assert.Equal (dim1, dim2);
  655. n1 = n2 = 1;
  656. dim1 = Dim.Percent (n1);
  657. dim2 = Dim.Percent (n2);
  658. Assert.Equal (dim1, dim2);
  659. n1 = n2 = 0.5f;
  660. dim1 = Dim.Percent (n1);
  661. dim2 = Dim.Percent (n2);
  662. Assert.Equal (dim1, dim2);
  663. n1 = n2 = 100f;
  664. dim1 = Dim.Percent (n1);
  665. dim2 = Dim.Percent (n2);
  666. Assert.Equal (dim1, dim2);
  667. n1 = n2 = 0.3f;
  668. dim1 = Dim.Percent (n1, true);
  669. dim2 = Dim.Percent (n2, true);
  670. Assert.Equal (dim1, dim2);
  671. n1 = n2 = 0.3f;
  672. dim1 = Dim.Percent (n1);
  673. dim2 = Dim.Percent (n2, true);
  674. Assert.NotEqual (dim1, dim2);
  675. n1 = 0;
  676. n2 = 1;
  677. dim1 = Dim.Percent (n1);
  678. dim2 = Dim.Percent (n2);
  679. Assert.NotEqual (dim1, dim2);
  680. n1 = 0.5f;
  681. n2 = 1.5f;
  682. dim1 = Dim.Percent (n1);
  683. dim2 = Dim.Percent (n2);
  684. Assert.NotEqual (dim1, dim2);
  685. }
  686. [Fact]
  687. public void Percent_Invalid_Throws ()
  688. {
  689. Dim dim = Dim.Percent (0);
  690. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  691. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  692. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  693. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  694. }
  695. [Fact]
  696. public void Percent_SetsValue ()
  697. {
  698. float f = 0;
  699. Dim dim = Dim.Percent (f);
  700. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  701. f = 0.5F;
  702. dim = Dim.Percent (f);
  703. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  704. f = 100;
  705. dim = Dim.Percent (f);
  706. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  707. }
  708. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  709. // TODO: A new test that calls SetRelativeLayout directly is needed.
  710. [Fact]
  711. [TestRespondersDisposed]
  712. public void PosCombine_View_Not_Added_Throws ()
  713. {
  714. var t = new View { Width = 80, Height = 50 };
  715. var super = new View { Width = Dim.Width (t) - 2, Height = Dim.Height (t) - 2 };
  716. t.Add (super);
  717. var sub = new View ();
  718. super.Add (sub);
  719. var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 };
  720. var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 };
  721. sub.Add (v1);
  722. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  723. sub.Width = Dim.Fill () - Dim.Width (v2);
  724. sub.Height = Dim.Fill () - Dim.Height (v2);
  725. t.BeginInit ();
  726. t.EndInit ();
  727. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  728. t.Dispose ();
  729. v2.Dispose ();
  730. }
  731. [Fact]
  732. [TestRespondersDisposed]
  733. public void SetsValue ()
  734. {
  735. var testVal = Rect.Empty;
  736. var testValView = new View { Frame = testVal };
  737. Dim dim = Dim.Width (testValView);
  738. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  739. testValView.Dispose ();
  740. testVal = new Rect (1, 2, 3, 4);
  741. testValView = new View { Frame = testVal };
  742. dim = Dim.Width (testValView);
  743. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  744. testValView.Dispose ();
  745. }
  746. [Fact]
  747. public void Sized_Equals ()
  748. {
  749. var n1 = 0;
  750. var n2 = 0;
  751. Dim dim1 = Dim.Sized (n1);
  752. Dim dim2 = Dim.Sized (n2);
  753. Assert.Equal (dim1, dim2);
  754. n1 = n2 = 1;
  755. dim1 = Dim.Sized (n1);
  756. dim2 = Dim.Sized (n2);
  757. Assert.Equal (dim1, dim2);
  758. n1 = n2 = -1;
  759. dim1 = Dim.Sized (n1);
  760. dim2 = Dim.Sized (n2);
  761. Assert.Equal (dim1, dim2);
  762. n1 = 0;
  763. n2 = 1;
  764. dim1 = Dim.Sized (n1);
  765. dim2 = Dim.Sized (n2);
  766. Assert.NotEqual (dim1, dim2);
  767. }
  768. [Fact]
  769. public void Sized_SetsValue ()
  770. {
  771. Dim dim = Dim.Sized (0);
  772. Assert.Equal ("Absolute(0)", dim.ToString ());
  773. var testVal = 5;
  774. dim = Dim.Sized (testVal);
  775. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  776. testVal = -1;
  777. dim = Dim.Sized (testVal);
  778. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  779. }
  780. [Fact]
  781. [TestRespondersDisposed]
  782. public void Width_Equals ()
  783. {
  784. var testRect1 = Rect.Empty;
  785. var view1 = new View { Frame = testRect1 };
  786. var testRect2 = Rect.Empty;
  787. var view2 = new View { Frame = testRect2 };
  788. Dim dim1 = Dim.Width (view1);
  789. Dim dim2 = Dim.Width (view1);
  790. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  791. Assert.Equal (dim1, dim2);
  792. dim2 = Dim.Width (view2);
  793. Assert.NotEqual (dim1, dim2);
  794. testRect1 = new Rect (0, 1, 2, 3);
  795. view1 = new View { Frame = testRect1 };
  796. testRect2 = new Rect (0, 1, 2, 3);
  797. dim1 = Dim.Width (view1);
  798. dim2 = Dim.Width (view1);
  799. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  800. Assert.Equal (dim1, dim2);
  801. testRect1 = new Rect (0, -1, 2, 3);
  802. view1 = new View { Frame = testRect1 };
  803. testRect2 = new Rect (0, -1, 2, 3);
  804. dim1 = Dim.Width (view1);
  805. dim2 = Dim.Width (view1);
  806. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  807. Assert.Equal (dim1, dim2);
  808. testRect1 = new Rect (0, -1, 2, 3);
  809. view1 = new View { Frame = testRect1 };
  810. testRect2 = Rect.Empty;
  811. view2 = new View { Frame = testRect2 };
  812. dim1 = Dim.Width (view1);
  813. dim2 = Dim.Width (view2);
  814. Assert.NotEqual (dim1, dim2);
  815. #if DEBUG_IDISPOSABLE
  816. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  817. Responder.Instances.Clear ();
  818. Assert.Empty (Responder.Instances);
  819. #endif
  820. }
  821. [Fact]
  822. public void Width_Set_To_Null_Throws ()
  823. {
  824. Dim dim = Dim.Width (null);
  825. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  826. }
  827. }