public void WhenNavigatingWithQueryString_ViewIsActivated()
        {
            // Prepare
            object view = new object();
            Uri viewUri = new Uri(view.GetType().Name + "?MyQuery=true", UriKind.Relative);

            IRegion region = new Region();
            region.Add(view);

            string regionName = "RegionName";
            RegionManager regionManager = new RegionManager();
            regionManager.Regions.Add(regionName, region);

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

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

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

            // Act
            bool isNavigationSuccessful = false;
            target.RequestNavigate(viewUri, nr => isNavigationSuccessful = nr.Result == true);

            // Verify
            Assert.IsTrue(isNavigationSuccessful);
            bool isViewActive = region.ActiveViews.Contains(view);
            Assert.IsTrue(isViewActive);
        }
        public void BeforeNavigating_NavigatingEventIsRaised()
        {
            // Prepare
            object view = new object();
            Uri viewUri = new Uri(view.GetType().Name, UriKind.Relative);

            IRegion region = new Region();
            region.Add(view);

            string regionName = "RegionName";
            RegionManager regionManager = new RegionManager();
            regionManager.Regions.Add(regionName, region);

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

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

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

            bool isNavigatingRaised = false;
            target.Navigating += delegate(object sender, RegionNavigationEventArgs e)
            {
                if (sender == target)
                {
                    isNavigatingRaised = true;
                }
            };

            // Act
            bool isNavigationSuccessful = false;
            target.RequestNavigate(viewUri, nr => isNavigationSuccessful = nr.Result == true);

            // Verify
            Assert.IsTrue(isNavigationSuccessful);
            Assert.IsTrue(isNavigatingRaised);
        }
        public void WhenRequestNavigateAwayOnDataContextAcceptsThroughCallback_ThenNavigationProceeds()
        {
            // 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(true))
                .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 RegionNavigationContentLoader(serviceLocator);
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

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

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

            // Verify
            view1DataContextMock.VerifyAll();
            Assert.IsTrue(navigationSucceeded);
            CollectionAssert.AreEqual(new object[] { view1, view2 }, region.ActiveViews.ToArray());
        }
        public void WhenNavigating_NavigationIsRecordedInJournal()
        {
            // Prepare
            object view = new object();
            Uri viewUri = new Uri(view.GetType().Name, UriKind.Relative);

            IRegion region = new Region();
            region.Add(view);

            string regionName = "RegionName";
            RegionManager regionManager = new RegionManager();
            regionManager.Regions.Add(regionName, region);

            IRegionNavigationJournalEntry journalEntry = new RegionNavigationJournalEntry();

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

            IServiceLocator serviceLocator = serviceLocatorMock.Object;
            RegionNavigationContentLoader contentLoader = new RegionNavigationContentLoader(serviceLocator);

            var journalMock = new Mock<IRegionNavigationJournal>();
            journalMock.Setup(x => x.RecordNavigation(journalEntry)).Verifiable();

            IRegionNavigationJournal journal = journalMock.Object;


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

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

            // Verify
            Assert.IsNotNull(journalEntry);
            Assert.AreEqual(viewUri, journalEntry.Uri);
            journalMock.VerifyAll();
        }
        public void WhenNavigatingAndBothViewAndDataContextImplementINavigationAware_ThenNavigatedIsInvokesOnNavigation()
        {
            // Prepare
            var region = new Region();

            Mock<FrameworkElement> mockFrameworkElement = new Mock<FrameworkElement>();
            Mock<INavigationAware> mockINavigationAwareView = mockFrameworkElement.As<INavigationAware>();
            mockINavigationAwareView.Setup(ina => ina.IsNavigationTarget(It.IsAny<NavigationContext>())).Returns(true);

            Mock<INavigationAware> mockINavigationAwareDataContext = new Mock<INavigationAware>();
            mockINavigationAwareDataContext.Setup(ina => ina.IsNavigationTarget(It.IsAny<NavigationContext>())).Returns(true);
            mockFrameworkElement.Object.DataContext = mockINavigationAwareDataContext.Object;

            var view = mockFrameworkElement.Object;
            region.Add(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 RegionNavigationContentLoader(serviceLocator);
            IRegionNavigationJournal journal = new Mock<IRegionNavigationJournal>().Object;

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

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

            // Verify
            mockINavigationAwareView.Verify(v => v.OnNavigatedTo(It.Is<NavigationContext>(nc => nc.Uri == navigationUri)));
            mockINavigationAwareDataContext.Verify(v => v.OnNavigatedTo(It.Is<NavigationContext>(nc => nc.Uri == navigationUri)));
        }