HotKeyTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 NewKeyDownEvent_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 NewKeyDownEvent_Ignores_Focus_KeyBindings_SuperView ()
  83. {
  84. var view = new View ();
  85. view.KeyBindings.Add (Key.A, Command.HotKey); // implies KeyBindingScope.Focused - so this should not be invoked
  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 NewKeyDownEvent_Honors_HotKey_KeyBindings_SuperView ()
  94. {
  95. var view = new View ();
  96. view.KeyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey);
  97. bool invoked = false;
  98. view.InvokingKeyBindings += (s, e) => { invoked = true; };
  99. var superView = new View ();
  100. superView.Add (view);
  101. var ke = Key.A;
  102. superView.NewKeyDownEvent (ke);
  103. Assert.True (invoked);
  104. }
  105. [Fact]
  106. public void NewKeyDownEvent_InNewKeyDownEventvokes_HotKey_Command_With_SuperView ()
  107. {
  108. var view = new View { HotKeySpecifier = (Rune)'^', Title = "^Test" };
  109. var superView = new View ();
  110. superView.Add (view);
  111. view.CanFocus = true;
  112. Assert.False (view.HasFocus);
  113. var ke = Key.T;
  114. superView.NewKeyDownEvent (ke);
  115. Assert.True (view.HasFocus);
  116. }
  117. [Fact]
  118. public void Set_RemovesOldKeyBindings ()
  119. {
  120. var view = new View ();
  121. view.HotKey = KeyCode.A;
  122. Assert.Equal (string.Empty, view.Title);
  123. Assert.Equal (KeyCode.A, view.HotKey);
  124. // Verify key bindings were set
  125. Command [] commands = view.KeyBindings.GetCommands (KeyCode.A);
  126. Assert.Contains (Command.HotKey, commands);
  127. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
  128. Assert.Contains (Command.HotKey, commands);
  129. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
  130. Assert.Contains (Command.HotKey, commands);
  131. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
  132. Assert.Contains (Command.HotKey, commands);
  133. // Now set again
  134. view.HotKey = KeyCode.B;
  135. Assert.Equal (string.Empty, view.Title);
  136. Assert.Equal (KeyCode.B, view.HotKey);
  137. commands = view.KeyBindings.GetCommands (KeyCode.A);
  138. Assert.DoesNotContain (Command.HotKey, commands);
  139. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
  140. Assert.DoesNotContain (Command.HotKey, commands);
  141. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
  142. Assert.DoesNotContain (Command.HotKey, commands);
  143. commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
  144. Assert.DoesNotContain (Command.HotKey, commands);
  145. }
  146. [Theory]
  147. [InlineData (KeyCode.A)]
  148. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  149. [InlineData (KeyCode.D1)]
  150. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)]
  151. [InlineData ((KeyCode)'!')]
  152. [InlineData ((KeyCode)'х')] // Cyrillic x
  153. [InlineData ((KeyCode)'你')] // Chinese ni
  154. [InlineData ((KeyCode)'ö')] // German o umlaut
  155. [InlineData (KeyCode.Null)]
  156. public void Set_Sets_WithValidKey (KeyCode key)
  157. {
  158. var view = new View ();
  159. view.HotKey = key;
  160. Assert.Equal (key, view.HotKey);
  161. }
  162. [Theory]
  163. [InlineData (KeyCode.A)]
  164. [InlineData (KeyCode.A | KeyCode.ShiftMask)]
  165. [InlineData (KeyCode.D1)]
  166. [InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
  167. [InlineData ((KeyCode)'х')] // Cyrillic x
  168. [InlineData ((KeyCode)'你')] // Chinese ni
  169. [InlineData ((KeyCode)'ö')] // German o umlaut
  170. public void Set_SetsKeyBindings (KeyCode key)
  171. {
  172. var view = new View ();
  173. view.HotKey = key;
  174. Assert.Equal (string.Empty, view.Title);
  175. Assert.Equal (key, view.HotKey);
  176. // Verify key bindings were set
  177. // As passed
  178. Command [] commands = view.KeyBindings.GetCommands (view.HotKey);
  179. Assert.Contains (Command.HotKey, commands);
  180. Key baseKey = view.HotKey.NoShift;
  181. // If A...Z, with and without shift
  182. if (baseKey.IsKeyCodeAtoZ)
  183. {
  184. commands = view.KeyBindings.GetCommands (view.HotKey.WithShift);
  185. Assert.Contains (Command.HotKey, commands);
  186. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift);
  187. Assert.Contains (Command.HotKey, commands);
  188. commands = view.KeyBindings.GetCommands (view.HotKey.WithAlt);
  189. Assert.Contains (Command.HotKey, commands);
  190. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift.WithAlt);
  191. Assert.Contains (Command.HotKey, commands);
  192. }
  193. else
  194. {
  195. // Non A..Z keys should not have shift bindings
  196. if (view.HotKey.IsShift)
  197. {
  198. commands = view.KeyBindings.GetCommands (view.HotKey.NoShift);
  199. Assert.Empty (commands);
  200. }
  201. else
  202. {
  203. commands = view.KeyBindings.GetCommands (view.HotKey.WithShift);
  204. Assert.Empty (commands);
  205. }
  206. }
  207. }
  208. [Fact]
  209. public void Set_Throws_If_Modifiers_Are_Included ()
  210. {
  211. var view = new View ();
  212. // A..Z must be naked (Alt is assumed)
  213. view.HotKey = Key.A.WithAlt;
  214. Assert.Throws<ArgumentException> (() => view.HotKey = Key.A.WithCtrl);
  215. Assert.Throws<ArgumentException> (
  216. () =>
  217. view.HotKey =
  218. KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask
  219. );
  220. // All others must not have Ctrl (Alt is assumed)
  221. view.HotKey = Key.D1.WithAlt;
  222. Assert.Throws<ArgumentException> (() => view.HotKey = Key.D1.WithCtrl);
  223. Assert.Throws<ArgumentException> (
  224. () =>
  225. view.HotKey =
  226. KeyCode.D1 | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask
  227. );
  228. // Shift is ok (e.g. this is '!')
  229. view.HotKey = Key.D1.WithShift;
  230. }
  231. [Theory]
  232. [InlineData (KeyCode.Delete)]
  233. [InlineData (KeyCode.Backspace)]
  234. [InlineData (KeyCode.Tab)]
  235. [InlineData (KeyCode.Enter)]
  236. [InlineData (KeyCode.Esc)]
  237. [InlineData (KeyCode.Space)]
  238. [InlineData (KeyCode.CursorLeft)]
  239. [InlineData (KeyCode.F1)]
  240. [InlineData (KeyCode.Null | KeyCode.ShiftMask)]
  241. public void Set_Throws_With_Invalid_Key (KeyCode key)
  242. {
  243. var view = new View ();
  244. Assert.Throws<ArgumentException> (() => view.HotKey = key);
  245. }
  246. [Theory]
  247. [InlineData ("Test", KeyCode.Null)]
  248. [InlineData ("^Test", KeyCode.T)]
  249. [InlineData ("T^est", KeyCode.E)]
  250. [InlineData ("Te^st", KeyCode.S)]
  251. [InlineData ("Tes^t", KeyCode.T)]
  252. [InlineData ("other", KeyCode.Null)]
  253. [InlineData ("oTher", KeyCode.Null)]
  254. [InlineData ("^Öther", (KeyCode)'Ö')]
  255. [InlineData ("^öther", (KeyCode)'ö')]
  256. // BUGBUG: '!' should be supported. Line 968 of TextFormatter filters on char.IsLetterOrDigit
  257. //[InlineData ("Test^!", (Key)'!')]
  258. public void Title_Change_Sets_HotKey (string title, KeyCode expectedHotKey)
  259. {
  260. var view = new View { HotKeySpecifier = new Rune ('^'), Title = "^Hello" };
  261. Assert.Equal (KeyCode.H, view.HotKey);
  262. view.Title = title;
  263. Assert.Equal (expectedHotKey, view.HotKey);
  264. }
  265. [Theory]
  266. [InlineData ("^Test")]
  267. public void Title_Empty_Sets_HotKey_To_Null (string title)
  268. {
  269. var view = new View { HotKeySpecifier = (Rune)'^', Title = title };
  270. Assert.Equal (title, view.Title);
  271. Assert.Equal (KeyCode.T, view.HotKey);
  272. view.Title = string.Empty;
  273. Assert.Equal ("", view.Title);
  274. Assert.Equal (KeyCode.Null, view.HotKey);
  275. }
  276. }