ShortcutTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. using JetBrains.Annotations;
  2. namespace Terminal.Gui.ViewsTests;
  3. [TestSubject (typeof (Shortcut))]
  4. public class ShortcutTests
  5. {
  6. [Fact]
  7. public void Constructor_Defaults ()
  8. {
  9. var shortcut = new Shortcut ();
  10. Assert.NotNull (shortcut);
  11. Assert.True (shortcut.CanFocus);
  12. Assert.IsType<DimAuto> (shortcut.Width);
  13. Assert.IsType<DimAuto> (shortcut.Height);
  14. }
  15. [Fact]
  16. public void Size_Defaults ()
  17. {
  18. var shortcut = new Shortcut ();
  19. shortcut.BeginInit();
  20. shortcut.EndInit ();
  21. shortcut.Layout ();
  22. Assert.Equal (2, shortcut.Frame.Width);
  23. Assert.Equal (1, shortcut.Frame.Height);
  24. Assert.Equal (2, shortcut.Viewport.Width);
  25. Assert.Equal (1, shortcut.Viewport.Height);
  26. Assert.Equal (0, shortcut.CommandView.Viewport.Width);
  27. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  28. Assert.Equal (0, shortcut.HelpView.Viewport.Width);
  29. Assert.Equal (0, shortcut.HelpView.Viewport.Height);
  30. Assert.Equal (0, shortcut.KeyView.Viewport.Width);
  31. Assert.Equal (0, shortcut.KeyView.Viewport.Height);
  32. // 0123456789
  33. // " 0 A "
  34. shortcut = new ()
  35. {
  36. Key = Key.A,
  37. HelpText = "0"
  38. };
  39. shortcut.BeginInit ();
  40. shortcut.EndInit ();
  41. shortcut.Layout ();
  42. Assert.Equal (8, shortcut.Frame.Width);
  43. Assert.Equal (1, shortcut.Frame.Height);
  44. Assert.Equal (8, shortcut.Viewport.Width);
  45. Assert.Equal (1, shortcut.Viewport.Height);
  46. Assert.Equal (0, shortcut.CommandView.Viewport.Width);
  47. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  48. Assert.Equal (1, shortcut.HelpView.Viewport.Width);
  49. Assert.Equal (1, shortcut.HelpView.Viewport.Height);
  50. Assert.Equal (1, shortcut.KeyView.Viewport.Width);
  51. Assert.Equal (1, shortcut.KeyView.Viewport.Height);
  52. // 0123456789
  53. // " C 0 A "
  54. shortcut = new ()
  55. {
  56. Title = "C",
  57. Key = Key.A,
  58. HelpText = "0"
  59. };
  60. shortcut.BeginInit ();
  61. shortcut.EndInit ();
  62. shortcut.Layout ();
  63. Assert.Equal (9, shortcut.Frame.Width);
  64. Assert.Equal (1, shortcut.Frame.Height);
  65. Assert.Equal (9, shortcut.Viewport.Width);
  66. Assert.Equal (1, shortcut.Viewport.Height);
  67. Assert.Equal (1, shortcut.CommandView.Viewport.Width);
  68. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  69. Assert.Equal (1, shortcut.HelpView.Viewport.Width);
  70. Assert.Equal (1, shortcut.HelpView.Viewport.Height);
  71. Assert.Equal (1, shortcut.KeyView.Viewport.Width);
  72. Assert.Equal (1, shortcut.KeyView.Viewport.Height);
  73. }
  74. [Theory]
  75. [InlineData ("", "", KeyCode.Null, 2)]
  76. [InlineData ("C", "", KeyCode.Null, 3)]
  77. [InlineData ("", "H", KeyCode.Null, 5)]
  78. [InlineData ("", "", KeyCode.K, 5)]
  79. [InlineData ("C", "", KeyCode.K, 6)]
  80. [InlineData ("C", "H", KeyCode.Null, 6)]
  81. [InlineData ("", "H", KeyCode.K, 8)]
  82. [InlineData ("C", "H", KeyCode.K, 9)]
  83. public void NaturalSize (string command, string help, KeyCode key, int expectedWidth)
  84. {
  85. var shortcut = new Shortcut
  86. {
  87. Title = command,
  88. HelpText = help,
  89. Key = key
  90. };
  91. Assert.IsType<DimAuto> (shortcut.Width);
  92. Assert.IsType<DimAuto> (shortcut.Height);
  93. shortcut.SetRelativeLayout (new (100, 100));
  94. // |0123456789
  95. // | C H K |
  96. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  97. }
  98. [Theory]
  99. [InlineData (5, 0, 3, 6)]
  100. [InlineData (6, 0, 3, 6)]
  101. [InlineData (7, 0, 3, 6)]
  102. [InlineData (8, 0, 3, 6)]
  103. [InlineData (9, 0, 3, 6)]
  104. [InlineData (10, 0, 4, 7)]
  105. [InlineData (11, 0, 5, 8)]
  106. public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
  107. {
  108. var shortcut = new Shortcut
  109. {
  110. Width = width,
  111. Title = "C",
  112. Text = "H",
  113. Key = Key.K
  114. };
  115. shortcut.LayoutSubviews ();
  116. shortcut.SetRelativeLayout (new (100, 100));
  117. // 0123456789
  118. // -C--H--K-
  119. Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
  120. Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
  121. Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
  122. }
  123. [Fact]
  124. public void CommandView_Text_And_Title_Track ()
  125. {
  126. var shortcut = new Shortcut
  127. {
  128. Title = "T"
  129. };
  130. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  131. shortcut = new ();
  132. shortcut.CommandView = new ()
  133. {
  134. Text = "T"
  135. };
  136. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  137. }
  138. [Fact]
  139. public void HelpText_And_Text_Are_The_Same ()
  140. {
  141. var shortcut = new Shortcut
  142. {
  143. Text = "H"
  144. };
  145. Assert.Equal (shortcut.Text, shortcut.HelpText);
  146. shortcut = new ()
  147. {
  148. HelpText = "H"
  149. };
  150. Assert.Equal (shortcut.Text, shortcut.HelpText);
  151. }
  152. [Theory]
  153. [InlineData (KeyCode.Null, "")]
  154. [InlineData (KeyCode.F1, "F1")]
  155. public void KeyView_Text_Tracks_Key (KeyCode key, string expected)
  156. {
  157. var shortcut = new Shortcut
  158. {
  159. Key = key
  160. };
  161. Assert.Equal (expected, shortcut.KeyView.Text);
  162. }
  163. // Test Key
  164. [Fact]
  165. public void Key_Defaults_To_Empty ()
  166. {
  167. var shortcut = new Shortcut ();
  168. Assert.Equal (Key.Empty, shortcut.Key);
  169. }
  170. [Fact]
  171. public void Key_Can_Be_Set ()
  172. {
  173. var shortcut = new Shortcut ();
  174. shortcut.Key = Key.F1;
  175. Assert.Equal (Key.F1, shortcut.Key);
  176. }
  177. [Fact]
  178. public void Key_Can_Be_Set_To_Empty ()
  179. {
  180. var shortcut = new Shortcut ();
  181. shortcut.Key = Key.Empty;
  182. Assert.Equal (Key.Empty, shortcut.Key);
  183. }
  184. [Fact]
  185. public void Key_Set_Binds_Key_To_CommandView_Accept ()
  186. {
  187. var shortcut = new Shortcut ();
  188. shortcut.Key = Key.F1;
  189. // TODO:
  190. }
  191. [Fact]
  192. public void Key_Changing_Removes_Previous_Binding ()
  193. {
  194. var shortcut = new Shortcut ();
  195. shortcut.Key = Key.A;
  196. Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
  197. shortcut.Key = Key.B;
  198. Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
  199. Assert.Contains (Key.B, shortcut.KeyBindings.Bindings.Keys);
  200. }
  201. // Test Key gets bound correctly
  202. [Fact]
  203. public void KeyBindingScope_Defaults_To_HotKey ()
  204. {
  205. var shortcut = new Shortcut ();
  206. Assert.Equal (KeyBindingScope.HotKey, shortcut.KeyBindingScope);
  207. }
  208. [Fact]
  209. public void KeyBindingScope_Can_Be_Set ()
  210. {
  211. var shortcut = new Shortcut ();
  212. shortcut.KeyBindingScope = KeyBindingScope.Application;
  213. Assert.Equal (KeyBindingScope.Application, shortcut.KeyBindingScope);
  214. }
  215. [Fact]
  216. public void KeyBindingScope_Changing_Adjusts_KeyBindings ()
  217. {
  218. var shortcut = new Shortcut ();
  219. shortcut.Key = Key.A;
  220. Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
  221. shortcut.KeyBindingScope = KeyBindingScope.Application;
  222. Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
  223. Assert.Contains (Key.A, Application.KeyBindings.Bindings.Keys);
  224. shortcut.KeyBindingScope = KeyBindingScope.HotKey;
  225. Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
  226. Assert.DoesNotContain (Key.A, Application.KeyBindings.Bindings.Keys);
  227. }
  228. [Theory]
  229. [InlineData (Orientation.Horizontal)]
  230. [InlineData (Orientation.Vertical)]
  231. public void Orientation_SetsCorrectly (Orientation orientation)
  232. {
  233. var shortcut = new Shortcut
  234. {
  235. Orientation = orientation
  236. };
  237. Assert.Equal (orientation, shortcut.Orientation);
  238. }
  239. [Theory]
  240. [InlineData (AlignmentModes.StartToEnd)]
  241. [InlineData (AlignmentModes.EndToStart)]
  242. public void AlignmentModes_SetsCorrectly (AlignmentModes alignmentModes)
  243. {
  244. var shortcut = new Shortcut
  245. {
  246. AlignmentModes = alignmentModes
  247. };
  248. Assert.Equal (alignmentModes, shortcut.AlignmentModes);
  249. }
  250. [Fact]
  251. public void Action_SetsAndGetsCorrectly ()
  252. {
  253. var actionInvoked = false;
  254. var shortcut = new Shortcut
  255. {
  256. Action = () => { actionInvoked = true; }
  257. };
  258. shortcut.Action.Invoke ();
  259. Assert.True (actionInvoked);
  260. }
  261. [Fact]
  262. public void Subview_Visibility_Controlled_By_Removal ()
  263. {
  264. var shortcut = new Shortcut ();
  265. Assert.True (shortcut.CommandView.Visible);
  266. Assert.Contains (shortcut.CommandView, shortcut.Subviews);
  267. Assert.True (shortcut.HelpView.Visible);
  268. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  269. Assert.True (shortcut.KeyView.Visible);
  270. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  271. shortcut.HelpText = "help";
  272. Assert.True (shortcut.HelpView.Visible);
  273. Assert.Contains (shortcut.HelpView, shortcut.Subviews);
  274. Assert.True (shortcut.KeyView.Visible);
  275. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  276. shortcut.Key = Key.A;
  277. Assert.True (shortcut.HelpView.Visible);
  278. Assert.Contains (shortcut.HelpView, shortcut.Subviews);
  279. Assert.True (shortcut.KeyView.Visible);
  280. Assert.Contains (shortcut.KeyView, shortcut.Subviews);
  281. shortcut.HelpView.Visible = false;
  282. shortcut.ShowHide ();
  283. Assert.False (shortcut.HelpView.Visible);
  284. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  285. Assert.True (shortcut.KeyView.Visible);
  286. Assert.Contains (shortcut.KeyView, shortcut.Subviews);
  287. shortcut.KeyView.Visible = false;
  288. shortcut.ShowHide ();
  289. Assert.False (shortcut.HelpView.Visible);
  290. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  291. Assert.False (shortcut.KeyView.Visible);
  292. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  293. }
  294. [Fact]
  295. public void Focus_CanFocus_Default_Is_True ()
  296. {
  297. Shortcut shortcut = new ();
  298. shortcut.Key = Key.A;
  299. shortcut.Text = "Help";
  300. shortcut.Title = "Command";
  301. Assert.True (shortcut.CanFocus);
  302. Assert.False (shortcut.CommandView.CanFocus);
  303. }
  304. [Fact]
  305. public void Focus_CanFocus_CommandView_Add_Tracks ()
  306. {
  307. Shortcut shortcut = new ();
  308. Assert.True (shortcut.CanFocus);
  309. Assert.False (shortcut.CommandView.CanFocus);
  310. shortcut.CommandView = new () { CanFocus = true };
  311. Assert.False (shortcut.CommandView.CanFocus);
  312. shortcut.CommandView.CanFocus = true;
  313. Assert.True (shortcut.CommandView.CanFocus);
  314. shortcut.CanFocus = false;
  315. Assert.False (shortcut.CanFocus);
  316. Assert.True (shortcut.CommandView.CanFocus);
  317. shortcut.CommandView.CanFocus = false;
  318. Assert.False (shortcut.CanFocus);
  319. Assert.False (shortcut.CommandView.CanFocus);
  320. shortcut.CommandView.CanFocus = true;
  321. Assert.False (shortcut.CanFocus);
  322. Assert.True (shortcut.CommandView.CanFocus);
  323. }
  324. [Theory]
  325. // 0123456789
  326. // " C 0 A "
  327. [InlineData (-1, 0)]
  328. [InlineData (0, 1)]
  329. [InlineData (1, 1)]
  330. [InlineData (2, 1)]
  331. [InlineData (3, 1)]
  332. [InlineData (4, 1)]
  333. [InlineData (5, 1)]
  334. [InlineData (6, 1)]
  335. [InlineData (7, 1)]
  336. [InlineData (8, 1)]
  337. [InlineData (9, 0)]
  338. public void MouseClick_Raises_Accepted (int x, int expectedAccepted)
  339. {
  340. Application.Top = new ();
  341. var shortcut = new Shortcut
  342. {
  343. Key = Key.A,
  344. Text = "0",
  345. Title = "C"
  346. };
  347. Application.Top.Add (shortcut);
  348. Application.Top.SetRelativeLayout (new (100, 100));
  349. Application.Top.LayoutSubviews ();
  350. var accepted = 0;
  351. shortcut.Accepting += (s, e) => accepted++;
  352. Application.RaiseMouseEvent (
  353. new ()
  354. {
  355. ScreenPosition = new (x, 0),
  356. Flags = MouseFlags.Button1Clicked
  357. });
  358. Assert.Equal (expectedAccepted, accepted);
  359. Application.Top.Dispose ();
  360. Application.ResetState (true);
  361. }
  362. [Theory]
  363. // 0123456789
  364. // " C 0 A "
  365. [InlineData (-1, 0, 0, 0, 0)]
  366. [InlineData (0, 0, 1, 1, 1)] // mouseX = 0 is on the CommandView.Margin, so Shortcut will get MouseClick
  367. [InlineData (1, 0, 1, 1, 1)] // mouseX = 1 is on the CommandView, so CommandView will get MouseClick
  368. [InlineData (2, 0, 1, 1, 1)] // mouseX = 2 is on the CommandView.Margin, so Shortcut will get MouseClick
  369. [InlineData (3, 0, 1, 1, 1)]
  370. [InlineData (4, 0, 1, 1, 1)]
  371. [InlineData (5, 0, 1, 1, 1)]
  372. [InlineData (6, 0, 1, 1, 1)]
  373. [InlineData (7, 0, 1, 1, 1)]
  374. [InlineData (8, 0, 1, 1, 1)]
  375. [InlineData (9, 0, 0, 0, 0)]
  376. public void MouseClick_Default_CommandView_Raises_Accepted_Selected_Correctly (
  377. int mouseX,
  378. int expectedCommandViewAccepted,
  379. int expectedCommandViewSelected,
  380. int expectedShortcutAccepted,
  381. int expectedShortcutSelected
  382. )
  383. {
  384. Application.Top = new ();
  385. var shortcut = new Shortcut
  386. {
  387. Title = "C",
  388. Key = Key.A,
  389. HelpText = "0"
  390. };
  391. var commandViewAcceptCount = 0;
  392. shortcut.CommandView.Accepting += (s, e) => { commandViewAcceptCount++; };
  393. var commandViewSelectCount = 0;
  394. shortcut.CommandView.Selecting += (s, e) => { commandViewSelectCount++; };
  395. var shortcutAcceptCount = 0;
  396. shortcut.Accepting += (s, e) => { shortcutAcceptCount++; };
  397. var shortcutSelectCount = 0;
  398. shortcut.Selecting += (s, e) => { shortcutSelectCount++; };
  399. Application.Top.Add (shortcut);
  400. Application.Top.SetRelativeLayout (new (100, 100));
  401. Application.Top.LayoutSubviews ();
  402. Application.RaiseMouseEvent (
  403. new ()
  404. {
  405. ScreenPosition = new (mouseX, 0),
  406. Flags = MouseFlags.Button1Clicked
  407. });
  408. Assert.Equal (expectedShortcutAccepted, shortcutAcceptCount);
  409. Assert.Equal (expectedShortcutSelected, shortcutSelectCount);
  410. Assert.Equal (expectedCommandViewAccepted, commandViewAcceptCount);
  411. Assert.Equal (expectedCommandViewSelected, commandViewSelectCount);
  412. Application.Top.Dispose ();
  413. Application.ResetState (true);
  414. }
  415. [Theory]
  416. // 0123456789
  417. // " C 0 A "
  418. [InlineData (-1, 0, 0)]
  419. [InlineData (0, 1, 0)]
  420. [InlineData (1, 1, 0)]
  421. [InlineData (2, 1, 0)]
  422. [InlineData (3, 1, 0)]
  423. [InlineData (4, 1, 0)]
  424. [InlineData (5, 1, 0)]
  425. [InlineData (6, 1, 0)]
  426. [InlineData (7, 1, 0)]
  427. [InlineData (8, 1, 0)]
  428. [InlineData (9, 0, 0)]
  429. public void MouseClick_Button_CommandView_Raises_Shortcut_Accepted (int mouseX, int expectedAccept, int expectedButtonAccept)
  430. {
  431. Application.Top = new ();
  432. var shortcut = new Shortcut
  433. {
  434. Key = Key.A,
  435. Text = "0"
  436. };
  437. shortcut.CommandView = new Button
  438. {
  439. Title = "C",
  440. NoDecorations = true,
  441. NoPadding = true,
  442. CanFocus = false
  443. };
  444. var buttonAccepted = 0;
  445. shortcut.CommandView.Accepting += (s, e) => { buttonAccepted++; };
  446. Application.Top.Add (shortcut);
  447. Application.Top.SetRelativeLayout (new (100, 100));
  448. Application.Top.LayoutSubviews ();
  449. var accepted = 0;
  450. shortcut.Accepting += (s, e) => { accepted++; };
  451. Application.RaiseMouseEvent (
  452. new ()
  453. {
  454. ScreenPosition = new (mouseX, 0),
  455. Flags = MouseFlags.Button1Clicked
  456. });
  457. Assert.Equal (expectedAccept, accepted);
  458. Assert.Equal (expectedButtonAccept, buttonAccepted);
  459. Application.Top.Dispose ();
  460. Application.ResetState (true);
  461. }
  462. [Theory]
  463. // 01234567890
  464. // " ☑C 0 A "
  465. [InlineData (-1, 0, 0)]
  466. [InlineData (0, 1, 0)]
  467. [InlineData (1, 1, 0)]
  468. [InlineData (2, 1, 0)]
  469. [InlineData (3, 1, 0)]
  470. [InlineData (4, 1, 0)]
  471. [InlineData (5, 1, 0)]
  472. [InlineData (6, 1, 0)]
  473. [InlineData (7, 1, 0)]
  474. [InlineData (8, 1, 0)]
  475. [InlineData (9, 1, 0)]
  476. [InlineData (10, 1, 0)]
  477. public void MouseClick_CheckBox_CommandView_Raises_Shortcut_Accepted_Selected_Correctly (int mouseX, int expectedAccepted, int expectedCheckboxAccepted)
  478. {
  479. Application.Top = new ();
  480. var shortcut = new Shortcut
  481. {
  482. Key = Key.A,
  483. Text = "0"
  484. };
  485. shortcut.CommandView = new CheckBox
  486. {
  487. Title = "C",
  488. CanFocus = false
  489. };
  490. var checkboxAccepted = 0;
  491. shortcut.CommandView.Accepting += (s, e) => { checkboxAccepted++; };
  492. var checkboxSelected = 0;
  493. shortcut.CommandView.Selecting += (s, e) =>
  494. {
  495. if (e.Cancel)
  496. {
  497. return;
  498. }
  499. checkboxSelected++;
  500. };
  501. Application.Top.Add (shortcut);
  502. Application.Top.SetRelativeLayout (new (100, 100));
  503. Application.Top.LayoutSubviews ();
  504. var selected = 0;
  505. shortcut.Selecting += (s, e) =>
  506. {
  507. selected++;
  508. };
  509. var accepted = 0;
  510. shortcut.Accepting += (s, e) =>
  511. {
  512. accepted++;
  513. e.Cancel = true;
  514. };
  515. Application.RaiseMouseEvent (
  516. new ()
  517. {
  518. ScreenPosition = new (mouseX, 0),
  519. Flags = MouseFlags.Button1Clicked
  520. });
  521. Assert.Equal (expectedAccepted, accepted);
  522. Assert.Equal (expectedAccepted, selected);
  523. Assert.Equal (expectedCheckboxAccepted, checkboxAccepted);
  524. Assert.Equal (expectedCheckboxAccepted, checkboxSelected);
  525. Application.Top.Dispose ();
  526. Application.ResetState (true);
  527. }
  528. [Theory]
  529. [InlineData (true, KeyCode.A, 1, 1)]
  530. [InlineData (true, KeyCode.C, 1, 1)]
  531. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1, 1)]
  532. [InlineData (true, KeyCode.Enter, 1, 1)]
  533. [InlineData (true, KeyCode.Space, 1, 1)]
  534. [InlineData (true, KeyCode.F1, 0, 0)]
  535. [InlineData (false, KeyCode.A, 1, 1)]
  536. [InlineData (false, KeyCode.C, 1, 1)]
  537. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1, 1)]
  538. [InlineData (false, KeyCode.Enter, 0, 0)]
  539. [InlineData (false, KeyCode.Space, 0, 0)]
  540. [InlineData (false, KeyCode.F1, 0, 0)]
  541. public void KeyDown_Raises_Accepted_Selected (bool canFocus, KeyCode key, int expectedAccept, int expectedSelect)
  542. {
  543. Application.Top = new ();
  544. var shortcut = new Shortcut
  545. {
  546. Key = Key.A,
  547. Text = "0",
  548. Title = "_C",
  549. CanFocus = canFocus
  550. };
  551. Application.Top.Add (shortcut);
  552. shortcut.SetFocus ();
  553. Assert.Equal (canFocus, shortcut.HasFocus);
  554. var accepted = 0;
  555. shortcut.Accepting += (s, e) => accepted++;
  556. var selected = 0;
  557. shortcut.Selecting += (s, e) => selected++;
  558. Application.RaiseKeyDownEvent (key);
  559. Assert.Equal (expectedAccept, accepted);
  560. Assert.Equal (expectedSelect, selected);
  561. Application.Top.Dispose ();
  562. Application.ResetState (true);
  563. }
  564. [Theory]
  565. [InlineData (true, KeyCode.A, 1, 1)]
  566. [InlineData (true, KeyCode.C, 1, 1)]
  567. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1, 1)]
  568. [InlineData (true, KeyCode.Enter, 1, 1)]
  569. [InlineData (true, KeyCode.Space, 1, 1)]
  570. [InlineData (true, KeyCode.F1, 0, 0)]
  571. [InlineData (false, KeyCode.A, 1, 1)]
  572. [InlineData (false, KeyCode.C, 1, 1)]
  573. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1, 1)]
  574. [InlineData (false, KeyCode.Enter, 0, 0)]
  575. [InlineData (false, KeyCode.Space, 0, 0)]
  576. [InlineData (false, KeyCode.F1, 0, 0)]
  577. public void KeyDown_CheckBox_Raises_Accepted_Selected (bool canFocus, KeyCode key, int expectedAccept, int expectedSelect)
  578. {
  579. Application.Top = new ();
  580. var shortcut = new Shortcut
  581. {
  582. Key = Key.A,
  583. Text = "0",
  584. CommandView = new CheckBox ()
  585. {
  586. Title = "_C"
  587. },
  588. CanFocus = canFocus
  589. };
  590. Application.Top.Add (shortcut);
  591. shortcut.SetFocus ();
  592. Assert.Equal (canFocus, shortcut.HasFocus);
  593. var accepted = 0;
  594. shortcut.Accepting += (s, e) =>
  595. {
  596. accepted++;
  597. e.Cancel = true;
  598. };
  599. var selected = 0;
  600. shortcut.Selecting += (s, e) => selected++;
  601. Application.RaiseKeyDownEvent (key);
  602. Assert.Equal (expectedAccept, accepted);
  603. Assert.Equal (expectedSelect, selected);
  604. Application.Top.Dispose ();
  605. Application.ResetState (true);
  606. }
  607. [Theory]
  608. [InlineData (KeyCode.A, 1)]
  609. [InlineData (KeyCode.C, 1)]
  610. [InlineData (KeyCode.C | KeyCode.AltMask, 1)]
  611. [InlineData (KeyCode.Enter, 1)]
  612. [InlineData (KeyCode.Space, 1)]
  613. [InlineData (KeyCode.F1, 0)]
  614. public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept)
  615. {
  616. Application.Top = new ();
  617. var shortcut = new Shortcut
  618. {
  619. Key = Key.A,
  620. KeyBindingScope = KeyBindingScope.Application,
  621. Text = "0",
  622. Title = "_C"
  623. };
  624. Application.Top.Add (shortcut);
  625. Application.Top.SetFocus ();
  626. var accepted = 0;
  627. shortcut.Accepting += (s, e) => accepted++;
  628. Application.RaiseKeyDownEvent (key);
  629. Assert.Equal (expectedAccept, accepted);
  630. Application.Top.Dispose ();
  631. Application.ResetState (true);
  632. }
  633. [Theory]
  634. [InlineData (true, KeyCode.A, 1)]
  635. [InlineData (true, KeyCode.C, 1)]
  636. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  637. [InlineData (true, KeyCode.Enter, 1)]
  638. [InlineData (true, KeyCode.Space, 1)]
  639. [InlineData (true, KeyCode.F1, 0)]
  640. [InlineData (false, KeyCode.A, 1)]
  641. [InlineData (false, KeyCode.C, 1)]
  642. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  643. [InlineData (false, KeyCode.Enter, 0)]
  644. [InlineData (false, KeyCode.Space, 0)]
  645. [InlineData (false, KeyCode.F1, 0)]
  646. [AutoInitShutdown]
  647. public void KeyDown_Invokes_Action (bool canFocus, KeyCode key, int expectedAction)
  648. {
  649. var current = new Toplevel ();
  650. var shortcut = new Shortcut
  651. {
  652. Key = Key.A,
  653. Text = "0",
  654. Title = "_C",
  655. CanFocus = canFocus
  656. };
  657. current.Add (shortcut);
  658. Application.Begin (current);
  659. Assert.Equal (canFocus, shortcut.HasFocus);
  660. var action = 0;
  661. shortcut.Action += () => action++;
  662. Application.RaiseKeyDownEvent (key);
  663. Assert.Equal (expectedAction, action);
  664. current.Dispose ();
  665. }
  666. [Theory]
  667. [InlineData (true, KeyCode.A, 1)]
  668. [InlineData (true, KeyCode.C, 1)]
  669. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  670. [InlineData (true, KeyCode.Enter, 1)]
  671. [InlineData (true, KeyCode.Space, 1)]
  672. [InlineData (true, KeyCode.F1, 0)]
  673. [InlineData (false, KeyCode.A, 1)]
  674. [InlineData (false, KeyCode.C, 1)]
  675. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  676. [InlineData (false, KeyCode.Enter, 0)]
  677. [InlineData (false, KeyCode.Space, 0)]
  678. [InlineData (false, KeyCode.F1, 0)]
  679. public void KeyDown_App_Scope_Invokes_Action (bool canFocus, KeyCode key, int expectedAction)
  680. {
  681. Application.Top = new ();
  682. var shortcut = new Shortcut
  683. {
  684. Key = Key.A,
  685. KeyBindingScope = KeyBindingScope.Application,
  686. Text = "0",
  687. Title = "_C",
  688. CanFocus = canFocus
  689. };
  690. Application.Top.Add (shortcut);
  691. Application.Top.SetFocus ();
  692. var action = 0;
  693. shortcut.Action += () => action++;
  694. Application.RaiseKeyDownEvent (key);
  695. Assert.Equal (expectedAction, action);
  696. Application.Top.Dispose ();
  697. Application.ResetState (true);
  698. }
  699. [Fact]
  700. public void ColorScheme_SetsAndGetsCorrectly ()
  701. {
  702. var colorScheme = new ColorScheme ();
  703. var shortcut = new Shortcut
  704. {
  705. ColorScheme = colorScheme
  706. };
  707. Assert.Same (colorScheme, shortcut.ColorScheme);
  708. }
  709. // https://github.com/gui-cs/Terminal.Gui/issues/3664
  710. [Fact]
  711. public void ColorScheme_SetColorScheme_Does_Not_Fault_3664 ()
  712. {
  713. Application.Top = new ();
  714. Application.Navigation = new ();
  715. var shortcut = new Shortcut ();
  716. Application.Top.ColorScheme = null;
  717. Assert.Null (shortcut.ColorScheme);
  718. shortcut.HasFocus = true;
  719. Assert.NotNull (shortcut.ColorScheme);
  720. Application.Top.Dispose ();
  721. Application.ResetState ();
  722. }
  723. }