Dim.AutoTests.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.Dim;
  4. namespace Terminal.Gui.LayoutTests;
  5. public partial class DimAutoTests (ITestOutputHelper output)
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. [SetupFakeDriver]
  9. [Fact]
  10. public void Change_To_Non_Auto_Resets_ContentSize ()
  11. {
  12. View view = new ()
  13. {
  14. Width = Auto (),
  15. Height = Auto (),
  16. Text = "01234"
  17. };
  18. view.SetRelativeLayout (new (100, 100));
  19. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  20. Assert.Equal (new (5, 1), view.GetContentSize ());
  21. // Change text to a longer string
  22. view.Text = "0123456789";
  23. Assert.Equal (new (0, 0, 10, 1), view.Frame);
  24. Assert.Equal (new (10, 1), view.GetContentSize ());
  25. // If ContentSize was reset, these should cause it to update
  26. view.Width = 5;
  27. view.Height = 1;
  28. Assert.Equal (new (5, 1), view.GetContentSize ());
  29. }
  30. [Theory]
  31. [InlineData (0, 0, 0, 0, 0)]
  32. [InlineData (0, 0, 5, 0, 0)]
  33. [InlineData (0, 0, 0, 5, 5)]
  34. [InlineData (0, 0, 5, 5, 5)]
  35. [InlineData (1, 0, 5, 0, 0)]
  36. [InlineData (1, 0, 0, 5, 5)]
  37. [InlineData (1, 0, 5, 5, 5)]
  38. [InlineData (1, 1, 5, 5, 6)]
  39. [InlineData (-1, 0, 5, 0, 0)]
  40. [InlineData (-1, 0, 0, 5, 5)]
  41. [InlineData (-1, 0, 5, 5, 5)]
  42. [InlineData (-1, -1, 5, 5, 4)]
  43. public void Height_Auto_Width_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  44. {
  45. var superView = new View
  46. {
  47. X = 0,
  48. Y = 0,
  49. Width = 10,
  50. Height = Auto (),
  51. ValidatePosDim = true
  52. };
  53. var subView = new View
  54. {
  55. X = subX,
  56. Y = subY,
  57. Width = subWidth,
  58. Height = subHeight,
  59. ValidatePosDim = true
  60. };
  61. superView.Add (subView);
  62. superView.BeginInit ();
  63. superView.EndInit ();
  64. superView.SetRelativeLayout (new (10, 10));
  65. Assert.Equal (new (0, 0, 10, expectedHeight), superView.Frame);
  66. }
  67. [Theory]
  68. [CombinatorialData]
  69. public void HotKey_TextFormatter_Height_Correct ([CombinatorialValues ("1234", "_1234", "1_234", "____")] string text)
  70. {
  71. View view = new ()
  72. {
  73. HotKeySpecifier = (Rune)'_',
  74. Text = text,
  75. Width = Auto (),
  76. Height = 1
  77. };
  78. Assert.Equal (4, view.TextFormatter.ConstrainToWidth);
  79. Assert.Equal (1, view.TextFormatter.ConstrainToHeight);
  80. view = new ()
  81. {
  82. HotKeySpecifier = (Rune)'_',
  83. TextDirection = TextDirection.TopBottom_LeftRight,
  84. Text = text,
  85. Width = 1,
  86. Height = Auto ()
  87. };
  88. Assert.Equal (1, view.TextFormatter.ConstrainToWidth);
  89. Assert.Equal (4, view.TextFormatter.ConstrainToHeight);
  90. }
  91. [Theory]
  92. [CombinatorialData]
  93. public void HotKey_TextFormatter_Width_Correct ([CombinatorialValues ("1234", "_1234", "1_234", "____")] string text)
  94. {
  95. View view = new ()
  96. {
  97. Text = text,
  98. Height = 1,
  99. Width = Auto ()
  100. };
  101. Assert.Equal (4, view.TextFormatter.ConstrainToWidth);
  102. Assert.Equal (1, view.TextFormatter.ConstrainToHeight);
  103. }
  104. [Fact]
  105. public void NoSubViews_Does_Nothing ()
  106. {
  107. var superView = new View
  108. {
  109. X = 0,
  110. Y = 0,
  111. Width = Auto (),
  112. Height = Auto (),
  113. ValidatePosDim = true
  114. };
  115. superView.BeginInit ();
  116. superView.EndInit ();
  117. superView.SetRelativeLayout (new (10, 10));
  118. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  119. superView.SetRelativeLayout (new (10, 10));
  120. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  121. }
  122. [Fact]
  123. public void NoSubViews_Does_Nothing_Vertical ()
  124. {
  125. var superView = new View
  126. {
  127. X = 0,
  128. Y = 0,
  129. Width = Auto (),
  130. Height = Auto (),
  131. TextDirection = TextDirection.TopBottom_LeftRight,
  132. ValidatePosDim = true
  133. };
  134. superView.BeginInit ();
  135. superView.EndInit ();
  136. superView.SetRelativeLayout (new (10, 10));
  137. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  138. superView.SetRelativeLayout (new (10, 10));
  139. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  140. }
  141. [Theory]
  142. [InlineData (0, 0, 0, 0, 0, 0)]
  143. [InlineData (0, 0, 5, 0, 5, 0)]
  144. [InlineData (0, 0, 0, 5, 0, 5)]
  145. [InlineData (0, 0, 5, 5, 5, 5)]
  146. [InlineData (1, 0, 5, 0, 6, 0)]
  147. [InlineData (1, 0, 0, 5, 1, 5)]
  148. [InlineData (1, 0, 5, 5, 6, 5)]
  149. [InlineData (1, 1, 5, 5, 6, 6)]
  150. [InlineData (-1, 0, 5, 0, 4, 0)]
  151. [InlineData (-1, 0, 0, 5, 0, 5)]
  152. [InlineData (-1, 0, 5, 5, 4, 5)]
  153. [InlineData (-1, -1, 5, 5, 4, 4)]
  154. public void SubView_Changes_SuperView_Size (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  155. {
  156. var superView = new View
  157. {
  158. X = 0,
  159. Y = 0,
  160. Width = Auto (),
  161. Height = Auto (),
  162. ValidatePosDim = true
  163. };
  164. var subView = new View
  165. {
  166. X = subX,
  167. Y = subY,
  168. Width = subWidth,
  169. Height = subHeight,
  170. ValidatePosDim = true
  171. };
  172. superView.Add (subView);
  173. superView.BeginInit ();
  174. superView.EndInit ();
  175. superView.SetRelativeLayout (new (10, 10));
  176. Assert.Equal (new (0, 0, expectedWidth, expectedHeight), superView.Frame);
  177. }
  178. [Fact]
  179. public void TestEquality ()
  180. {
  181. var a = new DimAuto (
  182. MaximumContentDim: null,
  183. MinimumContentDim: 1,
  184. Style: DimAutoStyle.Auto
  185. );
  186. var b = new DimAuto (
  187. MaximumContentDim: null,
  188. MinimumContentDim: 1,
  189. Style: DimAutoStyle.Auto
  190. );
  191. var c = new DimAuto(
  192. MaximumContentDim: 2,
  193. MinimumContentDim: 1,
  194. Style: DimAutoStyle.Auto
  195. );
  196. var d = new DimAuto (
  197. MaximumContentDim: null,
  198. MinimumContentDim: 1,
  199. Style: DimAutoStyle.Content
  200. );
  201. var e = new DimAuto (
  202. MaximumContentDim: null,
  203. MinimumContentDim: 2,
  204. Style: DimAutoStyle.Auto
  205. );
  206. // Test equality with same values
  207. Assert.True (a.Equals (b));
  208. Assert.True (a.GetHashCode () == b.GetHashCode ());
  209. // Test inequality with different MaximumContentDim
  210. Assert.False (a.Equals (c));
  211. Assert.False (a.GetHashCode () == c.GetHashCode ());
  212. // Test inequality with different Style
  213. Assert.False (a.Equals (d));
  214. Assert.False (a.GetHashCode () == d.GetHashCode ());
  215. // Test inequality with different MinimumContentDim
  216. Assert.False (a.Equals (e));
  217. Assert.False (a.GetHashCode () == e.GetHashCode ());
  218. // Test inequality with null
  219. Assert.False (a.Equals (null));
  220. }
  221. [Fact]
  222. public void TestEquality_Simple ()
  223. {
  224. Dim a = Auto ();
  225. Dim b = Auto ();
  226. Assert.True (a.Equals (b));
  227. Assert.True (a.GetHashCode () == b.GetHashCode ());
  228. }
  229. [Fact]
  230. public void TextFormatter_Settings_Change_View_Size ()
  231. {
  232. View view = new ()
  233. {
  234. Text = "_1234",
  235. Width = Auto ()
  236. };
  237. Assert.Equal (new (4, 0), view.Frame.Size);
  238. view.Height = 1;
  239. view.SetRelativeLayout (Application.Screen.Size);
  240. Assert.Equal (new (4, 1), view.Frame.Size);
  241. Size lastSize = view.Frame.Size;
  242. view.TextAlignment = Alignment.Fill;
  243. Assert.Equal (lastSize, view.Frame.Size);
  244. view = new ()
  245. {
  246. Text = "_1234",
  247. Width = Auto (),
  248. Height = 1
  249. };
  250. view.SetRelativeLayout (Application.Screen.Size);
  251. lastSize = view.Frame.Size;
  252. view.VerticalTextAlignment = Alignment.Center;
  253. Assert.Equal (lastSize, view.Frame.Size);
  254. view = new ()
  255. {
  256. Text = "_1234",
  257. Width = Auto (),
  258. Height = 1
  259. };
  260. view.SetRelativeLayout (Application.Screen.Size);
  261. lastSize = view.Frame.Size;
  262. view.HotKeySpecifier = (Rune)'*';
  263. view.SetRelativeLayout (Application.Screen.Size);
  264. Assert.NotEqual (lastSize, view.Frame.Size);
  265. view = new ()
  266. {
  267. Text = "_1234",
  268. Width = Auto (),
  269. Height = 1
  270. };
  271. view.SetRelativeLayout (Application.Screen.Size);
  272. lastSize = view.Frame.Size;
  273. view.Text = "*ABCD";
  274. Assert.NotEqual (lastSize, view.Frame.Size);
  275. }
  276. // Test validation
  277. [Fact]
  278. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  279. {
  280. var superView = new View
  281. {
  282. X = 0,
  283. Y = 0,
  284. Width = Auto (),
  285. Height = Auto (),
  286. ValidatePosDim = true
  287. };
  288. var subView = new View
  289. {
  290. X = 0,
  291. Y = 0,
  292. Width = Fill (),
  293. Height = 10,
  294. ValidatePosDim = true
  295. };
  296. superView.BeginInit ();
  297. superView.EndInit ();
  298. superView.Add (subView);
  299. subView.Width = 10;
  300. superView.Add (subView);
  301. superView.SetRelativeLayout (new (10, 10));
  302. superView.LayoutSubviews (); // no throw
  303. subView.Width = Fill ();
  304. superView.SetRelativeLayout (new (0, 0));
  305. subView.Width = 10;
  306. subView.Height = Fill ();
  307. superView.SetRelativeLayout (new (0, 0));
  308. subView.Height = 10;
  309. subView.Height = Percent (50);
  310. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  311. subView.Height = 10;
  312. subView.X = Pos.Center ();
  313. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  314. subView.X = 0;
  315. subView.Y = Pos.Center ();
  316. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  317. subView.Y = 0;
  318. subView.Width = 10;
  319. subView.Height = 10;
  320. subView.X = 0;
  321. subView.Y = 0;
  322. superView.SetRelativeLayout (new (0, 0));
  323. superView.LayoutSubviews ();
  324. }
  325. // Test validation
  326. [Fact]
  327. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  328. {
  329. var superView = new View
  330. {
  331. X = 0,
  332. Y = 0,
  333. Width = Auto (),
  334. Height = Auto (),
  335. ValidatePosDim = true
  336. };
  337. var subView = new View
  338. {
  339. X = 0,
  340. Y = 0,
  341. Width = 10,
  342. Height = 10
  343. };
  344. var subView2 = new View
  345. {
  346. X = 0,
  347. Y = 0,
  348. Width = 10,
  349. Height = 10
  350. };
  351. superView.Add (subView, subView2);
  352. superView.BeginInit ();
  353. superView.EndInit ();
  354. superView.SetRelativeLayout (new (0, 0));
  355. superView.LayoutSubviews (); // no throw
  356. subView.Height = Fill () + 3;
  357. superView.SetRelativeLayout (new (0, 0));
  358. subView.Height = 0;
  359. subView.Height = 3 + Fill ();
  360. superView.SetRelativeLayout (new (0, 0));
  361. subView.Height = 0;
  362. subView.Height = 3 + 5 + Fill ();
  363. superView.SetRelativeLayout (new (0, 0));
  364. subView.Height = 0;
  365. subView.Height = 3 + 5 + Percent (10);
  366. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  367. subView.Height = 0;
  368. // Tests nested Combine
  369. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Percent (10), 9));
  370. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  371. }
  372. [Fact]
  373. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  374. {
  375. var superView = new View
  376. {
  377. X = 0,
  378. Y = 0,
  379. Width = Auto (),
  380. Height = Auto (),
  381. ValidatePosDim = true
  382. };
  383. var subView = new View
  384. {
  385. X = 0,
  386. Y = 0,
  387. Width = 10,
  388. Height = 10
  389. };
  390. var subView2 = new View
  391. {
  392. X = 0,
  393. Y = 0,
  394. Width = 10,
  395. Height = 10
  396. };
  397. superView.Add (subView, subView2);
  398. superView.BeginInit ();
  399. superView.EndInit ();
  400. superView.SetRelativeLayout (new (0, 0));
  401. superView.LayoutSubviews (); // no throw
  402. subView.X = Pos.Right (subView2);
  403. superView.SetRelativeLayout (new (0, 0));
  404. superView.LayoutSubviews (); // no throw
  405. subView.X = Pos.Right (subView2) + 3;
  406. superView.SetRelativeLayout (new (0, 0)); // no throw
  407. superView.LayoutSubviews (); // no throw
  408. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  409. superView.SetRelativeLayout (new (0, 0)); // no throw
  410. subView.X = Pos.Center () + 3;
  411. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  412. subView.X = 0;
  413. subView.X = 3 + Pos.Center ();
  414. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  415. subView.X = 0;
  416. subView.X = 3 + 5 + Pos.Center ();
  417. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  418. subView.X = 0;
  419. subView.X = 3 + 5 + Pos.Percent (10);
  420. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  421. subView.X = 0;
  422. subView.X = Pos.Percent (10) + Pos.Center ();
  423. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  424. subView.X = 0;
  425. // Tests nested Combine
  426. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  427. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  428. subView.X = 0;
  429. }
  430. [Theory]
  431. [InlineData (0, 0, 0, 0, 0)]
  432. [InlineData (0, 0, 5, 0, 5)]
  433. [InlineData (0, 0, 0, 5, 0)]
  434. [InlineData (0, 0, 5, 5, 5)]
  435. [InlineData (1, 0, 5, 0, 6)]
  436. [InlineData (1, 0, 0, 5, 1)]
  437. [InlineData (1, 0, 5, 5, 6)]
  438. [InlineData (1, 1, 5, 5, 6)]
  439. [InlineData (-1, 0, 5, 0, 4)]
  440. [InlineData (-1, 0, 0, 5, 0)]
  441. [InlineData (-1, 0, 5, 5, 4)]
  442. [InlineData (-1, -1, 5, 5, 4)]
  443. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  444. {
  445. var superView = new View
  446. {
  447. X = 0,
  448. Y = 0,
  449. Width = Auto (),
  450. Height = 10,
  451. ValidatePosDim = true
  452. };
  453. var subView = new View
  454. {
  455. X = subX,
  456. Y = subY,
  457. Width = subWidth,
  458. Height = subHeight,
  459. ValidatePosDim = true
  460. };
  461. superView.Add (subView);
  462. superView.BeginInit ();
  463. superView.EndInit ();
  464. superView.SetRelativeLayout (new (10, 10));
  465. Assert.Equal (new (0, 0, expectedWidth, 10), superView.Frame);
  466. }
  467. // Test that when a view has Width set to DimAuto (min: x)
  468. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  469. [Theory]
  470. [InlineData (0, 0)]
  471. [InlineData (1, 1)]
  472. [InlineData (3, 3)]
  473. [InlineData (4, 4)]
  474. [InlineData (5, 5)] // No reason why it can exceed container
  475. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  476. {
  477. var superView = new View
  478. {
  479. X = 0,
  480. Y = 0,
  481. Width = Auto (minimumContentDim: min),
  482. Height = 1,
  483. ValidatePosDim = true
  484. };
  485. superView.BeginInit ();
  486. superView.EndInit ();
  487. superView.SetRelativeLayout (new (4, 1));
  488. Assert.Equal (expectedWidth, superView.Frame.Width);
  489. }
  490. [Theory]
  491. [InlineData (0, 1, 1)]
  492. [InlineData (1, 1, 1)]
  493. [InlineData (9, 1, 1)]
  494. [InlineData (10, 1, 1)]
  495. [InlineData (0, 10, 10)]
  496. [InlineData (1, 10, 10)]
  497. [InlineData (9, 10, 10)]
  498. [InlineData (10, 10, 10)]
  499. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  500. {
  501. var superView = new View
  502. {
  503. X = 0,
  504. Y = 0,
  505. Width = 10,
  506. Height = 1,
  507. ValidatePosDim = true
  508. };
  509. var subView = new View
  510. {
  511. X = subX,
  512. Y = 0,
  513. Width = Auto (DimAutoStyle.Content),
  514. Height = 1,
  515. ValidatePosDim = true
  516. };
  517. var subSubView = new View
  518. {
  519. X = 0,
  520. Y = 0,
  521. Width = subSubViewWidth,
  522. Height = 1,
  523. ValidatePosDim = true
  524. };
  525. subView.Add (subSubView);
  526. superView.Add (subView);
  527. superView.BeginInit ();
  528. superView.EndInit ();
  529. superView.SetRelativeLayout (superView.GetContentSize ());
  530. superView.LayoutSubviews ();
  531. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  532. }
  533. [Theory]
  534. [InlineData (0, 1, 1)]
  535. [InlineData (1, 1, 1)]
  536. [InlineData (9, 1, 1)]
  537. [InlineData (10, 1, 1)]
  538. [InlineData (0, 10, 10)]
  539. [InlineData (1, 10, 10)]
  540. [InlineData (9, 10, 10)]
  541. [InlineData (10, 10, 10)]
  542. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  543. {
  544. var superView = new View
  545. {
  546. X = 0,
  547. Y = 0,
  548. Width = 10,
  549. Height = 1,
  550. ValidatePosDim = true
  551. };
  552. var subView = new View
  553. {
  554. Text = new ('*', textLen),
  555. X = subX,
  556. Y = 0,
  557. Width = Auto (DimAutoStyle.Text),
  558. Height = 1,
  559. ValidatePosDim = true
  560. };
  561. superView.Add (subView);
  562. superView.BeginInit ();
  563. superView.EndInit ();
  564. superView.SetRelativeLayout (superView.GetContentSize ());
  565. superView.LayoutSubviews ();
  566. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  567. }
  568. private class DimAutoTestView : View
  569. {
  570. public DimAutoTestView ()
  571. {
  572. ValidatePosDim = true;
  573. Width = Auto ();
  574. Height = Auto ();
  575. }
  576. public DimAutoTestView (Dim width, Dim height)
  577. {
  578. ValidatePosDim = true;
  579. Width = width;
  580. Height = height;
  581. }
  582. public DimAutoTestView (string text, Dim width, Dim height)
  583. {
  584. ValidatePosDim = true;
  585. Text = text;
  586. Width = width;
  587. Height = height;
  588. }
  589. }
  590. #region DimAutoStyle.Auto tests
  591. [Theory]
  592. [InlineData ("", 0, 0)]
  593. [InlineData (" ", 1, 1)]
  594. [InlineData ("01234", 5, 1)]
  595. [InlineData ("01234\nABCDE", 5, 2)]
  596. public void DimAutoStyle_Auto_JustText_Sizes_Correctly (string text, int expectedW, int expectedH)
  597. {
  598. var view = new View ();
  599. view.Width = Auto ();
  600. view.Height = Auto ();
  601. view.Text = text;
  602. view.SetRelativeLayout (Application.Screen.Size);
  603. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  604. }
  605. [Fact]
  606. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  607. {
  608. var view = new View
  609. {
  610. Text = "0123\n4567",
  611. Width = Auto (),
  612. Height = Auto ()
  613. };
  614. view.SetRelativeLayout (new (100, 100));
  615. Assert.Equal (new (4, 2), view.Frame.Size);
  616. var subView = new View
  617. {
  618. Text = "ABCD",
  619. Width = Auto (),
  620. Height = Auto ()
  621. };
  622. view.Add (subView);
  623. view.SetRelativeLayout (new (100, 100));
  624. Assert.Equal (new (4, 2), view.Frame.Size);
  625. subView.Text = "ABCDE";
  626. view.SetRelativeLayout (new (100, 100));
  627. Assert.Equal (new (5, 2), view.Frame.Size);
  628. }
  629. [Theory]
  630. [InlineData ("01234", 5, 5)]
  631. [InlineData ("01234", 6, 6)]
  632. [InlineData ("01234", 4, 5)]
  633. [InlineData ("01234", 0, 5)]
  634. [InlineData ("", 5, 5)]
  635. [InlineData ("", 0, 0)]
  636. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  637. {
  638. View view = new ()
  639. {
  640. Width = Auto (),
  641. Height = 1,
  642. Text = text
  643. };
  644. View subView = new ()
  645. {
  646. Width = dimension,
  647. Height = 1
  648. };
  649. view.Add (subView);
  650. view.SetRelativeLayout (new (10, 10));
  651. Assert.Equal (expected, view.Frame.Width);
  652. }
  653. #endregion
  654. #region DimAutoStyle.Text tests
  655. [Fact]
  656. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  657. {
  658. var super = new View
  659. {
  660. Width = Fill (),
  661. Height = Fill ()
  662. };
  663. var view = new View
  664. {
  665. Text = "01234567",
  666. Width = Auto (DimAutoStyle.Text),
  667. Height = Auto (DimAutoStyle.Text)
  668. };
  669. super.Add (view);
  670. Rectangle expectedViewport = new (0, 0, 8, 1);
  671. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  672. Assert.Equal (expectedViewport, view.Frame);
  673. Assert.Equal (expectedViewport, view.Viewport);
  674. super.LayoutSubviews ();
  675. Assert.Equal (expectedViewport, view.Viewport);
  676. super.Dispose ();
  677. }
  678. [Theory]
  679. [InlineData ("", 0, 0)]
  680. [InlineData (" ", 1, 1)]
  681. [InlineData ("01234", 5, 1)]
  682. [InlineData ("01234\nABCDE", 5, 2)]
  683. public void DimAutoStyle_Text_Sizes_Correctly (string text, int expectedW, int expectedH)
  684. {
  685. var view = new View ();
  686. view.Width = Auto (DimAutoStyle.Text);
  687. view.Height = Auto (DimAutoStyle.Text);
  688. view.Text = text;
  689. view.SetRelativeLayout (Application.Screen.Size);
  690. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  691. }
  692. [Theory]
  693. [InlineData ("", 0, 0, 0, 0)]
  694. [InlineData (" ", 5, 5, 5, 5)]
  695. [InlineData ("01234", 5, 5, 5, 5)]
  696. [InlineData ("01234", 4, 3, 5, 3)]
  697. [InlineData ("01234ABCDE", 5, 0, 10, 1)]
  698. public void DimAutoStyle_Text_Sizes_Correctly_With_Min (string text, int minWidth, int minHeight, int expectedW, int expectedH)
  699. {
  700. var view = new View ();
  701. view.Width = Auto (DimAutoStyle.Text, minWidth);
  702. view.Height = Auto (DimAutoStyle.Text, minHeight);
  703. view.Text = text;
  704. view.SetRelativeLayout (Application.Screen.Size);
  705. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  706. }
  707. [Theory]
  708. [InlineData ("", 0, 0, 0)]
  709. [InlineData (" ", 5, 1, 1)]
  710. [InlineData ("01234", 5, 5, 1)]
  711. [InlineData ("01234", 4, 4, 2)]
  712. [InlineData ("01234ABCDE", 5, 5, 2)]
  713. [InlineData ("01234ABCDE", 1, 1, 10)]
  714. public void DimAutoStyle_Text_Sizes_Correctly_With_Max_Width (string text, int maxWidth, int expectedW, int expectedH)
  715. {
  716. var view = new View ();
  717. view.Width = Auto (DimAutoStyle.Text, maximumContentDim: maxWidth);
  718. view.Height = Auto (DimAutoStyle.Text);
  719. view.Text = text;
  720. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  721. }
  722. [Theory]
  723. [InlineData ("", 0, 0)]
  724. [InlineData (" ", 1, 1)]
  725. [InlineData ("01234", 5, 1)]
  726. [InlineData ("01234ABCDE", 10, 1)]
  727. [InlineData ("01234\nABCDE", 5, 2)]
  728. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_ContentSize (string text, int expectedW, int expectedH)
  729. {
  730. var view = new View ();
  731. view.Width = Auto (DimAutoStyle.Text);
  732. view.Height = Auto (DimAutoStyle.Text);
  733. view.SetContentSize (new (1, 1));
  734. view.Text = text;
  735. view.SetRelativeLayout (Application.Screen.Size);
  736. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  737. }
  738. [Theory]
  739. [InlineData ("", 0, 0)]
  740. [InlineData (" ", 1, 1)]
  741. [InlineData ("01234", 5, 1)]
  742. [InlineData ("01234ABCDE", 10, 1)]
  743. [InlineData ("01234\nABCDE", 5, 2)]
  744. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_SuperView (string text, int expectedW, int expectedH)
  745. {
  746. var superView = new View
  747. {
  748. Width = 1, Height = 1
  749. };
  750. var view = new View ();
  751. view.Width = Auto (DimAutoStyle.Text);
  752. view.Height = Auto (DimAutoStyle.Text);
  753. view.Text = text;
  754. superView.Add (view);
  755. superView.SetRelativeLayout (Application.Screen.Size);
  756. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  757. }
  758. [Fact]
  759. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  760. {
  761. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  762. view.SetRelativeLayout (new (10, 10));
  763. Assert.Equal (new (5, 1), view.Frame.Size);
  764. Assert.Equal (new (0, 0), view.Frame.Location);
  765. view.X = 0;
  766. view.Y = Pos.AnchorEnd (1);
  767. view.SetRelativeLayout (new (10, 10));
  768. Assert.Equal (new (5, 1), view.Frame.Size);
  769. Assert.Equal (new (0, 9), view.Frame.Location);
  770. view.Y = Pos.AnchorEnd ();
  771. view.SetRelativeLayout (new (10, 10));
  772. Assert.Equal (new (5, 1), view.Frame.Size);
  773. Assert.Equal (new (0, 9), view.Frame.Location);
  774. view.Y = Pos.AnchorEnd () - 1;
  775. view.SetRelativeLayout (new (10, 10));
  776. Assert.Equal (new (5, 1), view.Frame.Size);
  777. Assert.Equal (new (0, 8), view.Frame.Location);
  778. }
  779. #endregion DimAutoStyle.Text tests
  780. #region DimAutoStyle.Content tests
  781. // DimAutoStyle.Content tests
  782. [Fact]
  783. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  784. {
  785. var view = new View ();
  786. view.SetContentSize (new (10, 5));
  787. Dim dim = Auto (DimAutoStyle.Content);
  788. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  789. Assert.Equal (10, calculatedWidth);
  790. }
  791. [Fact]
  792. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  793. {
  794. var view = new View ();
  795. var subview = new View
  796. {
  797. Frame = new (50, 50, 1, 1)
  798. };
  799. view.SetContentSize (new (10, 5));
  800. Dim dim = Auto (DimAutoStyle.Content);
  801. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  802. Assert.Equal (10, calculatedWidth);
  803. }
  804. [Fact]
  805. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  806. {
  807. var view = new View { Text = "This is a test" };
  808. Dim dim = Auto (DimAutoStyle.Content);
  809. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  810. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  811. }
  812. [Fact]
  813. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  814. {
  815. var view = new View ();
  816. view.Add (new View { Frame = new (0, 0, 5, 5) }); // Smaller subview
  817. view.Add (new View { Frame = new (0, 0, 10, 10) }); // Larger subview
  818. Dim dim = Auto (DimAutoStyle.Content);
  819. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  820. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  821. }
  822. [Fact]
  823. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  824. {
  825. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  826. view.SetContentSize (new (5, 5));
  827. view.SetRelativeLayout (new (10, 10));
  828. Assert.Equal (new (5, 5), view.Frame.Size);
  829. }
  830. [Fact]
  831. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  832. {
  833. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  834. View subView = new ()
  835. {
  836. Width = 5,
  837. Height = 1
  838. };
  839. view.Add (subView);
  840. view.SetRelativeLayout (new (10, 10));
  841. Assert.Equal (new (5, 1), view.Frame.Size);
  842. Assert.Equal (new (0, 0), view.Frame.Location);
  843. view.X = 0;
  844. view.Y = Pos.AnchorEnd (1);
  845. view.SetRelativeLayout (new (10, 10));
  846. Assert.Equal (new (5, 1), view.Frame.Size);
  847. Assert.Equal (new (0, 9), view.Frame.Location);
  848. view.Y = Pos.AnchorEnd ();
  849. view.SetRelativeLayout (new (10, 10));
  850. Assert.Equal (new (5, 1), view.Frame.Size);
  851. Assert.Equal (new (0, 9), view.Frame.Location);
  852. view.Y = Pos.AnchorEnd () - 1;
  853. view.SetRelativeLayout (new (10, 10));
  854. Assert.Equal (new (5, 1), view.Frame.Size);
  855. Assert.Equal (new (0, 8), view.Frame.Location);
  856. }
  857. #endregion DimAutoStyle.Content tests
  858. // Test variations of Frame
  859. }