PageRenderTime 33ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Tests/Regions/RegionManagerExtensionsFixture.cs

#
C# | 175 lines | 128 code | 31 blank | 16 comment | 4 complexity | 5fe5cdbbe4d9a27ea7daf4d328a01c44 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 System.Collections.Generic;
 19using System.Collections.Specialized;
 20using System.ComponentModel;
 21using System.Linq;
 22using System.Text;
 23using Microsoft.Practices.Composite.Regions;
 24using Microsoft.Practices.Composite.Tests.Mocks;
 25using Microsoft.Practices.ServiceLocation;
 26using Microsoft.VisualStudio.TestTools.UnitTesting;
 27
 28namespace Microsoft.Practices.Composite.Tests.Regions
 29{
 30    [TestClass]
 31    public class RegionManagerExtensionsFixture
 32    {
 33        [TestMethod]
 34        public void CanAddViewToRegion()
 35        {
 36            var regionManager = new MockRegionManager();
 37            var view1 = new object();
 38            var view2 = new object();
 39
 40
 41            IRegion region = new MockRegion();
 42            region.Name = "RegionName";
 43            regionManager.Regions.Add(region);
 44
 45            regionManager.AddToRegion("RegionName", view1);
 46            regionManager.AddToRegion("RegionName", view2);
 47
 48            Assert.IsTrue(regionManager.Regions["RegionName"].Views.Contains(view1));
 49            Assert.IsTrue(regionManager.Regions["RegionName"].Views.Contains(view2));
 50        }
 51
 52        [TestMethod]
 53        public void CanRegisterViewType()
 54        {
 55            try
 56            {
 57                var mockRegionContentRegistry = new MockRegionContentRegistry();
 58
 59                string regionName = null;
 60                Type viewType = null;
 61
 62                mockRegionContentRegistry.RegisterContentWithViewType = (name, type) =>
 63                                                                            {
 64                                                                                regionName = name;
 65                                                                                viewType = type;
 66                                                                                return null;
 67                                                                            };
 68                ServiceLocator.SetLocatorProvider(
 69                    () => new MockServiceLocator(
 70                        () => mockRegionContentRegistry));
 71
 72                var regionManager = new MockRegionManager();
 73
 74                regionManager.RegisterViewWithRegion("Region1", typeof (object));
 75
 76                Assert.AreEqual(regionName, "Region1");
 77                Assert.AreEqual(viewType, typeof(object));
 78                
 79
 80            }
 81            finally
 82            {
 83                ServiceLocator.SetLocatorProvider(null);
 84            }
 85        }
 86
 87        [TestMethod]
 88        public void CanRegisterDelegate()
 89        {
 90            try
 91            {
 92                var mockRegionContentRegistry = new MockRegionContentRegistry();
 93
 94                string regionName = null;
 95                Func<object> contentDelegate = null;
 96
 97                Func<object> expectedDelegate = () => true;
 98                mockRegionContentRegistry.RegisterContentWithDelegate = (name, usedDelegate) =>
 99                {
100                    regionName = name;
101                    contentDelegate = usedDelegate;
102                    return null;
103                };
104                ServiceLocator.SetLocatorProvider(
105                    () => new MockServiceLocator(
106                        () => mockRegionContentRegistry));
107
108                var regionManager = new MockRegionManager();
109
110                regionManager.RegisterViewWithRegion("Region1", expectedDelegate);
111
112                Assert.AreEqual("Region1", regionName);
113                Assert.AreEqual(expectedDelegate, contentDelegate);
114
115
116            }
117            finally
118            {
119                ServiceLocator.SetLocatorProvider(null);
120            }
121        }
122
123        [TestMethod]
124        public void CanAddRegionToRegionManager()
125        {
126            var regionManager = new MockRegionManager();
127            var region = new MockRegion();
128
129            regionManager.Regions.Add("region", region);
130
131            Assert.AreEqual(1, regionManager.MockRegionCollection.Count);
132            Assert.AreEqual("region", region.Name);
133        }
134
135        [TestMethod]
136        [ExpectedException(typeof(ArgumentException))]
137        public void ShouldThrowIfRegionNameArgumentIsDifferentToRegionNameProperty()
138        {
139            var regionManager = new MockRegionManager();
140            var region = new MockRegion();
141
142            region.Name = "region";
143
144            regionManager.Regions.Add("another region", region);
145        }
146    }
147
148    internal class MockRegionContentRegistry : IRegionViewRegistry
149    {
150        public Func<string, Type, object> RegisterContentWithViewType;
151        public Func<string, Func<object>, object> RegisterContentWithDelegate;
152        public event EventHandler<ViewRegisteredEventArgs> ContentRegistered;
153        public IEnumerable<object> GetContents(string regionName)
154        {
155            return null;
156        }
157
158        void IRegionViewRegistry.RegisterViewWithRegion(string regionName, Type viewType)
159        {
160            if (RegisterContentWithViewType != null)
161            {
162                RegisterContentWithViewType(regionName, viewType);
163            }
164        }
165
166        void IRegionViewRegistry.RegisterViewWithRegion(string regionName, Func<object> getContentDelegate)
167        {
168            if (RegisterContentWithDelegate != null)
169            {
170                RegisterContentWithDelegate(regionName, getContentDelegate);
171            }
172
173        }
174    }
175}