ButtonTests.cs 20 KB

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