Files
Terminal.Gui/Tests/UnitTests/ConsoleDrivers/V2/ApplicationV2Tests.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

351 lines
12 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Moq;
namespace UnitTests.ConsoleDrivers.V2;
public class ApplicationV2Tests
{
private ApplicationV2 NewApplicationV2 ()
{
var netInput = new Mock<INetInput> ();
SetupRunInputMockMethodToBlock (netInput);
var winInput = new Mock<IWindowsInput> ();
SetupRunInputMockMethodToBlock (winInput);
return new (
()=>netInput.Object,
Mock.Of<IConsoleOutput>,
() => winInput.Object,
Mock.Of<IConsoleOutput>);
}
[Fact]
public void TestInit_CreatesKeybindings ()
{
var v2 = NewApplicationV2();
Application.KeyBindings.Clear();
Assert.Empty(Application.KeyBindings.GetBindings ());
v2.Init ();
Assert.NotEmpty (Application.KeyBindings.GetBindings ());
v2.Shutdown ();
}
[Fact]
public void TestInit_DriverIsFacade ()
{
var v2 = NewApplicationV2();
Assert.Null (Application.Driver);
v2.Init ();
Assert.NotNull (Application.Driver);
var type = Application.Driver.GetType ();
Assert.True(type.IsGenericType);
Assert.True (type.GetGenericTypeDefinition () == typeof (ConsoleDriverFacade<>));
v2.Shutdown ();
Assert.Null (Application.Driver);
}
[Fact]
public void TestInit_ExplicitlyRequestWin ()
{
var netInput = new Mock<INetInput> (MockBehavior.Strict);
var netOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
var winInput = new Mock<IWindowsInput> (MockBehavior.Strict);
var winOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
winInput.Setup (i => i.Initialize (It.IsAny<ConcurrentQueue<WindowsConsole.InputRecord>> ()))
.Verifiable(Times.Once);
SetupRunInputMockMethodToBlock (winInput);
winInput.Setup (i=>i.Dispose ())
.Verifiable(Times.Once);
winOutput.Setup (i => i.Dispose ())
.Verifiable (Times.Once);
var v2 = new ApplicationV2 (
()=> netInput.Object,
() => netOutput.Object,
() => winInput.Object,
() => winOutput.Object);
Assert.Null (Application.Driver);
v2.Init (null,"v2win");
Assert.NotNull (Application.Driver);
var type = Application.Driver.GetType ();
Assert.True (type.IsGenericType);
Assert.True (type.GetGenericTypeDefinition () == typeof (ConsoleDriverFacade<>));
v2.Shutdown ();
Assert.Null (Application.Driver);
winInput.VerifyAll();
}
[Fact]
public void TestInit_ExplicitlyRequestNet ()
{
var netInput = new Mock<INetInput> (MockBehavior.Strict);
var netOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
var winInput = new Mock<IWindowsInput> (MockBehavior.Strict);
var winOutput = new Mock<IConsoleOutput> (MockBehavior.Strict);
netInput.Setup (i => i.Initialize (It.IsAny<ConcurrentQueue<ConsoleKeyInfo>> ()))
.Verifiable (Times.Once);
SetupRunInputMockMethodToBlock (netInput);
netInput.Setup (i => i.Dispose ())
.Verifiable (Times.Once);
netOutput.Setup (i => i.Dispose ())
.Verifiable (Times.Once);
var v2 = new ApplicationV2 (
() => netInput.Object,
() => netOutput.Object,
() => winInput.Object,
() => winOutput.Object);
Assert.Null (Application.Driver);
v2.Init (null, "v2net");
Assert.NotNull (Application.Driver);
var type = Application.Driver.GetType ();
Assert.True (type.IsGenericType);
Assert.True (type.GetGenericTypeDefinition () == typeof (ConsoleDriverFacade<>));
v2.Shutdown ();
Assert.Null (Application.Driver);
netInput.VerifyAll ();
}
private void SetupRunInputMockMethodToBlock (Mock<IWindowsInput> winInput)
{
winInput.Setup (r => r.Run (It.IsAny<CancellationToken> ()))
.Callback<CancellationToken> (token =>
{
// Simulate an infinite loop that checks for cancellation
while (!token.IsCancellationRequested)
{
// Perform the action that should repeat in the loop
// This could be some mock behavior or just an empty loop depending on the context
}
})
.Verifiable (Times.Once);
}
private void SetupRunInputMockMethodToBlock (Mock<INetInput> netInput)
{
netInput.Setup (r => r.Run (It.IsAny<CancellationToken> ()))
.Callback<CancellationToken> (token =>
{
// Simulate an infinite loop that checks for cancellation
while (!token.IsCancellationRequested)
{
// Perform the action that should repeat in the loop
// This could be some mock behavior or just an empty loop depending on the context
}
})
.Verifiable (Times.Once);
}
[Fact]
public void Test_NoInitThrowOnRun ()
{
var app = NewApplicationV2();
var ex = Assert.Throws<NotInitializedException> (() => app.Run (new Window ()));
Assert.Equal ("Run cannot be accessed before Initialization", ex.Message);
}
[Fact]
public void Test_InitRunShutdown ()
{
var orig = ApplicationImpl.Instance;
var v2 = NewApplicationV2();
ApplicationImpl.ChangeInstance (v2);
v2.Init ();
var timeoutToken = v2.AddTimeout (TimeSpan.FromMilliseconds (150),
() =>
{
if (Application.Top != null)
{
Application.RequestStop ();
return true;
}
return true;
}
);
Assert.Null (Application.Top);
// Blocks until the timeout call is hit
v2.Run (new Window ());
Assert.True(v2.RemoveTimeout (timeoutToken));
Assert.Null (Application.Top);
v2.Shutdown ();
ApplicationImpl.ChangeInstance (orig);
}
[Fact]
public void Test_InitRunShutdown_Generic_IdleForExit ()
{
var orig = ApplicationImpl.Instance;
var v2 = NewApplicationV2 ();
ApplicationImpl.ChangeInstance (v2);
v2.Init ();
v2.AddIdle (IdleExit);
Assert.Null (Application.Top);
// Blocks until the timeout call is hit
v2.Run<Window> ();
Assert.Null (Application.Top);
v2.Shutdown ();
ApplicationImpl.ChangeInstance (orig);
}
private bool IdleExit ()
{
if (Application.Top != null)
{
Application.RequestStop ();
return true;
}
return true;
}
[Fact]
public void TestRepeatedShutdownCalls_DoNotDuplicateDisposeOutput ()
{
var netInput = new Mock<INetInput> ();
SetupRunInputMockMethodToBlock (netInput);
Mock<IConsoleOutput>? outputMock = null;
var v2 = new ApplicationV2(
() => netInput.Object,
()=> (outputMock = new Mock<IConsoleOutput>()).Object,
Mock.Of<IWindowsInput>,
Mock.Of<IConsoleOutput>);
v2.Init (null,"v2net");
v2.Shutdown ();
v2.Shutdown ();
outputMock.Verify(o=>o.Dispose (),Times.Once);
}
[Fact]
public void TestRepeatedInitCalls_WarnsAndIgnores ()
{
var v2 = NewApplicationV2 ();
Assert.Null (Application.Driver);
v2.Init ();
Assert.NotNull (Application.Driver);
var mockLogger = new Mock<ILogger> ();
var beforeLogger = Logging.Logger;
Logging.Logger = mockLogger.Object;
v2.Init ();
v2.Init ();
mockLogger.Verify(
l=>l.Log (LogLevel.Error,
It.IsAny<EventId> (),
It.Is<It.IsAnyType> ((v, t) => v.ToString () == "Init called multiple times without shutdown, ignoring."),
It.IsAny<Exception> (),
It.IsAny<Func<It.IsAnyType, Exception, string>> ())
,Times.Exactly (2));
v2.Shutdown ();
// Restore the original null logger to be polite to other tests
Logging.Logger = beforeLogger;
}
[Fact]
public void Test_Open_CallsContinueWithOnUIThread ()
{
var orig = ApplicationImpl.Instance;
var v2 = NewApplicationV2 ();
ApplicationImpl.ChangeInstance (v2);
v2.Init ();
var b = new Button ();
bool result = false;
b.Accepting +=
(_,_) =>
{
Task.Run (() =>
{
Task.Delay (300).Wait ();
}).ContinueWith (
(t, _) =>
{
// no longer loading
Application.Invoke (() =>
{
result = true;
Application.RequestStop ();
});
},
TaskScheduler.FromCurrentSynchronizationContext ());
};
v2.AddTimeout (TimeSpan.FromMilliseconds (150),
()=>
{
// Run asynchronous logic inside Task.Run
if (Application.Top != null)
{
b.NewKeyDownEvent (Key.Enter);
b.NewKeyUpEvent (Key.Enter);
return false;
}
return true;
});
Assert.Null (Application.Top);
var w = new Window ();
w.Add (b);
// Blocks until the timeout call is hit
v2.Run (w);
Assert.Null (Application.Top);
v2.Shutdown ();
ApplicationImpl.ChangeInstance (orig);
Assert.True (result);
}
}