LabelTests.cs 45 KB

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