Dim.FillTests.cs 4.6 KB

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