コード例 #1
0
        public void NotifyGenericMessageTest()
        {
            var listenerAlpha = new GenericHandler <Alpha>();
            var listenerBeta  = new GenericHandler <Beta>();
            IEventAggregator eventAggregator = CreateEventAggregator();

            eventAggregator.Subscribe(listenerAlpha);
            eventAggregator.Subscribe(listenerBeta);

            //Publish about alpha.
            var alpha = new Alpha();

            eventAggregator.Publish(eventAggregator, alpha);
            listenerAlpha.Count.ShouldEqual(1);
            listenerAlpha.Message.ShouldEqual(alpha);
            listenerAlpha.Sender.ShouldEqual(eventAggregator);
            listenerBeta.Count.ShouldEqual(0);

            //Publish about beta.
            var beta = new Beta();

            eventAggregator.Publish(eventAggregator, beta);
            listenerAlpha.Count.ShouldEqual(2);
            listenerAlpha.Message.ShouldEqual(beta);
            listenerAlpha.Sender.ShouldEqual(eventAggregator);
            listenerBeta.Count.ShouldEqual(1);
            listenerBeta.Message.ShouldEqual(beta);
            listenerBeta.Sender.ShouldEqual(eventAggregator);
        }
コード例 #2
0
        public void NotifySelfShouldNotThrowExceptionTest()
        {
            var listener          = new GenericHandler <object>();
            var containerListener = new GenericHandler <object>();
            var observable        = new TestObservable();

            observable.Subscribe(containerListener);

            IEventAggregator eventAggregator = CreateEventAggregator();

            eventAggregator.Subscribe(listener);
            eventAggregator.Subscribe(observable);
            observable.Subscribe(new TestObservable {
                Listeners = eventAggregator
            });

            eventAggregator.Publish(this, eventAggregator);
            listener.Count.ShouldEqual(1);
            listener.Sender.ShouldEqual(this);
            listener.Message.ShouldEqual(eventAggregator);

            containerListener.Count.ShouldEqual(0);
            containerListener.Sender.ShouldBeNull();
            containerListener.Message.ShouldBeNull();
        }
コード例 #3
0
        public void InstanceMethodUnsubscribeShouldBeSupported()
        {
            var listener = new GenericHandler <object>();
            IEventAggregator eventAggregator = CreateEventAggregator();
            var subscriber = eventAggregator.Subscribe <string>(listener.Handle, false);

            eventAggregator.Contains(subscriber).ShouldBeTrue();
            eventAggregator.GetSubscribers().Count.ShouldEqual(1);

            eventAggregator.Unsubscribe <object>(listener.Handle);
            eventAggregator.GetSubscribers().ShouldBeEmpty();
        }
コード例 #4
0
        public void NotifyOneListenerTest()
        {
            var listener = new GenericHandler <object>();
            IEventAggregator eventAggregator = CreateEventAggregator();

            eventAggregator.Subscribe(listener);

            eventAggregator.Publish(this, eventAggregator);
            listener.Count.ShouldEqual(1);
            listener.Sender.ShouldEqual(this);
            listener.Message.ShouldEqual(eventAggregator);
        }
コード例 #5
0
        public void ClearTest()
        {
            var listener = new GenericHandler <object>();
            IEventAggregator eventAggregator = CreateEventAggregator();
            var subscriber = eventAggregator.Subscribe(listener);

            eventAggregator.Contains(subscriber).ShouldBeTrue();
            eventAggregator.GetSubscribers().Count.ShouldEqual(1);

            eventAggregator.UnsubscribeAll();
            eventAggregator.GetSubscribers().Count.ShouldEqual(0);
        }
コード例 #6
0
        public void RemoveWeakListenerTest()
        {
            var listener = new GenericHandler <object>();
            IEventAggregator eventAggregator = CreateEventAggregator();

            eventAggregator.Subscribe(listener);
            eventAggregator.GetObservers().Contains(listener).ShouldBeTrue();
            eventAggregator.GetObservers().Count.ShouldEqual(1);

            eventAggregator.Unsubscribe(listener).ShouldBeTrue();
            eventAggregator.GetObservers().Count.ShouldEqual(0);
        }
コード例 #7
0
        public void AddWeakListenerTest()
        {
            var listener = new GenericHandler <object>();
            IEventAggregator eventAggregator = CreateEventAggregator();
            var subscriber = eventAggregator.Subscribe(listener);

            eventAggregator.Contains(subscriber).ShouldBeTrue();
            eventAggregator.GetSubscribers().Count.ShouldEqual(1);

            listener = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            eventAggregator.GetSubscribers().Count.ShouldEqual(0);
        }
コード例 #8
0
        public void NotifyListenerWithEmptyContainerTest()
        {
            var listener   = new GenericHandler <object>();
            var observable = new TestObservable {
                Listeners = null
            };

            IEventAggregator eventAggregator = CreateEventAggregator();

            eventAggregator.Subscribe(listener);
            eventAggregator.Subscribe(observable);
            eventAggregator.Publish(this, eventAggregator);
            listener.Count.ShouldEqual(1);
            listener.Sender.ShouldEqual(this);
            listener.Message.ShouldEqual(eventAggregator);
        }
コード例 #9
0
        public void InstanceNonWeakMethodShouldBeSupported()
        {
            var message  = new object();
            var listener = new GenericHandler <object>();
            IEventAggregator eventAggregator = CreateEventAggregator();
            var subscriber = eventAggregator.Subscribe <object>(listener.Handle, false);

            eventAggregator.Contains(subscriber).ShouldBeTrue();
            eventAggregator.GetSubscribers().Count.ShouldEqual(1);

            eventAggregator.Publish(eventAggregator, message);
            listener.Sender.ShouldEqual(eventAggregator);
            listener.Message.ShouldEqual(message);
            listener.Count.ShouldEqual(1);

            listener = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            eventAggregator.Contains(subscriber).ShouldBeTrue();
        }
コード例 #10
0
        public void NotifyListenerWithContainerTest()
        {
            var listener          = new GenericHandler <object>();
            var containerListener = new GenericHandler <object>();
            var stateChanged      = new TestObservable();

            stateChanged.Subscribe(containerListener);

            IEventAggregator eventAggregator = CreateEventAggregator();

            eventAggregator.Subscribe(listener);
            eventAggregator.Subscribe(stateChanged);
            eventAggregator.Publish(this, eventAggregator);
            listener.Count.ShouldEqual(1);
            listener.Sender.ShouldEqual(this);
            listener.Message.ShouldEqual(eventAggregator);

            containerListener.Count.ShouldEqual(0);
            containerListener.Sender.ShouldBeNull();
            containerListener.Message.ShouldBeNull();
        }