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

/V1/trunk/Source/CAL/Composite.Wpf.Tests/Regions/SelectorRegionAdapterFixture.cs

#
C# | 269 lines | 197 code | 56 blank | 16 comment | 0 complexity | 2618c0a22d06d1424ee7b12330a48edc MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  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
 18using System;
 19using System.Collections;
 20using System.Collections.Generic;
 21using System.Linq;
 22using System.Windows.Controls;
 23using System.Windows.Data;
 24using Microsoft.Practices.Composite.Regions;
 25using Microsoft.Practices.Composite.Wpf.Regions;
 26using Microsoft.Practices.Composite.Wpf.Tests.Mocks;
 27using Microsoft.VisualStudio.TestTools.UnitTesting;
 28
 29namespace Microsoft.Practices.Composite.Wpf.Tests.Regions
 30{
 31    [TestClass]
 32    public class SelectorRegionAdapterFixture
 33    {
 34        [TestMethod]
 35        public void AdapterAssociatesSelectorWithRegion()
 36        {
 37            var control = new TabControl();
 38            IRegionAdapter adapter = new TestableSelectorRegionAdapter();
 39
 40            IRegion region = adapter.Initialize(control);
 41            Assert.IsNotNull(region);
 42
 43            Assert.AreSame(control.ItemsSource, region.Views);
 44        }
 45
 46        [TestMethod]
 47        public void ShouldMoveAlreadyExistingContentInControlToRegion()
 48        {
 49            var control = new TabControl();
 50            var view = new object();
 51            control.Items.Add(view);
 52            IRegionAdapter adapter = new TestableSelectorRegionAdapter();
 53
 54            var region = (MockRegion)adapter.Initialize(control);
 55
 56            Assert.AreEqual(1, region.MockViews.Count());
 57            Assert.AreSame(view, region.MockViews.ElementAt(0));
 58            Assert.AreSame(view, control.Items[0]);
 59        }
 60
 61        [TestMethod]
 62        public void ControlWithExistingItemSourceThrows()
 63        {
 64            TabControl tabControl = new TabControl() { ItemsSource = new List<string>() };
 65
 66            IRegionAdapter adapter = new TestableSelectorRegionAdapter();
 67
 68            try
 69            {
 70                var region = (MockRegion)adapter.Initialize(tabControl);
 71                Assert.Fail();
 72            }
 73            catch (Exception ex)
 74            {
 75                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
 76                StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
 77            }
 78        }
 79
 80        [TestMethod]
 81        public void ControlWithExistingBindingOnItemsSourceWithNullValueThrows()
 82        {
 83            TabControl tabControl = new TabControl();
 84            Binding binding = new Binding("Enumerable");
 85            binding.Source = new SimpleModel() { Enumerable = null };
 86            BindingOperations.SetBinding(tabControl, TabControl.ItemsSourceProperty, binding);
 87
 88            IRegionAdapter adapter = new TestableSelectorRegionAdapter();
 89
 90            try
 91            {
 92                var region = (MockRegion)adapter.Initialize(tabControl);
 93                Assert.Fail();
 94            }
 95            catch (Exception ex)
 96            {
 97                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
 98                StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
 99            }
100        }
101
102        [TestMethod]
103        public void AdapterSynchronizesSelectorSelectionWithRegion()
104        {
105            TabControl tabControl = new TabControl();
106            object model1 = new object();
107            object model2 = new object();
108            IRegionAdapter adapter = new SelectorRegionAdapter();
109
110            var region = adapter.Initialize(tabControl);
111            region.Add(model1);
112            region.Add(model2);
113
114            Assert.IsFalse(region.ActiveViews.Contains(model2));
115
116            tabControl.SelectedItem = model2;
117
118            Assert.IsTrue(region.ActiveViews.Contains(model2));
119            Assert.IsFalse(region.ActiveViews.Contains(model1));
120
121            tabControl.SelectedItem = model1;
122
123            Assert.IsTrue(region.ActiveViews.Contains(model1));
124            Assert.IsFalse(region.ActiveViews.Contains(model2));
125        }
126
127        [TestMethod]
128        public void AdapterDoesNotPreventRegionFromBeingGarbageCollected()
129        {
130            TabControl tabControl = new TabControl();
131            object model = new object();
132            IRegionAdapter adapter = new SelectorRegionAdapter();
133
134            var region = adapter.Initialize(tabControl);
135            region.Add(model);
136
137            WeakReference regionWeakReference = new WeakReference(region);
138            WeakReference controlWeakReference = new WeakReference(tabControl);
139            Assert.IsTrue(regionWeakReference.IsAlive);
140            Assert.IsTrue(controlWeakReference.IsAlive);
141
142            region = null;
143            tabControl = null;
144            GC.Collect();
145            GC.Collect();
146
147            Assert.IsFalse(regionWeakReference.IsAlive);
148            Assert.IsFalse(controlWeakReference.IsAlive);
149        }
150
151        [TestMethod]
152        public void AdapterDoesNotPreventRegionFromBeingGarbageCollectedWhenSettingItemsSourceToNull()
153        {
154            TabControl tabControl = new TabControl();
155            object model = new object();
156            IRegionAdapter adapter = new SelectorRegionAdapter();
157
158            var region = adapter.Initialize(tabControl);
159            region.Add(model);
160
161            WeakReference regionWeakReference = new WeakReference(region);
162            Assert.IsTrue(regionWeakReference.IsAlive);
163
164            tabControl.ItemsSource = null;
165            region = null;
166            GC.Collect();
167            GC.Collect();
168
169            Assert.IsFalse(regionWeakReference.IsAlive);
170        }
171
172        [TestMethod]
173        public void AdapterDoesNotPreventControlFromBeingGarbageCollectedWhenSettingItemsSourceToNull()
174        {
175            TabControl tabControl = new TabControl();
176            object model = new object();
177            IRegionAdapter adapter = new SelectorRegionAdapter();
178
179            var region = adapter.Initialize(tabControl);
180            region.Add(model);
181
182            WeakReference controlWeakReference = new WeakReference(tabControl);
183            Assert.IsTrue(controlWeakReference.IsAlive);
184
185            tabControl.ItemsSource = null;
186            tabControl = null;
187            GC.Collect();
188            GC.Collect();
189
190            Assert.IsFalse(controlWeakReference.IsAlive);
191        }
192
193        [TestMethod]
194        public void RegionDoesNotGetUpdatedIfTheItemsSourceWasManuallyChanged()
195        {
196            TabControl tabControl = new TabControl();
197            object model = new object();
198            IRegionAdapter adapter = new SelectorRegionAdapter();
199
200            var region = adapter.Initialize(tabControl);
201            region.Add(model);
202
203            tabControl.ItemsSource = null;
204            tabControl.Items.Add(model);
205            Assert.IsFalse(region.ActiveViews.Contains(model));
206
207            tabControl.SelectedItem = model;
208
209            Assert.IsFalse(region.ActiveViews.Contains(model), "The region should not be updated");
210        }
211
212        [TestMethod]
213        public void ActivatingTheViewShouldUpdateTheSelectedItem()
214        {
215            TabControl tabControl = new TabControl();
216            var view1 = new object();
217            var view2 = new object();
218
219            IRegionAdapter adapter = new SelectorRegionAdapter();
220
221            var region = adapter.Initialize(tabControl);
222            region.Add(view1);
223            region.Add(view2);
224
225            Assert.AreNotEqual(view1, tabControl.SelectedItem);
226
227            region.Activate(view1);
228
229            Assert.AreEqual(view1, tabControl.SelectedItem);
230
231            region.Activate(view2);
232
233            Assert.AreEqual(view2, tabControl.SelectedItem);
234        }
235
236        [TestMethod]
237        public void DeactivatingTheSelectedViewShouldUpdateTheSelectedItem()
238        {
239            TabControl tabControl = new TabControl();
240            var view1 = new object();
241            IRegionAdapter adapter = new SelectorRegionAdapter();
242            var region = adapter.Initialize(tabControl);
243            region.Add(view1);
244
245            region.Activate(view1);
246
247            Assert.AreEqual(view1, tabControl.SelectedItem);
248
249            region.Deactivate(view1);
250
251            Assert.AreNotEqual(view1, tabControl.SelectedItem);
252        }
253
254        class SimpleModel
255        {
256            public IEnumerable Enumerable { get; set; }
257        }
258
259        internal class TestableSelectorRegionAdapter : SelectorRegionAdapter
260        {
261            private MockRegion region = new MockRegion();
262
263            protected override IRegion CreateRegion()
264            {
265                return region;
266            }
267        }
268    }
269}