PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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. using System;
  18. using System.Text;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Windows.Input;
  22. using Microsoft.Practices.Composite.Wpf.Commands;
  23. using Microsoft.VisualStudio.TestTools.UnitTesting;
  24. namespace Microsoft.Practices.Composite.Wpf.Tests.Commands
  25. {
  26. /// <summary>
  27. /// Summary description for DelegateCommandFixture
  28. /// </summary>
  29. [TestClass]
  30. public class DelegateCommandFixture
  31. {
  32. [TestMethod]
  33. public void ExecuteCallsPassedInExecuteDelegate()
  34. {
  35. var handlers = new DelegateHandlers();
  36. var command = new DelegateCommand<object>(handlers.Execute, null);
  37. object parameter = new object();
  38. command.Execute(parameter);
  39. Assert.AreSame(parameter, handlers.ExecuteParameter);
  40. }
  41. [TestMethod]
  42. public void CanExecuteCallsPassedInCanExecuteDelegate()
  43. {
  44. var handlers = new DelegateHandlers();
  45. var command = new DelegateCommand<object>(handlers.Execute, handlers.CanExecute);
  46. object parameter = new object();
  47. handlers.CanExecuteReturnValue = true;
  48. bool retVal = command.CanExecute(parameter);
  49. Assert.AreSame(parameter, handlers.CanExecuteParameter);
  50. Assert.AreEqual(handlers.CanExecuteReturnValue, retVal);
  51. }
  52. [TestMethod]
  53. public void CanExecuteReturnsTrueWithouthCanExecuteDelegate()
  54. {
  55. var handlers = new DelegateHandlers();
  56. var command = new DelegateCommand<object>(handlers.Execute);
  57. bool retVal = command.CanExecute(null);
  58. Assert.AreEqual(true, retVal);
  59. }
  60. [TestMethod]
  61. public void RaiseCanExecuteChangedRaisesCanExecuteChanged()
  62. {
  63. var handlers = new DelegateHandlers();
  64. var command = new DelegateCommand<object>(handlers.Execute);
  65. bool canExecuteChangedRaised = false;
  66. command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };
  67. command.RaiseCanExecuteChanged();
  68. Assert.IsTrue(canExecuteChangedRaised);
  69. }
  70. [TestMethod]
  71. public void ShouldAllowOnlyCanExecuteDelegate()
  72. {
  73. bool canExecuteCalled = false;
  74. var command = new DelegateCommand<int>(null, delegate
  75. {
  76. canExecuteCalled = true;
  77. return true;
  78. });
  79. command.CanExecute(0);
  80. Assert.IsTrue(canExecuteCalled);
  81. }
  82. [TestMethod]
  83. public void ShouldPassParameterInstanceOnExecute()
  84. {
  85. bool executeCalled = false;
  86. MyClass testClass = new MyClass();
  87. ICommand command = new DelegateCommand<MyClass>(delegate(MyClass parameter)
  88. {
  89. Assert.AreSame(testClass, parameter);
  90. executeCalled = true;
  91. });
  92. command.Execute(testClass);
  93. Assert.IsTrue(executeCalled);
  94. }
  95. [TestMethod]
  96. public void ShouldPassParameterInstanceOnCanExecute()
  97. {
  98. bool canExecuteCalled = false;
  99. MyClass testClass = new MyClass();
  100. ICommand command = new DelegateCommand<MyClass>(null, delegate(MyClass parameter)
  101. {
  102. Assert.AreSame(testClass, parameter);
  103. canExecuteCalled = true;
  104. return true;
  105. });
  106. command.CanExecute(testClass);
  107. Assert.IsTrue(canExecuteCalled);
  108. }
  109. [TestMethod, ExpectedException(typeof(ArgumentNullException))]
  110. public void ShouldThrowIfAllDelegatesAreNull()
  111. {
  112. var command = new DelegateCommand<int>(null, null);
  113. }
  114. [TestMethod]
  115. public void IsActivePropertyChangeFiresEvent()
  116. {
  117. bool fired = false;
  118. var command = new DelegateCommand<object>(DoNothing);
  119. command.IsActiveChanged += delegate { fired = true; };
  120. command.IsActive = true;
  121. Assert.IsTrue(fired);
  122. }
  123. public void DoNothing(object param)
  124. { }
  125. class DelegateHandlers
  126. {
  127. public bool CanExecuteReturnValue = true;
  128. public object CanExecuteParameter;
  129. public object ExecuteParameter;
  130. public bool CanExecute(object parameter)
  131. {
  132. CanExecuteParameter = parameter;
  133. return CanExecuteReturnValue;
  134. }
  135. public void Execute(object parameter)
  136. {
  137. ExecuteParameter = parameter;
  138. }
  139. }
  140. }
  141. internal class MyClass
  142. {
  143. }
  144. }