PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using Microsoft.Practices.Prism.Regions;
  21. using Microsoft.Silverlight.Testing;
  22. using Microsoft.VisualStudio.TestTools.UnitTesting;
  23. using Moq;
  24. using ViewSwitchingNavigation.Email.Model;
  25. using ViewSwitchingNavigation.Email.ViewModels;
  26. using ViewSwitchingNavigation.Infrastructure;
  27. namespace ViewSwitchingNavigation.Email.Tests
  28. {
  29. [TestClass]
  30. public class InboxViewModelFixture : SilverlightTest
  31. {
  32. [TestMethod]
  33. public void WhenCreated_ThenRequestsEmailsToService()
  34. {
  35. var emailServiceMock = new Mock<IEmailService>();
  36. var requested = false;
  37. emailServiceMock
  38. .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
  39. .Callback(() => requested = true);
  40. var viewModel = new InboxViewModel(emailServiceMock.Object, new Mock<IRegionManager>().Object);
  41. Assert.IsTrue(requested);
  42. }
  43. [TestMethod]
  44. [Asynchronous]
  45. [Timeout(5000)]
  46. public void WhenEmailsAreReturned_ThenViewModelIsPopulated()
  47. {
  48. var asyncResultMock = new Mock<IAsyncResult>();
  49. var emailServiceMock = new Mock<IEmailService>(MockBehavior.Strict);
  50. AsyncCallback callback = null;
  51. emailServiceMock
  52. .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
  53. .Callback<AsyncCallback, object>((ac, o) => callback = ac)
  54. .Returns(asyncResultMock.Object);
  55. var email = new EmailDocument { };
  56. emailServiceMock
  57. .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object))
  58. .Returns(new[] { email });
  59. var viewModel = new InboxViewModel(emailServiceMock.Object, new Mock<IRegionManager>().Object);
  60. this.EnqueueConditional(() => callback != null);
  61. this.EnqueueCallback(
  62. () =>
  63. {
  64. callback(asyncResultMock.Object);
  65. });
  66. this.EnqueueCallback(
  67. () =>
  68. {
  69. CollectionAssert.AreEqual(new[] { email }, viewModel.Messages.Cast<EmailDocument>().ToArray());
  70. emailServiceMock.VerifyAll();
  71. });
  72. this.EnqueueTestComplete();
  73. }
  74. [TestMethod]
  75. public void WhenEmailIsNotSelected_ThenCannotExecuteTheReplyCommand()
  76. {
  77. }
  78. [TestMethod]
  79. public void WhenEmailIsSelected_ThenCanExecuteTheReplyCommand()
  80. {
  81. }
  82. [TestMethod]
  83. public void WhenExecutingTheNewCommand_ThenNavigatesToComposeEmailView()
  84. {
  85. var emailServiceMock = new Mock<IEmailService>();
  86. Mock<IRegion> regionMock = new Mock<IRegion>();
  87. regionMock
  88. .Setup(x => x.RequestNavigate(new Uri("ComposeEmailView", UriKind.Relative), It.IsAny<Action<NavigationResult>>()))
  89. .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true)))
  90. .Verifiable();
  91. Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
  92. regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true);
  93. regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object);
  94. var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object);
  95. viewModel.ComposeMessageCommand.Execute(null);
  96. regionMock.VerifyAll();
  97. }
  98. [TestMethod]
  99. [Asynchronous]
  100. [Timeout(5000)]
  101. public void WhenExecutingTheReplyCommand_ThenNavigatesToComposeEmailViewWithId()
  102. {
  103. var email = new EmailDocument();
  104. var emailServiceMock = new Mock<IEmailService>();
  105. AsyncCallback callback = null;
  106. var asyncResultMock = new Mock<IAsyncResult>();
  107. emailServiceMock
  108. .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
  109. .Callback<AsyncCallback, object>((ac, o) => callback = ac)
  110. .Returns(asyncResultMock.Object);
  111. emailServiceMock
  112. .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object))
  113. .Returns(new[] { email });
  114. Mock<IRegion> regionMock = new Mock<IRegion>();
  115. regionMock
  116. .Setup(x => x.RequestNavigate(new Uri(@"ComposeEmailView?ReplyTo=" + email.Id.ToString("N"), UriKind.Relative), It.IsAny<Action<NavigationResult>>()))
  117. .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true)))
  118. .Verifiable();
  119. Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
  120. regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true);
  121. regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object);
  122. var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object);
  123. EnqueueConditional(() => callback != null);
  124. EnqueueCallback(
  125. () =>
  126. {
  127. callback(asyncResultMock.Object);
  128. });
  129. EnqueueConditional(
  130. () =>
  131. {
  132. return !viewModel.Messages.IsEmpty;
  133. });
  134. EnqueueCallback(
  135. () =>
  136. {
  137. viewModel.Messages.MoveCurrentToFirst();
  138. viewModel.ReplyMessageCommand.Execute(null);
  139. regionMock.VerifyAll();
  140. });
  141. EnqueueTestComplete();
  142. }
  143. [TestMethod]
  144. public void WhenExecutingTheOpenCommand_ThenNavigatesToEmailView()
  145. {
  146. var email = new EmailDocument();
  147. var emailServiceMock = new Mock<IEmailService>();
  148. Mock<IRegion> regionMock = new Mock<IRegion>();
  149. regionMock
  150. .Setup(x => x.RequestNavigate(new Uri(@"EmailView?EmailId=" + email.Id.ToString("N"), UriKind.Relative), It.IsAny<Action<NavigationResult>>()))
  151. .Callback<Uri, Action<NavigationResult>>((s, c) => c(new NavigationResult(null, true)))
  152. .Verifiable();
  153. Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
  154. regionManagerMock.Setup(x => x.Regions.ContainsRegionWithName(RegionNames.MainContentRegion)).Returns(true);
  155. regionManagerMock.Setup(x => x.Regions[RegionNames.MainContentRegion]).Returns(regionMock.Object);
  156. var viewModel = new InboxViewModel(emailServiceMock.Object, regionManagerMock.Object);
  157. viewModel.OpenMessageCommand.Execute(email);
  158. regionMock.VerifyAll();
  159. }
  160. public static Func<bool> SetupGetEmails(Mock<IEmailService> serviceMock, IEnumerable<EmailDocument> result)
  161. {
  162. var asyncResultMock = new Mock<IAsyncResult>();
  163. AsyncCallback callback = null;
  164. serviceMock
  165. .Setup(svc => svc.BeginGetEmailDocuments(It.IsAny<AsyncCallback>(), null))
  166. .Callback<AsyncCallback, object>((ac, o) => callback = ac)
  167. .Returns(asyncResultMock.Object);
  168. var email = new EmailDocument { };
  169. serviceMock
  170. .Setup(svc => svc.EndGetEmailDocuments(asyncResultMock.Object))
  171. .Returns(result);
  172. return () =>
  173. {
  174. if (callback == null)
  175. {
  176. return false;
  177. }
  178. callback(asyncResultMock.Object);
  179. return true;
  180. };
  181. }
  182. }
  183. }