#region CPL License /* Nuclex Framework Copyright (C) 2002-2011 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; using Microsoft.Xna.Framework.Graphics; using NUnit.Framework; using NMock; using Nuclex.Graphics; using Nuclex.Testing.Xna; using DeviceEventHandler = System.EventHandler; namespace Nuclex.Game { /// Unit test for the drawable component class [TestFixture] internal class DrawableComponentTest { #region interface IDrawableComponentSubscriber /// Interface for a subscriber to the DrawableComponent's events public interface IDrawableComponentSubscriber { /// /// Called when the component's drawing order has changed /// /// Component whose drawing order property has changed /// Not used void DrawOrderChanged(object sender, EventArgs arguments); /// /// Called when the Component's visible property has changed /// /// Component whose visible property has changed /// Not used void VisibleChanged(object sender, EventArgs arguments); } #endregion // interface IDrawableComponentSubscriber /// Called before each test is run [SetUp] public void Setup() { this.mockery = new MockFactory(); this.testComponent = new DrawableComponent(); } /// Called after each test has run [TearDown] public void Teardown() { if(this.mockery != null) { this.mockery.Dispose(); this.mockery = null; } } /// /// Verifies that the constructor of the drawable component is working /// [Test] public void TestConstructor() { DrawableComponent testComponent = new DrawableComponent(); } /// Tests whether the Initialize() method is working [Test] public void TestInitialize() { this.testComponent.Initialize(); } /// /// Tests whether the drawable component can draw itself /// [Test] public void TestDraw() { this.testComponent.Initialize(); this.testComponent.Draw(new GameTime()); } /// Verifies that the DrawOrder property is working correctly [Test] public void TestDrawOrder() { this.testComponent.DrawOrder = 4321; Assert.AreEqual(4321, this.testComponent.DrawOrder); } /// /// Verifies that the DrawOrder change event is triggered when the drawing order /// of the component is changed /// [Test] public void TestDrawOrderChangedEvent() { Mock subscriber = mockSubscriber(this.testComponent); subscriber.Expects.One.Method(m => m.DrawOrderChanged(null, null)).WithAnyArguments(); this.testComponent.DrawOrder = 4321; this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// Verifies that the Visible property is working correctly [Test] public void TestVisible() { this.testComponent.Visible = false; Assert.IsFalse(this.testComponent.Visible); this.testComponent.Visible = true; Assert.IsTrue(this.testComponent.Visible); } /// /// Verifies that the visible change event is triggered when the visibility /// of the component is changed /// [Test] public void TestVisibleChangedEvent() { this.testComponent.Visible = false; Mock subscriber = mockSubscriber(this.testComponent); subscriber.Expects.One.Method(m => m.VisibleChanged(null, null)).WithAnyArguments(); this.testComponent.Visible = true; this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// Mocks a subscriber for the events of a tracker /// Component to mock an event subscriber for /// The mocked event subscriber private Mock mockSubscriber( DrawableComponent drawableComponent ) { Mock mockedSubscriber = this.mockery.CreateMock(); drawableComponent.DrawOrderChanged += new DeviceEventHandler( mockedSubscriber.MockObject.DrawOrderChanged ); drawableComponent.VisibleChanged += new DeviceEventHandler( mockedSubscriber.MockObject.VisibleChanged ); return mockedSubscriber; } /// Mock object factory private MockFactory mockery; /// Component being tested private DrawableComponent testComponent; } } // namespace Nuclex.Game #endif // UNITTEST