コード例 #1
0
 public void AsyncIsExecuting()
 {
     asyncTestCommand = new AsyncCommand <int>(a => AsyncExecuteMethod(a));
     Assert.IsFalse(asyncTestCommand.IsExecuting);
     Assert.IsTrue(asyncTestCommand.CanExecute(100));
     executingAsyncMethod = true;
     EnqueueCallback(() => {
         asyncTestCommand.Execute(100);
         Assert.IsTrue(asyncTestCommand.IsExecuting);
         Assert.IsFalse(asyncTestCommand.CanExecute(100));
     });
     EnqueWaitForAsync(asyncTestCommand.executeTask);
     EnqueueWait(() => !asyncTestCommand.IsExecuting);
     EnqueueWindowUpdateLayout();
     Assert.IsFalse(asyncTestCommand.IsExecuting);
     Assert.IsTrue(asyncTestCommand.CanExecute(100));
     EnqueueConditional(() => !executingAsyncMethod);
     EnqueueTestComplete();
 }
コード例 #2
0
        public void AsyncCommand_Parameter_CanExecuteTrue_Test()
        {
            //Arrange
            AsyncCommand <int> command = new AsyncCommand <int>(IntParameterTask, canExecute: CanExecuteTrue);

            //Act

            //Assert
            Assert.IsTrue(command.CanExecute(null));
        }
コード例 #3
0
        public void IAsyncCommand_Parameter_CanExecuteFalse_Test()
        {
            //Arrange
            IAsyncCommand <int> command = new AsyncCommand <int>(IntParameterTask, CanExecuteFalse);

            //Act

            //Assert
            Assert.False(command.CanExecute(null));
        }
コード例 #4
0
        public void AsyncCommand_NoParameter_CanExecuteTrue_Test()
        {
            // Arrange
            var command = new AsyncCommand(NoParameterTask, parameter => CanExecuteTrue(parameter));

            // Act

            // Assert
            Assert.IsTrue(command.CanExecute(null));
        }
コード例 #5
0
        public void AsyncCommand_NoParameter_CanExecuteFalse_NoParameter_Test()
        {
            // Arrange
            var command = new AsyncCommand(NoParameterTask, () => CanExecuteFalse());

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
        }
コード例 #6
0
        public void AsyncCommand_Parameter_CanExecuteFalse_Test()
        {
            // Arrange
            var command = new AsyncCommand <int>(IntParameterTask, parameter => CanExecuteFalse(parameter));

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
        }
コード例 #7
0
        public void ICommand_Parameter_CanExecuteTrue_NoParameter_Test()
        {
            // Arrange
            ICommand command = new AsyncCommand <int>(IntParameterTask, () => CanExecuteTrue());

            // Act

            // Assert
            Assert.IsTrue(command.CanExecute(null));
        }
コード例 #8
0
        public async Task ICommand_NoParameter_CanExecuteChanged_DoesNotAllowMultipleExecutions_Test()
        {
            // Arrange
            var semaphoreSlim              = new SemaphoreSlim(1, 1);
            var canExecuteChangedCount     = 0;
            var handleCanExecuteChangedTCS = new TaskCompletionSource <int>();

            ICommand command = new AsyncCommand(() => IntParameterTask(Delay), allowsMultipleExecutions: false);

            command.CanExecuteChanged += handleCanExecuteChanged;

            async void handleCanExecuteChanged(object?sender, EventArgs e)
            {
                await semaphoreSlim.WaitAsync().ConfigureAwait(false);

                try
                {
                    if (++canExecuteChangedCount is 2)
                    {
                        command.CanExecuteChanged -= handleCanExecuteChanged;
                        handleCanExecuteChangedTCS.SetResult(canExecuteChangedCount);
                    }
                }
                finally
                {
                    semaphoreSlim.Release();
                }
            }

            // Act
            command.Execute(null);

            // Assert
            Assert.False(command.CanExecute(null));

            // Act
            var handleCanExecuteChangedREsult = await handleCanExecuteChangedTCS.Task.ConfigureAwait(false);

            // Assert
            Assert.IsTrue(command.CanExecute(null));
            Assert.AreEqual(2, canExecuteChangedCount);
            Assert.AreEqual(canExecuteChangedCount, handleCanExecuteChangedREsult);
        }
コード例 #9
0
        public void IAsyncCommand_NoParameter_CanExecuteTrue_Test()
        {
            // Arrange
            IAsyncCommand command = new AsyncCommand(NoParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
        }
コード例 #10
0
        public void AsyncCommand_NoParameter_CanExecuteFalse_Test()
        {
            //Arrange
            AsyncCommand command = new AsyncCommand(NoParameterTask, canExecute: CanExecuteFalse);

            //Act

            //Assert
            Assert.False(command.CanExecute(null));
        }
コード例 #11
0
        public void IAsyncCommand_CanExecute_InvalidValueTypeParameter()
        {
            // Arrange
            IAsyncCommand <int, string> command = new AsyncCommand <int, string>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.Throws <InvalidCommandParameterException>(() => command.CanExecute(true));
        }
コード例 #12
0
        public void IAsyncCommand_CanExecute_InvalidReferenceParameter()
        {
            // Arrange
            IAsyncCommand <int, bool> command = new AsyncCommand <int, bool>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.Throws <InvalidCommandParameterException>(() => command.CanExecute("Hello World"));
        }
コード例 #13
0
        public void IAsyncCommand_NoParameter_CanExecuteFalse_Test()
        {
            // Arrange
            IAsyncCommand command = new AsyncCommand(NoParameterTask, parameter => CanExecuteFalse(parameter));

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
        }
コード例 #14
0
 public void CanExecuteCondition(bool expected)
 {
     using (var condition = Mock.Of <ICondition>(x => x.IsSatisfied == expected))
     {
         using (var command = new AsyncCommand(() => Task.FromResult(1), condition))
         {
             Assert.AreEqual(2, ((AndCondition)command.Condition).Prerequisites.Count);
             Assert.AreEqual(expected, command.CanExecute());
         }
     }
 }
コード例 #15
0
        public void WithTwoConditions()
        {
            var mock1 = new Mock <ICondition>(MockBehavior.Strict);

            mock1.SetupGet(x => x.IsSatisfied).Returns(true);

            var mock2 = new Mock <ICondition>(MockBehavior.Strict);

            mock2.SetupGet(x => x.IsSatisfied).Returns(true);
            using (var command = new AsyncCommand(() => Task.Run(() => { }), mock1.Object, mock2.Object))
            {
                Assert.IsTrue(command.CanExecute());

                mock2.SetupGet(x => x.IsSatisfied).Returns(false);
                Assert.IsFalse(command.CanExecute());
            }

            mock1.Verify(x => x.Dispose(), Times.Never);
            mock2.Verify(x => x.Dispose(), Times.Never);
        }
コード例 #16
0
        public void AsyncCommand_NoParameter_NoCanExecute_Test()
        {
            // Arrange
            Func <bool> canExecute = null;
            var         command    = new AsyncCommand(NoParameterTask, canExecute);

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
        }
コード例 #17
0
 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);
 }
コード例 #18
0
        void AsyncCanExecuteTestCore(Func <int, Task> a)
        {
            asyncTestCommand = new AsyncCommand <int>(a, o => true);

            asyncTestCommand.Execute(100);
            Assert.IsTrue(asyncTestCommand.IsExecuting);
            Assert.IsFalse(asyncTestCommand.CanExecute(100));
            asyncTestCommand.Cancel();
            Assert.IsTrue(asyncTestCommand.IsExecuting);
            Assert.IsTrue(asyncTestCommand.ShouldCancel);
            Assert.IsTrue(asyncTestCommand.IsCancellationRequested);

            asyncTestCommand.Wait(latencyTime);
            Assert.IsTrue(asyncTestCommand.executeTask.IsCompleted);
            Assert.IsFalse(asyncTestCommand.IsExecuting);
            Assert.IsTrue(asyncTestCommand.CanExecute(100));
            asyncTestCommand = new AsyncCommand <int>(a, o => false);
            asyncTestCommand.Execute(100);
            Assert.IsFalse(asyncTestCommand.IsExecuting);
            Assert.IsFalse(asyncTestCommand.CanExecute(100));
        }
コード例 #19
0
        public void IAsyncCommand_Parameter_CanExecuteTrue_Test()
        {
            // Arrange
            IAsyncCommand <int>       command  = new AsyncCommand <int>(IntParameterTask, parameter => CanExecuteTrue(parameter));
            IAsyncCommand <int, bool> command2 = new AsyncCommand <int, bool>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
            Assert.True(command2.CanExecute(true));
        }
コード例 #20
0
        public void IAsyncCommand_Parameter_CanExecuteFalse_Test()
        {
            // Arrange
            IAsyncCommand <int>         command  = new AsyncCommand <int>(IntParameterTask, parameter => CanExecuteFalse(parameter));
            IAsyncCommand <int, string> command2 = new AsyncCommand <int, string>(IntParameterTask, CanExecuteFalse);

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
            Assert.False(command2.CanExecute("Hello World"));
        }
コード例 #21
0
        public void CanExecuteWithoutParameterTest()
        {
            bool canExecuteFired = false;
            bool canExecute      = false;

            var command = new AsyncCommand(() => Task.CompletedTask, () =>
            {
                canExecuteFired = true;
                return(canExecute);
            });

            Assert.False(canExecuteFired);

            Assert.False(command.CanExecute(new object()));
            Assert.True(canExecuteFired);

            canExecuteFired = false;
            canExecute      = true;

            Assert.True(command.CanExecute(new object()));
            Assert.True(canExecuteFired);
        }
コード例 #22
0
        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);
        }
コード例 #23
0
        public async Task ExecuteAsync_CorrectExecutionWithCanExecuteChanged_Async()
        {
            // 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
            await command.ExecuteAsync();

            // 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.
        }
コード例 #24
0
        public void AsyncCommand_NoParameter_NoCanExecute_Test()
        {
            // Arrange
            Func <bool>?canExecute = null;

#pragma warning disable CS8604 // Possible null reference argument.
            var command = new AsyncCommand(NoParameterTask, canExecute);
#pragma warning restore CS8604 // Possible null reference argument.

            // Act

            // Assert
            Assert.IsTrue(command.CanExecute(null));
        }
コード例 #25
0
        public void ExecuteThrows()
        {
            var exception = new Exception();

            using (var command = new AsyncCommand <int>(x => Task.Run(() => { throw exception; })))
            {
                command.Execute(0);
                Assert.ThrowsAsync <Exception>(() => command.Execution.Task);

                Assert.AreEqual(exception, command.Execution.InnerException);
                Assert.AreEqual(TaskStatus.Faulted, command.Execution.Status);
                Assert.AreEqual(true, command.CanExecute(0));
            }
        }
コード例 #26
0
        public void CanExecuteIsFalseIfCommandIsExecuting()
        {
            bool         CanExecute = true;
            AsyncCommand command    = null;

            Task action()
            {
                CanExecute = command.CanExecute(null); return(Task.CompletedTask);
            };
            command = new AsyncCommand(action);
            command.Execute(null);

            Assert.IsFalse(CanExecute);
        }
コード例 #27
0
        public void CanExecuteShouldInvokeCallback()
        {
            // arrange
            var canExecute = new Mock<Func<object, bool>>();

            canExecute.Setup( f => f( It.IsAny<object>() ) ).Returns( true );

            var command = new AsyncCommand<object>( p => Task.FromResult( 0 ), canExecute.Object );

            // act
            command.CanExecute();

            // assert
            canExecute.Verify( f => f( null ), Times.Once() );
        }
コード例 #28
0
        public void CannotExecuteWhileCommandIsRunningWithoutParameterTest()
        {
            using (var runningSemaphore = new SemaphoreSlim(0))
                using (var completionSemaphore = new SemaphoreSlim(0))
                {
                    var command = new AsyncCommand(async() =>
                    {
                        runningSemaphore.Release();
                        await completionSemaphore.WaitAsync().ConfigureAwait(false);
                    });

                    Assert.True(command.CanExecute(new object()));

                    command.Execute(new object());
                    runningSemaphore.Wait();

                    Assert.False(command.CanExecute(new object()));

                    completionSemaphore.Release();
                    command.Execution.TaskCompleted.Wait();

                    Assert.True(command.CanExecute(new object()));
                }
        }
コード例 #29
0
        public async Task TestCommand_With_Condition()
        {
            bool isCalled   = false;
            bool canExecute = false;

            var command = new AsyncCommand(async() =>
            {
                await Task.Delay(500);

                isCalled = true;
            }, () => canExecute);

            await command.ExecuteAsync();

            Assert.AreEqual(false, command.CanExecute(null));
            Assert.AreEqual(false, isCalled);

            canExecute = true;

            await command.ExecuteAsync();

            Assert.AreEqual(true, command.CanExecute(null));
            Assert.AreEqual(true, isCalled);
        }
コード例 #30
0
 public void AsyncCanExecuteTestCore(Func <int, Task> a)
 {
     asyncTestCommand = new AsyncCommand <int>(a, o => true);
     EnqueueCallback(() => {
         asyncTestCommand.Execute(100);
         Assert.IsTrue(asyncTestCommand.IsExecuting);
         Assert.IsFalse(asyncTestCommand.CanExecute(100));
         asyncTestCommand.Cancel();
         Assert.IsTrue(asyncTestCommand.IsExecuting);
         Assert.IsTrue(asyncTestCommand.ShouldCancel);
         Assert.IsTrue(asyncTestCommand.IsCancellationRequested);
     });
     EnqueueWait(() => asyncTestCommand.executeTask.IsCompleted);
     EnqueueWait(() => !asyncTestCommand.IsExecuting);
     EnqueueCallback(() => {
         Assert.IsFalse(asyncTestCommand.IsExecuting);
         Assert.IsTrue(asyncTestCommand.CanExecute(100));
         asyncTestCommand = new AsyncCommand <int>(a, o => false);
         asyncTestCommand.Execute(100);
         Assert.IsFalse(asyncTestCommand.IsExecuting);
         Assert.IsFalse(asyncTestCommand.CanExecute(100));
     });
     EnqueueTestComplete();
 }
コード例 #31
0
        public void CanExecuteWithoutParameterWithoutDelegateIsTrueByDefaultTest()
        {
            bool executeFired = false;

            var command = new AsyncCommand(() =>
            {
                executeFired = true;
                return(Task.CompletedTask);
            });

            Assert.True(command.CanExecute(new object()));
            Assert.False(executeFired);

            command.Execute(new object());
            Assert.True(executeFired);
        }