PageRenderTime 23ms CodeModel.GetById 7ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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