コード例 #1
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(actionDelegateReference, filterDelegateReference, mockSyncContext);

            var executionStrategy = eventSubscription.GetExecutionStrategy();

            Assert.NotNull(executionStrategy);

            object argument1 = new();

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

            Assert.Same(argument1, mockSyncContext.InvokeArg);
        }
コード例 #2
0
        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);
        }
コード例 #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]);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
 public void NullTargetInActionThrowsNonGeneric()
 {
     Assert.Throws <ArgumentException>(() =>
     {
         var actionDelegateReference = new MockDelegateReference()
         {
             Target = null
         };
         var eventSubscription = new EventSubscription(actionDelegateReference);
     });
 }
コード例 #6
0
 public void NullActionThrows()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var filterDelegateReference = new MockDelegateReference()
         {
             Target = (Predicate <object>)(arg => true)
         };
         var eventSubscription = new EventSubscription <object>(null, filterDelegateReference);
     });
 }
コード例 #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);
        }
コード例 #8
0
        public void DifferentTargetTypeInActionThrowsNonGeneric()
        {
            Assert.Throws <ArgumentException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action <int>) delegate { }
                };

                var eventSubscription = new EventSubscription(actionDelegateReference);
            });
        }
コード例 #9
0
        public void NullFilterThrows()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var actionDelegateReference = new MockDelegateReference()
                {
                    Target = (Action <object>) delegate { }
                };

                var eventSubscription = new EventSubscription <object>(actionDelegateReference,
                                                                       null);
            });
        }
コード例 #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);
        }
コード例 #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);
     });
 }
コード例 #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);
        }
コード例 #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);
        }
コード例 #14
0
        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);
        }
コード例 #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);
        }
コード例 #16
0
        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);
        }
コード例 #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);
        }