GridLayoutTests.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections.Generic;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Linq;
  4. using Avalonia.Controls.Utils;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests
  7. {
  8. public class GridLayoutTests
  9. {
  10. private const double Inf = double.PositiveInfinity;
  11. [Theory]
  12. [InlineData("100, 200, 300", 0d, 0d, new[] { 0d, 0d, 0d })]
  13. [InlineData("100, 200, 300", 800d, 600d, new[] { 100d, 200d, 300d })]
  14. [InlineData("100, 200, 300", 600d, 600d, new[] { 100d, 200d, 300d })]
  15. [InlineData("100, 200, 300", 400d, 400d, new[] { 100d, 200d, 100d })]
  16. public void MeasureArrange_AllPixelLength_Correct(string length, double containerLength,
  17. double expectedDesiredLength, IList<double> expectedLengthList)
  18. {
  19. TestRowDefinitionsOnly(length, containerLength, expectedDesiredLength, expectedLengthList);
  20. }
  21. [Theory]
  22. [InlineData("*,2*,3*", 0d, 0d, new[] { 0d, 0d, 0d })]
  23. [InlineData("*,2*,3*", 600d, 0d, new[] { 100d, 200d, 300d })]
  24. public void MeasureArrange_AllStarLength_Correct(string length, double containerLength,
  25. double expectedDesiredLength, IList<double> expectedLengthList)
  26. {
  27. TestRowDefinitionsOnly(length, containerLength, expectedDesiredLength, expectedLengthList);
  28. }
  29. [Theory]
  30. [InlineData("100,2*,3*", 0d, 0d, new[] { 0d, 0d, 0d })]
  31. [InlineData("100,2*,3*", 600d, 100d, new[] { 100d, 200d, 300d })]
  32. [InlineData("100,2*,3*", 100d, 100d, new[] { 100d, 0d, 0d })]
  33. [InlineData("100,2*,3*", 50d, 50d, new[] { 50d, 0d, 0d })]
  34. public void MeasureArrange_MixStarPixelLength_Correct(string length, double containerLength,
  35. double expectedDesiredLength, IList<double> expectedLengthList)
  36. {
  37. TestRowDefinitionsOnly(length, containerLength, expectedDesiredLength, expectedLengthList);
  38. }
  39. [Theory]
  40. [InlineData("100,200,Auto", 0d, 0d, new[] { 0d, 0d, 0d })]
  41. [InlineData("100,200,Auto", 600d, 300d, new[] { 100d, 200d, 0d })]
  42. [InlineData("100,200,Auto", 300d, 300d, new[] { 100d, 200d, 0d })]
  43. [InlineData("100,200,Auto", 200d, 200d, new[] { 100d, 100d, 0d })]
  44. [InlineData("100,200,Auto", 100d, 100d, new[] { 100d, 0d, 0d })]
  45. [InlineData("100,200,Auto", 50d, 50d, new[] { 50d, 0d, 0d })]
  46. public void MeasureArrange_MixAutoPixelLength_Correct(string length, double containerLength,
  47. double expectedDesiredLength, IList<double> expectedLengthList)
  48. {
  49. TestRowDefinitionsOnly(length, containerLength, expectedDesiredLength, expectedLengthList);
  50. }
  51. [Theory]
  52. [InlineData("*,2*,Auto", 0d, 0d, new[] { 0d, 0d, 0d })]
  53. [InlineData("*,2*,Auto", 600d, 0d, new[] { 200d, 400d, 0d })]
  54. public void MeasureArrange_MixAutoStarLength_Correct(string length, double containerLength,
  55. double expectedDesiredLength, IList<double> expectedLengthList)
  56. {
  57. TestRowDefinitionsOnly(length, containerLength, expectedDesiredLength, expectedLengthList);
  58. }
  59. [Theory]
  60. [InlineData("*,200,Auto", 0d, 0d, new[] { 0d, 0d, 0d })]
  61. [InlineData("*,200,Auto", 600d, 200d, new[] { 400d, 200d, 0d })]
  62. [InlineData("*,200,Auto", 200d, 200d, new[] { 0d, 200d, 0d })]
  63. [InlineData("*,200,Auto", 100d, 100d, new[] { 0d, 100d, 0d })]
  64. public void MeasureArrange_MixAutoStarPixelLength_Correct(string length, double containerLength,
  65. double expectedDesiredLength, IList<double> expectedLengthList)
  66. {
  67. TestRowDefinitionsOnly(length, containerLength, expectedDesiredLength, expectedLengthList);
  68. }
  69. [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local")]
  70. private static void TestRowDefinitionsOnly(string length, double containerLength,
  71. double expectedDesiredLength, IList<double> expectedLengthList)
  72. {
  73. // Arrange
  74. var layout = new GridLayout(new RowDefinitions(length));
  75. // Measure - Action & Assert
  76. var measure = layout.Measure(containerLength);
  77. Assert.Equal(expectedDesiredLength, measure.DesiredLength);
  78. Assert.Equal(expectedLengthList, measure.LengthList);
  79. // Arrange - Action & Assert
  80. var arrange = layout.Arrange(containerLength, measure);
  81. Assert.Equal(expectedLengthList, arrange.LengthList);
  82. }
  83. [Theory]
  84. [InlineData("100, 200, 300", 600d, new[] { 100d, 200d, 300d }, new[] { 100d, 200d, 300d })]
  85. [InlineData("*,2*,3*", 0d, new[] { Inf, Inf, Inf }, new[] { 0d, 0d, 0d })]
  86. [InlineData("100,2*,3*", 100d, new[] { 100d, Inf, Inf }, new[] { 100d, 0d, 0d })]
  87. [InlineData("100,200,Auto", 300d, new[] { 100d, 200d, 0d }, new[] { 100d, 200d, 0d })]
  88. [InlineData("*,2*,Auto", 0d, new[] { Inf, Inf, 0d }, new[] { 0d, 0d, 0d })]
  89. [InlineData("*,200,Auto", 200d, new[] { Inf, 200d, 0d }, new[] { 0d, 200d, 0d })]
  90. public void MeasureArrange_InfiniteMeasure_Correct(string length, double expectedDesiredLength,
  91. IList<double> expectedMeasureList, IList<double> expectedArrangeList)
  92. {
  93. // Arrange
  94. var layout = new GridLayout(new RowDefinitions(length));
  95. // Measure - Action & Assert
  96. var measure = layout.Measure(Inf);
  97. Assert.Equal(expectedDesiredLength, measure.DesiredLength);
  98. Assert.Equal(expectedMeasureList, measure.LengthList);
  99. // Arrange - Action & Assert
  100. var arrange = layout.Arrange(measure.DesiredLength, measure);
  101. Assert.Equal(expectedArrangeList, arrange.LengthList);
  102. }
  103. [Theory]
  104. [InlineData("Auto,*,*", new[] { 100d, 100d, 100d }, 600d, 300d, new[] { 100d, 250d, 250d })]
  105. public void MeasureArrange_ChildHasSize_Correct(string length,
  106. IList<double> childLengthList, double containerLength,
  107. double expectedDesiredLength, IList<double> expectedLengthList)
  108. {
  109. // Arrange
  110. var lengthList = new ColumnDefinitions(length);
  111. var layout = new GridLayout(lengthList);
  112. layout.AppendMeasureConventions(
  113. Enumerable.Range(0, lengthList.Count).ToDictionary(x => x, x => (x, 1)),
  114. x => childLengthList[x]);
  115. // Measure - Action & Assert
  116. var measure = layout.Measure(containerLength);
  117. Assert.Equal(expectedDesiredLength, measure.DesiredLength);
  118. Assert.Equal(expectedLengthList, measure.LengthList);
  119. // Arrange - Action & Assert
  120. var arrange = layout.Arrange(containerLength, measure);
  121. Assert.Equal(expectedLengthList, arrange.LengthList);
  122. }
  123. [Theory]
  124. [InlineData(Inf, 250d, new[] { 100d, Inf, Inf }, new[] { 100d, 50d, 100d })]
  125. [InlineData(400d, 250d, new[] { 100d, 100d, 200d }, new[] { 100d, 100d, 200d })]
  126. [InlineData(325d, 250d, new[] { 100d, 75d, 150d }, new[] { 100d, 75d, 150d })]
  127. [InlineData(250d, 250d, new[] { 100d, 50d, 100d }, new[] { 100d, 50d, 100d })]
  128. [InlineData(160d, 160d, new[] { 100d, 20d, 40d }, new[] { 100d, 20d, 40d })]
  129. public void MeasureArrange_ChildHasSizeAndHasMultiSpan_Correct(
  130. double containerLength, double expectedDesiredLength,
  131. IList<double> expectedMeasureLengthList, IList<double> expectedArrangeLengthList)
  132. {
  133. var length = "100,*,2*";
  134. var childLengthList = new[] { 150d, 150d, 150d };
  135. var spans = new[] { 1, 2, 1 };
  136. // Arrange
  137. var lengthList = new ColumnDefinitions(length);
  138. var layout = new GridLayout(lengthList);
  139. layout.AppendMeasureConventions(
  140. Enumerable.Range(0, lengthList.Count).ToDictionary(x => x, x => (x, spans[x])),
  141. x => childLengthList[x]);
  142. // Measure - Action & Assert
  143. var measure = layout.Measure(containerLength);
  144. Assert.Equal(expectedDesiredLength, measure.DesiredLength);
  145. Assert.Equal(expectedMeasureLengthList, measure.LengthList);
  146. // Arrange - Action & Assert
  147. var arrange = layout.Arrange(
  148. double.IsInfinity(containerLength) ? measure.DesiredLength : containerLength,
  149. measure);
  150. Assert.Equal(expectedArrangeLengthList, arrange.LengthList);
  151. }
  152. }
  153. }