Skip to content

Commit 2b36717

Browse files
Use new cancellation token
1 parent 3f11407 commit 2b36717

37 files changed

+293
-249
lines changed

src/CommunityToolkit.Maui.MediaElement/Extensions/PageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal static Page GetCurrentPage(this Page currentPage)
3131

3232
internal record struct ParentWindow
3333
{
34-
static Page CurrentPage => GetCurrentPage(Application.Current?.Windows[0].Page ?? throw new InvalidOperationException($"{nameof(Page)} cannot be null."));
34+
static Page CurrentPage => GetCurrentPage(Application.Current?.Windows[^1].Page ?? throw new InvalidOperationException($"{nameof(Page)} cannot be null."));
3535
/// <summary>
3636
/// Checks if the parent window is null.
3737
/// </summary>

src/CommunityToolkit.Maui.SourceGenerators/Generators/TextColorToGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace {{textStyleClassMetadata.Namespace}};
145145
public static Task<bool> TextColorTo{{textStyleClassMetadata.GenericArguments}}(this global::{{textStyleClassMetadata.Namespace}}.{{textStyleClassMetadata.ClassName}}{{textStyleClassMetadata.GenericArguments}} element, {{mauiColorFullName}} color, uint rate = 16u, uint length = 250u, Easing? easing = null, CancellationToken token = default)
146146
{{textStyleClassMetadata.GenericConstraints}}
147147
{
148+
token.ThrowIfCancellationRequested();
148149
ArgumentNullException.ThrowIfNull(element);
149150
ArgumentNullException.ThrowIfNull(color);
150151

src/CommunityToolkit.Maui.UnitTests/Alerts/SnackbarTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task SnackbarShow_CancellationTokenExpires()
3838
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
3939

4040
// Ensure CancellationToken expires
41-
await Task.Delay(100, CancellationToken.None);
41+
await Task.Delay(100, TestContext.Current.CancellationToken);
4242

4343
await Assert.ThrowsAsync<OperationCanceledException>(() => snackbar.Show(cts.Token));
4444
}
@@ -61,7 +61,7 @@ public async Task SnackbarDismiss_CancellationTokenExpires()
6161
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
6262

6363
// Ensure CancellationToken expires
64-
await Task.Delay(100, CancellationToken.None);
64+
await Task.Delay(100, TestContext.Current.CancellationToken);
6565

6666
await Assert.ThrowsAsync<OperationCanceledException>(() => snackbar.Dismiss(cts.Token));
6767
}
@@ -81,14 +81,14 @@ await Assert.ThrowsAsync<OperationCanceledException>(() =>
8181
[Fact(Timeout = (int)TestDuration.Short)]
8282
public async Task SnackbarShow_IsShownTrue()
8383
{
84-
await snackbar.Show(CancellationToken.None);
84+
await snackbar.Show(TestContext.Current.CancellationToken);
8585
Assert.True(Snackbar.IsShown);
8686
}
8787

8888
[Fact(Timeout = (int)TestDuration.Short)]
8989
public async Task SnackbarDismissed_IsShownFalse()
9090
{
91-
await snackbar.Dismiss(CancellationToken.None);
91+
await snackbar.Dismiss(TestContext.Current.CancellationToken);
9292
Assert.False(Snackbar.IsShown);
9393
}
9494

@@ -100,7 +100,7 @@ public async Task SnackbarShow_ShownEventRaised()
100100
{
101101
receivedEvents.Add(e);
102102
};
103-
await snackbar.Show(CancellationToken.None);
103+
await snackbar.Show(TestContext.Current.CancellationToken);
104104
Assert.Single(receivedEvents);
105105
}
106106

@@ -112,7 +112,7 @@ public async Task SnackbarDismiss_DismissedEventRaised()
112112
{
113113
receivedEvents.Add(e);
114114
};
115-
await snackbar.Dismiss(CancellationToken.None);
115+
await snackbar.Dismiss(TestContext.Current.CancellationToken);
116116
Assert.Single(receivedEvents);
117117
}
118118

@@ -125,7 +125,7 @@ public async Task VisualElement_DisplaySnackbar_ShownEventReceived()
125125
receivedEvents.Add(e);
126126
};
127127
var button = new Button();
128-
await button.DisplaySnackbar("message", token: CancellationToken.None);
128+
await button.DisplaySnackbar("message", token: TestContext.Current.CancellationToken);
129129
Assert.Single(receivedEvents);
130130
}
131131

@@ -210,13 +210,13 @@ public async Task SnackbarDismiss_CancellationTokenNotCancelled_NotReceiveExcept
210210
[Fact(Timeout = (int)TestDuration.Short)]
211211
public async Task SnackbarShow_CancellationTokenNone_NotReceiveException()
212212
{
213-
await snackbar.Invoking(x => x.Show(CancellationToken.None)).Should().NotThrowAsync<OperationCanceledException>();
213+
await snackbar.Invoking(x => x.Show(TestContext.Current.CancellationToken)).Should().NotThrowAsync<OperationCanceledException>();
214214
}
215215

216216
[Fact(Timeout = (int)TestDuration.Short)]
217217
public async Task SnackbarDismiss_CancellationTokenNone_NotReceiveException()
218218
{
219-
await snackbar.Invoking(x => x.Dismiss(CancellationToken.None)).Should().NotThrowAsync<OperationCanceledException>();
219+
await snackbar.Invoking(x => x.Dismiss(TestContext.Current.CancellationToken)).Should().NotThrowAsync<OperationCanceledException>();
220220
}
221221

222222
[Fact]
@@ -233,8 +233,8 @@ public async Task SnackbarNullValuesThrowArgumentNullException()
233233
});
234234
Assert.Throws<ArgumentNullException>(() => Snackbar.Make(null));
235235
Assert.Throws<ArgumentNullException>(() => Snackbar.Make(string.Empty, actionButtonText: null));
236-
await Assert.ThrowsAsync<ArgumentNullException>(() => new Button().DisplaySnackbar(null, token: CancellationToken.None));
237-
await Assert.ThrowsAsync<ArgumentNullException>(() => new Button().DisplaySnackbar(string.Empty, actionButtonText: null, token: CancellationToken.None));
236+
await Assert.ThrowsAsync<ArgumentNullException>(() => new Button().DisplaySnackbar(null, token: TestContext.Current.CancellationToken));
237+
await Assert.ThrowsAsync<ArgumentNullException>(() => new Button().DisplaySnackbar(string.Empty, actionButtonText: null, token: TestContext.Current.CancellationToken));
238238
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
239239
}
240240
}

src/CommunityToolkit.Maui.UnitTests/Alerts/ToastTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task ToastShow_CancellationTokenExpires()
2121
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
2222

2323
// Ensure CancellationToken expires
24-
await Task.Delay(100, CancellationToken.None);
24+
await Task.Delay(100, TestContext.Current.CancellationToken);
2525

2626
await Assert.ThrowsAsync<OperationCanceledException>(() => toast.Show(cts.Token));
2727
}
@@ -44,7 +44,7 @@ public async Task ToastDismiss_CancellationTokenExpires()
4444
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
4545

4646
// Ensure CancellationToken expires
47-
await Task.Delay(100, CancellationToken.None);
47+
await Task.Delay(100, TestContext.Current.CancellationToken);
4848

4949
await Assert.ThrowsAsync<OperationCanceledException>(() => toast.Dismiss(cts.Token));
5050
}
@@ -116,13 +116,13 @@ public async Task ToastDismiss_CancellationTokenNotCancelled_NotReceiveException
116116
[Fact(Timeout = (int)TestDuration.Short)]
117117
public async Task ToastShow_CancellationTokenNone_NotReceiveException()
118118
{
119-
await toast.Invoking(x => x.Show(CancellationToken.None)).Should().NotThrowAsync<OperationCanceledException>();
119+
await toast.Invoking(x => x.Show(TestContext.Current.CancellationToken)).Should().NotThrowAsync<OperationCanceledException>();
120120
}
121121

122122
[Fact(Timeout = (int)TestDuration.Short)]
123123
public async Task ToastDismiss_CancellationTokenNone_NotReceiveException()
124124
{
125-
await toast.Invoking(x => x.Dismiss(CancellationToken.None)).Should().NotThrowAsync<OperationCanceledException>();
125+
await toast.Invoking(x => x.Dismiss(TestContext.Current.CancellationToken)).Should().NotThrowAsync<OperationCanceledException>();
126126
}
127127

128128
[Fact]

src/CommunityToolkit.Maui.UnitTests/Animations/FadeAnimationTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public async Task AnimateShouldThrowWithNullView()
1313
FadeAnimation animation = new();
1414

1515
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
16-
await Assert.ThrowsAsync<ArgumentNullException>(() => animation.Animate(null, CancellationToken.None));
16+
await Assert.ThrowsAsync<ArgumentNullException>(() => animation.Animate(null, TestContext.Current.CancellationToken));
1717
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
1818
}
1919

@@ -29,10 +29,11 @@ public async Task CancellationTokenCanceled()
2929
};
3030
label.EnableAnimations();
3131

32-
await Assert.ThrowsAsync<TaskCanceledException>(() =>
32+
await Assert.ThrowsAsync<OperationCanceledException>(async () =>
3333
{
34-
cts.Cancel();
35-
return animation.Animate(label, cts.Token);
34+
await cts.CancelAsync();
35+
await Task.Delay(100, TestContext.Current.CancellationToken);
36+
await animation.Animate(label, cts.Token);
3637
});
3738
}
3839

@@ -48,7 +49,8 @@ public async Task CancellationTokenExpired()
4849
};
4950
label.EnableAnimations();
5051

51-
await Assert.ThrowsAsync<TaskCanceledException>(() => animation.Animate(label, cts.Token));
52+
await Task.Delay(100, TestContext.Current.CancellationToken);
53+
await Assert.ThrowsAsync<OperationCanceledException>(() => animation.Animate(label, cts.Token));
5254
}
5355

5456
[Fact(Timeout = (int)TestDuration.Medium)]
@@ -62,7 +64,7 @@ public async Task AnimateShouldReturnToOriginalOpacity()
6264
};
6365
label.EnableAnimations();
6466

65-
await animation.Animate(label, CancellationToken.None);
67+
await animation.Animate(label, TestContext.Current.CancellationToken);
6668

6769
label.Opacity.Should().Be(0.9);
6870
}

src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace CommunityToolkit.Maui.UnitTests;
55

66
public abstract class BaseHandlerTest : BaseTest
77
{
8-
protected BaseHandlerTest(IReadOnlyList<Type>? servicesToRegister = null)
8+
protected BaseHandlerTest()
99
{
10-
InitializeServicesAndSetMockApplication(servicesToRegister ?? [], out var serviceProvider);
10+
InitializeServicesAndSetMockApplication(out var serviceProvider);
1111
ServiceProvider = serviceProvider;
1212
}
1313

@@ -41,7 +41,7 @@ protected static TViewHandler CreateViewHandler<TViewHandler>(IView view, bool d
4141
return mockViewHandler;
4242
}
4343

44-
static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> transientServicesToRegister, out IServiceProvider serviceProvider)
44+
static void InitializeServicesAndSetMockApplication(out IServiceProvider serviceProvider)
4545
{
4646
var appBuilder = MauiApp.CreateBuilder()
4747
.UseMauiCommunityToolkit()
@@ -60,17 +60,13 @@ static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> trans
6060

6161
PopupService.ClearViewModelToViewMappings();
6262
PopupService.AddTransientPopup(mockPopup, mockPageViewModel, appBuilder.Services);
63+
var page = new ContentPage();
6364
#endregion
6465

65-
foreach (var service in transientServicesToRegister)
66-
{
67-
appBuilder.Services.AddTransient(service);
68-
}
69-
7066
var mauiApp = appBuilder.Build();
7167

7268
var application = (MockApplication)mauiApp.Services.GetRequiredService<IApplication>();
73-
application.AddWindow(new Window());
69+
application.AddWindow(new Window() { Page = page });
7470
serviceProvider = mauiApp.Services;
7571

7672
IPlatformApplication.Current = application;
@@ -79,5 +75,6 @@ static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> trans
7975
application.Handler.SetMauiContext(new HandlersContextStub(mauiApp.Services));
8076

8177
CreateElementHandler<MockPopupHandler>(mockPopup);
78+
CreateViewHandler<MockPageHandler>(page);
8279
}
8380
}

src/CommunityToolkit.Maui.UnitTests/Behaviors/AnimationBehaviorTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task AnimateCommandStartsAnimation()
7272
Behaviors = { behavior }
7373
}.EnableAnimations();
7474

75-
behavior.AnimateCommand.Execute(CancellationToken.None);
75+
behavior.AnimateCommand.Execute(TestContext.Current.CancellationToken);
7676

7777
await animationStartedTcs.Task;
7878
await animationEndedTcs.Task;
@@ -101,7 +101,7 @@ void HandleAnimationEnded(object? sender, EventArgs e)
101101
[Fact]
102102
public void AnimateCommandTokenCanceled()
103103
{
104-
TaskCanceledException? exception = null;
104+
OperationCanceledException? exception = null;
105105
var animationEndedTcs = new TaskCompletionSource();
106106
var animationCommandCts = new CancellationTokenSource();
107107

@@ -128,7 +128,7 @@ public void AnimateCommandTokenCanceled()
128128
await animationEndedTcs.Task;
129129
});
130130
}
131-
catch (TaskCanceledException e)
131+
catch (OperationCanceledException e)
132132
{
133133
exception = e;
134134
}
@@ -145,7 +145,7 @@ void HandleAnimationEnded(object? sender, EventArgs e)
145145
[Fact]
146146
public void AnimateCommandTokenExpired()
147147
{
148-
TaskCanceledException? exception = null;
148+
OperationCanceledException? exception = null;
149149
var animationEndedTcs = new TaskCompletionSource();
150150
var animationCommandCts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));
151151

@@ -171,7 +171,7 @@ public void AnimateCommandTokenExpired()
171171
await animationEndedTcs.Task;
172172
});
173173
}
174-
catch (TaskCanceledException e)
174+
catch (OperationCanceledException e)
175175
{
176176
exception = e;
177177
}

src/CommunityToolkit.Maui.UnitTests/Behaviors/CharactersValidationBehaviorTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task IsValid(CharacterType characterType, int minimumCharactersNumb
5050
entry.Behaviors.Add(behavior);
5151

5252
// Act
53-
await behavior.ForceValidate(CancellationToken.None);
53+
await behavior.ForceValidate(TestContext.Current.CancellationToken);
5454

5555
// Assert
5656
Assert.Equal(expectedValue, behavior.IsValid);
@@ -72,7 +72,7 @@ public async Task CancellationTokenExpired()
7272
// Act
7373

7474
// Ensure CancellationToken expires
75-
await Task.Delay(100, CancellationToken.None);
75+
await Task.Delay(100, TestContext.Current.CancellationToken);
7676

7777
// Assert
7878
await Assert.ThrowsAsync<OperationCanceledException>(async () => await behavior.ForceValidate(cts.Token));
@@ -94,7 +94,7 @@ public async Task CancellationTokenCanceled()
9494
// Act
9595

9696
// Ensure CancellationToken expires
97-
await Task.Delay(100, CancellationToken.None);
97+
await Task.Delay(100, TestContext.Current.CancellationToken);
9898

9999
// Assert
100100
await Assert.ThrowsAsync<OperationCanceledException>(async () =>

src/CommunityToolkit.Maui.UnitTests/Behaviors/EmailValidationBehaviorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public async Task IsValid(string value, bool expectedValue)
246246
entry.Behaviors.Add(behavior);
247247

248248
// Act
249-
await behavior.ForceValidate(CancellationToken.None);
249+
await behavior.ForceValidate(TestContext.Current.CancellationToken);
250250

251251
// Assert
252252
Assert.Equal(expectedValue, behavior.IsValid);
@@ -268,7 +268,7 @@ public async Task ForceValidateCancellationTokenExpired()
268268
// Act
269269

270270
// Ensure CancellationToken expires
271-
await Task.Delay(100, CancellationToken.None);
271+
await Task.Delay(100, TestContext.Current.CancellationToken);
272272

273273
// Assert
274274
await Assert.ThrowsAsync<OperationCanceledException>(async () => await behavior.ForceValidate(cts.Token));

src/CommunityToolkit.Maui.UnitTests/Behaviors/ImageTouchBehaviorTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public async Task VerifyPressedBackgroundImageChange()
7777

7878
Assert.Null(view.Source);
7979

80-
await imageTouchBehavior.ForceUpdateState(CancellationToken.None, false);
80+
await imageTouchBehavior.ForceUpdateState(TestContext.Current.CancellationToken, false);
8181
Assert.Equal(normalImageSource, view.Source);
8282
Assert.Same(normalImageSource, view.Source);
8383

@@ -106,7 +106,7 @@ public async Task VerifyPressedBackgroundImageAspectChange()
106106

107107
Assert.Equal(Aspect.AspectFit, view.Aspect);
108108

109-
await imageTouchBehavior.ForceUpdateState(CancellationToken.None, false);
109+
await imageTouchBehavior.ForceUpdateState(TestContext.Current.CancellationToken, false);
110110
Assert.Equal(Aspect.AspectFit, view.Aspect);
111111

112112
imageTouchBehavior.HandleTouch(TouchStatus.Started);
@@ -128,7 +128,7 @@ public async Task VerifyHoverBackgroundImageChange()
128128
imageTouchBehavior.DefaultImageSource = normalImageSource;
129129
imageTouchBehavior.HoveredImageSource = hoveredImageSource;
130130

131-
await imageTouchBehavior.ForceUpdateState(CancellationToken.None, false);
131+
await imageTouchBehavior.ForceUpdateState(TestContext.Current.CancellationToken, false);
132132
Assert.Same(normalImageSource, view.Source);
133133
Assert.Equal(normalImageSource, view.Source);
134134

@@ -155,7 +155,7 @@ public async Task VerifyHoverBackgroundImageAspectChange()
155155
imageTouchBehavior.HoveredImageSource = hoverImageSource;
156156
imageTouchBehavior.HoveredImageAspect = Aspect.AspectFill;
157157

158-
await imageTouchBehavior.ForceUpdateState(CancellationToken.None, false);
158+
await imageTouchBehavior.ForceUpdateState(TestContext.Current.CancellationToken, false);
159159
Assert.Equal(Aspect.AspectFit, view.Aspect);
160160

161161
imageTouchBehavior.HandleHover(HoverStatus.Entered);

0 commit comments

Comments
 (0)