ButtonTests.cs 21 KB

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