Example #1
0
        public void GenericRelayCommandNotObservingPropertiesShouldNotRaiseOnEmptyPropertyName()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var command = new RelayCommand <object>(o => { });

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

            commandTestObject.RaisePropertyChanged(null);

            Assert.False(canExecuteChangedRaised);
        }
        public void GenericAsyncRelayCommandNotObservingPropertiesShouldNotRaiseOnEmptyPropertyName()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var command = new global::Anori.WinUI.Commands.AsyncRelayCommand <object>(async o => await Task.Yield());

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

            commandTestObject.RaisePropertyChanged(null);

            Assert.False(canExecuteChangedRaised);
        }
        public void GenericObservableCommandOfObjectObservingPropertyShouldRaiseOnNullPropertyName()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var observer = new PropertyObserverFactory().ObservesProperty(commandTestObject.IntPropertyExpression);
            var command  = new ActivatableCanExecuteObserverCommand <object>(o => { }, observer).Activate();

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

            Assert.True(canExecuteChangedRaised);
        }
Example #4
0
        public void GenericObservableCommandOfObjectShouldNotObserveDuplicateProperties()
        {
            var commandTestObject = new CommandTestObject();
            var factory           = new PropertyObserverFactory();
            var observer1         = factory.ObservesProperty(commandTestObject.IntPropertyExpression);
            var observer2         = factory.ObservesProperty(commandTestObject.IntPropertyExpression);

            Assert.Throws <ArgumentException>(
                () =>
            {
                var command = new ActivatableCanExecuteObserverCommand <object>(o => { }, observer1, observer2);
            });
        }
Example #5
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);
        }
Example #6
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));
        }
Example #7
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));
        }
Example #8
0
        public void GenericObservableCommandOfObjectShouldObserveWithOwnerCanExecute()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var factory            = new PropertyObserverFactory();
            var canExecuteObserver = factory.ObservesCanExecute(commandTestObject, o => o.BoolProperty);

            ICommand command = new ActivatableCanExecuteObserverCommand <object>(o => { }, canExecuteObserver).Activate();

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

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

            commandTestObject.BoolProperty = true;

            Assert.True(canExecuteChangedRaised);
            Assert.True(command.CanExecute(null));
        }
Example #9
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);
        }
Example #10
0
        public void GenericObservableCommandOfObjectShouldObserveMultipleProperties()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var factory   = new PropertyObserverFactory();
            var observer1 = factory.ObservesProperty(commandTestObject.IntPropertyExpression);
            var observer2 = factory.ObservesProperty(commandTestObject.BoolPropertyExpression);

            var command = new ActivatableCanExecuteObserverCommand <object>(o => { }, observer1, observer2).Activate();

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

            commandTestObject.IntProperty = 10;

            Assert.True(canExecuteChangedRaised);

            canExecuteChangedRaised = false;

            commandTestObject.BoolProperty = true;

            Assert.True(canExecuteChangedRaised);
        }
Example #11
0
        public void GenericObservableCommandOfObjectShouldObserveComplexPropertyWhenRootPropertyIsNull()
        {
            var canExecuteChangedRaise = false;
            var commandTestObject      = new CommandTestObject {
                ComplexProperty = null
            };
            var factory   = new PropertyObserverFactory();
            var observer1 = factory.ObservesProperty(
                commandTestObject.ComplexPropertyInnerComplexPropertyIntPropertyExpression);

            var command = new ActivatableCanExecuteObserverCommand <object>(o => { }, observer1).Activate();

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

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

            commandTestObject.ComplexProperty = newComplexObject;

            Assert.True(canExecuteChangedRaise);
        }
Example #12
0
        public void GenericObservableCommandOfObjectPropertyObserverWithOwnerUnsubscribeUnusedListeners()
        {
            var canExecuteChangedRaiseCount = 0;
            var commandTestObject           = new CommandTestObject
            {
                ComplexProperty = new ComplexType
                {
                    IntProperty          = 1,
                    InnerComplexProperty = new ComplexType
                    {
                        IntProperty          = 1,
                        InnerComplexProperty =
                            new ComplexType
                        {
                            IntProperty = 1
                        }
                    }
                }
            };

            var factory   = new PropertyObserverFactory();
            var observer1 = factory.ObservesProperty(commandTestObject, o => o.ComplexProperty.IntProperty);
            var observer2 = factory.ObservesProperty(
                commandTestObject,
                o => o.ComplexProperty.InnerComplexProperty.IntProperty);
            var observer3 = factory.ObservesProperty(
                commandTestObject,
                o => o.ComplexProperty.InnerComplexProperty.InnerComplexProperty.IntProperty);
            var command = new ActivatableCanExecuteObserverCommand <object>(o => { }, observer1, observer2, observer3).Activate();

            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());
        }
Example #13
0
        public void GenericDelegateCommandOfObjectPropertyObserverUnsubscribeUnusedListeners()
        {
            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.ComplexPropertyIntPropertyExpression)
                          .ObservesProperty(
                commandTestObject.ComplexPropertyInnerComplexPropertyInnerComplexPropertyIntPropertyExpression)
                          .ObservesProperty(commandTestObject.ComplexPropertyInnerComplexPropertyIntPropertyExpression);

            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());
        }