ShortcutTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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 Shortcut ()
  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 Shortcut ()
  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. Shortcut 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. Shortcut 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. [AutoInitShutdown]
  333. public void MouseClick_Fires_Accept (int x, int expectedAccept)
  334. {
  335. var current = new Toplevel ();
  336. var shortcut = new Shortcut
  337. {
  338. Key = Key.A,
  339. Text = "0",
  340. Title = "C"
  341. };
  342. current.Add (shortcut);
  343. Application.Begin (current);
  344. var accepted = 0;
  345. shortcut.Accepted += (s, e) => accepted++;
  346. Application.OnMouseEvent (
  347. new ()
  348. {
  349. ScreenPosition = new (x, 0),
  350. Flags = MouseFlags.Button1Clicked
  351. });
  352. Assert.Equal (expectedAccept, accepted);
  353. current.Dispose ();
  354. }
  355. [Theory]
  356. // 0123456789
  357. // " C 0 A "
  358. [InlineData (-1, 0, 0, 0, 0)]
  359. [InlineData (0, 0, 1, 1, 1)] // mouseX = 0 is on the CommandView.Margin, so Shortcut will get MouseClick
  360. [InlineData (1, 0, 1, 1, 1)] // mouseX = 1 is on the CommandView, so CommandView will get MouseClick
  361. [InlineData (2, 1, 1, 1, 1)] // mouseX = 2 is on the CommandView.Margin, so Shortcut will get MouseClick
  362. [InlineData (3, 0, 1, 1, 1)]
  363. [InlineData (4, 0, 1, 1, 1)]
  364. [InlineData (5, 0, 1, 1, 1)]
  365. [InlineData (6, 0, 1, 1, 1)]
  366. [InlineData (7, 0, 1, 1, 1)]
  367. [InlineData (8, 0, 1, 1, 1)]
  368. [InlineData (9, 0, 0, 0, 0)]
  369. //[InlineData (1, 1, 1)]
  370. //[InlineData (2, 1, 0)]
  371. //[InlineData (3, 1, 0)]
  372. //[InlineData (4, 1, 0)]
  373. //[InlineData (5, 1, 0)]
  374. //[InlineData (6, 1, 0)]
  375. //[InlineData (7, 1, 0)]
  376. //[InlineData (8, 1, 0)]
  377. //[InlineData (9, 0, 0)]
  378. public void MouseClick_Default_CommandView_Raises_Accept_Select_Correctly (int mouseX, int expectedCommandViewAccept, int expectedCommandViewSelect,
  379. int expectedShortcutAccept, int expectedShortcutSelect)
  380. {
  381. Application.Top = new Toplevel ();
  382. var shortcut = new Shortcut
  383. {
  384. Title = "C",
  385. Key = Key.A,
  386. HelpText = "0"
  387. };
  388. var commandViewAcceptCount = 0;
  389. shortcut.CommandView.Accepted += (s, e) =>
  390. {
  391. commandViewAcceptCount++;
  392. };
  393. var commandViewSelectCount = 0;
  394. shortcut.CommandView.Selected += (s, e) =>
  395. {
  396. commandViewSelectCount++;
  397. };
  398. var shortcutAcceptCount = 0;
  399. shortcut.Accepted += (s, e) =>
  400. {
  401. shortcutAcceptCount++;
  402. };
  403. var shortcutSelectCount = 0;
  404. shortcut.Selected += (s, e) =>
  405. {
  406. shortcutSelectCount++;
  407. };
  408. Application.Top.Add (shortcut);
  409. Application.Top.SetRelativeLayout (new (100, 100));
  410. Application.Top.LayoutSubviews();
  411. Application.OnMouseEvent (
  412. new ()
  413. {
  414. ScreenPosition = new (mouseX, 0),
  415. Flags = MouseFlags.Button1Clicked
  416. });
  417. Assert.Equal (expectedShortcutAccept, shortcutAcceptCount);
  418. Assert.Equal (expectedShortcutSelect, shortcutSelectCount);
  419. Assert.Equal (expectedCommandViewAccept, commandViewAcceptCount);
  420. Assert.Equal (expectedCommandViewSelect, commandViewSelectCount);
  421. Application.Top.Dispose ();
  422. Application.ResetState (true);
  423. }
  424. [Theory]
  425. // 0123456789
  426. // " C 0 A "
  427. [InlineData (-1, 0, 0)]
  428. [InlineData (0, 1, 0)]
  429. [InlineData (1, 1, 1)]
  430. [InlineData (2, 1, 0)]
  431. [InlineData (3, 1, 0)]
  432. [InlineData (4, 1, 0)]
  433. [InlineData (5, 1, 0)]
  434. [InlineData (6, 1, 0)]
  435. [InlineData (7, 1, 0)]
  436. [InlineData (8, 1, 0)]
  437. [InlineData (9, 0, 0)]
  438. public void MouseClick_Button_CommandView_Raises_Shortcut_Accept
  439. (int mouseX, int expectedAccept, int expectedButtonAccept)
  440. {
  441. Application.Top = new Toplevel ();
  442. var shortcut = new Shortcut
  443. {
  444. Key = Key.A,
  445. Text = "0"
  446. };
  447. shortcut.CommandView = new Button
  448. {
  449. Title = "C",
  450. NoDecorations = true,
  451. NoPadding = true,
  452. CanFocus = false
  453. };
  454. var buttonAccepted = 0;
  455. shortcut.CommandView.Accepted += (s, e) =>
  456. {
  457. buttonAccepted++;
  458. // Must indicate handled
  459. e.Handled = true;
  460. };
  461. Application.Top.Add (shortcut);
  462. var accepted = 0;
  463. shortcut.Accepted += (s, e) => accepted++;
  464. //Assert.True (shortcut.HasFocus);
  465. Application.OnMouseEvent (
  466. new ()
  467. {
  468. ScreenPosition = new (mouseX, 0),
  469. Flags = MouseFlags.Button1Clicked
  470. });
  471. Assert.Equal (expectedButtonAccept, buttonAccepted);
  472. Assert.Equal (expectedAccept, accepted);
  473. Application.Top.Dispose ();
  474. Application.ResetState (true);
  475. }
  476. [Theory]
  477. [InlineData (true, KeyCode.A, 1)]
  478. [InlineData (true, KeyCode.C, 1)]
  479. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  480. [InlineData (true, KeyCode.Enter, 1)]
  481. [InlineData (true, KeyCode.Space, 0)]
  482. [InlineData (true, KeyCode.F1, 0)]
  483. [InlineData (false, KeyCode.A, 1)]
  484. [InlineData (false, KeyCode.C, 1)]
  485. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  486. [InlineData (false, KeyCode.Enter, 0)]
  487. [InlineData (false, KeyCode.Space, 0)]
  488. [InlineData (false, KeyCode.F1, 0)]
  489. [AutoInitShutdown]
  490. public void KeyDown_Invokes_Accept (bool canFocus, KeyCode key, int expectedAccept)
  491. {
  492. var current = new Toplevel ();
  493. var shortcut = new Shortcut
  494. {
  495. Key = Key.A,
  496. Text = "0",
  497. Title = "_C",
  498. CanFocus = canFocus
  499. };
  500. current.Add (shortcut);
  501. Application.Begin (current);
  502. Assert.Equal (canFocus, shortcut.HasFocus);
  503. var accepted = 0;
  504. shortcut.Accepted += (s, e) => accepted++;
  505. Application.OnKeyDown (key);
  506. Assert.Equal (expectedAccept, accepted);
  507. current.Dispose ();
  508. }
  509. [Theory]
  510. [InlineData (KeyCode.A, 1)]
  511. [InlineData (KeyCode.C, 1)]
  512. [InlineData (KeyCode.C | KeyCode.AltMask, 1)]
  513. [InlineData (KeyCode.Enter, 1)]
  514. [InlineData (KeyCode.Space, 0)]
  515. [InlineData (KeyCode.F1, 0)]
  516. public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept)
  517. {
  518. Application.Top = new Toplevel ();
  519. var shortcut = new Shortcut
  520. {
  521. Key = Key.A,
  522. KeyBindingScope = KeyBindingScope.Application,
  523. Text = "0",
  524. Title = "_C"
  525. };
  526. Application.Top.Add (shortcut);
  527. Application.Top.SetFocus ();
  528. var accepted = 0;
  529. shortcut.Accepted += (s, e) => accepted++;
  530. Application.OnKeyDown (key);
  531. Assert.Equal (expectedAccept, accepted);
  532. Application.Top.Dispose ();
  533. Application.ResetState (true);
  534. }
  535. [Theory]
  536. [InlineData (true, KeyCode.A, 1)]
  537. [InlineData (true, KeyCode.C, 1)]
  538. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  539. [InlineData (true, KeyCode.Enter, 1)]
  540. [InlineData (true, KeyCode.Space, 0)]
  541. [InlineData (true, KeyCode.F1, 0)]
  542. [InlineData (false, KeyCode.A, 1)]
  543. [InlineData (false, KeyCode.C, 1)]
  544. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  545. [InlineData (false, KeyCode.Enter, 0)]
  546. [InlineData (false, KeyCode.Space, 0)]
  547. [InlineData (false, KeyCode.F1, 0)]
  548. [AutoInitShutdown]
  549. public void KeyDown_Invokes_Action (bool canFocus, KeyCode key, int expectedAction)
  550. {
  551. var current = new Toplevel ();
  552. var shortcut = new Shortcut
  553. {
  554. Key = Key.A,
  555. Text = "0",
  556. Title = "_C",
  557. CanFocus = canFocus
  558. };
  559. current.Add (shortcut);
  560. Application.Begin (current);
  561. Assert.Equal (canFocus, shortcut.HasFocus);
  562. var action = 0;
  563. shortcut.Action += () => action++;
  564. Application.OnKeyDown (key);
  565. Assert.Equal (expectedAction, action);
  566. current.Dispose ();
  567. }
  568. [Theory]
  569. [InlineData (true, KeyCode.A, 1)]
  570. [InlineData (true, KeyCode.C, 1)]
  571. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  572. [InlineData (true, KeyCode.Enter, 1)]
  573. [InlineData (true, KeyCode.Space, 0)]
  574. [InlineData (true, KeyCode.F1, 0)]
  575. [InlineData (false, KeyCode.A, 1)]
  576. [InlineData (false, KeyCode.C, 1)]
  577. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  578. [InlineData (false, KeyCode.Enter, 0)]
  579. [InlineData (false, KeyCode.Space, 0)]
  580. [InlineData (false, KeyCode.F1, 0)]
  581. public void KeyDown_App_Scope_Invokes_Action (bool canFocus, KeyCode key, int expectedAction)
  582. {
  583. Application.Top = new Toplevel ();
  584. var shortcut = new Shortcut
  585. {
  586. Key = Key.A,
  587. KeyBindingScope = KeyBindingScope.Application,
  588. Text = "0",
  589. Title = "_C",
  590. CanFocus = canFocus
  591. };
  592. Application.Top.Add (shortcut);
  593. Application.Top.SetFocus ();
  594. var action = 0;
  595. shortcut.Action += () => action++;
  596. Application.OnKeyDown (key);
  597. Assert.Equal (expectedAction, action);
  598. Application.Top.Dispose ();
  599. Application.ResetState (true);
  600. }
  601. [Fact]
  602. public void ColorScheme_SetsAndGetsCorrectly ()
  603. {
  604. var colorScheme = new ColorScheme ();
  605. var shortcut = new Shortcut
  606. {
  607. ColorScheme = colorScheme
  608. };
  609. Assert.Same (colorScheme, shortcut.ColorScheme);
  610. }
  611. // https://github.com/gui-cs/Terminal.Gui/issues/3664
  612. [Fact]
  613. public void ColorScheme_SetColorScheme_Does_Not_Fault_3664 ()
  614. {
  615. Application.Top = new ();
  616. Application.Navigation = new ();
  617. Shortcut shortcut = new Shortcut ();
  618. Application.Top.ColorScheme = null;
  619. Assert.Null (shortcut.ColorScheme);
  620. shortcut.HasFocus = true;
  621. Assert.NotNull (shortcut.ColorScheme);
  622. Application.Top.Dispose ();
  623. Application.ResetState ();
  624. }
  625. }