ButtonTests.cs 22 KB

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