Skip to content

Commit d776a1e

Browse files
committed
Add unit tests for Execute raising CanExecuteChanged if needed
1 parent 3c71d4b commit d776a1e

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,97 @@ async void TestCallback(Action throwAction, Action completeAction)
325325

326326
Assert.IsTrue(success);
327327
}
328+
329+
// See https://github.com/CommunityToolkit/dotnet/issues/108
330+
[TestMethod]
331+
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged()
332+
{
333+
TaskCompletionSource<object?> tcs = new();
334+
335+
AsyncRelayCommand command = new(() => tcs.Task, allowConcurrentExecutions: true);
336+
337+
(object? Sender, EventArgs? Args) args = default;
338+
339+
command.CanExecuteChanged += (s, e) => args = (s, e);
340+
341+
Assert.IsTrue(command.CanExecute(null));
342+
343+
command.Execute(null);
344+
345+
Assert.IsNull(args.Sender);
346+
Assert.IsNull(args.Args);
347+
348+
Assert.IsTrue(command.CanExecute(null));
349+
350+
tcs.SetResult(null);
351+
}
352+
353+
[TestMethod]
354+
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged()
355+
{
356+
TaskCompletionSource<object?> tcs = new();
357+
358+
AsyncRelayCommand command = new(() => tcs.Task, allowConcurrentExecutions: false);
359+
360+
(object? Sender, EventArgs? Args) args = default;
361+
362+
command.CanExecuteChanged += (s, e) => args = (s, e);
363+
364+
Assert.IsTrue(command.CanExecute(null));
365+
366+
command.Execute(null);
367+
368+
Assert.AreSame(command, args.Sender);
369+
Assert.AreSame(EventArgs.Empty, args.Args);
370+
371+
Assert.IsFalse(command.CanExecute(null));
372+
373+
tcs.SetResult(null);
374+
}
375+
376+
[TestMethod]
377+
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged_WithCancellation()
378+
{
379+
TaskCompletionSource<object?> tcs = new();
380+
381+
AsyncRelayCommand command = new(token => tcs.Task, allowConcurrentExecutions: true);
382+
383+
(object? Sender, EventArgs? Args) args = default;
384+
385+
command.CanExecuteChanged += (s, e) => args = (s, e);
386+
387+
Assert.IsTrue(command.CanExecute(null));
388+
389+
command.Execute(null);
390+
391+
Assert.IsNull(args.Sender);
392+
Assert.IsNull(args.Args);
393+
394+
Assert.IsTrue(command.CanExecute(null));
395+
396+
tcs.SetResult(null);
397+
}
398+
399+
[TestMethod]
400+
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged_WithToken()
401+
{
402+
TaskCompletionSource<object?> tcs = new();
403+
404+
AsyncRelayCommand command = new(token => tcs.Task, allowConcurrentExecutions: false);
405+
406+
(object? Sender, EventArgs? Args) args = default;
407+
408+
command.CanExecuteChanged += (s, e) => args = (s, e);
409+
410+
Assert.IsTrue(command.CanExecute(null));
411+
412+
command.Execute(null);
413+
414+
Assert.AreSame(command, args.Sender);
415+
Assert.AreSame(EventArgs.Empty, args.Args);
416+
417+
Assert.IsFalse(command.CanExecute(null));
418+
419+
tcs.SetResult(null);
420+
}
328421
}

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,95 @@ async void TestCallback(Action throwAction, Action completeAction)
223223

224224
Assert.IsTrue(success);
225225
}
226+
227+
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged()
228+
{
229+
TaskCompletionSource<object?> tcs = new();
230+
231+
AsyncRelayCommand<string> command = new(s => tcs.Task, allowConcurrentExecutions: true);
232+
233+
(object? Sender, EventArgs? Args) args = default;
234+
235+
command.CanExecuteChanged += (s, e) => args = (s, e);
236+
237+
Assert.IsTrue(command.CanExecute(""));
238+
239+
command.Execute("");
240+
241+
Assert.IsNull(args.Sender);
242+
Assert.IsNull(args.Args);
243+
244+
Assert.IsTrue(command.CanExecute(""));
245+
246+
tcs.SetResult(null);
247+
}
248+
249+
[TestMethod]
250+
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged()
251+
{
252+
TaskCompletionSource<object?> tcs = new();
253+
254+
AsyncRelayCommand<string> command = new(s => tcs.Task, allowConcurrentExecutions: false);
255+
256+
(object? Sender, EventArgs? Args) args = default;
257+
258+
command.CanExecuteChanged += (s, e) => args = (s, e);
259+
260+
Assert.IsTrue(command.CanExecute(""));
261+
262+
command.Execute("");
263+
264+
Assert.AreSame(command, args.Sender);
265+
Assert.AreSame(EventArgs.Empty, args.Args);
266+
267+
Assert.IsFalse(command.CanExecute(""));
268+
269+
tcs.SetResult(null);
270+
}
271+
272+
[TestMethod]
273+
public void Test_AsyncRelayCommand_ExecuteDoesNotRaisesCanExecuteChanged_WithCancellation()
274+
{
275+
TaskCompletionSource<object?> tcs = new();
276+
277+
AsyncRelayCommand<string> command = new((s, token) => tcs.Task, allowConcurrentExecutions: true);
278+
279+
(object? Sender, EventArgs? Args) args = default;
280+
281+
command.CanExecuteChanged += (s, e) => args = (s, e);
282+
283+
Assert.IsTrue(command.CanExecute(""));
284+
285+
command.Execute("");
286+
287+
Assert.IsNull(args.Sender);
288+
Assert.IsNull(args.Args);
289+
290+
Assert.IsTrue(command.CanExecute(""));
291+
292+
tcs.SetResult(null);
293+
}
294+
295+
[TestMethod]
296+
public void Test_AsyncRelayCommand_ExecuteWithoutConcurrencyRaisesCanExecuteChanged_WithToken()
297+
{
298+
TaskCompletionSource<object?> tcs = new();
299+
300+
AsyncRelayCommand<string> command = new((s, token) => tcs.Task, allowConcurrentExecutions: false);
301+
302+
(object? Sender, EventArgs? Args) args = default;
303+
304+
command.CanExecuteChanged += (s, e) => args = (s, e);
305+
306+
Assert.IsTrue(command.CanExecute(""));
307+
308+
command.Execute("");
309+
310+
Assert.AreSame(command, args.Sender);
311+
Assert.AreSame(EventArgs.Empty, args.Args);
312+
313+
Assert.IsFalse(command.CanExecute(""));
314+
315+
tcs.SetResult(null);
316+
}
226317
}

0 commit comments

Comments
 (0)