summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-12-06 13:15:32 +0100
committerRui Marinho <me@ruimarinho.net>2016-12-06 12:15:32 +0000
commit20e2e12dce2f81b92e8682f128cd81e08469f485 (patch)
tree965f0715dc24af3c0d0bd17740da9d962ed12927 /Xamarin.Forms.Core.UnitTests
parent10c65d035869fdc587f6ddddc0d472033af61ee6 (diff)
downloadxamarin-forms-20e2e12dce2f81b92e8682f128cd81e08469f485.tar.gz
xamarin-forms-20e2e12dce2f81b92e8682f128cd81e08469f485.tar.bz2
xamarin-forms-20e2e12dce2f81b92e8682f128cd81e08469f485.zip
[C] detach Behaviors and Triggers on VE finalization (#555)
* [C] detach Behaviors and Triggers on VE finalization * update docs
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests')
-rw-r--r--Xamarin.Forms.Core.UnitTests/BehaviorTest.cs42
1 files changed, 41 insertions, 1 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs b/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
index 78e0cfb0..90217389 100644
--- a/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
+++ b/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
@@ -3,8 +3,9 @@ using NUnit.Framework;
namespace Xamarin.Forms.Core.UnitTests
{
- internal class MockBehavior<T> : Behavior<T> where T:BindableObject
+ class MockBehavior<T> : Behavior<T> where T:BindableObject
{
+ public static int AttachCount { get; set; }
public bool attached;
public bool detached;
@@ -12,11 +13,13 @@ namespace Xamarin.Forms.Core.UnitTests
{
base.OnAttachedTo (bindable);
attached = true;
+ AttachCount++;
AssociatedObject = bindable;
}
protected override void OnDetachingFrom (BindableObject bindable)
{
+ AttachCount--;
detached = true;
base.OnDetachingFrom (bindable);
AssociatedObject = null;
@@ -106,5 +109,42 @@ namespace Xamarin.Forms.Core.UnitTests
collection.Remove (behavior);
Assert.Null (behavior.AssociatedObject);
}
+
+ [Test]
+ //https://bugzilla.xamarin.com/show_bug.cgi?id=44074
+ public void TestBehaviorsAreDetachedBeforeGarbageCollection()
+ {
+ WeakReference weakBindable = null;
+
+ var attachCount = MockBehavior<VisualElement>.AttachCount;
+
+ int i = 0;
+ Action create = null;
+ create = () =>
+ {
+ if (i++ < 1024)
+ {
+ create();
+ return;
+ }
+
+ var bindable = new MockBindable
+ {
+ Behaviors = {
+ new MockBehavior<VisualElement> ()
+ }
+ };
+ weakBindable = new WeakReference(bindable);
+ };
+
+ create();
+
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ GC.Collect();
+
+ Assert.False(weakBindable.IsAlive);
+ Assert.AreEqual(attachCount, MockBehavior<VisualElement>.AttachCount);
+ }
}
} \ No newline at end of file