KeyBindingTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.InputTests;
  3. public class KeyBindingTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public KeyBindingTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. public void Add_Empty_Throws ()
  9. {
  10. var keyBindings = new KeyBindings ();
  11. List<Command> commands = new ();
  12. Assert.Throws<ArgumentException> (() => keyBindings.Add (Key.A, commands.ToArray ()));
  13. }
  14. [Fact]
  15. public void Add_Multiple_Adds ()
  16. {
  17. var keyBindings = new KeyBindings ();
  18. Command [] commands = { Command.Right, Command.Left };
  19. keyBindings.Add (Key.A, commands);
  20. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  21. Assert.Contains (Command.Right, resultCommands);
  22. Assert.Contains (Command.Left, resultCommands);
  23. keyBindings.Add (Key.B, commands);
  24. resultCommands = keyBindings.GetCommands (Key.B);
  25. Assert.Contains (Command.Right, resultCommands);
  26. Assert.Contains (Command.Left, resultCommands);
  27. }
  28. [Fact]
  29. public void Add_Single_Adds ()
  30. {
  31. var keyBindings = new KeyBindings ();
  32. keyBindings.Add (Key.A, Command.Default);
  33. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  34. Assert.Contains (Command.Default, resultCommands);
  35. keyBindings.Add (Key.B, Command.Default);
  36. resultCommands = keyBindings.GetCommands (Key.B);
  37. Assert.Contains (Command.Default, resultCommands);
  38. }
  39. // Clear
  40. [Fact]
  41. public void Clear_Clears ()
  42. {
  43. var keyBindings = new KeyBindings ();
  44. keyBindings.Add (Key.A, Command.Default);
  45. keyBindings.Add (Key.B, Command.Default);
  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.Default);
  71. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  72. Assert.Contains (Command.Default, 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.Default);
  135. Key resultKey = keyBindings.GetKeyFromCommands (Command.Default);
  136. Assert.Equal (Key.A, resultKey);
  137. }
  138. [Fact]
  139. public void Replace_Key ()
  140. {
  141. var keyBindings = new KeyBindings ();
  142. keyBindings.Add (Key.A, Command.Default);
  143. keyBindings.Add (Key.B, Command.Default);
  144. keyBindings.Add (Key.C, Command.Default);
  145. keyBindings.Add (Key.D, Command.Default);
  146. keyBindings.Replace (Key.A, Key.E);
  147. Assert.Empty (keyBindings.GetCommands (Key.A));
  148. Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
  149. keyBindings.Replace (Key.B, Key.E);
  150. Assert.Empty (keyBindings.GetCommands (Key.B));
  151. Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
  152. keyBindings.Replace (Key.C, Key.E);
  153. Assert.Empty (keyBindings.GetCommands (Key.C));
  154. Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
  155. keyBindings.Replace (Key.D, Key.E);
  156. Assert.Empty (keyBindings.GetCommands (Key.D));
  157. Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
  158. }
  159. // Add with scope does the right things
  160. [Theory]
  161. [InlineData (KeyBindingScope.Focused)]
  162. [InlineData (KeyBindingScope.HotKey)]
  163. [InlineData (KeyBindingScope.Application)]
  164. public void Scope_Add_Adds (KeyBindingScope scope)
  165. {
  166. var keyBindings = new KeyBindings ();
  167. Command [] commands = { Command.Right, Command.Left };
  168. keyBindings.Add (Key.A, scope, commands);
  169. KeyBinding binding = keyBindings.Get (Key.A);
  170. Assert.Contains (Command.Right, binding.Commands);
  171. Assert.Contains (Command.Left, binding.Commands);
  172. binding = keyBindings.Get (Key.A, scope);
  173. Assert.Contains (Command.Right, binding.Commands);
  174. Assert.Contains (Command.Left, binding.Commands);
  175. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  176. Assert.Contains (Command.Right, resultCommands);
  177. Assert.Contains (Command.Left, resultCommands);
  178. }
  179. [Theory]
  180. [InlineData (KeyBindingScope.Focused)]
  181. [InlineData (KeyBindingScope.HotKey)]
  182. [InlineData (KeyBindingScope.Application)]
  183. public void Scope_Get_Filters (KeyBindingScope scope)
  184. {
  185. var keyBindings = new KeyBindings ();
  186. Command [] commands = { Command.Right, Command.Left };
  187. keyBindings.Add (Key.A, scope, commands);
  188. KeyBinding binding = keyBindings.Get (Key.A);
  189. Assert.Contains (Command.Right, binding.Commands);
  190. Assert.Contains (Command.Left, binding.Commands);
  191. binding = keyBindings.Get (Key.A, scope);
  192. Assert.Contains (Command.Right, binding.Commands);
  193. Assert.Contains (Command.Left, binding.Commands);
  194. // negative test
  195. binding = keyBindings.Get (Key.A, (KeyBindingScope)(-1));
  196. Assert.Null (binding);
  197. Command [] resultCommands = keyBindings.GetCommands (Key.A);
  198. Assert.Contains (Command.Right, resultCommands);
  199. Assert.Contains (Command.Left, resultCommands);
  200. }
  201. // TryGet
  202. [Fact]
  203. public void TryGet_Unknown_ReturnsFalse ()
  204. {
  205. var keyBindings = new KeyBindings ();
  206. bool result = keyBindings.TryGet (Key.A, out KeyBinding _);
  207. Assert.False (result);
  208. }
  209. [Fact]
  210. public void TryGet_WithCommands_ReturnsTrue ()
  211. {
  212. var keyBindings = new KeyBindings ();
  213. keyBindings.Add (Key.A, Command.Default);
  214. bool result = keyBindings.TryGet (Key.A, out KeyBinding bindings);
  215. Assert.True (result);
  216. Assert.Contains (Command.Default, bindings.Commands);
  217. }
  218. }