TextTests.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. if (autoSize)
  682. {
  683. Size expectedSize = new (11, 1);
  684. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  685. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  686. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  687. // Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  688. }
  689. else
  690. {
  691. Size expectedSize = new (width, 1);
  692. Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
  693. Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
  694. Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
  695. //Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
  696. }
  697. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  698. string expected;
  699. if (autoSize)
  700. {
  701. expected = @"
  702. ┌────────────────────┐
  703. │Hello World │
  704. │Hello World │
  705. │Hello World │
  706. │Hello World │
  707. └────────────────────┘
  708. ";
  709. }
  710. else
  711. {
  712. expected = @"
  713. ┌────────────────────┐
  714. │Hello World │
  715. │ Hello World │
  716. │ Hello World│
  717. │Hello World│
  718. └────────────────────┘
  719. ";
  720. }
  721. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  722. Assert.Equal (new (0, 0, width + 2, 6), pos);
  723. top.Dispose ();
  724. }
  725. [Theory]
  726. [AutoInitShutdown]
  727. [InlineData (true)]
  728. [InlineData (false)]
  729. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  730. {
  731. var text = "Hello World";
  732. var height = 20;
  733. var lblLeft = new View
  734. {
  735. Text = text,
  736. Width = 1,
  737. Height = height,
  738. TextDirection = TextDirection.TopBottom_LeftRight
  739. };
  740. if (autoSize)
  741. {
  742. lblLeft.Width = Dim.Auto ();
  743. lblLeft.Height = Dim.Auto ();
  744. }
  745. var lblCenter = new View
  746. {
  747. Text = text,
  748. X = 2,
  749. Width = 1,
  750. Height = height,
  751. TextDirection = TextDirection.TopBottom_LeftRight,
  752. VerticalTextAlignment = Alignment.Center
  753. };
  754. if (autoSize)
  755. {
  756. lblCenter.Width = Dim.Auto ();
  757. lblCenter.Height = Dim.Auto ();
  758. }
  759. var lblRight = new View
  760. {
  761. Text = text,
  762. X = 4,
  763. Width = 1,
  764. Height = height,
  765. TextDirection = TextDirection.TopBottom_LeftRight,
  766. VerticalTextAlignment = Alignment.End
  767. };
  768. if (autoSize)
  769. {
  770. lblRight.Width = Dim.Auto ();
  771. lblRight.Height = Dim.Auto ();
  772. }
  773. var lblJust = new View
  774. {
  775. Text = text,
  776. X = 6,
  777. Width = 1,
  778. Height = height,
  779. TextDirection = TextDirection.TopBottom_LeftRight,
  780. VerticalTextAlignment = Alignment.Fill
  781. };
  782. if (autoSize)
  783. {
  784. lblJust.Width = Dim.Auto ();
  785. lblJust.Height = Dim.Auto ();
  786. }
  787. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  788. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  789. var top = new Toplevel ();
  790. top.Add (frame);
  791. Application.Begin (top);
  792. ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
  793. if (autoSize)
  794. {
  795. Assert.Equal (new (1, 11), lblLeft.TextFormatter.Size);
  796. Assert.Equal (new (1, 11), lblCenter.TextFormatter.Size);
  797. Assert.Equal (new (1, 11), lblRight.TextFormatter.Size);
  798. // Assert.Equal (new (1, 11), lblJust.TextFormatter.Size);
  799. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  800. }
  801. else
  802. {
  803. Assert.Equal (new (1, height), lblLeft.TextFormatter.Size);
  804. Assert.Equal (new (1, height), lblCenter.TextFormatter.Size);
  805. Assert.Equal (new (1, height), lblRight.TextFormatter.Size);
  806. Assert.Equal (new (1, height), lblJust.TextFormatter.Size);
  807. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  808. }
  809. string expected;
  810. if (autoSize)
  811. {
  812. expected = @"
  813. ┌───────┐
  814. │H H H H│
  815. │e e e e│
  816. │l l l l│
  817. │l l l l│
  818. │o o o o│
  819. │ │
  820. │W W W W│
  821. │o o o o│
  822. │r r r r│
  823. │l l l l│
  824. │d d d d│
  825. │ │
  826. │ │
  827. │ │
  828. │ │
  829. │ │
  830. │ │
  831. │ │
  832. │ │
  833. │ │
  834. └───────┘
  835. ";
  836. }
  837. else
  838. {
  839. expected = @"
  840. ┌───────┐
  841. │H H│
  842. │e e│
  843. │l l│
  844. │l l│
  845. │o H o│
  846. │ e │
  847. │W l │
  848. │o l │
  849. │r o │
  850. │l H │
  851. │d W e │
  852. │ o l │
  853. │ r l │
  854. │ l o │
  855. │ d │
  856. │ W W│
  857. │ o o│
  858. │ r r│
  859. │ l l│
  860. │ d d│
  861. └───────┘
  862. ";
  863. }
  864. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  865. Assert.Equal (new (0, 0, 9, height + 2), pos);
  866. top.Dispose ();
  867. }
  868. // Test that View.PreserveTrailingSpaces removes trailing spaces
  869. [Fact]
  870. public void PreserveTrailingSpaces_Removes_Trailing_Spaces ()
  871. {
  872. var view = new View { Text = "Hello World " };
  873. Assert.Equal ("Hello World ", view.TextFormatter.Text);
  874. view.TextFormatter.WordWrap = true;
  875. view.TextFormatter.Size = new (5, 3);
  876. view.PreserveTrailingSpaces = false;
  877. Assert.Equal ($"Hello{Environment.NewLine}World", view.TextFormatter.Format ());
  878. view.PreserveTrailingSpaces = true;
  879. Assert.Equal ($"Hello{Environment.NewLine} {Environment.NewLine}World", view.TextFormatter.Format ());
  880. }
  881. // View.PreserveTrailingSpaces Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  882. // or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  883. // If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  884. // <see cref = "Text" / > is formatted for display.The default is <see langword = "false" / >.
  885. [Fact]
  886. public void PreserveTrailingSpaces_Set_Get ()
  887. {
  888. var view = new View { Text = "Hello World" };
  889. Assert.False (view.PreserveTrailingSpaces);
  890. view.PreserveTrailingSpaces = true;
  891. Assert.True (view.PreserveTrailingSpaces);
  892. }
  893. // Setting TextFormatter DOES NOT update Text
  894. [Fact]
  895. public void SettingTextFormatterDoesNotUpdateText ()
  896. {
  897. var view = new View ();
  898. view.TextFormatter.Text = "Hello World";
  899. Assert.True (string.IsNullOrEmpty (view.Text));
  900. }
  901. // Setting Text updates TextFormatter
  902. [Fact]
  903. public void SettingTextUpdatesTextFormatter ()
  904. {
  905. var view = new View { Text = "Hello World" };
  906. Assert.Equal ("Hello World", view.Text);
  907. Assert.Equal ("Hello World", view.TextFormatter.Text);
  908. }
  909. // Setting Text does NOT set the HotKey
  910. [Fact]
  911. public void Text_Does_Not_Set_HotKey ()
  912. {
  913. var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
  914. Assert.NotEqual (Key.H, view.HotKey);
  915. }
  916. // Test that TextFormatter is init only
  917. [Fact]
  918. public void TextFormatterIsInitOnly ()
  919. {
  920. var view = new View ();
  921. // Use reflection to ensure the TextFormatter property is `init` only
  922. Assert.Contains (
  923. typeof (IsExternalInit),
  924. typeof (View).GetMethod ("set_TextFormatter")
  925. .ReturnParameter.GetRequiredCustomModifiers ());
  926. }
  927. // Test that the Text property is set correctly.
  928. [Fact]
  929. public void TextProperty ()
  930. {
  931. var view = new View { Text = "Hello World" };
  932. Assert.Equal ("Hello World", view.Text);
  933. }
  934. // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
  935. [Fact]
  936. public void UpdateTextFormatterText_Overridden ()
  937. {
  938. var view = new TestView { Text = "Hello World" };
  939. Assert.Equal ("Hello World", view.Text);
  940. Assert.Equal (">Hello World<", view.TextFormatter.Text);
  941. }
  942. private class TestView : View
  943. {
  944. protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
  945. }
  946. [Fact]
  947. public void TextDirection_Horizontal_Dims_Correct ()
  948. {
  949. // Initializes a view with a vertical direction
  950. var view = new View
  951. {
  952. Text = "01234",
  953. TextDirection = TextDirection.LeftRight_TopBottom,
  954. Width = Dim.Auto (DimAutoStyle.Text),
  955. Height = Dim.Auto (DimAutoStyle.Text)
  956. };
  957. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  958. Assert.Equal (new (0, 0, 5, 1), view.Viewport);
  959. view.BeginInit ();
  960. view.EndInit ();
  961. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  962. Assert.Equal (new (0, 0, 5, 1), view.Viewport);
  963. }
  964. // BUGBUG: this is a temporary test that helped identify #3469 - It needs to be expanded upon (and renamed)
  965. [Fact]
  966. public void TextDirection_Horizontal_Dims_Correct_WidthAbsolute ()
  967. {
  968. var view = new View
  969. {
  970. Text = "01234",
  971. TextDirection = TextDirection.LeftRight_TopBottom,
  972. TextAlignment = Alignment.Center,
  973. Width = 10,
  974. Height = Dim.Auto (DimAutoStyle.Text)
  975. };
  976. view.BeginInit ();
  977. view.EndInit ();
  978. Assert.Equal (new (0, 0, 10, 1), view.Frame);
  979. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  980. Assert.Equal (new (10, 1), view.TextFormatter.Size);
  981. }
  982. [Fact]
  983. public void TextDirection_Vertical_Dims_Correct ()
  984. {
  985. // Initializes a view with a vertical direction
  986. var view = new View
  987. {
  988. TextDirection = TextDirection.TopBottom_LeftRight,
  989. Text = "01234",
  990. Width = Dim.Auto (DimAutoStyle.Text),
  991. Height = Dim.Auto (DimAutoStyle.Text)
  992. };
  993. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  994. Assert.Equal (new (0, 0, 1, 5), view.Viewport);
  995. view.BeginInit ();
  996. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  997. view.EndInit ();
  998. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  999. Assert.Equal (new (0, 0, 1, 5), view.Viewport);
  1000. }
  1001. [Fact]
  1002. [SetupFakeDriver]
  1003. public void Narrow_Wide_Runes ()
  1004. {
  1005. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  1006. var top = new View { Width = 32, Height = 32 };
  1007. var text = $"First line{Environment.NewLine}Second line";
  1008. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  1009. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1010. horizontalView.TextFormatter.Size = new (20, 1);
  1011. var verticalView = new View
  1012. {
  1013. Y = 3,
  1014. Height = 20,
  1015. Width = 1,
  1016. Text = text,
  1017. TextDirection = TextDirection.TopBottom_LeftRight
  1018. };
  1019. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1020. verticalView.TextFormatter.Size = new (1, 20);
  1021. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  1022. frame.Add (horizontalView, verticalView);
  1023. top.Add (frame);
  1024. top.BeginInit ();
  1025. top.EndInit ();
  1026. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  1027. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  1028. top.Draw ();
  1029. var expected = @"
  1030. ┌──────────────────────────────┐
  1031. │First line Second li │
  1032. │ │
  1033. │ │
  1034. │F │
  1035. │i │
  1036. │r │
  1037. │s │
  1038. │t │
  1039. │ │
  1040. │l │
  1041. │i │
  1042. │n │
  1043. │e │
  1044. │ │
  1045. │S │
  1046. │e │
  1047. │c │
  1048. │o │
  1049. │n │
  1050. │d │
  1051. │ │
  1052. │l │
  1053. │i │
  1054. │ │
  1055. │ │
  1056. │ │
  1057. │ │
  1058. │ │
  1059. │ │
  1060. │ │
  1061. └──────────────────────────────┘
  1062. ";
  1063. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1064. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  1065. Assert.True (verticalView.TextFormatter.NeedsFormat);
  1066. // Autosize is off, so we have to explicitly set TextFormatter.Size
  1067. // We know these glpyhs are 2 cols wide, so we need to widen the view
  1068. verticalView.Width = 2;
  1069. verticalView.TextFormatter.Size = new (2, 20);
  1070. Assert.True (verticalView.TextFormatter.NeedsFormat);
  1071. top.Draw ();
  1072. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  1073. expected = @"
  1074. ┌──────────────────────────────┐
  1075. │First line Second li │
  1076. │ │
  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. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1108. }
  1109. // Test behavior of AutoSize property.
  1110. // - Default is false
  1111. // - Setting to true invalidates Height/Width
  1112. // - Setting to false invalidates Height/Width
  1113. }