PageRenderTime 51ms CodeModel.GetById 10ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

/V4/PrismLibrary/Desktop/Prism.UnityExtensions.Tests/UnityBootstrapperFixture.cs

#
C# | 285 lines | 221 code | 47 blank | 17 comment | 0 complexity | 392fd4f218d836d9d05ac9cb2d79b1be 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.Collections.Generic;
 18using System.Windows;
 19using System.Windows.Controls;
 20using Microsoft.Practices.Prism.UnityExtensions.Tests.Mocks;
 21using Microsoft.Practices.Prism.Logging;
 22using Microsoft.Practices.Prism.Modularity;
 23using Microsoft.Practices.Prism.Regions;
 24using Microsoft.Practices.Unity;
 25using Microsoft.VisualStudio.TestTools.UnitTesting;
 26
 27namespace Microsoft.Practices.Prism.UnityExtensions.Tests
 28{
 29    [TestClass]
 30    public class UnityBootstrapperFixture: BootstrapperFixtureBase
 31    {
 32        [TestMethod]
 33        public void ContainerDefaultsToNull()
 34        {
 35            var bootstrapper = new DefaultUnityBootstrapper();
 36            var container = bootstrapper.BaseContainer;
 37
 38            Assert.IsNull(container);
 39        }
 40
 41        [TestMethod]
 42        public void CanCreateConcreteBootstrapper()
 43        {
 44            new DefaultUnityBootstrapper();
 45        }
 46
 47        [TestMethod]
 48        public void CreateContainerShouldInitializeContainer()
 49        {
 50            var bootstrapper = new DefaultUnityBootstrapper();
 51
 52            IUnityContainer container = bootstrapper.CallCreateContainer();
 53
 54            Assert.IsNotNull(container);
 55            Assert.IsInstanceOfType(container, typeof(IUnityContainer));
 56        }
 57
 58        [TestMethod]
 59        public void ConfigureContainerAddsModuleCatalogToContainer()
 60        {
 61            var bootstrapper = new DefaultUnityBootstrapper();
 62            bootstrapper.Run();
 63
 64            var returnedCatalog = bootstrapper.BaseContainer.Resolve<IModuleCatalog>();
 65            Assert.IsNotNull(returnedCatalog);
 66            Assert.IsTrue(returnedCatalog is ModuleCatalog);
 67        }
 68
 69        [TestMethod]
 70        public void ConfigureContainerAddsLoggerFacadeToContainer()
 71        {
 72            var bootstrapper = new DefaultUnityBootstrapper();
 73            bootstrapper.Run();
 74
 75            var returnedCatalog = bootstrapper.BaseContainer.Resolve<ILoggerFacade>();
 76            Assert.IsNotNull(returnedCatalog);
 77        }
 78
 79        [TestMethod]
 80        public void ConfigureContainerAddsRegionNavigationJournalEntryToContainer()
 81        {
 82            var bootstrapper = new DefaultUnityBootstrapper();
 83            bootstrapper.Run();
 84
 85            var actual1 = bootstrapper.BaseContainer.Resolve<IRegionNavigationJournalEntry>();
 86            var actual2 = bootstrapper.BaseContainer.Resolve<IRegionNavigationJournalEntry>();
 87
 88            Assert.IsNotNull(actual1);
 89            Assert.IsNotNull(actual2);
 90            Assert.AreNotSame(actual1, actual2);
 91        }
 92
 93        [TestMethod]
 94        public void ConfigureContainerAddsRegionNavigationJournalToContainer()
 95        {
 96            var bootstrapper = new DefaultUnityBootstrapper();
 97            bootstrapper.Run();
 98
 99            var actual1 = bootstrapper.BaseContainer.Resolve<IRegionNavigationJournal>();
100            var actual2 = bootstrapper.BaseContainer.Resolve<IRegionNavigationJournal>();
101
102            Assert.IsNotNull(actual1);
103            Assert.IsNotNull(actual2);
104            Assert.AreNotSame(actual1, actual2);
105        }
106
107        [TestMethod]
108        public void ConfigureContainerAddsRegionNavigationServiceToContainer()
109        {
110            var bootstrapper = new DefaultUnityBootstrapper();
111            bootstrapper.Run();
112
113            var actual1 = bootstrapper.BaseContainer.Resolve<IRegionNavigationService>();
114            var actual2 = bootstrapper.BaseContainer.Resolve<IRegionNavigationService>();
115
116            Assert.IsNotNull(actual1);
117            Assert.IsNotNull(actual2);
118            Assert.AreNotSame(actual1, actual2);
119        }
120
121        [TestMethod]
122        public void ConfigureContainerAddsNavigationTargetHandlerToContainer()
123        {
124            var bootstrapper = new DefaultUnityBootstrapper();
125            bootstrapper.Run();
126
127            var actual1 = bootstrapper.BaseContainer.Resolve<IRegionNavigationContentLoader>();
128            var actual2 = bootstrapper.BaseContainer.Resolve<IRegionNavigationContentLoader>();
129
130            Assert.IsNotNull(actual1);
131            Assert.IsNotNull(actual2);
132            Assert.AreSame(actual1, actual2);
133        }
134
135        [TestMethod]
136        public void RegisterFrameworkExceptionTypesShouldRegisterActivationException()
137        {
138            var bootstrapper = new DefaultUnityBootstrapper();
139
140            bootstrapper.CallRegisterFrameworkExceptionTypes();
141
142            Assert.IsTrue(ExceptionExtensions.IsFrameworkExceptionRegistered(
143                typeof(Microsoft.Practices.ServiceLocation.ActivationException)));
144        }
145
146        [TestMethod]
147        public void RegisterFrameworkExceptionTypesShouldRegisterResolutionFailedException()
148        {
149            var bootstrapper = new DefaultUnityBootstrapper();
150
151            bootstrapper.CallRegisterFrameworkExceptionTypes();
152
153            Assert.IsTrue(ExceptionExtensions.IsFrameworkExceptionRegistered(
154                typeof(Microsoft.Practices.Unity.ResolutionFailedException)));
155        }
156    }
157
158    internal class DefaultUnityBootstrapper : UnityBootstrapper
159    {
160        public List<string> MethodCalls = new List<string>();
161        public bool InitializeModulesCalled;
162        public bool ConfigureRegionAdapterMappingsCalled;
163        public RegionAdapterMappings DefaultRegionAdapterMappings;
164        public bool CreateLoggerCalled;
165        public bool CreateModuleCatalogCalled;
166        public bool ConfigureContainerCalled;
167        public bool CreateShellCalled;
168        public bool CreateContainerCalled;
169        public bool ConfigureModuleCatalogCalled;
170        public bool InitializeShellCalled;
171        public bool ConfigureServiceLocatorCalled;
172        public bool ConfigureDefaultRegionBehaviorsCalled;
173        public DependencyObject ShellObject = new UserControl();
174
175        public DependencyObject BaseShell
176        {
177            get { return base.Shell; }
178        }
179        public IUnityContainer BaseContainer
180        {
181            get { return base.Container; }
182            set { base.Container = value; }
183        }
184
185        public MockLoggerAdapter BaseLogger
186        { get { return base.Logger as MockLoggerAdapter; } }
187
188        public IUnityContainer CallCreateContainer()
189        {
190            return this.CreateContainer();
191        }
192
193        protected override IUnityContainer CreateContainer()
194        {
195            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
196            this.CreateContainerCalled = true;
197            return base.CreateContainer();
198        }
199
200        protected override void ConfigureContainer()
201        {
202            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
203            this.ConfigureContainerCalled = true;
204            base.ConfigureContainer();
205        }
206
207        protected override ILoggerFacade CreateLogger()
208        {
209            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
210            this.CreateLoggerCalled = true;
211            return new MockLoggerAdapter();
212        }
213
214        protected override DependencyObject CreateShell()
215        {
216            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
217            this.CreateShellCalled = true;
218            return ShellObject;
219        }
220
221        protected override void ConfigureServiceLocator()
222        {
223            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
224            this.ConfigureServiceLocatorCalled = true;
225            base.ConfigureServiceLocator();
226        }
227        protected override IModuleCatalog CreateModuleCatalog()
228        {
229            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
230            this.CreateModuleCatalogCalled = true;
231            return base.CreateModuleCatalog();
232        }
233
234        protected override void ConfigureModuleCatalog()
235        {
236            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
237            this.ConfigureModuleCatalogCalled = true;
238            base.ConfigureModuleCatalog();
239        }
240
241        protected override void InitializeShell()
242        {
243            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
244            this.InitializeShellCalled = true;
245            // no op
246        }
247
248        protected override void InitializeModules()
249        {
250            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
251            this.InitializeModulesCalled = true;
252            base.InitializeModules();
253        }
254
255        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
256        {
257            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
258            this.ConfigureDefaultRegionBehaviorsCalled = true;
259            return base.ConfigureDefaultRegionBehaviors();
260        }
261
262        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
263        {
264            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
265            ConfigureRegionAdapterMappingsCalled = true;
266            var regionAdapterMappings = base.ConfigureRegionAdapterMappings();
267
268            DefaultRegionAdapterMappings = regionAdapterMappings;
269
270            return regionAdapterMappings;
271        }
272
273        protected override void RegisterFrameworkExceptionTypes()
274        {
275            this.MethodCalls.Add(System.Reflection.MethodBase.GetCurrentMethod().Name);
276            base.RegisterFrameworkExceptionTypes();
277        }
278
279        public void CallRegisterFrameworkExceptionTypes()
280        {
281            base.RegisterFrameworkExceptionTypes();
282        }
283    }
284    
285}