ShortcutTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. // TOOD: more
  15. }
  16. [Theory]
  17. [InlineData ("", "", KeyCode.Null, 2)]
  18. [InlineData ("C", "", KeyCode.Null, 3)]
  19. [InlineData ("", "H", KeyCode.Null, 5)]
  20. [InlineData ("", "", KeyCode.K, 5)]
  21. [InlineData ("C", "", KeyCode.K, 6)]
  22. [InlineData ("C", "H", KeyCode.Null, 6)]
  23. [InlineData ("", "H", KeyCode.K, 8)]
  24. [InlineData ("C", "H", KeyCode.K, 9)]
  25. public void NaturalSize (string command, string help, KeyCode key, int expectedWidth)
  26. {
  27. var shortcut = new Shortcut
  28. {
  29. Title = command,
  30. HelpText = help,
  31. Key = key
  32. };
  33. Assert.IsType<DimAuto> (shortcut.Width);
  34. Assert.IsType<DimAuto> (shortcut.Height);
  35. //shortcut.BeginInit();
  36. //shortcut.EndInit ();
  37. // shortcut.LayoutSubviews ();
  38. shortcut.SetRelativeLayout (new (100, 100));
  39. // |0123456789
  40. // | C H K |
  41. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  42. }
  43. [Theory]
  44. [InlineData (5, 0, 3, 6)]
  45. [InlineData (6, 0, 3, 6)]
  46. [InlineData (7, 0, 3, 6)]
  47. [InlineData (8, 0, 3, 6)]
  48. [InlineData (9, 0, 3, 6)]
  49. [InlineData (10, 0, 4, 7)]
  50. [InlineData (11, 0, 5, 8)]
  51. public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
  52. {
  53. var shortcut = new Shortcut
  54. {
  55. Width = width,
  56. Title = "C",
  57. Text = "H",
  58. Key = Key.K
  59. };
  60. shortcut.LayoutSubviews ();
  61. shortcut.SetRelativeLayout (new (100, 100));
  62. // 0123456789
  63. // -C--H--K-
  64. Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
  65. Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
  66. Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
  67. }
  68. [Fact]
  69. public void CommandView_Text_And_Title_Track ()
  70. {
  71. var shortcut = new Shortcut
  72. {
  73. Title = "T"
  74. };
  75. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  76. shortcut = new ();
  77. shortcut.CommandView = new ()
  78. {
  79. Text = "T"
  80. };
  81. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  82. }
  83. [Fact]
  84. public void HelpText_And_Text_Are_The_Same ()
  85. {
  86. var shortcut = new Shortcut
  87. {
  88. Text = "H"
  89. };
  90. Assert.Equal (shortcut.Text, shortcut.HelpText);
  91. shortcut = new ()
  92. {
  93. HelpText = "H"
  94. };
  95. Assert.Equal (shortcut.Text, shortcut.HelpText);
  96. }
  97. [Theory]
  98. [InlineData (KeyCode.Null, "")]
  99. [InlineData (KeyCode.F1, "F1")]
  100. public void KeyView_Text_Tracks_Key (KeyCode key, string expected)
  101. {
  102. var shortcut = new Shortcut
  103. {
  104. Key = key
  105. };
  106. Assert.Equal (expected, shortcut.KeyView.Text);
  107. }
  108. // Test Key
  109. [Fact]
  110. public void Key_Defaults_To_Empty ()
  111. {
  112. var shortcut = new Shortcut ();
  113. Assert.Equal (Key.Empty, shortcut.Key);
  114. }
  115. [Fact]
  116. public void Key_Can_Be_Set ()
  117. {
  118. var shortcut = new Shortcut ();
  119. shortcut.Key = Key.F1;
  120. Assert.Equal (Key.F1, shortcut.Key);
  121. }
  122. [Fact]
  123. public void Key_Can_Be_Set_To_Empty ()
  124. {
  125. var shortcut = new Shortcut ();
  126. shortcut.Key = Key.Empty;
  127. Assert.Equal (Key.Empty, shortcut.Key);
  128. }
  129. [Fact]
  130. public void Key_Set_Binds_Key_To_CommandView_Accept ()
  131. {
  132. var shortcut = new Shortcut ();
  133. shortcut.Key = Key.F1;
  134. // TODO:
  135. }
  136. [Fact]
  137. public void Key_Changing_Removes_Previous_Binding ()
  138. {
  139. Shortcut shortcut = new Shortcut ();
  140. shortcut.Key = Key.A;
  141. Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
  142. shortcut.Key = Key.B;
  143. Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
  144. Assert.Contains (Key.B, shortcut.KeyBindings.Bindings.Keys);
  145. }
  146. // Test Key gets bound correctly
  147. [Fact]
  148. public void KeyBindingScope_Defaults_To_HotKey ()
  149. {
  150. var shortcut = new Shortcut ();
  151. Assert.Equal (KeyBindingScope.HotKey, shortcut.KeyBindingScope);
  152. }
  153. [Fact]
  154. public void KeyBindingScope_Can_Be_Set ()
  155. {
  156. var shortcut = new Shortcut ();
  157. shortcut.KeyBindingScope = KeyBindingScope.Application;
  158. Assert.Equal (KeyBindingScope.Application, shortcut.KeyBindingScope);
  159. }
  160. [Fact]
  161. public void KeyBindingScope_Changing_Adjusts_KeyBindings ()
  162. {
  163. Shortcut shortcut = new Shortcut ();
  164. shortcut.Key = Key.A;
  165. Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
  166. shortcut.KeyBindingScope = KeyBindingScope.Application;
  167. Assert.DoesNotContain (Key.A, shortcut.KeyBindings.Bindings.Keys);
  168. Assert.Contains (Key.A, Application.KeyBindings.Bindings.Keys);
  169. shortcut.KeyBindingScope = KeyBindingScope.HotKey;
  170. Assert.Contains (Key.A, shortcut.KeyBindings.Bindings.Keys);
  171. Assert.DoesNotContain (Key.A, Application.KeyBindings.Bindings.Keys);
  172. }
  173. [Theory]
  174. [InlineData (Orientation.Horizontal)]
  175. [InlineData (Orientation.Vertical)]
  176. public void Orientation_SetsCorrectly (Orientation orientation)
  177. {
  178. var shortcut = new Shortcut
  179. {
  180. Orientation = orientation
  181. };
  182. Assert.Equal (orientation, shortcut.Orientation);
  183. }
  184. [Theory]
  185. [InlineData (AlignmentModes.StartToEnd)]
  186. [InlineData (AlignmentModes.EndToStart)]
  187. public void AlignmentModes_SetsCorrectly (AlignmentModes alignmentModes)
  188. {
  189. var shortcut = new Shortcut
  190. {
  191. AlignmentModes = alignmentModes
  192. };
  193. Assert.Equal (alignmentModes, shortcut.AlignmentModes);
  194. }
  195. [Fact]
  196. public void Action_SetsAndGetsCorrectly ()
  197. {
  198. var actionInvoked = false;
  199. var shortcut = new Shortcut
  200. {
  201. Action = () => { actionInvoked = true; }
  202. };
  203. shortcut.Action.Invoke ();
  204. Assert.True (actionInvoked);
  205. }
  206. [Fact]
  207. public void ColorScheme_SetsAndGetsCorrectly ()
  208. {
  209. var colorScheme = new ColorScheme ();
  210. var shortcut = new Shortcut
  211. {
  212. ColorScheme = colorScheme
  213. };
  214. Assert.Same (colorScheme, shortcut.ColorScheme);
  215. }
  216. [Fact]
  217. public void Subview_Visibility_Controlled_By_Removal ()
  218. {
  219. var shortcut = new Shortcut ();
  220. Assert.True (shortcut.CommandView.Visible);
  221. Assert.Contains (shortcut.CommandView, shortcut.Subviews);
  222. Assert.True (shortcut.HelpView.Visible);
  223. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  224. Assert.True (shortcut.KeyView.Visible);
  225. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  226. shortcut.HelpText = "help";
  227. Assert.True (shortcut.HelpView.Visible);
  228. Assert.Contains (shortcut.HelpView, shortcut.Subviews);
  229. Assert.True (shortcut.KeyView.Visible);
  230. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  231. shortcut.Key = Key.A;
  232. Assert.True (shortcut.HelpView.Visible);
  233. Assert.Contains (shortcut.HelpView, shortcut.Subviews);
  234. Assert.True (shortcut.KeyView.Visible);
  235. Assert.Contains (shortcut.KeyView, shortcut.Subviews);
  236. shortcut.HelpView.Visible = false;
  237. shortcut.ShowHide ();
  238. Assert.False (shortcut.HelpView.Visible);
  239. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  240. Assert.True (shortcut.KeyView.Visible);
  241. Assert.Contains (shortcut.KeyView, shortcut.Subviews);
  242. shortcut.KeyView.Visible = false;
  243. shortcut.ShowHide ();
  244. Assert.False (shortcut.HelpView.Visible);
  245. Assert.DoesNotContain (shortcut.HelpView, shortcut.Subviews);
  246. Assert.False (shortcut.KeyView.Visible);
  247. Assert.DoesNotContain (shortcut.KeyView, shortcut.Subviews);
  248. }
  249. [Fact]
  250. public void Focus_CanFocus_Default_Is_True ()
  251. {
  252. Shortcut shortcut = new ();
  253. shortcut.Key = Key.A;
  254. shortcut.Text = "Help";
  255. shortcut.Title = "Command";
  256. Assert.True (shortcut.CanFocus);
  257. Assert.False (shortcut.CommandView.CanFocus);
  258. }
  259. [Fact]
  260. public void Focus_CanFocus_CommandView_Add_Tracks ()
  261. {
  262. Shortcut shortcut = new ();
  263. Assert.True (shortcut.CanFocus);
  264. Assert.False (shortcut.CommandView.CanFocus);
  265. shortcut.CommandView = new () { CanFocus = true };
  266. Assert.False (shortcut.CommandView.CanFocus);
  267. shortcut.CommandView.CanFocus = true;
  268. Assert.True (shortcut.CommandView.CanFocus);
  269. shortcut.CanFocus = false;
  270. Assert.False (shortcut.CanFocus);
  271. Assert.True (shortcut.CommandView.CanFocus);
  272. shortcut.CommandView.CanFocus = false;
  273. Assert.False (shortcut.CanFocus);
  274. Assert.False (shortcut.CommandView.CanFocus);
  275. shortcut.CommandView.CanFocus = true;
  276. Assert.False (shortcut.CanFocus);
  277. Assert.True (shortcut.CommandView.CanFocus);
  278. }
  279. [Theory]
  280. // 0123456789
  281. // " C 0 A "
  282. [InlineData (-1, 0)]
  283. [InlineData (0, 1)]
  284. [InlineData (1, 1)]
  285. [InlineData (2, 1)]
  286. [InlineData (3, 1)]
  287. [InlineData (4, 1)]
  288. [InlineData (5, 1)]
  289. [InlineData (6, 1)]
  290. [InlineData (7, 1)]
  291. [InlineData (8, 1)]
  292. [InlineData (9, 0)]
  293. [AutoInitShutdown]
  294. public void MouseClick_Fires_Accept (int x, int expectedAccept)
  295. {
  296. var current = new Toplevel ();
  297. var shortcut = new Shortcut
  298. {
  299. Key = Key.A,
  300. Text = "0",
  301. Title = "C"
  302. };
  303. current.Add (shortcut);
  304. Application.Begin (current);
  305. var accepted = 0;
  306. shortcut.Accept += (s, e) => accepted++;
  307. Application.OnMouseEvent (
  308. new()
  309. {
  310. Position = new (x, 0),
  311. Flags = MouseFlags.Button1Clicked
  312. });
  313. Assert.Equal (expectedAccept, accepted);
  314. current.Dispose ();
  315. }
  316. [Theory]
  317. // 0123456789
  318. // " C 0 A "
  319. [InlineData (-1, 0, 0)]
  320. [InlineData (0, 1, 1)]
  321. [InlineData (1, 1, 1, Skip = "BUGBUG: This breaks. We need to fix the logic in the Shortcut class.")]
  322. [InlineData (2, 1, 1)]
  323. [InlineData (3, 1, 1)]
  324. [InlineData (4, 1, 1)]
  325. [InlineData (5, 1, 1)]
  326. [InlineData (6, 1, 1)]
  327. [InlineData (7, 1, 1)]
  328. [InlineData (8, 1, 1)]
  329. [InlineData (9, 0, 0)]
  330. [AutoInitShutdown]
  331. public void MouseClick_Button_CommandView_Fires_Accept (int x, int expectedAccept, int expectedButtonAccept)
  332. {
  333. var current = new Toplevel ();
  334. var shortcut = new Shortcut
  335. {
  336. Key = Key.A,
  337. Text = "0"
  338. };
  339. shortcut.CommandView = new Button
  340. {
  341. Title = "C",
  342. NoDecorations = true,
  343. NoPadding = true
  344. };
  345. var buttonAccepted = 0;
  346. shortcut.CommandView.Accept += (s, e) => { buttonAccepted++; };
  347. current.Add (shortcut);
  348. Application.Begin (current);
  349. var accepted = 0;
  350. shortcut.Accept += (s, e) => accepted++;
  351. //Assert.True (shortcut.HasFocus);
  352. Application.OnMouseEvent (
  353. new()
  354. {
  355. Position = new (x, 0),
  356. Flags = MouseFlags.Button1Clicked
  357. });
  358. Assert.Equal (expectedAccept, accepted);
  359. Assert.Equal (expectedButtonAccept, buttonAccepted);
  360. current.Dispose ();
  361. }
  362. [Theory]
  363. [InlineData (true, KeyCode.A, 1)]
  364. [InlineData (true, KeyCode.C, 1)]
  365. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  366. [InlineData (true, KeyCode.Enter, 1)]
  367. [InlineData (true, KeyCode.Space, 0)]
  368. [InlineData (true, KeyCode.F1, 0)]
  369. [InlineData (false, KeyCode.A, 1)]
  370. [InlineData (false, KeyCode.C, 1)]
  371. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  372. [InlineData (false, KeyCode.Enter, 0)]
  373. [InlineData (false, KeyCode.Space, 0)]
  374. [InlineData (false, KeyCode.F1, 0)]
  375. [AutoInitShutdown]
  376. public void KeyDown_Invokes_Accept (bool canFocus, KeyCode key, int expectedAccept)
  377. {
  378. var current = new Toplevel ();
  379. var shortcut = new Shortcut
  380. {
  381. Key = Key.A,
  382. Text = "0",
  383. Title = "_C",
  384. CanFocus = canFocus
  385. };
  386. current.Add (shortcut);
  387. Application.Begin (current);
  388. Assert.Equal (canFocus, shortcut.HasFocus);
  389. var accepted = 0;
  390. shortcut.Accept += (s, e) => accepted++;
  391. Application.OnKeyDown (key);
  392. Assert.Equal (expectedAccept, accepted);
  393. current.Dispose ();
  394. }
  395. [Theory]
  396. [InlineData (KeyCode.A, 1)]
  397. [InlineData (KeyCode.C, 1)]
  398. [InlineData (KeyCode.C | KeyCode.AltMask, 1)]
  399. [InlineData (KeyCode.Enter, 1)]
  400. [InlineData (KeyCode.Space, 0)]
  401. [InlineData (KeyCode.F1, 0)]
  402. [AutoInitShutdown]
  403. public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept)
  404. {
  405. var current = new Toplevel ();
  406. var shortcut = new Shortcut
  407. {
  408. Key = Key.A,
  409. KeyBindingScope = KeyBindingScope.Application,
  410. Text = "0",
  411. Title = "_C"
  412. };
  413. current.Add (shortcut);
  414. Application.Begin (current);
  415. var accepted = 0;
  416. shortcut.Accept += (s, e) => accepted++;
  417. Application.OnKeyDown (key);
  418. Assert.Equal (expectedAccept, accepted);
  419. current.Dispose ();
  420. }
  421. [Theory]
  422. [InlineData (true, KeyCode.A, 1)]
  423. [InlineData (true, KeyCode.C, 1)]
  424. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  425. [InlineData (true, KeyCode.Enter, 1)]
  426. [InlineData (true, KeyCode.Space, 0)]
  427. [InlineData (true, KeyCode.F1, 0)]
  428. [InlineData (false, KeyCode.A, 1)]
  429. [InlineData (false, KeyCode.C, 1)]
  430. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  431. [InlineData (false, KeyCode.Enter, 0)]
  432. [InlineData (false, KeyCode.Space, 0)]
  433. [InlineData (false, KeyCode.F1, 0)]
  434. [AutoInitShutdown]
  435. public void KeyDown_Invokes_Action (bool canFocus, KeyCode key, int expectedAction)
  436. {
  437. var current = new Toplevel ();
  438. var shortcut = new Shortcut
  439. {
  440. Key = Key.A,
  441. Text = "0",
  442. Title = "_C",
  443. CanFocus = canFocus
  444. };
  445. current.Add (shortcut);
  446. Application.Begin (current);
  447. Assert.Equal (canFocus, shortcut.HasFocus);
  448. var action = 0;
  449. shortcut.Action += () => action++;
  450. Application.OnKeyDown (key);
  451. Assert.Equal (expectedAction, action);
  452. current.Dispose ();
  453. }
  454. [Theory]
  455. [InlineData (true, KeyCode.A, 1)]
  456. [InlineData (true, KeyCode.C, 1)]
  457. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1)]
  458. [InlineData (true, KeyCode.Enter, 1)]
  459. [InlineData (true, KeyCode.Space, 0)]
  460. [InlineData (true, KeyCode.F1, 0)]
  461. [InlineData (false, KeyCode.A, 1)]
  462. [InlineData (false, KeyCode.C, 1)]
  463. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1)]
  464. [InlineData (false, KeyCode.Enter, 0)]
  465. [InlineData (false, KeyCode.Space, 0)]
  466. [InlineData (false, KeyCode.F1, 0)]
  467. [AutoInitShutdown]
  468. public void KeyDown_App_Scope_Invokes_Action (bool canFocus, KeyCode key, int expectedAction)
  469. {
  470. var current = new Toplevel ();
  471. var shortcut = new Shortcut
  472. {
  473. Key = Key.A,
  474. KeyBindingScope = KeyBindingScope.Application,
  475. Text = "0",
  476. Title = "_C",
  477. CanFocus = canFocus
  478. };
  479. current.Add (shortcut);
  480. Application.Begin (current);
  481. Assert.Equal (canFocus, shortcut.HasFocus);
  482. var action = 0;
  483. shortcut.Action += () => action++;
  484. Application.OnKeyDown (key);
  485. Assert.Equal (expectedAction, action);
  486. current.Dispose ();
  487. }
  488. }