Esempio n. 1
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);
        }
Esempio n. 2
0
        public void UnsubscribeShouldNotFailWithNonSubscriber()
        {
            TestableCompositePresentationEvent <string> compositePresentationEvent = new TestableCompositePresentationEvent <string>();

            Action <string> subscriber = delegate { };

            compositePresentationEvent.Unsubscribe(subscriber);
        }
Esempio 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());
        }
Esempio 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);
        }
Esempio n. 5
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>());
        }
Esempio n. 6
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));
        }
Esempio n. 7
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));
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public void CanSubscribeAndRaiseCustomEvent()
        {
            var     customEvent = new TestableCompositePresentationEvent <Payload>();
            Payload payload     = new Payload();
            var     action      = new ActionHelper();

            customEvent.Subscribe(action.Action);

            customEvent.Publish(payload);

            Assert.AreSame(action.ActionArg <Payload>(), payload);
        }
Esempio n. 10
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));
        }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
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");
        }
Esempio n. 14
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);
        }
Esempio n. 15
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)));
        }
Esempio n. 16
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);
        }