Dim.AutoTests.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. using System.Globalization;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. namespace Terminal.Gui.LayoutTests;
  6. public class DimAutoTests (ITestOutputHelper output)
  7. {
  8. private readonly ITestOutputHelper _output = output;
  9. private class DimAutoTestView : View
  10. {
  11. public DimAutoTestView ()
  12. {
  13. ValidatePosDim = true;
  14. Width = Dim.Auto ();
  15. Height = Dim.Auto ();
  16. }
  17. public DimAutoTestView (Dim width, Dim height)
  18. {
  19. ValidatePosDim = true;
  20. Width = width;
  21. Height = height;
  22. }
  23. public DimAutoTestView (string text, Dim width, Dim height)
  24. {
  25. ValidatePosDim = true;
  26. Text = text;
  27. Width = width;
  28. Height = height;
  29. }
  30. }
  31. // Test min - ensure that if min is specified in the DimAuto constructor it is honored
  32. [Fact]
  33. public void Min_Is_Honored ()
  34. {
  35. var superView = new View
  36. {
  37. X = 0,
  38. Y = 0,
  39. Width = Dim.Auto (minimumContentDim: 10),
  40. Height = Dim.Auto (minimumContentDim: 10),
  41. ValidatePosDim = true
  42. };
  43. var subView = new View
  44. {
  45. X = 0,
  46. Y = 0,
  47. Width = 5,
  48. Height = 5
  49. };
  50. superView.Add (subView);
  51. superView.BeginInit ();
  52. superView.EndInit ();
  53. superView.SetRelativeLayout (new (10, 10));
  54. superView.LayoutSubviews (); // no throw
  55. Assert.Equal (10, superView.Frame.Width);
  56. Assert.Equal (10, superView.Frame.Height);
  57. }
  58. [Theory]
  59. [InlineData (0, 2, 4)]
  60. [InlineData (1, 2, 4)]
  61. [InlineData (2, 2, 4)]
  62. [InlineData (3, 2, 5)]
  63. [InlineData (1, 0, 3)]
  64. public void Min_Absolute_Is_Content_Relative (int contentSize, int minAbsolute, int expected)
  65. {
  66. var view = new View
  67. {
  68. X = 0,
  69. Y = 0,
  70. Width = Dim.Auto (minimumContentDim: minAbsolute),
  71. BorderStyle = LineStyle.Single, // a 1 thick adornment
  72. ValidatePosDim = true
  73. };
  74. view.SetContentSize (new (contentSize, 0));
  75. Assert.Equal (expected, view.Frame.Width);
  76. }
  77. [Theory]
  78. [InlineData (1, 100, 100)]
  79. [InlineData (1, 50, 50)]
  80. public void Min_Percent (int contentSize, int minPercent, int expected)
  81. {
  82. var view = new View
  83. {
  84. X = 0,
  85. Y = 0,
  86. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  87. ValidatePosDim = true
  88. };
  89. view.SetContentSize (new (contentSize, 0));
  90. view.SetRelativeLayout (new (100, 100));
  91. Assert.Equal (expected, view.Frame.Width);
  92. }
  93. [Theory]
  94. [InlineData (1, 100, 102)]
  95. [InlineData (1, 50, 52)] // 50% of 100 is 50, but the border adds 2
  96. [InlineData (1, 30, 32)] // 30% of 100 is 30, but the border adds 2
  97. [InlineData (2, 30, 32)] // 30% of 100 is 30, but the border adds 2
  98. public void Min_Percent_Is_Content_Relative (int contentSize, int minPercent, int expected)
  99. {
  100. var view = new View
  101. {
  102. X = 0,
  103. Y = 0,
  104. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  105. BorderStyle = LineStyle.Single, // a 1 thick adornment
  106. ValidatePosDim = true
  107. };
  108. view.SetContentSize (new (contentSize, 0));
  109. view.SetRelativeLayout (new (100, 100));
  110. Assert.Equal (expected, view.Frame.Width);
  111. }
  112. // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
  113. [Fact]
  114. public void Min_Resets_If_Subview_Moves_Negative ()
  115. {
  116. var superView = new View
  117. {
  118. X = 0,
  119. Y = 0,
  120. Width = Dim.Auto (minimumContentDim: 10),
  121. Height = Dim.Auto (minimumContentDim: 10),
  122. ValidatePosDim = true
  123. };
  124. var subView = new View
  125. {
  126. X = 0,
  127. Y = 0,
  128. Width = 5,
  129. Height = 5
  130. };
  131. superView.Add (subView);
  132. superView.BeginInit ();
  133. superView.EndInit ();
  134. superView.SetRelativeLayout (new (10, 10));
  135. superView.LayoutSubviews (); // no throw
  136. Assert.Equal (10, superView.Frame.Width);
  137. Assert.Equal (10, superView.Frame.Height);
  138. subView.X = -1;
  139. subView.Y = -1;
  140. superView.SetRelativeLayout (new (10, 10));
  141. superView.LayoutSubviews (); // no throw
  142. Assert.Equal (5, subView.Frame.Width);
  143. Assert.Equal (5, subView.Frame.Height);
  144. Assert.Equal (10, superView.Frame.Width);
  145. Assert.Equal (10, superView.Frame.Height);
  146. }
  147. [Fact]
  148. public void Min_Resets_If_Subview_Shrinks ()
  149. {
  150. var superView = new View
  151. {
  152. X = 0,
  153. Y = 0,
  154. Width = Dim.Auto (minimumContentDim: 10),
  155. Height = Dim.Auto (minimumContentDim: 10),
  156. ValidatePosDim = true
  157. };
  158. var subView = new View
  159. {
  160. X = 0,
  161. Y = 0,
  162. Width = 5,
  163. Height = 5
  164. };
  165. superView.Add (subView);
  166. superView.BeginInit ();
  167. superView.EndInit ();
  168. superView.SetRelativeLayout (new (10, 10));
  169. superView.LayoutSubviews (); // no throw
  170. Assert.Equal (10, superView.Frame.Width);
  171. Assert.Equal (10, superView.Frame.Height);
  172. subView.Width = 3;
  173. subView.Height = 3;
  174. superView.SetRelativeLayout (new (10, 10));
  175. superView.LayoutSubviews (); // no throw
  176. Assert.Equal (3, subView.Frame.Width);
  177. Assert.Equal (3, subView.Frame.Height);
  178. Assert.Equal (10, superView.Frame.Width);
  179. Assert.Equal (10, superView.Frame.Height);
  180. }
  181. [Theory]
  182. [InlineData (0, 0, 0, 0, 0)]
  183. [InlineData (0, 0, 5, 0, 0)]
  184. [InlineData (0, 0, 0, 5, 5)]
  185. [InlineData (0, 0, 5, 5, 5)]
  186. [InlineData (1, 0, 5, 0, 0)]
  187. [InlineData (1, 0, 0, 5, 5)]
  188. [InlineData (1, 0, 5, 5, 5)]
  189. [InlineData (1, 1, 5, 5, 6)]
  190. [InlineData (-1, 0, 5, 0, 0)]
  191. [InlineData (-1, 0, 0, 5, 5)]
  192. [InlineData (-1, 0, 5, 5, 5)]
  193. [InlineData (-1, -1, 5, 5, 4)]
  194. public void Height_Auto_Width_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  195. {
  196. var superView = new View
  197. {
  198. X = 0,
  199. Y = 0,
  200. Width = 10,
  201. Height = Dim.Auto (),
  202. ValidatePosDim = true
  203. };
  204. var subView = new View
  205. {
  206. X = subX,
  207. Y = subY,
  208. Width = subWidth,
  209. Height = subHeight,
  210. ValidatePosDim = true
  211. };
  212. superView.Add (subView);
  213. superView.BeginInit ();
  214. superView.EndInit ();
  215. superView.SetRelativeLayout (new (10, 10));
  216. Assert.Equal (new Rectangle (0, 0, 10, expectedHeight), superView.Frame);
  217. }
  218. [Fact]
  219. public void NoSubViews_Does_Nothing ()
  220. {
  221. var superView = new View
  222. {
  223. X = 0,
  224. Y = 0,
  225. Width = Dim.Auto (),
  226. Height = Dim.Auto (),
  227. ValidatePosDim = true
  228. };
  229. superView.BeginInit ();
  230. superView.EndInit ();
  231. superView.SetRelativeLayout (new (10, 10));
  232. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  233. superView.SetRelativeLayout (new (10, 10));
  234. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  235. }
  236. [Fact]
  237. public void NoSubViews_Does_Nothing_Vertical ()
  238. {
  239. var superView = new View
  240. {
  241. X = 0,
  242. Y = 0,
  243. Width = Dim.Auto (),
  244. Height = Dim.Auto (),
  245. TextDirection = TextDirection.TopBottom_LeftRight,
  246. ValidatePosDim = true
  247. };
  248. superView.BeginInit ();
  249. superView.EndInit ();
  250. superView.SetRelativeLayout (new (10, 10));
  251. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  252. superView.SetRelativeLayout (new (10, 10));
  253. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  254. }
  255. [Theory]
  256. [InlineData (0, 0, 0, 0, 0, 0)]
  257. [InlineData (0, 0, 5, 0, 5, 0)]
  258. [InlineData (0, 0, 0, 5, 0, 5)]
  259. [InlineData (0, 0, 5, 5, 5, 5)]
  260. [InlineData (1, 0, 5, 0, 6, 0)]
  261. [InlineData (1, 0, 0, 5, 1, 5)]
  262. [InlineData (1, 0, 5, 5, 6, 5)]
  263. [InlineData (1, 1, 5, 5, 6, 6)]
  264. [InlineData (-1, 0, 5, 0, 4, 0)]
  265. [InlineData (-1, 0, 0, 5, 0, 5)]
  266. [InlineData (-1, 0, 5, 5, 4, 5)]
  267. [InlineData (-1, -1, 5, 5, 4, 4)]
  268. public void SubView_Changes_SuperView_Size (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  269. {
  270. var superView = new View
  271. {
  272. X = 0,
  273. Y = 0,
  274. Width = Dim.Auto (),
  275. Height = Dim.Auto (),
  276. ValidatePosDim = true
  277. };
  278. var subView = new View
  279. {
  280. X = subX,
  281. Y = subY,
  282. Width = subWidth,
  283. Height = subHeight,
  284. ValidatePosDim = true
  285. };
  286. superView.Add (subView);
  287. superView.BeginInit ();
  288. superView.EndInit ();
  289. superView.SetRelativeLayout (new (10, 10));
  290. Assert.Equal (new Rectangle (0, 0, expectedWidth, expectedHeight), superView.Frame);
  291. }
  292. // Test validation
  293. [Fact]
  294. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  295. {
  296. var superView = new View
  297. {
  298. X = 0,
  299. Y = 0,
  300. Width = Dim.Auto (),
  301. Height = Dim.Auto (),
  302. ValidatePosDim = true
  303. };
  304. var subView = new View
  305. {
  306. X = 0,
  307. Y = 0,
  308. Width = Dim.Fill (),
  309. Height = 10,
  310. ValidatePosDim = true
  311. };
  312. superView.BeginInit ();
  313. superView.EndInit ();
  314. superView.Add (subView);
  315. subView.Width = 10;
  316. superView.Add (subView);
  317. superView.SetRelativeLayout (new (10, 10));
  318. superView.LayoutSubviews (); // no throw
  319. subView.Width = Dim.Fill ();
  320. superView.SetRelativeLayout (new (0, 0));
  321. subView.Width = 10;
  322. subView.Height = Dim.Fill ();
  323. superView.SetRelativeLayout (new (0, 0));
  324. subView.Height = 10;
  325. subView.Height = Dim.Percent (50);
  326. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  327. subView.Height = 10;
  328. subView.X = Pos.Center ();
  329. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  330. subView.X = 0;
  331. subView.Y = Pos.Center ();
  332. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  333. subView.Y = 0;
  334. subView.Width = 10;
  335. subView.Height = 10;
  336. subView.X = 0;
  337. subView.Y = 0;
  338. superView.SetRelativeLayout (new (0, 0));
  339. superView.LayoutSubviews ();
  340. }
  341. // Test validation
  342. [Fact]
  343. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  344. {
  345. var superView = new View
  346. {
  347. X = 0,
  348. Y = 0,
  349. Width = Dim.Auto (),
  350. Height = Dim.Auto (),
  351. ValidatePosDim = true
  352. };
  353. var subView = new View
  354. {
  355. X = 0,
  356. Y = 0,
  357. Width = 10,
  358. Height = 10
  359. };
  360. var subView2 = new View
  361. {
  362. X = 0,
  363. Y = 0,
  364. Width = 10,
  365. Height = 10
  366. };
  367. superView.Add (subView, subView2);
  368. superView.BeginInit ();
  369. superView.EndInit ();
  370. superView.SetRelativeLayout (new (0, 0));
  371. superView.LayoutSubviews (); // no throw
  372. subView.Height = Dim.Fill () + 3;
  373. superView.SetRelativeLayout (new (0, 0));
  374. subView.Height = 0;
  375. subView.Height = 3 + Dim.Fill ();
  376. superView.SetRelativeLayout (new (0, 0));
  377. subView.Height = 0;
  378. subView.Height = 3 + 5 + Dim.Fill ();
  379. superView.SetRelativeLayout (new (0, 0));
  380. subView.Height = 0;
  381. subView.Height = 3 + 5 + Dim.Percent (10);
  382. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  383. subView.Height = 0;
  384. // Tests nested Combine
  385. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Dim.Percent (10), 9));
  386. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  387. }
  388. [Fact]
  389. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  390. {
  391. var superView = new View
  392. {
  393. X = 0,
  394. Y = 0,
  395. Width = Dim.Auto (),
  396. Height = Dim.Auto (),
  397. ValidatePosDim = true
  398. };
  399. var subView = new View
  400. {
  401. X = 0,
  402. Y = 0,
  403. Width = 10,
  404. Height = 10
  405. };
  406. var subView2 = new View
  407. {
  408. X = 0,
  409. Y = 0,
  410. Width = 10,
  411. Height = 10
  412. };
  413. superView.Add (subView, subView2);
  414. superView.BeginInit ();
  415. superView.EndInit ();
  416. superView.SetRelativeLayout (new (0, 0));
  417. superView.LayoutSubviews (); // no throw
  418. subView.X = Pos.Right (subView2);
  419. superView.SetRelativeLayout (new (0, 0));
  420. superView.LayoutSubviews (); // no throw
  421. subView.X = Pos.Right (subView2) + 3;
  422. superView.SetRelativeLayout (new (0, 0)); // no throw
  423. superView.LayoutSubviews (); // no throw
  424. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  425. superView.SetRelativeLayout (new (0, 0)); // no throw
  426. subView.X = Pos.Center () + 3;
  427. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  428. subView.X = 0;
  429. subView.X = 3 + Pos.Center ();
  430. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  431. subView.X = 0;
  432. subView.X = 3 + 5 + Pos.Center ();
  433. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  434. subView.X = 0;
  435. subView.X = 3 + 5 + Pos.Percent (10);
  436. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  437. subView.X = 0;
  438. subView.X = Pos.Percent (10) + Pos.Center ();
  439. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  440. subView.X = 0;
  441. // Tests nested Combine
  442. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  443. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  444. subView.X = 0;
  445. }
  446. [Theory]
  447. [InlineData (0, 0, 0, 0, 0)]
  448. [InlineData (0, 0, 5, 0, 5)]
  449. [InlineData (0, 0, 0, 5, 0)]
  450. [InlineData (0, 0, 5, 5, 5)]
  451. [InlineData (1, 0, 5, 0, 6)]
  452. [InlineData (1, 0, 0, 5, 1)]
  453. [InlineData (1, 0, 5, 5, 6)]
  454. [InlineData (1, 1, 5, 5, 6)]
  455. [InlineData (-1, 0, 5, 0, 4)]
  456. [InlineData (-1, 0, 0, 5, 0)]
  457. [InlineData (-1, 0, 5, 5, 4)]
  458. [InlineData (-1, -1, 5, 5, 4)]
  459. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  460. {
  461. var superView = new View
  462. {
  463. X = 0,
  464. Y = 0,
  465. Width = Dim.Auto (),
  466. Height = 10,
  467. ValidatePosDim = true
  468. };
  469. var subView = new View
  470. {
  471. X = subX,
  472. Y = subY,
  473. Width = subWidth,
  474. Height = subHeight,
  475. ValidatePosDim = true
  476. };
  477. superView.Add (subView);
  478. superView.BeginInit ();
  479. superView.EndInit ();
  480. superView.SetRelativeLayout (new (10, 10));
  481. Assert.Equal (new Rectangle (0, 0, expectedWidth, 10), superView.Frame);
  482. }
  483. // Test that when a view has Width set to DimAuto (min: x)
  484. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  485. [Theory]
  486. [InlineData (0, 0)]
  487. [InlineData (1, 1)]
  488. [InlineData (3, 3)]
  489. [InlineData (4, 4)]
  490. [InlineData (5, 5)] // No reason why it can exceed container
  491. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  492. {
  493. var superView = new View
  494. {
  495. X = 0,
  496. Y = 0,
  497. Width = Dim.Auto (minimumContentDim: min),
  498. Height = 1,
  499. ValidatePosDim = true
  500. };
  501. superView.BeginInit ();
  502. superView.EndInit ();
  503. superView.SetRelativeLayout (new (4, 1));
  504. Assert.Equal (expectedWidth, superView.Frame.Width);
  505. }
  506. [Theory]
  507. [InlineData (0, 1, 1)]
  508. [InlineData (1, 1, 1)]
  509. [InlineData (9, 1, 1)]
  510. [InlineData (10, 1, 1)]
  511. [InlineData (0, 10, 10)]
  512. [InlineData (1, 10, 10)]
  513. [InlineData (9, 10, 10)]
  514. [InlineData (10, 10, 10)]
  515. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  516. {
  517. var superView = new View
  518. {
  519. X = 0,
  520. Y = 0,
  521. Width = 10,
  522. Height = 1,
  523. ValidatePosDim = true
  524. };
  525. var subView = new View
  526. {
  527. Text = new string ('*', textLen),
  528. X = subX,
  529. Y = 0,
  530. Width = Dim.Auto (DimAutoStyle.Text),
  531. Height = 1,
  532. ValidatePosDim = true
  533. };
  534. superView.Add (subView);
  535. superView.BeginInit ();
  536. superView.EndInit ();
  537. superView.SetRelativeLayout (superView.GetContentSize ());
  538. superView.LayoutSubviews ();
  539. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  540. }
  541. [Theory]
  542. [InlineData (0, 1, 1)]
  543. [InlineData (1, 1, 1)]
  544. [InlineData (9, 1, 1)]
  545. [InlineData (10, 1, 1)]
  546. [InlineData (0, 10, 10)]
  547. [InlineData (1, 10, 10)]
  548. [InlineData (9, 10, 10)]
  549. [InlineData (10, 10, 10)]
  550. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  551. {
  552. var superView = new View
  553. {
  554. X = 0,
  555. Y = 0,
  556. Width = 10,
  557. Height = 1,
  558. ValidatePosDim = true
  559. };
  560. var subView = new View
  561. {
  562. X = subX,
  563. Y = 0,
  564. Width = Dim.Auto (DimAutoStyle.Content),
  565. Height = 1,
  566. ValidatePosDim = true
  567. };
  568. var subSubView = new View
  569. {
  570. X = 0,
  571. Y = 0,
  572. Width = subSubViewWidth,
  573. Height = 1,
  574. ValidatePosDim = true
  575. };
  576. subView.Add (subSubView);
  577. superView.Add (subView);
  578. superView.BeginInit ();
  579. superView.EndInit ();
  580. superView.SetRelativeLayout (superView.GetContentSize ());
  581. superView.LayoutSubviews ();
  582. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  583. }
  584. #region DimAutoStyle.Auto tests
  585. [Fact]
  586. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  587. {
  588. var view = new View ()
  589. {
  590. Text = "0123\n4567",
  591. Width = Auto (),
  592. Height = Auto (),
  593. };
  594. view.SetRelativeLayout (new Size (100, 100));
  595. Assert.Equal (new (4, 2), view.Frame.Size);
  596. var subView = new View ()
  597. {
  598. Text = "ABCD",
  599. Width = Auto (),
  600. Height = Auto (),
  601. };
  602. view.Add (subView);
  603. view.SetRelativeLayout (new Size (100, 100));
  604. Assert.Equal (new (4, 2), view.Frame.Size);
  605. subView.Text = "ABCDE";
  606. view.SetRelativeLayout (new Size (100, 100));
  607. Assert.Equal (new (5, 2), view.Frame.Size);
  608. }
  609. #endregion
  610. [Fact]
  611. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  612. {
  613. var super = new View ()
  614. {
  615. Width = Dim.Fill (),
  616. Height = Dim.Fill ()
  617. };
  618. var view = new View ()
  619. {
  620. Text = "01234567",
  621. Width = Auto (DimAutoStyle.Text),
  622. Height = Auto (DimAutoStyle.Text),
  623. };
  624. super.Add (view);
  625. Rectangle expectedViewport = new (0, 0, 8, 1);
  626. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  627. Assert.Equal (expectedViewport, view.Frame);
  628. Assert.Equal (expectedViewport, view.Viewport);
  629. super.LayoutSubviews ();
  630. Assert.Equal (expectedViewport, view.Viewport);
  631. super.Dispose ();
  632. }
  633. [Theory]
  634. [InlineData ("", 0, 0)]
  635. [InlineData (" ", 1, 1)]
  636. [InlineData ("01234", 5, 1)]
  637. public void DimAutoStyle_Text_Ignores_ContentSize (string text, int expectedW, int expectedH)
  638. {
  639. var view = new View ();
  640. view.Width = Auto (DimAutoStyle.Text);
  641. view.Height = Auto (DimAutoStyle.Text);
  642. view.SetContentSize (new (1, 1));
  643. view.Text = text;
  644. view.SetRelativeLayout(Application.Screen.Size);
  645. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  646. }
  647. [Fact]
  648. public void TextFormatter_Settings_Change_View_Size ()
  649. {
  650. View view = new ()
  651. {
  652. Text = "_1234",
  653. Width = Dim.Auto ()
  654. };
  655. Assert.Equal (Size.Empty, view.Frame.Size); // Height is 0, so width is 0 regardless of text
  656. view.Height = 1;
  657. view.SetRelativeLayout (Application.Screen.Size);
  658. Assert.Equal (new Size (4, 1), view.Frame.Size);
  659. Size lastSize = view.Frame.Size;
  660. view.TextAlignment = Alignment.Fill;
  661. Assert.Equal (lastSize, view.Frame.Size);
  662. view = new ()
  663. {
  664. Text = "_1234",
  665. Width = Dim.Auto (),
  666. Height = 1
  667. };
  668. view.SetRelativeLayout (Application.Screen.Size);
  669. lastSize = view.Frame.Size;
  670. view.VerticalTextAlignment = Alignment.Center;
  671. Assert.Equal (lastSize, view.Frame.Size);
  672. view = new ()
  673. {
  674. Text = "_1234",
  675. Width = Dim.Auto (),
  676. Height = 1,
  677. };
  678. view.SetRelativeLayout (Application.Screen.Size);
  679. lastSize = view.Frame.Size;
  680. view.HotKeySpecifier = (Rune)'*';
  681. view.SetRelativeLayout (Application.Screen.Size);
  682. Assert.NotEqual (lastSize, view.Frame.Size);
  683. view = new ()
  684. {
  685. Text = "_1234",
  686. Width = Dim.Auto (),
  687. Height = 1
  688. };
  689. view.SetRelativeLayout (Application.Screen.Size);
  690. lastSize = view.Frame.Size;
  691. view.Text = "*ABCD";
  692. Assert.NotEqual (lastSize, view.Frame.Size);
  693. }
  694. // Ensure TextFormatter.AutoSize is never used for View.Text
  695. [Fact]
  696. public void TextFormatter_Is_Not_Auto ()
  697. {
  698. View view = new ();
  699. Assert.False (view.TextFormatter.AutoSize);
  700. view.Width = Dim.Auto ();
  701. Assert.False (view.TextFormatter.AutoSize);
  702. view = new ();
  703. Assert.False (view.TextFormatter.AutoSize);
  704. view.Height = Dim.Auto ();
  705. Assert.False (view.TextFormatter.AutoSize);
  706. }
  707. [Theory]
  708. [InlineData ("1234", 4)]
  709. [InlineData ("_1234", 4)]
  710. public void Width_Auto_HotKey_TextFormatter_Size_Correct (string text, int expected)
  711. {
  712. View view = new ()
  713. {
  714. Text = text,
  715. Height = 1,
  716. Width = Dim.Auto ()
  717. };
  718. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  719. }
  720. [Theory]
  721. [InlineData ("1234", 4)]
  722. [InlineData ("_1234", 4)]
  723. public void Height_Auto_HotKey_TextFormatter_Size_Correct (string text, int expected)
  724. {
  725. View view = new ()
  726. {
  727. HotKeySpecifier = (Rune)'_',
  728. Text = text,
  729. Width = Auto (),
  730. Height = 1,
  731. };
  732. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  733. view = new ()
  734. {
  735. HotKeySpecifier = (Rune)'_',
  736. TextDirection = TextDirection.TopBottom_LeftRight,
  737. Text = text,
  738. Width = 1,
  739. Height = Auto (),
  740. };
  741. Assert.Equal (new (1, expected), view.TextFormatter.Size);
  742. }
  743. [SetupFakeDriver]
  744. [Fact]
  745. public void Change_To_Non_Auto_Resets_ContentSize ()
  746. {
  747. View view = new ()
  748. {
  749. Width = Auto (),
  750. Height = Auto (),
  751. Text = "01234"
  752. };
  753. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Frame);
  754. Assert.Equal (new Size (5, 1), view.GetContentSize ());
  755. // Change text to a longer string
  756. view.Text = "0123456789";
  757. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Frame);
  758. Assert.Equal (new Size (10, 1), view.GetContentSize ());
  759. // If ContentSize was reset, these should cause it to update
  760. view.Width = 5;
  761. view.Height = 1;
  762. Assert.Equal (new Size (5, 1), view.GetContentSize ());
  763. }
  764. // DimAutoStyle.Content tests
  765. [Fact]
  766. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  767. {
  768. var view = new View ();
  769. view.SetContentSize (new (10, 5));
  770. var dim = Dim.Auto (DimAutoStyle.Content);
  771. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  772. Assert.Equal (10, calculatedWidth);
  773. }
  774. [Fact]
  775. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  776. {
  777. var view = new View ();
  778. var subview = new View ()
  779. {
  780. Frame = new Rectangle (50, 50, 1, 1)
  781. };
  782. view.SetContentSize (new (10, 5));
  783. var dim = Dim.Auto (DimAutoStyle.Content);
  784. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  785. Assert.Equal (10, calculatedWidth);
  786. }
  787. [Fact]
  788. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  789. {
  790. var view = new View () { Text = "This is a test" };
  791. var dim = Dim.Auto (DimAutoStyle.Content);
  792. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  793. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  794. }
  795. [Fact]
  796. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  797. {
  798. var view = new View ();
  799. view.Add (new View () { Frame = new Rectangle (0, 0, 5, 5) }); // Smaller subview
  800. view.Add (new View () { Frame = new Rectangle (0, 0, 10, 10) }); // Larger subview
  801. var dim = Dim.Auto (DimAutoStyle.Content);
  802. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  803. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  804. }
  805. // All the Dim types
  806. [Theory]
  807. [InlineData (0, 15, 15)]
  808. [InlineData (1, 15, 16)]
  809. [InlineData (-1, 15, 14)]
  810. public void With_Subview_Using_DimAbsolute (int subViewOffset, int dimAbsoluteSize, int expectedSize)
  811. {
  812. var view = new View ();
  813. var subview = new View ()
  814. {
  815. X = subViewOffset,
  816. Y = subViewOffset,
  817. Width = Dim.Absolute (dimAbsoluteSize),
  818. Height = Dim.Absolute (dimAbsoluteSize)
  819. };
  820. view.Add (subview);
  821. var dim = Dim.Auto (DimAutoStyle.Content);
  822. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  823. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  824. Assert.Equal (expectedSize, calculatedWidth);
  825. Assert.Equal (expectedSize, calculatedHeight);
  826. }
  827. [Theory]
  828. [InlineData (0, 0, 0, 0, 0, 0, 0, 0)]
  829. [InlineData (0, 50, 0, 0, 0, 0, 0, 0)]
  830. [InlineData (0, 0, 100, 100, 100, 100, 100, 100)]
  831. [InlineData (0, 50, 100, 100, 100, 100, 100, 100)]
  832. public void With_Subview_Using_DimPercent (int subViewOffset, int percent, int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  833. {
  834. var view = new View ()
  835. {
  836. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  837. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  838. };
  839. var subview = new View ()
  840. {
  841. X = subViewOffset,
  842. Y = subViewOffset,
  843. Width = Dim.Percent (percent),
  844. Height = Dim.Percent (percent)
  845. };
  846. view.Add (subview);
  847. // Assuming the calculation is done after layout
  848. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  849. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  850. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  851. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  852. Assert.Equal (expectedWidth, calculatedWidth); // subview's width
  853. Assert.Equal (expectedHeight, calculatedHeight); // subview's height
  854. Assert.Equal (subViewOffset, calculatedX);
  855. Assert.Equal (subViewOffset, calculatedY);
  856. view.SetRelativeLayout (new (100, 100));
  857. view.LayoutSubviews ();
  858. Assert.Equal (expectedWidth * (percent / 100f), subview.Viewport.Width);
  859. Assert.Equal (expectedHeight * (percent / 100f), subview.Viewport.Height);
  860. }
  861. [Theory]
  862. [InlineData (0, 0, 0, 0, 0, 0)]
  863. [InlineData (0, 19, 0, 9, 0, 0)]
  864. [InlineData (0, 20, 0, 10, 0, 0)]
  865. [InlineData (0, 21, 0, 11, 0, 0)]
  866. [InlineData (1, 21, 1, 11, 1, 1)]
  867. [InlineData (21, 21, 11, 11, 21, 11)]
  868. public void With_Subview_Using_DimFill (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  869. {
  870. var view = new View ()
  871. {
  872. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  873. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  874. };
  875. var subview = new View ()
  876. {
  877. X = 0,
  878. Y = 0,
  879. Width = Fill (),
  880. Height = Fill ()
  881. };
  882. view.Add (subview);
  883. // Assuming the calculation is done after layout
  884. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  885. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  886. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  887. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  888. Assert.Equal (expectedWidth, calculatedWidth);
  889. Assert.Equal (expectedHeight, calculatedHeight);
  890. Assert.Equal (0, calculatedX);
  891. Assert.Equal (0, calculatedY);
  892. }
  893. [Theory]
  894. [InlineData (0, 0, 0, 0, 0, 0)]
  895. [InlineData (0, 19, 0, 9, 2, 4)]
  896. [InlineData (0, 20, 0, 10, 2, 4)]
  897. [InlineData (0, 21, 0, 11, 2, 4)]
  898. [InlineData (1, 21, 1, 11, 2, 4)]
  899. [InlineData (21, 21, 11, 11, 21, 11)]
  900. public void With_Subview_Using_DimFill_And_Another_Subview (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  901. {
  902. var view = new View ()
  903. {
  904. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  905. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  906. };
  907. var absView = new View ()
  908. {
  909. X = 1,
  910. Y = 2,
  911. Width = 1,
  912. Height = 2
  913. };
  914. view.Add (absView);
  915. var subview = new View ()
  916. {
  917. X = 0,
  918. Y = 0,
  919. Width = Fill (),
  920. Height = Fill ()
  921. };
  922. view.Add (subview);
  923. // Assuming the calculation is done after layout
  924. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  925. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  926. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  927. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  928. Assert.Equal (expectedWidth, calculatedWidth);
  929. Assert.Equal (expectedHeight, calculatedHeight);
  930. Assert.Equal (0, calculatedX);
  931. Assert.Equal (0, calculatedY);
  932. }
  933. [Fact]
  934. public void With_Subview_Using_DimFunc ()
  935. {
  936. var view = new View ();
  937. var subview = new View () { Width = Dim.Func (() => 20), Height = Dim.Func (() => 25) };
  938. view.Add (subview);
  939. subview.SetRelativeLayout (new (100, 100));
  940. var dim = Dim.Auto (DimAutoStyle.Content);
  941. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  942. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  943. Assert.Equal (20, calculatedWidth);
  944. Assert.Equal (25, calculatedHeight);
  945. }
  946. [Fact]
  947. public void With_Subview_Using_DimView ()
  948. {
  949. var view = new View ();
  950. var subview = new View () { Width = 30, Height = 40 };
  951. var subSubview = new View () { Width = Dim.Width (subview), Height = Dim.Height (subview) };
  952. view.Add (subview);
  953. view.Add (subSubview);
  954. subview.SetRelativeLayout (new (100, 100));
  955. var dim = Dim.Auto (DimAutoStyle.Content);
  956. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  957. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  958. // Expecting the size to match the subview, which is the largest
  959. Assert.Equal (30, calculatedWidth);
  960. Assert.Equal (40, calculatedHeight);
  961. }
  962. // Testing all Pos combinations
  963. [Theory]
  964. [InlineData (0, 0, 0, 0, 0, 0)]
  965. [InlineData (0, 19, 0, 9, 19, 9)]
  966. [InlineData (0, 20, 0, 10, 20, 10)]
  967. [InlineData (0, 21, 0, 11, 21, 11)]
  968. [InlineData (1, 21, 1, 11, 21, 11)]
  969. [InlineData (21, 21, 11, 11, 21, 11)]
  970. public void With_Subview_Using_PosAbsolute (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  971. {
  972. var view = new View ()
  973. {
  974. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  975. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  976. };
  977. var subview = new View ()
  978. {
  979. X = Pos.Absolute (10),
  980. Y = Pos.Absolute (5),
  981. Width = 20,
  982. Height = 10
  983. };
  984. view.Add (subview);
  985. // Assuming the calculation is done after layout
  986. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  987. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  988. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  989. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  990. Assert.Equal (expectedWidth, calculatedWidth);
  991. Assert.Equal (expectedHeight, calculatedHeight);
  992. Assert.Equal (0, calculatedX);
  993. Assert.Equal (0, calculatedY);
  994. }
  995. [Theory]
  996. [InlineData (0, 0, 0, 0, 0, 0)]
  997. [InlineData (0, 19, 0, 9, 19, 9)]
  998. [InlineData (0, 20, 0, 10, 20, 10)]
  999. [InlineData (0, 21, 0, 11, 20, 10)]
  1000. [InlineData (1, 21, 1, 11, 20, 10)]
  1001. [InlineData (21, 21, 11, 11, 21, 11)]
  1002. public void With_Subview_Using_PosPercent (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1003. {
  1004. var view = new View ()
  1005. {
  1006. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1007. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1008. };
  1009. var subview = new View ()
  1010. {
  1011. X = Pos.Percent (50),
  1012. Y = Pos.Percent (50),
  1013. Width = 20,
  1014. Height = 10
  1015. };
  1016. view.Add (subview);
  1017. // Assuming the calculation is done after layout
  1018. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1019. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1020. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1021. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1022. Assert.Equal (expectedWidth, calculatedWidth);
  1023. Assert.Equal (expectedHeight, calculatedHeight);
  1024. Assert.Equal (0, calculatedX);
  1025. Assert.Equal (0, calculatedY);
  1026. view.BeginInit ();
  1027. view.EndInit ();
  1028. // subview should be at 50% in the parent view
  1029. Assert.Equal ((int)(view.Viewport.Width * .50), subview.Frame.X);
  1030. Assert.Equal ((int)(view.Viewport.Height * .50), subview.Frame.Y);
  1031. }
  1032. [Theory]
  1033. [InlineData (0, 0, 0, 0, 0, 0)]
  1034. [InlineData (0, 19, 0, 9, 19, 9)]
  1035. [InlineData (0, 20, 0, 10, 20, 10)]
  1036. [InlineData (0, 21, 0, 11, 21, 11)]
  1037. [InlineData (1, 21, 1, 11, 21, 11)]
  1038. [InlineData (21, 21, 11, 11, 21, 11)]
  1039. public void With_Subview_Using_PosPercent_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1040. {
  1041. var view = new View ()
  1042. {
  1043. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1044. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1045. };
  1046. var subview = new View ()
  1047. {
  1048. X = Pos.Percent (50) + 1,
  1049. Y = 1 + Pos.Percent (50),
  1050. Width = 20,
  1051. Height = 10
  1052. };
  1053. view.Add (subview);
  1054. // Assuming the calculation is done after layout
  1055. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1056. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1057. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1058. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1059. Assert.Equal (expectedWidth, calculatedWidth);
  1060. Assert.Equal (expectedHeight, calculatedHeight);
  1061. Assert.Equal (0, calculatedX);
  1062. Assert.Equal (0, calculatedY);
  1063. view.BeginInit ();
  1064. view.EndInit ();
  1065. // subview should be at 50% in the parent view
  1066. Assert.Equal ((int)(view.Viewport.Width * .50) + 1, subview.Frame.X);
  1067. Assert.Equal ((int)(view.Viewport.Height * .50) + 1, subview.Frame.Y);
  1068. }
  1069. [Theory]
  1070. [InlineData (0, 0, 0, 0, 0, 0)]
  1071. [InlineData (0, 19, 0, 9, 19, 9)]
  1072. [InlineData (0, 20, 0, 10, 20, 10)]
  1073. [InlineData (0, 21, 0, 11, 20, 10)]
  1074. [InlineData (1, 21, 1, 11, 20, 10)]
  1075. [InlineData (21, 21, 11, 11, 21, 11)]
  1076. public void With_Subview_Using_PosCenter (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1077. {
  1078. var view = new View ()
  1079. {
  1080. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1081. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1082. };
  1083. var subview = new View ()
  1084. {
  1085. X = Pos.Center (),
  1086. Y = Pos.Center (),
  1087. Width = 20,
  1088. Height = 10
  1089. };
  1090. view.Add (subview);
  1091. // Assuming the calculation is done after layout
  1092. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1093. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1094. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1095. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1096. Assert.Equal (expectedWidth, calculatedWidth);
  1097. Assert.Equal (expectedHeight, calculatedHeight);
  1098. Assert.Equal (0, calculatedX);
  1099. Assert.Equal (0, calculatedY);
  1100. view.BeginInit ();
  1101. view.EndInit ();
  1102. // subview should be centered in the parent view + 1
  1103. Assert.Equal (view.Viewport.Width / 2 - subview.Frame.Width / 2, subview.Frame.X);
  1104. Assert.Equal (view.Viewport.Height / 2 - subview.Frame.Height / 2, subview.Frame.Y);
  1105. }
  1106. [Theory]
  1107. [InlineData (0, 0, 0, 0, 0, 0)]
  1108. [InlineData (0, 19, 0, 9, 19, 9)]
  1109. [InlineData (0, 18, 0, 8, 18, 8)]
  1110. [InlineData (0, 20, 0, 10, 20, 10)]
  1111. [InlineData (0, 21, 0, 11, 21, 11)]
  1112. [InlineData (1, 21, 1, 11, 21, 11)]
  1113. [InlineData (21, 21, 11, 11, 21, 11)]
  1114. public void With_Subview_Using_PosCenter_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1115. {
  1116. var view = new View ()
  1117. {
  1118. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1119. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1120. };
  1121. var subview = new View ()
  1122. {
  1123. X = Pos.Center () + 1,
  1124. Y = 1 + Pos.Center (),
  1125. Width = 20,
  1126. Height = 10
  1127. };
  1128. view.Add (subview);
  1129. // Assuming the calculation is done after layout
  1130. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1131. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1132. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1133. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1134. Assert.Equal (expectedWidth, calculatedWidth);
  1135. Assert.Equal (expectedHeight, calculatedHeight);
  1136. Assert.Equal (0, calculatedX);
  1137. Assert.Equal (0, calculatedY);
  1138. view.BeginInit ();
  1139. view.EndInit ();
  1140. // subview should be centered in the parent view + 1
  1141. Assert.Equal (view.Viewport.Width / 2 - subview.Frame.Width / 2 + 1, subview.Frame.X);
  1142. Assert.Equal (view.Viewport.Height / 2 - subview.Frame.Height / 2 + 1, subview.Frame.Y);
  1143. }
  1144. [Theory]
  1145. [InlineData (0, 0, 0, 0, 0, 0)]
  1146. [InlineData (0, 19, 0, 9, 19, 9)]
  1147. [InlineData (0, 18, 0, 8, 18, 8)]
  1148. [InlineData (0, 20, 0, 10, 20, 10)]
  1149. [InlineData (0, 21, 0, 11, 20, 10)]
  1150. [InlineData (1, 21, 1, 11, 20, 10)]
  1151. [InlineData (21, 21, 11, 11, 21, 11)]
  1152. public void With_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1153. {
  1154. var view = new View ()
  1155. {
  1156. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1157. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1158. };
  1159. var subview = new View ()
  1160. {
  1161. X = Pos.AnchorEnd (),
  1162. Y = Pos.AnchorEnd (),
  1163. Width = 20,
  1164. Height = 10
  1165. };
  1166. view.Add (subview);
  1167. // Assuming the calculation is done after layout
  1168. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1169. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1170. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1171. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1172. Assert.Equal (expectedWidth, calculatedWidth);
  1173. Assert.Equal (expectedHeight, calculatedHeight);
  1174. Assert.Equal (0, calculatedX);
  1175. Assert.Equal (0, calculatedY);
  1176. view.BeginInit ();
  1177. view.EndInit ();
  1178. // subview should be at the end of the view
  1179. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  1180. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  1181. }
  1182. [Fact]
  1183. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  1184. {
  1185. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  1186. view.SetRelativeLayout (new (10, 10));
  1187. Assert.Equal (new (5, 1), view.Frame.Size);
  1188. Assert.Equal (new (0, 0), view.Frame.Location);
  1189. view.X = 0;
  1190. view.Y = Pos.AnchorEnd (1);
  1191. view.SetRelativeLayout (new (10, 10));
  1192. Assert.Equal (new (5, 1), view.Frame.Size);
  1193. Assert.Equal (new (0, 9), view.Frame.Location);
  1194. view.Y = Pos.AnchorEnd ();
  1195. view.SetRelativeLayout (new (10, 10));
  1196. Assert.Equal (new (5, 1), view.Frame.Size);
  1197. Assert.Equal (new (0, 9), view.Frame.Location);
  1198. view.Y = Pos.AnchorEnd () - 1;
  1199. view.SetRelativeLayout (new (10, 10));
  1200. Assert.Equal (new (5, 1), view.Frame.Size);
  1201. Assert.Equal (new (0, 8), view.Frame.Location);
  1202. }
  1203. [Fact]
  1204. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  1205. {
  1206. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  1207. View subView = new ()
  1208. {
  1209. Width = 5,
  1210. Height = 1
  1211. };
  1212. view.Add (subView);
  1213. view.SetRelativeLayout (new (10, 10));
  1214. Assert.Equal (new (5, 1), view.Frame.Size);
  1215. Assert.Equal (new (0, 0), view.Frame.Location);
  1216. view.X = 0;
  1217. view.Y = Pos.AnchorEnd (1);
  1218. view.SetRelativeLayout (new (10, 10));
  1219. Assert.Equal (new (5, 1), view.Frame.Size);
  1220. Assert.Equal (new (0, 9), view.Frame.Location);
  1221. view.Y = Pos.AnchorEnd ();
  1222. view.SetRelativeLayout (new (10, 10));
  1223. Assert.Equal (new (5, 1), view.Frame.Size);
  1224. Assert.Equal (new (0, 9), view.Frame.Location);
  1225. view.Y = Pos.AnchorEnd () - 1;
  1226. view.SetRelativeLayout (new (10, 10));
  1227. Assert.Equal (new (5, 1), view.Frame.Size);
  1228. Assert.Equal (new (0, 8), view.Frame.Location);
  1229. }
  1230. [Theory]
  1231. [InlineData ("01234", 5, 5)]
  1232. [InlineData ("01234", 6, 6)]
  1233. [InlineData ("01234", 4, 5)]
  1234. [InlineData ("01234", 0, 5)]
  1235. [InlineData ("", 5, 5)]
  1236. [InlineData ("", 0, 0)]
  1237. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  1238. {
  1239. View view = new ()
  1240. {
  1241. Width = Auto (),
  1242. Height = 1,
  1243. Text = text
  1244. };
  1245. View subView = new ()
  1246. {
  1247. Width = dimension,
  1248. Height = 1,
  1249. };
  1250. view.Add (subView);
  1251. view.SetRelativeLayout (new (10, 10));
  1252. Assert.Equal (expected, view.Frame.Width);
  1253. }
  1254. [Fact]
  1255. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  1256. {
  1257. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  1258. view.SetContentSize (new (5, 5));
  1259. view.SetRelativeLayout (new (10, 10));
  1260. Assert.Equal (new (5, 5), view.Frame.Size);
  1261. }
  1262. // Test variations of Frame
  1263. }