ShortcutTests.cs 21 KB

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