Skip to content

Commit

Permalink
- add tests and fix failures
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Jul 29, 2024
1 parent 03954d4 commit 52fa36c
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ protected virtual void LoadView(IShellContext shellContext)

void InitialLoad(GenericGlobalLayoutListener listener, AView view)
{
// This means the handler was disconnected before the flyout was loaded
if (_shellContext?.Shell is null)
{
listener.Invalidate();
sfl.LayoutChanging -= OnFlyoutViewLayoutChanging;
return;
}

OnFlyoutViewLayoutChanging();

if (_flyoutContentView == null || ggll == null)
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Shell/ShellContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ shellSection.Parent is ShellItem shellItem &&

if (_contentCache is not null)
{
RemoveLogicalChild(_contentCache);
_contentCache.Unloaded -= OnPageUnloaded;
RemoveLogicalChild(_contentCache);
}

_contentCache = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,72 @@

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.None, 0, "Navigating Between Transient Shell States Recreates Pages")]
[Issue(IssueTracker.None, 0, "Validate Basic Service Lifetime Behavior On Shell")]
public class ShellTransientTests : Shell
{
List<ContentPage> _contentPages = new List<ContentPage>();
static List<ContentPage> _contentPages = new List<ContentPage>();
protected override void OnNavigated(ShellNavigatedEventArgs args)
{
base.OnNavigated(args);
LoadCurrentPage();
}

void LoadCurrentPage()
{
var navigatetoTransientPage = new Button
{
Text = "Navigate to transient page",
AutomationId = "NavigateToTransientPage",
Command = new Command(() =>
{
((ContentPage)this.CurrentPage).Content = new Label(){ Text = "Navigating. If you tried to navigate to the same page type, you'll be stuck here."};
this.CurrentItem = Items[0];
})
};

var navigateToNotRegisteredPage = new Button
{
Text = "Navigate to Unregistered page",
AutomationId = "NavigateToUnregisteredPage",
Command = new Command(() =>
{
((ContentPage)this.CurrentPage).Content = new Label(){ Text = "Navigating. If you tried to navigate to the same page type, you'll be stuck here."};
this.CurrentItem = Items[1];
})
};

var navigateToScopedPage = new Button
{
Text = "Navigate to scoped page",
AutomationId = "NavigateToScopedPage",
Command = new Command(() =>
{
((ContentPage)this.CurrentPage).Content = new Label(){ Text = "Navigating. If you tried to navigate to the same page type, you'll be stuck here."};
this.CurrentItem = Items[2];
})
};

var navigateToNewShell = new Button
{
Text = "Navigate to New Shell",
AutomationId = "NavigateToNewShell",
Command = new Command(() =>
{
this.Window.Page = new ShellTransientTests();
})
};

if (_contentPages.Contains(this.CurrentPage))
{
(CurrentPage as ContentPage).Content = new VerticalStackLayout()
{
Children =
{
new Label { Text = "Test Failed I am not a new page", AutomationId = "Failure" }
navigatetoTransientPage,
navigateToNotRegisteredPage,
navigateToScopedPage,
navigateToNewShell,
new Label { Text = "I am not a new page", AutomationId = "OldPage" }
}
};
}
Expand All @@ -28,13 +79,17 @@ protected override void OnNavigated(ShellNavigatedEventArgs args)
{
Children =
{
new Label { Text = "I am a new page", AutomationId = "Success" }
navigatetoTransientPage,
navigateToNotRegisteredPage,
navigateToScopedPage,
navigateToNewShell,
new Label { Text = "I am a new page", AutomationId = "NewPage" }
}
};
}

_contentPages.Add((ContentPage)this.CurrentPage);
}
}

public ShellTransientTests()
{
Expand All @@ -43,20 +98,19 @@ public ShellTransientTests()
ContentTemplate = new DataTemplate(typeof(TransientPage))
};


var shellContent2 = new ShellContent()
{
ContentTemplate = new DataTemplate(typeof(TransientPage))
ContentTemplate = new DataTemplate(typeof(ContentPage))
};

var shellContent3 = new ShellContent()
{
ContentTemplate = new DataTemplate(typeof(ContentPage))
ContentTemplate = new DataTemplate(typeof(ScopedPage))
};

Items.Add(new FlyoutItem()
{
Title = "Flyout Item 1",
Title = "Transient Page",
Items =
{
shellContent1
Expand All @@ -65,17 +119,16 @@ public ShellTransientTests()

Items.Add(new FlyoutItem()
{
Title = "Flyout Item 2",
Title = "Not Registered Page",
Items =
{
shellContent2
}
});


Items.Add(new FlyoutItem()
{
Title = "Flyout Item 3",
Title = "Scoped Page",
Items =
{
shellContent3
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/tests/TestCases.HostApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static MauiApp CreateMauiApp()
})
.Issue21109AddMappers();

appBuilder.Services.AddTransient<TransientPage>();
appBuilder.Services.AddScoped<ScopedPage>();
return appBuilder.Build();
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Controls/tests/TestCases.HostApp/ScopedPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

namespace Maui.Controls.Sample;

public class ScopedPage : ContentPage
{
static int i = 0;
public ScopedPage()
{
Index = i;
Content = new Label { Text = $"I'm a scoped page: {Index}" };
i++;
}

public int Index {get; private set;}
}
15 changes: 15 additions & 0 deletions src/Controls/tests/TestCases.HostApp/TransientPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

namespace Maui.Controls.Sample;

public class TransientPage : ContentPage
{
static int i = 0;
public TransientPage()
{
Index = i;
Content = new Label { Text = $"I'm a transient page: {Index}" };
i++;
}

public int Index {get; private set;}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.Maui.TestCases.Tests;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public partial class ShellTransientTests : _IssuesUITest
{
public ShellTransientTests(TestDevice device) : base(device) { }

public override string Issue => "Validate Basic Service Lifetime Behavior On Shell";

protected override bool ResetAfterEachTest => true;

[Test]
[Category(UITestCategories.Shell)]
public void ValidateBasicServiceLifetimePageBehavior()
{
// Navigate to Transient Page for the First time
App.WaitForElement("NewPage");

// Navigate to Unregistered Page
App.WaitForElement("NavigateToUnregisteredPage");
App.Tap("NavigateToUnregisteredPage");
App.WaitForElement("NewPage", "New Page Not Created For Initial Navigation to Unregistered Page");

// Navigate to Scoped Page for the First time
App.WaitForElement("NavigateToScopedPage");
App.Tap("NavigateToScopedPage");
App.WaitForElement("NewPage", "New Page Not Created For Initial Navigation to Scoped Page");

// Navigate to Transient Page for the Second time
App.WaitForElement("NavigateToTransientPage");
App.Tap("NavigateToTransientPage");
App.WaitForElement("NewPage", "New Page Not Created For Second Navigation To Transient Page");

// Navigate to Transient Page for the Second time
App.WaitForElement("NavigateToScopedPage");
App.Tap("NavigateToScopedPage");
App.WaitForElement("OldPage", "New Page Incorrectly Created For Scoped Page");

// Navigate to Unregistered Page
App.WaitForElement("NavigateToUnregisteredPage");
App.Tap("NavigateToUnregisteredPage");
App.WaitForElement("OldPage", "New Page Incorrectly Created For Unregistered Page");
}

[Test]
[Category(UITestCategories.Shell)]
public void SwappingShellInstancesRecreatesPages()
{
// Navigate to Scoped Page so we can test that it's resued in Swapped Shell
App.WaitForElement("NavigateToScopedPage");
App.Tap("NavigateToScopedPage");
App.WaitForElement("NewPage", "New Page Not Created For Initial Navigation to Scoped Page");

// Verify New Page Created for Transient Page
App.WaitForElement("NewPage");
App.Tap("NavigateToNewShell");
App.WaitForElement("NewPage");

// Navigate to Scoped Page on Second Shell
App.WaitForElement("NavigateToScopedPage");
App.Tap("NavigateToScopedPage");
App.WaitForElement("OldPage", "New Page Incorrectly Created For Scoped Page");
}
}
}

0 comments on commit 52fa36c

Please sign in to comment.