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();
        }
Exemplo n.º 4
0
        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;
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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();
        }
Exemplo n.º 11
0
 public async Task<bool> SendEmailDocumentAsync(EmailDocument email)
 {
     await Task.Delay(500);
     return true;
 }
Exemplo n.º 12
0
        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;
        }
Exemplo n.º 13
0
        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;
        }
Exemplo n.º 14
0
 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);
        }
Exemplo n.º 16
0
        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();
        }
Exemplo n.º 17
0
 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));
        }
Exemplo n.º 20
0
 public Task SendEmailDocumentAsync(EmailDocument email)
 {
     return(Task.Delay(500));
 }
Exemplo n.º 21
0
        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();
        }
Exemplo n.º 22
0
        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;
                };
        }