Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service

PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Transient);
appBuilder.Services.AddTransientPopup<MockPopup>();

appBuilder.Services.AddTransient<GarbageCollectionHeavySelfClosingPopup>();
#endregion

var mauiApp = appBuilder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,21 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
Assert.Equal(shellParameterBackgroundColorValue, view.BackgroundColor);
Assert.Equal(shellParameterViewModelTextValue, view.BindingContext.Text);
}

[Fact(Timeout = (int)TestDuration.Medium)]
public async Task ShowPopupAsync_ShouldSuccessfullyCompleteAndReturnResultUnderHeavyGarbageCollection()
{
// Arrange
var mockPopup = ServiceProvider.GetRequiredService<GarbageCollectionHeavySelfClosingPopup>();
var selfClosingPopup = ServiceProvider.GetRequiredService<GarbageCollectionHeavySelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
var result = await navigation.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, TestContext.Current.CancellationToken);

// Assert
Assert.Same(mockPopup.Result, result.Result);
Assert.False(result.WasDismissedByTappingOutsideOfPopup);
}

[Fact(Timeout = (int)TestDuration.Medium)]
public async Task ShowPopupAsync_ShouldReturnResultOnceClosed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,14 @@ public async Task ClosePopupAsyncT_ShouldClosePopupUsingPageAndReturnResult()
}
}

sealed class MockSelfClosingPopup : Popup<object?>, IQueryAttributable
class GarbageCollectionHeavySelfClosingPopup : MockSelfClosingPopup
{
public GarbageCollectionHeavySelfClosingPopup(MockPageViewModel viewModel, object? result = null) : base(viewModel, result)
{
}
}

class MockSelfClosingPopup : Popup<object?>, IQueryAttributable
{
public const int ExpectedResult = 2;

Expand All @@ -548,7 +555,9 @@ async void HandleTick(object? sender, EventArgs e)
timer.Tick -= HandleTick;
try
{
GC.Collect();
await CloseAsync(Result);
GC.Collect();
}
catch (InvalidOperationException)
{
Expand Down
9 changes: 2 additions & 7 deletions src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ partial class PopupPage : ContentPage, IQueryAttributable
readonly Popup popup;
readonly IPopupOptions popupOptions;
readonly Command tapOutsideOfPopupCommand;
readonly WeakEventManager popupClosedEventManager = new();

public PopupPage(View view, IPopupOptions popupOptions)
: this(view as Popup ?? CreatePopupFromView<Popup>(view), popupOptions)
Expand Down Expand Up @@ -63,11 +62,7 @@ public PopupPage(Popup popup, IPopupOptions popupOptions)
On<iOS>().SetModalPresentationStyle(UIModalPresentationStyle.OverFullScreen);
}

public event EventHandler<IPopupResult> PopupClosed
{
add => popupClosedEventManager.AddEventHandler(value);
remove => popupClosedEventManager.RemoveEventHandler(value);
}
public event EventHandler<IPopupResult>? PopupClosed;

// Prevent Content from being set by external class
// Casts `PopupPage.Content` to return typeof(PopupPageLayout)
Expand Down Expand Up @@ -101,7 +96,7 @@ public async Task CloseAsync(PopupResult result, CancellationToken token = defau
token.ThrowIfCancellationRequested();
await Navigation.PopModalAsync(false).WaitAsync(token);

popupClosedEventManager.HandleEvent(this, result, nameof(PopupClosed));
PopupClosed?.Invoke(this, result);
}

protected override bool OnBackButtonPressed()
Expand Down
Loading