ButtonTests.cs 24 KB

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