From ba008e987dfc6b1fe381bbd1f04e530e5cfb86a5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 4 Mar 2016 21:16:36 +0100 Subject: [PATCH] Demonstrate leak in test. --- tests/Perspex.LeakTests/ControlTests.cs | 35 +++++++++++++++++++++++++ tests/Perspex.UnitTests/TestServices.cs | 18 ++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/tests/Perspex.LeakTests/ControlTests.cs b/tests/Perspex.LeakTests/ControlTests.cs index 1577628c94..ffc8bc128e 100644 --- a/tests/Perspex.LeakTests/ControlTests.cs +++ b/tests/Perspex.LeakTests/ControlTests.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using JetBrains.dotMemoryUnit; +using Perspex.Collections; using Perspex.Controls; using Perspex.Controls.Primitives; using Perspex.Controls.Templates; @@ -207,6 +208,40 @@ namespace Perspex.LeakTests } } + [Fact] + public void TextBox_Class_Listeners_Are_Freed() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + TextBox textBox; + + var window = new Window + { + Content = textBox = new TextBox() + }; + + // Do a layout and make sure that TextBox gets added to visual tree and its + // template applied. + LayoutManager.Instance.ExecuteInitialLayoutPass(window); + Assert.Same(textBox, window.Presenter.Child); + + // Get the border from the TextBox template. + var border = textBox.GetTemplateChildren().FirstOrDefault(x => x.Name == "border"); + + // The TextBox should have subscriptions to its Classes collection from the + // default theme. + Assert.NotEmpty(((InccDebug)textBox.Classes).GetCollectionChangedSubscribers()); + + // Clear the content and ensure the TextBox is removed. + window.Content = null; + LayoutManager.Instance.ExecuteLayoutPass(); + Assert.Null(window.Presenter.Child); + + // Check that the TextBox has no subscriptions to its Classes collection. + Assert.Null(((InccDebug)textBox.Classes).GetCollectionChangedSubscribers()); + } + } + [Fact] public void TreeView_Is_Freed() { diff --git a/tests/Perspex.UnitTests/TestServices.cs b/tests/Perspex.UnitTests/TestServices.cs index 8369f8aed0..81d76d0af3 100644 --- a/tests/Perspex.UnitTests/TestServices.cs +++ b/tests/Perspex.UnitTests/TestServices.cs @@ -6,6 +6,7 @@ using System.Reflection; using Moq; using Perspex.Input; using Perspex.Layout; +using Perspex.Markup.Xaml; using Perspex.Platform; using Perspex.Shared.PlatformSupport; using Perspex.Styling; @@ -26,7 +27,7 @@ namespace Perspex.UnitTests renderInterface: s_fixture.Create(), standardCursorFactory: Mock.Of(), styler: new Styler(), - theme: () => new DefaultTheme(), + theme: () => CreateDefaultTheme(), threadingInterface: Mock.Of(x => x.CurrentThreadIsLoopThread == true), windowingPlatform: new MockWindowingPlatform()); @@ -106,5 +107,20 @@ namespace Perspex.UnitTests windowImpl: windowImpl ?? WindowImpl, windowingPlatform: windowingPlatform ?? WindowingPlatform); } + + private static Styles CreateDefaultTheme() + { + var result = new Styles + { + new DefaultTheme(), + }; + + var loader = new PerspexXamlLoader(); + var baseLight = (IStyle)loader.Load( + new Uri("resm:Perspex.Themes.Default.Accents.BaseLight.paml?assembly=Perspex.Themes.Default")); + result.Add(baseLight); + + return result; + } } }