Files
Terminal.Gui/UnitTests/Input/KeyBindingTests.cs
Tig 16055c53b0 Fixes #3039. Fix View.HotKey (#3249)
* Added View.DefaultCommand etc... Started on dedicated scenario

* Fixed un-shifted hotkeys -> Fixed Key Equals. Fixed WindowsDriver passing wrong key. Etc.

* Fixed Key Bindings and HotKeys

* Fixed Key Bindings and HotKeys

* Label now correctly supports hotkey

* Disabled unix hot keys because they are annoying and get in the way

* Updated nuget. fixed warnings

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Trying to fix ci/ci issue

* Changed TextChangingEventArgs to inherit from CancelEventArgs

* TextChangingEventArgs -> TextEventArgs

* Simplified Text events by having only on args class

* Fixed unit tests fail

* Simplified by removing TitleEventArgs

* POC of Title being primary for hotkey. Label and Button hacked to work

* POC of Title being primary for hotkey. Label and Button hacked to work - all unit tests pass

* Dropped Microsoft.NETFramework.ReferenceAssemblies

* Fixed Dialogs scenario hotkeys

* Fixed build warnings

* Fixed Border Title render bug

* Regiggering default command handling

* Regiggering default command handling

* Checkbox clean up

* Added StateEventArgs POC

* Command.Default -> Command.HotKey

* Command.Default -> Command.HotKey - fixed TableView

* Command.Default -> Command.HotKey - fixed TableView

* Updated reactive example

* Fixed Toplevel.BringOverlappedTopToFront - was reordering SubViews when it shouldn't

* WIP - broke

* Finished impl of StateEventArgs

* Deleted ToggleEventArgs.cs. Added StateEventArgs.cs

* XML doc fix

* Removed old code

* Removed commented out code

* Label.Clicked -> Label.Accept (missed this before)

* Removed Labels as Buttons scenario as it's not really  useful

* Moved SubView tests to own file

* Moved SubView tests to own file

* Simplified Text test

* Added OnAccept test

* Deleted DefaultCommand

* Modernized CheckBox

* New button test

* Cleaned up RadioGroup; added tests

* KeyCode->Key in ListView

* Added ListView unit tests

* ListView now does Accept correctly

* TreeView now does Accept correctly

* Cleaned up some TextField tests

* TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Fixed ComboBox to deal with TextView now handles Accept properly; updated CharMap and Adornments scenarios to test

* Removed un-needed using statement
2024-02-22 15:11:26 -07:00

289 lines
9.9 KiB
C#

using UICatalog.Scenarios;
using Xunit.Abstractions;
namespace Terminal.Gui.InputTests;
public class KeyBindingTests
{
private readonly ITestOutputHelper _output;
public KeyBindingTests (ITestOutputHelper output) { _output = output; }
[Fact]
public void Add_Empty_Throws ()
{
var keyBindings = new KeyBindings ();
List<Command> commands = new ();
Assert.Throws<ArgumentException> (() => keyBindings.Add (Key.A, commands.ToArray ()));
}
[Fact]
public void Add_Multiple_Adds ()
{
var keyBindings = new KeyBindings ();
Command [] commands = { Command.Right, Command.Left };
keyBindings.Add (Key.A, commands);
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
keyBindings.Add (Key.B, commands);
resultCommands = keyBindings.GetCommands (Key.B);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
[Fact]
public void Add_Single_Adds ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.HotKey);
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings.Add (Key.B, Command.HotKey);
resultCommands = keyBindings.GetCommands (Key.B);
Assert.Contains (Command.HotKey, resultCommands);
}
// Clear
[Fact]
public void Clear_Clears ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.B, Command.HotKey);
keyBindings.Clear ();
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Empty (resultCommands);
resultCommands = keyBindings.GetCommands (Key.B);
Assert.Empty (resultCommands);
}
[Fact]
public void Defaults ()
{
var keyBindings = new KeyBindings ();
Assert.Throws<InvalidOperationException> (() => keyBindings.GetKeyFromCommands (Command.Accept));
}
// GetCommands
[Fact]
public void GetCommands_Unknown_ReturnsEmpty ()
{
var keyBindings = new KeyBindings ();
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Empty (resultCommands);
}
[Fact]
public void GetCommands_WithCommands_ReturnsCommands ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.HotKey);
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
}
[Fact]
public void GetCommands_WithMultipleBindings_ReturnsCommands ()
{
var keyBindings = new KeyBindings ();
Command [] commands = { Command.Right, Command.Left };
keyBindings.Add (Key.A, commands);
keyBindings.Add (Key.B, commands);
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
resultCommands = keyBindings.GetCommands (Key.B);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
[Fact]
public void GetCommands_WithMultipleCommands_ReturnsCommands ()
{
var keyBindings = new KeyBindings ();
Command [] commands = { Command.Right, Command.Left };
keyBindings.Add (Key.A, commands);
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
[Fact]
public void GetKeyFromCommands_MultipleCommands ()
{
var keyBindings = new KeyBindings ();
Command [] commands1 = { Command.Right, Command.Left };
keyBindings.Add (Key.A, commands1);
Command [] commands2 = { Command.LineUp, Command.LineDown };
keyBindings.Add (Key.B, commands2);
Key key = keyBindings.GetKeyFromCommands (commands1);
Assert.Equal (Key.A, key);
key = keyBindings.GetKeyFromCommands (commands2);
Assert.Equal (Key.B, key);
// Negative case
Assert.Throws<InvalidOperationException> (() => key = keyBindings.GetKeyFromCommands (Command.EndOfLine));
}
[Fact]
public void GetKeyFromCommands_OneCommand ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Right);
Key key = keyBindings.GetKeyFromCommands (Command.Right);
Assert.Equal (Key.A, key);
// Negative case
Assert.Throws<InvalidOperationException> (() => key = keyBindings.GetKeyFromCommands (Command.Left));
}
// GetKeyFromCommands
[Fact]
public void GetKeyFromCommands_Unknown_Throws_InvalidOperationException ()
{
var keyBindings = new KeyBindings ();
Assert.Throws<InvalidOperationException> (() => keyBindings.GetKeyFromCommands (Command.Accept));
}
[Fact]
public void GetKeyFromCommands_WithCommands_ReturnsKey ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.HotKey);
Key resultKey = keyBindings.GetKeyFromCommands (Command.HotKey);
Assert.Equal (Key.A, resultKey);
}
[Fact]
public void Replace_Key ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.HotKey);
keyBindings.Add (Key.B, Command.HotKey);
keyBindings.Add (Key.C, Command.HotKey);
keyBindings.Add (Key.D, Command.HotKey);
keyBindings.Replace (Key.A, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.A));
Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
keyBindings.Replace (Key.B, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.B));
Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
keyBindings.Replace (Key.C, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.C));
Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
keyBindings.Replace (Key.D, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.D));
Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
}
// Add with scope does the right things
[Theory]
[InlineData (KeyBindingScope.Focused)]
[InlineData (KeyBindingScope.HotKey)]
[InlineData (KeyBindingScope.Application)]
public void Scope_Add_Adds (KeyBindingScope scope)
{
var keyBindings = new KeyBindings ();
Command [] commands = { Command.Right, Command.Left };
var key = new Key (Key.A);
keyBindings.Add (Key.A, scope, commands);
KeyBinding binding = keyBindings.Get (key);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
binding = keyBindings.Get (key, scope);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
Command [] resultCommands = keyBindings.GetCommands (key);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
[Theory]
[InlineData (KeyBindingScope.Focused)]
[InlineData (KeyBindingScope.HotKey)]
[InlineData (KeyBindingScope.Application)]
public void Scope_Get_Filters (KeyBindingScope scope)
{
var keyBindings = new KeyBindings ();
Command [] commands = { Command.Right, Command.Left };
var key = new Key (Key.A);
keyBindings.Add (key, scope, commands);
KeyBinding binding = keyBindings.Get (key);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
binding = keyBindings.Get (key, scope);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
// negative test
binding = keyBindings.Get (key, (KeyBindingScope)0);
Assert.Null (binding);
Command [] resultCommands = keyBindings.GetCommands (key);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
[Theory]
[InlineData (KeyBindingScope.Focused)]
[InlineData (KeyBindingScope.HotKey)]
[InlineData (KeyBindingScope.Application)]
public void Scope_TryGet_Filters (KeyBindingScope scope)
{
var keyBindings = new KeyBindings ();
Command [] commands = { Command.Right, Command.Left };
var key = new Key (Key.A);
keyBindings.Add (key, scope, commands);
bool success = keyBindings.TryGet (key, out KeyBinding binding);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
success = keyBindings.TryGet (key, scope, out binding);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
// negative test
success = keyBindings.TryGet (key, (KeyBindingScope)0, out binding);
Assert.False (success);
Command [] resultCommands = keyBindings.GetCommands (key);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
// TryGet
[Fact]
public void TryGet_Unknown_ReturnsFalse ()
{
var keyBindings = new KeyBindings ();
bool result = keyBindings.TryGet (Key.A, out KeyBinding _);
Assert.False (result);
}
[Fact]
public void TryGet_WithCommands_ReturnsTrue ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.HotKey);
bool result = keyBindings.TryGet (Key.A, out KeyBinding bindings);
Assert.True (result);
Assert.Contains (Command.HotKey, bindings.Commands);
}
}