LabelTests.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace UnitTests.ViewsTests;
  4. public class LabelTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void Text_Set_With_AnchorEnd_Works ()
  9. {
  10. var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" };
  11. label.X = Pos.AnchorEnd (0) - Pos.Func (_ => label.TextFormatter.Text.GetColumns ());
  12. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  13. win.Add (label);
  14. var top = new Runnable ();
  15. top.Add (win);
  16. Application.Begin (top);
  17. Application.Driver!.SetScreenSize (30, 5);
  18. AutoInitShutdownAttribute.RunIteration ();
  19. var expected = @"
  20. ┌────────────────────────────┐
  21. │ │
  22. │ Say Hello 你│
  23. │ │
  24. └────────────────────────────┘
  25. ";
  26. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  27. label.Text = "Say Hello 你 changed";
  28. AutoInitShutdownAttribute.RunIteration ();
  29. expected = @"
  30. ┌────────────────────────────┐
  31. │ │
  32. │ Say Hello 你 changed│
  33. │ │
  34. └────────────────────────────┘
  35. ";
  36. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  37. top.Dispose ();
  38. }
  39. [Fact]
  40. [AutoInitShutdown]
  41. public void Set_Text_With_Center ()
  42. {
  43. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  44. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  45. win.Add (label);
  46. var top = new Runnable ();
  47. top.Add (win);
  48. Application.Begin (top);
  49. Application.Driver!.SetScreenSize (30, 5);
  50. Application.LayoutAndDraw ();
  51. var expected = @"
  52. ┌────────────────────────────┐
  53. │ │
  54. │ Say Hello 你 │
  55. │ │
  56. └────────────────────────────┘
  57. ";
  58. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  59. label.Text = "Say Hello 你 changed";
  60. AutoInitShutdownAttribute.RunIteration ();
  61. expected = @"
  62. ┌────────────────────────────┐
  63. │ │
  64. │ Say Hello 你 changed │
  65. │ │
  66. └────────────────────────────┘
  67. ";
  68. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  69. top.Dispose ();
  70. }
  71. [Fact]
  72. [AutoInitShutdown]
  73. public void Label_Draw_Fill_Remaining ()
  74. {
  75. Size tfSize = new (80, 1);
  76. Label label = new () { Text = "This label needs to be cleared before rewritten.", Width = tfSize.Width, Height = tfSize.Height };
  77. TextFormatter tf1 = new () { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize };
  78. tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
  79. TextFormatter tf2 = new () { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize, FillRemaining = true };
  80. tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
  81. Runnable top = new ();
  82. top.Add (label);
  83. SessionToken sessionToken = Application.Begin (top);
  84. AutoInitShutdownAttribute.RunIteration ();
  85. Assert.False (label.TextFormatter.FillRemaining);
  86. Assert.False (tf1.FillRemaining);
  87. Assert.True (tf2.FillRemaining);
  88. AutoInitShutdownAttribute.RunIteration ();
  89. tf1.Draw (
  90. Application.Driver,
  91. new (new (0, 1), tfSize),
  92. label.GetAttributeForRole (VisualRole.Normal),
  93. label.GetAttributeForRole (VisualRole.HotNormal));
  94. tf2.Draw (
  95. Application.Driver,
  96. new (new (0, 2), tfSize),
  97. label.GetAttributeForRole (VisualRole.Normal),
  98. label.GetAttributeForRole (VisualRole.HotNormal));
  99. DriverAssert.AssertDriverContentsWithFrameAre (
  100. @"
  101. This label needs to be cleared before rewritten.
  102. This TextFormatter (tf1) without fill will not be cleared on rewritten.
  103. This TextFormatter (tf2) with fill will be cleared on rewritten. ",
  104. output
  105. );
  106. Assert.False (label.NeedsDraw);
  107. Assert.False (label.NeedsLayout);
  108. Assert.False (label.SubViewNeedsDraw);
  109. label.Text = "This label is rewritten.";
  110. Assert.True (label.NeedsDraw);
  111. Assert.True (label.NeedsLayout);
  112. //Assert.False (label.SubViewNeedsDraw);
  113. label.Draw ();
  114. tf1.Text = "This TextFormatter (tf1) is rewritten.";
  115. tf1.Draw (
  116. Application.Driver,
  117. new (new (0, 1), tfSize),
  118. label.GetAttributeForRole (VisualRole.Normal),
  119. label.GetAttributeForRole (VisualRole.HotNormal));
  120. tf2.Text = "This TextFormatter (tf2) is rewritten.";
  121. tf2.Draw (
  122. Application.Driver,
  123. new (new (0, 2), tfSize),
  124. label.GetAttributeForRole (VisualRole.Normal),
  125. label.GetAttributeForRole (VisualRole.HotNormal));
  126. DriverAssert.AssertDriverContentsWithFrameAre (
  127. @"
  128. This label is rewritten.
  129. This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
  130. This TextFormatter (tf2) is rewritten. ",
  131. output
  132. );
  133. top.Dispose ();
  134. }
  135. [Fact]
  136. [AutoInitShutdown]
  137. public void Label_Draw_Horizontal_Simple_Runes ()
  138. {
  139. var label = new Label { Text = "Demo Simple Text" };
  140. var top = new Runnable ();
  141. top.Add (label);
  142. Application.Begin (top);
  143. AutoInitShutdownAttribute.RunIteration ();
  144. Assert.Equal (new (0, 0, 16, 1), label.Frame);
  145. var expected = @"
  146. Demo Simple Text
  147. ";
  148. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  149. Assert.Equal (new (0, 0, 16, 1), pos);
  150. top.Dispose ();
  151. }
  152. [Fact]
  153. [AutoInitShutdown]
  154. public void Label_Draw_Vertical_Simple_Text ()
  155. {
  156. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Text" };
  157. var top = new Runnable ();
  158. top.Add (label);
  159. Application.Begin (top);
  160. AutoInitShutdownAttribute.RunIteration ();
  161. Assert.NotNull (label.Width);
  162. Assert.NotNull (label.Height);
  163. var expected = @"
  164. D
  165. e
  166. m
  167. o
  168. S
  169. i
  170. m
  171. p
  172. l
  173. e
  174. T
  175. e
  176. x
  177. t
  178. ";
  179. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  180. Assert.Equal (new (0, 0, 1, 16), pos);
  181. top.Dispose ();
  182. }
  183. [Fact]
  184. [AutoInitShutdown]
  185. public void Label_Draw_Vertical_Wide_Runes ()
  186. {
  187. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
  188. var top = new Runnable ();
  189. top.Add (label);
  190. Application.Begin (top);
  191. AutoInitShutdownAttribute.RunIteration ();
  192. var expected = @"
  193. ";
  194. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  195. Assert.Equal (new (0, 0, 2, 7), pos);
  196. top.Dispose ();
  197. }
  198. [Fact]
  199. [AutoInitShutdown]
  200. public void Update_Only_On_Or_After_Initialize ()
  201. {
  202. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  203. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  204. win.Add (label);
  205. var top = new Runnable ();
  206. top.Add (win);
  207. Assert.False (label.IsInitialized);
  208. Application.Begin (top);
  209. Application.Driver!.SetScreenSize (30, 5);
  210. Application.LayoutAndDraw ();
  211. Assert.True (label.IsInitialized);
  212. Assert.Equal ("Say Hello 你", label.Text);
  213. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  214. Assert.Equal (new (0, 0, 12, 1), label.Viewport);
  215. var expected = @"
  216. ┌────────────────────────────┐
  217. │ │
  218. │ Say Hello 你 │
  219. │ │
  220. └────────────────────────────┘
  221. ";
  222. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  223. Assert.Equal (new (0, 0, 30, 5), pos);
  224. top.Dispose ();
  225. }
  226. [Fact]
  227. [AutoInitShutdown]
  228. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  229. {
  230. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  231. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  232. win.Add (label);
  233. var top = new Runnable ();
  234. top.Add (win);
  235. Assert.False (label.IsInitialized);
  236. Application.Begin (top);
  237. Application.Driver!.SetScreenSize (30, 5);
  238. Application.LayoutAndDraw ();
  239. Assert.True (label.IsInitialized);
  240. Assert.Equal ("Say Hello 你", label.Text);
  241. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  242. Assert.Equal (new (0, 0, 12, 1), label.Viewport);
  243. var expected = @"
  244. ┌────────────────────────────┐
  245. │ │
  246. │ Say Hello 你 │
  247. │ │
  248. └────────────────────────────┘
  249. ";
  250. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  251. Assert.Equal (new (0, 0, 30, 5), pos);
  252. top.Dispose ();
  253. }
  254. [Fact]
  255. [SetupFakeApplication]
  256. public void Full_Border ()
  257. {
  258. var label = new Label
  259. {
  260. Driver = Application.Driver,
  261. BorderStyle = LineStyle.Single, Text = "Test"
  262. };
  263. label.BeginInit ();
  264. label.EndInit ();
  265. label.SetRelativeLayout (Application.Screen.Size);
  266. Assert.Equal (new (0, 0, 4, 1), label.Viewport);
  267. Assert.Equal (new (0, 0, 6, 3), label.Frame);
  268. label.Draw ();
  269. DriverAssert.AssertDriverContentsWithFrameAre (
  270. @"
  271. ┌┤Te├┐
  272. │Test│
  273. └────┘",
  274. output
  275. );
  276. label.Dispose ();
  277. }
  278. // These tests were formally in AutoSizetrue.cs. They are (poor) Label tests.
  279. private readonly string [] _expecteds = new string [21]
  280. {
  281. @"
  282. ┌────────────────────┐
  283. │View with long text │
  284. │ │
  285. └────────────────────┘",
  286. @"
  287. ┌────────────────────┐
  288. │View with long text │
  289. │Label 0 │
  290. │Label 0 │
  291. └────────────────────┘",
  292. @"
  293. ┌────────────────────┐
  294. │View with long text │
  295. │Label 0 │
  296. │Label 1 │
  297. │Label 1 │
  298. └────────────────────┘",
  299. @"
  300. ┌────────────────────┐
  301. │View with long text │
  302. │Label 0 │
  303. │Label 1 │
  304. │Label 2 │
  305. │Label 2 │
  306. └────────────────────┘",
  307. @"
  308. ┌────────────────────┐
  309. │View with long text │
  310. │Label 0 │
  311. │Label 1 │
  312. │Label 2 │
  313. │Label 3 │
  314. │Label 3 │
  315. └────────────────────┘",
  316. @"
  317. ┌────────────────────┐
  318. │View with long text │
  319. │Label 0 │
  320. │Label 1 │
  321. │Label 2 │
  322. │Label 3 │
  323. │Label 4 │
  324. │Label 4 │
  325. └────────────────────┘",
  326. @"
  327. ┌────────────────────┐
  328. │View with long text │
  329. │Label 0 │
  330. │Label 1 │
  331. │Label 2 │
  332. │Label 3 │
  333. │Label 4 │
  334. │Label 5 │
  335. │Label 5 │
  336. └────────────────────┘",
  337. @"
  338. ┌────────────────────┐
  339. │View with long text │
  340. │Label 0 │
  341. │Label 1 │
  342. │Label 2 │
  343. │Label 3 │
  344. │Label 4 │
  345. │Label 5 │
  346. │Label 6 │
  347. │Label 6 │
  348. └────────────────────┘",
  349. @"
  350. ┌────────────────────┐
  351. │View with long text │
  352. │Label 0 │
  353. │Label 1 │
  354. │Label 2 │
  355. │Label 3 │
  356. │Label 4 │
  357. │Label 5 │
  358. │Label 6 │
  359. │Label 7 │
  360. │Label 7 │
  361. └────────────────────┘",
  362. @"
  363. ┌────────────────────┐
  364. │View with long text │
  365. │Label 0 │
  366. │Label 1 │
  367. │Label 2 │
  368. │Label 3 │
  369. │Label 4 │
  370. │Label 5 │
  371. │Label 6 │
  372. │Label 7 │
  373. │Label 8 │
  374. │Label 8 │
  375. └────────────────────┘",
  376. @"
  377. ┌────────────────────┐
  378. │View with long text │
  379. │Label 0 │
  380. │Label 1 │
  381. │Label 2 │
  382. │Label 3 │
  383. │Label 4 │
  384. │Label 5 │
  385. │Label 6 │
  386. │Label 7 │
  387. │Label 8 │
  388. │Label 9 │
  389. │Label 9 │
  390. └────────────────────┘",
  391. @"
  392. ┌────────────────────┐
  393. │View with long text │
  394. │Label 0 │
  395. │Label 1 │
  396. │Label 2 │
  397. │Label 3 │
  398. │Label 4 │
  399. │Label 5 │
  400. │Label 6 │
  401. │Label 7 │
  402. │Label 8 │
  403. │Label 9 │
  404. │Label 10 │
  405. │Label 10 │
  406. └────────────────────┘",
  407. @"
  408. ┌────────────────────┐
  409. │View with long text │
  410. │Label 0 │
  411. │Label 1 │
  412. │Label 2 │
  413. │Label 3 │
  414. │Label 4 │
  415. │Label 5 │
  416. │Label 6 │
  417. │Label 7 │
  418. │Label 8 │
  419. │Label 9 │
  420. │Label 10 │
  421. │Label 11 │
  422. │Label 11 │
  423. └────────────────────┘",
  424. @"
  425. ┌────────────────────┐
  426. │View with long text │
  427. │Label 0 │
  428. │Label 1 │
  429. │Label 2 │
  430. │Label 3 │
  431. │Label 4 │
  432. │Label 5 │
  433. │Label 6 │
  434. │Label 7 │
  435. │Label 8 │
  436. │Label 9 │
  437. │Label 10 │
  438. │Label 11 │
  439. │Label 12 │
  440. │Label 12 │
  441. └────────────────────┘",
  442. @"
  443. ┌────────────────────┐
  444. │View with long text │
  445. │Label 0 │
  446. │Label 1 │
  447. │Label 2 │
  448. │Label 3 │
  449. │Label 4 │
  450. │Label 5 │
  451. │Label 6 │
  452. │Label 7 │
  453. │Label 8 │
  454. │Label 9 │
  455. │Label 10 │
  456. │Label 11 │
  457. │Label 12 │
  458. │Label 13 │
  459. │Label 13 │
  460. └────────────────────┘",
  461. @"
  462. ┌────────────────────┐
  463. │View with long text │
  464. │Label 0 │
  465. │Label 1 │
  466. │Label 2 │
  467. │Label 3 │
  468. │Label 4 │
  469. │Label 5 │
  470. │Label 6 │
  471. │Label 7 │
  472. │Label 8 │
  473. │Label 9 │
  474. │Label 10 │
  475. │Label 11 │
  476. │Label 12 │
  477. │Label 13 │
  478. │Label 14 │
  479. │Label 14 │
  480. └────────────────────┘",
  481. @"
  482. ┌────────────────────┐
  483. │View with long text │
  484. │Label 0 │
  485. │Label 1 │
  486. │Label 2 │
  487. │Label 3 │
  488. │Label 4 │
  489. │Label 5 │
  490. │Label 6 │
  491. │Label 7 │
  492. │Label 8 │
  493. │Label 9 │
  494. │Label 10 │
  495. │Label 11 │
  496. │Label 12 │
  497. │Label 13 │
  498. │Label 14 │
  499. │Label 15 │
  500. │Label 15 │
  501. └────────────────────┘",
  502. @"
  503. ┌────────────────────┐
  504. │View with long text │
  505. │Label 0 │
  506. │Label 1 │
  507. │Label 2 │
  508. │Label 3 │
  509. │Label 4 │
  510. │Label 5 │
  511. │Label 6 │
  512. │Label 7 │
  513. │Label 8 │
  514. │Label 9 │
  515. │Label 10 │
  516. │Label 11 │
  517. │Label 12 │
  518. │Label 13 │
  519. │Label 14 │
  520. │Label 15 │
  521. │Label 16 │
  522. │Label 16 │
  523. └────────────────────┘",
  524. @"
  525. ┌────────────────────┐
  526. │View with long text │
  527. │Label 0 │
  528. │Label 1 │
  529. │Label 2 │
  530. │Label 3 │
  531. │Label 4 │
  532. │Label 5 │
  533. │Label 6 │
  534. │Label 7 │
  535. │Label 8 │
  536. │Label 9 │
  537. │Label 10 │
  538. │Label 11 │
  539. │Label 12 │
  540. │Label 13 │
  541. │Label 14 │
  542. │Label 15 │
  543. │Label 16 │
  544. │Label 17 │
  545. │Label 17 │
  546. └────────────────────┘",
  547. @"
  548. ┌────────────────────┐
  549. │View with long text │
  550. │Label 0 │
  551. │Label 1 │
  552. │Label 2 │
  553. │Label 3 │
  554. │Label 4 │
  555. │Label 5 │
  556. │Label 6 │
  557. │Label 7 │
  558. │Label 8 │
  559. │Label 9 │
  560. │Label 10 │
  561. │Label 11 │
  562. │Label 12 │
  563. │Label 13 │
  564. │Label 14 │
  565. │Label 15 │
  566. │Label 16 │
  567. │Label 17 │
  568. │Label 18 │
  569. │Label 18 │
  570. └────────────────────┘",
  571. @"
  572. ┌────────────────────┐
  573. │View with long text │
  574. │Label 0 │
  575. │Label 1 │
  576. │Label 2 │
  577. │Label 3 │
  578. │Label 4 │
  579. │Label 5 │
  580. │Label 6 │
  581. │Label 7 │
  582. │Label 8 │
  583. │Label 9 │
  584. │Label 10 │
  585. │Label 11 │
  586. │Label 12 │
  587. │Label 13 │
  588. │Label 14 │
  589. │Label 15 │
  590. │Label 16 │
  591. │Label 17 │
  592. │Label 18 │
  593. │Label 19 │
  594. │Label 19 │
  595. └────────────────────┘"
  596. };
  597. // TODO: This is a Label test. Move to label tests if there's not already a test for this.
  598. [Fact]
  599. [AutoInitShutdown]
  600. public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  601. {
  602. var win = new Window ();
  603. var label = new Label
  604. {
  605. Text = "This should be the last line.",
  606. //Width = Dim.Fill (),
  607. X = 0, // keep unit test focused; don't use Center here
  608. Y = Pos.AnchorEnd (1)
  609. };
  610. win.Add (label);
  611. Runnable top = new ();
  612. top.Add (win);
  613. SessionToken rs = Application.Begin (top);
  614. Application.Driver!.SetScreenSize (40, 10);
  615. Application.LayoutAndDraw ();
  616. Assert.Equal (29, label.Text.Length);
  617. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  618. Assert.Equal (new (0, 0, 40, 10), win.Frame);
  619. Assert.Equal (new (0, 7, 29, 1), label.Frame);
  620. var expected = @"
  621. ┌──────────────────────────────────────┐
  622. │ │
  623. │ │
  624. │ │
  625. │ │
  626. │ │
  627. │ │
  628. │ │
  629. │This should be the last line. │
  630. └──────────────────────────────────────┘
  631. "
  632. ;
  633. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  634. Application.End (rs);
  635. top.Dispose ();
  636. }
  637. // TODO: This is a Label test. Move to label tests if there's not already a test for this.
  638. [Fact]
  639. [AutoInitShutdown]
  640. public void Bottom_Equal_Inside_Window ()
  641. {
  642. var win = new Window ();
  643. var label = new Label
  644. {
  645. Text = "This should be the last line.",
  646. //Width = Dim.Fill (),
  647. X = 0,
  648. Y = Pos.Bottom (win)
  649. - 3 // two lines top and bottom borders more one line above the bottom border
  650. };
  651. win.Add (label);
  652. Runnable top = new ();
  653. top.Add (win);
  654. SessionToken rs = Application.Begin (top);
  655. Application.Driver!.SetScreenSize (40, 10);
  656. Application.LayoutAndDraw ();
  657. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  658. Assert.Equal (new (0, 0, 40, 10), win.Frame);
  659. Assert.Equal (new (0, 7, 29, 1), label.Frame);
  660. var expected = @"
  661. ┌──────────────────────────────────────┐
  662. │ │
  663. │ │
  664. │ │
  665. │ │
  666. │ │
  667. │ │
  668. │ │
  669. │This should be the last line. │
  670. └──────────────────────────────────────┘
  671. ";
  672. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  673. Application.End (rs);
  674. top.Dispose ();
  675. }
  676. // TODO: This is a Dim test. Move to Dim tests.
  677. [Fact]
  678. [AutoInitShutdown]
  679. public void Dim_Subtract_Operator_With_Text ()
  680. {
  681. Runnable top = new ();
  682. var view = new View
  683. {
  684. Text = "View with long text",
  685. X = 0,
  686. Y = 0,
  687. Width = 20,
  688. Height = 1
  689. };
  690. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  691. var count = 20;
  692. List<Label> listLabels = new ();
  693. for (var i = 0; i < count; i++)
  694. {
  695. field.Text = $"Label {i}";
  696. var label = new Label { Text = field.Text, X = 0, Y = i + 1 /*, Width = 10*/ };
  697. view.Add (label);
  698. Assert.Equal ($"Label {i}", label.Text);
  699. Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  700. listLabels.Add (label);
  701. if (i == 0)
  702. {
  703. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  704. view.Height += 1;
  705. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  706. }
  707. else
  708. {
  709. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  710. view.Height += 1;
  711. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  712. }
  713. }
  714. field.KeyDown += (s, k) =>
  715. {
  716. if (k.KeyCode == KeyCode.Enter)
  717. {
  718. Application.Driver!.SetScreenSize (22, count + 4);
  719. Application.LayoutAndDraw ();
  720. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (_expecteds [count], output);
  721. Assert.Equal (new (0, 0, 22, count + 4), pos);
  722. if (count > 0)
  723. {
  724. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  725. view.Remove (listLabels [count - 1]);
  726. listLabels [count - 1].Dispose ();
  727. listLabels.RemoveAt (count - 1);
  728. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  729. view.Height -= 1;
  730. count--;
  731. if (listLabels.Count > 0)
  732. {
  733. field.Text = listLabels [count - 1].Text;
  734. }
  735. else
  736. {
  737. field.Text = string.Empty;
  738. }
  739. }
  740. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  741. }
  742. };
  743. Application.Iteration += OnApplicationOnIteration;
  744. var win = new Window ();
  745. win.Add (view);
  746. win.Add (field);
  747. top.Add (win);
  748. Application.Run (top);
  749. Application.Iteration -= OnApplicationOnIteration;
  750. Assert.Equal (0, count);
  751. Assert.Equal (count, listLabels.Count);
  752. top.Dispose ();
  753. return;
  754. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  755. {
  756. while (count > -1)
  757. {
  758. field.NewKeyDownEvent (Key.Enter);
  759. if (count == 0)
  760. {
  761. field.NewKeyDownEvent (Key.Enter);
  762. break;
  763. }
  764. }
  765. Application.RequestStop ();
  766. }
  767. }
  768. // TODO: This is a Label test. Move to Label tests.
  769. [Fact]
  770. [SetupFakeApplication]
  771. public void Label_Height_Zero_Stays_Zero ()
  772. {
  773. Application.Driver!.SetScreenSize (10, 4);
  774. var text = "Label";
  775. var label = new Label
  776. {
  777. Text = text
  778. };
  779. label.Width = Dim.Fill () - text.Length;
  780. label.Height = 0;
  781. var win = new View
  782. {
  783. App = ApplicationImpl.Instance,
  784. CanFocus = true, BorderStyle = LineStyle.Single, Width = Dim.Fill (), Height = Dim.Fill ()
  785. };
  786. win.Add (label);
  787. win.BeginInit ();
  788. win.EndInit ();
  789. win.LayoutSubViews ();
  790. win.Draw ();
  791. Assert.Equal (5, text.Length);
  792. Assert.Equal (new (0, 0, 3, 0), label.Frame);
  793. //Assert.Equal (new (5, 1), label.TextFormatter.Size);
  794. Assert.Single (label.TextFormatter.GetLines ());
  795. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  796. var expected = @"
  797. ┌────────┐
  798. │ │
  799. │ │
  800. └────────┘
  801. ";
  802. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  803. Assert.Equal (new (0, 0, 10, 4), pos);
  804. text = "0123456789";
  805. Assert.Equal (10, text.Length);
  806. label.Width = Dim.Fill () - text.Length;
  807. win.LayoutSubViews ();
  808. win.ClearViewport ();
  809. win.Draw ();
  810. Assert.Equal (Rectangle.Empty, label.Frame);
  811. // Assert.Equal (new (5, 1), label.TextFormatter.Size);
  812. //Exception exception = Record.Exception (
  813. // () => Assert.Equal (
  814. // new List<string> { string.Empty },
  815. // label.TextFormatter.GetLines ()
  816. // )
  817. // );
  818. //Assert.Null (exception);
  819. expected = @"
  820. ┌────────┐
  821. │ │
  822. │ │
  823. └────────┘
  824. ";
  825. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  826. Assert.Equal (new (0, 0, 10, 4), pos);
  827. }
  828. [Fact]
  829. [AutoInitShutdown]
  830. public void Dim_Add_Operator_With_Text ()
  831. {
  832. Runnable top = new ();
  833. var view = new View
  834. {
  835. Text = "View with long text",
  836. X = 0,
  837. Y = 0,
  838. Width = 20,
  839. Height = 1
  840. };
  841. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  842. var count = 0;
  843. List<Label> listLabels = new ();
  844. field.KeyDown += (s, k) =>
  845. {
  846. if (k.KeyCode == KeyCode.Enter)
  847. {
  848. Application.Driver!.SetScreenSize (22, count + 4);
  849. Application.LayoutAndDraw ();
  850. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (_expecteds [count], output);
  851. Assert.Equal (new (0, 0, 22, count + 4), pos);
  852. if (count < 20)
  853. {
  854. field.Text = $"Label {count}";
  855. var label = new Label { Text = field.Text, X = 0, Y = view.Viewport.Height /*, Width = 10*/ };
  856. view.Add (label);
  857. Assert.Equal ($"Label {count}", label.Text);
  858. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  859. listLabels.Add (label);
  860. //if (count == 0) {
  861. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  862. // view.Height += 2;
  863. //} else {
  864. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  865. view.Height += 1;
  866. //}
  867. count++;
  868. }
  869. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  870. }
  871. };
  872. Application.Iteration += OnApplicationOnIteration;
  873. var win = new Window ();
  874. win.Add (view);
  875. win.Add (field);
  876. top.Add (win);
  877. Application.Run (top);
  878. Application.Iteration -= OnApplicationOnIteration;
  879. Assert.Equal (20, count);
  880. Assert.Equal (count, listLabels.Count);
  881. top.Dispose ();
  882. return;
  883. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  884. {
  885. while (count < 21)
  886. {
  887. field.NewKeyDownEvent (Key.Enter);
  888. if (count == 20)
  889. {
  890. field.NewKeyDownEvent (Key.Enter);
  891. break;
  892. }
  893. }
  894. Application.RequestStop ();
  895. }
  896. }
  897. [Fact]
  898. [AutoInitShutdown]
  899. public void Label_IsEmpty_False_Minimum_Height ()
  900. {
  901. var text = "Label";
  902. var label = new Label
  903. {
  904. //Width = Dim.Fill () - text.Length,
  905. Text = text
  906. };
  907. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  908. win.Add (label);
  909. var top = new Runnable ();
  910. top.Add (win);
  911. Application.Begin (top);
  912. Application.Driver!.SetScreenSize (10, 4);
  913. Application.LayoutAndDraw ();
  914. Assert.Equal (5, text.Length);
  915. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  916. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  917. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  918. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  919. Assert.Equal (new (0, 0, 10, 4), Application.TopRunnableView!.Frame);
  920. var expected = @"
  921. ┌────────┐
  922. │Label │
  923. │ │
  924. └────────┘
  925. ";
  926. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  927. Assert.Equal (new (0, 0, 10, 4), pos);
  928. text = "0123456789";
  929. Assert.Equal (10, text.Length);
  930. //label.Width = Dim.Fill () - text.Length;
  931. AutoInitShutdownAttribute.RunIteration ();
  932. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  933. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  934. Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
  935. Assert.Null (exception);
  936. expected = @"
  937. ┌────────┐
  938. │Label │
  939. │ │
  940. └────────┘
  941. ";
  942. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  943. Assert.Equal (new (0, 0, 10, 4), pos);
  944. top.Dispose ();
  945. }
  946. [Fact]
  947. [AutoInitShutdown]
  948. public void Label_IsEmpty_False_Never_Return_Null_Lines ()
  949. {
  950. var text = "Label";
  951. var label = new Label
  952. {
  953. //Width = Dim.Fill () - text.Length,
  954. //Height = 1,
  955. Text = text
  956. };
  957. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  958. win.Add (label);
  959. var top = new Runnable ();
  960. top.Add (win);
  961. Application.Begin (top);
  962. Application.Driver!.SetScreenSize (10, 4);
  963. Application.LayoutAndDraw ();
  964. Assert.Equal (5, text.Length);
  965. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  966. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  967. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  968. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  969. Assert.Equal (new (0, 0, 10, 4), Application.TopRunnableView!.Frame);
  970. var expected = @"
  971. ┌────────┐
  972. │Label │
  973. │ │
  974. └────────┘
  975. ";
  976. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  977. Assert.Equal (new (0, 0, 10, 4), pos);
  978. text = "0123456789";
  979. Assert.Equal (10, text.Length);
  980. //label.Width = Dim.Fill () - text.Length;
  981. AutoInitShutdownAttribute.RunIteration ();
  982. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  983. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  984. Assert.Single (label.TextFormatter.GetLines ());
  985. expected = @"
  986. ┌────────┐
  987. │Label │
  988. │ │
  989. └────────┘
  990. ";
  991. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  992. Assert.Equal (new (0, 0, 10, 4), pos);
  993. top.Dispose ();
  994. }
  995. [Fact]
  996. public void Label_ResizeView_With_Dim_Absolute ()
  997. {
  998. var super = new View
  999. {
  1000. Width = Dim.Fill (),
  1001. Height = Dim.Fill ()
  1002. };
  1003. var label = new Label ();
  1004. label.Text = "New text";
  1005. super.Add (label);
  1006. super.LayoutSubViews ();
  1007. Rectangle expectedLabelBounds = new (0, 0, 8, 1);
  1008. Assert.Equal (expectedLabelBounds, label.Viewport);
  1009. super.Dispose ();
  1010. }
  1011. // https://github.com/gui-cs/Terminal.Gui/issues/3893
  1012. [Fact]
  1013. [SetupFakeApplication]
  1014. public void TestLabelUnderscoreMinus ()
  1015. {
  1016. var lbl = new Label
  1017. {
  1018. Text = "TextView with some more test_- text. Unicode shouldn't 𝔹Aℝ𝔽!"
  1019. };
  1020. lbl.Draw ();
  1021. }
  1022. }