ShortcutTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #nullable enable
  2. using JetBrains.Annotations;
  3. namespace ViewsTests;
  4. [TestSubject (typeof (Shortcut))]
  5. public class ShortcutTests
  6. {
  7. [Fact]
  8. public void Constructor_Defaults ()
  9. {
  10. var shortcut = new Shortcut ();
  11. Assert.NotNull (shortcut);
  12. Assert.True (shortcut.CanFocus);
  13. Assert.IsType<DimAuto> (shortcut.Width);
  14. Assert.IsType<DimAuto> (shortcut.Height);
  15. }
  16. [Fact]
  17. public void Size_Defaults ()
  18. {
  19. var shortcut = new Shortcut ();
  20. shortcut.Layout ();
  21. Assert.Equal (2, shortcut.Frame.Width);
  22. Assert.Equal (1, shortcut.Frame.Height);
  23. Assert.Equal (2, shortcut.Viewport.Width);
  24. Assert.Equal (1, shortcut.Viewport.Height);
  25. Assert.Equal (0, shortcut.CommandView.Viewport.Width);
  26. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  27. Assert.Equal (0, shortcut.HelpView.Viewport.Width);
  28. Assert.Equal (0, shortcut.HelpView.Viewport.Height);
  29. Assert.Equal (0, shortcut.KeyView.Viewport.Width);
  30. Assert.Equal (0, shortcut.KeyView.Viewport.Height);
  31. // 0123456789
  32. // " 0 A "
  33. shortcut = new ()
  34. {
  35. Key = Key.A,
  36. HelpText = "0"
  37. };
  38. shortcut.Layout ();
  39. Assert.Equal (8, shortcut.Frame.Width);
  40. Assert.Equal (1, shortcut.Frame.Height);
  41. Assert.Equal (8, shortcut.Viewport.Width);
  42. Assert.Equal (1, shortcut.Viewport.Height);
  43. Assert.Equal (0, shortcut.CommandView.Viewport.Width);
  44. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  45. Assert.Equal (1, shortcut.HelpView.Viewport.Width);
  46. Assert.Equal (1, shortcut.HelpView.Viewport.Height);
  47. Assert.Equal (1, shortcut.KeyView.Viewport.Width);
  48. Assert.Equal (1, shortcut.KeyView.Viewport.Height);
  49. // 0123456789
  50. // " C 0 A "
  51. shortcut = new ()
  52. {
  53. Title = "C",
  54. Key = Key.A,
  55. HelpText = "0"
  56. };
  57. shortcut.Layout ();
  58. Assert.Equal (9, shortcut.Frame.Width);
  59. Assert.Equal (1, shortcut.Frame.Height);
  60. Assert.Equal (9, shortcut.Viewport.Width);
  61. Assert.Equal (1, shortcut.Viewport.Height);
  62. Assert.Equal (1, shortcut.CommandView.Viewport.Width);
  63. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  64. Assert.Equal (1, shortcut.HelpView.Viewport.Width);
  65. Assert.Equal (1, shortcut.HelpView.Viewport.Height);
  66. Assert.Equal (1, shortcut.KeyView.Viewport.Width);
  67. Assert.Equal (1, shortcut.KeyView.Viewport.Height);
  68. }
  69. [Theory]
  70. [InlineData ("", "", KeyCode.Null, 2)]
  71. [InlineData ("C", "", KeyCode.Null, 3)]
  72. [InlineData ("", "H", KeyCode.Null, 5)]
  73. [InlineData ("", "", KeyCode.K, 5)]
  74. [InlineData ("C", "", KeyCode.K, 6)]
  75. [InlineData ("C", "H", KeyCode.Null, 6)]
  76. [InlineData ("", "H", KeyCode.K, 8)]
  77. [InlineData ("C", "H", KeyCode.K, 9)]
  78. public void NaturalSize (string command, string help, KeyCode key, int expectedWidth)
  79. {
  80. var shortcut = new Shortcut
  81. {
  82. HelpText = help,
  83. Key = key,
  84. Title = command
  85. };
  86. shortcut.Layout ();
  87. // |0123456789
  88. // | C H K |
  89. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  90. shortcut = new ()
  91. {
  92. HelpText = help,
  93. Title = command,
  94. Key = key
  95. };
  96. shortcut.Layout ();
  97. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  98. shortcut = new ()
  99. {
  100. HelpText = help,
  101. Key = key,
  102. Title = command
  103. };
  104. shortcut.Layout ();
  105. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  106. shortcut = new ()
  107. {
  108. Key = key,
  109. HelpText = help,
  110. Title = command
  111. };
  112. shortcut.Layout ();
  113. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  114. }
  115. [Theory]
  116. [InlineData (0, 0, 3, 3)]
  117. [InlineData (1, 0, 3, 3)]
  118. [InlineData (2, 0, 3, 3)]
  119. [InlineData (3, 0, 3, 3)]
  120. [InlineData (4, 0, 3, 3)]
  121. [InlineData (5, 0, 3, 3)]
  122. [InlineData (6, 0, 3, 3)]
  123. [InlineData (7, 0, 3, 4)]
  124. [InlineData (8, 0, 3, 5)]
  125. [InlineData (9, 0, 3, 6)]
  126. [InlineData (10, 0, 4, 7)]
  127. [InlineData (11, 0, 5, 8)]
  128. public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
  129. {
  130. var shortcut = new Shortcut
  131. {
  132. Width = width,
  133. Title = "C",
  134. Text = "H",
  135. Key = Key.K
  136. };
  137. shortcut.Layout ();
  138. // 01234
  139. // -C--K
  140. // 012345
  141. // -C--K-
  142. // 0123456
  143. // -C-H-K-
  144. // 01234567
  145. // -C--H-K-
  146. // 012345678
  147. // -C--H--K-
  148. // 0123456789
  149. // -C--H--K-
  150. Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
  151. Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
  152. Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
  153. }
  154. [Fact]
  155. public void CommandView_Text_And_Title_Track ()
  156. {
  157. var shortcut = new Shortcut
  158. {
  159. Title = "T"
  160. };
  161. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  162. shortcut = new ();
  163. shortcut.CommandView = new ()
  164. {
  165. Text = "T"
  166. };
  167. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  168. }
  169. [Fact]
  170. public void HelpText_And_Text_Are_The_Same ()
  171. {
  172. var shortcut = new Shortcut
  173. {
  174. Text = "H"
  175. };
  176. Assert.Equal (shortcut.Text, shortcut.HelpText);
  177. shortcut = new ()
  178. {
  179. HelpText = "H"
  180. };
  181. Assert.Equal (shortcut.Text, shortcut.HelpText);
  182. }
  183. [Theory]
  184. [InlineData (KeyCode.Null, "")]
  185. [InlineData (KeyCode.F1, "F1")]
  186. public void KeyView_Text_Tracks_Key (KeyCode key, string expected)
  187. {
  188. var shortcut = new Shortcut
  189. {
  190. Key = key
  191. };
  192. Assert.Equal (expected, shortcut.KeyView.Text);
  193. }
  194. // Test Key
  195. [Fact]
  196. public void Key_Defaults_To_Empty ()
  197. {
  198. var shortcut = new Shortcut ();
  199. Assert.Equal (Key.Empty, shortcut.Key);
  200. }
  201. [Fact]
  202. public void Key_Can_Be_Set ()
  203. {
  204. var shortcut = new Shortcut ();
  205. shortcut.Key = Key.F1;
  206. Assert.Equal (Key.F1, shortcut.Key);
  207. }
  208. [Fact]
  209. public void Key_Can_Be_Set_To_Empty ()
  210. {
  211. var shortcut = new Shortcut ();
  212. shortcut.Key = Key.Empty;
  213. Assert.Equal (Key.Empty, shortcut.Key);
  214. }
  215. [Fact]
  216. public void Key_Set_Binds_Key_To_CommandView_Accept ()
  217. {
  218. var shortcut = new Shortcut ();
  219. shortcut.Key = Key.F1;
  220. // TODO:
  221. }
  222. [Fact]
  223. public void Key_Changing_Removes_Previous_Binding ()
  224. {
  225. var shortcut = new Shortcut ();
  226. shortcut.Key = Key.A;
  227. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  228. shortcut.Key = Key.B;
  229. Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  230. Assert.True (shortcut.HotKeyBindings.TryGet (Key.B, out _));
  231. }
  232. // Test Key gets bound correctly
  233. [Fact]
  234. public void BindKeyToApplication_Defaults_To_HotKey ()
  235. {
  236. var shortcut = new Shortcut ();
  237. Assert.False (shortcut.BindKeyToApplication);
  238. }
  239. [Fact]
  240. public void BindKeyToApplication_Can_Be_Set ()
  241. {
  242. IApplication? app = Application.Create ();
  243. var shortcut = new Shortcut () { App = app };
  244. shortcut.BindKeyToApplication = true;
  245. Assert.True (shortcut.BindKeyToApplication);
  246. }
  247. [Fact]
  248. public void BindKeyToApplication_Changing_Adjusts_KeyBindings ()
  249. {
  250. var shortcut = new Shortcut ();
  251. shortcut.Key = Key.A;
  252. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  253. shortcut.App = Application.Create ();
  254. shortcut.BindKeyToApplication = true;
  255. shortcut.BeginInit ();
  256. shortcut.EndInit ();
  257. Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  258. Assert.True (shortcut.App?.Keyboard.KeyBindings.TryGet (Key.A, out _));
  259. shortcut.BindKeyToApplication = false;
  260. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  261. Assert.False (shortcut.App?.Keyboard.KeyBindings.TryGet (Key.A, out _));
  262. }
  263. [Theory]
  264. [InlineData (Orientation.Horizontal)]
  265. [InlineData (Orientation.Vertical)]
  266. public void Orientation_SetsCorrectly (Orientation orientation)
  267. {
  268. var shortcut = new Shortcut
  269. {
  270. Orientation = orientation
  271. };
  272. Assert.Equal (orientation, shortcut.Orientation);
  273. }
  274. [Theory]
  275. [InlineData (AlignmentModes.StartToEnd)]
  276. [InlineData (AlignmentModes.EndToStart)]
  277. public void AlignmentModes_SetsCorrectly (AlignmentModes alignmentModes)
  278. {
  279. var shortcut = new Shortcut
  280. {
  281. AlignmentModes = alignmentModes
  282. };
  283. Assert.Equal (alignmentModes, shortcut.AlignmentModes);
  284. }
  285. [Fact]
  286. public void Action_SetsAndGetsCorrectly ()
  287. {
  288. var actionInvoked = false;
  289. var shortcut = new Shortcut
  290. {
  291. Action = () => { actionInvoked = true; }
  292. };
  293. shortcut.Action.Invoke ();
  294. Assert.True (actionInvoked);
  295. }
  296. [Fact]
  297. public void SubView_Visibility_Controlled_By_Removal ()
  298. {
  299. var shortcut = new Shortcut ();
  300. Assert.True (shortcut.CommandView.Visible);
  301. Assert.Contains (shortcut.CommandView, shortcut.SubViews);
  302. Assert.True (shortcut.HelpView.Visible);
  303. Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
  304. Assert.True (shortcut.KeyView.Visible);
  305. Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
  306. shortcut.HelpText = "help";
  307. Assert.True (shortcut.HelpView.Visible);
  308. Assert.Contains (shortcut.HelpView, shortcut.SubViews);
  309. Assert.True (shortcut.KeyView.Visible);
  310. Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
  311. shortcut.Key = Key.A;
  312. Assert.True (shortcut.HelpView.Visible);
  313. Assert.Contains (shortcut.HelpView, shortcut.SubViews);
  314. Assert.True (shortcut.KeyView.Visible);
  315. Assert.Contains (shortcut.KeyView, shortcut.SubViews);
  316. shortcut.HelpView.Visible = false;
  317. shortcut.ShowHide ();
  318. Assert.False (shortcut.HelpView.Visible);
  319. Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
  320. Assert.True (shortcut.KeyView.Visible);
  321. Assert.Contains (shortcut.KeyView, shortcut.SubViews);
  322. shortcut.KeyView.Visible = false;
  323. shortcut.ShowHide ();
  324. Assert.False (shortcut.HelpView.Visible);
  325. Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
  326. Assert.False (shortcut.KeyView.Visible);
  327. Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
  328. }
  329. [Fact]
  330. public void Focus_CanFocus_Default_Is_True ()
  331. {
  332. Shortcut shortcut = new ();
  333. shortcut.Key = Key.A;
  334. shortcut.Text = "Help";
  335. shortcut.Title = "Command";
  336. Assert.True (shortcut.CanFocus);
  337. Assert.False (shortcut.CommandView.CanFocus);
  338. }
  339. [Fact]
  340. public void Focus_CanFocus_CommandView_Add_Tracks ()
  341. {
  342. Shortcut shortcut = new ();
  343. Assert.True (shortcut.CanFocus);
  344. Assert.False (shortcut.CommandView.CanFocus);
  345. shortcut.CommandView = new () { CanFocus = true };
  346. Assert.True (shortcut.CommandView.CanFocus);
  347. shortcut.CommandView.CanFocus = true;
  348. Assert.True (shortcut.CommandView.CanFocus);
  349. shortcut.CanFocus = false;
  350. Assert.False (shortcut.CanFocus);
  351. Assert.True (shortcut.CommandView.CanFocus);
  352. shortcut.CommandView.CanFocus = false;
  353. Assert.False (shortcut.CanFocus);
  354. Assert.False (shortcut.CommandView.CanFocus);
  355. shortcut.CommandView.CanFocus = true;
  356. Assert.False (shortcut.CanFocus);
  357. Assert.True (shortcut.CommandView.CanFocus);
  358. }
  359. [Theory (Skip = "Broke somehow!")]
  360. [InlineData (true, KeyCode.A, 1, 1)]
  361. [InlineData (true, KeyCode.C, 1, 1)]
  362. [InlineData (true, KeyCode.C | KeyCode.AltMask, 1, 1)]
  363. [InlineData (true, KeyCode.Enter, 1, 1)]
  364. [InlineData (true, KeyCode.Space, 1, 1)]
  365. [InlineData (true, KeyCode.F1, 0, 0)]
  366. [InlineData (false, KeyCode.A, 1, 1)]
  367. [InlineData (false, KeyCode.C, 1, 1)]
  368. [InlineData (false, KeyCode.C | KeyCode.AltMask, 1, 1)]
  369. [InlineData (false, KeyCode.Enter, 0, 0)]
  370. [InlineData (false, KeyCode.Space, 0, 0)]
  371. [InlineData (false, KeyCode.F1, 0, 0)]
  372. public void KeyDown_CheckBox_Raises_Accepted_Selected (bool canFocus, KeyCode key, int expectedAccept, int expectedSelect)
  373. {
  374. IApplication? app = Application.Create ();
  375. Runnable<bool> runnable = new ();
  376. app.Begin (runnable);
  377. var shortcut = new Shortcut
  378. {
  379. Key = Key.A,
  380. Text = "0",
  381. CommandView = new CheckBox ()
  382. {
  383. Title = "_C"
  384. },
  385. CanFocus = canFocus
  386. };
  387. runnable.Add (shortcut);
  388. Assert.Equal (canFocus, shortcut.HasFocus);
  389. var accepted = 0;
  390. shortcut.Accepting += (s, e) =>
  391. {
  392. accepted++;
  393. e.Handled = true;
  394. };
  395. var selected = 0;
  396. shortcut.Activating += (s, e) => selected++;
  397. app.Keyboard.RaiseKeyDownEvent (key);
  398. Assert.Equal (expectedAccept, accepted);
  399. Assert.Equal (expectedSelect, selected);
  400. }
  401. }