TextTests.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. using System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.ViewBaseTests;
  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 Runnable ();
  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 Runnable ();
  92. top.Add (win);
  93. SessionToken rs = Application.Begin (top);
  94. Application.Driver!.SetScreenSize (15, 15);
  95. Application.LayoutAndDraw ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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. AutoInitShutdownAttribute.RunIteration ();
  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 Runnable ();
  323. top.Add (win);
  324. SessionToken rs = Application.Begin (top);
  325. Application.Driver!.SetScreenSize (4, 10);
  326. Application.LayoutAndDraw ();
  327. Assert.Equal (5, text.Length);
  328. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  329. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  330. Assert.Equal (new () { "Views" }, view.TextFormatter.GetLines ());
  331. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  332. Assert.Equal (new (0, 0, 4, 10), Application.TopRunnableView.Frame);
  333. var expected = @"
  334. ┌──┐
  335. │V │
  336. │i │
  337. │e │
  338. │w │
  339. │s │
  340. │ │
  341. │ │
  342. │ │
  343. └──┘
  344. ";
  345. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  346. Assert.Equal (new (0, 0, 4, 10), pos);
  347. text = "0123456789";
  348. Assert.Equal (10, text.Length);
  349. //view.Height = Dim.Fill () - text.Length;
  350. AutoInitShutdownAttribute.RunIteration ();
  351. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  352. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  353. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  354. Assert.Null (exception);
  355. expected = @"
  356. ┌──┐
  357. │V │
  358. │i │
  359. │e │
  360. │w │
  361. │s │
  362. │ │
  363. │ │
  364. │ │
  365. └──┘
  366. ";
  367. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  368. Assert.Equal (new (0, 0, 4, 10), pos);
  369. top.Dispose ();
  370. }
  371. [Fact]
  372. [SetupFakeApplication]
  373. public void DimAuto_Vertical_TextDirection_Wide_Rune ()
  374. {
  375. var text = "界View";
  376. var view = new View
  377. {
  378. App = ApplicationImpl.Instance,
  379. TextDirection = TextDirection.TopBottom_LeftRight,
  380. Text = text,
  381. Width = Dim.Auto (),
  382. Height = Dim.Auto ()
  383. };
  384. view.SetRelativeLayout (new (4, 10));
  385. Assert.Equal (5, text.Length);
  386. // Vertical text - 2 wide, 5 down
  387. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  388. Assert.Equal (new (2, 5), view.TextFormatter.ConstrainToSize);
  389. Assert.Equal (new () { "界View" }, view.TextFormatter.GetLines ());
  390. view.Draw ();
  391. var expected = @"
  392. V
  393. i
  394. e
  395. w ";
  396. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  397. }
  398. [Fact]
  399. [AutoInitShutdown]
  400. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  401. {
  402. var text = $"0123456789{Environment.NewLine}01234567891";
  403. var horizontalView = new View
  404. {
  405. Width = Dim.Auto (),
  406. Height = Dim.Auto (),
  407. Text = text
  408. };
  409. var verticalView = new View
  410. {
  411. Width = Dim.Auto (),
  412. Height = Dim.Auto (),
  413. Y = 3,
  414. //Height = 11,
  415. //Width = 2,
  416. Text = text,
  417. TextDirection = TextDirection.TopBottom_LeftRight
  418. };
  419. var win = new Window
  420. {
  421. Width = Dim.Fill (),
  422. Height = Dim.Fill (),
  423. Text = "Window"
  424. };
  425. win.Add (horizontalView, verticalView);
  426. var top = new Runnable ();
  427. top.Add (win);
  428. SessionToken rs = Application.Begin (top);
  429. Application.Driver!.SetScreenSize (20, 20);
  430. Application.LayoutAndDraw ();
  431. Assert.Equal (new (0, 0, 11, 2), horizontalView.Frame);
  432. Assert.Equal (new (0, 3, 2, 11), verticalView.Frame);
  433. var expected = @"
  434. ┌──────────────────┐
  435. │0123456789 │
  436. │01234567891 │
  437. │ │
  438. │00 │
  439. │11 │
  440. │22 │
  441. │33 │
  442. │44 │
  443. │55 │
  444. │66 │
  445. │77 │
  446. │88 │
  447. │99 │
  448. │ 1 │
  449. │ │
  450. │ │
  451. │ │
  452. │ │
  453. └──────────────────┘
  454. ";
  455. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  456. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  457. AutoInitShutdownAttribute.RunIteration ();
  458. Assert.Equal (new (0, 3, 4, 4), verticalView.Frame);
  459. expected = @"
  460. ┌──────────────────┐
  461. │0123456789 │
  462. │01234567891 │
  463. │ │
  464. │最二 │
  465. │初行 │
  466. │の目 │
  467. │行 │
  468. │ │
  469. │ │
  470. │ │
  471. │ │
  472. │ │
  473. │ │
  474. │ │
  475. │ │
  476. │ │
  477. │ │
  478. │ │
  479. └──────────────────┘
  480. ";
  481. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  482. Application.End (rs);
  483. top.Dispose ();
  484. }
  485. [Fact]
  486. [AutoInitShutdown]
  487. public void Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  488. {
  489. var text = "Finish 終";
  490. var horizontalView = new View
  491. {
  492. Id = "horizontalView",
  493. Width = Dim.Auto (), Height = Dim.Auto (), Text = text
  494. };
  495. var verticalView = new View
  496. {
  497. Id = "verticalView",
  498. Y = 3,
  499. Width = Dim.Auto (),
  500. Height = Dim.Auto (),
  501. Text = text,
  502. TextDirection = TextDirection.TopBottom_LeftRight
  503. };
  504. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  505. win.Add (horizontalView, verticalView);
  506. var top = new Runnable ();
  507. top.Add (win);
  508. SessionToken rs = Application.Begin (top);
  509. Application.Driver!.SetScreenSize (22, 22);
  510. Application.LayoutAndDraw ();
  511. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.ConstrainToSize);
  512. Assert.Equal (new (2, 8), verticalView.TextFormatter.ConstrainToSize);
  513. //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  514. //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  515. var expected = @"
  516. ┌────────────────────┐
  517. │Finish 終 │
  518. │ │
  519. │ │
  520. │F │
  521. │i │
  522. │n │
  523. │i │
  524. │s │
  525. │h │
  526. │ │
  527. │終 │
  528. │ │
  529. │ │
  530. │ │
  531. │ │
  532. │ │
  533. │ │
  534. │ │
  535. │ │
  536. │ │
  537. └────────────────────┘
  538. ";
  539. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  540. verticalView.Text = "最初の行二行目";
  541. AutoInitShutdownAttribute.RunIteration ();
  542. // height was initialized with 8 and can only grow or keep initial value
  543. Assert.Equal (new (0, 3, 2, 7), verticalView.Frame);
  544. expected = @"
  545. ┌────────────────────┐
  546. │Finish 終 │
  547. │ │
  548. │ │
  549. │最 │
  550. │初 │
  551. │の │
  552. │行 │
  553. │二 │
  554. │行 │
  555. │目 │
  556. │ │
  557. │ │
  558. │ │
  559. │ │
  560. │ │
  561. │ │
  562. │ │
  563. │ │
  564. │ │
  565. │ │
  566. └────────────────────┘
  567. ";
  568. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  569. Application.End (rs);
  570. top.Dispose ();
  571. }
  572. [Fact]
  573. [AutoInitShutdown]
  574. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  575. {
  576. var lbl = new Label { Text = "123" };
  577. var top = new Runnable ();
  578. top.Add (lbl);
  579. SessionToken rs = Application.Begin (top);
  580. AutoInitShutdownAttribute.RunIteration ();
  581. Assert.Equal (new (0, 0, 3, 1), lbl.Frame);
  582. Assert.Equal ("123 ", GetContents ());
  583. lbl.Text = "12";
  584. lbl.Layout ();
  585. Assert.Equal (new (0, 0, 2, 1), lbl.Frame);
  586. Assert.Equal (new (0, 0, 2, 1), lbl.NeedsDrawRect);
  587. Assert.Equal (new (0, 0, 80, 25), lbl.SuperView.NeedsDrawRect);
  588. Assert.True (lbl.SuperView.NeedsLayout);
  589. AutoInitShutdownAttribute.RunIteration ();
  590. Assert.Equal ("12 ", GetContents ());
  591. string GetContents ()
  592. {
  593. var sb = new StringBuilder ();
  594. for (var i = 0; i < 4; i++)
  595. {
  596. sb.Append (Application.Driver?.Contents! [0, i].Grapheme);
  597. }
  598. return sb.ToString ();
  599. }
  600. Application.End (rs);
  601. top.Dispose ();
  602. }
  603. [Theory]
  604. [AutoInitShutdown]
  605. [InlineData (true)]
  606. [InlineData (false)]
  607. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  608. {
  609. var text = "Hello World";
  610. var width = 20;
  611. var lblLeft = new View
  612. {
  613. Text = text,
  614. Width = width,
  615. Height = 1
  616. };
  617. if (autoSize)
  618. {
  619. lblLeft.Width = Dim.Auto ();
  620. lblLeft.Height = Dim.Auto ();
  621. }
  622. var lblCenter = new View
  623. {
  624. Text = text,
  625. Y = 1,
  626. Width = width,
  627. Height = 1,
  628. TextAlignment = Alignment.Center
  629. };
  630. if (autoSize)
  631. {
  632. lblCenter.Width = Dim.Auto ();
  633. lblCenter.Height = Dim.Auto ();
  634. }
  635. var lblRight = new View
  636. {
  637. Text = text,
  638. Y = 2,
  639. Width = width,
  640. Height = 1,
  641. TextAlignment = Alignment.End
  642. };
  643. if (autoSize)
  644. {
  645. lblRight.Width = Dim.Auto ();
  646. lblRight.Height = Dim.Auto ();
  647. }
  648. var lblJust = new View
  649. {
  650. Text = text,
  651. Y = 3,
  652. Width = width,
  653. Height = 1,
  654. TextAlignment = Alignment.Fill
  655. };
  656. if (autoSize)
  657. {
  658. lblJust.Width = Dim.Auto ();
  659. lblJust.Height = Dim.Auto ();
  660. }
  661. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single };
  662. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  663. var top = new Runnable ();
  664. top.Add (frame);
  665. Application.Begin (top);
  666. Application.Driver!.SetScreenSize (width + 2, 6);
  667. Application.LayoutAndDraw ();
  668. // frame.Width is width + border wide (20 + 2) and 6 high
  669. if (autoSize)
  670. {
  671. Size expectedSize = new (11, 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. else
  678. {
  679. Size expectedSize = new (width, 1);
  680. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  681. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  682. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  683. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  684. }
  685. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  686. string expected;
  687. if (autoSize)
  688. {
  689. expected = @"
  690. ┌────────────────────┐
  691. │Hello World │
  692. │Hello World │
  693. │Hello World │
  694. │Hello World │
  695. └────────────────────┘
  696. ";
  697. }
  698. else
  699. {
  700. expected = @"
  701. ┌────────────────────┐
  702. │Hello World │
  703. │ Hello World │
  704. │ Hello World│
  705. │Hello World│
  706. └────────────────────┘
  707. ";
  708. }
  709. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  710. Assert.Equal (new (0, 0, width + 2, 6), pos);
  711. top.Dispose ();
  712. }
  713. [Theory]
  714. [AutoInitShutdown]
  715. [InlineData (true)]
  716. [InlineData (false)]
  717. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  718. {
  719. var text = "Hello World";
  720. var height = 20;
  721. var lblLeft = new View
  722. {
  723. Text = text,
  724. Width = 1,
  725. Height = height,
  726. TextDirection = TextDirection.TopBottom_LeftRight
  727. };
  728. if (autoSize)
  729. {
  730. lblLeft.Width = Dim.Auto ();
  731. lblLeft.Height = Dim.Auto ();
  732. }
  733. var lblCenter = new View
  734. {
  735. Text = text,
  736. X = 2,
  737. Width = 1,
  738. Height = height,
  739. TextDirection = TextDirection.TopBottom_LeftRight,
  740. VerticalTextAlignment = Alignment.Center
  741. };
  742. if (autoSize)
  743. {
  744. lblCenter.Width = Dim.Auto ();
  745. lblCenter.Height = Dim.Auto ();
  746. }
  747. var lblRight = new View
  748. {
  749. Text = text,
  750. X = 4,
  751. Width = 1,
  752. Height = height,
  753. TextDirection = TextDirection.TopBottom_LeftRight,
  754. VerticalTextAlignment = Alignment.End
  755. };
  756. if (autoSize)
  757. {
  758. lblRight.Width = Dim.Auto ();
  759. lblRight.Height = Dim.Auto ();
  760. }
  761. var lblJust = new View
  762. {
  763. Text = text,
  764. X = 6,
  765. Width = 1,
  766. Height = height,
  767. TextDirection = TextDirection.TopBottom_LeftRight,
  768. VerticalTextAlignment = Alignment.Fill
  769. };
  770. if (autoSize)
  771. {
  772. lblJust.Width = Dim.Auto ();
  773. lblJust.Height = Dim.Auto ();
  774. }
  775. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single };
  776. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  777. var top = new Runnable ();
  778. top.Add (frame);
  779. Application.Begin (top);
  780. Application.Driver!.SetScreenSize (9, height + 2);
  781. Application.LayoutAndDraw ();
  782. if (autoSize)
  783. {
  784. Assert.Equal (new (1, 11), lblLeft.TextFormatter.ConstrainToSize);
  785. Assert.Equal (new (1, 11), lblCenter.TextFormatter.ConstrainToSize);
  786. Assert.Equal (new (1, 11), lblRight.TextFormatter.ConstrainToSize);
  787. Assert.Equal (new (1, 11), lblJust.TextFormatter.ConstrainToSize);
  788. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  789. }
  790. else
  791. {
  792. Assert.Equal (new (1, height), lblLeft.TextFormatter.ConstrainToSize);
  793. Assert.Equal (new (1, height), lblCenter.TextFormatter.ConstrainToSize);
  794. Assert.Equal (new (1, height), lblRight.TextFormatter.ConstrainToSize);
  795. Assert.Equal (new (1, height), lblJust.TextFormatter.ConstrainToSize);
  796. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  797. }
  798. string expected;
  799. if (autoSize)
  800. {
  801. expected = @"
  802. ┌───────┐
  803. │H H H H│
  804. │e e e e│
  805. │l l l l│
  806. │l l l l│
  807. │o o o o│
  808. │ │
  809. │W W W W│
  810. │o o o o│
  811. │r r r r│
  812. │l l l l│
  813. │d d d d│
  814. │ │
  815. │ │
  816. │ │
  817. │ │
  818. │ │
  819. │ │
  820. │ │
  821. │ │
  822. │ │
  823. └───────┘
  824. ";
  825. }
  826. else
  827. {
  828. expected = @"
  829. ┌───────┐
  830. │H H│
  831. │e e│
  832. │l l│
  833. │l l│
  834. │o H o│
  835. │ e │
  836. │W l │
  837. │o l │
  838. │r o │
  839. │l H │
  840. │d W e │
  841. │ o l │
  842. │ r l │
  843. │ l o │
  844. │ d │
  845. │ W W│
  846. │ o o│
  847. │ r r│
  848. │ l l│
  849. │ d d│
  850. └───────┘
  851. ";
  852. }
  853. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  854. Assert.Equal (new (0, 0, 9, height + 2), pos);
  855. top.Dispose ();
  856. }
  857. [Fact]
  858. [SetupFakeApplication]
  859. public void Narrow_Wide_Runes ()
  860. {
  861. Application.Driver!.SetScreenSize (32, 32);
  862. var top = new View { Width = 32, Height = 32 };
  863. top.App = ApplicationImpl.Instance;
  864. var text = $"First line{Environment.NewLine}Second line";
  865. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  866. // Autosize is off, so we have to explicitly set TextFormatter.Size
  867. horizontalView.TextFormatter.ConstrainToSize = new (20, 1);
  868. var verticalView = new View
  869. {
  870. Y = 3,
  871. Height = 20,
  872. Width = 1,
  873. Text = text,
  874. TextDirection = TextDirection.TopBottom_LeftRight
  875. };
  876. // Autosize is off, so we have to explicitly set TextFormatter.Size
  877. verticalView.TextFormatter.ConstrainToSize = new (1, 20);
  878. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window", BorderStyle = LineStyle.Single };
  879. frame.Add (horizontalView, verticalView);
  880. top.Add (frame);
  881. top.BeginInit ();
  882. top.EndInit ();
  883. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  884. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  885. top.Draw ();
  886. var expected = @"
  887. ┌──────────────────────────────┐
  888. │First line Second li │
  889. │ │
  890. │ │
  891. │F │
  892. │i │
  893. │r │
  894. │s │
  895. │t │
  896. │ │
  897. │l │
  898. │i │
  899. │n │
  900. │e │
  901. │ │
  902. │S │
  903. │e │
  904. │c │
  905. │o │
  906. │n │
  907. │d │
  908. │ │
  909. │l │
  910. │i │
  911. │ │
  912. │ │
  913. │ │
  914. │ │
  915. │ │
  916. │ │
  917. │ │
  918. └──────────────────────────────┘
  919. ";
  920. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  921. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  922. Assert.True (verticalView.TextFormatter.NeedsFormat);
  923. // Autosize is off, so we have to explicitly set TextFormatter.Size
  924. // We know these glpyhs are 2 cols wide, so we need to widen the view
  925. verticalView.Width = 2;
  926. verticalView.TextFormatter.ConstrainToSize = new (2, 20);
  927. Assert.True (verticalView.TextFormatter.NeedsFormat);
  928. top.SetClipToScreen ();
  929. top.Draw ();
  930. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  931. expected = @"
  932. ┌──────────────────────────────┐
  933. │First line Second li │
  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. │ │
  960. │ │
  961. │ │
  962. │ │
  963. └──────────────────────────────┘
  964. ";
  965. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  966. }
  967. [Fact]
  968. [SetupFakeApplication]
  969. public void SetText_RendersCorrectly ()
  970. {
  971. View view;
  972. var text = "test";
  973. view = new Label { App = ApplicationImpl.Instance, Text = text };
  974. view.BeginInit ();
  975. view.EndInit ();
  976. view.Draw ();
  977. DriverAssert.AssertDriverContentsWithFrameAre (text, output);
  978. }
  979. }