don't throw exception if registering same route factory to same route name (#6775)
authorShane Neuville <shane94@hotmail.com>
Wed, 3 Jul 2019 23:22:23 +0000 (17:22 -0600)
committerSamantha Houts <samhouts@users.noreply.github.com>
Wed, 3 Jul 2019 23:22:23 +0000 (16:22 -0700)
Xamarin.Forms.Core.UnitTests/ShellTests.cs
Xamarin.Forms.Core/Routing.cs

index 23ecae8..a780453 100644 (file)
@@ -135,7 +135,15 @@ namespace Xamarin.Forms.Core.UnitTests
                        var route = "dogs";
                        Routing.RegisterRoute(route, typeof(ShellItem));
 
-                       Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ShellItem)));
+                       Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ContentPage)));
+               }
+
+               [Test]
+               public async Task SucceedWhenAddingDuplicateRouteOfSameType()
+               {
+                       var route = "dogs";
+                       Routing.RegisterRoute(route, typeof(ShellItem));
+                       Routing.RegisterRoute(route, typeof(ShellItem));
                }
 
                [Test]
index 8feecbf..8091186 100644 (file)
@@ -26,9 +26,7 @@ namespace Xamarin.Forms
                {
                        return IsImplicit(GetRoute(source));
                }
-
-               internal static bool CompareWithRegisteredRoutes(string compare) => s_routes.ContainsKey(compare);
-
+               
                internal static void Clear()
                {
                        s_routes.Clear();
@@ -114,7 +112,7 @@ namespace Xamarin.Forms
                {
                        if (!String.IsNullOrWhiteSpace(route))
                                route = FormatRoute(route);
-                       ValidateRoute(route);
+                       ValidateRoute(route, factory);
 
                        s_routes[route] = factory;
                }
@@ -127,12 +125,7 @@ namespace Xamarin.Forms
 
                public static void RegisterRoute(string route, Type type)
                {
-                       if(!String.IsNullOrWhiteSpace(route))
-                               route = FormatRoute(route);
-
-                       ValidateRoute(route);
-
-                       s_routes[route] = new TypeRouteFactory(type);
+                       RegisterRoute(route, new TypeRouteFactory(type));
                }
 
                public static void SetRoute(Element obj, string value)
@@ -140,11 +133,13 @@ namespace Xamarin.Forms
                        obj.SetValue(RouteProperty, value);
                }
 
-               static void ValidateRoute(string route)
+               static void ValidateRoute(string route, RouteFactory routeFactory)
                {
                        if (string.IsNullOrWhiteSpace(route))
                                throw new ArgumentNullException(nameof(route), "Route cannot be an empty string");
 
+                       routeFactory = routeFactory ?? throw new ArgumentNullException(nameof(routeFactory), "Route Factory cannot be null");
+
                        var uri = new Uri(route, UriKind.RelativeOrAbsolute);
 
                        var parts = uri.OriginalString.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
@@ -154,7 +149,8 @@ namespace Xamarin.Forms
                                        throw new ArgumentException($"Route contains invalid characters in \"{part}\"");
                        }
 
-                       if (CompareWithRegisteredRoutes(route))
+                       RouteFactory existingRegistration = null;
+                       if(s_routes.TryGetValue(route, out existingRegistration) && !existingRegistration.Equals(routeFactory))
                                throw new ArgumentException($"Duplicated Route: \"{route}\"");
                }
 
@@ -171,6 +167,18 @@ namespace Xamarin.Forms
                        {
                                return (Element)Activator.CreateInstance(_type);
                        }
+                       public override bool Equals(object obj)
+                       {
+                               if ((obj is TypeRouteFactory typeRouteFactory))
+                                       return typeRouteFactory._type == _type;
+
+                               return false;
+                       }
+
+                       public override int GetHashCode()
+                       {
+                               return _type.GetHashCode();
+                       }
                }
        }
 }
\ No newline at end of file