PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/spikes/AGCompositeApplicationLibrary/AGComposite.Wpf.Tests/Commands/DelegateCommandFixture.cs

#
C# | 173 lines | 125 code | 29 blank | 19 comment | 0 complexity | fad2dd5159c26e90bba4604611bab4f6 MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===============================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===============================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===============================================================================
 17
 18using System;
 19using System.Text;
 20using System.Collections.Generic;
 21using System.Linq;
 22using System.Windows.Input;
 23using Microsoft.Practices.Composite.Wpf.Commands;
 24using Microsoft.VisualStudio.TestTools.UnitTesting;
 25
 26namespace Microsoft.Practices.Composite.Wpf.Tests.Commands
 27{
 28    /// <summary>
 29    /// Summary description for DelegateCommandFixture
 30    /// </summary>
 31    [TestClass]
 32    public class DelegateCommandFixture
 33    {
 34        [TestMethod]
 35        public void ExecuteCallsPassedInExecuteDelegate()
 36        {
 37            var handlers = new DelegateHandlers();
 38            var command = new DelegateCommand<object>(handlers.Execute, null);
 39            object parameter = new object();
 40
 41            command.Execute(parameter);
 42
 43            Assert.AreSame(parameter, handlers.ExecuteParameter);
 44        }
 45
 46        [TestMethod]
 47        public void CanExecuteCallsPassedInCanExecuteDelegate()
 48        {
 49            var handlers = new DelegateHandlers();
 50            var command = new DelegateCommand<object>(handlers.Execute, handlers.CanExecute);
 51            object parameter = new object();
 52
 53            handlers.CanExecuteReturnValue = true;
 54            bool retVal = command.CanExecute(parameter);
 55
 56            Assert.AreSame(parameter, handlers.CanExecuteParameter);
 57            Assert.AreEqual(handlers.CanExecuteReturnValue, retVal);
 58        }
 59
 60        [TestMethod]
 61        public void CanExecuteReturnsTrueWithouthCanExecuteDelegate()
 62        {
 63            var handlers = new DelegateHandlers();
 64            var command = new DelegateCommand<object>(handlers.Execute);
 65
 66            bool retVal = command.CanExecute(null);
 67
 68            Assert.AreEqual(true, retVal);
 69        }
 70
 71
 72        [TestMethod]
 73        public void RaiseCanExecuteChangedRaisesCanExecuteChanged()
 74        {
 75            var handlers = new DelegateHandlers();
 76            var command = new DelegateCommand<object>(handlers.Execute);
 77            bool canExecuteChangedRaised = false;
 78            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };
 79
 80            command.RaiseCanExecuteChanged();
 81
 82            Assert.IsTrue(canExecuteChangedRaised);
 83        }
 84
 85        [TestMethod]
 86        public void ShouldAllowOnlyCanExecuteDelegate()
 87        {
 88            bool canExecuteCalled = false;
 89            var command = new DelegateCommand<int>(null, delegate
 90                                                             {
 91                                                                 canExecuteCalled = true;
 92                                                                 return true;
 93                                                             });
 94            command.CanExecute(0);
 95            Assert.IsTrue(canExecuteCalled);
 96        }
 97
 98        [TestMethod]
 99        public void ShouldPassParameterInstanceOnExecute()
100        {
101            bool executeCalled = false;
102            MyClass testClass = new MyClass();
103            ICommand command = new DelegateCommand<MyClass>(delegate(MyClass parameter)
104                                                                {
105                                                                    Assert.AreSame(testClass, parameter);
106                                                                    executeCalled = true;
107                                                                });
108
109            command.Execute(testClass);
110            Assert.IsTrue(executeCalled);
111        }
112
113        [TestMethod]
114        public void ShouldPassParameterInstanceOnCanExecute()
115        {
116            bool canExecuteCalled = false;
117            MyClass testClass = new MyClass();
118            ICommand command = new DelegateCommand<MyClass>(null, delegate(MyClass parameter)
119                                                                      {
120                                                                          Assert.AreSame(testClass, parameter);
121                                                                          canExecuteCalled = true;
122                                                                          return true;
123                                                                      });
124
125            command.CanExecute(testClass);
126            Assert.IsTrue(canExecuteCalled);
127        }
128
129        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
130        public void ShouldThrowIfAllDelegatesAreNull()
131        {
132            var command = new DelegateCommand<int>(null, null);
133        }
134
135
136        [TestMethod]
137        public void IsActivePropertyChangeFiresEvent()
138        {
139            bool fired = false;
140            var command = new DelegateCommand<object>(DoNothing);
141            command.IsActiveChanged += delegate { fired = true; };
142            command.IsActive = true;
143
144            Assert.IsTrue(fired);
145        }
146
147        public void DoNothing(object param)
148        { }
149
150
151        class DelegateHandlers
152        {
153            public bool CanExecuteReturnValue = true;
154            public object CanExecuteParameter;
155            public object ExecuteParameter;
156
157            public bool CanExecute(object parameter)
158            {
159                CanExecuteParameter = parameter;
160                return CanExecuteReturnValue;
161            }
162
163            public void Execute(object parameter)
164            {
165                ExecuteParameter = parameter;
166            }
167        }
168    }
169
170    internal class MyClass
171    {
172    }
173}