Browse Source

Add cleanup step to leak tests.

Because previously failures could occur depending on the order that leak tests were run in.
Steven Kirk 3 years ago
parent
commit
5aa7de52e7
1 changed files with 19 additions and 4 deletions
  1. 19 4
      tests/Avalonia.LeakTests/ControlTests.cs

+ 19 - 4
tests/Avalonia.LeakTests/ControlTests.cs

@@ -2,6 +2,7 @@ using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
+using System.Reactive.Disposables;
 using Avalonia.Controls;
 using Avalonia.Controls.Shapes;
 using Avalonia.Controls.Templates;
@@ -12,6 +13,7 @@ using Avalonia.Media;
 using Avalonia.Platform;
 using Avalonia.Rendering;
 using Avalonia.Styling;
+using Avalonia.Threading;
 using Avalonia.UnitTests;
 using Avalonia.VisualTree;
 using JetBrains.dotMemoryUnit;
@@ -730,10 +732,23 @@ namespace Avalonia.LeakTests
 
         private IDisposable Start()
         {
-            return UnitTestApplication.Start(TestServices.StyledWindow.With(
-                focusManager: new FocusManager(),
-                keyboardDevice: () => new KeyboardDevice(),
-                inputManager: new InputManager()));
+            void Cleanup()
+            {
+                // KeyboardDevice holds a reference to the focused item.
+                KeyboardDevice.Instance.SetFocusedElement(null, NavigationMethod.Unspecified, KeyModifiers.None);
+                
+                // Empty the dispatcher queue.
+                Dispatcher.UIThread.RunJobs();
+            }
+
+            return new CompositeDisposable
+            {
+                Disposable.Create(Cleanup),
+                UnitTestApplication.Start(TestServices.StyledWindow.With(
+                    focusManager: new FocusManager(),
+                    keyboardDevice: () => new KeyboardDevice(),
+                    inputManager: new InputManager()))
+            };
         }