PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism.Tests/Regions/LocatorNavigationTargetHandlerFixture.cs

#
C# | 402 lines | 215 code | 143 blank | 44 comment | 0 complexity | 381f760ee3b9697b36f3d842e345696c 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.Windows;
  19. using Microsoft.Practices.Prism.Regions;
  20. using Microsoft.Practices.Prism.TestSupport;
  21. using Microsoft.Practices.ServiceLocation;
  22. using Microsoft.VisualStudio.TestTools.UnitTesting;
  23. using Moq;
  24. namespace Microsoft.Practices.Prism.Tests.Regions
  25. {
  26. [TestClass]
  27. public class LocatorNavigationTargetHandlerFixture
  28. {
  29. [TestMethod]
  30. public void WhenViewExistsAndDoesNotImplementINavigationAware_ThenReturnsView()
  31. {
  32. // Arrange
  33. var serviceLocatorMock = new Mock<IServiceLocator>();
  34. var region = new Region();
  35. var view = new TestView();
  36. region.Add(view);
  37. var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative));
  38. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  39. // Act
  40. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  41. // Assert
  42. Assert.AreSame(view, returnedView);
  43. }
  44. [TestMethod]
  45. public void WhenRegionHasMultipleViews_ThenViewsWithMatchingTypeNameAreConsidered()
  46. {
  47. // Arrange
  48. var serviceLocatorMock = new Mock<IServiceLocator>();
  49. var region = new Region();
  50. var view1 = new TestView();
  51. var view2 = "view";
  52. region.Add(view1);
  53. region.Add(view2);
  54. var navigationContext = new NavigationContext(null, new Uri(view2.GetType().Name, UriKind.Relative));
  55. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  56. // Act
  57. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  58. // Assert
  59. Assert.AreSame(view2, returnedView);
  60. }
  61. [TestMethod]
  62. public void WhenRegionHasMultipleViews_ThenViewsWithMatchingFullTypeNameAreConsidered()
  63. {
  64. // Arrange
  65. var serviceLocatorMock = new Mock<IServiceLocator>();
  66. var region = new Region();
  67. var view1 = new TestView();
  68. var view2 = "view";
  69. region.Add(view1);
  70. region.Add(view2);
  71. var navigationContext = new NavigationContext(null, new Uri(view2.GetType().FullName, UriKind.Relative));
  72. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  73. // Act
  74. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  75. // Assert
  76. Assert.AreSame(view2, returnedView);
  77. }
  78. [TestMethod]
  79. public void WhenViewExistsAndImplementsINavigationAware_ThenViewIsQueriedForNavigationAndIsReturnedIfAcceptsIt()
  80. {
  81. // Arrange
  82. var serviceLocatorMock = new Mock<IServiceLocator>();
  83. var region = new Region();
  84. var viewMock = new Mock<INavigationAware>();
  85. viewMock
  86. .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
  87. .Returns(true)
  88. .Verifiable();
  89. region.Add(viewMock.Object);
  90. var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
  91. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  92. // Act
  93. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  94. // Assert
  95. Assert.AreSame(viewMock.Object, returnedView);
  96. viewMock.VerifyAll();
  97. }
  98. [TestMethod]
  99. public void WhenViewExistsAndHasDataContextThatImplementsINavigationAware_ThenDataContextIsQueriedForNavigationAndIsReturnedIfAcceptsIt()
  100. {
  101. // Arrange
  102. var serviceLocatorMock = new Mock<IServiceLocator>();
  103. var region = new Region();
  104. var dataContextMock = new Mock<INavigationAware>();
  105. dataContextMock
  106. .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
  107. .Returns(true)
  108. .Verifiable();
  109. var viewMock = new Mock<FrameworkElement>();
  110. viewMock.Object.DataContext = dataContextMock.Object;
  111. region.Add(viewMock.Object);
  112. var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
  113. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  114. // Act
  115. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  116. // Assert
  117. Assert.AreSame(viewMock.Object, returnedView);
  118. dataContextMock.VerifyAll();
  119. }
  120. [TestMethod]
  121. public void WhenNoCurrentMatchingViewExists_ThenReturnsNewlyCreatedInstanceWithServiceLocatorAddedToTheRegion()
  122. {
  123. // Arrange
  124. var serviceLocatorMock = new Mock<IServiceLocator>();
  125. var region = new Region();
  126. var view = new TestView();
  127. serviceLocatorMock
  128. .Setup(sl => sl.GetInstance<object>(view.GetType().Name))
  129. .Returns(view);
  130. var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative));
  131. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  132. // Act
  133. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  134. // Assert
  135. Assert.AreSame(view, returnedView);
  136. Assert.IsTrue(region.Views.Contains(view));
  137. }
  138. [TestMethod]
  139. public void WhenViewExistsAndImplementsINavigationAware_ThenViewIsQueriedForNavigationAndNewInstanceIsCreatedIfItRejectsIt()
  140. {
  141. // Arrange
  142. var serviceLocatorMock = new Mock<IServiceLocator>();
  143. var region = new Region();
  144. var viewMock = new Mock<INavigationAware>();
  145. viewMock
  146. .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
  147. .Returns(false)
  148. .Verifiable();
  149. region.Add(viewMock.Object);
  150. var newView = new TestView();
  151. serviceLocatorMock
  152. .Setup(sl => sl.GetInstance<object>(viewMock.Object.GetType().Name))
  153. .Returns(newView);
  154. var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
  155. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  156. // Act
  157. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  158. // Assert
  159. Assert.AreSame(newView, returnedView);
  160. Assert.IsTrue(region.Views.Contains(newView));
  161. viewMock.VerifyAll();
  162. }
  163. [TestMethod]
  164. public void WhenViewExistsAndHasDataContextThatImplementsINavigationAware_ThenDataContextIsQueriedForNavigationAndNewInstanceIsCreatedIfItRejectsIt()
  165. {
  166. // Arrange
  167. var serviceLocatorMock = new Mock<IServiceLocator>();
  168. var region = new Region();
  169. var dataContextMock = new Mock<INavigationAware>();
  170. dataContextMock
  171. .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
  172. .Returns(false)
  173. .Verifiable();
  174. var viewMock = new Mock<FrameworkElement>();
  175. viewMock.Object.DataContext = dataContextMock.Object;
  176. region.Add(viewMock.Object);
  177. var newView = new TestView();
  178. serviceLocatorMock
  179. .Setup(sl => sl.GetInstance<object>(viewMock.Object.GetType().Name))
  180. .Returns(newView);
  181. var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
  182. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  183. // Act
  184. var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  185. // Assert
  186. Assert.AreSame(newView, returnedView);
  187. Assert.IsTrue(region.Views.Contains(newView));
  188. dataContextMock.VerifyAll();
  189. }
  190. [TestMethod]
  191. public void WhenViewCannotBeCreated_ThenThrowsAnException()
  192. {
  193. var serviceLocatorMock = new Mock<IServiceLocator>();
  194. serviceLocatorMock
  195. .Setup(sl => sl.GetInstance<object>(typeof(TestView).Name))
  196. .Throws<ActivationException>();
  197. var region = new Region();
  198. var navigationContext = new NavigationContext(null, new Uri(typeof(TestView).Name, UriKind.Relative));
  199. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  200. // Act
  201. ExceptionAssert.Throws<InvalidOperationException>(
  202. () =>
  203. {
  204. navigationTargetHandler.LoadContent(region, navigationContext);
  205. });
  206. }
  207. [TestMethod]
  208. public void WhenViewAddedByHandlerDoesNotImplementINavigationAware_ThenReturnsView()
  209. {
  210. // Arrange
  211. var serviceLocatorMock = new Mock<IServiceLocator>();
  212. var region = new Region();
  213. var view = new TestView();
  214. serviceLocatorMock
  215. .Setup(sl => sl.GetInstance<object>(view.GetType().Name))
  216. .Returns(view);
  217. var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative));
  218. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  219. // Act
  220. var firstReturnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  221. var secondReturnedView = navigationTargetHandler.LoadContent(region, navigationContext);
  222. // Assert
  223. Assert.AreSame(view, firstReturnedView);
  224. Assert.AreSame(view, secondReturnedView);
  225. serviceLocatorMock.Verify(sl => sl.GetInstance<object>(view.GetType().Name), Times.Once());
  226. }
  227. [TestMethod]
  228. public void WhenRequestingContentForNullRegion_ThenThrows()
  229. {
  230. var serviceLocatorMock = new Mock<IServiceLocator>();
  231. var navigationContext = new NavigationContext(null, new Uri("/", UriKind.Relative));
  232. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  233. ExceptionAssert.Throws<ArgumentNullException>(
  234. () =>
  235. {
  236. navigationTargetHandler.LoadContent(null, navigationContext);
  237. });
  238. }
  239. [TestMethod]
  240. public void WhenRequestingContentForNullContext_ThenThrows()
  241. {
  242. var serviceLocatorMock = new Mock<IServiceLocator>();
  243. var region = new Region();
  244. var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
  245. ExceptionAssert.Throws<ArgumentNullException>(
  246. () =>
  247. {
  248. navigationTargetHandler.LoadContent(region, null);
  249. });
  250. }
  251. public class TestRegionNavigationContentLoader : RegionNavigationContentLoader
  252. {
  253. public TestRegionNavigationContentLoader(IServiceLocator serviceLocator)
  254. : base(serviceLocator)
  255. { }
  256. }
  257. public class TestView { }
  258. }
  259. }