Dim.AutoTests.cs 33 KB

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