2
0

Dim.Tests.cs 25 KB

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