ShortcutTests.cs 26 KB

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