|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
5 | 5 | using System;
|
| 6 | +using System.Collections.Generic; |
| 7 | +using System.ComponentModel; |
6 | 8 | using System.Threading.Tasks;
|
7 | 9 | using CommunityToolkit.Mvvm.Input;
|
8 | 10 | using CommunityToolkit.Mvvm.UnitTests.Helpers;
|
@@ -134,6 +136,50 @@ public void Test_AsyncRelayCommandOfT_NullWithValueType()
|
134 | 136 | _ = Assert.ThrowsException<NullReferenceException>(() => command.Execute(null));
|
135 | 137 | }
|
136 | 138 |
|
| 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 | + |
137 | 183 | [TestMethod]
|
138 | 184 | public async Task Test_AsyncRelayCommandOfT_AllowConcurrentExecutions_Disabled()
|
139 | 185 | {
|
|
0 commit comments