#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2010 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.Globalization;
using System.IO;
using System.Resources;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using NUnit.Framework;
using Nuclex.Testing.Xna;
namespace Nuclex.UserInterface.Visuals.Flat {
/// Unit tests for flat GUI graphics interface
[TestFixture]
internal class FlatGuiGraphicsTest {
/// Called before each test is run
[SetUp]
public void Setup() {
this.mockedGraphicsDeviceService = new MockedGraphicsDeviceService(
DeviceType.Reference
);
this.mockedGraphicsDeviceService.CreateDevice();
}
private void loadSkin(ResourceManager resourceManager, byte[] skinDescription) {
this.contentManager = new ResourceContentManager(
this.mockedGraphicsDeviceService.ServiceProvider, resourceManager
);
using(
MemoryStream skinStream = new MemoryStream(skinDescription, false)
) {
this.graphics = new FlatGuiGraphics(this.contentManager, skinStream);
}
}
/// Called after each test has run
[TearDown]
public void Teardown() {
if(this.graphics != null) {
this.graphics.Dispose();
this.graphics = null;
}
if(this.contentManager != null) {
this.contentManager.Dispose();
this.contentManager = null;
}
if(this.mockedGraphicsDeviceService != null) {
this.mockedGraphicsDeviceService.DestroyDevice();
this.mockedGraphicsDeviceService = null;
}
}
/// Verifies that the painter's constructor is working
[Test]
public void TestConstructor() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
Assert.IsNotNull(this.graphics); // for readability, graphics cannot be null here
}
/// Verifies that the painter can assign a clipping region
[Test]
public void TestClipRegion() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
RectangleF clipRegion = new RectangleF(4.0f, 4.0f, 12.0f, 12.0f);
this.graphics.BeginDrawing();
try {
using(this.graphics.SetClipRegion(clipRegion)) { }
// No exception means the test passes.
// Further validation would require visual inspection.
}
finally {
this.graphics.EndDrawing();
}
}
///
/// Verifies that the painter can handle a clipping region that leaves
/// the viewport
///
[Test]
public void TestClipRegionOffScreen() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
RectangleF clipRegion = new RectangleF(-20.0f, -4.0f, 4.0f, 20.0f);
this.graphics.BeginDrawing();
try {
using(this.graphics.SetClipRegion(clipRegion)) { }
// No exception means the test passes.
// Further validation would require visual inspection.
}
finally {
this.graphics.EndDrawing();
}
}
/// Verifies that the painter can draw skin elements
[Test]
public void TestDrawElement() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
this.graphics.BeginDrawing();
try {
this.graphics.DrawElement("window", new RectangleF(2.0f, 2.0f, 14.0f, 14.0f));
// No exception means the test passes.
// Further validation would require visual inspection.
}
finally {
this.graphics.EndDrawing();
}
}
/// Verifies that the painter can draw strings
[Test]
public void TestDrawString() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
this.graphics.BeginDrawing();
try {
this.graphics.DrawString(
"window", new RectangleF(2.0f, 2.0f, 14.0f, 14.0f), "Hello World"
);
// No exception means the test passes.
// Further validation would require visual inspection.
}
finally {
this.graphics.EndDrawing();
}
}
/// Verifies that the painter can draw a text cursor (caret)
[Test]
public void TestDrawCaret() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
this.graphics.BeginDrawing();
try {
this.graphics.DrawCaret(
"window", new RectangleF(2.0f, 2.0f, 14.0f, 14.0f), "Hello World", 5
);
// No exception means the test passes.
// Further validation would require visual inspection.
}
finally {
this.graphics.EndDrawing();
}
}
/// Verifies that the painter can measure the size of a string
[Test]
public void TestMeasureString() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
this.graphics.BeginDrawing();
try {
RectangleF size = this.graphics.MeasureString(
"window", new RectangleF(2.0f, 2.0f, 14.0f, 14.0f), "Hello World"
);
Assert.Greater(size.Width, 0.0f);
Assert.Greater(size.Height, 0.0f);
}
finally {
this.graphics.EndDrawing();
}
}
///
/// Verifies that the painter can measure the size of a string drawn to a frame
/// which contains no text
///
[Test]
public void TestMeasureStringOnTextlessFrame() {
loadSkin(
Resources.UnitTestResources.ResourceManager,
Resources.UnitTestResources.UnitTestSkin
);
this.graphics.BeginDrawing();
try {
RectangleF size = this.graphics.MeasureString(
"textless", new RectangleF(2.0f, 2.0f, 14.0f, 14.0f), "Hello World"
);
Assert.AreEqual(RectangleF.Empty, size);
}
finally {
this.graphics.EndDrawing();
}
}
///
/// Verifies that the painter can locate the opening between two characters
/// in a string that's closest to a specific position
///
[Test]
public void TestGetClosestOpening() {
loadSkin(
Resources.SuaveSkinResources.ResourceManager,
Resources.SuaveSkinResources.SuaveSkin
);
this.graphics.BeginDrawing();
try {
int index = this.graphics.GetClosestOpening(
"window",
new RectangleF(2.0f, 2.0f, 14.0f, 14.0f),
"Hello World",
new Vector2(1.0f, 8.0f)
);
Assert.AreEqual(0, index);
}
finally {
this.graphics.EndDrawing();
}
}
/// Tests whether strings can be positioned at all anchoring spots
[Test]
public void TestStringPositioning() {
loadSkin(
Resources.UnitTestResources.ResourceManager,
Resources.UnitTestResources.UnitTestSkin
);
this.graphics.BeginDrawing();
try {
this.graphics.DrawString(
"test", new RectangleF(2.0f, 2.0f, 12.0f, 12.0f), "Hello World"
);
}
finally {
this.graphics.EndDrawing();
}
}
///
/// Tests whether an exception is thrown if a skin element doesn't exist
///
[Test]
public void TestThrowOnMissingFrame() {
loadSkin(
Resources.UnitTestResources.ResourceManager,
Resources.UnitTestResources.UnitTestSkin
);
Assert.Throws(
delegate() {
this.graphics.DrawElement("this.does.not.exist", RectangleF.Empty);
}
);
}
///
/// Tests whether an exception is thrown if a skin contains an bad color constant
///
[Test]
public void TestThrowOnBadColor() {
Assert.Throws(
delegate() {
loadSkin(
Resources.UnitTestResources.ResourceManager,
Resources.UnitTestResources.BadColorSkin
);
}
);
}
///
/// Tests whether an exception is thrown if a skin contains an bad horizontal
/// text position
///
[Test]
public void TestThrowOnBadHorizontalPosition() {
Assert.Throws(
delegate() {
loadSkin(
Resources.UnitTestResources.ResourceManager,
Resources.UnitTestResources.BadHorizontalPositionSkin
);
}
);
}
///
/// Tests whether an exception is thrown if a skin contains an bad horizontal
/// text position
///
[Test]
public void TestThrowOnBadVerticalPosition() {
Assert.Throws(
delegate() {
loadSkin(
Resources.UnitTestResources.ResourceManager,
Resources.UnitTestResources.BadVerticalPositionSkin
);
}
);
}
/// Mocked graphics device used to run the unit tests
private MockedGraphicsDeviceService mockedGraphicsDeviceService;
/// Content manager holding the resources of the painter's skin
private ResourceContentManager contentManager;
/// GUI graphics interface being tested
private FlatGuiGraphics graphics;
}
} // namespace Nuclex.UserInterface.Visuals.Flat
#endif // UNITTEST