AutoSizeFalseTests.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. /// <summary>Tests of the <see cref="View.Text"/> property with <see cref="View.AutoSize"/> set to false.</summary>
  4. public class AutoSizeFalseTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  8. {
  9. var super = new View { Frame = new (0, 0, 30, 80) };
  10. var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  11. super.Add (view);
  12. Assert.False (view.AutoSize);
  13. view.Text = "New text\nNew line";
  14. super.LayoutSubviews ();
  15. Rectangle expectedViewBounds = new (0, 0, 30, 80);
  16. Assert.False (view.AutoSize);
  17. Assert.Equal (expectedViewBounds, view.Viewport);
  18. Assert.False (view.IsInitialized);
  19. super.BeginInit ();
  20. super.EndInit ();
  21. Assert.True (view.IsInitialized);
  22. Assert.False (view.AutoSize);
  23. Assert.Equal (expectedViewBounds, view.Viewport);
  24. }
  25. [Fact]
  26. [SetupFakeDriver]
  27. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  28. {
  29. var text = "Views";
  30. var view = new View { Width = Dim.Fill () - text.Length, Height = 1, Text = text };
  31. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  32. frame.Add (view);
  33. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  34. frame.BeginInit ();
  35. frame.EndInit ();
  36. frame.LayoutSubviews ();
  37. Assert.Equal (5, text.Length);
  38. Assert.False (view.AutoSize);
  39. Assert.Equal (new (0, 0, 3, 1), view.Frame);
  40. Assert.Equal (new (3, 1), view.TextFormatter.Size);
  41. Assert.Equal (new() { "Vie" }, view.TextFormatter.GetLines ());
  42. Assert.Equal (new (0, 0, 10, 4), frame.Frame);
  43. frame.LayoutSubviews ();
  44. frame.Clear ();
  45. frame.Draw ();
  46. var expected = @"
  47. ┌────────┐
  48. │Vie │
  49. │ │
  50. └────────┘
  51. ";
  52. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  53. Assert.Equal (new (0, 0, 10, 4), pos);
  54. text = "0123456789";
  55. Assert.Equal (10, text.Length);
  56. view.Width = Dim.Fill () - text.Length;
  57. frame.LayoutSubviews ();
  58. frame.Clear ();
  59. frame.Draw ();
  60. Assert.Equal (new (0, 0, 0, 1), view.Frame);
  61. Assert.Equal (new (0, 1), view.TextFormatter.Size);
  62. Assert.Equal (new() { string.Empty }, view.TextFormatter.GetLines ());
  63. expected = @"
  64. ┌────────┐
  65. │ │
  66. │ │
  67. └────────┘
  68. ";
  69. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  70. Assert.Equal (new (0, 0, 10, 4), pos);
  71. }
  72. [Fact]
  73. [SetupFakeDriver]
  74. public void AutoSize_False_Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  75. {
  76. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  77. var top = new View { Width = 32, Height = 32 };
  78. var text = $"First line{Environment.NewLine}Second line";
  79. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  80. // Autosize is off, so we have to explicitly set TextFormatter.Size
  81. horizontalView.TextFormatter.Size = new (20, 1);
  82. var verticalView = new View
  83. {
  84. Y = 3,
  85. Height = 20,
  86. Width = 1,
  87. Text = text,
  88. TextDirection = TextDirection.TopBottom_LeftRight
  89. };
  90. // Autosize is off, so we have to explicitly set TextFormatter.Size
  91. verticalView.TextFormatter.Size = new (1, 20);
  92. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  93. frame.Add (horizontalView, verticalView);
  94. top.Add (frame);
  95. top.BeginInit ();
  96. top.EndInit ();
  97. Assert.False (horizontalView.AutoSize);
  98. Assert.False (verticalView.AutoSize);
  99. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  100. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  101. top.Draw ();
  102. var expected = @"
  103. ┌──────────────────────────────┐
  104. │First line Second li │
  105. │ │
  106. │ │
  107. │F │
  108. │i │
  109. │r │
  110. │s │
  111. │t │
  112. │ │
  113. │l │
  114. │i │
  115. │n │
  116. │e │
  117. │ │
  118. │S │
  119. │e │
  120. │c │
  121. │o │
  122. │n │
  123. │d │
  124. │ │
  125. │l │
  126. │i │
  127. │ │
  128. │ │
  129. │ │
  130. │ │
  131. │ │
  132. │ │
  133. │ │
  134. └──────────────────────────────┘
  135. ";
  136. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  137. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  138. Assert.True (verticalView.TextFormatter.NeedsFormat);
  139. // Autosize is off, so we have to explicitly set TextFormatter.Size
  140. // We know these glpyhs are 2 cols wide, so we need to widen the view
  141. verticalView.Width = 2;
  142. verticalView.TextFormatter.Size = new (2, 20);
  143. Assert.True (verticalView.TextFormatter.NeedsFormat);
  144. top.Draw ();
  145. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  146. expected = @"
  147. ┌──────────────────────────────┐
  148. │First line Second li │
  149. │ │
  150. │ │
  151. │最 │
  152. │初 │
  153. │の │
  154. │行 │
  155. │ │
  156. │二 │
  157. │行 │
  158. │目 │
  159. │ │
  160. │ │
  161. │ │
  162. │ │
  163. │ │
  164. │ │
  165. │ │
  166. │ │
  167. │ │
  168. │ │
  169. │ │
  170. │ │
  171. │ │
  172. │ │
  173. │ │
  174. │ │
  175. │ │
  176. │ │
  177. │ │
  178. └──────────────────────────────┘
  179. ";
  180. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  181. }
  182. }