TextTests.cs 35 KB

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