From 4d6572b74e14025c5d0bdf0ba1f6d1f437f76ada Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 7 Dec 2015 20:45:36 +0100 Subject: [PATCH] Added StyleActivator leak test. Trying to locate source of StyleActivator leaks - this test doesn't find it. --- src/Perspex.Styling/Styling/Style.cs | 14 ++-- .../Perspex.LeakTests.csproj | 1 + tests/Perspex.LeakTests/StyleTests.cs | 69 +++++++++++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 tests/Perspex.LeakTests/StyleTests.cs diff --git a/src/Perspex.Styling/Styling/Style.cs b/src/Perspex.Styling/Styling/Style.cs index 112bc817a0..5c5f97417d 100644 --- a/src/Perspex.Styling/Styling/Style.cs +++ b/src/Perspex.Styling/Styling/Style.cs @@ -60,13 +60,13 @@ namespace Perspex.Styling var activator = match.ObservableResult ?? Observable.Never().StartWith(true); - if (visual != null) - { - var detached = Observable.FromEventPattern( - x => visual.DetachedFromVisualTree += x, - x => visual.DetachedFromVisualTree -= x); - activator = activator.TakeUntil(detached); - } + //if (visual != null) + //{ + // var detached = Observable.FromEventPattern( + // x => visual.DetachedFromVisualTree += x, + // x => visual.DetachedFromVisualTree -= x); + // activator = activator.TakeUntil(detached); + //} foreach (var setter in Setters) { diff --git a/tests/Perspex.LeakTests/Perspex.LeakTests.csproj b/tests/Perspex.LeakTests/Perspex.LeakTests.csproj index b2d67554c3..ba2450f8f9 100644 --- a/tests/Perspex.LeakTests/Perspex.LeakTests.csproj +++ b/tests/Perspex.LeakTests/Perspex.LeakTests.csproj @@ -88,6 +88,7 @@ + diff --git a/tests/Perspex.LeakTests/StyleTests.cs b/tests/Perspex.LeakTests/StyleTests.cs new file mode 100644 index 0000000000..82f8ebf1d5 --- /dev/null +++ b/tests/Perspex.LeakTests/StyleTests.cs @@ -0,0 +1,69 @@ +// Copyright (c) The Perspex Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using JetBrains.dotMemoryUnit; +using Perspex.Controls; +using Perspex.Controls.Primitives; +using Perspex.Controls.Templates; +using Perspex.Styling; +using Perspex.VisualTree; +using Xunit; +using Xunit.Abstractions; + +namespace Perspex.LeakTests +{ + [DotMemoryUnit(FailIfRunWithoutSupport = false)] + public class StyleTests + { + public StyleTests(ITestOutputHelper atr) + { + TestApp.Initialize(); + DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine); + } + + [Fact] + public void StyleActivator_Should_Be_Released() + { + Func run = () => + { + var window = new Window + { + Styles = new Styles + { + new Style(x => x.OfType().Class("foo")) + { + Setters = new[] + { + new Setter(Canvas.WidthProperty, 100), + } + } + }, + Content = new Canvas + { + Classes = new Classes("foo"), + } + }; + + // Do a layout and make sure that styled Canvas gets added to visual tree. + window.LayoutManager.ExecuteLayoutPass(); + Assert.IsType(window.Presenter.Child); + Assert.Equal(100, (window.Presenter.Child).Width); + + // Clear the content and ensure the Canvas is removed. + window.Content = null; + window.LayoutManager.ExecuteLayoutPass(); + Assert.Null(window.Presenter.Child); + + return window; + }; + + var result = run(); + + dotMemory.Check(memory => + Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); + } + } +}