Fixes #3071 & #3079. Key cast and static props are not correct (#3089)

* Removed char->Key cast. Added Key(char)

* Re-added char->key cast. Added unit tests

* Fixed standard keys to always return new instead of being readonly

* Re-fixed WindowsDriver to report shift/alt/ctrl as key/down/up

* Re-fixed WindowsDriver to report shift/alt/ctrl as key/down/up

* Adds string constructor to Key + tests.

* Simplified Key json

* Added string/Key cast operators.
This commit is contained in:
Tig
2023-12-27 15:50:37 -07:00
committed by GitHub
parent a7209bcd88
commit 80ef4b5e19
13 changed files with 881 additions and 717 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -278,16 +278,16 @@ public class KeyCodeJsonConverterTests {
public class KeyJsonConverterTests {
[Theory]
[InlineData (KeyCode.A, "{\"Key\":\"a\"}")]
[InlineData ((KeyCode)'â', "{\"Key\":\"â\"}")]
[InlineData (KeyCode.A | KeyCode.ShiftMask, "{\"Key\":\"A\"}")]
[InlineData (KeyCode.A | KeyCode.CtrlMask, "{\"Key\":\"Ctrl+A\"}")]
[InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "{\"Key\":\"Ctrl+Alt+A\"}")]
[InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "{\"Key\":\"Ctrl+Alt+A\"}")]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "{\"Key\":\"A\"}")]
[InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "{\"Key\":\"Ctrl+Alt+Delete\"}")]
[InlineData (KeyCode.D4, "{\"Key\":\"4\"}")]
[InlineData (KeyCode.Esc, "{\"Key\":\"Esc\"}")]
[InlineData (KeyCode.A, "\"a\"")]
[InlineData ((KeyCode)'â', "\"â\"")]
[InlineData (KeyCode.A | KeyCode.ShiftMask, "\"A\"")]
[InlineData (KeyCode.A | KeyCode.CtrlMask, "\"Ctrl+A\"")]
[InlineData (KeyCode.A | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+A\"")]
[InlineData ((KeyCode)'a' | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+A\"")]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, "\"A\"")]
[InlineData (KeyCode.Delete | KeyCode.AltMask | KeyCode.CtrlMask, "\"Ctrl+Alt+Delete\"")]
[InlineData (KeyCode.D4, "\"4\"")]
[InlineData (KeyCode.Esc, "\"Esc\"")]
public void TestKey_Serialize (KeyCode key, string expected)
{
// Arrange

View File

@@ -7,74 +7,74 @@ using System.Text;
using System.Threading.Tasks;
using static Terminal.Gui.ConfigurationManager;
namespace Terminal.Gui.ConfigurationTests {
public class SettingsScopeTests {
namespace Terminal.Gui.ConfigurationTests;
[Fact]
public void GetHardCodedDefaults_ShouldSetProperties ()
{
ConfigurationManager.Reset ();
public class SettingsScopeTests {
[Fact]
public void GetHardCodedDefaults_ShouldSetProperties ()
{
Reset ();
Assert.Equal (3, ((Dictionary<string, ThemeScope>)ConfigurationManager.Settings ["Themes"].PropertyValue).Count);
Assert.Equal (3, ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue).Count);
ConfigurationManager.GetHardCodedDefaults ();
Assert.NotEmpty (ConfigurationManager.Themes);
Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
GetHardCodedDefaults ();
Assert.NotEmpty (Themes);
Assert.Equal ("Default", Themes.Theme);
Assert.True (ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue is Key);
Assert.True (ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue is Key);
Assert.True (ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue is Key);
Assert.True (ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue is bool);
Assert.True (Settings ["Application.QuitKey"].PropertyValue is Key);
Assert.True (Settings ["Application.AlternateForwardKey"].PropertyValue is Key);
Assert.True (Settings ["Application.AlternateBackwardKey"].PropertyValue is Key);
Assert.True (Settings ["Application.IsMouseDisabled"].PropertyValue is bool);
Assert.True (ConfigurationManager.Settings ["Theme"].PropertyValue is string);
Assert.Equal ("Default", ConfigurationManager.Settings ["Theme"].PropertyValue as string);
Assert.True (Settings ["Theme"].PropertyValue is string);
Assert.Equal ("Default", Settings ["Theme"].PropertyValue as string);
Assert.True (ConfigurationManager.Settings ["Themes"].PropertyValue is Dictionary<string, ThemeScope>);
Assert.Single (((Dictionary<string, ThemeScope>)ConfigurationManager.Settings ["Themes"].PropertyValue));
Assert.True (Settings ["Themes"].PropertyValue is Dictionary<string, ThemeScope>);
Assert.Single ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue);
}
}
[Fact, AutoInitShutdown]
public void Apply_ShouldApplyProperties ()
{
// arrange
Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, ((Key)ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, ((Key)ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, ((Key)ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode);
Assert.False ((bool)ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue);
[Fact] [AutoInitShutdown]
public void Apply_ShouldApplyProperties ()
{
// arrange
Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.PageDown | KeyCode.CtrlMask, ((Key)Settings ["Application.AlternateForwardKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.PageUp | KeyCode.CtrlMask, ((Key)Settings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode);
Assert.False ((bool)Settings ["Application.IsMouseDisabled"].PropertyValue);
// act
ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.Q);
ConfigurationManager.Settings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
ConfigurationManager.Settings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
ConfigurationManager.Settings ["Application.IsMouseDisabled"].PropertyValue = true;
// act
Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.Q);
Settings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
Settings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
Settings ["Application.IsMouseDisabled"].PropertyValue = true;
ConfigurationManager.Settings.Apply ();
Settings.Apply ();
// assert
Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
Assert.True (Application.IsMouseDisabled);
}
// assert
Assert.Equal (KeyCode.Q, Application.QuitKey.KeyCode);
Assert.Equal (KeyCode.F, Application.AlternateForwardKey.KeyCode);
Assert.Equal (KeyCode.B, Application.AlternateBackwardKey.KeyCode);
Assert.True (Application.IsMouseDisabled);
}
[Fact, AutoInitShutdown]
public void CopyUpdatedProperitesFrom_ShouldCopyChangedPropertiesOnly ()
{
ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.End); ;
[Fact] [AutoInitShutdown]
public void CopyUpdatedPropertiesFrom_ShouldCopyChangedPropertiesOnly ()
{
Settings ["Application.QuitKey"].PropertyValue = new Key (KeyCode.End);
;
var updatedSettings = new SettingsScope ();
var updatedSettings = new SettingsScope ();
///Don't set Quitkey
updatedSettings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
updatedSettings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
updatedSettings ["Application.IsMouseDisabled"].PropertyValue = true;
///Don't set Quitkey
updatedSettings ["Application.AlternateForwardKey"].PropertyValue = new Key (KeyCode.F);
updatedSettings ["Application.AlternateBackwardKey"].PropertyValue = new Key (KeyCode.B);
updatedSettings ["Application.IsMouseDisabled"].PropertyValue = true;
ConfigurationManager.Settings.Update (updatedSettings);
Assert.Equal (KeyCode.End, ((Key)ConfigurationManager.Settings ["Application.QuitKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.F, ((Key)updatedSettings ["Application.AlternateForwardKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.B, ((Key)updatedSettings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode);
Assert.True ((bool)updatedSettings ["Application.IsMouseDisabled"].PropertyValue);
}
Settings.Update (updatedSettings);
Assert.Equal (KeyCode.End, ((Key)Settings ["Application.QuitKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.F, ((Key)updatedSettings ["Application.AlternateForwardKey"].PropertyValue).KeyCode);
Assert.Equal (KeyCode.B, ((Key)updatedSettings ["Application.AlternateBackwardKey"].PropertyValue).KeyCode);
Assert.True ((bool)updatedSettings ["Application.IsMouseDisabled"].PropertyValue);
}
}

View File

@@ -27,6 +27,126 @@ public class KeyTests {
Assert.Equal (key, eventArgs.KeyCode);
}
[Theory]
[InlineData ('a', KeyCode.A)]
[InlineData ('A', KeyCode.A | KeyCode.ShiftMask)]
[InlineData ('z', KeyCode.Z)]
[InlineData ('Z', KeyCode.Z | KeyCode.ShiftMask)]
[InlineData (' ', KeyCode.Space)]
[InlineData ('1', KeyCode.D1)]
[InlineData ('!', (KeyCode)'!')]
[InlineData ('\n', KeyCode.Enter)]
[InlineData ('\t', KeyCode.Tab)]
[InlineData ('\r', (KeyCode)13)]
[InlineData ('ó', (KeyCode)'ó')]
[InlineData ('Ó', (KeyCode)'Ó')]
[InlineData ('❿', (KeyCode)'❿')]
[InlineData ('☑', (KeyCode)'☑')]
[InlineData ('英', (KeyCode)'英')]
[InlineData ('{', (KeyCode)'{')]
[InlineData ('\'', (KeyCode)'\'')]
[InlineData ('\xFFFF', (KeyCode)0xFFFF)]
[InlineData ('\x0', (KeyCode)0x0)]
public void Constructor_Char (char ch, KeyCode expectedKeyCode)
{
var key = new Key (ch);
Assert.Equal (expectedKeyCode, key.KeyCode);
}
// TryParse
[Theory]
[InlineData ("a", KeyCode.A)]
[InlineData ("Ctrl+A", KeyCode.A | KeyCode.CtrlMask)]
[InlineData ("Alt+A", KeyCode.A | KeyCode.AltMask)]
[InlineData ("Shift+A", KeyCode.A | KeyCode.ShiftMask)]
[InlineData ("A", KeyCode.A | KeyCode.ShiftMask)]
[InlineData ("â", (KeyCode)'â')]
[InlineData ("Shift+â", (KeyCode)'â' | KeyCode.ShiftMask)]
[InlineData ("Shift+Â", (KeyCode)'Â' | KeyCode.ShiftMask)]
[InlineData ("Ctrl+Shift+CursorUp", KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.CursorUp)]
[InlineData ("Ctrl+Alt+Shift+CursorUp", KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.CursorUp)]
[InlineData ("ctrl+alt+shift+cursorup", KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.CursorUp)]
[InlineData ("CTRL+ALT+SHIFT+CURSORUP", KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.CursorUp)]
[InlineData ("Ctrl+Alt+Shift+Delete", KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.Delete)]
[InlineData ("Ctrl+Alt+Shift+Enter", KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.Enter)]
[InlineData ("Tab", KeyCode.Tab)]
[InlineData ("Shift+Tab", KeyCode.Tab | KeyCode.ShiftMask)]
[InlineData ("Ctrl+Tab", KeyCode.Tab | KeyCode.CtrlMask)]
[InlineData ("Alt+Tab", KeyCode.Tab | KeyCode.AltMask)]
[InlineData ("Ctrl+Shift+Tab", KeyCode.Tab | KeyCode.ShiftMask | KeyCode.CtrlMask)]
[InlineData ("Ctrl+Alt+Tab", KeyCode.Tab | KeyCode.AltMask | KeyCode.CtrlMask)]
[InlineData ("", KeyCode.Null)]
[InlineData (" ", KeyCode.Space)]
[InlineData ("Shift+ ", KeyCode.Space | KeyCode.ShiftMask)]
[InlineData ("Ctrl+ ", KeyCode.Space | KeyCode.CtrlMask)]
[InlineData ("Alt+ ", KeyCode.Space | KeyCode.AltMask)]
[InlineData ("F1", KeyCode.F1)]
[InlineData ("0", KeyCode.D0)]
[InlineData ("9", KeyCode.D9)]
[InlineData ("D0", KeyCode.D0)]
[InlineData ("65", KeyCode.A | KeyCode.ShiftMask)]
[InlineData ("97", KeyCode.A)]
[InlineData ("Shift", KeyCode.ShiftKey)]
[InlineData ("Ctrl", KeyCode.CtrlKey)]
[InlineData ("Ctrl-A", KeyCode.A | KeyCode.CtrlMask)]
[InlineData ("Alt-A", KeyCode.A | KeyCode.AltMask)]
[InlineData ("A-Ctrl", KeyCode.A | KeyCode.CtrlMask)]
[InlineData ("Alt-A-Ctrl", KeyCode.A | KeyCode.CtrlMask | KeyCode.AltMask)]
public void Constructor_String_Valid (string keyString, Key expected)
{
Key key = new Key (keyString);
Assert.Equal (((Key)expected).ToString (), key.ToString ());
}
[Theory]
[InlineData("Barf")]
public void Constructor_String_Invalid_Throws (string keyString)
{
Assert.Throws<ArgumentException> (() => new Key (keyString));
}
[Theory]
[InlineData ('a', KeyCode.A)]
[InlineData ('A', KeyCode.A | KeyCode.ShiftMask)]
[InlineData ('z', KeyCode.Z)]
[InlineData ('Z', KeyCode.Z | KeyCode.ShiftMask)]
[InlineData (' ', KeyCode.Space)]
[InlineData ('1', KeyCode.D1)]
[InlineData ('!', (KeyCode)'!')]
[InlineData ('\n', KeyCode.Enter)]
[InlineData ('\t', KeyCode.Tab)]
[InlineData ('\r', (KeyCode)13)]
[InlineData ('ó', (KeyCode)'ó')]
[InlineData ('Ó', (KeyCode)'Ó')]
[InlineData ('❿', (KeyCode)'❿')]
[InlineData ('☑', (KeyCode)'☑')]
[InlineData ('英', (KeyCode)'英')]
[InlineData ('{', (KeyCode)'{')]
[InlineData ('\'', (KeyCode)'\'')]
[InlineData ('\xFFFF', (KeyCode)0xFFFF)]
[InlineData ('\x0', (KeyCode)0x0)]
public void Cast_Char_To_Key (char ch, KeyCode expectedKeyCode)
{
var key = (Key)ch;
Assert.Equal (expectedKeyCode, key.KeyCode);
}
// string cast operators
[Fact]
public void Cast_String_To_Key ()
{
var key = (Key)"Ctrl+Q";
Assert.Equal (KeyCode.Q | KeyCode.CtrlMask, key.KeyCode);
}
[Fact]
public void Cast_Key_ToString ()
{
var str = (string)Key.Q.WithCtrl;
Assert.Equal ("Ctrl+Q", str);
}
// IsValid
[Theory]
[InlineData (KeyCode.A, true)]
@@ -64,6 +184,28 @@ public class KeyTests {
Assert.Equal (expected.ToString (), key.ToString ());
}
[Fact]
public void Standard_Keys_Always_New ()
{
// Make two local keys, and grab Key.A, which is a reference to a singleton.
Key aKey1 = Key.A;
Key aKey2 = Key.A;
// Assert the starting state that is expected
Assert.False (aKey1.Handled);
Assert.False (aKey2.Handled);
Assert.False (Key.A.Handled);
// Now set Handled true on one of our local keys
aKey1.Handled = true;
// Assert the newly-expected case
// The last two assertions will fail, because we have actually modified a singleton
Assert.True (aKey1.Handled);
Assert.False (aKey2.Handled);
Assert.False (Key.A.Handled);
}
[Theory]
[InlineData ((KeyCode)'a', true)]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, true)]
@@ -96,7 +238,7 @@ public class KeyTests {
[InlineData ((KeyCode)'ç' | KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask, '\0')]
[InlineData ((KeyCode)'a', 97)] // 97 or Key.Space | Key.A
[InlineData ((KeyCode)'A', 97)] // 65 or equivalent to Key.A, but A-Z are mapped to lower case by drivers
//[InlineData (Key.A, 97)] // 65 equivalent to (Key)'A', but A-Z are mapped to lower case by drivers
//[InlineData (Key.A, 97)] // 65 equivalent to (Key)'A', but A-Z are mapped to lower case by drivers
[InlineData (KeyCode.ShiftMask | KeyCode.A, 65)]
[InlineData (KeyCode.CtrlMask | KeyCode.A, '\0')]
[InlineData (KeyCode.AltMask | KeyCode.A, '\0')]
@@ -263,14 +405,6 @@ public class KeyTests {
[InlineData (KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.AltMask | KeyCode.CtrlMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.Null, "Null")]
[InlineData (KeyCode.ShiftMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.CtrlMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.AltMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.AltMask | KeyCode.CtrlMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask | KeyCode.AltMask | KeyCode.Null, "Null")]
[InlineData (KeyCode.AltKey, "AltKey")]
[InlineData (KeyCode.CtrlKey, "CtrlKey")]
[InlineData (KeyCode.ShiftKey, "ShiftKey")]

View File

@@ -26,7 +26,7 @@ public class AppendAutocompleteTests {
tf.PositionCursor ();
TestHelpers.AssertDriverContentsAre ("", output);
tf.NewKeyDownEvent ('f');
tf.NewKeyDownEvent (new ('f'));
tf.Draw ();
tf.PositionCursor ();