예제 #1
0
        public void CanExecuteDuringAsyncExecute2()
        {
            var tcs           = new TaskCompletionSource <object>();
            var canExecute    = false;
            var executeCalled = false;

            var command = new AsyncDelegateCommand(() =>
            {
                executeCalled = true;
                return(tcs.Task);
            }, () => canExecute);

            Assert.IsFalse(command.CanExecute(null));
            command.Execute(null);
            Assert.IsFalse(executeCalled);

            canExecute = true;
            Assert.IsTrue(command.CanExecute(null));
            command.Execute(null);

            executeCalled = false;
            Assert.IsFalse(command.CanExecute(null));
            command.Execute(null);
            Assert.IsFalse(executeCalled);

            tcs.SetResult(null);
            Assert.IsTrue(command.CanExecute(null));
            command.Execute(null);
            Assert.IsTrue(executeCalled);
        }
예제 #2
0
        public void CanExecuteDuringAsyncExecute()
        {
            AssertHelper.ExpectedException <ArgumentNullException>(() => new AsyncDelegateCommand((Func <Task>)null));

            var tcs           = new TaskCompletionSource <object>();
            var executeCalled = false;

            var command = new AsyncDelegateCommand(() =>
            {
                executeCalled = true;
                return(tcs.Task);
            });

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

            executeCalled = false;
            Assert.IsFalse(command.CanExecute(null));
            command.Execute(null);
            Assert.IsFalse(executeCalled);

            tcs.SetResult(null);
            Assert.IsTrue(command.CanExecute(null));
            command.Execute(null);
            Assert.IsTrue(executeCalled);
        }
예제 #3
0
        public async Task CanExecute_returns_false_during_execution_and_true_after_completion()
        {
            var tcs     = new TaskCompletionSource <bool>();
            var command = new AsyncDelegateCommand <int>(x => tcs.Task);

            command.Execute(42);
            command.CanExecute(42).Should().BeFalse();
            tcs.SetResult(true);
            await command.WaitForCompletionAsync();

            command.CanExecute(42).Should().BeTrue();
        }
예제 #4
0
        public void RaiseCanExecuteChangedTest()
        {
            var executed   = false;
            var canExecute = false;
            var command    = new AsyncDelegateCommand(() => { executed = true; return(Task.FromResult((object)null)); }, () => canExecute);

            Assert.IsFalse(command.CanExecute(null));
            canExecute = true;
            Assert.IsTrue(command.CanExecute(null));

            AssertHelper.CanExecuteChangedEvent(command, () => command.RaiseCanExecuteChanged());

            AssertHelper.CanExecuteChangedEvent(command, () => command.Execute(null), 2, ExpectedChangedCountMode.Exact);  // Because during execution CanExecute returns false
            Assert.IsTrue(executed);
        }
        public void Execute_CanExecuteFunction_NoOptions_ReturnsFalseAsCanExecute()
        {
            var funcMock      = new Mock <Func <object, Task> >();
            var asyncDelegate = new AsyncDelegateCommand <object>(funcMock.Object, canExecute: _ => false);

            Assert.IsFalse(asyncDelegate.CanExecute(null));
        }
        public void Execute_ExecuteOnce_ReturnsTrueBeforeExecution()
        {
            var funcMock      = new Mock <Func <object, Task> >();
            var asyncDelegate = new AsyncDelegateCommand <object>(funcMock.Object, executeOnce: true);

            Assert.IsTrue(asyncDelegate.CanExecute());
        }
예제 #7
0
        public void Execute_CanExecuteFunction_NoOptions_ReturnsTrueAsCanExecute()
        {
            var funcMock      = new Mock <Func <Task> >();
            var asyncDelegate = new AsyncDelegateCommand(funcMock.Object, canExecute: () => true);

            Assert.IsTrue(asyncDelegate.CanExecute());
        }
예제 #8
0
        public void Execute_ExecuteOnce_ReturnsFalseAfterExecution()
        {
            var funcMock      = new Mock <Func <Task> >();
            var asyncDelegate = new AsyncDelegateCommand(funcMock.Object, executeOnce: true);

            asyncDelegate.Execute();

            Assert.IsFalse(asyncDelegate.CanExecute());
        }
예제 #9
0
        public void CanExecuteDuringAsyncExecuteWithParameter()
        {
            var    tcs = new TaskCompletionSource <object>();
            string commandParameter = null;

            var command = new AsyncDelegateCommand(p =>
            {
                commandParameter = (string)p;
                return(tcs.Task);
            });

            Assert.IsTrue(command.CanExecute(null));
            command.Execute("test");
            Assert.IsFalse(command.CanExecute(null));
            tcs.SetResult(null);
            Assert.IsTrue(command.CanExecute(null));

            Assert.AreEqual("test", commandParameter);
        }
예제 #10
0
        public void AsyncDelegateCommand_CanExecute_Should_Invoke_Lambad()
        {
            bool invoked = false;
            var  c       = new AsyncDelegateCommand(_ => Task.CompletedTask, _ =>
            {
                invoked = true;
                return(true);
            });

            c.CanExecute(null).Should().BeTrue();
            invoked.Should().BeTrue();
        }
예제 #11
0
        public void CanExecute_returns_true_if_not_specified()
        {
            var command = new AsyncDelegateCommand <int>(x => Task.CompletedTask);

            command.CanExecute(42).Should().BeTrue();
        }
예제 #12
0
        public void CanExecute_returns_value_from_specified_delegate(int parameter, bool canExecute)
        {
            var command = new AsyncDelegateCommand <int>(x => Task.CompletedTask, x => x % 2 == 0);

            command.CanExecute(parameter).Should().Be(canExecute);
        }
예제 #13
0
        public void AsyncDelegateCommand_CanExecute_NotSpecified_Should_Returns_True()
        {
            var c = new AsyncDelegateCommand(_ => Task.CompletedTask);

            c.CanExecute(null).Should().BeTrue();
        }
예제 #14
0
        public void CanExecute_returns_value_from_specified_delegate(bool canExecute)
        {
            var command = new AsyncDelegateCommand(() => Task.CompletedTask, () => canExecute);

            command.CanExecute(null).Should().Be(canExecute);
        }