PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism.Tests/Regions/SelectorRegionAdapterFixture.cs

#
C# | 128 lines | 85 code | 27 blank | 16 comment | 0 complexity | 952f48b703c27162d925b490f28d8221 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;
 19using System.Collections.Generic;
 20using System.Linq;
 21using System.Windows.Controls;
 22using System.Windows.Data;
 23using Microsoft.Practices.Prism.Tests.Mocks;
 24using Microsoft.Practices.Prism.Regions;
 25using Microsoft.Practices.Prism.Regions.Behaviors;
 26using Microsoft.VisualStudio.TestTools.UnitTesting;
 27
 28namespace Microsoft.Practices.Prism.Tests.Regions
 29{
 30    [TestClass]
 31    public class SelectorRegionAdapterFixture
 32    {
 33        [TestMethod]
 34        public void AdapterAddsSelectorItemsSourceSyncBehavior()
 35        {
 36            var control = new ListBox();
 37            IRegionAdapter adapter = new TestableSelectorRegionAdapter();
 38
 39            IRegion region = adapter.Initialize(control, "Region1");
 40            Assert.IsNotNull(region);
 41
 42            Assert.IsInstanceOfType(region.Behaviors["SelectorItemsSourceSyncBehavior"], typeof(SelectorItemsSourceSyncBehavior));
 43            
 44        }
 45
 46
 47        [TestMethod]
 48        public void AdapterDoesNotPreventRegionFromBeingGarbageCollected()
 49        {
 50            var selector = new ListBox();
 51            object model = new object();
 52            IRegionAdapter adapter = new SelectorRegionAdapter(null);
 53
 54            var region = adapter.Initialize(selector, "Region1");
 55            region.Add(model);
 56
 57            WeakReference regionWeakReference = new WeakReference(region);
 58            WeakReference controlWeakReference = new WeakReference(selector);
 59            Assert.IsTrue(regionWeakReference.IsAlive);
 60            Assert.IsTrue(controlWeakReference.IsAlive);
 61
 62            region = null;
 63            selector = null;
 64            GC.Collect();
 65            GC.Collect();
 66
 67            Assert.IsFalse(regionWeakReference.IsAlive);
 68            Assert.IsFalse(controlWeakReference.IsAlive);
 69        }
 70
 71        [TestMethod]
 72        public void ActivatingTheViewShouldUpdateTheSelectedItem()
 73        {
 74            var selector = new ListBox();
 75            var view1 = new object();
 76            var view2 = new object();
 77
 78            IRegionAdapter adapter = new SelectorRegionAdapter(null);
 79
 80            var region = adapter.Initialize(selector, "Region1");
 81            region.Add(view1);
 82            region.Add(view2);
 83
 84            Assert.AreNotEqual(view1, selector.SelectedItem);
 85
 86            region.Activate(view1);
 87
 88            Assert.AreEqual(view1, selector.SelectedItem);
 89
 90            region.Activate(view2);
 91
 92            Assert.AreEqual(view2, selector.SelectedItem);
 93        }
 94
 95        [TestMethod]
 96        public void DeactivatingTheSelectedViewShouldUpdateTheSelectedItem()
 97        {
 98            var selector = new ListBox();
 99            var view1 = new object();
100            IRegionAdapter adapter = new SelectorRegionAdapter(null);
101            var region = adapter.Initialize(selector, "Region1");
102            region.Add(view1);
103
104            region.Activate(view1);
105
106            Assert.AreEqual(view1, selector.SelectedItem);
107
108            region.Deactivate(view1);
109
110            Assert.AreNotEqual(view1, selector.SelectedItem);
111        }
112
113
114        private class TestableSelectorRegionAdapter : SelectorRegionAdapter
115        {
116            public TestableSelectorRegionAdapter()
117                : base(null)
118            {
119            }
120
121
122            protected override IRegion CreateRegion()
123            {
124                return new Region();
125            }
126        }
127    }
128}