Skip to content

Commit

Permalink
Removed unused reference and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianSuess committed Oct 13, 2024
1 parent 1d99cd6 commit 6a1a0de
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 148 deletions.
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<PackageVersion Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageVersion Include="Avalonia.LinuxFramebuffer" Version="11.1.3" />
<PackageVersion Include="Avalonia.Markup.Xaml.Loader" Version="11.1.3" />
<PackageVersion Include="Avalonia.ReactiveUI" Version="11.1.3" /> <!-- Remove Avalonia.ReactiveUI -->
<PackageVersion Include="Avalonia.Themes.Simple" Version="11.1.3" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.1.3" />
<PackageVersion Include="Avalonia.Fonts.Inter" Version="11.1.3" />
Expand Down
292 changes: 145 additions & 147 deletions tests/Avalonia/Prism.Avalonia.Tests/Regions/RegionViewRegistryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IContainerExtension>();
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<MockContentObject>(result.ElementAt(0));
}
var containerMock = new Mock<IContainerExtension>();
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<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");

registry.ContentRegistered += listener.OnContentRegistered;
Assert.NotNull(result);
Assert.Single(result);
Assert.IsType<MockContentObject>(result.ElementAt(0));
}

registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject));
[Fact]
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);

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<MockContentObject>(result);
}
registry.RegisterViewWithRegion("MyRegion", typeof(MockContentObject));

[Fact]
public void CanRegisterContentAsDelegateAndRetrieveIt()
{
ContainerLocator.SetContainerExtension(Mock.Of<IContainerExtension>());
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<MockContentObject>(result);
}

Assert.NotNull(result);
Assert.Single(result);
Assert.Same(content, result.ElementAt(0));
}
[Fact]
public void CanRegisterContentAsDelegateAndRetrieveIt()
{
ContainerLocator.SetContainerExtension(Mock.Of<IContainerExtension>());
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<ViewRegisteredEventArgs>(FailWithInvalidOperationException);

try
{
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");
}
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<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);
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<IContainerExtension>();
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<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);
}

[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<IContainerExtension>();
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<Mocks.Views.Mock>(view);
Assert.NotNull(view.DataContext);
Assert.IsType<Mocks.ViewModels.MockViewModel>(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<Mocks.Views.Mock>(view);
Assert.NotNull(view.DataContext);
Assert.IsType<Mocks.ViewModels.MockViewModel>(view.DataContext);
}

var containerMock = new Mock<IContainerExtension>();
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<IContainerExtension>();
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<Mocks.Views.MockOptOut>(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<Mocks.Views.MockOptOut>(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)
{

}
}
}
}

0 comments on commit 6a1a0de

Please sign in to comment.