#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 Microsoft.Xna.Framework.Input;
using NUnit.Framework;
using Nuclex.Testing.Xna;
namespace Nuclex.Graphics {
/// Unit tests for the texture region class
[TestFixture]
internal class TextureRegion2DTest {
/// Called before each test is run
[SetUp]
public void Setup() {
this.mockedGraphicsDeviceService = new MockedGraphicsDeviceService();
this.mockedGraphicsDeviceService.CreateDevice();
}
/// Called after each test has run
[TearDown]
public void Teardown() {
if(this.mockedGraphicsDeviceService != null) {
this.mockedGraphicsDeviceService.DestroyDevice();
this.mockedGraphicsDeviceService = null;
}
}
///
/// Tests whether the minimal constructor of the texture region works
///
[Test]
public void TestConstructor() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region = new TextureRegion2D(testTexture);
Assert.AreSame(testTexture, region.Texture);
Assert.AreEqual(Vector2.Zero, region.Min);
Assert.AreEqual(Vector2.One, region.Max);
}
}
///
/// Test whether the explicit texture coordinate constructor is working
///
[Test]
public void TestFloatConstructor() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region = new TextureRegion2D(testTexture, 0.1f, 0.2f, 0.3f, 0.4f);
Assert.AreSame(testTexture, region.Texture);
Assert.AreEqual(new Vector2(0.1f, 0.2f), region.Min);
Assert.AreEqual(new Vector2(0.3f, 0.4f), region.Max);
}
}
///
/// Verifies that a texture region can be built from a texel values
///
[Test]
public void TestFromTexelInts() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 256
)
) {
TextureRegion2D region = TextureRegion2D.FromTexels(
testTexture, 16, 32, 96, 112
);
Vector2 min = new Vector2(16.0f / 128.0f, 32.0f / 256.0f);
Vector2 max = new Vector2(96.0f / 128.0f, 112.0f / 256.0f);
Assert.AreSame(testTexture, region.Texture);
Assert.AreEqual(min, region.Min);
Assert.AreEqual(max, region.Max);
}
}
///
/// Verifies that a texture region can be built from a texel point structures
///
[Test]
public void TestFromTexelPoints() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 256
)
) {
TextureRegion2D region = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(96, 112)
);
Vector2 min = new Vector2(16.0f / 128.0f, 32.0f / 256.0f);
Vector2 max = new Vector2(96.0f / 128.0f, 112.0f / 256.0f);
Assert.AreSame(testTexture, region.Texture);
Assert.AreEqual(min, region.Min);
Assert.AreEqual(max, region.Max);
}
}
///
/// Tests whether two differing instances produce different hash codes
///
[Test]
public void TestHashCodeOnDifferingInstances() {
using(
Texture2D testTexture1 = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 256
)
) {
using(
Texture2D testTexture2 = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
256, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture1, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture1, new Point(80, 96), new Point(112, 128)
);
Assert.AreNotEqual(region1.GetHashCode(), region2.GetHashCode());
}
}
}
///
/// Tests whether two equivalent instances produce an identical hash code
///
[Test]
public void TestHashCodeOnEquivalentInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
Assert.AreEqual(region1.GetHashCode(), region2.GetHashCode());
}
}
/// Tests the Equals() method performing a comparison against null
[Test]
public void TestEqualsOnNull() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
Assert.IsFalse(region.Equals(null));
}
}
/// Tests the Equals() method comparing two equal instances
[Test]
public void TestEqualsWithEquivalentInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
Assert.IsTrue(region1.Equals(region2));
}
}
/// Tests the Equals() method comparing two differing instances
[Test]
public void TestEqualsWithDifferingInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(80, 96), new Point(112, 128)
);
Assert.IsFalse(region1.Equals(region2));
}
}
/// Tests the equality operator with differing instances
[Test]
public void TestEqualityOnDifferingInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(80, 96), new Point(112, 128)
);
Assert.IsFalse(region1 == region2);
}
}
/// Tests the equality operator with equivalent instances
[Test]
public void TestEqualityOnEquivalentInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
Assert.IsTrue(region1 == region2);
}
}
/// Tests the inequality operator with differing instances
[Test]
public void TestInequalityOnDifferingInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(80, 96), new Point(112, 128)
);
Assert.IsTrue(region1 != region2);
}
}
/// Tests the inequality operator with equivalent instances
[Test]
public void TestInequalityOnEquivalentInstances() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region1 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
TextureRegion2D region2 = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
Assert.IsFalse(region1 != region2);
}
}
/// Tests the ToString() method of the string segment
[Test]
public void TestToString() {
using(
Texture2D testTexture = new Texture2D(
this.mockedGraphicsDeviceService.GraphicsDevice,
128, 128
)
) {
TextureRegion2D region = TextureRegion2D.FromTexels(
testTexture, new Point(16, 32), new Point(48, 64)
);
Assert.IsNotNull(region.ToString());
}
}
/// Mocked graphics service used to run the unit tests
private MockedGraphicsDeviceService mockedGraphicsDeviceService;
}
} // namespace Nuclex.Graphics
#endif // UNITTEST