#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 option control class
[TestFixture]
internal class OptionControlTest {
#region interface IOptionSubscriber
/// Interface for a subscriber to the option control's events
public interface IOptionSubscriber {
/// Called when the option control's state changes
/// Option control whose state has changed
/// Not used
void Changed(object sender, EventArgs arguments);
}
#endregion // interface IOptionSubscriber
/// 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 option can be changed using the mouse
[Test]
public void TestOptionToggleByMouse() {
OptionControl option = new OptionControl();
option.Bounds = new UniRectangle(10, 10, 100, 100);
Mock mockedSubscriber = mockSubscriber(option);
mockedSubscriber.Expects.One.Method(m => m.Changed(null, null)).WithAnyArguments();
Assert.IsFalse(option.Selected);
// Move the mouse over the button and do a left-click
option.ProcessMouseMove(0, 0, 50, 50);
option.ProcessMousePress(MouseButtons.Left);
option.ProcessMouseRelease(MouseButtons.Left);
this.mockery.VerifyAllExpectationsHaveBeenMet();
Assert.IsTrue(option.Selected);
}
///
/// 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() {
OptionControl option = new OptionControl();
option.Bounds = new UniRectangle(10, 10, 100, 100);
Mock mockedSubscriber = mockSubscriber(option);
mockedSubscriber.Expects.No.Method(m => m.Changed(null, null)).WithAnyArguments();
Assert.IsFalse(option.Selected);
// Move the mouse over the button and do a left-click
option.ProcessMouseMove(0, 0, 50, 50);
option.ProcessMousePress(MouseButtons.Left);
option.ProcessMouseMove(0, 0, 5, 5); // outside of the button
option.ProcessMouseRelease(MouseButtons.Left);
this.mockery.VerifyAllExpectationsHaveBeenMet();
Assert.IsFalse(option.Selected);
}
/// Mocks a subscriber for the events of an option
/// Option to mock an event subscriber for
/// The mocked event subscriber
private Mock mockSubscriber(OptionControl option) {
Mock mockedSubscriber = this.mockery.CreateMock();
option.Changed += new EventHandler(mockedSubscriber.MockObject.Changed);
return mockedSubscriber;
}
/// Manages mocked interfaces and verifies expectations
private MockFactory mockery;
}
} // namespace Nuclex.UserInterface.Controls.Desktop
#endif // UNITTEST