public void ShouldDisableButtonIfCanExecuteFalse()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand() { CanExecuteReturnValue = false };

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            Assert.IsTrue(clickableObject.IsEnabled);

            behavior.Command = command;
            Assert.IsFalse(clickableObject.IsEnabled);
        }
        public void ShouldHookUpClickCommandBehaviorWhenSettingProperty()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            Click.SetCommand(clickableObject, command);
            Assert.IsFalse(command.ExecuteCalled);

            clickableObject.RaiseClick();

            Assert.IsTrue(command.ExecuteCalled);
            Assert.AreSame(command, Click.GetCommand(clickableObject));
        }
        public void ShouldObserveClickEvent()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;
            Assert.IsFalse(command.ExecuteCalled);

            clickableObject.RaiseClick();

            Assert.IsTrue(command.ExecuteCalled);
        }
        public void ShouldUpdateCommandOnBehaviorWhenChangingProperty()
        {
            var clickableObject = new MockClickableObject();
            var oldCommand = new MockCommand();
            var newCommand = new MockCommand();

            Click.SetCommand(clickableObject, oldCommand);
            Click.SetCommand(clickableObject, newCommand);

            clickableObject.RaiseClick();

            Assert.IsTrue(newCommand.ExecuteCalled);
            Assert.IsFalse(oldCommand.ExecuteCalled);
        }
        public void ShouldReEnableButtonIfCanExecuteChangedRaises()
        {
            var clickableObject = new MockClickableObject();
            var mockCommand = new MockCommand() { CanExecuteReturnValue = false };

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = mockCommand;
            Assert.IsFalse(clickableObject.IsEnabled);

            mockCommand.CanExecuteReturnValue = true;
            mockCommand.RaiseCanExecuteChanged();

            Assert.IsTrue(clickableObject.IsEnabled);
        }
        public void ShouldExecuteWithCommandParameter()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();
            var parameter = new object();

            Click.SetCommand(clickableObject, command);
            Click.SetCommandParameter(clickableObject, parameter);
            Assert.IsNull(command.ExecuteParameter);

            clickableObject.RaiseClick();

            Assert.IsTrue(command.ExecuteCalled);
            Assert.IsNotNull(command.ExecuteParameter);
            Assert.AreSame(parameter, command.ExecuteParameter);
        }
        public void ShouldCallCanExecuteOnParameterChange()
        {
            var clickableObject = new MockClickableObject();

            var command = new MockCommand();
            var parameter = new object();
            Click.SetCommand(clickableObject, command);
            Assert.IsNull(command.CanExecuteParameter);
            Assert.IsTrue(clickableObject.IsEnabled);

            command.CanExecuteReturnValue = false;
            Click.SetCommandParameter(clickableObject, parameter);

            Assert.IsNotNull(command.CanExecuteParameter);
            Assert.AreSame(parameter, command.CanExecuteParameter);
            Assert.IsFalse(clickableObject.IsEnabled);
        }
        public void ChangingCommandsShouldExecuteOnNewCommand()
        {
            var clickableObject = new MockClickableObject();
            var originalCommand = new MockCommand() { CanExecuteReturnValue = true };
            var newCommand = new MockCommand() { CanExecuteReturnValue = true };

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = originalCommand;

            clickableObject.RaiseClick();
            Assert.IsFalse(newCommand.ExecuteCalled);

            behavior.Command = newCommand;
            clickableObject.RaiseClick();
            Assert.IsTrue(newCommand.ExecuteCalled);
        }
        public void ShouldUpdateEnabledStateIfCanExecuteChangedRaisesOnDelegateCommandAfterCollect()
        {
            var clickableObject = new MockClickableObject();
            var command = new DelegateCommand<object>(delegate { }, o => true);

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;
            clickableObject.IsEnabled = false;

            GC.Collect();

            command.RaiseCanExecuteChanged();
            Assert.IsTrue(clickableObject.IsEnabled);
        }
        public void ShouldExecuteWithCommandParameter()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();
            var parameter = new object();
            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;
            behavior.CommandParameter = parameter;
            Assert.IsNull(command.ExecuteParameter);

            clickableObject.RaiseClick();

            Assert.IsTrue(command.ExecuteCalled);
            Assert.IsNotNull(command.ExecuteParameter);
            Assert.AreSame(parameter, command.ExecuteParameter);
        }
        public void ShouldCallCanExecuteWithParameterWhenSettingCommand()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();
            var parameter = new object();
            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            command.CanExecuteReturnValue = false;
            behavior.CommandParameter = parameter;
            Assert.IsNull(command.CanExecuteParameter);
            Assert.IsTrue(clickableObject.IsEnabled);

            behavior.Command = command;

            Assert.IsNotNull(command.CanExecuteParameter);
            Assert.AreSame(parameter, command.CanExecuteParameter);
            Assert.IsFalse(clickableObject.IsEnabled);
        }
        public void DisposedControlStopsMonitoringCommandAfterFirstCanExecuteChanged()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;

            clickableObject = null;
            WeakReference commandReference = new WeakReference(command);
            GC.Collect();

            command.CanExecuteReturnValue = false;
            command.RaiseCanExecuteChanged();
            command = null;

            GC.Collect();

            Assert.IsFalse(commandReference.IsAlive);
        }
        public void DisposedControlDoesNotThrowOnCanExecuteChanged()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;

            WeakReference controlWeakReference = new WeakReference(clickableObject);
            clickableObject = null;
            behavior = null;
            GC.Collect();

            command.CanExecuteReturnValue = false;
            command.RaiseCanExecuteChanged();
        }
        public void CommandShouldNotKeepButtonAlive()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;

            WeakReference controlWeakReference = new WeakReference(clickableObject);
            clickableObject = null;
            behavior = null;
            GC.Collect();

            Assert.IsFalse(controlWeakReference.IsAlive);
            GC.KeepAlive(command);
        }
        public void ShouldNotMonitorCanExecuteChangedOnDisconnectedCommand()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;

            behavior.Command = null;

            Assert.IsTrue(clickableObject.IsEnabled);
            command.CanExecuteReturnValue = false;
            command.RaiseCanExecuteChanged();
            Assert.IsTrue(clickableObject.IsEnabled);
        }
        public void ShouldNotThrowWhenCommandSetToNull()
        {
            var clickableObject = new MockClickableObject();
            var command = new MockCommand();

            var behavior = new ButtonBaseClickCommandBehavior(clickableObject);
            behavior.Command = command;

            behavior.Command = null;
            clickableObject.RaiseClick();
        }