PageRenderTime 52ms CodeModel.GetById 14ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/V4/Quickstarts/View-Switching Navigation/Silverlight/View-Switching Navigation/ViewSwitchingNavigation.Email.Tests/InboxViewModelFixture.cs

#
C# | 227 lines | 167 code | 44 blank | 16 comment | 4 complexity | 3176856d3f0b650a1311cb528b1e90fe MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20using Microsoft.Practices.Prism.Regions;
 21using Microsoft.Silverlight.Testing;
 22using Microsoft.VisualStudio.TestTools.UnitTesting;
 23using Moq;
 24using ViewSwitchingNavigation.Email.Model;
 25using ViewSwitchingNavigation.Email.ViewModels;
 26using ViewSwitchingNavigation.Infrastructure;
 27
 28namespace ViewSwitchingNavigation.Email.Tests
 29{
 30    [TestClass]
 31    public class InboxViewModelFixture : SilverlightTest
 32    {
 33        [TestMethod]
 34        public void WhenCreated_ThenRequestsEmailsToService()
 35        {
 36            var emailServiceMock = new Mock<IEmailService>();
 37            var requested = false;
 38            emailServiceMock
 39                .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
 40                .Callback(() => requested = true);
 41
 42            var viewModel = new InboxViewModel(emailServiceMock.Object, new Mock<IRegionManager>().Object);
 43
 44            Assert.IsTrue(requested);
 45        }
 46
 47        [TestMethod]
 48        [Asynchronous]
 49        [Timeout(5000)]
 50        public void WhenEmailsAreReturned_ThenViewModelIsPopulated()
 51        {
 52            var asyncResultMock = new Mock<IAsyncResult>();
 53
 54            var emailServiceMock = new Mock<IEmailService>(MockBehavior.Strict);
 55            AsyncCallback callback = null;
 56            emailServiceMock
 57                .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
 58                .Callback<AsyncCallback, object>((ac, o) => callback = ac)
 59                .Returns(asyncResultMock.Object);
 60            var email = new EmailDocument { };
 61            emailServiceMock
 62                .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object))
 63                .Returns(new[] { email });
 64
 65
 66            var viewModel = new InboxViewModel(emailServiceMock.Object, new Mock<IRegionManager>().Object);
 67
 68            this.EnqueueConditional(() => callback != null);
 69
 70            this.EnqueueCallback(
 71                () =>
 72                {
 73                    callback(asyncResultMock.Object);
 74                });
 75
 76            this.EnqueueCallback(
 77                () =>
 78                {
 79                    CollectionAssert.AreEqual(new[] { email }, viewModel.Messages.Cast<EmailDocument>().ToArray());
 80                    emailServiceMock.VerifyAll();
 81                });
 82
 83            this.EnqueueTestComplete();
 84        }
 85
 86        [TestMethod]
 87        public void WhenEmailIsNotSelected_ThenCannotExecuteTheReplyCommand()
 88        {
 89        }
 90
 91        [TestMethod]
 92        public void WhenEmailIsSelected_ThenCanExecuteTheReplyCommand()
 93        {
 94        }
 95
 96        [TestMethod]
 97        public void WhenExecutingTheNewCommand_ThenNavigatesToComposeEmailView()
 98        {
 99            var emailServiceMock = new Mock<IEmailService>();
100
101
102            Mock<IRegion> regionMock = new Mock<IRegion>();
103            regionMock
104                .Setup(x => x.RequestNavigate(new Uri("ComposeEmailView", UriKind.Relative), It.IsAny<Action<NavigationResult>>()))
105                .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true)))
106                .Verifiable();
107
108            Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
109            regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true);
110            regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object);
111
112            var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object);
113
114            viewModel.ComposeMessageCommand.Execute(null);
115
116            regionMock.VerifyAll();
117        }
118
119        [TestMethod]
120        [Asynchronous]
121        [Timeout(5000)]
122        public void WhenExecutingTheReplyCommand_ThenNavigatesToComposeEmailViewWithId()
123        {
124            var email = new EmailDocument();
125
126            var emailServiceMock = new Mock<IEmailService>();
127            AsyncCallback callback = null;
128            var asyncResultMock = new Mock<IAsyncResult>();
129            emailServiceMock
130                .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
131                .Callback<AsyncCallback, object>((ac, o) => callback = ac)
132                .Returns(asyncResultMock.Object);
133            emailServiceMock
134                .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object))
135                .Returns(new[] { email });
136
137            Mock<IRegion> regionMock = new Mock<IRegion>();
138            regionMock
139                .Setup(x => x.RequestNavigate(new Uri(@"ComposeEmailView?ReplyTo=" + email.Id.ToString("N"), UriKind.Relative), It.IsAny<Action<NavigationResult>>()))
140                .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true)))
141                .Verifiable();
142
143            Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
144            regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true);
145            regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object);
146
147            var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object);
148
149            EnqueueConditional(() => callback != null);
150
151            EnqueueCallback(
152                () =>
153                {
154                    callback(asyncResultMock.Object);
155                });
156
157            EnqueueConditional(
158                () =>
159                {
160                    return !viewModel.Messages.IsEmpty;
161                });
162
163            EnqueueCallback(
164                () =>
165                {
166                    viewModel.Messages.MoveCurrentToFirst();
167
168                    viewModel.ReplyMessageCommand.Execute(null);
169
170                    regionMock.VerifyAll();
171                });
172
173            EnqueueTestComplete();
174        }
175
176
177        [TestMethod]
178        public void WhenExecutingTheOpenCommand_ThenNavigatesToEmailView()
179        {
180            var email = new EmailDocument();
181
182            var emailServiceMock = new Mock<IEmailService>();
183
184            Mock<IRegion> regionMock = new Mock<IRegion>();
185            regionMock
186                .Setup(x => x.RequestNavigate(new Uri(@"EmailView?EmailId=" + email.Id.ToString("N"), UriKind.Relative), It.IsAny<Action<NavigationResult>>()))
187                .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true)))
188                .Verifiable();
189
190            Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
191            regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true);
192            regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object);
193
194            var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object);
195
196            viewModel.OpenMessageCommand.Execute(email);
197
198            regionMock.VerifyAll();
199        }
200
201
202        public static Func<bool> SetupGetEmails(Mock<IEmailService> serviceMock, IEnumerable<EmailDocument> result)
203        {
204            var asyncResultMock = new Mock<IAsyncResult>();
205            AsyncCallback callback = null;
206            serviceMock
207                .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
208                .Callback<AsyncCallback, object>((ac, o) => callback = ac)
209                .Returns(asyncResultMock.Object);
210            var email = new EmailDocument { };
211            serviceMock
212                .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object))
213                .Returns(result);
214
215            return () =>
216                {
217                    if (callback == null)
218                    {
219                        return false;
220                    }
221
222                    callback(asyncResultMock.Object);
223                    return true;
224                };
225        }
226    }
227}