LabelTests.cs 43 KB

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