From 431407437bca1340780614fca1d7c95aebb6bb29 Mon Sep 17 00:00:00 2001 From: walterlv Date: Sat, 28 Apr 2018 13:40:37 +0800 Subject: [PATCH] Add unit test for grid layout: 1. Pixel row column 1. Start row column 1. Mix pixel star row column --- .../Avalonia.Controls.UnitTests.csproj | 3 + .../Avalonia.Controls.UnitTests/GridMocks.cs | 98 +++++++++++++++++++ .../Avalonia.Controls.UnitTests/GridTests.cs | 38 +++++++ 3 files changed, 139 insertions(+) create mode 100644 tests/Avalonia.Controls.UnitTests/GridMocks.cs diff --git a/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj b/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj index fe7e48e085..32fa6abe29 100644 --- a/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj +++ b/tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj @@ -3,6 +3,9 @@ netcoreapp2.0 Library + + latest + diff --git a/tests/Avalonia.Controls.UnitTests/GridMocks.cs b/tests/Avalonia.Controls.UnitTests/GridMocks.cs new file mode 100644 index 0000000000..7df604b501 --- /dev/null +++ b/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 + { + /// + /// Create a mock grid to test its row layout. + /// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`). + /// + /// The row definitions of this mock grid. + /// The measure height of this grid. PositiveInfinity by default. + /// The arrange height of this grid. DesiredSize.Height by default. + /// The mock grid that its children bounds will be tested. + [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; + } + + /// + /// Create a mock grid to test its column layout. + /// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`). + /// + /// The column definitions of this mock grid. + /// The measure width of this grid. PositiveInfinity by default. + /// The arrange width of this grid. DesiredSize.Width by default. + /// The mock grid that its children bounds will be tested. + [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 + { + /// + /// Assert all the children heights. + /// This method will assume that the grid children count equals row count. + /// + /// The children will be fetched through it. + /// Expected row values of every children. + 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); + } + } + + /// + /// Assert all the children widths. + /// This method will assume that the grid children count equals row count. + /// + /// The children will be fetched through it. + /// Expected column values of every children. + 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); + } + } + } +} diff --git a/tests/Avalonia.Controls.UnitTests/GridTests.cs b/tests/Avalonia.Controls.UnitTests/GridTests.cs index c5aea6501f..675408b75d 100644 --- a/tests/Avalonia.Controls.UnitTests/GridTests.cs +++ b/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); + } } }