Fixes #3947 Adds Fake driver and fixes fluent tests (iteration-zero) (#4225)

* Consider width2 chars that are not IsBmp

* Apply same fix in WindowsDriver

* Explicitly use type of local variable

* Revert changes to WindowsDriver

* Assume we are running in a terminal that supports true color by default unless user explicitly forces 16

* Switch to SetAttribute and WriteConsole instead of WriteConsoleOutput for 16 color mode

* Fix some cursor issues (WIP)

* Remove concept of 'dirty rows' from v2 as its never actually used

* Remove damageRegion as it does nothing

* Make string builder to console writing simpler

* Radically simplify Write method

* Simplify conditional logic

* Simplify restoring cursor position

* Reference local variable for console buffer

* Reduce calls to ConsoleWrite by accumulating till attribute changes

* When resizing v2 16 color mode on windows, recreate the back buffer to match its size

* Fixes for VTS enabled

* Fix _lastSize never being assigned

* Fixes VTS for Force16Colors

* Fixes force16Colors in VTS

* Fixes escape sequences always echoing in non-VTS

* Force Force16Colors in non-VTS. It have a bug in adding a newline in the last line

* WIP Add base class for NetOutput

* Abstract away how we change attribute

* WIP - Make WindowsOutput use base class

* WIP working to fix set cursor position

* Remove commented out code

* Fixes legacy output mode

* Fixes size with no alt buffer supported on VTS and size restore after maximized.

* Fix set cursor which also fixes the broken surrogate pairs

* Add force parameter

* Fixes an issue that only happens with Windows Terminal when paste surrogate pairs by press Ctrl+V

* In Windows escape sequences must be sent during the lifetime of the console which is created in input handle

* Ensure flush the input buffer before reset the console

* Flush input buffer before reset console in v2win

* Fixes issue in v2net not being refreshing the menu bar at start

* Only force layout and draw on size changed.

* Fix v2net issue not draw first line by forcing set cursor position

* Set _lastCursorPosition nullable and remove bool force from set cursor position

* Remove force parameter

* Add v2 version of fake driver attribute

* Make direct replacement and wire up window resizing events

* Update casts to use V2 fake driver instead

* Adjust interfaces to expose less internals

* Fix not raising iteration event in v2

* WIP investigate what it takes to do resize and redraw using TextAlignment_Centered as example

* Sketch adding component factory

* Create relevant fake component factories

* Add window size monitor into factory

* Fake size monitor injecting

* Add helper for faking console resize in AutoInitShutdown tests

* Fix size setting in FakeDriverV2

* Switch to new method

* Fix IsLegacy becoming false when using blank constructor

* Fix for Ready not being raised when showing same top twice also fixes garbage collection issue if running millions of top levels

* Fix tests

* Remove auto init

* Restore conditional compilation stuff

* Restore 'if running unit tests' logic

* Check only for the output being specific classes for the suppression

* Fix ShadowView blowing up with index out of bounds error

* Fix resize in fluent tests

* Fix for people using Iteration call directly

* Fix more calls to iteration to use
        AutoInitShutdownAttribute.RunIteration ();

* Add comment

* Remove assumption that Run with prior view not disposed should throw

* Fix timings in Dialog_Opened_From_Another_Dialog

* Fix Zero_Buttons_Works

* Standardize and fix Button_IsDefault_True_Return_His_Index_On_Accepting

* Fix iteration counts on MessageBoxTests

* Fix WizartTests and DrawTests_Ruler

* Implement SendKeys into ConsoleDriverFacade

* Fix SendKeys in console driver facade such that FileDialogTests works
Fix when Clip is null in popover

* Add missing dispose call to test

* Fix support for Esc in facade SendKeys

* Fix AutocompleteTests

* Fix various tests

* Replace LayoutAndDraw with run iteration

* Fix draw issues

* fix draw order

* Fix run iteration calls

* Fix unit tests

* Fix SendKeys in facade.

* Manipulate upper and lower cases.

* Add IsValidInput method to the interface.

* Fix SendKeys scenario

* Fixes surrogate pairs in the label

* Make tests more sensible - they are testing draw functionality.  Callbacks do not need to happen in Iteration method

* Fix tests and harden cleanup in AutoInitShutdownAttribute v2 lifecycle dispose

* Delete extra create input call

* Fix mocks and order of exceptions thrown in Run when things are not initialized

* Revert use of `MapConsoleKeyInfoToKeyCode`

* Ignore casing as it is not what test is really about

* Clear application top and top levels before each auto init shutdown test

* Fix for unstable tests

* Restore actually working SendKeys code

* option to pass logger in fluent ctor

* restore ToArray

* Fix SendKeys method and add extension to unit test

* Leverage the EscSeqUtils.MapConsoleKeyInfo method to avoid duplicate code

* Remove unnecessary hack

* Using only KeyCode for rKeys

* Recover modifier keys in surrogate pairs

* Reformat

* Remove iteration limit for benchmarking in v2

* remove iteration delay to identify bugs

* Remove nudge to unique key and make Then run on UI thread

* fix fluid assertions

* Ensure UI operations all happen on UI thread

* Add explicit error for WaitIteration during an invoke

* Remove timeout added for debug

* Catch failing asserts better

* Fix screenshot

* Fix null ref

* Fix race condition in processing input

* Test fixing

* Standardize asserts

* Remove calls to layout and draw, remove pointless lock and enable reading Cancelled from Dialog even if it is disposed

* fix bad merge

* Make logs access threadsafe

* add extra wait to remove race between iteration end and assert

* Code cleanup

* Remove test for crash on access Cancelled after dispose as this is no longer a restriction

* Change resize console to run on UI thread - fixing race condition with redrawing

* Restore original frame rate after test

* Restore nudge to unique key

* Code Cleanup

* Fix for cascading failures when an assert fails in a specific test

* fix for bad merge

* Address PR feedback

* Move classes to seperate files and add xmldoc

* xml doc warnings

* More xml comments docs

* Fix spelling

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
This commit is contained in:
Thomas Nind
2025-09-10 17:01:57 +01:00
committed by GitHub
parent 00aaefb962
commit 51dda7e69f
85 changed files with 1783 additions and 1130 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Reflection;
using UnitTests;
using Xunit.Abstractions;
using static Terminal.Gui.Configuration.ConfigurationManager;
@@ -40,7 +41,9 @@ public class ApplicationTests
Application.InitializedChanged += OnApplicationOnInitializedChanged;
Application.Init (new FakeDriver ());
var a = new AutoInitShutdownAttribute ();
a.Before (null);
Assert.True (initialized);
Assert.False (shutdown);
@@ -76,6 +79,8 @@ public class ApplicationTests
_timeoutLock = null;
}
a.After (null);
return;
void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
@@ -143,15 +148,13 @@ public class ApplicationTests
}
[Fact]
[AutoInitShutdown]
public void Begin_Null_Toplevel_Throws ()
{
// Setup Mock driver
Init ();
// Test null Toplevel
Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -163,21 +166,21 @@ public class ApplicationTests
public void Begin_Sets_Application_Top_To_Console_Size ()
{
Assert.Null (Application.Top);
AutoInitShutdownAttribute.FakeResize (new Size (80,25));
Toplevel top = new ();
Application.Begin (top);
Assert.Equal (new (0, 0, 80, 25), Application.Top!.Frame);
((FakeDriver)Application.Driver!).SetBufferSize (5, 5);
AutoInitShutdownAttribute.FakeResize(new Size(5, 5));
Assert.Equal (new (0, 0, 5, 5), Application.Top!.Frame);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void End_And_Shutdown_Should_Not_Dispose_ApplicationTop ()
{
Assert.Null (Application.Top);
Init ();
RunState rs = Application.Begin (new ());
Application.Top!.Title = "End_And_Shutdown_Should_Not_Dispose_ApplicationTop";
Assert.Equal (rs.Toplevel, Application.Top);
@@ -193,25 +196,24 @@ public class ApplicationTests
Toplevel top = Application.Top;
#if DEBUG_IDISPOSABLE
Exception exception = Record.Exception (() => Shutdown ());
Exception exception = Record.Exception (Application.Shutdown);
Assert.NotNull (exception);
Assert.False (top.WasDisposed);
top.Dispose ();
Assert.True (top.WasDisposed);
#endif
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
}
[Fact]
[AutoInitShutdown]
public void Init_Begin_End_Cleans_Up ()
{
// Start stopwatch
Stopwatch stopwatch = new Stopwatch ();
stopwatch.Start ();
Init ();
// Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
// if we don't stop
Application.Iteration += (s, a) => { Application.RequestStop (); };
@@ -237,14 +239,12 @@ public class ApplicationTests
Application.End (runstate);
Assert.NotNull (Application.Top);
Assert.NotNull (Application.MainLoop);
Assert.NotNull (Application.Driver);
topLevel.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
Assert.Null (Application.Driver);
// Stop stopwatch
@@ -255,7 +255,6 @@ public class ApplicationTests
}
[Theory]
[InlineData (typeof (FakeDriver))]
[InlineData (typeof (NetDriver))]
//[InlineData (typeof (ANSIDriver))]
@@ -268,7 +267,7 @@ public class ApplicationTests
Assert.NotNull (Application.Driver);
Assert.NotEqual (driver, Application.Driver);
Assert.Equal (driverType, Application.Driver?.GetType ());
Shutdown ();
Application.Shutdown ();
}
[Fact]
@@ -278,7 +277,7 @@ public class ApplicationTests
Assert.NotNull (Application.Driver);
Shutdown ();
Application.Shutdown ();
}
[Theory]
@@ -468,7 +467,7 @@ public class ApplicationTests
new FakeDriver ()
)
);
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -478,7 +477,7 @@ public class ApplicationTests
Application.InternalInit (new FakeDriver ());
Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver ()));
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -521,7 +520,7 @@ public class ApplicationTests
Assert.NotNull (Application.Driver);
topLevel.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -645,34 +644,13 @@ public class ApplicationTests
Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
}
private void Init ()
{
Application.Init (new FakeDriver ());
Assert.NotNull (Application.Driver);
Assert.NotNull (Application.MainLoop);
Assert.NotNull (SynchronizationContext.Current);
}
private void Shutdown ()
{
if (ApplicationImpl.Instance is ApplicationV2)
{
ApplicationImpl.Instance.Shutdown ();
}
else
{
Application.Shutdown ();
}
}
#region RunTests
[Fact]
[AutoInitShutdown]
public void Run_T_After_InitWithDriver_with_TopLevel_Does_Not_Throws ()
{
// Setup Mock driver
Init ();
Application.Iteration += (s, e) => Application.RequestStop ();
// Run<Toplevel> when already initialized or not with a Driver will not throw (because Window is derived from Toplevel)
@@ -681,7 +659,7 @@ public class ApplicationTests
Assert.True (Application.Top is Window);
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -691,9 +669,6 @@ public class ApplicationTests
[Fact]
public void Run_T_After_InitWithDriver_with_TopLevel_and_Driver_Does_Not_Throws ()
{
// Setup Mock driver
Init ();
Application.Iteration += (s, e) => Application.RequestStop ();
// Run<Toplevel> when already initialized or not with a Driver will not throw (because Window is derived from Toplevel)
@@ -708,7 +683,7 @@ public class ApplicationTests
Assert.True (Application.Top is Dialog);
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -716,10 +691,10 @@ public class ApplicationTests
}
[Fact]
[AutoInitShutdown]
[TestRespondersDisposed]
public void Run_T_After_Init_Does_Not_Disposes_Application_Top ()
{
Init ();
// Init doesn't create a Toplevel and assigned it to Application.Top
// but Begin does
@@ -742,7 +717,7 @@ public class ApplicationTests
Assert.True (initTop.WasDisposed);
#endif
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -751,18 +726,16 @@ public class ApplicationTests
[Fact]
[TestRespondersDisposed]
[AutoInitShutdown]
public void Run_T_After_InitWithDriver_with_TestTopLevel_DoesNotThrow ()
{
// Setup Mock driver
Init ();
Application.Iteration += (s, a) => { Application.RequestStop (); };
// Init has been called and we're passing no driver to Run<TestTopLevel>. This is ok.
Application.Run<Toplevel> ();
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -775,8 +748,8 @@ public class ApplicationTests
{
Application.ForceDriver = "FakeDriver";
Application.Init ();
Assert.Equal (typeof (FakeDriver), Application.Driver?.GetType ());
var a = new AutoInitShutdownAttribute ();
a.Before (null);
Application.Iteration += (s, a) => { Application.RequestStop (); };
@@ -784,25 +757,26 @@ public class ApplicationTests
Application.Run<Toplevel> ();
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
Assert.Null (Application.Driver);
a.After (null);
}
[Fact]
[TestRespondersDisposed]
[AutoInitShutdown]
public void Run_T_Init_Driver_Cleared_with_TestTopLevel_Throws ()
{
Init ();
Application.Driver = null;
// Init has been called, but Driver has been set to null. Bad.
Assert.Throws<InvalidOperationException> (() => Application.Run<Toplevel> ());
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -821,7 +795,7 @@ public class ApplicationTests
Assert.Equal (typeof (FakeDriver), Application.Driver?.GetType ());
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -838,7 +812,7 @@ public class ApplicationTests
Application.Run<Toplevel> (null, new FakeDriver ());
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.Null (Application.Top);
Assert.Null (Application.MainLoop);
@@ -847,10 +821,11 @@ public class ApplicationTests
[Fact]
[TestRespondersDisposed]
[AutoInitShutdown]
public void Run_RequestStop_Stops ()
{
// Setup Mock driver
Init ();
Application.Init ();
var top = new Toplevel ();
RunState rs = Application.Begin (top);
@@ -868,11 +843,9 @@ public class ApplicationTests
}
[Fact]
[AutoInitShutdown]
public void Run_Sets_Running_True ()
{
// Setup Mock driver
Init ();
var top = new Toplevel ();
RunState rs = Application.Begin (top);
Assert.NotNull (rs);
@@ -894,11 +867,9 @@ public class ApplicationTests
[Fact]
[TestRespondersDisposed]
[AutoInitShutdown]
public void Run_RunningFalse_Stops ()
{
// Setup Mock driver
Init ();
var top = new Toplevel ();
RunState rs = Application.Begin (top);
Assert.NotNull (rs);
@@ -915,10 +886,10 @@ public class ApplicationTests
}
[Fact]
[AutoInitShutdown]
[TestRespondersDisposed]
public void Run_Loaded_Ready_Unloaded_Events ()
{
Init ();
Toplevel top = new ();
var count = 0;
top.Loaded += (s, e) => count++;
@@ -933,17 +904,16 @@ public class ApplicationTests
// TODO: All Toplevel layout tests should be moved to ToplevelTests.cs
[Fact]
[AutoInitShutdown]
public void Run_A_Modal_Toplevel_Refresh_Background_On_Moving ()
{
Init ();
// Don't use Dialog here as it has more layout logic. Use Window instead.
var w = new Window
{
Width = 5, Height = 5,
Arrangement = ViewArrangement.Movable
};
((FakeDriver)Application.Driver!).SetBufferSize (10, 10);
AutoInitShutdownAttribute.FakeResize(new Size(10, 10));
RunState rs = Application.Begin (w);
// Don't use visuals to test as style of border can change over time.
@@ -963,10 +933,9 @@ public class ApplicationTests
}
[Fact]
[AutoInitShutdown]
public void End_Does_Not_Dispose ()
{
Init ();
var top = new Toplevel ();
Window w = new ();
@@ -1117,8 +1086,12 @@ public class ApplicationTests
private readonly object _forceDriverLock = new ();
[Theory]
[InlineData ("v2win", typeof (ConsoleDriverFacade<WindowsConsole.InputRecord>))]
[InlineData ("v2net", typeof (ConsoleDriverFacade<ConsoleKeyInfo>))]
// This test wants to Run which results in console handle errors, it wants to rely non drivers checking ConsoleDriver.RunningUnitTests
// And suppressing things that might fail, this is anti pattern, instead we should test this kind of thing with Mocking
// [InlineData ("v2win", typeof (ConsoleDriverFacade<WindowsConsole.InputRecord>))]
// [InlineData ("v2net", typeof (ConsoleDriverFacade<ConsoleKeyInfo>))]
[InlineData ("FakeDriver", typeof (FakeDriver))]
[InlineData ("NetDriver", typeof (NetDriver))]
[InlineData ("WindowsDriver", typeof (WindowsDriver))]
@@ -1160,7 +1133,7 @@ public class ApplicationTests
Assert.NotNull (Application.Top);
Assert.False (Application.Top!.Running);
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
Assert.True (result);
}
@@ -1176,7 +1149,7 @@ public class ApplicationTests
Assert.NotNull (Application.Top);
Assert.False (Application.Top!.Running);
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
}
[Fact]
@@ -1203,7 +1176,7 @@ public class ApplicationTests
Assert.NotNull (Application.Top);
Assert.False (Application.Top!.Running);
Application.Top!.Dispose ();
Shutdown ();
Application.Shutdown ();
}
// TODO: Add tests for Run that test errorHandler
@@ -1225,7 +1198,6 @@ public class ApplicationTests
isCompletedSuccessfully = true;
}
Init ();
Application.Shutdown ();
Assert.False (isCompletedSuccessfully);
@@ -1237,7 +1209,6 @@ public class ApplicationTests
[Fact]
public void Shutdown_Resets_SyncContext ()
{
Init ();
Application.Shutdown ();
Assert.Null (SynchronizationContext.Current);
}