Example #1
0
        public void TestComposite_PassParameter()
        {
            bool   command1Called = false;
            string command1Result = "";
            string checkValue     = "";

            var composite = new CompositeCommand();

            composite.Add(new DelegateCommand <string>((value) =>
            {
                command1Result = value;
                command1Called = true;
            },
                                                       (value) =>
            {
                checkValue = value;
                return(true);
            }));

            if (composite.CanExecute("Ok"))
            {
                composite.Execute("Ok");
            }

            Assert.IsTrue(command1Called);
            Assert.AreEqual("Ok", command1Result);
            Assert.AreEqual("Ok", checkValue);
        }
Example #2
0
        public void DispatchCommandDoesNotIncludeInactiveRegisteredCommandInVoting()
        {
            CompositeCommand       activeAwareCommand = new CompositeCommand(true);
            MockActiveAwareCommand command            = new MockActiveAwareCommand();

            activeAwareCommand.RegisterCommand(command);
            command.IsValid  = true;
            command.IsActive = false;

            Assert.False(activeAwareCommand.CanExecute(null), "Registered Click is inactive so should not participate in CanExecute vote");

            command.IsActive = true;

            Assert.True(activeAwareCommand.CanExecute(null));

            command.IsValid = false;

            Assert.False(activeAwareCommand.CanExecute(null));
        }
Example #3
0
        public void ReturnCanExecuteOfContainedCommandIfThereIsOnlyOneCommand(bool canExecuteSubCommand)
        {
            var subcmd = new Mock <ICommand>();

            subcmd.Setup(c => c.CanExecute(It.IsAny <object>())).Returns(canExecuteSubCommand);

            var cmd = new CompositeCommand(subcmd.Object);

            cmd.CanExecute(null).Should().Be(canExecuteSubCommand);
        }
Example #4
0
        public void UnregisterCommandDisconnectsIsActiveChangedDelegate()
        {
            CompositeCommand       activeAwareCommand = new CompositeCommand(true);
            MockActiveAwareCommand commandOne         = new MockActiveAwareCommand()
            {
                IsActive = true, IsValid = true
            };
            MockActiveAwareCommand commandTwo = new MockActiveAwareCommand()
            {
                IsActive = false, IsValid = false
            };

            activeAwareCommand.RegisterCommand(commandOne);
            activeAwareCommand.RegisterCommand(commandTwo);

            Assert.True(activeAwareCommand.CanExecute(null));

            activeAwareCommand.UnregisterCommand(commandOne);

            Assert.False(activeAwareCommand.CanExecute(null));
        }
        public void CanExecuteWithMultipleCommands()
        {
            bool canExecuteCommand1 = false;
            bool canExecuteCommand2 = false;
            var command1 = new DelegateCommand(
              () => { },
              () => canExecuteCommand1);
            var command2 = new DelegateCommand<int>(
              i => Assert.AreEqual(123, i),
              i =>
              {
                  Assert.AreEqual(123, i);
                  return canExecuteCommand2;
              });

            var compositeCommand = new CompositeCommand();
            Assert.IsFalse(compositeCommand.CanExecute(123));

            compositeCommand.RegisterCommand(command1);
            compositeCommand.RegisterCommand(command2);
            Assert.IsFalse(compositeCommand.CanExecute(123));

            canExecuteCommand1 = true;
            Assert.IsFalse(compositeCommand.CanExecute(123));

            canExecuteCommand2 = true;
            Assert.IsTrue(compositeCommand.CanExecute(123));

            canExecuteCommand1 = false;
            compositeCommand.UnregisterCommand(command1);
            Assert.IsTrue(compositeCommand.CanExecute(123));

            canExecuteCommand2 = false;
            Assert.IsFalse(compositeCommand.CanExecute(123));

            canExecuteCommand1 = true;
            canExecuteCommand2 = true;
            compositeCommand.UnregisterCommand(command2);
            Assert.IsFalse(compositeCommand.CanExecute(123));
        }
        private bool DoDeleteHimSelf(string value, Uri uri)
        {
            var deleteInfo = new Tuple <string, Uri>(value, uri);

            if (_compositeDeleteCommand.RegisteredCommands.Any())
            {
                if (_compositeDeleteCommand.CanExecute(deleteInfo))
                {
                    _compositeDeleteCommand.Execute(deleteInfo);
                    return(true);
                }
            }
            return(false);
        }
Example #7
0
        public void PassCorrectParameterForCanExecutes()
        {
            var parameter = "parameter";

            var subcmd = new Mock <ICommand>(MockBehavior.Strict);

            subcmd.Setup(c => c.CanExecute(parameter)).Returns(false);

            var cmd = new CompositeCommand(subcmd.Object);

            cmd.CanExecute(parameter);

            subcmd.Verify(c => c.CanExecute(parameter));
        }
Example #8
0
        public void ReturnCombinedCanExecuteCommandIfThereIsMoreThanOneCommand(
            bool canExecuteSubCommand1, bool canExecuteSubCommand2, bool canExecuteCompositeCommand)
        {
            var subcmd1 = new Mock <ICommand>();

            subcmd1.Setup(c => c.CanExecute(It.IsAny <object>())).Returns(canExecuteSubCommand1);

            var subcmd2 = new Mock <ICommand>();

            subcmd2.Setup(c => c.CanExecute(It.IsAny <object>())).Returns(canExecuteSubCommand2);

            var cmd = new CompositeCommand(new[] { subcmd1.Object, subcmd2.Object });

            cmd.CanExecute(null).Should().Be(canExecuteCompositeCommand);
        }
Example #9
0
        public void DispatchCommandShouldIgnoreInactiveCommandsInCanExecuteVote()
        {
            CompositeCommand       activeAwareCommand = new CompositeCommand(true);
            MockActiveAwareCommand commandOne         = new MockActiveAwareCommand()
            {
                IsActive = false, IsValid = false
            };
            MockActiveAwareCommand commandTwo = new MockActiveAwareCommand()
            {
                IsActive = true, IsValid = true
            };

            activeAwareCommand.RegisterCommand(commandOne);
            activeAwareCommand.RegisterCommand(commandTwo);

            Assert.True(activeAwareCommand.CanExecute(null));
        }
Example #10
0
        public void CanExecuteWithMultipleCommands()
        {
            bool canExecuteCommand1 = false;
            bool canExecuteCommand2 = false;
            var  command1           = new DelegateCommand(
                () => { },
                () => canExecuteCommand1);
            var command2 = new DelegateCommand <int>(
                i => Assert.AreEqual(123, i),
                i =>
            {
                Assert.AreEqual(123, i);
                return(canExecuteCommand2);
            });

            var compositeCommand = new CompositeCommand();

            Assert.IsFalse(compositeCommand.CanExecute(123));

            compositeCommand.RegisterCommand(command1);
            compositeCommand.RegisterCommand(command2);
            Assert.IsFalse(compositeCommand.CanExecute(123));

            canExecuteCommand1 = true;
            Assert.IsFalse(compositeCommand.CanExecute(123));

            canExecuteCommand2 = true;
            Assert.IsTrue(compositeCommand.CanExecute(123));

            canExecuteCommand1 = false;
            compositeCommand.UnregisterCommand(command1);
            Assert.IsTrue(compositeCommand.CanExecute(123));

            canExecuteCommand2 = false;
            Assert.IsFalse(compositeCommand.CanExecute(123));


            canExecuteCommand1 = true;
            canExecuteCommand2 = true;
            compositeCommand.UnregisterCommand(command2);
            Assert.IsFalse(compositeCommand.CanExecute(123));
        }
Example #11
0
        public void TestComposite_Check_ReturnsFalse()
        {
            bool command1Called = false;
            bool command2Called = false;
            bool isCheck1       = false;
            bool isCheck2       = false;

            var composite = new CompositeCommand();

            composite.Add(new DelegateCommand(() =>
            {
                command1Called = true;
            }, () =>
            {
                isCheck1 = true;
                return(false);
            }));
            composite.Add(new DelegateCommand(() =>
            {
                command2Called = true;
            }, () =>
            {
                isCheck2 = true;
                return(false);
            }));

            if (composite.CanExecute(null))
            {
                composite.Execute(null);
            }

            Assert.IsFalse(command1Called);
            Assert.IsFalse(command2Called);
            Assert.IsTrue(isCheck1);
            Assert.IsFalse(isCheck2);
        }
Example #12
0
        public void BeAbleToExecuteEmptyCommand()
        {
            var cmd = new CompositeCommand();

            cmd.CanExecute(null).Should().BeTrue();
        }