summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests
diff options
context:
space:
mode:
authorShane Neuville <shane94@hotmail.com>2017-03-24 12:58:28 -0600
committerJason Smith <jason.smith@xamarin.com>2017-03-24 11:58:28 -0700
commit377d24fd05e7fb597c4f9237c1596ed4fbf86f19 (patch)
tree9e7ddf6daf4a954d24d4087a0fde32baef30d776 /Xamarin.Forms.Core.UnitTests
parent63af84080454e7b108208b6e8e67bbf503e41209 (diff)
downloadxamarin-forms-377d24fd05e7fb597c4f9237c1596ed4fbf86f19.tar.gz
xamarin-forms-377d24fd05e7fb597c4f9237c1596ed4fbf86f19.tar.bz2
xamarin-forms-377d24fd05e7fb597c4f9237c1596ed4fbf86f19.zip
Setup a ConditionalWeakTable in ListProxy to hold weak references so that a ListView will work when connected to a Weak Source (#802)
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests')
-rw-r--r--Xamarin.Forms.Core.UnitTests/ListProxyTests.cs92
1 files changed, 92 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs b/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs
index 6fa85d2c..bf7f6eef 100644
--- a/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs
+++ b/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs
@@ -422,5 +422,97 @@ namespace Xamarin.Forms.Core.UnitTests
return Items.GetEnumerator ();
}
}
+
+ [Test]
+ public void WeakToWeak()
+ {
+ WeakCollectionChangedList list = new WeakCollectionChangedList();
+ var proxy = new ListProxy(list);
+
+ Assert.True(list.AddObject());
+
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ GC.Collect();
+
+ Assert.IsTrue(list.AddObject());
+
+ proxy = null;
+
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ GC.Collect();
+
+ Assert.IsFalse(list.AddObject());
+ }
+
+ public class WeakCollectionChangedList : List<object>, INotifyCollectionChanged
+ {
+ List<WeakHandler> handlers = new List<WeakHandler>();
+
+ public WeakCollectionChangedList()
+ {
+
+ }
+ public event NotifyCollectionChangedEventHandler CollectionChanged
+ {
+ add { handlers.Add(new WeakHandler(this, value)); }
+ remove { throw new NotImplementedException(); }
+ }
+
+
+ public bool AddObject()
+ {
+ bool invoked = false;
+ var me = new object();
+
+ foreach (var handler in handlers.ToList())
+ {
+ if (handler.IsActive)
+ {
+ invoked = true;
+ handler.Handler.DynamicInvoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, me));
+ }
+ else
+ {
+ handlers.Remove(handler);
+ }
+ }
+
+ return invoked;
+ }
+
+ class WeakHandler
+ {
+ WeakReference source;
+ WeakReference originalHandler;
+
+ public bool IsActive
+ {
+ get { return this.source != null && this.source.IsAlive && this.originalHandler != null && this.originalHandler.IsAlive; }
+ }
+
+ public NotifyCollectionChangedEventHandler Handler
+ {
+ get
+ {
+ if (this.originalHandler == null)
+ {
+ return default(NotifyCollectionChangedEventHandler);
+ }
+ else
+ {
+ return (NotifyCollectionChangedEventHandler)this.originalHandler.Target;
+ }
+ }
+ }
+
+ public WeakHandler(object source, NotifyCollectionChangedEventHandler originalHandler)
+ {
+ this.source = new WeakReference(source);
+ this.originalHandler = new WeakReference(originalHandler);
+ }
+ }
+ }
}
}