Fixes #4139. Application.Run<T> isn't initializing properly by setting the Application.ForceDriver property (#4142)

* Fixes #4139. Application.Run<T> isn't initializing properly by setting the Application.ForceDriver property

* Trying fix unit tests

* Only to force again CI tests because I haven't errors locally

* This should pass, unless RunningUnitTests is set to false somewhere

* Fix Unix unit tests and failures via ReSharper

* Changes suggested by @tig

* Prevent empty string

* Centralize all the entry logic in the InternalInit method

* Change GetDriverTypes to return a tuple
This commit is contained in:
BDisp
2025-06-12 17:48:05 +01:00
committed by GitHub
parent 76b7e52e12
commit ad1de255b1
17 changed files with 257 additions and 47 deletions

View File

@@ -1,4 +1,5 @@
using System.IO.Abstractions;
using System.Globalization;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Runtime.InteropServices;
using TerminalGuiFluentTesting;
@@ -12,6 +13,7 @@ public class FileDialogFluentTests
public FileDialogFluentTests (ITestOutputHelper outputHelper)
{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
_out = new TestOutputWriter (outputHelper);
}

View File

@@ -1,3 +1,4 @@
using System.Globalization;
using System.Reflection;
using TerminalGuiFluentTesting;
using Xunit.Abstractions;
@@ -13,6 +14,7 @@ public class MenuBarv2Tests
public MenuBarv2Tests (ITestOutputHelper outputHelper)
{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
_out = new TestOutputWriter (outputHelper);
}

View File

@@ -1,4 +1,4 @@
using System.Reflection;
using System.Globalization;
using TerminalGuiFluentTesting;
using Xunit.Abstractions;
@@ -7,9 +7,15 @@ namespace IntegrationTests.FluentTests;
/// <summary>
/// Tests for the PopoverMenu class
/// </summary>
public class PopoverMenuTests (ITestOutputHelper outputHelper)
public class PopoverMenuTests
{
private readonly TextWriter _out = new TestOutputWriter (outputHelper);
private readonly TextWriter _out;
public PopoverMenuTests (ITestOutputHelper outputHelper)
{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
_out = new TestOutputWriter (outputHelper);
}
[Theory]
[ClassData (typeof (V2TestDrivers))]

View File

@@ -655,7 +655,17 @@ public class ApplicationTests
Assert.NotNull (SynchronizationContext.Current);
}
private void Shutdown () { Application.Shutdown (); }
private void Shutdown ()
{
if (ApplicationImpl.Instance is ApplicationV2)
{
ApplicationImpl.Instance.Shutdown ();
}
else
{
Application.Shutdown ();
}
}
#region RunTests
@@ -1104,6 +1114,89 @@ public class ApplicationTests
Assert.Null (Application.Top);
}
private class TestToplevel : Toplevel { }
[Theory]
[InlineData ("v2win", typeof (ConsoleDriverFacade<WindowsConsole.InputRecord>))]
[InlineData ("v2net", typeof (ConsoleDriverFacade<ConsoleKeyInfo>))]
[InlineData ("FakeDriver", typeof (FakeDriver))]
[InlineData ("NetDriver", typeof (NetDriver))]
[InlineData ("WindowsDriver", typeof (WindowsDriver))]
[InlineData ("CursesDriver", typeof (CursesDriver))]
public void Run_T_Call_Init_ForceDriver_Should_Pick_Correct_Driver (string driverName, Type expectedType)
{
Assert.True (ConsoleDriver.RunningUnitTests);
var result = false;
Task.Run (() =>
{
Task.Delay (300).Wait ();
}).ContinueWith (
(t, _) =>
{
// no longer loading
Application.Invoke (() =>
{
result = true;
Application.RequestStop ();
});
},
TaskScheduler.FromCurrentSynchronizationContext ());
Application.ForceDriver = driverName;
Application.Run<TestToplevel> ();
Assert.NotNull (Application.Driver);
Assert.Equal (expectedType, Application.Driver?.GetType ());
Assert.NotNull (Application.Top);
Assert.False (Application.Top!.Running);
Application.Top!.Dispose ();
Shutdown ();
Assert.True (result);
}
[Fact]
public void Run_T_With_Legacy_Driver_Does_Not_Call_ResetState_After_Init ()
{
Assert.False (Application.Initialized);
Application.Init ();
Assert.True (Application.Initialized);
Application.Iteration += (_, _) => Application.RequestStop ();
Application.Run<TestToplevel> ();
Assert.NotNull (Application.Driver);
Assert.NotNull (Application.Top);
Assert.False (Application.Top!.Running);
Application.Top!.Dispose ();
Shutdown ();
}
[Fact]
public void Run_T_With_V2_Driver_Does_Not_Call_ResetState_After_Init ()
{
Assert.False (Application.Initialized);
Application.Init (null, "v2net");
Assert.True (Application.Initialized);
Task.Run (() =>
{
Task.Delay (300).Wait ();
}).ContinueWith (
(t, _) =>
{
// no longer loading
Application.Invoke (() =>
{
Application.RequestStop ();
});
},
TaskScheduler.FromCurrentSynchronizationContext ());
Application.Run<TestToplevel> ();
Assert.NotNull (Application.Driver);
Assert.NotNull (Application.Top);
Assert.False (Application.Top!.Running);
Application.Top!.Dispose ();
Shutdown ();
}
// TODO: Add tests for Run that test errorHandler
#endregion

View File

@@ -7,6 +7,10 @@ using Moq;
namespace UnitTests.ConsoleDrivers.V2;
public class ApplicationV2Tests
{
public ApplicationV2Tests ()
{
ConsoleDriver.RunningUnitTests = true;
}
private ApplicationV2 NewApplicationV2 ()
{
@@ -362,7 +366,6 @@ public class ApplicationV2Tests
if (Application.Top != null)
{
Application.RaiseKeyDownEvent (Application.QuitKey);
return false;
}
return false;
@@ -575,8 +578,6 @@ public class ApplicationV2Tests
{
b.NewKeyDownEvent (Key.Enter);
b.NewKeyUpEvent (Key.Enter);
return false;
}
return false;

View File

@@ -3,6 +3,11 @@
namespace UnitTests.ConsoleDrivers.V2;
public class WindowSizeMonitorTests
{
public WindowSizeMonitorTests ()
{
ConsoleDriver.RunningUnitTests = false;
}
[Fact]
public void TestWindowSizeMonitor_RaisesEventWhenChanges ()
{
@@ -70,7 +75,4 @@ public class WindowSizeMonitorTests
Assert.Single (result);
Assert.Equal (new Size (30, 20), result [0].Size);
}
}