public void WhenExecutingTheReplyCommand_ThenNavigatesToComposeEmailViewWithId() { var email = new EmailDocument(); EmailDocument[] emails = new EmailDocument[] { email }; var emailServiceMock = new Mock<IEmailService>(); var asyncResultMock = new Mock<IAsyncResult>(); emailServiceMock .Setup(svc => svc.GetEmailDocumentsAsync()) .Returns(Task.FromResult(emails.AsEnumerable())); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); regionManagerMock.Setup(x => x.RequestNavigate(RegionNames.MainContentRegion, @"ComposeEmailView?ReplyTo=" + email.Id.ToString("N"))) .Verifiable(); var viewModel = new TestInboxViewModel(emailServiceMock.Object, regionManagerMock.Object, emails); Assert.IsFalse(viewModel.Messages.IsEmpty); viewModel.Messages.MoveCurrentToFirst(); viewModel.ReplyMessageCommand.Execute(null); regionManagerMock.VerifyAll(); }
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); NavigationContext context = new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("location", UriKind.Relative)); context.Parameters.Add("EmailId", email.Id); ((INavigationAware)viewModel).OnNavigatedTo(context); context = new NavigationContext(new Mock<IRegionNavigationService>().Object, new Uri("location", UriKind.Relative)); context.Parameters.Add("EmailId", new Guid()); bool canNavigate = ((INavigationAware)viewModel).IsNavigationTarget(context); Assert.IsFalse(canNavigate); }
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<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); regionManagerMock.Setup(x => x.RequestNavigate(RegionNames.MainContentRegion, @"ComposeEmailView?ReplyTo=" + email.Id.ToString("N"))) .Verifiable(); EmailDocument[] emails = new EmailDocument[]{ email }; var viewModel = new TestInboxViewModel(emailServiceMock.Object, regionManagerMock.Object, emails); Assert.IsFalse(viewModel.Messages.IsEmpty); viewModel.Messages.MoveCurrentToFirst(); viewModel.ReplyMessageCommand.Execute(null); regionManagerMock.VerifyAll(); }
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 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 WhenExecutingTheOpenCommand_ThenNavigatesToEmailView() { var email = new EmailDocument(); var emailServiceMock = new Mock<IEmailService>(); Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>(); regionManagerMock.Setup(x => x.RequestNavigate(RegionNames.MainContentRegion, new Uri(@"EmailView?EmailId=" + email.Id.ToString("N"), UriKind.Relative))).Verifiable(); var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object); viewModel.OpenMessageCommand.Execute(email); regionManagerMock.VerifyAll(); }
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 async Task<bool> SendEmailDocumentAsync(EmailDocument email) { await Task.Delay(500); return true; }
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext) { // todo: 09 - 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 = _emailService.GetEmailDocument(replyToId); if (replyToEmail != null) { emailDocument.To = replyToEmail.From; emailDocument.Subject = "RE: " + replyToEmail.Subject; emailDocument.Text = Environment.NewLine + replyToEmail.Text .Split(Environment.NewLine.ToCharArray()) .Select(l => l.Length > 0 ? ">> " + l : l) .Aggregate((l1, l2) => l1 + Environment.NewLine + l2); } } else { var to = parameters[ToParameterKey]; if (to != null) { emailDocument.To = to; } } EmailDocument = emailDocument; // todo: 10 - 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 is useful if you need to navigate 'back' at some point after this // view model closes. _navigationJournal = navigationContext.NavigationService.Journal; }
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext) { // todo: 09 - 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 dictionary // built using the NavigationParameters class. // // 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; object replyTo; if (parameters.TryGetValue(ReplyToParameterKey, out replyTo)) { Guid replyToId; if (replyTo is Guid) { replyToId = (Guid)replyTo; } else { replyToId = Guid.Parse(replyTo.ToString()); } 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 { object to; if (parameters.TryGetValue(ToParameterKey, out to)) { emailDocument.To = to.ToString(); } } this.EmailDocument = emailDocument; // todo: 10 - 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 is useful if you need to navigate 'back' at some point after this // view model closes. this.navigationJournal = navigationContext.NavigationService.Journal; }
public Task SendEmailDocumentAsync(EmailDocument email) { return Task.Delay(500); }
public TestInboxViewModel(IEmailService emailService, IRegionManager regionManager, EmailDocument[] emails) : base(emailService, regionManager) { var viewCollection = this.Messages as ListCollectionView; foreach (var email in emails) { viewCollection.AddNewItem(email); } viewCollection.MoveCurrentTo(null); }
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(); }
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)); }
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext) { // todo: 15 - Orient to the right context // // When this view model is navigated to, it gathers the // requested EmailId from the navigation context's parameters. // // It also captures the navigation Journal so it // can offer a 'go back' command. var emailId = GetRequestedEmailId(navigationContext); if (emailId.HasValue) { this.Email = this.emailService.GetEmailDocument(emailId.Value); } 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 using and handmade Uri. // // The EmailViewModel retrieves this context by implementing the INavigationAware // interface. // NavigationParameters parameters = new NavigationParameters(); parameters.Add(EmailIdKey, document.Id.ToString("N")); this.regionManager.RequestNavigate(RegionNames.MainContentRegion, new Uri(EmailViewKey + parameters, UriKind.Relative)); }
public Task SendEmailDocumentAsync(EmailDocument email) { return(Task.Delay(500)); }
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 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; }; }