ButtonTests.cs 28 KB

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