Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.UnitTests.Mocks;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Extensions;

public class NavigatingFromEventArgsExtensionsTests : BaseViewTest
{
[Fact]
public async Task NavigatingFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnTrue()
{
// Arrange
MockApplication application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
IPopupService popupService = ServiceProvider.GetRequiredService<IPopupService>();

Shell shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
Page mainPage = shell.CurrentPage;
ShellContentPage shellContentPage = new();

Dictionary<string, object> shellParameters = new()
{
{ nameof(ContentPage.BackgroundColor), Colors.Orange }
};

TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new();
shellContentPage.NavigatingFromEventArgsReceived += (sender, args) =>
{
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(args.IsDestinationPageACommunityToolkitPopupPage());
};

// Act
await mainPage.Navigation.PushAsync(shellContentPage);
await popupService.ShowPopupAsync<ShortLivedMockPageViewModel>(shell, null, shellParameters, TestContext.Current.CancellationToken);
bool? isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task;

// Assert
Assert.True(isDestinationPageACommunityToolkitPopupPage);
}

[Fact]
public async Task NavigatingFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnFalse()
{
// Arrange
MockApplication application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
IPopupService popupService = ServiceProvider.GetRequiredService<IPopupService>();

Shell shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
Page mainPage = shell.CurrentPage;

ShellContentPage shellContentPage = new();
ShellContentPage anotherShellContentPage = new();

TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new();
shellContentPage.NavigatingFromEventArgsReceived += (sender, args) =>
{
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(args.IsDestinationPageACommunityToolkitPopupPage());
};

// Act
await mainPage.Navigation.PushAsync(shellContentPage);
await mainPage.Navigation.PushAsync(anotherShellContentPage);
bool? isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task;

// Assert
Assert.False(isDestinationPageACommunityToolkitPopupPage);
}

sealed class ShellContentPage : ContentPage
{
public event EventHandler<NavigatingFromEventArgs>? NavigatingFromEventArgsReceived;

protected override void OnNavigatingFrom(NavigatingFromEventArgs args)
{
base.OnNavigatingFrom(args);
NavigatingFromEventArgsReceived?.Invoke(this, args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace CommunityToolkit.Maui.Extensions;
public static class NavigatedFromEventArgsExtensions
{
/// <summary>
/// Determines whether the previous page was a Community Toolkit <see cref="Popup"/>.
/// Determines if the destination page is a Community Toolkit <see cref="Popup"/>.
/// </summary>
/// <param name="args">The current <see cref="NavigatedFromEventArgs"/>.</param>
/// <returns>A boolean indicating whether the previous page was a Community Toolkit <see cref="Popup"/>.</returns>
/// <returns>A boolean indicating if the destination page is a Community Toolkit <see cref="Popup"/>.</returns>
public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatedFromEventArgs args) => args.DestinationPage is PopupPage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CommunityToolkit.Maui.Views;

namespace CommunityToolkit.Maui.Extensions;

/// <summary>
/// Extension methods for <see cref="NavigatingFromEventArgs"/>.
/// </summary>
public static class NavigatingFromEventArgsExtensions
{
/// <summary>
/// Determines if the destination page will be a Community Toolkit <see cref="Popup"/>.
/// </summary>
/// <param name="args">The current <see cref="NavigatingFromEventArgs"/>.</param>
/// <returns>A boolean indicating if the destination page will be a Community Toolkit <see cref="Popup"/>.</returns>
public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatingFromEventArgs args) => args.DestinationPage is PopupPage;
}
Loading