Dim.AutoTests.MinMax.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. namespace Terminal.Gui.LayoutTests;
  2. public partial class DimAutoTests
  3. {
  4. #region minimumContentDim Tests
  5. [Fact]
  6. public void Min_Is_Honored ()
  7. {
  8. var superView = new View
  9. {
  10. X = 0,
  11. Y = 0,
  12. Width = Dim.Auto (minimumContentDim: 10),
  13. Height = Dim.Auto (minimumContentDim: 10),
  14. ValidatePosDim = true
  15. };
  16. var subView = new View
  17. {
  18. X = 0,
  19. Y = 0,
  20. Width = 5,
  21. Height = 5
  22. };
  23. superView.Add (subView);
  24. superView.BeginInit ();
  25. superView.EndInit ();
  26. superView.SetRelativeLayout (new (10, 10));
  27. superView.LayoutSubviews (); // no throw
  28. Assert.Equal (10, superView.Frame.Width);
  29. Assert.Equal (10, superView.Frame.Height);
  30. }
  31. [Theory]
  32. [InlineData (0, 2, 4)]
  33. [InlineData (1, 2, 4)]
  34. [InlineData (2, 2, 4)]
  35. [InlineData (3, 2, 5)]
  36. [InlineData (1, 0, 3)]
  37. public void Min_Absolute_Is_Content_Relative (int contentSize, int minAbsolute, int expected)
  38. {
  39. var view = new View
  40. {
  41. X = 0,
  42. Y = 0,
  43. Width = Dim.Auto (minimumContentDim: minAbsolute),
  44. BorderStyle = LineStyle.Single, // a 1 thick adornment
  45. ValidatePosDim = true
  46. };
  47. view.SetContentSize (new (contentSize, 0));
  48. view.Layout ();
  49. Assert.Equal (expected, view.Frame.Width);
  50. }
  51. [Theory]
  52. [InlineData (1, 100, 100)]
  53. [InlineData (1, 50, 50)]
  54. public void Min_Percent (int contentSize, int minPercent, int expected)
  55. {
  56. var view = new View
  57. {
  58. X = 0,
  59. Y = 0,
  60. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  61. ValidatePosDim = true
  62. };
  63. view.SetContentSize (new (contentSize, 0));
  64. view.SetRelativeLayout (new (100, 100));
  65. Assert.Equal (expected, view.Frame.Width);
  66. }
  67. [Theory]
  68. [InlineData (1, 100, 102)]
  69. [InlineData (1, 50, 52)] // 50% of 100 is 50, but the border adds 2
  70. [InlineData (1, 30, 32)] // 30% of 100 is 30, but the border adds 2
  71. [InlineData (2, 30, 32)] // 30% of 100 is 30, but the border adds 2
  72. public void Min_Percent_Is_Content_Relative (int contentSize, int minPercent, int expected)
  73. {
  74. var view = new View
  75. {
  76. X = 0,
  77. Y = 0,
  78. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  79. BorderStyle = LineStyle.Single, // a 1 thick adornment
  80. ValidatePosDim = true
  81. };
  82. view.SetContentSize (new (contentSize, 0));
  83. view.SetRelativeLayout (new (100, 100));
  84. Assert.Equal (expected, view.Frame.Width);
  85. }
  86. [Fact]
  87. public void Min_Resets_If_Subview_Moves_Negative ()
  88. {
  89. var superView = new View
  90. {
  91. X = 0,
  92. Y = 0,
  93. Width = Dim.Auto (minimumContentDim: 10),
  94. Height = Dim.Auto (minimumContentDim: 10),
  95. ValidatePosDim = true
  96. };
  97. var subView = new View
  98. {
  99. X = 0,
  100. Y = 0,
  101. Width = 5,
  102. Height = 5
  103. };
  104. superView.Add (subView);
  105. superView.BeginInit ();
  106. superView.EndInit ();
  107. superView.SetRelativeLayout (new (10, 10));
  108. superView.LayoutSubviews (); // no throw
  109. Assert.Equal (10, superView.Frame.Width);
  110. Assert.Equal (10, superView.Frame.Height);
  111. subView.X = -1;
  112. subView.Y = -1;
  113. superView.SetRelativeLayout (new (10, 10));
  114. superView.LayoutSubviews (); // no throw
  115. Assert.Equal (5, subView.Frame.Width);
  116. Assert.Equal (5, subView.Frame.Height);
  117. Assert.Equal (10, superView.Frame.Width);
  118. Assert.Equal (10, superView.Frame.Height);
  119. }
  120. [Fact]
  121. public void Min_Resets_If_Subview_Shrinks ()
  122. {
  123. var superView = new View
  124. {
  125. X = 0,
  126. Y = 0,
  127. Width = Dim.Auto (minimumContentDim: 10),
  128. Height = Dim.Auto (minimumContentDim: 10),
  129. ValidatePosDim = true
  130. };
  131. var subView = new View
  132. {
  133. X = 0,
  134. Y = 0,
  135. Width = 5,
  136. Height = 5
  137. };
  138. superView.Add (subView);
  139. superView.BeginInit ();
  140. superView.EndInit ();
  141. superView.SetRelativeLayout (new (10, 10));
  142. superView.LayoutSubviews (); // no throw
  143. Assert.Equal (10, superView.Frame.Width);
  144. Assert.Equal (10, superView.Frame.Height);
  145. subView.Width = 3;
  146. subView.Height = 3;
  147. superView.SetRelativeLayout (new (10, 10));
  148. superView.LayoutSubviews (); // no throw
  149. Assert.Equal (3, subView.Frame.Width);
  150. Assert.Equal (3, subView.Frame.Height);
  151. Assert.Equal (10, superView.Frame.Width);
  152. Assert.Equal (10, superView.Frame.Height);
  153. }
  154. #endregion minimumContentDim Tests
  155. // Test min - ensure that if min is specified in the DimAuto constructor it is honored
  156. // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
  157. #region maximumContentDim Tests
  158. // TODO: Add tests
  159. #endregion maximumContentDim Tests
  160. }