LabelTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. using System.ComponentModel;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class LabelTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public LabelTests (ITestOutputHelper output) { _output = output; }
  8. // Test that Title and Text are the same
  9. [Fact]
  10. public void Text_Mirrors_Title ()
  11. {
  12. var label = new Label ();
  13. label.Title = "Hello";
  14. Assert.Equal ("Hello", label.Title);
  15. Assert.Equal ("Hello", label.TitleTextFormatter.Text);
  16. Assert.Equal ("Hello", label.Text);
  17. Assert.Equal ("Hello", label.TextFormatter.Text);
  18. }
  19. [Fact]
  20. public void Title_Mirrors_Text ()
  21. {
  22. var label = new Label ();
  23. label.Text = "Hello";
  24. Assert.Equal ("Hello", label.Text);
  25. Assert.Equal ("Hello", label.TextFormatter.Text);
  26. Assert.Equal ("Hello", label.Title);
  27. Assert.Equal ("Hello", label.TitleTextFormatter.Text);
  28. }
  29. [Fact]
  30. public void HotKey_Command_SetsFocus_OnNextSubview ()
  31. {
  32. var superView = new View () { CanFocus = true };
  33. var label = new Label ();
  34. var nextSubview = new View () { CanFocus = true };
  35. superView.Add (label, nextSubview);
  36. superView.BeginInit ();
  37. superView.EndInit ();
  38. Assert.False (label.HasFocus);
  39. Assert.False (nextSubview.HasFocus);
  40. label.InvokeCommand (Command.HotKey);
  41. Assert.False (label.HasFocus);
  42. Assert.True (nextSubview.HasFocus);
  43. }
  44. [Fact]
  45. public void HotKey_Command_Does_Not_Accept ()
  46. {
  47. var label = new Label ();
  48. var accepted = false;
  49. label.Accept += LabelOnAccept;
  50. label.InvokeCommand (Command.HotKey);
  51. Assert.False (accepted);
  52. return;
  53. void LabelOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  54. }
  55. [Fact]
  56. [AutoInitShutdown]
  57. public void AutoSize_Stays_True_AnchorEnd ()
  58. {
  59. var label = new Label { Y = Pos.Center (), Text = "Say Hello 你", AutoSize = true };
  60. label.X = Pos.AnchorEnd () - Pos.Function (() => label.TextFormatter.Text.GetColumns ());
  61. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  62. win.Add (label);
  63. Application.Top.Add (win);
  64. Assert.True (label.AutoSize);
  65. Application.Begin (Application.Top);
  66. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  67. var expected = @"
  68. ┌────────────────────────────┐
  69. │ │
  70. │ Say Hello 你│
  71. │ │
  72. └────────────────────────────┘
  73. ";
  74. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  75. Assert.True (label.AutoSize);
  76. label.Text = "Say Hello 你 changed";
  77. Assert.True (label.AutoSize);
  78. Application.Refresh ();
  79. expected = @"
  80. ┌────────────────────────────┐
  81. │ │
  82. │ Say Hello 你 changed│
  83. │ │
  84. └────────────────────────────┘
  85. ";
  86. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  87. }
  88. [Fact]
  89. [AutoInitShutdown]
  90. public void AutoSize_Stays_True_Center ()
  91. {
  92. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  93. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  94. win.Add (label);
  95. Application.Top.Add (win);
  96. Assert.True (label.AutoSize);
  97. Application.Begin (Application.Top);
  98. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  99. var expected = @"
  100. ┌────────────────────────────┐
  101. │ │
  102. │ Say Hello 你 │
  103. │ │
  104. └────────────────────────────┘
  105. ";
  106. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  107. Assert.True (label.AutoSize);
  108. label.Text = "Say Hello 你 changed";
  109. Assert.True (label.AutoSize);
  110. Application.Refresh ();
  111. expected = @"
  112. ┌────────────────────────────┐
  113. │ │
  114. │ Say Hello 你 changed │
  115. │ │
  116. └────────────────────────────┘
  117. ";
  118. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  119. }
  120. [Fact]
  121. [AutoInitShutdown]
  122. public void AutoSize_Stays_True_With_EmptyText ()
  123. {
  124. var label = new Label { X = Pos.Center (), Y = Pos.Center (), AutoSize = true };
  125. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  126. win.Add (label);
  127. Application.Top.Add (win);
  128. Assert.True (label.AutoSize);
  129. label.Text = "Say Hello 你";
  130. Assert.True (label.AutoSize);
  131. Application.Begin (Application.Top);
  132. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  133. var expected = @"
  134. ┌────────────────────────────┐
  135. │ │
  136. │ Say Hello 你 │
  137. │ │
  138. └────────────────────────────┘
  139. ";
  140. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  141. }
  142. [Fact]
  143. [AutoInitShutdown]
  144. public void Constructors_Defaults ()
  145. {
  146. var label = new Label ();
  147. Assert.Equal (string.Empty, label.Text);
  148. Application.Top.Add (label);
  149. RunState rs = Application.Begin (Application.Top);
  150. Assert.Equal (TextAlignment.Left, label.TextAlignment);
  151. Assert.True (label.AutoSize);
  152. Assert.False (label.CanFocus);
  153. Assert.Equal (new Rectangle (0, 0, 0, 0), label.Frame);
  154. Assert.Equal (KeyCode.Null, label.HotKey);
  155. var expected = @"";
  156. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  157. Application.End (rs);
  158. label = new Label { Text = "Test" };
  159. Assert.True (label.AutoSize);
  160. Assert.Equal ("Test", label.Text);
  161. Application.Top.Add (label);
  162. rs = Application.Begin (Application.Top);
  163. Assert.Equal ("Test", label.TextFormatter.Text);
  164. Assert.Equal (new Rectangle (0, 0, 4, 1), label.Frame);
  165. expected = @"
  166. Test
  167. ";
  168. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  169. Application.End (rs);
  170. label = new Label { X = 3, Y = 4, Text = "Test" };
  171. Assert.Equal ("Test", label.Text);
  172. Application.Top.Add (label);
  173. rs = Application.Begin (Application.Top);
  174. Assert.Equal ("Test", label.TextFormatter.Text);
  175. Assert.Equal (new Rectangle (3, 4, 4, 1), label.Frame);
  176. expected = @"
  177. Test
  178. ";
  179. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  180. Application.End (rs);
  181. }
  182. [Fact]
  183. [AutoInitShutdown]
  184. public void Label_Draw_Fill_Remaining_AutoSize_True ()
  185. {
  186. var label = new Label { Text = "This label needs to be cleared before rewritten." };
  187. var tf1 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom };
  188. tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
  189. Size tf1Size = tf1.Size;
  190. var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, FillRemaining = true };
  191. tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
  192. Size tf2Size = tf2.Size;
  193. Application.Top.Add (label);
  194. Application.Begin (Application.Top);
  195. Assert.True (label.AutoSize);
  196. tf1.Draw (
  197. new Rectangle (new Point (0, 1), tf1Size),
  198. label.GetNormalColor (),
  199. label.ColorScheme.HotNormal
  200. );
  201. tf2.Draw (new Rectangle (new Point (0, 2), tf2Size), label.GetNormalColor (), label.ColorScheme.HotNormal);
  202. TestHelpers.AssertDriverContentsWithFrameAre (
  203. @"
  204. This label needs to be cleared before rewritten.
  205. This TextFormatter (tf1) without fill will not be cleared on rewritten.
  206. This TextFormatter (tf2) with fill will be cleared on rewritten.
  207. ",
  208. _output
  209. );
  210. label.Text = "This label is rewritten.";
  211. label.Draw ();
  212. tf1.Text = "This TextFormatter (tf1) is rewritten.";
  213. tf1.Draw (
  214. new Rectangle (new Point (0, 1), tf1Size),
  215. label.GetNormalColor (),
  216. label.ColorScheme.HotNormal
  217. );
  218. tf2.Text = "This TextFormatter (tf2) is rewritten.";
  219. tf2.Draw (new Rectangle (new Point (0, 2), tf2Size), label.GetNormalColor (), label.ColorScheme.HotNormal);
  220. TestHelpers.AssertDriverContentsWithFrameAre (
  221. @"
  222. This label is rewritten.
  223. This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
  224. This TextFormatter (tf2) is rewritten.
  225. ",
  226. _output
  227. );
  228. }
  229. [Fact]
  230. [AutoInitShutdown]
  231. public void Label_Draw_Horizontal_Simple_Runes ()
  232. {
  233. var label = new Label { Text = "Demo Simple Rune" };
  234. Application.Top.Add (label);
  235. Application.Begin (Application.Top);
  236. Assert.True (label.AutoSize);
  237. Assert.Equal (new Rectangle (0, 0, 16, 1), label.Frame);
  238. var expected = @"
  239. Demo Simple Rune
  240. ";
  241. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  242. Assert.Equal (new Rectangle (0, 0, 16, 1), pos);
  243. }
  244. [Fact]
  245. [AutoInitShutdown]
  246. public void Label_Draw_Vertical_Simple_Runes ()
  247. {
  248. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Rune" };
  249. Application.Top.Add (label);
  250. Application.Begin (Application.Top);
  251. Assert.NotNull (label.Width);
  252. Assert.NotNull (label.Height);
  253. var expected = @"
  254. D
  255. e
  256. m
  257. o
  258. S
  259. i
  260. m
  261. p
  262. l
  263. e
  264. R
  265. u
  266. n
  267. e
  268. ";
  269. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  270. Assert.Equal (new Rectangle (0, 0, 1, 16), pos);
  271. }
  272. [Fact]
  273. [AutoInitShutdown]
  274. public void Label_Draw_Vertical_Wide_Runes ()
  275. {
  276. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
  277. Application.Top.Add (label);
  278. Application.Begin (Application.Top);
  279. var expected = @"
  280. ";
  281. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  282. Assert.Equal (new Rectangle (0, 0, 2, 7), pos);
  283. }
  284. [Fact]
  285. [AutoInitShutdown]
  286. public void Label_HotKeyChanged_EventFires ()
  287. {
  288. var label = new Label { Text = "Yar" };
  289. label.HotKey = 'Y';
  290. object sender = null;
  291. KeyChangedEventArgs args = null;
  292. label.HotKeyChanged += (s, e) =>
  293. {
  294. sender = s;
  295. args = e;
  296. };
  297. label.HotKey = Key.R;
  298. Assert.Same (label, sender);
  299. Assert.Equal (KeyCode.Y | KeyCode.ShiftMask, args.OldKey);
  300. Assert.Equal (Key.R, args.NewKey);
  301. }
  302. [Fact]
  303. [AutoInitShutdown]
  304. public void Label_HotKeyChanged_EventFires_WithNone ()
  305. {
  306. var label = new Label ();
  307. object sender = null;
  308. KeyChangedEventArgs args = null;
  309. label.HotKeyChanged += (s, e) =>
  310. {
  311. sender = s;
  312. args = e;
  313. };
  314. label.HotKey = KeyCode.R;
  315. Assert.Same (label, sender);
  316. Assert.Equal (KeyCode.Null, args.OldKey);
  317. Assert.Equal (KeyCode.R, args.NewKey);
  318. }
  319. [Fact]
  320. public void TestAssignTextToLabel ()
  321. {
  322. View b = new Label { Text = "heya" };
  323. Assert.Equal ("heya", b.Text);
  324. Assert.Contains ("heya", b.TextFormatter.Text);
  325. b.Text = "heyb";
  326. Assert.Equal ("heyb", b.Text);
  327. Assert.Contains ("heyb", b.TextFormatter.Text);
  328. // with cast
  329. Assert.Equal ("heyb", ((Label)b).Text);
  330. }
  331. [Fact]
  332. [AutoInitShutdown]
  333. public void Update_Only_On_Or_After_Initialize ()
  334. {
  335. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  336. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  337. win.Add (label);
  338. Application.Top.Add (win);
  339. Assert.False (label.IsInitialized);
  340. Application.Begin (Application.Top);
  341. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  342. Assert.True (label.IsInitialized);
  343. Assert.Equal ("Say Hello 你", label.Text);
  344. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  345. Assert.Equal (new Rectangle (0, 0, 12, 1), label.Bounds);
  346. var expected = @"
  347. ┌────────────────────────────┐
  348. │ │
  349. │ Say Hello 你 │
  350. │ │
  351. └────────────────────────────┘
  352. ";
  353. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  354. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  355. }
  356. [Fact]
  357. [AutoInitShutdown]
  358. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  359. {
  360. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你", AutoSize = true };
  361. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  362. win.Add (label);
  363. Application.Top.Add (win);
  364. Assert.False (label.IsInitialized);
  365. Application.Begin (Application.Top);
  366. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  367. Assert.True (label.IsInitialized);
  368. Assert.Equal ("Say Hello 你", label.Text);
  369. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  370. Assert.Equal (new Rectangle (0, 0, 12, 1), label.Bounds);
  371. var expected = @"
  372. ┌────────────────────────────┐
  373. │ │
  374. │ Say Hello 你 │
  375. │ │
  376. └────────────────────────────┘
  377. ";
  378. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  379. Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
  380. }
  381. [Fact]
  382. [AutoInitShutdown]
  383. public void Full_Border ()
  384. {
  385. var label = new Label { Text = "Test", /*Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
  386. Application.Top.Add (label);
  387. Application.Begin (Application.Top);
  388. Assert.Equal (new (0, 0, 6, 3), label.Frame);
  389. Assert.Equal (new (0, 0, 4, 1), label.Bounds);
  390. TestHelpers.AssertDriverContentsWithFrameAre (
  391. @"
  392. ┌┤Te├┐
  393. │Test│
  394. └────┘",
  395. _output
  396. );
  397. }
  398. [Fact]
  399. [AutoInitShutdown]
  400. public void With_Top_Margin_Without_Top_Border ()
  401. {
  402. var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
  403. label.Margin.Thickness = new Thickness (0, 1, 0, 0);
  404. label.Border.Thickness = new Thickness (1, 0, 1, 1);
  405. Application.Top.Add (label);
  406. Application.Begin (Application.Top);
  407. Assert.Equal (new (0, 0, 6, 3), label.Frame);
  408. Assert.Equal (new (0, 0, 4, 1), label.Bounds);
  409. Application.Begin (Application.Top);
  410. TestHelpers.AssertDriverContentsWithFrameAre (
  411. @"
  412. │Test│
  413. └────┘",
  414. _output
  415. );
  416. }
  417. [Fact]
  418. [AutoInitShutdown]
  419. public void Without_Top_Border ()
  420. {
  421. var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
  422. label.Border.Thickness = new Thickness (1, 0, 1, 1);
  423. Application.Top.Add (label);
  424. Application.Begin (Application.Top);
  425. Assert.Equal (new (0, 0, 6, 2), label.Frame);
  426. Assert.Equal (new (0, 0, 4, 1), label.Bounds);
  427. Application.Begin (Application.Top);
  428. TestHelpers.AssertDriverContentsWithFrameAre (
  429. @"
  430. │Test│
  431. └────┘",
  432. _output
  433. );
  434. }
  435. }