瀏覽代碼

Moved AvaloniaObject leak tests.

From Avalonia.Base.UnitTests to Avalonia.LeakTests - the test was
failing on mono on OSX and I can't work out why. Leak tests don't
currently get run on mono CI.
Steven Kirk 9 年之前
父節點
當前提交
173352d109

+ 0 - 45
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Direct.cs

@@ -335,44 +335,6 @@ namespace Avalonia.Base.UnitTests
             Assert.Equal("second", target.Foo);
         }
 
-        [Fact]
-        public void Binding_To_Direct_Property_Does_Not_Get_Collected()
-        {
-            var target = new Class2();
-
-            Func<WeakReference> setupBinding = () =>
-            {
-                var source = new Subject<string>();
-                var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
-                source.OnNext("foo");
-                return new WeakReference(source);
-            };
-
-            var weakSource = setupBinding();
-
-            GC.Collect();
-
-            Assert.Equal("foo", target.Foo);
-            Assert.True(weakSource.IsAlive);
-        }
-
-        [Fact]
-        public void Binding_To_Direct_Property_Gets_Collected_When_Completed()
-        {
-            var target = new Class2();
-            var weakSource = SetupDirectBinding(target);
-
-            Action completeSource = () =>
-            {
-                ((ISubject<string>)weakSource.Target).OnCompleted();
-            };
-
-            completeSource();
-            GC.Collect();
-
-            Assert.False(weakSource.IsAlive);
-        }
-
         [Fact]
         public void Property_Notifies_Initialized()
         {
@@ -447,13 +409,6 @@ namespace Avalonia.Base.UnitTests
             Assert.True(called);
         }
 
-        private WeakReference SetupDirectBinding(Class2 target)
-        {
-            var source = new Subject<string>();
-            var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
-            return new WeakReference(source);
-        }
-
         private class Class1 : AvaloniaObject
         {
             public static readonly DirectProperty<Class1, string> FooProperty =

+ 14 - 0
tests/Avalonia.LeakTests/Avalonia.LeakTests.csproj

@@ -59,12 +59,25 @@
       <HintPath>..\..\packages\System.Reactive.Interfaces.3.0.0\lib\net45\System.Reactive.Interfaces.dll</HintPath>
       <Private>True</Private>
     </Reference>
+    <Reference Include="System.Reactive.Linq, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\System.Reactive.Linq.3.0.0\lib\net45\System.Reactive.Linq.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="System.Reactive.PlatformServices, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\System.Reactive.PlatformServices.3.0.0\lib\net45\System.Reactive.PlatformServices.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
+    <Reference Include="System.Reactive.Windows.Threading, Version=3.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\System.Reactive.Windows.Threading.3.0.0\lib\net45\System.Reactive.Windows.Threading.dll</HintPath>
+      <Private>True</Private>
+    </Reference>
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="System.Data" />
     <Reference Include="System.Net.Http" />
     <Reference Include="System.Xml" />
+    <Reference Include="WindowsBase" />
     <Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
       <HintPath>..\..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
       <Private>True</Private>
@@ -83,6 +96,7 @@
     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AvaloniaObjectTests.cs" />
     <Compile Include="ControlTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

+ 77 - 0
tests/Avalonia.LeakTests/AvaloniaObjectTests.cs

@@ -0,0 +1,77 @@
+using System;
+using System.Reactive.Subjects;
+using Xunit;
+
+namespace Avalonia.LeakTests
+{
+    public class AvaloniaObjectTests
+    {
+        [Fact]
+        public void Binding_To_Direct_Property_Does_Not_Get_Collected()
+        {
+            var target = new Class1();
+
+            Func<WeakReference> setupBinding = () =>
+            {
+                var source = new Subject<string>();
+                var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
+                source.OnNext("foo");
+                return new WeakReference(source);
+            };
+
+            var weakSource = setupBinding();
+
+            GC.Collect();
+
+            Assert.Equal("foo", target.Foo);
+            Assert.True(weakSource.IsAlive);
+        }
+
+        [Fact]
+        public void Binding_To_Direct_Property_Gets_Collected_When_Completed()
+        {
+            var target = new Class1();
+
+            Func<WeakReference> setupBinding = () =>
+            {
+                var source = new Subject<string>();
+                var sub = target.Bind((AvaloniaProperty)Class1.FooProperty, source);
+                return new WeakReference(source);
+            };
+
+            var weakSource = setupBinding();
+
+            Action completeSource = () =>
+            {
+                ((ISubject<string>)weakSource.Target).OnCompleted();
+            };
+
+            completeSource();
+            GC.Collect();
+
+            Assert.False(weakSource.IsAlive);
+        }
+
+        private class Class1 : AvaloniaObject
+        {
+            public static readonly DirectProperty<Class1, string> FooProperty =
+                AvaloniaProperty.RegisterDirect<Class1, string>(
+                    "Foo",
+                    o => o.Foo,
+                    (o, v) => o.Foo = v,
+                    unsetValue: "unset");
+
+            private string _foo = "initial2";
+
+            static Class1()
+            {
+            }
+
+            public string Foo
+            {
+                get { return _foo; }
+                set { SetAndRaise(FooProperty, ref _foo, value); }
+            }
+        }
+    }
+}

+ 4 - 0
tests/Avalonia.LeakTests/packages.config

@@ -4,8 +4,12 @@
   <package id="AutoFixture.AutoMoq" version="3.40.0" targetFramework="net45" />
   <package id="JetBrains.dotMemoryUnit" version="2.1.20150828.125449" targetFramework="net45" />
   <package id="Moq" version="4.2.1510.2205" targetFramework="net45" />
+  <package id="System.Reactive" version="3.0.0" targetFramework="net45" />
   <package id="System.Reactive.Core" version="3.0.0" targetFramework="net45" />
   <package id="System.Reactive.Interfaces" version="3.0.0" targetFramework="net45" />
+  <package id="System.Reactive.Linq" version="3.0.0" targetFramework="net45" />
+  <package id="System.Reactive.PlatformServices" version="3.0.0" targetFramework="net45" />
+  <package id="System.Reactive.Windows.Threading" version="3.0.0" targetFramework="net45" />
   <package id="xunit" version="2.1.0" targetFramework="net45" />
   <package id="xunit.abstractions" version="2.0.0" targetFramework="net46" />
   <package id="xunit.assert" version="2.1.0" targetFramework="net45" />