#region CPL License /* Nuclex Framework Copyright (C) 2002-2010 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion #if UNITTEST using System; using Nuclex.Support; using NUnit.Framework; using NMock; using Nuclex.Input; using Nuclex.UserInterface.Input; namespace Nuclex.UserInterface.Controls.Desktop { /// Unit Test for the button control class [TestFixture] internal class ButtonControlTest { #region interface IButtonSubscriber /// Interface for a subscriber to the button control's events public interface IButtonSubscriber { /// Called when the button control is pressed /// Button control that has been pressed /// Not used void Pressed(object sender, EventArgs arguments); } #endregion // interface IButtonSubscriber /// Called before each test is run [SetUp] public void Setup() { this.mockery = new MockFactory(); } /// Called after each test has run [TearDown] public void Teardown() { if(this.mockery != null) { this.mockery.Dispose(); this.mockery = null; } } /// Verifies whether the button can be pressed using the mouse [Test] public void TestButtonPressByMouse() { ButtonControl button = new ButtonControl(); button.Bounds = new UniRectangle(10, 10, 100, 100); Mock mockedSubscriber = mockSubscriber(button); mockedSubscriber.Expects.One.Method(m => m.Pressed(null, null)).WithAnyArguments(); // Move the mouse over the button and do a left-click button.ProcessMouseMove(0, 0, 50, 50); button.ProcessMousePress(MouseButtons.Left); button.ProcessMouseRelease(MouseButtons.Left); this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// /// Tests whether a button press can be aborted at the last second by moving /// the mouse away from the button before the mouse button is released /// [Test] public void TestLastSecondAbortByMouse() { ButtonControl button = new ButtonControl(); button.Bounds = new UniRectangle(10, 10, 100, 100); Mock mockedSubscriber = mockSubscriber(button); mockedSubscriber.Expects.No.Method(m => m.Pressed(null, null)).WithAnyArguments(); // Move the mouse over the button and do a left-click button.ProcessMouseMove(0, 0, 50, 50); button.ProcessMousePress(MouseButtons.Left); button.ProcessMouseMove(0, 0, 5, 5); // outside of the button button.ProcessMouseRelease(MouseButtons.Left); this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// Mocks a subscriber for the events of a button /// Button to mock an event subscriber for /// The mocked event subscriber private Mock mockSubscriber(ButtonControl button) { Mock mockedSubscriber = this.mockery.CreateMock(); button.Pressed += new EventHandler(mockedSubscriber.MockObject.Pressed); return mockedSubscriber; } /// Manages mocked interfaces and verifies expectations private MockFactory mockery; } } // namespace Nuclex.UserInterface.Controls.Desktop #endif // UNITTEST