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