#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.Content;
using Microsoft.Xna.Framework.Graphics;
using NUnit.Framework;
using Nuclex.Testing.Xna;
namespace Nuclex.Graphics.SpecialEffects.Water {
/// Unit tests for the grid class
[TestFixture]
internal class WaterSurfaceTest {
/// Executed before each test is run
[SetUp]
public void Setup() {
this.mockedGraphicsDeviceService = new MockedGraphicsDeviceService(DeviceType.Reference);
this.mockedGraphicsDeviceService.CreateDevice();
MockedGraphicsDeviceService mockedService = this.mockedGraphicsDeviceService;
this.contentManager = new ResourceContentManager(
GraphicsDeviceServiceHelper.MakePrivateServiceProvider(mockedService),
Resources.UnitTestResources.ResourceManager
);
}
/// Executed after each test has run
[TearDown]
public void Teardown() {
if(this.contentManager != null) {
this.contentManager.Dispose();
this.contentManager = null;
}
if(this.mockedGraphicsDeviceService != null) {
this.mockedGraphicsDeviceService.DestroyDevice();
this.mockedGraphicsDeviceService = null;
}
}
///
/// Verifies that the simple constructor of the Grid class is working
///
[Test]
public void TestSimpleConstructor() {
using(
WaterSurface surface = new WaterSurface(
this.mockedGraphicsDeviceService.GraphicsDevice,
new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f)
)
) {
Assert.IsNotNull(surface); // Nonsense; avoids compiler warning
}
}
///
/// Verifies that the complete constructor of the Grid class is working
///
[Test]
public void TestFullConstructor() {
using(
WaterSurface surface = new WaterSurface(
this.mockedGraphicsDeviceService.GraphicsDevice,
new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
4, 4
)
) {
Assert.IsNotNull(surface); // Nonsense; avoids compiler warning
}
}
///
/// Tests whether the water surface can select its index and vertex buffers
///
[Test]
public void TestSelectIndexAndVertexBuffer() {
using(
WaterSurface surface = new WaterSurface(
this.mockedGraphicsDeviceService.GraphicsDevice,
new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
4, 4
)
) {
GraphicsDevice graphicsDevice = this.mockedGraphicsDeviceService.GraphicsDevice;
graphicsDevice.Indices = null;
graphicsDevice.SetVertexBuffer(null);
Assert.IsNull(graphicsDevice.Indices);
Assert.AreEqual(0, graphicsDevice.GetVertexBuffers().Length);
surface.SelectVertexAndIndexBuffer();
Assert.IsNotNull(graphicsDevice.Indices);
Assert.IsNotNull(graphicsDevice.GetVertexBuffers()[0].VertexBuffer);
}
}
///
/// Tests whether the water surface can draw its water plane
///
[Test]
public void TestDrawWaterPlane() {
Effect waterSurfaceEffect = this.contentManager.Load(
"WaterSurfaceEffect"
);
using(
WaterSurface surface = new WaterSurface(
this.mockedGraphicsDeviceService.GraphicsDevice,
new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
4, 4
)
) {
surface.SelectVertexAndIndexBuffer();
EffectTechnique technique = waterSurfaceEffect.CurrentTechnique;
for (int pass = 0; pass < technique.Passes.Count; ++pass) {
technique.Passes[pass].Apply();
surface.DrawWaterPlane(new GameTime(), Camera.CreateDefaultOrthographic());
}
}
}
///
/// Tests whether the water surface can update its reflection texture
///
[Test]
public void TestUpdateReflection() {
using(
WaterSurface surface = new WaterSurface(
this.mockedGraphicsDeviceService.GraphicsDevice,
new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
4, 4
)
) {
surface.UpdateReflection(
new GameTime(), Camera.CreateDefaultOrthographic(),
new WaterSurface.SceneDrawDelegate(drawReflection)
);
Assert.IsNotNull(surface.ReflectionCamera);
Assert.IsNotNull(surface.ReflectionTexture);
}
}
///
/// Verifies that the water surface can survive a graphics device reset
///
[Test]
public void TestGraphicsDeviceReset() {
using(
WaterSurface surface = new WaterSurface(
this.mockedGraphicsDeviceService.GraphicsDevice,
new Vector2(-10.0f, -10.0f), new Vector2(10.0f, 10.0f),
4, 4
)
) {
this.mockedGraphicsDeviceService.ResetDevice();
surface.UpdateReflection(
new GameTime(), Camera.CreateDefaultOrthographic(),
new WaterSurface.SceneDrawDelegate(drawReflection)
);
Assert.IsNotNull(surface.ReflectionCamera);
Assert.IsNotNull(surface.ReflectionTexture);
}
}
/// Dummy that's supposed to draw the water's reflection
/// Snapshot of the game's timing values
/// Camera containing the viewer's position
private void drawReflection(GameTime gameTime, Camera camera) { }
///
/// Mocked graphics device service used for rendering in the unit test
///
private MockedGraphicsDeviceService mockedGraphicsDeviceService;
///
/// Content manager used to load the content for the unit test
///
private ResourceContentManager contentManager;
}
} // namespace Nuclex.Graphics.SpecialEffects.Water
#endif // UNITTEST