ShortcutTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using JetBrains.Annotations;
  2. using UnitTests.Parallelizable;
  3. namespace UnitTests_Parallelizable.ViewsTests;
  4. [Collection ("Global Test Setup")]
  5. [TestSubject (typeof (Shortcut))]
  6. public class ShortcutTests
  7. {
  8. [Fact]
  9. public void Constructor_Defaults ()
  10. {
  11. var shortcut = new Shortcut ();
  12. Assert.NotNull (shortcut);
  13. Assert.True (shortcut.CanFocus);
  14. Assert.IsType<DimAuto> (shortcut.Width);
  15. Assert.IsType<DimAuto> (shortcut.Height);
  16. }
  17. [Fact]
  18. public void Size_Defaults ()
  19. {
  20. var shortcut = new Shortcut ();
  21. shortcut.Layout ();
  22. Assert.Equal (2, shortcut.Frame.Width);
  23. Assert.Equal (1, shortcut.Frame.Height);
  24. Assert.Equal (2, shortcut.Viewport.Width);
  25. Assert.Equal (1, shortcut.Viewport.Height);
  26. Assert.Equal (0, shortcut.CommandView.Viewport.Width);
  27. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  28. Assert.Equal (0, shortcut.HelpView.Viewport.Width);
  29. Assert.Equal (0, shortcut.HelpView.Viewport.Height);
  30. Assert.Equal (0, shortcut.KeyView.Viewport.Width);
  31. Assert.Equal (0, shortcut.KeyView.Viewport.Height);
  32. // 0123456789
  33. // " 0 A "
  34. shortcut = new ()
  35. {
  36. Key = Key.A,
  37. HelpText = "0"
  38. };
  39. shortcut.Layout ();
  40. Assert.Equal (8, shortcut.Frame.Width);
  41. Assert.Equal (1, shortcut.Frame.Height);
  42. Assert.Equal (8, shortcut.Viewport.Width);
  43. Assert.Equal (1, shortcut.Viewport.Height);
  44. Assert.Equal (0, shortcut.CommandView.Viewport.Width);
  45. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  46. Assert.Equal (1, shortcut.HelpView.Viewport.Width);
  47. Assert.Equal (1, shortcut.HelpView.Viewport.Height);
  48. Assert.Equal (1, shortcut.KeyView.Viewport.Width);
  49. Assert.Equal (1, shortcut.KeyView.Viewport.Height);
  50. // 0123456789
  51. // " C 0 A "
  52. shortcut = new ()
  53. {
  54. Title = "C",
  55. Key = Key.A,
  56. HelpText = "0"
  57. };
  58. shortcut.Layout ();
  59. Assert.Equal (9, shortcut.Frame.Width);
  60. Assert.Equal (1, shortcut.Frame.Height);
  61. Assert.Equal (9, shortcut.Viewport.Width);
  62. Assert.Equal (1, shortcut.Viewport.Height);
  63. Assert.Equal (1, shortcut.CommandView.Viewport.Width);
  64. Assert.Equal (1, shortcut.CommandView.Viewport.Height);
  65. Assert.Equal (1, shortcut.HelpView.Viewport.Width);
  66. Assert.Equal (1, shortcut.HelpView.Viewport.Height);
  67. Assert.Equal (1, shortcut.KeyView.Viewport.Width);
  68. Assert.Equal (1, shortcut.KeyView.Viewport.Height);
  69. }
  70. [Theory]
  71. [InlineData ("", "", KeyCode.Null, 2)]
  72. [InlineData ("C", "", KeyCode.Null, 3)]
  73. [InlineData ("", "H", KeyCode.Null, 5)]
  74. [InlineData ("", "", KeyCode.K, 5)]
  75. [InlineData ("C", "", KeyCode.K, 6)]
  76. [InlineData ("C", "H", KeyCode.Null, 6)]
  77. [InlineData ("", "H", KeyCode.K, 8)]
  78. [InlineData ("C", "H", KeyCode.K, 9)]
  79. public void NaturalSize (string command, string help, KeyCode key, int expectedWidth)
  80. {
  81. var shortcut = new Shortcut
  82. {
  83. HelpText = help,
  84. Key = key,
  85. Title = command
  86. };
  87. shortcut.Layout ();
  88. // |0123456789
  89. // | C H K |
  90. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  91. shortcut = new ()
  92. {
  93. HelpText = help,
  94. Title = command,
  95. Key = key
  96. };
  97. shortcut.Layout ();
  98. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  99. shortcut = new ()
  100. {
  101. HelpText = help,
  102. Key = key,
  103. Title = command
  104. };
  105. shortcut.Layout ();
  106. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  107. shortcut = new ()
  108. {
  109. Key = key,
  110. HelpText = help,
  111. Title = command
  112. };
  113. shortcut.Layout ();
  114. Assert.Equal (expectedWidth, shortcut.Frame.Width);
  115. }
  116. [Theory]
  117. [InlineData (0, 0, 3, 3)]
  118. [InlineData (1, 0, 3, 3)]
  119. [InlineData (2, 0, 3, 3)]
  120. [InlineData (3, 0, 3, 3)]
  121. [InlineData (4, 0, 3, 3)]
  122. [InlineData (5, 0, 3, 3)]
  123. [InlineData (6, 0, 3, 3)]
  124. [InlineData (7, 0, 3, 4)]
  125. [InlineData (8, 0, 3, 5)]
  126. [InlineData (9, 0, 3, 6)]
  127. [InlineData (10, 0, 4, 7)]
  128. [InlineData (11, 0, 5, 8)]
  129. public void Set_Width_Layouts_Correctly (int width, int expectedCmdX, int expectedHelpX, int expectedKeyX)
  130. {
  131. var shortcut = new Shortcut
  132. {
  133. Width = width,
  134. Title = "C",
  135. Text = "H",
  136. Key = Key.K
  137. };
  138. shortcut.Layout ();
  139. // 01234
  140. // -C--K
  141. // 012345
  142. // -C--K-
  143. // 0123456
  144. // -C-H-K-
  145. // 01234567
  146. // -C--H-K-
  147. // 012345678
  148. // -C--H--K-
  149. // 0123456789
  150. // -C--H--K-
  151. Assert.Equal (expectedCmdX, shortcut.CommandView.Frame.X);
  152. Assert.Equal (expectedHelpX, shortcut.HelpView.Frame.X);
  153. Assert.Equal (expectedKeyX, shortcut.KeyView.Frame.X);
  154. }
  155. [Fact]
  156. public void CommandView_Text_And_Title_Track ()
  157. {
  158. var shortcut = new Shortcut
  159. {
  160. Title = "T"
  161. };
  162. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  163. shortcut = new ();
  164. shortcut.CommandView = new ()
  165. {
  166. Text = "T"
  167. };
  168. Assert.Equal (shortcut.Title, shortcut.CommandView.Text);
  169. }
  170. [Fact]
  171. public void HelpText_And_Text_Are_The_Same ()
  172. {
  173. var shortcut = new Shortcut
  174. {
  175. Text = "H"
  176. };
  177. Assert.Equal (shortcut.Text, shortcut.HelpText);
  178. shortcut = new ()
  179. {
  180. HelpText = "H"
  181. };
  182. Assert.Equal (shortcut.Text, shortcut.HelpText);
  183. }
  184. [Theory]
  185. [InlineData (KeyCode.Null, "")]
  186. [InlineData (KeyCode.F1, "F1")]
  187. public void KeyView_Text_Tracks_Key (KeyCode key, string expected)
  188. {
  189. var shortcut = new Shortcut
  190. {
  191. Key = key
  192. };
  193. Assert.Equal (expected, shortcut.KeyView.Text);
  194. }
  195. // Test Key
  196. [Fact]
  197. public void Key_Defaults_To_Empty ()
  198. {
  199. var shortcut = new Shortcut ();
  200. Assert.Equal (Key.Empty, shortcut.Key);
  201. }
  202. [Fact]
  203. public void Key_Can_Be_Set ()
  204. {
  205. var shortcut = new Shortcut ();
  206. shortcut.Key = Key.F1;
  207. Assert.Equal (Key.F1, shortcut.Key);
  208. }
  209. [Fact]
  210. public void Key_Can_Be_Set_To_Empty ()
  211. {
  212. var shortcut = new Shortcut ();
  213. shortcut.Key = Key.Empty;
  214. Assert.Equal (Key.Empty, shortcut.Key);
  215. }
  216. [Fact]
  217. public void Key_Set_Binds_Key_To_CommandView_Accept ()
  218. {
  219. var shortcut = new Shortcut ();
  220. shortcut.Key = Key.F1;
  221. // TODO:
  222. }
  223. [Fact]
  224. public void Key_Changing_Removes_Previous_Binding ()
  225. {
  226. var shortcut = new Shortcut ();
  227. shortcut.Key = Key.A;
  228. Assert.True (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  229. shortcut.Key = Key.B;
  230. Assert.False (shortcut.HotKeyBindings.TryGet (Key.A, out _));
  231. Assert.True (shortcut.HotKeyBindings.TryGet (Key.B, out _));
  232. }
  233. // Test Key gets bound correctly
  234. [Fact]
  235. public void BindKeyToApplication_Defaults_To_HotKey ()
  236. {
  237. var shortcut = new Shortcut ();
  238. Assert.False (shortcut.BindKeyToApplication);
  239. }
  240. [Fact]
  241. public void BindKeyToApplication_Can_Be_Set ()
  242. {
  243. var shortcut = new Shortcut ();
  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. }