Dim.FillTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.LayoutTests;
  4. public class DimFillTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Fact]
  8. public void DimFill_Equal ()
  9. {
  10. var margin1 = 0;
  11. var margin2 = 0;
  12. Dim dim1 = Dim.Fill (margin1);
  13. Dim dim2 = Dim.Fill (margin2);
  14. Assert.Equal (dim1, dim2);
  15. }
  16. // Tests that Dim.Fill honors the margin parameter correctly
  17. [Theory]
  18. [InlineData (0, true, 25)]
  19. [InlineData (0, false, 25)]
  20. [InlineData (1, true, 24)]
  21. [InlineData (1, false, 24)]
  22. [InlineData (2, true, 23)]
  23. [InlineData (2, false, 23)]
  24. [InlineData (-2, true, 27)]
  25. [InlineData (-2, false, 27)]
  26. public void DimFill_Margin (int margin, bool width, int expected)
  27. {
  28. var super = new View { Width = 25, Height = 25 };
  29. var view = new View
  30. {
  31. X = 0,
  32. Y = 0,
  33. Width = width ? Dim.Fill (margin) : 1,
  34. Height = width ? 1 : Dim.Fill (margin)
  35. };
  36. super.Add (view);
  37. super.BeginInit ();
  38. super.EndInit ();
  39. super.Layout ();
  40. Assert.Equal (25, super.Frame.Width);
  41. Assert.Equal (25, super.Frame.Height);
  42. if (width)
  43. {
  44. Assert.Equal (expected, view.Frame.Width);
  45. Assert.Equal (1, view.Frame.Height);
  46. }
  47. else
  48. {
  49. Assert.Equal (1, view.Frame.Width);
  50. Assert.Equal (expected, view.Frame.Height);
  51. }
  52. }
  53. // Tests that Dim.Fill fills the dimension REMAINING from the View's X position to the end of the super view's width
  54. [Theory]
  55. [InlineData (0, true, 25)]
  56. [InlineData (0, false, 25)]
  57. [InlineData (1, true, 24)]
  58. [InlineData (1, false, 24)]
  59. [InlineData (2, true, 23)]
  60. [InlineData (2, false, 23)]
  61. [InlineData (-2, true, 27)]
  62. [InlineData (-2, false, 27)]
  63. public void DimFill_Offset (int offset, bool width, int expected)
  64. {
  65. var super = new View { Width = 25, Height = 25 };
  66. var view = new View
  67. {
  68. X = width ? offset : 0,
  69. Y = width ? 0 : offset,
  70. Width = width ? Dim.Fill () : 1,
  71. Height = width ? 1 : Dim.Fill ()
  72. };
  73. super.Add (view);
  74. super.BeginInit ();
  75. super.EndInit ();
  76. super.Layout ();
  77. Assert.Equal (25, super.Frame.Width);
  78. Assert.Equal (25, super.Frame.Height);
  79. if (width)
  80. {
  81. Assert.Equal (expected, view.Frame.Width);
  82. Assert.Equal (1, view.Frame.Height);
  83. }
  84. else
  85. {
  86. Assert.Equal (1, view.Frame.Width);
  87. Assert.Equal (expected, view.Frame.Height);
  88. }
  89. }
  90. // TODO: Other Dim.Height tests (e.g. Equal?)
  91. [Fact]
  92. public void DimFill_SetsValue ()
  93. {
  94. var testMargin = 0;
  95. Dim dim = Dim.Fill ();
  96. Assert.Equal (testMargin, dim!.GetAnchor(0));
  97. testMargin = 0;
  98. dim = Dim.Fill (testMargin);
  99. Assert.Equal (testMargin, dim!.GetAnchor (0));
  100. testMargin = 5;
  101. dim = Dim.Fill (testMargin);
  102. Assert.Equal (-testMargin, dim!.GetAnchor (0));
  103. }
  104. [Fact]
  105. public void DimFill_Margin_Is_Dim_SetsValue ()
  106. {
  107. Dim testMargin = Dim.Func (() => 0);
  108. Dim dim = Dim.Fill (testMargin);
  109. Assert.Equal (0, dim!.GetAnchor (0));
  110. testMargin = Dim.Func (() => 5);
  111. dim = Dim.Fill (testMargin);
  112. Assert.Equal (-5, dim!.GetAnchor (0));
  113. }
  114. [Fact]
  115. public void DimFill_Calculate_ReturnsCorrectValue ()
  116. {
  117. var dim = Dim.Fill ();
  118. var result = dim.Calculate (0, 100, null, Dimension.None);
  119. Assert.Equal (100, result);
  120. }
  121. [Fact]
  122. public void ResizeView_With_Dim_Fill_After_IsInitialized ()
  123. {
  124. var super = new View { Frame = new (0, 0, 30, 80) };
  125. var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  126. super.Add (view);
  127. view.Text = "New text\nNew line";
  128. super.Layout ();
  129. Rectangle expectedViewBounds = new (0, 0, 30, 80);
  130. Assert.Equal (expectedViewBounds, view.Viewport);
  131. Assert.False (view.IsInitialized);
  132. super.BeginInit ();
  133. super.EndInit ();
  134. Assert.True (view.IsInitialized);
  135. Assert.Equal (expectedViewBounds, view.Viewport);
  136. }
  137. }