public void CanExecuteWithParam_CallDelegate()
        {
            // Arrange
            Task Execute() => Task.CompletedTask;

            var canExecuteCalled = false;

            bool CanExecute()
            {
                canExecuteCalled = true;
                return(true);
            }

            var errorHandler = new ErrorHandlerTestImplementation((cmd,
                                                                   exception) =>
            {
            });
            ICommand command = new AsyncCommand(Execute, CanExecute, errorHandler);

            // Act
            var canExecute = command.CanExecute(null);

            // Assert
            Assert.IsTrue(canExecute);
            Assert.IsTrue(canExecuteCalled);
        }
        public void RaiseCanExecuteChanged_InvokeEvent()
        {
            // Arrange
            Task Execute() => Task.CompletedTask;
            bool CanExecute() => true;

            var errorHandler = new ErrorHandlerTestImplementation((cmd,
                                                                   exception) =>
            {
            });
            object    invokedSender = null;
            EventArgs invokedArgs   = null;
            var       command       = new AsyncCommand(Execute, CanExecute, errorHandler);

            command.CanExecuteChanged += (sender,
                                          args) =>
            {
                invokedSender = sender;
                invokedArgs   = args;
            };

            // Act
            command.RaiseCanExecuteChanged();

            // Assert
            Assert.IsNotNull(invokedSender);
            Assert.AreSame(command, invokedSender);
            Assert.IsNotNull(invokedArgs);
            Assert.AreSame(EventArgs.Empty, invokedArgs);
        }
        public async Task ExecuteAsync_DoNotExecuteWhenCanExecuteReturnsFalse_Async()
        {
            // Arrange
            var executed = false;

            Task Execute()
            {
                executed = true;
                return(Task.CompletedTask);
            }

            bool CanExecute() => false;

            var errorHandler = new ErrorHandlerTestImplementation((cmd,
                                                                   exception) =>
            {
            });
            IAsyncCommand command = new AsyncCommand(Execute, CanExecute, errorHandler);

            // Act
            await command.ExecuteAsync();

            // Assert
            Assert.IsFalse(executed);
        }
        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.
        }