ButtonTests.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. // BUGBUG: This test is NOT a unit test and needs to be broken apart into
  30. // more specific tests (e.g. it tests Checkbox as well as Button)
  31. [Fact]
  32. [AutoInitShutdown]
  33. public void AutoSize_False_With_Fixed_Width ()
  34. {
  35. var tab = new View ();
  36. var lblWidth = 8;
  37. var view = new View
  38. {
  39. Y = 1,
  40. Width = lblWidth,
  41. Height = 1,
  42. TextAlignment = TextAlignment.Right,
  43. Text = "Find:"
  44. };
  45. tab.Add (view);
  46. var txtToFind = new TextField
  47. {
  48. X = Pos.Right (view) + 1, Y = Pos.Top (view), Width = 20, Text = "Testing buttons."
  49. };
  50. tab.Add (txtToFind);
  51. var btnFindNext = new Button
  52. {
  53. AutoSize = false,
  54. X = Pos.Right (txtToFind) + 1,
  55. Y = Pos.Top (view),
  56. Width = 20,
  57. Enabled = !string.IsNullOrEmpty (txtToFind.Text),
  58. TextAlignment = TextAlignment.Centered,
  59. IsDefault = true,
  60. Text = "Find _Next"
  61. };
  62. tab.Add (btnFindNext);
  63. var btnFindPrevious = new Button
  64. {
  65. AutoSize = false,
  66. X = Pos.Right (txtToFind) + 1,
  67. Y = Pos.Top (btnFindNext) + 1,
  68. Width = 20,
  69. Enabled = !string.IsNullOrEmpty (txtToFind.Text),
  70. TextAlignment = TextAlignment.Centered,
  71. Text = "Find _Previous"
  72. };
  73. tab.Add (btnFindPrevious);
  74. var btnCancel = new Button
  75. {
  76. AutoSize = false,
  77. X = Pos.Right (txtToFind) + 1,
  78. Y = Pos.Top (btnFindPrevious) + 2,
  79. Width = 20,
  80. TextAlignment = TextAlignment.Centered,
  81. Text = "Cancel"
  82. };
  83. tab.Add (btnCancel);
  84. var ckbMatchCase = new CheckBox { X = 0, Y = Pos.Top (txtToFind) + 2, Checked = true, Text = "Match c_ase" };
  85. tab.Add (ckbMatchCase);
  86. var ckbMatchWholeWord = new CheckBox
  87. {
  88. X = 0, Y = Pos.Top (ckbMatchCase) + 1, Checked = false, Text = "Match _whole word"
  89. };
  90. tab.Add (ckbMatchWholeWord);
  91. var tabView = new TabView { Width = Dim.Fill (), Height = Dim.Fill () };
  92. tabView.AddTab (new() { DisplayText = "Find", View = tab }, true);
  93. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  94. tab.Width = view.Width + txtToFind.Width + btnFindNext.Width + 2;
  95. tab.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
  96. win.Add (tabView);
  97. var top = new Toplevel ();
  98. top.Add (win);
  99. Application.Begin (top);
  100. ((FakeDriver)Application.Driver).SetBufferSize (54, 11);
  101. Assert.Equal (new (0, 0, 54, 11), win.Frame);
  102. Assert.Equal (new (0, 0, 52, 9), tabView.Frame);
  103. Assert.Equal (new (0, 0, 50, 7), tab.Frame);
  104. Assert.Equal (new (0, 1, 8, 1), view.Frame);
  105. Assert.Equal (new (9, 1, 20, 1), txtToFind.Frame);
  106. Assert.Equal (0, txtToFind.ScrollOffset);
  107. Assert.Equal (16, txtToFind.CursorPosition);
  108. Assert.Equal (new (30, 1, 20, 1), btnFindNext.Frame);
  109. Assert.Equal (new (30, 2, 20, 1), btnFindPrevious.Frame);
  110. Assert.Equal (new (30, 4, 20, 1), btnCancel.Frame);
  111. // Assert.Equal (new (0, 3, 12, 1), ckbMatchCase.Frame);
  112. // Assert.Equal (new (0, 4, 18, 1), ckbMatchWholeWord.Frame);
  113. var btn1 =
  114. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Find Next {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}";
  115. var btn2 = $"{CM.Glyphs.LeftBracket} Find Previous {CM.Glyphs.RightBracket}";
  116. var btn3 = $"{CM.Glyphs.LeftBracket} Cancel {CM.Glyphs.RightBracket}";
  117. var expected = @$"
  118. ┌────────────────────────────────────────────────────┐
  119. │╭────╮ │
  120. ││Find│ │
  121. ││ ╰─────────────────────────────────────────────╮│
  122. ││ ││
  123. ││ Find: Testing buttons. {btn1} ││
  124. ││ {btn2} ││
  125. ││{CM.Glyphs.Checked} Match case ││
  126. ││{CM.Glyphs.UnChecked} Match whole word {btn3} ││
  127. │└──────────────────────────────────────────────────┘│
  128. └────────────────────────────────────────────────────┘
  129. ";
  130. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  131. view.Dispose ();
  132. }
  133. [Fact]
  134. [AutoInitShutdown]
  135. public void AutoSize_Stays_True_AnchorEnd ()
  136. {
  137. var btn = new Button { Y = Pos.Center (), Text = "Say Hello 你", AutoSize = true };
  138. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  139. btn.X = Pos.AnchorEnd () - Pos.Function (() => btn.TextFormatter.Text.GetColumns ());
  140. btn.X = Pos.AnchorEnd () - Pos.Function (() => btn.TextFormatter.Text.GetColumns ());
  141. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  142. win.Add (btn);
  143. var top = new Toplevel ();
  144. top.Add (win);
  145. Assert.True (btn.AutoSize);
  146. Application.Begin (top);
  147. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  148. var expected = @$"
  149. ┌────────────────────────────┐
  150. │ │
  151. │ {btnTxt}│
  152. │ │
  153. └────────────────────────────┘
  154. ";
  155. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  156. Assert.True (btn.AutoSize);
  157. btn.Text = "Say Hello 你 changed";
  158. btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  159. Assert.True (btn.AutoSize);
  160. Application.Refresh ();
  161. expected = @$"
  162. ┌────────────────────────────┐
  163. │ │
  164. │ {btnTxt}│
  165. │ │
  166. └────────────────────────────┘
  167. ";
  168. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  169. top.Dispose ();
  170. }
  171. [Fact]
  172. [AutoInitShutdown]
  173. public void AutoSize_Stays_True_Center ()
  174. {
  175. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  176. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  177. win.Add (btn);
  178. var top = new Toplevel ();
  179. top.Add (win);
  180. Assert.True (btn.AutoSize);
  181. Application.Begin (top);
  182. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  183. var expected = @$"
  184. ┌────────────────────────────┐
  185. │ │
  186. │ {CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket} │
  187. │ │
  188. └────────────────────────────┘
  189. ";
  190. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  191. Assert.True (btn.AutoSize);
  192. btn.Text = "Say Hello 你 changed";
  193. Assert.True (btn.AutoSize);
  194. Application.Refresh ();
  195. expected = @$"
  196. ┌────────────────────────────┐
  197. │ │
  198. │ {CM.Glyphs.LeftBracket} Say Hello 你 changed {CM.Glyphs.RightBracket} │
  199. │ │
  200. └────────────────────────────┘
  201. ";
  202. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  203. top.Dispose ();
  204. }
  205. [Fact]
  206. [AutoInitShutdown]
  207. public void AutoSize_Stays_True_With_EmptyText ()
  208. {
  209. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), AutoSize = true };
  210. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  211. win.Add (btn);
  212. var top = new Toplevel ();
  213. top.Add (win);
  214. Assert.True (btn.AutoSize);
  215. btn.Text = "Say Hello 你";
  216. Assert.True (btn.AutoSize);
  217. Application.Begin (top);
  218. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  219. var expected = @$"
  220. ┌────────────────────────────┐
  221. │ │
  222. │ {CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket} │
  223. │ │
  224. └────────────────────────────┘
  225. ";
  226. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  227. top.Dispose ();
  228. }
  229. [Fact]
  230. [SetupFakeDriver]
  231. public void Button_AutoSize_False_With_Fixed_Width ()
  232. {
  233. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  234. var top = new View { Width = 20, Height = 5 };
  235. var btn1 = new Button
  236. {
  237. AutoSize = false,
  238. X = Pos.Center (),
  239. Y = Pos.Center (),
  240. Width = 16,
  241. Height = 1,
  242. Text = "Open me!"
  243. };
  244. var btn2 = new Button
  245. {
  246. AutoSize = false,
  247. X = Pos.Center (),
  248. Y = Pos.Center () + 1,
  249. Width = 16,
  250. Height = 1,
  251. Text = "Close me!"
  252. };
  253. top.Add (btn1, btn2);
  254. top.BeginInit ();
  255. top.EndInit ();
  256. top.Draw ();
  257. Assert.Equal ("{Width=16, Height=1}", btn1.TextFormatter.Size.ToString ());
  258. Assert.Equal ("{Width=16, Height=1}", btn2.TextFormatter.Size.ToString ());
  259. TestHelpers.AssertDriverContentsWithFrameAre (
  260. @$"
  261. {CM.Glyphs.LeftBracket} {btn1.Text} {CM.Glyphs.RightBracket}
  262. {CM.Glyphs.LeftBracket} {btn2.Text} {CM.Glyphs.RightBracket}",
  263. output
  264. );
  265. top.Dispose ();
  266. }
  267. [Fact]
  268. [AutoInitShutdown]
  269. public void Button_HotKeyChanged_EventFires ()
  270. {
  271. var btn = new Button { Text = "_Yar" };
  272. object sender = null;
  273. KeyChangedEventArgs args = null;
  274. btn.HotKeyChanged += (s, e) =>
  275. {
  276. sender = s;
  277. args = e;
  278. };
  279. btn.HotKeyChanged += (s, e) =>
  280. {
  281. sender = s;
  282. args = e;
  283. };
  284. btn.HotKey = KeyCode.R;
  285. Assert.Same (btn, sender);
  286. Assert.Equal (KeyCode.Y, args.OldKey);
  287. Assert.Equal (KeyCode.R, args.NewKey);
  288. btn.HotKey = KeyCode.R;
  289. Assert.Same (btn, sender);
  290. Assert.Equal (KeyCode.Y, args.OldKey);
  291. Assert.Equal (KeyCode.R, args.NewKey);
  292. btn.Dispose ();
  293. }
  294. [Fact]
  295. public void Button_HotKeyChanged_EventFires_WithNone ()
  296. {
  297. var btn = new Button ();
  298. object sender = null;
  299. KeyChangedEventArgs args = null;
  300. btn.HotKeyChanged += (s, e) =>
  301. {
  302. sender = s;
  303. args = e;
  304. };
  305. btn.HotKey = KeyCode.R;
  306. Assert.Same (btn, sender);
  307. Assert.Equal (KeyCode.Null, args.OldKey);
  308. Assert.Equal (KeyCode.R, args.NewKey);
  309. btn.Dispose ();
  310. }
  311. [Fact]
  312. [SetupFakeDriver]
  313. public void Constructors_Defaults ()
  314. {
  315. var btn = new Button ();
  316. Assert.Equal (string.Empty, btn.Text);
  317. btn.BeginInit ();
  318. btn.EndInit ();
  319. Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  320. Assert.False (btn.IsDefault);
  321. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  322. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  323. Assert.True (btn.CanFocus);
  324. Assert.Equal (new (0, 0, 4, 1), btn.Bounds);
  325. Assert.Equal (new (0, 0, 4, 1), btn.Frame);
  326. Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  327. Assert.False (btn.IsDefault);
  328. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  329. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  330. Assert.True (btn.CanFocus);
  331. Assert.Equal (new (0, 0, 4, 1), btn.Bounds);
  332. Assert.Equal (new (0, 0, 4, 1), btn.Frame);
  333. Assert.Equal (string.Empty, btn.Title);
  334. Assert.Equal (KeyCode.Null, btn.HotKey);
  335. btn.Draw ();
  336. var expected = @$"
  337. {CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}
  338. ";
  339. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  340. btn.Dispose ();
  341. btn = new() { Text = "_Test", IsDefault = true };
  342. btn.BeginInit ();
  343. btn.EndInit ();
  344. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  345. Assert.Equal (Key.T, btn.HotKey);
  346. Assert.Equal ("_Test", btn.Text);
  347. Assert.Equal (
  348. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}",
  349. btn.TextFormatter.Format ()
  350. );
  351. Assert.True (btn.IsDefault);
  352. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  353. Assert.True (btn.CanFocus);
  354. Assert.Equal (new (0, 0, 10, 1), btn.Bounds);
  355. Assert.Equal (new (0, 0, 10, 1), btn.Frame);
  356. Assert.Equal (KeyCode.T, btn.HotKey);
  357. btn.Dispose ();
  358. btn = new() { X = 1, Y = 2, Text = "_abc", IsDefault = true };
  359. btn.BeginInit ();
  360. btn.EndInit ();
  361. Assert.Equal ("_abc", btn.Text);
  362. Assert.Equal (Key.A, btn.HotKey);
  363. Assert.Equal (
  364. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}",
  365. btn.TextFormatter.Format ()
  366. );
  367. Assert.True (btn.IsDefault);
  368. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  369. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  370. Assert.True (btn.CanFocus);
  371. Application.Driver.ClearContents ();
  372. btn.Draw ();
  373. expected = @$"
  374. {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}
  375. ";
  376. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  377. Assert.Equal (new (0, 0, 9, 1), btn.Bounds);
  378. Assert.Equal (new (1, 2, 9, 1), btn.Frame);
  379. btn.Dispose ();
  380. }
  381. [Fact]
  382. [AutoInitShutdown]
  383. public void HotKeyChange_Works ()
  384. {
  385. var clicked = false;
  386. var btn = new Button { Text = "_Test" };
  387. btn.Accept += (s, e) => clicked = true;
  388. var top = new Toplevel ();
  389. top.Add (btn);
  390. Application.Begin (top);
  391. Assert.Equal (KeyCode.T, btn.HotKey);
  392. Assert.True (btn.NewKeyDownEvent (Key.T));
  393. Assert.True (clicked);
  394. clicked = false;
  395. Assert.True (btn.NewKeyDownEvent (Key.T.WithAlt));
  396. Assert.True (clicked);
  397. clicked = false;
  398. btn.HotKey = KeyCode.E;
  399. Assert.True (btn.NewKeyDownEvent (Key.E.WithAlt));
  400. Assert.True (clicked);
  401. top.Dispose ();
  402. }
  403. /// <summary>
  404. /// This test demonstrates how to change the activation key for Button as described in the README.md keyboard
  405. /// handling section
  406. /// </summary>
  407. [Fact]
  408. [AutoInitShutdown]
  409. public void KeyBindingExample ()
  410. {
  411. var pressed = 0;
  412. var btn = new Button { Text = "Press Me" };
  413. btn.Accept += (s, e) => pressed++;
  414. // The Button class supports the Default and Accept command
  415. Assert.Contains (Command.HotKey, btn.GetSupportedCommands ());
  416. Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
  417. var top = new Toplevel ();
  418. top.Add (btn);
  419. Application.Begin (top);
  420. // default keybinding is Space which results in keypress
  421. Application.OnKeyDown (new ((KeyCode)' '));
  422. Assert.Equal (1, pressed);
  423. // remove the default keybinding (Space)
  424. btn.KeyBindings.Clear (Command.HotKey);
  425. btn.KeyBindings.Clear (Command.Accept);
  426. // After clearing the default keystroke the Space button no longer does anything for the Button
  427. Application.OnKeyDown (new ((KeyCode)' '));
  428. Assert.Equal (1, pressed);
  429. // Set a new binding of b for the click (Accept) event
  430. btn.KeyBindings.Add (Key.B, Command.HotKey);
  431. btn.KeyBindings.Add (Key.B, Command.Accept);
  432. // now pressing B should call the button click event
  433. Application.OnKeyDown (Key.B);
  434. Assert.Equal (2, pressed);
  435. // now pressing Shift-B should NOT call the button click event
  436. Application.OnKeyDown (Key.B.WithShift);
  437. Assert.Equal (2, pressed);
  438. // now pressing Alt-B should NOT call the button click event
  439. Application.OnKeyDown (Key.B.WithAlt);
  440. Assert.Equal (2, pressed);
  441. // now pressing Shift-Alt-B should NOT call the button click event
  442. Application.OnKeyDown (Key.B.WithAlt.WithShift);
  443. Assert.Equal (2, pressed);
  444. top.Dispose ();
  445. }
  446. [Fact]
  447. [AutoInitShutdown]
  448. public void KeyBindings_Command ()
  449. {
  450. var clicked = false;
  451. var btn = new Button { Text = "_Test" };
  452. btn.Accept += (s, e) => clicked = true;
  453. var top = new Toplevel ();
  454. top.Add (btn);
  455. Application.Begin (top);
  456. // Hot key. Both alone and with alt
  457. Assert.Equal (KeyCode.T, btn.HotKey);
  458. Assert.True (btn.NewKeyDownEvent (Key.T));
  459. Assert.True (clicked);
  460. clicked = false;
  461. Assert.True (btn.NewKeyDownEvent (Key.T.WithAlt));
  462. Assert.True (clicked);
  463. clicked = false;
  464. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  465. Assert.True (clicked);
  466. clicked = false;
  467. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  468. Assert.True (clicked);
  469. clicked = false;
  470. // IsDefault = false
  471. // Space and Enter should work
  472. Assert.False (btn.IsDefault);
  473. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  474. Assert.True (clicked);
  475. clicked = false;
  476. // IsDefault = true
  477. // Space and Enter should work
  478. btn.IsDefault = true;
  479. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  480. Assert.True (clicked);
  481. clicked = false;
  482. // Toplevel does not handle Enter, so it should get passed on to button
  483. Assert.True (Application.Top.NewKeyDownEvent (Key.Enter));
  484. Assert.True (clicked);
  485. clicked = false;
  486. // Direct
  487. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  488. Assert.True (clicked);
  489. clicked = false;
  490. Assert.True (btn.NewKeyDownEvent (Key.Space));
  491. Assert.True (clicked);
  492. clicked = false;
  493. Assert.True (btn.NewKeyDownEvent (new ((KeyCode)'T')));
  494. Assert.True (clicked);
  495. clicked = false;
  496. // Change hotkey:
  497. btn.Text = "Te_st";
  498. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  499. Assert.True (clicked);
  500. clicked = false;
  501. top.Dispose ();
  502. }
  503. [Fact]
  504. public void HotKey_Command_Accepts ()
  505. {
  506. var button = new Button ();
  507. var accepted = false;
  508. button.Accept += ButtonOnAccept;
  509. button.InvokeCommand (Command.HotKey);
  510. Assert.True (accepted);
  511. button.Dispose ();
  512. return;
  513. void ButtonOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  514. }
  515. [Fact]
  516. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  517. {
  518. var button = new Button ();
  519. var acceptInvoked = false;
  520. button.Accept += ButtonAccept;
  521. bool? ret = button.InvokeCommand (Command.Accept);
  522. Assert.True (ret);
  523. Assert.True (acceptInvoked);
  524. button.Dispose ();
  525. return;
  526. void ButtonAccept (object sender, CancelEventArgs e)
  527. {
  528. acceptInvoked = true;
  529. e.Cancel = true;
  530. }
  531. }
  532. [Fact]
  533. public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
  534. {
  535. var super = new View ();
  536. var btn = new Button { Text = "_Test" };
  537. super.Add (btn);
  538. super.BeginInit ();
  539. super.EndInit ();
  540. Assert.Equal ("_Test", btn.Text);
  541. Assert.Equal (KeyCode.T, btn.HotKey);
  542. btn.Text = string.Empty;
  543. Assert.Equal ("", btn.Text);
  544. Assert.Equal (KeyCode.Null, btn.HotKey);
  545. btn.Text = string.Empty;
  546. Assert.Equal ("", btn.Text);
  547. Assert.Equal (KeyCode.Null, btn.HotKey);
  548. btn.Text = "Te_st";
  549. Assert.Equal ("Te_st", btn.Text);
  550. Assert.Equal (KeyCode.S, btn.HotKey);
  551. super.Dispose ();
  552. }
  553. [Fact]
  554. public void TestAssignTextToButton ()
  555. {
  556. View b = new Button { Text = "heya" };
  557. Assert.Equal ("heya", b.Text);
  558. Assert.Contains ("heya", b.TextFormatter.Text);
  559. b.Text = "heyb";
  560. Assert.Equal ("heyb", b.Text);
  561. Assert.Contains ("heyb", b.TextFormatter.Text);
  562. // with cast
  563. Assert.Equal ("heyb", ((Button)b).Text);
  564. b.Dispose ();
  565. }
  566. [Fact]
  567. [AutoInitShutdown]
  568. public void Update_Only_On_Or_After_Initialize ()
  569. {
  570. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  571. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  572. win.Add (btn);
  573. var top = new Toplevel ();
  574. top.Add (win);
  575. Assert.False (btn.IsInitialized);
  576. Application.Begin (top);
  577. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  578. Assert.True (btn.IsInitialized);
  579. Assert.Equal ("Say Hello 你", btn.Text);
  580. Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  581. Assert.Equal (new (0, 0, 16, 1), btn.Bounds);
  582. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  583. var expected = @$"
  584. ┌────────────────────────────┐
  585. │ │
  586. │ {btnTxt} │
  587. │ │
  588. └────────────────────────────┘
  589. ";
  590. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  591. Assert.Equal (new (0, 0, 30, 5), pos);
  592. top.Dispose ();
  593. }
  594. [Fact]
  595. [AutoInitShutdown]
  596. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  597. {
  598. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  599. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  600. win.Add (btn);
  601. var top = new Toplevel ();
  602. top.Add (win);
  603. Assert.False (btn.IsInitialized);
  604. Application.Begin (top);
  605. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  606. Assert.True (btn.IsInitialized);
  607. Assert.Equal ("Say Hello 你", btn.Text);
  608. Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  609. Assert.Equal (new (0, 0, 16, 1), btn.Bounds);
  610. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  611. var expected = @$"
  612. ┌────────────────────────────┐
  613. │ │
  614. │ {btnTxt} │
  615. │ │
  616. └────────────────────────────┘
  617. ";
  618. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  619. Assert.Equal (new (0, 0, 30, 5), pos);
  620. top.Dispose ();
  621. }
  622. }