ButtonTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 (0) - Pos.Function (() => btn.TextFormatter.Text.GetColumns ());
  140. btn.X = Pos.AnchorEnd (0) - 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_With_EmptyText ()
  174. {
  175. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), AutoSize = true };
  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. btn.Text = "Say Hello 你";
  182. Assert.True (btn.AutoSize);
  183. Application.Begin (top);
  184. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  185. var expected = @$"
  186. ┌────────────────────────────┐
  187. │ │
  188. │ {CM.Glyphs.LeftBracket} Say Hello 你 {CM.Glyphs.RightBracket} │
  189. │ │
  190. └────────────────────────────┘
  191. ";
  192. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  193. top.Dispose ();
  194. }
  195. [Fact]
  196. [SetupFakeDriver]
  197. public void Button_AutoSize_False_With_Fixed_Width ()
  198. {
  199. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  200. var top = new View { Width = 20, Height = 5 };
  201. var btn1 = new Button
  202. {
  203. AutoSize = false,
  204. X = Pos.Center (),
  205. Y = Pos.Center (),
  206. Width = 16,
  207. Height = 1,
  208. Text = "Open me!"
  209. };
  210. var btn2 = new Button
  211. {
  212. AutoSize = false,
  213. X = Pos.Center (),
  214. Y = Pos.Center () + 1,
  215. Width = 16,
  216. Height = 1,
  217. Text = "Close me!"
  218. };
  219. top.Add (btn1, btn2);
  220. top.BeginInit ();
  221. top.EndInit ();
  222. top.Draw ();
  223. Assert.Equal ("{Width=16, Height=1}", btn1.TextFormatter.Size.ToString ());
  224. Assert.Equal ("{Width=16, Height=1}", btn2.TextFormatter.Size.ToString ());
  225. TestHelpers.AssertDriverContentsWithFrameAre (
  226. @$"
  227. {CM.Glyphs.LeftBracket} {btn1.Text} {CM.Glyphs.RightBracket}
  228. {CM.Glyphs.LeftBracket} {btn2.Text} {CM.Glyphs.RightBracket}",
  229. output
  230. );
  231. top.Dispose ();
  232. }
  233. [Fact]
  234. [AutoInitShutdown]
  235. public void Button_HotKeyChanged_EventFires ()
  236. {
  237. var btn = new Button { Text = "_Yar" };
  238. object sender = null;
  239. KeyChangedEventArgs args = null;
  240. btn.HotKeyChanged += (s, e) =>
  241. {
  242. sender = s;
  243. args = e;
  244. };
  245. btn.HotKeyChanged += (s, e) =>
  246. {
  247. sender = s;
  248. args = e;
  249. };
  250. btn.HotKey = KeyCode.R;
  251. Assert.Same (btn, sender);
  252. Assert.Equal (KeyCode.Y, args.OldKey);
  253. Assert.Equal (KeyCode.R, args.NewKey);
  254. btn.HotKey = KeyCode.R;
  255. Assert.Same (btn, sender);
  256. Assert.Equal (KeyCode.Y, args.OldKey);
  257. Assert.Equal (KeyCode.R, args.NewKey);
  258. btn.Dispose ();
  259. }
  260. [Fact]
  261. public void Button_HotKeyChanged_EventFires_WithNone ()
  262. {
  263. var btn = new Button ();
  264. object sender = null;
  265. KeyChangedEventArgs args = null;
  266. btn.HotKeyChanged += (s, e) =>
  267. {
  268. sender = s;
  269. args = e;
  270. };
  271. btn.HotKey = KeyCode.R;
  272. Assert.Same (btn, sender);
  273. Assert.Equal (KeyCode.Null, args.OldKey);
  274. Assert.Equal (KeyCode.R, args.NewKey);
  275. btn.Dispose ();
  276. }
  277. [Fact]
  278. [SetupFakeDriver]
  279. public void Constructors_Defaults ()
  280. {
  281. var btn = new Button ();
  282. Assert.Equal (string.Empty, btn.Text);
  283. btn.BeginInit ();
  284. btn.EndInit ();
  285. btn.SetRelativeLayout(new (25,25));
  286. Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  287. Assert.False (btn.IsDefault);
  288. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  289. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  290. Assert.True (btn.CanFocus);
  291. Assert.Equal (new (0, 0, 4, 1), btn.Viewport);
  292. Assert.Equal (new (0, 0, 4, 1), btn.Frame);
  293. Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  294. Assert.False (btn.IsDefault);
  295. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  296. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  297. Assert.True (btn.CanFocus);
  298. Assert.Equal (new (0, 0, 4, 1), btn.Viewport);
  299. Assert.Equal (new (0, 0, 4, 1), btn.Frame);
  300. Assert.Equal (string.Empty, btn.Title);
  301. Assert.Equal (KeyCode.Null, btn.HotKey);
  302. btn.Draw ();
  303. var expected = @$"
  304. {CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}
  305. ";
  306. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  307. btn.Dispose ();
  308. btn = new () { Text = "_Test", IsDefault = true };
  309. btn.BeginInit ();
  310. btn.EndInit ();
  311. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  312. Assert.Equal (Key.T, btn.HotKey);
  313. Assert.Equal ("_Test", btn.Text);
  314. Assert.Equal (
  315. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}",
  316. btn.TextFormatter.Format ()
  317. );
  318. Assert.True (btn.IsDefault);
  319. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  320. Assert.True (btn.CanFocus);
  321. Assert.Equal (new (0, 0, 10, 1), btn.Viewport);
  322. Assert.Equal (new (0, 0, 10, 1), btn.Frame);
  323. Assert.Equal (KeyCode.T, btn.HotKey);
  324. btn.Dispose ();
  325. btn = new () { X = 1, Y = 2, Text = "_abc", IsDefault = true };
  326. btn.BeginInit ();
  327. btn.EndInit ();
  328. Assert.Equal ("_abc", btn.Text);
  329. Assert.Equal (Key.A, btn.HotKey);
  330. Assert.Equal (
  331. $"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}",
  332. btn.TextFormatter.Format ()
  333. );
  334. Assert.True (btn.IsDefault);
  335. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  336. Assert.Equal ('_', btn.HotKeySpecifier.Value);
  337. Assert.True (btn.CanFocus);
  338. Application.Driver.ClearContents ();
  339. btn.Draw ();
  340. expected = @$"
  341. {CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}
  342. ";
  343. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  344. Assert.Equal (new (0, 0, 9, 1), btn.Viewport);
  345. Assert.Equal (new (1, 2, 9, 1), btn.Frame);
  346. btn.Dispose ();
  347. }
  348. [Fact]
  349. [AutoInitShutdown]
  350. public void HotKeyChange_Works ()
  351. {
  352. var clicked = false;
  353. var btn = new Button { Text = "_Test" };
  354. btn.Accept += (s, e) => clicked = true;
  355. var top = new Toplevel ();
  356. top.Add (btn);
  357. Application.Begin (top);
  358. Assert.Equal (KeyCode.T, btn.HotKey);
  359. Assert.True (btn.NewKeyDownEvent (Key.T));
  360. Assert.True (clicked);
  361. clicked = false;
  362. Assert.True (btn.NewKeyDownEvent (Key.T.WithAlt));
  363. Assert.True (clicked);
  364. clicked = false;
  365. btn.HotKey = KeyCode.E;
  366. Assert.True (btn.NewKeyDownEvent (Key.E.WithAlt));
  367. Assert.True (clicked);
  368. top.Dispose ();
  369. }
  370. /// <summary>
  371. /// This test demonstrates how to change the activation key for Button as described in the README.md keyboard
  372. /// handling section
  373. /// </summary>
  374. [Fact]
  375. [AutoInitShutdown]
  376. public void KeyBindingExample ()
  377. {
  378. var pressed = 0;
  379. var btn = new Button { Text = "Press Me" };
  380. btn.Accept += (s, e) => pressed++;
  381. // The Button class supports the Default and Accept command
  382. Assert.Contains (Command.HotKey, btn.GetSupportedCommands ());
  383. Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
  384. var top = new Toplevel ();
  385. top.Add (btn);
  386. Application.Begin (top);
  387. // default keybinding is Space which results in keypress
  388. Application.OnKeyDown (new ((KeyCode)' '));
  389. Assert.Equal (1, pressed);
  390. // remove the default keybinding (Space)
  391. btn.KeyBindings.Clear (Command.HotKey);
  392. btn.KeyBindings.Clear (Command.Accept);
  393. // After clearing the default keystroke the Space button no longer does anything for the Button
  394. Application.OnKeyDown (new ((KeyCode)' '));
  395. Assert.Equal (1, pressed);
  396. // Set a new binding of b for the click (Accept) event
  397. btn.KeyBindings.Add (Key.B, Command.HotKey);
  398. btn.KeyBindings.Add (Key.B, Command.Accept);
  399. // now pressing B should call the button click event
  400. Application.OnKeyDown (Key.B);
  401. Assert.Equal (2, pressed);
  402. // now pressing Shift-B should NOT call the button click event
  403. Application.OnKeyDown (Key.B.WithShift);
  404. Assert.Equal (2, pressed);
  405. // now pressing Alt-B should NOT call the button click event
  406. Application.OnKeyDown (Key.B.WithAlt);
  407. Assert.Equal (2, pressed);
  408. // now pressing Shift-Alt-B should NOT call the button click event
  409. Application.OnKeyDown (Key.B.WithAlt.WithShift);
  410. Assert.Equal (2, pressed);
  411. top.Dispose ();
  412. }
  413. [Fact]
  414. [AutoInitShutdown]
  415. public void KeyBindings_Command ()
  416. {
  417. var clicked = false;
  418. var btn = new Button { Text = "_Test" };
  419. btn.Accept += (s, e) => clicked = true;
  420. var top = new Toplevel ();
  421. top.Add (btn);
  422. Application.Begin (top);
  423. // Hot key. Both alone and with alt
  424. Assert.Equal (KeyCode.T, btn.HotKey);
  425. Assert.True (btn.NewKeyDownEvent (Key.T));
  426. Assert.True (clicked);
  427. clicked = false;
  428. Assert.True (btn.NewKeyDownEvent (Key.T.WithAlt));
  429. Assert.True (clicked);
  430. clicked = false;
  431. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  432. Assert.True (clicked);
  433. clicked = false;
  434. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  435. Assert.True (clicked);
  436. clicked = false;
  437. // IsDefault = false
  438. // Space and Enter should work
  439. Assert.False (btn.IsDefault);
  440. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  441. Assert.True (clicked);
  442. clicked = false;
  443. // IsDefault = true
  444. // Space and Enter should work
  445. btn.IsDefault = true;
  446. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  447. Assert.True (clicked);
  448. clicked = false;
  449. // Toplevel does not handle Enter, so it should get passed on to button
  450. Assert.True (Application.Top.NewKeyDownEvent (Key.Enter));
  451. Assert.True (clicked);
  452. clicked = false;
  453. // Direct
  454. Assert.True (btn.NewKeyDownEvent (Key.Enter));
  455. Assert.True (clicked);
  456. clicked = false;
  457. Assert.True (btn.NewKeyDownEvent (Key.Space));
  458. Assert.True (clicked);
  459. clicked = false;
  460. Assert.True (btn.NewKeyDownEvent (new ((KeyCode)'T')));
  461. Assert.True (clicked);
  462. clicked = false;
  463. // Change hotkey:
  464. btn.Text = "Te_st";
  465. Assert.True (btn.NewKeyDownEvent (btn.HotKey));
  466. Assert.True (clicked);
  467. clicked = false;
  468. top.Dispose ();
  469. }
  470. [Fact]
  471. public void HotKey_Command_Accepts ()
  472. {
  473. var button = new Button ();
  474. var accepted = false;
  475. button.Accept += ButtonOnAccept;
  476. button.InvokeCommand (Command.HotKey);
  477. Assert.True (accepted);
  478. button.Dispose ();
  479. return;
  480. void ButtonOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  481. }
  482. [Fact]
  483. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  484. {
  485. var button = new Button ();
  486. var acceptInvoked = false;
  487. button.Accept += ButtonAccept;
  488. bool? ret = button.InvokeCommand (Command.Accept);
  489. Assert.True (ret);
  490. Assert.True (acceptInvoked);
  491. button.Dispose ();
  492. return;
  493. void ButtonAccept (object sender, CancelEventArgs e)
  494. {
  495. acceptInvoked = true;
  496. e.Cancel = true;
  497. }
  498. }
  499. [Fact]
  500. public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
  501. {
  502. var super = new View ();
  503. var btn = new Button { Text = "_Test" };
  504. super.Add (btn);
  505. super.BeginInit ();
  506. super.EndInit ();
  507. Assert.Equal ("_Test", btn.Text);
  508. Assert.Equal (KeyCode.T, btn.HotKey);
  509. btn.Text = string.Empty;
  510. Assert.Equal ("", btn.Text);
  511. Assert.Equal (KeyCode.Null, btn.HotKey);
  512. btn.Text = string.Empty;
  513. Assert.Equal ("", btn.Text);
  514. Assert.Equal (KeyCode.Null, btn.HotKey);
  515. btn.Text = "Te_st";
  516. Assert.Equal ("Te_st", btn.Text);
  517. Assert.Equal (KeyCode.S, btn.HotKey);
  518. super.Dispose ();
  519. }
  520. [Fact]
  521. public void TestAssignTextToButton ()
  522. {
  523. View b = new Button { Text = "heya" };
  524. Assert.Equal ("heya", b.Text);
  525. Assert.Contains ("heya", b.TextFormatter.Text);
  526. b.Text = "heyb";
  527. Assert.Equal ("heyb", b.Text);
  528. Assert.Contains ("heyb", b.TextFormatter.Text);
  529. // with cast
  530. Assert.Equal ("heyb", ((Button)b).Text);
  531. b.Dispose ();
  532. }
  533. [Fact]
  534. [AutoInitShutdown]
  535. public void Update_Only_On_Or_After_Initialize ()
  536. {
  537. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  538. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  539. win.Add (btn);
  540. var top = new Toplevel ();
  541. top.Add (win);
  542. Assert.False (btn.IsInitialized);
  543. Application.Begin (top);
  544. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  545. Assert.True (btn.IsInitialized);
  546. Assert.Equal ("Say Hello 你", btn.Text);
  547. Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  548. Assert.Equal (new (0, 0, 16, 1), btn.Viewport);
  549. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  550. var expected = @$"
  551. ┌────────────────────────────┐
  552. │ │
  553. │ {btnTxt} │
  554. │ │
  555. └────────────────────────────┘
  556. ";
  557. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  558. Assert.Equal (new (0, 0, 30, 5), pos);
  559. top.Dispose ();
  560. }
  561. [Fact]
  562. [AutoInitShutdown]
  563. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  564. {
  565. var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  566. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  567. win.Add (btn);
  568. var top = new Toplevel ();
  569. top.Add (win);
  570. Assert.False (btn.IsInitialized);
  571. Application.Begin (top);
  572. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  573. Assert.True (btn.IsInitialized);
  574. Assert.Equal ("Say Hello 你", btn.Text);
  575. Assert.Equal ($"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
  576. Assert.Equal (new (0, 0, 16, 1), btn.Viewport);
  577. var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
  578. var expected = @$"
  579. ┌────────────────────────────┐
  580. │ │
  581. │ {btnTxt} │
  582. │ │
  583. └────────────────────────────┘
  584. ";
  585. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  586. Assert.Equal (new (0, 0, 30, 5), pos);
  587. top.Dispose ();
  588. }
  589. [Theory]
  590. [InlineData (MouseFlags.Button1Pressed, MouseFlags.Button1Released, MouseFlags.Button1Clicked)]
  591. [InlineData (MouseFlags.Button2Pressed, MouseFlags.Button2Released, MouseFlags.Button2Clicked)]
  592. [InlineData (MouseFlags.Button3Pressed, MouseFlags.Button3Released, MouseFlags.Button3Clicked)]
  593. [InlineData (MouseFlags.Button4Pressed, MouseFlags.Button4Released, MouseFlags.Button4Clicked)]
  594. public void WantContinuousButtonPressed_True_ButtonClick_Accepts (MouseFlags pressed, MouseFlags released, MouseFlags clicked)
  595. {
  596. var me = new MouseEvent ();
  597. var button = new Button ()
  598. {
  599. AutoSize = false,
  600. Width = 1,
  601. Height = 1,
  602. WantContinuousButtonPressed = true
  603. };
  604. var acceptCount = 0;
  605. button.Accept += (s, e) => acceptCount++;
  606. me.Flags = pressed;
  607. button.NewMouseEvent (me);
  608. Assert.Equal (1, acceptCount);
  609. me.Flags = released;
  610. button.NewMouseEvent (me);
  611. Assert.Equal (1, acceptCount);
  612. me.Flags = clicked;
  613. button.NewMouseEvent (me);
  614. Assert.Equal (1, acceptCount);
  615. button.Dispose ();
  616. }
  617. [Theory]
  618. [InlineData (MouseFlags.Button1Pressed, MouseFlags.Button1Released, MouseFlags.Button1Clicked)]
  619. [InlineData (MouseFlags.Button2Pressed, MouseFlags.Button2Released, MouseFlags.Button2Clicked)]
  620. [InlineData (MouseFlags.Button3Pressed, MouseFlags.Button3Released, MouseFlags.Button3Clicked)]
  621. [InlineData (MouseFlags.Button4Pressed, MouseFlags.Button4Released, MouseFlags.Button4Clicked)]
  622. public void WantContinuousButtonPressed_True_ButtonPressRelease_Accepts (MouseFlags pressed, MouseFlags released, MouseFlags clicked)
  623. {
  624. var me = new MouseEvent ();
  625. var button = new Button ()
  626. {
  627. AutoSize = false,
  628. Width = 1,
  629. Height = 1,
  630. WantContinuousButtonPressed = true
  631. };
  632. var acceptCount = 0;
  633. button.Accept += (s, e) => acceptCount++;
  634. me.Flags = pressed;
  635. button.NewMouseEvent (me);
  636. Assert.Equal (1, acceptCount);
  637. me.Flags = released;
  638. button.NewMouseEvent (me);
  639. Assert.Equal (1, acceptCount);
  640. button.Dispose ();
  641. }
  642. }