Browse Source

Add unit test for grid layout:

1. Pixel row column
1. Start row column
1. Mix pixel star row column
pull/1517/head
walterlv 8 years ago
parent
commit
431407437b
  1. 3
      tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj
  2. 98
      tests/Avalonia.Controls.UnitTests/GridMocks.cs
  3. 38
      tests/Avalonia.Controls.UnitTests/GridTests.cs

3
tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj

@ -3,6 +3,9 @@
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp2.0|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<Import Project="..\..\build\UnitTests.NetCore.targets" />
<Import Project="..\..\build\Moq.props" />
<Import Project="..\..\build\XUnit.props" />

98
tests/Avalonia.Controls.UnitTests/GridMocks.cs

@ -0,0 +1,98 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Xunit;
namespace Avalonia.Controls.UnitTests
{
internal static class GridMock
{
/// <summary>
/// Create a mock grid to test its row layout.
/// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`).
/// </summary>
/// <param name="rows">The row definitions of this mock grid.</param>
/// <param name="measure">The measure height of this grid. PositiveInfinity by default.</param>
/// <param name="arrange">The arrange height of this grid. DesiredSize.Height by default.</param>
/// <returns>The mock grid that its children bounds will be tested.</returns>
[SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")]
internal static Grid New(RowDefinitions rows,
double measure = default, double arrange = default)
{
var grid = new Grid { RowDefinitions = rows };
for (var i = 0; i < rows.Count; i++)
{
grid.Children.Add(new Border { [Grid.RowProperty] = i });
}
grid.Measure(new Size(double.PositiveInfinity, measure == default ? double.PositiveInfinity : measure));
grid.Arrange(new Rect(0, 0, 0, arrange == default ? grid.DesiredSize.Width : arrange));
return grid;
}
/// <summary>
/// Create a mock grid to test its column layout.
/// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`).
/// </summary>
/// <param name="columns">The column definitions of this mock grid.</param>
/// <param name="measure">The measure width of this grid. PositiveInfinity by default.</param>
/// <param name="arrange">The arrange width of this grid. DesiredSize.Width by default.</param>
/// <returns>The mock grid that its children bounds will be tested.</returns>
[SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")]
internal static Grid New(ColumnDefinitions columns,
double measure = default, double arrange = default)
{
var grid = new Grid { ColumnDefinitions = columns };
for (var i = 0; i < columns.Count; i++)
{
grid.Children.Add(new Border { [Grid.ColumnProperty] = i });
}
grid.Measure(new Size(measure == default ? double.PositiveInfinity : measure, double.PositiveInfinity));
grid.Arrange(new Rect(0, 0, arrange == default ? grid.DesiredSize.Width : arrange, 0));
return grid;
}
}
internal static class GridAssert
{
/// <summary>
/// Assert all the children heights.
/// This method will assume that the grid children count equals row count.
/// </summary>
/// <param name="grid">The children will be fetched through it.</param>
/// <param name="rows">Expected row values of every children.</param>
internal static void ChildrenHeight(Grid grid, params double[] rows)
{
if (grid.Children.Count != rows.Length)
{
throw new NotSupportedException();
}
for (var i = 0; i < rows.Length; i++)
{
Assert.Equal(rows[i], grid.Children[i].Bounds.Height);
}
}
/// <summary>
/// Assert all the children widths.
/// This method will assume that the grid children count equals row count.
/// </summary>
/// <param name="grid">The children will be fetched through it.</param>
/// <param name="columns">Expected column values of every children.</param>
internal static void ChildrenWidth(Grid grid, params double[] columns)
{
if (grid.Children.Count != columns.Length)
{
throw new NotSupportedException();
}
for (var i = 0; i < columns.Length; i++)
{
Assert.Equal(columns[i], grid.Children[i].Bounds.Width);
}
}
}
}

38
tests/Avalonia.Controls.UnitTests/GridTests.cs

@ -1,6 +1,8 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Avalonia.Controls;
using Xunit;
@ -64,5 +66,41 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(new Rect(0, 25, 150, 25), target.Children[1].Bounds);
Assert.Equal(new Rect(154, 25, 50, 25), target.Children[2].Bounds);
}
[Fact]
public void Layout_PixelRowColumn_BoundsCorrect()
{
// Arrange & Action
var rowGrid = GridMock.New(new RowDefinitions("100,200,300"));
var columnGrid = GridMock.New(new ColumnDefinitions("50,100,150"));
// Assert
GridAssert.ChildrenHeight(rowGrid, 100, 200, 300);
GridAssert.ChildrenWidth(columnGrid, 50, 100, 150);
}
[Fact]
public void Layout_StarRowColumn_BoundsCorrect()
{
// Arrange & Action
var rowGrid = GridMock.New(new RowDefinitions("1*,2*,3*"), arrange: 600);
var columnGrid = GridMock.New(new ColumnDefinitions("*,*,2*"), arrange: 600);
// Assert
GridAssert.ChildrenHeight(rowGrid, 100, 200, 300);
GridAssert.ChildrenWidth(columnGrid, 150, 150, 300);
}
[Fact]
public void Layout_MixPixelStarRowColumn_BoundsCorrect()
{
// Arrange & Action
var rowGrid = GridMock.New(new RowDefinitions("1*,2*,150"), arrange: 600);
var columnGrid = GridMock.New(new ColumnDefinitions("1*,2*,150"), arrange: 600);
// Assert
GridAssert.ChildrenHeight(rowGrid, 150, 300, 150);
GridAssert.ChildrenWidth(columnGrid, 150, 300, 150);
}
}
}

Loading…
Cancel
Save