DimTests.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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.ViewTests;
  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, 50, false);
  25. Assert.Equal (10, result);
  26. }
  27. [Fact]
  28. public void DimCombine_Calculate_ReturnsCorrectValue ()
  29. {
  30. var dim1 = new DimAbsolute (10);
  31. var dim2 = new DimAbsolute (20);
  32. var dim = dim1 + dim2;
  33. var result = dim.Calculate (0, 100, null, Dim.Dimension.None, 50, false);
  34. Assert.Equal (30, result);
  35. }
  36. [Fact]
  37. public void DimFactor_Calculate_ReturnsCorrectValue ()
  38. {
  39. var dim = new DimFactor (0.5f);
  40. var result = dim.Calculate (0, 100, null, Dim.Dimension.None, 50, false);
  41. Assert.Equal (50, result);
  42. }
  43. [Fact]
  44. public void DimFill_Calculate_ReturnsCorrectValue ()
  45. {
  46. var dim = Dim.Fill ();
  47. var result = dim.Calculate (0, 100, null, Dim.Dimension.None, 50, false);
  48. Assert.Equal (100, result);
  49. }
  50. [Fact]
  51. public void DimFunc_Calculate_ReturnsCorrectValue ()
  52. {
  53. var dim = new DimFunc (() => 10);
  54. var result = dim.Calculate (0, 100, null, Dim.Dimension.None, 50, false);
  55. Assert.Equal (10, result);
  56. }
  57. [Fact]
  58. public void DimView_Calculate_ReturnsCorrectValue ()
  59. {
  60. var view = new View { Width = 10 };
  61. var dim = new DimView (view, Dimension.Width);
  62. var result = dim.Calculate (0, 100, null, Dim.Dimension.None, 50, false);
  63. Assert.Equal (10, result);
  64. }
  65. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  66. // A new test that does not depend on Application is needed.
  67. [Fact]
  68. [AutoInitShutdown]
  69. public void Add_Operator ()
  70. {
  71. Toplevel top = new ();
  72. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  73. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  74. var count = 0;
  75. field.KeyDown += (s, k) =>
  76. {
  77. if (k.KeyCode == KeyCode.Enter)
  78. {
  79. field.Text = $"Label {count}";
  80. var label = new Label { X = 0, Y = view.Viewport.Height, /*Width = 20,*/ Text = field.Text };
  81. view.Add (label);
  82. Assert.Equal ($"Label {count}", label.Text);
  83. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  84. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  85. view.Height += 1;
  86. count++;
  87. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  88. }
  89. };
  90. Application.Iteration += (s, a) =>
  91. {
  92. while (count < 20)
  93. {
  94. field.NewKeyDownEvent (Key.Enter);
  95. }
  96. Application.RequestStop ();
  97. };
  98. var win = new Window ();
  99. win.Add (view);
  100. win.Add (field);
  101. top.Add (win);
  102. Application.Run (top);
  103. top.Dispose ();
  104. Assert.Equal (20, count);
  105. }
  106. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  107. // TODO: A new test that calls SetRelativeLayout directly is needed.
  108. [Fact]
  109. [TestRespondersDisposed]
  110. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  111. {
  112. var super = new View { Width = 10, Height = 10, Text = "super" };
  113. var view = new View
  114. {
  115. Width = Dim.Width (super), // this is allowed
  116. Height = Dim.Height (super), // this is allowed
  117. Text = "view"
  118. };
  119. super.Add (view);
  120. super.BeginInit ();
  121. super.EndInit ();
  122. Exception exception = Record.Exception (super.LayoutSubviews);
  123. Assert.Null (exception);
  124. super.Dispose ();
  125. }
  126. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  127. // TODO: A new test that calls SetRelativeLayout directly is needed.
  128. [Fact]
  129. [AutoInitShutdown]
  130. public void Dim_Subtract_Operator ()
  131. {
  132. Toplevel top = new ();
  133. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  134. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  135. var count = 20;
  136. List<Label> listLabels = new ();
  137. for (var i = 0; i < count; i++)
  138. {
  139. field.Text = $"Label {i}";
  140. var label = new Label { X = 0, Y = view.Viewport.Height, /*Width = 20,*/ Text = field.Text };
  141. view.Add (label);
  142. Assert.Equal ($"Label {i}", label.Text);
  143. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  144. listLabels.Add (label);
  145. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  146. view.Height += 1;
  147. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  148. }
  149. field.KeyDown += (s, k) =>
  150. {
  151. if (k.KeyCode == KeyCode.Enter)
  152. {
  153. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  154. view.Remove (listLabels [count - 1]);
  155. listLabels [count - 1].Dispose ();
  156. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  157. view.Height -= 1;
  158. count--;
  159. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  160. }
  161. };
  162. Application.Iteration += (s, a) =>
  163. {
  164. while (count > 0)
  165. {
  166. field.NewKeyDownEvent (Key.Enter);
  167. }
  168. Application.RequestStop ();
  169. };
  170. var win = new Window ();
  171. win.Add (view);
  172. win.Add (field);
  173. top.Add (win);
  174. Application.Run (top);
  175. Assert.Equal (0, count);
  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_SyperView_Referencing_SubView_Throws ()
  182. {
  183. var super = new View { Width = 10, Height = 10, Text = "super" };
  184. var view2 = new View { Width = 10, Height = 10, Text = "view2" };
  185. var view = new View
  186. {
  187. Width = Dim.Width (view2), // this is not allowed
  188. Height = Dim.Height (view2), // this is not allowed
  189. Text = "view"
  190. };
  191. view.Add (view2);
  192. super.Add (view);
  193. super.BeginInit ();
  194. super.EndInit ();
  195. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  196. super.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
  203. Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  204. {
  205. var t = new View { Width = 80, Height = 25, Text = "top" };
  206. var w = new Window { Width = Dim.Fill (), Height = Dim.Sized (10) };
  207. var v = new View { Width = Dim.Width (w) - 2, Height = Dim.Percent (10), Text = "v" };
  208. w.Add (v);
  209. t.Add (w);
  210. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  211. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  212. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  213. t.LayoutSubviews ();
  214. Assert.Equal (2, v.Width = 2);
  215. Assert.Equal (2, v.Height = 2);
  216. // Force v to be LayoutStyle.Absolute;
  217. v.Frame = new Rectangle (0, 1, 3, 4);
  218. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  219. t.LayoutSubviews ();
  220. Assert.Equal (2, v.Width = 2);
  221. Assert.Equal (2, v.Height = 2);
  222. t.Dispose ();
  223. }
  224. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  225. // TODO: A new test that calls SetRelativeLayout directly is needed.
  226. [Fact]
  227. [TestRespondersDisposed]
  228. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  229. {
  230. var t = new View { Width = 80, Height = 25, Text = "top" };
  231. var w = new Window
  232. {
  233. X = 1,
  234. Y = 2,
  235. Width = 4,
  236. Height = 5,
  237. Title = "w"
  238. };
  239. t.Add (w);
  240. t.LayoutSubviews ();
  241. Assert.Equal (3, w.Width = 3);
  242. Assert.Equal (4, w.Height = 4);
  243. t.Dispose ();
  244. }
  245. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  246. // TODO: A new test that calls SetRelativeLayout directly is needed.
  247. [Fact]
  248. [TestRespondersDisposed]
  249. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  250. {
  251. var t = new View { Width = 80, Height = 25, Text = "top" };
  252. var w = new Window
  253. {
  254. Width = Dim.Width (t) - 2, // 78
  255. Height = Dim.Height (t) - 2 // 23
  256. };
  257. var f = new FrameView ();
  258. var v1 = new View
  259. {
  260. Width = Dim.Width (w) - 2, // 76
  261. Height = Dim.Height (w) - 2 // 21
  262. };
  263. var v2 = new View
  264. {
  265. Width = Dim.Width (v1) - 2, // 74
  266. Height = Dim.Height (v1) - 2 // 19
  267. };
  268. f.Add (v1, v2);
  269. w.Add (f);
  270. t.Add (w);
  271. t.BeginInit ();
  272. t.EndInit ();
  273. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  274. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  275. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  276. Exception exception = Record.Exception (t.LayoutSubviews);
  277. Assert.Null (exception);
  278. Assert.Equal (80, t.Frame.Width);
  279. Assert.Equal (25, t.Frame.Height);
  280. Assert.Equal (78, w.Frame.Width);
  281. Assert.Equal (23, w.Frame.Height);
  282. Assert.Equal (6, f.Frame.Width);
  283. Assert.Equal (6, f.Frame.Height);
  284. Assert.Equal (76, v1.Frame.Width);
  285. Assert.Equal (21, v1.Frame.Height);
  286. Assert.Equal (74, v2.Frame.Width);
  287. Assert.Equal (19, v2.Frame.Height);
  288. t.Dispose ();
  289. }
  290. // See #2461
  291. //[Fact]
  292. //public void Dim_Referencing_SuperView_Throws ()
  293. //{
  294. // var super = new View ("super") {
  295. // Width = 10,
  296. // Height = 10
  297. // };
  298. // var view = new View ("view") {
  299. // Width = Dim.Width (super), // this is not allowed
  300. // Height = Dim.Height (super), // this is not allowed
  301. // };
  302. // super.Add (view);
  303. // super.BeginInit ();
  304. // super.EndInit ();
  305. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  306. //}
  307. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  308. // TODO: A new test that calls SetRelativeLayout directly is needed.
  309. /// <summary>This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461</summary>
  310. [Fact]
  311. [TestRespondersDisposed]
  312. public void Combine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  313. {
  314. var t = new View { Width = 80, Height = 25 };
  315. var w = new Window
  316. {
  317. Width = Dim.Width (t) - 2, // 78
  318. Height = Dim.Height (t) - 2 // 23
  319. };
  320. var f = new FrameView ();
  321. var v1 = new View
  322. {
  323. Width = Dim.Width (w) - 2, // 76
  324. Height = Dim.Height (w) - 2 // 21
  325. };
  326. var v2 = new View
  327. {
  328. Width = Dim.Width (v1) - 2, // 74
  329. Height = Dim.Height (v1) - 2 // 19
  330. };
  331. f.Add (v1, v2);
  332. w.Add (f);
  333. t.Add (w);
  334. t.BeginInit ();
  335. t.EndInit ();
  336. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  337. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  338. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  339. Assert.Equal (80, t.Frame.Width);
  340. Assert.Equal (25, t.Frame.Height);
  341. Assert.Equal (78, w.Frame.Width);
  342. Assert.Equal (23, w.Frame.Height);
  343. Assert.Equal (6, f.Frame.Width);
  344. Assert.Equal (6, f.Frame.Height);
  345. Assert.Equal (76, v1.Frame.Width);
  346. Assert.Equal (21, v1.Frame.Height);
  347. Assert.Equal (74, v2.Frame.Width);
  348. Assert.Equal (19, v2.Frame.Height);
  349. t.Dispose ();
  350. }
  351. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  352. // TODO: A new test that calls SetRelativeLayout directly is needed.
  353. [Fact]
  354. [TestRespondersDisposed]
  355. public void Combine_View_Not_Added_Throws ()
  356. {
  357. var t = new View { Width = 80, Height = 50 };
  358. var super = new View { Width = Dim.Width (t) - 2, Height = Dim.Height (t) - 2 };
  359. t.Add (super);
  360. var sub = new View ();
  361. super.Add (sub);
  362. var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 };
  363. var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 };
  364. sub.Add (v1);
  365. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  366. sub.Width = Dim.Fill () - Dim.Width (v2);
  367. sub.Height = Dim.Fill () - Dim.Height (v2);
  368. t.BeginInit ();
  369. t.EndInit ();
  370. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  371. t.Dispose ();
  372. v2.Dispose ();
  373. }
  374. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  375. // TODO: A new test that calls SetRelativeLayout directly is needed.
  376. [Fact]
  377. [TestRespondersDisposed]
  378. public void
  379. Dim_Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  380. {
  381. var t = new View { Width = 80, Height = 25, Text = "top" };
  382. var w = new Window { Width = Dim.Fill (), Height = Dim.Sized (10) };
  383. var v = new View { Width = Dim.Width (w) - 2, Height = Dim.Percent (10), Text = "v" };
  384. w.Add (v);
  385. t.Add (w);
  386. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  387. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  388. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  389. t.LayoutSubviews ();
  390. Assert.Equal (2, v.Width = 2);
  391. Assert.Equal (2, v.Height = 2);
  392. // Force v to be LayoutStyle.Absolute;
  393. v.Frame = new Rectangle (0, 1, 3, 4);
  394. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  395. t.LayoutSubviews ();
  396. Assert.Equal (2, v.Width = 2);
  397. Assert.Equal (2, v.Height = 2);
  398. t.Dispose ();
  399. }
  400. [Fact]
  401. public void Fill_Equal ()
  402. {
  403. var margin1 = 0;
  404. var margin2 = 0;
  405. Dim dim1 = Dim.Fill (margin1);
  406. Dim dim2 = Dim.Fill (margin2);
  407. Assert.Equal (dim1, dim2);
  408. }
  409. // Tests that Dim.Fill honors the margin parameter correctly
  410. [Theory]
  411. [InlineData (0, true, 25)]
  412. [InlineData (0, false, 25)]
  413. [InlineData (1, true, 24)]
  414. [InlineData (1, false, 24)]
  415. [InlineData (2, true, 23)]
  416. [InlineData (2, false, 23)]
  417. [InlineData (-2, true, 27)]
  418. [InlineData (-2, false, 27)]
  419. public void Fill_Margin (int margin, bool width, int expected)
  420. {
  421. var super = new View { Width = 25, Height = 25 };
  422. var view = new View
  423. {
  424. X = 0,
  425. Y = 0,
  426. Width = width ? Dim.Fill (margin) : 1,
  427. Height = width ? 1 : Dim.Fill (margin)
  428. };
  429. super.Add (view);
  430. super.BeginInit ();
  431. super.EndInit ();
  432. super.LayoutSubviews ();
  433. Assert.Equal (25, super.Frame.Width);
  434. Assert.Equal (25, super.Frame.Height);
  435. if (width)
  436. {
  437. Assert.Equal (expected, view.Frame.Width);
  438. Assert.Equal (1, view.Frame.Height);
  439. }
  440. else
  441. {
  442. Assert.Equal (1, view.Frame.Width);
  443. Assert.Equal (expected, view.Frame.Height);
  444. }
  445. }
  446. // Tests that Dim.Fill fills the dimension REMAINING from the View's X position to the end of the super view's width
  447. [Theory]
  448. [InlineData (0, true, 25)]
  449. [InlineData (0, false, 25)]
  450. [InlineData (1, true, 24)]
  451. [InlineData (1, false, 24)]
  452. [InlineData (2, true, 23)]
  453. [InlineData (2, false, 23)]
  454. [InlineData (-2, true, 27)]
  455. [InlineData (-2, false, 27)]
  456. public void Fill_Offset (int offset, bool width, int expected)
  457. {
  458. var super = new View { Width = 25, Height = 25 };
  459. var view = new View
  460. {
  461. X = width ? offset : 0,
  462. Y = width ? 0 : offset,
  463. Width = width ? Dim.Fill () : 1,
  464. Height = width ? 1 : Dim.Fill ()
  465. };
  466. super.Add (view);
  467. super.BeginInit ();
  468. super.EndInit ();
  469. super.LayoutSubviews ();
  470. Assert.Equal (25, super.Frame.Width);
  471. Assert.Equal (25, super.Frame.Height);
  472. if (width)
  473. {
  474. Assert.Equal (expected, view.Frame.Width);
  475. Assert.Equal (1, view.Frame.Height);
  476. }
  477. else
  478. {
  479. Assert.Equal (1, view.Frame.Width);
  480. Assert.Equal (expected, view.Frame.Height);
  481. }
  482. }
  483. // TODO: Other Dim.Height tests (e.g. Equal?)
  484. [Fact]
  485. public void Fill_SetsValue ()
  486. {
  487. var testMargin = 0;
  488. Dim dim = Dim.Fill ();
  489. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  490. testMargin = 0;
  491. dim = Dim.Fill (testMargin);
  492. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  493. testMargin = 5;
  494. dim = Dim.Fill (testMargin);
  495. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  496. }
  497. [Fact]
  498. public void Function_Equal ()
  499. {
  500. Func<int> f1 = () => 0;
  501. Func<int> f2 = () => 0;
  502. Dim dim1 = Dim.Function (f1);
  503. Dim dim2 = Dim.Function (f2);
  504. Assert.Equal (dim1, dim2);
  505. f2 = () => 1;
  506. dim2 = Dim.Function (f2);
  507. Assert.NotEqual (dim1, dim2);
  508. }
  509. [Fact]
  510. public void Function_SetsValue ()
  511. {
  512. var text = "Test";
  513. Dim dim = Dim.Function (() => text.Length);
  514. Assert.Equal ("DimFunc(4)", dim.ToString ());
  515. text = "New Test";
  516. Assert.Equal ("DimFunc(8)", dim.ToString ());
  517. text = "";
  518. Assert.Equal ("DimFunc(0)", dim.ToString ());
  519. }
  520. [Fact]
  521. public void Height_Set_To_Null_Throws ()
  522. {
  523. Dim dim = Dim.Height (null);
  524. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  525. }
  526. [Fact]
  527. [TestRespondersDisposed]
  528. public void Height_SetsValue ()
  529. {
  530. var testVal = Rectangle.Empty;
  531. var testValview = new View { Frame = testVal };
  532. Dim dim = Dim.Height (testValview);
  533. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  534. testValview.Dispose ();
  535. testVal = new Rectangle (1, 2, 3, 4);
  536. testValview = new View { Frame = testVal };
  537. dim = Dim.Height (testValview);
  538. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  539. testValview.Dispose ();
  540. }
  541. [Fact]
  542. [TestRespondersDisposed]
  543. public void Internal_Tests ()
  544. {
  545. var dimFactor = new Dim.DimFactor (0.10F);
  546. Assert.Equal (10, dimFactor.Anchor (100));
  547. var dimAbsolute = new Dim.DimAbsolute (10);
  548. Assert.Equal (10, dimAbsolute.Anchor (0));
  549. var dimFill = new Dim.DimFill (1);
  550. Assert.Equal (99, dimFill.Anchor (100));
  551. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  552. Assert.Equal (dimCombine._left, dimFactor);
  553. Assert.Equal (dimCombine._right, dimAbsolute);
  554. Assert.Equal (20, dimCombine.Anchor (100));
  555. var view = new View { Frame = new Rectangle (20, 10, 20, 1) };
  556. var dimViewHeight = new Dim.DimView (view, Dimension.Height);
  557. Assert.Equal (1, dimViewHeight.Anchor (0));
  558. var dimViewWidth = new Dim.DimView (view, Dimension.Width);
  559. Assert.Equal (20, dimViewWidth.Anchor (0));
  560. view.Dispose ();
  561. }
  562. [Fact]
  563. public void New_Works ()
  564. {
  565. var dim = new Dim ();
  566. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  567. }
  568. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  569. // TODO: A new test that calls SetRelativeLayout directly is needed.
  570. [Fact]
  571. [AutoInitShutdown]
  572. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  573. {
  574. // Testing with the Button because it properly handles the Dim class.
  575. Toplevel t = new ();
  576. var w = new Window { Width = 100, Height = 100 };
  577. var f1 = new FrameView
  578. {
  579. X = 0,
  580. Y = 0,
  581. Width = Dim.Percent (50),
  582. Height = 5,
  583. Title = "f1"
  584. };
  585. var f2 = new FrameView
  586. {
  587. X = Pos.Right (f1),
  588. Y = 0,
  589. Width = Dim.Fill (),
  590. Height = 5,
  591. Title = "f2"
  592. };
  593. var v1 = new Button
  594. {
  595. AutoSize = false,
  596. X = Pos.X (f1) + 2,
  597. Y = Pos.Bottom (f1) + 2,
  598. Width = Dim.Width (f1) - 2,
  599. Height = Dim.Fill () - 2,
  600. ValidatePosDim = true,
  601. Text = "v1"
  602. };
  603. var v2 = new Button
  604. {
  605. AutoSize = false,
  606. X = Pos.X (f2) + 2,
  607. Y = Pos.Bottom (f2) + 2,
  608. Width = Dim.Width (f2) - 2,
  609. Height = Dim.Fill () - 2,
  610. ValidatePosDim = true,
  611. Text = "v2"
  612. };
  613. var v3 = new Button
  614. {
  615. AutoSize = false,
  616. Width = Dim.Percent (10),
  617. Height = Dim.Percent (10),
  618. ValidatePosDim = true,
  619. Text = "v3"
  620. };
  621. var v4 = new Button
  622. {
  623. AutoSize = false,
  624. Width = Dim.Sized (50),
  625. Height = Dim.Sized (50),
  626. ValidatePosDim = true,
  627. Text = "v4"
  628. };
  629. var v5 = new Button
  630. {
  631. AutoSize = false,
  632. Width = Dim.Width (v1) - Dim.Width (v3),
  633. Height = Dim.Height (v1) - Dim.Height (v3),
  634. ValidatePosDim = true,
  635. Text = "v5"
  636. };
  637. var v6 = new Button
  638. {
  639. AutoSize = false,
  640. X = Pos.X (f2),
  641. Y = Pos.Bottom (f2) + 2,
  642. Width = Dim.Percent (20, true),
  643. Height = Dim.Percent (20, true),
  644. ValidatePosDim = true,
  645. Text = "v6"
  646. };
  647. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  648. t.Add (w);
  649. t.Ready += (s, e) =>
  650. {
  651. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  652. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  653. Assert.Equal (100, w.Frame.Width);
  654. Assert.Equal (100, w.Frame.Height);
  655. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  656. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  657. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  658. Assert.Equal (5, f1.Frame.Height);
  659. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  660. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  661. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  662. Assert.Equal (5, f2.Frame.Height);
  663. #if DEBUG
  664. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  665. #else
  666. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  667. #endif
  668. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  669. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  670. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  671. #if DEBUG
  672. Assert.Equal (
  673. $"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))",
  674. v2.Width.ToString ()
  675. #else
  676. Assert.Equal (
  677. $"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))",
  678. v2.Width.ToString ()
  679. #endif
  680. );
  681. #if DEBUG
  682. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  683. #else
  684. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  685. #endif
  686. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  687. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  688. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  689. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  690. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  691. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  692. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  693. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  694. Assert.Equal (50, v4.Frame.Width);
  695. Assert.Equal (50, v4.Frame.Height);
  696. #if DEBUG
  697. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Viewport}))", v5.Width.ToString ());
  698. #else
  699. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Viewport}))", v5.Height.ToString ( ));
  700. #endif
  701. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  702. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  703. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  704. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  705. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  706. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  707. w.Width = 200;
  708. Assert.True (t.LayoutNeeded);
  709. w.Height = 200;
  710. t.LayoutSubviews ();
  711. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  712. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  713. Assert.Equal (200, w.Frame.Width);
  714. Assert.Equal (200, w.Frame.Height);
  715. f1.Text = "Frame1";
  716. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  717. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  718. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  719. Assert.Equal (5, f1.Frame.Height);
  720. f2.Text = "Frame2";
  721. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  722. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  723. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  724. Assert.Equal (5, f2.Frame.Height);
  725. v1.Text = "Button1";
  726. #if DEBUG
  727. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  728. #else
  729. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  730. #endif
  731. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  732. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  733. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  734. v2.Text = "Button2";
  735. #if DEBUG
  736. Assert.Equal ($"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  737. #else
  738. Assert.Equal ($"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  739. #endif
  740. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  741. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  742. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  743. v3.Text = "Button3";
  744. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  745. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  746. // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  747. Assert.Equal (19, v3.Frame.Width);
  748. // 199*10%=19
  749. Assert.Equal (19, v3.Frame.Height);
  750. v4.Text = "Button4";
  751. v4.AutoSize = false;
  752. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  753. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  754. Assert.Equal (50, v4.Frame.Width);
  755. Assert.Equal (50, v4.Frame.Height);
  756. v4.AutoSize = true;
  757. Assert.Equal (Dim.Auto (DimAutoStyle.Text), v4.Width);
  758. Assert.Equal (Dim.Auto (DimAutoStyle.Text), v4.Height);
  759. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  760. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  761. v5.Text = "Button5";
  762. #if DEBUG
  763. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Frame}))", v5.Width.ToString ());
  764. Assert.Equal ($"Combine(View(Height,Button(v1){v1.Frame})-View(Height,Button(v3){v3.Frame}))", v5.Height.ToString ());
  765. #else
  766. Assert.Equal ($"Combine(View(Width,Button(){v1.Frame})-View(Width,Button(){v3.Frame}))", v5.Width.ToString ());
  767. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Frame}))", v5.Height.ToString ());
  768. #endif
  769. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  770. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  771. v6.Text = "Button6";
  772. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  773. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  774. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  775. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  776. };
  777. Application.Iteration += (s, a) => Application.RequestStop ();
  778. Application.Run (t);
  779. }
  780. [Fact]
  781. public void Percent_Equals ()
  782. {
  783. float n1 = 0;
  784. float n2 = 0;
  785. Dim dim1 = Dim.Percent (n1);
  786. Dim dim2 = Dim.Percent (n2);
  787. Assert.Equal (dim1, dim2);
  788. n1 = n2 = 1;
  789. dim1 = Dim.Percent (n1);
  790. dim2 = Dim.Percent (n2);
  791. Assert.Equal (dim1, dim2);
  792. n1 = n2 = 0.5f;
  793. dim1 = Dim.Percent (n1);
  794. dim2 = Dim.Percent (n2);
  795. Assert.Equal (dim1, dim2);
  796. n1 = n2 = 100f;
  797. dim1 = Dim.Percent (n1);
  798. dim2 = Dim.Percent (n2);
  799. Assert.Equal (dim1, dim2);
  800. n1 = n2 = 0.3f;
  801. dim1 = Dim.Percent (n1, true);
  802. dim2 = Dim.Percent (n2, true);
  803. Assert.Equal (dim1, dim2);
  804. n1 = n2 = 0.3f;
  805. dim1 = Dim.Percent (n1);
  806. dim2 = Dim.Percent (n2, true);
  807. Assert.NotEqual (dim1, dim2);
  808. n1 = 0;
  809. n2 = 1;
  810. dim1 = Dim.Percent (n1);
  811. dim2 = Dim.Percent (n2);
  812. Assert.NotEqual (dim1, dim2);
  813. n1 = 0.5f;
  814. n2 = 1.5f;
  815. dim1 = Dim.Percent (n1);
  816. dim2 = Dim.Percent (n2);
  817. Assert.NotEqual (dim1, dim2);
  818. }
  819. [Fact]
  820. public void Percent_Invalid_Throws ()
  821. {
  822. Dim dim = Dim.Percent (0);
  823. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  824. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  825. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  826. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  827. }
  828. [Theory]
  829. [InlineData (0, false, true, 12)]
  830. [InlineData (0, false, false, 12)]
  831. [InlineData (1, false, true, 12)]
  832. [InlineData (1, false, false, 12)]
  833. [InlineData (2, false, true, 12)]
  834. [InlineData (2, false, false, 12)]
  835. [InlineData (0, true, true, 12)]
  836. [InlineData (0, true, false, 12)]
  837. [InlineData (1, true, true, 12)]
  838. [InlineData (1, true, false, 12)]
  839. [InlineData (2, true, true, 11)]
  840. [InlineData (2, true, false, 11)]
  841. public void Percent_Position (int position, bool usePosition, bool width, int expected)
  842. {
  843. var super = new View { Width = 25, Height = 25 };
  844. var view = new View
  845. {
  846. X = width ? position : 0,
  847. Y = width ? 0 : position,
  848. Width = width ? Dim.Percent (50, usePosition) : 1,
  849. Height = width ? 1 : Dim.Percent (50, usePosition)
  850. };
  851. super.Add (view);
  852. super.BeginInit ();
  853. super.EndInit ();
  854. super.LayoutSubviews ();
  855. Assert.Equal (25, super.Frame.Width);
  856. Assert.Equal (25, super.Frame.Height);
  857. if (width)
  858. {
  859. Assert.Equal (expected, view.Frame.Width);
  860. Assert.Equal (1, view.Frame.Height);
  861. }
  862. else
  863. {
  864. Assert.Equal (1, view.Frame.Width);
  865. Assert.Equal (expected, view.Frame.Height);
  866. }
  867. }
  868. [Theory]
  869. [InlineData (0, true)]
  870. [InlineData (0, false)]
  871. [InlineData (50, true)]
  872. [InlineData (50, false)]
  873. public void Percent_PlusOne (int startingDistance, bool testHorizontal)
  874. {
  875. var super = new View { Width = 100, Height = 100 };
  876. var view = new View
  877. {
  878. X = testHorizontal ? startingDistance : 0,
  879. Y = testHorizontal ? 0 : startingDistance,
  880. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  881. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  882. };
  883. super.Add (view);
  884. super.BeginInit ();
  885. super.EndInit ();
  886. super.LayoutSubviews ();
  887. Assert.Equal (100, super.Frame.Width);
  888. Assert.Equal (100, super.Frame.Height);
  889. if (testHorizontal)
  890. {
  891. Assert.Equal (51, view.Frame.Width);
  892. Assert.Equal (1, view.Frame.Height);
  893. }
  894. else
  895. {
  896. Assert.Equal (1, view.Frame.Width);
  897. Assert.Equal (51, view.Frame.Height);
  898. }
  899. }
  900. [Fact]
  901. public void Percent_SetsValue ()
  902. {
  903. float f = 0;
  904. Dim dim = Dim.Percent (f);
  905. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  906. f = 0.5F;
  907. dim = Dim.Percent (f);
  908. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  909. f = 100;
  910. dim = Dim.Percent (f);
  911. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  912. }
  913. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  914. // TODO: A new test that calls SetRelativeLayout directly is needed.
  915. [Fact]
  916. [TestRespondersDisposed]
  917. public void Referencing_SuperView_Does_Not_Throw ()
  918. {
  919. var super = new View { Width = 10, Height = 10, Text = "super" };
  920. var view = new View
  921. {
  922. Width = Dim.Width (super), // this is allowed
  923. Height = Dim.Height (super), // this is allowed
  924. Text = "view"
  925. };
  926. super.Add (view);
  927. super.BeginInit ();
  928. super.EndInit ();
  929. Exception exception = Record.Exception (super.LayoutSubviews);
  930. Assert.Null (exception);
  931. super.Dispose ();
  932. }
  933. [Fact]
  934. public void Sized_Equals ()
  935. {
  936. var n1 = 0;
  937. var n2 = 0;
  938. Dim dim1 = Dim.Sized (n1);
  939. Dim dim2 = Dim.Sized (n2);
  940. Assert.Equal (dim1, dim2);
  941. n1 = n2 = 1;
  942. dim1 = Dim.Sized (n1);
  943. dim2 = Dim.Sized (n2);
  944. Assert.Equal (dim1, dim2);
  945. n1 = n2 = -1;
  946. dim1 = Dim.Sized (n1);
  947. dim2 = Dim.Sized (n2);
  948. Assert.Equal (dim1, dim2);
  949. n1 = 0;
  950. n2 = 1;
  951. dim1 = Dim.Sized (n1);
  952. dim2 = Dim.Sized (n2);
  953. Assert.NotEqual (dim1, dim2);
  954. }
  955. [Fact]
  956. public void Sized_SetsValue ()
  957. {
  958. Dim dim = Dim.Sized (0);
  959. Assert.Equal ("Absolute(0)", dim.ToString ());
  960. var testVal = 5;
  961. dim = Dim.Sized (testVal);
  962. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  963. testVal = -1;
  964. dim = Dim.Sized (testVal);
  965. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  966. }
  967. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  968. // TODO: A new test that calls SetRelativeLayout directly is needed.
  969. [Fact]
  970. [AutoInitShutdown]
  971. public void Subtract_Operator ()
  972. {
  973. Toplevel top = new Toplevel ();
  974. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  975. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  976. var count = 20;
  977. List<Label> listLabels = new ();
  978. for (var i = 0; i < count; i++)
  979. {
  980. field.Text = $"Label {i}";
  981. var label = new Label { X = 0, Y = view.Viewport.Height, /*Width = 20,*/ Text = field.Text };
  982. view.Add (label);
  983. Assert.Equal ($"Label {i}", label.Text);
  984. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  985. listLabels.Add (label);
  986. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  987. view.Height += 1;
  988. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  989. }
  990. field.KeyDown += (s, k) =>
  991. {
  992. if (k.KeyCode == KeyCode.Enter)
  993. {
  994. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  995. view.Remove (listLabels [count - 1]);
  996. listLabels [count - 1].Dispose ();
  997. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  998. view.Height -= 1;
  999. count--;
  1000. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  1001. }
  1002. };
  1003. Application.Iteration += (s, a) =>
  1004. {
  1005. while (count > 0)
  1006. {
  1007. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1008. }
  1009. Application.RequestStop ();
  1010. };
  1011. var win = new Window ();
  1012. win.Add (view);
  1013. win.Add (field);
  1014. top.Add (win);
  1015. Application.Run (top);
  1016. top.Dispose ();
  1017. Assert.Equal (0, count);
  1018. }
  1019. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1020. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1021. [Fact]
  1022. [TestRespondersDisposed]
  1023. public void SyperView_Referencing_SubView_Throws ()
  1024. {
  1025. var super = new View { Width = 10, Height = 10, Text = "super" };
  1026. var view2 = new View { Width = 10, Height = 10, Text = "view2" };
  1027. var view = new View
  1028. {
  1029. Width = Dim.Width (view2), // this is not allowed
  1030. Height = Dim.Height (view2), // this is not allowed
  1031. Text = "view"
  1032. };
  1033. view.Add (view2);
  1034. super.Add (view);
  1035. super.BeginInit ();
  1036. super.EndInit ();
  1037. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  1038. super.Dispose ();
  1039. }
  1040. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1041. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1042. [Fact]
  1043. [TestRespondersDisposed]
  1044. public void Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  1045. {
  1046. var t = new View { Width = 80, Height = 25, Text = "top" };
  1047. var w = new Window
  1048. {
  1049. X = 1,
  1050. Y = 2,
  1051. Width = 4,
  1052. Height = 5,
  1053. Title = "w"
  1054. };
  1055. t.Add (w);
  1056. t.LayoutSubviews ();
  1057. Assert.Equal (3, w.Width = 3);
  1058. Assert.Equal (4, w.Height = 4);
  1059. t.Dispose ();
  1060. }
  1061. [Fact]
  1062. [TestRespondersDisposed]
  1063. public void Width_Equals ()
  1064. {
  1065. var testRect1 = Rectangle.Empty;
  1066. var view1 = new View { Frame = testRect1 };
  1067. var testRect2 = Rectangle.Empty;
  1068. var view2 = new View { Frame = testRect2 };
  1069. Dim dim1 = Dim.Width (view1);
  1070. Dim dim2 = Dim.Width (view1);
  1071. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  1072. Assert.Equal (dim1, dim2);
  1073. dim2 = Dim.Width (view2);
  1074. Assert.NotEqual (dim1, dim2);
  1075. testRect1 = new Rectangle (0, 1, 2, 3);
  1076. view1 = new View { Frame = testRect1 };
  1077. testRect2 = new Rectangle (0, 1, 2, 3);
  1078. dim1 = Dim.Width (view1);
  1079. dim2 = Dim.Width (view1);
  1080. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  1081. Assert.Equal (dim1, dim2);
  1082. testRect1 = new Rectangle (0, -1, 2, 3);
  1083. view1 = new View { Frame = testRect1 };
  1084. testRect2 = new Rectangle (0, -1, 2, 3);
  1085. dim1 = Dim.Width (view1);
  1086. dim2 = Dim.Width (view1);
  1087. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  1088. Assert.Equal (dim1, dim2);
  1089. testRect1 = new Rectangle (0, -1, 2, 3);
  1090. view1 = new View { Frame = testRect1 };
  1091. testRect2 = Rectangle.Empty;
  1092. view2 = new View { Frame = testRect2 };
  1093. dim1 = Dim.Width (view1);
  1094. dim2 = Dim.Width (view2);
  1095. Assert.NotEqual (dim1, dim2);
  1096. #if DEBUG_IDISPOSABLE
  1097. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  1098. Responder.Instances.Clear ();
  1099. Assert.Empty (Responder.Instances);
  1100. #endif
  1101. }
  1102. [Fact]
  1103. public void Width_Set_To_Null_Throws ()
  1104. {
  1105. Dim dim = Dim.Width (null);
  1106. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  1107. }
  1108. [Fact]
  1109. [TestRespondersDisposed]
  1110. public void Width_SetsValue ()
  1111. {
  1112. var testVal = Rectangle.Empty;
  1113. var testValView = new View { Frame = testVal };
  1114. Dim dim = Dim.Width (testValView);
  1115. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  1116. testValView.Dispose ();
  1117. testVal = new Rectangle (1, 2, 3, 4);
  1118. testValView = new View { Frame = testVal };
  1119. dim = Dim.Width (testValView);
  1120. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  1121. testValView.Dispose ();
  1122. }
  1123. }