Dim.AutoTests.PosTypes.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. namespace Terminal.Gui.LayoutTests;
  2. public partial class DimAutoTests
  3. {
  4. // Tests all the Pos Types in Subview scenarios to ensure DimAutoStyle.Content is calculated correctly
  5. #region PosAbsolute
  6. [Theory]
  7. [InlineData (0, 0, 0, 0, 0, 0)]
  8. [InlineData (0, 19, 0, 9, 19, 9)]
  9. [InlineData (0, 20, 0, 10, 20, 10)]
  10. [InlineData (0, 21, 0, 11, 21, 11)]
  11. [InlineData (1, 21, 1, 11, 21, 11)]
  12. [InlineData (21, 21, 11, 11, 21, 11)]
  13. public void With_Subview_Using_PosAbsolute (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  14. {
  15. var view = new View
  16. {
  17. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  18. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  19. };
  20. var subview = new View
  21. {
  22. X = Pos.Absolute (10),
  23. Y = Pos.Absolute (5),
  24. Width = 20,
  25. Height = 10
  26. };
  27. view.Add (subview);
  28. // Assuming the calculation is done after layout
  29. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  30. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  31. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  32. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  33. Assert.Equal (expectedWidth, calculatedWidth);
  34. Assert.Equal (expectedHeight, calculatedHeight);
  35. Assert.Equal (0, calculatedX);
  36. Assert.Equal (0, calculatedY);
  37. }
  38. #endregion PosAbsolute
  39. #region PosAlign
  40. //[Theory]
  41. //[InlineData (0, 0, 0, 0, 0, 0)]
  42. //[InlineData (0, 19, 0, 9, 19, 9)]
  43. //[InlineData (0, 20, 0, 10, 20, 10)]
  44. //[InlineData (0, 21, 0, 11, 20, 10)]
  45. //[InlineData (1, 21, 1, 11, 20, 10)]
  46. //[InlineData (21, 21, 11, 11, 21, 11)]
  47. //public void With_Subview_Using_PosAlign (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  48. //{
  49. // var view = new View
  50. // {
  51. // Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  52. // Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  53. // };
  54. // var subview = new View
  55. // {
  56. // X = Pos.Align (Alignment.Center),
  57. // Y = Pos.Absolute (5),
  58. // Width = 20,
  59. // Height = 10
  60. // };
  61. // view.Add (subview);
  62. // // Assuming the calculation is done after layout
  63. // int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  64. // int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  65. // int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  66. // int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  67. // Assert.Equal (expectedWidth, calculatedWidth);
  68. // Assert.Equal (expectedHeight, calculatedHeight);
  69. // Assert.Equal (0, calculatedX);
  70. // Assert.Equal (0, calculatedY);
  71. //}
  72. #endregion PosAlign
  73. #region PosPercent
  74. [Theory]
  75. [InlineData (0, 0, 0, 0, 0, 0)]
  76. [InlineData (0, 19, 0, 9, 19, 9)]
  77. [InlineData (0, 20, 0, 10, 20, 10)]
  78. [InlineData (0, 21, 0, 11, 20, 10)]
  79. [InlineData (1, 21, 1, 11, 20, 10)]
  80. [InlineData (21, 21, 11, 11, 21, 11)]
  81. public void With_Subview_Using_PosPercent (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  82. {
  83. var view = new View
  84. {
  85. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  86. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  87. };
  88. var subview = new View
  89. {
  90. X = Pos.Percent (50),
  91. Y = Pos.Percent (50),
  92. Width = 20,
  93. Height = 10
  94. };
  95. view.Add (subview);
  96. // Assuming the calculation is done after layout
  97. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  98. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  99. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  100. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  101. Assert.Equal (expectedWidth, calculatedWidth);
  102. Assert.Equal (expectedHeight, calculatedHeight);
  103. Assert.Equal (0, calculatedX);
  104. Assert.Equal (0, calculatedY);
  105. view.BeginInit ();
  106. view.EndInit ();
  107. // subview should be at 50% in the parent view
  108. Assert.Equal ((int)(view.Viewport.Width * .50), subview.Frame.X);
  109. Assert.Equal ((int)(view.Viewport.Height * .50), subview.Frame.Y);
  110. }
  111. [Theory]
  112. [InlineData (0, 0, 0, 0, 0, 0)]
  113. [InlineData (0, 19, 0, 9, 19, 9)]
  114. [InlineData (0, 20, 0, 10, 20, 10)]
  115. [InlineData (0, 21, 0, 11, 21, 11)]
  116. [InlineData (1, 21, 1, 11, 21, 11)]
  117. [InlineData (21, 21, 11, 11, 21, 11)]
  118. public void With_Subview_Using_PosPercent_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  119. {
  120. var view = new View
  121. {
  122. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  123. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  124. };
  125. var subview = new View
  126. {
  127. X = Pos.Percent (50) + 1,
  128. Y = 1 + Pos.Percent (50),
  129. Width = 20,
  130. Height = 10
  131. };
  132. view.Add (subview);
  133. // Assuming the calculation is done after layout
  134. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  135. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  136. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  137. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  138. Assert.Equal (expectedWidth, calculatedWidth);
  139. Assert.Equal (expectedHeight, calculatedHeight);
  140. Assert.Equal (0, calculatedX);
  141. Assert.Equal (0, calculatedY);
  142. view.BeginInit ();
  143. view.EndInit ();
  144. // subview should be at 50% in the parent view
  145. Assert.Equal ((int)(view.Viewport.Width * .50) + 1, subview.Frame.X);
  146. Assert.Equal ((int)(view.Viewport.Height * .50) + 1, subview.Frame.Y);
  147. }
  148. #endregion PosPercent
  149. #region PosCenter
  150. [Theory]
  151. [InlineData (0, 0, 0, 0, 0, 0)]
  152. [InlineData (0, 19, 0, 9, 19, 9)]
  153. [InlineData (0, 20, 0, 10, 20, 10)]
  154. [InlineData (0, 21, 0, 11, 20, 10)]
  155. [InlineData (1, 21, 1, 11, 20, 10)]
  156. [InlineData (21, 21, 11, 11, 21, 11)]
  157. public void With_Subview_Using_PosCenter (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  158. {
  159. var view = new View
  160. {
  161. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  162. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  163. };
  164. var subview = new View
  165. {
  166. X = Pos.Center (),
  167. Y = Pos.Center (),
  168. Width = 20,
  169. Height = 10
  170. };
  171. view.Add (subview);
  172. // Assuming the calculation is done after layout
  173. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  174. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  175. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  176. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  177. Assert.Equal (expectedWidth, calculatedWidth);
  178. Assert.Equal (expectedHeight, calculatedHeight);
  179. Assert.Equal (0, calculatedX);
  180. Assert.Equal (0, calculatedY);
  181. view.BeginInit ();
  182. view.EndInit ();
  183. // subview should be centered in the parent view + 1
  184. Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2, subview.Frame.X);
  185. Assert.Equal ((view.Viewport.Height - subview.Frame.Height) / 2, subview.Frame.Y);
  186. }
  187. [Theory]
  188. [InlineData (0, 0, 0, 0, 0, 0)]
  189. [InlineData (0, 19, 0, 9, 19, 9)]
  190. [InlineData (0, 18, 0, 8, 18, 8)]
  191. [InlineData (0, 20, 0, 10, 20, 10)]
  192. [InlineData (0, 21, 0, 11, 21, 11)]
  193. [InlineData (1, 21, 1, 11, 21, 11)]
  194. [InlineData (21, 21, 11, 11, 21, 11)]
  195. public void With_Subview_Using_PosCenter_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  196. {
  197. var view = new View
  198. {
  199. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  200. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  201. };
  202. var subview = new View
  203. {
  204. X = Pos.Center () + 1,
  205. Y = 1 + Pos.Center (),
  206. Width = 20,
  207. Height = 10
  208. };
  209. view.Add (subview);
  210. // Assuming the calculation is done after layout
  211. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  212. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  213. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  214. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  215. Assert.Equal (expectedWidth, calculatedWidth);
  216. Assert.Equal (expectedHeight, calculatedHeight);
  217. Assert.Equal (0, calculatedX);
  218. Assert.Equal (0, calculatedY);
  219. view.BeginInit ();
  220. view.EndInit ();
  221. // subview should be centered in the parent view + 1
  222. Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2 + 1, subview.Frame.X);
  223. Assert.Equal ((view.Viewport.Height - subview.Frame.Height) / 2 + 1, subview.Frame.Y);
  224. }
  225. #endregion PosCenter
  226. #region PosView
  227. // TODO: Need PosView tests
  228. [Fact]
  229. public void With_Subview_Using_PosView ()
  230. {
  231. var view = new View ()
  232. {
  233. Width = Dim.Auto (),
  234. Height = Dim.Auto (),
  235. };
  236. var subview1 = new View { X = 1, Y = 2, Width = 1, Height = 2 };
  237. var subview2 = new View { X = Pos.Top (subview1), Y = Pos.Bottom (subview1), Width = 1, Height = 2 };
  238. view.Add (subview1, subview2);
  239. view.SetRelativeLayout (new (100, 100));
  240. // subview1.X + subview1.Width + subview2.Width
  241. Assert.Equal (1 + 1 + 1, view.Frame.Width);
  242. // subview1.Y + subview1.Height + subview2.Height
  243. Assert.Equal (2 + 2 + 2, view.Frame.Height);
  244. }
  245. #endregion PosView
  246. #region PosAnchorEnd
  247. [Theory]
  248. [InlineData (0, 0, 0, 0, 0, 0)]
  249. [InlineData (0, 19, 0, 9, 19, 9)]
  250. [InlineData (0, 18, 0, 8, 18, 8)]
  251. [InlineData (0, 20, 0, 10, 20, 10)]
  252. [InlineData (0, 21, 0, 11, 20, 10)]
  253. [InlineData (1, 21, 1, 11, 20, 10)]
  254. [InlineData (21, 21, 11, 11, 21, 11)]
  255. public void With_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  256. {
  257. var view = new View
  258. {
  259. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  260. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  261. };
  262. var subview = new View
  263. {
  264. X = Pos.AnchorEnd (),
  265. Y = Pos.AnchorEnd (),
  266. Width = 20,
  267. Height = 10
  268. };
  269. view.Add (subview);
  270. // Assuming the calculation is done after layout
  271. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  272. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  273. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  274. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  275. Assert.Equal (expectedWidth, calculatedWidth);
  276. Assert.Equal (expectedHeight, calculatedHeight);
  277. Assert.Equal (0, calculatedX);
  278. Assert.Equal (0, calculatedY);
  279. view.BeginInit ();
  280. view.EndInit ();
  281. // subview should be at the end of the view
  282. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  283. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  284. }
  285. [Theory]
  286. [InlineData (0, 0, 0, 0, 0, 0)]
  287. [InlineData (0, 19, 0, 9, 19, 9)]
  288. [InlineData (0, 18, 0, 8, 18, 8)]
  289. [InlineData (0, 20, 0, 10, 20, 10)]
  290. [InlineData (0, 21, 0, 11, 21, 11)]
  291. [InlineData (1, 21, 1, 11, 21, 11)]
  292. [InlineData (21, 21, 11, 11, 21, 11)]
  293. [InlineData (0, 30, 0, 20, 25, 15)]
  294. public void With_Subview_And_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  295. {
  296. var view = new View
  297. {
  298. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  299. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  300. };
  301. var otherView = new View
  302. {
  303. Width = 5,
  304. Height = 5
  305. };
  306. view.Add (otherView);
  307. var subview = new View
  308. {
  309. X = Pos.AnchorEnd (),
  310. Y = Pos.AnchorEnd (),
  311. Width = 20,
  312. Height = 10
  313. };
  314. view.Add (subview);
  315. // Assuming the calculation is done after layout
  316. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  317. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  318. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  319. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  320. Assert.Equal (expectedWidth, calculatedWidth);
  321. Assert.Equal (expectedHeight, calculatedHeight);
  322. Assert.Equal (0, calculatedX);
  323. Assert.Equal (0, calculatedY);
  324. view.BeginInit ();
  325. view.EndInit ();
  326. // subview should be at the end of the view
  327. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  328. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  329. }
  330. [Theory]
  331. [InlineData (0, 0, 0, 0, 0, 0)]
  332. [InlineData (0, 19, 0, 9, 19, 9)]
  333. [InlineData (0, 18, 0, 8, 18, 8)]
  334. [InlineData (0, 20, 0, 10, 20, 10)]
  335. [InlineData (0, 21, 0, 11, 21, 11)]
  336. [InlineData (1, 21, 1, 11, 21, 11)]
  337. [InlineData (21, 21, 11, 11, 21, 11)]
  338. [InlineData (0, 30, 0, 20, 25, 15)]
  339. public void With_DimAutoSubview_And_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  340. {
  341. var view = new View
  342. {
  343. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  344. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  345. };
  346. var otherView = new View
  347. {
  348. Text = "01234\n01234\n01234\n01234\n01234",
  349. Width = Dim.Auto(),
  350. Height = Dim.Auto ()
  351. };
  352. view.Add (otherView);
  353. var subview = new View
  354. {
  355. X = Pos.AnchorEnd (),
  356. Y = Pos.AnchorEnd (),
  357. Width = 20,
  358. Height = 10
  359. };
  360. view.Add (subview);
  361. // Assuming the calculation is done after layout
  362. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  363. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  364. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  365. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  366. Assert.Equal (expectedWidth, calculatedWidth);
  367. Assert.Equal (expectedHeight, calculatedHeight);
  368. Assert.Equal (0, calculatedX);
  369. Assert.Equal (0, calculatedY);
  370. view.BeginInit ();
  371. view.EndInit ();
  372. // subview should be at the end of the view
  373. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  374. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  375. }
  376. [Theory]
  377. [InlineData (0, 0, 0, 0, 0, 0)]
  378. [InlineData (0, 19, 0, 9, 19, 9)]
  379. [InlineData (0, 18, 0, 8, 18, 8)]
  380. [InlineData (0, 20, 0, 10, 20, 10)]
  381. [InlineData (0, 21, 0, 11, 21, 11)]
  382. [InlineData (1, 21, 1, 11, 21, 11)]
  383. [InlineData (21, 21, 11, 11, 21, 11)]
  384. [InlineData (0, 30, 0, 20, 26, 16)]
  385. public void With_PosViewSubview_And_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  386. {
  387. var view = new View
  388. {
  389. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  390. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  391. };
  392. var otherView = new View
  393. {
  394. Width = 1,
  395. Height = 1,
  396. };
  397. view.Add (otherView);
  398. var posViewView = new View
  399. {
  400. X = Pos.Bottom(otherView),
  401. Y = Pos.Right(otherView),
  402. Width = 5,
  403. Height = 5,
  404. };
  405. view.Add (posViewView);
  406. var subview = new View
  407. {
  408. X = Pos.AnchorEnd (),
  409. Y = Pos.AnchorEnd (),
  410. Width = 20,
  411. Height = 10
  412. };
  413. view.Add (subview);
  414. // Assuming the calculation is done after layout
  415. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  416. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  417. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  418. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  419. Assert.Equal (expectedWidth, calculatedWidth);
  420. Assert.Equal (expectedHeight, calculatedHeight);
  421. Assert.Equal (0, calculatedX);
  422. Assert.Equal (0, calculatedY);
  423. view.BeginInit ();
  424. view.EndInit ();
  425. // subview should be at the end of the view
  426. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  427. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  428. }
  429. [Theory]
  430. [InlineData (0, 0, 0, 0, 0, 0)]
  431. [InlineData (0, 19, 0, 9, 19, 9)]
  432. [InlineData (0, 18, 0, 8, 18, 8)]
  433. [InlineData (0, 20, 0, 10, 20, 10)]
  434. [InlineData (0, 21, 0, 11, 21, 11)]
  435. [InlineData (1, 21, 1, 11, 21, 11)]
  436. [InlineData (21, 21, 11, 11, 21, 11)]
  437. [InlineData (0, 30, 0, 20, 22, 12)]
  438. public void With_DimViewSubview_And_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  439. {
  440. var view = new View
  441. {
  442. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  443. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  444. };
  445. var otherView = new View
  446. {
  447. Width = 1,
  448. Height = 1,
  449. };
  450. view.Add (otherView);
  451. var dimViewView = new View
  452. {
  453. Id = "dimViewView",
  454. X = 1,
  455. Y = 1,
  456. Width = Dim.Width (otherView),
  457. Height = Dim.Height (otherView),
  458. };
  459. view.Add (dimViewView);
  460. var subview = new View
  461. {
  462. X = Pos.AnchorEnd (),
  463. Y = Pos.AnchorEnd (),
  464. Width = 20,
  465. Height = 10
  466. };
  467. view.Add (subview);
  468. // Assuming the calculation is done after layout
  469. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  470. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  471. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  472. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  473. Assert.Equal (expectedWidth, calculatedWidth);
  474. Assert.Equal (expectedHeight, calculatedHeight);
  475. Assert.Equal (0, calculatedX);
  476. Assert.Equal (0, calculatedY);
  477. view.BeginInit ();
  478. view.EndInit ();
  479. // subview should be at the end of the view
  480. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  481. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  482. }
  483. [Theory]
  484. [InlineData (0, 10, 0, 10, 10, 2)]
  485. [InlineData (0, 5, 0, 5, 5, 3)] // max width of 5 should cause wordwrap at 5 giving a height of 2 + 1
  486. [InlineData (0, 19, 0, 9, 11, 2)]
  487. //[InlineData (0, 20, 0, 10, 20, 10)]
  488. //[InlineData (0, 21, 0, 11, 21, 11)]
  489. //[InlineData (1, 21, 1, 11, 21, 11)]
  490. //[InlineData (21, 21, 11, 11, 21, 11)]
  491. public void With_Text_And_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  492. {
  493. var view = new View
  494. {
  495. Text = "01234ABCDE",
  496. Width = Dim.Auto (),
  497. Height = Dim.Auto ()
  498. };
  499. // Without a subview, width should be 10
  500. // Without a subview, height should be 1
  501. view.SetRelativeLayout (Application.Screen.Size);
  502. Assert.Equal (10, view.Frame.Width);
  503. Assert.Equal (1, view.Frame.Height);
  504. view.Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth);
  505. view.Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight);
  506. var subview = new View
  507. {
  508. X = Pos.AnchorEnd (),
  509. Y = Pos.AnchorEnd (),
  510. Width = 1,
  511. Height = 1
  512. };
  513. view.Add (subview);
  514. // Assuming the calculation is done after layout
  515. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  516. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  517. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  518. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  519. Assert.Equal (expectedWidth, calculatedWidth);
  520. Assert.Equal (expectedHeight, calculatedHeight);
  521. Assert.Equal (0, calculatedX);
  522. Assert.Equal (0, calculatedY);
  523. }
  524. #endregion PosAnchorEnd
  525. #region PosFunc
  526. [Fact]
  527. public void With_Subview_Using_PosFunc ()
  528. {
  529. var view = new View ()
  530. {
  531. Width = Dim.Auto (),
  532. Height = Dim.Auto (),
  533. };
  534. var subview = new View { X = Pos.Func (() => 20), Y = Pos.Func (() => 25) };
  535. view.Add (subview);
  536. view.SetRelativeLayout (new (100, 100));
  537. Assert.Equal (20, view.Frame.Width);
  538. Assert.Equal (25, view.Frame.Height);
  539. }
  540. #endregion PosFunc
  541. #region PosCombine
  542. // TODO: Need more PosCombine tests
  543. #endregion PosCombine
  544. }