Skip to content

Commit 9c0a1b3

Browse files
committed
Minor bug fixes, add more unit tests
1 parent c9f5b9f commit 9c0a1b3

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

CommunityToolkit.Mvvm/Input/AsyncRelayCommand.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ public Task ExecuteAsync(object? parameter)
292292

293293
CancellationTokenSource cancellationTokenSource = this.cancellationTokenSource = new();
294294

295-
PropertyChanged?.Invoke(this, IsCancellationRequestedChangedEventArgs);
296-
297295
// Invoke the cancelable command delegate with a new linked token
298296
executionTask = ExecutionTask = this.cancelableExecute!(cancellationTokenSource.Token);
299297
}

CommunityToolkit.Mvvm/Input/AsyncRelayCommand{T}.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,6 @@ public Task ExecuteAsync(T? parameter)
295295

296296
CancellationTokenSource cancellationTokenSource = this.cancellationTokenSource = new();
297297

298-
PropertyChanged?.Invoke(this, AsyncRelayCommand.IsCancellationRequestedChangedEventArgs);
299-
300298
// Invoke the cancelable command delegate with a new linked token
301299
executionTask = ExecutionTask = this.cancelableExecute!(parameter, cancellationTokenSource.Token);
302300
}

tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand{T}.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.ComponentModel;
68
using System.Threading.Tasks;
79
using CommunityToolkit.Mvvm.Input;
810
using CommunityToolkit.Mvvm.UnitTests.Helpers;
@@ -134,6 +136,50 @@ public void Test_AsyncRelayCommandOfT_NullWithValueType()
134136
_ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
135137
}
136138

139+
[TestMethod]
140+
public async Task Test_AsyncRelayCommandOfT_WithCancellation()
141+
{
142+
// See comments in Test_AsyncRelayCommand_WithCancellation for the logic below
143+
TaskCompletionSource<object?> tcs = new();
144+
AsyncRelayCommand<string> command = new((s, token) => tcs.Task);
145+
146+
List<PropertyChangedEventArgs> args = new();
147+
148+
command.PropertyChanged += (s, e) => args.Add(e);
149+
150+
Assert.IsTrue(command.CanExecute(null));
151+
Assert.IsTrue(command.CanExecute("Hello"));
152+
153+
Assert.IsFalse(command.CanBeCanceled);
154+
Assert.IsFalse(command.IsCancellationRequested);
155+
156+
command.Execute(null);
157+
158+
Assert.IsTrue(command.CanBeCanceled);
159+
Assert.IsFalse(command.IsCancellationRequested);
160+
161+
Assert.AreEqual(args.Count, 4);
162+
Assert.AreEqual(args[0].PropertyName, nameof(IAsyncRelayCommand.ExecutionTask));
163+
Assert.AreEqual(args[1].PropertyName, nameof(IAsyncRelayCommand.IsRunning));
164+
Assert.AreEqual(args[2].PropertyName, nameof(IAsyncRelayCommand.CanBeCanceled));
165+
Assert.AreEqual(args[3].PropertyName, nameof(IAsyncRelayCommand.IsCancellationRequested));
166+
167+
command.Cancel();
168+
169+
Assert.AreEqual(args.Count, 6);
170+
Assert.AreEqual(args[4].PropertyName, nameof(IAsyncRelayCommand.CanBeCanceled));
171+
Assert.AreEqual(args[5].PropertyName, nameof(IAsyncRelayCommand.IsCancellationRequested));
172+
173+
Assert.IsTrue(command.IsCancellationRequested);
174+
175+
tcs.SetResult(null);
176+
177+
await command.ExecutionTask!;
178+
179+
Assert.IsFalse(command.CanBeCanceled);
180+
Assert.IsTrue(command.IsCancellationRequested);
181+
}
182+
137183
[TestMethod]
138184
public async Task Test_AsyncRelayCommandOfT_AllowConcurrentExecutions_Disabled()
139185
{

0 commit comments

Comments
 (0)