#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 System.Collections.Generic; using Microsoft.Xna.Framework.Input; using NUnit.Framework; using Nuclex.Input; using Nuclex.UserInterface.Input; namespace Nuclex.UserInterface.Controls { /// Unit Test for the pressable control [TestFixture] internal class PressableControlTest { #region class TestPressableControl /// /// Test implementation of a pressable control used in the unit tests /// private class TestPressableControl : PressableControl { /// Called when the control is pressed protected override void OnPressed() { ++this.PressCount; base.OnPressed(); } /// Number of times the button was pressed public int PressCount; } #endregion // class TestPressableControl /// Verifies whether the control can be pressed using the mouse [Test] public void TestPressByMouse() { TestPressableControl pressable = new TestPressableControl(); pressable.Bounds = new UniRectangle(10, 10, 100, 100); // Move the mouse over the control and do a left-click pressable.ProcessMouseMove(0, 0, 50, 50); pressable.ProcessMousePress(MouseButtons.Left); pressable.ProcessMouseRelease(MouseButtons.Left); Assert.AreEqual(1, pressable.PressCount); } /// /// Tests whether a press can be aborted at the last second by moving /// the mouse away from the control before the mouse button is released /// [Test] public void TestLastSecondAbortByMouse() { TestPressableControl pressable = new TestPressableControl(); pressable.Bounds = new UniRectangle(10, 10, 100, 100); // Move the mouse over the control and do a left-click pressable.ProcessMouseMove(0, 0, 50, 50); pressable.ProcessMousePress(MouseButtons.Left); pressable.ProcessMouseMove(0, 0, 5, 5); // outside of the button pressable.ProcessMouseRelease(MouseButtons.Left); Assert.AreEqual(0, pressable.PressCount); } /// Ensures that a disabled control cannot be pressed [Test] public void TestPressDisabledControl() { TestPressableControl pressable = new TestPressableControl(); pressable.Enabled = false; pressable.Bounds = new UniRectangle(10, 10, 100, 100); // Move the mouse over the control and do a left-click pressable.ProcessMouseMove(0, 0, 50, 50); pressable.ProcessMousePress(MouseButtons.Left); pressable.ProcessMouseRelease(MouseButtons.Left); Assert.AreEqual(0, pressable.PressCount); } /// /// Verifies that the pressable control can be pressed using the space bar /// if it is in focus /// [Test] public void TestPressWithSpaceBar() { TestPressableControl pressable = new TestPressableControl(); pressable.Enabled = false; Screen screen = new Screen(); screen.Desktop.Children.Add(pressable); screen.FocusedControl = pressable; // Press the space bar on the control pressable.ProcessKeyPress(Keys.Space, false); pressable.ProcessKeyRelease(Keys.Space); Assert.AreEqual(1, pressable.PressCount); } /// /// Verifies that pressed keys are replayed to a control if those keys have been /// ignore before but now account to the control because it has been activated. /// [Test] public void TestIgnoredKeys() { TestPressableControl pressable = new TestPressableControl(); pressable.Enabled = false; Screen screen = new Screen(); screen.Desktop.Children.Add(pressable); screen.FocusedControl = pressable; // Press the space bar on the control after pressing 'G' screen.InjectKeyPress(Keys.G); screen.InjectKeyPress(Keys.G); screen.InjectKeyPress(Keys.Space); screen.InjectKeyRelease(Keys.G); screen.InjectKeyRelease(Keys.Space); Assert.IsFalse(pressable.Depressed); } /// /// Verifies that sending an invalid command to a control causes an exception /// [Test] public void TestThrowOnInvalidCommand() { TestPressableControl pressable = new TestPressableControl(); Assert.Throws( delegate() { pressable.ProcessCommand((Command)(-1)); } ); } /// /// Verifies that the pressable control can be operated by the game pad /// [Test] public void TestShortcutByGamepad() { TestPressableControl pressable = new TestPressableControl(); pressable.ShortcutButton = Buttons.A; pressable.ProcessButtonPress(Buttons.B); pressable.ProcessButtonRelease(Buttons.B); Assert.AreEqual(0, pressable.PressCount); pressable.ProcessButtonPress(Buttons.A); pressable.ProcessButtonRelease(Buttons.A); Assert.AreEqual(1, pressable.PressCount); } /// /// Tests whether the pressable control can be operated by the keyboard /// /// Shortcut button that will be assigned to the control /// Equivalent key on the keyboard for the shortcut button [ Test, TestCase(Buttons.A, Keys.A), TestCase(Buttons.B, Keys.B), TestCase(Buttons.Back, Keys.Back), TestCase(Buttons.LeftShoulder, Keys.L), TestCase(Buttons.LeftStick, Keys.LeftControl), TestCase(Buttons.RightShoulder, Keys.R), TestCase(Buttons.RightStick, Keys.RightControl), TestCase(Buttons.Start, Keys.Enter), TestCase(Buttons.X, Keys.X), TestCase(Buttons.Y, Keys.Y) ] public void TestShortcutByKeyboard(Buttons button, Keys key) { TestPressableControl pressable = new TestPressableControl(); pressable.ShortcutButton = button; Assert.IsFalse(pressable.ProcessKeyPress(Keys.D, false)); Assert.AreEqual(0, pressable.PressCount); Assert.IsTrue(pressable.ProcessKeyPress(key, false)); pressable.ProcessKeyRelease(key); Assert.AreEqual(1, pressable.PressCount); } /// /// Verifies that the control ignores an invalid shortcut button /// [Test] public void TestIgnoreInvalidShortcut() { TestPressableControl pressable = new TestPressableControl(); pressable.ShortcutButton = (Buttons)(-1); Assert.IsFalse(pressable.ProcessKeyPress(Keys.D, false)); } /// /// Verifies that a shortcut button can be assigned to the control /// [Test] public void TestShortcutAssignment() { TestPressableControl pressable = new TestPressableControl(); Assert.IsFalse(pressable.ShortcutButton.HasValue); pressable.ShortcutButton = Buttons.A; Assert.IsTrue(pressable.ShortcutButton.HasValue); Assert.AreEqual(Buttons.A, pressable.ShortcutButton.Value); } } } // namespace Nuclex.UserInterface.Controls #endif // UNITTEST