TextTests.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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.IsLayoutNeeded ());
  602. Application.Refresh();
  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. [Theory]
  617. [AutoInitShutdown]
  618. [InlineData (true)]
  619. [InlineData (false)]
  620. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  621. {
  622. var text = "Hello World";
  623. var width = 20;
  624. var lblLeft = new View
  625. {
  626. Text = text,
  627. Width = width,
  628. Height = 1
  629. };
  630. if (autoSize)
  631. {
  632. lblLeft.Width = Dim.Auto ();
  633. lblLeft.Height = Dim.Auto ();
  634. }
  635. var lblCenter = new View
  636. {
  637. Text = text,
  638. Y = 1,
  639. Width = width,
  640. Height = 1,
  641. TextAlignment = Alignment.Center
  642. };
  643. if (autoSize)
  644. {
  645. lblCenter.Width = Dim.Auto ();
  646. lblCenter.Height = Dim.Auto ();
  647. }
  648. var lblRight = new View
  649. {
  650. Text = text,
  651. Y = 2,
  652. Width = width,
  653. Height = 1,
  654. TextAlignment = Alignment.End
  655. };
  656. if (autoSize)
  657. {
  658. lblRight.Width = Dim.Auto ();
  659. lblRight.Height = Dim.Auto ();
  660. }
  661. var lblJust = new View
  662. {
  663. Text = text,
  664. Y = 3,
  665. Width = width,
  666. Height = 1,
  667. TextAlignment = Alignment.Fill
  668. };
  669. if (autoSize)
  670. {
  671. lblJust.Width = Dim.Auto ();
  672. lblJust.Height = Dim.Auto ();
  673. }
  674. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  675. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  676. var top = new Toplevel ();
  677. top.Add (frame);
  678. Application.Begin (top);
  679. ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
  680. // frame.Width is width + border wide (20 + 2) and 6 high
  681. if (autoSize)
  682. {
  683. Size expectedSize = new (11, 1);
  684. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  685. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  686. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  687. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  688. }
  689. else
  690. {
  691. Size expectedSize = new (width, 1);
  692. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  693. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  694. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  695. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  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.ConstrainToSize);
  796. Assert.Equal (new (1, 11), lblCenter.TextFormatter.ConstrainToSize);
  797. Assert.Equal (new (1, 11), lblRight.TextFormatter.ConstrainToSize);
  798. Assert.Equal (new (1, 11), lblJust.TextFormatter.ConstrainToSize);
  799. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  800. }
  801. else
  802. {
  803. Assert.Equal (new (1, height), lblLeft.TextFormatter.ConstrainToSize);
  804. Assert.Equal (new (1, height), lblCenter.TextFormatter.ConstrainToSize);
  805. Assert.Equal (new (1, height), lblRight.TextFormatter.ConstrainToSize);
  806. Assert.Equal (new (1, height), lblJust.TextFormatter.ConstrainToSize);
  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.ConstrainToSize = 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.ConstrainToSize);
  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.ConstrainToSize = 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.ConstrainToSize = 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.ConstrainToSize = 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. }