PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/StockTrader RI/Desktop/StockTraderRI.Infrastructure.Tests/Behaviors/ReturnCommandBehaviorFixture.cs

#
C# | 107 lines | 75 code | 16 blank | 16 comment | 0 complexity | 807edcf7bcc863ff78c7ec495a40f886 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.Windows.Controls;
  19. using System.Windows.Input;
  20. using Microsoft.VisualStudio.TestTools.UnitTesting;
  21. using StockTraderRI.Infrastructure.Behaviors;
  22. namespace StockTraderRI.Infrastructure.Tests.Behaviors
  23. {
  24. [TestClass]
  25. public class ReturnCommandBehaviorFixture
  26. {
  27. [TestMethod]
  28. public void ShouldExecuteCommandOnEnter()
  29. {
  30. var textBox = new TextBox();
  31. textBox.Text = "MyParameter";
  32. textBox.AcceptsReturn = true;
  33. var command = new MockCommand();
  34. Assert.IsTrue(textBox.AcceptsReturn);
  35. TestableReturnCommandBehavior behavior = new TestableReturnCommandBehavior(textBox);
  36. Assert.IsFalse(textBox.AcceptsReturn);
  37. behavior.Command = command;
  38. behavior.InvokeKeyPressed(Key.Enter);
  39. Assert.IsTrue(command.ExecuteCalled);
  40. Assert.AreEqual("MyParameter", command.ExecuteParameter);
  41. }
  42. [TestMethod]
  43. public void ShouldNotExecuteCommandOnKeyDifferentThanEnter()
  44. {
  45. var textBox = new TextBox();
  46. textBox.Text = "MyParameter";
  47. TestableReturnCommandBehavior behavior = new TestableReturnCommandBehavior(textBox);
  48. var command = new MockCommand();
  49. behavior.Command = command;
  50. behavior.InvokeKeyPressed(Key.Space);
  51. Assert.IsFalse(command.ExecuteCalled);
  52. }
  53. [TestMethod]
  54. public void ShouldSetEmptyTextAfterCommandExecution()
  55. {
  56. var textBox = new TextBox();
  57. var command = new MockCommand();
  58. TestableReturnCommandBehavior behavior = new TestableReturnCommandBehavior(textBox);
  59. behavior.Command = command;
  60. behavior.DefaultTextAfterCommandExecution = "MyDefaultText";
  61. behavior.InvokeKeyPressed(Key.Enter);
  62. Assert.AreEqual(string.Empty, textBox.Text);
  63. }
  64. }
  65. internal class MockCommand : ICommand
  66. {
  67. public bool ExecuteCalled;
  68. public object ExecuteParameter;
  69. public event EventHandler CanExecuteChanged;
  70. public bool CanExecute(object parameter)
  71. {
  72. return true;
  73. }
  74. public void Execute(object parameter)
  75. {
  76. ExecuteCalled = true;
  77. ExecuteParameter = parameter;
  78. }
  79. }
  80. internal class TestableReturnCommandBehavior : ReturnCommandBehavior
  81. {
  82. public TestableReturnCommandBehavior(TextBox textBox)
  83. : base(textBox)
  84. {
  85. }
  86. public void InvokeKeyPressed(Key keyPressed)
  87. {
  88. base.KeyPressed(keyPressed);
  89. }
  90. }
  91. }