Skip to content

Commit

Permalink
adding tests for AutoRegisterForNavigation - fixes #2088
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Apr 24, 2020
1 parent 66a24a9 commit 9bdc2ea
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 43 deletions.
42 changes: 2 additions & 40 deletions PrismLibrary.sln
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Unity.Uno", "src\Uno\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.DryIoc.Uno", "src\Uno\Prism.DryIoc.Uno\Prism.DryIoc.Uno.csproj", "{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MockApp", "tests\Forms\MockApp\MockApp.csproj", "{EBA75E47-213D-4C3E-B5C5-FB3D3513A729}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Containers\Prism.DryIoc.Shared\Prism.DryIoc.Shared.projitems*{02e0ecaa-b8c6-4eab-a9ab-164b1b99af35}*SharedItemsImports = 5
Expand Down Expand Up @@ -277,42 +279,6 @@ Global
{2E8F565D-9D13-424E-BD86-C5A362F9AAE7}.Release|x64.Build.0 = Release|Any CPU
{2E8F565D-9D13-424E-BD86-C5A362F9AAE7}.Release|x86.ActiveCfg = Release|Any CPU
{2E8F565D-9D13-424E-BD86-C5A362F9AAE7}.Release|x86.Build.0 = Release|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Debug|x64.ActiveCfg = Debug|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Debug|x64.Build.0 = Debug|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Debug|x86.ActiveCfg = Debug|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Debug|x86.Build.0 = Debug|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Release|Any CPU.Build.0 = Release|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Release|x64.ActiveCfg = Release|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Release|x64.Build.0 = Release|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Release|x86.ActiveCfg = Release|Any CPU
{D007EECA-A0AE-470C-9365-86135CEFA12E}.Release|x86.Build.0 = Release|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Debug|x64.ActiveCfg = Debug|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Debug|x64.Build.0 = Debug|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Debug|x86.ActiveCfg = Debug|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Debug|x86.Build.0 = Debug|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Release|Any CPU.Build.0 = Release|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Release|x64.ActiveCfg = Release|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Release|x64.Build.0 = Release|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Release|x86.ActiveCfg = Release|Any CPU
{9A1C368F-30AE-4C82-BE03-9168CE089B56}.Release|x86.Build.0 = Release|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Debug|x64.ActiveCfg = Debug|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Debug|x64.Build.0 = Debug|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Debug|x86.ActiveCfg = Debug|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Debug|x86.Build.0 = Debug|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Release|Any CPU.Build.0 = Release|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Release|x64.ActiveCfg = Release|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Release|x64.Build.0 = Release|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Release|x86.ActiveCfg = Release|Any CPU
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -341,10 +307,6 @@ Global
{75B1C655-8365-4130-8B79-C748BF0403F2} = {F3664D7A-6FF5-4D1F-9F5F-26EE87F032D3}
{D66336A6-07E5-401A-A710-DAEDD6975D59} = {75B1C655-8365-4130-8B79-C748BF0403F2}
{6E7EC81D-DA39-4C4F-A898-0148558C34F4} = {75B1C655-8365-4130-8B79-C748BF0403F2}
{8F959801-D494-4CAF-9437-90F30472E169} = {F3664D7A-6FF5-4D1F-9F5F-26EE87F032D3}
{D007EECA-A0AE-470C-9365-86135CEFA12E} = {8F959801-D494-4CAF-9437-90F30472E169}
{9A1C368F-30AE-4C82-BE03-9168CE089B56} = {8F959801-D494-4CAF-9437-90F30472E169}
{02E0ECAA-B8C6-4EAB-A9AB-164B1B99AF35} = {8F959801-D494-4CAF-9437-90F30472E169}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C7433AE2-B1A0-4C1A-887E-5CAA7AAF67A6}
Expand Down
15 changes: 15 additions & 0 deletions PrismLibrary_Forms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Prism.Unity.Shared", "src\C
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Prism.DryIoc.Shared", "src\Containers\Prism.DryIoc.Shared\Prism.DryIoc.Shared.shproj", "{6E7EC81D-DA39-4C4F-A898-0148558C34F4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MockApp", "tests\Forms\MockApp\MockApp.csproj", "{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
tests\Forms\Prism.DI.Forms.Tests\Prism.DI.Forms.Tests.projitems*{2fd30caf-6930-41f3-8104-3d018c8bdd9e}*SharedItemsImports = 5
Expand Down Expand Up @@ -150,6 +152,18 @@ Global
{2FD30CAF-6930-41F3-8104-3D018C8BDD9E}.Release|x64.Build.0 = Release|Any CPU
{2FD30CAF-6930-41F3-8104-3D018C8BDD9E}.Release|x86.ActiveCfg = Release|Any CPU
{2FD30CAF-6930-41F3-8104-3D018C8BDD9E}.Release|x86.Build.0 = Release|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Debug|x64.ActiveCfg = Debug|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Debug|x64.Build.0 = Debug|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Debug|x86.ActiveCfg = Debug|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Debug|x86.Build.0 = Debug|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Release|Any CPU.Build.0 = Release|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Release|x64.ActiveCfg = Release|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Release|x64.Build.0 = Release|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Release|x86.ActiveCfg = Release|Any CPU
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -169,6 +183,7 @@ Global
{09DC3C91-141C-468E-9CE6-71091A95BED8} = {07A089D7-5FAF-492A-BE7F-B854E255E32C}
{D66336A6-07E5-401A-A710-DAEDD6975D59} = {09DC3C91-141C-468E-9CE6-71091A95BED8}
{6E7EC81D-DA39-4C4F-A898-0148558C34F4} = {09DC3C91-141C-468E-9CE6-71091A95BED8}
{0E7ACB55-03FD-4EF6-A116-D6E85BC0C605} = {CF8CB255-8A76-48A7-B09A-88B14648D3A8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E03CA9C3-F24B-442E-B378-C325728E9E29}
Expand Down
9 changes: 7 additions & 2 deletions src/Forms/Prism.Forms/Ioc/AutoRegistrationViewNameProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ internal static class AutoRegistrationViewNameProvider
public static void SetDefaultProvider(Func<Type, string> navigationSegmentNameProvider) =>
_defaultProvider = navigationSegmentNameProvider;

public static string GetNavigationSegmentName(Type viewType) =>
_defaultProvider(viewType);
public static string GetNavigationSegmentName(Type viewType)
{
if (_defaultProvider is null)
_defaultProvider = DefaultProvider;

return _defaultProvider(viewType);
}

private static string DefaultProvider(Type type) =>
type.Name;
Expand Down
5 changes: 4 additions & 1 deletion src/Forms/Prism.Forms/Ioc/TypeAutoLoadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ private static void RegisterViewsAutomatically(IContainerRegistry containerRegis

private static void RegisterView(IContainerRegistry containerRegistry, Type viewType, bool checkIfRegistered = false)
{
if (viewType.IsAbstract)
return;

var name = AutoRegistrationViewNameProvider.GetNavigationSegmentName(viewType);

if(!checkIfRegistered || containerRegistry.IsRegistered<object>(name))
if(!checkIfRegistered || !containerRegistry.IsRegistered<object>(name))
{
containerRegistry.RegisterForNavigation(viewType, name);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Forms/MockApp/MockApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.13.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Forms\Prism.Forms\Prism.Forms.csproj" />
<ProjectReference Include="..\..\..\src\Prism.Core\Prism.Core.csproj" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions tests/Forms/MockApp/MockPrismApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Moq;
using Prism;
using Prism.Behaviors;
using Prism.Common;
using Prism.Ioc;
using Prism.Logging;
using Prism.Modularity;
using Prism.Navigation;

namespace MockApp
{
[AutoRegisterForNavigation]
public class MockPrismApp : PrismApplicationBase, IApplicationProvider
{
public Mock<IContainerExtension> MockContainer { get; private set; }

public static Func<Type, string> PageNameDelegate { get; set; }

protected override void Initialize()
{
ContainerLocator.ResetContainer();
MockContainer = new Mock<IContainerExtension>();
MockContainer.Setup(x => x.Resolve(typeof(IModuleCatalog)))
.Returns(new ModuleCatalog());
MockContainer.Setup(x => x.Resolve(typeof(INavigationService), NavigationServiceName))
.Returns(new PageNavigationService(MockContainer.Object, this, new PageBehaviorFactory(), new EmptyLogger()));
base.Initialize();
}

protected override IContainerExtension CreateContainerExtension()
{
return MockContainer.Object;
}

protected override void OnInitialized()
{
// Clean up the Delegate for the next test.
PageNameDelegate = null;
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{

}

protected override string GetNavigationSegmentNameFromType(Type pageType)
{
if(PageNameDelegate is null)
return base.GetNavigationSegmentNameFromType(pageType);

return PageNameDelegate(pageType);
}
}
}
8 changes: 8 additions & 0 deletions tests/Forms/MockApp/Views/AbstractPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Xamarin.Forms;

namespace MockApp.Views
{
public abstract class AbstractPage : ContentPage
{
}
}
8 changes: 8 additions & 0 deletions tests/Forms/MockApp/Views/AnotherPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Xamarin.Forms;

namespace MockApp.Views
{
public class AnotherPage : ContentPage
{
}
}
8 changes: 8 additions & 0 deletions tests/Forms/MockApp/Views/FooBarPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Xamarin.Forms;

namespace MockApp.Views
{
public class FooBarPage : ContentPage
{
}
}
8 changes: 8 additions & 0 deletions tests/Forms/MockApp/Views/SomePage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Xamarin.Forms;

namespace MockApp.Views
{
public class SomePage : ContentPage
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
using Humanizer;
using MockApp;
using MockApp.Views;
using Moq;
using Xamarin.Forms;
using Xunit;

namespace Prism.Forms.Tests.Ioc
{
public class AutoRegisterForNavigationFixture
{
public AutoRegisterForNavigationFixture()
{
Xamarin.Forms.Mocks.MockForms.Init();
}

[Theory]
[InlineData(typeof(SomePage))]
[InlineData(typeof(AnotherPage))]
[InlineData(typeof(FooBarPage))]
[InlineData(typeof(NavigationPage))]
[InlineData(typeof(TabbedPage))]
public void RegistersExpectedPagesWithTypeName(Type pageName)
{
var app = new MockPrismApp();
app.MockContainer.Verify(x => x.Register(typeof(object), pageName, pageName.Name));
}

[Fact]
public void DoesNotAutoRegisterAbstractPage()
{
var app = new MockPrismApp();

app.MockContainer.Verify(x => x.Register(typeof(object), typeof(AbstractPage), nameof(AbstractPage)), Times.Never);
}

[Theory]
[InlineData(typeof(SomePage), "somePage")]
[InlineData(typeof(AnotherPage), "anotherPage")]
[InlineData(typeof(FooBarPage), "fooBarPage")]
[InlineData(typeof(NavigationPage), "navigationPage")]
[InlineData(typeof(TabbedPage), "tabbedPage")]
public void RegistersExpectedPagesWithCustomNames(Type pageName, string expectedName)
{
MockPrismApp.PageNameDelegate = t => t.Name.Camelize();
var app = new MockPrismApp();
app.MockContainer.Verify(x => x.Register(typeof(object), pageName, expectedName));
}
}
}
2 changes: 2 additions & 0 deletions tests/Forms/Prism.Forms.Tests/Prism.Forms.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.8.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Xamarin.Forms" Version="3.6.0.344457" />
<PackageReference Include="Xamarin.Forms.Mocks" Version="3.5.0.3" />
Expand All @@ -19,6 +20,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\src\Forms\Prism.Forms\Prism.Forms.csproj" />
<ProjectReference Include="..\MockApp\MockApp.csproj" />
</ItemGroup>

</Project>

0 comments on commit 9bdc2ea

Please sign in to comment.