PageRenderTime 32ms CodeModel.GetById 22ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/spikes/AGCompositeApplicationLibrary/AGComposite.Tests/Events/EventSubscriptionFixture.cs

#
C# | 265 lines | 196 code | 53 blank | 16 comment | 0 complexity | f5cc7538701991a9769eba43fdc8fde7 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//===============================================================================
 17
 18
 19using System;
 20using System.Collections.Generic;
 21using Microsoft.Practices.Composite.Events;
 22using Microsoft.Practices.Composite.Tests.Mocks;
 23using Microsoft.VisualStudio.TestTools.UnitTesting;
 24
 25namespace Microsoft.Practices.Composite.Tests.Events
 26{
 27    [TestClass]
 28    public class EventSubscriptionFixture
 29    {
 30        [TestMethod]
 31        [ExpectedException(typeof(ArgumentException))]
 32        public void NullTargetInActionThrows()
 33        {
 34            var actionDelegateReference = new MockDelegateReference()
 35            {
 36                Target = null
 37            };
 38            var filterDelegateReference = new MockDelegateReference()
 39            {
 40                Target = (Predicate<object>)(arg =>
 41                {
 42                    return true;
 43                })
 44            };
 45            var eventSubscription = new EventSubscription<object>(actionDelegateReference,
 46                                                                            filterDelegateReference);
 47        }
 48
 49        [TestMethod]
 50        [ExpectedException(typeof(ArgumentException))]
 51        public void DifferentTargetTypeInActionThrows()
 52        {
 53            var actionDelegateReference = new MockDelegateReference()
 54            {
 55                Target = (Action<object>)delegate { }
 56            };
 57            var filterDelegateReference = new MockDelegateReference()
 58            {
 59                Target = (Predicate<string>)(arg =>
 60                {
 61                    return true;
 62                })
 63            };
 64            var eventSubscription = new EventSubscription<string>(actionDelegateReference,
 65                                                                            filterDelegateReference);
 66        }
 67
 68        [TestMethod]
 69        [ExpectedException(typeof(ArgumentNullException))]
 70        public void NullActionThrows()
 71        {
 72            var filterDelegateReference = new MockDelegateReference()
 73            {
 74                Target = (Predicate<object>)(arg =>
 75                {
 76                    return true;
 77                })
 78            };
 79            var eventSubscription = new EventSubscription<object>(null,
 80                                                                            filterDelegateReference);
 81        }
 82
 83        [TestMethod]
 84        [ExpectedException(typeof(ArgumentException))]
 85        public void NullTargetInFilterThrows()
 86        {
 87            var actionDelegateReference = new MockDelegateReference()
 88            {
 89                Target = (Action<object>)delegate { }
 90            };
 91
 92            var filterDelegateReference = new MockDelegateReference()
 93            {
 94                Target = null
 95            };
 96            var eventSubscription = new EventSubscription<object>(actionDelegateReference,
 97                                                                            filterDelegateReference);
 98        }
 99
100
101        [TestMethod]
102        [ExpectedException(typeof(ArgumentException))]
103        public void DifferentTargetTypeInFilterThrows()
104        {
105            var actionDelegateReference = new MockDelegateReference()
106            {
107                Target = (Action<string>)delegate { }
108            };
109
110            var filterDelegateReference = new MockDelegateReference()
111            {
112                Target = (Predicate<object>)(arg =>
113                {
114                    return true;
115                })
116            };
117            var eventSubscription = new EventSubscription<string>(actionDelegateReference,
118                                                                            filterDelegateReference);
119        }
120
121        [TestMethod]
122        [ExpectedException(typeof(ArgumentNullException))]
123        public void NullFilterThrows()
124        {
125            var actionDelegateReference = new MockDelegateReference()
126            {
127                Target = (Action<object>)delegate { }
128            };
129
130            var eventSubscription = new EventSubscription<object>(actionDelegateReference,
131                                                                            null);
132        }
133
134        [TestMethod]
135        public void CanInitEventSubscription()
136        {
137            var actionDelegateReference = new MockDelegateReference((Action<object>)delegate { });
138            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate { return true; });
139            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);
140
141            var subscriptionToken = new SubscriptionToken();
142
143            eventSubscription.SubscriptionToken = subscriptionToken;
144
145            Assert.AreSame(actionDelegateReference.Target, eventSubscription.Action);
146            Assert.AreSame(filterDelegateReference.Target, eventSubscription.Filter);
147            Assert.AreSame(subscriptionToken, eventSubscription.SubscriptionToken);
148        }
149
150        [TestMethod]
151        public void GetPublishActionReturnsDelegateThatExecutesTheFilterAndThenTheAction()
152        {
153            var executedDelegates = new List<string>();
154            var actionDelegateReference =
155                new MockDelegateReference((Action<object>)delegate { executedDelegates.Add("Action"); });
156
157            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate
158                                                {
159                                                    executedDelegates.Add(
160                                                        "Filter");
161                                                    return true;
162
163                                                });
164
165            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);
166
167
168            var publishAction = eventSubscription.GetExecutionStrategy();
169
170            Assert.IsNotNull(publishAction);
171
172            publishAction.Invoke(null);
173
174            Assert.AreEqual(2, executedDelegates.Count);
175            Assert.AreEqual("Filter", executedDelegates[0]);
176            Assert.AreEqual("Action", executedDelegates[1]);
177        }
178
179        [TestMethod]
180        public void GetPublishActionReturnsNullIfActionIsNull()
181        {
182            var actionDelegateReference = new MockDelegateReference((Action<object>)delegate { });
183            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate { return true; });
184
185            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);
186
187            var publishAction = eventSubscription.GetExecutionStrategy();
188
189            Assert.IsNotNull(publishAction);
190
191            actionDelegateReference.Target = null;
192
193            publishAction = eventSubscription.GetExecutionStrategy();
194
195            Assert.IsNull(publishAction);
196        }
197
198        [TestMethod]
199        public void GetPublishActionReturnsNullIfFilterIsNull()
200        {
201            var actionDelegateReference = new MockDelegateReference((Action<object>)delegate { });
202            var filterDelegateReference = new MockDelegateReference((Predicate<object>)delegate { return true; });
203
204            var eventSubscription = new EventSubscription<object>(actionDelegateReference, filterDelegateReference);
205
206            var publishAction = eventSubscription.GetExecutionStrategy();
207
208            Assert.IsNotNull(publishAction);
209
210            filterDelegateReference.Target = null;
211
212            publishAction = eventSubscription.GetExecutionStrategy();
213
214            Assert.IsNull(publishAction);
215        }
216
217        [TestMethod]
218        public void GetPublishActionDoesNotExecuteActionIfFilterReturnsFalse()
219        {
220            bool actionExecuted = false;
221            var actionDelegateReference = new MockDelegateReference()
222            {
223                Target = (Action<int>)delegate { actionExecuted = true; }
224            };
225            var filterDelegateReference = new MockDelegateReference((Predicate<int>)delegate
226                                                                                            {
227                                                                                                return false;
228                                                                                            });
229
230            var eventSubscription = new EventSubscription<int>(actionDelegateReference, filterDelegateReference);
231
232
233            var publishAction = eventSubscription.GetExecutionStrategy();
234
235            publishAction.Invoke(new object[] { null });
236
237            Assert.IsFalse(actionExecuted);
238        }
239
240        [TestMethod]
241        public void StrategyPassesArgumentToDelegates()
242        {
243            string passedArgumentToAction = null;
244            string passedArgumentToFilter = null;
245
246            var actionDelegateReference = new MockDelegateReference((Action<string>)(obj => passedArgumentToAction = obj));
247            var filterDelegateReference = new MockDelegateReference((Predicate<string>)(obj =>
248                                                                                            {
249                                                                                                passedArgumentToFilter = obj;
250                                                                                                return true;
251                                                                                            }));
252
253            var eventSubscription = new EventSubscription<string>(actionDelegateReference, filterDelegateReference);
254            var publishAction = eventSubscription.GetExecutionStrategy();
255
256            publishAction.Invoke(new[] { "TestString" });
257
258            Assert.AreEqual("TestString", passedArgumentToAction);
259            Assert.AreEqual("TestString", passedArgumentToFilter);
260        }
261
262
263
264    }
265}