#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 System.Runtime.InteropServices; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using NUnit.Framework; using Nuclex.Testing.Xna; namespace Nuclex.Graphics.SpecialEffects { /// Unit test for the static mesh graphics resource keeper [TestFixture] internal class StaticMeshTest { #region struct TestVertex /// /// Vertex used to unit-test the static mesh graphics resource keepr /// [StructLayout(LayoutKind.Sequential)] private struct TestVertex : IVertexType { /// A vertex element of type Vector2 [VertexElement(VertexElementUsage.Position)] public Vector3 Position; /// A vertex element of type Color [VertexElement(VertexElementUsage.Color)] public Color Color; /// Provides a declaration for this vertex type VertexDeclaration IVertexType.VertexDeclaration { get { return TestVertex.VertexDeclaration; } } /// Vertex declaration for this vertex structure public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(VertexDeclarationHelper.BuildElementList()); } #endregion // struct TestVertex #region class TestStaticMesh /// Dummy static mesh class used for unit testing private class TestStaticMesh : StaticMesh { /// /// Initializes a new static mesh that automatically determines its vertex format /// /// Graphics device the static mesh lives on /// Number of vertices in the static mesh public TestStaticMesh(GraphicsDevice graphicsDevice, int vertexCount) : base(graphicsDevice, vertexCount) { } /// Selects the static meshes' vertex buffer public new void Select() { base.Select(); } /// Vertex buffer containing the test meshes' vertices public new VertexBuffer VertexBuffer { get { return base.VertexBuffer; } } } #endregion // class TestStaticMesh /// /// Verifies that the simple constructor of the static mesh class is working /// [Test] public void TestSimpleConstructor() { MockedGraphicsDeviceService mockGraphicsDeviceService = new MockedGraphicsDeviceService(); using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) { using( TestStaticMesh test = new TestStaticMesh( mockGraphicsDeviceService.GraphicsDevice, 4 ) ) { } } } /// /// Verifies that the constructor rolls back when an exception occurs in it /// [Test] public void TestThrowInConstructorRollback() { MockedGraphicsDeviceService mockGraphicsDeviceService = new MockedGraphicsDeviceService(); using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) { Assert.Throws( delegate() { using( TestStaticMesh test = new TestStaticMesh( mockGraphicsDeviceService.GraphicsDevice, -1 ) ) { } } ); } } /// /// Tests whether the static meshes' Select() method is implemented correctly /// [Test] public void TestSelect() { MockedGraphicsDeviceService mockGraphicsDeviceService = new MockedGraphicsDeviceService(); using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) { using( TestStaticMesh test = new TestStaticMesh( mockGraphicsDeviceService.GraphicsDevice, 4 ) ) { test.Select(); Assert.AreSame( test.VertexBuffer, mockGraphicsDeviceService.GraphicsDevice.GetVertexBuffers()[0].VertexBuffer ); } } } /// /// Only exists to prevent the compiler from complaining about unused fields /// protected void AvoidCompilerWarnings() { TestVertex v; v.Color = Color.Red; v.Position = Vector3.Zero; } } } // namespace Nuclex.Graphics.SpecialEffects #endif // UNITTEST