Skip to content
29 changes: 29 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupPageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,35 @@ void HandlePopupPageClosed(object? sender, IPopupResult e)
}
}

[Fact]
public async Task ShowPopupAsync_FromModalNavigationPage_ShouldCloseSuccessfully()
{
// Remove shell navigation
Application.Current?.Windows[0].Page = new ContentPage() { Title = "Root" };
var rootPage = Application.Current?.Windows[0].Page as ContentPage ?? throw new InvalidOperationException("Root page must be a ContentPage");

var modalNavigationPage = new NavigationPage(new ContentPage() { Title = "Modal Navigation Page" });
var popup = new Popup<string>();

// Act - Push modal navigation page
await rootPage.Navigation.PushModalAsync(modalNavigationPage, false);

// Assert - Verify modal stack has the modal navigation page
Assert.Single(rootPage.Navigation.ModalStack);
Assert.Same(modalNavigationPage, rootPage.Navigation.ModalStack[0]);

// Act
var showPopupAsyncTask = modalNavigationPage.ShowPopupAsync<string>(popup, token: TestContext.Current.CancellationToken);
await popup.CloseAsync("Hello", TestContext.Current.CancellationToken);
var result = await showPopupAsyncTask;

// Assert
result.Result.Should().Be("Hello");

// Cleanup
await rootPage.Navigation.PopModalAsync(false);
}

[Fact]
public void PopupPageT_Close_ShouldThrowOperationCanceledException_WhenTokenIsCancelled()
{
Expand Down
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ public sealed class PopupBlockedException(in Page currentVisibleModalPage) : Inv
/// <summary>
/// An <see cref="InvalidOperationException"/> thrown when an invalid operation is performed on a <see cref="Popup"/>.
/// </summary>
public class InvalidPopupOperationException(in string message) : InvalidOperationException(message);
public class InvalidPopupOperationException(in string message) : InvalidOperationException(message);
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task CloseAsync(PopupResult result, CancellationToken token = defau
var customPageContainer = Navigation.ModalStack.OfType<IPageContainer<Page>>().LastOrDefault();
if (customPageContainer is not null && customPageContainer.CurrentPage is not PopupPage)
{
throw new PopupNotFoundException();
customPageContainer = null;
}

var popupPageToClose = customPageContainer?.CurrentPage as PopupPage
Expand Down
Loading