瀏覽代碼

Fixed more tests

Nikita Tsukanov 2 年之前
父節點
當前提交
91e4bd00b6

+ 8 - 0
src/Avalonia.Controls/Control.cs

@@ -527,5 +527,13 @@ namespace Avalonia.Controls
                 }
             }
         }
+
+        // Since we are resetting the dispatcher instance, the callback might never arrive
+        internal static void ResetLoadedQueueForUnitTests()
+        {
+            _loadedQueue.Clear();
+            _loadedProcessingQueue.Clear();
+            _isLoadedProcessing = false;
+        }
     }
 }

+ 4 - 0
tests/Avalonia.Controls.UnitTests/LoadedTests.cs

@@ -11,6 +11,8 @@ public class LoadedTests
     [Fact]
     public void Window_Loads_And_Unloads()
     {
+        // Some other tests are populating the queue and are not resetting the dispatcher, so we need to purge it
+        Control.ResetLoadedQueueForUnitTests();
         using (UnitTestApplication.Start(TestServices.StyledWindow))
         {
             int loadedCount = 0, unloadedCount = 0;
@@ -40,6 +42,8 @@ public class LoadedTests
     [Fact]
     public void Control_Loads_And_Unloads()
     {
+        // Some other tests are populating the queue and are not resetting the dispatcher, so we need to purge it
+        Control.ResetLoadedQueueForUnitTests();
         using (UnitTestApplication.Start(TestServices.StyledWindow))
         {
             int loadedCount = 0, unloadedCount = 0;

+ 5 - 1
tests/Avalonia.UnitTests/UnitTestApplication.cs

@@ -8,6 +8,7 @@ using Avalonia.Rendering;
 using Avalonia.Threading;
 using System.Reactive.Disposables;
 using System.Reactive.Concurrency;
+using System.Threading;
 using Avalonia.Input.Platform;
 using Avalonia.Animation;
 using Avalonia.Media;
@@ -42,7 +43,9 @@ namespace Avalonia.UnitTests
         public static IDisposable Start(TestServices services = null)
         {
             var scope = AvaloniaLocator.EnterScope();
-            var app = new UnitTestApplication(services);
+            SynchronizationContext.SetSynchronizationContext(null);
+            AvaloniaSynchronizationContext.InstallIfNeeded();
+            _ = new UnitTestApplication(services);
             Dispatcher.ResetForUnitTests();
             return Disposable.Create(() =>
             {
@@ -53,6 +56,7 @@ namespace Avalonia.UnitTests
 
                 scope.Dispose();
                 Dispatcher.ResetForUnitTests();
+                SynchronizationContext.SetSynchronizationContext(null);
             });
         }