PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/CAL/Composite.Wpf.Tests/Regions/RegionManagerFixture.cs

#
C# | 180 lines | 127 code | 34 blank | 19 comment | 0 complexity | e5f504efbfdce5c5eeddc16efaf02c93 MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  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
 18using System;
 19using System.Collections.Generic;
 20using System.Windows;
 21using System.Windows.Controls;
 22using Microsoft.Practices.Composite.Wpf.Tests.Mocks;
 23using Microsoft.Practices.Composite.Regions;
 24using Microsoft.Practices.Composite.Wpf.Regions;
 25using Microsoft.VisualStudio.TestTools.UnitTesting;
 26
 27namespace Microsoft.Practices.Composite.Wpf.Tests.Regions
 28{
 29    [TestClass]
 30    public class RegionManagerFixture
 31    {
 32        [TestMethod]
 33        public void CanAddRegion()
 34        {
 35            IRegion region1 = new MockRegion();
 36
 37            RegionManager regionManager = new RegionManager();
 38            regionManager.Regions.Add("MainRegion", region1);
 39
 40            IRegion region2 = regionManager.Regions["MainRegion"];
 41            Assert.AreSame(region1, region2);
 42        }
 43
 44        [TestMethod]
 45        [ExpectedException(typeof(KeyNotFoundException))]
 46        public void ShouldFailIfRegionDoesntExists()
 47        {
 48            RegionManager regionManager = new RegionManager();
 49            IRegion region = regionManager.Regions["nonExistentRegion"];
 50        }
 51
 52        [TestMethod]
 53        public void CanCheckTheExistenceOfARegion()
 54        {
 55            RegionManager regionManager = new RegionManager();
 56            bool result = regionManager.Regions.ContainsKey("noRegion");
 57
 58            Assert.IsFalse(result);
 59
 60            IRegion region = new MockRegion();
 61
 62            regionManager.Regions.Add("noRegion", region);
 63
 64            result = regionManager.Regions.ContainsKey("noRegion");
 65
 66            Assert.IsTrue(result);
 67        }
 68
 69        [TestMethod]
 70        [ExpectedException(typeof(ArgumentException))]
 71        public void AddingMultipleRegionsWithSameNameThrowsArgumentException()
 72        {
 73            var regionManager = new RegionManager();
 74            regionManager.Regions.Add("region name", new MockRegion());
 75            regionManager.Regions.Add("region name", new MockRegion());
 76        }
 77
 78        [TestMethod]
 79        public void AddPassesItselfAsTheRegionManagerOfTheRegion()
 80        {
 81            var regionManager = new RegionManager();
 82            var region = new MockRegion();
 83            regionManager.Regions.Add("region", region);
 84
 85            Assert.AreSame(regionManager, region.RegionManager);
 86        }
 87
 88        [TestMethod]
 89        public void CreateRegionManagerCreatesANewInstance()
 90        {
 91            var regionManager = new RegionManager();
 92            var createdRegionManager = regionManager.CreateRegionManager();
 93            Assert.IsNotNull(createdRegionManager);
 94            Assert.IsInstanceOfType(createdRegionManager, typeof(RegionManager));
 95            Assert.AreNotSame(regionManager, createdRegionManager);
 96        }
 97
 98        [TestMethod]
 99        public void ShouldAttachNewRegionByUsingRegisteredRegionAdapterMappings()
100        {
101            var mappings = new RegionAdapterMappings();
102            var mockRegionAdapter = new MockRegionAdapter();
103            mappings.RegisterMapping(typeof(DependencyObject), mockRegionAdapter);
104            var regionManager = new RegionManager(mappings);
105            var control = new DependencyObject();
106
107            regionManager.AttachNewRegion(control, "TestRegionName");
108
109            Assert.IsTrue(mockRegionAdapter.InitializeCalled);
110            Assert.AreEqual(control, mockRegionAdapter.InitializeArgument);
111        }
112
113        [TestMethod]
114        public void CanRemoveRegion()
115        {
116            var regionManager = new RegionManager();
117            IRegion region = new MockRegion();
118            regionManager.Regions.Add("TestRegion", region);
119
120            regionManager.Regions.Remove("TestRegion");
121
122            Assert.IsFalse(regionManager.Regions.ContainsKey("TestRegion"));
123        }
124
125        [TestMethod]
126        public void ShouldRemoveRegionManagerWhenRemoving()
127        {
128            var regionManager = new RegionManager();
129            var region = new MockRegion();
130            regionManager.Regions.Add("TestRegion", region);
131
132            regionManager.Regions.Remove("TestRegion");
133
134            Assert.IsNull(region.RegionManager);
135        }
136
137
138        [TestMethod]
139        public void RegionGetsAddedInRegionManagerWhenAddedIntoAScope()
140        {
141            var mappings = new RegionAdapterMappings();
142            mappings.RegisterMapping(typeof(DependencyObject), new MockRegionAdapter());
143
144            RegionManager regionManager = new RegionManager(mappings);
145            var regionScopeControl = new ContentControl();
146            RegionManager.SetRegionManager(regionScopeControl, regionManager);
147
148            var control = new FrameworkElement();
149            control.SetValue(RegionManager.RegionNameProperty, "TestRegion");
150
151            Assert.IsFalse(regionManager.Regions.ContainsKey("TestRegion"));
152            regionScopeControl.Content = control;
153            Assert.IsTrue(regionManager.Regions.ContainsKey("TestRegion"));
154            Assert.IsNotNull(regionManager.Regions["TestRegion"]);
155        }
156
157        [TestMethod]
158        public void RegionGetsRemovedFromRegionManagerWhenRemovedFromScope()
159        {
160            var mappings = new RegionAdapterMappings();
161            mappings.RegisterMapping(typeof(DependencyObject), new MockRegionAdapter());
162
163            RegionManager regionManager = new RegionManager(mappings);
164            var regionScopeControl = new ContentControl();
165            RegionManager.SetRegionManager(regionScopeControl, regionManager);
166            var control = new FrameworkElement();
167            control.SetValue(RegionManager.RegionNameProperty, "TestRegion");
168            regionScopeControl.Content = control;
169            Assert.IsTrue(regionManager.Regions.ContainsKey("TestRegion"));
170
171            regionScopeControl.Content = null;
172
173            Assert.IsFalse(regionManager.Regions.ContainsKey("TestRegion"));
174        }
175
176        /*
177         * TODO: AttachNewRegion should throw if mappings is null
178         */
179    }
180}