1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01: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() {
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") {
OnPressed = e => {
this.current = (this.current + 1) % Easings.Length;

3
Jenkinsfile vendored
View file

@ -4,7 +4,8 @@ pipeline {
stage('Cake Build') {
steps {
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') {

View file

@ -227,13 +227,11 @@ namespace MLEM.Ui.Elements {
var targetArea = (Rectangle) this.GetRenderTargetArea();
if (targetArea.Width <= 0 || targetArea.Height <= 0)
return;
#if !TEST
if (this.renderTarget == null || targetArea.Width != this.renderTarget.Width || targetArea.Height != this.renderTarget.Height) {
if (this.renderTarget != null)
this.renderTarget.Dispose();
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();
if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) {
this.OnPressed += async e => {
#if !TEST
if (!KeyboardInput.IsVisible) {
var title = this.MobileTitle ?? this.PlaceholderText;
var result = await KeyboardInput.Show(title, this.MobileDescription, this.Text);
if (result != null)
this.SetText(result.Replace('\n', ' '), true);
}
#endif
};
}
this.OnTextInput += (element, key, character) => {

View file

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

View file

@ -126,11 +126,7 @@ namespace MLEM.Textures {
/// <param name="uv">The top left corner of this area</param>
/// <param name="size">The size of this area</param>
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) {
#endif
}
}

View file

@ -1,16 +1,19 @@
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Data;
using MLEM.Data.Content;
using MLEM.Textures;
using NUnit.Framework;
using Tests.Stub;
namespace Tests {
public class TestDataTextureAtlas {
[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);
var table = atlas["LongTableUp"];

View file

@ -4,7 +4,6 @@ using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Textures;
using NUnit.Framework;
using Tests.Stub;
namespace Tests {
public class FormattingTests {
@ -22,10 +21,11 @@ namespace Tests {
[Test]
public void TestFormatting() {
using var game = TestGame.Create();
var formatter = new TextFormatter();
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.";
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.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);

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

View file

@ -6,11 +6,22 @@ using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using NUnit.Framework;
using Tests.Stub;
namespace Tests {
public class UiTests {
private TestGame game;
[SetUp]
public void SetUp() {
this.game = TestGame.Create();
}
[TearDown]
public void TearDown() {
this.game?.Dispose();
}
[Test]
public void TestInvalidPanel() {
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);
}
private static void AddAndUpdate(Element element) {
var ui = new UiSystem(null, new StubStyle(), null, false);
ui.Viewport = new Rectangle(0, 0, 1280, 720);
ui.Add("Test", element);
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();
}
}
}