Dim.AutoTests.PosTypes.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 PosPercent
  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_PosPercent (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.Percent (50),
  57. Y = Pos.Percent (50),
  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. view.BeginInit ();
  72. view.EndInit ();
  73. // subview should be at 50% in the parent view
  74. Assert.Equal ((int)(view.Viewport.Width * .50), subview.Frame.X);
  75. Assert.Equal ((int)(view.Viewport.Height * .50), subview.Frame.Y);
  76. }
  77. [Theory]
  78. [InlineData (0, 0, 0, 0, 0, 0)]
  79. [InlineData (0, 19, 0, 9, 19, 9)]
  80. [InlineData (0, 20, 0, 10, 20, 10)]
  81. [InlineData (0, 21, 0, 11, 21, 11)]
  82. [InlineData (1, 21, 1, 11, 21, 11)]
  83. [InlineData (21, 21, 11, 11, 21, 11)]
  84. public void With_Subview_Using_PosPercent_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  85. {
  86. var view = new View
  87. {
  88. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  89. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  90. };
  91. var subview = new View
  92. {
  93. X = Pos.Percent (50) + 1,
  94. Y = 1 + Pos.Percent (50),
  95. Width = 20,
  96. Height = 10
  97. };
  98. view.Add (subview);
  99. // Assuming the calculation is done after layout
  100. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  101. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  102. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  103. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  104. Assert.Equal (expectedWidth, calculatedWidth);
  105. Assert.Equal (expectedHeight, calculatedHeight);
  106. Assert.Equal (0, calculatedX);
  107. Assert.Equal (0, calculatedY);
  108. view.BeginInit ();
  109. view.EndInit ();
  110. // subview should be at 50% in the parent view
  111. Assert.Equal ((int)(view.Viewport.Width * .50) + 1, subview.Frame.X);
  112. Assert.Equal ((int)(view.Viewport.Height * .50) + 1, subview.Frame.Y);
  113. }
  114. #endregion PosPercent
  115. #region PosCenter
  116. [Theory]
  117. [InlineData (0, 0, 0, 0, 0, 0)]
  118. [InlineData (0, 19, 0, 9, 19, 9)]
  119. [InlineData (0, 20, 0, 10, 20, 10)]
  120. [InlineData (0, 21, 0, 11, 20, 10)]
  121. [InlineData (1, 21, 1, 11, 20, 10)]
  122. [InlineData (21, 21, 11, 11, 21, 11)]
  123. public void With_Subview_Using_PosCenter (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  124. {
  125. var view = new View
  126. {
  127. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  128. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  129. };
  130. var subview = new View
  131. {
  132. X = Pos.Center (),
  133. Y = Pos.Center (),
  134. Width = 20,
  135. Height = 10
  136. };
  137. view.Add (subview);
  138. // Assuming the calculation is done after layout
  139. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  140. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  141. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  142. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  143. Assert.Equal (expectedWidth, calculatedWidth);
  144. Assert.Equal (expectedHeight, calculatedHeight);
  145. Assert.Equal (0, calculatedX);
  146. Assert.Equal (0, calculatedY);
  147. view.BeginInit ();
  148. view.EndInit ();
  149. // subview should be centered in the parent view + 1
  150. Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2, subview.Frame.X);
  151. Assert.Equal ((view.Viewport.Height - subview.Frame.Height) / 2, subview.Frame.Y);
  152. }
  153. [Theory]
  154. [InlineData (0, 0, 0, 0, 0, 0)]
  155. [InlineData (0, 19, 0, 9, 19, 9)]
  156. [InlineData (0, 18, 0, 8, 18, 8)]
  157. [InlineData (0, 20, 0, 10, 20, 10)]
  158. [InlineData (0, 21, 0, 11, 21, 11)]
  159. [InlineData (1, 21, 1, 11, 21, 11)]
  160. [InlineData (21, 21, 11, 11, 21, 11)]
  161. public void With_Subview_Using_PosCenter_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  162. {
  163. var view = new View
  164. {
  165. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  166. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  167. };
  168. var subview = new View
  169. {
  170. X = Pos.Center () + 1,
  171. Y = 1 + Pos.Center (),
  172. Width = 20,
  173. Height = 10
  174. };
  175. view.Add (subview);
  176. // Assuming the calculation is done after layout
  177. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  178. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  179. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  180. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  181. Assert.Equal (expectedWidth, calculatedWidth);
  182. Assert.Equal (expectedHeight, calculatedHeight);
  183. Assert.Equal (0, calculatedX);
  184. Assert.Equal (0, calculatedY);
  185. view.BeginInit ();
  186. view.EndInit ();
  187. // subview should be centered in the parent view + 1
  188. Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2 + 1, subview.Frame.X);
  189. Assert.Equal ((view.Viewport.Height - subview.Frame.Height) / 2 + 1, subview.Frame.Y);
  190. }
  191. #endregion PosCenter
  192. #region PosView
  193. // TODO: Need PosView tests
  194. [Fact]
  195. public void With_Subview_Using_PosView ()
  196. {
  197. var view = new View ()
  198. {
  199. Width = Dim.Auto (),
  200. Height = Dim.Auto (),
  201. };
  202. var subview1 = new View { X = 1, Y = 2, Width = 1, Height = 2 };
  203. var subview2 = new View { X = Pos.Top (subview1), Y = Pos.Bottom (subview1), Width = 1, Height = 2 };
  204. view.Add (subview1, subview2);
  205. view.SetRelativeLayout (new (100, 100));
  206. // subview1.X + subview1.Width + subview2.Width
  207. Assert.Equal (1 + 1 + 1, view.Frame.Width);
  208. // subview1.Y + subview1.Height + subview2.Height
  209. Assert.Equal (2 + 2 + 2, view.Frame.Height);
  210. }
  211. #endregion PosView
  212. #region PosAnchorEnd
  213. [Theory]
  214. [InlineData (0, 0, 0, 0, 0, 0)]
  215. [InlineData (0, 19, 0, 9, 19, 9)]
  216. [InlineData (0, 18, 0, 8, 18, 8)]
  217. [InlineData (0, 20, 0, 10, 20, 10)]
  218. [InlineData (0, 21, 0, 11, 20, 10)]
  219. [InlineData (1, 21, 1, 11, 20, 10)]
  220. [InlineData (21, 21, 11, 11, 21, 11)]
  221. public void With_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  222. {
  223. var view = new View
  224. {
  225. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  226. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  227. };
  228. var subview = new View
  229. {
  230. X = Pos.AnchorEnd (),
  231. Y = Pos.AnchorEnd (),
  232. Width = 20,
  233. Height = 10
  234. };
  235. view.Add (subview);
  236. // Assuming the calculation is done after layout
  237. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  238. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  239. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  240. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  241. Assert.Equal (expectedWidth, calculatedWidth);
  242. Assert.Equal (expectedHeight, calculatedHeight);
  243. Assert.Equal (0, calculatedX);
  244. Assert.Equal (0, calculatedY);
  245. view.BeginInit ();
  246. view.EndInit ();
  247. // subview should be at the end of the view
  248. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  249. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  250. }
  251. [Theory]
  252. [InlineData (0, 10, 0, 10, 10, 2)]
  253. [InlineData (0, 5, 0, 5, 5, 3)] // max width of 5 should cause wordwrap at 5 giving a height of 2 + 1
  254. //[InlineData (0, 19, 0, 9, 19, 9)]
  255. //[InlineData (0, 20, 0, 10, 20, 10)]
  256. //[InlineData (0, 21, 0, 11, 21, 11)]
  257. //[InlineData (1, 21, 1, 11, 21, 11)]
  258. //[InlineData (21, 21, 11, 11, 21, 11)]
  259. public void With_Text_And_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  260. {
  261. var view = new View
  262. {
  263. Text = "01234ABCDE",
  264. Width = Dim.Auto (),
  265. Height = Dim.Auto ()
  266. };
  267. // Without a subview, width should be 10
  268. // Without a subview, height should be 1
  269. view.SetRelativeLayout (Application.Screen.Size);
  270. Assert.Equal (10, view.Frame.Width);
  271. Assert.Equal (1, view.Frame.Height);
  272. view.Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth);
  273. view.Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight);
  274. var subview = new View
  275. {
  276. X = Pos.AnchorEnd (),
  277. Y = Pos.AnchorEnd (),
  278. Width = 1,
  279. Height = 1
  280. };
  281. view.Add (subview);
  282. // Assuming the calculation is done after layout
  283. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  284. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  285. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  286. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  287. Assert.Equal (expectedWidth, calculatedWidth);
  288. Assert.Equal (expectedHeight, calculatedHeight);
  289. Assert.Equal (0, calculatedX);
  290. Assert.Equal (0, calculatedY);
  291. }
  292. #endregion PosAnchorEnd
  293. #region PosFunc
  294. [Fact]
  295. public void With_Subview_Using_PosFunc ()
  296. {
  297. var view = new View ()
  298. {
  299. Width = Dim.Auto (),
  300. Height = Dim.Auto (),
  301. };
  302. var subview = new View { X = Pos.Func (() => 20), Y = Pos.Func (() => 25) };
  303. view.Add (subview);
  304. view.SetRelativeLayout (new (100, 100));
  305. Assert.Equal (20, view.Frame.Width);
  306. Assert.Equal (25, view.Frame.Height);
  307. }
  308. #endregion PosFunc
  309. #region PosCombine
  310. // TODO: Need more PosCombine tests
  311. #endregion PosCombine
  312. }