Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure NavBar closes Popup regardless of being within a Page #466

Merged
merged 2 commits into from
Feb 8, 2023
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
204 changes: 204 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/NavigationBarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Disposables;
using Uno.Toolkit.RuntimeTests.Extensions;
using Uno.Toolkit.RuntimeTests.Helpers;
using Uno.Toolkit.RuntimeTests.Tests.TestPages;
using Uno.Toolkit.UI;
using Uno.UI.RuntimeTests;
using Windows.System;

#if IS_WINUI
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
#endif


namespace Uno.Toolkit.RuntimeTests.Tests
{
[TestClass]
[RunsOnUIThread]
internal partial class NavigationBarTests
{
[TestMethod]
[DataRow(MainCommandMode.Back, DisplayName = nameof(MainCommandMode.Back))]
[DataRow(MainCommandMode.Action, DisplayName = nameof(MainCommandMode.Action))]
public async Task MainCommand_In_Popup_Without_Page(MainCommandMode mainCommandMode)
{
var shouldGoBack = mainCommandMode == MainCommandMode.Back;
var navigationBar = new NavigationBar { Content = "Title", MainCommandMode = mainCommandMode };
var popup = new Popup { Width = 100, Height = 100, HorizontalOffset = 100, VerticalOffset = 100, Child = new StackPanel { Children = { navigationBar } } };
var content = new StackPanel { Children = { popup } };

try
{
await UnitTestUIContentHelperEx.SetContentAndWait(content);

popup.IsOpen = true;

await UnitTestsUIContentHelper.WaitForIdle();
await UnitTestsUIContentHelper.WaitForLoaded(popup);

Assert.IsTrue(navigationBar.TryPerformMainCommand() == shouldGoBack, "Unexpected result from TryPerformMainCommand");

await UnitTestsUIContentHelper.WaitForIdle();

Assert.IsTrue(popup.IsOpen == !shouldGoBack, "Popup is in an incorrect state");
}
finally
{
popup.IsOpen = false;
}
}

[TestMethod]
[DataRow(MainCommandMode.Back, DisplayName = nameof(MainCommandMode.Back))]
[DataRow(MainCommandMode.Action, DisplayName = nameof(MainCommandMode.Action))]
public async Task MainCommand_In_Popup_With_Page(MainCommandMode mainCommandMode)
{
NavigationBar? firstPageNavBar = null;
var shouldGoBack = mainCommandMode == MainCommandMode.Back;
var popup = new Popup { Width = 100, Height = 100, HorizontalOffset = 100, VerticalOffset = 100 };
var content = new Border { Width = 100, Height = 100, Child = popup };
var frame = new Frame() { Width = 400, Height = 400 };

popup.Child = frame;
try
{
await UnitTestUIContentHelperEx.SetContentAndWait(content);

popup.IsOpen = true;

await UnitTestsUIContentHelper.WaitForIdle();

frame.Navigate(typeof(NavBarFirstPage));

await UnitTestsUIContentHelper.WaitForIdle();

var firstPage = frame.Content as NavBarFirstPage;
if (firstPage?.FindChild<NavigationBar>() is { } firstNavBar)
{
firstPageNavBar = firstNavBar;
firstNavBar.MainCommandMode = mainCommandMode;
}

await UnitTestsUIContentHelper.WaitForLoaded(firstPageNavBar!);

Assert.IsTrue(firstPageNavBar!.TryPerformMainCommand() == shouldGoBack, "Unexpected result from TryPerformMainCommand");

await UnitTestsUIContentHelper.WaitForIdle();

Assert.IsTrue(popup.IsOpen == !shouldGoBack, "Popup is in an incorrect state");
}
finally
{
popup.IsOpen = false;
}
}

[TestMethod]
[DataRow(MainCommandMode.Back, DisplayName = nameof(MainCommandMode.Back))]
[DataRow(MainCommandMode.Action, DisplayName = nameof(MainCommandMode.Action))]
public async Task MainCommand_In_Popup_With_Page_With_BackStack(MainCommandMode mainCommandMode)
{
NavigationBar? firstPageNavBar = null;
NavigationBar? secondPageNavBar = null;

var shouldGoBack = mainCommandMode == MainCommandMode.Back;
var popup = new Popup { Width = 100, Height = 100, HorizontalOffset = 100, VerticalOffset = 100 };

var content = new Border { Width = 100, Height = 100, Child = popup };
var frame = new Frame() { Width = 400, Height = 400 };

popup.Child = frame;

try
{
await UnitTestUIContentHelperEx.SetContentAndWait(content);

popup.IsOpen = true;

await UnitTestsUIContentHelper.WaitForIdle();

frame.Navigate(typeof(NavBarFirstPage));

await UnitTestsUIContentHelper.WaitForIdle();

var firstPage = frame.Content as NavBarFirstPage;
if (firstPage?.FindChild<NavigationBar>() is { } firstNavBar)
{
firstPageNavBar = firstNavBar;
firstNavBar.MainCommandMode = mainCommandMode;
}

frame.Navigate(typeof(NavBarSecondPage));

await UnitTestsUIContentHelper.WaitForIdle();

var secondPage = frame.Content as NavBarSecondPage;
if (secondPage?.FindChild<NavigationBar>() is { } secondNavBar)
{
secondPageNavBar = secondNavBar;
secondNavBar.MainCommandMode = mainCommandMode;
}

await UnitTestsUIContentHelper.WaitForLoaded(secondPageNavBar!);

//Assert that the back was handled by the NavBar
Assert.IsTrue(secondPageNavBar!.TryPerformMainCommand() == shouldGoBack, "Unexpected result from TryPerformMainCommand");

if (mainCommandMode == MainCommandMode.Back)
{
await UnitTestsUIContentHelper.WaitForLoaded(firstPageNavBar!);
Assert.IsTrue(frame.CurrentSourcePageType == typeof(NavBarFirstPage), "Expected to navigate back to NavBarFirstPage");
}
else
{
await UnitTestsUIContentHelper.WaitForIdle();
Assert.IsTrue(frame.CurrentSourcePageType == typeof(NavBarSecondPage), "Expected to stay on NavBarSecondPage");
}

Assert.IsTrue(popup.IsOpen, "Expected Popup to remain open");

// Now we try to GoBack again
if (mainCommandMode == MainCommandMode.Back)
{
Assert.IsTrue(firstPageNavBar!.TryPerformMainCommand(), "Expected Back to be handled by NavigationBar");
}
else
{
Assert.IsFalse(secondPageNavBar!.TryPerformMainCommand(), "Expected Back to not be handled by NavigationBar");
}
await UnitTestsUIContentHelper.WaitForIdle();


if (mainCommandMode == MainCommandMode.Back)
{
Assert.IsFalse(popup.IsOpen, "Expected Popup to be closed");
}
else
{
Assert.IsTrue(frame.CurrentSourcePageType == typeof(NavBarSecondPage), "Expected to stay on NavBarSecondPage");
}

}
finally
{
popup.IsOpen = false;
}
}
}
}

14 changes: 14 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/TestPages/NavBarFirstPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:utu="using:Uno.Toolkit.UI"
x:Class="Uno.Toolkit.RuntimeTests.Tests.TestPages.NavBarFirstPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<utu:NavigationBar Content="NavBarFirstPage" Width="100"/>
<TextBlock Grid.Row="1"
Text="Hello from NavBarFirstPage" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Uno.Toolkit.UI;

#if IS_WINUI
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
#else
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
#endif

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.Toolkit.RuntimeTests.Tests.TestPages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class NavBarFirstPage : Page
{
public NavBarFirstPage()
{
this.InitializeComponent();
}
}
}
14 changes: 14 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/TestPages/NavBarSecondPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:utu="using:Uno.Toolkit.UI"
x:Class="Uno.Toolkit.RuntimeTests.Tests.TestPages.NavBarSecondPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<utu:NavigationBar Content="NavBarSecondPage" />
<TextBlock Grid.Row="1"
Text="Hello from NavBarSecondPage" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Uno.Toolkit.UI;

#if IS_WINUI
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
#else
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
#endif
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.Toolkit.RuntimeTests.Tests.TestPages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class NavBarSecondPage : Page
{
public NavBarSecondPage()
{
this.InitializeComponent();
}
}
}
33 changes: 20 additions & 13 deletions src/Uno.Toolkit.UI/Controls/NavigationBar/NavigationBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected override void OnApplyTemplate()

internal bool TryPerformMainCommand()
{
if (MainCommandMode == MainCommandMode.Action)
if (MainCommandMode != MainCommandMode.Back)
{
return false;
}
Expand All @@ -107,22 +107,29 @@ internal bool TryPerformMainCommand()
if (page.Frame is { Visibility: Visibility.Visible } frame
&& frame.CurrentSourcePageType == page.GetType())
{
if (MainCommandMode == MainCommandMode.Back)

if (frame.CanGoBack == false && _popupHost is { })
{
// If we are within a Page that is hosted within a Popup and the BackStack is empty:
// close the Popup
_popupHost.IsOpen = false;
return true;
}
else if (frame.CanGoBack)
{
if (frame.CanGoBack == false && _popupHost is { })
{
_popupHost.IsOpen = false;
return true;
}
else if (frame.CanGoBack)
{
frame.GoBack();
return true;
}

frame.GoBack();
return true;
}
}
}
else if (_popupHost is { })
{
// If we are not hosted within a Page but we are still within a Popup:
// close the Popup
_popupHost.IsOpen = false;
return true;
}

return false;
}

Expand Down