using System;
using System.Collections;
using System.Collections.Generic;
using Xunit;
namespace Terminal.Gui.ConsoleDrivers;
public class ConsoleKeyMappingTests {
[Theory]
[InlineData ((KeyCode)'a' | KeyCode.ShiftMask, ConsoleKey.A, KeyCode.A, 'A')]
[InlineData ((KeyCode)'A', ConsoleKey.A, (KeyCode)'a', 'a')]
[InlineData ((KeyCode)'à' | KeyCode.ShiftMask, ConsoleKey.A, (KeyCode)'À', 'À')]
[InlineData ((KeyCode)'À', ConsoleKey.A, (KeyCode)'à', 'à')]
[InlineData ((KeyCode)'ü' | KeyCode.ShiftMask, ConsoleKey.U, (KeyCode)'Ü', 'Ü')]
[InlineData ((KeyCode)'Ü', ConsoleKey.U, (KeyCode)'ü', 'ü')]
[InlineData ((KeyCode)'ý' | KeyCode.ShiftMask, ConsoleKey.Y, (KeyCode)'Ý', 'Ý')]
[InlineData ((KeyCode)'Ý', ConsoleKey.Y, (KeyCode)'ý', 'ý')]
[InlineData ((KeyCode)'!' | KeyCode.ShiftMask, ConsoleKey.D1, (KeyCode)'!', '!')]
[InlineData (KeyCode.D1, ConsoleKey.D1, KeyCode.D1, '1')]
[InlineData ((KeyCode)'/' | KeyCode.ShiftMask, ConsoleKey.D7, (KeyCode)'/', '/')]
[InlineData (KeyCode.D7, ConsoleKey.D7, KeyCode.D7, '7')]
[InlineData (KeyCode.PageDown | KeyCode.ShiftMask, ConsoleKey.PageDown, KeyCode.Null, '\0')]
[InlineData (KeyCode.PageDown, ConsoleKey.PageDown, KeyCode.Null, '\0')]
public void TestIfEqual (KeyCode key, ConsoleKey expectedConsoleKey, KeyCode expectedKey, char expectedChar)
{
var consoleKeyInfo = ConsoleKeyMapping.GetConsoleKeyFromKey (key);
Assert.Equal (consoleKeyInfo.Key, expectedConsoleKey);
Assert.Equal ((char)expectedKey, expectedChar);
Assert.Equal (consoleKeyInfo.KeyChar, expectedChar);
}
static object packetLock = new object ();
///
/// Sometimes when using remote tools EventKeyRecord sends 'virtual keystrokes'.
/// These are indicated with the wVirtualKeyCode of 231. When we see this code
/// then we need to look to the unicode character (UnicodeChar) instead of the key
/// when telling the rest of the framework what button was pressed. For full details
/// see: https://github.com/gui-cs/Terminal.Gui/issues/2008
///
[Theory]
[AutoInitShutdown]
[ClassData (typeof (PacketTest))]
public void TestVKPacket (uint unicodeCharacter, bool shift, bool alt, bool control, uint initialVirtualKey,
uint initialScanCode, KeyCode expectedRemapping, uint expectedVirtualKey, uint expectedScanCode)
{
lock (packetLock) {
Application._forceFakeConsole = true;
Application.Init ();
var modifiers = new ConsoleModifiers ();
if (shift) {
modifiers |= ConsoleModifiers.Shift;
}
if (alt) {
modifiers |= ConsoleModifiers.Alt;
}
if (control) {
modifiers |= ConsoleModifiers.Control;
}
ConsoleKeyInfo consoleKeyInfo = ConsoleKeyMapping.GetConsoleKeyFromKey (unicodeCharacter, modifiers, out uint scanCode);
Assert.Equal ((uint)consoleKeyInfo.Key, initialVirtualKey);
if (scanCode > 0 && consoleKeyInfo.KeyChar == 0) {
Assert.Equal (0, (double)consoleKeyInfo.KeyChar);
} else {
Assert.Equal (consoleKeyInfo.KeyChar, unicodeCharacter);
}
Assert.Equal ((uint)consoleKeyInfo.Key, expectedVirtualKey);
Assert.Equal (scanCode, initialScanCode);
Assert.Equal (scanCode, expectedScanCode);
var top = Application.Top;
top.KeyDown += (s, e) => {
Assert.Equal (Key.ToString (expectedRemapping), Key.ToString (e.KeyCode));
e.Handled = true;
Application.RequestStop ();
};
int iterations = -1;
Application.Iteration += (s, a) => {
iterations++;
if (iterations == 0) {
Application.Driver.SendKeys (consoleKeyInfo.KeyChar, ConsoleKey.Packet, shift, alt, control);
}
};
Application.Run ();
Application.Shutdown ();
}
}
public class PacketTest : IEnumerable, IEnumerable