Files
Terminal.Gui/UnitTests/Input/KeyBindingTests.cs
Tig d2ad11248f Fixes #3127 - View Layout improvements - Redefines how LayoutStyle works. Fixes AutoSize etc... (#3130)
* Removes CheckAbsoulte and updates unit tests to match

* Fixed code that was dependent on ToString behavior vs. direct test for null

* Dim/Pos != null WIP

* Moved AutoSize specific tests out of Pos/Dim tests

* Broke out AutoSize = false tests to new file

* Commented test TODOs

* New test

* Removed unused API and cleaned up code

* Removed unused API and cleaned up code

* Cleaned up code

* Cleaned up code

* reorg'd Toplevel tests

* Fixed Create and related unit tests

* Added test from #3136

* Removed TopLevel.Create

* Fixed SetCurrentOverlappedAsTop

* Updated pull request template

* Updated pull request template

* Revert "Updated pull request template"

This reverts commit d807190dd9.

* reverting

* re-reverting

* Fixed every thing but autosize scenarios??

* Fixed hexview

* Fixed contextmenu

* Fixed more minor issues in tests

* Fixed more minor issues in tests

* Debugging Dialog test failure

* Fixed bad Dialog test. Was cleary invalid

* Fixed OnResizeNeeded bug

* Fixed OnResizeNeeded bug

* Fixed UICatalog to not eat exceptions

* Fixed TextView

* Removed Frame overrides

* Made Frame non-virtual

* Fixed radioGroup

* Fixed TabView

* Hcked ScrolLBarView unit tests to pass

* All AutoSize tests pass!

* All tests pass!!!!!!!

* Updated API docs. Cleaned up code.

* Fixed ColorPicker

* Added 'Bounds =' unit tests

* Refactored TextFormatter.Size setting logic

* Cleaned up OnResizeNeeded (api docs and usages)

* Merges in #3019 changes. Makes OnResizeNeeded non-virtual. If we find a use-case where someone wants to override it we can change this back.

* Fixed FileDialog bounds warning

* Removed resharper settings from editorconfig

* Added Pos.Center test to AllViewsTests.cs.
Modernized RadioGroup.
Fixed ProgressBar.

* Reverted formatting

* Reverted formatting

* Reverted formatting

* Reverted formatting

* Reverted formatting

* Reverted formatting

* Reverted formatting

* Code cleanup

* Code cleanup

* Code cleanup

* Code cleanup

* Code cleanup

* Code cleanup

* Code cleanup

* Code cleanup

* Reverted formatting
2024-01-12 17:43:35 -07:00

288 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Abstractions;
namespace Terminal.Gui.InputTests;
public class KeyBindingTests {
readonly ITestOutputHelper _output;
public KeyBindingTests (ITestOutputHelper output)
{
this._output = output;
}
[Fact]
public void Defaults ()
{
var keyBindings = new KeyBindings ();
Assert.Throws<InvalidOperationException> (() => keyBindings.GetKeyFromCommands (Command.Accept));
}
[Fact]
public void Add_Single_Adds ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Default);
var resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Default, resultCommands);
keyBindings.Add (Key.B, Command.Default);
resultCommands = keyBindings.GetCommands (Key.B);
Assert.Contains (Command.Default, resultCommands);
}
[Fact]
public void Add_Multiple_Adds ()
{
var keyBindings = new KeyBindings ();
var commands = new Command [] {
Command.Right,
Command.Left
};
keyBindings.Add (Key.A, commands);
var 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_Empty_Throws ()
{
var keyBindings = new KeyBindings ();
var commands = new List<Command> ();
Assert.Throws<ArgumentException> (() => keyBindings.Add (Key.A, commands.ToArray ()));
}
// 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 ();
var commands = new Command [] {
Command.Right,
Command.Left
};
keyBindings.Add (Key.A, scope, commands);
var binding = keyBindings.Get (Key.A);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
binding = keyBindings.Get (Key.A, scope);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
var resultCommands = keyBindings.GetCommands (Key.A);
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 ();
var commands = new Command [] {
Command.Right,
Command.Left
};
keyBindings.Add (Key.A, scope, commands);
var binding = keyBindings.Get (Key.A);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
binding = keyBindings.Get (Key.A, scope);
Assert.Contains (Command.Right, binding.Commands);
Assert.Contains (Command.Left, binding.Commands);
// negative test
binding = keyBindings.Get (Key.A, (KeyBindingScope)(int)-1);
Assert.Null (binding);
var resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
// Clear
[Fact]
public void Clear_Clears ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Default);
keyBindings.Add (Key.B, Command.Default);
keyBindings.Clear ();
var resultCommands = keyBindings.GetCommands (Key.A);
Assert.Empty (resultCommands);
resultCommands = keyBindings.GetCommands (Key.B);
Assert.Empty (resultCommands);
}
// GetCommands
[Fact]
public void GetCommands_Unknown_ReturnsEmpty ()
{
var keyBindings = new KeyBindings ();
var resultCommands = keyBindings.GetCommands (Key.A);
Assert.Empty (resultCommands);
}
[Fact]
public void GetCommands_WithCommands_ReturnsCommands ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Default);
var resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Default, resultCommands);
}
[Fact]
public void GetCommands_WithMultipleCommands_ReturnsCommands ()
{
var keyBindings = new KeyBindings ();
var commands = new Command [] {
Command.Right,
Command.Left
};
keyBindings.Add (Key.A, commands);
var resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.Right, resultCommands);
Assert.Contains (Command.Left, resultCommands);
}
[Fact]
public void GetCommands_WithMultipleBindings_ReturnsCommands ()
{
var keyBindings = new KeyBindings ();
var commands = new Command [] {
Command.Right,
Command.Left
};
keyBindings.Add (Key.A, commands);
keyBindings.Add (Key.B, commands);
var 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);
}
// 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.Default);
var resultKey = keyBindings.GetKeyFromCommands (Command.Default);
Assert.Equal (Key.A, resultKey);
}
[Fact]
public void Replace_Key ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Default);
keyBindings.Add (Key.B, Command.Default);
keyBindings.Add (Key.C, Command.Default);
keyBindings.Add (Key.D, Command.Default);
keyBindings.Replace (Key.A, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.A));
Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
keyBindings.Replace (Key.B, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.B));
Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
keyBindings.Replace (Key.C, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.C));
Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
keyBindings.Replace (Key.D, Key.E);
Assert.Empty (keyBindings.GetCommands (Key.D));
Assert.Contains (Command.Default, keyBindings.GetCommands (Key.E));
}
// TryGet
[Fact]
public void TryGet_Unknown_ReturnsFalse ()
{
var keyBindings = new KeyBindings ();
var result = keyBindings.TryGet (Key.A, out var _);
Assert.False (result);
}
[Fact]
public void TryGet_WithCommands_ReturnsTrue ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Default);
var result = keyBindings.TryGet (Key.A, out var bindings);
Assert.True (result);
Assert.Contains (Command.Default, bindings.Commands);
}
[Fact]
public void GetKeyFromCommands_OneCommand ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, Command.Right);
var key = keyBindings.GetKeyFromCommands (Command.Right);
Assert.Equal (Key.A, key);
// Negative case
Assert.Throws<InvalidOperationException> (() => key = keyBindings.GetKeyFromCommands (Command.Left));
}
[Fact]
public void GetKeyFromCommands_MultipleCommands ()
{
var keyBindings = new KeyBindings ();
var commands1 = new Command [] {
Command.Right,
Command.Left
};
keyBindings.Add (Key.A, commands1);
var commands2 = new Command [] {
Command.LineUp,
Command.LineDown
};
keyBindings.Add (Key.B, commands2);
var 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));
}
}