2
0

ViewKeyBindingTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.RaiseKeyDownEvent (Key.A);
  17. Assert.False (invoked);
  18. Assert.True (view.ApplicationCommand);
  19. invoked = false;
  20. Application.RaiseKeyDownEvent (Key.H);
  21. Assert.True (invoked);
  22. invoked = false;
  23. Assert.False (view.HasFocus);
  24. Application.RaiseKeyDownEvent (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.RaiseKeyDownEvent (Key.F);
  32. Assert.True (invoked);
  33. Assert.True (view.ApplicationCommand);
  34. Assert.True (view.HotKeyCommand);
  35. Assert.True (view.FocusedCommand);
  36. top.Dispose ();
  37. }
  38. [Fact]
  39. [AutoInitShutdown]
  40. public void Focus_KeyBinding_Negative ()
  41. {
  42. var view = new ScopedKeyBindingView ();
  43. var invoked = false;
  44. view.InvokingKeyBindings += (s, e) => invoked = true;
  45. var top = new Toplevel ();
  46. top.Add (view);
  47. Application.Begin (top);
  48. Application.RaiseKeyDownEvent (Key.Z);
  49. Assert.False (invoked);
  50. Assert.False (view.ApplicationCommand);
  51. Assert.False (view.HotKeyCommand);
  52. Assert.False (view.FocusedCommand);
  53. invoked = false;
  54. Assert.False (view.HasFocus);
  55. Application.RaiseKeyDownEvent (Key.F);
  56. Assert.False (invoked);
  57. Assert.False (view.ApplicationCommand);
  58. Assert.False (view.HotKeyCommand);
  59. Assert.False (view.FocusedCommand);
  60. top.Dispose ();
  61. }
  62. [Fact]
  63. [AutoInitShutdown]
  64. public void HotKey_KeyBinding ()
  65. {
  66. var view = new ScopedKeyBindingView ();
  67. var invoked = false;
  68. view.InvokingKeyBindings += (s, e) => invoked = true;
  69. var top = new Toplevel ();
  70. top.Add (view);
  71. Application.Begin (top);
  72. invoked = false;
  73. Application.RaiseKeyDownEvent (Key.H);
  74. Assert.True (invoked);
  75. Assert.True (view.HotKeyCommand);
  76. view.HotKey = KeyCode.Z;
  77. invoked = false;
  78. view.HotKeyCommand = false;
  79. Application.RaiseKeyDownEvent (Key.H); // old hot key
  80. Assert.False (invoked);
  81. Assert.False (view.HotKeyCommand);
  82. Application.RaiseKeyDownEvent (Key.Z); // new hot key
  83. Assert.True (invoked);
  84. Assert.True (view.HotKeyCommand);
  85. top.Dispose ();
  86. }
  87. [Fact]
  88. [AutoInitShutdown]
  89. public void HotKey_KeyBinding_Negative ()
  90. {
  91. var view = new ScopedKeyBindingView ();
  92. var invoked = false;
  93. view.InvokingKeyBindings += (s, e) => invoked = true;
  94. var top = new Toplevel ();
  95. top.Add (view);
  96. Application.Begin (top);
  97. Application.RaiseKeyDownEvent (Key.Z);
  98. Assert.False (invoked);
  99. Assert.False (view.HotKeyCommand);
  100. invoked = false;
  101. Application.RaiseKeyDownEvent (Key.F);
  102. Assert.False (view.HotKeyCommand);
  103. top.Dispose ();
  104. }
  105. // tests that test KeyBindingScope.Focus and KeyBindingScope.HotKey (tests for KeyBindingScope.Application are in Application/KeyboardTests.cs)
  106. public class ScopedKeyBindingView : View
  107. {
  108. public ScopedKeyBindingView ()
  109. {
  110. AddCommand (Command.Save, () => ApplicationCommand = true);
  111. AddCommand (Command.HotKey, () => HotKeyCommand = true);
  112. AddCommand (Command.Left, () => FocusedCommand = true);
  113. Application.KeyBindings.Add (Key.A, this, Command.Save);
  114. HotKey = KeyCode.H;
  115. KeyBindings.Add (Key.F, KeyBindingScope.Focused, Command.Left);
  116. }
  117. public bool ApplicationCommand { get; set; }
  118. public bool FocusedCommand { get; set; }
  119. public bool HotKeyCommand { get; set; }
  120. }
  121. }