ShortcutTests.cs 26 KB

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