public void CanRegisterContentAsDelegateAndRetrieveIt() { var registry = new RegionViewRegistry(null); var content = new MockContentObject(); registry.RegisterViewWithRegion("MyRegion", () => content); var result = registry.GetContents("MyRegion"); Assert.NotNull(result); Assert.Single(result); Assert.Same(content, result.ElementAt(0)); }
private MockServiceLocator GetServiceLocator(string[] defaultRegions) { MockServiceLocator locator = new MockServiceLocator(); var regionViewRegistry = new RegionViewRegistry(locator); var behavior = new AutoPopulateRegionBehavior(regionViewRegistry); var regionBehaviorFactory = new RegionBehaviorFactory(locator); regionBehaviorFactory.AddIfMissing(AutoPopulateRegionBehavior.BehaviorKey, typeof(AutoPopulateRegionBehavior)); var regionManager = new RegionManager(regionBehaviorFactory); locator.GetInstance = (type) => { if (type == typeof(IRegionManager)) { return(regionManager); } if (type == typeof(IRegionViewRegistry)) { return(regionViewRegistry); } if (type == typeof(AutoPopulateRegionBehavior)) { return(behavior); } if (type == typeof(IRegionBehaviorFactory)) { return(regionBehaviorFactory); } if (type == typeof(IServiceLocator)) { return(locator); } if (type == typeof(MockView)) { return(new MockView()); } if (type == typeof(object)) { return(new object()); } return(null); }; foreach (var region in defaultRegions) { regionManager.Regions.Add(new MockRegion { Name = region }); } return(locator); }
public void OnRegisterErrorShouldSkipFrameworkExceptions() { ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException)); var registry = new RegionViewRegistry(null); registry.ContentRegistered += new EventHandler <ViewRegisteredEventArgs>(FailWithFrameworkException); var ex = Record.Exception(() => registry.RegisterViewWithRegion("R1", typeof(object))); Assert.NotNull(ex); Assert.IsType <ViewRegistrationException>(ex); Assert.Contains("Dont do this", ex.Message); Assert.Contains("R1", ex.Message); }
public void ShouldNotPreventSubscribersFromBeingGarbageCollected() { var registry = new RegionViewRegistry(null); var subscriber = new MySubscriberClass(); registry.ContentRegistered += subscriber.OnContentRegistered; WeakReference subscriberWeakReference = new WeakReference(subscriber); subscriber = null; GC.Collect(); Assert.False(subscriberWeakReference.IsAlive); }
public void CanRegisterContentAndRetrieveIt() { var containerMock = new Mock <IContainerExtension>(); containerMock.Setup(c => c.Resolve(typeof(MockContentObject))).Returns(new MockContentObject()); var registry = new RegionViewRegistry(containerMock.Object); registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject)); var result = registry.GetContents("MyRegion"); //Assert.Equal(typeof(MockContentObject), calledType); Assert.NotNull(result); Assert.Single(result); Assert.IsType <MockContentObject>(result.ElementAt(0)); }
public void ShouldRaiseEventWhenAddingContent() { var listener = new MySubscriberClass(); var containerMock = new Mock <IContainerExtension>(); containerMock.Setup(c => c.Resolve(typeof(MockContentObject))).Returns(new MockContentObject()); var registry = new RegionViewRegistry(containerMock.Object); registry.ContentRegistered += listener.OnContentRegistered; registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject)); Assert.NotNull(listener.onViewRegisteredArguments); Assert.NotNull(listener.onViewRegisteredArguments.GetView); var result = listener.onViewRegisteredArguments.GetView(); Assert.NotNull(result); Assert.IsType <MockContentObject>(result); }
public void ShouldRaiseEventWhenAddingContent() { var listener = new MySubscriberClass(); MockServiceLocator locator = new MockServiceLocator(); locator.GetInstance = (type) => new MockContentObject(); var registry = new RegionViewRegistry(locator); registry.ContentRegistered += listener.OnContentRegistered; registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject)); Assert.IsNotNull(listener.onViewRegisteredArguments); Assert.IsNotNull(listener.onViewRegisteredArguments.GetView); var result = listener.onViewRegisteredArguments.GetView(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(MockContentObject)); }
public void CanRegisterContentAndRetrieveIt() { MockServiceLocator locator = new MockServiceLocator(); Type calledType = null; locator.GetInstance = (type) => { calledType = type; return(new MockContentObject()); }; var registry = new RegionViewRegistry(locator); registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject)); var result = registry.GetContents("MyRegion"); Assert.AreEqual(typeof(MockContentObject), calledType); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); Assert.IsInstanceOfType(result.ElementAt(0), typeof(MockContentObject)); }
public void OnRegisterErrorShouldGiveClearException() { var registry = new RegionViewRegistry(null); registry.ContentRegistered += new EventHandler <ViewRegisteredEventArgs>(FailWithInvalidOperationException); try { registry.RegisterViewWithRegion("R1", typeof(object)); //Assert.Fail(); } catch (ViewRegistrationException ex) { Assert.Contains("Dont do this", ex.Message); Assert.Contains("R1", ex.Message); Assert.Equal("Dont do this", ex.InnerException.Message); } catch (Exception) { //Assert.Fail("Wrong exception type"); } }
public void OnRegisterErrorShouldSkipFrameworkExceptions() { ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException)); var registry = new RegionViewRegistry(null); registry.ContentRegistered += new EventHandler <ViewRegisteredEventArgs>(FailWithFrameworkException); try { registry.RegisterViewWithRegion("R1", typeof(object)); Assert.Fail(); } catch (ViewRegistrationException ex) { Assert.IsTrue(ex.Message.Contains("Dont do this")); Assert.IsTrue(ex.Message.Contains("R1")); } catch (Exception) { Assert.Fail("Wrong exception type"); } }
public void RegisterViewWithRegion_ShouldNotHaveViewModel_OnOptOut() { ViewModelLocatorFixture.ResetViewModelLocationProvider(); var containerMock = new Mock <IContainerExtension>(); containerMock.Setup(c => c.Resolve(typeof(Mocks.Views.MockOptOut))).Returns(new Mocks.Views.MockOptOut()); containerMock.Setup(c => c.Resolve(typeof(Mocks.ViewModels.MockOptOutViewModel))).Returns(new Mocks.ViewModels.MockOptOutViewModel()); var registry = new RegionViewRegistry(containerMock.Object); registry.RegisterViewWithRegion("MyRegion", typeof(Mocks.Views.MockOptOut)); var result = registry.GetContents("MyRegion"); Assert.NotNull(result); Assert.Single(result); var view = result.ElementAt(0) as FrameworkElement; Assert.IsType <Mocks.Views.MockOptOut>(view); Assert.Null(view.DataContext); }
/// <summary> /// Creates new ReservationsModule obj /// </summary> /// <param name="regionViewRegistry">RegionViewRegistry inected thru prism</param> public ReservationsModule(RegionViewRegistry regionViewRegistry) { _regionRegistry = regionViewRegistry; _container = (UnityContainer)ServiceLocator.Current.GetInstance <IUnityContainer>(); }