Exemplo n.º 1
0
        public void ActivateNonAddedViewThrows()
        {
            IRegion region = new Region();

            object nonAddedView = new object();

            region.Activate(nonAddedView);
        }
Exemplo n.º 2
0
        public void ActivateNullViewThrows()
        {
            var ex = Assert.Throws <ArgumentNullException>(() =>
            {
                IRegion region = new Region();

                region.Activate(null);
            });
        }
        public void WhenViewModelAcceptsNavigationOutAfterNewIncomingRequestIsReceived_ThenOriginalRequestIsIgnored()
        {
            var region = new Region();

            var viewModelMock = new Mock <IConfirmRegionNavigationRequest>();

            var viewMock = new Mock <View>();
            var view     = viewMock.Object;

            view.BindingContext = viewModelMock.Object;

            var confirmationRequests = new List <Action <bool> >();

            viewModelMock
            .Setup(icnr => icnr.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Callback <INavigationContext, Action <bool> >((nc, c) => { confirmationRequests.Add(c); });

            region.Add(view);
            region.Activate(view);

            var navigationUri = new Uri("", UriKind.Relative);

            var containerMock = new Mock <IContainerExtension>();

            containerMock
            .Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry)))
            .Returns(new RegionNavigationJournalEntry());

            var contentLoaderMock = new Mock <IRegionNavigationContentLoader>();

            contentLoaderMock
            .Setup(cl => cl.LoadContent(region, It.IsAny <INavigationContext>()))
            .Returns(view);

            var container     = containerMock.Object;
            var contentLoader = contentLoaderMock.Object;
            var journal       = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            IRegionNavigationResult firstNavigation  = null;
            IRegionNavigationResult secondNavigation = null;

            target.RequestNavigate(navigationUri, nr => firstNavigation  = nr);
            target.RequestNavigate(navigationUri, nr => secondNavigation = nr);

            Assert.Equal(2, confirmationRequests.Count);

            confirmationRequests[0](true);
            confirmationRequests[1](true);

            Assert.False(firstNavigation.Result);
            Assert.True(secondNavigation.Result.Value);
        }
Exemplo n.º 4
0
        public void ActivateNonAddedViewThrows()
        {
            var ex = Assert.Throws <ArgumentException>(() =>
            {
                IRegion region = new Region();

                var nonAddedView = new ContentView();

                region.Activate(nonAddedView);
            });
        }
Exemplo n.º 5
0
        public void RemovingViewAlsoRemovesItFromActiveViews()
        {
            IRegion region = new Region();
            var     model  = new ContentView();

            region.Add(model);
            region.Activate(model);
            Assert.True(region.ActiveViews.Contains(model));

            region.Remove(model);

            Assert.False(region.ActiveViews.Contains(model));
        }
        public void WhenRequestNavigateAwayOnBindingContextRejectsThroughCallback_ThenNavigationDoesNotProceed()
        {
            // Prepare
            var region = new Region();

            var view1BindingContextMock = new Mock <IConfirmRegionNavigationRequest>();

            view1BindingContextMock
            .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Callback <INavigationContext, Action <bool> >((nc, c) => c(false))
            .Verifiable();

            var view1Mock = new Mock <View>();
            var view1     = view1Mock.Object;

            view1.BindingContext = view1BindingContextMock.Object;

            var view2 = new StackLayout();

            region.Add(view1);
            region.Add(view2);

            region.Activate(view1);

            var navigationUri = new Uri(view2.GetType().Name, UriKind.Relative);

            var containerMock = new Mock <IContainerExtension>();

            containerMock.Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry))).Returns(new RegionNavigationJournalEntry());

            IContainerExtension container    = containerMock.Object;
            var contentLoader                = new Mock <RegionNavigationContentLoader>(container).Object;
            IRegionNavigationJournal journal = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            // Act
            var navigationFailed = false;

            target.RequestNavigate(navigationUri, nr => { navigationFailed = nr.Result == false; });

            // Verify
            view1BindingContextMock.VerifyAll();
            Assert.True(navigationFailed);
            Assert.Equal(new object[] { view1 }, region.ActiveViews.ToArray());
        }
        public void WhenNavigationFromViewThatIsNavigationAware_OnlyNotifiesOnNavigateFromForActiveViews()
        {
            // Arrange

            bool navigationFromInvoked = false;

            var region = new Region();

            var viewMock = new Mock <View>();

            viewMock
            .As <IRegionAware>()
            .Setup(x => x.OnNavigatedFrom(It.IsAny <INavigationContext>())).Callback(() => navigationFromInvoked = true);
            var view = viewMock.Object;

            region.Add(view);

            var targetViewMock = new Mock <StackLayout>();

            targetViewMock.As <IRegionAware>();
            region.Add(targetViewMock.Object);

            var activeViewMock = new Mock <Grid>();

            activeViewMock.As <IRegionAware>();
            region.Add(activeViewMock.Object);

            region.Activate(activeViewMock.Object);

            var containerMock = new Mock <IContainerExtension>();

            containerMock.Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry))).Returns(new RegionNavigationJournalEntry());

            var navigationUri                = new Uri(targetViewMock.Object.GetType().Name, UriKind.Relative);
            IContainerExtension container    = containerMock.Object;
            var contentLoader                = new Mock <RegionNavigationContentLoader>(container).Object;
            IRegionNavigationJournal journal = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            Assert.False(navigationFromInvoked);
        }
        public void WhenNavigatingAndBindingContextOnCurrentlyActiveViewImplementsINavigateWithVeto_ThenNavigationRequestQueriesForVeto()
        {
            // Prepare
            var region = new Region();

            var viewModelMock = new Mock <IConfirmRegionNavigationRequest>();

            viewModelMock
            .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Verifiable();

            var viewMock = new Mock <View>();

            var view = viewMock.Object;

            view.BindingContext = viewModelMock.Object;

            region.Add(view);
            region.Activate(view);

            var navigationUri = new Uri(view.GetType().Name, UriKind.Relative);

            var containerMock = new Mock <IContainerExtension>();

            containerMock.Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry))).Returns(new RegionNavigationJournalEntry());

            IContainerExtension container    = containerMock.Object;
            var contentLoader                = new Mock <RegionNavigationContentLoader>(container).Object;
            IRegionNavigationJournal journal = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            viewModelMock.VerifyAll();
        }
Exemplo n.º 9
0
        public void ShowViewFiresViewShowedEvent()
        {
            bool viewActivated = false;

            IRegion region = new Region();
            var     model  = new ContentView();

            region.ActiveViews.CollectionChanged += (o, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Contains(model))
                {
                    viewActivated = true;
                }
            };
            region.Add(model);
            Assert.False(viewActivated);

            region.Activate(model);

            Assert.True(viewActivated);
        }
Exemplo n.º 10
0
        public void WhenNavigatingFromActiveViewWithNavigatinAwareDataConext_NotifiesContextOfNavigatingFrom()
        {
            // Arrange
            var region = new Region();

            var mockBindingContext = new Mock <IRegionAware>();

            var view1Mock = new Mock <View>();
            var view1     = view1Mock.Object;

            view1.BindingContext = mockBindingContext.Object;

            region.Add(view1);

            var view2 = new StackLayout();

            region.Add(view2);

            region.Activate(view1);

            var containerMock = new Mock <IContainerExtension>();

            containerMock.Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry))).Returns(new RegionNavigationJournalEntry());

            var navigationUri                = new Uri(view2.GetType().Name, UriKind.Relative);
            IContainerExtension container    = containerMock.Object;
            var contentLoader                = new Mock <RegionNavigationContentLoader>(container).Object;
            IRegionNavigationJournal journal = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            mockBindingContext.Verify(v => v.OnNavigatedFrom(It.Is <INavigationContext>(ctx => ctx.Uri == navigationUri && ctx.Parameters.Count() == 0)));
        }
Exemplo n.º 11
0
        public void WhenTargetViewCreationThrowsWithAsyncConfirmation_ThenExceptionIsProvidedToNavigationCallback()
        {
            var containerMock = new Mock <IContainerExtension>();

            var targetException   = new Exception();
            var targetHandlerMock = new Mock <IRegionNavigationContentLoader>();

            targetHandlerMock
            .Setup(th => th.LoadContent(It.IsAny <IRegion>(), It.IsAny <INavigationContext>()))
            .Throws(targetException);

            var journalMock = new Mock <IRegionNavigationJournal>();

            Action <bool> navigationCallback = null;
            var           viewMock           = new Mock <View>();

            viewMock
            .As <IConfirmRegionNavigationRequest>()
            .Setup(v => v.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Callback <INavigationContext, Action <bool> >((nc, c) => { navigationCallback = c; });

            var region = new Region();

            region.Add(viewMock.Object);
            region.Activate(viewMock.Object);

            var target = new RegionNavigationService(containerMock.Object, targetHandlerMock.Object, journalMock.Object)
            {
                Region = region
            };

            IRegionNavigationResult result = null;

            target.RequestNavigate(new Uri("", UriKind.Relative), nr => result = nr);
            navigationCallback(true);

            Assert.NotNull(result);
            Assert.Same(targetException, result.Error);
        }
Exemplo n.º 12
0
        private void ExecuteNavigation(NavigationContext navigationContext, object[] activeViews, Action <NavigationResult> navigationCallback)
        {
            try
            {
                NotifyActiveViewsNavigatingFrom(navigationContext, activeViews);

                object view = _regionNavigationContentLoader.LoadContent(Region, navigationContext);

                // Raise the navigating event just before activating the view.
                RaiseNavigating(navigationContext);

                Region.Activate(view);

                // Update the navigation journal before notifying others of navigation
                IRegionNavigationJournalEntry journalEntry = _container.Resolve <IRegionNavigationJournalEntry>();
                journalEntry.Uri        = navigationContext.Uri;
                journalEntry.Parameters = navigationContext.Parameters;

                bool persistInHistory = PersistInHistory(view);

                Journal.RecordNavigation(journalEntry, persistInHistory);

                // The view can be informed of navigation
                Action <INavigationAware> action = (n) => n.OnNavigatedTo(navigationContext);
                MvvmHelpers.ViewAndViewModelAction(view, action);

                navigationCallback(new NavigationResult(navigationContext, true));

                // Raise the navigated event when navigation is completed.
                RaiseNavigated(navigationContext);
            }
            catch (Exception e)
            {
                NotifyNavigationFailed(navigationContext, navigationCallback, e);
            }
        }
Exemplo n.º 13
0
        public void ShowViewFiresViewShowedEvent()
        {
            bool viewActivated = false;

            IRegion region = new Region();
            object model = new object();
            region.ActiveViews.CollectionChanged += (o, e) =>
                                                        {
                                                            if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Contains(model))
                                                                viewActivated = true;
                                                        };
            region.Add(model);
            Assert.IsFalse(viewActivated);

            region.Activate(model);

            Assert.IsTrue(viewActivated);
        }
        public void WhenViewModelAcceptsNavigationOutAfterNewIncomingRequestIsReceived_ThenOriginalRequestIsIgnored()
        {
            var region = new Region();

            var viewModelMock = new Mock<IConfirmNavigationRequest>();

            var viewMock = new Mock<FrameworkElement>();
            var view = viewMock.Object;
            view.DataContext = viewModelMock.Object;

            var confirmationRequests = new List<Action<bool>>();

            viewModelMock
                .Setup(icnr => icnr.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Callback<NavigationContext, Action<bool>>((nc, c) => { confirmationRequests.Add(c); });

            region.Add(view);
            region.Activate(view);

            var navigationUri = new Uri("", UriKind.Relative);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock
                .Setup(x => x.GetInstance<IRegionNavigationJournalEntry>())
                .Returns(new RegionNavigationJournalEntry());

            var contentLoaderMock = new Mock<IRegionNavigationContentLoader>();
            contentLoaderMock
                .Setup(cl => cl.LoadContent(region, It.IsAny<NavigationContext>()))
                .Returns(view);

            var serviceLocator = serviceLocatorMock.Object;
            var contentLoader = contentLoaderMock.Object;
            var journal = new Mock<IRegionNavigationJournal>().Object;

            var target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            bool firstNavigation = false;
            bool secondNavigation = false;
            target.RequestNavigate(navigationUri, nr => firstNavigation = nr.Result.Value);
            target.RequestNavigate(navigationUri, nr => secondNavigation = nr.Result.Value);

            Assert.AreEqual(2, confirmationRequests.Count);

            confirmationRequests[0](true);
            confirmationRequests[1](true);

            Assert.IsFalse(firstNavigation);
            Assert.IsTrue(secondNavigation);
        }
Exemplo n.º 15
0
        public void ActivateNullViewThrows()
        {
            IRegion region = new Region();

            region.Activate(null);
        }
Exemplo n.º 16
0
        public void RemovingViewAlsoRemovesItFromActiveViews()
        {
            IRegion region = new Region();
            object model = new object();
            region.Add(model);
            region.Activate(model);
            Assert.IsTrue(region.ActiveViews.Contains(model));

            region.Remove(model);

            Assert.IsFalse(region.ActiveViews.Contains(model));
        }
Exemplo n.º 17
0
        public void WhenNavigating_ThenNavigationRequestQueriesForVetoOnAllActiveViewsIfAllSucceed()
        {
            // Prepare
            var region = new Region();

            var view1Mock = new Mock <View>();

            view1Mock
            .As <IConfirmRegionNavigationRequest>()
            .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Callback <INavigationContext, Action <bool> >((nc, c) => c(true))
            .Verifiable();

            var view1 = view1Mock.Object;

            region.Add(view1);
            region.Activate(view1);

            var view2Mock = new Mock <StackLayout>();

            view2Mock.As <IConfirmRegionNavigationRequest>();

            var view2 = view2Mock.Object;

            region.Add(view2);

            var view3Mock = new Mock <Grid>();

            view3Mock.As <IRegionAware>();

            var view3 = view3Mock.Object;

            region.Add(view3);
            region.Activate(view3);

            var view4Mock = new Mock <Frame>();

            view4Mock
            .As <IConfirmRegionNavigationRequest>()
            .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Callback <INavigationContext, Action <bool> >((nc, c) => c(true))
            .Verifiable();

            var view4 = view4Mock.Object;

            region.Add(view4);
            region.Activate(view4);

            var navigationUri = new Uri(view1.GetType().Name, UriKind.Relative);

            var containerMock = new Mock <IContainerExtension>();

            containerMock.Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry))).Returns(new RegionNavigationJournalEntry());

            IContainerExtension container    = containerMock.Object;
            var contentLoader                = new Mock <RegionNavigationContentLoader>(container).Object;
            IRegionNavigationJournal journal = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            view1Mock.VerifyAll();
            view2Mock
            .As <IConfirmRegionNavigationRequest>()
            .Verify(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()), Times.Never());
            view3Mock.VerifyAll();
            view4Mock.VerifyAll();
        }
Exemplo n.º 18
0
        public void WhenNavigationFailsBecauseBindingContextForActiveViewRejectsIt_ThenNavigationFailedIsRaised()
        {
            // Prepare
            var region = new Region {
                Name = "RegionName"
            };

            var viewModel1Mock = new Mock <IConfirmRegionNavigationRequest>();

            viewModel1Mock
            .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >()))
            .Callback <INavigationContext, Action <bool> >((nc, c) => c(false))
            .Verifiable();

            var view1Mock = new Mock <View>();
            var view1     = view1Mock.Object;

            view1.BindingContext = viewModel1Mock.Object;

            var view2 = new StackLayout();

            region.Add(view1);
            region.Add(view2);

            region.Activate(view1);

            var navigationUri = new Uri(view2.GetType().Name, UriKind.Relative);

            var containerMock = new Mock <IContainerExtension>();

            containerMock.Setup(x => x.Resolve(typeof(IRegionNavigationJournalEntry))).Returns(new RegionNavigationJournalEntry());

            var contentLoaderMock = new Mock <IRegionNavigationContentLoader>();

            contentLoaderMock
            .Setup(cl => cl.LoadContent(region, It.IsAny <INavigationContext>()))
            .Returns(view2);

            var container     = containerMock.Object;
            var contentLoader = contentLoaderMock.Object;
            var journal       = Mock.Of <IRegionNavigationJournal>();

            var target = new RegionNavigationService(container, contentLoader, journal)
            {
                Region = region
            };

            RegionNavigationFailedEventArgs eventArgs = null;

            target.NavigationFailed += delegate(object sender, RegionNavigationFailedEventArgs e)
            {
                if (sender == target)
                {
                    eventArgs = e;
                }
            };

            // Act
            bool?isNavigationSuccessful = null;

            target.RequestNavigate(navigationUri, nr => isNavigationSuccessful = nr.Result);

            // Verify
            viewModel1Mock.VerifyAll();
            Assert.False(isNavigationSuccessful.Value);
            Assert.NotNull(eventArgs);
            Assert.Null(eventArgs.Error);
        }
        public void WhenNavigating_ThenNavigationRequestQueriesForVetoOnAllActiveViewsIfAllSucceed()
        {
            // Prepare
            var region = new Region();

            var view1Mock = new Mock<IConfirmNavigationRequest>();
            view1Mock
                .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Callback<NavigationContext, Action<bool>>((nc, c) => c(true))
                .Verifiable();

            var view1 = view1Mock.Object;
            region.Add(view1);
            region.Activate(view1);

            var view2Mock = new Mock<IConfirmNavigationRequest>();

            var view2 = view2Mock.Object;
            region.Add(view2);

            var view3Mock = new Mock<INavigationAware>();

            var view3 = view3Mock.Object;
            region.Add(view3);
            region.Activate(view3);

            var view4Mock = new Mock<IConfirmNavigationRequest>();
            view4Mock
                .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Callback<NavigationContext, Action<bool>>((nc, c) => c(true))
                .Verifiable();

            var view4 = view4Mock.Object;
            region.Add(view4);
            region.Activate(view4);

            var navigationUri = new Uri(view1.GetType().Name, UriKind.Relative);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IRegionNavigationJournalEntry>()).Returns(new RegionNavigationJournalEntry());

            IServiceLocator serviceLocator = serviceLocatorMock.Object;
            RegionNavigationContentLoader contentLoader = new Mock<RegionNavigationContentLoader>(serviceLocator).Object;
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

            RegionNavigationService target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            view1Mock.VerifyAll();
            view2Mock.Verify(ina => ina.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()), Times.Never());
            view3Mock.VerifyAll();
            view4Mock.VerifyAll();
        }
        public void WhenNavigationFailsBecauseDataContextForActiveViewRejectsIt_ThenNavigationFailedIsRaised()
        {
            // Prepare
            var region = new Region { Name = "RegionName" };

            var viewModel1Mock = new Mock<IConfirmNavigationRequest>();
            viewModel1Mock
                .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Callback<NavigationContext, Action<bool>>((nc, c) => c(false))
                .Verifiable();


            var view1Mock = new Mock<FrameworkElement>();
            var view1 = view1Mock.Object;
            view1.DataContext = viewModel1Mock.Object;

            var view2 = new object();

            region.Add(view1);
            region.Add(view2);

            region.Activate(view1);

            var navigationUri = new Uri(view2.GetType().Name, UriKind.Relative);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock
                .Setup(x => x.GetInstance<IRegionNavigationJournalEntry>())
                .Returns(new RegionNavigationJournalEntry());

            var contentLoaderMock = new Mock<IRegionNavigationContentLoader>();
            contentLoaderMock
                .Setup(cl => cl.LoadContent(region, It.IsAny<NavigationContext>()))
                .Returns(view2);

            var serviceLocator = serviceLocatorMock.Object;
            var contentLoader = contentLoaderMock.Object;
            var journal = new Mock<IRegionNavigationJournal>().Object;

            var target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            RegionNavigationFailedEventArgs eventArgs = null;
            target.NavigationFailed += delegate(object sender, RegionNavigationFailedEventArgs e)
            {
                if (sender == target)
                {
                    eventArgs = e;
                }
            };

            // Act
            bool? isNavigationSuccessful = null;
            target.RequestNavigate(navigationUri, nr => isNavigationSuccessful = nr.Result);

            // Verify
            viewModel1Mock.VerifyAll();
            Assert.IsFalse(isNavigationSuccessful.Value);
            Assert.IsNotNull(eventArgs);
            Assert.IsNull(eventArgs.Error);
        }
        public void WhenNavigatingAndDataContextOnCurrentlyActiveViewImplementsINavigateWithVeto_ThenNavigationRequestQueriesForVeto()
        {
            // Prepare
            var region = new Region();

            var viewModelMock = new Mock<IConfirmNavigationRequest>();
            viewModelMock
                .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Verifiable();

            var viewMock = new Mock<FrameworkElement>();

            var view = viewMock.Object;
            view.DataContext = viewModelMock.Object;

            region.Add(view);
            region.Activate(view);

            var navigationUri = new Uri(view.GetType().Name, UriKind.Relative);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IRegionNavigationJournalEntry>()).Returns(new RegionNavigationJournalEntry());

            IServiceLocator serviceLocator = serviceLocatorMock.Object;
            RegionNavigationContentLoader contentLoader = new Mock<RegionNavigationContentLoader>(serviceLocator).Object;
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

            RegionNavigationService target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            viewModelMock.VerifyAll();
        }
        public void WhenNavigatingFromActiveViewWithNavigatinAwareDataConext_NotifiesContextOfNavigatingFrom()
        {
            // Arrange
            var region = new Region();

            var mockDataContext = new Mock<INavigationAware>();

            var view1Mock = new Mock<FrameworkElement>();
            var view1 = view1Mock.Object;
            view1.DataContext = mockDataContext.Object;

            region.Add(view1);

            var view2 = new object();
            region.Add(view2);

            region.Activate(view1);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IRegionNavigationJournalEntry>()).Returns(new RegionNavigationJournalEntry());

            var navigationUri = new Uri(view2.GetType().Name, UriKind.Relative);
            IServiceLocator serviceLocator = serviceLocatorMock.Object;
            RegionNavigationContentLoader contentLoader = new Mock<RegionNavigationContentLoader>(serviceLocator).Object;
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

            RegionNavigationService target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            mockDataContext.Verify(v => v.OnNavigatedFrom(It.Is<NavigationContext>(ctx => ctx.Uri == navigationUri && ctx.Parameters.Count() == 0)));
        }
        public void WhenNavigationFromViewThatIsNavigationAware_OnlyNotifiesOnNavigateFromForActiveViews()
        {
            // Arrange

            bool navigationFromInvoked = false;

            var region = new Region();

            var viewMock = new Mock<INavigationAware>();
            viewMock
                .Setup(x => x.OnNavigatedFrom(It.IsAny<NavigationContext>())).Callback(() => navigationFromInvoked = true);
            var view = viewMock.Object;
            region.Add(view);

            var targetViewMock = new Mock<INavigationAware>();
            region.Add(targetViewMock.Object);

            var activeViewMock = new Mock<INavigationAware>();
            region.Add(activeViewMock.Object);

            region.Activate(activeViewMock.Object);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IRegionNavigationJournalEntry>()).Returns(new RegionNavigationJournalEntry());

            var navigationUri = new Uri(targetViewMock.Object.GetType().Name, UriKind.Relative);
            IServiceLocator serviceLocator = serviceLocatorMock.Object;
            RegionNavigationContentLoader contentLoader = new Mock<RegionNavigationContentLoader>(serviceLocator).Object;
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

            RegionNavigationService target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            // Act
            target.RequestNavigate(navigationUri, nr => { });

            // Verify
            Assert.IsFalse(navigationFromInvoked);
        }
        public void WhenRequestNavigateAwayOnDataContextRejectsThroughCallback_ThenNavigationDoesNotProceed()
        {
            // Prepare
            var region = new Region();

            var view1DataContextMock = new Mock<IConfirmNavigationRequest>();
            view1DataContextMock
                .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Callback<NavigationContext, Action<bool>>((nc, c) => c(false))
                .Verifiable();

            var view1Mock = new Mock<FrameworkElement>();
            var view1 = view1Mock.Object;
            view1.DataContext = view1DataContextMock.Object;

            var view2 = new object();

            region.Add(view1);
            region.Add(view2);

            region.Activate(view1);

            var navigationUri = new Uri(view2.GetType().Name, UriKind.Relative);

            var serviceLocatorMock = new Mock<IServiceLocator>();
            serviceLocatorMock.Setup(x => x.GetInstance<IRegionNavigationJournalEntry>()).Returns(new RegionNavigationJournalEntry());

            IServiceLocator serviceLocator = serviceLocatorMock.Object;
            RegionNavigationContentLoader contentLoader = new Mock<RegionNavigationContentLoader>(serviceLocator).Object;
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

            RegionNavigationService target = new RegionNavigationService(serviceLocator, contentLoader, journal);
            target.Region = region;

            // Act
            var navigationFailed = false;
            target.RequestNavigate(navigationUri, nr => { navigationFailed = nr.Result == false; });

            // Verify
            view1DataContextMock.VerifyAll();
            Assert.IsTrue(navigationFailed);
            CollectionAssert.AreEqual(new object[] { view1 }, region.ActiveViews.ToArray());
        }
        public void WhenParentViewGetsActivatedOrDeactivated_ThenSyncedChildViewModelThatIsNotAFrameworkElementIsNotUpdated()
        {
            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 ActiveAwareObject();            

            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.Activate(childActiveAwareView);

            Assert.IsTrue(childActiveAwareView.IsActive);

            region.Deactivate(view);

            Assert.IsTrue(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 WhenTargetViewCreationThrowsWithAsyncConfirmation_ThenExceptionIsProvidedToNavigationCallback()
        {
            var serviceLocatorMock = new Mock<IServiceLocator>();

            var targetException = new Exception();
            var targetHandlerMock = new Mock<IRegionNavigationContentLoader>();
            targetHandlerMock
                .Setup(th => th.LoadContent(It.IsAny<IRegion>(), It.IsAny<NavigationContext>()))
                .Throws(targetException);

            var journalMock = new Mock<IRegionNavigationJournal>();

            Action<bool> navigationCallback = null;
            var viewMock = new Mock<IConfirmNavigationRequest>();
            viewMock
                .Setup(v => v.ConfirmNavigationRequest(It.IsAny<NavigationContext>(), It.IsAny<Action<bool>>()))
                .Callback<NavigationContext, Action<bool>>((nc, c) => { navigationCallback = c; });

            var region = new Region();
            region.Add(viewMock.Object);
            region.Activate(viewMock.Object);

            var target = new RegionNavigationService(serviceLocatorMock.Object, targetHandlerMock.Object, journalMock.Object);
            target.Region = region;

            NavigationResult result = null;
            target.RequestNavigate(new Uri("", UriKind.Relative), nr => result = nr);
            navigationCallback(true);

            Assert.IsNotNull(result);
            Assert.AreSame(targetException, result.Error);
        }