Browse Source

Fix failing unit tests in debug mode.

#7369 introduced a validating layer over `WindowBase.PlatformImpl` that is only enabled in debug mode. This validating layer was causing unit tests to fail in debug mode but not on CI which runs tests in release mode.

Fix the problems:

- `PopupRoot` was not correctly disposing itself on `Dispose` - make it call `HandleClosed` in order to perform the same steps as when the popup is closed via other means
- Some unit tests try to access the `PlatformImpl` to get hold of a mock. Added `ValidatingWindowImpl.Unwrap` to allow this
- `ValidatingWindowBaseImpl.Activated` was setting the wrong property on the wrapped `PlatformImpl`.
Steven Kirk 3 years ago
parent
commit
888cc33170

+ 1 - 1
src/Avalonia.Controls/Primitives/PopupRoot.cs

@@ -74,7 +74,7 @@ namespace Avalonia.Controls.Primitives
         IStyleHost IStyleHost.StylingParent => Parent;
 
         /// <inheritdoc/>
-        public void Dispose() => PlatformImpl?.Dispose();
+        public void Dispose() => HandleClosed();
 
         private void UpdatePosition()
         {

+ 1 - 0
src/Avalonia.Controls/Properties/AssemblyInfo.cs

@@ -3,6 +3,7 @@ using Avalonia.Metadata;
 
 [assembly: InternalsVisibleTo("Avalonia.Controls.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c1bba1142285fe0419326fb25866ba62c47e6c2b5c1ab0c95b46413fad375471232cb81706932e1cef38781b9ebd39d5100401bacb651c6c5bbf59e571e81b3bc08d2a622004e08b1a6ece82a7e0b9857525c86d2b95fab4bc3dce148558d7f3ae61aa3a234086902aeface87d9dfdd32b9d2fe3c6dd4055b5ab4b104998bd87")]
 [assembly: InternalsVisibleTo("Avalonia.DesignerSupport, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c1bba1142285fe0419326fb25866ba62c47e6c2b5c1ab0c95b46413fad375471232cb81706932e1cef38781b9ebd39d5100401bacb651c6c5bbf59e571e81b3bc08d2a622004e08b1a6ece82a7e0b9857525c86d2b95fab4bc3dce148558d7f3ae61aa3a234086902aeface87d9dfdd32b9d2fe3c6dd4055b5ab4b104998bd87")]
+[assembly: InternalsVisibleTo("Avalonia.LeakTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c1bba1142285fe0419326fb25866ba62c47e6c2b5c1ab0c95b46413fad375471232cb81706932e1cef38781b9ebd39d5100401bacb651c6c5bbf59e571e81b3bc08d2a622004e08b1a6ece82a7e0b9857525c86d2b95fab4bc3dce148558d7f3ae61aa3a234086902aeface87d9dfdd32b9d2fe3c6dd4055b5ab4b104998bd87")]
 
 [assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia")]
 [assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Controls")]

+ 8 - 1
src/Avalonia.Controls/ValidatingToplevel.cs

@@ -184,7 +184,7 @@ internal class ValidatingWindowBaseImpl : ValidatingToplevelImpl, IWindowBaseImp
     public Action Activated
     {
         get => Inner.Activated;
-        set => Inner.Deactivated = value;
+        set => Inner.Activated = value;
     }
 
     public IPlatformHandle Handle => Inner.Handle;
@@ -211,6 +211,13 @@ internal class ValidatingWindowImpl : ValidatingWindowBaseImpl, IWindowImpl
         }
     }
     
+    public static IWindowImpl Unwrap(IWindowImpl impl)
+    {
+        if (impl is ValidatingWindowImpl v)
+            return v.Inner;
+        return impl;
+    }
+
     public static IWindowImpl Wrap(IWindowImpl impl)
     {
 #if DEBUG

+ 1 - 1
tests/Avalonia.Controls.UnitTests/WindowTests.cs

@@ -821,7 +821,7 @@ namespace Avalonia.Controls.UnitTests
                     target.Width = 410;
                     target.LayoutManager.ExecuteLayoutPass();
 
-                    var windowImpl = Mock.Get(target.PlatformImpl);
+                    var windowImpl = Mock.Get(ValidatingWindowImpl.Unwrap(target.PlatformImpl));
                     windowImpl.Verify(x => x.Resize(new Size(410, 800), PlatformResizeReason.Application));
                     Assert.Equal(410, target.Width);
                 }

+ 2 - 2
tests/Avalonia.LeakTests/ControlTests.cs

@@ -496,7 +496,7 @@ namespace Avalonia.LeakTests
                 
                 AttachShowAndDetachContextMenu(window);
 
-                Mock.Get(window.PlatformImpl).Invocations.Clear();
+                Mock.Get(ValidatingWindowImpl.Unwrap(window.PlatformImpl)).Invocations.Clear();
                 dotMemory.Check(memory =>
                     Assert.Equal(initialMenuCount, memory.GetObjects(where => where.Type.Is<ContextMenu>()).ObjectsCount));
                 dotMemory.Check(memory =>
@@ -541,7 +541,7 @@ namespace Avalonia.LeakTests
                 BuildAndShowContextMenu(window);
                 BuildAndShowContextMenu(window);
 
-                Mock.Get(window.PlatformImpl).Invocations.Clear();
+                Mock.Get(ValidatingWindowImpl.Unwrap(window.PlatformImpl)).Invocations.Clear();
                 dotMemory.Check(memory =>
                     Assert.Equal(initialMenuCount, memory.GetObjects(where => where.Type.Is<ContextMenu>()).ObjectsCount));
                 dotMemory.Check(memory =>