2019-04-11 07:54:50 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Xamarin.Forms.Internals;
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Core.UnitTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ShellUriHandlerTests : ShellTestBase
|
|
|
|
|
{
|
|
|
|
|
[TearDown]
|
|
|
|
|
public override void TearDown()
|
|
|
|
|
{
|
|
|
|
|
base.TearDown();
|
|
|
|
|
Routing.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 21:27:46 +03:00
|
|
|
|
[Test]
|
|
|
|
|
public void NodeWalkingBasic()
|
|
|
|
|
{
|
|
|
|
|
var shell = new TestShell(
|
|
|
|
|
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals2"),
|
|
|
|
|
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ShellUriHandler.NodeLocation nodeLocation = new ShellUriHandler.NodeLocation();
|
|
|
|
|
nodeLocation.SetNode(shell);
|
|
|
|
|
|
|
|
|
|
nodeLocation = nodeLocation.WalkToNextNode();
|
|
|
|
|
Assert.AreEqual(nodeLocation.Content, shell.Items[0].Items[0].Items[0]);
|
|
|
|
|
|
|
|
|
|
nodeLocation = nodeLocation.WalkToNextNode();
|
|
|
|
|
Assert.AreEqual(nodeLocation.Content, shell.Items[1].Items[0].Items[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void NodeWalkingMultipleContent()
|
|
|
|
|
{
|
|
|
|
|
var shell = new TestShell(
|
|
|
|
|
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals1"),
|
|
|
|
|
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals2"),
|
|
|
|
|
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals3"),
|
|
|
|
|
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals4")
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var content = CreateShellContent();
|
|
|
|
|
shell.Items[1].Items[0].Items.Add(content);
|
|
|
|
|
shell.Items[2].Items[0].Items.Add(CreateShellContent());
|
|
|
|
|
|
|
|
|
|
// add a section with now content
|
|
|
|
|
shell.Items[0].Items.Add(new ShellSection());
|
|
|
|
|
|
|
|
|
|
ShellUriHandler.NodeLocation nodeLocation = new ShellUriHandler.NodeLocation();
|
|
|
|
|
nodeLocation.SetNode(content);
|
|
|
|
|
|
|
|
|
|
nodeLocation = nodeLocation.WalkToNextNode();
|
|
|
|
|
Assert.AreEqual(shell.Items[2].Items[0].Items[0], nodeLocation.Content);
|
|
|
|
|
|
|
|
|
|
nodeLocation = nodeLocation.WalkToNextNode();
|
|
|
|
|
Assert.AreEqual(shell.Items[2].Items[0].Items[1], nodeLocation.Content);
|
|
|
|
|
|
|
|
|
|
nodeLocation = nodeLocation.WalkToNextNode();
|
|
|
|
|
Assert.AreEqual(shell.Items[3].Items[0].Items[0], nodeLocation.Content);
|
|
|
|
|
}
|
2019-04-30 02:40:12 +03:00
|
|
|
|
|
2019-04-25 01:25:12 +03:00
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
|
public async Task GlobalRegisterAbsoluteMatching()
|
2019-04-11 07:54:50 +03:00
|
|
|
|
{
|
2019-05-09 02:15:43 +03:00
|
|
|
|
var shell = new Shell();
|
2020-09-29 13:15:44 +03:00
|
|
|
|
Routing.RegisterRoute("/seg1/seg2/seg3", typeof(object));
|
2019-04-25 01:25:12 +03:00
|
|
|
|
var request = ShellUriHandler.GetNavigationRequest(shell, CreateUri("/seg1/seg2/seg3"));
|
2019-04-11 07:54:50 +03:00
|
|
|
|
|
2019-05-09 02:15:43 +03:00
|
|
|
|
Assert.AreEqual("app://shell/IMPL_shell/seg1/seg2/seg3", request.Request.FullUri.ToString());
|
2019-04-11 07:54:50 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellRelativeGlobalRegistration()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellItemRoute: "item1", shellContentRoute: "rootlevelcontent1", shellSectionRoute: "section1");
|
|
|
|
|
var item2 = CreateShellItem(asImplicit: true, shellItemRoute: "item2", shellContentRoute: "rootlevelcontent1", shellSectionRoute: "section1");
|
|
|
|
|
|
|
|
|
|
Routing.RegisterRoute("section0/edit", typeof(ContentPage));
|
|
|
|
|
Routing.RegisterRoute("item1/section1/edit", typeof(ContentPage));
|
|
|
|
|
Routing.RegisterRoute("item2/section1/edit", typeof(ContentPage));
|
|
|
|
|
Routing.RegisterRoute("//edit", typeof(ContentPage));
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
shell.Items.Add(item2);
|
|
|
|
|
await shell.GoToAsync("//item1/section1/rootlevelcontent1");
|
2019-04-25 01:25:12 +03:00
|
|
|
|
var request = ShellUriHandler.GetNavigationRequest(shell, CreateUri("section1/edit"), true);
|
2019-04-11 07:54:50 +03:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, request.Request.GlobalRoutes.Count);
|
|
|
|
|
Assert.AreEqual("item1/section1/edit", request.Request.GlobalRoutes.First());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellSectionWithRelativeEditUpOneLevelMultiple()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1", shellSectionRoute: "section1");
|
|
|
|
|
|
|
|
|
|
Routing.RegisterRoute("section1/edit", typeof(ContentPage));
|
|
|
|
|
Routing.RegisterRoute("section1/add", typeof(ContentPage));
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
|
|
|
|
|
var request = ShellUriHandler.GetNavigationRequest(shell, CreateUri("//rootlevelcontent1/add/edit"));
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(2, request.Request.GlobalRoutes.Count);
|
|
|
|
|
Assert.AreEqual("section1/add", request.Request.GlobalRoutes.First());
|
|
|
|
|
Assert.AreEqual("section1/edit", request.Request.GlobalRoutes.Skip(1).First());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellSectionWithGlobalRouteRelative()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1", shellSectionRoute: "section1");
|
|
|
|
|
|
|
|
|
|
Routing.RegisterRoute("edit", typeof(ContentPage));
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
|
|
|
|
|
await shell.GoToAsync("//rootlevelcontent1");
|
|
|
|
|
var request = ShellUriHandler.GetNavigationRequest(shell, CreateUri("edit"));
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, request.Request.GlobalRoutes.Count);
|
|
|
|
|
Assert.AreEqual("edit", request.Request.GlobalRoutes.First());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellSectionWithRelativeEditUpOneLevel()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1", shellSectionRoute: "section1");
|
|
|
|
|
|
|
|
|
|
Routing.RegisterRoute("section1/edit", typeof(ContentPage));
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
|
|
|
|
|
await shell.GoToAsync("//rootlevelcontent1");
|
2019-04-25 01:25:12 +03:00
|
|
|
|
var request = ShellUriHandler.GetNavigationRequest(shell, CreateUri("edit"), true);
|
2019-04-11 07:54:50 +03:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual("section1/edit", request.Request.GlobalRoutes.First());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellContentOnly()
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var builders = ShellUriHandler.GenerateRoutePaths(shell, CreateUri("//rootlevelcontent1"));
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, builders.Count);
|
|
|
|
|
Assert.AreEqual("//rootlevelcontent1", builders.First().PathNoImplicit);
|
|
|
|
|
|
|
|
|
|
builders = ShellUriHandler.GenerateRoutePaths(shell, CreateUri("//rootlevelcontent2"));
|
|
|
|
|
Assert.AreEqual(1, builders.Count);
|
|
|
|
|
Assert.AreEqual("//rootlevelcontent2", builders.First().PathNoImplicit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellSectionAndContentOnly()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
2020-09-29 13:15:44 +03:00
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent", shellSectionRoute: "section1");
|
2019-04-11 07:54:50 +03:00
|
|
|
|
var item2 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent", shellSectionRoute: "section2");
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
shell.Items.Add(item2);
|
|
|
|
|
|
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
|
var builders = ShellUriHandler.GenerateRoutePaths(shell, CreateUri("//section1/rootlevelcontent")).Select(x => x.PathNoImplicit).ToArray();
|
2019-04-11 07:54:50 +03:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, builders.Length);
|
|
|
|
|
Assert.IsTrue(builders.Contains("//section1/rootlevelcontent"));
|
|
|
|
|
|
|
|
|
|
builders = ShellUriHandler.GenerateRoutePaths(shell, CreateUri("//section2/rootlevelcontent")).Select(x => x.PathNoImplicit).ToArray();
|
|
|
|
|
Assert.AreEqual(1, builders.Length);
|
|
|
|
|
Assert.IsTrue(builders.Contains("//section2/rootlevelcontent"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public async Task ShellItemAndContentOnly()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent", shellItemRoute: "item1");
|
|
|
|
|
var item2 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent", shellItemRoute: "item2");
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
shell.Items.Add(item2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var builders = ShellUriHandler.GenerateRoutePaths(shell, CreateUri("//item1/rootlevelcontent")).Select(x => x.PathNoImplicit).ToArray();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, builders.Length);
|
|
|
|
|
Assert.IsTrue(builders.Contains("//item1/rootlevelcontent"));
|
|
|
|
|
|
|
|
|
|
builders = ShellUriHandler.GenerateRoutePaths(shell, CreateUri("//item2/rootlevelcontent")).Select(x => x.PathNoImplicit).ToArray();
|
|
|
|
|
Assert.AreEqual(1, builders.Length);
|
|
|
|
|
Assert.IsTrue(builders.Contains("//item2/rootlevelcontent"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-04-25 01:25:12 +03:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task AbsoluteNavigationToRelativeWithGlobal()
|
|
|
|
|
{
|
2019-05-09 02:15:43 +03:00
|
|
|
|
var shell = new Shell();
|
2019-04-25 01:25:12 +03:00
|
|
|
|
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "dogs");
|
|
|
|
|
var item2 = CreateShellItem(asImplicit: true, shellSectionRoute: "domestic", shellContentRoute: "cats", shellItemRoute: "animals");
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
shell.Items.Add(item2);
|
|
|
|
|
|
|
|
|
|
Routing.RegisterRoute("catdetails", typeof(ContentPage));
|
2019-05-09 02:15:43 +03:00
|
|
|
|
await shell.GoToAsync($"//animals/domestic/cats/catdetails?name=domestic");
|
2019-04-25 01:25:12 +03:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(
|
2019-05-09 02:15:43 +03:00
|
|
|
|
"//animals/domestic/cats/catdetails",
|
2019-04-25 01:25:12 +03:00
|
|
|
|
shell.CurrentState.FullLocation.ToString()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 19:50:31 +03:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task RelativeNavigationToShellElementThrows()
|
|
|
|
|
{
|
|
|
|
|
var shell = new Shell();
|
|
|
|
|
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "dogs");
|
|
|
|
|
var item2 = CreateShellItem(asImplicit: true, shellSectionRoute: "domestic", shellContentRoute: "cats", shellItemRoute: "animals");
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
shell.Items.Add(item2);
|
|
|
|
|
|
|
|
|
|
Assert.That(async () => await shell.GoToAsync($"domestic"), Throws.Exception);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 21:27:46 +03:00
|
|
|
|
|
2019-04-25 01:25:12 +03:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task RelativeNavigationWithRoute()
|
|
|
|
|
{
|
2019-05-09 02:15:43 +03:00
|
|
|
|
var shell = new Shell();
|
2019-04-25 01:25:12 +03:00
|
|
|
|
|
|
|
|
|
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "dogs");
|
|
|
|
|
var item2 = CreateShellItem(asImplicit: true, shellSectionRoute: "domestic", shellContentRoute: "cats", shellItemRoute: "animals");
|
|
|
|
|
|
|
|
|
|
shell.Items.Add(item1);
|
|
|
|
|
shell.Items.Add(item2);
|
|
|
|
|
|
|
|
|
|
Routing.RegisterRoute("catdetails", typeof(ContentPage));
|
|
|
|
|
Assert.That(async () => await shell.GoToAsync($"cats/catdetails?name=domestic"), Throws.Exception);
|
|
|
|
|
|
|
|
|
|
// once relative routing with a stack is fixed then we can remove the above exception check and add below back in
|
|
|
|
|
// await shell.GoToAsync($"cats/catdetails?name=domestic")
|
|
|
|
|
//Assert.AreEqual(
|
2019-05-09 02:15:43 +03:00
|
|
|
|
// "//animals/domestic/cats/catdetails",
|
2019-04-25 01:25:12 +03:00
|
|
|
|
// shell.CurrentState.Location.ToString()
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 07:54:50 +03:00
|
|
|
|
[Test]
|
|
|
|
|
public async Task ConvertToStandardFormat()
|
|
|
|
|
{
|
2019-05-09 02:15:43 +03:00
|
|
|
|
var shell = new Shell();
|
2019-04-11 07:54:50 +03:00
|
|
|
|
|
|
|
|
|
Uri[] TestUris = new Uri[] {
|
|
|
|
|
CreateUri("path"),
|
|
|
|
|
CreateUri("//path"),
|
|
|
|
|
CreateUri("/path"),
|
2019-05-09 02:15:43 +03:00
|
|
|
|
CreateUri("shell/path"),
|
|
|
|
|
CreateUri("//shell/path"),
|
|
|
|
|
CreateUri("/shell/path"),
|
|
|
|
|
CreateUri("IMPL_shell/path"),
|
|
|
|
|
CreateUri("//IMPL_shell/path"),
|
|
|
|
|
CreateUri("/IMPL_shell/path"),
|
|
|
|
|
CreateUri("shell/IMPL_shell/path"),
|
|
|
|
|
CreateUri("//shell/IMPL_shell/path"),
|
|
|
|
|
CreateUri("/shell/IMPL_shell/path"),
|
2019-04-11 07:54:50 +03:00
|
|
|
|
CreateUri("app://path"),
|
|
|
|
|
CreateUri("app:/path"),
|
2019-05-09 02:15:43 +03:00
|
|
|
|
CreateUri("app://shell/path"),
|
|
|
|
|
CreateUri("app:/shell/path"),
|
|
|
|
|
CreateUri("app://shell/IMPL_shell/path"),
|
|
|
|
|
CreateUri("app:/shell/IMPL_shell/path"),
|
|
|
|
|
CreateUri("app:/shell/IMPL_shell\\path")
|
2019-04-11 07:54:50 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
|
foreach (var uri in TestUris)
|
2019-04-11 07:54:50 +03:00
|
|
|
|
{
|
2019-05-09 02:15:43 +03:00
|
|
|
|
Assert.AreEqual(new Uri("app://shell/IMPL_shell/path"), ShellUriHandler.ConvertToStandardFormat(shell, uri), $"{uri}");
|
2019-04-11 07:54:50 +03:00
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
|
if (!uri.IsAbsoluteUri)
|
2019-04-11 07:54:50 +03:00
|
|
|
|
{
|
|
|
|
|
var reverse = new Uri(uri.OriginalString.Replace("/", "\\"), UriKind.Relative);
|
2019-05-09 02:15:43 +03:00
|
|
|
|
Assert.AreEqual(new Uri("app://shell/IMPL_shell/path"), ShellUriHandler.ConvertToStandardFormat(shell, reverse));
|
2019-04-11 07:54:50 +03:00
|
|
|
|
}
|
2020-09-29 13:15:44 +03:00
|
|
|
|
|
2019-04-11 07:54:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|