1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

added some UI tests

This commit is contained in:
Ell 2021-04-01 19:36:56 +02:00
parent 64c2ebd4ac
commit e7fd026a33
8 changed files with 113 additions and 4 deletions

View file

@ -90,7 +90,6 @@ namespace MLEM.Ui.Elements {
/// The size of this element, where X represents the width and Y represents the height. /// The size of this element, where X represents the width and Y represents the height.
/// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value. /// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value.
/// If the x (or y) value of the size is negative, the width (or height) is seen as a percentage of the element's resulting height (or width). /// If the x (or y) value of the size is negative, the width (or height) is seen as a percentage of the element's resulting height (or width).
/// If <see cref="SetWidthBasedOnChildren"/> is true, this property's X value is ignored and overridden. If <see cref="SetHeightBasedOnChildren"/> is true, this property's Y value is ignored and overridden.
/// </summary> /// </summary>
/// <example> /// <example>
/// The following example combines both types of percentage-based sizing. /// The following example combines both types of percentage-based sizing.

View file

@ -227,11 +227,13 @@ 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,12 +144,14 @@ 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,6 +453,7 @@ namespace MLEM.Input {
return true; return true;
} }
} }
sample = default;
return false; return false;
} }
@ -540,8 +541,10 @@ 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>
@ -549,8 +552,10 @@ 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,7 +126,11 @@ 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) { this(region?.Texture, (region?.Position ?? Point.Zero) + uv, size) {
#else
this(region.Texture, region.Position + uv, size) {
#endif
} }
} }

11
Tests/Stub/StubStyle.cs Normal file
View file

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

View file

@ -1,16 +1,36 @@
<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>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" /> <!-- TODO switch to project references with test framework (https://github.com/MonoGame/MonoGame/issues/7474) -->
<ProjectReference Include="..\MLEM.Data\MLEM.Data.csproj" /> <!-- right now, we have to do it like this to allow for the TEST constant to go through -->
<ProjectReference Include="..\MLEM\MLEM.csproj" /> <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" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<!-- "transitive" dependencies -->
<PackageReference Include="Lidgren.Network" Version="1.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="TextCopy" Version="4.3.0" />
<!-- regular dependencies -->
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" /> <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="coverlet.collector" Version="3.0.3" /> <PackageReference Include="coverlet.collector" Version="3.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />

66
Tests/UiTests.cs Normal file
View file

@ -0,0 +1,66 @@
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Misc;
using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using NUnit.Framework;
using Tests.Stub;
namespace Tests {
public class UiTests {
[Test]
public void TestInvalidPanel() {
var invalidPanel = new Panel(Anchor.Center, Vector2.Zero, Vector2.Zero) {
SetWidthBasedOnChildren = true,
SetHeightBasedOnChildren = true
};
invalidPanel.AddChild(new Paragraph(Anchor.AutoRight, 1, "This is some test text!", true));
invalidPanel.AddChild(new VerticalSpace(1));
Assert.Throws<ArithmeticException>(() => AddAndUpdate(invalidPanel));
}
[Test]
public void TestComplexPanel() {
var group = new Group(Anchor.TopLeft, Vector2.One, false);
var panel = group.AddChild(new Panel(Anchor.Center, new Vector2(150, 150), Vector2.Zero, false, true, new Point(5, 10), false) {
ChildPadding = new Padding(5, 10, 5, 5)
});
for (var i = 0; i < 5; i++) {
var button = panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) {
SetHeightBasedOnChildren = true,
Padding = new Padding(0, 0, 0, 1),
ChildPadding = new Vector2(3)
});
button.AddChild(new Group(Anchor.AutoLeft, new Vector2(0.5F, 30), false) {
CanBeMoused = false
});
}
AddAndUpdate(group);
// group has 1 panel with 1 scroll bar, and the panel's 10 children
Assert.AreEqual(1, group.GetChildren().Count());
Assert.AreEqual(12, group.GetChildren(regardGrandchildren: true).Count());
// panel 1 scroll bar and 5 buttons, each button has 1 group, so 11 grandchildren
Assert.AreEqual(6, panel.GetChildren().Count());
Assert.AreEqual(11, panel.GetChildren(regardGrandchildren: true).Count());
var testBtn = panel.GetChildren<Button>().First();
// panel's width is 150, minus child padding of 5 left and 10 right
Assert.AreEqual(testBtn.DisplayArea.Width, 150 - 5 - 10);
// button's width, minus child padding of 3 left and 3 right, divided by 2 because of group's width
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);
element.ForceUpdateArea();
}
}
}