From cc8e3343cf0256c7444bba1fd4ceb12a730846c2 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Wed, 5 Mar 2014 12:37:46 +0000 Subject: [PATCH] Finish off (?) the PropertyChangedExtensionsTests --- Stylet/PropertyChangedExtensions.cs | 1 + .../PropertyChangedExtensionsTests.cs | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Stylet/PropertyChangedExtensions.cs b/Stylet/PropertyChangedExtensions.cs index 1ec8631..cab47b5 100644 --- a/Stylet/PropertyChangedExtensions.cs +++ b/Stylet/PropertyChangedExtensions.cs @@ -31,6 +31,7 @@ namespace Stylet this.binder = new WeakReference(binder); this.inpc = new WeakReference(inpc); this.propertyName = propertyName; + // We need to keep this strongly, in case its target is a compiler-generated class instance this.handler = handler; } diff --git a/StyletUnitTests/PropertyChangedExtensionsTests.cs b/StyletUnitTests/PropertyChangedExtensionsTests.cs index e6a1447..5845ee2 100644 --- a/StyletUnitTests/PropertyChangedExtensionsTests.cs +++ b/StyletUnitTests/PropertyChangedExtensionsTests.cs @@ -169,14 +169,30 @@ namespace StyletUnitTests var weakBinding = new WeakReference(binding); var notifying = new NotifyingClass(); - // Retain binder, in case that affects anything - var binder = binding.BindWeak(notifying); + binding.BindWeak(notifying); binding = null; GC.Collect(); Assert.IsFalse(weakBinding.TryGetTarget(out binding)); } + [Test] + public void WeakBindingRetainsClassIfIPropertyChangedBindingRetained() + { + var binding = new BindingClass(); + + // Means of determining whether the class has been disposed + var weakBinding = new WeakReference(binding); + + var notifying = new NotifyingClass(); + // Retain this + var binder = binding.BindWeak(notifying); + + binding = null; + GC.Collect(); + Assert.IsTrue(weakBinding.TryGetTarget(out binding)); + } + [Test] public void WeakBindingDoesNotRetainNotifier() { @@ -191,5 +207,17 @@ namespace StyletUnitTests GC.Collect(); Assert.IsFalse(weakNotifying.TryGetTarget(out notifying)); } + + [Test] + public void WeakBindingUnbinds() + { + string newVal = null; + var c1 = new NotifyingClass(); + var binding = c1.BindWeak(this, x => x.Bar, x => newVal = x); + binding.Unbind(); + c1.Bar = "bar"; + + Assert.AreEqual(null, newVal); + } } }