TextTests.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. using System.Runtime.CompilerServices;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. /// <summary>
  6. /// Tests of the <see cref="View.Text"/> and <see cref="View.TextFormatter"/> properties.
  7. /// </summary>
  8. public class TextTests (ITestOutputHelper output)
  9. {
  10. // TextFormatter.Size should be empty unless DimAuto is set or ContentSize is set
  11. [Theory]
  12. [InlineData ("", 0, 0)]
  13. [InlineData (" ", 0, 0)]
  14. [InlineData ("01234", 0, 0)]
  15. public void TextFormatter_Size_Default (string text, int expectedW, int expectedH)
  16. {
  17. var view = new View ();
  18. view.Text = text;
  19. view.Layout ();
  20. Assert.Equal (new (expectedW, expectedH), view.TextFormatter.ConstrainToSize);
  21. }
  22. // TextFormatter.Size should track ContentSize (without DimAuto)
  23. [Theory]
  24. [InlineData ("", 1, 1)]
  25. [InlineData (" ", 1, 1)]
  26. [InlineData ("01234", 1, 1)]
  27. public void TextFormatter_Size_Tracks_ContentSize (string text, int expectedW, int expectedH)
  28. {
  29. var view = new View ();
  30. view.SetContentSize (new (1, 1));
  31. view.Text = text;
  32. view.Layout ();
  33. Assert.Equal (new (expectedW, expectedH), view.TextFormatter.ConstrainToSize);
  34. }
  35. [Fact]
  36. [SetupFakeDriver]
  37. public void Setting_With_Height_Horizontal ()
  38. {
  39. var top = new View { Width = 25, Height = 25 };
  40. var label = new Label { Text = "Hello", /* Width = 10, Height = 2, */ ValidatePosDim = true };
  41. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  42. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  43. top.Add (label, viewX, viewY);
  44. top.Layout ();
  45. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  46. top.LayoutSubviews ();
  47. top.Draw ();
  48. var expected = @"
  49. HelloX
  50. Y
  51. ";
  52. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  53. label.Width = 10;
  54. label.Height = 2;
  55. Assert.Equal (new (0, 0, 10, 2), label.Frame);
  56. top.LayoutSubviews ();
  57. View.SetClipToScreen ();
  58. top.Draw ();
  59. expected = @"
  60. Hello X
  61. Y
  62. ";
  63. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  64. }
  65. [Fact]
  66. [AutoInitShutdown]
  67. public void Setting_With_Height_Vertical ()
  68. {
  69. // BUGBUG: Label is Width = Dim.Auto (), Height = Dim.Auto (), so Width & Height are ignored
  70. var label = new Label
  71. { /*Width = 2, Height = 10, */
  72. TextDirection = TextDirection.TopBottom_LeftRight, ValidatePosDim = true
  73. };
  74. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  75. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  76. var top = new Toplevel ();
  77. top.Add (label, viewX, viewY);
  78. RunState rs = Application.Begin (top);
  79. label.Text = "Hello";
  80. Application.RunIteration (ref rs);
  81. Assert.Equal (new (0, 0, 1, 5), label.Frame);
  82. var expected = @"
  83. HX
  84. e
  85. l
  86. l
  87. o
  88. Y
  89. ";
  90. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  91. label.Width = 2;
  92. label.Height = 10;
  93. Application.RunIteration (ref rs);
  94. Assert.Equal (new (0, 0, 2, 10), label.Frame);
  95. expected = @"
  96. H X
  97. e
  98. l
  99. l
  100. o
  101. Y
  102. "
  103. ;
  104. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  105. Application.End (rs);
  106. top.Dispose ();
  107. }
  108. [Fact]
  109. [AutoInitShutdown]
  110. public void TextDirection_Toggle ()
  111. {
  112. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  113. var view = new View ();
  114. win.Add (view);
  115. var top = new Toplevel ();
  116. top.Add (win);
  117. RunState rs = Application.Begin (top);
  118. ((FakeDriver)Application.Driver!).SetBufferSize (15, 15);
  119. Assert.Equal (new (0, 0, 15, 15), win.Frame);
  120. Assert.Equal (new (0, 0, 15, 15), win.Margin.Frame);
  121. Assert.Equal (new (0, 0, 15, 15), win.Border.Frame);
  122. Assert.Equal (new (1, 1, 13, 13), win.Padding.Frame);
  123. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  124. Assert.Equal (Rectangle.Empty, view.Frame);
  125. Assert.Equal ("Absolute(0)", view.X.ToString ());
  126. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  127. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  128. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  129. var expected = @"
  130. ┌─────────────┐
  131. │ │
  132. │ │
  133. │ │
  134. │ │
  135. │ │
  136. │ │
  137. │ │
  138. │ │
  139. │ │
  140. │ │
  141. │ │
  142. │ │
  143. │ │
  144. └─────────────┘
  145. ";
  146. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  147. view.Text = "Hello World";
  148. view.Width = 11;
  149. view.Height = 1;
  150. Application.RunIteration (ref rs);
  151. Assert.Equal (new (0, 0, 11, 1), view.Frame);
  152. Assert.Equal ("Absolute(0)", view.X.ToString ());
  153. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  154. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  155. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  156. expected = @"
  157. ┌─────────────┐
  158. │Hello World │
  159. │ │
  160. │ │
  161. │ │
  162. │ │
  163. │ │
  164. │ │
  165. │ │
  166. │ │
  167. │ │
  168. │ │
  169. │ │
  170. │ │
  171. └─────────────┘
  172. ";
  173. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  174. view.Width = Dim.Auto ();
  175. view.Height = Dim.Auto ();
  176. view.Text = "Hello Worlds";
  177. Application.RunIteration (ref rs);
  178. int len = "Hello Worlds".Length;
  179. Assert.Equal (12, len);
  180. Assert.Equal (new (0, 0, len, 1), view.Frame);
  181. expected = @"
  182. ┌─────────────┐
  183. │Hello Worlds │
  184. │ │
  185. │ │
  186. │ │
  187. │ │
  188. │ │
  189. │ │
  190. │ │
  191. │ │
  192. │ │
  193. │ │
  194. │ │
  195. │ │
  196. └─────────────┘
  197. ";
  198. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  199. view.TextDirection = TextDirection.TopBottom_LeftRight;
  200. Application.RunIteration (ref rs);
  201. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  202. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  203. expected = @"
  204. ┌─────────────┐
  205. │H │
  206. │e │
  207. │l │
  208. │l │
  209. │o │
  210. │ │
  211. │W │
  212. │o │
  213. │r │
  214. │l │
  215. │d │
  216. │s │
  217. │ │
  218. └─────────────┘
  219. ";
  220. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  221. // Setting to false causes Width and Height to be set to the current ContentSize
  222. view.Width = 1;
  223. view.Height = 12;
  224. Application.RunIteration (ref rs);
  225. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  226. view.Width = 12;
  227. view.Height = 1;
  228. view.TextFormatter.ConstrainToSize = new (12, 1);
  229. Application.RunIteration (ref rs);
  230. Assert.Equal (new (12, 1), view.TextFormatter.ConstrainToSize);
  231. Assert.Equal (new (0, 0, 12, 1), view.Frame);
  232. top.ClearViewport ();
  233. view.SetNeedsDraw ();
  234. view.Draw ();
  235. expected = @" HelloWorlds";
  236. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  237. Application.RunIteration (ref rs);
  238. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  239. // that the text will be spread "vertically" across 1 line.
  240. // Hence no space.
  241. expected = @"
  242. ┌─────────────┐
  243. │HelloWorlds │
  244. │ │
  245. │ │
  246. │ │
  247. │ │
  248. │ │
  249. │ │
  250. │ │
  251. │ │
  252. │ │
  253. │ │
  254. │ │
  255. │ │
  256. └─────────────┘
  257. ";
  258. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  259. view.PreserveTrailingSpaces = true;
  260. Application.RunIteration (ref rs);
  261. Assert.Equal (new (0, 0, 12, 1), view.Frame);
  262. expected = @"
  263. ┌─────────────┐
  264. │Hello Worlds │
  265. │ │
  266. │ │
  267. │ │
  268. │ │
  269. │ │
  270. │ │
  271. │ │
  272. │ │
  273. │ │
  274. │ │
  275. │ │
  276. │ │
  277. └─────────────┘
  278. ";
  279. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  280. view.PreserveTrailingSpaces = false;
  281. Rectangle f = view.Frame;
  282. view.Width = f.Height;
  283. view.Height = f.Width;
  284. view.TextDirection = TextDirection.TopBottom_LeftRight;
  285. Application.RunIteration (ref rs);
  286. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  287. expected = @"
  288. ┌─────────────┐
  289. │H │
  290. │e │
  291. │l │
  292. │l │
  293. │o │
  294. │ │
  295. │W │
  296. │o │
  297. │r │
  298. │l │
  299. │d │
  300. │s │
  301. │ │
  302. └─────────────┘
  303. ";
  304. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  305. view.Width = Dim.Auto ();
  306. view.Height = Dim.Auto ();
  307. Application.RunIteration (ref rs);
  308. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  309. expected = @"
  310. ┌─────────────┐
  311. │H │
  312. │e │
  313. │l │
  314. │l │
  315. │o │
  316. │ │
  317. │W │
  318. │o │
  319. │r │
  320. │l │
  321. │d │
  322. │s │
  323. │ │
  324. └─────────────┘
  325. ";
  326. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  327. Application.End (rs);
  328. top.Dispose ();
  329. }
  330. [Fact]
  331. [AutoInitShutdown]
  332. public void View_IsEmpty_False_Minimum_Width ()
  333. {
  334. var text = "Views";
  335. var view = new View
  336. {
  337. TextDirection = TextDirection.TopBottom_LeftRight,
  338. Height = Dim.Fill () - text.Length,
  339. Text = text
  340. };
  341. view.Width = Dim.Auto ();
  342. view.Height = Dim.Auto ();
  343. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  344. win.Add (view);
  345. var top = new Toplevel ();
  346. top.Add (win);
  347. RunState rs = Application.Begin (top);
  348. ((FakeDriver)Application.Driver!).SetBufferSize (4, 10);
  349. Assert.Equal (5, text.Length);
  350. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  351. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  352. Assert.Equal (new () { "Views" }, view.TextFormatter.GetLines ());
  353. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  354. Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
  355. var expected = @"
  356. ┌──┐
  357. │V │
  358. │i │
  359. │e │
  360. │w │
  361. │s │
  362. │ │
  363. │ │
  364. │ │
  365. └──┘
  366. ";
  367. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  368. Assert.Equal (new (0, 0, 4, 10), pos);
  369. text = "0123456789";
  370. Assert.Equal (10, text.Length);
  371. //view.Height = Dim.Fill () - text.Length;
  372. Application.RunIteration (ref rs);
  373. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  374. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  375. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  376. Assert.Null (exception);
  377. expected = @"
  378. ┌──┐
  379. │V │
  380. │i │
  381. │e │
  382. │w │
  383. │s │
  384. │ │
  385. │ │
  386. │ │
  387. └──┘
  388. ";
  389. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  390. Assert.Equal (new (0, 0, 4, 10), pos);
  391. top.Dispose ();
  392. }
  393. [Fact]
  394. [SetupFakeDriver]
  395. public void DimAuto_Vertical_TextDirection_Wide_Rune ()
  396. {
  397. var text = "界View";
  398. var view = new View
  399. {
  400. TextDirection = TextDirection.TopBottom_LeftRight,
  401. Text = text,
  402. Width = Dim.Auto (),
  403. Height = Dim.Auto ()
  404. };
  405. view.SetRelativeLayout (new Size (4, 10));
  406. Assert.Equal (5, text.Length);
  407. // Vertical text - 2 wide, 5 down
  408. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  409. Assert.Equal (new (2, 5), view.TextFormatter.ConstrainToSize);
  410. Assert.Equal (new () { "界View" }, view.TextFormatter.GetLines ());
  411. view.Draw ();
  412. var expected = @"
  413. V
  414. i
  415. e
  416. w ";
  417. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  418. }
  419. [Fact]
  420. [AutoInitShutdown]
  421. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  422. {
  423. var text = $"0123456789{Environment.NewLine}01234567891";
  424. var horizontalView = new View
  425. {
  426. Width = Dim.Auto (),
  427. Height = Dim.Auto (),
  428. Text = text
  429. };
  430. var verticalView = new View
  431. {
  432. Width = Dim.Auto (),
  433. Height = Dim.Auto (),
  434. Y = 3,
  435. //Height = 11,
  436. //Width = 2,
  437. Text = text,
  438. TextDirection = TextDirection.TopBottom_LeftRight
  439. };
  440. var win = new Window
  441. {
  442. Width = Dim.Fill (),
  443. Height = Dim.Fill (),
  444. Text = "Window"
  445. };
  446. win.Add (horizontalView, verticalView);
  447. var top = new Toplevel ();
  448. top.Add (win);
  449. RunState rs = Application.Begin (top);
  450. ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
  451. Assert.Equal (new (0, 0, 11, 2), horizontalView.Frame);
  452. Assert.Equal (new (0, 3, 2, 11), verticalView.Frame);
  453. var expected = @"
  454. ┌──────────────────┐
  455. │0123456789 │
  456. │01234567891 │
  457. │ │
  458. │00 │
  459. │11 │
  460. │22 │
  461. │33 │
  462. │44 │
  463. │55 │
  464. │66 │
  465. │77 │
  466. │88 │
  467. │99 │
  468. │ 1 │
  469. │ │
  470. │ │
  471. │ │
  472. │ │
  473. └──────────────────┘
  474. ";
  475. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  476. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  477. Application.RunIteration (ref rs);
  478. Assert.Equal (new (0, 3, 4, 4), verticalView.Frame);
  479. expected = @"
  480. ┌──────────────────┐
  481. │0123456789 │
  482. │01234567891 │
  483. │ │
  484. │最二 │
  485. │初行 │
  486. │の目 │
  487. │行 │
  488. │ │
  489. │ │
  490. │ │
  491. │ │
  492. │ │
  493. │ │
  494. │ │
  495. │ │
  496. │ │
  497. │ │
  498. │ │
  499. └──────────────────┘
  500. ";
  501. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  502. Application.End (rs);
  503. top.Dispose ();
  504. }
  505. [Fact]
  506. [AutoInitShutdown]
  507. public void Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  508. {
  509. var text = "Finish 終";
  510. var horizontalView = new View
  511. {
  512. Id = "horizontalView",
  513. Width = Dim.Auto (), Height = Dim.Auto (), Text = text
  514. };
  515. var verticalView = new View
  516. {
  517. Id = "verticalView",
  518. Y = 3,
  519. Width = Dim.Auto (),
  520. Height = Dim.Auto (),
  521. Text = text,
  522. TextDirection = TextDirection.TopBottom_LeftRight
  523. };
  524. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  525. win.Add (horizontalView, verticalView);
  526. var top = new Toplevel ();
  527. top.Add (win);
  528. RunState rs = Application.Begin (top);
  529. ((FakeDriver)Application.Driver!).SetBufferSize (22, 22);
  530. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.ConstrainToSize);
  531. Assert.Equal (new (2, 8), verticalView.TextFormatter.ConstrainToSize);
  532. //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  533. //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  534. var expected = @"
  535. ┌────────────────────┐
  536. │Finish 終 │
  537. │ │
  538. │ │
  539. │F │
  540. │i │
  541. │n │
  542. │i │
  543. │s │
  544. │h │
  545. │ │
  546. │終 │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. │ │
  552. │ │
  553. │ │
  554. │ │
  555. │ │
  556. └────────────────────┘
  557. ";
  558. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  559. verticalView.Text = "最初の行二行目";
  560. Application.RunIteration (ref rs);
  561. // height was initialized with 8 and can only grow or keep initial value
  562. Assert.Equal (new (0, 3, 2, 7), verticalView.Frame);
  563. expected = @"
  564. ┌────────────────────┐
  565. │Finish 終 │
  566. │ │
  567. │ │
  568. │最 │
  569. │初 │
  570. │の │
  571. │行 │
  572. │二 │
  573. │行 │
  574. │目 │
  575. │ │
  576. │ │
  577. │ │
  578. │ │
  579. │ │
  580. │ │
  581. │ │
  582. │ │
  583. │ │
  584. │ │
  585. └────────────────────┘
  586. ";
  587. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  588. Application.End (rs);
  589. top.Dispose ();
  590. }
  591. [Fact]
  592. [AutoInitShutdown]
  593. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  594. {
  595. var lbl = new Label { Text = "123" };
  596. var top = new Toplevel ();
  597. top.Add (lbl);
  598. RunState rs = Application.Begin (top);
  599. Application.RunIteration (ref rs);
  600. Assert.Equal (new (0, 0, 3, 1), lbl.Frame);
  601. Assert.Equal ("123 ", GetContents ());
  602. lbl.Text = "12";
  603. lbl.Layout ();
  604. Assert.Equal (new (0, 0, 2, 1), lbl.Frame);
  605. Assert.Equal (new (0, 0, 2, 1), lbl._needsDrawRect);
  606. Assert.Equal (new (0, 0, 80, 25), lbl.SuperView._needsDrawRect);
  607. Assert.True (lbl.SuperView.NeedsLayout);
  608. Application.RunIteration (ref rs);
  609. Assert.Equal ("12 ", GetContents ());
  610. string GetContents ()
  611. {
  612. var text = "";
  613. for (var i = 0; i < 4; i++)
  614. {
  615. text += Application.Driver?.Contents [0, i].Rune;
  616. }
  617. return text;
  618. }
  619. Application.End (rs);
  620. top.Dispose ();
  621. }
  622. [Theory]
  623. [AutoInitShutdown]
  624. [InlineData (true)]
  625. [InlineData (false)]
  626. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  627. {
  628. var text = "Hello World";
  629. var width = 20;
  630. var lblLeft = new View
  631. {
  632. Text = text,
  633. Width = width,
  634. Height = 1
  635. };
  636. if (autoSize)
  637. {
  638. lblLeft.Width = Dim.Auto ();
  639. lblLeft.Height = Dim.Auto ();
  640. }
  641. var lblCenter = new View
  642. {
  643. Text = text,
  644. Y = 1,
  645. Width = width,
  646. Height = 1,
  647. TextAlignment = Alignment.Center
  648. };
  649. if (autoSize)
  650. {
  651. lblCenter.Width = Dim.Auto ();
  652. lblCenter.Height = Dim.Auto ();
  653. }
  654. var lblRight = new View
  655. {
  656. Text = text,
  657. Y = 2,
  658. Width = width,
  659. Height = 1,
  660. TextAlignment = Alignment.End
  661. };
  662. if (autoSize)
  663. {
  664. lblRight.Width = Dim.Auto ();
  665. lblRight.Height = Dim.Auto ();
  666. }
  667. var lblJust = new View
  668. {
  669. Text = text,
  670. Y = 3,
  671. Width = width,
  672. Height = 1,
  673. TextAlignment = Alignment.Fill
  674. };
  675. if (autoSize)
  676. {
  677. lblJust.Width = Dim.Auto ();
  678. lblJust.Height = Dim.Auto ();
  679. }
  680. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  681. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  682. var top = new Toplevel ();
  683. top.Add (frame);
  684. Application.Begin (top);
  685. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  686. // frame.Width is width + border wide (20 + 2) and 6 high
  687. if (autoSize)
  688. {
  689. Size expectedSize = new (11, 1);
  690. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  691. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  692. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  693. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  694. }
  695. else
  696. {
  697. Size expectedSize = new (width, 1);
  698. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  699. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  700. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  701. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  702. }
  703. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  704. string expected;
  705. if (autoSize)
  706. {
  707. expected = @"
  708. ┌────────────────────┐
  709. │Hello World │
  710. │Hello World │
  711. │Hello World │
  712. │Hello World │
  713. └────────────────────┘
  714. ";
  715. }
  716. else
  717. {
  718. expected = @"
  719. ┌────────────────────┐
  720. │Hello World │
  721. │ Hello World │
  722. │ Hello World│
  723. │Hello World│
  724. └────────────────────┘
  725. ";
  726. }
  727. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  728. Assert.Equal (new (0, 0, width + 2, 6), pos);
  729. top.Dispose ();
  730. }
  731. [Theory]
  732. [AutoInitShutdown]
  733. [InlineData (true)]
  734. [InlineData (false)]
  735. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  736. {
  737. var text = "Hello World";
  738. var height = 20;
  739. var lblLeft = new View
  740. {
  741. Text = text,
  742. Width = 1,
  743. Height = height,
  744. TextDirection = TextDirection.TopBottom_LeftRight
  745. };
  746. if (autoSize)
  747. {
  748. lblLeft.Width = Dim.Auto ();
  749. lblLeft.Height = Dim.Auto ();
  750. }
  751. var lblCenter = new View
  752. {
  753. Text = text,
  754. X = 2,
  755. Width = 1,
  756. Height = height,
  757. TextDirection = TextDirection.TopBottom_LeftRight,
  758. VerticalTextAlignment = Alignment.Center
  759. };
  760. if (autoSize)
  761. {
  762. lblCenter.Width = Dim.Auto ();
  763. lblCenter.Height = Dim.Auto ();
  764. }
  765. var lblRight = new View
  766. {
  767. Text = text,
  768. X = 4,
  769. Width = 1,
  770. Height = height,
  771. TextDirection = TextDirection.TopBottom_LeftRight,
  772. VerticalTextAlignment = Alignment.End
  773. };
  774. if (autoSize)
  775. {
  776. lblRight.Width = Dim.Auto ();
  777. lblRight.Height = Dim.Auto ();
  778. }
  779. var lblJust = new View
  780. {
  781. Text = text,
  782. X = 6,
  783. Width = 1,
  784. Height = height,
  785. TextDirection = TextDirection.TopBottom_LeftRight,
  786. VerticalTextAlignment = Alignment.Fill
  787. };
  788. if (autoSize)
  789. {
  790. lblJust.Width = Dim.Auto ();
  791. lblJust.Height = Dim.Auto ();
  792. }
  793. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  794. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  795. var top = new Toplevel ();
  796. top.Add (frame);
  797. Application.Begin (top);
  798. ((FakeDriver)Application.Driver!).SetBufferSize (9, height + 2);
  799. if (autoSize)
  800. {
  801. Assert.Equal (new (1, 11), lblLeft.TextFormatter.ConstrainToSize);
  802. Assert.Equal (new (1, 11), lblCenter.TextFormatter.ConstrainToSize);
  803. Assert.Equal (new (1, 11), lblRight.TextFormatter.ConstrainToSize);
  804. Assert.Equal (new (1, 11), lblJust.TextFormatter.ConstrainToSize);
  805. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  806. }
  807. else
  808. {
  809. Assert.Equal (new (1, height), lblLeft.TextFormatter.ConstrainToSize);
  810. Assert.Equal (new (1, height), lblCenter.TextFormatter.ConstrainToSize);
  811. Assert.Equal (new (1, height), lblRight.TextFormatter.ConstrainToSize);
  812. Assert.Equal (new (1, height), lblJust.TextFormatter.ConstrainToSize);
  813. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  814. }
  815. string expected;
  816. if (autoSize)
  817. {
  818. expected = @"
  819. ┌───────┐
  820. │H H H H│
  821. │e e e e│
  822. │l l l l│
  823. │l l l l│
  824. │o o o o│
  825. │ │
  826. │W W W W│
  827. │o o o o│
  828. │r r r r│
  829. │l l l l│
  830. │d d d d│
  831. │ │
  832. │ │
  833. │ │
  834. │ │
  835. │ │
  836. │ │
  837. │ │
  838. │ │
  839. │ │
  840. └───────┘
  841. ";
  842. }
  843. else
  844. {
  845. expected = @"
  846. ┌───────┐
  847. │H H│
  848. │e e│
  849. │l l│
  850. │l l│
  851. │o H o│
  852. │ e │
  853. │W l │
  854. │o l │
  855. │r o │
  856. │l H │
  857. │d W e │
  858. │ o l │
  859. │ r l │
  860. │ l o │
  861. │ d │
  862. │ W W│
  863. │ o o│
  864. │ r r│
  865. │ l l│
  866. │ d d│
  867. └───────┘
  868. ";
  869. }
  870. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  871. Assert.Equal (new (0, 0, 9, height + 2), pos);
  872. top.Dispose ();
  873. }
  874. // Test that View.PreserveTrailingSpaces removes trailing spaces
  875. [Fact]
  876. public void PreserveTrailingSpaces_Removes_Trailing_Spaces ()
  877. {
  878. var view = new View { Text = "Hello World " };
  879. Assert.Equal ("Hello World ", view.TextFormatter.Text);
  880. view.TextFormatter.WordWrap = true;
  881. view.TextFormatter.ConstrainToSize = new (5, 3);
  882. view.PreserveTrailingSpaces = false;
  883. Assert.Equal ($"Hello{Environment.NewLine}World", view.TextFormatter.Format ());
  884. view.PreserveTrailingSpaces = true;
  885. Assert.Equal ($"Hello{Environment.NewLine} {Environment.NewLine}World", view.TextFormatter.Format ());
  886. }
  887. // View.PreserveTrailingSpaces Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  888. // or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  889. // If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  890. // <see cref = "Text" / > is formatted for display.The default is <see langword = "false" / >.
  891. [Fact]
  892. public void PreserveTrailingSpaces_Set_Get ()
  893. {
  894. var view = new View { Text = "Hello World" };
  895. Assert.False (view.PreserveTrailingSpaces);
  896. view.PreserveTrailingSpaces = true;
  897. Assert.True (view.PreserveTrailingSpaces);
  898. }
  899. // Setting TextFormatter DOES NOT update Text
  900. [Fact]
  901. public void SettingTextFormatterDoesNotUpdateText ()
  902. {
  903. var view = new View ();
  904. view.TextFormatter.Text = "Hello World";
  905. Assert.True (string.IsNullOrEmpty (view.Text));
  906. }
  907. // Setting Text updates TextFormatter
  908. [Fact]
  909. public void SettingTextUpdatesTextFormatter ()
  910. {
  911. var view = new View { Text = "Hello World" };
  912. Assert.Equal ("Hello World", view.Text);
  913. Assert.Equal ("Hello World", view.TextFormatter.Text);
  914. }
  915. // Setting Text does NOT set the HotKey
  916. [Fact]
  917. public void Text_Does_Not_Set_HotKey ()
  918. {
  919. var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
  920. Assert.NotEqual (Key.H, view.HotKey);
  921. }
  922. // Test that TextFormatter is init only
  923. [Fact]
  924. public void TextFormatterIsInitOnly ()
  925. {
  926. var view = new View ();
  927. // Use reflection to ensure the TextFormatter property is `init` only
  928. Assert.Contains (
  929. typeof (IsExternalInit),
  930. typeof (View).GetMethod ("set_TextFormatter")
  931. .ReturnParameter.GetRequiredCustomModifiers ());
  932. }
  933. // Test that the Text property is set correctly.
  934. [Fact]
  935. public void TextProperty ()
  936. {
  937. var view = new View { Text = "Hello World" };
  938. Assert.Equal ("Hello World", view.Text);
  939. }
  940. // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
  941. [Fact]
  942. public void UpdateTextFormatterText_Overridden ()
  943. {
  944. var view = new TestView { Text = "Hello World" };
  945. Assert.Equal ("Hello World", view.Text);
  946. Assert.Equal (">Hello World<", view.TextFormatter.Text);
  947. }
  948. private class TestView : View
  949. {
  950. protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
  951. }
  952. [Fact]
  953. public void TextDirection_Horizontal_Dims_Correct ()
  954. {
  955. // Initializes a view with a vertical direction
  956. var view = new View
  957. {
  958. Text = "01234",
  959. TextDirection = TextDirection.LeftRight_TopBottom,
  960. Width = Dim.Auto (DimAutoStyle.Text),
  961. Height = Dim.Auto (DimAutoStyle.Text)
  962. };
  963. Assert.True (view.NeedsLayout);
  964. view.Layout ();
  965. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  966. Assert.Equal (new (0, 0, 5, 1), view.Viewport);
  967. view.BeginInit ();
  968. view.EndInit ();
  969. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  970. Assert.Equal (new (0, 0, 5, 1), view.Viewport);
  971. }
  972. // BUGBUG: this is a temporary test that helped identify #3469 - It needs to be expanded upon (and renamed)
  973. [Fact]
  974. public void TextDirection_Horizontal_Dims_Correct_WidthAbsolute ()
  975. {
  976. var view = new View
  977. {
  978. Text = "01234",
  979. TextDirection = TextDirection.LeftRight_TopBottom,
  980. TextAlignment = Alignment.Center,
  981. Width = 10,
  982. Height = Dim.Auto (DimAutoStyle.Text)
  983. };
  984. view.BeginInit ();
  985. view.EndInit ();
  986. Assert.Equal (new (0, 0, 10, 1), view.Frame);
  987. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  988. Assert.Equal (new (10, 1), view.TextFormatter.ConstrainToSize);
  989. }
  990. [Fact]
  991. public void TextDirection_Vertical_Dims_Correct ()
  992. {
  993. // Initializes a view with a vertical direction
  994. var view = new View
  995. {
  996. TextDirection = TextDirection.TopBottom_LeftRight,
  997. Text = "01234",
  998. Width = Dim.Auto (DimAutoStyle.Text),
  999. Height = Dim.Auto (DimAutoStyle.Text)
  1000. };
  1001. view.Layout ();
  1002. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1003. Assert.Equal (new (0, 0, 1, 5), view.Viewport);
  1004. }
  1005. [Fact]
  1006. [SetupFakeDriver]
  1007. public void Narrow_Wide_Runes ()
  1008. {
  1009. ((FakeDriver)Application.Driver!).SetBufferSize (32, 32);
  1010. var top = new View { Width = 32, Height = 32 };
  1011. var text = $"First line{Environment.NewLine}Second line";
  1012. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  1013. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1014. horizontalView.TextFormatter.ConstrainToSize = new (20, 1);
  1015. var verticalView = new View
  1016. {
  1017. Y = 3,
  1018. Height = 20,
  1019. Width = 1,
  1020. Text = text,
  1021. TextDirection = TextDirection.TopBottom_LeftRight
  1022. };
  1023. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1024. verticalView.TextFormatter.ConstrainToSize = new (1, 20);
  1025. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  1026. frame.Add (horizontalView, verticalView);
  1027. top.Add (frame);
  1028. top.BeginInit ();
  1029. top.EndInit ();
  1030. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  1031. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  1032. top.Draw ();
  1033. var expected = @"
  1034. ┌──────────────────────────────┐
  1035. │First line Second li │
  1036. │ │
  1037. │ │
  1038. │F │
  1039. │i │
  1040. │r │
  1041. │s │
  1042. │t │
  1043. │ │
  1044. │l │
  1045. │i │
  1046. │n │
  1047. │e │
  1048. │ │
  1049. │S │
  1050. │e │
  1051. │c │
  1052. │o │
  1053. │n │
  1054. │d │
  1055. │ │
  1056. │l │
  1057. │i │
  1058. │ │
  1059. │ │
  1060. │ │
  1061. │ │
  1062. │ │
  1063. │ │
  1064. │ │
  1065. └──────────────────────────────┘
  1066. ";
  1067. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1068. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  1069. Assert.True (verticalView.TextFormatter.NeedsFormat);
  1070. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1071. // We know these glpyhs are 2 cols wide, so we need to widen the view
  1072. verticalView.Width = 2;
  1073. verticalView.TextFormatter.ConstrainToSize = new (2, 20);
  1074. Assert.True (verticalView.TextFormatter.NeedsFormat);
  1075. View.SetClipToScreen ();
  1076. top.Draw ();
  1077. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  1078. expected = @"
  1079. ┌──────────────────────────────┐
  1080. │First line Second li │
  1081. │ │
  1082. │ │
  1083. │最 │
  1084. │初 │
  1085. │の │
  1086. │行 │
  1087. │ │
  1088. │二 │
  1089. │行 │
  1090. │目 │
  1091. │ │
  1092. │ │
  1093. │ │
  1094. │ │
  1095. │ │
  1096. │ │
  1097. │ │
  1098. │ │
  1099. │ │
  1100. │ │
  1101. │ │
  1102. │ │
  1103. │ │
  1104. │ │
  1105. │ │
  1106. │ │
  1107. │ │
  1108. │ │
  1109. │ │
  1110. └──────────────────────────────┘
  1111. ";
  1112. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1113. }
  1114. [Fact]
  1115. [SetupFakeDriver]
  1116. public void SetText_RendersCorrectly ()
  1117. {
  1118. View view;
  1119. var text = "test";
  1120. view = new Label { Text = text };
  1121. view.BeginInit ();
  1122. view.EndInit ();
  1123. view.Draw ();
  1124. TestHelpers.AssertDriverContentsWithFrameAre (text, output);
  1125. }
  1126. }