TextTests.cs 31 KB

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