#region CPL License /* Nuclex Framework Copyright (C) 2002-2009 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.Testing.Xna { /// Unit tests for the graphics device mock test helper [TestFixture] public class MockedGraphicsDeviceServiceTest { #region interface IGraphicsDeviceServiceSubscriber /// Subscriber for the event of the graphics device service public interface IGraphicsDeviceServiceSubscriber { /// Called when a graphics device has been created /// /// Graphics device service that created a graphics device /// /// Not used void DeviceCreated(object sender, EventArgs arguments); /// Called when a graphics device is about to be destroyed /// /// Graphics device service that is about to destroy its graphics device /// /// Not used void DeviceDisposing(object sender, EventArgs arguments); /// Called when the graphics device is about to reset itself /// /// Graphics device service whose graphics device is about to reset itself /// /// Not used void DeviceResetting(object sender, EventArgs arguments); /// Called when the graphics device has completed a reset /// /// Graphics device service whose graphics device has completed a reset /// /// Not used void DeviceReset(object sender, EventArgs arguments); } #endregion // interface IGraphicsDeviceSubscriber /// Initialization routine executed before each test is run [SetUp] public void Setup() { this.mockery = new MockFactory(); } /// Tests whether the mock's service provider is set up correctly [Test] public void TestServiceProvider() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(); IServiceProvider serviceProvider = mock.ServiceProvider; Assert.IsNotNull(serviceProvider); IGraphicsDeviceService service = (IGraphicsDeviceService)serviceProvider.GetService( typeof(IGraphicsDeviceService) ); Assert.AreSame(mock, service); } /// Tests whether a graphics device can be created [Test] public void TestGraphicsDeviceCreation() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(); using(IDisposable keeper = mock.CreateDevice()) { Assert.IsNotNull(mock.GraphicsDevice); } } /// /// Verifies that the graphics device is destroyed when the keeper returned /// by the CreateDevice() method gets disposed explicitely. /// [Test] public void TestAutomaticGraphicsDeviceDestruction() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(); try { using(IDisposable keeper = mock.CreateDevice()) { Assert.IsNotNull(mock.GraphicsDevice); throw new ArithmeticException("Test exception"); } } catch(ArithmeticException) { // Munch } Assert.IsNull(mock.GraphicsDevice); } /// /// Verifies that the mocked graphics device service fires its events /// [Test] public void TestGraphicsDeviceServiceEvents() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(); Mock mockedSubscriber = mockSubscriber(mock); mockedSubscriber.Expects.One.Method( m => m.DeviceCreated(null, null) ).WithAnyArguments(); using(IDisposable keeper = mock.CreateDevice()) { this.mockery.VerifyAllExpectationsHaveBeenMet(); mockedSubscriber.Expects.One.Method( m => m.DeviceResetting(null, null) ).WithAnyArguments(); mockedSubscriber.Expects.One.Method( m => m.DeviceReset(null, null) ).WithAnyArguments(); mock.ResetDevice(); this.mockery.VerifyAllExpectationsHaveBeenMet(); mockedSubscriber.Expects.One.Method( m => m.DeviceDisposing(null, null) ).WithAnyArguments(); } this.mockery.VerifyAllExpectationsHaveBeenMet(); } /// /// Tests whether the graphics device can be destroyed manually even /// though it the RAII helper is used without causing an exception /// [Test] public void TestRedundantDestroyInvocation() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(); using(IDisposable keeper = mock.CreateDevice()) { mock.DestroyDevice(); } // should not cause an exception } /// /// Verifies that the mocked graphics device service cleans up the graphics /// device and all of its resources again when an exception occurs during /// its creation /// [Test] public void TestExceptionDuringDeviceCreation() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService(); Mock mockedSubscriber = mockSubscriber(mock); mockedSubscriber.Expects.One.Method( m => m.DeviceCreated(null, null) ).WithAnyArguments(); mock.DeviceCreated += (DeviceEventHandler)delegate(object sender, EventArgs arguments) { Assert.IsNotNull(mock.GraphicsDevice); throw new ArithmeticException("Test exception"); }; try { mock.CreateDevice(); } catch(ArithmeticException) { // Munch } Assert.IsNull(mock.GraphicsDevice); } /// /// Verifies that the mocked graphics device service can cope with /// a NotSupportedException when the reference rasterizer is selected /// [Test] public void TestNotSupportedExceptionForReferenceRasterizer() { MockedGraphicsDeviceService mock = new MockedGraphicsDeviceService( DeviceType.Reference ); mock.DeviceCreated += delegate(object sender, EventArgs arguments) { throw new InvalidOperationException("Simulated error for unit testing"); }; Console.Error.WriteLine( "The next line should contain an error message indicating that the reference " + "rasterizer could not be created" ); Assert.Throws( delegate() { mock.CreateDevice(); mock.DestroyDevice(); } ); } /// /// Mocks a subscriber for the events of the mocked graphics device service /// /// The mocked event subscriber private Mock mockSubscriber( IGraphicsDeviceService graphicsDeviceService ) { Mock mockedSubscriber = this.mockery.CreateMock(); graphicsDeviceService.DeviceCreated += new DeviceEventHandler( mockedSubscriber.MockObject.DeviceCreated ); graphicsDeviceService.DeviceResetting += new DeviceEventHandler( mockedSubscriber.MockObject.DeviceResetting ); graphicsDeviceService.DeviceReset += new DeviceEventHandler( mockedSubscriber.MockObject.DeviceReset ); graphicsDeviceService.DeviceDisposing += new DeviceEventHandler( mockedSubscriber.MockObject.DeviceDisposing ); return mockedSubscriber; } /// Mock object factory private MockFactory mockery; } } // namespace Nuclex.Testing.Xna #endif // UNITTEST