Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[Shell] refactor of processing uris (#5852)
Browse files Browse the repository at this point in the history
fixes #5790
  • Loading branch information
PureWeen authored and samhouts committed Apr 11, 2019
1 parent a58f4d7 commit 58662a1
Show file tree
Hide file tree
Showing 17 changed files with 1,500 additions and 216 deletions.
115 changes: 115 additions & 0 deletions Xamarin.Forms.Core.UnitTests/ShellTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class ShellTestBase : BaseTestFixture
{
[SetUp]
public override void Setup()
{
Device.SetFlags(new[] { Shell.ShellExperimental });
base.Setup();

}

[TearDown]
public override void TearDown()
{
base.TearDown();

}

protected Uri CreateUri(string uri) => new Uri(uri, UriKind.RelativeOrAbsolute);

protected ShellSection MakeSimpleShellSection(string route, string contentRoute)
{
return MakeSimpleShellSection(route, contentRoute, new ShellTestPage());
}

protected ShellSection MakeSimpleShellSection(string route, string contentRoute, ContentPage contentPage)
{
var shellSection = new ShellSection();
shellSection.Route = route;
var shellContent = new ShellContent { Content = contentPage, Route = contentRoute };
shellSection.Items.Add(shellContent);
return shellSection;
}

[QueryProperty("SomeQueryParameter", "SomeQueryParameter")]
public class ShellTestPage : ContentPage
{
public string SomeQueryParameter { get; set; }
}

protected ShellItem CreateShellItem(TemplatedPage page = null, bool asImplicit = false, string shellContentRoute = null, string shellSectionRoute = null, string shellItemRoute = null)
{
page = page ?? new ContentPage();
ShellItem item = null;
var section = CreateShellSection(page, asImplicit, shellContentRoute, shellSectionRoute);

if (!String.IsNullOrWhiteSpace(shellItemRoute))
{
item = new ShellItem();
item.Route = shellItemRoute;
item.Items.Add(section);
}
else if (asImplicit)
item = ShellItem.CreateFromShellSection(section);
else
{
item = new ShellItem();
item.Items.Add(section);
}

return item;
}

protected ShellSection CreateShellSection(TemplatedPage page = null, bool asImplicit = false, string shellContentRoute = null, string shellSectionRoute = null)
{
var content = CreateShellContent(page, asImplicit, shellContentRoute);

ShellSection section = null;

if (!String.IsNullOrWhiteSpace(shellSectionRoute))
{
section = new ShellSection();
section.Route = shellSectionRoute;
section.Items.Add(content);
}
else if (asImplicit)
section = ShellSection.CreateFromShellContent(content);
else
{
section = new ShellSection();
section.Items.Add(content);
}

return section;
}

protected ShellContent CreateShellContent(TemplatedPage page = null, bool asImplicit = false, string shellContentRoute = null)
{
page = page ?? new ContentPage();
ShellContent content = null;

if(!String.IsNullOrWhiteSpace(shellContentRoute))
{
content = new ShellContent() { Content = page };
content.Route = shellContentRoute;
}
else if (asImplicit)
content = (ShellContent)page;
else
content = new ShellContent() { Content = page };


return content;
}

}
}
113 changes: 85 additions & 28 deletions Xamarin.Forms.Core.UnitTests/ShellTests.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class ShellTests : BaseTestFixture
public class ShellTests : ShellTestBase
{
[SetUp]
public override void Setup()
{
Device.SetFlags(new[] { Shell.ShellExperimental });
base.Setup();

}

[Test]
public void DefaultState()
Expand Down Expand Up @@ -77,26 +71,6 @@ public void CurrentItemDoesNotChangeOnSecondAdd()
Assert.AreEqual(shellItem, shell.CurrentItem);
}

ShellSection MakeSimpleShellSection(string route, string contentRoute)
{
return MakeSimpleShellSection(route, contentRoute, new ShellTestPage());
}

ShellSection MakeSimpleShellSection (string route, string contentRoute, ContentPage contentPage)
{
var shellSection = new ShellSection();
shellSection.Route = route;
var shellContent = new ShellContent { Content = contentPage, Route = contentRoute };
shellSection.Items.Add(shellContent);
return shellSection;
}

[QueryProperty("SomeQueryParameter", "SomeQueryParameter")]
public class ShellTestPage : ContentPage
{
public string SomeQueryParameter { get; set; }
}

[Test]
public void SimpleGoTo()
{
Expand Down Expand Up @@ -127,6 +101,37 @@ public void SimpleGoTo()
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/two/tabfour/content/"));
}

[Test]
public async Task CaseIgnoreRouting()
{
var routes = new[] { "Tab1", "TAB2", "@-_-@", "+:~", "=%", "Super_Simple+-Route.doc", "1/2", @"1\2/3", "app://tab" };

foreach (var route in routes)
{
var formattedRoute = Routing.FormatRoute(route);
Routing.RegisterRoute(formattedRoute, typeof(ShellItem));

var content1 = Routing.GetOrCreateContent(formattedRoute);
Assert.IsNotNull(content1);
Assert.AreEqual(Routing.GetRoute(content1), formattedRoute);
}

Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("app://IMPL_tab21", typeof(ShellItem)));

Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute(@"app:\\IMPL_tab21", typeof(ShellItem)));

Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute(string.Empty, typeof(ShellItem)));

Assert.Catch(typeof(ArgumentNullException), () => Routing.RegisterRoute(null, typeof(ShellItem)));

Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("tab1/IMPL_tab11", typeof(ShellItem)));

Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("IMPL_shell", typeof(ShellItem)));

Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("app://tab2/IMPL_tab21", typeof(ShellItem)));
}


[Test]
public async Task RelativeGoTo()
{
Expand Down Expand Up @@ -165,6 +170,8 @@ public async Task RelativeGoTo()
await shell.GoToAsync("/tab23");
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/two/tab23/content/"));

/*
* removing support for .. notation for now
await shell.GoToAsync("../one/tab11");
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/one/tab11/content/"));
Expand All @@ -180,6 +187,7 @@ public async Task RelativeGoTo()
await shell.GoToAsync(new ShellNavigationState($"../one/tab11#fragment"));
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("app:///s/one/tab11/content/"));
*/
}

[Test]
Expand Down Expand Up @@ -313,5 +321,54 @@ public void FlyoutHeaderProjection()

Assert.AreEqual(((IShellController)shell).FlyoutHeader, label);
}

[Test]
public async Task FlyoutNavigateToImplicitContentPage()
{
var shell = new Shell();
var shellITem = new ShellItem() { FlyoutDisplayOptions = FlyoutDisplayOptions.AsMultipleItems, };
var shellSection = new ShellSection() { Title = "can navigate to" };
shellSection.Items.Add(new ContentPage());

var shellSection2 = new ShellSection() { Title = "can navigate to" };
shellSection2.Items.Add(new ContentPage());

var implicitSection = CreateShellSection(new ContentPage(), asImplicit: true);

shellITem.Items.Add(shellSection);
shellITem.Items.Add(shellSection2);
shellITem.Items.Add(implicitSection);

shell.Items.Add(shellITem);
IShellController shellController = (IShellController)shell;

await shellController.OnFlyoutItemSelectedAsync(shellSection2);
Assert.AreEqual(shellSection2, shell.CurrentItem.CurrentItem);

await shellController.OnFlyoutItemSelectedAsync(shellSection);
Assert.AreEqual(shellSection, shell.CurrentItem.CurrentItem);

await shellController.OnFlyoutItemSelectedAsync(implicitSection);
Assert.AreEqual(implicitSection, shell.CurrentItem.CurrentItem);

}


[Test]
public async Task UriNavigationTests()
{
var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1");
var item2 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent2");

shell.Items.Add(item1);
shell.Items.Add(item2);

shell.GoToAsync("//rootlevelcontent2");
Assert.AreEqual(shell.CurrentItem, item2);

shell.GoToAsync("//rootlevelcontent1");
Assert.AreEqual(shell.CurrentItem, item1);
}
}
}
Loading

0 comments on commit 58662a1

Please sign in to comment.