PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Linq;
  19. using Microsoft.Practices.Prism.Tests.Mocks;
  20. using Microsoft.Practices.Prism.Regions;
  21. using Microsoft.VisualStudio.TestTools.UnitTesting;
  22. namespace Microsoft.Practices.Prism.Tests.Regions
  23. {
  24. [TestClass]
  25. public class RegionBehaviorFactoryFixture
  26. {
  27. [TestMethod]
  28. public void CanRegisterType()
  29. {
  30. RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
  31. factory.AddIfMissing("key1", typeof(MockRegionBehavior));
  32. factory.AddIfMissing("key2", typeof(MockRegionBehavior));
  33. Assert.AreEqual(2, factory.Count());
  34. Assert.IsTrue(factory.ContainsKey("key1"));
  35. }
  36. [TestMethod]
  37. public void WillNotAddTypesWithDuplicateKeys()
  38. {
  39. RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
  40. factory.AddIfMissing("key1", typeof(MockRegionBehavior));
  41. factory.AddIfMissing("key1", typeof(MockRegionBehavior));
  42. Assert.AreEqual(1, factory.Count());
  43. }
  44. [TestMethod]
  45. [ExpectedException(typeof(ArgumentException))]
  46. public void AddTypeThatDoesNotInheritFromIRegionBehaviorThrows()
  47. {
  48. RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
  49. factory.AddIfMissing("key1", typeof(object));
  50. }
  51. [TestMethod]
  52. public void CanCreateRegisteredType()
  53. {
  54. var expectedBehavior = new MockRegionBehavior();
  55. RegionBehaviorFactory factory = new RegionBehaviorFactory(new MockServiceLocator() { GetInstance = (t) => expectedBehavior });
  56. factory.AddIfMissing("key1", typeof(MockRegionBehavior));
  57. var behavior = factory.CreateFromKey("key1");
  58. Assert.AreSame(expectedBehavior, behavior);
  59. }
  60. [TestMethod]
  61. [ExpectedException(typeof(ArgumentException))]
  62. public void CreateWithUnknownKeyThrows()
  63. {
  64. RegionBehaviorFactory factory = new RegionBehaviorFactory(null);
  65. factory.CreateFromKey("Key1");
  66. }
  67. }
  68. }