Dim.AutoTests.cs 29 KB

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