TextTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. using System.Collections.Generic;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. /// <summary>
  6. /// Tests of the <see cref="View.Text"/> property with <see cref="View.AutoSize"/> set to false.
  7. /// </summary>
  8. public class TextTests {
  9. readonly ITestOutputHelper _output;
  10. public TextTests (ITestOutputHelper output) => _output = output;
  11. [Fact]
  12. [AutoInitShutdown]
  13. public void AutoSize_False_View_IsEmpty_False_Return_Null_Lines ()
  14. {
  15. var text = "Views";
  16. var view = new View {
  17. Width = Dim.Fill () - text.Length,
  18. Height = 1,
  19. Text = text
  20. };
  21. var win = new Window {
  22. Width = Dim.Fill (),
  23. Height = Dim.Fill ()
  24. };
  25. win.Add (view);
  26. Application.Top.Add (win);
  27. Application.Begin (Application.Top);
  28. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  29. Assert.Equal (5, text.Length);
  30. Assert.False (view.AutoSize);
  31. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  32. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  33. Assert.Equal (new List<string> { "Vie" }, view.TextFormatter.Lines);
  34. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  35. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  36. var expected = @"
  37. ┌────────┐
  38. │Vie │
  39. │ │
  40. └────────┘
  41. ";
  42. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  43. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  44. text = "0123456789";
  45. Assert.Equal (10, text.Length);
  46. view.Width = Dim.Fill () - text.Length;
  47. Application.Refresh ();
  48. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  49. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  50. Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines);
  51. expected = @"
  52. ┌────────┐
  53. │ │
  54. │ │
  55. └────────┘
  56. ";
  57. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  58. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  59. }
  60. [Fact]
  61. [AutoInitShutdown]
  62. public void AutoSize_False_View_IsEmpty_True_Minimum_Height ()
  63. {
  64. var text = "Views";
  65. var view = new View {
  66. Width = Dim.Fill () - text.Length,
  67. Text = text
  68. };
  69. var win = new Window {
  70. Width = Dim.Fill (),
  71. Height = Dim.Fill ()
  72. };
  73. win.Add (view);
  74. Application.Top.Add (win);
  75. Application.Begin (Application.Top);
  76. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  77. Assert.Equal (5, text.Length);
  78. Assert.False (view.AutoSize);
  79. Assert.Equal (new Rect (0, 0, 3, 1), view.Frame);
  80. Assert.Equal (new Size (3, 1), view.TextFormatter.Size);
  81. Assert.Single (view.TextFormatter.Lines);
  82. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  83. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  84. var expected = @"
  85. ┌────────┐
  86. │Vie │
  87. │ │
  88. └────────┘
  89. ";
  90. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  91. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  92. text = "0123456789";
  93. Assert.Equal (10, text.Length);
  94. view.Width = Dim.Fill () - text.Length;
  95. Application.Refresh ();
  96. Assert.Equal (new Rect (0, 0, 0, 1), view.Frame);
  97. Assert.Equal (new Size (0, 1), view.TextFormatter.Size);
  98. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines));
  99. Assert.Null (exception);
  100. expected = @"
  101. ┌────────┐
  102. │ │
  103. │ │
  104. └────────┘
  105. ";
  106. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  107. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  108. }
  109. [Fact]
  110. [AutoInitShutdown]
  111. public void AutoSize_False_Label_IsEmpty_True_Return_Null_Lines ()
  112. {
  113. var text = "Label";
  114. var label = new Label {
  115. Width = Dim.Fill () - text.Length,
  116. Height = 1,
  117. Text = text,
  118. AutoSize = false
  119. };
  120. var win = new Window {
  121. Width = Dim.Fill (),
  122. Height = Dim.Fill ()
  123. };
  124. win.Add (label);
  125. Application.Top.Add (win);
  126. Application.Begin (Application.Top);
  127. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  128. Assert.Equal (5, text.Length);
  129. Assert.False (label.AutoSize);
  130. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  131. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  132. Assert.Equal (new List<string> { "Lab" }, label.TextFormatter.Lines);
  133. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  134. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  135. var expected = @"
  136. ┌────────┐
  137. │Lab │
  138. │ │
  139. └────────┘
  140. ";
  141. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  142. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  143. text = "0123456789";
  144. Assert.Equal (10, text.Length);
  145. label.Width = Dim.Fill () - text.Length;
  146. Application.Refresh ();
  147. Assert.False (label.AutoSize);
  148. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  149. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  150. Assert.Equal (new List<string> { string.Empty }, label.TextFormatter.Lines);
  151. expected = @"
  152. ┌────────┐
  153. │ │
  154. │ │
  155. └────────┘
  156. ";
  157. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  158. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  159. }
  160. [Fact]
  161. [AutoInitShutdown]
  162. public void AutoSize_False_Label_Height_Zero_Returns_Minimum_Height ()
  163. {
  164. var text = "Label";
  165. var label = new Label {
  166. Width = Dim.Fill () - text.Length,
  167. Text = text,
  168. AutoSize = false
  169. };
  170. var win = new Window {
  171. Width = Dim.Fill (),
  172. Height = Dim.Fill ()
  173. };
  174. win.Add (label);
  175. Application.Top.Add (win);
  176. Application.Begin (Application.Top);
  177. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  178. Assert.Equal (5, text.Length);
  179. Assert.False (label.AutoSize);
  180. Assert.Equal (new Rect (0, 0, 3, 1), label.Frame);
  181. Assert.Equal (new Size (3, 1), label.TextFormatter.Size);
  182. Assert.Single (label.TextFormatter.Lines);
  183. Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
  184. Assert.Equal (new Rect (0, 0, 10, 4), Application.Top.Frame);
  185. var expected = @"
  186. ┌────────┐
  187. │Lab │
  188. │ │
  189. └────────┘
  190. ";
  191. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  192. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  193. text = "0123456789";
  194. Assert.Equal (10, text.Length);
  195. label.Width = Dim.Fill () - text.Length;
  196. Application.Refresh ();
  197. Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
  198. Assert.Equal (new Size (0, 1), label.TextFormatter.Size);
  199. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, label.TextFormatter.Lines));
  200. Assert.Null (exception);
  201. expected = @"
  202. ┌────────┐
  203. │ │
  204. │ │
  205. └────────┘
  206. ";
  207. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  208. Assert.Equal (new Rect (0, 0, 10, 4), pos);
  209. }
  210. [Fact]
  211. [AutoInitShutdown]
  212. public void AutoSize_False_View_Width_Null_Returns_Host_Frame_Width ()
  213. {
  214. var text = "Views";
  215. var view = new View {
  216. TextDirection = TextDirection.TopBottom_LeftRight,
  217. Height = Dim.Fill () - text.Length,
  218. Text = text
  219. };
  220. var win = new Window {
  221. Width = Dim.Fill (),
  222. Height = Dim.Fill ()
  223. };
  224. win.Add (view);
  225. Application.Top.Add (win);
  226. Application.Begin (Application.Top);
  227. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  228. Assert.Equal (5, text.Length);
  229. Assert.False (view.AutoSize);
  230. Assert.Equal (new Rect (0, 0, 1, 3), view.Frame);
  231. Assert.Equal (new Size (1, 3), view.TextFormatter.Size);
  232. Assert.Single (view.TextFormatter.Lines);
  233. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  234. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  235. var expected = @"
  236. ┌──┐
  237. │V │
  238. │i │
  239. │e │
  240. │ │
  241. │ │
  242. │ │
  243. │ │
  244. │ │
  245. └──┘
  246. ";
  247. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  248. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  249. text = "0123456789";
  250. Assert.Equal (10, text.Length);
  251. view.Height = Dim.Fill () - text.Length;
  252. Application.Refresh ();
  253. Assert.Equal (new Rect (0, 0, 1, 0), view.Frame);
  254. Assert.Equal (new Size (1, 0), view.TextFormatter.Size);
  255. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines));
  256. Assert.Null (exception);
  257. expected = @"
  258. ┌──┐
  259. │ │
  260. │ │
  261. │ │
  262. │ │
  263. │ │
  264. │ │
  265. │ │
  266. │ │
  267. └──┘
  268. ";
  269. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  270. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  271. }
  272. [Fact]
  273. [AutoInitShutdown]
  274. public void AutoSize_False_View_Width_Zero_Returns_Minimum_Width_With_Wide_Rune ()
  275. {
  276. var text = "界View";
  277. var view = new View {
  278. TextDirection = TextDirection.TopBottom_LeftRight,
  279. Height = Dim.Fill () - text.Length,
  280. Text = text
  281. };
  282. var win = new Window {
  283. Width = Dim.Fill (),
  284. Height = Dim.Fill ()
  285. };
  286. win.Add (view);
  287. Application.Top.Add (win);
  288. Application.Begin (Application.Top);
  289. ((FakeDriver)Application.Driver).SetBufferSize (4, 10);
  290. Assert.Equal (5, text.Length);
  291. Assert.False (view.AutoSize);
  292. Assert.Equal (new Rect (0, 0, 2, 3), view.Frame);
  293. Assert.Equal (new Size (2, 3), view.TextFormatter.Size);
  294. Assert.Single (view.TextFormatter.Lines);
  295. Assert.Equal (new Rect (0, 0, 4, 10), win.Frame);
  296. Assert.Equal (new Rect (0, 0, 4, 10), Application.Top.Frame);
  297. var expected = @"
  298. ┌──┐
  299. │界│
  300. │V │
  301. │i │
  302. │ │
  303. │ │
  304. │ │
  305. │ │
  306. │ │
  307. └──┘
  308. ";
  309. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  310. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  311. text = "0123456789";
  312. Assert.Equal (10, text.Length);
  313. view.Height = Dim.Fill () - text.Length;
  314. Application.Refresh ();
  315. Assert.Equal (new Rect (0, 0, 2, 0), view.Frame);
  316. Assert.Equal (new Size (2, 0), view.TextFormatter.Size);
  317. var exception = Record.Exception (() => Assert.Equal (new List<string> { string.Empty }, view.TextFormatter.Lines));
  318. Assert.Null (exception);
  319. expected = @"
  320. ┌──┐
  321. │ │
  322. │ │
  323. │ │
  324. │ │
  325. │ │
  326. │ │
  327. │ │
  328. │ │
  329. └──┘
  330. ";
  331. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  332. Assert.Equal (new Rect (0, 0, 4, 10), pos);
  333. }
  334. [Fact]
  335. public void AutoSize_False_If_Text_Empty ()
  336. {
  337. var view1 = new View ();
  338. var view2 = new View ("");
  339. var view3 = new View { Text = "" };
  340. Assert.False (view1.AutoSize);
  341. Assert.False (view2.AutoSize);
  342. Assert.False (view3.AutoSize);
  343. view1.Dispose ();
  344. view2.Dispose ();
  345. view3.Dispose ();
  346. }
  347. [Fact]
  348. public void AutoSize_False_If_Text_Is_Not_Empty ()
  349. {
  350. var view1 = new View ();
  351. view1.Text = "Hello World";
  352. var view2 = new View ("Hello World");
  353. var view3 = new View { Text = "Hello World" };
  354. Assert.False (view1.AutoSize);
  355. Assert.False (view2.AutoSize);
  356. Assert.False (view3.AutoSize);
  357. view1.Dispose ();
  358. view2.Dispose ();
  359. view3.Dispose ();
  360. }
  361. [Fact]
  362. public void AutoSize_False_ResizeView_Is_Always_False ()
  363. {
  364. var super = new View ();
  365. var label = new Label { AutoSize = false };
  366. super.Add (label);
  367. label.Text = "New text";
  368. super.LayoutSubviews ();
  369. Assert.False (label.AutoSize);
  370. Assert.Equal ("(0,0,0,1)", label.Bounds.ToString ());
  371. super.Dispose ();
  372. }
  373. [Fact]
  374. [AutoInitShutdown]
  375. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  376. {
  377. var win = new Window (new Rect (0, 0, 30, 80));
  378. var label = new Label { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  379. win.Add (label);
  380. Application.Top.Add (win);
  381. Assert.False (label.AutoSize);
  382. Assert.Equal ("(0,0,80,25)", label.Bounds.ToString ());
  383. label.Text = "New text\nNew line";
  384. Application.Top.LayoutSubviews ();
  385. Assert.False (label.AutoSize);
  386. Assert.Equal ("(0,0,28,78)", label.Bounds.ToString ());
  387. Assert.False (label.IsInitialized);
  388. var rs = Application.Begin (Application.Top);
  389. Assert.True (label.IsInitialized);
  390. Assert.False (label.AutoSize);
  391. Assert.Equal ("(0,0,28,78)", label.Bounds.ToString ());
  392. Application.End (rs);
  393. }
  394. [Fact]
  395. [AutoInitShutdown]
  396. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  397. {
  398. var view1 = new View { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  399. var view2 = new View { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  400. var view3 = new View { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  401. var view4 = new View {
  402. Text = "Say Hello view4 你",
  403. AutoSize = false,
  404. Width = 10,
  405. Height = 5,
  406. TextDirection = TextDirection.TopBottom_LeftRight
  407. };
  408. var view5 = new View {
  409. Text = "Say Hello view5 你",
  410. Width = 10,
  411. Height = 5,
  412. AutoSize = false,
  413. TextDirection = TextDirection.TopBottom_LeftRight
  414. };
  415. var view6 = new View {
  416. AutoSize = false,
  417. Width = 10,
  418. Height = 5,
  419. TextDirection = TextDirection.TopBottom_LeftRight,
  420. Text = "Say Hello view6 你"
  421. };
  422. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  423. Assert.False (view1.IsInitialized);
  424. Assert.False (view2.IsInitialized);
  425. Assert.False (view3.IsInitialized);
  426. Assert.False (view4.IsInitialized);
  427. Assert.False (view5.IsInitialized);
  428. Assert.False (view1.AutoSize);
  429. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  430. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  431. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  432. Assert.False (view2.AutoSize);
  433. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  434. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  435. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  436. Assert.False (view3.AutoSize);
  437. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  438. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  439. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  440. Assert.False (view4.AutoSize);
  441. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  442. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  443. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  444. Assert.False (view5.AutoSize);
  445. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  446. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  447. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  448. Assert.False (view6.AutoSize);
  449. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  450. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  451. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  452. var rs = Application.Begin (Application.Top);
  453. Assert.True (view1.IsInitialized);
  454. Assert.True (view2.IsInitialized);
  455. Assert.True (view3.IsInitialized);
  456. Assert.True (view4.IsInitialized);
  457. Assert.True (view5.IsInitialized);
  458. Assert.False (view1.AutoSize);
  459. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  460. Assert.Equal ("Absolute(10)", view1.Width.ToString ());
  461. Assert.Equal ("Absolute(5)", view1.Height.ToString ());
  462. Assert.False (view2.AutoSize);
  463. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  464. Assert.Equal ("Absolute(10)", view2.Width.ToString ());
  465. Assert.Equal ("Absolute(5)", view2.Height.ToString ());
  466. Assert.False (view3.AutoSize);
  467. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  468. Assert.Equal ("Absolute(10)", view3.Width.ToString ());
  469. Assert.Equal ("Absolute(5)", view3.Height.ToString ());
  470. Assert.False (view4.AutoSize);
  471. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  472. Assert.Equal ("Absolute(10)", view4.Width.ToString ());
  473. Assert.Equal ("Absolute(5)", view4.Height.ToString ());
  474. Assert.False (view5.AutoSize);
  475. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  476. Assert.Equal ("Absolute(10)", view5.Width.ToString ());
  477. Assert.Equal ("Absolute(5)", view5.Height.ToString ());
  478. Assert.False (view6.AutoSize);
  479. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  480. Assert.Equal ("Absolute(10)", view6.Width.ToString ());
  481. Assert.Equal ("Absolute(5)", view6.Height.ToString ());
  482. Application.End (rs);
  483. }
  484. }