KeyBindingTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using UICatalog.Scenarios;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.InputTests;
  4. public class KeyBindingTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public KeyBindingTests (ITestOutputHelper output) { _output = output; }
  8. [Fact]
  9. public void Add_Empty_Throws ()
  10. {
  11. var keyBindings = new KeyBindings ();
  12. List<Command> commands = new ();
  13. Assert.Throws<ArgumentException> (() => keyBindings.Add (Key.A, commands.ToArray ()));
  14. }
  15. [Fact]
  16. public void Add_Multiple_Adds ()
  17. {
  18. var keyBindings = new KeyBindings ();
  19. Command [] commands = { Command.Right, Command.Left };
  20. keyBindings.Add (Key.A, commands);
  21. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  22. Assert.Contains (Command.Right, resultCommands);
  23. Assert.Contains (Command.Left, resultCommands);
  24. keyBindings.Add (Key.B, commands);
  25. resultCommands = keyBindings.GetCommands (Key.B);
  26. Assert.Contains (Command.Right, resultCommands);
  27. Assert.Contains (Command.Left, resultCommands);
  28. }
  29. [Fact]
  30. public void Add_Single_Adds ()
  31. {
  32. var keyBindings = new KeyBindings ();
  33. keyBindings.Add (Key.A, Command.HotKey);
  34. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  35. Assert.Contains (Command.HotKey, resultCommands);
  36. keyBindings.Add (Key.B, Command.HotKey);
  37. resultCommands = keyBindings.GetCommands (Key.B);
  38. Assert.Contains (Command.HotKey, resultCommands);
  39. }
  40. // Clear
  41. [Fact]
  42. public void Clear_Clears ()
  43. {
  44. var keyBindings = new KeyBindings ();
  45. keyBindings.Add (Key.B, Command.HotKey);
  46. keyBindings.Clear ();
  47. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  48. Assert.Empty (resultCommands);
  49. resultCommands = keyBindings.GetCommands (Key.B);
  50. Assert.Empty (resultCommands);
  51. }
  52. [Fact]
  53. public void Defaults ()
  54. {
  55. var keyBindings = new KeyBindings ();
  56. Assert.Throws<InvalidOperationException> (() => keyBindings.GetKeyFromCommands (Command.Accept));
  57. }
  58. // GetCommands
  59. [Fact]
  60. public void GetCommands_Unknown_ReturnsEmpty ()
  61. {
  62. var keyBindings = new KeyBindings ();
  63. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  64. Assert.Empty (resultCommands);
  65. }
  66. [Fact]
  67. public void GetCommands_WithCommands_ReturnsCommands ()
  68. {
  69. var keyBindings = new KeyBindings ();
  70. keyBindings.Add (Key.A, Command.HotKey);
  71. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  72. Assert.Contains (Command.HotKey, resultCommands);
  73. }
  74. [Fact]
  75. public void GetCommands_WithMultipleBindings_ReturnsCommands ()
  76. {
  77. var keyBindings = new KeyBindings ();
  78. Command [] commands = { Command.Right, Command.Left };
  79. keyBindings.Add (Key.A, commands);
  80. keyBindings.Add (Key.B, commands);
  81. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  82. Assert.Contains (Command.Right, resultCommands);
  83. Assert.Contains (Command.Left, resultCommands);
  84. resultCommands = keyBindings.GetCommands (Key.B);
  85. Assert.Contains (Command.Right, resultCommands);
  86. Assert.Contains (Command.Left, resultCommands);
  87. }
  88. [Fact]
  89. public void GetCommands_WithMultipleCommands_ReturnsCommands ()
  90. {
  91. var keyBindings = new KeyBindings ();
  92. Command [] commands = { Command.Right, Command.Left };
  93. keyBindings.Add (Key.A, commands);
  94. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  95. Assert.Contains (Command.Right, resultCommands);
  96. Assert.Contains (Command.Left, resultCommands);
  97. }
  98. [Fact]
  99. public void GetKeyFromCommands_MultipleCommands ()
  100. {
  101. var keyBindings = new KeyBindings ();
  102. Command [] commands1 = { Command.Right, Command.Left };
  103. keyBindings.Add (Key.A, commands1);
  104. Command [] commands2 = { Command.LineUp, Command.LineDown };
  105. keyBindings.Add (Key.B, commands2);
  106. Key key = keyBindings.GetKeyFromCommands (commands1);
  107. Assert.Equal (Key.A, key);
  108. key = keyBindings.GetKeyFromCommands (commands2);
  109. Assert.Equal (Key.B, key);
  110. // Negative case
  111. Assert.Throws<InvalidOperationException> (() => key = keyBindings.GetKeyFromCommands (Command.EndOfLine));
  112. }
  113. [Fact]
  114. public void GetKeyFromCommands_OneCommand ()
  115. {
  116. var keyBindings = new KeyBindings ();
  117. keyBindings.Add (Key.A, Command.Right);
  118. Key key = keyBindings.GetKeyFromCommands (Command.Right);
  119. Assert.Equal (Key.A, key);
  120. // Negative case
  121. Assert.Throws<InvalidOperationException> (() => key = keyBindings.GetKeyFromCommands (Command.Left));
  122. }
  123. // GetKeyFromCommands
  124. [Fact]
  125. public void GetKeyFromCommands_Unknown_Throws_InvalidOperationException ()
  126. {
  127. var keyBindings = new KeyBindings ();
  128. Assert.Throws<InvalidOperationException> (() => keyBindings.GetKeyFromCommands (Command.Accept));
  129. }
  130. [Fact]
  131. public void GetKeyFromCommands_WithCommands_ReturnsKey ()
  132. {
  133. var keyBindings = new KeyBindings ();
  134. keyBindings.Add (Key.A, Command.HotKey);
  135. Key resultKey = keyBindings.GetKeyFromCommands (Command.HotKey);
  136. Assert.Equal (Key.A, resultKey);
  137. }
  138. // Add should not allow duplicates
  139. [Fact]
  140. public void Add_Replaces_If_Exists ()
  141. {
  142. var keyBindings = new KeyBindings ();
  143. keyBindings.Add (Key.A, Command.HotKey);
  144. keyBindings.Add (Key.A, Command.Accept);
  145. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  146. Assert.DoesNotContain (Command.HotKey, resultCommands);
  147. keyBindings = new KeyBindings ();
  148. keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.HotKey);
  149. keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept);
  150. resultCommands = keyBindings.GetCommands (Key.A);
  151. Assert.DoesNotContain (Command.HotKey, resultCommands);
  152. keyBindings = new KeyBindings ();
  153. keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey);
  154. keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept);
  155. resultCommands = keyBindings.GetCommands (Key.A);
  156. Assert.DoesNotContain (Command.HotKey, resultCommands);
  157. keyBindings = new KeyBindings ();
  158. keyBindings.Add (Key.A, new KeyBinding (new [] { Command.HotKey }, KeyBindingScope.HotKey));
  159. keyBindings.Add (Key.A, new KeyBinding (new [] { Command.Accept }, KeyBindingScope.HotKey));
  160. resultCommands = keyBindings.GetCommands (Key.A);
  161. Assert.DoesNotContain (Command.HotKey, resultCommands);
  162. }
  163. [Fact]
  164. public void Replace_Key ()
  165. {
  166. var keyBindings = new KeyBindings ();
  167. keyBindings.Add (Key.A, Command.HotKey);
  168. keyBindings.Add (Key.B, Command.HotKey);
  169. keyBindings.Add (Key.C, Command.HotKey);
  170. keyBindings.Add (Key.D, Command.HotKey);
  171. keyBindings.Replace (Key.A, Key.E);
  172. Assert.Empty (keyBindings.GetCommands (Key.A));
  173. Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
  174. keyBindings.Replace (Key.B, Key.F);
  175. Assert.Empty (keyBindings.GetCommands (Key.B));
  176. Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.F));
  177. keyBindings.Replace (Key.C, Key.G);
  178. Assert.Empty (keyBindings.GetCommands (Key.C));
  179. Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.G));
  180. keyBindings.Replace (Key.D, Key.H);
  181. Assert.Empty (keyBindings.GetCommands (Key.D));
  182. Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.H));
  183. }
  184. // Add with scope does the right things
  185. [Theory]
  186. [InlineData (KeyBindingScope.Focused)]
  187. [InlineData (KeyBindingScope.HotKey)]
  188. [InlineData (KeyBindingScope.Application)]
  189. public void Scope_Add_Adds (KeyBindingScope scope)
  190. {
  191. var keyBindings = new KeyBindings ();
  192. Command [] commands = { Command.Right, Command.Left };
  193. var key = new Key (Key.A);
  194. keyBindings.Add (Key.A, scope, commands);
  195. KeyBinding binding = keyBindings.Get (key);
  196. Assert.Contains (Command.Right, binding.Commands);
  197. Assert.Contains (Command.Left, binding.Commands);
  198. binding = keyBindings.Get (key, scope);
  199. Assert.Contains (Command.Right, binding.Commands);
  200. Assert.Contains (Command.Left, binding.Commands);
  201. Command [] resultCommands = keyBindings.GetCommands (key);
  202. Assert.Contains (Command.Right, resultCommands);
  203. Assert.Contains (Command.Left, resultCommands);
  204. }
  205. [Theory]
  206. [InlineData (KeyBindingScope.Focused)]
  207. [InlineData (KeyBindingScope.HotKey)]
  208. [InlineData (KeyBindingScope.Application)]
  209. public void Scope_Get_Filters (KeyBindingScope scope)
  210. {
  211. var keyBindings = new KeyBindings ();
  212. Command [] commands = { Command.Right, Command.Left };
  213. var key = new Key (Key.A);
  214. keyBindings.Add (key, scope, commands);
  215. KeyBinding binding = keyBindings.Get (key);
  216. Assert.Contains (Command.Right, binding.Commands);
  217. Assert.Contains (Command.Left, binding.Commands);
  218. binding = keyBindings.Get (key, scope);
  219. Assert.Contains (Command.Right, binding.Commands);
  220. Assert.Contains (Command.Left, binding.Commands);
  221. // negative test
  222. binding = keyBindings.Get (key, (KeyBindingScope)0);
  223. Assert.Null (binding);
  224. Command [] resultCommands = keyBindings.GetCommands (key);
  225. Assert.Contains (Command.Right, resultCommands);
  226. Assert.Contains (Command.Left, resultCommands);
  227. }
  228. [Theory]
  229. [InlineData (KeyBindingScope.Focused)]
  230. [InlineData (KeyBindingScope.HotKey)]
  231. [InlineData (KeyBindingScope.Application)]
  232. public void Scope_TryGet_Filters (KeyBindingScope scope)
  233. {
  234. var keyBindings = new KeyBindings ();
  235. Command [] commands = { Command.Right, Command.Left };
  236. var key = new Key (Key.A);
  237. keyBindings.Add (key, scope, commands);
  238. bool success = keyBindings.TryGet (key, out KeyBinding binding);
  239. Assert.Contains (Command.Right, binding.Commands);
  240. Assert.Contains (Command.Left, binding.Commands);
  241. success = keyBindings.TryGet (key, scope, out binding);
  242. Assert.Contains (Command.Right, binding.Commands);
  243. Assert.Contains (Command.Left, binding.Commands);
  244. // negative test
  245. success = keyBindings.TryGet (key, (KeyBindingScope)0, out binding);
  246. Assert.False (success);
  247. Command [] resultCommands = keyBindings.GetCommands (key);
  248. Assert.Contains (Command.Right, resultCommands);
  249. Assert.Contains (Command.Left, resultCommands);
  250. }
  251. // TryGet
  252. [Fact]
  253. public void TryGet_Unknown_ReturnsFalse ()
  254. {
  255. var keyBindings = new KeyBindings ();
  256. bool result = keyBindings.TryGet (Key.A, out KeyBinding _);
  257. Assert.False (result);
  258. }
  259. [Fact]
  260. public void TryGet_WithCommands_ReturnsTrue ()
  261. {
  262. var keyBindings = new KeyBindings ();
  263. keyBindings.Add (Key.A, Command.HotKey);
  264. bool result = keyBindings.TryGet (Key.A, out KeyBinding bindings);
  265. Assert.True (result);
  266. Assert.Contains (Command.HotKey, bindings.Commands);
  267. }
  268. }