KeyBindingsTests.cs 4.5 KB

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