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(actionDelegateReference, filterDelegateReference, mockSyncContext);

            var executionStrategy = eventSubscription.GetExecutionStrategy();

            Assert.NotNull(executionStrategy);

            object argument1 = new();

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

            Assert.Same(argument1, mockSyncContext.InvokeArg);
        }
        public void ShouldReceiveDelegateOnDifferentThread()
        {
            ManualResetEvent completeEvent = new(false);

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            SynchronizationContext calledSyncContext = null;

            void action(object obj)
            {
                calledSyncContext = SynchronizationContext.Current;
                completeEvent.Set();
            }

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

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


            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.NotNull(publishAction);

            publishAction.Invoke(null);

            completeEvent.WaitOne(5000);

            Assert.NotEqual(SynchronizationContext.Current, calledSyncContext);
        }
Exemplo n.º 3
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.NotNull(publishAction);

            publishAction.Invoke(null);

            Assert.Equal(2, executedDelegates.Count);
            Assert.Equal("Filter", executedDelegates[0]);
            Assert.Equal("Action", executedDelegates[1]);
        }
        public void ShouldReceiveDelegateOnDifferentThreadNonGeneric()
        {
            var completeEvent = new ManualResetEvent(false);

            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            SynchronizationContext calledSyncContext = null;

            void action()
            {
                calledSyncContext = SynchronizationContext.Current;
                completeEvent.Set();
            }

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action)action
            };

            var eventSubscription = new BackgroundEventSubscription(actionDelegateReference);

            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.NotNull(publishAction);

            publishAction.Invoke(null);

            completeEvent.WaitOne(5000);

            Assert.NotEqual(SynchronizationContext.Current, calledSyncContext);
        }
Exemplo n.º 5
0
 public void NullTargetInActionThrowsNonGeneric()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var actionDelegateReference = new MockDelegateReference()
         {
             Target = null
         };
         var eventSubscription = new EventSubscription(actionDelegateReference);
     });
 }
Exemplo n.º 6
0
 public void NullActionThrows()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var filterDelegateReference = new MockDelegateReference()
         {
             Target = (Predicate <object>)(arg => true)
         };
         var eventSubscription = new EventSubscription <object>(null, filterDelegateReference);
     });
 }
Exemplo n.º 7
0
        public void CanInitEventSubscriptionNonGeneric()
        {
            var actionDelegateReference = new MockDelegateReference((Action) delegate { });
            var eventSubscription       = new EventSubscription(actionDelegateReference);

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

            eventSubscription.SubscriptionToken = subscriptionToken;

            Assert.Same(actionDelegateReference.Target, eventSubscription.Action);
            Assert.Same(subscriptionToken, eventSubscription.SubscriptionToken);
        }
Exemplo n.º 8
0
        public void DifferentTargetTypeInActionThrowsNonGeneric()
        {
            Assert.Throws <ArgumentException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action <int>) delegate { }
                };

                var eventSubscription = new EventSubscription(actionDelegateReference);
            });
        }
Exemplo n.º 9
0
        public void NullFilterThrows()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action <object>) delegate { }
                };

                var eventSubscription = new EventSubscription <object>(actionDelegateReference,
                                                                       null);
            });
        }
Exemplo n.º 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.Same(actionDelegateReference.Target, eventSubscription.Action);
            Assert.Same(filterDelegateReference.Target, eventSubscription.Filter);
            Assert.Same(subscriptionToken, eventSubscription.SubscriptionToken);
        }
Exemplo n.º 11
0
 public void DifferentTargetTypeInActionThrows()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var actionDelegateReference = new MockDelegateReference()
         {
             Target = (Action <int>) delegate { }
         };
         var filterDelegateReference = new MockDelegateReference()
         {
             Target = (Predicate <string>)(arg => true)
         };
         var eventSubscription = new EventSubscription <string>(actionDelegateReference, filterDelegateReference);
     });
 }
Exemplo n.º 12
0
        public void GetPublishActionReturnsNullIfActionIsNullNonGeneric()
        {
            var actionDelegateReference = new MockDelegateReference((Action) delegate { });

            var eventSubscription = new EventSubscription(actionDelegateReference);

            var publishAction = eventSubscription.GetExecutionStrategy();

            Assert.NotNull(publishAction);

            actionDelegateReference.Target = null;

            publishAction = eventSubscription.GetExecutionStrategy();

            Assert.Null(publishAction);
        }
Exemplo n.º 13
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.NotNull(publishAction);

            filterDelegateReference.Target = null;

            publishAction = eventSubscription.GetExecutionStrategy();

            Assert.Null(publishAction);
        }
        public void ShouldCallInvokeOnDispatcherNonGeneric()
        {
            DispatcherEventSubscription eventSubscription = null;

            IDelegateReference actionDelegateReference = new MockDelegateReference()
            {
                Target = (Action)(() =>
                                  { })
            };

            var mockSyncContext = new MockSynchronizationContext();

            eventSubscription = new DispatcherEventSubscription(actionDelegateReference, mockSyncContext);

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

            Assert.True(mockSyncContext.InvokeCalled);
        }
Exemplo n.º 15
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.Equal("TestString", passedArgumentToAction);
            Assert.Equal("TestString", passedArgumentToFilter);
        }
        public void ShouldCallInvokeOnDispatcher()
        {
            DispatcherEventSubscription <object> eventSubscription = null;

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

            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.True(mockSyncContext.InvokeCalled);
        }
Exemplo n.º 17
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.False(actionExecuted);
        }