#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 NUnit.Framework; using NMock; using Nuclex.Input; using Nuclex.Support; using Nuclex.UserInterface.Input; namespace Nuclex.UserInterface.Controls.Desktop { /// Unit Test for the choice control class [TestFixture] internal class ChoiceControlTest { #region interface IChoiceSubscriber /// Interface for a subscriber to the choice control's events public interface IChoiceSubscriber { /// Called when the choice control's state changes /// Choice control whose state has changed /// Not used void Changed(object sender, EventArgs arguments); } #endregion // interface IChoiceSubscriber /// Called before each test is run [SetUp] public void Setup() { this.mockery = new MockFactory(); this.parent = new Control(); this.child1 = new ChoiceControl(); this.child1.Bounds = new UniRectangle(10, 10, 100, 100); this.child2 = new ChoiceControl(); this.child2.Bounds = new UniRectangle(10, 110, 100, 100); this.parent.Children.Add(this.child1); this.parent.Children.Add(this.child2); } /// Called after each test has run [TearDown] public void Teardown() { if(this.mockery != null) { this.mockery.Dispose(); this.mockery = null; } } /// Verifies that the option can work without a parent [Test] public void TestChoiceWithoutParent() { ChoiceControl choice = new ChoiceControl(); choice.Bounds = new UniRectangle(10, 10, 100, 100); Mock mockedSubscriber = mockSubscriber(choice); mockedSubscriber.Expects.One.Method(m => m.Changed(null, null)).WithAnyArguments(); Assert.IsFalse(choice.Selected); // Move the mouse over the choice and do a left-click choice.ProcessMouseMove(0, 0, 50, 50); choice.ProcessMousePress(MouseButtons.Left); choice.ProcessMouseRelease(MouseButtons.Left); this.mockery.VerifyAllExpectationsHaveBeenMet(); Assert.IsTrue(choice.Selected); } /// /// Verifies that a selected choice remains selected if the user clicks on it /// [Test] public void TestClickOnSelectedChoice() { ChoiceControl choice = new ChoiceControl(); choice.Bounds = new UniRectangle(10, 10, 100, 100); choice.Selected = true; Mock mockedSubscriber = mockSubscriber(choice); mockedSubscriber.Expects.No.Method(m => m.Changed(null, null)).WithAnyArguments(); Assert.IsTrue(choice.Selected); // Move the mouse over the choice and do a left-click choice.ProcessMouseMove(0, 0, 50, 50); choice.ProcessMousePress(MouseButtons.Left); choice.ProcessMouseRelease(MouseButtons.Left); this.mockery.VerifyAllExpectationsHaveBeenMet(); Assert.IsTrue(choice.Selected); } /// /// Verifies that all other choices in the same parent are disabled when /// a choice is clicked on /// [Test] public void TestToggleChoicesByMouse() { this.child1.Selected = true; Mock mockedSubscriber1 = mockSubscriber(this.child1); Mock mockedSubscriber2 = mockSubscriber(this.child2); mockedSubscriber1.Expects.One.Method(m => m.Changed(null, null)).WithAnyArguments(); mockedSubscriber2.Expects.One.Method(m => m.Changed(null, null)).WithAnyArguments(); Assert.IsTrue(this.child1.Selected); Assert.IsFalse(this.child2.Selected); // Move the mouse over the choice and do a left-click this.child2.ProcessMouseMove(0, 0, 50, 150); this.child2.ProcessMousePress(MouseButtons.Left); this.child2.ProcessMouseRelease(MouseButtons.Left); this.mockery.VerifyAllExpectationsHaveBeenMet(); Assert.IsFalse(this.child1.Selected); Assert.IsTrue(this.child2.Selected); } /// /// Tests whether a choice click can be aborted at the last second by moving /// the mouse away from the choice before the mouse button is released /// [Test] public void TestLastSecondAbortByMouse() { this.child1.Selected = true; Mock mockedSubscriber1 = mockSubscriber(this.child1); Mock mockedSubscriber2 = mockSubscriber(this.child2); mockedSubscriber1.Expects.No.Method(m => m.Changed(null, null)).WithAnyArguments(); mockedSubscriber2.Expects.No.Method(m => m.Changed(null, null)).WithAnyArguments(); Assert.IsTrue(this.child1.Selected); Assert.IsFalse(this.child2.Selected); // Move the mouse over the button and do a left-click this.child2.ProcessMouseMove(0, 0, 50, 150); this.child2.ProcessMousePress(MouseButtons.Left); this.child2.ProcessMouseMove(0, 0, 5, 5); // outside of the button this.child2.ProcessMouseRelease(MouseButtons.Left); this.mockery.VerifyAllExpectationsHaveBeenMet(); Assert.IsTrue(this.child1.Selected); Assert.IsFalse(this.child2.Selected); } /// Mocks a subscriber for the events of a choice /// Choice to mock an event subscriber for /// The mocked event subscriber private Mock mockSubscriber(ChoiceControl choice) { Mock mockedSubscriber = this.mockery.CreateMock(); choice.Changed += new EventHandler(mockedSubscriber.MockObject.Changed); return mockedSubscriber; } /// Manages mocked interfaces and verifies expectations private MockFactory mockery; /// Parent control the choice controls are contained in private Control parent; /// First choice control in the parent private ChoiceControl child1; /// Second choice control in the parent private ChoiceControl child2; } } // namespace Nuclex.UserInterface.Controls.Desktop #endif // UNITTEST