Dim.Tests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. using System.Globalization;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.PosDimTests;
  8. public class DimTests
  9. {
  10. private readonly ITestOutputHelper _output;
  11. public DimTests (ITestOutputHelper output)
  12. {
  13. _output = output;
  14. Console.OutputEncoding = Encoding.Default;
  15. // Change current culture
  16. var culture = CultureInfo.CreateSpecificCulture ("en-US");
  17. Thread.CurrentThread.CurrentCulture = culture;
  18. Thread.CurrentThread.CurrentUICulture = culture;
  19. }
  20. [Fact]
  21. public void DimAbsolute_Calculate_ReturnsCorrectValue ()
  22. {
  23. var dim = new DimAbsolute (10);
  24. var result = dim.Calculate (0, 100, null, Dim.Dimension.None);
  25. Assert.Equal (10, result);
  26. }
  27. [Fact]
  28. public void DimView_Calculate_ReturnsCorrectValue ()
  29. {
  30. var view = new View { Width = 10 };
  31. var dim = new DimView (view, Dimension.Width);
  32. var result = dim.Calculate (0, 100, null, Dim.Dimension.None);
  33. Assert.Equal (10, result);
  34. }
  35. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  36. // A new test that does not depend on Application is needed.
  37. [Fact]
  38. [AutoInitShutdown]
  39. public void Dim_Add_Operator ()
  40. {
  41. Toplevel top = new ();
  42. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  43. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  44. var count = 0;
  45. field.KeyDown += (s, k) =>
  46. {
  47. if (k.KeyCode == KeyCode.Enter)
  48. {
  49. field.Text = $"Label {count}";
  50. var label = new Label { X = 0, Y = view.Viewport.Height, /*Width = 20,*/ Text = field.Text };
  51. view.Add (label);
  52. Assert.Equal ($"Label {count}", label.Text);
  53. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  54. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  55. view.Height += 1;
  56. count++;
  57. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  58. }
  59. };
  60. Application.Iteration += (s, a) =>
  61. {
  62. while (count < 20)
  63. {
  64. field.NewKeyDownEvent (Key.Enter);
  65. }
  66. Application.RequestStop ();
  67. };
  68. var win = new Window ();
  69. win.Add (view);
  70. win.Add (field);
  71. top.Add (win);
  72. Application.Run (top);
  73. top.Dispose ();
  74. Assert.Equal (20, count);
  75. }
  76. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  77. // TODO: A new test that calls SetRelativeLayout directly is needed.
  78. [Fact]
  79. [TestRespondersDisposed]
  80. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  81. {
  82. var super = new View { Width = 10, Height = 10, Text = "super" };
  83. var view = new View
  84. {
  85. Width = Dim.Width (super), // this is allowed
  86. Height = Dim.Height (super), // this is allowed
  87. Text = "view"
  88. };
  89. super.Add (view);
  90. super.BeginInit ();
  91. super.EndInit ();
  92. Exception exception = Record.Exception (super.LayoutSubviews);
  93. Assert.Null (exception);
  94. super.Dispose ();
  95. }
  96. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  97. // TODO: A new test that calls SetRelativeLayout directly is needed.
  98. [Fact]
  99. [AutoInitShutdown]
  100. public void Dim_Subtract_Operator ()
  101. {
  102. Toplevel top = new ();
  103. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  104. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  105. var count = 20;
  106. List<Label> listLabels = new ();
  107. for (var i = 0; i < count; i++)
  108. {
  109. field.Text = $"Label {i}";
  110. var label = new Label { X = 0, Y = view.Viewport.Height, /*Width = 20,*/ Text = field.Text };
  111. view.Add (label);
  112. Assert.Equal ($"Label {i}", label.Text);
  113. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  114. listLabels.Add (label);
  115. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  116. view.Height += 1;
  117. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  118. }
  119. field.KeyDown += (s, k) =>
  120. {
  121. if (k.KeyCode == KeyCode.Enter)
  122. {
  123. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  124. view.Remove (listLabels [count - 1]);
  125. listLabels [count - 1].Dispose ();
  126. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  127. view.Height -= 1;
  128. count--;
  129. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  130. }
  131. };
  132. Application.Iteration += (s, a) =>
  133. {
  134. while (count > 0)
  135. {
  136. field.NewKeyDownEvent (Key.Enter);
  137. }
  138. Application.RequestStop ();
  139. };
  140. var win = new Window ();
  141. win.Add (view);
  142. win.Add (field);
  143. top.Add (win);
  144. Application.Run (top);
  145. Assert.Equal (0, count);
  146. }
  147. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  148. // TODO: A new test that calls SetRelativeLayout directly is needed.
  149. [Fact]
  150. [TestRespondersDisposed]
  151. public void Dim_SyperView_Referencing_SubView_Throws ()
  152. {
  153. var super = new View { Width = 10, Height = 10, Text = "super" };
  154. var view2 = new View { Width = 10, Height = 10, Text = "view2" };
  155. var view = new View
  156. {
  157. Width = Dim.Width (view2), // this is not allowed
  158. Height = Dim.Height (view2), // this is not allowed
  159. Text = "view"
  160. };
  161. view.Add (view2);
  162. super.Add (view);
  163. super.BeginInit ();
  164. super.EndInit ();
  165. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  166. super.Dispose ();
  167. }
  168. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  169. // TODO: A new test that calls SetRelativeLayout directly is needed.
  170. [Fact]
  171. [TestRespondersDisposed]
  172. public void
  173. Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  174. {
  175. var t = new View { Width = 80, Height = 25, Text = "top" };
  176. var w = new Window { Width = Dim.Fill (), Height = Dim.Sized (10) };
  177. var v = new View { Width = Dim.Width (w) - 2, Height = Dim.Percent (10), Text = "v" };
  178. w.Add (v);
  179. t.Add (w);
  180. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  181. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  182. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  183. t.LayoutSubviews ();
  184. Assert.Equal (2, v.Width = 2);
  185. Assert.Equal (2, v.Height = 2);
  186. // Force v to be LayoutStyle.Absolute;
  187. v.Frame = new Rectangle (0, 1, 3, 4);
  188. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  189. t.LayoutSubviews ();
  190. Assert.Equal (2, v.Width = 2);
  191. Assert.Equal (2, v.Height = 2);
  192. t.Dispose ();
  193. }
  194. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  195. // TODO: A new test that calls SetRelativeLayout directly is needed.
  196. [Fact]
  197. [TestRespondersDisposed]
  198. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  199. {
  200. var t = new View { Width = 80, Height = 25, Text = "top" };
  201. var w = new Window
  202. {
  203. X = 1,
  204. Y = 2,
  205. Width = 4,
  206. Height = 5,
  207. Title = "w"
  208. };
  209. t.Add (w);
  210. t.LayoutSubviews ();
  211. Assert.Equal (3, w.Width = 3);
  212. Assert.Equal (4, w.Height = 4);
  213. t.Dispose ();
  214. }
  215. // See #2461
  216. //[Fact]
  217. //public void Dim_Referencing_SuperView_Throws ()
  218. //{
  219. // var super = new View ("super") {
  220. // Width = 10,
  221. // Height = 10
  222. // };
  223. // var view = new View ("view") {
  224. // Width = Dim.Width (super), // this is not allowed
  225. // Height = Dim.Height (super), // this is not allowed
  226. // };
  227. // super.Add (view);
  228. // super.BeginInit ();
  229. // super.EndInit ();
  230. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  231. //}
  232. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  233. // TODO: A new test that calls SetRelativeLayout directly is needed.
  234. [Fact]
  235. [TestRespondersDisposed]
  236. public void
  237. Dim_Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  238. {
  239. var t = new View { Width = 80, Height = 25, Text = "top" };
  240. var w = new Window { Width = Dim.Fill (), Height = Dim.Sized (10) };
  241. var v = new View { Width = Dim.Width (w) - 2, Height = Dim.Percent (10), Text = "v" };
  242. w.Add (v);
  243. t.Add (w);
  244. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  245. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  246. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  247. t.LayoutSubviews ();
  248. Assert.Equal (2, v.Width = 2);
  249. Assert.Equal (2, v.Height = 2);
  250. // Force v to be LayoutStyle.Absolute;
  251. v.Frame = new Rectangle (0, 1, 3, 4);
  252. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  253. t.LayoutSubviews ();
  254. Assert.Equal (2, v.Width = 2);
  255. Assert.Equal (2, v.Height = 2);
  256. t.Dispose ();
  257. }
  258. [Fact]
  259. public void DimHeight_Set_To_Null_Throws ()
  260. {
  261. Dim dim = Dim.Height (null);
  262. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  263. }
  264. [Fact]
  265. [TestRespondersDisposed]
  266. public void DimHeight_SetsValue ()
  267. {
  268. var testVal = Rectangle.Empty;
  269. var testValview = new View { Frame = testVal };
  270. Dim dim = Dim.Height (testValview);
  271. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  272. testValview.Dispose ();
  273. testVal = new Rectangle (1, 2, 3, 4);
  274. testValview = new View { Frame = testVal };
  275. dim = Dim.Height (testValview);
  276. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  277. testValview.Dispose ();
  278. }
  279. [Fact]
  280. [TestRespondersDisposed]
  281. public void Internal_Tests ()
  282. {
  283. var dimFactor = new Dim.DimFactor (0.10F);
  284. Assert.Equal (10, dimFactor.Anchor (100));
  285. var dimAbsolute = new Dim.DimAbsolute (10);
  286. Assert.Equal (10, dimAbsolute.Anchor (0));
  287. var dimFill = new Dim.DimFill (1);
  288. Assert.Equal (99, dimFill.Anchor (100));
  289. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  290. Assert.Equal (dimCombine._left, dimFactor);
  291. Assert.Equal (dimCombine._right, dimAbsolute);
  292. Assert.Equal (20, dimCombine.Anchor (100));
  293. var view = new View { Frame = new Rectangle (20, 10, 20, 1) };
  294. var dimViewHeight = new Dim.DimView (view, Dimension.Height);
  295. Assert.Equal (1, dimViewHeight.Anchor (0));
  296. var dimViewWidth = new Dim.DimView (view, Dimension.Width);
  297. Assert.Equal (20, dimViewWidth.Anchor (0));
  298. view.Dispose ();
  299. }
  300. [Fact]
  301. public void New_Works ()
  302. {
  303. var dim = new Dim ();
  304. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  305. }
  306. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  307. // TODO: A new test that calls SetRelativeLayout directly is needed.
  308. [Fact]
  309. [AutoInitShutdown]
  310. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  311. {
  312. // Testing with the Button because it properly handles the Dim class.
  313. Toplevel t = new ();
  314. var w = new Window { Width = 100, Height = 100 };
  315. var f1 = new FrameView
  316. {
  317. X = 0,
  318. Y = 0,
  319. Width = Dim.Percent (50),
  320. Height = 5,
  321. Title = "f1"
  322. };
  323. var f2 = new FrameView
  324. {
  325. X = Pos.Right (f1),
  326. Y = 0,
  327. Width = Dim.Fill (),
  328. Height = 5,
  329. Title = "f2"
  330. };
  331. var v1 = new Button
  332. {
  333. X = Pos.X (f1) + 2,
  334. Y = Pos.Bottom (f1) + 2,
  335. Width = Dim.Width (f1) - 2,
  336. Height = Dim.Fill () - 2,
  337. ValidatePosDim = true,
  338. Text = "v1"
  339. };
  340. var v2 = new Button
  341. {
  342. X = Pos.X (f2) + 2,
  343. Y = Pos.Bottom (f2) + 2,
  344. Width = Dim.Width (f2) - 2,
  345. Height = Dim.Fill () - 2,
  346. ValidatePosDim = true,
  347. Text = "v2"
  348. };
  349. var v3 = new Button
  350. {
  351. Width = Dim.Percent (10),
  352. Height = Dim.Percent (10),
  353. ValidatePosDim = true,
  354. Text = "v3"
  355. };
  356. var v4 = new Button
  357. {
  358. Width = Dim.Sized (50),
  359. Height = Dim.Sized (50),
  360. ValidatePosDim = true,
  361. Text = "v4"
  362. };
  363. var v5 = new Button
  364. {
  365. Width = Dim.Width (v1) - Dim.Width (v3),
  366. Height = Dim.Height (v1) - Dim.Height (v3),
  367. ValidatePosDim = true,
  368. Text = "v5"
  369. };
  370. var v6 = new Button
  371. {
  372. X = Pos.X (f2),
  373. Y = Pos.Bottom (f2) + 2,
  374. Width = Dim.Percent (20, true),
  375. Height = Dim.Percent (20, true),
  376. ValidatePosDim = true,
  377. Text = "v6"
  378. };
  379. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  380. t.Add (w);
  381. t.Ready += (s, e) =>
  382. {
  383. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  384. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  385. Assert.Equal (100, w.Frame.Width);
  386. Assert.Equal (100, w.Frame.Height);
  387. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  388. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  389. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  390. Assert.Equal (5, f1.Frame.Height);
  391. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  392. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  393. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  394. Assert.Equal (5, f2.Frame.Height);
  395. #if DEBUG
  396. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  397. #else
  398. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  399. #endif
  400. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  401. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  402. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  403. #if DEBUG
  404. Assert.Equal (
  405. $"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))",
  406. v2.Width.ToString ()
  407. #else
  408. Assert.Equal (
  409. $"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))",
  410. v2.Width.ToString ()
  411. #endif
  412. );
  413. #if DEBUG
  414. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  415. #else
  416. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  417. #endif
  418. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  419. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  420. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  421. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  422. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  423. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  424. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  425. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  426. Assert.Equal (50, v4.Frame.Width);
  427. Assert.Equal (50, v4.Frame.Height);
  428. #if DEBUG
  429. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Viewport}))", v5.Width.ToString ());
  430. #else
  431. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Viewport}))", v5.Height.ToString ( ));
  432. #endif
  433. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  434. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  435. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  436. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  437. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  438. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  439. w.Width = 200;
  440. Assert.True (t.LayoutNeeded);
  441. w.Height = 200;
  442. t.LayoutSubviews ();
  443. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  444. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  445. Assert.Equal (200, w.Frame.Width);
  446. Assert.Equal (200, w.Frame.Height);
  447. f1.Text = "Frame1";
  448. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  449. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  450. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  451. Assert.Equal (5, f1.Frame.Height);
  452. f2.Text = "Frame2";
  453. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  454. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  455. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  456. Assert.Equal (5, f2.Frame.Height);
  457. v1.Text = "Button1";
  458. #if DEBUG
  459. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  460. #else
  461. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  462. #endif
  463. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  464. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  465. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  466. v2.Text = "Button2";
  467. #if DEBUG
  468. Assert.Equal ($"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  469. #else
  470. Assert.Equal ($"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  471. #endif
  472. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  473. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  474. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  475. v3.Text = "Button3";
  476. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  477. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  478. // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  479. Assert.Equal (19, v3.Frame.Width);
  480. // 199*10%=19
  481. Assert.Equal (19, v3.Frame.Height);
  482. v4.Text = "Button4";
  483. v4.AutoSize = true;
  484. Assert.Equal (Dim.Auto (DimAutoStyle.Text), v4.Width);
  485. Assert.Equal (Dim.Auto (DimAutoStyle.Text), v4.Height);
  486. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  487. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  488. v5.Text = "Button5";
  489. #if DEBUG
  490. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Frame}))", v5.Width.ToString ());
  491. Assert.Equal ($"Combine(View(Height,Button(v1){v1.Frame})-View(Height,Button(v3){v3.Frame}))", v5.Height.ToString ());
  492. #else
  493. Assert.Equal ($"Combine(View(Width,Button(){v1.Frame})-View(Width,Button(){v3.Frame}))", v5.Width.ToString ());
  494. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Frame}))", v5.Height.ToString ());
  495. #endif
  496. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  497. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  498. v6.Text = "Button6";
  499. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  500. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  501. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  502. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  503. };
  504. Application.Iteration += (s, a) => Application.RequestStop ();
  505. Application.Run (t);
  506. }
  507. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  508. // TODO: A new test that calls SetRelativeLayout directly is needed.
  509. [Fact]
  510. [TestRespondersDisposed]
  511. public void Referencing_SuperView_Does_Not_Throw ()
  512. {
  513. var super = new View { Width = 10, Height = 10, Text = "super" };
  514. var view = new View
  515. {
  516. Width = Dim.Width (super), // this is allowed
  517. Height = Dim.Height (super), // this is allowed
  518. Text = "view"
  519. };
  520. super.Add (view);
  521. super.BeginInit ();
  522. super.EndInit ();
  523. Exception exception = Record.Exception (super.LayoutSubviews);
  524. Assert.Null (exception);
  525. super.Dispose ();
  526. }
  527. [Fact]
  528. public void DimSized_Equals ()
  529. {
  530. var n1 = 0;
  531. var n2 = 0;
  532. Dim dim1 = Dim.Sized (n1);
  533. Dim dim2 = Dim.Sized (n2);
  534. Assert.Equal (dim1, dim2);
  535. n1 = n2 = 1;
  536. dim1 = Dim.Sized (n1);
  537. dim2 = Dim.Sized (n2);
  538. Assert.Equal (dim1, dim2);
  539. n1 = n2 = -1;
  540. dim1 = Dim.Sized (n1);
  541. dim2 = Dim.Sized (n2);
  542. Assert.Equal (dim1, dim2);
  543. n1 = 0;
  544. n2 = 1;
  545. dim1 = Dim.Sized (n1);
  546. dim2 = Dim.Sized (n2);
  547. Assert.NotEqual (dim1, dim2);
  548. }
  549. [Fact]
  550. public void DimSized_SetsValue ()
  551. {
  552. Dim dim = Dim.Sized (0);
  553. Assert.Equal ("Absolute(0)", dim.ToString ());
  554. var testVal = 5;
  555. dim = Dim.Sized (testVal);
  556. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  557. testVal = -1;
  558. dim = Dim.Sized (testVal);
  559. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  560. }
  561. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  562. // TODO: A new test that calls SetRelativeLayout directly is needed.
  563. [Fact]
  564. [TestRespondersDisposed]
  565. public void SyperView_Referencing_SubView_Throws ()
  566. {
  567. var super = new View { Width = 10, Height = 10, Text = "super" };
  568. var view2 = new View { Width = 10, Height = 10, Text = "view2" };
  569. var view = new View
  570. {
  571. Width = Dim.Width (view2), // this is not allowed
  572. Height = Dim.Height (view2), // this is not allowed
  573. Text = "view"
  574. };
  575. view.Add (view2);
  576. super.Add (view);
  577. super.BeginInit ();
  578. super.EndInit ();
  579. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  580. super.Dispose ();
  581. }
  582. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  583. // TODO: A new test that calls SetRelativeLayout directly is needed.
  584. [Fact]
  585. [TestRespondersDisposed]
  586. public void Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  587. {
  588. var t = new View { Width = 80, Height = 25, Text = "top" };
  589. var w = new Window
  590. {
  591. X = 1,
  592. Y = 2,
  593. Width = 4,
  594. Height = 5,
  595. Title = "w"
  596. };
  597. t.Add (w);
  598. t.LayoutSubviews ();
  599. Assert.Equal (3, w.Width = 3);
  600. Assert.Equal (4, w.Height = 4);
  601. t.Dispose ();
  602. }
  603. [Fact]
  604. [TestRespondersDisposed]
  605. public void DimWidth_Equals ()
  606. {
  607. var testRect1 = Rectangle.Empty;
  608. var view1 = new View { Frame = testRect1 };
  609. var testRect2 = Rectangle.Empty;
  610. var view2 = new View { Frame = testRect2 };
  611. Dim dim1 = Dim.Width (view1);
  612. Dim dim2 = Dim.Width (view1);
  613. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  614. Assert.Equal (dim1, dim2);
  615. dim2 = Dim.Width (view2);
  616. Assert.NotEqual (dim1, dim2);
  617. testRect1 = new Rectangle (0, 1, 2, 3);
  618. view1 = new View { Frame = testRect1 };
  619. testRect2 = new Rectangle (0, 1, 2, 3);
  620. dim1 = Dim.Width (view1);
  621. dim2 = Dim.Width (view1);
  622. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  623. Assert.Equal (dim1, dim2);
  624. testRect1 = new Rectangle (0, -1, 2, 3);
  625. view1 = new View { Frame = testRect1 };
  626. testRect2 = new Rectangle (0, -1, 2, 3);
  627. dim1 = Dim.Width (view1);
  628. dim2 = Dim.Width (view1);
  629. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  630. Assert.Equal (dim1, dim2);
  631. testRect1 = new Rectangle (0, -1, 2, 3);
  632. view1 = new View { Frame = testRect1 };
  633. testRect2 = Rectangle.Empty;
  634. view2 = new View { Frame = testRect2 };
  635. dim1 = Dim.Width (view1);
  636. dim2 = Dim.Width (view2);
  637. Assert.NotEqual (dim1, dim2);
  638. #if DEBUG_IDISPOSABLE
  639. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  640. Responder.Instances.Clear ();
  641. Assert.Empty (Responder.Instances);
  642. #endif
  643. }
  644. [Fact]
  645. public void DimWidth_Set_To_Null_Throws ()
  646. {
  647. Dim dim = Dim.Width (null);
  648. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  649. }
  650. [Fact]
  651. [TestRespondersDisposed]
  652. public void DimWidth_SetsValue ()
  653. {
  654. var testVal = Rectangle.Empty;
  655. var testValView = new View { Frame = testVal };
  656. Dim dim = Dim.Width (testValView);
  657. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  658. testValView.Dispose ();
  659. testVal = new Rectangle (1, 2, 3, 4);
  660. testValView = new View { Frame = testVal };
  661. dim = Dim.Width (testValView);
  662. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  663. testValView.Dispose ();
  664. }
  665. }