LabelTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #nullable enable
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace ViewsTests;
  5. /// <summary>
  6. /// Pure unit tests for <see cref="Label"/> that don't require Application.Driver or Application context.
  7. /// These tests can run in parallel without interference.
  8. /// </summary>
  9. public class LabelTests (ITestOutputHelper output) : FakeDriverBase
  10. {
  11. [Fact]
  12. public void Text_Mirrors_Title ()
  13. {
  14. var label = new Label ();
  15. label.Title = "Hello";
  16. Assert.Equal ("Hello", label.Title);
  17. Assert.Equal ("Hello", label.TitleTextFormatter.Text);
  18. Assert.Equal ("Hello", label.Text);
  19. Assert.Equal ("Hello", label.TextFormatter.Text);
  20. }
  21. [Fact]
  22. public void Title_Mirrors_Text ()
  23. {
  24. var label = new Label ();
  25. label.Text = "Hello";
  26. Assert.Equal ("Hello", label.Text);
  27. Assert.Equal ("Hello", label.TextFormatter.Text);
  28. Assert.Equal ("Hello", label.Title);
  29. Assert.Equal ("Hello", label.TitleTextFormatter.Text);
  30. }
  31. [Theory]
  32. [CombinatorialData]
  33. public void HotKey_Command_SetsFocus_OnNextSubView (bool hasHotKey)
  34. {
  35. var superView = new View { CanFocus = true };
  36. var label = new Label ();
  37. label.HotKey = hasHotKey ? Key.A.WithAlt : Key.Empty;
  38. var nextSubView = new View { CanFocus = true };
  39. superView.Add (label, nextSubView);
  40. superView.BeginInit ();
  41. superView.EndInit ();
  42. Assert.False (label.HasFocus);
  43. Assert.False (nextSubView.HasFocus);
  44. label.InvokeCommand (Command.HotKey);
  45. Assert.False (label.HasFocus);
  46. Assert.Equal (hasHotKey, nextSubView.HasFocus);
  47. }
  48. [Theory]
  49. [CombinatorialData]
  50. public void MouseClick_SetsFocus_OnNextSubView (bool hasHotKey)
  51. {
  52. var superView = new View { CanFocus = true, Height = 1, Width = 15 };
  53. var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
  54. var label = new Label { X = 2 };
  55. label.HotKey = hasHotKey ? Key.X.WithAlt : Key.Empty;
  56. var nextSubView = new View { CanFocus = true, X = 4, Width = 4, Height = 1 };
  57. superView.Add (focusedView, label, nextSubView);
  58. superView.BeginInit ();
  59. superView.EndInit ();
  60. Assert.False (focusedView.HasFocus);
  61. Assert.False (label.HasFocus);
  62. Assert.False (nextSubView.HasFocus);
  63. label.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
  64. Assert.False (label.HasFocus);
  65. Assert.Equal (hasHotKey, nextSubView.HasFocus);
  66. }
  67. [Fact]
  68. public void HotKey_Command_Does_Not_Accept ()
  69. {
  70. var label = new Label ();
  71. var accepted = false;
  72. label.Accepting += LabelOnAccept;
  73. label.InvokeCommand (Command.HotKey);
  74. Assert.False (accepted);
  75. return;
  76. void LabelOnAccept (object? sender, CommandEventArgs e) { accepted = true; }
  77. }
  78. [Fact]
  79. public void Constructors_Defaults ()
  80. {
  81. var label = new Label ();
  82. Assert.Equal (string.Empty, label.Text);
  83. Assert.Equal (Alignment.Start, label.TextAlignment);
  84. Assert.False (label.CanFocus);
  85. Assert.Equal (new (0, 0, 0, 0), label.Frame);
  86. Assert.Equal (KeyCode.Null, label.HotKey);
  87. }
  88. [Fact]
  89. public void Label_HotKeyChanged_EventFires ()
  90. {
  91. var label = new Label ();
  92. var fired = false;
  93. Key oldKey = Key.Empty;
  94. Key newKey = Key.Empty;
  95. label.HotKeyChanged += (s, e) =>
  96. {
  97. fired = true;
  98. oldKey = e.OldKey;
  99. newKey = e.NewKey;
  100. };
  101. label.HotKey = Key.A.WithAlt;
  102. Assert.True (fired);
  103. Assert.Equal (Key.Empty, oldKey);
  104. Assert.Equal (Key.A.WithAlt, newKey);
  105. }
  106. [Fact]
  107. public void Label_HotKeyChanged_EventFires_WithNone ()
  108. {
  109. var label = new Label { HotKey = Key.A.WithAlt };
  110. var fired = false;
  111. Key oldKey = Key.Empty;
  112. Key newKey = Key.Empty;
  113. label.HotKeyChanged += (s, e) =>
  114. {
  115. fired = true;
  116. oldKey = e.OldKey;
  117. newKey = e.NewKey;
  118. };
  119. label.HotKey = Key.Empty;
  120. Assert.True (fired);
  121. Assert.Equal (Key.A.WithAlt, oldKey);
  122. Assert.Equal (Key.Empty, newKey);
  123. }
  124. [Fact]
  125. public void TestAssignTextToLabel ()
  126. {
  127. var label = new Label ();
  128. label.Text = "Test";
  129. Assert.Equal ("Test", label.Text);
  130. }
  131. [Fact]
  132. public void CanFocus_False_HotKey_SetsFocus_Next ()
  133. {
  134. View otherView = new ()
  135. {
  136. Text = "otherView",
  137. CanFocus = true
  138. };
  139. Label label = new ()
  140. {
  141. Text = "_label"
  142. };
  143. View nextView = new ()
  144. {
  145. Text = "nextView",
  146. CanFocus = true
  147. };
  148. IApplication app = Application.Create ();
  149. Runnable<bool> runnable = new ();
  150. app.Begin (runnable);
  151. runnable.Add (otherView, label, nextView);
  152. otherView.SetFocus ();
  153. // runnable.SetFocus ();
  154. Assert.True (otherView.HasFocus);
  155. Assert.True (app.Keyboard.RaiseKeyDownEvent (label.HotKey));
  156. Assert.False (otherView.HasFocus);
  157. Assert.False (label.HasFocus);
  158. Assert.True (nextView.HasFocus);
  159. }
  160. [Fact]
  161. public void CanFocus_False_MouseClick_SetsFocus_Next ()
  162. {
  163. View otherView = new () { X = 0, Y = 0, Width = 1, Height = 1, Id = "otherView", CanFocus = true };
  164. Label label = new () { X = 0, Y = 1, Text = "_label" };
  165. View nextView = new ()
  166. {
  167. X = Pos.Right (label), Y = Pos.Top (label), Width = 1, Height = 1, Id = "nextView", CanFocus = true
  168. };
  169. IApplication app = Application.Create ();
  170. Runnable<bool> runnable = new ();
  171. app.Begin (runnable);
  172. runnable.Add (otherView, label, nextView);
  173. otherView.SetFocus ();
  174. // click on label
  175. app.Mouse.RaiseMouseEvent (new () { ScreenPosition = label.Frame.Location, Flags = MouseFlags.Button1Clicked });
  176. Assert.False (label.HasFocus);
  177. Assert.True (nextView.HasFocus);
  178. }
  179. [Fact]
  180. public void CanFocus_True_HotKey_SetsFocus ()
  181. {
  182. Label label = new ()
  183. {
  184. Text = "_label",
  185. CanFocus = true
  186. };
  187. View view = new ()
  188. {
  189. Text = "view",
  190. CanFocus = true
  191. };
  192. IApplication app = Application.Create ();
  193. Runnable<bool> runnable = new ();
  194. app.Begin (runnable);
  195. runnable.Add (label, view);
  196. view.SetFocus ();
  197. Assert.True (label.CanFocus);
  198. Assert.False (label.HasFocus);
  199. Assert.True (view.CanFocus);
  200. Assert.True (view.HasFocus);
  201. // No focused view accepts Tab, and there's no other view to focus, so OnKeyDown returns false
  202. Assert.True (app.Keyboard.RaiseKeyDownEvent (label.HotKey));
  203. Assert.True (label.HasFocus);
  204. Assert.False (view.HasFocus);
  205. }
  206. [Fact]
  207. public void CanFocus_True_MouseClick_Focuses ()
  208. {
  209. Label label = new ()
  210. {
  211. Text = "label",
  212. X = 0,
  213. Y = 0,
  214. CanFocus = true
  215. };
  216. View otherView = new ()
  217. {
  218. Text = "view",
  219. X = 0,
  220. Y = 1,
  221. Width = 4,
  222. Height = 1,
  223. CanFocus = true
  224. };
  225. IApplication app = Application.Create ();
  226. Runnable<bool> runnable = new ()
  227. {
  228. Width = 10,
  229. Height = 10
  230. }; ;
  231. app.Begin (runnable);
  232. runnable.Add (label, otherView);
  233. label.SetFocus ();
  234. Assert.True (label.CanFocus);
  235. Assert.True (label.HasFocus);
  236. Assert.True (otherView.CanFocus);
  237. Assert.False (otherView.HasFocus);
  238. otherView.SetFocus ();
  239. Assert.True (otherView.HasFocus);
  240. // label can focus, so clicking on it set focus
  241. app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked });
  242. Assert.True (label.HasFocus);
  243. Assert.False (otherView.HasFocus);
  244. // click on view
  245. app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (0, 1), Flags = MouseFlags.Button1Clicked });
  246. Assert.False (label.HasFocus);
  247. Assert.True (otherView.HasFocus);
  248. }
  249. [Fact]
  250. public void With_Top_Margin_Without_Top_Border ()
  251. {
  252. IApplication app = Application.Create ();
  253. app.Init ("Fake");
  254. Runnable<bool> runnable = new ()
  255. {
  256. Width = 10,
  257. Height = 10
  258. }; ;
  259. app.Begin (runnable);
  260. var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
  261. label.Margin!.Thickness = new (0, 1, 0, 0);
  262. label.Border!.Thickness = new (1, 0, 1, 1);
  263. runnable.Add (label);
  264. app.LayoutAndDraw ();
  265. Assert.Equal (new (0, 0, 6, 3), label.Frame);
  266. Assert.Equal (new (0, 0, 4, 1), label.Viewport);
  267. DriverAssert.AssertDriverContentsWithFrameAre (
  268. @"
  269. │Test│
  270. └────┘",
  271. output,
  272. app.Driver
  273. );
  274. }
  275. [Fact]
  276. public void Without_Top_Border ()
  277. {
  278. IApplication app = Application.Create ();
  279. app.Init ("Fake");
  280. Runnable<bool> runnable = new ()
  281. {
  282. Width = 10,
  283. Height = 10
  284. }; ;
  285. app.Begin (runnable);
  286. var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
  287. label.Border!.Thickness = new (1, 0, 1, 1);
  288. runnable.Add (label);
  289. app.LayoutAndDraw ();
  290. Assert.Equal (new (0, 0, 6, 2), label.Frame);
  291. Assert.Equal (new (0, 0, 4, 1), label.Viewport);
  292. DriverAssert.AssertDriverContentsWithFrameAre (
  293. @"
  294. │Test│
  295. └────┘",
  296. output,
  297. app.Driver
  298. );
  299. }
  300. }