Example #1
0
        public void ShouldCreateRegion()
        {
            try
            {
                var regionManager = new MockRegionManager();
                ServiceLocator.SetLocatorProvider(() => new MockServiceLocator(() => regionManager));

                FrameworkElement hostControl = new MockFrameworkElement();
                RegionPopupBehaviors.RegisterNewPopupRegion(hostControl, "MyPopupRegion");

                Assert.IsTrue(regionManager.MockRegions.Regions.ContainsKey("MyPopupRegion"));
                Assert.IsNotNull(regionManager.MockRegions.Regions["MyPopupRegion"]);
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"], typeof(SingleActiveRegion));
                Assert.IsTrue(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors.ContainsKey(DialogActivationBehavior.BehaviorKey));
#if SILVERLIGHT
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(PopupDialogActivationBehavior));
#else
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(WindowDialogActivationBehavior));
#endif
            }
            finally
            {
                ServiceLocator.SetLocatorProvider(() => null);
            }
        }
        public void ShouldCreateRegion()
        {
            try
            {
                var regionManager = new MockRegionManager();
                ServiceLocator.SetLocatorProvider(() => new MockServiceLocator(() => regionManager));

                FrameworkElement hostControl = new MockFrameworkElement();
                RegionPopupBehaviors.RegisterNewPopupRegion(hostControl, "MyPopupRegion");
                RegionPopupBehaviors.SetCreatePopupRegionWithName(hostControl, "MyPopupRegion");
                Assert.AreEqual("MyPopupRegion", RegionPopupBehaviors.GetCreatePopupRegionWithName(hostControl));

                var style = new Style();
                RegionPopupBehaviors.SetContainerWindowStyle(hostControl, style);
                Assert.AreEqual(style, RegionPopupBehaviors.GetContainerWindowStyle(hostControl));

                Assert.IsTrue(regionManager.MockRegions.Regions.ContainsKey("MyPopupRegion"));
                Assert.IsNotNull(regionManager.MockRegions.Regions["MyPopupRegion"]);
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"], typeof(SingleActiveRegion));
                Assert.IsTrue(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors.ContainsKey(DialogActivationBehavior.BehaviorKey));
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(WindowDialogActivationBehavior));
            }
            finally
            {
                ServiceLocator.SetLocatorProvider(() => null);
            }
        }
Example #3
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);
        }
        public void ShouldCreateRegion()
        {
            try
            {
                var regionManager            = new MockRegionManager();
                var windowManager            = new MockWindowManager();
                var dialogActivationBehavior = new DialogActivationBehavior(windowManager);

                ServiceLocator.SetLocatorProvider(
                    () =>
                    new MockServiceLocator(
                        new Tuple <Type, Func <object> >(typeof(IRegionManager), () => regionManager),
                        new Tuple <Type, Func <object> >(typeof(DialogActivationBehavior), () => dialogActivationBehavior)));

                FrameworkElement hostControl = new MockFrameworkElement();
                RegionWindowBehaviors.RegisterNewWindowRegion(hostControl, "MyPopupRegion");

                Assert.IsTrue(regionManager.MockRegions.Regions.ContainsKey("MyPopupRegion"));
                Assert.IsNotNull(regionManager.MockRegions.Regions["MyPopupRegion"]);
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"], typeof(AllActiveRegion));
                Assert.IsTrue(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors.ContainsKey(DialogActivationBehavior.BehaviorKey));
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(DialogActivationBehavior));
            }
            finally
            {
                ServiceLocator.SetLocatorProvider(() => null);
            }
        }
        public async Task RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control1 = new MockFrameworkElement();
                    var control2 = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                                       {
                                           GetRegionName = d => d == control1 ? "Region1" : "Region2"
                                       };

                    var adapter = new MockRegionAdapter() { Accessor = accessor };

                    var behavior1 = DelayedRegionCreationBehaviorFixture.GetBehavior(control1, accessor, adapter);
                    var behavior2 = DelayedRegionCreationBehaviorFixture.GetBehavior(control2, accessor, adapter);

                    behavior1.Attach();
                    behavior2.Attach();

                    accessor.UpdateRegions();

                    Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
                    Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
                    Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));
                });
        }
        public virtual void RemovesOnlyDeactivatedItemsInRegionBasedOnDataContextKeepAlive()
        {
            // Arrange
            var retionItemDataContextToKeepAlive = new Mock <IRegionMemberLifetime>();

            retionItemDataContextToKeepAlive.Setup(i => i.KeepAlive).Returns(true);

            var regionItemToKeepAlive = new MockFrameworkElement();

            regionItemToKeepAlive.DataContext = retionItemDataContextToKeepAlive.Object;
            Region.Add(regionItemToKeepAlive);
            Region.Activate(regionItemToKeepAlive);

            var regionItemMock = new Mock <IRegionMemberLifetime>();

            regionItemMock.Setup(i => i.KeepAlive).Returns(false);

            var regionItem = new MockFrameworkElement();

            regionItem.DataContext = regionItemMock.Object;

            Region.Add(regionItem);
            Region.Activate(regionItem);

            // Act
            Region.Deactivate(regionItem);

            // Assert
            Assert.IsFalse(Region.Views.Contains(regionItem));
            Assert.IsTrue(Region.Views.Contains(regionItemToKeepAlive));
        }
Example #7
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);
        }
Example #8
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);
        }
Example #9
0
        public void WhenClearChildViewsPropertyIsTrue_ThenChildViewsRegionManagerIsCleared()
        {
            var regionManager = new MockRegionManager();

            var region = new Region();

            region.RegionManager = regionManager;

            var behavior = new ClearChildViewsRegionBehavior();

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

            var childView = new MockFrameworkElement();

            region.Add(childView);

            ClearChildViewsRegionBehavior.SetClearChildViews(childView, true);

            Assert.Equal(regionManager, childView.GetValue(RegionManager.RegionManagerProperty));

            region.RegionManager = null;

            Assert.Null(childView.GetValue(RegionManager.RegionManagerProperty));
        }
Example #10
0
        public void WhenRegionManagerChangesToNotNullValue_ThenChildViewsRegionManagerIsNotCleared()
        {
            var regionManager = new MockRegionManager();

            var region = new Region();

            region.RegionManager = regionManager;

            var behavior = new ClearChildViewsRegionBehavior();

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

            var childView = new MockFrameworkElement();

            region.Add(childView);

            childView.SetValue(ClearChildViewsRegionBehavior.ClearChildViewsProperty, true);

            Assert.AreEqual(regionManager, childView.GetValue(RegionManager.RegionManagerProperty));

            region.RegionManager = new MockRegionManager();

            Assert.IsNotNull(childView.GetValue(RegionManager.RegionManagerProperty));
        }
Example #11
0
        public void RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
        {
            var control1 = new MockFrameworkElement();
            var control2 = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
            {
                GetRegionName = d => d == control1 ? "Region1" : "Region2"
            };

            var adapter = new MockRegionAdapter();

            adapter.Accessor = accessor;

            var behavior1 = this.GetBehavior(control1, accessor, adapter);
            var behavior2 = this.GetBehavior(control2, accessor, adapter);

            behavior1.Attach();
            behavior2.Attach();

            accessor.UpdateRegions();

            Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
            Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
            Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));
        }
Example #12
0
        public void ShouldCreateRegion()
        {
            try
            {
                var regionManager = new MockRegionManager();
                ServiceLocator.SetLocatorProvider(() => new MockServiceLocator(() => regionManager));

                FrameworkElement hostControl = new MockFrameworkElement();
                RegionPopupBehaviors.RegisterNewPopupRegion(hostControl, "MyPopupRegion");
                RegionPopupBehaviors.SetCreatePopupRegionWithName(hostControl, "MyPopupRegion");
                Assert.AreEqual("MyPopupRegion", RegionPopupBehaviors.GetCreatePopupRegionWithName(hostControl));

                var style = new Style();
                RegionPopupBehaviors.SetContainerWindowStyle(hostControl, style);
                Assert.AreEqual(style, RegionPopupBehaviors.GetContainerWindowStyle(hostControl));

                Assert.IsTrue(regionManager.MockRegions.Regions.ContainsKey("MyPopupRegion"));
                Assert.IsNotNull(regionManager.MockRegions.Regions["MyPopupRegion"]);
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"], typeof(SingleActiveRegion));
                Assert.IsTrue(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors.ContainsKey(DialogActivationBehavior.BehaviorKey));
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(WindowDialogActivationBehavior));
            }
            finally
            {
                ServiceLocator.SetLocatorProvider(() => null);
            }
        }
        public void ShouldCreateRegion()
        {
            try
            {
                var regionManager = new MockRegionManager();
                ServiceLocator.SetLocatorProvider(() => new MockServiceLocator(() => regionManager));

                FrameworkElement hostControl = new MockFrameworkElement();
                RegionPopupBehaviors.RegisterNewPopupRegion(hostControl, "MyPopupRegion");

                Assert.IsTrue(regionManager.MockRegions.Regions.ContainsKey("MyPopupRegion"));
                Assert.IsNotNull(regionManager.MockRegions.Regions["MyPopupRegion"]);
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"], typeof(SingleActiveRegion));
                Assert.IsTrue(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors.ContainsKey(DialogActivationBehavior.BehaviorKey));
#if SILVERLIGHT
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(PopupDialogActivationBehavior));
#else
                Assert.IsInstanceOfType(regionManager.MockRegions.Regions["MyPopupRegion"].Behaviors[DialogActivationBehavior.BehaviorKey], typeof(WindowDialogActivationBehavior));
#endif
            }
            finally
            {
                ServiceLocator.SetLocatorProvider(() => null);
            }
        }
        public void TriggerReceivesNotificationsFromBindedInteractionRequest()
        {
            var page = new MockFrameworkElement();

            var myNotificationAwareObject = new MyNotificationAwareClass();
            var binding = new Binding("InteractionRequest")
            {
                Source = myNotificationAwareObject, Mode = BindingMode.OneWay
            };
            var trigger = new InteractionRequestTrigger()
            {
                SourceObject = binding,
            };

            InteractionRequestedEventArgs eventArgs = null;
            var actionListener = new ActionListener((e) => { eventArgs = (InteractionRequestedEventArgs)e; });

            trigger.Actions.Add(actionListener);
            trigger.Attach(page);

            myNotificationAwareObject.InteractionRequest.Raise(new Notification {
                Title = "Bar"
            });
            Assert.IsNotNull(eventArgs);
            Assert.AreEqual("Bar", eventArgs.Context.Title);
        }
        public void RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
        {
            var control1 = new MockFrameworkElement();
            var control2 = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
                               {
                                   GetRegionName = d => d == control1 ? "Region1" : "Region2"
                               };

            var adapter = new MockRegionAdapter();
            adapter.Accessor = accessor;

            var behavior1 = this.GetBehavior(control1, accessor, adapter);
            var behavior2 = this.GetBehavior(control2, accessor, adapter);

            behavior1.Attach();
            behavior2.Attach();

            accessor.UpdateRegions();

            Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
            Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
            Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));

        }
        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 RegionGetsCreatedWhenAccessingRegions()
        {
            var control = new MockFrameworkElement();

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

            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();

            accessor.UpdateRegions();

            Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
            Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
        }
        public virtual void WillRemoveDeactivatedItemIfDataContextKeepAliveAttributeFalse()
        {
            // Arrange
            var regionItemDataContext = new RegionMemberNotKeptAlive();
            var regionItem            = new MockFrameworkElement()
            {
                DataContext = regionItemDataContext
            };

            Region.Add(regionItem);
            Region.Activate(regionItem);

            // Act
            Region.Deactivate(regionItem);

            // Assert
            Assert.IsFalse(Region.Views.Contains(regionItem));
        }
        public void RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
        {
            var control = new MockFrameworkElement();

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

            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();
            accessor.UpdateRegions();
            IRegion region = RegionManager.GetObservableRegion(control).Value;

            accessor.UpdateRegions();

            Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
        }
        public void ShouldSetDataContextInContainerTabItemToContainedFrameworkElementsDataContext()
        {
            var tabControl = new TabControl();
            var adapter    = new TabControlRegionAdapter(null);
            var region     = adapter.Initialize(tabControl, "region");

            var dataContext = new object();
            var view        = new MockFrameworkElement()
            {
                DataContext = dataContext
            };

            region.Add(view);

            TabItem container = (TabItem)tabControl.Items[0];

            Assert.AreSame(dataContext, container.DataContext);
        }
        public void RegionGetsCreatedWhenAccessingRegions()
        {
            var control = new MockFrameworkElement();

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

            var behavior = this.GetBehavior(control, accessor);

            behavior.Attach();

            accessor.UpdateRegions();

            Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
            Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
        }
Example #22
0
        public void BehaviorShouldUnhookEventWhenDetaching()
        {
            var control = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
            {
                GetRegionName = d => "myRegionName",
            };
            var behavior = this.GetBehavior(control, accessor);

            behavior.Attach();

            int startingCount = accessor.GetSubscribersCount();

            behavior.Detach();

            Assert.AreEqual <int>(startingCount - 1, accessor.GetSubscribersCount());
        }
Example #23
0
        public void WhenWindowContentIsSet_ShouldWrapContentInCommonWindow()
        {
            MockFrameworkElement      element           = new MockFrameworkElement();
            TestablePopupWindowAction popupWindowAction = new TestablePopupWindowAction();

            popupWindowAction.WindowContent = element;

            INotification notification = new Notification();

            notification.Title   = "Title";
            notification.Content = "Content";

            Window window = popupWindowAction.GetWindow(notification);

            Assert.IsNotType <DefaultNotificationWindow>(window);
            Assert.IsNotType <DefaultConfirmationWindow>(window);
            Assert.IsType <DefaultWindow>(window);
        }
Example #24
0
        public void RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
        {
            var control = new MockFrameworkElement();

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

            var behavior = this.GetBehavior(control, accessor);

            behavior.Attach();
            accessor.UpdateRegions();
            IRegion region = RegionManager.GetObservableRegion(control).Value;

            accessor.UpdateRegions();

            Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
        }
        public void WhenClearChildViewsPropertyIsNotSet_ThenChildViewsRegionManagerIsNotCleared()
        {
            var regionManager = new MockRegionManager();

            var region = new Region();
            region.RegionManager = regionManager;

            var behavior = new ClearChildViewsRegionBehavior();
            behavior.Region = region;
            behavior.Attach();

            var childView = new MockFrameworkElement();
            region.Add(childView);

            Assert.AreEqual(regionManager, childView.GetValue(RegionManager.RegionManagerProperty));

            region.RegionManager = null;

            Assert.AreEqual(regionManager, childView.GetValue(RegionManager.RegionManagerProperty));
        }
Example #26
0
        public void WhenDataContextOfWindowContentImplementsIInteractionRequestAware_ShouldSetUpProperties()
        {
            MockInteractionRequestAwareElement dataContext = new MockInteractionRequestAwareElement();
            MockFrameworkElement      element           = new MockFrameworkElement();
            TestablePopupWindowAction popupWindowAction = new TestablePopupWindowAction();

            element.DataContext             = dataContext;
            popupWindowAction.WindowContent = element;

            INotification notification = new Notification();

            notification.Title   = "Title";
            notification.Content = "Content";

            Window window = popupWindowAction.GetWindow(notification);

            Assert.NotNull(dataContext.Notification);
            Assert.Same(dataContext.Notification, notification);
            Assert.NotNull(dataContext.FinishInteraction);
        }
        public virtual void RemovesRegionItemIfDataContextReturnsKeepAliveFalse()
        {
            // Arrange
            var regionItemMock = new Mock <IRegionMemberLifetime>();

            regionItemMock.Setup(i => i.KeepAlive).Returns(false);

            var regionItem = new MockFrameworkElement();

            regionItem.DataContext = regionItemMock.Object;

            Region.Add(regionItem);
            Region.Activate(regionItem);

            // Act
            Region.Deactivate(regionItem);

            // Assert
            Assert.IsFalse(Region.Views.Contains(regionItem));
        }
        public async Task RegionGetsCreatedWhenAccessingRegions()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

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

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();

                    accessor.UpdateRegions();

                    Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
                    Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
                });
        }
Example #29
0
        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 WhenAssociatedObjectIsUnloadedAndDiscarded_ShouldBeGarbageCollected()
        {
            InteractionRequest <INotification> request = new InteractionRequest <INotification>();
            TestableInteractionRequestTrigger  trigger = new TestableInteractionRequestTrigger();
            MockFrameworkElement associatedObject      = new MockFrameworkElement();

            trigger.Attach(associatedObject);
            trigger.SourceObject = request;

            WeakReference weakTrigger = new WeakReference(trigger);

            trigger = null;

            GC.Collect();
            Assert.IsTrue(weakTrigger.IsAlive);

            associatedObject.RaiseUnloaded();
            associatedObject = null;
            GC.Collect();
            Assert.IsFalse(weakTrigger.IsAlive);
        }
        public async Task WhenClearChildViewsPropertyIsNotSet_ThenChildViewsRegionManagerIsNotCleared()
        {
            await ExecuteOnUIThread(() =>
                {
                    var regionManager = new MockRegionManager();

                    var region = new Region() { RegionManager = regionManager };

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

                    var childView = new MockFrameworkElement();
                    region.Add(childView);

                    Assert.AreEqual(regionManager, childView.GetValue(RegionManager.RegionManagerProperty));

                    region.RegionManager = null;

                    Assert.AreEqual(regionManager, childView.GetValue(RegionManager.RegionManagerProperty));
                });
        }
        public void ShouldCleanupBehaviorOnceRegionIsCreated()
        {
            var control = new MockFrameworkElement();

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

            var           behavior = this.GetBehavior(control, accessor);
            WeakReference behaviorWeakReference = new WeakReference(behavior);

            behavior.Attach();
            accessor.UpdateRegions();
            Assert.IsTrue(behaviorWeakReference.IsAlive);
            GC.KeepAlive(behavior);

            behavior = null;
            GC.Collect();

            Assert.IsFalse(behaviorWeakReference.IsAlive);
        }
Example #33
0
        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 WhenAssociatedObjectIsReloaded_ShouldReactToEventBeingRaisedAgain()
        {
            InteractionRequest <INotification> request = new InteractionRequest <INotification>();
            TestableInteractionRequestTrigger  trigger = new TestableInteractionRequestTrigger();
            MockFrameworkElement associatedObject      = new MockFrameworkElement();

            trigger.Attach(associatedObject);
            trigger.SourceObject = request;

            Assert.IsTrue(trigger.ExecutionCount == 0);

            request.Raise(new Notification());
            Assert.IsTrue(trigger.ExecutionCount == 1);

            associatedObject.RaiseUnloaded();
            request.Raise(new Notification());
            Assert.IsTrue(trigger.ExecutionCount == 1);

            associatedObject.RaiseLoaded();
            request.Raise(new Notification());
            Assert.IsTrue(trigger.ExecutionCount == 2);
        }
Example #35
0
        public void BehaviorDoesNotPreventControlFromBeingGarbageCollected()
        {
            var           control = new MockFrameworkElement();
            WeakReference controlWeakReference = new WeakReference(control);

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

            var behavior = this.GetBehavior(control, accessor);

            behavior.Attach();

            Assert.IsTrue(controlWeakReference.IsAlive);
            GC.KeepAlive(control);

            control = null;
            GC.Collect();

            Assert.IsFalse(controlWeakReference.IsAlive);
        }
Example #36
0
        public void WhenViewHasBeenRemovedAndRegionManagerPropertyCleared_ThenItCanBeAddedAgainToARegion()
        {
            IRegion region = new Region {
                RegionManager = new MockRegionManager()
            };

            var view = new MockFrameworkElement();

            var scopedRegionManager = region.Add(view, null, true);

            Assert.Equal(view, region.Views.First());

            region.Remove(view);

            view.ClearValue(RegionManager.RegionManagerProperty);

            Assert.Empty(region.Views.Cast <object>());

            var newScopedRegion = region.Add(view, null, true);

            Assert.Equal(view, region.Views.First());

            Assert.Same(newScopedRegion, view.GetValue(RegionManager.RegionManagerProperty));
        }
Example #37
0
        public void RegionGetsCreatedWhenAccessingRegions()
        {
            var control1 = new MockFrameworkElement();
            var control2 = new MockFrameworkContentElement();

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

            var behavior1 = this.GetBehavior(control1, accessor);

            behavior1.Attach();
            var behavior2 = this.GetBehavior(control2, accessor);

            behavior2.Attach();

            accessor.UpdateRegions();

            Assert.NotNull(RegionManager.GetObservableRegion(control1).Value);
            Assert.IsAssignableFrom <IRegion>(RegionManager.GetObservableRegion(control1).Value);
            Assert.NotNull(RegionManager.GetObservableRegion(control2).Value);
            Assert.IsAssignableFrom <IRegion>(RegionManager.GetObservableRegion(control2).Value);
        }
        public async Task BehaviorShouldUnhookEventWhenDetaching()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                                       {
                                           GetRegionName = d => "myRegionName",
                                       };
                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();

                    int startingCount = accessor.GetSubscribersCount();

                    behavior.Detach();

                    Assert.AreEqual<int>(startingCount - 1, accessor.GetSubscribersCount());
                });
        }
        public async Task ShouldCleanupBehaviorOnceRegionIsCreated()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

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

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    WeakReference behaviorWeakReference = new WeakReference(behavior);
                    behavior.Attach();
                    accessor.UpdateRegions();
                    Assert.IsTrue(behaviorWeakReference.IsAlive);
                    GC.KeepAlive(behavior);

                    behavior = null;
                    GC.Collect();

                    Assert.IsFalse(behaviorWeakReference.IsAlive);
                });
        }
        public async Task RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

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

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();
                    accessor.UpdateRegions();
                    IRegion region = RegionManager.GetObservableRegion(control).Value;

                    accessor.UpdateRegions();

                    Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
                });
        }
        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 virtual void RemovesRegionItemIfDataContextReturnsKeepAliveFalse()
        {
            // Arrange
            var regionItemMock = new Mock<IRegionMemberLifetime>();
            regionItemMock.Setup(i => i.KeepAlive).Returns(false);

            var regionItem = new MockFrameworkElement();
            regionItem.DataContext = regionItemMock.Object;

            Region.Add(regionItem);
            Region.Activate(regionItem);

            // Act
            Region.Deactivate(regionItem);

            // Assert
            Assert.IsFalse(Region.Views.Contains(regionItem));
        }
        public void BehaviorShouldUnhookEventWhenDetaching()
        {
            var control = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
                               {
                                   GetRegionName = d => "myRegionName",
                               };
            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();

            int startingCount = accessor.GetSubscribersCount();

            behavior.Detach();

            Assert.AreEqual<int>(startingCount - 1, accessor.GetSubscribersCount());
        }
        public virtual void WillRemoveDeactivatedItemIfDataContextKeepAliveAttributeFalse()
        {
            // Arrange
            var regionItemDataContext = new RegionMemberNotKeptAlive();
            var regionItem = new MockFrameworkElement() { DataContext = regionItemDataContext };
            Region.Add(regionItem);
            Region.Activate(regionItem);

            // Act
            Region.Deactivate(regionItem);

            // Assert
            Assert.IsFalse(Region.Views.Contains(regionItem));
        }
        public void WhenViewHasBeenRemovedAndRegionManagerPropertyCleared_ThenItCanBeAddedAgainToARegion()
        {
            IRegion region = new Region { RegionManager = new MockRegionManager() };

            var view = new MockFrameworkElement();

            var scopedRegionManager = region.Add(view, null, true);

            Assert.AreEqual(view, region.Views.First());

            region.Remove(view);

            view.ClearValue(RegionManager.RegionManagerProperty);

            Assert.AreEqual(0, region.Views.Cast<object>().Count());

            var newScopedRegion = region.Add(view, null, true);

            Assert.AreEqual(view, region.Views.First());

            Assert.AreSame(newScopedRegion, view.GetValue(RegionManager.RegionManagerProperty));
        }
        public void ShouldSetDataContextInContainerTabItemToContainedFrameworkElementsDataContext()
        {
            var tabControl = new TabControl();
            var adapter = new TabControlRegionAdapter(null);
            var region = adapter.Initialize(tabControl, "region");

            var dataContext = new object();
            var view = new MockFrameworkElement() { DataContext = dataContext };
            region.Add(view);

            TabItem container = (TabItem)tabControl.Items[0];
            Assert.AreSame(dataContext, container.DataContext);
        }
        public async Task WhenAViewIsRemovedFromARegion_ThenRegionContextIsNotClearedInRegion()
        {
            await ExecuteOnUIThread(() =>
                {
                    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 BehaviorDoesNotPreventControlFromBeingGarbageCollectedWhenRegionWasCreated()
        {
            var control = new MockFrameworkElement();
            WeakReference controlWeakReference = new WeakReference(control);

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

            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();
            accessor.UpdateRegions();
            
            Assert.IsTrue(controlWeakReference.IsAlive);
            GC.KeepAlive(control);

            control = null;
            GC.Collect();

            Assert.IsFalse(controlWeakReference.IsAlive);
        }
        public async Task BehaviorDoesNotPreventControlFromBeingGarbageCollectedWhenRegionWasCreated()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();
                    WeakReference controlWeakReference = new WeakReference(control);

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

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();
                    accessor.UpdateRegions();

                    Assert.IsTrue(controlWeakReference.IsAlive);
                    GC.KeepAlive(control);

                    control = null;
                    GC.Collect();

                    Assert.IsFalse(controlWeakReference.IsAlive);
                });
        }
        public virtual void RemovesOnlyDeactivatedItemsInRegionBasedOnDataContextKeepAlive()
        {
            // Arrange
            var retionItemDataContextToKeepAlive = new Mock<IRegionMemberLifetime>();
            retionItemDataContextToKeepAlive.Setup(i => i.KeepAlive).Returns(true);

            var regionItemToKeepAlive = new MockFrameworkElement();
            regionItemToKeepAlive.DataContext = retionItemDataContextToKeepAlive.Object;
            Region.Add(regionItemToKeepAlive);
            Region.Activate(regionItemToKeepAlive);

            var regionItemMock = new Mock<IRegionMemberLifetime>();
            regionItemMock.Setup(i => i.KeepAlive).Returns(false);

            var regionItem = new MockFrameworkElement();
            regionItem.DataContext = regionItemMock.Object;

            Region.Add(regionItem);
            Region.Activate(regionItem);

            // Act
            Region.Deactivate(regionItem);

            // Assert
            Assert.IsFalse(Region.Views.Contains(regionItem));
            Assert.IsTrue(Region.Views.Contains(regionItemToKeepAlive));
        }
        public void ShouldCleanupBehaviorOnceRegionIsCreated()
        {
            var control = new MockFrameworkElement();

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

            var behavior = this.GetBehavior(control, accessor);
            WeakReference behaviorWeakReference = new WeakReference(behavior);
            behavior.Attach();
            accessor.UpdateRegions();
            Assert.IsTrue(behaviorWeakReference.IsAlive);
            GC.KeepAlive(behavior);

            behavior = null;
            GC.Collect();

            Assert.IsFalse(behaviorWeakReference.IsAlive);
        }