Files
Terminal.Gui/UnitTests/View/HotKeyTests.cs
Tig dcb3b359ad Fixes #2926 - Refactor KeyEvent and KeyEventEventArgs to simplify (#2927)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge

* Rearranged Event.cs to Keyboard.cs and Mouse.cs

* Renamed KeyEventEventArgs KeyEventArgs

* temp renamed KeyEvent OldKeyEvent

* Merged KeyEvent into KeyEventArgs

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Added Responder.KeyPressed

* Removed unused references

* Fixed arg naming

* InvokeKeybindings->InvokeKeyBindings

* InvokeKeybindings->InvokeKeyBindings

* Fixed unit tests fail

* More progress on refactoring key input; still broken and probably wrong

* Moved OnKeyPressed out of Responder and made ProcessKeyPrssed non-virtual

* Updated API docs

* Moved key handling from Responder to View

* Updated API docs

* Updated HotKey API docs

* Updated shortcut API docs

* Fixed responder unit tests

* Removed Shortcut from View as it is not used

* Removed unneeded OnHotKey override from Button

* Fixed BackTab logic

* Button now uses Key Bindings exclusively

* Button now uses Key Bindings exclusively

* Updated keyboard.md docs

* Fixed unit tests to account for Toplevel handling default button

* Added View.InvokeCommand API

* Modernized RadioGroup

* Removed ColdKey

* Modernized (partially) StatusBar

* Worked around FileDialog issue with Ctrl-F

* Fixed driver unit test; view must be focused to reciev key pressed

* Application code cleanup

* Start on updaing menu

* Menu now mostly works

* Menu Select refinement

* Fixed known menu bugs!

* Enabled HotKey to cause focus- experimental

* Fixes #3022 & adds unit test to prove it

* Actually Fixes #3022 & adds unit test to prove it

* Working through hotkey issues

* Misc fixes

* removed hot/cold key stuff from Keys scenario

* Fixed scenarios

* Simplified shortcut string handling

* Modernized Checkbox

* Modernized TileView

* Updated API docs

* Updated API docs

* attempting to publish v2 docs

* Revert "attempting to publish v2 docs"

This reverts commit 59dcec111b.

* Playing with api docs

* Removed Key.BackTab

* Removed Caps/Scroll/Numlock

* Partial removal of keymodifiers - unit tests pass

* Partial removal of keymodifiers - broke netdriver somewhere

* WindowsDriver & added KeyEventArgsTests

* Fixing menu shortcut/hotkeys - broke Menu.cs into separate files

* Fixed MenuBar!

* Finished modernizing Menu/MenuBar

* Removed Key.a-z. Broke lots of stuff

* checkout@v4

* progress on key mapping and formatting

* VK tests are still failing

* Fixed some unit tests

* Added Hotkey and Keybinding unit tests

* fixed unit test

* All unit tests pass again...

* Fixed broken unit tests

* KeyEventArgs.KeyValue -> AsRune

* Fixed bugs. Still some broken

* Added KeyEventArgs.IsAlpha. Added KeyEventArgs.cast ops. Fixed bugs. Unit tests pass

* Fixed WindowsDriver

* Oops.

* Refactoring based on bdisp's help. Not complete!

* removed calling into subviews from OnKeyBindings

* removed calling into subviews from OnKeyBindings

* Improved View KeyEvent unit tests

* More hotkey unit tests

* BIg change - Got rid of KeyPress w/in Application/Drivers

* Unit tests now pass again

* Refreshed API docs

* Better HotKey logic. More progress. Getting close.

* Fixed handling of shifted chars like ö

* Minor code cleanup

* Minor code cleanup2

* Why is build Action failing?

* Why is build Action failing??

* upgraded to .net8 to try to fix weird CI/CD build errors

* upgraded to .net8 to try to fix weird CI/CD build errors2

* Disabling TextViewTests to diagnose build errors

* reenable TextViewTests to diagnose build errors

* Arrrrrrg

* Merged v2_develop

* Fixed uppercase accented keys in WindowsDriver

* Fixed key binding api docs

* Experimental impl of CommandScope.SubViews for MenuBar

* Removed dead code from application.cs

* Removed dead code from application.cs

* Removed dead code from ConsoleDriver.cs

* Cleaned up some key binding stuff

* Disabled Alt to activate menu for now

* Updated label commands

* Fixed menu bugs. Upgraded menu unit tests

* Fixed unit tests

* Working on NetDriver

* fixed netdriver

* Fixed issues called out by @bdisp CR

* fixed CursesDriver

* added todo to netdriver

* Cherry picked treeview test fix 1b415e5

* Fix NetDriver.

* CommandScope->KeyBindingScope

* Address some tznind feedback

* Refactored KeyBindings big time!

* Added key consts to KeyEventArgs and renamed Key to ConsoleDriverKey

* Fixed some API docs

* Moved ConsoleDriverKey to ConsoleDriver.cs

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* renamed file I forgot to rename before

* Updated name and API docs of KeyEventArgs.isAlpha

* Fixed issues with OnKeyUp not doing the right thing.

* Fixed MainLoop.Running never being used

* Fixed MainLoop.Running never being used - unit tests

* Claned up BUGBUG comments

* Disabled a unit test to see why ci/cd tests are failing

* Removed defunct commented code

* Removed more defunct commented code

* Re-eanbled unit test; jsut removing one test case...

* Disabled more...

* Renambed Global->Applicaton and updated scope API docs

* Disabled more unit tests...

* Removed dead code

* Disabled more unit tests...2

* Disabled more unit tests...3

* Renambed Global->Applicaton and updated scope API docs 2

* Added more KeyBinding scope tests

* Added more KeyBinding scope tests2

* ConsoleDriverKey too long. Key too ambiguous. Settled on KeyCode. (Partialy because eventually I want to intro a class named Key).

* KeyEventArgs improvements. cast to Rune must be explicit as it's lossy

* Fixed warnings

* Renamed KeyEventArgs to Key... progress on fixing broken stuff that resulted

* Fix ConsoleKeyMapping bugs.

* Fix NetDriver issue from converting a lower case to a upper case.

* Started migration to Key from KeyCode - e.g. made HotKeys all consistent.

* Fixed build warnings

* Added key defns to Key

* KeyBindings now uses Key vs. KeyCode

* Verified by tweaking UICatalog

* Fixed treeview test ... again

* Renamed ProcessKeyDown/Up to NewKeyDown/Up and OnKeyPressed to OnProcessKeyDown to make things more clear

* Added test AllViews_KeyDown_All_EventsFire unit tests and fixed a few Views that were wrong

* fixed stupid KeyUp event bug

* If key not handled, return false for datefield

* dotnet test --no-restore --verbosity diag

* dotnet test --blame

* run tests on windows

* Fix TestVKPacket unit test and move it to ConsoleKeyMappingTests.cs file.

* Remove unnecessary commented code.

* Tweaked unit tests and removed Key.BareKey

* Fixed little details and updated api docs

* updated api docs

* AddKeyBindingsForHotKey: KeyCode->Key

* Cleaned up more old KeyCode usages. Added TODOs

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2023-12-16 12:04:23 -07:00

307 lines
9.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using Xunit;
using Xunit.Abstractions;
using System.Text;
namespace Terminal.Gui.ViewTests;
public class HotKeyTests {
readonly ITestOutputHelper _output;
public HotKeyTests (ITestOutputHelper output)
{
this._output = output;
}
[Fact]
public void Defaults ()
{
var view = new View ();
Assert.Equal (string.Empty, view.Title);
Assert.Equal (KeyCode.Null, view.HotKey);
// Verify key bindings were set
var commands = view.KeyBindings.GetCommands (KeyCode.Null);
Assert.Empty (commands);
}
[Theory]
[InlineData (KeyCode.A)]
[InlineData ((KeyCode)'a')]
[InlineData (KeyCode.A | KeyCode.ShiftMask)]
[InlineData (KeyCode.D1)]
[InlineData (KeyCode.D1 | KeyCode.ShiftMask)]
[InlineData ((KeyCode)'!')]
[InlineData ((KeyCode)'х')] // Cyrillic x
[InlineData ((KeyCode)'你')] // Chinese ni
[InlineData ((KeyCode)'ö')] // German o umlaut
[InlineData (KeyCode.Null)]
public void Set_Sets_WithValidKey (KeyCode key)
{
var view = new View ();
view.HotKey = key;
Assert.Equal (key, view.HotKey);
}
[Theory]
[InlineData (KeyCode.A)]
[InlineData (KeyCode.A | KeyCode.ShiftMask)]
[InlineData (KeyCode.D1)]
[InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
[InlineData ((KeyCode)'х')] // Cyrillic x
[InlineData ((KeyCode)'你')] // Chinese ni
[InlineData ((KeyCode)'ö')] // German o umlaut
public void Set_SetsKeyBindings (Key key)
{
var view = new View ();
view.HotKey = key;
Assert.Equal (string.Empty, view.Title);
Assert.Equal (key, view.HotKey);
// Verify key bindings were set
// As passed
var commands = view.KeyBindings.GetCommands (key);
Assert.Contains (Command.Accept, commands);
var baseKey = key.NoShift;
// If A...Z, with and without shift
if (baseKey.IsKeyCodeAtoZ) {
commands = view.KeyBindings.GetCommands (key.WithShift);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key.NoShift);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key.WithAlt);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key.NoShift.WithAlt);
Assert.Contains (Command.Accept, commands);
} else {
// Non A..Z keys should not have shift bindings
if (key.IsShift) {
commands = view.KeyBindings.GetCommands (key.NoShift);
Assert.Empty (commands);
} else {
commands = view.KeyBindings.GetCommands (key.WithShift);
Assert.Empty (commands);
}
}
}
[Fact]
public void Set_RemovesOldKeyBindings ()
{
var view = new View ();
view.HotKey = KeyCode.A;
Assert.Equal (string.Empty, view.Title);
Assert.Equal (KeyCode.A, view.HotKey);
// Verify key bindings were set
var commands = view.KeyBindings.GetCommands (KeyCode.A);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
Assert.Contains (Command.Accept, commands);
// Now set again
view.HotKey = KeyCode.B;
Assert.Equal (string.Empty, view.Title);
Assert.Equal (KeyCode.B, view.HotKey);
commands = view.KeyBindings.GetCommands (KeyCode.A);
Assert.DoesNotContain (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask);
Assert.DoesNotContain (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.AltMask);
Assert.DoesNotContain (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask);
Assert.DoesNotContain (Command.Accept, commands);
}
[Fact]
public void Set_Throws_If_Modifiers_Are_Included ()
{
var view = new View ();
// A..Z must be naked (Alt is assumed)
view.HotKey = KeyCode.A | KeyCode.AltMask;
Assert.Throws<ArgumentException> (() => view.HotKey = KeyCode.A | KeyCode.CtrlMask);
Assert.Throws<ArgumentException> (() => view.HotKey = KeyCode.A | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask);
// All others must not have Ctrl (Alt is assumed)
view.HotKey = KeyCode.D1 | KeyCode.AltMask;
Assert.Throws<ArgumentException> (() => view.HotKey = KeyCode.D1 | KeyCode.CtrlMask);
Assert.Throws<ArgumentException> (() => view.HotKey = KeyCode.D1 | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask);
// Shift is ok (e.g. this is '!')
view.HotKey = KeyCode.D1 | KeyCode.ShiftMask;
}
[Theory]
[InlineData (KeyCode.A)]
[InlineData (KeyCode.A | KeyCode.ShiftMask)]
[InlineData (KeyCode.D1)]
[InlineData (KeyCode.D1 | KeyCode.ShiftMask)] // '!'
[InlineData ((KeyCode)'х')] // Cyrillic x
[InlineData ((KeyCode)'你')] // Chinese ni
public void AddKeyBindingsForHotKey_Sets (KeyCode key)
{
var view = new View ();
view.HotKey = KeyCode.Z;
Assert.Equal (string.Empty, view.Title);
Assert.Equal (KeyCode.Z, view.HotKey);
view.AddKeyBindingsForHotKey (KeyCode.Null, key);
// Verify key bindings were set
// As passed
var commands = view.KeyBindings.GetCommands (key);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key | KeyCode.AltMask);
Assert.Contains (Command.Accept, commands);
var baseKey = key & ~KeyCode.ShiftMask;
// If A...Z, with and without shift
if (baseKey is >= KeyCode.A and <= KeyCode.Z) {
commands = view.KeyBindings.GetCommands (key | KeyCode.ShiftMask);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key | KeyCode.AltMask);
Assert.Contains (Command.Accept, commands);
commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask | KeyCode.AltMask);
Assert.Contains (Command.Accept, commands);
} else {
// Non A..Z keys should not have shift bindings
if (key.HasFlag (KeyCode.ShiftMask)) {
commands = view.KeyBindings.GetCommands (key & ~KeyCode.ShiftMask);
Assert.Empty (commands);
} else {
commands = view.KeyBindings.GetCommands (key | KeyCode.ShiftMask);
Assert.Empty (commands);
}
}
}
[Theory]
[InlineData (KeyCode.Delete)]
[InlineData (KeyCode.Backspace)]
[InlineData (KeyCode.Tab)]
[InlineData (KeyCode.Enter)]
[InlineData (KeyCode.Esc)]
[InlineData (KeyCode.Space)]
[InlineData (KeyCode.CursorLeft)]
[InlineData (KeyCode.F1)]
[InlineData (KeyCode.Unknown)]
public void Set_Throws_With_Invalid_Key (KeyCode key)
{
var view = new View ();
Assert.Throws<ArgumentException> (() => view.HotKey = key);
}
[Theory]
[InlineData ("Test", KeyCode.T)]
[InlineData ("^Test", KeyCode.T)]
[InlineData ("T^est", KeyCode.E)]
[InlineData ("Te^st", KeyCode.S)]
[InlineData ("Tes^t", KeyCode.T)]
[InlineData ("other", KeyCode.Null)]
[InlineData ("oTher", KeyCode.T)]
[InlineData ("^Öther", (KeyCode)'Ö')]
[InlineData ("^öther", (KeyCode)'ö')]
// BUGBUG: '!' should be supported. Line 968 of TextFormatter filters on char.IsLetterOrDigit
//[InlineData ("Test^!", (Key)'!')]
public void Text_Change_Sets_HotKey (string text, KeyCode expectedHotKey)
{
var view = new View () {
HotKeySpecifier = new Rune ('^'),
Text = "^Hello"
};
Assert.Equal (KeyCode.H, view.HotKey);
view.Text = text;
Assert.Equal (expectedHotKey, view.HotKey);
}
[Theory]
[InlineData("^Test")]
public void Text_Empty_Sets_HotKey_To_Null (string text)
{
var view = new View () {
HotKeySpecifier = (Rune)'^',
Text = text
};
Assert.Equal (text, view.Text);
Assert.Equal (KeyCode.T, view.HotKey);
view.Text = string.Empty;
Assert.Equal ("", view.Text);
Assert.Equal (KeyCode.Null, view.HotKey);
}
[Theory]
[InlineData (KeyCode.Null, true)] // non-shift
[InlineData (KeyCode.ShiftMask, true)]
[InlineData (KeyCode.AltMask, true)]
[InlineData (KeyCode.ShiftMask | KeyCode.AltMask, true)]
[InlineData (KeyCode.CtrlMask, false)]
[InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask, false)]
public void KeyPress_Runs_Default_HotKey_Command (KeyCode mask, bool expected)
{
var view = new View () {
HotKeySpecifier = (Rune)'^',
Text = "^Test"
};
view.CanFocus = true;
Assert.False (view.HasFocus);
view.NewKeyDownEvent (new (KeyCode.T | mask));
Assert.Equal (expected, view.HasFocus);
}
[Fact]
public void ProcessKeyDown_Invokes_HotKey_Command_With_SuperView ()
{
var view = new View () {
HotKeySpecifier = (Rune)'^',
Text = "^Test"
};
var superView = new View ();
superView.Add (view);
view.CanFocus = true;
Assert.False (view.HasFocus);
var ke = new Key (KeyCode.T);
superView.NewKeyDownEvent (ke);
Assert.True (view.HasFocus);
}
[Fact]
public void ProcessKeyDown_Ignores_KeyBindings_Out_Of_Scope_SuperView ()
{
var view = new View ();
view.KeyBindings.Add (KeyCode.A, Command.Default);
view.InvokingKeyBindings += (s, e) => {
Assert.Fail ();
};
var superView = new View ();
superView.Add (view);
var ke = new Key (KeyCode.A);
superView.NewKeyDownEvent (ke);
}
}