Files
Terminal.Gui/UnitTests/Application/RunStateTests.cs
usr c7942ae3bb Fix issue where the TestViewsDisposeCorrectly was not doing what it was supposed to do (#2964)
* add Disposal Test and fix an ssue where the CopyClipboard test was failing

* Update ViewDisposalTest.cs

* Update ViewDisposalTest.cs: Some Formatting, and adding code comments.

* Fix ViewDisposalTests (Wasn't working the way it was supposed to)

* update test

* update test

* update test

* try to fix as many conflicts as possible

* make test output prettier

* fix formatting

* Fix Subviews not being empty after disposing on all views.

* The fail cause was Application.Top not being disposed.

* Fix others containers that weren't being removed.

* Revert "The fail cause was Application.Top not being disposed."

This reverts commit 0c2183ed9e.

* Application.Top isn't null and need disposing.

* Fixes #2985. Application.RunState must be responsible for dispose the Toplevel property.

* Change the unit test with ans without Application.Shutdown method.

* Update ViewDisposeTests to actually check wether ALL views have been disposed (not just container)

* small additional check just to be safe

* Update ViewDisposalTest.cs: Formatting

* Update ViewDisposalTest.cs: Minor change to re-trigger Action

TestVKPacket is acting up again. Maybe the test is running async and is receiving scan codes from other instances?

---------

Co-authored-by: John Züchler <john.zuechler@eks-intec.de>
Co-authored-by: BDisp <bd.bdisp@gmail.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
2023-11-15 09:22:22 -07:00

151 lines
3.4 KiB
C#

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Terminal.Gui;
using Xunit;
// Alias Console to MockConsole so we don't accidentally use Console
using Console = Terminal.Gui.FakeConsole;
namespace Terminal.Gui.ApplicationTests {
/// <summary>
/// These tests focus on Application.RunState and the various ways it can be changed.
/// </summary>
public class RunStateTests {
public RunStateTests ()
{
#if DEBUG_IDISPOSABLE
Responder.Instances.Clear ();
Application.RunState.Instances.Clear ();
#endif
}
[Fact]
public void New_Creates_RunState ()
{
var rs = new Application.RunState (null);
Assert.Null (rs.Toplevel);
var top = new Toplevel ();
rs = new Application.RunState (top);
Assert.Equal (top, rs.Toplevel);
}
[Fact]
public void Dispose_Cleans_Up_RunState ()
{
var rs = new Application.RunState (null);
Assert.NotNull (rs);
// Should not throw because Toplevel was null
rs.Dispose ();
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
#endif
var top = new Toplevel ();
rs = new Application.RunState (top);
Assert.NotNull (rs);
// Should not throw because Toplevel was cleaned up
var exception = Record.Exception (() => rs.Dispose ());
Assert.Null (exception);
Assert.Null (rs.Toplevel);
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
Assert.True (top.WasDisposed);
#endif
}
void Init ()
{
Application.Init (new FakeDriver ());
Assert.NotNull (Application.Driver);
Assert.NotNull (Application.MainLoop);
Assert.NotNull (SynchronizationContext.Current);
}
void Shutdown ()
{
Application.Shutdown ();
#if DEBUG_IDISPOSABLE
// Validate there are no outstanding RunState-based instances left
foreach (var inst in Application.RunState.Instances) Assert.True (inst.WasDisposed);
#endif
}
[Fact]
public void Begin_End_Cleans_Up_RunState ()
{
// Setup Mock driver
Init ();
// Test null Toplevel
Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
var top = new Toplevel ();
var rs = Application.Begin (top);
Assert.NotNull (rs);
Assert.Equal (top, Application.Current);
Application.End (rs);
Assert.Null (Application.Current);
Assert.Null (Application.Top);
Assert.NotNull (Application.MainLoop);
Assert.NotNull (Application.Driver);
Shutdown ();
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
#endif
Assert.Null (Application.MainLoop);
Assert.Null (Application.Driver);
}
WeakReference CreateToplevelInstance ()
{
// Setup Mock driver
Init ();
var top = new Toplevel ();
var rs = Application.Begin (top);
Assert.NotNull (rs);
Assert.Equal (top, Application.Current);
Assert.Equal (top, Application.Top);
Application.End (rs);
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
Assert.True (top.WasDisposed);
#endif
Assert.Null (Application.Current);
Assert.Null (Application.Top);
Assert.NotNull (top);
Assert.NotNull (Application.MainLoop);
Assert.NotNull (Application.Driver);
return new WeakReference (top, true);
}
[Fact]
public void Begin_End_Cleans_Up_RunState_Without_Shutdown ()
{
WeakReference wrInstance = CreateToplevelInstance ();
GC.Collect ();
GC.WaitForPendingFinalizers ();
Assert.False (wrInstance.IsAlive);
// Shutdown Mock driver
Shutdown ();
Assert.Null (Application.MainLoop);
Assert.Null (Application.Driver);
}
}
}