PageRenderTime 24ms CodeModel.GetById 9ms app.highlight 11ms RepoModel.GetById 1ms 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//===================================================================================
 17using System;
 18using System.Windows;
 19using Microsoft.Practices.Prism.Regions;
 20using Microsoft.Practices.Prism.TestSupport;
 21using Microsoft.Practices.ServiceLocation;
 22using Microsoft.VisualStudio.TestTools.UnitTesting;
 23using Moq;
 24
 25namespace Microsoft.Practices.Prism.Tests.Regions
 26{
 27    [TestClass]
 28    public class LocatorNavigationTargetHandlerFixture
 29    {
 30        [TestMethod]
 31        public void WhenViewExistsAndDoesNotImplementINavigationAware_ThenReturnsView()
 32        {
 33            // Arrange
 34
 35            var serviceLocatorMock = new Mock<IServiceLocator>();
 36
 37            var region = new Region();
 38
 39            var view = new TestView();
 40
 41            region.Add(view);
 42
 43            var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative));
 44
 45            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
 46
 47
 48            // Act
 49
 50            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
 51
 52
 53            // Assert
 54
 55            Assert.AreSame(view, returnedView);
 56        }
 57
 58        [TestMethod]
 59        public void WhenRegionHasMultipleViews_ThenViewsWithMatchingTypeNameAreConsidered()
 60        {
 61            // Arrange
 62
 63            var serviceLocatorMock = new Mock<IServiceLocator>();
 64
 65            var region = new Region();
 66
 67            var view1 = new TestView();
 68            var view2 = "view";
 69
 70            region.Add(view1);
 71            region.Add(view2);
 72
 73            var navigationContext = new NavigationContext(null, new Uri(view2.GetType().Name, UriKind.Relative));
 74
 75            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
 76
 77
 78            // Act
 79
 80            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
 81
 82
 83            // Assert
 84
 85            Assert.AreSame(view2, returnedView);
 86        }
 87
 88        [TestMethod]
 89        public void WhenRegionHasMultipleViews_ThenViewsWithMatchingFullTypeNameAreConsidered()
 90        {
 91            // Arrange
 92
 93            var serviceLocatorMock = new Mock<IServiceLocator>();
 94
 95            var region = new Region();
 96
 97            var view1 = new TestView();
 98            var view2 = "view";
 99
100            region.Add(view1);
101            region.Add(view2);
102
103            var navigationContext = new NavigationContext(null, new Uri(view2.GetType().FullName, UriKind.Relative));
104
105            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
106
107
108            // Act
109
110            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
111
112
113            // Assert
114
115            Assert.AreSame(view2, returnedView);
116        }
117
118        [TestMethod]
119        public void WhenViewExistsAndImplementsINavigationAware_ThenViewIsQueriedForNavigationAndIsReturnedIfAcceptsIt()
120        {
121            // Arrange
122
123            var serviceLocatorMock = new Mock<IServiceLocator>();
124
125            var region = new Region();
126
127            var viewMock = new Mock<INavigationAware>();
128            viewMock
129                .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
130                .Returns(true)
131                .Verifiable();
132
133            region.Add(viewMock.Object);
134
135            var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
136
137            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
138
139
140            // Act
141
142            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
143
144
145            // Assert
146
147            Assert.AreSame(viewMock.Object, returnedView);
148            viewMock.VerifyAll();
149        }
150
151        [TestMethod]
152        public void WhenViewExistsAndHasDataContextThatImplementsINavigationAware_ThenDataContextIsQueriedForNavigationAndIsReturnedIfAcceptsIt()
153        {
154            // Arrange
155
156            var serviceLocatorMock = new Mock<IServiceLocator>();
157
158            var region = new Region();
159
160            var dataContextMock = new Mock<INavigationAware>();
161            dataContextMock
162                .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
163                .Returns(true)
164                .Verifiable();
165            var viewMock = new Mock<FrameworkElement>();
166            viewMock.Object.DataContext = dataContextMock.Object;
167
168            region.Add(viewMock.Object);
169
170            var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
171
172            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
173
174
175            // Act
176
177            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
178
179
180            // Assert
181
182            Assert.AreSame(viewMock.Object, returnedView);
183            dataContextMock.VerifyAll();
184        }
185
186        [TestMethod]
187        public void WhenNoCurrentMatchingViewExists_ThenReturnsNewlyCreatedInstanceWithServiceLocatorAddedToTheRegion()
188        {
189            // Arrange
190
191            var serviceLocatorMock = new Mock<IServiceLocator>();
192
193            var region = new Region();
194
195            var view = new TestView();
196
197            serviceLocatorMock
198                .Setup(sl => sl.GetInstance<object>(view.GetType().Name))
199                .Returns(view);
200
201            var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative));
202
203            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
204
205
206            // Act
207
208            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
209
210
211            // Assert
212
213            Assert.AreSame(view, returnedView);
214            Assert.IsTrue(region.Views.Contains(view));
215        }
216
217        [TestMethod]
218        public void WhenViewExistsAndImplementsINavigationAware_ThenViewIsQueriedForNavigationAndNewInstanceIsCreatedIfItRejectsIt()
219        {
220            // Arrange
221
222            var serviceLocatorMock = new Mock<IServiceLocator>();
223
224            var region = new Region();
225
226            var viewMock = new Mock<INavigationAware>();
227            viewMock
228                .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
229                .Returns(false)
230                .Verifiable();
231
232            region.Add(viewMock.Object);
233
234            var newView = new TestView();
235
236            serviceLocatorMock
237                .Setup(sl => sl.GetInstance<object>(viewMock.Object.GetType().Name))
238                .Returns(newView);
239
240            var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
241
242            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
243
244
245            // Act
246
247            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
248
249
250            // Assert
251
252            Assert.AreSame(newView, returnedView);
253            Assert.IsTrue(region.Views.Contains(newView));
254            viewMock.VerifyAll();
255        }
256
257        [TestMethod]
258        public void WhenViewExistsAndHasDataContextThatImplementsINavigationAware_ThenDataContextIsQueriedForNavigationAndNewInstanceIsCreatedIfItRejectsIt()
259        {
260            // Arrange
261
262            var serviceLocatorMock = new Mock<IServiceLocator>();
263
264            var region = new Region();
265
266            var dataContextMock = new Mock<INavigationAware>();
267            dataContextMock
268                .Setup(v => v.IsNavigationTarget(It.IsAny<NavigationContext>()))
269                .Returns(false)
270                .Verifiable();
271            var viewMock = new Mock<FrameworkElement>();
272            viewMock.Object.DataContext = dataContextMock.Object;
273
274            region.Add(viewMock.Object);
275
276            var newView = new TestView();
277
278            serviceLocatorMock
279                .Setup(sl => sl.GetInstance<object>(viewMock.Object.GetType().Name))
280                .Returns(newView);
281
282            var navigationContext = new NavigationContext(null, new Uri(viewMock.Object.GetType().Name, UriKind.Relative));
283
284            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
285
286
287            // Act
288
289            var returnedView = navigationTargetHandler.LoadContent(region, navigationContext);
290
291
292            // Assert
293
294            Assert.AreSame(newView, returnedView);
295            Assert.IsTrue(region.Views.Contains(newView));
296            dataContextMock.VerifyAll();
297        }
298
299        [TestMethod]
300        public void WhenViewCannotBeCreated_ThenThrowsAnException()
301        {
302            var serviceLocatorMock = new Mock<IServiceLocator>();
303            serviceLocatorMock
304                .Setup(sl => sl.GetInstance<object>(typeof(TestView).Name))
305                .Throws<ActivationException>();
306
307            var region = new Region();
308
309            var navigationContext = new NavigationContext(null, new Uri(typeof(TestView).Name, UriKind.Relative));
310
311            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
312
313
314            // Act
315
316            ExceptionAssert.Throws<InvalidOperationException>(
317                () =>
318                {
319                    navigationTargetHandler.LoadContent(region, navigationContext);
320
321                });
322        }
323
324        [TestMethod]
325        public void WhenViewAddedByHandlerDoesNotImplementINavigationAware_ThenReturnsView()
326        {
327            // Arrange
328
329            var serviceLocatorMock = new Mock<IServiceLocator>();
330
331            var region = new Region();
332
333            var view = new TestView();
334
335            serviceLocatorMock
336                .Setup(sl => sl.GetInstance<object>(view.GetType().Name))
337                .Returns(view);
338
339            var navigationContext = new NavigationContext(null, new Uri(view.GetType().Name, UriKind.Relative));
340
341            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
342
343
344            // Act
345
346            var firstReturnedView = navigationTargetHandler.LoadContent(region, navigationContext);
347            var secondReturnedView = navigationTargetHandler.LoadContent(region, navigationContext);
348
349
350            // Assert
351
352            Assert.AreSame(view, firstReturnedView);
353            Assert.AreSame(view, secondReturnedView);
354            serviceLocatorMock.Verify(sl => sl.GetInstance<object>(view.GetType().Name), Times.Once());
355        }
356
357        [TestMethod]
358        public void WhenRequestingContentForNullRegion_ThenThrows()
359        {
360            var serviceLocatorMock = new Mock<IServiceLocator>();
361
362            var navigationContext = new NavigationContext(null, new Uri("/", UriKind.Relative));
363
364            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
365
366
367            ExceptionAssert.Throws<ArgumentNullException>(
368                () =>
369                {
370                    navigationTargetHandler.LoadContent(null, navigationContext);
371
372                });
373        }
374
375        [TestMethod]
376        public void WhenRequestingContentForNullContext_ThenThrows()
377        {
378            var serviceLocatorMock = new Mock<IServiceLocator>();
379
380            var region = new Region();
381
382            var navigationTargetHandler = new TestRegionNavigationContentLoader(serviceLocatorMock.Object);
383
384
385            ExceptionAssert.Throws<ArgumentNullException>(
386                () =>
387                {
388                    navigationTargetHandler.LoadContent(region, null);
389
390                });
391        }
392
393        public class TestRegionNavigationContentLoader : RegionNavigationContentLoader
394        {
395            public TestRegionNavigationContentLoader(IServiceLocator serviceLocator)
396                : base(serviceLocator)
397            { }
398        }
399
400        public class TestView { }
401    }
402}