TextTests.cs 31 KB

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