PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
C# | 84 lines | 52 code | 16 blank | 16 comment | 0 complexity | 099bcae7a09c5d042e50f916c6de89ea 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.Linq;
19using Microsoft.Practices.Prism.Tests.Mocks;
20using Microsoft.Practices.Prism.Regions;
21using Microsoft.VisualStudio.TestTools.UnitTesting;
22
23namespace Microsoft.Practices.Prism.Tests.Regions
24{
25    [TestClass]
26    public class RegionBehaviorFactoryFixture
27    {
28        [TestMethod]
29        public void CanRegisterType()
30        {
31            RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
32
33            factory.AddIfMissing("key1", typeof(MockRegionBehavior));
34            factory.AddIfMissing("key2", typeof(MockRegionBehavior));
35
36            Assert.AreEqual(2, factory.Count());
37            Assert.IsTrue(factory.ContainsKey("key1"));
38            
39        }
40
41        [TestMethod]
42        public void WillNotAddTypesWithDuplicateKeys()
43        {
44            RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
45
46            factory.AddIfMissing("key1", typeof(MockRegionBehavior));
47            factory.AddIfMissing("key1", typeof(MockRegionBehavior));
48
49            Assert.AreEqual(1, factory.Count());
50        }
51
52        [TestMethod]
53        [ExpectedException(typeof(ArgumentException))]
54        public void AddTypeThatDoesNotInheritFromIRegionBehaviorThrows()
55        {
56            RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
57
58            factory.AddIfMissing("key1", typeof(object));
59        }
60
61        [TestMethod]
62        public void CanCreateRegisteredType()
63        {
64            var expectedBehavior = new MockRegionBehavior();
65
66            RegionBehaviorFactory factory = new RegionBehaviorFactory(new MockServiceLocator() { GetInstance = (t) => expectedBehavior });
67
68            factory.AddIfMissing("key1", typeof(MockRegionBehavior));
69            var behavior = factory.CreateFromKey("key1");
70
71            Assert.AreSame(expectedBehavior, behavior);
72        }
73
74        [TestMethod]
75        [ExpectedException(typeof(ArgumentException))]
76        public void CreateWithUnknownKeyThrows()
77        {
78            RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
79
80            factory.CreateFromKey("Key1");
81        }
82
83    }
84}