ShortcutTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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()
  90. {
  91. HelpText = help,
  92. Title = command,
  93. Key = key
  94. };
  95. shortcut.Layout ();
  96. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  97. shortcut = new()
  98. {
  99. HelpText = help,
  100. Key = key,
  101. Title = command
  102. };
  103. shortcut.Layout ();
  104. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  105. shortcut = new()
  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.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  227. shortcut.Key = Key.B;
  228. Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  229. Assert.True (shortcut.HotKeyBindings.TryGet (Key.B, out _));
  230. }
  231. // Test Key gets bound correctly
  232. [Fact]
  233. public void BindKeyToApplication_Defaults_To_HotKey ()
  234. {
  235. var shortcut = new Shortcut ();
  236. Assert.False (shortcut.BindKeyToApplication);
  237. }
  238. [Fact]
  239. public void BindKeyToApplication_Can_Be_Set ()
  240. {
  241. var shortcut = new Shortcut ();
  242. shortcut.BindKeyToApplication = true;
  243. Assert.True (shortcut.BindKeyToApplication);
  244. }
  245. [Fact]
  246. public void BindKeyToApplication_Changing_Adjusts_KeyBindings ()
  247. {
  248. var shortcut = new Shortcut ();
  249. shortcut.Key = Key.A;
  250. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  251. shortcut.BindKeyToApplication = true;
  252. Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  253. Assert.True (Application.KeyBindings.TryGet (Key.A, out _));
  254. shortcut.BindKeyToApplication = false;
  255. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  256. Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
  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. [Fact]
  355. public void ColorScheme_SetsAndGetsCorrectly ()
  356. {
  357. var colorScheme = new ColorScheme ();
  358. var shortcut = new Shortcut
  359. {
  360. ColorScheme = colorScheme
  361. };
  362. Assert.Same (colorScheme, shortcut.ColorScheme);
  363. }
  364. }