예제 #1
0
        public void GenericDelegateCommandOfObjectShouldObserveCanExecuteAndObserveOtherProperties()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();

            ICommand command = new ActivatablePropertyObserverCommand <object>(o => { })
                               .ObservesCanExecute(commandTestObject.BoolPropertyExpression)
                               .ObservesProperty(commandTestObject.IntPropertyExpression);

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };

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

            commandTestObject.IntProperty = 10;

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

            canExecuteChangedRaised = false;
            Assert.False(canExecuteChangedRaised);

            commandTestObject.BoolProperty = true;

            Assert.True(canExecuteChangedRaised);
            Assert.True(command.CanExecute(null));
        }
예제 #2
0
 public void ShouldThrowIfExecuteMethodDelegateNull()
 {
     Assert.Throws <ArgumentNullException>(
         () =>
     {
         var command = new ActivatablePropertyObserverCommand <object>(null);
     });
 }
예제 #3
0
 public void DelegateCommandOfObjectGenericShouldThrowIfCanExecuteMethodDelegateNull()
 {
     Assert.Throws <ArgumentNullException>(
         () =>
     {
         var command = new ActivatablePropertyObserverCommand <object>(o => { }, null);
     });
 }
예제 #4
0
 public void WhenConstructedWithGenericTypeIsNonNullableValueType_Throws()
 {
     Assert.Throws <InvalidCastException>(
         () =>
     {
         var command = new ActivatablePropertyObserverCommand <int>(param => { });
     });
 }
예제 #5
0
 public void DelegateCommandOfObjectShouldThrowIfAllDelegatesAreNull()
 {
     Assert.Throws <ArgumentNullException>(
         () =>
     {
         var command = new ActivatablePropertyObserverCommand <object>(null, null);
     });
 }
예제 #6
0
        public void CanExecuteOfObjectReturnsTrueWithoutCanExecuteDelegate()
        {
            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatablePropertyObserverCommand <object>(handlers.Execute);

            var retVal = command.CanExecute(null);

            Assert.True(retVal);
        }
예제 #7
0
        public void IsActivePropertyChangeFiresEventOfObject()
        {
            var fired   = false;
            var command = new ActivatablePropertyObserverCommand <object>(this.DoNothing);

            command.IsActiveChanged += delegate { fired = true; };
            command.Activate();

            Assert.True(fired);
        }
예제 #8
0
        public void WhenConstructedWithGenericTypeOfObject_InitializesValues()
        {
            // Prepare

            // Act
            var actual = new ActivatablePropertyObserverCommand <object>(param => { });

            // verify
            Assert.NotNull(actual);
        }
예제 #9
0
        public void ExecuteCallsPassedInExecuteDelegate()
        {
            var handlers  = new DelegateObjectHandlers();
            var command   = new ActivatablePropertyObserverCommand <object>(handlers.Execute);
            var parameter = new object();

            command.Execute(parameter);

            Assert.AreSame(parameter, handlers.ExecuteParameter);
        }
예제 #10
0
        public void GenericDelegateCommandOfObjectObservingPropertyWithOwnerShouldRaiseOnEmptyPropertyName()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var command = new ActivatablePropertyObserverCommand <object>(o => { }).ObservesProperty(commandTestObject, o => o.IntProperty);

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };
            commandTestObject.RaisePropertyChanged(string.Empty);

            Assert.True(canExecuteChangedRaised);
        }
예제 #11
0
        public void CanExecuteOfObjectCallsPassedInCanExecuteDelegate()
        {
            var handlers  = new DelegateObjectHandlers();
            var command   = new ActivatablePropertyObserverCommand <object>(handlers.Execute, handlers.CanExecute);
            var parameter = new object();

            handlers.CanExecuteReturnValue = true;
            var retVal = command.CanExecute(parameter);

            Assert.AreSame(parameter, handlers.CanExecuteParameter);
            Assert.AreEqual(handlers.CanExecuteReturnValue, retVal);
        }
예제 #12
0
        public void GenericDelegateCommandOfObjectShouldNotObserveWithOwnerDuplicateProperties()
        {
            var commandTestObject = new CommandTestObject();

            Assert.Throws <ArgumentException>(
                () =>
            {
                var command = new ActivatablePropertyObserverCommand <object>(o => { })
                              .ObservesProperty(commandTestObject, o => o.IntProperty)
                              .ObservesProperty(commandTestObject, o => o.IntProperty);
            });
        }
예제 #13
0
        public void GenericDelegateCommandOfObjectShouldNotObserveWithOwnerDuplicateCanExecute()
        {
            var commandTestObject = new CommandTestObject();

            Assert.Throws <ArgumentException>(
                () =>
            {
                ICommand command = new ActivatablePropertyObserverCommand <object>(o => { })
                                   .ObservesCanExecute(commandTestObject, o1 => o1.BoolProperty)
                                   .ObservesCanExecute(commandTestObject, o2 => o2.BoolProperty);
            });
        }
예제 #14
0
        public void RaiseCanExecuteChangedRaisesCanExecuteChanged()
        {
            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatablePropertyObserverCommand <object>(handlers.Execute);
            var canExecuteChangedRaised = false;

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };

            command.RaiseCanExecuteChanged();

            Assert.True(canExecuteChangedRaised);
        }
예제 #15
0
        public void GenericDelegateCommandOfObjectNotObservingPropertiesShouldNotRaiseOnEmptyPropertyName()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var command = new ActivatablePropertyObserverCommand <object>(o => { });

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };

            commandTestObject.RaisePropertyChanged(null);

            Assert.False(canExecuteChangedRaised);
        }
예제 #16
0
        public void GenericDelegateCommandOfObjectShouldObserveOneProperty()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var command =
                new ActivatablePropertyObserverCommand <object>(o => { }).ObservesProperty(commandTestObject.IntPropertyExpression);

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };

            commandTestObject.IntProperty = 10;

            Assert.True(canExecuteChangedRaised);
        }
예제 #17
0
        public void CanRemoveCanExecuteOfObjectChangedHandler()
        {
            var command = new ActivatablePropertyObserverCommand <object>(o => { });

            var canExecuteChangedRaised = false;

            void Handler(object s, EventArgs e) => canExecuteChangedRaised = true;

            command.CanExecuteChanged += Handler;
            command.CanExecuteChanged -= Handler;
            command.RaiseCanExecuteChanged();

            Assert.False(canExecuteChangedRaised);
        }
예제 #18
0
        public void ShouldPassParameterInstanceOnExecute()
        {
            var      executeCalled = false;
            var      testClass     = new MyClass();
            ICommand command       = new ActivatablePropertyObserverCommand <MyClass>(
                delegate(MyClass parameter)
            {
                Assert.AreSame(testClass, parameter);
                executeCalled = true;
            });

            command.Execute(testClass);
            Assert.True(executeCalled);
        }
예제 #19
0
        public void GenericDelegateCommandOfNullableIntWithNullableParameterShouldObserveCanExecute()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();

            ICommand command =
                new ActivatablePropertyObserverCommand <int?>(o => { }).ObservesCanExecute(commandTestObject.BoolPropertyExpression);

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };

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

            commandTestObject.BoolProperty = true;

            Assert.True(canExecuteChangedRaised);
            Assert.True(command.CanExecute(null));
        }
예제 #20
0
        public void GenericDelegateCommandOfObjectShouldObserveWithOwnerCanExecute()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();

            ICommand command =
                new ActivatablePropertyObserverCommand <object>(o1 => { }).ObservesCanExecute(commandTestObject, o2 => o2.BoolProperty);

            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };

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

            commandTestObject.BoolProperty = true;

            Assert.True(canExecuteChangedRaised);
            Assert.True(command.CanExecute(null));
        }
예제 #21
0
        public void GenericDelegateCommandOfObjectShouldObserveComplexPropertyWhenParentPropertyIsNull()
        {
            var canExecuteChangedRaise = false;
            var commandTestObject      = new CommandTestObject {
                ComplexProperty = new ComplexType()
            };
            var command = new ActivatablePropertyObserverCommand <object>(o => { }).ObservesProperty(
                commandTestObject.ComplexPropertyInnerComplexPropertyIntPropertyExpression);

            command.CanExecuteChanged += delegate { canExecuteChangedRaise = true; };

            var newComplexObject = new ComplexType {
                InnerComplexProperty = new ComplexType {
                    IntProperty = 10
                }
            };

            commandTestObject.ComplexProperty.InnerComplexProperty = newComplexObject;

            Assert.True(canExecuteChangedRaise);
        }
예제 #22
0
        public void GenericDelegateCommandOfObjectPropertyObserverWithOwnerUnsubscribeUnusedListeners()
        {
            var canExecuteChangedRaiseCount = 0;
            var commandTestObject           = new CommandTestObject
            {
                ComplexProperty = new ComplexType
                {
                    IntProperty          = 1,
                    InnerComplexProperty = new ComplexType
                    {
                        IntProperty          = 1,
                        InnerComplexProperty =
                            new ComplexType
                        {
                            IntProperty = 1
                        }
                    }
                }
            };

            var command = new ActivatablePropertyObserverCommand <object>(o => { })
                          .ObservesProperty(commandTestObject, o => o.ComplexProperty.IntProperty)
                          .ObservesProperty(
                commandTestObject,
                o => o.ComplexProperty.InnerComplexProperty.InnerComplexProperty.IntProperty)
                          .ObservesProperty(commandTestObject, o => o.ComplexProperty.InnerComplexProperty.IntProperty);

            command.CanExecuteChanged += delegate { canExecuteChangedRaiseCount++; };

            commandTestObject.ComplexProperty.IntProperty = 2;
            commandTestObject.ComplexProperty.InnerComplexProperty.InnerComplexProperty.IntProperty = 2;
            commandTestObject.ComplexProperty.InnerComplexProperty.IntProperty = 2;

            Assert.AreEqual(3, canExecuteChangedRaiseCount);

            var innerInnerComplexProp = commandTestObject.ComplexProperty.InnerComplexProperty.InnerComplexProperty;
            var innerComplexProp      = commandTestObject.ComplexProperty.InnerComplexProperty;
            var complexProp           = commandTestObject.ComplexProperty;

            commandTestObject.ComplexProperty = new ComplexType
            {
                InnerComplexProperty = new ComplexType
                {
                    InnerComplexProperty =
                        new ComplexType()
                }
            };

            Assert.AreEqual(0, innerInnerComplexProp.GetPropertyChangedSubscribedLength());
            Assert.AreEqual(0, innerComplexProp.GetPropertyChangedSubscribedLength());
            Assert.AreEqual(0, complexProp.GetPropertyChangedSubscribedLength());

            innerInnerComplexProp = commandTestObject.ComplexProperty.InnerComplexProperty.InnerComplexProperty;
            innerComplexProp      = commandTestObject.ComplexProperty.InnerComplexProperty;
            complexProp           = commandTestObject.ComplexProperty;

            commandTestObject.ComplexProperty = null;

            Assert.AreEqual(0, innerInnerComplexProp.GetPropertyChangedSubscribedLength());
            Assert.AreEqual(0, innerComplexProp.GetPropertyChangedSubscribedLength());
            Assert.AreEqual(0, complexProp.GetPropertyChangedSubscribedLength());
        }
예제 #23
0
        public void IsActivePropertyIsFalseByDefaultOfObject()
        {
            var command = new ActivatablePropertyObserverCommand <object>(this.DoNothing);

            Assert.False(command.IsActive);
        }