Skip to content

Commit

Permalink
chore: fixing Region Tests for Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Sep 9, 2023
1 parent 930e576 commit 2838d27
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using Prism.Navigation;
using Prism.Navigation.Regions;
using Prism.Navigation.Regions;

namespace Prism.Ioc
{
Expand Down
6 changes: 3 additions & 3 deletions src/Forms/Prism.Forms.Regions/Navigation/Regions/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,20 @@ private void InnerAdd(object view, string viewName, IRegionManager scopedRegionM
{
if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)
{
throw new InvalidOperationException(Resources.RegionViewExistsException);
throw new RegionViewException(Resources.RegionViewExistsException);
}

if (view is not VisualElement visualElement)
{
throw new Exception("View is not a Visual Element");
throw new RegionViewException(Resources.RegionViewIsNotVisualElementException);
}

var itemMetadata = new ItemMetadata(visualElement);
if (!string.IsNullOrEmpty(viewName))
{
if (ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName));
throw new RegionViewException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName));
}
itemMetadata.Name = viewName;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Forms/Prism.Forms.Regions/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Forms/Prism.Forms.Regions/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@
<data name="RegionViewExistsException" xml:space="preserve">
<value>View already exists in region.</value>
</data>
<data name="RegionViewIsNotVisualElementException" xml:space="preserve">
<value>View is not a Visual Element.</value>
</data>
<data name="RegionViewNameExistsException" xml:space="preserve">
<value>View with name '{0}' already exists in the region.</value>
</data>
Expand Down
39 changes: 39 additions & 0 deletions src/Prism.Core/Navigation/Regions/RegionViewException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Runtime.Serialization;

namespace Prism.Navigation.Regions;

/// <summary>
/// An exception when there is an issue with a View being added to a Region
/// </summary>
public sealed class RegionViewException : RegionException
{
/// <summary>
/// Initializes a new <see cref="RegionViewException"/>
/// </summary>
public RegionViewException()
{
}

/// <summary>
/// Initializes a new <see cref="RegionViewException"/>
/// </summary>
/// <param name="message">The Exception Message.</param>
public RegionViewException(string message) : base(message)
{
}

/// <inheritdoc />
public RegionViewException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

/// <summary>
/// Initializes a new <see cref="RegionViewException"/>
/// </summary>
/// <param name="message">The Exception Message.</param>
/// <param name="innerException">The Inner <see cref="Exception"/>.</param>
public RegionViewException(string message, Exception innerException) : base(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void CanAddAndRetrieveNamedViewInstance()
[Fact]
public void AddingDuplicateNamedViewThrows()
{
var ex = Assert.Throws<InvalidOperationException>(() =>
var ex = Assert.Throws<RegionViewException>(() =>
{
IRegion region = new Region();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Xamarin.Forms;
using Xunit;
using Region = Prism.Navigation.Regions.Region;
using NuGet.Frameworks;

namespace Prism.Forms.Regions.Tests
{
Expand Down Expand Up @@ -1004,7 +1005,10 @@ public void WhenNavigatingWithNullCallback_ThenThrows()
var region = new Region();

var navigationUri = new Uri("/", UriKind.Relative);
IContainerExtension container = new Mock<IContainerExtension>().Object;
var containerMock = new Mock<IContainerExtension>();
containerMock.Setup(x => x.Resolve(typeof(IActiveRegionHelper)))
.Returns(new RegionResolverOverrides());
var container = containerMock.Object;
var contentLoader = new Mock<RegionNavigationContentLoader>(container).Object;
IRegionNavigationJournal journal = Mock.Of<IRegionNavigationJournal>();

Expand All @@ -1013,11 +1017,11 @@ public void WhenNavigatingWithNullCallback_ThenThrows()
Region = region
};

ExceptionAssert.Throws<ArgumentNullException>(
() =>
{
target.RequestNavigate(navigationUri);
});
Exception ex = null;
target.RequestNavigate(navigationUri, x => ex = x.Exception);

Assert.NotNull(ex);
Assert.IsType<RegionViewException>(ex);
}

[Fact]
Expand All @@ -1040,7 +1044,11 @@ public void WhenNavigatingWithNoRegionSet_ThenMarshallExceptionToCallback()
[Fact]
public void WhenNavigatingWithNullUri_ThenMarshallExceptionToCallback()
{
IContainerExtension container = new Mock<IContainerExtension>().Object;

var containerMock = new Mock<IContainerExtension>();
containerMock.Setup(x => x.Resolve(typeof(IActiveRegionHelper)))
.Returns(new RegionResolverOverrides());
var container = containerMock.Object;
var contentLoader = new Mock<RegionNavigationContentLoader>(container).Object;
IRegionNavigationJournal journal = Mock.Of<IRegionNavigationJournal>();

Expand All @@ -1053,7 +1061,7 @@ public void WhenNavigatingWithNullUri_ThenMarshallExceptionToCallback()
target.RequestNavigate("", nr => error = nr.Exception);

Assert.NotNull(error);
Assert.IsType<ArgumentNullException>(error);
Assert.IsType<RegionViewException>(error);
}


Expand Down

0 comments on commit 2838d27

Please sign in to comment.