From 888cc331705bc11c2b189799d4a00756366c144a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 31 Jan 2022 13:51:28 +0100 Subject: [PATCH] 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`. --- src/Avalonia.Controls/Primitives/PopupRoot.cs | 2 +- src/Avalonia.Controls/Properties/AssemblyInfo.cs | 1 + src/Avalonia.Controls/ValidatingToplevel.cs | 9 ++++++++- tests/Avalonia.Controls.UnitTests/WindowTests.cs | 2 +- tests/Avalonia.LeakTests/ControlTests.cs | 4 ++-- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/PopupRoot.cs b/src/Avalonia.Controls/Primitives/PopupRoot.cs index c59c7c11d9..9d26eb05f3 100644 --- a/src/Avalonia.Controls/Primitives/PopupRoot.cs +++ b/src/Avalonia.Controls/Primitives/PopupRoot.cs @@ -74,7 +74,7 @@ namespace Avalonia.Controls.Primitives IStyleHost IStyleHost.StylingParent => Parent; /// - public void Dispose() => PlatformImpl?.Dispose(); + public void Dispose() => HandleClosed(); private void UpdatePosition() { diff --git a/src/Avalonia.Controls/Properties/AssemblyInfo.cs b/src/Avalonia.Controls/Properties/AssemblyInfo.cs index d1743611cd..05561a38ef 100644 --- a/src/Avalonia.Controls/Properties/AssemblyInfo.cs +++ b/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")] diff --git a/src/Avalonia.Controls/ValidatingToplevel.cs b/src/Avalonia.Controls/ValidatingToplevel.cs index d1edd28ebc..4fb9b7c99f 100644 --- a/src/Avalonia.Controls/ValidatingToplevel.cs +++ b/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 diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index eb128ef038..4166242455 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/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); } diff --git a/tests/Avalonia.LeakTests/ControlTests.cs b/tests/Avalonia.LeakTests/ControlTests.cs index 087d42370e..eed767e771 100644 --- a/tests/Avalonia.LeakTests/ControlTests.cs +++ b/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()).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()).ObjectsCount)); dotMemory.Check(memory =>