浏览代码

Added @bdisp's suggested tests

Charlie Kindel 2 年之前
父节点
当前提交
857e123c65
共有 4 个文件被更改,包括 76 次插入26 次删除
  1. 3 3
      UICatalog/Scenarios/BackgroundWorkerCollection.cs
  2. 20 21
      UICatalog/UICatalog.cs
  3. 15 1
      UnitTests/ApplicationTests.cs
  4. 38 1
      UnitTests/MdiTests.cs

+ 3 - 3
UICatalog/Scenarios/BackgroundWorkerCollection.cs

@@ -22,9 +22,9 @@ namespace UICatalog.Scenarios {
 			// BUGBUG: work around Issue #520: Ensuring the `Toplevel` created by `Init` gets Disposed...
 			// BUGBUG: work around Issue #520: Ensuring the `Toplevel` created by `Init` gets Disposed...
 			// For Scenarios that want to use `Applciation.Run<T>` to create a new Toplevel: 
 			// For Scenarios that want to use `Applciation.Run<T>` to create a new Toplevel: 
 			// Override `Run` and call `Application.Top.Dispose` before calling `Application.Run<T>`.
 			// Override `Run` and call `Application.Top.Dispose` before calling `Application.Run<T>`.
-			if (Application.Top != null) {
-				Application.Top.Dispose ();
-			}
+			//if (Application.Top != null) {
+			//	Application.Top.Dispose ();
+			//}
 
 
 			Application.Run<MdiMain> ();
 			Application.Run<MdiMain> ();
 		}
 		}

+ 20 - 21
UICatalog/UICatalog.cs

@@ -90,14 +90,14 @@ namespace UICatalog {
 			_aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
 			_aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
 
 
 			Scenario scenario;
 			Scenario scenario;
-			while ((scenario = SelectScenario ()) != null) {
+			while ((scenario = RunUICatalogTopLevel ()) != null) {
 				VerifyObjectsWereDisposed ();
 				VerifyObjectsWereDisposed ();
 				scenario.Init (_colorScheme);
 				scenario.Init (_colorScheme);
 				scenario.Setup ();
 				scenario.Setup ();
 				scenario.Run ();
 				scenario.Run ();
 
 
 				// This call to Application.Shutdown brackets the Application.Init call
 				// This call to Application.Shutdown brackets the Application.Init call
-				// made by Scenario.Init()
+				// made by Scenario.Init() above
 				Application.Shutdown ();
 				Application.Shutdown ();
 
 
 				VerifyObjectsWereDisposed ();
 				VerifyObjectsWereDisposed ();
@@ -105,6 +105,24 @@ namespace UICatalog {
 			VerifyObjectsWereDisposed ();
 			VerifyObjectsWereDisposed ();
 		}
 		}
 
 
+		/// <summary>
+		/// 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.
+		/// </summary>
+		/// <returns></returns>
+		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<UICatalogTopLevel> ();
+			Application.Shutdown ();
+
+			return _selectedScenario;
+		}
+
 		static List<Scenario> _scenarios;
 		static List<Scenario> _scenarios;
 		static List<string> _categories;
 		static List<string> _categories;
 		static int _nameColumnWidth;
 		static int _nameColumnWidth;
@@ -533,25 +551,6 @@ namespace UICatalog {
 #endif
 #endif
 		}
 		}
 
 
-		/// <summary>
-		/// 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.
-		/// </summary>
-		/// <returns></returns>
-		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<UICatalogTopLevel> ();
-			Application.Shutdown ();
-
-			return _selectedScenario;
-		}
-
 		static void OpenUrl (string url)
 		static void OpenUrl (string url)
 		{
 		{
 			try {
 			try {

+ 15 - 1
UnitTests/ApplicationTests.cs

@@ -70,7 +70,7 @@ namespace Terminal.Gui.Core {
 			Assert.Equal (80, Application.Driver.Cols);
 			Assert.Equal (80, Application.Driver.Cols);
 			Assert.Equal (25, Application.Driver.Rows);
 			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
 			// So we need to dispose it manually
 			Application.Top.Dispose ();
 			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]
 		[Fact]
 		public void Init_Unbalanced_Throwss ()
 		public void Init_Unbalanced_Throwss ()
 		{
 		{

+ 38 - 1
UnitTests/MdiTests.cs

@@ -17,7 +17,44 @@ namespace Terminal.Gui.Core {
 			Application.RunState.Instances.Clear ();
 			Application.RunState.Instances.Clear ();
 #endif
 #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]
 		[Fact, AutoInitShutdown]
 		public void Application_RequestStop_With_Params_On_A_Not_MdiContainer_Always_Use_Application_Current ()
 		public void Application_RequestStop_With_Params_On_A_Not_MdiContainer_Always_Use_Application_Current ()
 		{
 		{