ViewKeyBindingTests.cs 4.1 KB

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