ShortcutTests.cs 26 KB

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