MeasureTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls;
  4. using Xunit;
  5. namespace Avalonia.Layout.UnitTests
  6. {
  7. public class MeasureTests
  8. {
  9. [Fact]
  10. public void Margin_Should_Be_Included_In_DesiredSize()
  11. {
  12. var decorator = new Decorator
  13. {
  14. Width = 100,
  15. Height = 100,
  16. Margin = new Thickness(8),
  17. };
  18. decorator.Measure(Size.Infinity);
  19. Assert.Equal(new Size(116, 116), decorator.DesiredSize);
  20. }
  21. [Fact]
  22. public void Invalidating_Child_Should_Not_Invalidate_Parent()
  23. {
  24. var panel = new StackPanel();
  25. var child = new Border();
  26. panel.Children.Add(child);
  27. panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  28. Assert.Equal(new Size(0, 0), panel.DesiredSize);
  29. child.Width = 100;
  30. child.Height = 100;
  31. Assert.True(panel.IsMeasureValid);
  32. Assert.False(child.IsMeasureValid);
  33. panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  34. Assert.Equal(new Size(0, 0), panel.DesiredSize);
  35. }
  36. [Fact]
  37. public void Removing_From_Parent_Should_Invalidate_Measure_Of_Control_And_Descendents()
  38. {
  39. var panel = new StackPanel();
  40. var child2 = new Border();
  41. var child1 = new Border { Child = child2 };
  42. panel.Children.Add(child1);
  43. panel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  44. Assert.True(child1.IsMeasureValid);
  45. Assert.True(child2.IsMeasureValid);
  46. panel.Children.Remove(child1);
  47. Assert.False(child1.IsMeasureValid);
  48. Assert.False(child2.IsMeasureValid);
  49. }
  50. [Fact]
  51. public void Negative_Margin_Larger_Than_Constraint_Should_Request_Width_0()
  52. {
  53. Control target;
  54. var outer = new Decorator
  55. {
  56. Width = 100,
  57. Height = 100,
  58. Child = target = new Control
  59. {
  60. Margin = new Thickness(-100, 0, 0, 0),
  61. }
  62. };
  63. outer.Measure(Size.Infinity);
  64. Assert.Equal(0, target.DesiredSize.Width);
  65. }
  66. [Fact]
  67. public void Negative_Margin_Larger_Than_Constraint_Should_Request_Height_0()
  68. {
  69. Control target;
  70. var outer = new Decorator
  71. {
  72. Width = 100,
  73. Height = 100,
  74. Child = target = new Control
  75. {
  76. Margin = new Thickness(0, -100, 0, 0),
  77. }
  78. };
  79. outer.Measure(Size.Infinity);
  80. Assert.Equal(0, target.DesiredSize.Height);
  81. }
  82. [Fact]
  83. public void Margin_Should_Affect_AvailableSize()
  84. {
  85. MeasureTest target;
  86. var outer = new Decorator
  87. {
  88. Width = 100,
  89. Height = 100,
  90. Child = target = new MeasureTest
  91. {
  92. Margin = new Thickness(10),
  93. }
  94. };
  95. outer.Measure(Size.Infinity);
  96. Assert.Equal(new Size(80, 80), target.AvailableSize);
  97. }
  98. [Fact]
  99. public void Margin_Should_Be_Applied_Before_Width_Height()
  100. {
  101. MeasureTest target;
  102. var outer = new Decorator
  103. {
  104. Width = 100,
  105. Height = 100,
  106. Child = target = new MeasureTest
  107. {
  108. Width = 80,
  109. Height = 80,
  110. Margin = new Thickness(10),
  111. }
  112. };
  113. outer.Measure(Size.Infinity);
  114. Assert.Equal(new Size(80, 80), target.AvailableSize);
  115. }
  116. class MeasureTest : Control
  117. {
  118. public Size? AvailableSize { get; private set; }
  119. protected override Size MeasureOverride(Size availableSize)
  120. {
  121. AvailableSize = availableSize;
  122. return availableSize;
  123. }
  124. }
  125. }
  126. }