A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'True'.
Inheritance: RelayCommandBase
        public void RelayCommandOfT_Ctor_EmptyPredicate_CanAlwaysExecute()
        {
            // Setup
            var command = new RelayCommand<object>(x => { });

            // Act + Verify
            Assert.IsTrue(command.CanExecute(null));
        }
        public void RelayCommand_Ctor_EmptyPredicate_CanAlwaysExecute()
        {
            // Setup
            var command = new RelayCommand(() => { });

            // Act + Verify
            Assert.IsTrue(command.CanExecute());
        }
 public void ContextualCommandViewModel_Ctor_NullArgChecks()
 {
     var command = new RelayCommand(() => { });
     ContextualCommandViewModel suppressAnalysisWarning;
     Exceptions.Expect<ArgumentNullException>(() =>
     {
         suppressAnalysisWarning = new ContextualCommandViewModel(null, command);
     });
 }
        /// <summary>
        /// Creates an instance of contextual command view model
        /// </summary>
        /// <param name="fixedContext">Required context</param>
        /// <param name="command">Optional real command to trigger and pass the fixed context to</param>
        public ContextualCommandViewModel(object fixedContext, ICommand command)
        {
            if (fixedContext == null)
            {
                throw new ArgumentNullException(nameof(fixedContext));
            }

            this.fixedContext = fixedContext;
            this.proxyCommand = new RelayCommand(this.Execute, this.CanExecute);
            this.SetCommand(command);
        }
        public void ContextualCommandViewModel_CommandInvocation()
        {
            // Setup
            bool canExecute = false;
            bool executed = false;
            var realCommand = new RelayCommand<object>(
                (state) => 
                {
                    Assert.AreEqual(this, state);
                    executed = true;
                },
                (state) => 
                {
                    Assert.AreEqual(this, state);
                    return canExecute;
                });
            var testSubject = new ContextualCommandViewModel(this, realCommand);

            // Sanity
            Assert.IsNotNull(testSubject.Command);
            Assert.AreSame(realCommand, testSubject.InternalRealCommand);

            // Case 1: Can't execute
            canExecute = false;
            // Act
            Assert.IsFalse(testSubject.Command.CanExecute(null), "CanExecute wasn't called as expected");

            // Case 2: Can execute
            canExecute = true;

            // Act
            Assert.IsTrue(testSubject.Command.CanExecute(null), "CanExecute wasn't called as expected");

            // Case 3: Execute
            // Act
            testSubject.Command.Execute(null);
            Assert.IsTrue(executed, "Execute wasn't called as expected");
        }
        public void ContextualCommandViewModel_DisplayText()
        {
            // Setup
            var context = new object();
            var command = new RelayCommand(() => { });
            var testSubject = new ContextualCommandViewModel(context, command);

            using (var tracker = new PropertyChangedTracker(testSubject))
            {
                // Case 1: null
                // Act + Verify
                Assert.IsNull(testSubject.DisplayText, "Expected display text to return null when not set");

                // Case 2: static
                testSubject.DisplayText = "foobar9000";
                // Act + Verify
                Assert.AreEqual("foobar9000", testSubject.DisplayText, "Unexpected static display text");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.DisplayText), 1);

                // Case 3: dynamic
                var funcInvoked = false;
                Func<object, string> func = x => 
                {
                    funcInvoked = true;
                    return "1234";
                };
                testSubject.SetDynamicDisplayText(func);
                // Act + Verify
                Assert.AreEqual("1234", testSubject.DisplayText, "Unexpected dynamic display text");
                Assert.IsTrue(funcInvoked, "Dynamic display text function was not invoked");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.DisplayText), 2);
            }

            // Case 4: dynamic - null exception
            Exceptions.Expect<ArgumentNullException>(() => testSubject.SetDynamicDisplayText(null));
        }
        public void ContextualCommandViewModel_Icon()
        {
            // Setup
            var context = new object();
            var command = new RelayCommand(() => { });
            var testSubject = new ContextualCommandViewModel(context, command);

            using (var tracker = new PropertyChangedTracker(testSubject))
            {
                // Case 1: null
                // Act + Verify
                Assert.IsNull(testSubject.Icon, "Expected icon to return null when not set");

                // Case 2: static
                var staticIcon = new IconViewModel(null);
                testSubject.Icon = staticIcon;
                // Act + Verify
                Assert.AreSame(staticIcon, testSubject.Icon, "Unexpected static icon");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.Icon), 1);

                // Case 3: dynamic
                var dynamicIcon = new IconViewModel(null);
                var funcInvoked = false;
                Func<object, IconViewModel> func = x => 
                {
                    funcInvoked = true;
                    return dynamicIcon;
                };
                testSubject.SetDynamicIcon(func);
                // Act + Verify
                Assert.AreSame(dynamicIcon, testSubject.Icon, "Unexpected dynamic icon");
                Assert.IsTrue(funcInvoked, "Dynamic icon function  was not invoked");
                tracker.AssertPropertyChangedRaised(nameof(testSubject.Icon), 2);
            }

            // Case 4: dynamic - null exception
            Exceptions.Expect<ArgumentNullException>(() => testSubject.SetDynamicIcon(null));
        }