Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ public override void RequestStop (Toplevel? top)
{
Logging.Logger.LogInformation ($"RequestStop '{top}'");

top ??= Application.Top;

if (top == null)
{
return;
}

var ev = new ToplevelClosingEventArgs (top);
top.OnClosing (ev);

if (ev.Cancel)
{
return;
}

top.Running = false;

// TODO: This definition of stop seems sketchy
Application.TopLevels.TryPop (out _);

Expand All @@ -205,6 +222,9 @@ public override void RequestStop (Toplevel? top)
{
Application.Top = null;
}

// Notify that it is closed
top.OnClosed (top);
}

/// <inheritdoc/>
Expand Down
47 changes: 47 additions & 0 deletions Tests/UnitTests/ConsoleDrivers/V2/ApplicationV2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,53 @@ public void Test_InitRunShutdown_Generic_IdleForExit ()

ApplicationImpl.ChangeInstance (orig);
}

[Fact]
public void Test_V2_ClosingRaised ()
{
var orig = ApplicationImpl.Instance;

var v2 = NewApplicationV2 ();
ApplicationImpl.ChangeInstance (v2);

v2.Init ();

int closing=0;
int closed = 0;
var t=new Toplevel ();
t.Closing
+= (_, a) =>
{
// Cancel the first time
if (closing==0)
{
a.Cancel = true;
}
closing++;
Assert.Same(t,a.RequestingTop);
};

t.Closed
+= (_, a) =>
{
closed++;
Assert.Same (t, a.Toplevel);
};

v2.AddIdle (IdleExit);

// Blocks until the timeout call is hit

v2.Run (t);

Assert.Null (Application.Top);
v2.Shutdown ();

ApplicationImpl.ChangeInstance (orig);

Assert.Equal (2,closing);
Assert.Equal (1, closed);
}
private bool IdleExit ()
{
if (Application.Top != null)
Expand Down