Browse Source

Fixed Scenario tests

Tig 1 year ago
parent
commit
9372cce69c

+ 1 - 1
Scripts/Terminal.Gui.PowerShell.Analyzers.psd1

@@ -42,7 +42,7 @@ PowerShellHostName = 'ConsoleHost'
 # PowerShellHostVersion = ''
 # PowerShellHostVersion = ''
 
 
 # Processor architecture (None, X86, Amd64) required by this module
 # Processor architecture (None, X86, Amd64) required by this module
-ProcessorArchitecture = 'Amd64'
+ProcessorArchitecture = ''
 
 
 # Modules that must be imported into the global environment prior to importing this module
 # Modules that must be imported into the global environment prior to importing this module
 RequiredModules = @('Microsoft.PowerShell.Management','Microsoft.PowerShell.Utility','./Terminal.Gui.PowerShell.Core.psd1')
 RequiredModules = @('Microsoft.PowerShell.Management','Microsoft.PowerShell.Utility','./Terminal.Gui.PowerShell.Core.psd1')

+ 1 - 1
Scripts/Terminal.Gui.PowerShell.Build.psd1

@@ -36,7 +36,7 @@ PowerShellHostVersion = '7.4.0'
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Has nothing to do with runtime use of Terminal.Gui.
 # Has nothing to do with runtime use of Terminal.Gui.
-ProcessorArchitecture = 'Amd64'
+ProcessorArchitecture = ''
 
 
 # Modules that must be imported into the global environment prior to importing this module
 # Modules that must be imported into the global environment prior to importing this module
 RequiredModules = @(
 RequiredModules = @(

+ 1 - 1
Scripts/Terminal.Gui.PowerShell.Core.psd1

@@ -44,7 +44,7 @@ PowerShellHostVersion = '7.4.0'
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Has nothing to do with runtime use of Terminal.Gui.
 # Has nothing to do with runtime use of Terminal.Gui.
-ProcessorArchitecture = 'Amd64'
+ProcessorArchitecture = ''
 
 
 # Modules that must be imported into the global environment prior to importing this module
 # Modules that must be imported into the global environment prior to importing this module
 RequiredModules = @(
 RequiredModules = @(

+ 1 - 1
Scripts/Terminal.Gui.PowerShell.Git.psd1

@@ -44,7 +44,7 @@ PowerShellHostVersion = '7.4.0'
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Has nothing to do with runtime use of Terminal.Gui.
 # Has nothing to do with runtime use of Terminal.Gui.
-ProcessorArchitecture = 'AMD64'
+ProcessorArchitecture = ''
 
 
 # Modules that must be imported into the global environment prior to importing this module
 # Modules that must be imported into the global environment prior to importing this module
 RequiredModules = @(
 RequiredModules = @(

+ 1 - 1
Scripts/Terminal.Gui.PowerShell.psd1

@@ -49,7 +49,7 @@ PowerShellHostVersion = '7.4.0'
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Processor architecture (None, MSIL, X86, IA64, Amd64, Arm, or an empty string) required by this module. One value only.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Set to AMD64 here because development on Terminal.Gui isn't really supported on anything else.
 # Has nothing to do with runtime use of Terminal.Gui.
 # Has nothing to do with runtime use of Terminal.Gui.
-ProcessorArchitecture = 'Amd64'
+ProcessorArchitecture = ''
 
 
 # Modules that must be imported into the global environment prior to importing this module
 # Modules that must be imported into the global environment prior to importing this module
 RequiredModules = @(
 RequiredModules = @(

+ 6 - 2
UnitTests/Application/MainLoopTests.cs

@@ -620,9 +620,9 @@ public class MainLoopTests
     }
     }
 
 
     [Fact]
     [Fact]
-    [AutoInitShutdown]
     public async Task InvokeLeakTest ()
     public async Task InvokeLeakTest ()
     {
     {
+        Application.Init ();
         Random r = new ();
         Random r = new ();
         TextField tf = new ();
         TextField tf = new ();
         var top = new Toplevel ();
         var top = new Toplevel ();
@@ -641,10 +641,10 @@ public class MainLoopTests
 
 
         Assert.Equal (numIncrements * numPasses, tbCounter);
         Assert.Equal (numIncrements * numPasses, tbCounter);
         top.Dispose ();
         top.Dispose ();
+        Application.Shutdown ();
     }
     }
 
 
     [Theory]
     [Theory]
-    [AutoInitShutdown]
     [MemberData (nameof (TestAddIdle))]
     [MemberData (nameof (TestAddIdle))]
     public void Mainloop_Invoke_Or_AddIdle_Can_Be_Used_For_Events_Or_Actions (
     public void Mainloop_Invoke_Or_AddIdle_Can_Be_Used_For_Events_Or_Actions (
         Action action,
         Action action,
@@ -658,6 +658,8 @@ public class MainLoopTests
         int pfour
         int pfour
     )
     )
     {
     {
+        Application.Init ();
+
         total = 0;
         total = 0;
         btn = null;
         btn = null;
         clickMe = pclickMe;
         clickMe = pclickMe;
@@ -720,6 +722,8 @@ public class MainLoopTests
         Assert.True (taskCompleted);
         Assert.True (taskCompleted);
         Assert.Equal (clickMe, btn.Text);
         Assert.Equal (clickMe, btn.Text);
         Assert.Equal (four, total);
         Assert.Equal (four, total);
+
+        Application.Shutdown ();
     }
     }
 
 
     [Fact]
     [Fact]

+ 6 - 2
UnitTests/Application/SynchronizatonContextTests.cs

@@ -5,9 +5,9 @@ namespace Terminal.Gui.ApplicationTests;
 public class SyncrhonizationContextTests
 public class SyncrhonizationContextTests
 {
 {
     [Fact]
     [Fact]
-    [AutoInitShutdown]
     public void SynchronizationContext_CreateCopy ()
     public void SynchronizationContext_CreateCopy ()
     {
     {
+        Application.Init ();
         SynchronizationContext context = SynchronizationContext.Current;
         SynchronizationContext context = SynchronizationContext.Current;
         Assert.NotNull (context);
         Assert.NotNull (context);
 
 
@@ -15,12 +15,13 @@ public class SyncrhonizationContextTests
         Assert.NotNull (contextCopy);
         Assert.NotNull (contextCopy);
 
 
         Assert.NotEqual (context, contextCopy);
         Assert.NotEqual (context, contextCopy);
+        Application.Shutdown ();
     }
     }
 
 
     [Fact]
     [Fact]
-    [AutoInitShutdown]
     public void SynchronizationContext_Post ()
     public void SynchronizationContext_Post ()
     {
     {
+        Application.Init ();
         SynchronizationContext context = SynchronizationContext.Current;
         SynchronizationContext context = SynchronizationContext.Current;
 
 
         var success = false;
         var success = false;
@@ -48,12 +49,14 @@ public class SyncrhonizationContextTests
         // blocks here until the RequestStop is processed at the end of the test
         // blocks here until the RequestStop is processed at the end of the test
         Application.Run ().Dispose ();
         Application.Run ().Dispose ();
         Assert.True (success);
         Assert.True (success);
+        Application.Shutdown ();
     }
     }
 
 
     [Fact]
     [Fact]
     [AutoInitShutdown]
     [AutoInitShutdown]
     public void SynchronizationContext_Send ()
     public void SynchronizationContext_Send ()
     {
     {
+        Application.Init ();
         SynchronizationContext context = SynchronizationContext.Current;
         SynchronizationContext context = SynchronizationContext.Current;
 
 
         var success = false;
         var success = false;
@@ -81,5 +84,6 @@ public class SyncrhonizationContextTests
         // blocks here until the RequestStop is processed at the end of the test
         // blocks here until the RequestStop is processed at the end of the test
         Application.Run ().Dispose ();
         Application.Run ().Dispose ();
         Assert.True (success);
         Assert.True (success);
+        Application.Shutdown ();
     }
     }
 }
 }

+ 40 - 28
UnitTests/UICatalog/ScenarioTests.cs

@@ -23,6 +23,7 @@ public class ScenarioTests : TestsAllViews
                      .Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
                      .Where (type => type.IsClass && !type.IsAbstract && type.IsSubclassOf (typeof (Scenario)))
                      .Select (type => new object [] { type });
                      .Select (type => new object [] { type });
 
 
+    private readonly object _timeoutLock = new object ();
 
 
     /// <summary>
     /// <summary>
     ///     <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
     ///     <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
@@ -32,61 +33,72 @@ public class ScenarioTests : TestsAllViews
     [MemberData (nameof (AllScenarioTypes))]
     [MemberData (nameof (AllScenarioTypes))]
     public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType)
     public void All_Scenarios_Quit_And_Init_Shutdown_Properly (Type scenarioType)
     {
     {
+        // If a previous test failed, this will ensure that the Application is in a clean state
         Application.ResetState (true);
         Application.ResetState (true);
 
 
         _output.WriteLine ($"Running Scenario '{scenarioType}'");
         _output.WriteLine ($"Running Scenario '{scenarioType}'");
-
         Scenario scenario = (Scenario)Activator.CreateInstance (scenarioType);
         Scenario scenario = (Scenario)Activator.CreateInstance (scenarioType);
 
 
         uint abortTime = 500;
         uint abortTime = 500;
-
         bool initialized = false;
         bool initialized = false;
         bool shutdown = false;
         bool shutdown = false;
-
         object timeout = null;
         object timeout = null;
 
 
-        Application.InitializedChanged += (s, a) =>
-                                          {
-                                              if (a.NewValue)
-                                              {
-                                                  //output.WriteLine ($"  Add timeout to force quit after {abortTime}ms");
-                                                  timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
+        void OnApplicationOnInitializedChanged (object s, StateEventArgs<bool> a)
+        {
+            if (a.NewValue)
+            {
+                lock (_timeoutLock)
+                {
+                    timeout = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
+                }
 
 
-                                                  Application.Iteration += OnApplicationOnIteration;
-                                                  initialized = true;
-                                              }
-                                              else
-                                              {
-                                                  if (timeout is { })
-                                                  {
-                                                      Application.RemoveTimeout (timeout);
-                                                      timeout = null;
-                                                  }
-                                                  Application.Iteration -= OnApplicationOnIteration;
-                                                  shutdown = true;
-                                              }
-                                          };
+                Application.Iteration += OnApplicationOnIteration;
+                initialized = true;
+            }
+            else
+            {
+                Application.Iteration -= OnApplicationOnIteration;
+                shutdown = true;
+            }
+        }
+
+        Application.InitializedChanged += OnApplicationOnInitializedChanged;
 
 
         scenario.Main ();
         scenario.Main ();
         scenario.Dispose ();
         scenario.Dispose ();
         scenario = null;
         scenario = null;
 
 
+        Application.InitializedChanged -= OnApplicationOnInitializedChanged;
+
+        lock (_timeoutLock)
+        {
+            if (timeout is { })
+            {
+                Application.RemoveTimeout (timeout);
+                timeout = null;
+            }
+        }
+
+
         Assert.True (initialized);
         Assert.True (initialized);
         Assert.True (shutdown);
         Assert.True (shutdown);
 
 
 #if DEBUG_IDISPOSABLE
 #if DEBUG_IDISPOSABLE
         Assert.Empty (Responder.Instances);
         Assert.Empty (Responder.Instances);
 #endif
 #endif
-        Application.Shutdown ();
         return;
         return;
 
 
         // If the scenario doesn't close within 500ms, this will force it to quit
         // If the scenario doesn't close within 500ms, this will force it to quit
         bool ForceCloseCallback ()
         bool ForceCloseCallback ()
         {
         {
-            if (timeout is { })
+            lock (_timeoutLock)
             {
             {
-                Application.RemoveTimeout (timeout);
-                timeout = null;
+                if (timeout is { })
+                {
+                    Application.RemoveTimeout (timeout);
+                    timeout = null;
+                }
             }
             }
             Application.ResetState (true);
             Application.ResetState (true);
             Assert.Fail (
             Assert.Fail (
@@ -97,7 +109,7 @@ public class ScenarioTests : TestsAllViews
 
 
         void OnApplicationOnIteration (object s, IterationEventArgs a)
         void OnApplicationOnIteration (object s, IterationEventArgs a)
         {
         {
-            if (scenario is { })
+            if (Application._initialized)
             {
             {
                 // Press QuitKey 
                 // Press QuitKey 
                 Application.OnKeyDown (Application.QuitKey);
                 Application.OnKeyDown (Application.QuitKey);