Dim.AutoTests.cs 28 KB

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