LayoutableTests.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. using (Start(target.Object))
  93. {
  94. var control = new Decorator();
  95. var root = new LayoutTestRoot { Child = control };
  96. root.Measure(Size.Infinity);
  97. root.Arrange(new Rect(root.DesiredSize));
  98. target.ResetCalls();
  99. control.InvalidateMeasure();
  100. control.InvalidateMeasure();
  101. target.Verify(x => x.InvalidateMeasure(control), Times.Once());
  102. }
  103. }
  104. [Fact]
  105. public void Only_Calls_LayoutManager_InvalidateArrange_Once()
  106. {
  107. var target = new Mock<ILayoutManager>();
  108. using (Start(target.Object))
  109. {
  110. var control = new Decorator();
  111. var root = new LayoutTestRoot { Child = control };
  112. root.Measure(Size.Infinity);
  113. root.Arrange(new Rect(root.DesiredSize));
  114. target.ResetCalls();
  115. control.InvalidateArrange();
  116. control.InvalidateArrange();
  117. target.Verify(x => x.InvalidateArrange(control), Times.Once());
  118. }
  119. }
  120. [Fact]
  121. public void Attaching_Control_To_Tree_Invalidates_Parent_Measure()
  122. {
  123. var target = new Mock<ILayoutManager>();
  124. using (Start(target.Object))
  125. {
  126. var control = new Decorator();
  127. var root = new LayoutTestRoot { Child = control };
  128. root.Measure(Size.Infinity);
  129. root.Arrange(new Rect(root.DesiredSize));
  130. Assert.True(control.IsMeasureValid);
  131. root.Child = null;
  132. root.Measure(Size.Infinity);
  133. root.Arrange(new Rect(root.DesiredSize));
  134. Assert.False(control.IsMeasureValid);
  135. Assert.True(root.IsMeasureValid);
  136. target.ResetCalls();
  137. root.Child = control;
  138. Assert.False(root.IsMeasureValid);
  139. Assert.False(control.IsMeasureValid);
  140. target.Verify(x => x.InvalidateMeasure(root), Times.Once());
  141. }
  142. }
  143. private IDisposable Start(ILayoutManager layoutManager)
  144. {
  145. var result = AvaloniaLocator.EnterScope();
  146. AvaloniaLocator.CurrentMutable.Bind<ILayoutManager>().ToConstant(layoutManager);
  147. return result;
  148. }
  149. private class TestLayoutable : Layoutable
  150. {
  151. public Size ArrangeSize { get; private set; }
  152. public Size MeasureResult { get; set; } = new Size(10, 10);
  153. public Size MeasureSize { get; private set; }
  154. protected override Size MeasureOverride(Size availableSize)
  155. {
  156. MeasureSize = availableSize;
  157. return MeasureResult;
  158. }
  159. protected override Size ArrangeOverride(Size finalSize)
  160. {
  161. ArrangeSize = finalSize;
  162. return base.ArrangeOverride(finalSize);
  163. }
  164. }
  165. }
  166. }