PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism.Tests/Regions/Behaviors/DelayedRegionCreationBehaviorFixture.cs

#
C# | 205 lines | 145 code | 44 blank | 16 comment | 2 complexity | dabf94cd59ddf67389e19c269bec6c7b 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.Linq;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using Microsoft.Practices.Prism.Tests.Mocks;
  23. using Microsoft.Practices.Prism.Regions;
  24. using Microsoft.Practices.Prism.Regions.Behaviors;
  25. using Microsoft.VisualStudio.TestTools.UnitTesting;
  26. namespace Microsoft.Practices.Prism.Tests.Regions.Behaviors
  27. {
  28. [TestClass]
  29. public class DelayedRegionCreationBehaviorFixture
  30. {
  31. private DelayedRegionCreationBehavior GetBehavior(DependencyObject control, MockRegionManagerAccessor accessor, MockRegionAdapter adapter)
  32. {
  33. var mappings = new RegionAdapterMappings();
  34. mappings.RegisterMapping(control.GetType(), adapter);
  35. var behavior = new DelayedRegionCreationBehavior(mappings);
  36. behavior.RegionManagerAccessor = accessor;
  37. behavior.TargetElement = control;
  38. return behavior;
  39. }
  40. private DelayedRegionCreationBehavior GetBehavior(DependencyObject control, MockRegionManagerAccessor accessor)
  41. {
  42. return GetBehavior(control, accessor, new MockRegionAdapter());
  43. }
  44. [TestMethod]
  45. public void RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
  46. {
  47. var control1 = new MockFrameworkElement();
  48. var control2 = new MockFrameworkElement();
  49. var accessor = new MockRegionManagerAccessor
  50. {
  51. GetRegionName = d => d == control1 ? "Region1" : "Region2"
  52. };
  53. var adapter = new MockRegionAdapter();
  54. adapter.Accessor = accessor;
  55. var behavior1 = this.GetBehavior(control1, accessor, adapter);
  56. var behavior2 = this.GetBehavior(control2, accessor, adapter);
  57. behavior1.Attach();
  58. behavior2.Attach();
  59. accessor.UpdateRegions();
  60. Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
  61. Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
  62. Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));
  63. }
  64. [TestMethod]
  65. public void RegionGetsCreatedWhenAccessingRegions()
  66. {
  67. var control = new MockFrameworkElement();
  68. var accessor = new MockRegionManagerAccessor
  69. {
  70. GetRegionName = d => "myRegionName"
  71. };
  72. var behavior = this.GetBehavior(control, accessor);
  73. behavior.Attach();
  74. accessor.UpdateRegions();
  75. Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
  76. Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
  77. }
  78. [TestMethod]
  79. public void RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
  80. {
  81. var control = new MockFrameworkElement();
  82. var accessor = new MockRegionManagerAccessor
  83. {
  84. GetRegionName = d => "myRegionName"
  85. };
  86. var behavior = this.GetBehavior(control, accessor);
  87. behavior.Attach();
  88. accessor.UpdateRegions();
  89. IRegion region = RegionManager.GetObservableRegion(control).Value;
  90. accessor.UpdateRegions();
  91. Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
  92. }
  93. [TestMethod]
  94. public void BehaviorDoesNotPreventControlFromBeingGarbageCollected()
  95. {
  96. var control = new MockFrameworkElement();
  97. WeakReference controlWeakReference = new WeakReference(control);
  98. var accessor = new MockRegionManagerAccessor
  99. {
  100. GetRegionName = d => "myRegionName"
  101. };
  102. var behavior = this.GetBehavior(control, accessor);
  103. behavior.Attach();
  104. Assert.IsTrue(controlWeakReference.IsAlive);
  105. GC.KeepAlive(control);
  106. control = null;
  107. GC.Collect();
  108. Assert.IsFalse(controlWeakReference.IsAlive);
  109. }
  110. [TestMethod]
  111. public void BehaviorDoesNotPreventControlFromBeingGarbageCollectedWhenRegionWasCreated()
  112. {
  113. var control = new MockFrameworkElement();
  114. WeakReference controlWeakReference = new WeakReference(control);
  115. var accessor = new MockRegionManagerAccessor
  116. {
  117. GetRegionName = d => "myRegionName"
  118. };
  119. var behavior = this.GetBehavior(control, accessor);
  120. behavior.Attach();
  121. accessor.UpdateRegions();
  122. Assert.IsTrue(controlWeakReference.IsAlive);
  123. GC.KeepAlive(control);
  124. control = null;
  125. GC.Collect();
  126. Assert.IsFalse(controlWeakReference.IsAlive);
  127. }
  128. [TestMethod]
  129. public void BehaviorShouldUnhookEventWhenDetaching()
  130. {
  131. var control = new MockFrameworkElement();
  132. var accessor = new MockRegionManagerAccessor
  133. {
  134. GetRegionName = d => "myRegionName",
  135. };
  136. var behavior = this.GetBehavior(control, accessor);
  137. behavior.Attach();
  138. int startingCount = accessor.GetSubscribersCount();
  139. behavior.Detach();
  140. Assert.AreEqual<int>(startingCount - 1, accessor.GetSubscribersCount());
  141. }
  142. [TestMethod]
  143. public void ShouldCleanupBehaviorOnceRegionIsCreated()
  144. {
  145. var control = new MockFrameworkElement();
  146. var accessor = new MockRegionManagerAccessor
  147. {
  148. GetRegionName = d => "myRegionName"
  149. };
  150. var behavior = this.GetBehavior(control, accessor);
  151. WeakReference behaviorWeakReference = new WeakReference(behavior);
  152. behavior.Attach();
  153. accessor.UpdateRegions();
  154. Assert.IsTrue(behaviorWeakReference.IsAlive);
  155. GC.KeepAlive(behavior);
  156. behavior = null;
  157. GC.Collect();
  158. Assert.IsFalse(behaviorWeakReference.IsAlive);
  159. }
  160. }
  161. }