TextTests.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. using System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.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. [Fact]
  11. [SetupFakeApplication]
  12. public void Setting_With_Height_Horizontal ()
  13. {
  14. var top = new View { Width = 25, Height = 25 };
  15. top.App = ApplicationImpl.Instance;
  16. var label = new Label { Text = "Hello", /* Width = 10, Height = 2, */ ValidatePosDim = true };
  17. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  18. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  19. top.Add (label, viewX, viewY);
  20. top.Layout ();
  21. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  22. top.LayoutSubViews ();
  23. top.Draw ();
  24. var expected = @"
  25. HelloX
  26. Y
  27. ";
  28. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  29. label.Width = 10;
  30. label.Height = 2;
  31. Assert.Equal (new (0, 0, 10, 2), label.Frame);
  32. top.LayoutSubViews ();
  33. top.SetClipToScreen ();
  34. top.Draw ();
  35. expected = @"
  36. Hello X
  37. Y
  38. ";
  39. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  40. }
  41. [Fact]
  42. [AutoInitShutdown]
  43. public void Setting_With_Height_Vertical ()
  44. {
  45. // BUGBUG: Label is Width = Dim.Auto (), Height = Dim.Auto (), so Width & Height are ignored
  46. var label = new Label
  47. { /*Width = 2, Height = 10, */
  48. TextDirection = TextDirection.TopBottom_LeftRight, ValidatePosDim = true
  49. };
  50. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  51. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  52. var top = new Toplevel ();
  53. top.Add (label, viewX, viewY);
  54. SessionToken rs = Application.Begin (top);
  55. label.Text = "Hello";
  56. AutoInitShutdownAttribute.RunIteration ();
  57. Assert.Equal (new (0, 0, 1, 5), label.Frame);
  58. var expected = @"
  59. HX
  60. e
  61. l
  62. l
  63. o
  64. Y
  65. ";
  66. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  67. label.Width = 2;
  68. label.Height = 10;
  69. AutoInitShutdownAttribute.RunIteration ();
  70. Assert.Equal (new (0, 0, 2, 10), label.Frame);
  71. expected = @"
  72. H X
  73. e
  74. l
  75. l
  76. o
  77. Y
  78. "
  79. ;
  80. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  81. Application.End (rs);
  82. top.Dispose ();
  83. }
  84. [Fact]
  85. [AutoInitShutdown]
  86. public void TextDirection_Toggle ()
  87. {
  88. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  89. var view = new View ();
  90. win.Add (view);
  91. var top = new Toplevel ();
  92. top.Add (win);
  93. SessionToken rs = Application.Begin (top);
  94. Application.Driver!.SetScreenSize (15, 15);
  95. Assert.Equal (new (0, 0, 15, 15), win.Frame);
  96. Assert.Equal (new (0, 0, 15, 15), win.Margin!.Frame);
  97. Assert.Equal (new (0, 0, 15, 15), win.Border!.Frame);
  98. Assert.Equal (new (1, 1, 13, 13), win.Padding.Frame);
  99. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  100. Assert.Equal (Rectangle.Empty, view.Frame);
  101. Assert.Equal ("Absolute(0)", view.X.ToString ());
  102. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  103. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  104. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  105. var expected = @"
  106. ┌─────────────┐
  107. │ │
  108. │ │
  109. │ │
  110. │ │
  111. │ │
  112. │ │
  113. │ │
  114. │ │
  115. │ │
  116. │ │
  117. │ │
  118. │ │
  119. │ │
  120. └─────────────┘
  121. ";
  122. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  123. view.Text = "Hello World";
  124. view.Width = 11;
  125. view.Height = 1;
  126. AutoInitShutdownAttribute.RunIteration ();
  127. Assert.Equal (new (0, 0, 11, 1), view.Frame);
  128. Assert.Equal ("Absolute(0)", view.X.ToString ());
  129. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  130. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  131. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  132. expected = @"
  133. ┌─────────────┐
  134. │Hello World │
  135. │ │
  136. │ │
  137. │ │
  138. │ │
  139. │ │
  140. │ │
  141. │ │
  142. │ │
  143. │ │
  144. │ │
  145. │ │
  146. │ │
  147. └─────────────┘
  148. ";
  149. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  150. view.Width = Dim.Auto ();
  151. view.Height = Dim.Auto ();
  152. view.Text = "Hello Worlds";
  153. AutoInitShutdownAttribute.RunIteration ();
  154. int len = "Hello Worlds".Length;
  155. Assert.Equal (12, len);
  156. Assert.Equal (new (0, 0, len, 1), view.Frame);
  157. expected = @"
  158. ┌─────────────┐
  159. │Hello Worlds │
  160. │ │
  161. │ │
  162. │ │
  163. │ │
  164. │ │
  165. │ │
  166. │ │
  167. │ │
  168. │ │
  169. │ │
  170. │ │
  171. │ │
  172. └─────────────┘
  173. ";
  174. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  175. view.TextDirection = TextDirection.TopBottom_LeftRight;
  176. AutoInitShutdownAttribute.RunIteration ();
  177. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  178. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  179. expected = @"
  180. ┌─────────────┐
  181. │H │
  182. │e │
  183. │l │
  184. │l │
  185. │o │
  186. │ │
  187. │W │
  188. │o │
  189. │r │
  190. │l │
  191. │d │
  192. │s │
  193. │ │
  194. └─────────────┘
  195. ";
  196. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  197. // Setting to false causes Width and Height to be set to the current ContentSize
  198. view.Width = 1;
  199. view.Height = 12;
  200. AutoInitShutdownAttribute.RunIteration ();
  201. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  202. view.Width = 12;
  203. view.Height = 1;
  204. view.TextFormatter.ConstrainToSize = new (12, 1);
  205. AutoInitShutdownAttribute.RunIteration ();
  206. Assert.Equal (new (12, 1), view.TextFormatter.ConstrainToSize);
  207. Assert.Equal (new (0, 0, 12, 1), view.Frame);
  208. top.ClearViewport ();
  209. view.SetNeedsDraw ();
  210. view.Draw ();
  211. expected = @" HelloWorlds";
  212. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  213. AutoInitShutdownAttribute.RunIteration ();
  214. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  215. // that the text will be spread "vertically" across 1 line.
  216. // Hence no space.
  217. expected = @"
  218. ┌─────────────┐
  219. │HelloWorlds │
  220. │ │
  221. │ │
  222. │ │
  223. │ │
  224. │ │
  225. │ │
  226. │ │
  227. │ │
  228. │ │
  229. │ │
  230. │ │
  231. │ │
  232. └─────────────┘
  233. ";
  234. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  235. view.PreserveTrailingSpaces = true;
  236. AutoInitShutdownAttribute.RunIteration ();
  237. Assert.Equal (new (0, 0, 12, 1), view.Frame);
  238. expected = @"
  239. ┌─────────────┐
  240. │Hello Worlds │
  241. │ │
  242. │ │
  243. │ │
  244. │ │
  245. │ │
  246. │ │
  247. │ │
  248. │ │
  249. │ │
  250. │ │
  251. │ │
  252. │ │
  253. └─────────────┘
  254. ";
  255. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  256. view.PreserveTrailingSpaces = false;
  257. Rectangle f = view.Frame;
  258. view.Width = f.Height;
  259. view.Height = f.Width;
  260. view.TextDirection = TextDirection.TopBottom_LeftRight;
  261. AutoInitShutdownAttribute.RunIteration ();
  262. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  263. expected = @"
  264. ┌─────────────┐
  265. │H │
  266. │e │
  267. │l │
  268. │l │
  269. │o │
  270. │ │
  271. │W │
  272. │o │
  273. │r │
  274. │l │
  275. │d │
  276. │s │
  277. │ │
  278. └─────────────┘
  279. ";
  280. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  281. view.Width = Dim.Auto ();
  282. view.Height = Dim.Auto ();
  283. AutoInitShutdownAttribute.RunIteration ();
  284. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  285. expected = @"
  286. ┌─────────────┐
  287. │H │
  288. │e │
  289. │l │
  290. │l │
  291. │o │
  292. │ │
  293. │W │
  294. │o │
  295. │r │
  296. │l │
  297. │d │
  298. │s │
  299. │ │
  300. └─────────────┘
  301. ";
  302. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  303. Application.End (rs);
  304. top.Dispose ();
  305. }
  306. [Fact]
  307. [AutoInitShutdown]
  308. public void View_IsEmpty_False_Minimum_Width ()
  309. {
  310. var text = "Views";
  311. var view = new View
  312. {
  313. TextDirection = TextDirection.TopBottom_LeftRight,
  314. Height = Dim.Fill () - text.Length,
  315. Text = text
  316. };
  317. view.Width = Dim.Auto ();
  318. view.Height = Dim.Auto ();
  319. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  320. win.Add (view);
  321. var top = new Toplevel ();
  322. top.Add (win);
  323. SessionToken rs = Application.Begin (top);
  324. Application.Driver!.SetScreenSize (4, 10);
  325. Assert.Equal (5, text.Length);
  326. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  327. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  328. Assert.Equal (new () { "Views" }, view.TextFormatter.GetLines ());
  329. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  330. Assert.Equal (new (0, 0, 4, 10), Application.Current.Frame);
  331. var expected = @"
  332. ┌──┐
  333. │V │
  334. │i │
  335. │e │
  336. │w │
  337. │s │
  338. │ │
  339. │ │
  340. │ │
  341. └──┘
  342. ";
  343. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  344. Assert.Equal (new (0, 0, 4, 10), pos);
  345. text = "0123456789";
  346. Assert.Equal (10, text.Length);
  347. //view.Height = Dim.Fill () - text.Length;
  348. AutoInitShutdownAttribute.RunIteration ();
  349. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  350. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  351. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  352. Assert.Null (exception);
  353. expected = @"
  354. ┌──┐
  355. │V │
  356. │i │
  357. │e │
  358. │w │
  359. │s │
  360. │ │
  361. │ │
  362. │ │
  363. └──┘
  364. ";
  365. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  366. Assert.Equal (new (0, 0, 4, 10), pos);
  367. top.Dispose ();
  368. }
  369. [Fact]
  370. [SetupFakeApplication]
  371. public void DimAuto_Vertical_TextDirection_Wide_Rune ()
  372. {
  373. var text = "界View";
  374. var view = new View
  375. {
  376. App = ApplicationImpl.Instance,
  377. TextDirection = TextDirection.TopBottom_LeftRight,
  378. Text = text,
  379. Width = Dim.Auto (),
  380. Height = Dim.Auto ()
  381. };
  382. view.SetRelativeLayout (new (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. SessionToken rs = Application.Begin (top);
  427. Application.Driver!.SetScreenSize (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. AutoInitShutdownAttribute.RunIteration ();
  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. SessionToken rs = Application.Begin (top);
  506. Application.Driver!.SetScreenSize (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. AutoInitShutdownAttribute.RunIteration ();
  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. SessionToken rs = Application.Begin (top);
  576. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  586. Assert.Equal ("12 ", GetContents ());
  587. string GetContents ()
  588. {
  589. var sb = new StringBuilder ();
  590. for (var i = 0; i < 4; i++)
  591. {
  592. sb.Append (Application.Driver?.Contents! [0, i].Grapheme);
  593. }
  594. return sb.ToString ();
  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 (), BorderStyle = LineStyle.Single };
  658. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  659. var top = new Toplevel ();
  660. top.Add (frame);
  661. Application.Begin (top);
  662. Application.Driver!.SetScreenSize (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 (), BorderStyle = LineStyle.Single };
  771. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  772. var top = new Toplevel ();
  773. top.Add (frame);
  774. Application.Begin (top);
  775. Application.Driver!.SetScreenSize (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. [SetupFakeApplication]
  853. public void Narrow_Wide_Runes ()
  854. {
  855. Application.Driver!.SetScreenSize (32, 32);
  856. var top = new View { Width = 32, Height = 32 };
  857. top.App = ApplicationImpl.Instance;
  858. var text = $"First line{Environment.NewLine}Second line";
  859. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  860. // Autosize is off, so we have to explicitly set TextFormatter.Size
  861. horizontalView.TextFormatter.ConstrainToSize = new (20, 1);
  862. var verticalView = new View
  863. {
  864. Y = 3,
  865. Height = 20,
  866. Width = 1,
  867. Text = text,
  868. TextDirection = TextDirection.TopBottom_LeftRight
  869. };
  870. // Autosize is off, so we have to explicitly set TextFormatter.Size
  871. verticalView.TextFormatter.ConstrainToSize = new (1, 20);
  872. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window", BorderStyle = LineStyle.Single };
  873. frame.Add (horizontalView, verticalView);
  874. top.Add (frame);
  875. top.BeginInit ();
  876. top.EndInit ();
  877. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  878. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  879. top.Draw ();
  880. var expected = @"
  881. ┌──────────────────────────────┐
  882. │First line Second li │
  883. │ │
  884. │ │
  885. │F │
  886. │i │
  887. │r │
  888. │s │
  889. │t │
  890. │ │
  891. │l │
  892. │i │
  893. │n │
  894. │e │
  895. │ │
  896. │S │
  897. │e │
  898. │c │
  899. │o │
  900. │n │
  901. │d │
  902. │ │
  903. │l │
  904. │i │
  905. │ │
  906. │ │
  907. │ │
  908. │ │
  909. │ │
  910. │ │
  911. │ │
  912. └──────────────────────────────┘
  913. ";
  914. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  915. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  916. Assert.True (verticalView.TextFormatter.NeedsFormat);
  917. // Autosize is off, so we have to explicitly set TextFormatter.Size
  918. // We know these glpyhs are 2 cols wide, so we need to widen the view
  919. verticalView.Width = 2;
  920. verticalView.TextFormatter.ConstrainToSize = new (2, 20);
  921. Assert.True (verticalView.TextFormatter.NeedsFormat);
  922. top.SetClipToScreen ();
  923. top.Draw ();
  924. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  925. expected = @"
  926. ┌──────────────────────────────┐
  927. │First line Second li │
  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. ";
  959. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  960. }
  961. [Fact]
  962. [SetupFakeApplication]
  963. public void SetText_RendersCorrectly ()
  964. {
  965. View view;
  966. var text = "test";
  967. view = new Label { App = ApplicationImpl.Instance, Text = text };
  968. view.BeginInit ();
  969. view.EndInit ();
  970. view.Draw ();
  971. DriverAssert.AssertDriverContentsWithFrameAre (text, output);
  972. }
  973. }