Application Keybinding tests

This commit is contained in:
Tig
2024-06-08 09:19:38 -06:00
parent c2dcd28a15
commit ab655feadc
4 changed files with 156 additions and 42 deletions

View File

@@ -193,6 +193,9 @@ public class ApplicationTests
Assert.Empty (Application._topLevels);
Assert.Null (Application._mouseEnteredView);
// Keyboard
Assert.Empty (Application.GetViewsWithKeyBindings ());
// Events - Can't check
//Assert.Null (Application.NotifyNewRunState);
//Assert.Null (Application.NotifyNewRunState);
@@ -225,6 +228,7 @@ public class ApplicationTests
Application.AlternateBackwardKey = Key.A;
Application.AlternateForwardKey = Key.B;
Application.QuitKey = Key.C;
Application.AddKeyBinding(Key.A, new View ());
//Application.OverlappedChildren = new List<View> ();
//Application.OverlappedTop =

View File

@@ -2,6 +2,9 @@
namespace Terminal.Gui.ApplicationTests;
/// <summary>
/// Application tests for keyboard support.
/// </summary>
public class KeyboardTests
{
private readonly ITestOutputHelper _output;
@@ -15,6 +18,46 @@ public class KeyboardTests
#endif
}
[Fact]
[AutoInitShutdown]
public void QuitKey_Getter_Setter ()
{
Toplevel top = new ();
var isQuiting = false;
top.Closing += (s, e) =>
{
isQuiting = true;
e.Cancel = true;
};
Application.Begin (top);
top.Running = true;
Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
Assert.True (isQuiting);
isQuiting = false;
Application.OnKeyDown (Key.Q.WithCtrl);
Assert.True (isQuiting);
isQuiting = false;
Application.QuitKey = Key.C.WithCtrl;
Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
Assert.False (isQuiting);
Application.OnKeyDown (Key.Q.WithCtrl);
Assert.False (isQuiting);
Application.OnKeyDown (Application.QuitKey);
Assert.True (isQuiting);
// Reset the QuitKey to avoid throws errors on another tests
Application.QuitKey = Key.Q.WithCtrl;
top.Dispose ();
}
[Fact]
public void AlternateForwardKey_AlternateBackwardKey_Tests ()
{
@@ -320,7 +363,7 @@ public class KeyboardTests
[Fact]
[AutoInitShutdown]
public void OnKeyDown_Application_KeyBinding ()
public void KeyBinding_OnKeyDown ()
{
var view = new ScopedKeyBindingView ();
var invoked = false;
@@ -365,7 +408,7 @@ public class KeyboardTests
[Fact]
[AutoInitShutdown]
public void OnKeyDown_Application_KeyBinding_Negative ()
public void KeyBinding_OnKeyDown_Negative ()
{
var view = new ScopedKeyBindingView ();
var invoked = false;
@@ -391,46 +434,73 @@ public class KeyboardTests
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void QuitKey_Getter_Setter ()
public void KeyBinding_AddKeyBinding_Adds ()
{
Toplevel top = new ();
var isQuiting = false;
View view1 = new ();
Application.AddKeyBinding (Key.A, view1);
top.Closing += (s, e) =>
{
isQuiting = true;
e.Cancel = true;
};
View view2 = new ();
Application.AddKeyBinding (Key.A, view2);
Application.Begin (top);
top.Running = true;
Assert.True (Application.TryGetKeyBindings (Key.A, out List<View> views));
Assert.Contains (view1, views);
Assert.Contains (view2, views);
Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, Application.QuitKey.KeyCode);
Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
Assert.True (isQuiting);
isQuiting = false;
Application.OnKeyDown (Key.Q.WithCtrl);
Assert.True (isQuiting);
isQuiting = false;
Application.QuitKey = Key.C.WithCtrl;
Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
Assert.False (isQuiting);
Application.OnKeyDown (Key.Q.WithCtrl);
Assert.False (isQuiting);
Application.OnKeyDown (Application.QuitKey);
Assert.True (isQuiting);
// Reset the QuitKey to avoid throws errors on another tests
Application.QuitKey = Key.Q.WithCtrl;
top.Dispose ();
Assert.False (Application.TryGetKeyBindings (Key.B, out List<View> _));
}
// test Application key Bindings
[Fact]
[AutoInitShutdown]
public void KeyBinding_ViewKeyBindings_Add_Adds ()
{
View view1 = new ();
view1.KeyBindings.Add (Key.A, KeyBindingScope.Application, Command.Save);
view1.KeyBindings.Add (Key.B, KeyBindingScope.HotKey, Command.Left);
Assert.Single (Application.GetViewsWithKeyBindings ());
View view2 = new ();
view2.KeyBindings.Add (Key.A, KeyBindingScope.Application, Command.Save);
view2.KeyBindings.Add (Key.B, KeyBindingScope.HotKey, Command.Left);
Assert.True (Application.TryGetKeyBindings (Key.A, out List<View> views));
Assert.Contains (view1, views);
Assert.Contains (view2, views);
Assert.False (Application.TryGetKeyBindings (Key.B, out List<View> _));
}
[Fact]
[AutoInitShutdown]
public void KeyBinding_RemoveKeyBinding_Removes ()
{
View view1 = new ();
Application.AddKeyBinding (Key.A, view1);
Assert.True (Application.TryGetKeyBindings (Key.A, out List<View> views));
Assert.Contains (view1, views);
Application.RemoveKeyBinding (Key.A, view1);
Assert.False (Application.TryGetKeyBindings (Key.A, out List<View> _));
}
[Fact]
[AutoInitShutdown]
public void KeyBinding_ViewKeyBindings_RemoveKeyBinding_Removes ()
{
View view1 = new ();
view1.KeyBindings.Add (Key.A, KeyBindingScope.Application, Command.Save);
Assert.True (Application.TryGetKeyBindings (Key.A, out List<View> views));
Assert.Contains (view1, views);
view1.KeyBindings.Remove (Key.A);
Assert.False (Application.TryGetKeyBindings (Key.A, out List<View> _));
}
// Test View for testing Application key Bindings
public class ScopedKeyBindingView : View
{
public ScopedKeyBindingView ()