Work on ability to test both v2net and v2win

This commit is contained in:
tznind
2025-03-19 20:54:13 +00:00
committed by Tig
parent 56d3835c37
commit 7712bc64b1
6 changed files with 104 additions and 20 deletions

View File

@@ -16,7 +16,7 @@ public class NetInputProcessor : InputProcessor<ConsoleKeyInfo>
/// to input stream.
/// </remarks>
/// </summary>
public static bool GenerateTestCasesForKeyPresses = false;
public static bool GenerateTestCasesForKeyPresses = true;
#pragma warning restore CA2211
/// <inheritdoc/>

View File

@@ -24,7 +24,7 @@
<Lollipop Position="0.2" />
</Class>
<Class Name="TerminalGuiFluentTesting.FakeWindowsInput" Collapsed="true">
<Position X="6" Y="2.75" Width="1.5" />
<Position X="6" Y="2.75" Width="1.75" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>FakeWindowsInput.cs</FileName>
@@ -39,13 +39,13 @@
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="TerminalGuiFluentTesting.GuiTestContext&lt;T&gt;" BaseTypeListCollapsed="true">
<Class Name="TerminalGuiFluentTesting.GuiTestContext" BaseTypeListCollapsed="true">
<Position X="2.25" Y="0.5" Width="2.25" />
<Compartments>
<Compartment Name="Fields" Collapsed="true" />
</Compartments>
<TypeIdentifier>
<HashCode>ABJAAAIAACBAAQAAgYAAAAAgABIEgAQAIAAIBACAIgA=</HashCode>
<HashCode>ABJAAAIAACBACQAAg4AAAAAgAJIEgAQAKACIBACAIgI=</HashCode>
<FileName>GuiTestContext.cs</FileName>
</TypeIdentifier>
<ShowAsAssociation>
@@ -55,5 +55,21 @@
</ShowAsAssociation>
<Lollipop Position="0.2" />
</Class>
<Class Name="TerminalGuiFluentTesting.TextWriterLoggerProvider" Collapsed="true">
<Position X="10" Y="2.75" Width="2" />
<TypeIdentifier>
<HashCode>AAAAAIAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
<FileName>TextWriterLoggerProvider.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Class Name="TerminalGuiFluentTesting.TextWriterLogger" Collapsed="true">
<Position X="10" Y="1.75" Width="1.5" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAEAAAAAAAgAAAAAAAAIAAAAAAA=</HashCode>
<FileName>TextWriterLogger.cs</FileName>
</TypeIdentifier>
<Lollipop Position="0.2" />
</Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>

View File

@@ -16,12 +16,14 @@ public class GuiTestContext : IDisposable
private readonly FakeNetInput _netInput;
private View? _lastView;
private readonly StringBuilder _logsSb;
private readonly V2TestDriver _driver;
internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height)
internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height, V2TestDriver driver)
{
IApplication origApp = ApplicationImpl.Instance;
ILogger? origLogger = Logging.Logger;
_logsSb = new ();
_driver = driver;
_netInput = new (_cts.Token);
_winInput = new (_cts.Token);
@@ -51,7 +53,7 @@ public class GuiTestContext : IDisposable
.CreateLogger ("Test Logging");
Logging.Logger = logger;
v2.Init (null, "v2win");
v2.Init (null, GetDriverName());
booting.Release ();
@@ -84,6 +86,17 @@ public class GuiTestContext : IDisposable
WaitIteration ();
}
private string GetDriverName ()
{
return _driver switch
{
V2TestDriver.V2Win => "v2win",
V2TestDriver.V2Net => "v2net",
_ =>
throw new ArgumentOutOfRangeException ()
};
}
/// <summary>
/// Stops the application and waits for the background thread to exit.
/// </summary>
@@ -212,6 +225,8 @@ 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
_winInput.InputBuffer.Enqueue (
new ()
{
@@ -241,11 +256,25 @@ public class GuiTestContext : IDisposable
public GuiTestContext Down ()
{
SendWindowsKey (ConsoleKeyMapping.VK.DOWN);
switch (_driver)
{
case V2TestDriver.V2Win:
SendWindowsKey (ConsoleKeyMapping.VK.DOWN);
break;
case V2TestDriver.V2Net:
// TODO: Support ansi sequence
throw new NotImplementedException ("Coming soon");
break;
default:
throw new ArgumentOutOfRangeException ();
}
return this;
}
public GuiTestContext Right ()
{
SendWindowsKey (ConsoleKeyMapping.VK.RIGHT);
@@ -269,15 +298,25 @@ public class GuiTestContext : IDisposable
public GuiTestContext Enter ()
{
SendWindowsKey (
new WindowsConsole.KeyEventRecord
{
UnicodeChar = '\r',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed,
wRepeatCount = 1,
wVirtualKeyCode = ConsoleKeyMapping.VK.RETURN,
wVirtualScanCode = 28
});
switch (_driver)
{
case V2TestDriver.V2Win:
SendWindowsKey (
new WindowsConsole.KeyEventRecord
{
UnicodeChar = '\r',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed,
wRepeatCount = 1,
wVirtualKeyCode = ConsoleKeyMapping.VK.RETURN,
wVirtualScanCode = 28
});
break;
case V2TestDriver.V2Net:
SendNetKey (new ('\r', ConsoleKey.Enter, false, false, false));
break;
default:
throw new ArgumentOutOfRangeException ();
}
return this;
}
@@ -311,6 +350,13 @@ public class GuiTestContext : IDisposable
WaitIteration ();
}
private void SendNetKey (ConsoleKeyInfo consoleKeyInfo)
{
_netInput.InputBuffer.Enqueue (consoleKeyInfo);
WaitIteration ();
}
/// <summary>
/// Sends a special key e.g. cursor key that does not map to a specific character
/// </summary>

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TerminalGuiFluentTesting;
/// <summary>
/// Which v2 driver should be used
/// </summary>
public enum V2TestDriver
{
V2Win,
V2Net
}

View File

@@ -12,8 +12,12 @@ public static class With
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="v2TestDriver">Which v2 v2TestDriver to use for the test</param>
/// <returns></returns>
public static GuiTestContext A<T> (int width, int height) where T : Toplevel, new () { return new (() => new T (), width, height); }
public static GuiTestContext A<T> (int width, int height, V2TestDriver v2TestDriver = V2TestDriver.V2Win) where T : Toplevel, new ()
{
return new (() => new T (), width, height,v2TestDriver);
}
/// <summary>
/// The global timeout to allow for any given application to run for before shutting down.

View File

@@ -79,8 +79,10 @@ public class BasicFluentAssertionTests
Assert.True (clicked);
}
[Fact]
public void ContextMenu_OpenSubmenu ()
[Theory]
[InlineData(V2TestDriver.V2Win)]
//[InlineData (V2TestDriver.V2Net)] // TODO
public void ContextMenu_OpenSubmenu (V2TestDriver v2TestDriver)
{
var clicked = false;
@@ -110,7 +112,7 @@ public class BasicFluentAssertionTests
]
);
using GuiTestContext c = With.A<Window> (40, 10)
using GuiTestContext c = With.A<Window> (40, 10,v2TestDriver)
.WithContextMenu (ctx, menuItems)
.ScreenShot ("Before open menu", _out)