public async Task CommandCanAlwaysBeInvokedDirectly_RegardlessWhetherTheCommandCanBeExecutedOrNot()
        {
            int                    number     = 0;
            bool                   canExecute = false;
            IAsyncCommand          command    = new AsyncRelayCommand(() => { number++; return(Task.CompletedTask); }, () => canExecute);
            IAsyncCommand <string> commandT   = new AsyncRelayCommand <string>(_ => { number--; return(Task.CompletedTask); }, _ => canExecute);

            Assert.False(command.CanExecute());
            await command.ExecuteAsync();

            Assert.Equal(1, number);
            Assert.False(commandT.CanExecute("F0"));
            await commandT.ExecuteAsync("F0");

            Assert.Equal(0, number);

            canExecute = true;

            Assert.True(command.CanExecute());
            await command.ExecuteAsync();

            Assert.Equal(1, number);
            Assert.True(commandT.CanExecute("F0"));
            await commandT.ExecuteAsync("F0");

            Assert.Equal(0, number);
        }
示例#2
0
        public async Task ContextValidation_Exception()
        {
            CancellationToken token = new CancellationToken();
            var invoked             = false;

            var contextMock = new Mock <IAsyncExecutionContext>();

            contextMock.SetupGet(o => o.IsBusy).Returns(false);
            contextMock.Setup(o => o.PrepareExecution(out token));
            contextMock.Setup(o => o.FinalizeExecution());

            var exceptionHandlerMock = new Mock <IExceptionHandler>();

            exceptionHandlerMock.Setup(o => o.HandleException(It.IsAny <Exception>())).Returns(false);

            var rc = new AsyncRelayCommand <int>(
                contextMock.Object,
                async(i, t) =>
            {
                invoked = true;
                throw new InvalidOperationException();
            });

            rc.ExceptionHandler = exceptionHandlerMock.Object;

            await rc.ExecuteAsync(10);

            contextMock.VerifyGet(o => o.IsBusy, Times.AtLeastOnce);
            contextMock.Verify(o => o.PrepareExecution(out token), Times.Once);
            contextMock.Verify(o => o.FinalizeExecution(), Times.Once);

            exceptionHandlerMock.Verify(o => o.HandleException(It.IsAny <Exception>()), Times.Once);

            Assert.IsTrue(invoked);
        }
示例#3
0
        public async Task ContextValidation_CanExectue()
        {
            CancellationToken token = new CancellationToken();
            var invoked             = false;

            var mock = new Mock <IAsyncExecutionContext>();

            mock.SetupGet(o => o.IsBusy).Returns(false);
            mock.Setup(o => o.PrepareExecution(out token));
            mock.Setup(o => o.FinalizeExecution());

            var rc = new AsyncRelayCommand <int>(
                mock.Object,
                async(i, t) =>
            {
                invoked = true;
                Assert.AreEqual(token, t);
            },
                (i) => false);

            await rc.ExecuteAsync(10);

            mock.VerifyGet(o => o.IsBusy, Times.AtLeastOnce());
            mock.Verify(o => o.PrepareExecution(out token), Times.Never());
            mock.Verify(o => o.FinalizeExecution(), Times.Never());

            Assert.IsFalse(invoked);
        }
示例#4
0
        public async Task TestExecutionSetOnExecuteAsync()
        {
            var cmd = new AsyncRelayCommand(() => Task.CompletedTask);

            var t = cmd.ExecuteAsync();

            Assert.IsNotNull(cmd.Execution);

            await t;
        }
        public async Task InContrastToTheNonGenericCommandWhichDoesNotSupportDataToBePassed_DataIsUsedByTheStronglyTypedCommand()
        {
            int number = 0;
            IAsyncCommand <int> commandT = new AsyncRelayCommand <int>(addend => { number += addend; return(Task.CompletedTask); }, integer => integer % 2 == 0);

            Assert.False(commandT.CanExecute(1));
            Assert.True(commandT.CanExecute(2));

            Assert.Equal(0, number);
            await commandT.ExecuteAsync(1);

            Assert.Equal(1, number);
            await commandT.ExecuteAsync(2);

            Assert.Equal(3, number);
            await commandT.ExecuteAsync(3);

            Assert.Equal(6, number);
            await commandT.ExecuteAsync(-9);

            Assert.Equal(-3, number);
        }
        public async Task ExecuteTheAsyncRelayCommandOnTheCurrentCommandTarget()
        {
            int                    number   = 0;
            IAsyncCommand          command  = new AsyncRelayCommand(() => { number++; return(Task.CompletedTask); });
            IAsyncCommand <string> commandT = new AsyncRelayCommand <string>(_ => { number--; return(Task.CompletedTask); });

            Assert.Equal(0, number);
            await command.ExecuteAsync();

            Assert.Equal(1, number);
            await commandT.ExecuteAsync("F0");

            Assert.Equal(0, number);
        }
        public async Task ExecuteAsync_Await_ActionIsExecuted()
        {
            var commandExecuted = false;

            var command = new AsyncRelayCommand(_ =>
            {
                Assert.False(commandExecuted);
                commandExecuted = true;
                return(Task.CompletedTask);
            });

            await command.ExecuteAsync(null);

            Assert.True(commandExecuted);
        }
        public async Task ExecuteAsync_CanExecuteIsFalse_ActionIsNotExecuted()
        {
            var commandExecuted = false;

            var command = new AsyncRelayCommand(_ =>
            {
                Assert.False(commandExecuted);
                commandExecuted = true;
                return(Task.CompletedTask);
            }, _ => false);

            await command.ExecuteAsync(null);

            Assert.False(commandExecuted);
        }
示例#9
0
        public async Task TestExplicitExecuteAsyncRunsDelegate()
        {
            int executeRunCount = 0;

            Func <Task> execute = () =>
            {
                executeRunCount++;
                return(Task.CompletedTask);
            };

            IAsyncCommand cmd = new AsyncRelayCommand(execute);

            await cmd.ExecuteAsync(null);

            Assert.AreEqual(1, executeRunCount);
        }
示例#10
0
        public async Task TestExplicitExecuteAsync()
        {
            int executeRunCount = 0;

            Func <int, Task> execute = (x) =>
            {
                executeRunCount++;
                return(Task.CompletedTask);
            };

            IAsyncCommand cmd = new AsyncRelayCommand <int>(execute);

            await cmd.ExecuteAsync(5);

            Assert.AreEqual(1, executeRunCount);
        }
        public async Task Test_AsyncRelayCommand_AlwaysEnabled()
        {
            int ticks = 0;

            var command = new AsyncRelayCommand(async() =>
            {
                await Task.Delay(1000);
                ticks++;
                await Task.Delay(1000);
            });

            Assert.IsTrue(command.CanExecute(null));
            Assert.IsTrue(command.CanExecute(new object()));

            Assert.IsFalse(command.CanBeCanceled);
            Assert.IsFalse(command.IsCancellationRequested);

            (object, EventArgs)args = default;

            command.CanExecuteChanged += (s, e) => args = (s, e);

            command.NotifyCanExecuteChanged();

            Assert.AreSame(args.Item1, command);
            Assert.AreSame(args.Item2, EventArgs.Empty);

            Assert.IsNull(command.ExecutionTask);
            Assert.IsFalse(command.IsRunning);

            Task task = command.ExecuteAsync(null);

            Assert.IsNotNull(command.ExecutionTask);
            Assert.AreSame(command.ExecutionTask, task);
            Assert.IsTrue(command.IsRunning);

            await task;

            Assert.IsFalse(command.IsRunning);

            Assert.AreEqual(ticks, 1);

            command.Execute(new object());

            await command.ExecutionTask !;

            Assert.AreEqual(ticks, 2);
        }
        public async Task Test_AsyncRelayCommandOfT_AlwaysEnabled()
        {
            int ticks = 0;

            var command = new AsyncRelayCommand <string>(async s =>
            {
                await Task.Delay(1000);
                ticks = int.Parse(s);
                await Task.Delay(1000);
            });

            Assert.IsTrue(command.CanExecute(null));
            Assert.IsTrue(command.CanExecute("1"));

            (object, EventArgs)args = default;

            command.CanExecuteChanged += (s, e) => args = (s, e);

            command.NotifyCanExecuteChanged();

            Assert.AreSame(args.Item1, command);
            Assert.AreSame(args.Item2, EventArgs.Empty);

            Assert.IsNull(command.ExecutionTask);
            Assert.IsFalse(command.IsRunning);

            Task task = command.ExecuteAsync((object)"42");

            Assert.IsNotNull(command.ExecutionTask);
            Assert.AreSame(command.ExecutionTask, task);
            Assert.IsTrue(command.IsRunning);

            await task;

            Assert.IsFalse(command.IsRunning);

            Assert.AreEqual(ticks, 42);

            command.Execute("2");

            await command.ExecutionTask !;

            Assert.AreEqual(ticks, 2);

            Assert.ThrowsException <InvalidCastException>(() => command.Execute(new object()));
        }
示例#13
0
        public async Task TestPropertyChangedRaisedOnExecuteAsyncForExecution()
        {
            var cmd = new AsyncRelayCommand(() => Task.CompletedTask);

            var changes = new List <string>();

            cmd.PropertyChanged += (sender, e) =>
            {
                changes.Add(e.PropertyName);
            };

            var t = cmd.ExecuteAsync();

            CollectionAssert.AreEqual(new[] { nameof(AsyncRelayCommand.Execution) }, changes);

            await t;
        }
示例#14
0
        public async Task TestExplicitExecuteAsyncWrongType()
        {
            int executeRunCount = 0;

            Func <int, Task> execute = (x) =>
            {
                executeRunCount++;
                return(Task.CompletedTask);
            };

            IAsyncCommand cmd = new AsyncRelayCommand <int>(execute);


            await Assert.ThrowsExceptionAsync <InvalidCastException>(
                async() => await cmd.ExecuteAsync("")
                );

            Assert.AreEqual(0, executeRunCount);
        }