#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 Nuclex.Testing.Xna;
namespace Nuclex.Graphics.SpecialEffects {
/// Unit test for the indexed mesh graphics resource keeper
[TestFixture]
internal class IndexedStaticMeshTest {
#region struct TestVertex
///
/// Vertex used to unit-test the static mesh graphics resource keepr
///
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 TestIndexedStaticMesh : IndexedStaticMesh {
///
/// Initializes a new static mesh that automatically determines its vertex format
///
/// Graphics device the static mesh lives on
/// Number of vertices used by the static mesh
/// Number of indices in the static mesh
public TestIndexedStaticMesh(
GraphicsDevice graphicsDevice, int vertexCount, int indexCount
) :
base(graphicsDevice, vertexCount, indexCount) { }
/// Selects the static meshes' vertex buffer
public new void Select() {
base.Select();
}
/// Index buffer containing the test meshes' indices
public new IndexBuffer IndexBuffer {
get { return base.IndexBuffer; }
}
}
#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(
TestIndexedStaticMesh test = new TestIndexedStaticMesh(
mockGraphicsDeviceService.GraphicsDevice, 4, 4
)
) { }
}
}
///
/// Verifies that the simple constructor rolls back when an exception occurs in it
///
[Test]
public void TestThrowInSimpleConstructorRollback() {
MockedGraphicsDeviceService mockGraphicsDeviceService =
new MockedGraphicsDeviceService();
using(IDisposable keeper = mockGraphicsDeviceService.CreateDevice()) {
Assert.Throws(
delegate() {
using(
TestIndexedStaticMesh test = new TestIndexedStaticMesh(
mockGraphicsDeviceService.GraphicsDevice, 4, -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(
TestIndexedStaticMesh test = new TestIndexedStaticMesh(
mockGraphicsDeviceService.GraphicsDevice, 4, 4
)
) {
test.Select();
Assert.AreSame(
test.IndexBuffer,
mockGraphicsDeviceService.GraphicsDevice.Indices
);
}
}
}
///
/// 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