public void SetsIsActivePropertyOnBothIActiveAwareViewAndDataContext()
        {
            var region = new MockPresentationRegion();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();
            var collection = region.MockActiveViews.Items;

            var activeAwareMock = new Mock<IActiveAware>();
            activeAwareMock.SetupProperty(o => o.IsActive);
            var activeAwareObject = activeAwareMock.Object;

            var frameworkElementMock = new Mock<FrameworkElement>();
            frameworkElementMock.As<IActiveAware>().SetupProperty(o => o.IsActive);
            var frameworkElement = frameworkElementMock.Object;
            frameworkElement.DataContext = activeAwareObject;

            Assert.IsFalse(((IActiveAware)frameworkElement).IsActive);
            Assert.IsFalse(activeAwareObject.IsActive);
            collection.Add(frameworkElement);

            Assert.IsTrue(((IActiveAware)frameworkElement).IsActive);
            Assert.IsTrue(activeAwareObject.IsActive);

            collection.Remove(frameworkElement);
            Assert.IsFalse(((IActiveAware)frameworkElement).IsActive);
            Assert.IsFalse(activeAwareObject.IsActive);
        }
        public void AttachShouldNotThrowWhenHostControlNull()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            behavior.Attach();
        }
        public void AddPassesItselfAsTheRegionManagerOfTheRegion()
        {
            var regionManager = new RegionManager();
            var region = new MockPresentationRegion();
            region.Name = "region";
            regionManager.Regions.Add(region);

            Assert.AreSame(regionManager, region.RegionManager);
        }
        public void CanAddRegion()
        {
            IRegion region1 = new MockPresentationRegion();
            region1.Name = "MainRegion";

            RegionManager regionManager = new RegionManager();
            regionManager.Regions.Add(region1);

            IRegion region2 = regionManager.Regions["MainRegion"];
            Assert.AreSame(region1, region2);
        }
        public IRegion Initialize(object regionTarget, string regionName)
        {
            CreatedRegions.Add(regionName);

            var region = new MockPresentationRegion();
            RegionManager.GetObservableRegion(regionTarget as DependencyObject).Value = region;

            // Fire update regions again. This also happens if a region is created and added to the regionmanager
            if (this.Accessor != null)
                Accessor.UpdateRegions();

            return region;
        }
        public void ShouldRemoveContextToViewRemovedFromRegion()
        {
            var behavior = new BindRegionContextToDependencyObjectBehavior();
            var region = new MockPresentationRegion();
            var view = new MockDependencyObject();
            region.Add(view);
            behavior.Region = region;
            region.Context = "MyContext";
            behavior.Attach();

            region.Remove(view);

            var context = RegionContext.GetObservableContext(view);
            Assert.IsNull(context.Value);
        }
        public void CanCheckTheExistenceOfARegion()
        {
            RegionManager regionManager = new RegionManager();
            bool result = regionManager.Regions.ContainsRegionWithName("noRegion");

            Assert.IsFalse(result);

            IRegion region = new MockPresentationRegion();
            region.Name = "noRegion";
            regionManager.Regions.Add(region);

            result = regionManager.Regions.ContainsRegionWithName("noRegion");

            Assert.IsTrue(result);
        }
        public void ShouldSetRegionContextOnAlreadyAddedViews()
        {
            var behavior = new BindRegionContextToDependencyObjectBehavior();
            var region = new MockPresentationRegion();
            var view = new MockDependencyObject();
            region.Add(view);
            behavior.Region = region;
            region.Context = "MyContext";

            behavior.Attach();

            var context = RegionContext.GetObservableContext(view);
            Assert.IsNotNull(context.Value);
            Assert.AreEqual("MyContext", context.Value);
        }
        public void ShouldGetInitialValueFromHostAndSetOnRegion()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            DependencyObject mockDependencyObject = new MockDependencyObject();
            behavior.HostControl = mockDependencyObject;

            RegionContext.GetObservableContext(mockDependencyObject).Value = "NewValue";

            Assert.IsNull(region.Context);
            behavior.Attach();
            Assert.AreEqual("NewValue", region.Context);

        }
예제 #10
0
        public IRegion Initialize(object regionTarget, string regionName)
        {
            CreatedRegions.Add(regionName);

            var region = new MockPresentationRegion();

            RegionManager.GetObservableRegion(regionTarget as DependencyObject).Value = region;

            // Fire update regions again. This also happens if a region is created and added to the regionmanager
            if (this.Accessor != null)
            {
                Accessor.UpdateRegions();
            }

            return(region);
        }
        public void ShouldSetRegionContextOnContextChange()
        {
            var behavior = new BindRegionContextToDependencyObjectBehavior();
            var region = new MockPresentationRegion();
            var view = new MockDependencyObject();
            region.Add(view);
            behavior.Region = region;
            region.Context = "MyContext";
            behavior.Attach();
            Assert.AreEqual("MyContext", RegionContext.GetObservableContext(view).Value);

            region.Context = "MyNewContext";
            region.OnPropertyChange("Context");

            Assert.AreEqual("MyNewContext", RegionContext.GetObservableContext(view).Value);
        }
        public void ShouldGetViewsFromRegistryOnAttach()
        {
            var region = new MockPresentationRegion() { Name = "MyRegion" };
            var viewFactory = new MockRegionContentRegistry();
            var view = new object();
            viewFactory.GetContentsReturnValue.Add(view);
            var behavior = new AutoPopulateRegionBehavior(viewFactory)
                               {
                                   Region = region
                               };

            behavior.Attach();

            Assert.AreEqual("MyRegion", viewFactory.GetContentsArgumentRegionName);
            Assert.AreEqual(1, region.MockViews.Items.Count);
            Assert.AreEqual(view, region.MockViews.Items[0]);
        }
        public void WhenAViewIsRemovedFromARegion_ThenRegionContextIsNotClearedInRegion()
        {
            var behavior = new BindRegionContextToDependencyObjectBehavior();
            var region = new MockPresentationRegion();

            behavior.Region = region;
            behavior.Attach();

            var myView = new MockFrameworkElement();
            
            region.Add(myView);
            region.Context = "new context";

            region.Remove(myView);

            Assert.IsNotNull(region.Context);
        }
        public void ShouldUpdateHostControlRegionContextValueWhenContextOfRegionChanges()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            DependencyObject mockDependencyObject = new MockDependencyObject();
            behavior.HostControl = mockDependencyObject;

            ObservableObject<object> observableRegionContext = RegionContext.GetObservableContext(mockDependencyObject);

            behavior.Attach();
            Assert.IsNull(observableRegionContext.Value);
            region.Context = "NewValue";

            Assert.AreEqual("NewValue", observableRegionContext.Value);

        }
        public void SetsIsActivePropertyOnIActiveAwareObjects()
        {
            var region = new MockPresentationRegion();
            region.RegionManager = new MockRegionManager();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();
            var collection = region.MockActiveViews.Items;

            ActiveAwareFrameworkElement activeAwareObject = new ActiveAwareFrameworkElement();

            Assert.IsFalse(activeAwareObject.IsActive);
            collection.Add(activeAwareObject);

            Assert.IsTrue(activeAwareObject.IsActive);

            collection.Remove(activeAwareObject);
            Assert.IsFalse(activeAwareObject.IsActive);
        }
        public void SetsIsActivePropertyOnIActiveAwareDataContexts()
        {
            var region = new MockPresentationRegion();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();
            var collection = region.MockActiveViews.Items;

            ActiveAwareFrameworkElement activeAwareObject = new ActiveAwareFrameworkElement();

            var frameworkElementMock = new Mock<FrameworkElement>();
            var frameworkElement = frameworkElementMock.Object;
            frameworkElement.DataContext = activeAwareObject;

            Assert.IsFalse(activeAwareObject.IsActive);
            collection.Add(frameworkElement);

            Assert.IsTrue(activeAwareObject.IsActive);

            collection.Remove(frameworkElement);
            Assert.IsFalse(activeAwareObject.IsActive);
        }
        public void CanAttachBeforeSettingName()
        {
            var region = new MockPresentationRegion() { Name = null };
            var viewFactory = new MockRegionContentRegistry();
            var view = new object();
            viewFactory.GetContentsReturnValue.Add(view);
            var behavior = new AutoPopulateRegionBehavior(viewFactory)
            {
                Region = region
            };

            behavior.Attach();
            Assert.IsFalse(viewFactory.GetContentsCalled);

            region.Name = "MyRegion";

            Assert.IsTrue(viewFactory.GetContentsCalled);
            Assert.AreEqual("MyRegion", viewFactory.GetContentsArgumentRegionName);
            Assert.AreEqual(1, region.MockViews.Items.Count);
            Assert.AreEqual(view, region.MockViews.Items[0]);
        }
        public void DoesNotThrowWhenAddingNonActiveAwareObjects()
        {
            var region = new MockPresentationRegion();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();
            var collection = region.MockActiveViews.Items;

            collection.Add(new object());
        }
        public void DetachStopsListeningForChanges()
        {
            var region = new MockPresentationRegion();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            var collection = region.MockActiveViews.Items;
            behavior.Attach();
            behavior.Detach();
            ActiveAwareFrameworkElement activeAwareObject = new ActiveAwareFrameworkElement();

            collection.Add(activeAwareObject);

            Assert.IsFalse(activeAwareObject.IsActive);
        }
        public void DoesNotThrowWhenAddingNonActiveAwareDataContexts()
        {
            var region = new MockPresentationRegion();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();
            var collection = region.MockActiveViews.Items;

            var frameworkElementMock = new Mock<FrameworkElement>();
            var frameworkElement = frameworkElementMock.Object;
            frameworkElement.DataContext = new object();


            collection.Add(frameworkElement);
        }
        public void ChangingRegionContextObservableObjectValueShouldAlsoChangeRegionContextDependencyProperty()
        {
            MockPresentationRegion region = new MockPresentationRegion();

            SyncRegionContextWithHostBehavior behavior = new SyncRegionContextWithHostBehavior();
            behavior.Region = region;
            DependencyObject hostControl = new MockDependencyObject();
            behavior.HostControl = hostControl;

            behavior.Attach();

            Assert.IsNull(RegionManager.GetRegionContext(hostControl));
            RegionContext.GetObservableContext(hostControl).Value = "NewValue";

            Assert.AreEqual("NewValue", RegionManager.GetRegionContext(hostControl));
        }
        public void WhenParentViewGetsActivatedOrDeactivated_ThenSyncedChildViewNotInActiveViewsIsNotUpdated()
        {
            var scopedRegionManager = new RegionManager();
            var scopedRegion = new Region { Name="MyScopedRegion", RegionManager = scopedRegionManager };
            scopedRegionManager.Regions.Add(scopedRegion);
            var behaviorForScopedRegion = new RegionActiveAwareBehavior { Region = scopedRegion };
            behaviorForScopedRegion.Attach();
            var childActiveAwareView = new SyncedActiveAwareObject();

            var region = new MockPresentationRegion();
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();

            var view = new MockFrameworkElement();
            region.Add(view);
            RegionManager.SetRegionManager(view, scopedRegionManager);
            region.Activate(view);

            scopedRegion.Add(childActiveAwareView);
            scopedRegion.Deactivate(childActiveAwareView);

            Assert.IsFalse(childActiveAwareView.IsActive);

            region.Deactivate(view);

            Assert.IsFalse(childActiveAwareView.IsActive);

            region.Activate(view);

            Assert.IsFalse(childActiveAwareView.IsActive);
        }
        public void WhenParentViewWithoutScopedRegionGetsActivatedOrDeactivated_ThenSyncedChildViewIsNotUpdated()
        {
            var commonRegionManager = new RegionManager();
            var nonScopedRegion = new Region { Name="MyRegion", RegionManager = commonRegionManager };
            commonRegionManager.Regions.Add(nonScopedRegion);
            var behaviorForScopedRegion = new RegionActiveAwareBehavior { Region = nonScopedRegion };
            behaviorForScopedRegion.Attach();
            var childActiveAwareView = new SyncedActiveAwareObject();

            var region = new MockPresentationRegion { RegionManager = commonRegionManager };
            var behavior = new RegionActiveAwareBehavior { Region = region };
            behavior.Attach();

            var view = new MockFrameworkElement();
            region.Add(view);
            RegionManager.SetRegionManager(view, commonRegionManager);
            region.Activate(view);

            nonScopedRegion.Add(childActiveAwareView);
            nonScopedRegion.Activate(childActiveAwareView);

            Assert.IsTrue(childActiveAwareView.IsActive);

            region.Deactivate(view);

            Assert.IsTrue(childActiveAwareView.IsActive);
        }
        public void ShouldRemoveRegionManagerWhenRemoving()
        {
            var regionManager = new RegionManager();
            var region = new MockPresentationRegion();
            region.Name = "TestRegion";
            regionManager.Regions.Add(region);

            regionManager.Regions.Remove("TestRegion");

            Assert.IsNull(region.RegionManager);
        }
        public void CanRemoveRegion()
        {
            var regionManager = new RegionManager();
            IRegion region = new MockPresentationRegion();
            region.Name = "TestRegion";
            regionManager.Regions.Add(region);

            regionManager.Regions.Remove("TestRegion");

            Assert.IsFalse(regionManager.Regions.ContainsRegionWithName("TestRegion"));
        }
        public void ShouldSetObservableRegionContextWhenRegionContextChanges()
        {
            var region = new MockPresentationRegion();
            var view = new MockDependencyObject();

            var observableObject = RegionContext.GetObservableContext(view);

            bool propertyChangedCalled = false;
            observableObject.PropertyChanged += (sender, args) => propertyChangedCalled = true;

            Assert.IsNull(observableObject.Value);
            RegionManager.SetRegionContext(view, "MyContext");
            Assert.IsTrue(propertyChangedCalled);
            Assert.AreEqual("MyContext", observableObject.Value);
        }