public void WhenRegionConstructed_SortComparisonIsDefault() { IRegion region = new Region(); Assert.IsNotNull(region.SortComparison); Assert.AreEqual(region.SortComparison, Region.DefaultSortComparison); }
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))); }
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 WhenRegionHasMultipleViews_ThenViewsWithMatchingFullTypeNameAreConsidered() { // Arrange var serviceLocatorMock = new Mock<IServiceLocator>(); var region = new Region(); var view1 = new TestView(); var view2 = "view"; region.Add(view1); region.Add(view2); var navigationContext = new NavigationContext(null, new Uri(view2.GetType().FullName, UriKind.Relative)); var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object); // Act var returnedView = navigationTargetHandler.LoadContent(region, navigationContext); // Assert Assert.AreSame(view2, returnedView); }
public void AddingDuplicateNamedViewThrows() { IRegion region = new Region(); region.Add(new object(), "MyView"); region.Add(new object(), "MyView"); }
public void WhenViewExistsAndDoesNotImplementINavigationAware_ThenReturnsView() { // Arrange var serviceLocatorMock = new Mock<IServiceLocator>(); var region = new Region(); var view = new TestView(); region.Add(view); var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative)); var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object); // Act var returnedView = navigationTargetHandler.LoadContent(region, navigationContext); // Assert Assert.AreSame(view, returnedView); }
public void WhenNoCurrentMatchingViewExists_ThenReturnsNewlyCreatedInstanceWithServiceLocatorAddedToTheRegion() { // Arrange var serviceLocatorMock = new Mock<IServiceLocator>(); var region = new Region(); var view = new TestView(); serviceLocatorMock .Setup(sl => sl.GetInstance<object>(view.GetType().Name)) .Returns(view); var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative)); var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object); // Act var returnedView = navigationTargetHandler.LoadContent(region, navigationContext); // Assert Assert.AreSame(view, returnedView); Assert.IsTrue(region.Views.Contains(view)); }
public void ActivateNonAddedViewThrows() { IRegion region = new Region(); object nonAddedView = new object(); region.Activate(nonAddedView); }
public void RemoveInexistentViewThrows() { IRegion region = new Region(); object view = new object(); region.Remove(view); Assert.AreEqual(0, region.Views.Cast<object>().Count()); }
public void AddingNonDependencyObjectToRegionDoesNotThrow() { IRegion region = new Region(); object model = new object(); region.Add(model); Assert.AreEqual(1, region.Views.Cast<object>().Count()); }
public void CanRemoveContentFromRegion() { IRegion region = new Region(); object view = new object(); region.Add(view); region.Remove(view); Assert.AreEqual(0, region.Views.Cast<object>().Count()); }
public void CanAddContentToRegion() { IRegion region = new Region(); Assert.AreEqual(0, region.Views.Cast<object>().Count()); region.Add(new object()); Assert.AreEqual(1, region.Views.Cast<object>().Count()); }
public void AddNamedViewIsAlsoListedInViewsCollection() { IRegion region = new Region(); object myView = new object(); region.Add(myView, "MyView"); Assert.AreEqual(1, region.Views.Cast<object>().Count()); Assert.AreSame(myView, region.Views.Cast<object>().ElementAt(0)); }
public void CanAddAndRetrieveNamedViewInstance() { IRegion region = new Region(); object myView = new object(); region.Add(myView, "MyView"); object returnedView = region.GetView("MyView"); Assert.IsNotNull(returnedView); Assert.AreSame(returnedView, myView); }
public void RegionExposesCollectionOfContainedViews() { IRegion region = new Region(); object view = new object(); region.Add(view); var views = region.Views; Assert.IsNotNull(views); Assert.AreEqual(1, views.Cast<object>().Count()); Assert.AreSame(view, views.Cast<object>().ElementAt(0)); }
public void MissingCandidateViewCreatesViewInRegion() { this.ConfigureMockServiceLocator(); // We cannot access the UnityRegionNavigationContentLoader directly so we need to call its // GetCandidatesFromRegion method through a navigation request. IRegion testRegion = new Region(); Assert.AreEqual(0, testRegion.Views.Count()); Assert.AreEqual(0, testRegion.ActiveViews.Count()); testRegion.RequestNavigate("MockView1"); Assert.AreEqual(1, testRegion.Views.Count()); Assert.AreEqual(1, testRegion.ActiveViews.Count()); }
public void WhenCreatingANewContextForAUriWithAQuery_ThenNewContextInitializesPropertiesAndExtractsTheQuery() { var uri = new Uri("test?name=value", UriKind.Relative); var navigationJournalMock = new Mock<IRegionNavigationJournal>(); var navigationServiceMock = new Mock<IRegionNavigationService>(); IRegion region = new Region(); navigationServiceMock.SetupGet(n => n.Region).Returns(region); navigationServiceMock.SetupGet(x => x.Journal).Returns(navigationJournalMock.Object); var context = new NavigationContext(navigationServiceMock.Object, uri); Assert.AreSame(navigationServiceMock.Object, context.NavigationService); Assert.AreEqual(uri, context.Uri); Assert.AreEqual(1, context.Parameters.Count()); Assert.AreEqual("value", context.Parameters["name"]); }
public void ShouldFindCandidateViewWithFriendlyNameInRegion() { this.ConfigureMockServiceLocator(); // We cannot access the MefRegionNavigationContentLoader directly so we need to call its // GetCandidatesFromRegion method through a navigation request. IRegion testRegion = new Region(); MockView2 view = new MockView2(); testRegion.Add(view); testRegion.Deactivate(view); testRegion.RequestNavigate("SomeView"); Assert.IsTrue(testRegion.Views.Contains(view)); Assert.IsTrue(testRegion.Views.Count() == 1); Assert.IsTrue(testRegion.ActiveViews.Count() == 1); Assert.IsTrue(testRegion.ActiveViews.Contains(view)); }
public void WhenCreatingANewContextForAUriWithAQueryAndNavigationParameters_NoDuplicateParametersAreCreated() { var uri = new Uri("test?name=value", UriKind.Relative); var navParameters = new NavigationParameters(); navParameters.Add("name", "value"); var navigationJournalMock = new Mock<IRegionNavigationJournal>(); var navigationServiceMock = new Mock<IRegionNavigationService>(); IRegion region = new Region(); navigationServiceMock.SetupGet(n => n.Region).Returns(region); navigationServiceMock.SetupGet(x => x.Journal).Returns(navigationJournalMock.Object); var context = new NavigationContext(navigationServiceMock.Object, uri, navParameters); Assert.AreEqual(uri, context.Uri); Assert.AreEqual(1, context.Parameters.Count()); Assert.AreEqual("value", context.Parameters["name"]); }
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)); }
public void AddingSameViewTwiceThrows() { object view = new object(); IRegion region = new Region(); region.Add(view); try { region.Add(view); Assert.Fail(); } catch (InvalidOperationException ex) { Assert.AreEqual("View already exists in region.", ex.Message); } catch { Assert.Fail(); } }
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 ShouldFindCandidateViewInRegion() { IUnityContainer container = new UnityContainer(); container.RegisterType<object, MockView>("MockView"); this.ConfigureMockServiceLocator(container); // We cannot access the UnityRegionNavigationContentLoader directly so we need to call its // GetCandidatesFromRegion method through a navigation request. IRegion testRegion = new Region(); MockView view = new MockView(); testRegion.Add(view); testRegion.Deactivate(view); testRegion.RequestNavigate("MockView"); Assert.IsTrue(testRegion.Views.Contains(view)); Assert.IsTrue(testRegion.Views.Count() == 1); Assert.IsTrue(testRegion.ActiveViews.Count() == 1); Assert.IsTrue(testRegion.ActiveViews.Contains(view)); }
public void WhenNavigatingAndBindingContextImplementsIRegionAware_ThenNavigatedIsInvokesOnNavigation() { // Prepare var region = new Region(); var mockView = new Mock <View>(); var mockIRegionAwareBindingContext = new Mock <IRegionAware>(); mockIRegionAwareBindingContext .Setup(ina => ina.IsNavigationTarget(It.IsAny <INavigationContext>())) .Returns(true); mockView.Object.BindingContext = mockIRegionAwareBindingContext.Object; var view = mockView.Object; region.Add(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 RegionNavigationContentLoader(container); IRegionNavigationJournal journal = Mock.Of <IRegionNavigationJournal>(); var target = new RegionNavigationService(container, contentLoader, journal) { Region = region }; // Act target.RequestNavigate(navigationUri, nr => { }); // Verify mockIRegionAwareBindingContext.Verify(v => v.OnNavigatedTo(It.Is <INavigationContext>(nc => nc.Uri == navigationUri))); }
public void WhenNavigatingAndCurrentlyActiveViewImplementsINavigateWithVeto_ThenNavigationRequestQueriesForVeto() { // Prepare var region = new Region(); var viewMock = new Mock <View>(); viewMock .As <IConfirmRegionNavigationRequest>() .Setup(ina => ina.ConfirmNavigationRequest(It.IsAny <INavigationContext>(), It.IsAny <Action <bool> >())) .Verifiable(); var view = viewMock.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 viewMock.VerifyAll(); }
public void AddNamedViewWithNullOrEmptyStringNameThrows() { IRegion region = new Region(); region.Add(new object(), string.Empty); }
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(); }
public void WhenViewsWithSortHintsAdded_RegionSortsViews() { IRegion region = new Region(); object view1 = new ViewOrder1(); object view2 = new ViewOrder2(); object view3 = new ViewOrder3(); region.Add(view1); region.Add(view2); region.Add(view3); Assert.AreEqual(3, region.Views.Count()); Assert.AreSame(view2, region.Views.ElementAt(0)); Assert.AreSame(view3, region.Views.ElementAt(1)); Assert.AreSame(view1, region.Views.ElementAt(2)); }
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 ViewRemovedEventPassesTheViewRemovedInTheEventArgs() { object removedView = null; IRegion region = new Region(); region.Views.CollectionChanged += (sender, e) => { if (e.Action == NotifyCollectionChangedAction.Remove) removedView = e.OldItems[0]; }; object model = new object(); region.Add(model); Assert.IsNull(removedView); region.Remove(model); Assert.AreSame(model, removedView); }
public void ViewAddedEventPassesTheViewAddedInTheEventArgs() { object viewAdded = null; IRegion region = new Region(); region.Views.CollectionChanged += (sender, e) => { if (e.Action == NotifyCollectionChangedAction.Add) { viewAdded = e.NewItems[0]; } }; object model = new object(); Assert.IsNull(viewAdded); region.Add(model); Assert.IsNotNull(viewAdded); Assert.AreSame(model, viewAdded); }
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 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 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 GetViewReturnsNullWhenViewDoesNotExistInRegion() { IRegion region = new Region(); Assert.Null(region.GetView("InexistentView")); }