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. Assert.Equal (expected, view.Frame.Width);
  49. }
  50. [Theory]
  51. [InlineData (1, 100, 100)]
  52. [InlineData (1, 50, 50)]
  53. public void Min_Percent (int contentSize, int minPercent, int expected)
  54. {
  55. var view = new View
  56. {
  57. X = 0,
  58. Y = 0,
  59. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  60. ValidatePosDim = true
  61. };
  62. view.SetContentSize (new (contentSize, 0));
  63. view.SetRelativeLayout (new (100, 100));
  64. Assert.Equal (expected, view.Frame.Width);
  65. }
  66. [Theory]
  67. [InlineData (1, 100, 102)]
  68. [InlineData (1, 50, 52)] // 50% of 100 is 50, but the border adds 2
  69. [InlineData (1, 30, 32)] // 30% of 100 is 30, but the border adds 2
  70. [InlineData (2, 30, 32)] // 30% of 100 is 30, but the border adds 2
  71. public void Min_Percent_Is_Content_Relative (int contentSize, int minPercent, int expected)
  72. {
  73. var view = new View
  74. {
  75. X = 0,
  76. Y = 0,
  77. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  78. BorderStyle = LineStyle.Single, // a 1 thick adornment
  79. ValidatePosDim = true
  80. };
  81. view.SetContentSize (new (contentSize, 0));
  82. view.SetRelativeLayout (new (100, 100));
  83. Assert.Equal (expected, view.Frame.Width);
  84. }
  85. [Fact]
  86. public void Min_Resets_If_Subview_Moves_Negative ()
  87. {
  88. var superView = new View
  89. {
  90. X = 0,
  91. Y = 0,
  92. Width = Dim.Auto (minimumContentDim: 10),
  93. Height = Dim.Auto (minimumContentDim: 10),
  94. ValidatePosDim = true
  95. };
  96. var subView = new View
  97. {
  98. X = 0,
  99. Y = 0,
  100. Width = 5,
  101. Height = 5
  102. };
  103. superView.Add (subView);
  104. superView.BeginInit ();
  105. superView.EndInit ();
  106. superView.SetRelativeLayout (new (10, 10));
  107. superView.LayoutSubviews (); // no throw
  108. Assert.Equal (10, superView.Frame.Width);
  109. Assert.Equal (10, superView.Frame.Height);
  110. subView.X = -1;
  111. subView.Y = -1;
  112. superView.SetRelativeLayout (new (10, 10));
  113. superView.LayoutSubviews (); // no throw
  114. Assert.Equal (5, subView.Frame.Width);
  115. Assert.Equal (5, subView.Frame.Height);
  116. Assert.Equal (10, superView.Frame.Width);
  117. Assert.Equal (10, superView.Frame.Height);
  118. }
  119. [Fact]
  120. public void Min_Resets_If_Subview_Shrinks ()
  121. {
  122. var superView = new View
  123. {
  124. X = 0,
  125. Y = 0,
  126. Width = Dim.Auto (minimumContentDim: 10),
  127. Height = Dim.Auto (minimumContentDim: 10),
  128. ValidatePosDim = true
  129. };
  130. var subView = new View
  131. {
  132. X = 0,
  133. Y = 0,
  134. Width = 5,
  135. Height = 5
  136. };
  137. superView.Add (subView);
  138. superView.BeginInit ();
  139. superView.EndInit ();
  140. superView.SetRelativeLayout (new (10, 10));
  141. superView.LayoutSubviews (); // no throw
  142. Assert.Equal (10, superView.Frame.Width);
  143. Assert.Equal (10, superView.Frame.Height);
  144. subView.Width = 3;
  145. subView.Height = 3;
  146. superView.SetRelativeLayout (new (10, 10));
  147. superView.LayoutSubviews (); // no throw
  148. Assert.Equal (3, subView.Frame.Width);
  149. Assert.Equal (3, subView.Frame.Height);
  150. Assert.Equal (10, superView.Frame.Width);
  151. Assert.Equal (10, superView.Frame.Height);
  152. }
  153. #endregion minimumContentDim Tests
  154. // Test min - ensure that if min is specified in the DimAuto constructor it is honored
  155. // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
  156. #region maximumContentDim Tests
  157. // TODO: Add tests
  158. #endregion maximumContentDim Tests
  159. }