Files
Terminal.Gui/UnitTests/ConsoleDrivers/MainLoopDriverTests.cs
Tig 7af54f369d Robustness improvements in prep for implementing Virtual Terminal Sequences (#3094)
* Fixes #2616. Support combining sequences that don't normalize

* Decouples Application from ConsoleDriver in TestHelpers

* Updates driver tests to match new arch

* Start on making all driver tests test all drivers

* Improves handling if combining marks.

* Fix unit tests fails.

* Fix unit tests fails.

* Handling combining mask.

* Tying to fix this unit test that sometimes fail.

* Add support for combining mask on NetDriver.

* Enable CombiningMarks as List<Rune>.

* Prevents combining marks on invalid runes default and space.

* Formatting for CI tests.

* Fix non-normalized combining mark to add 1 to Col.

* Reformatting for retest the CI.

* Forces non-normalized CMs to be ignored.

* Initial experiment

* Created ANSiDriver. Updated UI Catalog command line handling

* Fixed ForceDriver logic

* Fixed ForceDriver logic

* Updating P/Invoke

* Force16 colors WIP

* Fixed 16 colo mode

* Updated unit tests

* UI catalog tweak

* Added chinese scenario from bdisp

* Disabled AnsiDriver unit tests for now.

* Code cleanup

* Initial commit (fork from v2_fixes_2610_WT_VTS)

* Code cleanup

* Removed nativemethods.txt

* Removed not needed native stuff

* Code cleanup

* Ensures command line handler doesn't eat exceptions

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2024-01-04 22:35:18 -07:00

277 lines
11 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Xunit;
using Xunit.Abstractions;
// Alias Console to MockConsole so we don't accidentally use Console
using Console = Terminal.Gui.FakeConsole;
namespace Terminal.Gui.DriverTests;
public class MainLoopDriverTests {
public MainLoopDriverTests (ITestOutputHelper output) => ConsoleDriver.RunningUnitTests = true;
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_Constructs_Disposes (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
// Check default values
Assert.NotNull (mainLoop);
Assert.Equal (mainLoopDriver, mainLoop.MainLoopDriver);
Assert.Empty (mainLoop.IdleHandlers);
Assert.Empty (mainLoop.Timeouts);
Assert.False (mainLoop.Running);
// Clean up
mainLoop.Dispose ();
// TODO: It'd be nice if we could really verify IMainLoopDriver.TearDown was called
// and that it was actually cleaned up.
Assert.Null (mainLoop.MainLoopDriver);
Assert.Empty (mainLoop.IdleHandlers);
Assert.Empty (mainLoop.Timeouts);
Assert.False (mainLoop.Running);
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_AddTimeout_ValidParameters_ReturnsToken (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool callbackInvoked = false;
object token = mainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
callbackInvoked = true;
return false;
});
Assert.NotNull (token);
mainLoop.RunIteration (); // Run an iteration to process the timeout
Assert.False (callbackInvoked); // Callback should not be invoked immediately
Thread.Sleep (200); // Wait for the timeout
mainLoop.RunIteration (); // Run an iteration to process the timeout
Assert.True (callbackInvoked); // Callback should be invoked after the timeout
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_RemoveTimeout_ValidToken_ReturnsTrue (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
object token = mainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), () => false);
bool result = mainLoop.RemoveTimeout (token);
Assert.True (result);
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_RemoveTimeout_InvalidToken_ReturnsFalse (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool result = mainLoop.RemoveTimeout (new object ());
Assert.False (result);
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_AddIdle_ValidIdleHandler_ReturnsToken (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool idleHandlerInvoked = false;
bool IdleHandler ()
{
idleHandlerInvoked = true;
return false;
}
var token = mainLoop.AddIdle (IdleHandler);
Assert.NotNull (token);
Assert.False (idleHandlerInvoked); // Idle handler should not be invoked immediately
mainLoop.RunIteration (); // Run an iteration to process the idle handler
Assert.True (idleHandlerInvoked); // Idle handler should be invoked after processing
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_RemoveIdle_ValidToken_ReturnsTrue (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool IdleHandler () => false;
var token = mainLoop.AddIdle (IdleHandler);
bool result = mainLoop.RemoveIdle (token);
Assert.True (result);
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_RemoveIdle_InvalidToken_ReturnsFalse (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool result = mainLoop.RemoveIdle (() => false);
Assert.False (result);
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_RunIteration_ValidIdleHandler_CallsIdleHandler (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool idleHandlerInvoked = false;
var idleHandler = () => {
idleHandlerInvoked = true;
return false;
};
mainLoop.AddIdle (idleHandler);
mainLoop.RunIteration (); // Run an iteration to process the idle handler
Assert.True (idleHandlerInvoked);
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_CheckTimersAndIdleHandlers_NoTimersOrIdleHandlers_ReturnsFalse (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
bool result = mainLoop.CheckTimersAndIdleHandlers (out int waitTimeout);
Assert.False (result);
Assert.Equal (-1, waitTimeout);
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_CheckTimersAndIdleHandlers_TimersActive_ReturnsTrue (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
mainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), () => false);
bool result = mainLoop.CheckTimersAndIdleHandlers (out int waitTimeout);
Assert.True (result);
Assert.True (waitTimeout >= 0);
mainLoop.Dispose ();
}
[Theory]
[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//[InlineData (typeof (ANSIDriver), typeof (AnsiMainLoopDriver))]
public void MainLoop_CheckTimersAndIdleHandlers_IdleHandlersActive_ReturnsTrue (Type driverType, Type mainLoopDriverType)
{
var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
var mainLoop = new MainLoop (mainLoopDriver);
mainLoop.AddIdle (() => false);
bool result = mainLoop.CheckTimersAndIdleHandlers (out int waitTimeout);
Assert.True (result);
Assert.Equal (-1, waitTimeout);
mainLoop.Dispose ();
}
//[Theory]
//[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
//[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
//[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
//[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
//public void MainLoop_Invoke_ValidAction_RunsAction (Type driverType, Type mainLoopDriverType)
//{
// var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
// var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
// var mainLoop = new MainLoop (mainLoopDriver);
// var actionInvoked = false;
// mainLoop.Invoke (() => { actionInvoked = true; });
// mainLoop.RunIteration (); // Run an iteration to process the action.
// Assert.True (actionInvoked);
// mainLoop.Dispose ();
//}
}