Beispiel #1
0
        public void AsyncValueCommand_ChangeCanExecute_Test()
        {
            // Arrange
            var canCommandExecute       = false;
            var didCanExecuteChangeFire = false;

            var command = new AsyncValueCommand(NoParameterTask, commandCanExecute);

            command.CanExecuteChanged += handleCanExecuteChanged;

            bool commandCanExecute(object parameter) => canCommandExecute;

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

            // Act
            canCommandExecute = true;

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

            // Act
#pragma warning disable CS0618 // Type or member is obsolete
            command.ChangeCanExecute();
#pragma warning restore CS0618 // Type or member is obsolete

            // Assert
            Assert.True(didCanExecuteChangeFire);
            Assert.True(command.CanExecute(null));

            void handleCanExecuteChanged(object sender, EventArgs e) => didCanExecuteChangeFire = true;
        }
        public async Task ICommand_NoParameter_CanExecuteChanged_AllowsMultipleExecutions_Test()
        {
            // Arrange
            var canExecuteChangedCount = 0;

            ICommand command = new AsyncValueCommand(() => IntParameterTask(Delay));

            command.CanExecuteChanged += handleCanExecuteChanged;

            void handleCanExecuteChanged(object sender, EventArgs e) => canExecuteChangedCount++;

            // Act
            command.Execute(null);

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

            // Act
            await IntParameterTask(Delay);
            await IntParameterTask(Delay);

            // Assert
            Assert.True(command.CanExecute(null));
            Assert.Equal(0, canExecuteChangedCount);
        }
Beispiel #3
0
        public async Task AsyncValueCommand_NoParameter_CanExecuteChanged_DoesNotAllowMultipleExecutions_Test()
        {
            // Arrange
            var canExecuteChangedCount = 0;

            var command = new AsyncValueCommand(() => IntParameterTask(Delay), allowsMultipleExecutions: false);

            command.CanExecuteChanged += handleCanExecuteChanged;

            void handleCanExecuteChanged(object sender, EventArgs e) => canExecuteChangedCount++;

            Assert.False(command.AllowsMultipleExecutions);

            // Act
            var asyncCommandTask = command.ExecuteAsync();

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

            // Act
            await asyncCommandTask;

            // Assert
            Assert.True(command.CanExecute(null));
            Assert.Equal(2, canExecuteChangedCount);
        }
Beispiel #4
0
        public async Task AsyncValueCommand_NoParameter_CanExecuteChanged_AllowsMultipleExecutions_Test()
        {
            // Arrange
            var canExecuteChangedCount = 0;

            var command = new AsyncValueCommand(() => IntParameterTask(Delay));

            command.CanExecuteChanged += handleCanExecuteChanged;

            void handleCanExecuteChanged(object?sender, EventArgs e) => canExecuteChangedCount++;

            Assert.IsTrue(command.AllowsMultipleExecutions);

            // Act
            var asyncCommandTask = command.ExecuteAsync();

            // Assert
            Assert.IsTrue(command.IsExecuting);
            Assert.IsTrue(command.CanExecute(null));

            // Act
            await asyncCommandTask.ConfigureAwait(false);

            // Assert
            Assert.IsTrue(command.CanExecute(null));
            Assert.AreEqual(0, canExecuteChangedCount);

            command.CanExecuteChanged -= handleCanExecuteChanged;
        }
Beispiel #5
0
        public void AsyncValueCommandCanExecuteChanged_Test()
        {
            // Arrange
            var canCommandExecute       = false;
            var didCanExecuteChangeFire = false;

            var command = new AsyncValueCommand(NoParameterTask, commandCanExecute);

            command.CanExecuteChanged += handleCanExecuteChanged;

            bool commandCanExecute(object parameter) => canCommandExecute;

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

            // Act
            canCommandExecute = true;

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

            // Act
            command.RaiseCanExecuteChanged();

            // Assert
            Assert.True(didCanExecuteChangeFire);
            Assert.True(command.CanExecute(null));

            void handleCanExecuteChanged(object sender, EventArgs e) => didCanExecuteChangeFire = true;
        }
Beispiel #6
0
        public async Task ICommand_Parameter_CanExecuteChanged_AllowsMultipleExecutions_Test()
        {
            // Arrange
            var canExecuteChangedCount = 0;

            ICommand command = new AsyncValueCommand <int>(IntParameterTask);

            command.CanExecuteChanged += handleCanExecuteChanged;

            void handleCanExecuteChanged(object?sender, EventArgs e) => canExecuteChangedCount++;

            // Act
            command.Execute(Delay);

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

            // Act
            await IntParameterTask(Delay).ConfigureAwait(false);

            // Assert
            Assert.IsTrue(command.CanExecute(null));
            Assert.AreEqual(0, canExecuteChangedCount);

            command.CanExecuteChanged -= handleCanExecuteChanged;
        }
        public void ICommand_Parameter_CanExecuteChanged_Test()
        {
            //Arrange
            ICommand command = new AsyncValueCommand <int>(IntParameterTask, CanExecuteDynamic);

            //Act

            //Assert
            Assert.True(command.CanExecute(true));
            Assert.False(command.CanExecute(false));
        }
        public void IAsyncValueCommand_Parameter_CanExecuteTrue_Test()
        {
            // Arrange
            IAsyncValueCommand <int>         command  = new AsyncValueCommand <int>(IntParameterTask, parameter => CanExecuteTrue(parameter));
            IAsyncValueCommand <int, string> command2 = new AsyncValueCommand <int, string>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
            Assert.True(command.CanExecute("Hello World"));
        }
Beispiel #9
0
        public async Task AsyncValueCommand_NoParameter_CanExecuteChanged_DoesNotAllowMultipleExecutions_Test()
        {
            // Arrange
            var semaphoreSlim              = new SemaphoreSlim(1, 1);
            var canExecuteChangedCount     = 0;
            var handleCanExecuteChangedTCS = new TaskCompletionSource <int>();

            var command = new AsyncValueCommand(() => 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();
                }
            }

            Assert.False(command.AllowsMultipleExecutions);

            // Act
            var asyncCommandTask = command.ExecuteAsync();

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

            // Act
            await asyncCommandTask.ConfigureAwait(false);

            var handleCanExecuteChangedResult = await handleCanExecuteChangedTCS.Task.ConfigureAwait(false);

            // Assert
            Assert.IsTrue(command.CanExecute(null));
            Assert.AreEqual(2, canExecuteChangedCount);
            Assert.AreEqual(canExecuteChangedCount, handleCanExecuteChangedResult);
        }
Beispiel #10
0
        public async Task AsyncValueCommand_ChangeCanExecute_Test()
        {
            // Arrange
            var semaphoreSlim = new SemaphoreSlim(1, 1);
            var handleCanExecuteChangedTCS = new TaskCompletionSource <object?>();

            var canCommandExecute       = false;
            var didCanExecuteChangeFire = false;

            var command = new AsyncValueCommand(NoParameterTask, commandCanExecute);

            command.CanExecuteChanged += handleCanExecuteChanged;

            bool commandCanExecute(object?parameter) => canCommandExecute;

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

            // Act
            canCommandExecute = true;

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

            // Act
            command.ChangeCanExecute();
            await handleCanExecuteChangedTCS.Task.ConfigureAwait(false);

            // Assert
            Assert.IsTrue(didCanExecuteChangeFire);
            Assert.IsTrue(command.CanExecute(null));

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

                try
                {
                    command.CanExecuteChanged -= handleCanExecuteChanged;
                    didCanExecuteChangeFire    = true;
                    handleCanExecuteChangedTCS.SetResult(null);
                }
                finally
                {
                    semaphoreSlim.Release();
                }
            }
        }
        public void IAsyncValueCommand_NoParameter_CanExecuteTrue_NoParameter_Test()
        {
            // Arrange
            IAsyncValueCommand command = new AsyncValueCommand(NoParameterTask, () => CanExecuteTrue());

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
        }
        public void IAsyncCommand_CanExecute_InvalidReferenceParameter()
        {
            // Arrange
            IAsyncValueCommand <int, bool> command = new AsyncValueCommand <int, bool>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.Throws <InvalidCommandParameterException>(() => command.CanExecute("Hello World"));
        }
        public void ICommand_NoParameter_CanExecuteFalse_Test()
        {
            //Arrange
            ICommand command = new AsyncValueCommand(NoParameterTask, CanExecuteFalse);

            //Act

            //Assert
            Assert.False(command.CanExecute(null));
        }
        public void AsyncValueCommandNoParameter_CanExecuteTrue_Test()
        {
            //Arrange
            AsyncValueCommand command = new AsyncValueCommand(NoParameterTask, CanExecuteTrue);

            //Act

            //Assert
            Assert.IsTrue(command.CanExecute(null));
        }
        public void AsyncValueCommandParameter_CanExecuteFalse_Test()
        {
            //Arrange
            AsyncValueCommand <int> command = new AsyncValueCommand <int>(IntParameterTask, CanExecuteFalse);

            //Act

            //Assert
            Assert.False(command.CanExecute(null));
        }
        public void ICommand_Parameter_CanExecuteTrue_Test()
        {
            //Arrange
            ICommand command = new AsyncValueCommand <int>(IntParameterTask, CanExecuteTrue);

            //Act

            //Assert
            Assert.True(command.CanExecute(null));
        }
        public void ICommand_Parameter_CanExecuteFalse_NoParameter_Test()
        {
            // Arrange
            ICommand command = new AsyncValueCommand <int>(IntParameterTask, () => CanExecuteFalse());

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
        }
        public void IAsyncCommand_CanExecute_InvalidValueTypeParameter()
        {
            // Arrange
            IAsyncValueCommand <int, string> command = new AsyncValueCommand <int, string>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert
            Assert.Throws <InvalidCommandParameterException>(() => command.CanExecute(true));
        }
Beispiel #19
0
        public void AsyncValueCommandNoParameter_CanExecuteFalseNoParameter_Test()
        {
            // Arrange
            var command = new AsyncValueCommand(NoParameterTask, () => CanExecuteFalse());

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
        }
Beispiel #20
0
        public void AsyncValueCommandNoParameter_CanExecuteTrue_Test()
        {
            // Arrange
            var command = new AsyncValueCommand(NoParameterTask, parameter => CanExecuteTrue(parameter));

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
        }
Beispiel #21
0
        public void AsyncValueCommandNoParameter_NoCanExecute_Test()
        {
            // Arrange
            Func <bool> canExecute = null;
            var         command    = new AsyncValueCommand(NoParameterTask, canExecute);

            // Act

            // Assert
            Assert.True(command.CanExecute(null));
        }
Beispiel #22
0
        public void AsyncValueCommandParameter_CanExecuteFalse_Test()
        {
            // Arrange
            var command  = new AsyncValueCommand <int>(IntParameterTask, parameter => CanExecuteFalse(parameter));
            var command2 = new AsyncValueCommand <int, string>(IntParameterTask, CanExecuteFalse);

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
            Assert.False(command2.CanExecute("Hello World"));
        }
        public void IAsyncValueCommand_Parameter_CanExecuteFalse_NoParameter_Test()
        {
            // Arrange
            IAsyncValueCommand <int>       command  = new AsyncValueCommand <int>(IntParameterTask, () => CanExecuteFalse());
            IAsyncValueCommand <int, bool> command2 = new AsyncValueCommand <int, bool>(IntParameterTask, CanExecuteFalse);

            // Act

            // Assert
            Assert.False(command.CanExecute(null));
            Assert.False(command2.CanExecute(true));
        }
Beispiel #24
0
        public void AsyncValueCommandParameter_CanExecuteTrue_NoParameterTest()
        {
            // Arrange
            var command  = new AsyncValueCommand <int>(IntParameterTask, () => CanExecuteTrue());
            var command2 = new AsyncValueCommand <int, bool>(IntParameterTask, CanExecuteTrue);

            // Act

            // Assert

            Assert.True(command.CanExecute(null));
            Assert.True(command2.CanExecute(true));
        }
Beispiel #25
0
        public async Task AsyncValueCommand_ChangeCanExecute_Test()
        {
            // Arrange
            var handleCanExecuteChangedTCS = new TaskCompletionSource <object>();

            var canCommandExecute       = false;
            var didCanExecuteChangeFire = false;

            var command = new AsyncValueCommand(NoParameterTask, commandCanExecute);

            command.CanExecuteChanged += handleCanExecuteChanged;

            bool commandCanExecute(object parameter) => canCommandExecute;

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

            // Act
            canCommandExecute = true;

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

            // Act
#pragma warning disable CS0618 // Type or member is obsolete
            command.ChangeCanExecute();
            await handleCanExecuteChangedTCS.Task;
#pragma warning restore CS0618 // Type or member is obsolete

            // Assert
            Assert.True(didCanExecuteChangeFire);
            Assert.True(command.CanExecute(null));

            void handleCanExecuteChanged(object sender, EventArgs e)
            {
                didCanExecuteChangeFire = true;
                handleCanExecuteChangedTCS.SetResult(null);
            }
        }
Beispiel #26
0
        public void AsyncValueCommandNoParameter_NoCanExecute_Test()
        {
            // Arrange
            Func <bool>?canExecute = null;

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

            // Act

            // Assert
            Assert.IsTrue(command.CanExecute(null));
        }
Beispiel #27
0
        public async Task AsyncValueCommand_RaiseCanExecuteChanged_Test()
        {
            // Arrange
            var handleCanExecuteChangedTCS = new TaskCompletionSource <object>();

            var canCommandExecute       = false;
            var didCanExecuteChangeFire = false;

            var command = new AsyncValueCommand(NoParameterTask, commandCanExecute);

            command.CanExecuteChanged += handleCanExecuteChanged;

            bool commandCanExecute(object parameter) => canCommandExecute;

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

            // Act
            canCommandExecute = true;

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

            // Act
            command.RaiseCanExecuteChanged();
            await handleCanExecuteChangedTCS.Task;

            // Assert
            Assert.True(didCanExecuteChangeFire);
            Assert.True(command.CanExecute(null));

            void handleCanExecuteChanged(object sender, EventArgs e)
            {
                didCanExecuteChangeFire = true;
                handleCanExecuteChangedTCS.SetResult(null);
            }
        }