From 260f2a4cc4b7221cee0060dab167179b639151dd Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 30 Nov 2015 22:25:04 +0100 Subject: [PATCH] Added some more leak tests. All passing. --- tests/Perspex.LeakTests/ControlTests.cs | 107 ++++++++++++++++++++++++ tests/Perspex.LeakTests/TestApp.cs | 4 + 2 files changed, 111 insertions(+) diff --git a/tests/Perspex.LeakTests/ControlTests.cs b/tests/Perspex.LeakTests/ControlTests.cs index a44532dc5c..abc4280357 100644 --- a/tests/Perspex.LeakTests/ControlTests.cs +++ b/tests/Perspex.LeakTests/ControlTests.cs @@ -7,6 +7,7 @@ using System.Linq; using JetBrains.dotMemoryUnit; using Perspex.Controls; using Perspex.Controls.Templates; +using Perspex.VisualTree; using Xunit; using Xunit.Abstractions; @@ -81,6 +82,112 @@ namespace Perspex.LeakTests Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); } + [Fact] + public void ScrollViewer_With_Content_Is_Freed() + { + Func run = () => + { + var window = new Window + { + Content = new ScrollViewer + { + Content = new Canvas() + } + }; + + // Do a layout and make sure that ScrollViewer gets added to visual tree and its + // template applied. + window.LayoutManager.ExecuteLayoutPass(); + Assert.IsType(window.Presenter.Child); + Assert.IsType(((ScrollViewer)window.Presenter.Child).Presenter.Child); + + // Clear the content and ensure the ScrollViewer 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)); + dotMemory.Check(memory => + Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); + } + + [Fact] + public void TextBox_Is_Freed() + { + Func run = () => + { + var window = new Window + { + Content = new TextBox() + }; + + // Do a layout and make sure that TextBox gets added to visual tree and its + // template applied. + window.LayoutManager.ExecuteLayoutPass(); + Assert.IsType(window.Presenter.Child); + Assert.NotEqual(0, window.Presenter.Child.GetVisualChildren().Count()); + + // Clear the content and ensure the TextBox 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)); + } + + [Fact] + public void TextBox_With_Xaml_Binding_Is_Freed() + { + Func run = () => + { + var window = new Window + { + DataContext = new Node { Name = "foo" }, + Content = new TextBox() + }; + + var binding = new Perspex.Markup.Xaml.Data.Binding + { + Path = "Name" + }; + + binding.Bind((TextBox)window.Content, TextBox.TextProperty); + + // Do a layout and make sure that TextBox gets added to visual tree and its + // Text property set. + window.LayoutManager.ExecuteLayoutPass(); + Assert.IsType(window.Presenter.Child); + Assert.Equal("foo", ((TextBox)window.Presenter.Child).Text); + + // Clear the content and DataContext and ensure the TextBox is removed. + window.Content = null; + window.DataContext = 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)); + dotMemory.Check(memory => + Assert.Equal(0, memory.GetObjects(where => where.Type.Is()).ObjectsCount)); + } + [Fact] public void TreeView_Is_Freed() { diff --git a/tests/Perspex.LeakTests/TestApp.cs b/tests/Perspex.LeakTests/TestApp.cs index d0df7c18fc..8c08c57fc3 100644 --- a/tests/Perspex.LeakTests/TestApp.cs +++ b/tests/Perspex.LeakTests/TestApp.cs @@ -20,11 +20,15 @@ namespace Perspex.LeakTests var fixture = new Fixture().Customize(new AutoMoqCustomization()); var windowImpl = new Mock(); var renderInterface = fixture.Create(); + var threadingInterface = Mock.Of(x => + x.CurrentThreadIsLoopThread == true); PerspexLocator.CurrentMutable .Bind().ToConstant(new AssetLoader()) .Bind().ToConstant(new PclPlatformWrapper()) .Bind().ToConstant(renderInterface) + .Bind().ToConstant(threadingInterface) + .Bind().ToConstant(new Mock().Object) .Bind().ToConstant(new WindowingPlatformMock(() => windowImpl.Object)); Styles = new DefaultTheme();