TextTests.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. [SetupFakeDriver]
  393. public void DimAuto_Vertical_TextDirection_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. view.SetRelativeLayout (new Size (4, 10));
  404. Assert.Equal (5, text.Length);
  405. // Vertical text - 2 wide, 5 down
  406. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  407. Assert.Equal (new (2, 5), view.TextFormatter.Size);
  408. Assert.Equal (new () { "界View" }, view.TextFormatter.GetLines ());
  409. view.Draw ();
  410. var expected = @"
  411. V
  412. i
  413. e
  414. w ";
  415. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  416. }
  417. [Fact]
  418. [AutoInitShutdown]
  419. public void AutoSize_True_Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  420. {
  421. var text = $"0123456789{Environment.NewLine}01234567891";
  422. var horizontalView = new View
  423. {
  424. Width = Dim.Auto (),
  425. Height = Dim.Auto (),
  426. Text = text
  427. };
  428. var verticalView = new View
  429. {
  430. Width = Dim.Auto (),
  431. Height = Dim.Auto (),
  432. Y = 3,
  433. //Height = 11,
  434. //Width = 2,
  435. Text = text,
  436. TextDirection = TextDirection.TopBottom_LeftRight
  437. };
  438. var win = new Window
  439. {
  440. Width = Dim.Fill (),
  441. Height = Dim.Fill (),
  442. Text = "Window"
  443. };
  444. win.Add (horizontalView, verticalView);
  445. var top = new Toplevel ();
  446. top.Add (win);
  447. RunState rs = Application.Begin (top);
  448. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  449. Assert.Equal (new (0, 0, 11, 2), horizontalView.Frame);
  450. Assert.Equal (new (0, 3, 2, 11), verticalView.Frame);
  451. var expected = @"
  452. ┌──────────────────┐
  453. │0123456789 │
  454. │01234567891 │
  455. │ │
  456. │00 │
  457. │11 │
  458. │22 │
  459. │33 │
  460. │44 │
  461. │55 │
  462. │66 │
  463. │77 │
  464. │88 │
  465. │99 │
  466. │ 1 │
  467. │ │
  468. │ │
  469. │ │
  470. │ │
  471. └──────────────────┘
  472. ";
  473. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  474. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  475. Application.Top.Draw ();
  476. Assert.Equal (new (0, 3, 4, 4), verticalView.Frame);
  477. expected = @"
  478. ┌──────────────────┐
  479. │0123456789 │
  480. │01234567891 │
  481. │ │
  482. │最二 │
  483. │初行 │
  484. │の目 │
  485. │行 │
  486. │ │
  487. │ │
  488. │ │
  489. │ │
  490. │ │
  491. │ │
  492. │ │
  493. │ │
  494. │ │
  495. │ │
  496. │ │
  497. └──────────────────┘
  498. ";
  499. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  500. Application.End (rs);
  501. top.Dispose ();
  502. }
  503. [Fact]
  504. [AutoInitShutdown]
  505. public void AutoSize_True_Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  506. {
  507. var text = "Finish 終";
  508. var horizontalView = new View
  509. {
  510. Id = "horizontalView",
  511. Width = Dim.Auto (), Height = Dim.Auto (), Text = text
  512. };
  513. var verticalView = new View
  514. {
  515. Id = "verticalView",
  516. Y = 3,
  517. Width = Dim.Auto (),
  518. Height = Dim.Auto (),
  519. Text = text,
  520. TextDirection = TextDirection.TopBottom_LeftRight
  521. };
  522. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  523. win.Add (horizontalView, verticalView);
  524. var top = new Toplevel ();
  525. top.Add (win);
  526. RunState rs = Application.Begin (top);
  527. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  528. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.Size);
  529. Assert.Equal (new (2, 8), verticalView.TextFormatter.Size);
  530. //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  531. //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  532. var expected = @"
  533. ┌────────────────────┐
  534. │Finish 終 │
  535. │ │
  536. │ │
  537. │F │
  538. │i │
  539. │n │
  540. │i │
  541. │s │
  542. │h │
  543. │ │
  544. │終 │
  545. │ │
  546. │ │
  547. │ │
  548. │ │
  549. │ │
  550. │ │
  551. │ │
  552. │ │
  553. │ │
  554. └────────────────────┘
  555. ";
  556. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  557. verticalView.Text = "最初の行二行目";
  558. Application.Top.Draw ();
  559. // height was initialized with 8 and can only grow or keep initial value
  560. Assert.Equal (new (0, 3, 2, 7), verticalView.Frame);
  561. expected = @"
  562. ┌────────────────────┐
  563. │Finish 終 │
  564. │ │
  565. │ │
  566. │最 │
  567. │初 │
  568. │の │
  569. │行 │
  570. │二 │
  571. │行 │
  572. │目 │
  573. │ │
  574. │ │
  575. │ │
  576. │ │
  577. │ │
  578. │ │
  579. │ │
  580. │ │
  581. │ │
  582. │ │
  583. └────────────────────┘
  584. ";
  585. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  586. Application.End (rs);
  587. top.Dispose ();
  588. }
  589. [Fact]
  590. [AutoInitShutdown]
  591. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  592. {
  593. var lbl = new Label { Text = "123" };
  594. var top = new Toplevel ();
  595. top.Add (lbl);
  596. RunState rs = Application.Begin (top);
  597. Assert.Equal ("123 ", GetContents ());
  598. lbl.Text = "12";
  599. Assert.Equal (new (0, 0, 2, 1), lbl.Frame);
  600. Assert.Equal (new (0, 0, 3, 1), lbl._needsDisplayRect);
  601. Assert.Equal (new (0, 0, 0, 0), lbl.SuperView._needsDisplayRect);
  602. Assert.True (lbl.SuperView.LayoutNeeded);
  603. lbl.SuperView.Draw ();
  604. Assert.Equal ("12 ", GetContents ());
  605. string GetContents ()
  606. {
  607. var text = "";
  608. for (var i = 0; i < 4; i++)
  609. {
  610. text += Application.Driver.Contents [0, i].Rune;
  611. }
  612. return text;
  613. }
  614. Application.End (rs);
  615. top.Dispose ();
  616. }
  617. [Theory]
  618. [AutoInitShutdown]
  619. [InlineData (true)]
  620. [InlineData (false)]
  621. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  622. {
  623. var text = "Hello World";
  624. var width = 20;
  625. var lblLeft = new View
  626. {
  627. Text = text,
  628. Width = width,
  629. Height = 1
  630. };
  631. if (autoSize)
  632. {
  633. lblLeft.Width = Dim.Auto ();
  634. lblLeft.Height = Dim.Auto ();
  635. }
  636. var lblCenter = new View
  637. {
  638. Text = text,
  639. Y = 1,
  640. Width = width,
  641. Height = 1,
  642. TextAlignment = Alignment.Center
  643. };
  644. if (autoSize)
  645. {
  646. lblCenter.Width = Dim.Auto ();
  647. lblCenter.Height = Dim.Auto ();
  648. }
  649. var lblRight = new View
  650. {
  651. Text = text,
  652. Y = 2,
  653. Width = width,
  654. Height = 1,
  655. TextAlignment = Alignment.End
  656. };
  657. if (autoSize)
  658. {
  659. lblRight.Width = Dim.Auto ();
  660. lblRight.Height = Dim.Auto ();
  661. }
  662. var lblJust = new View
  663. {
  664. Text = text,
  665. Y = 3,
  666. Width = width,
  667. Height = 1,
  668. TextAlignment = Alignment.Fill
  669. };
  670. if (autoSize)
  671. {
  672. lblJust.Width = Dim.Auto ();
  673. lblJust.Height = Dim.Auto ();
  674. }
  675. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  676. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  677. var top = new Toplevel ();
  678. top.Add (frame);
  679. Application.Begin (top);
  680. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  681. // frame.Width is width + border wide (20 + 2) and 6 high
  682. if (autoSize)
  683. {
  684. Size expectedSize = new (11, 1);
  685. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  686. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  687. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  688. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  689. }
  690. else
  691. {
  692. Size expectedSize = new (width, 1);
  693. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  694. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  695. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  696. Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  697. }
  698. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  699. string expected;
  700. if (autoSize)
  701. {
  702. expected = @"
  703. ┌────────────────────┐
  704. │Hello World │
  705. │Hello World │
  706. │Hello World │
  707. │Hello World │
  708. └────────────────────┘
  709. ";
  710. }
  711. else
  712. {
  713. expected = @"
  714. ┌────────────────────┐
  715. │Hello World │
  716. │ Hello World │
  717. │ Hello World│
  718. │Hello World│
  719. └────────────────────┘
  720. ";
  721. }
  722. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  723. Assert.Equal (new (0, 0, width + 2, 6), pos);
  724. top.Dispose ();
  725. }
  726. [Theory]
  727. [AutoInitShutdown]
  728. [InlineData (true)]
  729. [InlineData (false)]
  730. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  731. {
  732. var text = "Hello World";
  733. var height = 20;
  734. var lblLeft = new View
  735. {
  736. Text = text,
  737. Width = 1,
  738. Height = height,
  739. TextDirection = TextDirection.TopBottom_LeftRight
  740. };
  741. if (autoSize)
  742. {
  743. lblLeft.Width = Dim.Auto ();
  744. lblLeft.Height = Dim.Auto ();
  745. }
  746. var lblCenter = new View
  747. {
  748. Text = text,
  749. X = 2,
  750. Width = 1,
  751. Height = height,
  752. TextDirection = TextDirection.TopBottom_LeftRight,
  753. VerticalTextAlignment = Alignment.Center
  754. };
  755. if (autoSize)
  756. {
  757. lblCenter.Width = Dim.Auto ();
  758. lblCenter.Height = Dim.Auto ();
  759. }
  760. var lblRight = new View
  761. {
  762. Text = text,
  763. X = 4,
  764. Width = 1,
  765. Height = height,
  766. TextDirection = TextDirection.TopBottom_LeftRight,
  767. VerticalTextAlignment = Alignment.End
  768. };
  769. if (autoSize)
  770. {
  771. lblRight.Width = Dim.Auto ();
  772. lblRight.Height = Dim.Auto ();
  773. }
  774. var lblJust = new View
  775. {
  776. Text = text,
  777. X = 6,
  778. Width = 1,
  779. Height = height,
  780. TextDirection = TextDirection.TopBottom_LeftRight,
  781. VerticalTextAlignment = Alignment.Fill
  782. };
  783. if (autoSize)
  784. {
  785. lblJust.Width = Dim.Auto ();
  786. lblJust.Height = Dim.Auto ();
  787. }
  788. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  789. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  790. var top = new Toplevel ();
  791. top.Add (frame);
  792. Application.Begin (top);
  793. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  794. if (autoSize)
  795. {
  796. Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);
  797. Assert.Equal (new (1, 11), lblCenter.TextFormatter.Size);
  798. Assert.Equal (new (1, 11), lblRight.TextFormatter.Size);
  799. Assert.Equal (new (1, 11), lblJust.TextFormatter.Size);
  800. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  801. }
  802. else
  803. {
  804. Assert.Equal (new (1, height), lblLeft.TextFormatter.Size);
  805. Assert.Equal (new (1, height), lblCenter.TextFormatter.Size);
  806. Assert.Equal (new (1, height), lblRight.TextFormatter.Size);
  807. Assert.Equal (new (1, height), lblJust.TextFormatter.Size);
  808. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  809. }
  810. string expected;
  811. if (autoSize)
  812. {
  813. expected = @"
  814. ┌───────┐
  815. │H H H H│
  816. │e e e e│
  817. │l l l l│
  818. │l l l l│
  819. │o o o o│
  820. │ │
  821. │W W W W│
  822. │o o o o│
  823. │r r r r│
  824. │l l l l│
  825. │d d d d│
  826. │ │
  827. │ │
  828. │ │
  829. │ │
  830. │ │
  831. │ │
  832. │ │
  833. │ │
  834. │ │
  835. └───────┘
  836. ";
  837. }
  838. else
  839. {
  840. expected = @"
  841. ┌───────┐
  842. │H H│
  843. │e e│
  844. │l l│
  845. │l l│
  846. │o H o│
  847. │ e │
  848. │W l │
  849. │o l │
  850. │r o │
  851. │l H │
  852. │d W e │
  853. │ o l │
  854. │ r l │
  855. │ l o │
  856. │ d │
  857. │ W W│
  858. │ o o│
  859. │ r r│
  860. │ l l│
  861. │ d d│
  862. └───────┘
  863. ";
  864. }
  865. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  866. Assert.Equal (new (0, 0, 9, height + 2), pos);
  867. top.Dispose ();
  868. }
  869. // Test that View.PreserveTrailingSpaces removes trailing spaces
  870. [Fact]
  871. public void PreserveTrailingSpaces_Removes_Trailing_Spaces ()
  872. {
  873. var view = new View { Text = "Hello World " };
  874. Assert.Equal ("Hello World ", view.TextFormatter.Text);
  875. view.TextFormatter.WordWrap = true;
  876. view.TextFormatter.Size = new (5, 3);
  877. view.PreserveTrailingSpaces = false;
  878. Assert.Equal ($"Hello{Environment.NewLine}World", view.TextFormatter.Format ());
  879. view.PreserveTrailingSpaces = true;
  880. Assert.Equal ($"Hello{Environment.NewLine} {Environment.NewLine}World", view.TextFormatter.Format ());
  881. }
  882. // View.PreserveTrailingSpaces Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  883. // or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  884. // If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  885. // <see cref = "Text" / > is formatted for display.The default is <see langword = "false" / >.
  886. [Fact]
  887. public void PreserveTrailingSpaces_Set_Get ()
  888. {
  889. var view = new View { Text = "Hello World" };
  890. Assert.False (view.PreserveTrailingSpaces);
  891. view.PreserveTrailingSpaces = true;
  892. Assert.True (view.PreserveTrailingSpaces);
  893. }
  894. // Setting TextFormatter DOES NOT update Text
  895. [Fact]
  896. public void SettingTextFormatterDoesNotUpdateText ()
  897. {
  898. var view = new View ();
  899. view.TextFormatter.Text = "Hello World";
  900. Assert.True (string.IsNullOrEmpty (view.Text));
  901. }
  902. // Setting Text updates TextFormatter
  903. [Fact]
  904. public void SettingTextUpdatesTextFormatter ()
  905. {
  906. var view = new View { Text = "Hello World" };
  907. Assert.Equal ("Hello World", view.Text);
  908. Assert.Equal ("Hello World", view.TextFormatter.Text);
  909. }
  910. // Setting Text does NOT set the HotKey
  911. [Fact]
  912. public void Text_Does_Not_Set_HotKey ()
  913. {
  914. var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
  915. Assert.NotEqual (Key.H, view.HotKey);
  916. }
  917. // Test that TextFormatter is init only
  918. [Fact]
  919. public void TextFormatterIsInitOnly ()
  920. {
  921. var view = new View ();
  922. // Use reflection to ensure the TextFormatter property is `init` only
  923. Assert.Contains (
  924. typeof (IsExternalInit),
  925. typeof (View).GetMethod ("set_TextFormatter")
  926. .ReturnParameter.GetRequiredCustomModifiers ());
  927. }
  928. // Test that the Text property is set correctly.
  929. [Fact]
  930. public void TextProperty ()
  931. {
  932. var view = new View { Text = "Hello World" };
  933. Assert.Equal ("Hello World", view.Text);
  934. }
  935. // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
  936. [Fact]
  937. public void UpdateTextFormatterText_Overridden ()
  938. {
  939. var view = new TestView { Text = "Hello World" };
  940. Assert.Equal ("Hello World", view.Text);
  941. Assert.Equal (">Hello World<", view.TextFormatter.Text);
  942. }
  943. private class TestView : View
  944. {
  945. protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
  946. }
  947. [Fact]
  948. public void TextDirection_Horizontal_Dims_Correct ()
  949. {
  950. // Initializes a view with a vertical direction
  951. var view = new View
  952. {
  953. Text = "01234",
  954. TextDirection = TextDirection.LeftRight_TopBottom,
  955. Width = Dim.Auto (DimAutoStyle.Text),
  956. Height = Dim.Auto (DimAutoStyle.Text)
  957. };
  958. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  959. Assert.Equal (new (0, 0, 5, 1), view.Viewport);
  960. view.BeginInit ();
  961. view.EndInit ();
  962. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  963. Assert.Equal (new (0, 0, 5, 1), view.Viewport);
  964. }
  965. // BUGBUG: this is a temporary test that helped identify #3469 - It needs to be expanded upon (and renamed)
  966. [Fact]
  967. public void TextDirection_Horizontal_Dims_Correct_WidthAbsolute ()
  968. {
  969. var view = new View
  970. {
  971. Text = "01234",
  972. TextDirection = TextDirection.LeftRight_TopBottom,
  973. TextAlignment = Alignment.Center,
  974. Width = 10,
  975. Height = Dim.Auto (DimAutoStyle.Text)
  976. };
  977. view.BeginInit ();
  978. view.EndInit ();
  979. Assert.Equal (new (0, 0, 10, 1), view.Frame);
  980. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  981. Assert.Equal (new (10, 1), view.TextFormatter.Size);
  982. }
  983. [Fact]
  984. public void TextDirection_Vertical_Dims_Correct ()
  985. {
  986. // Initializes a view with a vertical direction
  987. var view = new View
  988. {
  989. TextDirection = TextDirection.TopBottom_LeftRight,
  990. Text = "01234",
  991. Width = Dim.Auto (DimAutoStyle.Text),
  992. Height = Dim.Auto (DimAutoStyle.Text)
  993. };
  994. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  995. Assert.Equal (new (0, 0, 1, 5), view.Viewport);
  996. view.BeginInit ();
  997. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  998. view.EndInit ();
  999. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  1000. Assert.Equal (new (0, 0, 1, 5), view.Viewport);
  1001. }
  1002. [Fact]
  1003. [SetupFakeDriver]
  1004. public void Narrow_Wide_Runes ()
  1005. {
  1006. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  1007. var top = new View { Width = 32, Height = 32 };
  1008. var text = $"First line{Environment.NewLine}Second line";
  1009. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  1010. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1011. horizontalView.TextFormatter.Size = new (20, 1);
  1012. var verticalView = new View
  1013. {
  1014. Y = 3,
  1015. Height = 20,
  1016. Width = 1,
  1017. Text = text,
  1018. TextDirection = TextDirection.TopBottom_LeftRight
  1019. };
  1020. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1021. verticalView.TextFormatter.Size = new (1, 20);
  1022. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  1023. frame.Add (horizontalView, verticalView);
  1024. top.Add (frame);
  1025. top.BeginInit ();
  1026. top.EndInit ();
  1027. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  1028. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  1029. top.Draw ();
  1030. var expected = @"
  1031. ┌──────────────────────────────┐
  1032. │First line Second li │
  1033. │ │
  1034. │ │
  1035. │F │
  1036. │i │
  1037. │r │
  1038. │s │
  1039. │t │
  1040. │ │
  1041. │l │
  1042. │i │
  1043. │n │
  1044. │e │
  1045. │ │
  1046. │S │
  1047. │e │
  1048. │c │
  1049. │o │
  1050. │n │
  1051. │d │
  1052. │ │
  1053. │l │
  1054. │i │
  1055. │ │
  1056. │ │
  1057. │ │
  1058. │ │
  1059. │ │
  1060. │ │
  1061. │ │
  1062. └──────────────────────────────┘
  1063. ";
  1064. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1065. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  1066. Assert.True (verticalView.TextFormatter.NeedsFormat);
  1067. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1068. // We know these glpyhs are 2 cols wide, so we need to widen the view
  1069. verticalView.Width = 2;
  1070. verticalView.TextFormatter.Size = new (2, 20);
  1071. Assert.True (verticalView.TextFormatter.NeedsFormat);
  1072. top.Draw ();
  1073. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  1074. expected = @"
  1075. ┌──────────────────────────────┐
  1076. │First line Second li │
  1077. │ │
  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. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1109. }
  1110. // Test behavior of AutoSize property.
  1111. // - Default is false
  1112. // - Setting to true invalidates Height/Width
  1113. // - Setting to false invalidates Height/Width
  1114. }