public void WhenEmailsAreReturned_ThenViewModelIsPopulated() { var asyncResultMock = new Mock<IAsyncResult>(); var emailServiceMock = new Mock<IEmailService>(MockBehavior.Strict); AsyncCallback callback = null; emailServiceMock .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null)) .Callback<AsyncCallback, object>((ac, o) => callback = ac) .Returns(asyncResultMock.Object); var email = new EmailDocument { }; emailServiceMock .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object)) .Returns(new[] { email }); var viewModel = new InboxViewModel(emailServiceMock.Object, new Mock<IRegionManager>().Object); this.EnqueueConditional(() => callback != null); this.EnqueueCallback( () => { callback(asyncResultMock.Object); }); this.EnqueueCallback( () => { CollectionAssert.AreEqual(new[] { email }, viewModel.Messages.Cast<EmailDocument>().ToArray()); emailServiceMock.VerifyAll(); }); this.EnqueueTestComplete(); }
public IAsyncResult BeginSendEmailDocument(EmailDocument email, AsyncCallback callback, object userState) { var asyncResult = new AsyncResult<object>(callback, userState); ThreadPool.QueueUserWorkItem( o => { Thread.Sleep(500); asyncResult.SetComplete(null, false); }); return asyncResult; }
public void WhenNavigatedToWithAReplyToQueryParameter_ThenRepliesToTheAppropriateMessage() { var replyToEmail = new EmailDocument { From = "*****@*****.**", To = "", Subject = "", Text = "" }; var emailServiceMock = new Mock<IEmailService>(); emailServiceMock .Setup(svc => svc.GetEmailDocument(replyToEmail.Id)) .Returns(replyToEmail); var viewModel = new ComposeEmailViewModel(emailServiceMock.Object); var uriQuery = new UriQuery(); uriQuery.Add("ReplyTo", replyToEmail.Id.ToString("N")); ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("" + uriQuery.ToString(), UriKind.Relative))); Assert.AreEqual("*****@*****.**", viewModel.EmailDocument.To); }
public void WhenAskedCanNavigateForDifferentQuery_ThenReturnsFalse() { var email = new EmailDocument(); var emailServiceMock = new Mock<IEmailService>(); emailServiceMock .Setup(svc => svc.GetEmailDocument(email.Id)) .Returns(email) .Verifiable(); var viewModel = new EmailViewModel(emailServiceMock.Object); ((INavigationAware)viewModel).OnNavigatedTo(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("location?EmailId=" + email.Id.ToString("N"), UriKind.Relative))); bool canNavigate = ((INavigationAware)viewModel).IsNavigationTarget(new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("location?EmailId=" + Guid.NewGuid().ToString("N"), UriKind.Relative))); Assert.IsFalse(canNavigate); }
public void WhenNavigatedTo_ThenRequestsEmailFromService() { var email = new EmailDocument(); var emailServiceMock = new Mock<IEmailService>(); emailServiceMock .Setup(svc => svc.GetEmailDocument(email.Id)) .Returns(email) .Verifiable(); var viewModel = new EmailViewModel(emailServiceMock.Object); var notified = false; viewModel.PropertyChanged += (s, o) => notified = o.PropertyName == "Email"; ((INavigationAware)viewModel).OnNavigatedTo( new NavigationContext( new Mock<IRegionNavigationService>().Object, new Uri("location?EmailId=" + email.Id.ToString("N"), UriKind.Relative))); Assert.IsTrue(notified); emailServiceMock.VerifyAll(); }
public static Func<bool> SetupGetEmails(Mock<IEmailService> serviceMock, IEnumerable<EmailDocument> result) { var asyncResultMock = new Mock<IAsyncResult>(); AsyncCallback callback = null; serviceMock .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null)) .Callback<AsyncCallback, object>((ac, o) => callback = ac) .Returns(asyncResultMock.Object); var email = new EmailDocument { }; serviceMock .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object)) .Returns(result); return () => { if (callback == null) { return false; } callback(asyncResultMock.Object); return true; }; }
public void WhenExecutingTheOpenCommand_ThenNavigatesToEmailView() { var email = new EmailDocument(); var emailServiceMock = new Mock<IEmailService>(); Mock<IRegion> regionMock = new Mock<IRegion>(); regionMock .Setup(x => x.RequestNavigate(new Uri(@"EmailView?EmailId=" + email.Id.ToString("N"), UriKind.Relative), It.IsAny<Action<NavigationResult>>())) .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true))) .Verifiable(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true); regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object); var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object); viewModel.OpenMessageCommand.Execute(email); regionMock.VerifyAll(); }
public void WhenExecutingTheReplyCommand_ThenNavigatesToComposeEmailViewWithId() { var email = new EmailDocument(); var emailServiceMock = new Mock<IEmailService>(); AsyncCallback callback = null; var asyncResultMock = new Mock<IAsyncResult>(); emailServiceMock .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null)) .Callback<AsyncCallback, object>((ac, o) => callback = ac) .Returns(asyncResultMock.Object); emailServiceMock .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object)) .Returns(new[] { email }); Mock<IRegion> regionMock = new Mock<IRegion>(); regionMock .Setup(x => x.RequestNavigate(new Uri(@"ComposeEmailView?ReplyTo=" + email.Id.ToString("N"), UriKind.Relative), It.IsAny<Action<NavigationResult>>())) .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true))) .Verifiable(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true); regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object); var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object); EnqueueConditional(() => callback != null); EnqueueCallback( () => { callback(asyncResultMock.Object); }); EnqueueConditional( () => { return !viewModel.Messages.IsEmpty; }); EnqueueCallback( () => { viewModel.Messages.MoveCurrentToFirst(); viewModel.ReplyMessageCommand.Execute(null); regionMock.VerifyAll(); }); EnqueueTestComplete(); }
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext) { // todo: 08 - Email OnNavigatedTo : Accessing navigation context. // When this view model is navigated to it gains access to the // NavigationContext to determine if we are composing a new email // or replying to an existing one. // // The navigation context offers the context information through // the Parameters property that is a string/value dictionairy // built from the navigation Uri. // // In this example, we look for the 'ReplyTo' value to // determine if we are replying to an email and, if so, // retrieving it's relevant information from the email service // to pre-populate response values. // var emailDocument = new EmailDocument(); var parameters = navigationContext.Parameters; var replyTo = parameters[ReplyToParameterKey]; Guid replyToId; if (replyTo != null && Guid.TryParse(replyTo, out replyToId)) { var replyToEmail = this.emailService.GetEmailDocument(replyToId); if (replyToEmail != null) { emailDocument.To = replyToEmail.From; emailDocument.Subject = Resources.ResponseMessagePrefix + replyToEmail.Subject; emailDocument.Text = Environment.NewLine + replyToEmail.Text .Split(Environment.NewLine.ToCharArray()) .Select(l => l.Length > 0 ? Resources.ResponseLinePrefix + l : l) .Aggregate((l1, l2) => l1 + Environment.NewLine + l2); } } else { var to = parameters[ToParameterKey]; if (to != null) { emailDocument.To = to; } } this.EmailDocument = emailDocument; // todo: 09 - Email OnNaviatedTo : capture the navigation service journal // You can capture the navigation service or navigation service journal // to navigate the region you're placed in without having to expressly // know which region to navigate. this.navigationJournal = navigationContext.NavigationService.Journal; }
private void OpenMessage(EmailDocument document) { // todo: 04 - Open Email: Navigating to a view in a region with context // When navigating, you can also supply context so the target view or // viewmodel can orient their data to something appropriate. In this case, // we've chosen to pass the email id in a name/value pair as part of the Uri. // // The EmailViewModel retrieves this context by implementing the INavigationAware // interface. // var builder = new StringBuilder(); builder.Append(EmailViewKey); var query = new UriQuery(); query.Add(EmailIdKey, document.Id.ToString("N")); builder.Append(query); this.regionManager.RequestNavigate(RegionNames.MainContentRegion, new Uri(builder.ToString(), UriKind.Relative), nr => { }); }
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext) { // When we're navigated to see if there is an emailId // associated with the request. If so, retrieve the email document. var emailId = GetRequestedEmailId(navigationContext); if (emailId.HasValue) { this.Email = this.emailService.GetEmailDocument(emailId.Value); } this.navigationJournal = navigationContext.NavigationService.Journal; }