LabelTests.cs 47 KB

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