#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;
using TestVertex = Microsoft.Xna.Framework.Graphics.VertexPositionColor;
namespace Nuclex.Graphics.Batching {
/// Unit tests for the dynamic buffer based batch drawer
[TestFixture]
internal class DynamicBufferBatchDrawerTest {
#region class Creator
/// Manages the lifetime of a batch drawer instance
private class Creator : IDisposable {
/// Initializes a new batch drawer creator
public Creator() {
this.mockedService = new MockedGraphicsDeviceService(DeviceType.Reference);
this.graphicsDeviceKeeper = this.mockedService.CreateDevice();
try {
this.vertexDeclaration = TestVertex.VertexDeclaration;
this.batchDrawer = new DynamicBufferBatchDrawer(
this.mockedService.GraphicsDevice
);
}
catch(Exception) {
Dispose();
throw;
}
}
/// Immediately releases all resources owned by the instance
public void Dispose() {
if(this.batchDrawer != null) {
this.batchDrawer.Dispose();
this.batchDrawer = null;
}
if(this.graphicsDeviceKeeper != null) {
this.graphicsDeviceKeeper.Dispose();
this.graphicsDeviceKeeper = null;
}
this.mockedService = null;
}
/// Graphics device the batch drawer is using
public GraphicsDevice GraphicsDevice {
get { return this.mockedService.GraphicsDevice; }
}
/// The batch drawer being tested
public DynamicBufferBatchDrawer BatchDrawer {
get { return this.batchDrawer; }
}
/// Mocked graphics device service the drawer operates on
private MockedGraphicsDeviceService mockedService;
/// Keeps the graphics device alive
private IDisposable graphicsDeviceKeeper;
/// Vertex declaration for the vertices we use for testing
private VertexDeclaration vertexDeclaration;
/// Batch drawer being tested
private DynamicBufferBatchDrawer batchDrawer;
}
#endregion // class Creator
///
/// Verifies that the constructor of the user primitive batch drawer works
///
[Test]
public void TestConstructor() {
using(Creator creator = new Creator()) {
Assert.IsNotNull(creator.BatchDrawer);
}
}
///
/// Tests whethe the MaximumBatchSize property of the batcher can be accessed
///
[Test]
public void TestMaximumBatchSize() {
using(Creator creator = new Creator()) {
Assert.Greater(creator.BatchDrawer.MaximumBatchSize, 4);
}
}
///
/// Tests whether the Select() method without vertex indices is working
///
[Test]
public void TestSelectWithoutIndices() {
using(Creator creator = new Creator()) {
TestVertex[] vertices = new TestVertex[9];
creator.BatchDrawer.Select(vertices, 9);
}
}
///
/// Tests whether the Select() method without vertex indices is working
///
[Test]
public void TestSelectMultipleWithoutIndices() {
using(Creator creator = new Creator()) {
TestVertex[] vertices = new TestVertex[9];
// This will cause the drawer to run out of buffer segments and cause
// a discarding lock on the buffers
for(int index = 0; index < 8; ++index) {
creator.BatchDrawer.Select(vertices, 9);
}
}
}
///
/// Tests whether the Select() method with vertex indices is working
///
[Test]
public void TestSelectWithIndices() {
using(Creator creator = new Creator()) {
TestVertex[] vertices = new TestVertex[9];
short[] indices = new short[9];
creator.BatchDrawer.Select(vertices, 9, indices, 9);
}
}
///
/// Tests whether the Select() method with vertex indices is working
///
[Test]
public void TestSelectMultipleWithIndices() {
using(Creator creator = new Creator()) {
TestVertex[] vertices = new TestVertex[9];
short[] indices = new short[9];
// This will cause the drawer to run out of buffer segments and cause
// a discarding lock on the buffers
for(int index = 0; index < 8; ++index) {
creator.BatchDrawer.Select(vertices, 9, indices, 9);
}
}
}
///
/// Tests whether the Draw() method without vertex indices is working
///
[Test]
public void TestDrawWithoutIndices() {
using(Creator creator = new Creator()) {
TestVertex[] vertices = new TestVertex[9];
using(
BasicEffectDrawContext context = new BasicEffectDrawContext(
creator.GraphicsDevice
)
) {
creator.BatchDrawer.Select(vertices, 9);
creator.BatchDrawer.Draw(0, 9, PrimitiveType.TriangleList, context);
}
}
}
///
/// Tests whether the Draw() method with vertex indices is working
///
[Test]
public void TestDrawWithIndices() {
using(Creator creator = new Creator()) {
TestVertex[] vertices = new TestVertex[9];
short[] indices = new short[9];
creator.BatchDrawer.Select(vertices, 9, indices, 9);
creator.BatchDrawer.Draw(
0, 9,
0, 9,
PrimitiveType.TriangleList,
new BasicEffectDrawContext(creator.GraphicsDevice)
);
}
}
}
} // namespace Nuclex.Graphics
#endif // UNITTEST