1
0
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:
Sergio Pedri 2020-08-22 01:16:29 +02:00
parent 134ba4bc31
commit 23c6a90559

View File

@ -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);
}
}
}