ButtonTests.cs 27 KB

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