LayoutableTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using Avalonia.Controls;
  3. using Moq;
  4. using Xunit;
  5. namespace Avalonia.Layout.UnitTests
  6. {
  7. public class LayoutableTests
  8. {
  9. [Theory]
  10. [InlineData(0, 0, 0, 0, 100, 100)]
  11. [InlineData(10, 0, 0, 0, 90, 100)]
  12. [InlineData(10, 0, 5, 0, 85, 100)]
  13. [InlineData(0, 10, 0, 0, 100, 90)]
  14. [InlineData(0, 10, 0, 5, 100, 85)]
  15. [InlineData(4, 4, 6, 7, 90, 89)]
  16. public void Margin_Is_Applied_To_MeasureOverride_Size(
  17. double l,
  18. double t,
  19. double r,
  20. double b,
  21. double expectedWidth,
  22. double expectedHeight)
  23. {
  24. var target = new TestLayoutable
  25. {
  26. Margin = new Thickness(l, t, r, b),
  27. };
  28. target.Measure(new Size(100, 100));
  29. Assert.Equal(new Size(expectedWidth, expectedHeight), target.MeasureSize);
  30. }
  31. [Theory]
  32. [InlineData(HorizontalAlignment.Stretch, 100)]
  33. [InlineData(HorizontalAlignment.Left, 10)]
  34. [InlineData(HorizontalAlignment.Center, 10)]
  35. [InlineData(HorizontalAlignment.Right, 10)]
  36. public void HorizontalAlignment_Is_Applied_To_ArrangeOverride_Size(
  37. HorizontalAlignment h,
  38. double expectedWidth)
  39. {
  40. var target = new TestLayoutable
  41. {
  42. HorizontalAlignment = h,
  43. };
  44. target.Measure(Size.Infinity);
  45. target.Arrange(new Rect(0, 0, 100, 100));
  46. Assert.Equal(new Size(expectedWidth, 100), target.ArrangeSize);
  47. }
  48. [Theory]
  49. [InlineData(VerticalAlignment.Stretch, 100)]
  50. [InlineData(VerticalAlignment.Top, 10)]
  51. [InlineData(VerticalAlignment.Center, 10)]
  52. [InlineData(VerticalAlignment.Bottom, 10)]
  53. public void VerticalAlignment_Is_Applied_To_ArrangeOverride_Size(
  54. VerticalAlignment v,
  55. double expectedHeight)
  56. {
  57. var target = new TestLayoutable
  58. {
  59. VerticalAlignment = v,
  60. };
  61. target.Measure(Size.Infinity);
  62. target.Arrange(new Rect(0, 0, 100, 100));
  63. Assert.Equal(new Size(100, expectedHeight), target.ArrangeSize);
  64. }
  65. [Theory]
  66. [InlineData(0, 0, 0, 0, 100, 100)]
  67. [InlineData(10, 0, 0, 0, 90, 100)]
  68. [InlineData(10, 0, 5, 0, 85, 100)]
  69. [InlineData(0, 10, 0, 0, 100, 90)]
  70. [InlineData(0, 10, 0, 5, 100, 85)]
  71. [InlineData(4, 4, 6, 7, 90, 89)]
  72. public void Margin_Is_Applied_To_ArrangeOverride_Size(
  73. double l,
  74. double t,
  75. double r,
  76. double b,
  77. double expectedWidth,
  78. double expectedHeight)
  79. {
  80. var target = new TestLayoutable
  81. {
  82. Margin = new Thickness(l, t, r, b),
  83. };
  84. target.Measure(Size.Infinity);
  85. target.Arrange(new Rect(0, 0, 100, 100));
  86. Assert.Equal(new Size(expectedWidth, expectedHeight), target.ArrangeSize);
  87. }
  88. [Fact]
  89. public void Only_Calls_LayoutManager_InvalidateMeasure_Once()
  90. {
  91. var target = new Mock<ILayoutManager>();
  92. var control = new Decorator();
  93. var root = new LayoutTestRoot
  94. {
  95. Child = control,
  96. LayoutManager = target.Object,
  97. };
  98. root.Measure(Size.Infinity);
  99. root.Arrange(new Rect(root.DesiredSize));
  100. target.ResetCalls();
  101. control.InvalidateMeasure();
  102. control.InvalidateMeasure();
  103. target.Verify(x => x.InvalidateMeasure(control), Times.Once());
  104. }
  105. [Fact]
  106. public void Only_Calls_LayoutManager_InvalidateArrange_Once()
  107. {
  108. var target = new Mock<ILayoutManager>();
  109. var control = new Decorator();
  110. var root = new LayoutTestRoot
  111. {
  112. Child = control,
  113. LayoutManager = target.Object,
  114. };
  115. root.Measure(Size.Infinity);
  116. root.Arrange(new Rect(root.DesiredSize));
  117. target.ResetCalls();
  118. control.InvalidateArrange();
  119. control.InvalidateArrange();
  120. target.Verify(x => x.InvalidateArrange(control), Times.Once());
  121. }
  122. [Fact]
  123. public void Attaching_Control_To_Tree_Invalidates_Parent_Measure()
  124. {
  125. var target = new Mock<ILayoutManager>();
  126. var control = new Decorator();
  127. var root = new LayoutTestRoot
  128. {
  129. Child = control,
  130. LayoutManager = target.Object,
  131. };
  132. root.Measure(Size.Infinity);
  133. root.Arrange(new Rect(root.DesiredSize));
  134. Assert.True(control.IsMeasureValid);
  135. root.Child = null;
  136. root.Measure(Size.Infinity);
  137. root.Arrange(new Rect(root.DesiredSize));
  138. Assert.False(control.IsMeasureValid);
  139. Assert.True(root.IsMeasureValid);
  140. target.ResetCalls();
  141. root.Child = control;
  142. Assert.False(root.IsMeasureValid);
  143. Assert.False(control.IsMeasureValid);
  144. target.Verify(x => x.InvalidateMeasure(root), Times.Once());
  145. }
  146. private class TestLayoutable : Layoutable
  147. {
  148. public Size ArrangeSize { get; private set; }
  149. public Size MeasureResult { get; set; } = new Size(10, 10);
  150. public Size MeasureSize { get; private set; }
  151. protected override Size MeasureOverride(Size availableSize)
  152. {
  153. MeasureSize = availableSize;
  154. return MeasureResult;
  155. }
  156. protected override Size ArrangeOverride(Size finalSize)
  157. {
  158. ArrangeSize = finalSize;
  159. return base.ArrangeOverride(finalSize);
  160. }
  161. }
  162. }
  163. }