1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-03 16:45:17 +02:00

convert tests to use graphics

This commit is contained in:
Ell 2021-04-02 17:12:27 +02:00
parent 2287df00af
commit 2055c3a6ef
14 changed files with 55 additions and 98 deletions

View file

@ -25,7 +25,7 @@ namespace Demos {
public override void LoadContent() { public override void LoadContent() {
base.LoadContent(); base.LoadContent();
this.group = new Group(Anchor.TopCenter, Vector2.One) {SetWidthBasedOnChildren = true}; this.group = new Group(Anchor.TopCenter, Vector2.One) {CanBeMoused = false};
this.group.AddChild(new Button(Anchor.AutoCenter, new Vector2(30, 10), "Next") { this.group.AddChild(new Button(Anchor.AutoCenter, new Vector2(30, 10), "Next") {
OnPressed = e => { OnPressed = e => {
this.current = (this.current + 1) % Easings.Length; this.current = (this.current + 1) % Easings.Length;

3
Jenkinsfile vendored
View file

@ -4,7 +4,8 @@ pipeline {
stage('Cake Build') { stage('Cake Build') {
steps { steps {
sh 'dotnet tool restore' sh 'dotnet tool restore'
sh 'dotnet dotnet-cake --Target=Publish --Branch=' + env.BRANCH_NAME // we use xvfb to allow for graphics-dependent tests
sh 'xvfb-run -a /usr/bin/dotnet dotnet-cake --Target=Publish --Branch=' + env.BRANCH_NAME
} }
} }
stage('Document') { stage('Document') {

View file

@ -227,13 +227,11 @@ namespace MLEM.Ui.Elements {
var targetArea = (Rectangle) this.GetRenderTargetArea(); var targetArea = (Rectangle) this.GetRenderTargetArea();
if (targetArea.Width <= 0 || targetArea.Height <= 0) if (targetArea.Width <= 0 || targetArea.Height <= 0)
return; return;
#if !TEST
if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) { if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) {
if (this.renderTarget != null) if (this.renderTarget != null)
this.renderTarget.Dispose(); this.renderTarget.Dispose();
this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.Game.GraphicsDevice, targetArea.Width, targetArea.Height); this.renderTarget = targetArea.IsEmpty ? null : new RenderTarget2D(this.System.Game.GraphicsDevice, targetArea.Width, targetArea.Height);
} }
#endif
} }
} }

View file

@ -144,14 +144,12 @@ namespace MLEM.Ui.Elements {
TextInputWrapper.EnsureExists(); TextInputWrapper.EnsureExists();
if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) { if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) {
this.OnPressed += async e => { this.OnPressed += async e => {
#if !TEST
if (!KeyboardInput.IsVisible) { if (!KeyboardInput.IsVisible) {
var title = this.MobileTitle ?? this.PlaceholderText; var title = this.MobileTitle ?? this.PlaceholderText;
var result = await KeyboardInput.Show(title, this.MobileDescription, this.Text); var result = await KeyboardInput.Show(title, this.MobileDescription, this.Text);
if (result != null) if (result != null)
this.SetText(result.Replace('\n', ' '), true); this.SetText(result.Replace('\n', ' '), true);
} }
#endif
}; };
} }
this.OnTextInput += (element, key, character) => { this.OnTextInput += (element, key, character) => {

View file

@ -453,7 +453,6 @@ namespace MLEM.Input {
return true; return true;
} }
} }
sample = default;
return false; return false;
} }
@ -541,10 +540,8 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="gestures">The gestures to enable</param> /// <param name="gestures">The gestures to enable</param>
public static void EnableGestures(params GestureType[] gestures) { public static void EnableGestures(params GestureType[] gestures) {
#if !TEST
foreach (var gesture in gestures) foreach (var gesture in gestures)
TouchPanel.EnabledGestures |= gesture; TouchPanel.EnabledGestures |= gesture;
#endif
} }
/// <summary> /// <summary>
@ -552,10 +549,8 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <param name="gestures">The gestures to disable</param> /// <param name="gestures">The gestures to disable</param>
public static void DisableGestures(params GestureType[] gestures) { public static void DisableGestures(params GestureType[] gestures) {
#if !TEST
foreach (var gesture in gestures) foreach (var gesture in gestures)
TouchPanel.EnabledGestures &= ~gesture; TouchPanel.EnabledGestures &= ~gesture;
#endif
} }
/// <summary> /// <summary>

View file

@ -126,11 +126,7 @@ namespace MLEM.Textures {
/// <param name="uv">The top left corner of this area</param> /// <param name="uv">The top left corner of this area</param>
/// <param name="size">The size of this area</param> /// <param name="size">The size of this area</param>
public TextureRegion(TextureRegion region, Point uv, Point size) : public TextureRegion(TextureRegion region, Point uv, Point size) :
#if TEST
this(region?.Texture, (region?.Position ?? Point.Zero) + uv, size) {
#else
this(region.Texture, region.Position + uv, size) { this(region.Texture, region.Position + uv, size) {
#endif
} }
} }

View file

@ -1,16 +1,19 @@
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Data; using MLEM.Data;
using MLEM.Data.Content; using MLEM.Data.Content;
using MLEM.Textures;
using NUnit.Framework; using NUnit.Framework;
using Tests.Stub;
namespace Tests { namespace Tests {
public class TestDataTextureAtlas { public class TestDataTextureAtlas {
[Test] [Test]
public void Test() { public void Test() {
var atlas = DataTextureAtlas.LoadAtlasData(null, new RawContentManager(new StubServices()), "Texture.atlas"); using var game = TestGame.Create();
using var texture = new Texture2D(game.GraphicsDevice, 1, 1);
var atlas = DataTextureAtlas.LoadAtlasData(new TextureRegion(texture), game.RawContent, "Texture.atlas");
Assert.AreEqual(atlas.Regions.Count(), 5); Assert.AreEqual(atlas.Regions.Count(), 5);
var table = atlas["LongTableUp"]; var table = atlas["LongTableUp"];

View file

@ -4,7 +4,6 @@ using MLEM.Formatting;
using MLEM.Formatting.Codes; using MLEM.Formatting.Codes;
using MLEM.Textures; using MLEM.Textures;
using NUnit.Framework; using NUnit.Framework;
using Tests.Stub;
namespace Tests { namespace Tests {
public class FormattingTests { public class FormattingTests {
@ -22,10 +21,11 @@ namespace Tests {
[Test] [Test]
public void TestFormatting() { public void TestFormatting() {
using var game = TestGame.Create();
var formatter = new TextFormatter(); var formatter = new TextFormatter();
formatter.AddImage("Test", new TextureRegion((Texture2D) null, 0, 8, 24, 24)); formatter.AddImage("Test", new TextureRegion((Texture2D) null, 0, 8, 24, 24));
const string strg = "Lorem Ipsum <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> an unknown printer took a galley of type and scrambled it to make a type specimen book."; const string strg = "Lorem Ipsum <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> an unknown printer took a galley of type and scrambled it to make a type specimen book.";
var ret = formatter.Tokenize(new StubFont(), strg); var ret = formatter.Tokenize(game.UiSystem.Style.Font, strg);
Assert.AreEqual(ret.Tokens.Length, 13); Assert.AreEqual(ret.Tokens.Length, 13);
Assert.AreEqual(ret.DisplayString, "Lorem Ipsum \u2003 is simply dummy text of the \u2003 printing and typesetting \u2003 industry. Lorem Ipsum has been the industry's standard dummy text \u2003 ever since the \u2003 1500s, when \u2003\u2003\u2003\u2003\u2003\u2003\u2003 an unknown printer took a galley of type and scrambled it to make a type specimen book."); Assert.AreEqual(ret.DisplayString, "Lorem Ipsum \u2003 is simply dummy text of the \u2003 printing and typesetting \u2003 industry. Lorem Ipsum has been the industry's standard dummy text \u2003 ever since the \u2003 1500s, when \u2003\u2003\u2003\u2003\u2003\u2003\u2003 an unknown printer took a galley of type and scrambled it to make a type specimen book.");
Assert.AreEqual(ret.AllCodes.Length, 12); Assert.AreEqual(ret.AllCodes.Length, 12);

View file

@ -1,36 +0,0 @@
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Font;
namespace Tests.Stub {
public class StubFont : GenericFont {
public override GenericFont Bold => this;
public override GenericFont Italic => this;
public override float LineHeight => 1;
protected override Vector2 MeasureChar(char c) {
return Vector2.Zero;
}
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
}
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
}
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
}
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
}
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
}
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
}
}
}

View file

@ -1,11 +0,0 @@
using System;
namespace Tests.Stub {
public class StubServices : IServiceProvider {
public object GetService(Type serviceType) {
return null;
}
}
}

View file

@ -1,11 +0,0 @@
using MLEM.Ui.Style;
namespace Tests.Stub {
public class StubStyle : UiStyle {
public StubStyle() {
this.Font = new StubFont();
}
}
}

24
Tests/TestGame.cs Normal file
View file

@ -0,0 +1,24 @@
using MLEM.Data.Content;
using MLEM.Startup;
namespace Tests {
public class TestGame : MlemGame {
public RawContentManager RawContent { get; private set; }
private TestGame() {
}
protected override void LoadContent() {
base.LoadContent();
this.RawContent = new RawContentManager(this.Services, this.Content.RootDirectory);
}
public static TestGame Create() {
var game = new TestGame();
game.RunOneFrame();
return game;
}
}
}

View file

@ -1,25 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<DefineConstants>TEST</DefineConstants>
<VSTestLogger>nunit</VSTestLogger> <VSTestLogger>nunit</VSTestLogger>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<!-- TODO switch to project references with test framework (https://github.com/MonoGame/MonoGame/issues/7474) --> <ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
<!-- right now, we have to do it like this to allow for the TEST constant to go through --> <ProjectReference Include="..\MLEM.Data\MLEM.Data.csproj" />
<Compile Include="..\MLEM\**\*.cs"> <ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
<Link>MLEM\%(RecursiveDir)%(Filename)%(Extension)</Link> <ProjectReference Include="..\MLEM\MLEM.csproj" />
</Compile>
<Compile Remove="..\MLEM\obj\**\*.cs" />
<Compile Include="..\MLEM.Ui\**\*.cs">
<Link>MLEM.Ui\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
<Compile Remove="..\MLEM.Ui\obj\**\*.cs" />
<Compile Include="..\MLEM.Data\**\*.cs">
<Link>MLEM.Data\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
<Compile Remove="..\MLEM.Data\obj\**\*.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -6,11 +6,22 @@ using MLEM.Ui;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
using NUnit.Framework; using NUnit.Framework;
using Tests.Stub;
namespace Tests { namespace Tests {
public class UiTests { public class UiTests {
private TestGame game;
[SetUp]
public void SetUp() {
this.game = TestGame.Create();
}
[TearDown]
public void TearDown() {
this.game?.Dispose();
}
[Test] [Test]
public void TestInvalidPanel() { public void TestInvalidPanel() {
var invalidPanel = new Panel(Anchor.Center, Vector2.Zero, Vector2.Zero) { var invalidPanel = new Panel(Anchor.Center, Vector2.Zero, Vector2.Zero) {
@ -55,12 +66,12 @@ namespace Tests {
Assert.AreEqual(testBtn.GetChildren<Group>().Single().DisplayArea.Width, (150 - 5 - 10 - 3 - 3) * 0.5F); Assert.AreEqual(testBtn.GetChildren<Group>().Single().DisplayArea.Width, (150 - 5 - 10 - 3 - 3) * 0.5F);
} }
private static void AddAndUpdate(Element element) { private void AddAndUpdate(Element element) {
var ui = new UiSystem(null, new StubStyle(), null, false); foreach (var root in this.game.UiSystem.GetRootElements())
ui.Viewport = new Rectangle(0, 0, 1280, 720); this.game.UiSystem.Remove(root.Name);
ui.Add("Test", element);
this.game.UiSystem.Add("Test", element);
element.ForceUpdateArea(); element.ForceUpdateArea();
} }
} }
} }