ButtonTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests {
  5. public class ButtonTests {
  6. readonly ITestOutputHelper output;
  7. public ButtonTests (ITestOutputHelper output)
  8. {
  9. this.output = output;
  10. }
  11. [Fact, AutoInitShutdown]
  12. public void Constructors_Defaults ()
  13. {
  14. var btn = new Button ();
  15. Assert.Equal (string.Empty, btn.Text);
  16. Application.Top.Add (btn);
  17. var rs = Application.Begin (Application.Top);
  18. Assert.Equal ("[ ]", btn.TextFormatter.Text);
  19. Assert.False (btn.IsDefault);
  20. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  21. Assert.Equal ('_', btn.HotKeySpecifier);
  22. Assert.True (btn.CanFocus);
  23. Assert.Equal (new Rect (0, 0, 4, 1), btn.Bounds);
  24. Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame);
  25. Assert.Equal (Key.Null, btn.HotKey);
  26. var expected = @"
  27. [ ]
  28. ";
  29. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  30. Application.End (rs);
  31. btn = new Button ("ARGS", true) { Text = "Test" };
  32. Assert.Equal ("Test", btn.Text);
  33. Application.Top.Add (btn);
  34. rs = Application.Begin (Application.Top);
  35. Assert.Equal ("[◦ Test ◦]", btn.TextFormatter.Text);
  36. Assert.True (btn.IsDefault);
  37. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  38. Assert.Equal ('_', btn.HotKeySpecifier);
  39. Assert.True (btn.CanFocus);
  40. Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
  41. Assert.Equal (new Rect (0, 0, 10, 1), btn.Frame);
  42. Assert.Equal (Key.T, btn.HotKey);
  43. expected = @"
  44. [◦ Test ◦]
  45. ";
  46. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  47. Application.End (rs);
  48. btn = new Button (3, 4, "Test", true);
  49. Assert.Equal ("Test", btn.Text);
  50. Application.Top.Add (btn);
  51. rs = Application.Begin (Application.Top);
  52. Assert.Equal ("[◦ Test ◦]", btn.TextFormatter.Text);
  53. Assert.True (btn.IsDefault);
  54. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  55. Assert.Equal ('_', btn.HotKeySpecifier);
  56. Assert.True (btn.CanFocus);
  57. Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
  58. Assert.Equal (new Rect (3, 4, 10, 1), btn.Frame);
  59. Assert.Equal (Key.T, btn.HotKey);
  60. expected = @"
  61. [◦ Test ◦]
  62. ";
  63. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  64. Application.End (rs);
  65. }
  66. [Fact]
  67. [AutoInitShutdown]
  68. public void KeyBindings_Command ()
  69. {
  70. var clicked = false;
  71. Button btn = new Button ("Test");
  72. btn.Clicked += (s, e) => clicked = true;
  73. Application.Top.Add (btn);
  74. Application.Begin (Application.Top);
  75. Assert.Equal (Key.T, btn.HotKey);
  76. Assert.False (btn.ProcessHotKey (new KeyEvent (Key.T, new KeyModifiers ())));
  77. Assert.False (clicked);
  78. Assert.True (btn.ProcessHotKey (new KeyEvent (Key.T | Key.AltMask, new KeyModifiers () { Alt = true })));
  79. Assert.True (clicked);
  80. clicked = false;
  81. Assert.False (btn.IsDefault);
  82. Assert.False (btn.ProcessColdKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  83. Assert.False (clicked);
  84. btn.IsDefault = true;
  85. Assert.True (btn.ProcessColdKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  86. Assert.True (clicked);
  87. clicked = false;
  88. Assert.True (btn.ProcessColdKey (new KeyEvent (Key.AltMask | Key.T, new KeyModifiers ())));
  89. Assert.True (clicked);
  90. clicked = false;
  91. Assert.True (btn.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  92. Assert.True (clicked);
  93. clicked = false;
  94. Assert.True (btn.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
  95. Assert.True (clicked);
  96. clicked = false;
  97. Assert.True (btn.ProcessKey (new KeyEvent ((Key)'t', new KeyModifiers ())));
  98. Assert.True (clicked);
  99. clicked = false;
  100. Assert.True (btn.ProcessKey (new KeyEvent (Key.Space | btn.HotKey, new KeyModifiers ())));
  101. Assert.True (clicked);
  102. btn.Text = "Te_st";
  103. clicked = false;
  104. Assert.True (btn.ProcessKey (new KeyEvent (Key.Space | btn.HotKey, new KeyModifiers ())));
  105. Assert.True (clicked);
  106. }
  107. [Fact]
  108. [AutoInitShutdown]
  109. public void ChangeHotKey ()
  110. {
  111. var clicked = false;
  112. Button btn = new Button ("Test");
  113. btn.Clicked += (s, e) => clicked = true;
  114. Application.Top.Add (btn);
  115. Application.Begin (Application.Top);
  116. Assert.Equal (Key.T, btn.HotKey);
  117. Assert.False (btn.ProcessHotKey (new KeyEvent (Key.T, new KeyModifiers ())));
  118. Assert.False (clicked);
  119. Assert.True (btn.ProcessHotKey (new KeyEvent (Key.T | Key.AltMask, new KeyModifiers () { Alt = true })));
  120. Assert.True (clicked);
  121. clicked = false;
  122. btn.HotKey = Key.E;
  123. Assert.True (btn.ProcessHotKey (new KeyEvent (Key.E | Key.AltMask, new KeyModifiers () { Alt = true })));
  124. Assert.True (clicked);
  125. }
  126. /// <summary>
  127. /// This test demonstrates how to change the activation key for Button
  128. /// as described in the README.md keyboard handling section
  129. /// </summary>
  130. [Fact]
  131. [AutoInitShutdown]
  132. public void KeyBindingExample ()
  133. {
  134. int pressed = 0;
  135. var btn = new Button ("Press Me");
  136. btn.Clicked += (s, e) => pressed++;
  137. // The Button class supports the Accept command
  138. Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
  139. Application.Top.Add (btn);
  140. Application.Begin (Application.Top);
  141. // default keybinding is Enter which results in keypress
  142. Application.Driver.SendKeys ('\n', ConsoleKey.Enter, false, false, false);
  143. Assert.Equal (1, pressed);
  144. // remove the default keybinding (Enter)
  145. btn.ClearKeybinding (Command.Accept);
  146. // After clearing the default keystroke the Enter button no longer does anything for the Button
  147. Application.Driver.SendKeys ('\n', ConsoleKey.Enter, false, false, false);
  148. Assert.Equal (1, pressed);
  149. // Set a new binding of b for the click (Accept) event
  150. btn.AddKeyBinding (Key.b, Command.Accept);
  151. // now pressing B should call the button click event
  152. Application.Driver.SendKeys ('b', ConsoleKey.B, false, false, false);
  153. Assert.Equal (2, pressed);
  154. }
  155. [Fact]
  156. public void TestAssignTextToButton ()
  157. {
  158. View b = new Button () { Text = "heya" };
  159. Assert.Equal ("heya", b.Text);
  160. Assert.True (b.TextFormatter.Text.Contains ("heya"));
  161. b.Text = "heyb";
  162. Assert.Equal ("heyb", b.Text);
  163. Assert.True (b.TextFormatter.Text.Contains ("heyb"));
  164. // with cast
  165. Assert.Equal ("heyb", ((Button)b).Text);
  166. }
  167. [Fact]
  168. public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
  169. {
  170. var super = new View ();
  171. var btn = new Button ("Test");
  172. super.Add (btn);
  173. super.BeginInit ();
  174. super.EndInit ();
  175. Assert.Equal ("Test", btn.Text);
  176. Assert.Equal (Key.T, btn.HotKey);
  177. btn.Text = string.Empty;
  178. Assert.Equal ("", btn.Text);
  179. Assert.Equal (Key.Null, btn.HotKey);
  180. btn.Text = "Te_st";
  181. Assert.Equal ("Te_st", btn.Text);
  182. Assert.Equal (Key.S, btn.HotKey);
  183. }
  184. [Fact, AutoInitShutdown]
  185. public void Update_Only_On_Or_After_Initialize ()
  186. {
  187. var btn = new Button ("Say Hello 你") {
  188. X = Pos.Center (),
  189. Y = Pos.Center ()
  190. };
  191. var win = new Window () {
  192. Width = Dim.Fill (),
  193. Height = Dim.Fill ()
  194. };
  195. win.Add (btn);
  196. Application.Top.Add (win);
  197. Assert.False (btn.IsInitialized);
  198. Application.Begin (Application.Top);
  199. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  200. Assert.True (btn.IsInitialized);
  201. Assert.Equal ("Say Hello 你", btn.Text);
  202. Assert.Equal ("[ Say Hello 你 ]", btn.TextFormatter.Text);
  203. Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds);
  204. var expected = @"
  205. ┌────────────────────────────┐
  206. │ │
  207. │ [ Say Hello 你 ] │
  208. │ │
  209. └────────────────────────────┘
  210. ";
  211. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  212. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  213. }
  214. [Fact, AutoInitShutdown]
  215. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  216. {
  217. var btn = new Button () {
  218. X = Pos.Center (),
  219. Y = Pos.Center (),
  220. Text = "Say Hello 你"
  221. };
  222. var win = new Window () {
  223. Width = Dim.Fill (),
  224. Height = Dim.Fill (),
  225. };
  226. win.Add (btn);
  227. Application.Top.Add (win);
  228. Assert.False (btn.IsInitialized);
  229. Application.Begin (Application.Top);
  230. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  231. Assert.True (btn.IsInitialized);
  232. Assert.Equal ("Say Hello 你", btn.Text);
  233. Assert.Equal ("[ Say Hello 你 ]", btn.TextFormatter.Text);
  234. Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds);
  235. var expected = @"
  236. ┌────────────────────────────┐
  237. │ │
  238. │ [ Say Hello 你 ] │
  239. │ │
  240. └────────────────────────────┘
  241. ";
  242. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  243. Assert.Equal (new Rect (0, 0, 30, 5), pos);
  244. }
  245. [Fact, AutoInitShutdown]
  246. public void AutoSize_Stays_True_With_EmptyText ()
  247. {
  248. var btn = new Button () {
  249. X = Pos.Center (),
  250. Y = Pos.Center (),
  251. AutoSize = true
  252. };
  253. var win = new Window () {
  254. Width = Dim.Fill (),
  255. Height = Dim.Fill (),
  256. };
  257. win.Add (btn);
  258. Application.Top.Add (win);
  259. Assert.True (btn.AutoSize);
  260. btn.Text = "Say Hello 你";
  261. Assert.True (btn.AutoSize);
  262. Application.Begin (Application.Top);
  263. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  264. var expected = @"
  265. ┌────────────────────────────┐
  266. │ │
  267. │ [ Say Hello 你 ] │
  268. │ │
  269. └────────────────────────────┘
  270. ";
  271. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  272. }
  273. [Fact, AutoInitShutdown]
  274. public void AutoSize_Stays_True_Center ()
  275. {
  276. var btn = new Button () {
  277. X = Pos.Center (),
  278. Y = Pos.Center (),
  279. Text = "Say Hello 你"
  280. };
  281. var win = new Window () {
  282. Width = Dim.Fill (),
  283. Height = Dim.Fill (),
  284. };
  285. win.Add (btn);
  286. Application.Top.Add (win);
  287. Assert.True (btn.AutoSize);
  288. Application.Begin (Application.Top);
  289. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  290. var expected = @"
  291. ┌────────────────────────────┐
  292. │ │
  293. │ [ Say Hello 你 ] │
  294. │ │
  295. └────────────────────────────┘
  296. ";
  297. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  298. Assert.True (btn.AutoSize);
  299. btn.Text = "Say Hello 你 changed";
  300. Assert.True (btn.AutoSize);
  301. Application.Refresh ();
  302. expected = @"
  303. ┌────────────────────────────┐
  304. │ │
  305. │ [ Say Hello 你 changed ] │
  306. │ │
  307. └────────────────────────────┘
  308. ";
  309. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  310. }
  311. [Fact, AutoInitShutdown]
  312. public void AutoSize_Stays_True_AnchorEnd ()
  313. {
  314. var btn = new Button () {
  315. Y = Pos.Center (),
  316. Text = "Say Hello 你",
  317. AutoSize = true
  318. };
  319. btn.X = Pos.AnchorEnd () - Pos.Function (() => TextFormatter.GetTextWidth (btn.TextFormatter.Text));
  320. var win = new Window () {
  321. Width = Dim.Fill (),
  322. Height = Dim.Fill (),
  323. };
  324. win.Add (btn);
  325. Application.Top.Add (win);
  326. Assert.True (btn.AutoSize);
  327. Application.Begin (Application.Top);
  328. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  329. var expected = @"
  330. ┌────────────────────────────┐
  331. │ │
  332. │ [ Say Hello 你 ]│
  333. │ │
  334. └────────────────────────────┘
  335. ";
  336. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  337. Assert.True (btn.AutoSize);
  338. btn.Text = "Say Hello 你 changed";
  339. Assert.True (btn.AutoSize);
  340. Application.Refresh ();
  341. expected = @"
  342. ┌────────────────────────────┐
  343. │ │
  344. │ [ Say Hello 你 changed ]│
  345. │ │
  346. └────────────────────────────┘
  347. ";
  348. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  349. }
  350. [Fact, AutoInitShutdown]
  351. public void AutoSize_False_With_Fixed_Width ()
  352. {
  353. var tab = new View ();
  354. var lblWidth = 8;
  355. var label = new Label ("Find:") {
  356. Y = 1,
  357. Width = lblWidth,
  358. TextAlignment = TextAlignment.Right,
  359. AutoSize = false
  360. };
  361. tab.Add (label);
  362. var txtToFind = new TextField ("Testing buttons.") {
  363. X = Pos.Right (label) + 1,
  364. Y = Pos.Top (label),
  365. Width = 20
  366. };
  367. tab.Add (txtToFind);
  368. var btnFindNext = new Button ("Find _Next") {
  369. X = Pos.Right (txtToFind) + 1,
  370. Y = Pos.Top (label),
  371. Width = 20,
  372. Enabled = !txtToFind.Text.IsEmpty,
  373. TextAlignment = TextAlignment.Centered,
  374. IsDefault = true,
  375. AutoSize = false
  376. };
  377. tab.Add (btnFindNext);
  378. var btnFindPrevious = new Button ("Find _Previous") {
  379. X = Pos.Right (txtToFind) + 1,
  380. Y = Pos.Top (btnFindNext) + 1,
  381. Width = 20,
  382. Enabled = !txtToFind.Text.IsEmpty,
  383. TextAlignment = TextAlignment.Centered,
  384. AutoSize = false
  385. };
  386. tab.Add (btnFindPrevious);
  387. var btnCancel = new Button ("Cancel") {
  388. X = Pos.Right (txtToFind) + 1,
  389. Y = Pos.Top (btnFindPrevious) + 2,
  390. Width = 20,
  391. TextAlignment = TextAlignment.Centered,
  392. AutoSize = false
  393. };
  394. tab.Add (btnCancel);
  395. var ckbMatchCase = new CheckBox ("Match c_ase") {
  396. X = 0,
  397. Y = Pos.Top (txtToFind) + 2,
  398. Checked = true
  399. };
  400. tab.Add (ckbMatchCase);
  401. var ckbMatchWholeWord = new CheckBox ("Match _whole word") {
  402. X = 0,
  403. Y = Pos.Top (ckbMatchCase) + 1,
  404. Checked = false
  405. };
  406. tab.Add (ckbMatchWholeWord);
  407. var tabView = new TabView () {
  408. Width = Dim.Fill (),
  409. Height = Dim.Fill ()
  410. };
  411. tabView.AddTab (new TabView.Tab ("Find", tab), true);
  412. var win = new Window () {
  413. Width = Dim.Fill (),
  414. Height = Dim.Fill ()
  415. };
  416. tab.Width = label.Width + txtToFind.Width + btnFindNext.Width + 2;
  417. tab.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
  418. win.Add (tabView);
  419. Application.Top.Add (win);
  420. Application.Begin (Application.Top);
  421. ((FakeDriver)Application.Driver).SetBufferSize (54, 11);
  422. Assert.Equal (new Rect (0, 0, 54, 11), win.Frame);
  423. Assert.Equal (new Rect (0, 0, 52, 9), tabView.Frame);
  424. Assert.Equal (new Rect (0, 0, 50, 7), tab.Frame);
  425. Assert.Equal (new Rect (0, 1, 8, 1), label.Frame);
  426. Assert.Equal (new Rect (9, 1, 20, 1), txtToFind.Frame);
  427. Assert.Equal (0, txtToFind.ScrollOffset);
  428. Assert.Equal (16, txtToFind.CursorPosition);
  429. Assert.Equal (new Rect (30, 1, 20, 1), btnFindNext.Frame);
  430. Assert.Equal (new Rect (30, 2, 20, 1), btnFindPrevious.Frame);
  431. Assert.Equal (new Rect (30, 4, 20, 1), btnCancel.Frame);
  432. Assert.Equal (new Rect (0, 3, 12, 1), ckbMatchCase.Frame);
  433. Assert.Equal (new Rect (0, 4, 18, 1), ckbMatchWholeWord.Frame);
  434. var expected = @"
  435. ┌────────────────────────────────────────────────────┐
  436. │┌────┐ │
  437. ││Find│ │
  438. ││ └─────────────────────────────────────────────┐│
  439. ││ ││
  440. ││ Find: Testing buttons. [◦ Find Next ◦] ││
  441. ││ [ Find Previous ] ││
  442. ││√ Match case ││
  443. ││╴ Match whole word [ Cancel ] ││
  444. │└──────────────────────────────────────────────────┘│
  445. └────────────────────────────────────────────────────┘
  446. ";
  447. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  448. }
  449. [Fact, AutoInitShutdown]
  450. public void Pos_Center_Layout_AutoSize_True ()
  451. {
  452. var button = new Button ("Process keys") {
  453. X = Pos.Center (),
  454. Y = Pos.Center (),
  455. IsDefault = true
  456. };
  457. var win = new Window () {
  458. Width = Dim.Fill (),
  459. Height = Dim.Fill ()
  460. };
  461. win.Add (button);
  462. Application.Top.Add (win);
  463. Application.Begin (Application.Top);
  464. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  465. Assert.True (button.AutoSize);
  466. Assert.Equal (new Rect (5, 1, 18, 1), button.Frame);
  467. var expected = @"
  468. ┌────────────────────────────┐
  469. │ │
  470. │ [◦ Process keys ◦] │
  471. │ │
  472. └────────────────────────────┘
  473. ";
  474. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  475. }
  476. [Fact, AutoInitShutdown]
  477. public void Pos_Center_Layout_AutoSize_False ()
  478. {
  479. var button = new Button ("Process keys") {
  480. X = Pos.Center (),
  481. Y = Pos.Center (),
  482. Width = 20,
  483. IsDefault = true,
  484. AutoSize = false
  485. };
  486. var win = new Window () {
  487. Width = Dim.Fill (),
  488. Height = Dim.Fill ()
  489. };
  490. win.Add (button);
  491. Application.Top.Add (win);
  492. Application.Begin (Application.Top);
  493. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  494. Assert.False (button.AutoSize);
  495. Assert.Equal (new Rect (4, 1, 20, 1), button.Frame);
  496. var expected = @"
  497. ┌────────────────────────────┐
  498. │ │
  499. │ [◦ Process keys ◦] │
  500. │ │
  501. └────────────────────────────┘
  502. ";
  503. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  504. }
  505. [Fact, AutoInitShutdown]
  506. public void Button_HotKeyChanged_EventFires ()
  507. {
  508. var btn = new Button ("Yar");
  509. object sender = null;
  510. KeyChangedEventArgs args = null;
  511. btn.HotKeyChanged += (s, e) => {
  512. sender = s;
  513. args = e;
  514. };
  515. btn.HotKey = Key.r;
  516. Assert.Same (btn, sender);
  517. Assert.Equal (Key.Y, args.OldKey);
  518. Assert.Equal (Key.r, args.NewKey);
  519. }
  520. [Fact, AutoInitShutdown]
  521. public void Button_HotKeyChanged_EventFires_WithNone ()
  522. {
  523. var btn = new Button ();
  524. object sender = null;
  525. KeyChangedEventArgs args = null;
  526. btn.HotKeyChanged += (s, e) => {
  527. sender = s;
  528. args = e;
  529. };
  530. btn.HotKey = Key.r;
  531. Assert.Same (btn, sender);
  532. Assert.Equal (Key.Null, args.OldKey);
  533. Assert.Equal (Key.r, args.NewKey);
  534. }
  535. }
  536. }