Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 4 additions & 5 deletions src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
#endregion

#region Register Services for PopupServiceTests

var mockPageViewModel = new MockPageViewModel();
var mockPopup = new MockSelfClosingPopup(mockPageViewModel, new());

PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Transient);

appBuilder.Services.AddTransientPopup<LongLivedSelfClosingPopup, LongLivedMockPageViewModel>();
appBuilder.Services.AddTransientPopup<ShortLivedSelfClosingPopup, ShortLivedMockPageViewModel>();

appBuilder.Services.AddTransientPopup<MockPopup>();
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task ClosePopupT_NullNavigation_ShouldThrowArgumentNullException()
public async Task ShowPopupAsync_WithPopupType_ShowsPopupAndClosesPopup()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
navigation.ShowPopup(selfClosingPopup);
Expand Down Expand Up @@ -228,7 +228,7 @@ public void ShowPopupAsync_Shell_WithViewType_ShowsPopup()
public async Task ShowPopupAsync_AwaitingShowPopupAsync_EnsurePreviousPopupClosed()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
await navigation.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, TestContext.Current.CancellationToken);
Expand All @@ -249,7 +249,7 @@ public async Task ShowPopupAsync_Shell_AwaitingShowPopupAsync_EnsurePreviousPopu
Application.Current.Windows[0].Page = shell;

var shellNavigation = Shell.Current.Navigation;
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
Expand All @@ -264,7 +264,7 @@ public async Task ShowPopupAsync_Shell_AwaitingShowPopupAsync_EnsurePreviousPopu
public void ShowPopup_NavigationModalStackCountIncreases()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
Assert.Empty(navigation.ModalStack);

// Act
Expand Down Expand Up @@ -345,7 +345,7 @@ public void ShowPopupWithView_Shell_NavigationModalStackCountIncreases()
public void ShowPopup_MultiplePopupsDisplayed()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
navigation.ShowPopup(selfClosingPopup, PopupOptions.Empty);
Expand All @@ -359,7 +359,7 @@ public void ShowPopup_MultiplePopupsDisplayed()
public void ShowPopup_Shell_MultiplePopupsDisplayed()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var shell = new Shell();
shell.Items.Add(new MockPage(new MockPageViewModel()));

Expand Down Expand Up @@ -421,7 +421,7 @@ public void ShowPopupAsync_WithCustomOptions_AppliesOptions()
// Arrange
var onTappingOutsideOfPopup = () => { };

var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var options = new PopupOptions
{
PageOverlayColor = Colors.Red,
Expand Down Expand Up @@ -493,7 +493,7 @@ public void ShowPopupAsync_Shell_WithCustomOptions_AppliesOptions()
var shellNavigation = Shell.Current.Navigation;
var onTappingOutsideOfPopup = () => { };

var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var options = new PopupOptions
{
PageOverlayColor = Colors.Red,
Expand Down Expand Up @@ -713,7 +713,7 @@ public void ShowPopupAsyncWithView_Shell_WithCustomOptions_AppliesOptions()
public async Task ShowPopupAsync_CancellationTokenExpired()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));

// Act
Expand All @@ -734,7 +734,7 @@ public async Task ShowPopupAsync_Shell_CancellationTokenExpired()
Application.Current.Windows[0].Page = shell;

var shellNavigation = Shell.Current.Navigation;
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));

Expand Down Expand Up @@ -788,7 +788,7 @@ public async Task ShowPopupAsyncWithView_Shell_CancellationTokenExpired()
public async Task ShowPopupAsync_CancellationTokenCanceled()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));

// Act
Expand All @@ -809,7 +809,7 @@ public async Task ShowPopupAsync_Shell_CancellationTokenCanceled()
Application.Current.Windows[0].Page = shell;

var shellNavigation = Shell.Current.Navigation;
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));

Expand Down Expand Up @@ -863,15 +863,15 @@ public async Task ShowPopupAsyncWithView_Shell_CancellationTokenCanceled()
public async Task ShowPopupAsync_ShouldValidateProperBindingContext()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();

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

// Assert
Assert.Same(popupInstance.BindingContext, popupViewModel);
Assert.NotNull(popupInstance.BindingContext);
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
}

[Fact(Timeout = (int)TestDuration.Medium)]
Expand All @@ -885,15 +885,15 @@ public async Task ShowPopupAsync_Shell_ShouldValidateProperBindingContext()
Application.Current.Windows[0].Page = shell;

var shellNavigation = Shell.Current.Navigation;
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();

// Act
await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);

// Assert
Assert.Same(popupInstance.BindingContext, popupViewModel);
Assert.NotNull(popupInstance.BindingContext);
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
Assert.Equal(shellParameterBackgroundColorValue, selfClosingPopup.BackgroundColor);
}

Expand All @@ -902,8 +902,7 @@ public async Task ShowPopupAsyncWithView_ShouldValidateProperBindingContext()
{
// Arrange
var view = new Grid();
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();

// Act
var showPopupTask = navigation.ShowPopupAsync<object?>(view, PopupOptions.Empty, TestContext.Current.CancellationToken);
Expand All @@ -914,7 +913,8 @@ public async Task ShowPopupAsyncWithView_ShouldValidateProperBindingContext()
await showPopupTask;

// Assert
Assert.Same(popupInstance.BindingContext, popupViewModel);
Assert.NotNull(popupInstance.BindingContext);
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
}

[Fact(Timeout = (int)TestDuration.Medium)]
Expand All @@ -929,8 +929,7 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex

var shellNavigation = Shell.Current.Navigation;
var view = new ViewWithIQueryAttributable(new ViewModelWithIQueryAttributable());
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();

// Act
var showPopupTask = shell.ShowPopupAsync<object?>(view, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
Expand All @@ -941,7 +940,8 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
await showPopupTask;

// Assert
Assert.Same(popupInstance.BindingContext, popupViewModel);
Assert.NotNull(popupInstance.BindingContext);
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
Assert.Equal(shellParameterBackgroundColorValue, view.BackgroundColor);
Assert.Equal(shellParameterViewModelTextValue, view.BindingContext.Text);
}
Expand All @@ -950,8 +950,8 @@ public async Task ShowPopupAsyncWithView_Shell_ShouldValidateProperBindingContex
public async Task ShowPopupAsync_ShouldReturnResultOnceClosed()
{
// Arrange
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var mockPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
var result = await navigation.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, TestContext.Current.CancellationToken);
Expand All @@ -972,8 +972,8 @@ public async Task ShowPopupAsync_Shell_ShouldReturnResultOnceClosed()
Application.Current.Windows[0].Page = shell;

var shellNavigation = Shell.Current.Navigation;
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var mockPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act
var result = await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
Expand Down Expand Up @@ -1070,7 +1070,7 @@ public async Task ShowPopupAsync_Shell_ShouldThrowArgumentNullException_WhenView
public async Task ShowPopupAsync_ShouldThrowArgumentNullException_WhenNavigationIsNull()
{
// Arrange
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act / Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Expand All @@ -1089,7 +1089,7 @@ public async Task ShowPopupAsync_Shell_ShouldThrowArgumentNullException_WhenNavi
Application.Current.Windows[0].Page = shell;

var shellNavigation = Shell.Current.Navigation;
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();

// Act/Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Expand Down
Loading
Loading