Exemplo n.º 1
0
        public void CanHaveMultipleSubscribersAndRaiseCustomEvent()
        {
            var     customEvent = new TestableCompositePresentationEvent <Payload>();
            Payload payload     = new Payload();
            var     action1     = new ActionHelper();
            var     action2     = new ActionHelper();

            customEvent.Subscribe(action1.Action);
            customEvent.Subscribe(action2.Action);

            customEvent.Publish(payload);

            Assert.AreSame(action1.ActionArg <Payload>(), payload);
            Assert.AreSame(action2.ActionArg <Payload>(), payload);
        }
Exemplo n.º 2
0
        public void ShouldNotExecuteOnGarbageCollectedFilterReferenceWhenNotKeepAlive()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();

            bool wasCalled   = false;
            var  actionEvent = new ActionHelper()
            {
                ActionToExecute = () => wasCalled = true
            };

            ExternalFilter filter = new ExternalFilter();

            CompositePresentationEvent.Subscribe(actionEvent.Action, ThreadOption.PublisherThread, false, filter.AlwaysTrueFilter);

            CompositePresentationEvent.Publish("testPayload");
            Assert.IsTrue(wasCalled);

            wasCalled = false;
            WeakReference filterReference = new WeakReference(filter);

            filter = null;
            GC.Collect();
            Assert.IsFalse(filterReference.IsAlive);

            CompositePresentationEvent.Publish("testPayload");
            Assert.IsFalse(wasCalled);
        }
Exemplo n.º 3
0
        public void SubscribeDefaultsToPublisherThread()
        {
            var             CompositePresentationEvent = new TestableCompositePresentationEvent <string>();
            Action <string> action = delegate { };
            var             token  = CompositePresentationEvent.Subscribe(action, true);

            Assert.AreEqual(1, CompositePresentationEvent.BaseSubscriptions.Count);
            Assert.AreEqual(typeof(EventSubscription <string>), CompositePresentationEvent.BaseSubscriptions.ElementAt(0).GetType());
        }
Exemplo n.º 4
0
        public void CanSubscribeAndRaiseEvent()
        {
            TestableCompositePresentationEvent <string> compositePresentationEvent = new TestableCompositePresentationEvent <string>();
            bool published = false;

            compositePresentationEvent.Subscribe(delegate { published = true; }, ThreadOption.PublisherThread, true, delegate { return(true); });
            compositePresentationEvent.Publish(null);

            Assert.IsTrue(published);
        }
Exemplo n.º 5
0
        public void FilterEnablesActionTarget()
        {
            TestableCompositePresentationEvent <string> compositePresentationEvent = new TestableCompositePresentationEvent <string>();
            var goodFilter = new MockFilter {
                FilterReturnValue = true
            };
            var actionGoodFilter = new ActionHelper();
            var badFilter        = new MockFilter {
                FilterReturnValue = false
            };
            var actionBadFilter = new ActionHelper();

            compositePresentationEvent.Subscribe(actionGoodFilter.Action, ThreadOption.PublisherThread, true, goodFilter.FilterString);
            compositePresentationEvent.Subscribe(actionBadFilter.Action, ThreadOption.PublisherThread, true, badFilter.FilterString);

            compositePresentationEvent.Publish("test");

            Assert.IsTrue(actionGoodFilter.ActionCalled);
            Assert.IsFalse(actionBadFilter.ActionCalled);
        }
Exemplo n.º 6
0
        public void SubscribeTakesExecuteDelegateThreadOptionAndFilter()
        {
            TestableCompositePresentationEvent <string> compositePresentationEvent = new TestableCompositePresentationEvent <string>();
            var action = new ActionHelper();

            compositePresentationEvent.Subscribe(action.Action);

            compositePresentationEvent.Publish("test");

            Assert.AreEqual("test", action.ActionArg <string>());
        }
Exemplo n.º 7
0
        public void ContainsShouldSearchByToken()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();
            var emptyAction = new ActionHelper();
            var token       = CompositePresentationEvent.Subscribe(emptyAction.Action);

            Assert.IsTrue(CompositePresentationEvent.Contains(token));

            CompositePresentationEvent.Unsubscribe(emptyAction.Action);
            Assert.IsFalse(CompositePresentationEvent.Contains(token));
        }
Exemplo n.º 8
0
        public void RegisterReturnsTokenThatCanBeUsedToUnsubscribe()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();
            var emptyAction = new ActionHelper();

            var token = CompositePresentationEvent.Subscribe(emptyAction.Action);

            CompositePresentationEvent.Unsubscribe(token);

            Assert.IsFalse(CompositePresentationEvent.Contains(emptyAction.Action));
        }
Exemplo n.º 9
0
        public void InlineDelegateDeclarationsDoesNotGetCollectedIncorrectlyWithWeakReferences()
        {
            var  CompositePresentationEvent = new TestableCompositePresentationEvent <string>();
            bool published = false;

            CompositePresentationEvent.Subscribe(delegate { published = true; }, ThreadOption.PublisherThread, false, delegate { return(true); });
            GC.Collect();
            CompositePresentationEvent.Publish(null);

            Assert.IsTrue(published);
        }
Exemplo n.º 10
0
        public void CanAddSubscriptionWhileEventIsFiring()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();

            var emptyAction        = new ActionHelper();
            var subscriptionAction = new ActionHelper
            {
                ActionToExecute = (() =>
                                   CompositePresentationEvent.Subscribe(
                                       emptyAction.Action))
            };

            CompositePresentationEvent.Subscribe(subscriptionAction.Action);

            Assert.IsFalse(CompositePresentationEvent.Contains(emptyAction.Action));

            CompositePresentationEvent.Publish(null);

            Assert.IsTrue((CompositePresentationEvent.Contains(emptyAction.Action)));
        }
Exemplo n.º 11
0
        public void ShouldUnsubscribeASingleDelegate()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();

            int callCount = 0;

            var actionEvent = new ActionHelper()
            {
                ActionToExecute = () => callCount++
            };

            CompositePresentationEvent.Subscribe(actionEvent.Action);
            CompositePresentationEvent.Subscribe(actionEvent.Action);

            CompositePresentationEvent.Publish(null);
            Assert.AreEqual <int>(2, callCount);

            callCount = 0;
            CompositePresentationEvent.Unsubscribe(actionEvent.Action);
            CompositePresentationEvent.Publish(null);
            Assert.AreEqual <int>(1, callCount);
        }
Exemplo n.º 12
0
        public void ShouldUnsubscribeFromUIThread()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();

            var actionEvent = new ActionHelper();

            CompositePresentationEvent.Subscribe(
                actionEvent.Action,
                ThreadOption.UIThread);

            Assert.IsTrue(CompositePresentationEvent.Contains(actionEvent.Action));
            CompositePresentationEvent.Unsubscribe(actionEvent.Action);
            Assert.IsFalse(CompositePresentationEvent.Contains(actionEvent.Action));
        }
Exemplo n.º 13
0
        public void SubscribeDefaultsThreadOptionAndNoFilter()
        {
            TestableCompositePresentationEvent <string> compositePresentationEvent = new TestableCompositePresentationEvent <string>();
            int calledThreadID = -1;
            var myAction       = new ActionHelper()
            {
                ActionToExecute          =
                    () => calledThreadID = Thread.CurrentThread.ManagedThreadId
            };

            compositePresentationEvent.Subscribe(myAction.Action);

            compositePresentationEvent.Publish("test");

            Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, calledThreadID);
        }
Exemplo n.º 14
0
        public void ShouldNotGarbageCollectDelegateReferenceWhenUsingKeepAlive()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();

            var externalAction = new ExternalAction();

            CompositePresentationEvent.Subscribe(externalAction.ExecuteAction, ThreadOption.PublisherThread, true);

            WeakReference actionEventReference = new WeakReference(externalAction);

            externalAction = null;
            GC.Collect();
            GC.Collect();
            Assert.IsTrue(actionEventReference.IsAlive);

            CompositePresentationEvent.Publish("testPayload");

            Assert.AreEqual("testPayload", ((ExternalAction)actionEventReference.Target).PassedValue);
        }
Exemplo n.º 15
0
        public void ShouldNotExecuteOnGarbageCollectedDelegateReferenceWhenNotKeepAlive()
        {
            var CompositePresentationEvent = new TestableCompositePresentationEvent <string>();

            ExternalAction externalAction = new ExternalAction();

            CompositePresentationEvent.Subscribe(externalAction.ExecuteAction);

            CompositePresentationEvent.Publish("testPayload");
            Assert.AreEqual("testPayload", externalAction.PassedValue);

            WeakReference actionEventReference = new WeakReference(externalAction);

            externalAction = null;
            GC.Collect();
            Assert.IsFalse(actionEventReference.IsAlive);

            CompositePresentationEvent.Publish("testPayload");
        }