Dim.AutoTests.DimTypes.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. namespace Terminal.Gui.LayoutTests;
  2. public partial class DimAutoTests
  3. {
  4. // Tests all the Dim Types in Subview scenarios to ensure DimAutoStyle.Content is calculated correctly
  5. #region DimAbsolute
  6. [Theory]
  7. [InlineData (0, 15, 15)]
  8. [InlineData (1, 15, 16)]
  9. [InlineData (-1, 15, 14)]
  10. public void With_Subview_Using_DimAbsolute (int subViewOffset, int dimAbsoluteSize, int expectedSize)
  11. {
  12. var view = new View ();
  13. var subview = new View
  14. {
  15. X = subViewOffset,
  16. Y = subViewOffset,
  17. Width = Dim.Absolute (dimAbsoluteSize),
  18. Height = Dim.Absolute (dimAbsoluteSize)
  19. };
  20. view.Add (subview);
  21. Dim dim = Dim.Auto (DimAutoStyle.Content);
  22. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  23. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  24. Assert.Equal (expectedSize, calculatedWidth);
  25. Assert.Equal (expectedSize, calculatedHeight);
  26. }
  27. #endregion DimAbsolute
  28. #region DimPercent
  29. [Theory]
  30. [InlineData (0, 0, 0, 0, 0, 0, 0, 0)]
  31. [InlineData (0, 50, 0, 0, 0, 0, 0, 0)]
  32. [InlineData (0, 0, 100, 100, 100, 100, 100, 100)]
  33. [InlineData (0, 50, 100, 100, 100, 100, 100, 100)]
  34. public void With_Subview_Using_DimPercent (
  35. int subViewOffset,
  36. int percent,
  37. int minWidth,
  38. int maxWidth,
  39. int minHeight,
  40. int maxHeight,
  41. int expectedWidth,
  42. int expectedHeight
  43. )
  44. {
  45. var view = new View
  46. {
  47. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  48. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  49. };
  50. var subview = new View
  51. {
  52. X = subViewOffset,
  53. Y = subViewOffset,
  54. Width = Dim.Percent (percent),
  55. Height = Dim.Percent (percent)
  56. };
  57. view.Add (subview);
  58. // Assuming the calculation is done after layout
  59. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  60. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  61. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  62. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  63. Assert.Equal (expectedWidth, calculatedWidth); // subview's width
  64. Assert.Equal (expectedHeight, calculatedHeight); // subview's height
  65. Assert.Equal (subViewOffset, calculatedX);
  66. Assert.Equal (subViewOffset, calculatedY);
  67. view.SetRelativeLayout (new (100, 100));
  68. view.LayoutSubviews ();
  69. Assert.Equal (expectedWidth * (percent / 100f), subview.Viewport.Width);
  70. Assert.Equal (expectedHeight * (percent / 100f), subview.Viewport.Height);
  71. }
  72. #endregion DimPercent
  73. #region DimFill
  74. [Theory]
  75. [InlineData (0, 0, 0, 0, 0, 0)]
  76. [InlineData (0, 19, 0, 9, 0, 0)]
  77. [InlineData (0, 20, 0, 10, 0, 0)]
  78. [InlineData (0, 21, 0, 11, 0, 0)]
  79. [InlineData (1, 21, 1, 11, 1, 1)]
  80. [InlineData (21, 21, 11, 11, 21, 11)]
  81. public void With_Subview_Using_DimFill (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 = 0,
  91. Y = 0,
  92. Width = Dim.Fill (),
  93. Height = Dim.Fill ()
  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. }
  106. [Theory]
  107. [InlineData (0, 0, 0, 0, 0, 0)]
  108. [InlineData (0, 19, 0, 9, 2, 4)]
  109. [InlineData (0, 20, 0, 10, 2, 4)]
  110. [InlineData (0, 21, 0, 11, 2, 4)]
  111. [InlineData (1, 21, 1, 11, 2, 4)]
  112. [InlineData (21, 21, 11, 11, 21, 11)]
  113. public void With_Subview_Using_DimFill_And_Another_Subview (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  114. {
  115. var view = new View
  116. {
  117. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  118. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  119. };
  120. var absView = new View
  121. {
  122. X = 1,
  123. Y = 2,
  124. Width = 1,
  125. Height = 2
  126. };
  127. view.Add (absView);
  128. var subview = new View
  129. {
  130. X = 0,
  131. Y = 0,
  132. Width = Dim.Fill (),
  133. Height = Dim.Fill ()
  134. };
  135. view.Add (subview);
  136. // Assuming the calculation is done after layout
  137. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  138. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  139. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  140. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  141. Assert.Equal (expectedWidth, calculatedWidth);
  142. Assert.Equal (expectedHeight, calculatedHeight);
  143. Assert.Equal (0, calculatedX);
  144. Assert.Equal (0, calculatedY);
  145. }
  146. #endregion
  147. #region DimFunc
  148. [Fact]
  149. public void With_Subview_Using_DimFunc ()
  150. {
  151. var view = new View ();
  152. var subview = new View { Width = Dim.Func (() => 20), Height = Dim.Func (() => 25) };
  153. view.Add (subview);
  154. subview.SetRelativeLayout (new (100, 100));
  155. Dim dim = Dim.Auto (DimAutoStyle.Content);
  156. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  157. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  158. Assert.Equal (20, calculatedWidth);
  159. Assert.Equal (25, calculatedHeight);
  160. }
  161. #endregion DimFunc
  162. #region DimView
  163. [Fact]
  164. public void With_Subview_Using_DimView ()
  165. {
  166. var view = new View ();
  167. var subview = new View { Width = 30, Height = 40 };
  168. var subSubview = new View { Width = Dim.Width (subview), Height = Dim.Height (subview) };
  169. view.Add (subview);
  170. view.Add (subSubview);
  171. subview.SetRelativeLayout (new (100, 100));
  172. Dim dim = Dim.Auto (DimAutoStyle.Content);
  173. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  174. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  175. // Expecting the size to match the subview, which is the largest
  176. Assert.Equal (30, calculatedWidth);
  177. Assert.Equal (40, calculatedHeight);
  178. }
  179. #endregion DimView
  180. #region DimCombine
  181. // TODO: Need DimCombine tests
  182. #endregion DimCombine
  183. }