public void AllowMultipleExecutionTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a, o => true) { AllowMultipleExecution = true }; EnqueueCallback(() => { asyncTestCommand.Execute(100); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); }); EnqueueWait(() => asyncTestCommand.executeTask.IsCompleted); EnqueueWait(() => !asyncTestCommand.IsExecuting); EnqueueCallback(() => { Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand = new AsyncCommand <int>(a, o => false) { AllowMultipleExecution = true }; asyncTestCommand.Execute(100); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.CanExecute(100)); }); EnqueueTestComplete(); }
public async Task ICommand_Parameter_CanExecuteChanged_AllowsMultipleExecutions_Test() { // Arrange var canExecuteChangedCount = 0; ICommand command = new AsyncCommand <int>(IntParameterTask); void handleCanExecuteChanged(object sender, EventArgs e) => canExecuteChangedCount++; // Act command.Execute(Delay); // Assert Assert.True(command.CanExecute(null)); // Act await IntParameterTask(Delay); await IntParameterTask(Delay); // Assert Assert.True(command.CanExecute(null)); Assert.Equal(0, canExecuteChangedCount); command.CanExecuteChanged += handleCanExecuteChanged; }
public async Task ICommand_NoParameter_CanExecuteChanged_DoesNotAllowMultipleExecutions_Test() { // Arrange var canExecuteChangedCount = 0; ICommand command = new AsyncCommand(() => IntParameterTask(Delay), allowsMultipleExecutions: false); command.CanExecuteChanged += handleCanExecuteChanged; void handleCanExecuteChanged(object sender, EventArgs e) => canExecuteChangedCount++; // Act command.Execute(null); // Assert Assert.False(command.CanExecute(null)); // Act await IntParameterTask(Delay); await IntParameterTask(Delay); // Assert Assert.True(command.CanExecute(null)); Assert.Equal(2, canExecuteChangedCount); }
public async Task ExecuteNotifiesCanExecuteChanged() { var count = 0; var tcs = new TaskCompletionSource <int>(); using (var command = new AsyncCommand(() => tcs.Task)) { command.CanExecuteChanged += (_, __) => count++; var isExecutingCount = 0; using (command.ObservePropertyChangedSlim(nameof(command.IsExecuting), signalInitial: false) .Subscribe(_ => isExecutingCount++)) { Assert.AreEqual(0, isExecutingCount); Assert.IsFalse(command.IsExecuting); Assert.IsFalse(command.CancelCommand.CanExecute()); command.Execute(); Assert.AreEqual(1, isExecutingCount); Assert.IsTrue(command.IsExecuting); Assert.IsFalse(command.CancelCommand.CanExecute()); Assert.AreEqual(1, count); tcs.SetResult(1); await command.Execution.Task.ConfigureAwait(false); Assert.AreEqual(2, isExecutingCount); Assert.IsFalse(command.IsExecuting); Assert.AreEqual(2, count); } } }
public async Task ICommand_ExecuteAsync_InvalidReferenceTypeParameter_Test() { //Arrange InvalidCommandParameterException actualInvalidCommandParameterException = null; InvalidCommandParameterException expectedInvalidCommandParameterException = new InvalidCommandParameterException(typeof(int), typeof(string)); ICommand command = new AsyncCommand <int>(IntParameterTask); //Act try { command.Execute("Hello World"); await NoParameterTask(); await NoParameterTask(); } catch (InvalidCommandParameterException e) { actualInvalidCommandParameterException = e; } //Assert Assert.IsNotNull(actualInvalidCommandParameterException); Assert.AreEqual(expectedInvalidCommandParameterException.Message, actualInvalidCommandParameterException.Message); }
public async Task ExecuteNotifiesTaskStatus() { // http://stackoverflow.com/questions/34811639/is-there-a-way-to-be-notified-when-task-status-changes-to-running var tcs = new TaskCompletionSource <int>(); using (var command = new AsyncCommand(() => tcs.Task)) { var taskStatuses = new List <TaskStatus>(); using (command.ObservePropertyChangedWithValue(x => x.Execution.Status) .Subscribe(x => taskStatuses.Add(x.EventArgs.Value))) { Assert.IsFalse(command.IsExecuting); Assert.IsFalse(command.CancelCommand.CanExecute()); command.Execute(); Assert.IsTrue(command.IsExecuting); Assert.IsFalse(command.CancelCommand.CanExecute()); var expectedStatuses = new List <TaskStatus> { TaskStatus.Created, TaskStatus.WaitingForActivation, TaskStatus.Running, }; CollectionAssert.AreEqual(expectedStatuses, taskStatuses); tcs.SetResult(1); await command.Execution.Task.ConfigureAwait(false); Assert.IsFalse(command.IsExecuting); expectedStatuses.Add(TaskStatus.RanToCompletion); CollectionAssert.AreEqual(expectedStatuses, taskStatuses); } } }
void AllowMultipleExecutionTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a, o => true) { AllowMultipleExecution = true }; asyncTestCommand.Execute(100); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand.Wait(latencyTime); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand = new AsyncCommand <int>(a, o => false) { AllowMultipleExecution = true }; asyncTestCommand.Execute(100); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.CanExecute(100)); }
public async Task ICommand_NoParameter_CanExecuteChanged_AllowsMultipleExecutions_Test() { // Arrange var canExecuteChangedCount = 0; ICommand command = new AsyncCommand(() => IntParameterTask(Delay)); command.CanExecuteChanged += handleCanExecuteChanged; void handleCanExecuteChanged(object?sender, EventArgs e) => canExecuteChangedCount++; // Act command.Execute(null); // Assert Assert.IsTrue(command.CanExecute(null)); // Act await IntParameterTask(Delay).ConfigureAwait(false); await IntParameterTask(Delay).ConfigureAwait(false); // Assert Assert.IsTrue(command.CanExecute(null)); Assert.AreEqual(0, canExecuteChangedCount); command.CanExecuteChanged -= handleCanExecuteChanged; }
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed void TestExecuteOnCanExecuteBehavior(bool shouldExecute) { bool executed = false; ICommand asyncCommand = new AsyncCommand(() => Task.Run(() => { executed = true; }), () => shouldExecute); asyncCommand.Execute(null); Assert.That(executed == shouldExecute, Is.EqualTo(executed == shouldExecute).After(delayInMilliseconds: 10, pollingInterval: 1)); }
public void CanExecuteDefaultActionNoCoalescingTest() { ICommand asyncCommand = new AsyncCommand(() => Task.Run(async() => { await _tcs.Task; })); Assert.IsTrue(asyncCommand.CanExecute(null)); asyncCommand.Execute(null); Assert.IsTrue(asyncCommand.CanExecute(null)); }
public void CanExecuteReturnsFalseWhenTaskIsRunning() { var command = new AsyncCommand(async() => await Task.Delay(500)); command.Execute(null); Assert.That(command.CanExecute(null), Is.False); }
public void ExecuteCanceled() { var tcs = new TaskCompletionSource <int>(); tcs.SetCanceled(); using var command = new AsyncCommand(() => tcs.Task); command.Execute(); Assert.AreSame(tcs.Task, command.Execution !.Task); }
public void CommandExecutesCommandTest() { bool called = false; var command = new AsyncCommand(async x => called = true); command.Execute(null); Assert.That(called, Is.True); }
public void CancelAndWaitTest2() { asyncTestCommand = new AsyncCommand <int>(CancelInsideCommandAndWaitMethod); executingAsyncMethod = true; asyncTestCommand.Execute(100); asyncTestCommand.Wait(); Assert.AreEqual(false, executingAsyncMethod); EnqueueTestComplete(); }
public void IAsyncCommand_Execute_InvalidReferenceParameter() { // Arrange IAsyncCommand <int, bool> command = new AsyncCommand <int, bool>(IntParameterTask, CanExecuteTrue); // Act // Assert Assert.Throws <InvalidCommandParameterException>(() => command.Execute("Hello World")); }
public void IAsyncCommand_Execute_InvalidValueTypeParameter() { // Arrange IAsyncCommand <string, bool> command = new AsyncCommand <string, bool>(StringParameterTask, CanExecuteTrue); // Act // Assert Assert.Throws <InvalidCommandParameterException>(() => command.Execute(true)); }
public async Task ICommand_Execute_StringParameter_Test(string parameter) { //Arrange ICommand command = new AsyncCommand <string>(StringParameterTask); //Act command.Execute(parameter); await NoParameterTask(); //Assert }
public async Task ICommand_Execute_NullableParameter_Test(int?parameter) { // Arrange ICommand command = new AsyncCommand <int?>(NullableParameterTask); // Act command.Execute(parameter); await NoParameterTask().ConfigureAwait(false); // Assert }
public async Task ICommand_Execute_IntParameter_Test(int parameter) { //Arrange ICommand command = new AsyncCommand <int>(IntParameterTask); //Act command.Execute(parameter); await NoParameterTask(); //Assert }
public void CanCancel() { using var command = new AsyncCommand <int>((i, x) => x.AsObservable().FirstAsync().ToTask()); Assert.IsTrue(command.CanExecute(0)); Assert.IsFalse(command.CancelCommand.CanExecute()); command.Execute(0); Assert.IsTrue(command.CancelCommand.CanExecute()); command.CancelCommand.Execute(); Assert.IsFalse(command.CancelCommand.CanExecute()); Assert.IsInstanceOf <Condition>(command.Condition); }
public void WaitTest() { asyncTestCommand = new AsyncCommand <int>((a) => AsyncExecuteMethod(a)); executingAsyncMethod = true; asyncTestCommand.Execute(100); asyncTestCommand.Wait(); Assert.AreEqual(false, executingAsyncMethod); asyncTestCommand.Wait(); Assert.AreEqual(false, executingAsyncMethod); EnqueueTestComplete(); }
public void ExecuteWithoutParameterTest() { bool executeFired = false; bool canExecute = false; var command = new AsyncCommand(() => { executeFired = true; return(Task.CompletedTask); }, () => canExecute); Assert.False(executeFired); command.Execute(new object()); Assert.False(executeFired); canExecute = true; command.Execute(new object()); Assert.True(executeFired); }
// Contructor for DesignTime Only public CompetitionViewModel() { _competition = new Competition() { Id = 11, Name = "MyCompetition" }; _dataService = SimpleIoc.Default.GetInstance <IFootballDataService>(); GetCurrentStandingsCommand = new AsyncCommand <List <Team> >(() => _dataService.GetCurrentStandingsAsync(_competition.Id)); GetTodayFixturesCommand = new AsyncCommand <List <Match> >(() => _dataService.GetFixturesForDateAsync(_competition.Id, DateTime.Today)); GetTodayFixturesCommand.Execute(null); GetCurrentStandingsCommand.Execute(null); }
public async Task CannotExecuteWhileRunning() { using var resetEvent = new ManualResetEventSlim(); using var command = new AsyncCommand(() => Task.Run(() => resetEvent.Wait())); Assert.IsTrue(command.CanExecute()); command.Execute(); Assert.IsFalse(command.CanExecute()); resetEvent.Set(); await command.Execution !.Task.ConfigureAwait(false); Assert.IsTrue(command.CanExecute()); }
public void CanExecute_False() { var command = new AsyncCommand() { Action = (p, _) => Task.Delay(100, _), CanExecute = p => false }; command.Execute(); Assert.False(command.IsRunning); Assert.Null(command.Task); }
public void ExecuteDoesNotExecuteTheActionIfCanExecuteIsFalse() { var run = false; var command = new AsyncCommand <object>(async o => { await AsyncMethod(() => run = true); }, o => false); command.Execute(null); run.Should().BeFalse(); }
public async Task ExecuteFinished() { var finished = Task.FromResult(1); var command = new AsyncCommand <int>(x => finished); Assert.IsTrue(command.CanExecute(0)); command.Execute(0); await command.Execution.Task.ConfigureAwait(false); Assert.IsTrue(command.CanExecute(0)); Assert.AreSame(finished, command.Execution.Task); Assert.AreSame(finished, command.Execution.Completed); }
public void CommandExecutesAction() { var value = 0; Task action() { value = 1; return(Task.CompletedTask); }; var command = new AsyncCommand(action); command.Execute(null); Assert.AreEqual(1, value); }
public void CanCancel() { var tcs = new TaskCompletionSource <int>(); using var command = new AsyncCommand(x => tcs.Task); Assert.IsTrue(command.CanExecute()); Assert.IsFalse(command.CancelCommand.CanExecute()); command.Execute(); Assert.IsTrue(command.CancelCommand.CanExecute()); command.CancelCommand.Execute(); Assert.IsFalse(command.CancelCommand.CanExecute()); Assert.IsInstanceOf <Condition>(command.Condition); }
public void ExecuteWithParam_CorrectExecutionWithCanExecuteChanged() { // Arrange var executed = false; Task Execute() { executed = true; return(Task.CompletedTask); } bool CanExecute() => true; var errorHandler = new ErrorHandlerTestImplementation((cmd, exception) => { }); IAsyncCommand command = new AsyncCommand(Execute, CanExecute, errorHandler); var invokedSenderList = new List <object>(); var invokedArgsList = new List <EventArgs>(); var invokedCanExecuteStateList = new List <bool>(); command.CanExecuteChanged += (sender, args) => { if (sender != null) { invokedSenderList.Add(sender); } if (args != null) { invokedArgsList.Add(args); } invokedCanExecuteStateList.Add(command.CanExecute()); }; // Act command.Execute(null); // Assert Assert.IsTrue(executed); Assert.AreEqual(2, invokedSenderList.Count); Assert.AreSame(command, invokedSenderList[0]); Assert.AreSame(command, invokedSenderList[1]); Assert.AreEqual(2, invokedArgsList.Count); Assert.AreSame(EventArgs.Empty, invokedArgsList[0]); Assert.AreSame(EventArgs.Empty, invokedArgsList[1]); Assert.AreEqual(2, invokedCanExecuteStateList.Count); Assert.IsFalse(invokedCanExecuteStateList[0]); // Cannot execute during first execution, even when the CanExecute delegate returns true. Assert.IsTrue(invokedCanExecuteStateList[1]); // Can execute after the execution has finished. }
public void ExecuteShouldInvokeCallback() { // arrange var execute = new Mock<Func<object, Task>>(); execute.Setup( f => f( It.IsAny<object>() ) ).Returns( Task.FromResult( 0 ) ); var command = new AsyncCommand<object>( execute.Object ); var executed = false; command.Executed += ( s, e ) => executed = true; // act command.Execute(); // assert Assert.True( executed ); execute.Verify( f => f( null ), Times.Once() ); }