2
0

TextTests.cs 37 KB

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