GridMocks.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Xunit;
  4. namespace Avalonia.Controls.UnitTests
  5. {
  6. internal static class GridMock
  7. {
  8. /// <summary>
  9. /// Create a mock grid to test its row layout.
  10. /// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`).
  11. /// </summary>
  12. /// <param name="measure">The measure height of this grid. PositiveInfinity by default.</param>
  13. /// <param name="arrange">The arrange height of this grid. DesiredSize.Height by default.</param>
  14. /// <returns>The mock grid that its children bounds will be tested.</returns>
  15. internal static Grid New(Size measure = default, Size arrange = default)
  16. {
  17. var grid = new Grid();
  18. grid.Children.Add(new Border());
  19. grid.Measure(measure == default ? new Size(double.PositiveInfinity, double.PositiveInfinity) : measure);
  20. grid.Arrange(new Rect(default, arrange == default ? grid.DesiredSize : arrange));
  21. return grid;
  22. }
  23. /// <summary>
  24. /// Create a mock grid to test its row layout.
  25. /// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`).
  26. /// </summary>
  27. /// <param name="rows">The row definitions of this mock grid.</param>
  28. /// <param name="measure">The measure height of this grid. PositiveInfinity by default.</param>
  29. /// <param name="arrange">The arrange height of this grid. DesiredSize.Height by default.</param>
  30. /// <returns>The mock grid that its children bounds will be tested.</returns>
  31. [SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")]
  32. internal static Grid New(RowDefinitions rows,
  33. double measure = default, double arrange = default)
  34. {
  35. var grid = new Grid { RowDefinitions = rows };
  36. for (var i = 0; i < rows.Count; i++)
  37. {
  38. grid.Children.Add(new Border { [Grid.RowProperty] = i });
  39. }
  40. grid.Measure(new Size(double.PositiveInfinity, measure == default ? double.PositiveInfinity : measure));
  41. if (arrange == default)
  42. {
  43. arrange = measure == default ? grid.DesiredSize.Width : measure;
  44. }
  45. grid.Arrange(new Rect(0, 0, 0, arrange));
  46. return grid;
  47. }
  48. /// <summary>
  49. /// Create a mock grid to test its column layout.
  50. /// This method contains Arrange (`new Grid()`) and Action (`Measure()`/`Arrange()`).
  51. /// </summary>
  52. /// <param name="columns">The column definitions of this mock grid.</param>
  53. /// <param name="measure">The measure width of this grid. PositiveInfinity by default.</param>
  54. /// <param name="arrange">The arrange width of this grid. DesiredSize.Width by default.</param>
  55. /// <returns>The mock grid that its children bounds will be tested.</returns>
  56. [SuppressMessage("ReSharper", "CompareOfFloatsByEqualityOperator")]
  57. internal static Grid New(ColumnDefinitions columns,
  58. double measure = default, double arrange = default)
  59. {
  60. var grid = new Grid { ColumnDefinitions = columns };
  61. for (var i = 0; i < columns.Count; i++)
  62. {
  63. grid.Children.Add(new Border { [Grid.ColumnProperty] = i });
  64. }
  65. grid.Measure(new Size(measure == default ? double.PositiveInfinity : measure, double.PositiveInfinity));
  66. if (arrange == default)
  67. {
  68. arrange = measure == default ? grid.DesiredSize.Width : measure;
  69. }
  70. grid.Arrange(new Rect(0, 0, arrange, 0));
  71. return grid;
  72. }
  73. }
  74. internal static class GridAssert
  75. {
  76. /// <summary>
  77. /// Assert all the children heights.
  78. /// This method will assume that the grid children count equals row count.
  79. /// </summary>
  80. /// <param name="grid">The children will be fetched through it.</param>
  81. /// <param name="rows">Expected row values of every children.</param>
  82. internal static void ChildrenHeight(Grid grid, params double[] rows)
  83. {
  84. if (grid.Children.Count != rows.Length)
  85. {
  86. throw new NotSupportedException();
  87. }
  88. for (var i = 0; i < rows.Length; i++)
  89. {
  90. Assert.Equal(rows[i], grid.Children[i].Bounds.Height);
  91. }
  92. }
  93. /// <summary>
  94. /// Assert all the children widths.
  95. /// This method will assume that the grid children count equals row count.
  96. /// </summary>
  97. /// <param name="grid">The children will be fetched through it.</param>
  98. /// <param name="columns">Expected column values of every children.</param>
  99. internal static void ChildrenWidth(Grid grid, params double[] columns)
  100. {
  101. if (grid.Children.Count != columns.Length)
  102. {
  103. throw new NotSupportedException();
  104. }
  105. for (var i = 0; i < columns.Length; i++)
  106. {
  107. Assert.Equal(columns[i], grid.Children[i].Bounds.Width);
  108. }
  109. }
  110. }
  111. }