PageRenderTime 36ms CodeModel.GetById 18ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Presentation.Tests/Regions/Behaviors/DelayedRegionCreationBehaviorFixture.cs

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