/PrismLibrary/Desktop/Prism.Tests/Regions/RegionManagerExtensionsFixture.cs

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