Esempio n. 1
0
        public void GenericObservableCommandOfObjectShouldObserveCanExecuteAndObserveOtherProperties()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var factory            = new PropertyObserverFactory();
            var canExecuteObserver = factory.ObservesCanExecute(commandTestObject, o => o.BoolProperty);
            var observer1          = factory.ObservesProperty(commandTestObject, o => o.IntProperty);

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

            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));
        }
Esempio n. 2
0
 public void ShouldThrowIfExecuteMethodDelegateNull()
 {
     Assert.Throws <ArgumentNullException>(
         () =>
     {
         var command = new ActivatableCanExecuteObserverCommand <object>(null, new DummyObserver());
     });
 }
Esempio n. 3
0
 public void ShouldThrowIfAllDelegatesAreNull()
 {
     Assert.Throws <ArgumentNullException>(
         () =>
     {
         var command = new ActivatableCanExecuteObserverCommand <object>(null, null as IPropertyObserver);
     });
 }
        public void CanExecuteReturnsTrueWithoutCanExecuteDelegate()
        {
            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatableCanExecuteObserverCommand <object>(handlers.Execute, new CommandManagerObserver());

            var condition = command.CanExecute(null);

            Assert.True(condition);
        }
        public void WhenConstructedWithGenericTypeOfNullable_InitializesValues()
        {
            // Prepare

            // Act
            var actual = new ActivatableCanExecuteObserverCommand <int?>(param => { }, new CommandManagerObserver());

            // verify
            Assert.NotNull(actual);
        }
Esempio n. 6
0
        public void WhenConstructedWithGenericTypeOfObject_InitializesValues()
        {
            // Prepare

            // Act
            var actual = new ActivatableCanExecuteObserverCommand <object>(param => { }, new DummyObserver());

            // verify
            Assert.NotNull(actual);
        }
        public void RaiseCanExecuteChangedNoRaiseCanExecuteChanged()
        {
            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatableCanExecuteObserverCommand <object>(handlers.Execute, o => true, new CommandManagerObserver());
            var canExecuteChangedRaised = false;

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

            Assert.False(canExecuteChangedRaised);
        }
        public void ExecuteCallsOfTPassedInExecuteDelegate()
        {
            var handlers  = new DelegateObjectHandlers();
            var command   = new ActivatableCanExecuteObserverCommand <object>(handlers.Execute, new CommandManagerObserver());
            var parameter = new object();

            command.Execute(parameter);

            Assert.AreSame(parameter, handlers.ExecuteParameter);
        }
        public void ExecuteCallsCanExecuteFalse()
        {
            var handlers = new DelegateObjectHandlers();
            var command  =
                new ActivatableCanExecuteObserverCommand <object>(handlers.Execute, o => false, new CommandManagerObserver()) as ICommand;
            var parameter = new object();

            command.Execute(parameter);

            Assert.AreSame(null, handlers.ExecuteParameter);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        public void RaiseCanExecuteChangedRaisesCanExecuteChanged()
        {
            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatableCanExecuteObserverCommand <object>(handlers.Execute, new DummyObserver());
            var canExecuteChangedRaised = false;

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

            command.RaisePropertyChanged();

            Assert.True(canExecuteChangedRaised);
        }
        public void GenericObservableCommandNotObservingPropertiesShouldNotRaiseOnEmptyPropertyName()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var command = new ActivatableCanExecuteObserverCommand <object>(o => { }, new CommandManagerObserver());

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

            commandTestObject.RaisePropertyChanged(null);

            Assert.False(canExecuteChangedRaised);
        }
Esempio n. 13
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);
            });
        }
        public void RaiseCanExecuteChangedRaisesCanExecuteChanged()
        {
            var canExecuteChangedRaised = false;

            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatableCanExecuteObserverCommand <object>(handlers.Execute, true, o => false, new CommandManagerObserver());

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

            CommandManager.InvalidateRequerySuggested();
            Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(() => { }));

            Assert.True(canExecuteChangedRaised);
        }
        public void CanExecuteCallsPassedInCanExecuteDelegate()
        {
            var handlers = new DelegateObjectHandlers();
            var command  = new ActivatableCanExecuteObserverCommand <object>(
                handlers.Execute,
                handlers.CanExecute,
                new CommandManagerObserver());
            var parameter = new object();

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

            Assert.AreSame(parameter, handlers.CanExecuteParameter);
            Assert.AreEqual(handlers.CanExecuteReturnValue, actual);
        }
Esempio n. 16
0
        public void ShouldPassParameterInstanceOnExecute()
        {
            var      executeCalled = false;
            var      testClass     = new MyClass();
            ICommand command       = new ActivatableCanExecuteObserverCommand <MyClass>(
                delegate(MyClass parameter)
            {
                Assert.AreSame(testClass, parameter);
                executeCalled = true;
            },
                new DummyObserver());

            command.Execute(testClass);
            Assert.True(executeCalled);
        }
Esempio n. 17
0
        public void GenericObservableCommandOfObjectShouldObserveOneProperty()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var factory   = new PropertyObserverFactory();
            var observer1 = factory.ObservesProperty(commandTestObject.IntPropertyExpression);

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

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

            commandTestObject.IntProperty = 10;

            Assert.True(canExecuteChangedRaised);
        }
        public void CanRemoveCanExecuteChangedHandler()
        {
            var command = new ActivatableCanExecuteObserverCommand <object>(o => { }, new CommandManagerObserver());
            var canExecuteChangedRaised = false;

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

            command.CanExecuteChanged += Handler;
            command.CanExecuteChanged -= Handler;

            CommandManager.InvalidateRequerySuggested();
            Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(() => { }));

            Assert.False(canExecuteChangedRaised);
        }
        public void ShouldPassParameterInstanceOnCanExecute()
        {
            var      canExecuteCalled = false;
            var      testClass        = new MyClass();
            ICommand command          = new ActivatableCanExecuteObserverCommand <MyClass>(
                p => { },
                delegate(MyClass parameter)
            {
                Assert.AreSame(testClass, parameter);
                canExecuteCalled = true;
                return(true);
            },
                new CommandManagerObserver());

            command.CanExecute(testClass);

            Assert.True(canExecuteCalled);
        }
Esempio n. 20
0
        public void GenericObservableCommandOfNullableIntWithNullableParameterShouldObserveCanExecute()
        {
            var canExecuteChangedRaised = false;
            var commandTestObject       = new CommandTestObject();
            var factory   = new PropertyObserverFactory();
            var observer1 = factory.ObservesCanExecute(commandTestObject.BoolPropertyExpression);

            ICommand command = new ActivatableCanExecuteObserverCommand <int?>(o => { }, observer1).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));
        }
Esempio n. 21
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);
        }
Esempio n. 22
0
        public PropertyObservableTestViewModel()
        {
            var commandFactory = CommandBuilder.Builder;
            //var canExecuteObserverAnd =
            //    new PropertyObserverFactory().ObservesCanExecute(() => this.Condition1 && this.Condition2);
            //TestAndCommand = new ActivatableCanExecuteObserverCommand(() => { }, canExecuteObserverAnd);

            Action syncExecution = () => { };
            Action<CancellationToken> concurrencySyncExecution = (c) => { };
            Func<CancellationToken, Task> concurrencyAsyncExecution = async (c) => await Task.Yield();

            IConcurrencyAsyncCommand testAndCommand;
            TestAndCommand = testAndCommand = commandFactory
                .Command(concurrencyAsyncExecution)
                .ObservesCanExecute(() => this.Condition1 && this.Condition2)
                .Build();

            ((IActivatable)testAndCommand).Activate();

            var canExecuteObserverOr =
                new PropertyObserverFactory().ObservesCanExecute(() => this.Condition1 || this.Condition2);
            TestOrCommand = new ActivatableCanExecuteObserverCommand(() => { }, canExecuteObserverOr);
            TestOrCommand.Activate();
        }
Esempio n. 23
0
        public void WhenConstructedWithGenericTypeIsNonNullableValueType_Throws()
        {
            var command = new ActivatableCanExecuteObserverCommand <int>(param => { }, new DummyObserver());

            command.Execute(1);
        }
Esempio n. 24
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());
        }