TextTests.cs 40 KB

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