ShortcutTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using JetBrains.Annotations;
  2. namespace Terminal.Gui.ViewsTests;
  3. [Collection ("Global Test Setup")]
  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. var shortcut = new Shortcut ();
  243. shortcut.BindKeyToApplication = true;
  244. Assert.True (shortcut.BindKeyToApplication);
  245. }
  246. [Fact]
  247. public void BindKeyToApplication_Changing_Adjusts_KeyBindings ()
  248. {
  249. var shortcut = new Shortcut ();
  250. shortcut.Key = Key.A;
  251. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  252. shortcut.BindKeyToApplication = true;
  253. Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  254. Assert.True (Application.KeyBindings.TryGet (Key.A, out _));
  255. shortcut.BindKeyToApplication = false;
  256. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  257. Assert.False (Application.KeyBindings.TryGet (Key.A, out _));
  258. }
  259. [Theory]
  260. [InlineData (Orientation.Horizontal)]
  261. [InlineData (Orientation.Vertical)]
  262. public void Orientation_SetsCorrectly (Orientation orientation)
  263. {
  264. var shortcut = new Shortcut
  265. {
  266. Orientation = orientation
  267. };
  268. Assert.Equal (orientation, shortcut.Orientation);
  269. }
  270. [Theory]
  271. [InlineData (AlignmentModes.StartToEnd)]
  272. [InlineData (AlignmentModes.EndToStart)]
  273. public void AlignmentModes_SetsCorrectly (AlignmentModes alignmentModes)
  274. {
  275. var shortcut = new Shortcut
  276. {
  277. AlignmentModes = alignmentModes
  278. };
  279. Assert.Equal (alignmentModes, shortcut.AlignmentModes);
  280. }
  281. [Fact]
  282. public void Action_SetsAndGetsCorrectly ()
  283. {
  284. var actionInvoked = false;
  285. var shortcut = new Shortcut
  286. {
  287. Action = () => { actionInvoked = true; }
  288. };
  289. shortcut.Action.Invoke ();
  290. Assert.True (actionInvoked);
  291. }
  292. [Fact]
  293. public void SubView_Visibility_Controlled_By_Removal ()
  294. {
  295. var shortcut = new Shortcut ();
  296. Assert.True (shortcut.CommandView.Visible);
  297. Assert.Contains (shortcut.CommandView, shortcut.SubViews);
  298. Assert.True (shortcut.HelpView.Visible);
  299. Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
  300. Assert.True (shortcut.KeyView.Visible);
  301. Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
  302. shortcut.HelpText = "help";
  303. Assert.True (shortcut.HelpView.Visible);
  304. Assert.Contains (shortcut.HelpView, shortcut.SubViews);
  305. Assert.True (shortcut.KeyView.Visible);
  306. Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
  307. shortcut.Key = Key.A;
  308. Assert.True (shortcut.HelpView.Visible);
  309. Assert.Contains (shortcut.HelpView, shortcut.SubViews);
  310. Assert.True (shortcut.KeyView.Visible);
  311. Assert.Contains (shortcut.KeyView, shortcut.SubViews);
  312. shortcut.HelpView.Visible = false;
  313. shortcut.ShowHide ();
  314. Assert.False (shortcut.HelpView.Visible);
  315. Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
  316. Assert.True (shortcut.KeyView.Visible);
  317. Assert.Contains (shortcut.KeyView, shortcut.SubViews);
  318. shortcut.KeyView.Visible = false;
  319. shortcut.ShowHide ();
  320. Assert.False (shortcut.HelpView.Visible);
  321. Assert.DoesNotContain (shortcut.HelpView, shortcut.SubViews);
  322. Assert.False (shortcut.KeyView.Visible);
  323. Assert.DoesNotContain (shortcut.KeyView, shortcut.SubViews);
  324. }
  325. [Fact]
  326. public void Focus_CanFocus_Default_Is_True ()
  327. {
  328. Shortcut shortcut = new ();
  329. shortcut.Key = Key.A;
  330. shortcut.Text = "Help";
  331. shortcut.Title = "Command";
  332. Assert.True (shortcut.CanFocus);
  333. Assert.False (shortcut.CommandView.CanFocus);
  334. }
  335. [Fact]
  336. public void Focus_CanFocus_CommandView_Add_Tracks ()
  337. {
  338. Shortcut shortcut = new ();
  339. Assert.True (shortcut.CanFocus);
  340. Assert.False (shortcut.CommandView.CanFocus);
  341. shortcut.CommandView = new () { CanFocus = true };
  342. Assert.True (shortcut.CommandView.CanFocus);
  343. shortcut.CommandView.CanFocus = true;
  344. Assert.True (shortcut.CommandView.CanFocus);
  345. shortcut.CanFocus = false;
  346. Assert.False (shortcut.CanFocus);
  347. Assert.True (shortcut.CommandView.CanFocus);
  348. shortcut.CommandView.CanFocus = false;
  349. Assert.False (shortcut.CanFocus);
  350. Assert.False (shortcut.CommandView.CanFocus);
  351. shortcut.CommandView.CanFocus = true;
  352. Assert.False (shortcut.CanFocus);
  353. Assert.True (shortcut.CommandView.CanFocus);
  354. }
  355. }