From aa6b9045bcb5a2a0259f14caae8c3133a153b599 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 3 Dec 2015 21:34:07 +0100 Subject: [PATCH] Added new leak test. --- tests/Perspex.LeakTests/ControlTests.cs | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Perspex.LeakTests/ControlTests.cs b/tests/Perspex.LeakTests/ControlTests.cs index c770fb1c16..3a68e55f5c 100644 --- a/tests/Perspex.LeakTests/ControlTests.cs +++ b/tests/Perspex.LeakTests/ControlTests.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using JetBrains.dotMemoryUnit; using Perspex.Controls; +using Perspex.Controls.Primitives; using Perspex.Controls.Templates; using Perspex.VisualTree; using Xunit; @@ -82,6 +83,36 @@ namespace Perspex.LeakTests Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); } + [Fact] + public void Templated_Child_Is_Freed_When_Template_Cleared() + { + Func run = () => + { + var window = new Window + { + Content = new TestTemplatedControl() + }; + + // Do a layout and make sure that the control gets added to visual tree and its + // template applied. + window.LayoutManager.ExecuteLayoutPass(); + Assert.IsType(window.Presenter.Child); + Assert.IsType(window.Presenter.Child.GetVisualChildren().SingleOrDefault()); + + // Clear the template and ensure the control template gets removed + ((TestTemplatedControl)window.Content).Template = null; + window.LayoutManager.ExecuteLayoutPass(); + Assert.Equal(0, window.Presenter.Child.GetVisualChildren().Count()); + + return window; + }; + + var result = run(); + + dotMemory.Check(memory => + Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); + } + [Fact] public void ScrollViewer_With_Content_Is_Freed() { @@ -266,6 +297,14 @@ namespace Perspex.LeakTests Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); } + private class TestTemplatedControl : TemplatedControl + { + public TestTemplatedControl() + { + Template = new FuncControlTemplate(_ => new Canvas()); + } + } + private class Node { public string Name { get; set; }