PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/V4/PrismLibrary/Desktop/Prism.Tests/Regions/RegionAdapterMappingsFixture.cs

#
C# | 112 lines | 78 code | 18 blank | 16 comment | 0 complexity | 35501bda08dfcf1249b1a4272b108a1f 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.Windows.Controls;
 20using Microsoft.Practices.Prism.Tests.Mocks;
 21using Microsoft.Practices.Prism.Regions;
 22using Microsoft.VisualStudio.TestTools.UnitTesting;
 23
 24namespace Microsoft.Practices.Prism.Tests.Regions
 25{
 26    [TestClass]
 27    public class RegionAdapterMappingsFixture
 28    {
 29        [TestMethod]
 30        public void ShouldGetRegisteredMapping()
 31        {
 32            var regionAdapterMappings = new RegionAdapterMappings();
 33            Type registeredType = typeof(ItemsControl);
 34            var regionAdapter = new MockRegionAdapter();
 35
 36            regionAdapterMappings.RegisterMapping(registeredType, regionAdapter);
 37            var returnedAdapter = regionAdapterMappings.GetMapping(registeredType);
 38
 39            Assert.IsNotNull(returnedAdapter);
 40            Assert.AreSame(regionAdapter, returnedAdapter);
 41        }
 42
 43        [TestMethod]
 44        public void ShouldGetMappingForDerivedTypesThanTheRegisteredOnes()
 45        {
 46            var regionAdapterMappings = new RegionAdapterMappings();
 47            var regionAdapter = new MockRegionAdapter();
 48
 49            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), regionAdapter);
 50            var returnedAdapter = regionAdapterMappings.GetMapping(typeof(ItemsControlDescendant));
 51
 52            Assert.IsNotNull(returnedAdapter);
 53            Assert.AreSame(regionAdapter, returnedAdapter);
 54        }
 55
 56        [TestMethod]
 57        [ExpectedException(typeof(KeyNotFoundException))]
 58        public void GetMappingOfUnregisteredTypeThrows()
 59        {
 60            var regionAdapterMappings = new RegionAdapterMappings();
 61            regionAdapterMappings.GetMapping(typeof(object));
 62        }
 63
 64        [TestMethod]
 65        public void ShouldGetTheMostSpecializedMapping()
 66        {
 67            var regionAdapterMappings = new RegionAdapterMappings();
 68            var genericAdapter = new MockRegionAdapter();
 69            var specializedAdapter = new MockRegionAdapter();
 70
 71            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), genericAdapter);
 72            regionAdapterMappings.RegisterMapping(typeof(ItemsControlDescendant), specializedAdapter);
 73            var returnedAdapter = regionAdapterMappings.GetMapping(typeof(ItemsControlDescendant));
 74
 75            Assert.IsNotNull(returnedAdapter);
 76            Assert.AreSame(specializedAdapter, returnedAdapter);
 77        }
 78
 79        [TestMethod]
 80        [ExpectedException(typeof(InvalidOperationException))]
 81        public void RegisterAMappingThatAlreadyExistsThrows()
 82        {
 83            var regionAdapterMappings = new RegionAdapterMappings();
 84            var regionAdapter = new MockRegionAdapter();
 85
 86            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), regionAdapter);
 87            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), regionAdapter);
 88        }
 89
 90        [TestMethod]
 91        [ExpectedException(typeof(ArgumentNullException))]
 92        public void NullControlThrows()
 93        {
 94            var regionAdapterMappings = new RegionAdapterMappings();
 95            var regionAdapter = new MockRegionAdapter();
 96
 97            regionAdapterMappings.RegisterMapping(null, regionAdapter);
 98        }
 99
100        [TestMethod]
101        [ExpectedException(typeof(ArgumentNullException))]
102        public void NullAdapterThrows()
103        {
104            var regionAdapterMappings = new RegionAdapterMappings();
105
106            regionAdapterMappings.RegisterMapping(typeof(ItemsControl), null);
107        }
108
109        class ItemsControlDescendant : ItemsControl { }
110
111    }
112}