TextTests.cs 40 KB

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