HotKeyTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. public class HotKeyTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public HotKeyTests (ITestOutputHelper output) { _output = output; }
  8. [Theory]
  9. [InlineData (KeyCode.A)]
  10. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  11. [InlineData (KeyCode.D1)]
  12. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
  13. [InlineData ((KeyCode)'х')] // Cyrillic x
  14. [InlineData ((KeyCode)'你')] // Chinese ni
  15. public void AddKeyBindingsForHotKey_Sets (KeyCode key)
  16. {
  17. var view = new View ();
  18. view.HotKey = KeyCode.Z;
  19. Assert.Equal (string.Empty, view.Title);
  20. Assert.Equal (KeyCode.Z, view.HotKey);
  21. view.AddKeyBindingsForHotKey (KeyCode.Null, key);
  22. // Verify key bindings were set
  23. // As passed
  24. Command [] commands = view.KeyBindings.GetCommands (key);
  25. Assert.Contains (Command.HotKey, commands);
  26. commands = view.KeyBindings.GetCommands (key | KeyCode.AltMask);
  27. Assert.Contains (Command.HotKey, commands);
  28. KeyCode baseKey = key & ~KeyCode.ShiftMask;
  29. // If A...Z, with and without shift
  30. if (baseKey is >= KeyCode.A and <= KeyCode.Z)
  31. {
  32. commands = view.KeyBindings.GetCommands (key | KeyCode.ShiftMask);
  33. Assert.Contains (Command.HotKey, commands);
  34. commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask);
  35. Assert.Contains (Command.HotKey, commands);
  36. commands = view.KeyBindings.GetCommands (key | KeyCode.AltMask);
  37. Assert.Contains (Command.HotKey, commands);
  38. commands = view.KeyBindings.GetCommands ((key & ~KeyCode.ShiftMask) | KeyCode.AltMask);
  39. Assert.Contains (Command.HotKey, commands);
  40. }
  41. else
  42. {
  43. // Non A..Z keys should not have shift bindings
  44. if (key.HasFlag (KeyCode.ShiftMask))
  45. {
  46. commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask);
  47. Assert.Empty (commands);
  48. }
  49. else
  50. {
  51. commands = view.KeyBindings.GetCommands (key | KeyCode.ShiftMask);
  52. Assert.Empty (commands);
  53. }
  54. }
  55. }
  56. [Fact]
  57. public void Defaults ()
  58. {
  59. var view = new View ();
  60. Assert.Equal (string.Empty, view.Title);
  61. Assert.Equal (KeyCode.Null, view.HotKey);
  62. // Verify key bindings were set
  63. Command [] commands = view.KeyBindings.GetCommands (KeyCode.Null);
  64. Assert.Empty (commands);
  65. }
  66. [Theory]
  67. [InlineData (KeyCode.Null, true)] // non-shift
  68. [InlineData (KeyCode.ShiftMask, true)]
  69. [InlineData (KeyCode.AltMask, true)]
  70. [InlineData (KeyCode.ShiftMask | KeyCode.AltMask, true)]
  71. [InlineData (KeyCode.CtrlMask, false)]
  72. [InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask, false)]
  73. public void KeyPress_Runs_Default_HotKey_Command (KeyCode mask, bool expected)
  74. {
  75. var view = new View { HotKeySpecifier = (Rune)'^', Title = "^Test" };
  76. view.CanFocus = true;
  77. Assert.False (view.HasFocus);
  78. view.NewKeyDownEvent (KeyCode.T | mask);
  79. Assert.Equal (expected, view.HasFocus);
  80. }
  81. [Fact]
  82. public void ProcessKeyDown_Ignores_KeyBindings_Out_Of_Scope_SuperView ()
  83. {
  84. var view = new View ();
  85. view.KeyBindings.Add (Key.A, Command.HotKey);
  86. view.InvokingKeyBindings += (s, e) => { Assert.Fail (); };
  87. var superView = new View ();
  88. superView.Add (view);
  89. var ke = Key.A;
  90. superView.NewKeyDownEvent (ke);
  91. }
  92. [Fact]
  93. public void ProcessKeyDown_Invokes_HotKey_Command_With_SuperView ()
  94. {
  95. var view = new View { HotKeySpecifier = (Rune)'^', Title = "^Test" };
  96. var superView = new View ();
  97. superView.Add (view);
  98. view.CanFocus = true;
  99. Assert.False (view.HasFocus);
  100. var ke = Key.T;
  101. superView.NewKeyDownEvent (ke);
  102. Assert.True (view.HasFocus);
  103. }
  104. [Fact]
  105. public void Set_RemovesOldKeyBindings ()
  106. {
  107. var view = new View ();
  108. view.HotKey = KeyCode.A;
  109. Assert.Equal (string.Empty, view.Title);
  110. Assert.Equal (KeyCode.A, view.HotKey);
  111. // Verify key bindings were set
  112. Command [] commands = view.KeyBindings.GetCommands (KeyCode.A);
  113. Assert.Contains (Command.HotKey, commands);
  114. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
  115. Assert.Contains (Command.HotKey, commands);
  116. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
  117. Assert.Contains (Command.HotKey, commands);
  118. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
  119. Assert.Contains (Command.HotKey, commands);
  120. // Now set again
  121. view.HotKey = KeyCode.B;
  122. Assert.Equal (string.Empty, view.Title);
  123. Assert.Equal (KeyCode.B, view.HotKey);
  124. commands = view.KeyBindings.GetCommands (KeyCode.A);
  125. Assert.DoesNotContain (Command.HotKey, commands);
  126. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
  127. Assert.DoesNotContain (Command.HotKey, commands);
  128. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
  129. Assert.DoesNotContain (Command.HotKey, commands);
  130. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
  131. Assert.DoesNotContain (Command.HotKey, commands);
  132. }
  133. [Theory]
  134. [InlineData (KeyCode.A)]
  135. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  136. [InlineData (KeyCode.D1)]
  137. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)]
  138. [InlineData ((KeyCode)'!')]
  139. [InlineData ((KeyCode)'х')] // Cyrillic x
  140. [InlineData ((KeyCode)'你')] // Chinese ni
  141. [InlineData ((KeyCode)'ö')] // German o umlaut
  142. [InlineData (KeyCode.Null)]
  143. public void Set_Sets_WithValidKey (KeyCode key)
  144. {
  145. var view = new View ();
  146. view.HotKey = key;
  147. Assert.Equal (key, view.HotKey);
  148. }
  149. [Theory]
  150. [InlineData (KeyCode.A)]
  151. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  152. [InlineData (KeyCode.D1)]
  153. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
  154. [InlineData ((KeyCode)'х')] // Cyrillic x
  155. [InlineData ((KeyCode)'你')] // Chinese ni
  156. [InlineData ((KeyCode)'ö')] // German o umlaut
  157. public void Set_SetsKeyBindings (KeyCode key)
  158. {
  159. var view = new View ();
  160. view.HotKey = key;
  161. Assert.Equal (string.Empty, view.Title);
  162. Assert.Equal (key, view.HotKey);
  163. // Verify key bindings were set
  164. // As passed
  165. Command [] commands = view.KeyBindings.GetCommands (view.HotKey);
  166. Assert.Contains (Command.HotKey, commands);
  167. Key baseKey = view.HotKey.NoShift;
  168. // If A...Z, with and without shift
  169. if (baseKey.IsKeyCodeAtoZ)
  170. {
  171. commands = view.KeyBindings.GetCommands (view.HotKey.WithShift);
  172. Assert.Contains (Command.HotKey, commands);
  173. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift);
  174. Assert.Contains (Command.HotKey, commands);
  175. commands = view.KeyBindings.GetCommands (view.HotKey.WithAlt);
  176. Assert.Contains (Command.HotKey, commands);
  177. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift.WithAlt);
  178. Assert.Contains (Command.HotKey, commands);
  179. }
  180. else
  181. {
  182. // Non A..Z keys should not have shift bindings
  183. if (view.HotKey.IsShift)
  184. {
  185. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift);
  186. Assert.Empty (commands);
  187. }
  188. else
  189. {
  190. commands = view.KeyBindings.GetCommands (view.HotKey.WithShift);
  191. Assert.Empty (commands);
  192. }
  193. }
  194. }
  195. [Fact]
  196. public void Set_Throws_If_Modifiers_Are_Included ()
  197. {
  198. var view = new View ();
  199. // A..Z must be naked (Alt is assumed)
  200. view.HotKey = Key.A.WithAlt;
  201. Assert.Throws<ArgumentException> (() => view.HotKey = Key.A.WithCtrl);
  202. Assert.Throws<ArgumentException> (
  203. () =>
  204. view.HotKey =
  205. KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask
  206. );
  207. // All others must not have Ctrl (Alt is assumed)
  208. view.HotKey = Key.D1.WithAlt;
  209. Assert.Throws<ArgumentException> (() => view.HotKey = Key.D1.WithCtrl);
  210. Assert.Throws<ArgumentException> (
  211. () =>
  212. view.HotKey =
  213. KeyCode.D1 | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask
  214. );
  215. // Shift is ok (e.g. this is '!')
  216. view.HotKey = Key.D1.WithShift;
  217. }
  218. [Theory]
  219. [InlineData (KeyCode.Delete)]
  220. [InlineData (KeyCode.Backspace)]
  221. [InlineData (KeyCode.Tab)]
  222. [InlineData (KeyCode.Enter)]
  223. [InlineData (KeyCode.Esc)]
  224. [InlineData (KeyCode.Space)]
  225. [InlineData (KeyCode.CursorLeft)]
  226. [InlineData (KeyCode.F1)]
  227. [InlineData (KeyCode.Null | KeyCode.ShiftMask)]
  228. public void Set_Throws_With_Invalid_Key (KeyCode key)
  229. {
  230. var view = new View ();
  231. Assert.Throws<ArgumentException> (() => view.HotKey = key);
  232. }
  233. [Theory]
  234. [InlineData ("Test", KeyCode.Null)]
  235. [InlineData ("^Test", KeyCode.T)]
  236. [InlineData ("T^est", KeyCode.E)]
  237. [InlineData ("Te^st", KeyCode.S)]
  238. [InlineData ("Tes^t", KeyCode.T)]
  239. [InlineData ("other", KeyCode.Null)]
  240. [InlineData ("oTher", KeyCode.Null)]
  241. [InlineData ("^Öther", (KeyCode)'Ö')]
  242. [InlineData ("^öther", (KeyCode)'ö')]
  243. // BUGBUG: '!' should be supported. Line 968 of TextFormatter filters on char.IsLetterOrDigit
  244. //[InlineData ("Test^!", (Key)'!')]
  245. public void Title_Change_Sets_HotKey (string title, KeyCode expectedHotKey)
  246. {
  247. var view = new View { HotKeySpecifier = new Rune ('^'), Title = "^Hello" };
  248. Assert.Equal (KeyCode.H, view.HotKey);
  249. view.Title = title;
  250. Assert.Equal (expectedHotKey, view.HotKey);
  251. }
  252. [Theory]
  253. [InlineData ("^Test")]
  254. public void Title_Empty_Sets_HotKey_To_Null (string title)
  255. {
  256. var view = new View { HotKeySpecifier = (Rune)'^', Title = title };
  257. Assert.Equal (title, view.Title);
  258. Assert.Equal (KeyCode.T, view.HotKey);
  259. view.Title = string.Empty;
  260. Assert.Equal ("", view.Title);
  261. Assert.Equal (KeyCode.Null, view.HotKey);
  262. }
  263. }