Dim.AutoTests.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. {
  183. MaximumContentDim = null,
  184. MinimumContentDim = 1,
  185. Style = DimAutoStyle.Auto
  186. };
  187. var b = new DimAuto
  188. {
  189. MaximumContentDim = null,
  190. MinimumContentDim = 1,
  191. Style = DimAutoStyle.Auto
  192. };
  193. var c = new DimAuto
  194. {
  195. MaximumContentDim = 2,
  196. MinimumContentDim = 1,
  197. Style = DimAutoStyle.Auto
  198. };
  199. var d = new DimAuto
  200. {
  201. MaximumContentDim = null,
  202. MinimumContentDim = 1,
  203. Style = DimAutoStyle.Content
  204. };
  205. var e = new DimAuto
  206. {
  207. MaximumContentDim = null,
  208. MinimumContentDim = 2,
  209. Style = DimAutoStyle.Auto
  210. };
  211. // Test equality with same values
  212. Assert.True (a.Equals (b));
  213. Assert.True (a.GetHashCode () == b.GetHashCode ());
  214. // Test inequality with different MaximumContentDim
  215. Assert.False (a.Equals (c));
  216. Assert.False (a.GetHashCode () == c.GetHashCode ());
  217. // Test inequality with different Style
  218. Assert.False (a.Equals (d));
  219. Assert.False (a.GetHashCode () == d.GetHashCode ());
  220. // Test inequality with different MinimumContentDim
  221. Assert.False (a.Equals (e));
  222. Assert.False (a.GetHashCode () == e.GetHashCode ());
  223. // Test inequality with null
  224. Assert.False (a.Equals (null));
  225. }
  226. [Fact]
  227. public void TestEquality_Simple ()
  228. {
  229. Dim a = Auto ();
  230. Dim b = Auto ();
  231. Assert.True (a.Equals (b));
  232. Assert.True (a.GetHashCode () == b.GetHashCode ());
  233. }
  234. [Fact]
  235. public void TextFormatter_Settings_Change_View_Size ()
  236. {
  237. View view = new ()
  238. {
  239. Text = "_1234",
  240. Width = Auto ()
  241. };
  242. Assert.Equal (new (4, 0), view.Frame.Size);
  243. view.Height = 1;
  244. view.SetRelativeLayout (Application.Screen.Size);
  245. Assert.Equal (new (4, 1), view.Frame.Size);
  246. Size lastSize = view.Frame.Size;
  247. view.TextAlignment = Alignment.Fill;
  248. Assert.Equal (lastSize, view.Frame.Size);
  249. view = new ()
  250. {
  251. Text = "_1234",
  252. Width = Auto (),
  253. Height = 1
  254. };
  255. view.SetRelativeLayout (Application.Screen.Size);
  256. lastSize = view.Frame.Size;
  257. view.VerticalTextAlignment = Alignment.Center;
  258. Assert.Equal (lastSize, view.Frame.Size);
  259. view = new ()
  260. {
  261. Text = "_1234",
  262. Width = Auto (),
  263. Height = 1
  264. };
  265. view.SetRelativeLayout (Application.Screen.Size);
  266. lastSize = view.Frame.Size;
  267. view.HotKeySpecifier = (Rune)'*';
  268. view.SetRelativeLayout (Application.Screen.Size);
  269. Assert.NotEqual (lastSize, view.Frame.Size);
  270. view = new ()
  271. {
  272. Text = "_1234",
  273. Width = Auto (),
  274. Height = 1
  275. };
  276. view.SetRelativeLayout (Application.Screen.Size);
  277. lastSize = view.Frame.Size;
  278. view.Text = "*ABCD";
  279. Assert.NotEqual (lastSize, view.Frame.Size);
  280. }
  281. // Test validation
  282. [Fact]
  283. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  284. {
  285. var superView = new View
  286. {
  287. X = 0,
  288. Y = 0,
  289. Width = Auto (),
  290. Height = Auto (),
  291. ValidatePosDim = true
  292. };
  293. var subView = new View
  294. {
  295. X = 0,
  296. Y = 0,
  297. Width = Fill (),
  298. Height = 10,
  299. ValidatePosDim = true
  300. };
  301. superView.BeginInit ();
  302. superView.EndInit ();
  303. superView.Add (subView);
  304. subView.Width = 10;
  305. superView.Add (subView);
  306. superView.SetRelativeLayout (new (10, 10));
  307. superView.LayoutSubviews (); // no throw
  308. subView.Width = Fill ();
  309. superView.SetRelativeLayout (new (0, 0));
  310. subView.Width = 10;
  311. subView.Height = Fill ();
  312. superView.SetRelativeLayout (new (0, 0));
  313. subView.Height = 10;
  314. subView.Height = Percent (50);
  315. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  316. subView.Height = 10;
  317. subView.X = Pos.Center ();
  318. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  319. subView.X = 0;
  320. subView.Y = Pos.Center ();
  321. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  322. subView.Y = 0;
  323. subView.Width = 10;
  324. subView.Height = 10;
  325. subView.X = 0;
  326. subView.Y = 0;
  327. superView.SetRelativeLayout (new (0, 0));
  328. superView.LayoutSubviews ();
  329. }
  330. // Test validation
  331. [Fact]
  332. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  333. {
  334. var superView = new View
  335. {
  336. X = 0,
  337. Y = 0,
  338. Width = Auto (),
  339. Height = Auto (),
  340. ValidatePosDim = true
  341. };
  342. var subView = new View
  343. {
  344. X = 0,
  345. Y = 0,
  346. Width = 10,
  347. Height = 10
  348. };
  349. var subView2 = new View
  350. {
  351. X = 0,
  352. Y = 0,
  353. Width = 10,
  354. Height = 10
  355. };
  356. superView.Add (subView, subView2);
  357. superView.BeginInit ();
  358. superView.EndInit ();
  359. superView.SetRelativeLayout (new (0, 0));
  360. superView.LayoutSubviews (); // no throw
  361. subView.Height = Fill () + 3;
  362. superView.SetRelativeLayout (new (0, 0));
  363. subView.Height = 0;
  364. subView.Height = 3 + Fill ();
  365. superView.SetRelativeLayout (new (0, 0));
  366. subView.Height = 0;
  367. subView.Height = 3 + 5 + Fill ();
  368. superView.SetRelativeLayout (new (0, 0));
  369. subView.Height = 0;
  370. subView.Height = 3 + 5 + Percent (10);
  371. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  372. subView.Height = 0;
  373. // Tests nested Combine
  374. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Percent (10), 9));
  375. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  376. }
  377. [Fact]
  378. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  379. {
  380. var superView = new View
  381. {
  382. X = 0,
  383. Y = 0,
  384. Width = Auto (),
  385. Height = Auto (),
  386. ValidatePosDim = true
  387. };
  388. var subView = new View
  389. {
  390. X = 0,
  391. Y = 0,
  392. Width = 10,
  393. Height = 10
  394. };
  395. var subView2 = new View
  396. {
  397. X = 0,
  398. Y = 0,
  399. Width = 10,
  400. Height = 10
  401. };
  402. superView.Add (subView, subView2);
  403. superView.BeginInit ();
  404. superView.EndInit ();
  405. superView.SetRelativeLayout (new (0, 0));
  406. superView.LayoutSubviews (); // no throw
  407. subView.X = Pos.Right (subView2);
  408. superView.SetRelativeLayout (new (0, 0));
  409. superView.LayoutSubviews (); // no throw
  410. subView.X = Pos.Right (subView2) + 3;
  411. superView.SetRelativeLayout (new (0, 0)); // no throw
  412. superView.LayoutSubviews (); // no throw
  413. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  414. superView.SetRelativeLayout (new (0, 0)); // no throw
  415. subView.X = Pos.Center () + 3;
  416. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  417. subView.X = 0;
  418. subView.X = 3 + Pos.Center ();
  419. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  420. subView.X = 0;
  421. subView.X = 3 + 5 + Pos.Center ();
  422. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  423. subView.X = 0;
  424. subView.X = 3 + 5 + Pos.Percent (10);
  425. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  426. subView.X = 0;
  427. subView.X = Pos.Percent (10) + Pos.Center ();
  428. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  429. subView.X = 0;
  430. // Tests nested Combine
  431. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  432. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  433. subView.X = 0;
  434. }
  435. [Theory]
  436. [InlineData (0, 0, 0, 0, 0)]
  437. [InlineData (0, 0, 5, 0, 5)]
  438. [InlineData (0, 0, 0, 5, 0)]
  439. [InlineData (0, 0, 5, 5, 5)]
  440. [InlineData (1, 0, 5, 0, 6)]
  441. [InlineData (1, 0, 0, 5, 1)]
  442. [InlineData (1, 0, 5, 5, 6)]
  443. [InlineData (1, 1, 5, 5, 6)]
  444. [InlineData (-1, 0, 5, 0, 4)]
  445. [InlineData (-1, 0, 0, 5, 0)]
  446. [InlineData (-1, 0, 5, 5, 4)]
  447. [InlineData (-1, -1, 5, 5, 4)]
  448. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  449. {
  450. var superView = new View
  451. {
  452. X = 0,
  453. Y = 0,
  454. Width = Auto (),
  455. Height = 10,
  456. ValidatePosDim = true
  457. };
  458. var subView = new View
  459. {
  460. X = subX,
  461. Y = subY,
  462. Width = subWidth,
  463. Height = subHeight,
  464. ValidatePosDim = true
  465. };
  466. superView.Add (subView);
  467. superView.BeginInit ();
  468. superView.EndInit ();
  469. superView.SetRelativeLayout (new (10, 10));
  470. Assert.Equal (new (0, 0, expectedWidth, 10), superView.Frame);
  471. }
  472. // Test that when a view has Width set to DimAuto (min: x)
  473. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  474. [Theory]
  475. [InlineData (0, 0)]
  476. [InlineData (1, 1)]
  477. [InlineData (3, 3)]
  478. [InlineData (4, 4)]
  479. [InlineData (5, 5)] // No reason why it can exceed container
  480. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  481. {
  482. var superView = new View
  483. {
  484. X = 0,
  485. Y = 0,
  486. Width = Auto (minimumContentDim: min),
  487. Height = 1,
  488. ValidatePosDim = true
  489. };
  490. superView.BeginInit ();
  491. superView.EndInit ();
  492. superView.SetRelativeLayout (new (4, 1));
  493. Assert.Equal (expectedWidth, superView.Frame.Width);
  494. }
  495. [Theory]
  496. [InlineData (0, 1, 1)]
  497. [InlineData (1, 1, 1)]
  498. [InlineData (9, 1, 1)]
  499. [InlineData (10, 1, 1)]
  500. [InlineData (0, 10, 10)]
  501. [InlineData (1, 10, 10)]
  502. [InlineData (9, 10, 10)]
  503. [InlineData (10, 10, 10)]
  504. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  505. {
  506. var superView = new View
  507. {
  508. X = 0,
  509. Y = 0,
  510. Width = 10,
  511. Height = 1,
  512. ValidatePosDim = true
  513. };
  514. var subView = new View
  515. {
  516. X = subX,
  517. Y = 0,
  518. Width = Auto (DimAutoStyle.Content),
  519. Height = 1,
  520. ValidatePosDim = true
  521. };
  522. var subSubView = new View
  523. {
  524. X = 0,
  525. Y = 0,
  526. Width = subSubViewWidth,
  527. Height = 1,
  528. ValidatePosDim = true
  529. };
  530. subView.Add (subSubView);
  531. superView.Add (subView);
  532. superView.BeginInit ();
  533. superView.EndInit ();
  534. superView.SetRelativeLayout (superView.GetContentSize ());
  535. superView.LayoutSubviews ();
  536. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  537. }
  538. [Theory]
  539. [InlineData (0, 1, 1)]
  540. [InlineData (1, 1, 1)]
  541. [InlineData (9, 1, 1)]
  542. [InlineData (10, 1, 1)]
  543. [InlineData (0, 10, 10)]
  544. [InlineData (1, 10, 10)]
  545. [InlineData (9, 10, 10)]
  546. [InlineData (10, 10, 10)]
  547. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  548. {
  549. var superView = new View
  550. {
  551. X = 0,
  552. Y = 0,
  553. Width = 10,
  554. Height = 1,
  555. ValidatePosDim = true
  556. };
  557. var subView = new View
  558. {
  559. Text = new ('*', textLen),
  560. X = subX,
  561. Y = 0,
  562. Width = Auto (DimAutoStyle.Text),
  563. Height = 1,
  564. ValidatePosDim = true
  565. };
  566. superView.Add (subView);
  567. superView.BeginInit ();
  568. superView.EndInit ();
  569. superView.SetRelativeLayout (superView.GetContentSize ());
  570. superView.LayoutSubviews ();
  571. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  572. }
  573. private class DimAutoTestView : View
  574. {
  575. public DimAutoTestView ()
  576. {
  577. ValidatePosDim = true;
  578. Width = Auto ();
  579. Height = Auto ();
  580. }
  581. public DimAutoTestView (Dim width, Dim height)
  582. {
  583. ValidatePosDim = true;
  584. Width = width;
  585. Height = height;
  586. }
  587. public DimAutoTestView (string text, Dim width, Dim height)
  588. {
  589. ValidatePosDim = true;
  590. Text = text;
  591. Width = width;
  592. Height = height;
  593. }
  594. }
  595. #region DimAutoStyle.Auto tests
  596. [Theory]
  597. [InlineData ("", 0, 0)]
  598. [InlineData (" ", 1, 1)]
  599. [InlineData ("01234", 5, 1)]
  600. [InlineData ("01234\nABCDE", 5, 2)]
  601. public void DimAutoStyle_Auto_JustText_Sizes_Correctly (string text, int expectedW, int expectedH)
  602. {
  603. var view = new View ();
  604. view.Width = Auto ();
  605. view.Height = Auto ();
  606. view.Text = text;
  607. view.SetRelativeLayout (Application.Screen.Size);
  608. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  609. }
  610. [Fact]
  611. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  612. {
  613. var view = new View
  614. {
  615. Text = "0123\n4567",
  616. Width = Auto (),
  617. Height = Auto ()
  618. };
  619. view.SetRelativeLayout (new (100, 100));
  620. Assert.Equal (new (4, 2), view.Frame.Size);
  621. var subView = new View
  622. {
  623. Text = "ABCD",
  624. Width = Auto (),
  625. Height = Auto ()
  626. };
  627. view.Add (subView);
  628. view.SetRelativeLayout (new (100, 100));
  629. Assert.Equal (new (4, 2), view.Frame.Size);
  630. subView.Text = "ABCDE";
  631. view.SetRelativeLayout (new (100, 100));
  632. Assert.Equal (new (5, 2), view.Frame.Size);
  633. }
  634. [Theory]
  635. [InlineData ("01234", 5, 5)]
  636. [InlineData ("01234", 6, 6)]
  637. [InlineData ("01234", 4, 5)]
  638. [InlineData ("01234", 0, 5)]
  639. [InlineData ("", 5, 5)]
  640. [InlineData ("", 0, 0)]
  641. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  642. {
  643. View view = new ()
  644. {
  645. Width = Auto (),
  646. Height = 1,
  647. Text = text
  648. };
  649. View subView = new ()
  650. {
  651. Width = dimension,
  652. Height = 1
  653. };
  654. view.Add (subView);
  655. view.SetRelativeLayout (new (10, 10));
  656. Assert.Equal (expected, view.Frame.Width);
  657. }
  658. #endregion
  659. #region DimAutoStyle.Text tests
  660. [Fact]
  661. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  662. {
  663. var super = new View
  664. {
  665. Width = Fill (),
  666. Height = Fill ()
  667. };
  668. var view = new View
  669. {
  670. Text = "01234567",
  671. Width = Auto (DimAutoStyle.Text),
  672. Height = Auto (DimAutoStyle.Text)
  673. };
  674. super.Add (view);
  675. Rectangle expectedViewport = new (0, 0, 8, 1);
  676. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  677. Assert.Equal (expectedViewport, view.Frame);
  678. Assert.Equal (expectedViewport, view.Viewport);
  679. super.LayoutSubviews ();
  680. Assert.Equal (expectedViewport, view.Viewport);
  681. super.Dispose ();
  682. }
  683. [Theory]
  684. [InlineData ("", 0, 0)]
  685. [InlineData (" ", 1, 1)]
  686. [InlineData ("01234", 5, 1)]
  687. [InlineData ("01234\nABCDE", 5, 2)]
  688. public void DimAutoStyle_Text_Sizes_Correctly (string text, int expectedW, int expectedH)
  689. {
  690. var view = new View ();
  691. view.Width = Auto (DimAutoStyle.Text);
  692. view.Height = Auto (DimAutoStyle.Text);
  693. view.Text = text;
  694. view.SetRelativeLayout (Application.Screen.Size);
  695. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  696. }
  697. [Theory]
  698. [InlineData ("", 0, 0, 0, 0)]
  699. [InlineData (" ", 5, 5, 5, 5)]
  700. [InlineData ("01234", 5, 5, 5, 5)]
  701. [InlineData ("01234", 4, 3, 5, 3)]
  702. [InlineData ("01234ABCDE", 5, 0, 10, 1)]
  703. public void DimAutoStyle_Text_Sizes_Correctly_With_Min (string text, int minWidth, int minHeight, int expectedW, int expectedH)
  704. {
  705. var view = new View ();
  706. view.Width = Auto (DimAutoStyle.Text, minWidth);
  707. view.Height = Auto (DimAutoStyle.Text, minHeight);
  708. view.Text = text;
  709. view.SetRelativeLayout (Application.Screen.Size);
  710. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  711. }
  712. [Theory]
  713. [InlineData ("", 0, 0, 0)]
  714. [InlineData (" ", 5, 1, 1)]
  715. [InlineData ("01234", 5, 5, 1)]
  716. [InlineData ("01234", 4, 4, 2)]
  717. [InlineData ("01234ABCDE", 5, 5, 2)]
  718. [InlineData ("01234ABCDE", 1, 1, 10)]
  719. public void DimAutoStyle_Text_Sizes_Correctly_With_Max_Width (string text, int maxWidth, int expectedW, int expectedH)
  720. {
  721. var view = new View ();
  722. view.Width = Auto (DimAutoStyle.Text, maximumContentDim: maxWidth);
  723. view.Height = Auto (DimAutoStyle.Text);
  724. view.Text = text;
  725. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  726. }
  727. [Theory]
  728. [InlineData ("", 0, 0)]
  729. [InlineData (" ", 1, 1)]
  730. [InlineData ("01234", 5, 1)]
  731. [InlineData ("01234ABCDE", 10, 1)]
  732. [InlineData ("01234\nABCDE", 5, 2)]
  733. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_ContentSize (string text, int expectedW, int expectedH)
  734. {
  735. var view = new View ();
  736. view.Width = Auto (DimAutoStyle.Text);
  737. view.Height = Auto (DimAutoStyle.Text);
  738. view.SetContentSize (new (1, 1));
  739. view.Text = text;
  740. view.SetRelativeLayout (Application.Screen.Size);
  741. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  742. }
  743. [Theory]
  744. [InlineData ("", 0, 0)]
  745. [InlineData (" ", 1, 1)]
  746. [InlineData ("01234", 5, 1)]
  747. [InlineData ("01234ABCDE", 10, 1)]
  748. [InlineData ("01234\nABCDE", 5, 2)]
  749. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_SuperView (string text, int expectedW, int expectedH)
  750. {
  751. var superView = new View
  752. {
  753. Width = 1, Height = 1
  754. };
  755. var view = new View ();
  756. view.Width = Auto (DimAutoStyle.Text);
  757. view.Height = Auto (DimAutoStyle.Text);
  758. view.Text = text;
  759. superView.Add (view);
  760. superView.SetRelativeLayout (Application.Screen.Size);
  761. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  762. }
  763. [Fact]
  764. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  765. {
  766. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  767. view.SetRelativeLayout (new (10, 10));
  768. Assert.Equal (new (5, 1), view.Frame.Size);
  769. Assert.Equal (new (0, 0), view.Frame.Location);
  770. view.X = 0;
  771. view.Y = Pos.AnchorEnd (1);
  772. view.SetRelativeLayout (new (10, 10));
  773. Assert.Equal (new (5, 1), view.Frame.Size);
  774. Assert.Equal (new (0, 9), view.Frame.Location);
  775. view.Y = Pos.AnchorEnd ();
  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 () - 1;
  780. view.SetRelativeLayout (new (10, 10));
  781. Assert.Equal (new (5, 1), view.Frame.Size);
  782. Assert.Equal (new (0, 8), view.Frame.Location);
  783. }
  784. #endregion DimAutoStyle.Text tests
  785. #region DimAutoStyle.Content tests
  786. // DimAutoStyle.Content tests
  787. [Fact]
  788. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  789. {
  790. var view = new View ();
  791. view.SetContentSize (new (10, 5));
  792. Dim dim = Auto (DimAutoStyle.Content);
  793. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  794. Assert.Equal (10, calculatedWidth);
  795. }
  796. [Fact]
  797. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  798. {
  799. var view = new View ();
  800. var subview = new View
  801. {
  802. Frame = new (50, 50, 1, 1)
  803. };
  804. view.SetContentSize (new (10, 5));
  805. Dim dim = Auto (DimAutoStyle.Content);
  806. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  807. Assert.Equal (10, calculatedWidth);
  808. }
  809. [Fact]
  810. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  811. {
  812. var view = new View { Text = "This is a test" };
  813. Dim dim = Auto (DimAutoStyle.Content);
  814. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  815. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  816. }
  817. [Fact]
  818. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  819. {
  820. var view = new View ();
  821. view.Add (new View { Frame = new (0, 0, 5, 5) }); // Smaller subview
  822. view.Add (new View { Frame = new (0, 0, 10, 10) }); // Larger subview
  823. Dim dim = Auto (DimAutoStyle.Content);
  824. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  825. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  826. }
  827. [Fact]
  828. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  829. {
  830. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  831. view.SetContentSize (new (5, 5));
  832. view.SetRelativeLayout (new (10, 10));
  833. Assert.Equal (new (5, 5), view.Frame.Size);
  834. }
  835. [Fact]
  836. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  837. {
  838. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  839. View subView = new ()
  840. {
  841. Width = 5,
  842. Height = 1
  843. };
  844. view.Add (subView);
  845. view.SetRelativeLayout (new (10, 10));
  846. Assert.Equal (new (5, 1), view.Frame.Size);
  847. Assert.Equal (new (0, 0), view.Frame.Location);
  848. view.X = 0;
  849. view.Y = Pos.AnchorEnd (1);
  850. view.SetRelativeLayout (new (10, 10));
  851. Assert.Equal (new (5, 1), view.Frame.Size);
  852. Assert.Equal (new (0, 9), view.Frame.Location);
  853. view.Y = Pos.AnchorEnd ();
  854. view.SetRelativeLayout (new (10, 10));
  855. Assert.Equal (new (5, 1), view.Frame.Size);
  856. Assert.Equal (new (0, 9), view.Frame.Location);
  857. view.Y = Pos.AnchorEnd () - 1;
  858. view.SetRelativeLayout (new (10, 10));
  859. Assert.Equal (new (5, 1), view.Frame.Size);
  860. Assert.Equal (new (0, 8), view.Frame.Location);
  861. }
  862. #endregion DimAutoStyle.Content tests
  863. // Test variations of Frame
  864. }