ButtonTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. using System.ComponentModel;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class ButtonTests (ITestOutputHelper output)
  5. {
  6. // Test that Title and Text are the same
  7. [Fact]
  8. public void Text_Mirrors_Title ()
  9. {
  10. var view = new Button ();
  11. view.Title = "Hello";
  12. Assert.Equal ("Hello", view.Title);
  13. Assert.Equal ("Hello", view.TitleTextFormatter.Text);
  14. Assert.Equal ("Hello", view.Text);
  15. Assert.Equal ($"{CM.Glyphs.LeftBracket} Hello {CM.Glyphs.RightBracket}", view.TextFormatter.Text);
  16. view.Dispose ();
  17. }
  18. [Fact]
  19. public void Title_Mirrors_Text ()
  20. {
  21. var view = new Button ();
  22. view.Text = "Hello";
  23. Assert.Equal ("Hello", view.Text);
  24. Assert.Equal ($"{CM.Glyphs.LeftBracket} Hello {CM.Glyphs.RightBracket}", view.TextFormatter.Text);
  25. Assert.Equal ("Hello", view.Title);
  26. Assert.Equal ("Hello", view.TitleTextFormatter.Text);
  27. view.Dispose ();
  28. }
  29. [Theory]
  30. [InlineData ("01234", 0, 0, 0, 0)]
  31. [InlineData ("01234", 1, 0, 1, 0)]
  32. [InlineData ("01234", 0, 1, 0, 1)]
  33. [InlineData ("01234", 1, 1, 1, 1)]
  34. [InlineData ("01234", 10, 1, 10, 1)]
  35. [InlineData ("01234", 10, 3, 10, 3)]
  36. [InlineData ("0_1234", 0, 0, 0, 0)]
  37. [InlineData ("0_1234", 1, 0, 1, 0)]
  38. [InlineData ("0_1234", 0, 1, 0, 1)]
  39. [InlineData ("0_1234", 1, 1, 1, 1)]
  40. [InlineData ("0_1234", 10, 1, 10, 1)]
  41. [InlineData ("0_12你", 10, 3, 10, 3)]
  42. [InlineData ("0_12你", 0, 0, 0, 0)]
  43. [InlineData ("0_12你", 1, 0, 1, 0)]
  44. [InlineData ("0_12你", 0, 1, 0, 1)]
  45. [InlineData ("0_12你", 1, 1, 1, 1)]
  46. [InlineData ("0_12你", 10, 1, 10, 1)]
  47. public void Button_AbsoluteSize_Text (string text, int width, int height, int expectedWidth, int expectedHeight)
  48. {
  49. var btn1 = new Button
  50. {
  51. Text = text,
  52. Width = width,
  53. Height = height,
  54. };
  55. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.Frame.Size);
  56. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.Viewport.Size);
  57. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.GetContentSize ());
  58. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.TextFormatter.Size);
  59. btn1.Dispose ();
  60. }
  61. [Theory]
  62. [InlineData (0, 0, 0, 0)]
  63. [InlineData (1, 0, 1, 0)]
  64. [InlineData (0, 1, 0, 1)]
  65. [InlineData (1, 1, 1, 1)]
  66. [InlineData (10, 1, 10, 1)]
  67. [InlineData (10, 3, 10, 3)]
  68. public void Button_AbsoluteSize_DefaultText (int width, int height, int expectedWidth, int expectedHeight)
  69. {
  70. var btn1 = new Button
  71. {
  72. Width = width,
  73. Height = height,
  74. };
  75. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.Frame.Size);
  76. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.Viewport.Size);
  77. Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.TextFormatter.Size);
  78. btn1.Dispose ();
  79. }
  80. [Fact]
  81. public void Button_HotKeyChanged_EventFires ()
  82. {
  83. var btn = new Button { Text = "_Yar" };
  84. object sender = null;
  85. KeyChangedEventArgs args = null;
  86. btn.HotKeyChanged += (s, e) =>
  87. {
  88. sender = s;
  89. args = e;
  90. };
  91. btn.HotKeyChanged += (s, e) =>
  92. {
  93. sender = s;
  94. args = e;
  95. };
  96. btn.HotKey = KeyCode.R;
  97. Assert.Same (btn, sender);
  98. Assert.Equal (KeyCode.Y, args.OldKey);
  99. Assert.Equal (KeyCode.R, args.NewKey);
  100. btn.HotKey = KeyCode.R;
  101. Assert.Same (btn, sender);
  102. Assert.Equal (KeyCode.Y, args.OldKey);
  103. Assert.Equal (KeyCode.R, args.NewKey);
  104. btn.Dispose ();
  105. }
  106. [Fact]
  107. public void Button_HotKeyChanged_EventFires_WithNone ()
  108. {
  109. var btn = new Button ();
  110. object sender = null;
  111. KeyChangedEventArgs args = null;
  112. btn.HotKeyChanged += (s, e) =>
  113. {
  114. sender = s;
  115. args = e;
  116. };
  117. btn.HotKey = KeyCode.R;
  118. Assert.Same (btn, sender);
  119. Assert.Equal (KeyCode.Null, args.OldKey);
  120. Assert.Equal (KeyCode.R, args.NewKey);
  121. btn.Dispose ();
  122. }
  123. [Fact]
  124. [SetupFakeDriver]
  125. public void Constructors_Defaults ()
  126. {
  127. var btn = new Button ();
  128. Assert.Equal (string.Empty, btn.Text);
  129. btn.BeginInit ();
  130. btn.EndInit ();
  131. btn.SetRelativeLayout (new (100, 100));
  132. Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  133. Assert.False (btn.IsDefault);
  134. Assert.Equal (Alignment.Center, btn.TextAlignment);
  135. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  136. Assert.True (btn.CanFocus);
  137. Assert.Equal (new (0, 0, 4, 1), btn.Viewport);
  138. Assert.Equal (new (0, 0, 4, 1), btn.Frame);
  139. Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  140. Assert.False (btn.IsDefault);
  141. Assert.Equal (Alignment.Center, btn.TextAlignment);
  142. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  143. Assert.True (btn.CanFocus);
  144. Assert.Equal (new (0, 0, 4, 1), btn.Viewport);
  145. Assert.Equal (new (0, 0, 4, 1), btn.Frame);
  146. Assert.Equal (string.Empty, btn.Title);
  147. Assert.Equal (KeyCode.Null, btn.HotKey);
  148. btn.Draw ();
  149. var expected = @$"
  150. {CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}
  151. ";
  152. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  153. btn.Dispose ();
  154. btn = new () { Text = "_Test", IsDefault = true };
  155. Assert.Equal (new (10, 1), btn.TextFormatter.Size);
  156. btn.BeginInit ();
  157. btn.EndInit ();
  158. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  159. Assert.Equal (Key.T, btn.HotKey);
  160. Assert.Equal ("_Test", btn.Text);
  161. Assert.Equal (
  162. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}",
  163. btn.TextFormatter.Format ()
  164. );
  165. Assert.True (btn.IsDefault);
  166. Assert.Equal (Alignment.Center, btn.TextAlignment);
  167. Assert.True (btn.CanFocus);
  168. btn.SetRelativeLayout (new (100, 100));
  169. // 0123456789012345678901234567890123456789
  170. // [* Test *]
  171. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  172. Assert.Equal (10, btn.TextFormatter.Format ().Length);
  173. Assert.Equal (new (10, 1), btn.TextFormatter.Size);
  174. Assert.Equal (new (10, 1), btn.GetContentSize ());
  175. Assert.Equal (new (0, 0, 10, 1), btn.Viewport);
  176. Assert.Equal (new (0, 0, 10, 1), btn.Frame);
  177. Assert.Equal (KeyCode.T, btn.HotKey);
  178. btn.Dispose ();
  179. btn = new () { X = 1, Y = 2, Text = "_abc", IsDefault = true };
  180. btn.BeginInit ();
  181. btn.EndInit ();
  182. Assert.Equal ("_abc", btn.Text);
  183. Assert.Equal (Key.A, btn.HotKey);
  184. Assert.Equal (
  185. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}",
  186. btn.TextFormatter.Format ()
  187. );
  188. Assert.True (btn.IsDefault);
  189. Assert.Equal (Alignment.Center, btn.TextAlignment);
  190. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  191. Assert.True (btn.CanFocus);
  192. Application.Driver.ClearContents ();
  193. btn.Draw ();
  194. expected = @$"
  195. {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}
  196. ";
  197. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  198. Assert.Equal (new (0, 0, 9, 1), btn.Viewport);
  199. Assert.Equal (new (1, 2, 9, 1), btn.Frame);
  200. btn.Dispose ();
  201. }
  202. [Fact]
  203. public void HotKeyChange_Works ()
  204. {
  205. var clicked = false;
  206. var btn = new Button { Text = "_Test" };
  207. btn.Accept += (s, e) => clicked = true;
  208. Assert.Equal (KeyCode.T, btn.HotKey);
  209. Assert.True (btn.NewKeyDownEvent (Key.T));
  210. Assert.True (clicked);
  211. clicked = false;
  212. Assert.True (btn.NewKeyDownEvent (Key.T.WithAlt));
  213. Assert.True (clicked);
  214. clicked = false;
  215. btn.HotKey = KeyCode.E;
  216. Assert.True (btn.NewKeyDownEvent (Key.E.WithAlt));
  217. Assert.True (clicked);
  218. }
  219. /// <summary>
  220. /// This test demonstrates how to change the activation key for Button as described in the README.md keyboard
  221. /// handling section
  222. /// </summary>
  223. [Fact]
  224. [AutoInitShutdown]
  225. public void KeyBindingExample ()
  226. {
  227. var pressed = 0;
  228. var btn = new Button { Text = "Press Me" };
  229. btn.Accept += (s, e) => pressed++;
  230. // The Button class supports the Default and Accept command
  231. Assert.Contains (Command.HotKey, btn.GetSupportedCommands ());
  232. Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
  233. var top = new Toplevel ();
  234. top.Add (btn);
  235. Application.Begin (top);
  236. // default keybinding is Space which results in keypress
  237. Application.OnKeyDown (new ((KeyCode)' '));
  238. Assert.Equal (1, pressed);
  239. // remove the default keybinding (Space)
  240. btn.KeyBindings.Clear (Command.HotKey);
  241. btn.KeyBindings.Clear (Command.Accept);
  242. // After clearing the default keystroke the Space button no longer does anything for the Button
  243. Application.OnKeyDown (new ((KeyCode)' '));
  244. Assert.Equal (1, pressed);
  245. // Set a new binding of b for the click (Accept) event
  246. btn.KeyBindings.Add (Key.B, Command.HotKey);
  247. btn.KeyBindings.Add (Key.B, Command.Accept);
  248. // now pressing B should call the button click event
  249. Application.OnKeyDown (Key.B);
  250. Assert.Equal (2, pressed);
  251. // now pressing Shift-B should NOT call the button click event
  252. Application.OnKeyDown (Key.B.WithShift);
  253. Assert.Equal (2, pressed);
  254. // now pressing Alt-B should NOT call the button click event
  255. Application.OnKeyDown (Key.B.WithAlt);
  256. Assert.Equal (2, pressed);
  257. // now pressing Shift-Alt-B should NOT call the button click event
  258. Application.OnKeyDown (Key.B.WithAlt.WithShift);
  259. Assert.Equal (2, pressed);
  260. top.Dispose ();
  261. }
  262. [Fact]
  263. [AutoInitShutdown]
  264. public void KeyBindings_Command ()
  265. {
  266. var clicked = false;
  267. var btn = new Button { Text = "_Test" };
  268. btn.Accept += (s, e) => clicked = true;
  269. var top = new Toplevel ();
  270. top.Add (btn);
  271. Application.Begin (top);
  272. // Hot key. Both alone and with alt
  273. Assert.Equal (KeyCode.T, btn.HotKey);
  274. Assert.True (btn.NewKeyDownEvent (Key.T));
  275. Assert.True (clicked);
  276. clicked = false;
  277. Assert.True (btn.NewKeyDownEvent (Key.T.WithAlt));
  278. Assert.True (clicked);
  279. clicked = false;
  280. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  281. Assert.True (clicked);
  282. clicked = false;
  283. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  284. Assert.True (clicked);
  285. clicked = false;
  286. // IsDefault = false
  287. // Space and Enter should work
  288. Assert.False (btn.IsDefault);
  289. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  290. Assert.True (clicked);
  291. clicked = false;
  292. // IsDefault = true
  293. // Space and Enter should work
  294. btn.IsDefault = true;
  295. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  296. Assert.True (clicked);
  297. clicked = false;
  298. // Toplevel does not handle Enter, so it should get passed on to button
  299. Assert.True (Application.Top.NewKeyDownEvent (Key.Enter));
  300. Assert.True (clicked);
  301. clicked = false;
  302. // Direct
  303. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  304. Assert.True (clicked);
  305. clicked = false;
  306. Assert.True (btn.NewKeyDownEvent (Key.Space));
  307. Assert.True (clicked);
  308. clicked = false;
  309. Assert.True (btn.NewKeyDownEvent (new ((KeyCode)'T')));
  310. Assert.True (clicked);
  311. clicked = false;
  312. // Change hotkey:
  313. btn.Text = "Te_st";
  314. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  315. Assert.True (clicked);
  316. clicked = false;
  317. top.Dispose ();
  318. }
  319. [Fact]
  320. public void HotKey_Command_Accepts ()
  321. {
  322. var button = new Button ();
  323. var accepted = false;
  324. button.Accept += ButtonOnAccept;
  325. button.InvokeCommand (Command.HotKey);
  326. Assert.True (accepted);
  327. button.Dispose ();
  328. return;
  329. void ButtonOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  330. }
  331. [Fact]
  332. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  333. {
  334. var button = new Button ();
  335. var acceptInvoked = false;
  336. button.Accept += ButtonAccept;
  337. bool? ret = button.InvokeCommand (Command.Accept);
  338. Assert.True (ret);
  339. Assert.True (acceptInvoked);
  340. button.Dispose ();
  341. return;
  342. void ButtonAccept (object sender, CancelEventArgs e)
  343. {
  344. acceptInvoked = true;
  345. e.Cancel = true;
  346. }
  347. }
  348. [Fact]
  349. public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
  350. {
  351. var super = new View ();
  352. var btn = new Button { Text = "_Test" };
  353. super.Add (btn);
  354. super.BeginInit ();
  355. super.EndInit ();
  356. Assert.Equal ("_Test", btn.Text);
  357. Assert.Equal (KeyCode.T, btn.HotKey);
  358. btn.Text = string.Empty;
  359. Assert.Equal ("", btn.Text);
  360. Assert.Equal (KeyCode.Null, btn.HotKey);
  361. btn.Text = string.Empty;
  362. Assert.Equal ("", btn.Text);
  363. Assert.Equal (KeyCode.Null, btn.HotKey);
  364. btn.Text = "Te_st";
  365. Assert.Equal ("Te_st", btn.Text);
  366. Assert.Equal (KeyCode.S, btn.HotKey);
  367. super.Dispose ();
  368. }
  369. [Fact]
  370. public void TestAssignTextToButton ()
  371. {
  372. View b = new Button { Text = "heya" };
  373. Assert.Equal ("heya", b.Text);
  374. Assert.Contains ("heya", b.TextFormatter.Text);
  375. b.Text = "heyb";
  376. Assert.Equal ("heyb", b.Text);
  377. Assert.Contains ("heyb", b.TextFormatter.Text);
  378. // with cast
  379. Assert.Equal ("heyb", ((Button)b).Text);
  380. b.Dispose ();
  381. }
  382. [Fact]
  383. [AutoInitShutdown]
  384. public void Update_Only_On_Or_After_Initialize ()
  385. {
  386. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  387. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  388. win.Add (btn);
  389. var top = new Toplevel ();
  390. top.Add (win);
  391. Assert.False (btn.IsInitialized);
  392. Application.Begin (top);
  393. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  394. Assert.True (btn.IsInitialized);
  395. Assert.Equal ("Say Hello 你", btn.Text);
  396. Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  397. Assert.Equal (new (0, 0, 16, 1), btn.Viewport);
  398. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  399. var expected = @$"
  400. ┌────────────────────────────┐
  401. │ │
  402. │ {btnTxt} │
  403. │ │
  404. └────────────────────────────┘
  405. ";
  406. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  407. Assert.Equal (new (0, 0, 30, 5), pos);
  408. top.Dispose ();
  409. }
  410. [Fact]
  411. [AutoInitShutdown]
  412. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  413. {
  414. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  415. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  416. win.Add (btn);
  417. var top = new Toplevel ();
  418. top.Add (win);
  419. Assert.False (btn.IsInitialized);
  420. Application.Begin (top);
  421. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  422. Assert.True (btn.IsInitialized);
  423. Assert.Equal ("Say Hello 你", btn.Text);
  424. Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  425. Assert.Equal (new (0, 0, 16, 1), btn.Viewport);
  426. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  427. var expected = @$"
  428. ┌────────────────────────────┐
  429. │ │
  430. │ {btnTxt} │
  431. │ │
  432. └────────────────────────────┘
  433. ";
  434. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  435. Assert.Equal (new (0, 0, 30, 5), pos);
  436. top.Dispose ();
  437. }
  438. [Theory]
  439. [InlineData (MouseFlags.Button1Pressed, MouseFlags.Button1Released, MouseFlags.Button1Clicked)]
  440. [InlineData (MouseFlags.Button2Pressed, MouseFlags.Button2Released, MouseFlags.Button2Clicked)]
  441. [InlineData (MouseFlags.Button3Pressed, MouseFlags.Button3Released, MouseFlags.Button3Clicked)]
  442. [InlineData (MouseFlags.Button4Pressed, MouseFlags.Button4Released, MouseFlags.Button4Clicked)]
  443. public void WantContinuousButtonPressed_True_ButtonClick_Accepts (MouseFlags pressed, MouseFlags released, MouseFlags clicked)
  444. {
  445. var me = new MouseEvent ();
  446. var button = new Button ()
  447. {
  448. Width = 1,
  449. Height = 1,
  450. WantContinuousButtonPressed = true
  451. };
  452. var acceptCount = 0;
  453. button.Accept += (s, e) => acceptCount++;
  454. me.Flags = pressed;
  455. button.NewMouseEvent (me);
  456. Assert.Equal (1, acceptCount);
  457. me.Flags = released;
  458. button.NewMouseEvent (me);
  459. Assert.Equal (1, acceptCount);
  460. me.Flags = clicked;
  461. button.NewMouseEvent (me);
  462. Assert.Equal (1, acceptCount);
  463. button.Dispose ();
  464. }
  465. [Theory]
  466. [InlineData (MouseFlags.Button1Pressed, MouseFlags.Button1Released)]
  467. [InlineData (MouseFlags.Button2Pressed, MouseFlags.Button2Released)]
  468. [InlineData (MouseFlags.Button3Pressed, MouseFlags.Button3Released)]
  469. [InlineData (MouseFlags.Button4Pressed, MouseFlags.Button4Released)]
  470. public void WantContinuousButtonPressed_True_ButtonPressRelease_Accepts (MouseFlags pressed, MouseFlags released)
  471. {
  472. var me = new MouseEvent ();
  473. var button = new Button ()
  474. {
  475. Width = 1,
  476. Height = 1,
  477. WantContinuousButtonPressed = true
  478. };
  479. var acceptCount = 0;
  480. button.Accept += (s, e) => acceptCount++;
  481. me.Flags = pressed;
  482. button.NewMouseEvent (me);
  483. Assert.Equal (1, acceptCount);
  484. me.Flags = released;
  485. button.NewMouseEvent (me);
  486. Assert.Equal (1, acceptCount);
  487. button.Dispose ();
  488. }
  489. }