ViewKeyBindingTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class ViewKeyBindingTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public ViewKeyBindingTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void Focus_KeyBinding ()
  10. {
  11. var view = new ScopedKeyBindingView ();
  12. var invoked = false;
  13. view.InvokingKeyBindings += (s, e) => invoked = true;
  14. var top = new Toplevel ();
  15. top.Add (view);
  16. Application.Begin (top);
  17. Application.OnKeyDown (Key.A);
  18. Assert.True (invoked);
  19. invoked = false;
  20. Application.OnKeyDown (Key.H);
  21. Assert.True (invoked);
  22. invoked = false;
  23. Assert.False (view.HasFocus);
  24. Application.OnKeyDown (Key.F);
  25. Assert.False (invoked);
  26. Assert.False (view.FocusedCommand);
  27. invoked = false;
  28. view.CanFocus = true;
  29. view.SetFocus ();
  30. Assert.True (view.HasFocus);
  31. Application.OnKeyDown (Key.F);
  32. Assert.True (invoked);
  33. Assert.True (view.ApplicationCommand);
  34. Assert.True (view.HotKeyCommand);
  35. Assert.True (view.FocusedCommand);
  36. }
  37. [Fact]
  38. [AutoInitShutdown]
  39. public void Focus_KeyBinding_Negative ()
  40. {
  41. var view = new ScopedKeyBindingView ();
  42. var invoked = false;
  43. view.InvokingKeyBindings += (s, e) => invoked = true;
  44. var top = new Toplevel ();
  45. top.Add (view);
  46. Application.Begin (top);
  47. Application.OnKeyDown (Key.Z);
  48. Assert.False (invoked);
  49. Assert.False (view.ApplicationCommand);
  50. Assert.False (view.HotKeyCommand);
  51. Assert.False (view.FocusedCommand);
  52. invoked = false;
  53. Assert.False (view.HasFocus);
  54. Application.OnKeyDown (Key.F);
  55. Assert.False (invoked);
  56. Assert.False (view.ApplicationCommand);
  57. Assert.False (view.HotKeyCommand);
  58. Assert.False (view.FocusedCommand);
  59. }
  60. [Fact]
  61. [AutoInitShutdown]
  62. public void HotKey_KeyBinding ()
  63. {
  64. var view = new ScopedKeyBindingView ();
  65. var invoked = false;
  66. view.InvokingKeyBindings += (s, e) => invoked = true;
  67. var top = new Toplevel ();
  68. top.Add (view);
  69. Application.Begin (top);
  70. invoked = false;
  71. Application.OnKeyDown (Key.H);
  72. Assert.True (invoked);
  73. Assert.True (view.HotKeyCommand);
  74. view.HotKey = KeyCode.Z;
  75. invoked = false;
  76. view.HotKeyCommand = false;
  77. Application.OnKeyDown (Key.H); // old hot key
  78. Assert.False (invoked);
  79. Assert.False (view.HotKeyCommand);
  80. Application.OnKeyDown (Key.Z); // new hot key
  81. Assert.True (invoked);
  82. Assert.True (view.HotKeyCommand);
  83. }
  84. [Fact]
  85. [AutoInitShutdown]
  86. public void HotKey_KeyBinding_Negative ()
  87. {
  88. var view = new ScopedKeyBindingView ();
  89. var invoked = false;
  90. view.InvokingKeyBindings += (s, e) => invoked = true;
  91. var top = new Toplevel ();
  92. top.Add (view);
  93. Application.Begin (top);
  94. Application.OnKeyDown (Key.Z);
  95. Assert.False (invoked);
  96. Assert.False (view.HotKeyCommand);
  97. invoked = false;
  98. Application.OnKeyDown (Key.F);
  99. Assert.False (view.HotKeyCommand);
  100. }
  101. // tests that test KeyBindingScope.Focus and KeyBindingScope.HotKey (tests for KeyBindingScope.Application are in Application/KeyboardTests.cs)
  102. public class ScopedKeyBindingView : View
  103. {
  104. public ScopedKeyBindingView ()
  105. {
  106. AddCommand (Command.Save, () => ApplicationCommand = true);
  107. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  108. AddCommand (Command.Left, () => FocusedCommand = true);
  109. KeyBindings.Add (Key.A, KeyBindingScope.Application, Command.Save);
  110. HotKey = KeyCode.H;
  111. KeyBindings.Add (Key.F, KeyBindingScope.Focused, Command.Left);
  112. }
  113. public bool ApplicationCommand { get; set; }
  114. public bool FocusedCommand { get; set; }
  115. public bool HotKeyCommand { get; set; }
  116. }
  117. }