ButtonTests.cs 20 KB

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