diff --git a/Directory.Packages.props b/Directory.Packages.props index ce938e062..835a7f410 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -64,7 +64,6 @@ - diff --git a/tests/Avalonia/Prism.Avalonia.Tests/Regions/RegionViewRegistryFixture.cs b/tests/Avalonia/Prism.Avalonia.Tests/Regions/RegionViewRegistryFixture.cs index e3e9aa16e..1965219cf 100644 --- a/tests/Avalonia/Prism.Avalonia.Tests/Regions/RegionViewRegistryFixture.cs +++ b/tests/Avalonia/Prism.Avalonia.Tests/Regions/RegionViewRegistryFixture.cs @@ -8,196 +8,194 @@ using Prism.Navigation.Regions; using Xunit; -namespace Prism.Avalonia.Tests.Regions +namespace Prism.Avalonia.Tests.Regions; + +public class RegionViewRegistryFixture { - public class RegionViewRegistryFixture + [Fact] + public void CanRegisterContentAndRetrieveIt() { - [Fact] - public void CanRegisterContentAndRetrieveIt() - { - var containerMock = new Mock(); - ContainerLocator.SetContainerExtension(containerMock.Object); - 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(result.ElementAt(0)); - } + var containerMock = new Mock(); + ContainerLocator.SetContainerExtension(containerMock.Object); + containerMock.Setup(c => c.Resolve(typeof(MockContentObject))).Returns(new MockContentObject()); + var registry = new RegionViewRegistry(containerMock.Object); - [Fact] - public void ShouldRaiseEventWhenAddingContent() - { - var listener = new MySubscriberClass(); - var containerMock = new Mock(); - 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"); - registry.ContentRegistered += listener.OnContentRegistered; + Assert.NotNull(result); + Assert.Single(result); + Assert.IsType(result.ElementAt(0)); + } - registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject)); + [Fact] + public void ShouldRaiseEventWhenAddingContent() + { + var listener = new MySubscriberClass(); + var containerMock = new Mock(); + containerMock.Setup(c => c.Resolve(typeof(MockContentObject))).Returns(new MockContentObject()); + var registry = new RegionViewRegistry(containerMock.Object); - Assert.NotNull(listener.onViewRegisteredArguments); - Assert.NotNull(listener.onViewRegisteredArguments.GetView); + registry.ContentRegistered += listener.OnContentRegistered; - var result = listener.onViewRegisteredArguments.GetView(containerMock.Object); - Assert.NotNull(result); - Assert.IsType(result); - } + registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject)); - [Fact] - public void CanRegisterContentAsDelegateAndRetrieveIt() - { - ContainerLocator.SetContainerExtension(Mock.Of()); - var registry = new RegionViewRegistry(null); - var content = new MockContentObject(); + Assert.NotNull(listener.onViewRegisteredArguments); + Assert.NotNull(listener.onViewRegisteredArguments.GetView); - registry.RegisterViewWithRegion("MyRegion", () => content); - var result = registry.GetContents("MyRegion"); + var result = listener.onViewRegisteredArguments.GetView(containerMock.Object); + Assert.NotNull(result); + Assert.IsType(result); + } - Assert.NotNull(result); - Assert.Single(result); - Assert.Same(content, result.ElementAt(0)); - } + [Fact] + public void CanRegisterContentAsDelegateAndRetrieveIt() + { + ContainerLocator.SetContainerExtension(Mock.Of()); + var registry = new RegionViewRegistry(null); + var content = new MockContentObject(); - [Fact] - public async Task ShouldNotPreventSubscribersFromBeingGarbageCollected() - { - var registry = new RegionViewRegistry(null); - var subscriber = new MySubscriberClass(); - registry.ContentRegistered += subscriber.OnContentRegistered; + registry.RegisterViewWithRegion("MyRegion", () => content); + var result = registry.GetContents("MyRegion"); - WeakReference subscriberWeakReference = new WeakReference(subscriber); + Assert.NotNull(result); + Assert.Single(result); + Assert.Same(content, result.ElementAt(0)); + } - subscriber = null; - await Task.Delay(50); - GC.Collect(); + [Fact] + public async Task ShouldNotPreventSubscribersFromBeingGarbageCollected() + { + var registry = new RegionViewRegistry(null); + var subscriber = new MySubscriberClass(); + registry.ContentRegistered += subscriber.OnContentRegistered; - Assert.False(subscriberWeakReference.IsAlive); - } + WeakReference subscriberWeakReference = new WeakReference(subscriber); + + subscriber = null; + await Task.Delay(50); + GC.Collect(); + + Assert.False(subscriberWeakReference.IsAlive); + } - [Fact] - public void OnRegisterErrorShouldGiveClearException() + [Fact] + public void OnRegisterErrorShouldGiveClearException() + { + var registry = new RegionViewRegistry(null); + registry.ContentRegistered += new EventHandler(FailWithInvalidOperationException); + + try { - var registry = new RegionViewRegistry(null); - registry.ContentRegistered += new EventHandler(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"); - } + registry.RegisterViewWithRegion("R1", typeof(object)); + //Assert.Fail(); } - - [Fact] - public void OnRegisterErrorShouldSkipFrameworkExceptions() + catch (ViewRegistrationException ex) { - ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException)); - var registry = new RegionViewRegistry(null); - registry.ContentRegistered += new EventHandler(FailWithFrameworkException); - var ex = Record.Exception(() => registry.RegisterViewWithRegion("R1", typeof(object))); - Assert.NotNull(ex); - Assert.IsType(ex); Assert.Contains("Dont do this", ex.Message); Assert.Contains("R1", ex.Message); + Assert.Equal("Dont do this", ex.InnerException.Message); } - - [StaFact(DisplayName = "Flaky test, runs alone but not in a group")] - public void RegisterViewWithRegion_ShouldHaveViewModel_ByDefault() + catch (Exception) { - ViewModelLocatorFixture.ResetViewModelLocationProvider(); + //Assert.Fail("Wrong exception type"); + } + } - var containerMock = new Mock(); - ContainerLocator.SetContainerExtension(containerMock.Object); - containerMock.Setup(c => c.Resolve(typeof(Mocks.Views.Mock))).Returns(new Mocks.Views.Mock()); - containerMock.Setup(c => c.Resolve(typeof(Mocks.ViewModels.MockViewModel))).Returns(new Mocks.ViewModels.MockViewModel()); - var registry = new RegionViewRegistry(containerMock.Object); + [Fact] + public void OnRegisterErrorShouldSkipFrameworkExceptions() + { + ExceptionExtensions.RegisterFrameworkExceptionType(typeof(FrameworkException)); + var registry = new RegionViewRegistry(null); + registry.ContentRegistered += new EventHandler(FailWithFrameworkException); + var ex = Record.Exception(() => registry.RegisterViewWithRegion("R1", typeof(object))); + Assert.NotNull(ex); + Assert.IsType(ex); + Assert.Contains("Dont do this", ex.Message); + Assert.Contains("R1", ex.Message); + } + + [StaFact(DisplayName = "Flaky test, runs alone but not in a group")] + public void RegisterViewWithRegion_ShouldHaveViewModel_ByDefault() + { + ViewModelLocatorFixture.ResetViewModelLocationProvider(); - registry.RegisterViewWithRegion("MyRegion", typeof(Mocks.Views.Mock)); + var containerMock = new Mock(); + ContainerLocator.SetContainerExtension(containerMock.Object); + containerMock.Setup(c => c.Resolve(typeof(Mocks.Views.Mock))).Returns(new Mocks.Views.Mock()); + containerMock.Setup(c => c.Resolve(typeof(Mocks.ViewModels.MockViewModel))).Returns(new Mocks.ViewModels.MockViewModel()); + var registry = new RegionViewRegistry(containerMock.Object); - var result = registry.GetContents("MyRegion"); - Assert.NotNull(result); - Assert.Single(result); + registry.RegisterViewWithRegion("MyRegion", typeof(Mocks.Views.Mock)); - var view = result.ElementAt(0) as Control; - Assert.IsType(view); - Assert.NotNull(view.DataContext); - Assert.IsType(view.DataContext); - } + var result = registry.GetContents("MyRegion"); + Assert.NotNull(result); + Assert.Single(result); - [StaFact(DisplayName = "Flaky test, runs alone but not in a group")] - public void RegisterViewWithRegion_ShouldNotHaveViewModel_OnOptOut() - { - ViewModelLocatorFixture.ResetViewModelLocationProvider(); + var view = result.ElementAt(0) as Control; + Assert.IsType(view); + Assert.NotNull(view.DataContext); + Assert.IsType(view.DataContext); + } - var containerMock = new Mock(); - ContainerLocator.SetContainerExtension(containerMock.Object); - 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); + [StaFact(DisplayName = "Flaky test, runs alone but not in a group")] + public void RegisterViewWithRegion_ShouldNotHaveViewModel_OnOptOut() + { + ViewModelLocatorFixture.ResetViewModelLocationProvider(); - registry.RegisterViewWithRegion("MyRegion", typeof(Mocks.Views.MockOptOut)); + var containerMock = new Mock(); + ContainerLocator.SetContainerExtension(containerMock.Object); + 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); - var result = registry.GetContents("MyRegion"); - Assert.NotNull(result); - Assert.Single(result); + registry.RegisterViewWithRegion("MyRegion", typeof(Mocks.Views.MockOptOut)); - var view = result.ElementAt(0) as Control; - Assert.IsType(view); - Assert.Null(view.DataContext); - } + var result = registry.GetContents("MyRegion"); + Assert.NotNull(result); + Assert.Single(result); - private void FailWithFrameworkException(object sender, ViewRegisteredEventArgs e) - { - try - { - FailWithInvalidOperationException(sender, e); - } - catch (Exception ex) - { - throw new FrameworkException(ex); - } - } + var view = result.ElementAt(0) as Control; + Assert.IsType(view); + Assert.NotNull(view.DataContext); + } - private void FailWithInvalidOperationException(object sender, ViewRegisteredEventArgs e) + private void FailWithFrameworkException(object sender, ViewRegisteredEventArgs e) + { + try { - throw new InvalidOperationException("Dont do this"); + FailWithInvalidOperationException(sender, e); } - - private class MockContentObject + catch (Exception ex) { + throw new FrameworkException(ex); } + } + + private void FailWithInvalidOperationException(object sender, ViewRegisteredEventArgs e) + { + throw new InvalidOperationException("Dont do this"); + } - private class MySubscriberClass + private class MockContentObject + { + } + + private class MySubscriberClass + { + public ViewRegisteredEventArgs onViewRegisteredArguments; + public void OnContentRegistered(object sender, ViewRegisteredEventArgs e) { - public ViewRegisteredEventArgs onViewRegisteredArguments; - public void OnContentRegistered(object sender, ViewRegisteredEventArgs e) - { - onViewRegisteredArguments = e; - } + onViewRegisteredArguments = e; } + } - private class FrameworkException : Exception + private class FrameworkException : Exception + { + public FrameworkException(Exception innerException) + : base("", innerException) { - public FrameworkException(Exception innerException) - : base("", innerException) - { - } } } }