Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Oct 26, 2023
1 parent 6dd3f26 commit ee76673
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 2 deletions.
2 changes: 1 addition & 1 deletion samples/Myra.Samples.Layout2D/MyraSamplesLayout2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override void LoadContent()
{
Text = "Calc"
};
btnA.Click += (object sender, EventArgs e) => { btnB.Layout2d.Expresion = (_desktop.GetWidgetByID("Expression") as TextBox).Text; _desktop.InvalidateLayout(); _desktop.UpdateLayout(); };
btnA.Click += (object sender, EventArgs e) => { btnB.Layout2d.Expresion = (_desktop.FindChild("Expression") as TextBox).Text; _desktop.InvalidateLayout(); _desktop.UpdateLayout(); };
panel.Widgets.Add(btnA);

g.Widgets.Add(panel);
Expand Down
53 changes: 53 additions & 0 deletions src/Myra.Tests/GridTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.Xna.Framework;
using Myra.Graphics2D.UI;
using NUnit.Framework;

namespace Myra.Tests
{
[TestFixture]
public class GridTests
{
private static Project LoadFromResource(string name)
{
var xml = typeof(GridTests).Assembly.ReadResourceAsString("Resources.GridTests." + name);

return Project.LoadFromXml(xml);
}

[Test]
public static void TestSimpleProportionsPart()
{
var project = LoadFromResource("SimpleProportionsPart.xmmp");
var grid = (Grid)project.Root;

grid.Arrange(new Rectangle(0, 0, 400, 400));

Assert.AreEqual(100, grid.Widgets[0].ContainerBounds.Width);
Assert.AreEqual(200, grid.Widgets[0].ContainerBounds.Height);
Assert.AreEqual(300, grid.Widgets[1].ContainerBounds.Width);
Assert.AreEqual(200, grid.Widgets[1].ContainerBounds.Height);
Assert.AreEqual(100, grid.Widgets[2].ContainerBounds.Width);
Assert.AreEqual(200, grid.Widgets[2].ContainerBounds.Height);
Assert.AreEqual(300, grid.Widgets[3].ContainerBounds.Width);
Assert.AreEqual(200, grid.Widgets[3].ContainerBounds.Height);
}

[Test]
public static void TestSimpleAutoFill()
{
var project = LoadFromResource("SimpleAutoFill.xmmp");
var grid = (Grid)project.Root;

grid.Arrange(new Rectangle(0, 0, 400, 500));

Assert.AreEqual(100, grid.Widgets[0].ContainerBounds.Width);
Assert.AreEqual(450, grid.Widgets[0].ContainerBounds.Height);
Assert.AreEqual(300, grid.Widgets[1].ContainerBounds.Width);
Assert.AreEqual(450, grid.Widgets[1].ContainerBounds.Height);
Assert.AreEqual(100, grid.Widgets[2].ContainerBounds.Width);
Assert.AreEqual(50, grid.Widgets[2].ContainerBounds.Height);
Assert.AreEqual(300, grid.Widgets[3].ContainerBounds.Width);
Assert.AreEqual(50, grid.Widgets[3].ContainerBounds.Height);
}
}
}
65 changes: 65 additions & 0 deletions src/Myra.Tests/Res.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.IO;
using System.Reflection;
using System;

namespace Myra.Tests
{
internal static class Res
{
/// <summary>
/// Open assembly resource stream by relative name
/// </summary>
/// <param name="assembly"></param>
/// <param name="path"></param>
/// <returns></returns>
public static Stream OpenResourceStream(this Assembly assembly, string path)
{
// Once you figure out the name, pass it in as the argument here.
path = assembly.GetName().Name + "." + path;
var stream = assembly.GetManifestResourceStream(path);
if (stream == null)
{
throw new Exception($"Could not find resource at path '{path}'");
}

return stream;
}

/// <summary>
/// Reads assembly resource as byte array by relative name
/// </summary>
/// <param name="assembly"></param>
/// <param name="path"></param>
/// <returns></returns>
public static byte[] ReadResourceAsBytes(this Assembly assembly, string path)
{
var ms = new MemoryStream();
using (var input = assembly.OpenResourceStream(path))
{
input.CopyTo(ms);

return ms.ToArray();
}
}

/// <summary>
/// Reads assembly resource as string by relative name
/// </summary>
/// <param name="assembly"></param>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadResourceAsString(this Assembly assembly, string path)
{
string result;
using (var input = assembly.OpenResourceStream(path))
{
using (var textReader = new StreamReader(input))
{
result = textReader.ReadToEnd();
}
}

return result;
}
}
}
16 changes: 16 additions & 0 deletions src/Myra.Tests/Resources/GridTests/SimpleAutoFill.xmmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project>
<Grid>
<Grid.ColumnsProportions>
<Proportion Type="Auto" />
<Proportion Type="Fill" />
</Grid.ColumnsProportions>
<Grid.RowsProportions>
<Proportion Type="Fill" />
<Proportion Type="Auto" />
</Grid.RowsProportions>
<Label Text="Test" Width="100" Height="50" />
<Label Text="Test" Width="100" Height="50" Grid.Column="1" />
<Label Text="Test" Width="100" Height="50" Grid.Row="1" />
<Label Text="Test" Width="100" Height="50" Grid.Column="1" Grid.Row="1" />
</Grid>
</Project>
16 changes: 16 additions & 0 deletions src/Myra.Tests/Resources/GridTests/SimpleProportionsPart.xmmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project>
<Grid>
<Grid.ColumnsProportions>
<Proportion Type="Part" Value="1" />
<Proportion Type="Part" Value="3" />
</Grid.ColumnsProportions>
<Grid.RowsProportions>
<Proportion Type="Part" Value="1" />
<Proportion Type="Part" Value="1" />
</Grid.RowsProportions>
<Label Text="Test" />
<Label Text="Test" Grid.Column="1" />
<Label Text="Test" Grid.Row="1" />
<Label Text="Test" Grid.Column="1" Grid.Row="1" />
</Grid>
</Project>
2 changes: 1 addition & 1 deletion src/Myra/Graphics2D/UI/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static Project LoadFromXml<T>(XDocument xDoc, AssetManager assetManager =

var result = new Project();

if (stylesheet != null)
if (stylesheetPathAttr != null)
{
if (assetManager == null)
{
Expand Down

0 comments on commit ee76673

Please sign in to comment.