mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Initial plan * Remove legacy drivers and reorganize v2 architecture Co-authored-by: tig <585482+tig@users.noreply.github.com> * Extract Windows key helper utilities and fix build Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix all test references to legacy drivers Co-authored-by: tig <585482+tig@users.noreply.github.com> * Update documentation to reflect new driver architecture Co-authored-by: tig <585482+tig@users.noreply.github.com> * Remove V2.cd diagram file Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix test failures: support legacy drivers and update exception handling Co-authored-by: tig <585482+tig@users.noreply.github.com> * updated driver names * Move V2 tests from ConsoleDrivers/V2 to proper locations Co-authored-by: tig <585482+tig@users.noreply.github.com> * Rename ApplicationV2 to ModernApplicationImpl to remove v2 terminology Co-authored-by: tig <585482+tig@users.noreply.github.com> * Remove V2 terminology from test drivers and FakeDriver classes Co-authored-by: tig <585482+tig@users.noreply.github.com> * Merge ModernApplicationImpl into ApplicationImpl and move to App folder Co-authored-by: tig <585482+tig@users.noreply.github.com> * Create modern FakeDriver with component factory architecture in Terminal.Gui project Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor: Move non-platform-dependent code from /Drivers to /App Co-authored-by: tig <585482+tig@users.noreply.github.com> * Code cleanup and org * Unit test reorg * Refactor MainLoop architecture: rename classes and enhance documentation for clarity Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add comprehensive FakeDriver tests (WIP - some tests need fixes) Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fixed FakeDriver build failures * Fix all FakeDriver test failures - Application.Top creation and clipboard behaviors Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fixed FakeDriver build failures2 * Remove hanging legacy FakeDriver tests that use Console.MockKeyPresses Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fixed some tests * Fixed more tests * Fixed more tests * Fix bad copilot (#4277) * Update Terminal.Gui/Drivers/FakeDriver/FakeConsoleOutput.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor Application Init and Update Tests Refactored `Application.Init` to improve initialization logic: - Added fallback to `ForceDriver` when `driverName` is null. - Changed repeated `Init` calls to throw `InvalidOperationException`. - Updated `_driverName` assignment logic for robustness. Enhanced `IConsoleDriver` with detailed remarks on implementations. Revised test cases to align with updated `Application.Init` behavior: - Replaced `FakeDriver` with `null` and `driverName: "fake"`. - Skipped or commented out tests incompatible with new logic. - Improved formatting and removed redundant setup code. Improved code style and consistency across the codebase: - Standardized parameter formatting and spacing. - Removed outdated comments and unused code. General cleanup to enhance readability and maintainability. * Warp fix copilot (#4278) * More fixes (#4279) * Fixes/works around test failures and temporarily disable failing test Updated `FakeDriver` to set `RunningUnitTests` to `true` and initialize dimensions using `FakeConsole`. Modified `TestRespondersDisposedAttribute` to set `ConsoleDriver.RunningUnitTests` in the `Before` method, ensuring proper behavior during unit tests. Temporarily disabled the `Button_CanFocus_False_Raises_Accepted_Correctly` test in `ViewCommandTests` by adding a `Skip` parameter to the `[Fact]` attribute, referencing issue #4270. * Allow all tests to run despite failures in UnitTests Modified the `dotnet test` command in the `Run UnitTestsParallelizable` step to set `xunit.stopOnFail` to `false`. This ensures that the test runner does not stop execution on the first failure, allowing all tests to execute regardless of individual test outcomes. * Refactor ApplicationScreenTests for cleaner setup/teardown Refactored `ClearContents_Called_When_Top_Frame_Changes` test: - Added `[AutoInitShutdown]` attribute for automatic lifecycle management. - Replaced manual `Application.Init` and `Application.Top` setup with `Application.Begin` and `RunState`. - Simplified event handling by defining `ClearedContents` handler inline. - Removed explicit cleanup logic, relying on `Application.End` for teardown. Updated `using` directives to include `UnitTests` namespace. * Attempt to fix intermittent local test failures. Update ApplicationImpl initialization parameter Changed the second parameter of the `impl.Init` method in the `FakeApplicationFactory` class from `"dotnet"` to `"fake"`. * Code cleanup to cause Action to re-run. * Stop tests on first failure in UnitTestsParallelizable Updated the `dotnet test` command in `unit-tests.yml` to set the `xunit.stopOnFail` parameter to `true`. This change ensures that test execution halts immediately upon encountering a failure, allowing quicker identification and resolution of issues. Note that this may prevent the full test suite from running in the event of a failure. * Allow all tests to run despite failures in CI Updated `unit-tests.yml` to set `xunit.stopOnFail` to `false` in both `Run UnitTests` and `Run UnitTestsParallelizable` steps. This ensures that the test runner does not stop execution on the first test failure, allowing all tests to complete even if some fail. * Enhance RuneExtensions docs and update user dictionary Updated the `<remarks>` section in `RuneExtensions.GetColumns` to include details about the `wcwidth` implementation and improved readability with `<para>` tags. Added `wcwidth` to the user dictionary in `Terminal.sln.DotSettings` to avoid spelling errors. * Improve XML doc formatting in RuneExtensions.cs Updated the remarks section of the `GetColumns` method in the `RuneExtensions` class to enhance readability by reformatting and properly indenting `<para>` tags. The content remains unchanged, describing the method's implementation via `wcwidth` and its role as a Terminal.Gui extension for `System.Text.Rune`. * Refactor drivers and improve clipboard handling Replaced legacy drivers (`CursesDriver`, `NetDriver`) with `UnixDriver` and `DotNetDriver` across the codebase, including comments, method names, and test cases. Updated documentation and remarks to reflect the new driver names and platforms. Revamped clipboard handling with new platform-specific implementations: `UnixClipboard` for Unix, `MacOSXClipboard` for macOS, and `WSLClipboard` for Linux under WSL. Removed the old `CursesClipboard` and consolidated clipboard logic. Updated test cases to align with the new drivers and clipboard implementations. Improved naming consistency and cleaned up redundant code. Updated the README and documentation to reflect these changes. * Remove `PlatformColor` from `Attribute` struct This commit removes the `PlatformColor` property from the `Attribute` struct, simplifying the codebase by eliminating platform-specific color handling. The following changes were made: - Removed `PlatformColor` from the `Attribute` struct, including its initialization, usage, and related comments. - Updated constructors to no longer initialize or use `PlatformColor`. - Modified `Equals` and `GetHashCode` methods to exclude `PlatformColor`. - Updated `UnixComponentFactory` documentation to remove references to "v2unix." - Renamed `v2TestDriver` to `testDriver` in the `With` class for clarity. - Removed `PlatformColor` references in `DriverAssert` and related error messages. - Deleted test cases in `AttributeTests` that relied on `PlatformColor`. - Cleaned up comments and TODOs related to `PlatformColor` and `UnixDriver`. These changes reflect a shift away from platform-dependent color management, improving code clarity and reducing complexity. Remove `PlatformColor` and simplify `Attribute` logic The `PlatformColor` property has been removed from the `Attribute` struct, along with its associated logic, simplifying the codebase and eliminating platform-specific dependencies. Constructors, equality checks, and hash code generation in `Attribute` have been updated accordingly. The `CurrentAttribute` property in `ConsoleDriver` and `OutputBuffer` has been simplified, removing dependencies on `Application.Driver`. The `MakeColor` method logic has been removed or simplified in related classes. Tests in `AttributeTests` have been refactored to reflect these changes, focusing on `Foreground`, `Background`, and `Style`. Unix-specific logic tied to `PlatformColor` has been eliminated. Additional updates include renaming parameters in the `With` class for clarity, simplifying `DriverAssert` output, and performing minor code cleanups to improve readability and maintainability. * Refactor Terminal.Gui driver architecture for v2 Updated documentation to reflect the new modular driver architecture in Terminal.Gui v2. - Revised `namespace-drivers.md` to include new components (`IConsoleInput`, `IConsoleOutput`, `IInputProcessor`, `IOutputBuffer`, `IWindowSizeMonitor`) and terminal size monitoring. - Replaced "Key Components" with "Architecture Overview" and added details on the **Component Factory** pattern. - Documented the four driver implementations (`DotNetDriver`, `WindowsDriver`, `UnixDriver`, `FakeDriver`) and their platform-specific optimizations. - Added a "Threading Model" section to explain the multi-threaded design for responsive input handling. - Updated examples to demonstrate driver capabilities and explicit driver selection. In `drivers.md`: - Expanded the "Overview" to emphasize the modular, component-based architecture. - Reorganized "Drivers" into "Available Drivers" and added details on `FakeDriver` for unit testing. - Added sections on "Initialization Flow," "Shutdown Flow," and platform-specific driver details. - Provided examples for accessing driver components and creating custom drivers. In `index.md`: - Updated "Cross Platform" feature to reflect new driver names and clarified compatibility with SSH and monochrome terminals. * Moved files around --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com> Co-authored-by: Tig <tig@users.noreply.github.com> Co-authored-by: Thomas Nind <31306100+tznind@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1233 lines
39 KiB
C#
1233 lines
39 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using JetBrains.Annotations;
|
|
using Terminal.Gui.Drivers;
|
|
using UnitTests;
|
|
using Xunit.Abstractions;
|
|
using static Terminal.Gui.Configuration.ConfigurationManager;
|
|
|
|
// Alias Console to MockConsole so we don't accidentally use Console
|
|
|
|
namespace Terminal.Gui.ApplicationTests;
|
|
|
|
public class ApplicationTests
|
|
{
|
|
public ApplicationTests (ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
ConsoleDriver.RunningUnitTests = true;
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
View.EnableDebugIDisposableAsserts = true;
|
|
View.Instances.Clear ();
|
|
RunState.Instances.Clear ();
|
|
#endif
|
|
}
|
|
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
private object _timeoutLock;
|
|
|
|
[Fact]
|
|
public void AddTimeout_Fires ()
|
|
{
|
|
Assert.Null (_timeoutLock);
|
|
_timeoutLock = new ();
|
|
|
|
uint timeoutTime = 250;
|
|
var initialized = false;
|
|
var iteration = 0;
|
|
var shutdown = false;
|
|
object timeout = null;
|
|
var timeoutCount = 0;
|
|
|
|
Application.InitializedChanged += OnApplicationOnInitializedChanged;
|
|
|
|
var a = new AutoInitShutdownAttribute ();
|
|
a.Before (null);
|
|
|
|
Assert.True (initialized);
|
|
Assert.False (shutdown);
|
|
|
|
_output.WriteLine ("Application.Run<Toplevel> ().Dispose ()..");
|
|
Application.Run<Toplevel> ().Dispose ();
|
|
_output.WriteLine ("Back from Application.Run<Toplevel> ().Dispose ()");
|
|
|
|
Assert.True (initialized);
|
|
Assert.False (shutdown);
|
|
|
|
Assert.Equal (1, timeoutCount);
|
|
Application.Shutdown ();
|
|
|
|
Application.InitializedChanged -= OnApplicationOnInitializedChanged;
|
|
|
|
lock (_timeoutLock)
|
|
{
|
|
if (timeout is { })
|
|
{
|
|
Application.RemoveTimeout (timeout);
|
|
timeout = null;
|
|
}
|
|
}
|
|
|
|
Assert.True (initialized);
|
|
Assert.True (shutdown);
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.Empty (View.Instances);
|
|
#endif
|
|
lock (_timeoutLock)
|
|
{
|
|
_timeoutLock = null;
|
|
}
|
|
|
|
|
|
a.After (null);
|
|
return;
|
|
|
|
void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
|
|
{
|
|
if (a.Value)
|
|
{
|
|
Application.Iteration += OnApplicationOnIteration;
|
|
initialized = true;
|
|
|
|
lock (_timeoutLock)
|
|
{
|
|
_output.WriteLine ($"Setting timeout for {timeoutTime}ms");
|
|
timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (timeoutTime), TimeoutCallback);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Application.Iteration -= OnApplicationOnIteration;
|
|
shutdown = true;
|
|
}
|
|
}
|
|
|
|
bool TimeoutCallback ()
|
|
{
|
|
lock (_timeoutLock)
|
|
{
|
|
_output.WriteLine ($"TimeoutCallback. Count: {++timeoutCount}. Application Iteration: {iteration}");
|
|
|
|
if (timeout is { })
|
|
{
|
|
_output.WriteLine (" Nulling timeout.");
|
|
timeout = null;
|
|
}
|
|
}
|
|
|
|
// False means "don't re-do timer and remove it"
|
|
return false;
|
|
}
|
|
|
|
void OnApplicationOnIteration (object s, IterationEventArgs a)
|
|
{
|
|
lock (_timeoutLock)
|
|
{
|
|
if (timeoutCount > 0)
|
|
{
|
|
_output.WriteLine ($"Iteration #{iteration} - Timeout fired. Calling Application.RequestStop.");
|
|
Application.RequestStop ();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
iteration++;
|
|
|
|
// Simulate a delay
|
|
Thread.Sleep ((int)timeoutTime / 10);
|
|
|
|
// Worst case scenario - something went wrong
|
|
if (Application.Initialized && iteration > 25)
|
|
{
|
|
_output.WriteLine ($"Too many iterations ({iteration}): Calling Application.RequestStop.");
|
|
Application.RequestStop ();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Begin_Null_Toplevel_Throws ()
|
|
{
|
|
// Test null Toplevel
|
|
Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
|
|
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown (verifyShutdown: true)]
|
|
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);
|
|
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);
|
|
|
|
RunState rs = Application.Begin (new ());
|
|
Application.Top!.Title = "End_And_Shutdown_Should_Not_Dispose_ApplicationTop";
|
|
Assert.Equal (rs.Toplevel, Application.Top);
|
|
Application.End (rs);
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.True (rs.WasDisposed);
|
|
Assert.False (Application.Top!.WasDisposed); // Is true because the rs.Toplevel is the same as Application.Top
|
|
#endif
|
|
|
|
Assert.Null (rs.Toplevel);
|
|
|
|
Toplevel top = Application.Top;
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Exception exception = Record.Exception (Application.Shutdown);
|
|
Assert.NotNull (exception);
|
|
Assert.False (top.WasDisposed);
|
|
top.Dispose ();
|
|
Assert.True (top.WasDisposed);
|
|
#endif
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Init_Begin_End_Cleans_Up ()
|
|
{
|
|
// Start stopwatch
|
|
Stopwatch stopwatch = new Stopwatch ();
|
|
stopwatch.Start ();
|
|
|
|
// 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 (); };
|
|
|
|
RunState runstate = null;
|
|
|
|
EventHandler<RunStateEventArgs> newRunStateFn = (s, e) =>
|
|
{
|
|
Assert.NotNull (e.State);
|
|
runstate = e.State;
|
|
};
|
|
Application.NotifyNewRunState += newRunStateFn;
|
|
|
|
var topLevel = new Toplevel ();
|
|
RunState rs = Application.Begin (topLevel);
|
|
Assert.NotNull (rs);
|
|
Assert.NotNull (runstate);
|
|
Assert.Equal (rs, runstate);
|
|
|
|
Assert.Equal (topLevel, Application.Top);
|
|
|
|
Application.NotifyNewRunState -= newRunStateFn;
|
|
Application.End (runstate);
|
|
|
|
Assert.NotNull (Application.Top);
|
|
Assert.NotNull (Application.Driver);
|
|
|
|
topLevel.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.Driver);
|
|
|
|
// Stop stopwatch
|
|
stopwatch.Stop ();
|
|
|
|
_output.WriteLine ($"Load took {stopwatch.ElapsedMilliseconds} ms");
|
|
|
|
}
|
|
|
|
// Legacy driver test - all InlineData commented out
|
|
//[Theory]
|
|
////[InlineData (typeof (DotNetDriver))]
|
|
|
|
////[InlineData (typeof (ANSIDriver))]
|
|
////[InlineData (typeof (WindowsDriver))]
|
|
////[InlineData (typeof (UnixDriver))]
|
|
//public void Init_DriverName_Should_Pick_Correct_Driver (Type driverType)
|
|
//{
|
|
// var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
|
|
// Application.Init (driverName: driverType.Name);
|
|
// Assert.NotNull (Application.Driver);
|
|
// Assert.NotEqual (driver, Application.Driver);
|
|
// Assert.Equal (driverType, Application.Driver?.GetType ());
|
|
// Application.Shutdown ();
|
|
//}
|
|
|
|
[Fact]
|
|
public void Init_Null_Driver_Should_Pick_A_Driver ()
|
|
{
|
|
Application.Init ();
|
|
|
|
Assert.NotNull (Application.Driver);
|
|
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData (typeof (FakeDriver))]
|
|
//[InlineData (typeof (DotNetDriver))]
|
|
//[InlineData (typeof (WindowsDriver))]
|
|
//[InlineData (typeof (UnixDriver))]
|
|
public void Init_ResetState_Resets_Properties (Type driverType)
|
|
{
|
|
ThrowOnJsonErrors = true;
|
|
|
|
// For all the fields/properties of Application, check that they are reset to their default values
|
|
|
|
// Set some values
|
|
|
|
Application.Init (driverName: driverType.Name);
|
|
|
|
// Application.IsInitialized = true;
|
|
|
|
// Reset
|
|
Application.ResetState ();
|
|
|
|
void CheckReset ()
|
|
{
|
|
// Check that all fields and properties are set to their default values
|
|
|
|
// Public Properties
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MouseGrabHandler.MouseGrabView);
|
|
|
|
// Don't check Application.ForceDriver
|
|
// Assert.Empty (Application.ForceDriver);
|
|
// Don't check Application.Force16Colors
|
|
//Assert.False (Application.Force16Colors);
|
|
Assert.Null (Application.Driver);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.False (Application.EndAfterFirstIteration);
|
|
|
|
// Commented out because if CM changed the defaults, those changes should
|
|
// persist across Inits.
|
|
//Assert.Equal (Key.Tab.WithShift, Application.PrevTabKey);
|
|
//Assert.Equal (Key.Tab, Application.NextTabKey);
|
|
//Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
|
|
//Assert.Equal (Key.F6, Application.NextTabGroupKey);
|
|
//Assert.Equal (Key.Esc, Application.QuitKey);
|
|
|
|
// Internal properties
|
|
Assert.False (Application.Initialized);
|
|
Assert.Equal (Application.GetSupportedCultures (), Application.SupportedCultures);
|
|
Assert.Equal (Application.GetAvailableCulturesFromEmbeddedResources (), Application.SupportedCultures);
|
|
Assert.False (Application._forceFakeConsole);
|
|
Assert.Equal (-1, Application.MainThreadId);
|
|
Assert.Empty (Application.TopLevels);
|
|
Assert.Empty (Application.CachedViewsUnderMouse);
|
|
|
|
// Mouse
|
|
// Do not reset _lastMousePosition
|
|
//Assert.Null (Application._lastMousePosition);
|
|
|
|
// Navigation
|
|
Assert.Null (Application.Navigation);
|
|
|
|
// Popover
|
|
Assert.Null (Application.Popover);
|
|
|
|
// Events - Can't check
|
|
//Assert.Null (Application.NotifyNewRunState);
|
|
//Assert.Null (Application.NotifyNewRunState);
|
|
//Assert.Null (Application.Iteration);
|
|
//Assert.Null (Application.SizeChanging);
|
|
//Assert.Null (Application.GrabbedMouse);
|
|
//Assert.Null (Application.UnGrabbingMouse);
|
|
//Assert.Null (Application.GrabbedMouse);
|
|
//Assert.Null (Application.UnGrabbedMouse);
|
|
//Assert.Null (Application.MouseEvent);
|
|
//Assert.Null (Application.KeyDown);
|
|
//Assert.Null (Application.KeyUp);
|
|
}
|
|
|
|
CheckReset ();
|
|
|
|
// Set the values that can be set
|
|
Application.Initialized = true;
|
|
Application._forceFakeConsole = true;
|
|
Application.MainThreadId = 1;
|
|
|
|
//Application._topLevels = new List<Toplevel> ();
|
|
Application.CachedViewsUnderMouse.Clear ();
|
|
|
|
//Application.SupportedCultures = new List<CultureInfo> ();
|
|
Application.Force16Colors = true;
|
|
|
|
//Application.ForceDriver = "driver";
|
|
Application.EndAfterFirstIteration = true;
|
|
Application.PrevTabGroupKey = Key.A;
|
|
Application.NextTabGroupKey = Key.B;
|
|
Application.QuitKey = Key.C;
|
|
Application.KeyBindings.Add (Key.D, Command.Cancel);
|
|
|
|
Application.CachedViewsUnderMouse.Clear ();
|
|
|
|
//Application.WantContinuousButtonPressedView = new View ();
|
|
|
|
// Mouse
|
|
Application.LastMousePosition = new Point (1, 1);
|
|
|
|
Application.Navigation = new ();
|
|
|
|
Application.ResetState ();
|
|
CheckReset ();
|
|
|
|
ThrowOnJsonErrors = false;
|
|
}
|
|
|
|
[Fact]
|
|
public void Init_Shutdown_Cleans_Up ()
|
|
{
|
|
// Verify initial state is per spec
|
|
//Pre_Init_State ();
|
|
|
|
Application.Init (new FakeDriver ());
|
|
|
|
// Verify post-Init state is correct
|
|
//Post_Init_State ();
|
|
|
|
Application.Shutdown ();
|
|
|
|
// Verify state is back to initial
|
|
//Pre_Init_State ();
|
|
#if DEBUG_IDISPOSABLE
|
|
|
|
// Validate there are no outstanding Responder-based instances
|
|
// after a scenario was selected to run. This proves the main UI Catalog
|
|
// 'app' closed cleanly.
|
|
Assert.Empty (View.Instances);
|
|
#endif
|
|
}
|
|
|
|
[Fact]
|
|
public void Shutdown_Alone_Does_Nothing () { Application.Shutdown (); }
|
|
|
|
[Theory]
|
|
[InlineData (typeof (FakeDriver))]
|
|
//[InlineData (typeof (DotNetDriver))]
|
|
//[InlineData (typeof (WindowsDriver))]
|
|
//[InlineData (typeof (UnixDriver))]
|
|
public void Init_Shutdown_Fire_InitializedChanged (Type driverType)
|
|
{
|
|
var initialized = false;
|
|
var shutdown = false;
|
|
|
|
Application.InitializedChanged += OnApplicationOnInitializedChanged;
|
|
|
|
Application.Init (driverName: driverType.Name);
|
|
Assert.True (initialized);
|
|
Assert.False (shutdown);
|
|
|
|
Application.Shutdown ();
|
|
Assert.True (initialized);
|
|
Assert.True (shutdown);
|
|
|
|
Application.InitializedChanged -= OnApplicationOnInitializedChanged;
|
|
|
|
return;
|
|
|
|
void OnApplicationOnInitializedChanged (object s, EventArgs<bool> a)
|
|
{
|
|
if (a.Value)
|
|
{
|
|
initialized = true;
|
|
}
|
|
else
|
|
{
|
|
shutdown = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Init_Unbalanced_Throws ()
|
|
{
|
|
Assert.Throws<InvalidOperationException> (
|
|
() =>
|
|
Application.InternalInit (
|
|
new FakeDriver ()
|
|
)
|
|
);
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Init_Unbalanced_Throws2 ()
|
|
{
|
|
// Now try the other way
|
|
Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver ()));
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
public void Init_WithoutTopLevelFactory_Begin_End_Cleans_Up ()
|
|
{
|
|
// 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 (); };
|
|
|
|
// NOTE: Run<T>, when called after Init has been called behaves differently than
|
|
// when called if Init has not been called.
|
|
Toplevel topLevel = new ();
|
|
Application.InternalInit (new FakeDriver ());
|
|
|
|
RunState runstate = null;
|
|
|
|
EventHandler<RunStateEventArgs> newRunStateFn = (s, e) =>
|
|
{
|
|
Assert.NotNull (e.State);
|
|
runstate = e.State;
|
|
};
|
|
Application.NotifyNewRunState += newRunStateFn;
|
|
|
|
RunState rs = Application.Begin (topLevel);
|
|
Assert.NotNull (rs);
|
|
Assert.NotNull (runstate);
|
|
Assert.Equal (rs, runstate);
|
|
|
|
Assert.Equal (topLevel, Application.Top);
|
|
|
|
Application.NotifyNewRunState -= newRunStateFn;
|
|
Application.End (runstate);
|
|
|
|
Assert.NotNull (Application.Top);
|
|
Assert.NotNull (Application.MainLoop);
|
|
Assert.NotNull (Application.Driver);
|
|
|
|
topLevel.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact (Skip = "FakeDriver is not allowed, use AutoInitShutdown attribute instead")]
|
|
public void Init_NoParam_ForceDriver_Works ()
|
|
{
|
|
Application.ForceDriver = "Fake";
|
|
Application.Init ();
|
|
//Assert.IsType<FakeConsoleInput>(Application.Drive);
|
|
//Assert.IsType<FakeDriver> (Application.Driver);
|
|
Application.ResetState ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Init_KeyBindings_Are_Not_Reset ()
|
|
{
|
|
Debug.Assert (!IsEnabled);
|
|
|
|
try
|
|
{
|
|
// arrange
|
|
ThrowOnJsonErrors = true;
|
|
|
|
Application.QuitKey = Key.Q;
|
|
Assert.Equal (Key.Q, Application.QuitKey);
|
|
|
|
Application.Init (new FakeDriver ());
|
|
|
|
Assert.Equal (Key.Q, Application.QuitKey);
|
|
}
|
|
finally
|
|
{
|
|
Application.ResetState (false);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown (verifyShutdown: true)]
|
|
public void Internal_Properties_Correct ()
|
|
{
|
|
Assert.True (Application.Initialized);
|
|
Assert.Null (Application.Top);
|
|
RunState rs = Application.Begin (new ());
|
|
Assert.Equal (Application.Top, rs.Toplevel);
|
|
Assert.Null (Application.MouseGrabHandler.MouseGrabView); // public
|
|
Application.Top!.Dispose ();
|
|
}
|
|
|
|
// Invoke Tests
|
|
// TODO: Test with threading scenarios
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Invoke_Adds_Idle ()
|
|
{
|
|
var top = new Toplevel ();
|
|
RunState rs = Application.Begin (top);
|
|
var firstIteration = false;
|
|
|
|
var actionCalled = 0;
|
|
Application.Invoke (() => { actionCalled++; });
|
|
Application.RunIteration (ref rs, firstIteration);
|
|
Assert.Equal (1, actionCalled);
|
|
top.Dispose ();
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Run_Iteration_Fires ()
|
|
{
|
|
var iteration = 0;
|
|
|
|
Application.Init (null, driverName: "fake");
|
|
|
|
Application.Iteration += Application_Iteration;
|
|
Application.Run<Toplevel> ().Dispose ();
|
|
|
|
Assert.Equal (1, iteration);
|
|
Application.Shutdown ();
|
|
|
|
return;
|
|
|
|
void Application_Iteration (object sender, IterationEventArgs e)
|
|
{
|
|
if (iteration > 0)
|
|
{
|
|
Assert.Fail ();
|
|
}
|
|
|
|
iteration++;
|
|
Application.RequestStop ();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Screen_Size_Changes ()
|
|
{
|
|
var driver = Application.Driver;
|
|
|
|
AutoInitShutdownAttribute.FakeResize (new Size (80,25));
|
|
|
|
Assert.Equal (new (0, 0, 80, 25), driver.Screen);
|
|
Assert.Equal (new (0, 0, 80, 25), Application.Screen);
|
|
|
|
// TODO: Should not be possible to manually change these at whim!
|
|
driver.Cols = 100;
|
|
driver.Rows = 30;
|
|
// IConsoleDriver.Screen isn't assignable
|
|
//driver.Screen = new (0, 0, driver.Cols, Rows);
|
|
|
|
AutoInitShutdownAttribute.FakeResize (new Size (100, 30));
|
|
|
|
Assert.Equal (new (0, 0, 100, 30), driver.Screen);
|
|
|
|
// Assert does not make sense
|
|
// Assert.NotEqual (new (0, 0, 100, 30), Application.Screen);
|
|
// Assert.Equal (new (0, 0, 80, 25), Application.Screen);
|
|
Application.Screen = new (0, 0, driver.Cols, driver.Rows);
|
|
Assert.Equal (new (0, 0, 100, 30), driver.Screen);
|
|
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
public void InitState_Throws_If_Driver_Is_Null ()
|
|
{
|
|
Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
|
|
}
|
|
|
|
|
|
#region RunTests
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Run_T_After_InitWithDriver_with_TopLevel_Does_Not_Throws ()
|
|
{
|
|
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)
|
|
// Using another type not derived from Toplevel will throws at compile time
|
|
Application.Run<Window> ();
|
|
Assert.True (Application.Top is Window);
|
|
|
|
Application.Top!.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
public void Run_T_After_InitWithDriver_with_TopLevel_and_Driver_Does_Not_Throws ()
|
|
{
|
|
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)
|
|
// Using another type not derived from Toplevel will throws at compile time
|
|
Application.Run<Window> (null, new FakeDriver ());
|
|
Assert.True (Application.Top is Window);
|
|
|
|
Application.Top!.Dispose ();
|
|
|
|
// Run<Toplevel> when already initialized or not with a Driver will not throw (because Dialog is derived from Toplevel)
|
|
Application.Run<Dialog> (null, new FakeDriver ());
|
|
Assert.True (Application.Top is Dialog);
|
|
|
|
Application.Top!.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
[TestRespondersDisposed]
|
|
public void Run_T_After_Init_Does_Not_Disposes_Application_Top ()
|
|
{
|
|
|
|
// Init doesn't create a Toplevel and assigned it to Application.Top
|
|
// but Begin does
|
|
var initTop = new Toplevel ();
|
|
|
|
Application.Iteration += (s, a) =>
|
|
{
|
|
Assert.NotEqual (initTop, Application.Top);
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (initTop.WasDisposed);
|
|
#endif
|
|
Application.RequestStop ();
|
|
};
|
|
|
|
Application.Run<Toplevel> ();
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (initTop.WasDisposed);
|
|
initTop.Dispose ();
|
|
Assert.True (initTop.WasDisposed);
|
|
#endif
|
|
Application.Top!.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[TestRespondersDisposed]
|
|
[AutoInitShutdown]
|
|
public void Run_T_After_InitWithDriver_with_TestTopLevel_DoesNotThrow ()
|
|
{
|
|
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 ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[TestRespondersDisposed]
|
|
public void Run_T_After_InitNullDriver_with_TestTopLevel_DoesNotThrow ()
|
|
{
|
|
Application.ForceDriver = "FakeDriver";
|
|
|
|
var a = new AutoInitShutdownAttribute ();
|
|
a.Before (null);
|
|
|
|
Application.Iteration += (s, a) => { Application.RequestStop (); };
|
|
|
|
// Init has been called, selecting FakeDriver; we're passing no driver to Run<TestTopLevel>. Should be fine.
|
|
Application.Run<Toplevel> ();
|
|
|
|
Application.Top!.Dispose ();
|
|
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 ()
|
|
{
|
|
Application.Driver = null;
|
|
|
|
// Init has been called, but Driver has been set to null. Bad.
|
|
Assert.Throws<InvalidOperationException> (() => Application.Run<Toplevel> ());
|
|
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact(Skip = "FakeDriver is not allowed, use AutoInitShutdown attribute instead")]
|
|
[TestRespondersDisposed]
|
|
public void Run_T_NoInit_DoesNotThrow ()
|
|
{
|
|
Application.ForceDriver = "FakeDriver";
|
|
|
|
Application.Iteration += (s, a) => { Application.RequestStop (); };
|
|
|
|
Application.Run<Toplevel> ();
|
|
Assert.Equal (typeof (FakeDriver), Application.Driver?.GetType ());
|
|
|
|
Application.Top!.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[TestRespondersDisposed]
|
|
public void Run_T_NoInit_WithDriver_DoesNotThrow ()
|
|
{
|
|
Application.Iteration += (s, a) => { Application.RequestStop (); };
|
|
|
|
// Init has NOT been called and we're passing a valid driver to Run<TestTopLevel>. This is ok.
|
|
Application.Run<Toplevel> (null, new FakeDriver ());
|
|
|
|
Application.Top!.Dispose ();
|
|
Application.Shutdown ();
|
|
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[TestRespondersDisposed]
|
|
[AutoInitShutdown]
|
|
public void Run_RequestStop_Stops ()
|
|
{
|
|
var top = new Toplevel ();
|
|
RunState rs = Application.Begin (top);
|
|
Assert.NotNull (rs);
|
|
|
|
Application.Iteration += (s, a) => { Application.RequestStop (); };
|
|
|
|
Application.Run (top);
|
|
|
|
top.Dispose ();
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Run_Sets_Running_True ()
|
|
{
|
|
var top = new Toplevel ();
|
|
RunState rs = Application.Begin (top);
|
|
Assert.NotNull (rs);
|
|
|
|
Application.Iteration += (s, a) =>
|
|
{
|
|
Assert.True (top.Running);
|
|
top.Running = false;
|
|
};
|
|
|
|
Application.Run (top);
|
|
|
|
top.Dispose ();
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[TestRespondersDisposed]
|
|
[AutoInitShutdown]
|
|
public void Run_RunningFalse_Stops ()
|
|
{
|
|
var top = new Toplevel ();
|
|
RunState rs = Application.Begin (top);
|
|
Assert.NotNull (rs);
|
|
|
|
Application.Iteration += (s, a) => { top.Running = false; };
|
|
|
|
Application.Run (top);
|
|
|
|
top.Dispose ();
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
Assert.Null (Application.MainLoop);
|
|
Assert.Null (Application.Driver);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
[TestRespondersDisposed]
|
|
public void Run_Loaded_Ready_Unloaded_Events ()
|
|
{
|
|
Toplevel top = new ();
|
|
var count = 0;
|
|
top.Loaded += (s, e) => count++;
|
|
top.Ready += (s, e) => count++;
|
|
top.Unloaded += (s, e) => count++;
|
|
Application.Iteration += (s, a) => Application.RequestStop ();
|
|
Application.Run (top);
|
|
top.Dispose ();
|
|
Application.Shutdown ();
|
|
Assert.Equal (3, count);
|
|
}
|
|
|
|
// TODO: All Toplevel layout tests should be moved to ToplevelTests.cs
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Run_A_Modal_Toplevel_Refresh_Background_On_Moving ()
|
|
{
|
|
// 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
|
|
};
|
|
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.
|
|
Assert.Equal (new (0, 0), w.Frame.Location);
|
|
|
|
Application.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed });
|
|
Assert.Equal (w.Border, Application.MouseGrabHandler.MouseGrabView);
|
|
Assert.Equal (new (0, 0), w.Frame.Location);
|
|
|
|
// Move down and to the right.
|
|
Application.RaiseMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition });
|
|
Assert.Equal (new (1, 1), w.Frame.Location);
|
|
|
|
Application.End (rs);
|
|
w.Dispose ();
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void End_Does_Not_Dispose ()
|
|
{
|
|
var top = new Toplevel ();
|
|
|
|
Window w = new ();
|
|
w.Ready += (s, e) => Application.RequestStop (); // Causes `End` to be called
|
|
Application.Run (w);
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (w.WasDisposed);
|
|
#endif
|
|
|
|
Assert.NotNull (w);
|
|
Assert.Equal (string.Empty, w.Title); // Valid - w has not been disposed. The user may want to run it again
|
|
Assert.NotNull (Application.Top);
|
|
Assert.Equal (w, Application.Top);
|
|
Assert.NotEqual (top, Application.Top);
|
|
|
|
Application.Run (w); // Valid - w has not been disposed.
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (w.WasDisposed);
|
|
Exception exception = Record.Exception (Application.Shutdown); // Invalid - w has not been disposed.
|
|
Assert.NotNull (exception);
|
|
|
|
w.Dispose ();
|
|
Assert.True (w.WasDisposed);
|
|
|
|
//exception = Record.Exception (
|
|
// () => Application.Run (
|
|
// w)); // Invalid - w has been disposed. Run it in debug mode will throw, otherwise the user may want to run it again
|
|
//Assert.NotNull (exception);
|
|
|
|
// TODO: Re-enable this when we are done debug logging of ctx.Source.Title in RaiseSelecting
|
|
//exception = Record.Exception (() => Assert.Equal (string.Empty, w.Title)); // Invalid - w has been disposed and cannot be accessed
|
|
//Assert.NotNull (exception);
|
|
//exception = Record.Exception (() => w.Title = "NewTitle"); // Invalid - w has been disposed and cannot be accessed
|
|
//Assert.NotNull (exception);
|
|
#endif
|
|
Application.Shutdown ();
|
|
Assert.NotNull (w);
|
|
Assert.NotNull (top);
|
|
Assert.Null (Application.Top);
|
|
}
|
|
|
|
[Fact]
|
|
public void Run_Creates_Top_Without_Init ()
|
|
{
|
|
var driver = new FakeDriver ();
|
|
|
|
Assert.Null (Application.Top);
|
|
|
|
Application.Iteration += (s, e) =>
|
|
{
|
|
Assert.NotNull (Application.Top);
|
|
Application.RequestStop ();
|
|
};
|
|
Toplevel top = Application.Run (null, driver);
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.Equal (top, Application.Top);
|
|
Assert.False (top.WasDisposed);
|
|
Exception exception = Record.Exception (Application.Shutdown);
|
|
Assert.NotNull (exception);
|
|
Assert.False (top.WasDisposed);
|
|
#endif
|
|
|
|
// It's up to caller to dispose it
|
|
top.Dispose ();
|
|
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.True (top.WasDisposed);
|
|
#endif
|
|
Assert.NotNull (Application.Top);
|
|
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
}
|
|
|
|
[Fact]
|
|
public void Run_T_Creates_Top_Without_Init ()
|
|
{
|
|
var driver = new FakeDriver ();
|
|
|
|
Assert.Null (Application.Top);
|
|
|
|
Application.Iteration += (s, e) =>
|
|
{
|
|
Assert.NotNull (Application.Top);
|
|
Application.RequestStop ();
|
|
};
|
|
Application.Run<Toplevel> (null, driver);
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (Application.Top!.WasDisposed);
|
|
Exception exception = Record.Exception (Application.Shutdown);
|
|
Assert.NotNull (exception);
|
|
Assert.False (Application.Top!.WasDisposed);
|
|
|
|
// It's up to caller to dispose it
|
|
Application.Top!.Dispose ();
|
|
Assert.True (Application.Top!.WasDisposed);
|
|
#endif
|
|
Assert.NotNull (Application.Top);
|
|
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
}
|
|
|
|
[Fact]
|
|
public void Run_t_Does_Not_Creates_Top_Without_Init ()
|
|
{
|
|
// When a Toplevel is created it must already have all the Application configuration loaded
|
|
// This is only possible by two ways:
|
|
// 1 - Using Application.Init first
|
|
// 2 - Using Application.Run() or Application.Run<T>()
|
|
// The Application.Run(new(Toplevel)) must always call Application.Init() first because
|
|
// the new(Toplevel) may be a derived class that is possible using Application static
|
|
// properties that is only available after the Application.Init was called
|
|
var driver = new FakeDriver ();
|
|
|
|
Assert.Null (Application.Top);
|
|
|
|
Assert.Throws<NotInitializedException> (() => Application.Run (new Toplevel ()));
|
|
|
|
Application.Init (driver);
|
|
|
|
Application.Iteration += (s, e) =>
|
|
{
|
|
Assert.NotNull (Application.Top);
|
|
Application.RequestStop ();
|
|
};
|
|
Application.Run (new Toplevel ());
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (Application.Top!.WasDisposed);
|
|
Exception exception = Record.Exception (Application.Shutdown);
|
|
Assert.NotNull (exception);
|
|
Assert.False (Application.Top!.WasDisposed);
|
|
|
|
// It's up to caller to dispose it
|
|
Application.Top!.Dispose ();
|
|
Assert.True (Application.Top!.WasDisposed);
|
|
#endif
|
|
Assert.NotNull (Application.Top);
|
|
|
|
Application.Shutdown ();
|
|
Assert.Null (Application.Top);
|
|
}
|
|
|
|
private class TestToplevel : Toplevel { }
|
|
|
|
private readonly object _forceDriverLock = new ();
|
|
|
|
/*
|
|
[Theory]
|
|
|
|
// 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>))]
|
|
|
|
// FakeDriver is not allowed, use AutoInitShutdown attribute instead
|
|
//[InlineData ("FakeDriver", typeof (FakeDriver))]
|
|
//[InlineData ("DotNetDriver", typeof (DotNetDriver))]
|
|
//[InlineData ("WindowsDriver", typeof (WindowsDriver))]
|
|
//[InlineData ("UnixDriver", typeof (UnixDriver))]
|
|
public void Run_T_Call_Init_ForceDriver_Should_Pick_Correct_Driver (string driverName, Type expectedType)
|
|
{
|
|
Assert.True (ConsoleDriver.RunningUnitTests);
|
|
|
|
var result = false;
|
|
|
|
lock (_forceDriverLock)
|
|
{
|
|
Task.Run (() =>
|
|
{
|
|
while (!Application.Initialized)
|
|
{
|
|
Task.Delay (300).Wait ();
|
|
}
|
|
})
|
|
.ContinueWith (
|
|
(t, _) =>
|
|
{
|
|
// no longer loading
|
|
Assert.True (Application.Initialized);
|
|
|
|
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 ();
|
|
Application.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 ();
|
|
Application.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 ();
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
// TODO: Add tests for Run that test errorHandler
|
|
|
|
#endregion
|
|
|
|
#region ShutdownTests
|
|
|
|
[Fact]
|
|
public async Task Shutdown_Allows_Async ()
|
|
{
|
|
var isCompletedSuccessfully = false;
|
|
|
|
async Task TaskWithAsyncContinuation ()
|
|
{
|
|
await Task.Yield ();
|
|
await Task.Yield ();
|
|
|
|
isCompletedSuccessfully = true;
|
|
}
|
|
|
|
Application.Shutdown ();
|
|
|
|
Assert.False (isCompletedSuccessfully);
|
|
await TaskWithAsyncContinuation ();
|
|
Thread.Sleep (100);
|
|
Assert.True (isCompletedSuccessfully);
|
|
}
|
|
|
|
[Fact]
|
|
public void Shutdown_Resets_SyncContext ()
|
|
{
|
|
Application.Shutdown ();
|
|
Assert.Null (SynchronizationContext.Current);
|
|
}
|
|
|
|
#endregion
|
|
}
|