From a9a9b5bb70e1fd9ac40dabd80a4dc7976ed94829 Mon Sep 17 00:00:00 2001 From: tznind Date: Thu, 20 Mar 2025 08:13:48 +0000 Subject: [PATCH] Add net ansi sequences --- .../ConsoleDrivers/V2/NetInputProcessor.cs | 3 +- TerminalGuiFluentTesting/GuiTestContext.cs | 120 ++++++++++++++---- TerminalGuiFluentTesting/NetSequences.cs | 53 ++++++++ .../FluentTests/BasicFluentAssertionTests.cs | 2 +- UICatalog/Properties/launchSettings.json | 2 +- 5 files changed, 148 insertions(+), 32 deletions(-) create mode 100644 TerminalGuiFluentTesting/NetSequences.cs diff --git a/Terminal.Gui/ConsoleDrivers/V2/NetInputProcessor.cs b/Terminal.Gui/ConsoleDrivers/V2/NetInputProcessor.cs index 0cc188322..6bc1b739b 100644 --- a/Terminal.Gui/ConsoleDrivers/V2/NetInputProcessor.cs +++ b/Terminal.Gui/ConsoleDrivers/V2/NetInputProcessor.cs @@ -49,11 +49,10 @@ public class NetInputProcessor : InputProcessor private static string FormatConsoleKeyInfoForTestCase (ConsoleKeyInfo input) { string charLiteral = input.KeyChar == '\0' ? @"'\0'" : $"'{input.KeyChar}'"; - var expectedLiteral = "new Rune('todo')"; return $"new ConsoleKeyInfo({charLiteral}, ConsoleKey.{input.Key}, " + $"{input.Modifiers.HasFlag (ConsoleModifiers.Shift).ToString ().ToLower ()}, " + $"{input.Modifiers.HasFlag (ConsoleModifiers.Alt).ToString ().ToLower ()}, " - + $"{input.Modifiers.HasFlag (ConsoleModifiers.Control).ToString ().ToLower ()}), {expectedLiteral}"; + + $"{input.Modifiers.HasFlag (ConsoleModifiers.Control).ToString ().ToLower ()}),"; } } diff --git a/TerminalGuiFluentTesting/GuiTestContext.cs b/TerminalGuiFluentTesting/GuiTestContext.cs index 8678df507..5b4c697d9 100644 --- a/TerminalGuiFluentTesting/GuiTestContext.cs +++ b/TerminalGuiFluentTesting/GuiTestContext.cs @@ -225,29 +225,50 @@ public class GuiTestContext : IDisposable private GuiTestContext Click (WindowsConsole.ButtonState btn, int screenX, int screenY) { - // TODO: Support net style ansi escape sequence generation for arrow keys + switch (_driver) + { + case V2TestDriver.V2Win: - _winInput.InputBuffer.Enqueue ( - new () - { - EventType = WindowsConsole.EventType.Mouse, - MouseEvent = new () - { - ButtonState = btn, - MousePosition = new ((short)screenX, (short)screenY) - } - }); + _winInput.InputBuffer.Enqueue ( + new () + { + EventType = WindowsConsole.EventType.Mouse, + MouseEvent = new () + { + ButtonState = btn, + MousePosition = new ((short)screenX, (short)screenY) + } + }); - _winInput.InputBuffer.Enqueue ( - new () - { - EventType = WindowsConsole.EventType.Mouse, - MouseEvent = new () - { - ButtonState = WindowsConsole.ButtonState.NoButtonPressed, - MousePosition = new ((short)screenX, (short)screenY) - } - }); + _winInput.InputBuffer.Enqueue ( + new () + { + EventType = WindowsConsole.EventType.Mouse, + MouseEvent = new () + { + ButtonState = WindowsConsole.ButtonState.NoButtonPressed, + MousePosition = new ((short)screenX, (short)screenY) + } + }); + break; + case V2TestDriver.V2Net: + + int netButton = btn switch + { + WindowsConsole.ButtonState.Button1Pressed => 0, + WindowsConsole.ButtonState.Button2Pressed => 1, + WindowsConsole.ButtonState.Button3Pressed => 2, + WindowsConsole.ButtonState.RightmostButtonPressed => 2, + _ => throw new ArgumentOutOfRangeException(nameof(btn)) + }; + foreach (var k in NetSequences.Click(netButton,screenX,screenY)) + { + SendNetKey (k); + } + break; + default: + throw new ArgumentOutOfRangeException (); + } WaitIteration (); @@ -260,11 +281,13 @@ public class GuiTestContext : IDisposable { case V2TestDriver.V2Win: SendWindowsKey (ConsoleKeyMapping.VK.DOWN); + WaitIteration (); break; case V2TestDriver.V2Net: - // TODO: Support ansi sequence - - throw new NotImplementedException ("Coming soon"); + foreach (var k in NetSequences.Down) + { + SendNetKey (k); + } break; default: throw new ArgumentOutOfRangeException (); @@ -277,21 +300,63 @@ public class GuiTestContext : IDisposable public GuiTestContext Right () { - SendWindowsKey (ConsoleKeyMapping.VK.RIGHT); + switch (_driver) + { + case V2TestDriver.V2Win: + SendWindowsKey (ConsoleKeyMapping.VK.RIGHT); + WaitIteration (); + break; + case V2TestDriver.V2Net: + foreach (var k in NetSequences.Right) + { + SendNetKey (k); + } + break; + default: + throw new ArgumentOutOfRangeException (); + } return this; } public GuiTestContext Left () { - SendWindowsKey (ConsoleKeyMapping.VK.LEFT); + switch (_driver) + { + case V2TestDriver.V2Win: + SendWindowsKey (ConsoleKeyMapping.VK.LEFT); + WaitIteration (); + break; + case V2TestDriver.V2Net: + foreach (var k in NetSequences.Left) + { + SendNetKey (k); + } + break; + default: + throw new ArgumentOutOfRangeException (); + } return this; } public GuiTestContext Up () { - SendWindowsKey (ConsoleKeyMapping.VK.UP); + switch (_driver) + { + case V2TestDriver.V2Win: + SendWindowsKey (ConsoleKeyMapping.VK.UP); + WaitIteration (); + break; + case V2TestDriver.V2Net: + foreach (var k in NetSequences.Up) + { + SendNetKey (k); + } + break; + default: + throw new ArgumentOutOfRangeException (); + } return this; } @@ -354,7 +419,6 @@ public class GuiTestContext : IDisposable private void SendNetKey (ConsoleKeyInfo consoleKeyInfo) { _netInput.InputBuffer.Enqueue (consoleKeyInfo); - WaitIteration (); } /// diff --git a/TerminalGuiFluentTesting/NetSequences.cs b/TerminalGuiFluentTesting/NetSequences.cs new file mode 100644 index 000000000..10256b126 --- /dev/null +++ b/TerminalGuiFluentTesting/NetSequences.cs @@ -0,0 +1,53 @@ +namespace TerminalGuiFluentTesting; +class NetSequences +{ + public static ConsoleKeyInfo [] Down = new [] + { + new ConsoleKeyInfo('\x1B', ConsoleKey.Enter, false, false, false), + new ConsoleKeyInfo('[', ConsoleKey.None, false, false, false), + new ConsoleKeyInfo('B', ConsoleKey.None, false, false, false), + }; + + public static ConsoleKeyInfo [] Up = new [] + { + new ConsoleKeyInfo('\x1B', ConsoleKey.Enter, false, false, false), + new ConsoleKeyInfo('[', ConsoleKey.None, false, false, false), + new ConsoleKeyInfo('A', ConsoleKey.None, false, false, false), + }; + + public static ConsoleKeyInfo [] Left = new [] + { + new ConsoleKeyInfo('\x1B', ConsoleKey.Enter, false, false, false), + new ConsoleKeyInfo('[', ConsoleKey.None, false, false, false), + new ConsoleKeyInfo('D', ConsoleKey.None, false, false, false), + }; + + public static ConsoleKeyInfo [] Right = new [] + { + new ConsoleKeyInfo('\x1B', ConsoleKey.Enter, false, false, false), + new ConsoleKeyInfo('[', ConsoleKey.None, false, false, false), + new ConsoleKeyInfo('C', ConsoleKey.None, false, false, false), + }; + + public static IEnumerable Click (int button, int screenX, int screenY) + { + // Adjust for 1-based coordinates + int adjustedX = screenX + 1; + int adjustedY = screenY + 1; + + // Mouse press sequence + var sequence = $"\x1B[<{button};{adjustedX};{adjustedY}M"; + foreach (char c in sequence) + { + yield return new ConsoleKeyInfo (c, ConsoleKey.None, false, false, false); + } + + // Mouse release sequence + sequence = $"\x1B[<{button};{adjustedX};{adjustedY}m"; + foreach (char c in sequence) + { + yield return new ConsoleKeyInfo (c, ConsoleKey.None, false, false, false); + } + } + +} diff --git a/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs b/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs index 3c2f31886..95af2afba 100644 --- a/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs +++ b/Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs @@ -81,7 +81,7 @@ public class BasicFluentAssertionTests [Theory] [InlineData(V2TestDriver.V2Win)] - //[InlineData (V2TestDriver.V2Net)] // TODO + [InlineData (V2TestDriver.V2Net)] public void ContextMenu_OpenSubmenu (V2TestDriver v2TestDriver) { var clicked = false; diff --git a/UICatalog/Properties/launchSettings.json b/UICatalog/Properties/launchSettings.json index 793cef998..858308a30 100644 --- a/UICatalog/Properties/launchSettings.json +++ b/UICatalog/Properties/launchSettings.json @@ -17,7 +17,7 @@ }, "UICatalog --driver v2net": { "commandName": "Project", - "commandLineArgs": "--driver v2net" + "commandLineArgs": "--driver v2net -dl Trace" }, "WSL: UICatalog": { "commandName": "Executable",