AutoSizeFalseTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Pos;
  3. namespace Terminal.Gui.ViewTests;
  4. /// <summary>Tests of the <see cref="View.Text"/> property with <see cref="View.AutoSize"/> set to false.</summary>
  5. public class AutoSizeFalseTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public AutoSizeFalseTests (ITestOutputHelper output) { _output = output; }
  9. [Fact]
  10. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Different_Orders ()
  11. {
  12. var top = new View { Height = 25, Width = 80 };
  13. var view1 = new View { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  14. var view2 = new View { Text = "Say Hello view2 你", AutoSize = false, Width = 10, Height = 5 };
  15. var view3 = new View { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  16. var view4 = new View
  17. {
  18. Text = "Say Hello view4 你",
  19. AutoSize = false,
  20. Width = 10,
  21. Height = 5,
  22. TextDirection = TextDirection.TopBottom_LeftRight
  23. };
  24. var view5 = new View
  25. {
  26. Text = "Say Hello view5 你",
  27. AutoSize = false,
  28. Width = 10,
  29. Height = 5,
  30. TextDirection = TextDirection.TopBottom_LeftRight
  31. };
  32. var view6 = new View
  33. {
  34. AutoSize = false,
  35. Width = 10,
  36. Height = 5,
  37. TextDirection = TextDirection.TopBottom_LeftRight,
  38. Text = "Say Hello view6 你"
  39. };
  40. top.Add (view1, view2, view3, view4, view5, view6);
  41. Assert.False (view1.IsInitialized);
  42. Assert.False (view2.IsInitialized);
  43. Assert.False (view3.IsInitialized);
  44. Assert.False (view4.IsInitialized);
  45. Assert.False (view5.IsInitialized);
  46. Assert.False (view1.AutoSize);
  47. Assert.Equal (new Rectangle (0, 0, 10, 5), view1.Frame);
  48. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  49. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  50. Assert.False (view2.AutoSize);
  51. Assert.Equal (new Rectangle (0, 0, 10, 5), view2.Frame);
  52. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  53. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  54. Assert.False (view3.AutoSize);
  55. Assert.Equal (new Rectangle (0, 0, 10, 5), view3.Frame);
  56. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  57. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  58. Assert.False (view4.AutoSize);
  59. Assert.Equal (new Rectangle (0, 0, 10, 5), view4.Frame);
  60. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  61. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  62. Assert.False (view5.AutoSize);
  63. Assert.Equal (new Rectangle (0, 0, 10, 5), view5.Frame);
  64. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  65. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  66. Assert.False (view6.AutoSize);
  67. Assert.Equal (new Rectangle (0, 0, 10, 5), view6.Frame);
  68. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  69. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  70. top.BeginInit ();
  71. top.EndInit ();
  72. Assert.True (view1.IsInitialized);
  73. Assert.True (view2.IsInitialized);
  74. Assert.True (view3.IsInitialized);
  75. Assert.True (view4.IsInitialized);
  76. Assert.True (view5.IsInitialized);
  77. Assert.False (view1.AutoSize);
  78. Assert.Equal (new Rectangle (0, 0, 10, 5), view1.Frame);
  79. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  80. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  81. Assert.False (view2.AutoSize);
  82. Assert.Equal (new Rectangle (0, 0, 10, 5), view2.Frame);
  83. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  84. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  85. Assert.False (view3.AutoSize);
  86. Assert.Equal (new Rectangle (0, 0, 10, 5), view3.Frame);
  87. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  88. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  89. Assert.False (view4.AutoSize);
  90. Assert.Equal (new Rectangle (0, 0, 10, 5), view4.Frame);
  91. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  92. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  93. Assert.False (view5.AutoSize);
  94. Assert.Equal (new Rectangle (0, 0, 10, 5), view5.Frame);
  95. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  96. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  97. Assert.False (view6.AutoSize);
  98. Assert.Equal (new Rectangle (0, 0, 10, 5), view6.Frame);
  99. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  100. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  101. }
  102. [Fact]
  103. public void AutoSize_False_If_Text_Empty ()
  104. {
  105. var view1 = new View ();
  106. var view3 = new View { Text = "" };
  107. Assert.False (view1.AutoSize);
  108. Assert.False (view3.AutoSize);
  109. view1.Dispose ();
  110. view3.Dispose ();
  111. }
  112. [Fact]
  113. public void AutoSize_False_If_Text_Is_Not_Empty ()
  114. {
  115. var view1 = new View ();
  116. view1.Text = "Hello World";
  117. var view3 = new View { Text = "Hello World" };
  118. Assert.False (view1.AutoSize);
  119. Assert.False (view3.AutoSize);
  120. view1.Dispose ();
  121. view3.Dispose ();
  122. }
  123. [Fact]
  124. public void AutoSize_False_ResizeView_Is_Always_False ()
  125. {
  126. var super = new View ();
  127. var view = new View ();
  128. super.Add (view);
  129. view.Text = "New text";
  130. super.LayoutSubviews ();
  131. Rectangle expectedViewBounds = new (0, 0, 0, 0);
  132. Assert.False (view.AutoSize);
  133. Assert.Equal (expectedViewBounds, view.Viewport);
  134. super.Dispose ();
  135. }
  136. [Fact]
  137. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  138. {
  139. var super = new View { Frame = new Rectangle (0, 0, 30, 80) };
  140. var view = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  141. super.Add (view);
  142. Assert.False (view.AutoSize);
  143. view.Text = "New text\nNew line";
  144. super.LayoutSubviews ();
  145. Rectangle expectedViewBounds = new (0, 0, 30, 80);
  146. Assert.False (view.AutoSize);
  147. Assert.Equal (expectedViewBounds, view.Viewport);
  148. Assert.False (view.IsInitialized);
  149. super.BeginInit ();
  150. super.EndInit ();
  151. Assert.True (view.IsInitialized);
  152. Assert.False (view.AutoSize);
  153. Assert.Equal (expectedViewBounds, view.Viewport);
  154. }
  155. [Fact]
  156. [SetupFakeDriver]
  157. public void AutoSize_False_Setting_AutoSize_False_Keeps_Dims ()
  158. {
  159. var super = new View { Width = 10, Height = 10 };
  160. var view = new View ();
  161. view.Width = 2;
  162. view.Height = 1;
  163. Assert.Equal ("Absolute(2)", view.Width.ToString ());
  164. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  165. super.Add (view);
  166. super.BeginInit ();
  167. super.EndInit ();
  168. super.LayoutSubviews ();
  169. Assert.Equal ("Absolute(2)", view.Width.ToString ());
  170. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  171. view.AutoSize = true;
  172. Assert.Equal (Dim.Auto(Dim.DimAutoStyle.Text), view.Width);
  173. Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Text), view.Height);
  174. view.AutoSize = false;
  175. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  176. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  177. view.Width = 2;
  178. view.Height = 1;
  179. Assert.Equal ("Absolute(2)", view.Width.ToString ());
  180. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  181. view.AutoSize = false;
  182. Assert.Equal ("Absolute(2)", view.Width.ToString ());
  183. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  184. }
  185. [Fact]
  186. [SetupFakeDriver]
  187. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  188. {
  189. var text = "Views";
  190. var view = new View { Width = Dim.Fill () - text.Length, Height = 1, Text = text };
  191. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  192. frame.Add (view);
  193. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  194. frame.BeginInit ();
  195. frame.EndInit ();
  196. frame.LayoutSubviews ();
  197. Assert.Equal (5, text.Length);
  198. Assert.False (view.AutoSize);
  199. Assert.Equal (new (0, 0, 3, 1), view.Frame);
  200. Assert.Equal (new (3, 1), view.TextFormatter.Size);
  201. Assert.Equal (new List<string> { "Vie" }, view.TextFormatter.GetLines ());
  202. Assert.Equal (new (0, 0, 10, 4), frame.Frame);
  203. frame.LayoutSubviews ();
  204. frame.Clear ();
  205. frame.Draw ();
  206. var expected = @"
  207. ┌────────┐
  208. │Vie │
  209. │ │
  210. └────────┘
  211. ";
  212. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  213. Assert.Equal (new (0, 0, 10, 4), pos);
  214. text = "0123456789";
  215. Assert.Equal (10, text.Length);
  216. view.Width = Dim.Fill () - text.Length;
  217. frame.LayoutSubviews ();
  218. frame.Clear ();
  219. frame.Draw ();
  220. Assert.Equal (new (0, 0, 0, 1), view.Frame);
  221. Assert.Equal (new (0, 1), view.TextFormatter.Size);
  222. Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.GetLines ());
  223. expected = @"
  224. ┌────────┐
  225. │ │
  226. │ │
  227. └────────┘
  228. ";
  229. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  230. Assert.Equal (new (0, 0, 10, 4), pos);
  231. }
  232. [Fact]
  233. [SetupFakeDriver]
  234. public void AutoSize_False_Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  235. {
  236. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  237. var top = new View { Width = 32, Height = 32 };
  238. var text = $"First line{Environment.NewLine}Second line";
  239. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  240. // Autosize is off, so we have to explicitly set TextFormatter.Size
  241. horizontalView.TextFormatter.Size = new (20, 1);
  242. var verticalView = new View
  243. {
  244. Y = 3,
  245. Height = 20,
  246. Width = 1,
  247. Text = text,
  248. TextDirection = TextDirection.TopBottom_LeftRight
  249. };
  250. // Autosize is off, so we have to explicitly set TextFormatter.Size
  251. verticalView.TextFormatter.Size = new (1, 20);
  252. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  253. frame.Add (horizontalView, verticalView);
  254. top.Add (frame);
  255. top.BeginInit ();
  256. top.EndInit ();
  257. Assert.False (horizontalView.AutoSize);
  258. Assert.False (verticalView.AutoSize);
  259. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  260. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  261. top.Draw ();
  262. var expected = @"
  263. ┌──────────────────────────────┐
  264. │First line Second li │
  265. │ │
  266. │ │
  267. │F │
  268. │i │
  269. │r │
  270. │s │
  271. │t │
  272. │ │
  273. │l │
  274. │i │
  275. │n │
  276. │e │
  277. │ │
  278. │S │
  279. │e │
  280. │c │
  281. │o │
  282. │n │
  283. │d │
  284. │ │
  285. │l │
  286. │i │
  287. │ │
  288. │ │
  289. │ │
  290. │ │
  291. │ │
  292. │ │
  293. │ │
  294. └──────────────────────────────┘
  295. ";
  296. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  297. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  298. Assert.True (verticalView.TextFormatter.NeedsFormat);
  299. // Autosize is off, so we have to explicitly set TextFormatter.Size
  300. // We know these glpyhs are 2 cols wide, so we need to widen the view
  301. verticalView.Width = 2;
  302. verticalView.TextFormatter.Size = new (2, 20);
  303. Assert.True (verticalView.TextFormatter.NeedsFormat);
  304. top.Draw ();
  305. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  306. expected = @"
  307. ┌──────────────────────────────┐
  308. │First line Second li │
  309. │ │
  310. │ │
  311. │最 │
  312. │初 │
  313. │の │
  314. │行 │
  315. │ │
  316. │二 │
  317. │行 │
  318. │目 │
  319. │ │
  320. │ │
  321. │ │
  322. │ │
  323. │ │
  324. │ │
  325. │ │
  326. │ │
  327. │ │
  328. │ │
  329. │ │
  330. │ │
  331. │ │
  332. │ │
  333. │ │
  334. │ │
  335. │ │
  336. │ │
  337. │ │
  338. └──────────────────────────────┘
  339. ";
  340. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  341. }
  342. }