Skip to content

Commit

Permalink
[Housekeeping] Fix Unit Test Failure: `Catastrophic failure: System.A…
Browse files Browse the repository at this point in the history
…rgumentOutOfRangeException` (#2479)

* Update tests

* Use `Assert.ThrowsAnyAsync`

---------

Co-authored-by: Brandon Minnick <[email protected]>
  • Loading branch information
TheCodeTraveler and TheCodeTraveler authored Jan 29, 2025
1 parent 0bf5414 commit d694f5f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ jobs:
run: dotnet build ${{ env.PathToLibrarySolution }} -c Release -p:PackageVersion=${{ env.NugetPackageVersion }} -p:Version=${{ env.NugetPackageVersion }}

- name: Run All Unit Tests
run: dotnet test -c Release ${{ env.PathToLibrarySolution }} --settings ".runsettings" --collect "XPlat code coverage" --logger trx --results-directory ${{ runner.temp }}
run: dotnet test -c Release ${{ env.PathToLibrarySolution }} --settings ".runsettings" --collect "XPlat code coverage" --logger trx --results-directory ${{ runner.temp }} --logger GitHubActions

- name: Publish Test Results
if: runner.os == 'Windows'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiPackageVersion)" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" PrivateAssets="All" />
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="All" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiPackageVersion)"/>
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
68 changes: 20 additions & 48 deletions src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ namespace CommunityToolkit.Maui.UnitTests.Views;
public class PopupTests : BaseHandlerTest
{
const string resultWhenUserTapsOutsideOfPopup = "User Tapped Outside of Popup";
readonly IPopup popup = new MockPopup();
readonly MockPopup popup = new();
readonly MockPopupHandler popupHandler;

public PopupTests()
{
popupHandler = CreateElementHandler<MockPopupHandler>(popup);
Assert.IsType<IPopup>(new MockPopup(), exactMatch: false);
}

Expand Down Expand Up @@ -49,15 +51,13 @@ public async Task ShowPopupAsync_CancellationTokenExpired()

app.Windows[0].Page = page;

var popupHandler = CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

// Ensure CancellationToken Has Expired
await Task.Delay(100, CancellationToken.None);

await Assert.ThrowsAsync<TaskCanceledException>(() => page.ShowPopupAsync((MockPopup)popup, cts.Token));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => page.ShowPopupAsync(popup, cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -80,15 +80,13 @@ public async Task ShowPopupAsync_CancellationTokenCancelled()

app.Windows[0].Page = page;

var popupHandler = CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

// Ensure CancellationToken Has Expired
await cts.CancelAsync();

await Assert.ThrowsAsync<TaskCanceledException>(() => page.ShowPopupAsync((MockPopup)popup, cts.Token));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => page.ShowPopupAsync(popup, cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -111,15 +109,13 @@ public async Task CloseAsync_CancellationTokenExpired()

app.Windows[0].Page = page;

var popupHandler = CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

// Ensure CancellationToken Has Expired
await Task.Delay(100, CancellationToken.None);

await Assert.ThrowsAsync<TaskCanceledException>(() => ((MockPopup)popup).CloseAsync(token: cts.Token));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => popup.CloseAsync(token: cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -142,15 +138,13 @@ public async Task CloseAsync_CancellationTokenCancelled()

app.Windows[0].Page = page;

var popupHandler = CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

// Ensure CancellationToken Has Expired
await cts.CancelAsync();

await Assert.ThrowsAsync<TaskCanceledException>(() => ((MockPopup)popup).CloseAsync(token: cts.Token));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => popup.CloseAsync(token: cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -171,17 +165,15 @@ public async Task OnOpenedMapperIsCalled()

app.Windows[0].Page = page;

var popupHandler = CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

page.ShowPopup((MockPopup)popup);
page.ShowPopup(popup);
Assert.Equal(1, popupHandler.OnOpenedCount);
popup.OnDismissedByTappingOutsideOfPopup();
await popup.OnDismissedByTappingOutsideOfPopup(CancellationToken.None);

var popupTask = page.ShowPopupAsync((MockPopup)popup, CancellationToken.None);
popup.OnDismissedByTappingOutsideOfPopup();
var popupTask = page.ShowPopupAsync(popup, CancellationToken.None);
await popup.OnDismissedByTappingOutsideOfPopup(CancellationToken.None);

await popupTask;

Expand All @@ -202,7 +194,7 @@ public async Task PopupDismissedByTappingOutsideOfPopup()
}
};

((MockPopup)popup).Closed += (s, e) =>
popup.Closed += (s, e) =>
{
Assert.Equal(popup, s);
popupClosedTCS.SetResult(((string?)e.Result, e.WasDismissedByTappingOutsideOfPopup));
Expand All @@ -213,12 +205,10 @@ public async Task PopupDismissedByTappingOutsideOfPopup()

app.Windows[0].Page = page;

CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

popup.OnDismissedByTappingOutsideOfPopup();
await popup.OnDismissedByTappingOutsideOfPopup(CancellationToken.None);

var (result, wasDismissedByTappingOutsideOfPopup) = await popupClosedTCS.Task;

Expand Down Expand Up @@ -247,20 +237,17 @@ public async Task OnDismissedWithResult()

app.Windows[0].Page = page;

// Make sure that our popup will have a Handler
CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

((MockPopup)popup).Closed += (_, e) =>
popup.Closed += (_, e) =>
{
result = e.Result;
isPopupDismissed = true;
closedTCS.TrySetResult();
closedTCS.SetResult();
};

((MockPopup)popup).Close(new object());
popup.Close(new object());
await closedTCS.Task;

Assert.True(isPopupDismissed);
Expand Down Expand Up @@ -288,19 +275,16 @@ public async Task OnDismissedWithoutResult()

app.Windows[0].Page = page;

// Make sure that our popup will have a Handler
CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

((MockPopup)popup).Closed += (_, e) =>
popup.Closed += (_, e) =>
{
result = e.Result;
isPopupDismissed = true;
};

await ((MockPopup)popup).CloseAsync(token: CancellationToken.None);
await popup.CloseAsync(token: CancellationToken.None);

Assert.True(isPopupDismissed);
Assert.Null(result);
Expand Down Expand Up @@ -341,17 +325,14 @@ public async Task ShowPopup_IsLogicalChild()

app.Windows[0].Page = page;

// Make sure that our popup will have a Handler
CreateElementHandler<MockPopupHandler>(popup);

Assert.NotNull(popup.Handler);
Assert.NotNull(page.Handler);

Assert.Single(page.LogicalChildrenInternal);
page.ShowPopup((MockPopup)popup);
page.ShowPopup(popup);
Assert.Equal(2, page.LogicalChildrenInternal.Count);

await ((MockPopup)popup).CloseAsync(token: CancellationToken.None);
await popup.CloseAsync(token: CancellationToken.None);
Assert.Single(page.LogicalChildrenInternal);
}

Expand All @@ -361,15 +342,6 @@ public MockPopup()
{
ResultWhenUserTapsOutsideOfPopup = resultWhenUserTapsOutsideOfPopup;
}

protected override async Task OnClosed(object? result, bool wasDismissedByTappingOutsideOfPopup, CancellationToken token = default)
{
await Task.Delay(100, token);

((IPopup)this).HandlerCompleteTCS.TrySetResult();

await base.OnClosed(result, wasDismissedByTappingOutsideOfPopup, token);
}
}

sealed class PopupViewModel : INotifyPropertyChanged
Expand Down
2 changes: 2 additions & 0 deletions src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ internal virtual void OnOpened() =>
/// <param name="token"><see cref="CancellationToken"/></param>
protected virtual async Task OnClosed(object? result, bool wasDismissedByTappingOutsideOfPopup, CancellationToken token = default)
{
token.ThrowIfCancellationRequested();

((IPopup)this).OnClosed(result);
((IResourceDictionary)resources).ValuesChanged -= OnResourcesChanged;

Expand Down

0 comments on commit d694f5f

Please sign in to comment.