Dim.FillTests.cs 4.6 KB

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