From cae8a2ab5cd1745ecf5cc73100690a50ed4e4dd7 Mon Sep 17 00:00:00 2001 From: walterlv Date: Mon, 30 Apr 2018 15:53:03 +0800 Subject: [PATCH] Add not enough length unit test for all test facts. --- src/Avalonia.Controls/Utils/GridLayout.cs | 25 +++++--- .../GridLayoutTests.cs | 63 ++++++++++++++++++- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Controls/Utils/GridLayout.cs b/src/Avalonia.Controls/Utils/GridLayout.cs index 717c696296..12aa6cf4b8 100644 --- a/src/Avalonia.Controls/Utils/GridLayout.cs +++ b/src/Avalonia.Controls/Utils/GridLayout.cs @@ -194,7 +194,7 @@ namespace Avalonia.Controls.Utils else if (finalLength - measure.ContainerLength < -LayoutTolerance) { // If the final length is smaller, we measure the M6/6 procedure only. - var dynamicConvention = ExpandStars(measure.LeanLengthList, measure.ContainerLength); + var dynamicConvention = ExpandStars(measure.LeanLengthList, finalLength); measure = new MeasureResult(finalLength, measure.DesiredLength, measure.GreedyDesiredLength, measure.LeanLengthList, dynamicConvention); } @@ -240,23 +240,28 @@ namespace Avalonia.Controls.Utils } } - if (double.IsInfinity(starUnitLength)) - { - starUnitLength = 0.0; - } + Debug.Assert(dynamicConvention.All(x => !x.Length.IsAuto)); - foreach (var convention in dynamicConvention.Where(x => x.Length.IsStar)) + var starUnit = starUnitLength; + var result = dynamicConvention.Select(x => { - convention.Fix(starUnitLength * convention.Length.Value); - } + if (x.Length.IsStar) + { + return double.IsInfinity(starUnit) ? double.PositiveInfinity : starUnit * x.Length.Value; + } - Debug.Assert(dynamicConvention.All(x => x.Length.IsAbsolute)); + return x.Length.Value; + }).ToList(); - return dynamicConvention.Select(x => x.Length.Value).ToList(); + return result; } private static void Clip(IList lengthList, double constraint) { + if (double.IsInfinity(constraint)) + { + return; + } var measureLength = 0.0; for (var i = 0; i < lengthList.Count; i++) { diff --git a/tests/Avalonia.Controls.UnitTests/GridLayoutTests.cs b/tests/Avalonia.Controls.UnitTests/GridLayoutTests.cs index 981adc2682..19d1f4e5c3 100644 --- a/tests/Avalonia.Controls.UnitTests/GridLayoutTests.cs +++ b/tests/Avalonia.Controls.UnitTests/GridLayoutTests.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; using Avalonia.Controls.Utils; using Xunit; @@ -7,8 +8,9 @@ namespace Avalonia.Controls.UnitTests { public class GridLayoutTests { + private const double Inf = double.PositiveInfinity; + [Theory] - [InlineData("100, 200, 300", double.PositiveInfinity, 600d, new[] { 100d, 200d, 300d })] [InlineData("100, 200, 300", 0d, 0d, new[] { 0d, 0d, 0d })] [InlineData("100, 200, 300", 800d, 600d, new[] { 100d, 200d, 300d })] [InlineData("100, 200, 300", 600d, 600d, new[] { 100d, 200d, 300d })] @@ -20,7 +22,6 @@ namespace Avalonia.Controls.UnitTests } [Theory] - [InlineData("*,2*,3*", double.PositiveInfinity, 0d, new[] { 0d, 0d, 0d })] [InlineData("*,2*,3*", 0d, 0d, new[] { 0d, 0d, 0d })] [InlineData("*,2*,3*", 600d, 0d, new[] { 100d, 200d, 300d })] public void MeasureArrange_AllStarLength_Correct(string length, double containerLength, @@ -30,7 +31,10 @@ namespace Avalonia.Controls.UnitTests } [Theory] + [InlineData("100,2*,3*", 0d, 0d, new[] { 0d, 0d, 0d })] [InlineData("100,2*,3*", 600d, 100d, new[] { 100d, 200d, 300d })] + [InlineData("100,2*,3*", 100d, 100d, new[] { 100d, 0d, 0d })] + [InlineData("100,2*,3*", 50d, 50d, new[] { 50d, 0d, 0d })] public void MeasureArrange_MixStarPixelLength_Correct(string length, double containerLength, double expectedDesiredLength, IList expectedLengthList) { @@ -38,7 +42,12 @@ namespace Avalonia.Controls.UnitTests } [Theory] + [InlineData("100,200,Auto", 0d, 0d, new[] { 0d, 0d, 0d })] [InlineData("100,200,Auto", 600d, 300d, new[] { 100d, 200d, 0d })] + [InlineData("100,200,Auto", 300d, 300d, new[] { 100d, 200d, 0d })] + [InlineData("100,200,Auto", 200d, 200d, new[] { 100d, 100d, 0d })] + [InlineData("100,200,Auto", 100d, 100d, new[] { 100d, 0d, 0d })] + [InlineData("100,200,Auto", 50d, 50d, new[] { 50d, 0d, 0d })] public void MeasureArrange_MixAutoPixelLength_Correct(string length, double containerLength, double expectedDesiredLength, IList expectedLengthList) { @@ -46,6 +55,7 @@ namespace Avalonia.Controls.UnitTests } [Theory] + [InlineData("*,2*,Auto", 0d, 0d, new[] { 0d, 0d, 0d })] [InlineData("*,2*,Auto", 600d, 0d, new[] { 200d, 400d, 0d })] public void MeasureArrange_MixAutoStarLength_Correct(string length, double containerLength, double expectedDesiredLength, IList expectedLengthList) @@ -54,7 +64,10 @@ namespace Avalonia.Controls.UnitTests } [Theory] + [InlineData("*,200,Auto", 0d, 0d, new[] { 0d, 0d, 0d })] [InlineData("*,200,Auto", 600d, 200d, new[] { 400d, 200d, 0d })] + [InlineData("*,200,Auto", 200d, 200d, new[] { 0d, 200d, 0d })] + [InlineData("*,200,Auto", 100d, 100d, new[] { 0d, 100d, 0d })] public void MeasureArrange_MixAutoStarPixelLength_Correct(string length, double containerLength, double expectedDesiredLength, IList expectedLengthList) { @@ -77,5 +90,51 @@ namespace Avalonia.Controls.UnitTests var arrange = layout.Arrange(containerLength, measure); Assert.Equal(expectedLengthList, arrange.LengthList); } + + [Theory] + [InlineData("100, 200, 300", 600d, new[] { 100d, 200d, 300d }, new[] { 100d, 200d, 300d })] + [InlineData("*,2*,3*", 0d, new[] { Inf, Inf, Inf }, new[] { 0d, 0d, 0d })] + [InlineData("100,2*,3*", 100d, new[] { 100d, Inf, Inf }, new[] { 100d, 0d, 0d })] + [InlineData("100,200,Auto", 300d, new[] { 100d, 200d, 0d }, new[] { 100d, 200d, 0d })] + [InlineData("*,2*,Auto", 0d, new[] { Inf, Inf, 0d }, new[] { 0d, 0d, 0d })] + [InlineData("*,200,Auto", 200d, new[] { Inf, 200d, 0d }, new[] { 0d, 200d, 0d })] + public void MeasureArrange_InfiniteMeasure_Correct(string length, double expectedDesiredLength, + IList expectedMeasureList, IList expectedArrangeList) + { + // Arrange + var layout = new GridLayout(new RowDefinitions(length)); + + // Measure - Action & Assert + var measure = layout.Measure(Inf); + Assert.Equal(expectedDesiredLength, measure.DesiredLength); + Assert.Equal(expectedMeasureList, measure.LengthList); + + // Arrange - Action & Assert + var arrange = layout.Arrange(measure.DesiredLength, measure); + Assert.Equal(expectedArrangeList, arrange.LengthList); + } + + [Theory] + [InlineData("Auto,*,*", new[] { 100d, 100d, 100d }, 600d, 100d, new[] { 100d, 250d, 250d })] + public void MeasureArrange_ChildHasSize_Correct(string length, + IList childLengthList, double containerLength, + double expectedDesiredLength, IList expectedLengthList) + { + // Arrange + var lengthList = new ColumnDefinitions(length); + var layout = new GridLayout(lengthList); + layout.AppendMeasureConventions( + Enumerable.Range(0, lengthList.Count).ToDictionary(x => x, x => (x, 1)), + x => childLengthList[x]); + + // Measure - Action & Assert + var measure = layout.Measure(containerLength); + Assert.Equal(expectedDesiredLength, measure.DesiredLength); + Assert.Equal(expectedLengthList, measure.LengthList); + + // Arrange - Action & Assert + var arrange = layout.Arrange(containerLength, measure); + Assert.Equal(expectedLengthList, arrange.LengthList); + } } }