Example #1
0
        public void ShouldReceiveDelegateOnDifferentThread()
        {
            ManualResetEvent completeEvent = new ManualResetEvent(false);

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            SynchronizationContext calledSyncContext = null;
            Action <object>        action            = delegate
            {
                calledSyncContext = SynchronizationContext.Current;
                completeEvent.Set();
            };

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target = action
            };
            IDelegateReference filterDelegateReference = new MockDelegateReference()
            {
                Target = (Predicate <object>) delegate { return(true); }
            };

            var eventSubscription = new BackgroundEventSubscription <object>(actionDelegateReference, filterDelegateReference);


            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(publishAction);

            publishAction.Invoke(null);

            completeEvent.WaitOne(5000);

            Assert.AreNotEqual(SynchronizationContext.Current, calledSyncContext);
        }
Example #2
0
        public void GetPublishActionReturnsDelegateThatExecutesTheFilterAndThenTheAction()
        {
            var executedDelegates       = new List <string>();
            var actionDelegateReference =
                new MockDelegateReference((Action <object>) delegate { executedDelegates.Add("Action"); });

            var filterDelegateReference = new MockDelegateReference((Predicate <object>) delegate
            {
                executedDelegates.Add(
                    "Filter");
                return(true);
            });

            var eventSubscription = new EventSubscription <object>(actionDelegateReference, filterDelegateReference);


            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(publishAction);

            publishAction.Invoke(null);

            Assert.AreEqual(2, executedDelegates.Count);
            Assert.AreEqual("Filter", executedDelegates[0]);
            Assert.AreEqual("Action", executedDelegates[1]);
        }
        public void ShouldReceiveDelegateOnDifferentThread()
        {
            ManualResetEvent completeEvent = new ManualResetEvent(false);
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            SynchronizationContext calledSyncContext = null;
            Action<object> action = delegate
            {
                calledSyncContext = SynchronizationContext.Current;
                completeEvent.Set();
            };

            IDelegateReference actionDelegateReference = new MockDelegateReference() { Target = action };
            IDelegateReference filterDelegateReference = new MockDelegateReference() { Target = (Predicate<object>)delegate { return true; } };

            var eventSubscription = new BackgroundEventSubscription<object>(actionDelegateReference, filterDelegateReference);


            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(publishAction);

            publishAction.Invoke(null);

            completeEvent.WaitOne(5000);
            
            Assert.AreNotEqual(SynchronizationContext.Current, calledSyncContext);
        }
        public void ShouldPassParametersCorrectly()
        {
            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target =
                    (Action<object>)(arg1 =>
                    {
                        return;
                    })
            };
            IDelegateReference filterDelegateReference = new MockDelegateReference
            {
                Target = (Predicate<object>)(arg => true)
            };

            var mockSyncContext = new MockSynchronizationContext();

            DispatcherEventSubscription<object> eventSubscription = new DispatcherEventSubscription<object>(actionDelegateReference, filterDelegateReference, mockSyncContext);

            var executionStrategy = eventSubscription.GetExecutionStrategy();
            Assert.IsNotNull(executionStrategy);

            object argument1 = new object();

            executionStrategy.Invoke(new[] { argument1 });

            Assert.AreSame(argument1, mockSyncContext.InvokeArg);
        }
Example #5
0
        public void ShouldPassParametersCorrectly()
        {
            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target =
                    (Action <object>)(arg1 =>
                {
                    return;
                })
            };
            IDelegateReference filterDelegateReference = new MockDelegateReference
            {
                Target = (Predicate <object>)(arg => true)
            };

            var mockSyncContext = new MockSynchronizationContext();

            DispatcherEventSubscription <object> eventSubscription = new DispatcherEventSubscription <object>(actionDelegateReference, filterDelegateReference, mockSyncContext);

            var executionStrategy = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(executionStrategy);

            object argument1 = new object();

            executionStrategy.Invoke(new[] { argument1 });

            Assert.AreSame(argument1, mockSyncContext.InvokeArg);
        }
        public void NullFilterThrows()
        {
            var actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action <object>) delegate { }
            };

            var eventSubscription = new EventSubscription <object>(actionDelegateReference, null);
        }
 public void NullActionThrows()
 {
     var filterDelegateReference = new MockDelegateReference()
     {
         Target = (Predicate <object>)(arg =>
         {
             return(true);
         })
     };
     var eventSubscription = new EventSubscription <object>(null, filterDelegateReference);
 }
 public void NullActionThrows()
 {
     var filterDelegateReference = new MockDelegateReference()
     {
         Target = (Predicate<object>)(arg =>
         {
             return true;
         })
     };
     var eventSubscription = new EventSubscription<object>(null, filterDelegateReference);
 }
Example #9
0
        public void NullFilterThrows()
        {
            Assert.ThrowsException <ArgumentNullException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action <object>) delegate { }
                };

                var eventSubscription = new EventSubscription <object>(actionDelegateReference,
                                                                       null);
            });
        }
Example #10
0
        public void CanInitEventSubscription()
        {
            var actionDelegateReference = new MockDelegateReference((Action <object>) delegate { });
            var filterDelegateReference = new MockDelegateReference((Predicate <object>) delegate { return(true); });
            var eventSubscription       = new EventSubscription <object>(actionDelegateReference, filterDelegateReference);

            var subscriptionToken = new SubscriptionToken(t => { });

            eventSubscription.SubscriptionToken = subscriptionToken;

            Assert.AreSame(actionDelegateReference.Target, eventSubscription.Action);
            Assert.AreSame(filterDelegateReference.Target, eventSubscription.Filter);
            Assert.AreSame(subscriptionToken, eventSubscription.SubscriptionToken);
        }
        public void NullTargetInFilterThrows()
        {
            var actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action<object>)delegate { }
            };

            var filterDelegateReference = new MockDelegateReference()
            {
                Target = null
            };
            var eventSubscription = new EventSubscription<object>(actionDelegateReference,
                                                                                filterDelegateReference);
        }
 public void DifferentTargetTypeInActionThrows()
 {
     var actionDelegateReference = new MockDelegateReference()
     {
         Target = (Action<int>)delegate { }
     };
     var filterDelegateReference = new MockDelegateReference()
     {
         Target = (Predicate<string>)(arg =>
         {
             return true;
         })
     };
     var eventSubscription = new EventSubscription<string>(actionDelegateReference,
                                                                         filterDelegateReference);
 }
 public void DifferentTargetTypeInActionThrows()
 {
     var actionDelegateReference = new MockDelegateReference()
     {
         Target = (Action <int>) delegate { }
     };
     var filterDelegateReference = new MockDelegateReference()
     {
         Target = (Predicate <string>)(arg =>
         {
             return(true);
         })
     };
     var eventSubscription = new EventSubscription <string>(actionDelegateReference,
                                                            filterDelegateReference);
 }
        public void NullTargetInFilterThrows()
        {
            Assert.ThrowsException<ArgumentException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action<object>)delegate { }
                };

                var filterDelegateReference = new MockDelegateReference()
                {
                    Target = null
                };
                var eventSubscription = new EventSubscription<object>(actionDelegateReference,
                                                                                filterDelegateReference);
            });
        }
Example #15
0
        public void GetPublishActionReturnsNullIfFilterIsNull()
        {
            var actionDelegateReference = new MockDelegateReference((Action <object>) delegate { });
            var filterDelegateReference = new MockDelegateReference((Predicate <object>) delegate { return(true); });

            var eventSubscription = new EventSubscription <object>(actionDelegateReference, filterDelegateReference);

            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(publishAction);

            filterDelegateReference.Target = null;

            publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNull(publishAction);
        }
Example #16
0
 public void NullTargetInActionThrows()
 {
     Assert.ThrowsException <ArgumentException>(() =>
     {
         var actionDelegateReference = new MockDelegateReference()
         {
             Target = null
         };
         var filterDelegateReference = new MockDelegateReference()
         {
             Target = (Predicate <object>)(arg =>
             {
                 return(true);
             })
         };
         var eventSubscription = new EventSubscription <object>(actionDelegateReference,
                                                                filterDelegateReference);
     });
 }
 public void NullTargetInActionThrows()
 {
     Assert.ThrowsException<ArgumentException>(() =>
     {
         var actionDelegateReference = new MockDelegateReference()
         {
             Target = null
         };
         var filterDelegateReference = new MockDelegateReference()
         {
             Target = (Predicate<object>)(arg =>
             {
                 return true;
             })
         };
         var eventSubscription = new EventSubscription<object>(actionDelegateReference,
                                                                         filterDelegateReference);
     });
 }
Example #18
0
        public void StrategyPassesArgumentToDelegates()
        {
            string passedArgumentToAction = null;
            string passedArgumentToFilter = null;

            var actionDelegateReference = new MockDelegateReference((Action <string>)(obj => passedArgumentToAction = obj));
            var filterDelegateReference = new MockDelegateReference((Predicate <string>)(obj =>
            {
                passedArgumentToFilter = obj;
                return(true);
            }));

            var eventSubscription = new EventSubscription <string>(actionDelegateReference, filterDelegateReference);
            var publishAction     = eventSubscription.GetExecutionStrategy();

            publishAction.Invoke(new[] { "TestString" });

            Assert.AreEqual("TestString", passedArgumentToAction);
            Assert.AreEqual("TestString", passedArgumentToFilter);
        }
Example #19
0
        public void DifferentTargetTypeInFilterThrows()
        {
            Assert.ThrowsException <ArgumentException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action <string>) delegate { }
                };

                var filterDelegateReference = new MockDelegateReference()
                {
                    Target = (Predicate <int>)(arg =>
                    {
                        return(true);
                    })
                };
                var eventSubscription = new EventSubscription <string>(actionDelegateReference,
                                                                       filterDelegateReference);
            });
        }
Example #20
0
        public void GetPublishActionDoesNotExecuteActionIfFilterReturnsFalse()
        {
            bool actionExecuted          = false;
            var  actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action <int>) delegate { actionExecuted = true; }
            };
            var filterDelegateReference = new MockDelegateReference((Predicate <int>) delegate
            {
                return(false);
            });

            var eventSubscription = new EventSubscription <int>(actionDelegateReference, filterDelegateReference);


            var publishAction = eventSubscription.GetExecutionStrategy();

            publishAction.Invoke(new object[] { null });

            Assert.IsFalse(actionExecuted);
        }
        public void ShouldCallInvokeOnDispatcher()
        {
            DispatcherEventSubscription<object> eventSubscription = null;

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action<object>)(arg =>
                {
                    return;
                })
            };

            IDelegateReference filterDelegateReference = new MockDelegateReference
            {
                Target = (Predicate<object>)(arg => true)
            };
            var mockSyncContext = new MockSynchronizationContext();

            eventSubscription = new DispatcherEventSubscription<object>(actionDelegateReference, filterDelegateReference, mockSyncContext);

            eventSubscription.GetExecutionStrategy().Invoke(new object[0]);

            Assert.IsTrue(mockSyncContext.InvokeCalled);
        }
Example #22
0
        public void ShouldCallInvokeOnDispatcher()
        {
            DispatcherEventSubscription <object> eventSubscription = null;

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action <object>)(arg =>
                {
                    return;
                })
            };

            IDelegateReference filterDelegateReference = new MockDelegateReference
            {
                Target = (Predicate <object>)(arg => true)
            };
            var mockSyncContext = new MockSynchronizationContext();

            eventSubscription = new DispatcherEventSubscription <object>(actionDelegateReference, filterDelegateReference, mockSyncContext);

            eventSubscription.GetExecutionStrategy().Invoke(new object[0]);

            Assert.IsTrue(mockSyncContext.InvokeCalled);
        }
        public void CanInitEventSubscription()
        {
            var actionDelegateReference = new MockDelegateReference((Action<object>)delegate { });
            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate { return true; });
            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);

            var subscriptionToken = new SubscriptionToken(t => { });

            eventSubscription.SubscriptionToken = subscriptionToken;

            Assert.AreSame(actionDelegateReference.Target, eventSubscription.Action);
            Assert.AreSame(filterDelegateReference.Target, eventSubscription.Filter);
            Assert.AreSame(subscriptionToken, eventSubscription.SubscriptionToken);
        }
        public void DifferentTargetTypeInFilterThrows()
        {
            Assert.ThrowsException<ArgumentException>(() =>
            {

                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action<string>)delegate { }
                };

                var filterDelegateReference = new MockDelegateReference()
                {
                    Target = (Predicate<int>)(arg =>
                    {
                        return true;
                    })
                };
                var eventSubscription = new EventSubscription<string>(actionDelegateReference,
                                                                                filterDelegateReference);
            });
        }
        public void GetPublishActionReturnsNullIfFilterIsNull()
        {
            var actionDelegateReference = new MockDelegateReference((Action<object>)delegate { });
            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate { return true; });

            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);

            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(publishAction);

            filterDelegateReference.Target = null;

            publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNull(publishAction);
        }
        public void GetPublishActionDoesNotExecuteActionIfFilterReturnsFalse()
        {
            bool actionExecuted = false;
            var actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action<int>)delegate { actionExecuted = true; }
            };
            var filterDelegateReference = new MockDelegateReference((Predicate<int>)delegate
                                                                                            {
                                                                                                return false;
                                                                                            });

            var eventSubscription = new EventSubscription<int>(actionDelegateReference, filterDelegateReference);


            var publishAction = eventSubscription.GetExecutionStrategy();

            publishAction.Invoke(new object[] { null });

            Assert.IsFalse(actionExecuted);
        }
        public void StrategyPassesArgumentToDelegates()
        {
            string passedArgumentToAction = null;
            string passedArgumentToFilter = null;

            var actionDelegateReference = new MockDelegateReference((Action<string>)(obj => passedArgumentToAction = obj));
            var filterDelegateReference = new MockDelegateReference((Predicate<string>)(obj =>
                                                                                            {
                                                                                                passedArgumentToFilter = obj;
                                                                                                return true;
                                                                                            }));

            var eventSubscription = new EventSubscription<string>(actionDelegateReference, filterDelegateReference);
            var publishAction = eventSubscription.GetExecutionStrategy();

            publishAction.Invoke(new[] { "TestString" });

            Assert.AreEqual("TestString", passedArgumentToAction);
            Assert.AreEqual("TestString", passedArgumentToFilter);
        }
        public void GetPublishActionReturnsDelegateThatExecutesTheFilterAndThenTheAction()
        {
            var executedDelegates = new List<string>();
            var actionDelegateReference =
                new MockDelegateReference((Action<object>)delegate { executedDelegates.Add("Action"); });

            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate
                                                {
                                                    executedDelegates.Add(
                                                        "Filter");
                                                    return true;

                                                });

            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);


            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.IsNotNull(publishAction);

            publishAction.Invoke(null);

            Assert.AreEqual(2, executedDelegates.Count);
            Assert.AreEqual("Filter", executedDelegates[0]);
            Assert.AreEqual("Action", executedDelegates[1]);
        }