Skip to content

Commit f4fdb35

Browse files
Remove exception swallowing and update tests to exhibit expected behaviour (#2749)
* Remove exception swallowing and update tests to exhibit expected behaviour * Remove leftover debugging class * Remove unused using * Use `ThrowsAnyAsync` * Refactorings based on code review suggestions * Remove timeout * Fix Merge Conflicts * `dotnet format` * Update formatting * Use `AddTransientPopup` --------- Co-authored-by: Shaun Lawrence <[email protected]> Co-authored-by: Brandon Minnick <[email protected]> Co-authored-by: Brandon Minnick <[email protected]>
1 parent 0fa1945 commit f4fdb35

File tree

9 files changed

+190
-147
lines changed

9 files changed

+190
-147
lines changed

src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
7777

7878
#region Register Services for PopupServiceTests
7979

80-
var mockPageViewModel = new MockPageViewModel();
81-
var mockPopup = new MockSelfClosingPopup(mockPageViewModel, new());
82-
83-
PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Transient);
80+
appBuilder.Services.AddTransientPopup<LongLivedSelfClosingPopup, LongLivedMockPageViewModel>();
81+
appBuilder.Services.AddTransientPopup<ShortLivedSelfClosingPopup, ShortLivedMockPageViewModel>();
82+
appBuilder.Services.AddTransientPopup<GarbageCollectionHeavySelfClosingPopup, MockPageViewModel>();
8483

8584
appBuilder.Services.AddTransientPopup<MockPopup>();
86-
appBuilder.Services.AddTransient<GarbageCollectionHeavySelfClosingPopup>();
8785
#endregion
8886

8987
var mauiApp = appBuilder.Build();

src/CommunityToolkit.Maui.UnitTests/Extensions/PopupExtensionsTests.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task ClosePopupT_NullNavigation_ShouldThrowArgumentNullException()
124124
public async Task ShowPopupAsync_WithPopupType_ShowsPopupAndClosesPopup()
125125
{
126126
// Arrange
127-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
127+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
128128

129129
// Act
130130
navigation.ShowPopup(selfClosingPopup);
@@ -228,7 +228,7 @@ public void ShowPopupAsync_Shell_WithViewType_ShowsPopup()
228228
public async Task ShowPopupAsync_AwaitingShowPopupAsync_EnsurePreviousPopupClosed()
229229
{
230230
// Arrange
231-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
231+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
232232

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

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

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

270270
// Act
@@ -345,7 +345,7 @@ public void ShowPopupWithView_Shell_NavigationModalStackCountIncreases()
345345
public void ShowPopup_MultiplePopupsDisplayed()
346346
{
347347
// Arrange
348-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
348+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
349349

350350
// Act
351351
navigation.ShowPopup(selfClosingPopup, PopupOptions.Empty);
@@ -359,7 +359,7 @@ public void ShowPopup_MultiplePopupsDisplayed()
359359
public void ShowPopup_Shell_MultiplePopupsDisplayed()
360360
{
361361
// Arrange
362-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
362+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
363363
var shell = new Shell();
364364
shell.Items.Add(new MockPage(new MockPageViewModel()));
365365

@@ -421,7 +421,7 @@ public void ShowPopupAsync_WithCustomOptions_AppliesOptions()
421421
// Arrange
422422
var onTappingOutsideOfPopup = () => { };
423423

424-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
424+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
425425
var options = new PopupOptions
426426
{
427427
PageOverlayColor = Colors.Red,
@@ -493,7 +493,7 @@ public void ShowPopupAsync_Shell_WithCustomOptions_AppliesOptions()
493493
var shellNavigation = Shell.Current.Navigation;
494494
var onTappingOutsideOfPopup = () => { };
495495

496-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
496+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
497497
var options = new PopupOptions
498498
{
499499
PageOverlayColor = Colors.Red,
@@ -713,7 +713,7 @@ public void ShowPopupAsyncWithView_Shell_WithCustomOptions_AppliesOptions()
713713
public async Task ShowPopupAsync_CancellationTokenExpired()
714714
{
715715
// Arrange
716-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
716+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
717717
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
718718

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

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

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

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

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

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

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

@@ -863,15 +863,15 @@ public async Task ShowPopupAsyncWithView_Shell_CancellationTokenCanceled()
863863
public async Task ShowPopupAsync_ShouldValidateProperBindingContext()
864864
{
865865
// Arrange
866-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
867-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
868-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
866+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
867+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
869868

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

873872
// Assert
874-
Assert.Same(popupInstance.BindingContext, popupViewModel);
873+
Assert.NotNull(popupInstance.BindingContext);
874+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
875875
}
876876

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

887887
var shellNavigation = Shell.Current.Navigation;
888-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
889-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
890-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
888+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
889+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
891890

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

895894
// Assert
896-
Assert.Same(popupInstance.BindingContext, popupViewModel);
895+
Assert.NotNull(popupInstance.BindingContext);
896+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
897897
Assert.Equal(shellParameterBackgroundColorValue, selfClosingPopup.BackgroundColor);
898898
}
899899

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

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

916915
// Assert
917-
Assert.Same(popupInstance.BindingContext, popupViewModel);
916+
Assert.NotNull(popupInstance.BindingContext);
917+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
918918
}
919919

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

930930
var shellNavigation = Shell.Current.Navigation;
931931
var view = new ViewWithIQueryAttributable(new ViewModelWithIQueryAttributable());
932-
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
933-
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
932+
var popupInstance = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
934933

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

943942
// Assert
944-
Assert.Same(popupInstance.BindingContext, popupViewModel);
943+
Assert.NotNull(popupInstance.BindingContext);
944+
Assert.IsType<ShortLivedMockPageViewModel>(popupInstance.BindingContext);
945945
Assert.Equal(shellParameterBackgroundColorValue, view.BackgroundColor);
946946
Assert.Equal(shellParameterViewModelTextValue, view.BindingContext.Text);
947947
}
@@ -965,8 +965,8 @@ public async Task ShowPopupAsync_ShouldSuccessfullyCompleteAndReturnResultUnderH
965965
public async Task ShowPopupAsync_ShouldReturnResultOnceClosed()
966966
{
967967
// Arrange
968-
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
969-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
968+
var mockPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
969+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
970970

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

989989
var shellNavigation = Shell.Current.Navigation;
990-
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
991-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
990+
var mockPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>();
991+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
992992

993993
// Act
994994
var result = await shell.ShowPopupAsync<object?>(selfClosingPopup, PopupOptions.Empty, shellParameters, TestContext.Current.CancellationToken);
@@ -1085,7 +1085,7 @@ public async Task ShowPopupAsync_Shell_ShouldThrowArgumentNullException_WhenView
10851085
public async Task ShowPopupAsync_ShouldThrowArgumentNullException_WhenNavigationIsNull()
10861086
{
10871087
// Arrange
1088-
var selfClosingPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>() ?? throw new InvalidOperationException();
1088+
var selfClosingPopup = ServiceProvider.GetRequiredService<ShortLivedSelfClosingPopup>() ?? throw new InvalidOperationException();
10891089

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

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

11091109
// Act/Assert
11101110
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.

0 commit comments

Comments
 (0)