Browse Source

Added some more leak tests.

All passing.
Steven Kirk 10 years ago
parent
commit
260f2a4cc4
2 changed files with 111 additions and 0 deletions
  1. 107 0
      tests/Perspex.LeakTests/ControlTests.cs
  2. 4 0
      tests/Perspex.LeakTests/TestApp.cs

+ 107 - 0
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<Canvas>()).ObjectsCount));
         }
 
+        [Fact]
+        public void ScrollViewer_With_Content_Is_Freed()
+        {
+            Func<Window> 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<ScrollViewer>(window.Presenter.Child);
+                Assert.IsType<Canvas>(((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<TextBox>()).ObjectsCount));
+            dotMemory.Check(memory =>
+                Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
+        }
+
+        [Fact]
+        public void TextBox_Is_Freed()
+        {
+            Func<Window> 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<TextBox>(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<TextBox>()).ObjectsCount));
+        }
+
+        [Fact]
+        public void TextBox_With_Xaml_Binding_Is_Freed()
+        {
+            Func<Window> 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<TextBox>(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<TextBox>()).ObjectsCount));
+            dotMemory.Check(memory =>
+                Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Node>()).ObjectsCount));
+        }
+
         [Fact]
         public void TreeView_Is_Freed()
         {

+ 4 - 0
tests/Perspex.LeakTests/TestApp.cs

@@ -20,11 +20,15 @@ namespace Perspex.LeakTests
             var fixture = new Fixture().Customize(new AutoMoqCustomization());
             var windowImpl = new Mock<IWindowImpl>();
             var renderInterface = fixture.Create<IPlatformRenderInterface>();
+            var threadingInterface = Mock.Of<IPlatformThreadingInterface>(x =>
+                x.CurrentThreadIsLoopThread == true);
 
             PerspexLocator.CurrentMutable
                 .Bind<IAssetLoader>().ToConstant(new AssetLoader())
                 .Bind<IPclPlatformWrapper>().ToConstant(new PclPlatformWrapper())
                 .Bind<IPlatformRenderInterface>().ToConstant(renderInterface)
+                .Bind<IPlatformThreadingInterface>().ToConstant(threadingInterface)
+                .Bind<IStandardCursorFactory>().ToConstant(new Mock<IStandardCursorFactory>().Object)
                 .Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock(() => windowImpl.Object));
 
             Styles = new DefaultTheme();