TextTests.cs 31 KB

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