LabelTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class LabelTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public LabelTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void AutoSize_Stays_True_AnchorEnd ()
  10. {
  11. var label = new Label { Y = Pos.Center (), Text = "Say Hello 你", AutoSize = true };
  12. label.X = Pos.AnchorEnd () - Pos.Function (() => label.TextFormatter.Text.GetColumns ());
  13. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  14. win.Add (label);
  15. Application.Top.Add (win);
  16. Assert.True (label.AutoSize);
  17. Application.Begin (Application.Top);
  18. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  19. var expected = @"
  20. ┌────────────────────────────┐
  21. │ │
  22. │ Say Hello 你│
  23. │ │
  24. └────────────────────────────┘
  25. ";
  26. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  27. Assert.True (label.AutoSize);
  28. label.Text = "Say Hello 你 changed";
  29. Assert.True (label.AutoSize);
  30. Application.Refresh ();
  31. expected = @"
  32. ┌────────────────────────────┐
  33. │ │
  34. │ Say Hello 你 changed│
  35. │ │
  36. └────────────────────────────┘
  37. ";
  38. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  39. }
  40. [Fact]
  41. [AutoInitShutdown]
  42. public void AutoSize_Stays_True_Center ()
  43. {
  44. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  45. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  46. win.Add (label);
  47. Application.Top.Add (win);
  48. Assert.True (label.AutoSize);
  49. Application.Begin (Application.Top);
  50. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  51. var expected = @"
  52. ┌────────────────────────────┐
  53. │ │
  54. │ Say Hello 你 │
  55. │ │
  56. └────────────────────────────┘
  57. ";
  58. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  59. Assert.True (label.AutoSize);
  60. label.Text = "Say Hello 你 changed";
  61. Assert.True (label.AutoSize);
  62. Application.Refresh ();
  63. expected = @"
  64. ┌────────────────────────────┐
  65. │ │
  66. │ Say Hello 你 changed │
  67. │ │
  68. └────────────────────────────┘
  69. ";
  70. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  71. }
  72. [Fact]
  73. [AutoInitShutdown]
  74. public void AutoSize_Stays_True_With_EmptyText ()
  75. {
  76. var label = new Label { X = Pos.Center (), Y = Pos.Center (), AutoSize = true };
  77. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  78. win.Add (label);
  79. Application.Top.Add (win);
  80. Assert.True (label.AutoSize);
  81. label.Text = "Say Hello 你";
  82. Assert.True (label.AutoSize);
  83. Application.Begin (Application.Top);
  84. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  85. var expected = @"
  86. ┌────────────────────────────┐
  87. │ │
  88. │ Say Hello 你 │
  89. │ │
  90. └────────────────────────────┘
  91. ";
  92. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  93. }
  94. [Fact]
  95. [AutoInitShutdown]
  96. public void Constructors_Defaults ()
  97. {
  98. var label = new Label ();
  99. Assert.Equal (string.Empty, label.Text);
  100. Application.Top.Add (label);
  101. RunState rs = Application.Begin (Application.Top);
  102. Assert.Equal (TextAlignment.Left, label.TextAlignment);
  103. Assert.True (label.AutoSize);
  104. Assert.False (label.CanFocus);
  105. Assert.Equal (new Rect (0, 0, 0, 0), label.Frame);
  106. Assert.Equal (KeyCode.Null, label.HotKey);
  107. var expected = @"";
  108. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  109. Application.End (rs);
  110. label = new Label { Text = "Test" };
  111. Assert.True (label.AutoSize);
  112. Assert.Equal ("Test", label.Text);
  113. Application.Top.Add (label);
  114. rs = Application.Begin (Application.Top);
  115. Assert.Equal ("Test", label.TextFormatter.Text);
  116. Assert.Equal (new Rect (0, 0, 4, 1), label.Frame);
  117. expected = @"
  118. Test
  119. ";
  120. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  121. Application.End (rs);
  122. label = new Label { X = 3, Y = 4, Text = "Test" };
  123. Assert.Equal ("Test", label.Text);
  124. Application.Top.Add (label);
  125. rs = Application.Begin (Application.Top);
  126. Assert.Equal ("Test", label.TextFormatter.Text);
  127. Assert.Equal (new Rect (3, 4, 4, 1), label.Frame);
  128. expected = @"
  129. Test
  130. ";
  131. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  132. Application.End (rs);
  133. }
  134. [Fact]
  135. [AutoInitShutdown]
  136. public void Label_Draw_Fill_Remaining_AutoSize_True ()
  137. {
  138. var label = new Label { Text = "This label needs to be cleared before rewritten." };
  139. var tf1 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom };
  140. tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
  141. Size tf1Size = tf1.Size;
  142. var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, FillRemaining = true };
  143. tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
  144. Size tf2Size = tf2.Size;
  145. Application.Top.Add (label);
  146. Application.Begin (Application.Top);
  147. Assert.True (label.AutoSize);
  148. tf1.Draw (
  149. new Rect (new Point (0, 1), tf1Size),
  150. label.GetNormalColor (),
  151. label.ColorScheme.HotNormal
  152. );
  153. tf2.Draw (new Rect (new Point (0, 2), tf2Size), label.GetNormalColor (), label.ColorScheme.HotNormal);
  154. TestHelpers.AssertDriverContentsWithFrameAre (
  155. @"
  156. This label needs to be cleared before rewritten.
  157. This TextFormatter (tf1) without fill will not be cleared on rewritten.
  158. This TextFormatter (tf2) with fill will be cleared on rewritten.
  159. ",
  160. _output
  161. );
  162. label.Text = "This label is rewritten.";
  163. label.Draw ();
  164. tf1.Text = "This TextFormatter (tf1) is rewritten.";
  165. tf1.Draw (
  166. new Rect (new Point (0, 1), tf1Size),
  167. label.GetNormalColor (),
  168. label.ColorScheme.HotNormal
  169. );
  170. tf2.Text = "This TextFormatter (tf2) is rewritten.";
  171. tf2.Draw (new Rect (new Point (0, 2), tf2Size), label.GetNormalColor (), label.ColorScheme.HotNormal);
  172. TestHelpers.AssertDriverContentsWithFrameAre (
  173. @"
  174. This label is rewritten.
  175. This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
  176. This TextFormatter (tf2) is rewritten.
  177. ",
  178. _output
  179. );
  180. }
  181. [Fact]
  182. [AutoInitShutdown]
  183. public void Label_Draw_Horizontal_Simple_Runes ()
  184. {
  185. var label = new Label { Text = "Demo Simple Rune" };
  186. Application.Top.Add (label);
  187. Application.Begin (Application.Top);
  188. Assert.True (label.AutoSize);
  189. Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
  190. var expected = @"
  191. Demo Simple Rune
  192. ";
  193. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  194. Assert.Equal (new Rect (0, 0, 16, 1), pos);
  195. }
  196. [Fact]
  197. [AutoInitShutdown]
  198. public void Label_Draw_Vertical_Simple_Runes ()
  199. {
  200. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Rune" };
  201. Application.Top.Add (label);
  202. Application.Begin (Application.Top);
  203. Assert.NotNull (label.Width);
  204. Assert.NotNull (label.Height);
  205. var expected = @"
  206. D
  207. e
  208. m
  209. o
  210. S
  211. i
  212. m
  213. p
  214. l
  215. e
  216. R
  217. u
  218. n
  219. e
  220. ";
  221. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  222. Assert.Equal (new Rect (0, 0, 1, 16), pos);
  223. }
  224. [Fact]
  225. [AutoInitShutdown]
  226. public void Label_Draw_Vertical_Wide_Runes ()
  227. {
  228. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
  229. Application.Top.Add (label);
  230. Application.Begin (Application.Top);
  231. var expected = @"
  232. ";
  233. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  234. Assert.Equal (new Rect (0, 0, 2, 7), pos);
  235. }
  236. [Fact]
  237. [AutoInitShutdown]
  238. public void Label_HotKeyChanged_EventFires ()
  239. {
  240. var label = new Label { Text = "Yar" };
  241. label.HotKey = 'Y';
  242. object sender = null;
  243. KeyChangedEventArgs args = null;
  244. label.HotKeyChanged += (s, e) =>
  245. {
  246. sender = s;
  247. args = e;
  248. };
  249. label.HotKey = Key.R;
  250. Assert.Same (label, sender);
  251. Assert.Equal (KeyCode.Y | KeyCode.ShiftMask, args.OldKey);
  252. Assert.Equal (Key.R, args.NewKey);
  253. }
  254. [Fact]
  255. [AutoInitShutdown]
  256. public void Label_HotKeyChanged_EventFires_WithNone ()
  257. {
  258. var label = new Label ();
  259. object sender = null;
  260. KeyChangedEventArgs args = null;
  261. label.HotKeyChanged += (s, e) =>
  262. {
  263. sender = s;
  264. args = e;
  265. };
  266. label.HotKey = KeyCode.R;
  267. Assert.Same (label, sender);
  268. Assert.Equal (KeyCode.Null, args.OldKey);
  269. Assert.Equal (KeyCode.R, args.NewKey);
  270. }
  271. [Fact]
  272. public void TestAssignTextToLabel ()
  273. {
  274. View b = new Label { Text = "heya" };
  275. Assert.Equal ("heya", b.Text);
  276. Assert.Contains ("heya", b.TextFormatter.Text);
  277. b.Text = "heyb";
  278. Assert.Equal ("heyb", b.Text);
  279. Assert.Contains ("heyb", b.TextFormatter.Text);
  280. // with cast
  281. Assert.Equal ("heyb", ((Label)b).Text);
  282. }
  283. [Fact]
  284. [AutoInitShutdown]
  285. public void Update_Only_On_Or_After_Initialize ()
  286. {
  287. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  288. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  289. win.Add (label);
  290. Application.Top.Add (win);
  291. Assert.False (label.IsInitialized);
  292. Application.Begin (Application.Top);
  293. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  294. Assert.True (label.IsInitialized);
  295. Assert.Equal ("Say Hello 你", label.Text);
  296. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  297. Assert.Equal (new Rect (0, 0, 12, 1), label.Bounds);
  298. var expected = @"
  299. ┌────────────────────────────┐
  300. │ │
  301. │ Say Hello 你 │
  302. │ │
  303. └────────────────────────────┘
  304. ";
  305. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  306. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  307. }
  308. [Fact]
  309. [AutoInitShutdown]
  310. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  311. {
  312. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你", AutoSize = true };
  313. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  314. win.Add (label);
  315. Application.Top.Add (win);
  316. Assert.False (label.IsInitialized);
  317. Application.Begin (Application.Top);
  318. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  319. Assert.True (label.IsInitialized);
  320. Assert.Equal ("Say Hello 你", label.Text);
  321. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  322. Assert.Equal (new Rect (0, 0, 12, 1), label.Bounds);
  323. var expected = @"
  324. ┌────────────────────────────┐
  325. │ │
  326. │ Say Hello 你 │
  327. │ │
  328. └────────────────────────────┘
  329. ";
  330. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  331. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  332. }
  333. }