Browse Source

Add net ansi sequences

tznind 4 tháng trước cách đây
mục cha
commit
a9a9b5bb70

+ 1 - 2
Terminal.Gui/ConsoleDrivers/V2/NetInputProcessor.cs

@@ -49,11 +49,10 @@ public class NetInputProcessor : InputProcessor<ConsoleKeyInfo>
     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 ()}),";
     }
 }

+ 92 - 28
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)
+                                                   }
+                                               });
+                break;
+            case V2TestDriver.V2Net:
 
-        _winInput.InputBuffer.Enqueue (
-                                       new ()
-                                       {
-                                           EventType = WindowsConsole.EventType.Mouse,
-                                           MouseEvent = new ()
-                                           {
-                                               ButtonState = WindowsConsole.ButtonState.NoButtonPressed,
-                                               MousePosition = new ((short)screenX, (short)screenY)
-                                           }
-                                       });
+                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 ();
     }
 
     /// <summary>

+ 53 - 0
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<ConsoleKeyInfo> 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);
+        }
+    }
+
+}

+ 1 - 1
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;

+ 1 - 1
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",