diff --git a/PrismLibrary.sln b/PrismLibrary.sln index b1f73cd722..5a33e2c769 100644 --- a/PrismLibrary.sln +++ b/PrismLibrary.sln @@ -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 @@ -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 @@ -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} diff --git a/PrismLibrary_Forms.sln b/PrismLibrary_Forms.sln index 7e389eea03..ffd172e914 100644 --- a/PrismLibrary_Forms.sln +++ b/PrismLibrary_Forms.sln @@ -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 @@ -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 @@ -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} diff --git a/src/Forms/Prism.Forms/Ioc/AutoRegistrationViewNameProvider.cs b/src/Forms/Prism.Forms/Ioc/AutoRegistrationViewNameProvider.cs index 93e46fc4e9..76d8e14fb9 100644 --- a/src/Forms/Prism.Forms/Ioc/AutoRegistrationViewNameProvider.cs +++ b/src/Forms/Prism.Forms/Ioc/AutoRegistrationViewNameProvider.cs @@ -9,8 +9,13 @@ internal static class AutoRegistrationViewNameProvider public static void SetDefaultProvider(Func 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; diff --git a/src/Forms/Prism.Forms/Ioc/TypeAutoLoadExtensions.cs b/src/Forms/Prism.Forms/Ioc/TypeAutoLoadExtensions.cs index ae5d4e1188..2cb0588929 100644 --- a/src/Forms/Prism.Forms/Ioc/TypeAutoLoadExtensions.cs +++ b/src/Forms/Prism.Forms/Ioc/TypeAutoLoadExtensions.cs @@ -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(name)) + if(!checkIfRegistered || !containerRegistry.IsRegistered(name)) { containerRegistry.RegisterForNavigation(viewType, name); } diff --git a/tests/Forms/MockApp/MockApp.csproj b/tests/Forms/MockApp/MockApp.csproj new file mode 100644 index 0000000000..2c374e3262 --- /dev/null +++ b/tests/Forms/MockApp/MockApp.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + + + + + + + + + + + + diff --git a/tests/Forms/MockApp/MockPrismApp.cs b/tests/Forms/MockApp/MockPrismApp.cs new file mode 100644 index 0000000000..6a01824edd --- /dev/null +++ b/tests/Forms/MockApp/MockPrismApp.cs @@ -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 MockContainer { get; private set; } + + public static Func PageNameDelegate { get; set; } + + protected override void Initialize() + { + ContainerLocator.ResetContainer(); + MockContainer = new Mock(); + 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); + } + } +} diff --git a/tests/Forms/MockApp/Views/AbstractPage.cs b/tests/Forms/MockApp/Views/AbstractPage.cs new file mode 100644 index 0000000000..d57b6b76fa --- /dev/null +++ b/tests/Forms/MockApp/Views/AbstractPage.cs @@ -0,0 +1,8 @@ +using Xamarin.Forms; + +namespace MockApp.Views +{ + public abstract class AbstractPage : ContentPage + { + } +} diff --git a/tests/Forms/MockApp/Views/AnotherPage.cs b/tests/Forms/MockApp/Views/AnotherPage.cs new file mode 100644 index 0000000000..5090b895a8 --- /dev/null +++ b/tests/Forms/MockApp/Views/AnotherPage.cs @@ -0,0 +1,8 @@ +using Xamarin.Forms; + +namespace MockApp.Views +{ + public class AnotherPage : ContentPage + { + } +} diff --git a/tests/Forms/MockApp/Views/FooBarPage.cs b/tests/Forms/MockApp/Views/FooBarPage.cs new file mode 100644 index 0000000000..3a4216de49 --- /dev/null +++ b/tests/Forms/MockApp/Views/FooBarPage.cs @@ -0,0 +1,8 @@ +using Xamarin.Forms; + +namespace MockApp.Views +{ + public class FooBarPage : ContentPage + { + } +} diff --git a/tests/Forms/MockApp/Views/SomePage.cs b/tests/Forms/MockApp/Views/SomePage.cs new file mode 100644 index 0000000000..ae3b3c7bb1 --- /dev/null +++ b/tests/Forms/MockApp/Views/SomePage.cs @@ -0,0 +1,8 @@ +using Xamarin.Forms; + +namespace MockApp.Views +{ + public class SomePage : ContentPage + { + } +} diff --git a/tests/Forms/Prism.Forms.Tests/Ioc/AutoRegisterForNavigationFixture.cs b/tests/Forms/Prism.Forms.Tests/Ioc/AutoRegisterForNavigationFixture.cs new file mode 100644 index 0000000000..cbe8e1e829 --- /dev/null +++ b/tests/Forms/Prism.Forms.Tests/Ioc/AutoRegisterForNavigationFixture.cs @@ -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)); + } + } +} diff --git a/tests/Forms/Prism.Forms.Tests/Prism.Forms.Tests.csproj b/tests/Forms/Prism.Forms.Tests/Prism.Forms.Tests.csproj index 02aef4a965..9d7ef9ce06 100644 --- a/tests/Forms/Prism.Forms.Tests/Prism.Forms.Tests.csproj +++ b/tests/Forms/Prism.Forms.Tests/Prism.Forms.Tests.csproj @@ -7,6 +7,7 @@ + @@ -19,6 +20,7 @@ +