Пример #1
0
        public async Task NavigateFromCurrentViewModel_Calls_VieModel_OnNavigatedFrom()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());

                bool viewModelNavigatedFromCalled = false;
                var viewModel = new MockPageViewModel();
                viewModel.OnNavigatedFromCommand = (frameState, suspending) =>
                {
                    Assert.IsFalse(suspending);
                    viewModelNavigatedFromCalled = true;
                };

                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();

                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                // Initial navigatio
                navigationService.Navigate("page0", 0);

                // Set up the frame's content with a Page
                var view         = new MockPage();
                view.DataContext = viewModel;
                frame.Content    = view;

                // Navigate to fire the NavigatedToCurrentViewModel
                navigationService.Navigate("page1", 1);

                Assert.IsTrue(viewModelNavigatedFromCalled);
            });
        }
Пример #2
0
        public async Task Navigate_Raises_NavigationStateChangedEvent()
        {
            var tcs             = new TaskCompletionSource <bool>();
            var eventAggregator = new EventAggregator();

            eventAggregator.GetEvent <NavigationStateChangedEvent>().Subscribe((args) => tcs.SetResult(args.StateChange == StateChangeType.Forward));

            await DispatcherHelper.ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame(), eventAggregator);
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                navigationService.Navigate("Mock", 1);
            });

            await Task.WhenAny(tcs.Task, Task.Delay(200));

            if (tcs.Task.IsCompleted)
            {
                Assert.IsTrue(tcs.Task.Result);
            }
            else
            {
                Assert.Fail("NavigationStateChangedEvent event wasn't raised within 200 ms.");
            }
        }
Пример #3
0
        public async Task RestoreSavedNavigation_ClearsOldForwardNavigation()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                var frameSessionState   = new Dictionary <string, object>();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => frameSessionState;
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPageWithViewModel), sessionStateService);

                navigationService.Navigate("Page1", 1);
                Assert.AreEqual(1, frameSessionState.Count, "VM 1 state only");

                navigationService.Navigate("Page2", 2);
                Assert.AreEqual(2, frameSessionState.Count, "VM 1 and 2");

                navigationService.Navigate("Page3", 3);
                Assert.AreEqual(3, frameSessionState.Count, "VM 1, 2, and 3");

                navigationService.GoBack();
                Assert.AreEqual(2, ((Dictionary <string, object>)frameSessionState["ViewModel-2"]).Count);
                Assert.AreEqual(3, frameSessionState.Count, "VM 1, 2, and 3");

                navigationService.Navigate("Page4", 4);
                Assert.AreEqual(0, ((Dictionary <string, object>)frameSessionState["ViewModel-2"]).Count);
                Assert.AreEqual(3, frameSessionState.Count, "VM 1, 2, and 4");
            });
        }
Пример #4
0
        public async Task GoBack_When_CanGoBack()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                bool resultFirstNavigation = navigationService.Navigate("MockPage", 1);

                Assert.IsInstanceOfType(frame.Content, typeof(MockPage));
                Assert.AreEqual(1, ((MockPage)frame.Content).PageParameter);
                Assert.IsFalse(navigationService.CanGoBack());

                bool resultSecondNavigation = navigationService.Navigate("Mock", 2);

                Assert.IsInstanceOfType(frame.Content, typeof(MockPage));
                Assert.AreEqual(2, ((MockPage)frame.Content).PageParameter);
                Assert.IsTrue(navigationService.CanGoBack());

                navigationService.GoBack();

                Assert.IsInstanceOfType(frame.Content, typeof(MockPage));
                Assert.AreEqual(1, ((MockPage)frame.Content).PageParameter);
                Assert.IsFalse(navigationService.CanGoBack());
            });
        }
Пример #5
0
        public async Task NavigateToCurrentViewModel_Calls_VieModel_OnNavigatedTo()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());

                bool viewModelNavigatedToCalled = false;
                var viewModel = new MockPageViewModel();
                viewModel.OnNavigatedFromCommand = (a, b) => Assert.IsTrue(true);
                viewModel.OnNavigatedToCommand   = (parameter, navigationMode, frameState) =>
                {
                    Assert.AreEqual(NavigationMode.New, navigationMode);
                    viewModelNavigatedToCalled = true;
                };

                // Set up the viewModel to the Page we navigated
                frame.Navigated += (sender, e) =>
                {
                    var view         = frame.Content as FrameworkElement;
                    view.DataContext = viewModel;
                };

                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();

                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                navigationService.Navigate("Mock", 1);

                Assert.IsTrue(viewModelNavigatedToCalled);
            });
        }
        public async Task Remove_All_Back_Button_Collapsed()
        {
            await DispatcherHelper.ExecuteOnUIThread(() =>
            {
                var eventAggregator     = new EventAggregator();
                var frame               = new FrameFacadeAdapter(new Frame(), eventAggregator);
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService    = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);
                var deviceGestureService = new DeviceGestureService(eventAggregator);
                deviceGestureService.UseTitleBarBackButton = true;
                var navigationManager = SystemNavigationManager.GetForCurrentView();

                // Reset back button visibility before running, can't do this in TestInitialize because CoreWindow sometimes isn't ready
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

                Assert.AreEqual(AppViewBackButtonVisibility.Collapsed, navigationManager.AppViewBackButtonVisibility);

                navigationService.Navigate("Mock", 1);

                Assert.AreEqual(AppViewBackButtonVisibility.Collapsed, navigationManager.AppViewBackButtonVisibility);

                navigationService.Navigate("Mock", 2);

                Assert.AreEqual(AppViewBackButtonVisibility.Visible, navigationManager.AppViewBackButtonVisibility);

                navigationService.RemoveAllPages();

                Assert.AreEqual(AppViewBackButtonVisibility.Collapsed, navigationManager.AppViewBackButtonVisibility);
            });
        }
        public async Task Navigate_To_Page_With_UseTitleBarBackButton_False_Does_Not_Change_Back_Button_Visibility()
        {
            await DispatcherHelper.ExecuteOnUIThread(() =>
            {
                var eventAggregator     = new EventAggregator();
                var frame               = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService    = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);
                var deviceGestureService = new DeviceGestureService(eventAggregator);
                deviceGestureService.UseTitleBarBackButton = true;
                var navigationManager = SystemNavigationManager.GetForCurrentView();

                // Reset back button visibility before running, can't do this in TestInitialize because CoreWindow sometimes isn't ready
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

                // If it's collapsed and we navigate, it stays collapsed
                Assert.AreEqual(AppViewBackButtonVisibility.Collapsed, navigationManager.AppViewBackButtonVisibility);
                navigationService.Navigate("Mock", 1);
                Assert.AreEqual(AppViewBackButtonVisibility.Collapsed, navigationManager.AppViewBackButtonVisibility);

                // If it's visible and our back stack is emptied, it stays visible
                navigationManager.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
                navigationService.ClearHistory();
                Assert.AreEqual(AppViewBackButtonVisibility.Visible, navigationManager.AppViewBackButtonVisibility);
            });
        }
Пример #8
0
        public async Task GetSignedInUserAsync_Calls_VerifyActiveSessionAsync()
        {
            bool verifyActiveSessionCalled = false;
            var  sessionStateService       = new MockSessionStateService();
            var  identityService           = new MockIdentityService()
            {
                LogOnAsyncDelegate = (userId, password) => Task.FromResult(new LogOnResult {
                    UserInfo = new UserInfo {
                        UserName = userId
                    }
                }),
                VerifyActiveSessionDelegate = (userName) =>
                {
                    verifyActiveSessionCalled = true;
                    return(Task.FromResult(true));
                }
            };

            var target = new AccountService(identityService, sessionStateService, null);
            await target.SignInUserAsync("TestUserName", "TestPassword", false);

            var user = await target.VerifyUserAuthenticationAsync();

            Assert.IsTrue(verifyActiveSessionCalled);
            Assert.IsNotNull(user);
            Assert.IsTrue(user.UserName == "TestUserName");
        }
Пример #9
0
        public async Task GetSignedInUserAsync_SignsInUsingCredentialStore_IfNoActiveSession()
        {
            var sessionStateService = new MockSessionStateService();
            var identityService     = new MockIdentityService()
            {
                LogOnAsyncDelegate = (userId, password) => Task.FromResult(new LogOnResult {
                    UserInfo = new UserInfo {
                        UserName = userId
                    }
                }),
                VerifyActiveSessionDelegate = (userName) => Task.FromResult(false)
            };
            var credentialStore = new MockCredentialStore()
            {
                GetSavedCredentialsDelegate = (s) => new PasswordCredential(AccountService.PasswordVaultResourceName, "TestUserName", "TestPassword"),
                SaveCredentialsDelegate     = (a, b, c) => Task.Delay(0)
            };

            var target = new AccountService(identityService, sessionStateService, credentialStore);
            await target.SignInUserAsync("TestUserName", "TestPassword", true);

            var user = await target.VerifyUserAuthenticationAsync();

            Assert.IsNotNull(user);
            Assert.IsTrue(user.UserName == "TestUserName");
        }
Пример #10
0
        public async Task CheckIfUserSignedIn_ReturnsUserInfo_IfSessionIsStillInactiveButHasSavedCredentials()
        {
            var sessionStateService = new MockSessionStateService();
            var identityService     = new MockIdentityService();

            identityService.VerifyActiveSessionDelegate = (userName) => Task.FromResult(false);
            identityService.LogOnAsyncDelegate          =
                (userName, password) =>
            {
                Assert.AreEqual("TestUserName", userName);
                Assert.AreEqual("TestPassword", password);
                return(Task.FromResult(new LogOnResult()
                {
                    UserInfo = new UserInfo()
                    {
                        UserName = "******"
                    }
                }));
            };
            var credentialStore = new MockCredentialStore();

            credentialStore.GetSavedCredentialsDelegate = s => new PasswordCredential(AccountService.PasswordVaultResourceName, "TestUserName", "TestPassword");
            var target = new AccountService(identityService, sessionStateService, credentialStore);

            var userInfo = await target.VerifyUserAuthenticationAsync();

            Assert.IsNotNull(userInfo);
            Assert.AreEqual("ReturnedUserName", userInfo.UserName);
        }
Пример #11
0
        public async Task SignOut_RaisesUserChangedEvent()
        {
            bool userChangedRaised   = false;
            var  sessionStateService = new MockSessionStateService();
            var  credentialStore     = new MockCredentialStore
            {
                GetSavedCredentialsDelegate    = s => null,
                RemoveSavedCredentialsDelegate = s => Task.Delay(0)
            };

            var target = new AccountService(null, sessionStateService, credentialStore);

            target.UserChanged += (sender, args) =>
            {
                userChangedRaised = true;
                Assert.IsNull(args.NewUserInfo);
            };

            target.SignOut();

            Assert.IsTrue(userChangedRaised);

            var signedInUser = await target.VerifyUserAuthenticationAsync();

            Assert.IsNull(signedInUser);
        }
Пример #12
0
        public async Task SuccessfullSignIn_RaisesUserChangedEvent()
        {
            bool userChangedFired = false;

            var identityService     = new MockIdentityService();
            var sessionStateService = new MockSessionStateService();

            identityService.LogOnAsyncDelegate = (userId, password) =>
            {
                return(Task.FromResult(new LogOnResult {
                    UserInfo = new UserInfo {
                        UserName = userId
                    }
                }));
            };

            var target = new AccountService(identityService, sessionStateService, null);

            target.UserChanged += (sender, userInfo) => { userChangedFired = true; };

            var retVal = await target.SignInUserAsync("TestUserName", "TestPassword", false);

            Assert.IsTrue(retVal);
            Assert.IsTrue(userChangedFired);
        }
Пример #13
0
        public async Task CheckIfUserSignedIn_ReturnsUserInfo_IfSessionIsStillLive()
        {
            var sessionStateService = new MockSessionStateService();
            var identityService     = new MockIdentityService();

            identityService.VerifyActiveSessionDelegate = (userName) => Task.FromResult(true);
            identityService.LogOnAsyncDelegate          = (userName, password) =>
            {
                return(Task.FromResult(new LogOnResult()
                {
                    UserInfo = new UserInfo()
                    {
                        UserName = "******"
                    }
                }));
            };

            var  target       = new AccountService(identityService, sessionStateService, null);
            bool userSignedIn = await target.SignInUserAsync("TestUsername", "password", false);

            Assert.IsTrue(userSignedIn);

            var userInfo = await target.VerifyUserAuthenticationAsync();

            Assert.IsNotNull(userInfo);
        }
Пример #14
0
        public async Task CheckIfUserSignedIn_ReturnsNull_IfSessionIsStillInactiveAndNoSavedCredentials()
        {
            var identityService = new MockIdentityService();

            identityService.VerifyActiveSessionDelegate = (userName) => Task.FromResult(false);
            var credentialStore = new MockCredentialStore();

            credentialStore.GetSavedCredentialsDelegate = s => null;
            var sessionStateService = new MockSessionStateService();
            var target = new AccountService(identityService, sessionStateService, credentialStore);

            var userInfo = await target.VerifyUserAuthenticationAsync();

            Assert.IsNull(userInfo);
        }
Пример #15
0
        public async Task Navigate_To_Invalid_Page()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();

                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(string), sessionStateService);

                bool result = navigationService.Navigate("Mock", 1);

                Assert.IsFalse(result);
                Assert.IsNull(frame.Content);
            });
        }
Пример #16
0
        public async Task PageTokenThatCannotBeResolved_ThrowsMeaningfulException()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                var frameSessionState   = new Dictionary <string, object>();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => frameSessionState;

                Func <string, Type> unresolvablePageTokenReturnsNull = pageToken => null;
                var navigationService = new FrameNavigationService(frame, unresolvablePageTokenReturnsNull, sessionStateService);

                Assert.ThrowsException <ArgumentException>(
                    () => navigationService.Navigate("anything", 1)
                    );
            });
        }
Пример #17
0
        public async Task Navigate_To_Valid_Page()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                bool result = navigationService.Navigate("Mock", 1);

                Assert.IsTrue(result);
                Assert.IsNotNull(frame.Content);
                Assert.IsInstanceOfType(frame.Content, typeof(MockPage));
                Assert.AreEqual(1, ((MockPage)frame.Content).PageParameter);
            });
        }
        public async Task Navigate_To_Page_With_Null_Aggregator_Still_Works()
        {
            await DispatcherHelper.ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);
                var navigationManager = SystemNavigationManager.GetForCurrentView();

                // Reset back button visibility before running, can't do this in TestInitialize because CoreWindow sometimes isn't ready
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

                bool result = navigationService.Navigate("Mock", 1);

                Assert.IsTrue(result);
            });
        }
Пример #19
0
        public async Task FailedSignIn_DoesNotRaiseUserChangedEvent()
        {
            bool userChangedFired = false;

            var identityService     = new MockIdentityService();
            var sessionStateService = new MockSessionStateService();

            identityService.LogOnAsyncDelegate = (userId, password) =>
            {
                throw new Exception();
            };

            var target = new AccountService(identityService, sessionStateService, null);

            target.UserChanged += (sender, userInfo) => { userChangedFired = true; };

            var retVal = await target.SignInUserAsync("TestUserName", "TestPassword", false);

            Assert.IsFalse(retVal);
            Assert.IsFalse(userChangedFired);
        }
Пример #20
0
        public async Task Resuming_Calls_ViewModel_OnNavigatedTo()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                navigationService.Navigate("Mock", 1);

                var viewModel = new MockPageViewModel();
                viewModel.OnNavigatedToCommand = (navigationParameter, navigationMode, frameState) =>
                {
                    Assert.AreEqual(NavigationMode.Refresh, navigationMode);
                };

                var page         = (MockPage)frame.Content;
                page.DataContext = viewModel;

                navigationService.RestoreSavedNavigation();
            });
        }
Пример #21
0
        public async Task Suspending_Calls_VieModel_OnNavigatedFrom()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => new Dictionary <string, object>();
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPage), sessionStateService);

                navigationService.Navigate("Mock", 1);

                var viewModel = new MockPageViewModel();
                viewModel.OnNavigatedFromCommand = (frameState, suspending) =>
                {
                    Assert.IsTrue(suspending);
                };

                var page         = (MockPage)frame.Content;
                page.DataContext = viewModel;

                navigationService.Suspending();
            });
        }
Пример #22
0
        public async Task ViewModelOnNavigatedFromCalled_WithItsOwnStateDictionary()
        {
            await ExecuteOnUIThread(() =>
            {
                var frame = new FrameFacadeAdapter(new Frame());
                var sessionStateService = new MockSessionStateService();
                var frameSessionState   = new Dictionary <string, object>();
                sessionStateService.GetSessionStateForFrameDelegate = (currentFrame) => frameSessionState;
                var navigationService = new FrameNavigationService(frame, (pageToken) => typeof(MockPageWithViewModel), sessionStateService);

                navigationService.Navigate("Page1", 1);
                Assert.AreEqual(1, frameSessionState.Count, "VM 1 state only");

                navigationService.Navigate("Page2", 2);
                Assert.AreEqual(2, frameSessionState.Count, "VM 1 and 2");

                navigationService.Navigate("Page1", 1);
                Assert.AreEqual(3, frameSessionState.Count, "VM 1, 2, and 1 again.");

                navigationService.Navigate("Page3", 3);
                Assert.AreEqual(4, frameSessionState.Count, "VM 1, 2, 1, and 3");
            });
        }
        public void ShoppingCartMerged_WhenAnonymousUserLogsIn()
        {
            bool mergeShoppingCartsCalled  = false;
            bool alertMessageServiceCalled = false;
            var  anonymousCartItems        = new List <ShoppingCartItem>
            {
                new ShoppingCartItem
                {
                    Quantity = 1, Product = new Product {
                        ProductNumber = "123"
                    }
                }
            };
            var testUserCartItems = new List <ShoppingCartItem>
            {
                new ShoppingCartItem
                {
                    Quantity = 2, Product = new Product {
                        ProductNumber = "123"
                    }
                }
            };

            var shoppingCartService = new MockShoppingCartService()
            {
                GetShoppingCartAsyncDelegate = s =>
                {
                    switch (s)
                    {
                    case "AnonymousId":
                        return(Task.FromResult(new ShoppingCart(anonymousCartItems)));

                    default:
                        return(Task.FromResult(new ShoppingCart(testUserCartItems)));
                    }
                },
                MergeShoppingCartsAsyncDelegate = (s, s1) =>
                {
                    mergeShoppingCartsCalled = true;
                    Assert.AreEqual("AnonymousId", s);
                    Assert.AreEqual("TestUserName", s1);
                    return(Task.FromResult(true));
                }
            };
            var accountService           = new MockAccountService();
            var shoppingCartUpdatedEvent = new MockShoppingCartUpdatedEvent
            {
                PublishDelegate = () => { }
            };

            var eventAggregator = new MockEventAggregator()
            {
                GetEventDelegate = (a) => shoppingCartUpdatedEvent
            };
            var sessionStateService = new MockSessionStateService();

            sessionStateService.SessionState[ShoppingCartRepository.ShoppingCartIdKey] = "AnonymousId";

            var target = new ShoppingCartRepository(shoppingCartService, accountService, eventAggregator, sessionStateService);

            accountService.RaiseUserChanged(new UserInfo {
                UserName = "******"
            }, null);

            Assert.IsTrue(mergeShoppingCartsCalled);
        }