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

Fix string constructor for ShellNavigationState #13478

Merged
merged 1 commit into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 296 additions & 0 deletions Xamarin.Forms.Core.UnitTests/ShellNavigatingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace Xamarin.Forms.Core.UnitTests
[TestFixture]
public class ShellNavigatingTests : ShellTestBase
{
[TearDown]
public override void TearDown()
{
base.TearDown();
Routing.Clear();
}

[Test]
public void CancelNavigation()
Expand Down Expand Up @@ -637,6 +643,296 @@ public async Task InitialNavigatingArgs()
null, "//item");
}


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this is just moving code to the correct file. No changes happen here

[TestCase(true, 2)]
[TestCase(false, 2)]
[TestCase(true, 3)]
[TestCase(false, 3)]
public async Task ShellItemContentRouteWithGlobalRouteRelative(bool modal, int depth)
{
var shell = new Shell();
var item1 = CreateShellItem<FlyoutItem>(asImplicit: true, shellItemRoute: "animals", shellContentRoute: "monkeys");

string route = "monkeys/details";

if (depth == 3)
{
route = "animals/monkeys/details";
}

if (modal)
Routing.RegisterRoute(route, typeof(ShellModalTests.ModalTestPage));
else
Routing.RegisterRoute(route, typeof(ContentPage));

shell.Items.Add(item1);

await shell.GoToAsync("details");
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//animals/monkeys/details"));
}

[TestCase(true)]
[TestCase(false)]
public async Task GotoSameGlobalRoutesCollapsesUriCorrectly(bool modal)
{
var shell = new Shell();
var item1 = CreateShellItem<FlyoutItem>(asImplicit: true, shellItemRoute: "animals", shellContentRoute: "monkeys");

if (modal)
Routing.RegisterRoute("details", typeof(ShellModalTests.ModalTestPage));
else
Routing.RegisterRoute("details", typeof(ContentPage));

shell.Items.Add(item1);

await shell.GoToAsync("details");
await shell.GoToAsync("details");
Assert.That(shell.CurrentState.Location.ToString(), Is.EqualTo("//animals/monkeys/details/details"));
}

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

Routing.RegisterRoute("edit", typeof(ContentPage));

shell.Items.Add(item1);

var request = ShellUriHandler.GetNavigationRequest(shell, CreateUri("//rootlevelcontent1/edit"));

Assert.AreEqual(1, request.Request.GlobalRoutes.Count);
Assert.AreEqual("edit", request.Request.GlobalRoutes.First());
}

[Test]
public async Task ShellSectionWithRelativeEdit()
{
var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1", shellSectionRoute: "section1");
var editShellContent = CreateShellContent(shellContentRoute: "edit");


item1.Items[0].Items.Add(editShellContent);
shell.Items.Add(item1);

await shell.GoToAsync("//rootlevelcontent1");
var location = shell.CurrentState.FullLocation;
await shell.NavigationManager.GoToAsync("edit", false, true);

Assert.AreEqual(editShellContent, shell.CurrentItem.CurrentItem.CurrentItem);
}


[Test]
public async Task ShellContentOnlyWithGlobalEdit()
{
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);

Routing.RegisterRoute("//rootlevelcontent1/edit", typeof(ContentPage));
await shell.GoToAsync("//rootlevelcontent1/edit");
}


[Test]
public async Task RouteWithGlobalPageRoute()
{

var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellItemRoute: "animals", shellSectionRoute: "domestic", shellContentRoute: "dogs");
var item2 = CreateShellItem(asImplicit: true, shellItemRoute: "animals", shellSectionRoute: "domestic", shellContentRoute: "cats");

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

Routing.RegisterRoute("catdetails", typeof(ContentPage));
await shell.GoToAsync("//cats/catdetails?name=3");

Assert.AreEqual("//animals/domestic/cats/catdetails", shell.CurrentState.Location.ToString());
}

[Test]
public async Task AbsoluteRoutingToPage()
{

var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellItemRoute: "animals", shellSectionRoute: "domestic", shellContentRoute: "dogs");
shell.Items.Add(item1);

Routing.RegisterRoute("catdetails", typeof(ContentPage));

Assert.That(async () => await shell.GoToAsync($"//catdetails"), Throws.Exception);
}

[Test]
public async Task LocationRemovesImplicit()
{

var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1");

shell.Items.Add(item1);

Assert.AreEqual("//rootlevelcontent1", shell.CurrentState.Location.ToString());
}

[Test]
public async Task GlobalNavigateTwice()
{

var shell = new Shell();
var item1 = CreateShellItem(asImplicit: true, shellContentRoute: "rootlevelcontent1");

shell.Items.Add(item1);
Routing.RegisterRoute("cat", typeof(ContentPage));
Routing.RegisterRoute("details", typeof(ContentPage));

await shell.GoToAsync("cat");
await shell.GoToAsync("details");

Assert.AreEqual("//rootlevelcontent1/cat/details", shell.CurrentState.Location.ToString());
await shell.GoToAsync("//rootlevelcontent1/details");
Assert.AreEqual("//rootlevelcontent1/details", shell.CurrentState.Location.ToString());
}

[Test]
public async Task GlobalRoutesRegisteredHierarchicallyNavigateCorrectly()
{
Routing.RegisterRoute("first", typeof(TestPage1));
Routing.RegisterRoute("first/second", typeof(TestPage2));
Routing.RegisterRoute("first/second/third", typeof(TestPage3));
var shell = new TestShell(
CreateShellItem(shellContentRoute: "MainPage")
);

await shell.GoToAsync("//MainPage/first/second");

Assert.AreEqual(typeof(TestPage1), shell.Navigation.NavigationStack[1].GetType());
Assert.AreEqual(typeof(TestPage2), shell.Navigation.NavigationStack[2].GetType());

await shell.GoToAsync("//MainPage/first/second/third");

Assert.AreEqual(typeof(TestPage1), shell.Navigation.NavigationStack[1].GetType());
Assert.AreEqual(typeof(TestPage2), shell.Navigation.NavigationStack[2].GetType());
Assert.AreEqual(typeof(TestPage3), shell.Navigation.NavigationStack[3].GetType());
}

[Test]
public async Task GlobalRoutesRegisteredHierarchicallyNavigateCorrectlyVariation()
{
Routing.RegisterRoute("monkeys/monkeyDetails", typeof(TestPage1));
Routing.RegisterRoute("monkeyDetails/monkeygenome", typeof(TestPage2));
var shell = new TestShell(
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals2"),
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals")
);

await shell.GoToAsync("//animals/monkeys/monkeyDetails?id=123");
await shell.GoToAsync("monkeygenome");
Assert.AreEqual("//animals/monkeys/monkeyDetails/monkeygenome", shell.CurrentState.Location.ToString());
}

[Test]
public async Task GlobalRoutesRegisteredHierarchicallyWithDoublePop()
{
Routing.RegisterRoute("monkeys/monkeyDetails", typeof(TestPage1));
Routing.RegisterRoute("monkeyDetails/monkeygenome", typeof(TestPage2));
var shell = new TestShell(
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals2"),
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals")
);

await shell.GoToAsync("//animals/monkeys/monkeyDetails?id=123");
await shell.GoToAsync("monkeygenome");
await shell.GoToAsync("../..");
Assert.AreEqual("//animals/monkeys", shell.CurrentState.Location.ToString());
}

[Test]
public async Task GlobalRoutesRegisteredHierarchicallyWithDoubleSplash()
{
Routing.RegisterRoute("//animals/monkeys/monkeyDetails", typeof(TestPage1));
var shell = new TestShell(
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals")
);

await shell.GoToAsync("//animals/monkeys/monkeyDetails?id=123");
Assert.AreEqual("//animals/monkeys/monkeyDetails", shell.CurrentState.Location.ToString());
}


[Test]
public async Task RemovePageWithNestedRoutes()
{
Routing.RegisterRoute("monkeys/monkeyDetails", typeof(TestPage1));
Routing.RegisterRoute("monkeyDetails/monkeygenome", typeof(TestPage2));
var shell = new TestShell(
CreateShellItem(shellContentRoute: "monkeys", shellItemRoute: "animals")
);

await shell.GoToAsync("//animals/monkeys/monkeyDetails");
await shell.GoToAsync("monkeygenome");
shell.Navigation.RemovePage(shell.Navigation.NavigationStack[1]);
await shell.Navigation.PopAsync();
}

[Test]
public async Task GlobalRoutesRegisteredHierarchicallyNavigateCorrectlyWithAdditionalItems()
{
Routing.RegisterRoute("monkeys/monkeyDetails", typeof(TestPage1));
Routing.RegisterRoute("monkeyDetails/monkeygenome", typeof(TestPage2));
var shell = new TestShell(
CreateShellItem(shellContentRoute: "cats", shellSectionRoute: "domestic", shellItemRoute: "animals")
);

shell.Items[0].Items.Add(CreateShellContent(shellContentRoute: "monkeys"));
shell.Items[0].Items.Add(CreateShellContent(shellContentRoute: "elephants"));
shell.Items[0].Items.Add(CreateShellContent(shellContentRoute: "bears"));
shell.Items[0].Items[0].Items.Add(CreateShellContent(shellContentRoute: "dogs"));
shell.Items.Add(CreateShellContent(shellContentRoute: "about"));
await shell.GoToAsync("//animals/monkeys/monkeyDetails?id=123");
await shell.GoToAsync("monkeygenome");
Assert.AreEqual("//animals/monkeys/monkeyDetails/monkeygenome", shell.CurrentState.Location.ToString());
}

[Test]
public async Task GoBackFromRouteWithMultiplePaths()
{
Routing.RegisterRoute("monkeys/monkeyDetails", typeof(TestPage1));

var shell = new TestShell(
CreateShellItem()
);

await shell.GoToAsync("monkeys/monkeyDetails");
await shell.GoToAsync("monkeys/monkeyDetails");
await shell.Navigation.PopAsync();
await shell.Navigation.PopAsync();
}


[Test]
public async Task GoBackFromRouteWithMultiplePathsHierarchical()
{
Routing.RegisterRoute("monkeys/monkeyDetails", typeof(TestPage1));
Routing.RegisterRoute("monkeyDetails/monkeygenome", typeof(TestPage2));

var shell = new TestShell(
CreateShellItem()
);

await shell.GoToAsync("monkeys/monkeyDetails");
await shell.GoToAsync("monkeygenome");
await shell.Navigation.PopAsync();
await shell.Navigation.PopAsync();
}

public class NavigationMonitoringTab : Tab
{
public List<string> NavigationsFired = new List<string>();
Expand Down
35 changes: 35 additions & 0 deletions Xamarin.Forms.Core.UnitTests/ShellNavigationStateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class ShellNavigationStateTests : ShellTestBase
{
[Test]
public void LocationInitializedWithUri()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These Tests are new

{
var uri = new Uri($"//test/IMPL_TEST/D_FAULT_TEST", UriKind.Relative);
var uriState = new ShellNavigationState(uri);

Assert.AreEqual("//test", uriState.Location.ToString());
Assert.AreEqual("//test/IMPL_TEST/D_FAULT_TEST", uriState.FullLocation.ToString());
}

[Test]
public void LocationInitializedWithString()
{
var uri = new Uri("//test/IMPL_TEST/D_FAULT_TEST", UriKind.Relative);
var strState = new ShellNavigationState(uri.ToString());

Assert.AreEqual("//test", strState.Location.ToString());
Assert.AreEqual("//test/IMPL_TEST/D_FAULT_TEST", strState.FullLocation.ToString());
}
}
}
Loading