Files
Terminal.Gui/Tests/UnitTests/AutoInitShutdownAttribute.cs
Tig e311cad7ac Partial on #2975 - Replaces Menu v1 in many places with v2 (#4040)
* touching publish.yml

* Fixed UICatalog bugs. Added fluent tests.

* marked v1 menu stuff as obsolte

* Tweaks.
Added View.GetSubMenus<type>().

* fixed unit tests

* general messing around

* general messing around

* Playing with Fluent

* ColorScheme tweaks

* WIP: ColorScheme tweaks

* Playing with Fluent

* Merged from laptop2

* Hacky-ish fixes to:
- #4016
- #4014

* Fixed Region bug preventing menus without borders from working

* Tweaks

* Fixed a bunch of CM issues

* Fixed OoptionSelector

* ip

* FixedCM issues

* Fixed CM issues2

* Revert "FixedCM issues"

This reverts commit dd6c6a70a3.

* Reverted stuff

* Found and fixed bug in AllViews_Center_Properly

* Fixed CM issues2

* removed menuv2 onapplied.
Changed how UICatalog Applys CM

* changed test time out to see if it helkps with ubuntu fails

* reset app on fail?

* back to 1500ms

* Made StatusBar nullable.

* Code Cleanup.

* HexEditor Code Cleanup.

* HexEditor Code Cleanup.

* Back to 3000ms. Sigh.

* Trying different logic

* Trying different logic2

* Fixed potential crash in runlop

* Fixed potential crash in runlop2

* Tweaked Spinner stuff

* Removed TabView from TextEffects scenario. Not needed and possible culprit.

* back to 2000ms

* WIP: Revamping menu scenarios

* Menu Scenario refinements.
Fixed a few bugs.
Code cleanup.

* fixed unit test

* Fixed warnings

* Fixed warnings2

* Fixed File.Exit

* WIP: Dealing with QuitKey struggles

* WIP: Dealing with QuitKey struggles 2

* WIP: Dealing with QuitKey struggles 3

* Fixed ListView collection nav bug

* Fixed a bunch of menu stuff.
Fixed Appv2 stuff.

* Lots of refactoring and fixing

* Lots of unit test issues

* Fixed DebugIDisposable issues

* Fixed release build issue

* Fixed release build issue 2

* DebugIDisposable -> EnableDebugIDisposableAsserts and more

* DebugIDisposable -> EnableDebugIDisposableAsserts and more 2

* Fixed Menus scenario - context menu

* Added @bdisp suggested assert. Commented it out as it breaks tests.

* Code cleanup

* Fixed disposed but

* Fixed UICatalog exit

* Fixed Unit test I broke.
Added 'Minimal' Theme that turns off all borders etc...
2025-05-29 14:08:47 -06:00

140 lines
5.4 KiB
C#

using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
namespace UnitTests;
/// <summary>
/// Enables test functions annotated with the [AutoInitShutdown] attribute to
/// automatically call Application.Init at start of the test and Application.Shutdown after the
/// test exits.
/// This is necessary because a) Application is a singleton and Init/Shutdown must be called
/// as a pair, and b) all unit test functions should be atomic..
/// </summary>
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
{
/// <summary>
/// Initializes a [AutoInitShutdown] attribute, which determines if/how Application.Init and Application.Shutdown
/// are automatically called Before/After a test runs.
/// </summary>
/// <param name="autoInit">If true, Application.Init will be called Before the test runs.</param>
/// <param name="consoleDriverType">
/// Determines which IConsoleDriver (FakeDriver, WindowsDriver, CursesDriver, NetDriver)
/// will be used when Application.Init is called. If null FakeDriver will be used. Only valid if
/// <paramref name="autoInit"/> is true.
/// </param>
/// <param name="useFakeClipboard">
/// If true, will force the use of <see cref="FakeDriver.FakeClipboard"/>. Only valid if
/// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
/// </param>
/// <param name="fakeClipboardAlwaysThrowsNotSupportedException">
/// Only valid if <paramref name="autoInit"/> is true. Only
/// valid if <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
/// </param>
/// <param name="fakeClipboardIsSupportedAlwaysTrue">
/// Only valid if <paramref name="autoInit"/> is true. Only valid if
/// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
/// </param>
/// <param name="configLocation">Determines what config file locations <see cref="ConfigurationManager"/> will load from.</param>
/// <param name="verifyShutdown">If true and <see cref="Application.Initialized"/> is true, the test will fail.</param>
public AutoInitShutdownAttribute (
bool autoInit = true,
Type consoleDriverType = null,
bool useFakeClipboard = true,
bool fakeClipboardAlwaysThrowsNotSupportedException = false,
bool fakeClipboardIsSupportedAlwaysTrue = false,
ConfigLocations configLocation = ConfigLocations.Default, // DefaultOnly is the default for tests
bool verifyShutdown = false
)
{
AutoInit = autoInit;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
_driverType = consoleDriverType ?? typeof (FakeDriver);
FakeDriver.FakeBehaviors.UseFakeClipboard = useFakeClipboard;
FakeDriver.FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException =
fakeClipboardAlwaysThrowsNotSupportedException;
FakeDriver.FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
ConfigurationManager.Locations = configLocation;
_verifyShutdown = verifyShutdown;
}
private readonly bool _verifyShutdown;
private readonly Type _driverType;
public override void After (MethodInfo methodUnderTest)
{
Debug.WriteLine ($"After: {methodUnderTest.Name}");
// Turn off diagnostic flags in case some test left them on
View.Diagnostics = ViewDiagnosticFlags.Off;
if (AutoInit)
{
// try
{
if (!_verifyShutdown)
{
Application.ResetState (ignoreDisposed: true);
}
Application.Shutdown ();
#if DEBUG_IDISPOSABLE
if (View.Instances.Count == 0)
{
Assert.Empty (View.Instances);
}
else
{
View.Instances.Clear ();
}
#endif
}
//catch (Exception e)
//{
// Assert.Fail ($"Application.Shutdown threw an exception after the test exited: {e}");
//}
//finally
{
#if DEBUG_IDISPOSABLE
View.Instances.Clear ();
Application.ResetState (true);
#endif
}
}
// Reset to defaults
ConfigurationManager.Locations = ConfigLocations.Default;
ConfigurationManager.Reset ();
// Enable subsequent tests that call Init to get all config files (the default).
//Locations = ConfigLocations.All;
}
public override void Before (MethodInfo methodUnderTest)
{
Debug.WriteLine ($"Before: {methodUnderTest.Name}");
if (AutoInit)
{
#if DEBUG_IDISPOSABLE
View.EnableDebugIDisposableAsserts = true;
// Clear out any lingering Responder instances from previous tests
if (View.Instances.Count == 0)
{
Assert.Empty (View.Instances);
}
else
{
View.Instances.Clear ();
}
#endif
Application.Init ((IConsoleDriver)Activator.CreateInstance (_driverType));
}
}
private bool AutoInit { get; }
}