diff --git a/UICatalog/Scenarios/BackgroundWorkerCollection.cs b/UICatalog/Scenarios/BackgroundWorkerCollection.cs index ec73bcf76..b351d19ba 100644 --- a/UICatalog/Scenarios/BackgroundWorkerCollection.cs +++ b/UICatalog/Scenarios/BackgroundWorkerCollection.cs @@ -22,9 +22,9 @@ namespace UICatalog.Scenarios { // BUGBUG: work around Issue #520: Ensuring the `Toplevel` created by `Init` gets Disposed... // For Scenarios that want to use `Applciation.Run` to create a new Toplevel: // Override `Run` and call `Application.Top.Dispose` before calling `Application.Run`. - if (Application.Top != null) { - Application.Top.Dispose (); - } + //if (Application.Top != null) { + // Application.Top.Dispose (); + //} Application.Run (); } diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index c5f896b77..d4a7594d5 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -90,14 +90,14 @@ namespace UICatalog { _aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui"); Scenario scenario; - while ((scenario = SelectScenario ()) != null) { + while ((scenario = RunUICatalogTopLevel ()) != null) { VerifyObjectsWereDisposed (); scenario.Init (_colorScheme); scenario.Setup (); scenario.Run (); // This call to Application.Shutdown brackets the Application.Init call - // made by Scenario.Init() + // made by Scenario.Init() above Application.Shutdown (); VerifyObjectsWereDisposed (); @@ -105,6 +105,24 @@ namespace UICatalog { VerifyObjectsWereDisposed (); } + /// + /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the + /// UI Catalog main app UI is killed and the Scenario is run as though it were Application.Top. + /// When the Scenario exits, this function exits. + /// + /// + static Scenario RunUICatalogTopLevel () + { + Application.UseSystemConsole = _useSystemConsole; + + // Run UI Catalog UI. When it exits, if _selectedScenario is != null then + // a Scenario was selected. Otherwise, the user wants to exit UI Catalog. + Application.Run (); + Application.Shutdown (); + + return _selectedScenario; + } + static List _scenarios; static List _categories; static int _nameColumnWidth; @@ -533,25 +551,6 @@ namespace UICatalog { #endif } - /// - /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the - /// UI Catalog main app UI is killed and the Scenario is run as though it were Application.Top. - /// When the Scenario exits, this function exits. - /// - /// - static Scenario SelectScenario () - { - Application.UseSystemConsole = _useSystemConsole; - //var top = new UICatalogTopLevel (); - - // Run UI Catalog UI. When it exits, if _selectedScenario is != null then - // a Scenario was selected. Otherwise, the user wants to exit UI Catalog. - Application.Run (); - Application.Shutdown (); - - return _selectedScenario; - } - static void OpenUrl (string url) { try { diff --git a/UnitTests/ApplicationTests.cs b/UnitTests/ApplicationTests.cs index c10aaf211..01f938d48 100644 --- a/UnitTests/ApplicationTests.cs +++ b/UnitTests/ApplicationTests.cs @@ -70,7 +70,7 @@ namespace Terminal.Gui.Core { Assert.Equal (80, Application.Driver.Cols); Assert.Equal (25, Application.Driver.Rows); - // Because of #520, the Toplevel created by Application.Init is not disposed by Shutdown + // BUGBUG: Because of #520, the Toplevel created by Application.Init is not disposed by Shutdown // So we need to dispose it manually Application.Top.Dispose (); @@ -87,6 +87,20 @@ namespace Terminal.Gui.Core { } } + [Fact] + public void Init_Shutdown_Toplevel_Not_Disposed () + { + Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + + Application.Shutdown (); + + Assert.Single (Responder.Instances); + foreach (var inst in Responder.Instances) { + // BUGBUG: Because of #520, the Toplevel created by Application.Init is not disposed by Shutdown + Assert.False (inst.WasDisposed); + } + } + [Fact] public void Init_Unbalanced_Throwss () { diff --git a/UnitTests/MdiTests.cs b/UnitTests/MdiTests.cs index 4f4f13e11..34b8e9ddb 100644 --- a/UnitTests/MdiTests.cs +++ b/UnitTests/MdiTests.cs @@ -17,7 +17,44 @@ namespace Terminal.Gui.Core { Application.RunState.Instances.Clear (); #endif } - + + + [Fact] + public void Dispose_Toplevel_IsMdiContainer_False_With_Begin_End () + { + Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + + var top = new Toplevel (); + var rs = Application.Begin (top); + Application.End (rs); + + Application.Shutdown (); + + Assert.Equal (2, Responder.Instances.Count); + // BUGBUG: Because of #520, the Toplevel created by Application.Init is not disposed by Shutdown + // Change this to True once fixed. + Assert.False (Responder.Instances [0].WasDisposed); + Assert.True(Responder.Instances [1].WasDisposed); + } + + [Fact] + public void Dispose_Toplevel_IsMdiContainer_True_With_Begin () + { + Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))); + + var mdi = new Toplevel { IsMdiContainer = true }; + var rs = Application.Begin (mdi); + Application.End (rs); + + Application.Shutdown (); + + Assert.Equal (2, Responder.Instances.Count); + // BUGBUG: Because of #520, the Toplevel created by Application.Init is not disposed by Shutdown + // Change this to True once fixed. + Assert.False (Responder.Instances [0].WasDisposed); + Assert.True (Responder.Instances [1].WasDisposed); + } + [Fact, AutoInitShutdown] public void Application_RequestStop_With_Params_On_A_Not_MdiContainer_Always_Use_Application_Current () {