PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms 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//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20using System.Windows;
 21using System.Windows.Controls;
 22using Microsoft.Practices.Prism.Tests.Mocks;
 23using Microsoft.Practices.Prism.Regions;
 24using Microsoft.Practices.Prism.Regions.Behaviors;
 25using Microsoft.VisualStudio.TestTools.UnitTesting;
 26
 27namespace Microsoft.Practices.Prism.Tests.Regions.Behaviors
 28{
 29    [TestClass]
 30    public class DelayedRegionCreationBehaviorFixture
 31    {
 32        private DelayedRegionCreationBehavior GetBehavior(DependencyObject control, MockRegionManagerAccessor accessor, MockRegionAdapter adapter)
 33        {
 34            var mappings = new RegionAdapterMappings();
 35            mappings.RegisterMapping(control.GetType(), adapter);
 36            var behavior = new DelayedRegionCreationBehavior(mappings);
 37            behavior.RegionManagerAccessor = accessor;
 38            behavior.TargetElement = control;
 39            return behavior;
 40        }
 41
 42
 43        private DelayedRegionCreationBehavior GetBehavior(DependencyObject control, MockRegionManagerAccessor accessor)
 44        {
 45            return GetBehavior(control, accessor, new MockRegionAdapter());
 46        }
 47
 48        [TestMethod]
 49        public void RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
 50        {
 51            var control1 = new MockFrameworkElement();
 52            var control2 = new MockFrameworkElement();
 53
 54            var accessor = new MockRegionManagerAccessor
 55                               {
 56                                   GetRegionName = d => d == control1 ? "Region1" : "Region2"
 57                               };
 58
 59            var adapter = new MockRegionAdapter();
 60            adapter.Accessor = accessor;
 61
 62            var behavior1 = this.GetBehavior(control1, accessor, adapter);
 63            var behavior2 = this.GetBehavior(control2, accessor, adapter);
 64
 65            behavior1.Attach();
 66            behavior2.Attach();
 67
 68            accessor.UpdateRegions();
 69
 70            Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
 71            Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
 72            Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));
 73
 74        }
 75
 76
 77        [TestMethod]
 78        public void RegionGetsCreatedWhenAccessingRegions()
 79        {
 80            var control = new MockFrameworkElement();
 81
 82            var accessor = new MockRegionManagerAccessor
 83                               {
 84                                   GetRegionName = d => "myRegionName"
 85                               };
 86
 87            var behavior = this.GetBehavior(control, accessor);
 88            behavior.Attach();
 89
 90            accessor.UpdateRegions();
 91
 92            Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
 93            Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
 94        }
 95
 96        [TestMethod]
 97        public void RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
 98        {
 99            var control = new MockFrameworkElement();
100
101            var accessor = new MockRegionManagerAccessor
102            {
103                GetRegionName = d => "myRegionName"
104            };
105
106            var behavior = this.GetBehavior(control, accessor);
107            behavior.Attach();
108            accessor.UpdateRegions();
109            IRegion region = RegionManager.GetObservableRegion(control).Value;
110
111            accessor.UpdateRegions();
112
113            Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
114        }
115
116        [TestMethod]
117        public void BehaviorDoesNotPreventControlFromBeingGarbageCollected()
118        {
119            var control = new MockFrameworkElement();
120            WeakReference controlWeakReference = new WeakReference(control);
121
122            var accessor = new MockRegionManagerAccessor
123                               {
124                                   GetRegionName = d => "myRegionName"
125                               };
126
127            var behavior = this.GetBehavior(control, accessor);
128            behavior.Attach();
129
130            Assert.IsTrue(controlWeakReference.IsAlive);
131            GC.KeepAlive(control);
132
133            control = null;
134            GC.Collect();
135
136            Assert.IsFalse(controlWeakReference.IsAlive);
137        }
138
139        [TestMethod]
140        public void BehaviorDoesNotPreventControlFromBeingGarbageCollectedWhenRegionWasCreated()
141        {
142            var control = new MockFrameworkElement();
143            WeakReference controlWeakReference = new WeakReference(control);
144
145            var accessor = new MockRegionManagerAccessor
146            {
147                GetRegionName = d => "myRegionName"
148            };
149
150            var behavior = this.GetBehavior(control, accessor);
151            behavior.Attach();
152            accessor.UpdateRegions();
153            
154            Assert.IsTrue(controlWeakReference.IsAlive);
155            GC.KeepAlive(control);
156
157            control = null;
158            GC.Collect();
159
160            Assert.IsFalse(controlWeakReference.IsAlive);
161        }
162
163        [TestMethod]
164        public void BehaviorShouldUnhookEventWhenDetaching()
165        {
166            var control = new MockFrameworkElement();
167
168            var accessor = new MockRegionManagerAccessor
169                               {
170                                   GetRegionName = d => "myRegionName",
171                               };
172            var behavior = this.GetBehavior(control, accessor);
173            behavior.Attach();
174
175            int startingCount = accessor.GetSubscribersCount();
176
177            behavior.Detach();
178
179            Assert.AreEqual<int>(startingCount - 1, accessor.GetSubscribersCount());
180        }
181
182        [TestMethod]
183        public void ShouldCleanupBehaviorOnceRegionIsCreated()
184        {
185            var control = new MockFrameworkElement();
186
187            var accessor = new MockRegionManagerAccessor
188            {
189                GetRegionName = d => "myRegionName"
190            };
191
192            var behavior = this.GetBehavior(control, accessor);
193            WeakReference behaviorWeakReference = new WeakReference(behavior);
194            behavior.Attach();
195            accessor.UpdateRegions();
196            Assert.IsTrue(behaviorWeakReference.IsAlive);
197            GC.KeepAlive(behavior);
198
199            behavior = null;
200            GC.Collect();
201
202            Assert.IsFalse(behaviorWeakReference.IsAlive);
203        }
204    }
205}