summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-08-10 05:51:33 -0600
committerStephane Delcroix <stephane@delcroix.org>2016-08-10 13:51:33 +0200
commit1823c1ad1463aa30deb289bc9538a68a5316164b (patch)
tree1bfde6ef72b26311f728f507f750b5961483dec2 /Xamarin.Forms.Core
parent43e90a38840600a2556be38cc331de4a0d948834 (diff)
downloadxamarin-forms-1823c1ad1463aa30deb289bc9538a68a5316164b.tar.gz
xamarin-forms-1823c1ad1463aa30deb289bc9538a68a5316164b.tar.bz2
xamarin-forms-1823c1ad1463aa30deb289bc9538a68a5316164b.zip
Unit tests for the PCL WeakEventManager (#280)
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r--Xamarin.Forms.Core/WeakEventManager.cs47
1 files changed, 32 insertions, 15 deletions
diff --git a/Xamarin.Forms.Core/WeakEventManager.cs b/Xamarin.Forms.Core/WeakEventManager.cs
index 226f329a..11dabfb8 100644
--- a/Xamarin.Forms.Core/WeakEventManager.cs
+++ b/Xamarin.Forms.Core/WeakEventManager.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
+using static System.String;
namespace Xamarin.Forms
{
@@ -12,7 +13,7 @@ namespace Xamarin.Forms
public void AddEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler)
where TEventArgs : EventArgs
{
- if (eventName == null)
+ if (IsNullOrEmpty(eventName))
{
throw new ArgumentNullException(nameof(eventName));
}
@@ -27,7 +28,7 @@ namespace Xamarin.Forms
public void AddEventHandler(string eventName, EventHandler handler)
{
- if (eventName == null)
+ if (IsNullOrEmpty(eventName))
{
throw new ArgumentNullException(nameof(eventName));
}
@@ -43,24 +44,38 @@ namespace Xamarin.Forms
public void HandleEvent(object sender, object args, string eventName)
{
var toRaise = new List<Tuple<object, MethodInfo>>();
+ var toRemove = new List<Subscription>();
List<Subscription> target;
if (_eventHandlers.TryGetValue(eventName, out target))
{
foreach (Subscription subscription in target)
{
+ bool isStatic = subscription.Subscriber == null;
+ if (isStatic)
+ {
+ // For a static method, we'll just pass null as the first parameter of MethodInfo.Invoke
+ toRaise.Add(Tuple.Create<object, MethodInfo>(null, subscription.Handler));
+ continue;
+ }
+
object subscriber = subscription.Subscriber.Target;
if (subscriber == null)
{
// The subscriber was collected, so there's no need to keep this subscription around
- target.Remove(subscription);
+ toRemove.Add(subscription);
}
else
{
toRaise.Add(Tuple.Create(subscriber, subscription.Handler));
}
}
+
+ foreach (Subscription subscription in toRemove)
+ {
+ target.Remove(subscription);
+ }
}
foreach (Tuple<object, MethodInfo> tuple in toRaise)
@@ -72,7 +87,7 @@ namespace Xamarin.Forms
public void RemoveEventHandler<TEventArgs>(string eventName, EventHandler<TEventArgs> handler)
where TEventArgs : EventArgs
{
- if (eventName == null)
+ if (IsNullOrEmpty(eventName))
{
throw new ArgumentNullException(nameof(eventName));
}
@@ -87,7 +102,7 @@ namespace Xamarin.Forms
public void RemoveEventHandler(string eventName, EventHandler handler)
{
- if (eventName == null)
+ if (IsNullOrEmpty(eventName))
{
throw new ArgumentNullException(nameof(eventName));
}
@@ -102,14 +117,21 @@ namespace Xamarin.Forms
void AddEventHandler(string eventName, object handlerTarget, MethodInfo methodInfo)
{
- List<Subscription> target;
- if (!_eventHandlers.TryGetValue(eventName, out target))
+ List<Subscription> targets;
+ if (!_eventHandlers.TryGetValue(eventName, out targets))
{
- target = new List<Subscription>();
- _eventHandlers.Add(eventName, target);
+ targets = new List<Subscription>();
+ _eventHandlers.Add(eventName, targets);
}
- target.Add(new Subscription(new WeakReference(handlerTarget), methodInfo));
+ if (handlerTarget == null)
+ {
+ // This event handler is a static method
+ targets.Add(new Subscription(null, methodInfo));
+ return;
+ }
+
+ targets.Add(new Subscription(new WeakReference(handlerTarget), methodInfo));
}
void RemoveEventHandler(string eventName, object handlerTarget, MemberInfo methodInfo)
@@ -138,11 +160,6 @@ namespace Xamarin.Forms
{
public Subscription(WeakReference subscriber, MethodInfo handler)
{
- if (subscriber == null)
- {
- throw new ArgumentNullException(nameof(subscriber));
- }
-
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));