From d8d29bf10de9165ec94d3cb6ee0bd65c682ba283 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 4 Apr 2021 18:31:51 +0200 Subject: [PATCH] added a few more tests --- Tests/CameraTests.cs | 30 +++++++++++++++ Tests/TexturePackerTests.cs | 76 +++++++++++++++++++++++++++++++++++++ Tests/UiTests.cs | 19 ++++++++-- 3 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 Tests/CameraTests.cs create mode 100644 Tests/TexturePackerTests.cs diff --git a/Tests/CameraTests.cs b/Tests/CameraTests.cs new file mode 100644 index 0000000..faa319d --- /dev/null +++ b/Tests/CameraTests.cs @@ -0,0 +1,30 @@ +using Microsoft.Xna.Framework; +using MLEM.Cameras; +using NUnit.Framework; + +namespace Tests { + public class CameraTests { + + private TestGame game; + + [SetUp] + public void SetUp() { + this.game = TestGame.Create(); + } + + [TearDown] + public void TearDown() { + this.game?.Dispose(); + } + + [Test] + public void TestConversions([Range(-4, 4, 4F)] float x, [Range(-4, 4, 4F)] float y) { + var camera = new Camera(this.game.GraphicsDevice); + var pos = new Vector2(x, y); + var cam = camera.ToCameraPos(pos); + var ret = camera.ToWorldPos(cam); + Assert.AreEqual(pos, ret); + } + + } +} \ No newline at end of file diff --git a/Tests/TexturePackerTests.cs b/Tests/TexturePackerTests.cs new file mode 100644 index 0000000..90cf5cb --- /dev/null +++ b/Tests/TexturePackerTests.cs @@ -0,0 +1,76 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Data; +using MLEM.Extensions; +using MLEM.Textures; +using NUnit.Framework; + +namespace Tests { + public class TexturePackerTests { + + private Texture2D testTexture; + private TestGame game; + + [SetUp] + public void SetUp() { + this.game = TestGame.Create(); + this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256); + } + + [TearDown] + public void TearDown() { + this.game?.Dispose(); + this.testTexture?.Dispose(); + } + + [Test] + public void TestPacking() { + using var packer = new RuntimeTexturePacker(); + for (var i = 0; i < 5; i++) { + var width = 16 * (i + 1); + packer.Add(new TextureRegion(this.testTexture, 0, 0, width, 64), r => { + Assert.AreEqual(r.Width, width); + Assert.AreEqual(r.Height, 64); + }); + } + packer.Pack(this.game.GraphicsDevice); + Assert.AreEqual(packer.PackedTexture.Width, 16 + 32 + 48 + 64 + 80); + Assert.AreEqual(packer.PackedTexture.Height, 64); + } + + [Test] + public void TestBounds() { + // test forced max width + using var packer = new RuntimeTexturePacker(128); + Assert.Throws(() => { + packer.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), StubResult); + }); + + // test auto-expanding width + using var packer2 = new RuntimeTexturePacker(128, true); + Assert.DoesNotThrow(() => { + packer2.Add(new TextureRegion(this.testTexture, 0, 0, 256, 128), StubResult); + }); + packer2.Pack(this.game.GraphicsDevice); + + // test power of two forcing + using var packer3 = new RuntimeTexturePacker(128, forcePowerOfTwo: true); + packer3.Add(new TextureRegion(this.testTexture, 0, 0, 37, 170), StubResult); + packer3.Pack(this.game.GraphicsDevice); + Assert.AreEqual(64, packer3.PackedTexture.Width); + Assert.AreEqual(256, packer3.PackedTexture.Height); + + // test square forcing + using var packer4 = new RuntimeTexturePacker(128, forceSquare: true); + packer4.Add(new TextureRegion(this.testTexture, 0, 0, 37, 170), StubResult); + packer4.Pack(this.game.GraphicsDevice); + Assert.AreEqual(170, packer4.PackedTexture.Width); + Assert.AreEqual(170, packer4.PackedTexture.Height); + } + + private static void StubResult(TextureRegion region) { + } + + } +} \ No newline at end of file diff --git a/Tests/UiTests.cs b/Tests/UiTests.cs index c265237..37f462c 100644 --- a/Tests/UiTests.cs +++ b/Tests/UiTests.cs @@ -30,7 +30,7 @@ namespace Tests { }; invalidPanel.AddChild(new Paragraph(Anchor.AutoRight, 1, "This is some test text!", true)); invalidPanel.AddChild(new VerticalSpace(1)); - Assert.Throws(() => AddAndUpdate(invalidPanel)); + Assert.Throws(() => this.AddAndUpdate(invalidPanel)); } [Test] @@ -49,7 +49,7 @@ namespace Tests { CanBeMoused = false }); } - AddAndUpdate(group); + this.AddAndUpdate(group); // group has 1 panel with 1 scroll bar, and the panel's 10 children Assert.AreEqual(1, group.GetChildren().Count()); @@ -66,12 +66,25 @@ namespace Tests { Assert.AreEqual(testBtn.GetChildren().Single().DisplayArea.Width, (150 - 5 - 10 - 3 - 3) * 0.5F); } + [Test] + public void TestStyle() { + var style = new StyleProp(); + Assert.AreEqual(null, style.Value); + style.SetFromStyle("from style"); + Assert.AreEqual("from style", style.Value); + style.Set("custom"); + Assert.AreEqual("custom", style.Value); + style.SetFromStyle("from style again"); + Assert.AreEqual("custom", style.Value); + } + private void AddAndUpdate(Element element) { foreach (var root in this.game.UiSystem.GetRootElements()) this.game.UiSystem.Remove(root.Name); - + this.game.UiSystem.Add("Test", element); element.ForceUpdateArea(); } + } } \ No newline at end of file