mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-04-13 20:15:45 +02:00
Added unit tests for cancelable async commands
This commit is contained in:
parent
134ba4bc31
commit
23c6a90559
@ -28,6 +28,9 @@ public async Task Test_AsyncRelayCommand_AlwaysEnabled()
|
||||
Assert.IsTrue(command.CanExecute(null));
|
||||
Assert.IsTrue(command.CanExecute(new object()));
|
||||
|
||||
Assert.IsFalse(command.CanBeCanceled);
|
||||
Assert.IsFalse(command.IsCancellationRequested);
|
||||
|
||||
(object, EventArgs) args = default;
|
||||
|
||||
command.CanExecuteChanged += (s, e) => args = (s, e);
|
||||
@ -75,6 +78,9 @@ public void Test_AsyncRelayCommand_WithCanExecuteFunctionTrue()
|
||||
Assert.IsTrue(command.CanExecute(null));
|
||||
Assert.IsTrue(command.CanExecute(new object()));
|
||||
|
||||
Assert.IsFalse(command.CanBeCanceled);
|
||||
Assert.IsFalse(command.IsCancellationRequested);
|
||||
|
||||
command.Execute(null);
|
||||
|
||||
Assert.AreEqual(ticks, 1);
|
||||
@ -100,6 +106,9 @@ public void Test_AsyncRelayCommand_WithCanExecuteFunctionFalse()
|
||||
Assert.IsFalse(command.CanExecute(null));
|
||||
Assert.IsFalse(command.CanExecute(new object()));
|
||||
|
||||
Assert.IsFalse(command.CanBeCanceled);
|
||||
Assert.IsFalse(command.IsCancellationRequested);
|
||||
|
||||
command.Execute(null);
|
||||
|
||||
Assert.AreEqual(ticks, 0);
|
||||
@ -108,5 +117,34 @@ public void Test_AsyncRelayCommand_WithCanExecuteFunctionFalse()
|
||||
|
||||
Assert.AreEqual(ticks, 0);
|
||||
}
|
||||
|
||||
[TestCategory("Mvvm")]
|
||||
[TestMethod]
|
||||
public async Task Test_AsyncRelayCommand_WithCancellation()
|
||||
{
|
||||
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
|
||||
|
||||
var command = new AsyncRelayCommand(token => tcs.Task);
|
||||
|
||||
Assert.IsTrue(command.CanExecute(null));
|
||||
Assert.IsTrue(command.CanExecute(new object()));
|
||||
|
||||
Assert.IsTrue(command.CanBeCanceled);
|
||||
Assert.IsFalse(command.IsCancellationRequested);
|
||||
|
||||
command.Execute(null);
|
||||
|
||||
Assert.IsFalse(command.IsCancellationRequested);
|
||||
|
||||
command.Cancel();
|
||||
|
||||
Assert.IsTrue(command.IsCancellationRequested);
|
||||
|
||||
tcs.SetResult(null);
|
||||
|
||||
await command.ExecutionTask!;
|
||||
|
||||
Assert.IsTrue(command.IsCancellationRequested);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user