From 2055c3a6ef382cf2c9f189be2fae0dc76c061b2e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 2 Apr 2021 17:12:27 +0200 Subject: [PATCH] convert tests to use graphics --- Demos/EasingsDemo.cs | 2 +- Jenkinsfile | 3 ++- MLEM.Ui/Elements/Panel.cs | 2 -- MLEM.Ui/Elements/TextField.cs | 2 -- MLEM/Input/InputHandler.cs | 5 ----- MLEM/Textures/TextureRegion.cs | 4 ---- Tests/DataTextureAtlasTests.cs | 7 +++++-- Tests/FormattingTests.cs | 4 ++-- Tests/Stub/StubFont.cs | 36 ---------------------------------- Tests/Stub/StubServices.cs | 11 ----------- Tests/Stub/StubStyle.cs | 11 ----------- Tests/TestGame.cs | 24 +++++++++++++++++++++++ Tests/Tests.csproj | 19 ++++-------------- Tests/UiTests.cs | 23 ++++++++++++++++------ 14 files changed, 55 insertions(+), 98 deletions(-) delete mode 100644 Tests/Stub/StubFont.cs delete mode 100644 Tests/Stub/StubServices.cs delete mode 100644 Tests/Stub/StubStyle.cs create mode 100644 Tests/TestGame.cs diff --git a/Demos/EasingsDemo.cs b/Demos/EasingsDemo.cs index de0a324..dde79f8 100644 --- a/Demos/EasingsDemo.cs +++ b/Demos/EasingsDemo.cs @@ -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; diff --git a/Jenkinsfile b/Jenkinsfile index c15d443..c2662a2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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') { diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 8c1e27e..101444f 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -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 } } diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index de0c099..279a41b 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -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) => { diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs index c05f5b5..3da5667 100644 --- a/MLEM/Input/InputHandler.cs +++ b/MLEM/Input/InputHandler.cs @@ -453,7 +453,6 @@ namespace MLEM.Input { return true; } } - sample = default; return false; } @@ -541,10 +540,8 @@ namespace MLEM.Input { /// /// The gestures to enable public static void EnableGestures(params GestureType[] gestures) { - #if !TEST foreach (var gesture in gestures) TouchPanel.EnabledGestures |= gesture; - #endif } /// @@ -552,10 +549,8 @@ namespace MLEM.Input { /// /// The gestures to disable public static void DisableGestures(params GestureType[] gestures) { - #if !TEST foreach (var gesture in gestures) TouchPanel.EnabledGestures &= ~gesture; - #endif } /// diff --git a/MLEM/Textures/TextureRegion.cs b/MLEM/Textures/TextureRegion.cs index d4a12a9..2f69183 100644 --- a/MLEM/Textures/TextureRegion.cs +++ b/MLEM/Textures/TextureRegion.cs @@ -126,11 +126,7 @@ namespace MLEM.Textures { /// The top left corner of this area /// The size of this area 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 } } diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index 95db88b..3274793 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -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"]; diff --git a/Tests/FormattingTests.cs b/Tests/FormattingTests.cs index 3cc2613..b732e59 100644 --- a/Tests/FormattingTests.cs +++ b/Tests/FormattingTests.cs @@ -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 is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when 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); diff --git a/Tests/Stub/StubFont.cs b/Tests/Stub/StubFont.cs deleted file mode 100644 index 83b2e0e..0000000 --- a/Tests/Stub/StubFont.cs +++ /dev/null @@ -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) { - } - - } -} \ No newline at end of file diff --git a/Tests/Stub/StubServices.cs b/Tests/Stub/StubServices.cs deleted file mode 100644 index 9f5f0ee..0000000 --- a/Tests/Stub/StubServices.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace Tests.Stub { - public class StubServices : IServiceProvider { - - public object GetService(Type serviceType) { - return null; - } - - } -} \ No newline at end of file diff --git a/Tests/Stub/StubStyle.cs b/Tests/Stub/StubStyle.cs deleted file mode 100644 index eb3ad42..0000000 --- a/Tests/Stub/StubStyle.cs +++ /dev/null @@ -1,11 +0,0 @@ -using MLEM.Ui.Style; - -namespace Tests.Stub { - public class StubStyle : UiStyle { - - public StubStyle() { - this.Font = new StubFont(); - } - - } -} \ No newline at end of file diff --git a/Tests/TestGame.cs b/Tests/TestGame.cs new file mode 100644 index 0000000..3b27675 --- /dev/null +++ b/Tests/TestGame.cs @@ -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; + } + + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index f524306..eaed4bf 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,25 +1,14 @@ netcoreapp3.1 - TEST nunit - - - - MLEM\%(RecursiveDir)%(Filename)%(Extension) - - - - MLEM.Ui\%(RecursiveDir)%(Filename)%(Extension) - - - - MLEM.Data\%(RecursiveDir)%(Filename)%(Extension) - - + + + + diff --git a/Tests/UiTests.cs b/Tests/UiTests.cs index bf98ca3..c265237 100644 --- a/Tests/UiTests.cs +++ b/Tests/UiTests.cs @@ -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().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(); } - } } \ No newline at end of file