KeyboardTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #nullable enable
  2. using Terminal.Gui.App;
  3. namespace ApplicationTests.Keyboard;
  4. /// <summary>
  5. /// Parallelizable tests for keyboard handling.
  6. /// These tests use isolated instances of <see cref="IKeyboard"/> to avoid static state dependencies.
  7. /// </summary>
  8. public class KeyboardTests
  9. {
  10. [Fact]
  11. public void Init_CreatesKeybindings ()
  12. {
  13. IApplication app = Application.Create ();
  14. app.Keyboard.KeyBindings.Clear ();
  15. Assert.Empty (app.Keyboard.KeyBindings.GetBindings ());
  16. app.Init ("fake");
  17. Assert.NotEmpty (app.Keyboard.KeyBindings.GetBindings ());
  18. app.Dispose ();
  19. }
  20. [Fact]
  21. public void Constructor_InitializesKeyBindings ()
  22. {
  23. // Arrange & Act
  24. var keyboard = new KeyboardImpl ();
  25. // Assert
  26. Assert.NotNull (keyboard.KeyBindings);
  27. // Verify that some default bindings exist
  28. Assert.True (keyboard.KeyBindings.TryGet (keyboard.QuitKey, out _));
  29. }
  30. [Fact]
  31. public void QuitKey_DefaultValue_IsEsc ()
  32. {
  33. // Arrange
  34. var keyboard = new KeyboardImpl ();
  35. // Assert
  36. Assert.Equal (Key.Esc, keyboard.QuitKey);
  37. }
  38. [Fact]
  39. public void QuitKey_SetValue_UpdatesKeyBindings ()
  40. {
  41. // Arrange
  42. var keyboard = new KeyboardImpl ();
  43. Key newQuitKey = Key.Q.WithCtrl;
  44. // Act
  45. keyboard.QuitKey = newQuitKey;
  46. // Assert
  47. Assert.Equal (newQuitKey, keyboard.QuitKey);
  48. Assert.True (keyboard.KeyBindings.TryGet (newQuitKey, out KeyBinding binding));
  49. Assert.Contains (Command.Quit, binding.Commands);
  50. }
  51. [Fact]
  52. public void ArrangeKey_DefaultValue_IsCtrlF5 ()
  53. {
  54. // Arrange
  55. var keyboard = new KeyboardImpl ();
  56. // Assert
  57. Assert.Equal (Key.F5.WithCtrl, keyboard.ArrangeKey);
  58. }
  59. [Fact]
  60. public void NextTabKey_DefaultValue_IsTab ()
  61. {
  62. // Arrange
  63. var keyboard = new KeyboardImpl ();
  64. // Assert
  65. Assert.Equal (Key.Tab, keyboard.NextTabKey);
  66. }
  67. [Fact]
  68. public void PrevTabKey_DefaultValue_IsShiftTab ()
  69. {
  70. // Arrange
  71. var keyboard = new KeyboardImpl ();
  72. // Assert
  73. Assert.Equal (Key.Tab.WithShift, keyboard.PrevTabKey);
  74. }
  75. [Fact]
  76. public void NextTabGroupKey_DefaultValue_IsF6 ()
  77. {
  78. // Arrange
  79. var keyboard = new KeyboardImpl ();
  80. // Assert
  81. Assert.Equal (Key.F6, keyboard.NextTabGroupKey);
  82. }
  83. [Fact]
  84. public void PrevTabGroupKey_DefaultValue_IsShiftF6 ()
  85. {
  86. // Arrange
  87. var keyboard = new KeyboardImpl ();
  88. // Assert
  89. Assert.Equal (Key.F6.WithShift, keyboard.PrevTabGroupKey);
  90. }
  91. [Fact]
  92. public void KeyBindings_Add_CanAddCustomBinding ()
  93. {
  94. // Arrange
  95. var keyboard = new KeyboardImpl ();
  96. Key customKey = Key.K.WithCtrl;
  97. // Act
  98. keyboard.KeyBindings.Add (customKey, Command.Accept);
  99. // Assert
  100. Assert.True (keyboard.KeyBindings.TryGet (customKey, out KeyBinding binding));
  101. Assert.Contains (Command.Accept, binding.Commands);
  102. }
  103. [Fact]
  104. public void KeyBindings_Remove_CanRemoveBinding ()
  105. {
  106. // Arrange
  107. var keyboard = new KeyboardImpl ();
  108. Key customKey = Key.K.WithCtrl;
  109. keyboard.KeyBindings.Add (customKey, Command.Accept);
  110. // Act
  111. keyboard.KeyBindings.Remove (customKey);
  112. // Assert
  113. Assert.False (keyboard.KeyBindings.TryGet (customKey, out _));
  114. }
  115. [Fact]
  116. public void KeyDown_Event_CanBeSubscribed ()
  117. {
  118. // Arrange
  119. var keyboard = new KeyboardImpl ();
  120. bool eventRaised = false;
  121. // Act
  122. keyboard.KeyDown += (sender, key) =>
  123. {
  124. eventRaised = true;
  125. };
  126. // Assert - event subscription doesn't throw
  127. Assert.False (eventRaised); // Event hasn't been raised yet
  128. }
  129. [Fact]
  130. public void KeyUp_Event_CanBeSubscribed ()
  131. {
  132. // Arrange
  133. var keyboard = new KeyboardImpl ();
  134. bool eventRaised = false;
  135. // Act
  136. keyboard.KeyUp += (sender, key) =>
  137. {
  138. eventRaised = true;
  139. };
  140. // Assert - event subscription doesn't throw
  141. Assert.False (eventRaised); // Event hasn't been raised yet
  142. }
  143. [Fact]
  144. public void InvokeCommand_WithInvalidCommand_ThrowsNotSupportedException ()
  145. {
  146. // Arrange
  147. var keyboard = new KeyboardImpl ();
  148. // Pick a command that isn't registered
  149. Command invalidCommand = (Command)9999;
  150. Key testKey = Key.A;
  151. var binding = new KeyBinding ([invalidCommand]);
  152. // Act & Assert
  153. Assert.Throws<NotSupportedException> (() => keyboard.InvokeCommand (invalidCommand, testKey, binding));
  154. }
  155. [Fact]
  156. public void Multiple_Keyboards_CanExistIndependently ()
  157. {
  158. // Arrange & Act
  159. var keyboard1 = new KeyboardImpl ();
  160. var keyboard2 = new KeyboardImpl ();
  161. keyboard1.QuitKey = Key.Q.WithCtrl;
  162. keyboard2.QuitKey = Key.X.WithCtrl;
  163. // Assert - each keyboard maintains independent state
  164. Assert.Equal (Key.Q.WithCtrl, keyboard1.QuitKey);
  165. Assert.Equal (Key.X.WithCtrl, keyboard2.QuitKey);
  166. Assert.NotEqual (keyboard1.QuitKey, keyboard2.QuitKey);
  167. }
  168. [Fact]
  169. public void KeyBindings_Replace_UpdatesExistingBinding ()
  170. {
  171. // Arrange
  172. var keyboard = new KeyboardImpl ();
  173. Key oldKey = Key.Esc;
  174. Key newKey = Key.Q.WithCtrl;
  175. // Verify initial state
  176. Assert.True (keyboard.KeyBindings.TryGet (oldKey, out KeyBinding oldBinding));
  177. Assert.Contains (Command.Quit, oldBinding.Commands);
  178. // Act
  179. keyboard.KeyBindings.Replace (oldKey, newKey);
  180. // Assert - old key should no longer have the binding
  181. Assert.False (keyboard.KeyBindings.TryGet (oldKey, out _));
  182. // New key should have the binding
  183. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding newBinding));
  184. Assert.Contains (Command.Quit, newBinding.Commands);
  185. }
  186. [Fact]
  187. public void KeyBindings_Clear_RemovesAllBindings ()
  188. {
  189. // Arrange
  190. var keyboard = new KeyboardImpl ();
  191. // Verify initial state has bindings
  192. Assert.True (keyboard.KeyBindings.TryGet (keyboard.QuitKey, out _));
  193. // Act
  194. keyboard.KeyBindings.Clear ();
  195. // Assert - previously existing binding is gone
  196. Assert.False (keyboard.KeyBindings.TryGet (keyboard.QuitKey, out _));
  197. }
  198. [Fact]
  199. public void AddKeyBindings_PopulatesDefaultBindings ()
  200. {
  201. // Arrange
  202. var keyboard = new KeyboardImpl ();
  203. keyboard.KeyBindings.Clear ();
  204. Assert.False (keyboard.KeyBindings.TryGet (keyboard.QuitKey, out _));
  205. // Act
  206. keyboard.AddKeyBindings ();
  207. // Assert
  208. Assert.True (keyboard.KeyBindings.TryGet (keyboard.QuitKey, out KeyBinding binding));
  209. Assert.Contains (Command.Quit, binding.Commands);
  210. }
  211. // Migrated from UnitTests/Application/KeyboardTests.cs
  212. [Fact]
  213. public void KeyBindings_Add_Adds ()
  214. {
  215. // Arrange
  216. var keyboard = new KeyboardImpl ();
  217. // Act
  218. keyboard.KeyBindings.Add (Key.A, Command.Accept);
  219. keyboard.KeyBindings.Add (Key.B, Command.Accept);
  220. // Assert
  221. Assert.True (keyboard.KeyBindings.TryGet (Key.A, out KeyBinding binding));
  222. Assert.Null (binding.Target);
  223. Assert.True (keyboard.KeyBindings.TryGet (Key.B, out binding));
  224. Assert.Null (binding.Target);
  225. }
  226. [Fact]
  227. public void KeyBindings_Remove_Removes ()
  228. {
  229. // Arrange
  230. var keyboard = new KeyboardImpl ();
  231. keyboard.KeyBindings.Add (Key.A, Command.Accept);
  232. Assert.True (keyboard.KeyBindings.TryGet (Key.A, out _));
  233. // Act
  234. keyboard.KeyBindings.Remove (Key.A);
  235. // Assert
  236. Assert.False (keyboard.KeyBindings.TryGet (Key.A, out _));
  237. }
  238. [Fact]
  239. public void QuitKey_Default_Is_Esc ()
  240. {
  241. // Arrange & Act
  242. var keyboard = new KeyboardImpl ();
  243. // Assert
  244. Assert.Equal (Key.Esc, keyboard.QuitKey);
  245. }
  246. [Fact]
  247. public void QuitKey_Setter_UpdatesBindings ()
  248. {
  249. // Arrange
  250. var keyboard = new KeyboardImpl ();
  251. Key prevKey = keyboard.QuitKey;
  252. // Act - Change QuitKey
  253. keyboard.QuitKey = Key.C.WithCtrl;
  254. // Assert - Old key should no longer trigger quit
  255. Assert.False (keyboard.KeyBindings.TryGet (prevKey, out _));
  256. // New key should trigger quit
  257. Assert.True (keyboard.KeyBindings.TryGet (Key.C.WithCtrl, out KeyBinding binding));
  258. Assert.Contains (Command.Quit, binding.Commands);
  259. }
  260. [Fact]
  261. public void NextTabKey_Setter_UpdatesBindings ()
  262. {
  263. // Arrange
  264. var keyboard = new KeyboardImpl ();
  265. Key prevKey = keyboard.NextTabKey;
  266. Key newKey = Key.N.WithCtrl;
  267. // Act
  268. keyboard.NextTabKey = newKey;
  269. // Assert
  270. Assert.Equal (newKey, keyboard.NextTabKey);
  271. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding binding));
  272. Assert.Contains (Command.NextTabStop, binding.Commands);
  273. }
  274. [Fact]
  275. public void PrevTabKey_Setter_UpdatesBindings ()
  276. {
  277. // Arrange
  278. var keyboard = new KeyboardImpl ();
  279. Key newKey = Key.P.WithCtrl;
  280. // Act
  281. keyboard.PrevTabKey = newKey;
  282. // Assert
  283. Assert.Equal (newKey, keyboard.PrevTabKey);
  284. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding binding));
  285. Assert.Contains (Command.PreviousTabStop, binding.Commands);
  286. }
  287. [Fact]
  288. public void NextTabGroupKey_Setter_UpdatesBindings ()
  289. {
  290. // Arrange
  291. var keyboard = new KeyboardImpl ();
  292. Key newKey = Key.PageDown.WithCtrl;
  293. // Act
  294. keyboard.NextTabGroupKey = newKey;
  295. // Assert
  296. Assert.Equal (newKey, keyboard.NextTabGroupKey);
  297. Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, keyboard.NextTabGroupKey.KeyCode);
  298. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding binding));
  299. Assert.Contains (Command.NextTabGroup, binding.Commands);
  300. }
  301. [Fact]
  302. public void PrevTabGroupKey_Setter_UpdatesBindings ()
  303. {
  304. // Arrange
  305. var keyboard = new KeyboardImpl ();
  306. Key newKey = Key.PageUp.WithCtrl;
  307. // Act
  308. keyboard.PrevTabGroupKey = newKey;
  309. // Assert
  310. Assert.Equal (newKey, keyboard.PrevTabGroupKey);
  311. Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, keyboard.PrevTabGroupKey.KeyCode);
  312. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding binding));
  313. Assert.Contains (Command.PreviousTabGroup, binding.Commands);
  314. }
  315. [Fact]
  316. public void ArrangeKey_Setter_UpdatesBindings ()
  317. {
  318. // Arrange
  319. var keyboard = new KeyboardImpl ();
  320. Key newKey = Key.A.WithCtrl;
  321. // Act
  322. keyboard.ArrangeKey = newKey;
  323. // Assert
  324. Assert.Equal (newKey, keyboard.ArrangeKey);
  325. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding binding));
  326. Assert.Contains (Command.Arrange, binding.Commands);
  327. }
  328. [Fact]
  329. public void KeyBindings_AddWithTarget_StoresTarget ()
  330. {
  331. // Arrange
  332. var keyboard = new KeyboardImpl ();
  333. var view = new View ();
  334. // Act
  335. keyboard.KeyBindings.Add (Key.A.WithCtrl, view, Command.Accept);
  336. // Assert
  337. Assert.True (keyboard.KeyBindings.TryGet (Key.A.WithCtrl, out KeyBinding binding));
  338. Assert.Equal (view, binding.Target);
  339. Assert.Contains (Command.Accept, binding.Commands);
  340. view.Dispose ();
  341. }
  342. [Fact]
  343. public void InvokeCommandsBoundToKey_ReturnsNull_WhenNoBindingExists ()
  344. {
  345. // Arrange
  346. var keyboard = new KeyboardImpl ();
  347. Key unboundKey = Key.Z.WithAlt.WithCtrl;
  348. // Act
  349. bool? result = keyboard.InvokeCommandsBoundToKey (unboundKey);
  350. // Assert
  351. Assert.Null (result);
  352. }
  353. [Fact]
  354. public void InvokeCommandsBoundToKey_InvokesCommand_WhenBindingExists ()
  355. {
  356. // Arrange
  357. var keyboard = new KeyboardImpl ();
  358. // QuitKey has a bound command by default
  359. // Act
  360. bool? result = keyboard.InvokeCommandsBoundToKey (keyboard.QuitKey);
  361. // Assert
  362. // Command.Quit would normally call Application.RequestStop,
  363. // but in isolation it should return true (handled)
  364. Assert.NotNull (result);
  365. }
  366. [Fact]
  367. public void Multiple_Keyboards_Independent_KeyBindings ()
  368. {
  369. // Arrange
  370. var keyboard1 = new KeyboardImpl ();
  371. var keyboard2 = new KeyboardImpl ();
  372. // Act
  373. keyboard1.KeyBindings.Add (Key.X, Command.Accept);
  374. keyboard2.KeyBindings.Add (Key.Y, Command.Cancel);
  375. // Assert
  376. Assert.True (keyboard1.KeyBindings.TryGet (Key.X, out _));
  377. Assert.False (keyboard1.KeyBindings.TryGet (Key.Y, out _));
  378. Assert.True (keyboard2.KeyBindings.TryGet (Key.Y, out _));
  379. Assert.False (keyboard2.KeyBindings.TryGet (Key.X, out _));
  380. }
  381. [Fact]
  382. public void KeyBindings_Replace_PreservesCommandsForNewKey ()
  383. {
  384. // Arrange
  385. var keyboard = new KeyboardImpl ();
  386. Key oldKey = Key.Esc;
  387. Key newKey = Key.Q.WithCtrl;
  388. // Get the commands from the old binding
  389. Assert.True (keyboard.KeyBindings.TryGet (oldKey, out KeyBinding oldBinding));
  390. Command [] oldCommands = oldBinding.Commands.ToArray ();
  391. // Act
  392. keyboard.KeyBindings.Replace (oldKey, newKey);
  393. // Assert - new key should have the same commands
  394. Assert.True (keyboard.KeyBindings.TryGet (newKey, out KeyBinding newBinding));
  395. Assert.Equal (oldCommands, newBinding.Commands);
  396. }
  397. }