Dim.Tests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  124. {
  125. var t = new View { Width = 80, Height = 25, Text = "top" };
  126. var w = new Window
  127. {
  128. X = 1,
  129. Y = 2,
  130. Width = 4,
  131. Height = 5,
  132. Title = "w"
  133. };
  134. t.Add (w);
  135. t.LayoutSubviews ();
  136. Assert.Equal (3, w.Width = 3);
  137. Assert.Equal (4, w.Height = 4);
  138. t.Dispose ();
  139. }
  140. [Fact]
  141. public void DimHeight_Set_To_Null_Throws ()
  142. {
  143. Dim dim = Dim.Height (null);
  144. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  145. }
  146. [Fact]
  147. [TestRespondersDisposed]
  148. public void DimHeight_SetsValue ()
  149. {
  150. var testVal = Rectangle.Empty;
  151. var testValview = new View { Frame = testVal };
  152. Dim dim = Dim.Height (testValview);
  153. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  154. testValview.Dispose ();
  155. testVal = new Rectangle (1, 2, 3, 4);
  156. testValview = new View { Frame = testVal };
  157. dim = Dim.Height (testValview);
  158. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  159. testValview.Dispose ();
  160. }
  161. [Fact]
  162. [TestRespondersDisposed]
  163. public void Internal_Tests ()
  164. {
  165. var dimFactor = new DimPercent (10);
  166. Assert.Equal (10, dimFactor.GetAnchor (100));
  167. var dimAbsolute = new DimAbsolute (10);
  168. Assert.Equal (10, dimAbsolute.GetAnchor (0));
  169. var dimFill = new DimFill (1);
  170. Assert.Equal (99, dimFill.GetAnchor (100));
  171. var dimCombine = new DimCombine (AddOrSubtract.Add, dimFactor, dimAbsolute);
  172. Assert.Equal (dimCombine.Left, dimFactor);
  173. Assert.Equal (dimCombine.Right, dimAbsolute);
  174. Assert.Equal (20, dimCombine.GetAnchor (100));
  175. var view = new View { Frame = new Rectangle (20, 10, 20, 1) };
  176. var dimViewHeight = new DimView (view, Dimension.Height);
  177. Assert.Equal (1, dimViewHeight.GetAnchor (0));
  178. var dimViewWidth = new DimView (view, Dimension.Width);
  179. Assert.Equal (20, dimViewWidth.GetAnchor (0));
  180. view.Dispose ();
  181. }
  182. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  183. // TODO: A new test that calls SetRelativeLayout directly is needed.
  184. [Fact]
  185. [AutoInitShutdown]
  186. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  187. {
  188. // Testing with the Button because it properly handles the Dim class.
  189. Toplevel t = new ();
  190. var w = new Window { Width = 100, Height = 100 };
  191. var f1 = new FrameView
  192. {
  193. X = 0,
  194. Y = 0,
  195. Width = Dim.Percent (50),
  196. Height = 5,
  197. Title = "f1"
  198. };
  199. var f2 = new FrameView
  200. {
  201. X = Pos.Right (f1),
  202. Y = 0,
  203. Width = Dim.Fill (),
  204. Height = 5,
  205. Title = "f2"
  206. };
  207. var v1 = new Button
  208. {
  209. X = Pos.X (f1) + 2,
  210. Y = Pos.Bottom (f1) + 2,
  211. Width = Dim.Width (f1) - 2,
  212. Height = Dim.Fill () - 2,
  213. ValidatePosDim = true,
  214. Text = "v1"
  215. };
  216. var v2 = new Button
  217. {
  218. X = Pos.X (f2) + 2,
  219. Y = Pos.Bottom (f2) + 2,
  220. Width = Dim.Width (f2) - 2,
  221. Height = Dim.Fill () - 2,
  222. ValidatePosDim = true,
  223. Text = "v2"
  224. };
  225. var v3 = new Button
  226. {
  227. Width = Dim.Percent (10),
  228. Height = Dim.Percent (10),
  229. ValidatePosDim = true,
  230. Text = "v3"
  231. };
  232. var v4 = new Button
  233. {
  234. Width = Dim.Absolute (50),
  235. Height = Dim.Absolute (50),
  236. ValidatePosDim = true,
  237. Text = "v4"
  238. };
  239. var v5 = new Button
  240. {
  241. Width = Dim.Width (v1) - Dim.Width (v3),
  242. Height = Dim.Height (v1) - Dim.Height (v3),
  243. ValidatePosDim = true,
  244. Text = "v5"
  245. };
  246. var v6 = new Button
  247. {
  248. X = Pos.X (f2),
  249. Y = Pos.Bottom (f2) + 2,
  250. Width = Dim.Percent (20, DimPercentMode.Position),
  251. Height = Dim.Percent (20, DimPercentMode.Position),
  252. ValidatePosDim = true,
  253. Text = "v6"
  254. };
  255. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  256. t.Add (w);
  257. t.Ready += (s, e) =>
  258. {
  259. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  260. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  261. Assert.Equal (100, w.Frame.Width);
  262. Assert.Equal (100, w.Frame.Height);
  263. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  264. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  265. Assert.Equal (5, f1.Frame.Height);
  266. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  267. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  268. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  269. Assert.Equal (5, f2.Frame.Height);
  270. #if DEBUG
  271. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  272. #else
  273. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  274. #endif
  275. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  276. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  277. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  278. #if DEBUG
  279. Assert.Equal (
  280. $"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))",
  281. v2.Width.ToString ()
  282. #else
  283. Assert.Equal (
  284. $"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))",
  285. v2.Width.ToString ()
  286. #endif
  287. );
  288. #if DEBUG
  289. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  290. #else
  291. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  292. #endif
  293. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  294. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  295. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  296. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  297. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  298. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  299. Assert.Equal (50, v4.Frame.Width);
  300. Assert.Equal (50, v4.Frame.Height);
  301. #if DEBUG
  302. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Viewport}))", v5.Width.ToString ());
  303. #else
  304. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Viewport}))", v5.Height.ToString ( ));
  305. #endif
  306. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  307. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  308. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  309. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  310. w.Width = 200;
  311. Assert.True (t.LayoutNeeded);
  312. w.Height = 200;
  313. t.LayoutSubviews ();
  314. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  315. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  316. Assert.Equal (200, w.Frame.Width);
  317. Assert.Equal (200, w.Frame.Height);
  318. f1.Text = "Frame1";
  319. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  320. Assert.Equal (5, f1.Frame.Height);
  321. f2.Text = "Frame2";
  322. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  323. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  324. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  325. Assert.Equal (5, f2.Frame.Height);
  326. v1.Text = "Button1";
  327. #if DEBUG
  328. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  329. #else
  330. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  331. #endif
  332. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  333. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  334. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  335. v2.Text = "Button2";
  336. #if DEBUG
  337. Assert.Equal ($"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  338. #else
  339. Assert.Equal ($"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  340. #endif
  341. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  342. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  343. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  344. v3.Text = "Button3";
  345. // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  346. Assert.Equal (19, v3.Frame.Width);
  347. // 199*10%=19
  348. Assert.Equal (19, v3.Frame.Height);
  349. v4.Text = "Button4";
  350. v4.Width = Auto(DimAutoStyle.Text);
  351. v4.Height = Auto (DimAutoStyle.Text);
  352. Assert.Equal (Dim.Auto (DimAutoStyle.Text), v4.Width);
  353. Assert.Equal (Dim.Auto (DimAutoStyle.Text), v4.Height);
  354. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is DimAbsolute
  355. Assert.Equal (1, v4.Frame.Height); // 1 because is DimAbsolute
  356. v5.Text = "Button5";
  357. #if DEBUG
  358. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Frame}))", v5.Width.ToString ());
  359. Assert.Equal ($"Combine(View(Height,Button(v1){v1.Frame})-View(Height,Button(v3){v3.Frame}))", v5.Height.ToString ());
  360. #else
  361. Assert.Equal ($"Combine(View(Width,Button(){v1.Frame})-View(Width,Button(){v3.Frame}))", v5.Width.ToString ());
  362. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Frame}))", v5.Height.ToString ());
  363. #endif
  364. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  365. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  366. v6.Text = "Button6";
  367. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  368. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  369. };
  370. Application.Iteration += (s, a) => Application.RequestStop ();
  371. Application.Run (t);
  372. }
  373. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  374. // TODO: A new test that calls SetRelativeLayout directly is needed.
  375. [Fact]
  376. [TestRespondersDisposed]
  377. public void Referencing_SuperView_Does_Not_Throw ()
  378. {
  379. var super = new View { Width = 10, Height = 10, Text = "super" };
  380. var view = new View
  381. {
  382. Width = Dim.Width (super), // this is allowed
  383. Height = Dim.Height (super), // this is allowed
  384. Text = "view"
  385. };
  386. super.Add (view);
  387. super.BeginInit ();
  388. super.EndInit ();
  389. Exception exception = Record.Exception (super.LayoutSubviews);
  390. Assert.Null (exception);
  391. super.Dispose ();
  392. }
  393. [Fact]
  394. public void DimSized_Equals ()
  395. {
  396. var n1 = 0;
  397. var n2 = 0;
  398. Dim dim1 = Dim.Absolute (n1);
  399. Dim dim2 = Dim.Absolute (n2);
  400. Assert.Equal (dim1, dim2);
  401. n1 = n2 = 1;
  402. dim1 = Dim.Absolute (n1);
  403. dim2 = Dim.Absolute (n2);
  404. Assert.Equal (dim1, dim2);
  405. n1 = n2 = -1;
  406. dim1 = Dim.Absolute (n1);
  407. dim2 = Dim.Absolute (n2);
  408. Assert.Equal (dim1, dim2);
  409. n1 = 0;
  410. n2 = 1;
  411. dim1 = Dim.Absolute (n1);
  412. dim2 = Dim.Absolute (n2);
  413. Assert.NotEqual (dim1, dim2);
  414. }
  415. [Fact]
  416. public void DimSized_SetsValue ()
  417. {
  418. Dim dim = Dim.Absolute (0);
  419. Assert.Equal ("Absolute(0)", dim.ToString ());
  420. var testVal = 5;
  421. dim = Dim.Absolute (testVal);
  422. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  423. testVal = -1;
  424. dim = Dim.Absolute (testVal);
  425. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  426. }
  427. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  428. // TODO: A new test that calls SetRelativeLayout directly is needed.
  429. [Fact]
  430. [TestRespondersDisposed]
  431. public void SyperView_Referencing_SubView_Throws ()
  432. {
  433. var super = new View { Width = 10, Height = 10, Text = "super" };
  434. var view2 = new View { Width = 10, Height = 10, Text = "view2" };
  435. var view = new View
  436. {
  437. Width = Dim.Width (view2), // this is not allowed
  438. Height = Dim.Height (view2), // this is not allowed
  439. Text = "view"
  440. };
  441. view.Add (view2);
  442. super.Add (view);
  443. super.BeginInit ();
  444. super.EndInit ();
  445. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  446. super.Dispose ();
  447. }
  448. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  449. // TODO: A new test that calls SetRelativeLayout directly is needed.
  450. [Fact]
  451. [TestRespondersDisposed]
  452. public void Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  453. {
  454. var t = new View { Width = 80, Height = 25, Text = "top" };
  455. var w = new Window
  456. {
  457. X = 1,
  458. Y = 2,
  459. Width = 4,
  460. Height = 5,
  461. Title = "w"
  462. };
  463. t.Add (w);
  464. t.LayoutSubviews ();
  465. Assert.Equal (3, w.Width = 3);
  466. Assert.Equal (4, w.Height = 4);
  467. t.Dispose ();
  468. }
  469. [Fact]
  470. [TestRespondersDisposed]
  471. public void DimWidth_Equals ()
  472. {
  473. var testRect1 = Rectangle.Empty;
  474. var view1 = new View { Frame = testRect1 };
  475. var testRect2 = Rectangle.Empty;
  476. var view2 = new View { Frame = testRect2 };
  477. Dim dim1 = Dim.Width (view1);
  478. Dim dim2 = Dim.Width (view1);
  479. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  480. Assert.Equal (dim1, dim2);
  481. dim2 = Dim.Width (view2);
  482. Assert.NotEqual (dim1, dim2);
  483. testRect1 = new Rectangle (0, 1, 2, 3);
  484. view1 = new View { Frame = testRect1 };
  485. testRect2 = new Rectangle (0, 1, 2, 3);
  486. dim1 = Dim.Width (view1);
  487. dim2 = Dim.Width (view1);
  488. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  489. Assert.Equal (dim1, dim2);
  490. testRect1 = new Rectangle (0, -1, 2, 3);
  491. view1 = new View { Frame = testRect1 };
  492. testRect2 = new Rectangle (0, -1, 2, 3);
  493. dim1 = Dim.Width (view1);
  494. dim2 = Dim.Width (view1);
  495. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  496. Assert.Equal (dim1, dim2);
  497. testRect1 = new Rectangle (0, -1, 2, 3);
  498. view1 = new View { Frame = testRect1 };
  499. testRect2 = Rectangle.Empty;
  500. view2 = new View { Frame = testRect2 };
  501. dim1 = Dim.Width (view1);
  502. dim2 = Dim.Width (view2);
  503. Assert.NotEqual (dim1, dim2);
  504. #if DEBUG_IDISPOSABLE
  505. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  506. Responder.Instances.Clear ();
  507. Assert.Empty (Responder.Instances);
  508. #endif
  509. }
  510. [Fact]
  511. public void DimWidth_Set_To_Null_Throws ()
  512. {
  513. Dim dim = Dim.Width (null);
  514. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  515. }
  516. [Fact]
  517. [TestRespondersDisposed]
  518. public void DimWidth_SetsValue ()
  519. {
  520. var testVal = Rectangle.Empty;
  521. var testValView = new View { Frame = testVal };
  522. Dim dim = Dim.Width (testValView);
  523. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  524. testValView.Dispose ();
  525. testVal = new Rectangle (1, 2, 3, 4);
  526. testValView = new View { Frame = testVal };
  527. dim = Dim.Width (testValView);
  528. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  529. testValView.Dispose ();
  530. }
  531. }