PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Tests/Events/EventBaseFixture.cs

#
C# | 130 lines | 88 code | 26 blank | 16 comment | 0 complexity | 7e4dd4e58f93ac78a7fb2a8d76771618 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using Microsoft.Practices.Composite.Events;
 19using Microsoft.VisualStudio.TestTools.UnitTesting;
 20
 21namespace Microsoft.Practices.Composite.Tests.Events
 22{
 23    [TestClass]
 24    public class EventBaseFixture
 25    {
 26        [TestMethod]
 27        public void CanPublishSimpleEvents()
 28        {
 29            var eventBase = new TestableEventBase();
 30            var eventSubscription = new MockEventSubscription();
 31            bool eventPublished = false;
 32            eventSubscription.GetPublishActionReturnValue = delegate
 33                                                                {
 34                                                                    eventPublished = true;
 35                                                                };
 36            eventBase.Subscribe(eventSubscription);
 37
 38            eventBase.Publish();
 39
 40            Assert.IsTrue(eventSubscription.GetPublishActionCalled);
 41            Assert.IsTrue(eventPublished);
 42        }
 43
 44        [TestMethod]
 45        public void CanHaveMultipleSubscribersAndRaiseCustomEvent()
 46        {
 47            var customEvent = new TestableEventBase();
 48            Payload payload = new Payload();
 49            object[] received1 = null;
 50            object[] received2 = null;
 51            var eventSubscription1 = new MockEventSubscription();
 52            eventSubscription1.GetPublishActionReturnValue = delegate(object[] args) { received1 = args; };
 53            var eventSubscription2 = new MockEventSubscription();
 54            eventSubscription2.GetPublishActionReturnValue = delegate(object[] args) { received2 = args; };
 55
 56            customEvent.Subscribe(eventSubscription1);
 57            customEvent.Subscribe(eventSubscription2);
 58
 59            customEvent.Publish(payload);
 60
 61            Assert.AreEqual(1, received1.Length);
 62            Assert.AreSame(received1[0], payload);
 63
 64            Assert.AreEqual(1, received2.Length);
 65            Assert.AreSame(received2[0], payload);
 66        }
 67
 68        [TestMethod]
 69        public void ShouldSubscribeAndUnsubscribe()
 70        {
 71            var eventBase = new TestableEventBase();
 72
 73            var eventSubscription = new MockEventSubscription();
 74            eventBase.Subscribe(eventSubscription);
 75
 76            Assert.IsNotNull(eventSubscription.SubscriptionToken);
 77            Assert.IsTrue(eventBase.Contains(eventSubscription.SubscriptionToken));
 78
 79            eventBase.Unsubscribe(eventSubscription.SubscriptionToken);
 80
 81            Assert.IsFalse(eventBase.Contains(eventSubscription.SubscriptionToken));
 82        }
 83
 84        [TestMethod]
 85        public void WhenEventSubscriptionActionIsNullPruneItFromList()
 86        {
 87            var eventBase = new TestableEventBase();
 88
 89            var eventSubscription = new MockEventSubscription();
 90            eventSubscription.GetPublishActionReturnValue = null;
 91
 92            var token = eventBase.Subscribe(eventSubscription);
 93
 94            eventBase.Publish();
 95
 96            Assert.IsFalse(eventBase.Contains(token));
 97        }
 98
 99
100        class TestableEventBase : EventBase
101        {
102            public SubscriptionToken Subscribe(IEventSubscription subscription)
103            {
104                return base.InternalSubscribe(subscription);
105            }
106
107            public void Publish(params object[] arguments)
108            {
109                base.InternalPublish(arguments);
110            }
111        }
112
113        class MockEventSubscription : IEventSubscription
114        {
115            public Action<object[]> GetPublishActionReturnValue;
116            public bool GetPublishActionCalled;
117
118            public Action<object[]> GetExecutionStrategy()
119            {
120                GetPublishActionCalled = true;
121                return GetPublishActionReturnValue;
122            }
123
124            public SubscriptionToken SubscriptionToken { get; set; }
125        }
126
127        class Payload { }
128
129    }
130}