TextTests.cs 38 KB

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