#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 DeviceEventHandler = System.EventHandler; namespace Nuclex.Game { /// Unit test for the component class [TestFixture] internal class ComponentTest { #region interface IComponentSubscriber /// Interface for a subscriber to the Component's events public interface IComponentSubscriber { /// /// Called when the value of the Enabled property has changed /// /// Component whose Enabled property has changed /// Not used void EnabledChanged(object sender, EventArgs arguments); /// /// Called when the Component's update order has changed /// /// Component whose update order property has changed /// Not used void UpdateOrderChanged(object sender, EventArgs arguments); } #endregion // interface IComponentSubscriber /// 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 that new component instances can be created [Test] public void TestConstructor() { Component testComponent = new Component(); Assert.IsNotNull(testComponent); // nonsense, avoids compiler warning } /// /// Tests whether the Initialize() method of the Component class can be called /// [Test] public void TestInitialize() { Component testComponent = new Component(); testComponent.Initialize(); // No exception means success } /// /// Tests whether the Update() method of the Component class can be called /// [Test] public void TestUpdate() { Component testComponent = new Component(); testComponent.Update(new GameTime()); // No exception means success } /// /// Tests whether the update order of the Component class can be changed /// [Test] public void TestChangeUpdateOrder() { Component testComponent = new Component(); testComponent.UpdateOrder = 123; Assert.AreEqual(123, testComponent.UpdateOrder); } /// /// Tests whether changing the update order of the Component instance causes /// the update order change event to be triggered /// [Test] public void TestUpdateOrderChangeEvent() { Component testComponent = new Component(); Mock mockedSubscriber = mockSubscriber(testComponent); mockedSubscriber.Expects.One.Method( m => m.UpdateOrderChanged(null, null) ).WithAnyArguments(); testComponent.UpdateOrder = 123; this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// /// Tests whether Component can be enabled or disabled /// [Test] public void TestEnableDisable() { Component testComponent = new Component(); testComponent.Enabled = true; Assert.IsTrue(testComponent.Enabled); testComponent.Enabled = false; Assert.IsFalse(testComponent.Enabled); } /// /// Tests whether enabled or disabling the Component instance causes the /// 'enabled changed' event to be triggered /// [Test] public void TestEnabledChangeEvent() { Component testComponent = new Component(); testComponent.Enabled = true; Mock mockedSubscriber = mockSubscriber(testComponent); mockedSubscriber.Expects.One.Method(m => m.EnabledChanged(null, null)).WithAnyArguments(); testComponent.Enabled = false; 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(Component component) { Mock mockedSubscriber = this.mockery.CreateMock(); component.EnabledChanged += new DeviceEventHandler( mockedSubscriber.MockObject.EnabledChanged ); component.UpdateOrderChanged += new DeviceEventHandler( mockedSubscriber.MockObject.UpdateOrderChanged ); return mockedSubscriber; } /// Mock object factory private MockFactory mockery; } } // namespace Nuclex.Game #endif // UNITTEST