예제 #1
0
        public void RegionDoesNotGetAddedTwiceWhenUpdatingRegions()
        {
            var regionManager = new MockRegionManager();
            var control       = new MockFrameworkElement();

            var regionScopeControl = new ContentControl();
            var accessor           = new MockRegionManagerAccessor
            {
                GetRegionManager = d => d == regionScopeControl ? regionManager : null
            };

            var behavior = new RegionManagerRegistrationBehavior()
            {
                RegionManagerAccessor = accessor,
                Region = new MockPresentationRegion()
                {
                    Name = "myRegionName"
                },
                HostControl = control
            };

            behavior.Attach();

            Assert.IsFalse(regionManager.MockRegionCollection.AddCalled);

            regionScopeControl.Content = control;
            accessor.UpdateRegions();

            Assert.IsTrue(regionManager.MockRegionCollection.AddCalled);
            regionManager.MockRegionCollection.AddCalled = false;

            accessor.UpdateRegions();
            Assert.IsFalse(regionManager.MockRegionCollection.AddCalled);
        }
예제 #2
0
        public void BehaviorDoesNotPreventRegionManagerFromBeingGarbageCollected()
        {
            var control       = new MockFrameworkElement();
            var regionManager = new MockRegionManager();
            var regionManagerWeakReference = new WeakReference(regionManager);

            var accessor = new MockRegionManagerAccessor
            {
                GetRegionName    = d => "myRegionName",
                GetRegionManager = d => regionManager
            };

            var behavior = new RegionManagerRegistrationBehavior()
            {
                RegionManagerAccessor = accessor,
                Region      = new MockPresentationRegion(),
                HostControl = control
            };

            behavior.Attach();

            Assert.IsTrue(regionManagerWeakReference.IsAlive);
            GC.KeepAlive(regionManager);

            regionManager = null;

            GC.Collect();

            Assert.IsFalse(regionManagerWeakReference.IsAlive);
        }
예제 #3
0
        public void CanAttachBeforeSettingName()
        {
            var control       = new ItemsControl();
            var regionManager = new MockRegionManager();
            var accessor      = new MockRegionManagerAccessor
            {
                GetRegionManager = d => regionManager
            };
            var region = new MockPresentationRegion()
            {
                Name = null
            };
            var behavior = new RegionManagerRegistrationBehavior()
            {
                RegionManagerAccessor = accessor,
                Region      = region,
                HostControl = control
            };

            behavior.Attach();
            Assert.IsFalse(regionManager.MockRegionCollection.AddCalled);

            region.Name = "myRegionName";

            Assert.IsTrue(regionManager.MockRegionCollection.AddCalled);
            Assert.AreSame(region, regionManager.MockRegionCollection.AddArgument);
        }
예제 #4
0
        public void RegionGetsRemovedFromRegionManagerWhenRemovedFromScope()
        {
            var regionManager      = new MockRegionManager();
            var control            = new MockFrameworkElement();
            var regionScopeControl = new ContentControl();
            var accessor           = new MockRegionManagerAccessor
            {
                GetRegionManager = d => d == regionScopeControl ? regionManager : null
            };

            var region = new MockPresentationRegion()
            {
                Name = "myRegionName"
            };
            var behavior = new RegionManagerRegistrationBehavior()
            {
                RegionManagerAccessor = accessor,
                Region      = region,
                HostControl = control
            };

            behavior.Attach();

            regionScopeControl.Content = control;
            accessor.UpdateRegions();
            Assert.IsTrue(regionManager.MockRegionCollection.AddCalled);
            Assert.AreSame(region, regionManager.MockRegionCollection.AddArgument);

            regionScopeControl.Content = null;
            accessor.UpdateRegions();

            Assert.IsTrue(regionManager.MockRegionCollection.RemoveCalled);
        }
예제 #5
0
        public void HostControlSetAfterAttachThrows()
        {
            var behavior     = new RegionManagerRegistrationBehavior();
            var hostControl1 = new MockDependencyObject();
            var hostControl2 = new MockDependencyObject();

            behavior.HostControl = hostControl1;
            behavior.Attach();
            behavior.HostControl = hostControl2;
        }
예제 #6
0
 public void HostControlSetAfterAttachThrows()
 {
     var ex = Assert.Throws <InvalidOperationException>(() =>
     {
         var behavior         = new RegionManagerRegistrationBehavior();
         var hostControl1     = new MockDependencyObject();
         var hostControl2     = new MockDependencyObject();
         behavior.HostControl = hostControl1;
         behavior.Attach();
         behavior.HostControl = hostControl2;
     });
 }
예제 #7
0
        public void DoesNotFailIfRegionManagerIsNotSet()
        {
            var control  = new ItemsControl();
            var accessor = new MockRegionManagerAccessor();

            var behavior = new RegionManagerRegistrationBehavior()
            {
                RegionManagerAccessor = accessor,
                Region = new MockPresentationRegion()
                {
                    Name = "myRegionWithoutManager"
                },
                HostControl = control
            };

            behavior.Attach();
        }
예제 #8
0
        public void ShouldRegisterRegionIfRegionManagerIsSet()
        {
            var control       = new ItemsControl();
            var regionManager = new MockRegionManager();
            var accessor      = new MockRegionManagerAccessor
            {
                GetRegionManager = d => regionManager
            };
            var region = new MockPresentationRegion()
            {
                Name = "myRegionName"
            };
            var behavior = new RegionManagerRegistrationBehavior()
            {
                RegionManagerAccessor = accessor,
                Region      = region,
                HostControl = control
            };

            behavior.Attach();

            Assert.IsTrue(regionManager.MockRegionCollection.AddCalled);
            Assert.AreSame(region, regionManager.MockRegionCollection.AddArgument);
        }