Add automation test for selecting a submenu item

This commit is contained in:
tznind
2025-03-19 20:33:37 +00:00
committed by Tig
parent e94b8b1bdc
commit 29355c8b63
2 changed files with 156 additions and 72 deletions

View File

@@ -62,7 +62,8 @@ public class GuiTestContext : IDisposable
ApplicationImpl.ChangeInstance (v2);
var logger = LoggerFactory.Create (builder =>
builder.AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
builder.SetMinimumLevel (LogLevel.Trace)
.AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
.CreateLogger ("Test Logging");
Logging.Logger = logger;
@@ -254,52 +255,85 @@ public class GuiTestContext : IDisposable
public GuiTestContext Down ()
{
_winInput.InputBuffer.Enqueue (
new()
{
EventType = WindowsConsole.EventType.Key,
KeyEvent = new()
{
bKeyDown = true,
wRepeatCount = 0,
wVirtualKeyCode = ConsoleKeyMapping.VK.DOWN,
wVirtualScanCode = 0,
UnicodeChar = '\0',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed
}
});
_winInput.InputBuffer.Enqueue (
new()
{
EventType = WindowsConsole.EventType.Key,
KeyEvent = new()
{
bKeyDown = false,
wRepeatCount = 0,
wVirtualKeyCode = ConsoleKeyMapping.VK.DOWN,
wVirtualScanCode = 0,
UnicodeChar = '\0',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed
}
});
WaitIteration ();
SendWindowsKey (ConsoleKeyMapping.VK.DOWN);
return this;
}
public GuiTestContext Right ()
{
SendWindowsKey (ConsoleKeyMapping.VK.RIGHT);
return this;
}
public GuiTestContext Left ()
{
SendWindowsKey (ConsoleKeyMapping.VK.LEFT);
return this;
}
public GuiTestContext Up ()
{
SendWindowsKey (ConsoleKeyMapping.VK.UP);
return this;
}
public GuiTestContext Enter ()
{
SendWindowsKey (
new WindowsConsole.KeyEventRecord ()
{
UnicodeChar = '\r',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed,
wRepeatCount = 1,
wVirtualKeyCode = ConsoleKeyMapping.VK.RETURN,
wVirtualScanCode = 28
});
return this;
}
/// <summary>
/// Send a full windows OS key including both down and up.
/// </summary>
/// <param name="fullKey"></param>
private void SendWindowsKey (WindowsConsole.KeyEventRecord fullKey)
{
var down = fullKey;
var up = fullKey; // because struct this is new copy
down.bKeyDown = true;
up.bKeyDown = false;
_winInput.InputBuffer.Enqueue (
new()
new ()
{
EventType = WindowsConsole.EventType.Key,
KeyEvent = new()
KeyEvent = down
});
_winInput.InputBuffer.Enqueue (
new ()
{
EventType = WindowsConsole.EventType.Key,
KeyEvent = up
});
WaitIteration ();
}
/// <summary>
/// Sends a special key e.g. cursor key that does not map to a specific character
/// </summary>
/// <param name="specialKey"></param>
private void SendWindowsKey (ConsoleKeyMapping.VK specialKey)
{
_winInput.InputBuffer.Enqueue (
new ()
{
EventType = WindowsConsole.EventType.Key,
KeyEvent = new ()
{
bKeyDown = true,
wRepeatCount = 0,
wVirtualKeyCode = ConsoleKeyMapping.VK.RETURN,
wVirtualKeyCode = specialKey,
wVirtualScanCode = 0,
UnicodeChar = '\0',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed
@@ -307,14 +341,14 @@ public class GuiTestContext : IDisposable
});
_winInput.InputBuffer.Enqueue (
new()
new ()
{
EventType = WindowsConsole.EventType.Key,
KeyEvent = new()
KeyEvent = new ()
{
bKeyDown = false,
wRepeatCount = 0,
wVirtualKeyCode = ConsoleKeyMapping.VK.RETURN,
wVirtualKeyCode = specialKey,
wVirtualScanCode = 0,
UnicodeChar = '\0',
dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed
@@ -322,10 +356,9 @@ public class GuiTestContext : IDisposable
});
WaitIteration ();
return this;
}
public GuiTestContext WithContextMenu (ContextMenu ctx, MenuBarItem menuItems)
{
LastView.MouseEvent += (s, e) =>

View File

@@ -4,31 +4,28 @@ using TerminalGuiFluentTesting;
using Xunit.Abstractions;
namespace IntegrationTests.FluentTests;
public class BasicFluentAssertionTests
{
private readonly TextWriter _out;
public class TestOutputWriter : TextWriter
{
private readonly ITestOutputHelper _output;
public TestOutputWriter (ITestOutputHelper output)
{
_output = output;
}
public TestOutputWriter (ITestOutputHelper output) { _output = output; }
public override void WriteLine (string? value)
{
_output.WriteLine (value ?? string.Empty);
}
public override void WriteLine (string? value) { _output.WriteLine (value ?? string.Empty); }
public override Encoding Encoding => Encoding.UTF8;
}
public BasicFluentAssertionTests (ITestOutputHelper outputHelper) { _out = new TestOutputWriter(outputHelper); }
public BasicFluentAssertionTests (ITestOutputHelper outputHelper) { _out = new TestOutputWriter (outputHelper); }
[Fact]
public void GuiTestContext_StartsAndStopsWithoutError ()
{
using var context = With.A<Window> (40, 10);
using GuiTestContext context = With.A<Window> (40, 10);
// No actual assertions are needed — if no exceptions are thrown, it's working
context.Stop ();
@@ -37,22 +34,23 @@ public class BasicFluentAssertionTests
[Fact]
public void GuiTestContext_ForgotToStop ()
{
using var context = With.A<Window> (40, 10);
using GuiTestContext context = With.A<Window> (40, 10);
}
[Fact]
public void TestWindowsResize ()
{
var lbl = new Label ()
{
Width = Dim.Fill ()
};
using var c = With.A<Window> (40, 10)
.Add (lbl )
.Then (()=>Assert.Equal(lbl.Frame.Width,38)) // Window has 2 border
.ResizeConsole (20,20)
.Then (() => Assert.Equal(lbl.Frame.Width, 18))
.Stop ();
var lbl = new Label
{
Width = Dim.Fill ()
};
using GuiTestContext c = With.A<Window> (40, 10)
.Add (lbl)
.Then (() => Assert.Equal (lbl.Frame.Width, 38)) // Window has 2 border
.ResizeConsole (20, 20)
.Then (() => Assert.Equal (lbl.Frame.Width, 18))
.Stop ();
}
[Fact]
@@ -61,21 +59,74 @@ public class BasicFluentAssertionTests
var clicked = false;
var ctx = new ContextMenu ();
var menuItems = new MenuBarItem (
[
new ("_New File", string.Empty, () => { clicked = true; })
]
);
using var c = With.A<Window> (40, 10)
.WithContextMenu(ctx,menuItems)
.ScreenShot ("Before open menu", _out)
// Click in main area inside border
.RightClick(1,1)
.ScreenShot ("After open menu",_out)
.LeftClick (3, 3)
.Stop ()
.WriteOutLogs (_out);
using GuiTestContext c = With.A<Window> (40, 10)
.WithContextMenu (ctx, menuItems)
.ScreenShot ("Before open menu", _out)
// Click in main area inside border
.RightClick (1, 1)
.ScreenShot ("After open menu", _out)
.LeftClick (3, 3)
.Stop ()
.WriteOutLogs (_out);
Assert.True (clicked);
}
[Fact]
public void ContextMenu_OpenSubmenu ()
{
var clicked = false;
var ctx = new ContextMenu ();
var menuItems = new MenuBarItem (
[
new MenuItem ("One", "", null),
new MenuItem ("Two", "", null),
new MenuItem ("Three", "", null),
new MenuBarItem (
"Four",
[
new MenuItem ("SubMenu1", "", null),
new MenuItem ("SubMenu2", "", ()=>clicked=true),
new MenuItem ("SubMenu3", "", null),
new MenuItem ("SubMenu4", "", null),
new MenuItem ("SubMenu5", "", null),
new MenuItem ("SubMenu6", "", null),
new MenuItem ("SubMenu7", "", null)
]
),
new MenuItem ("Five", "", null),
new MenuItem ("Six", "", null)
]
);
using GuiTestContext c = With.A<Window> (40, 10)
.WithContextMenu (ctx, menuItems)
.ScreenShot ("Before open menu", _out)
// Click in main area inside border
.RightClick (1, 1)
.ScreenShot ("After open menu", _out)
.Down ()
.Down ()
.Down ()
.Right()
.ScreenShot ("After open submenu", _out)
.Down ()
.Enter ()
.ScreenShot ("Menu should be closed after selecting", _out)
.Stop ()
.WriteOutLogs (_out);
Assert.True (clicked);
}
}