ShortcutTests.cs 22 KB

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