BorderTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Avalonia.Layout;
  2. using Avalonia.Media;
  3. using Avalonia.Rendering;
  4. using Avalonia.UnitTests;
  5. using Avalonia.VisualTree;
  6. using Moq;
  7. using Xunit;
  8. namespace Avalonia.Controls.UnitTests
  9. {
  10. public class BorderTests : ScopedTestBase
  11. {
  12. [Fact]
  13. public void Measure_Should_Return_BorderThickness_Plus_Padding_When_No_Child_Present()
  14. {
  15. var target = new Border
  16. {
  17. Padding = new Thickness(6),
  18. BorderThickness = new Thickness(4)
  19. };
  20. target.Measure(new Size(100, 100));
  21. Assert.Equal(new Size(20, 20), target.DesiredSize);
  22. }
  23. [Fact]
  24. public void Child_Should_Arrange_With_Zero_Height_Width_If_Padding_Greater_Than_Child_Size()
  25. {
  26. Border content;
  27. var target = new Border
  28. {
  29. Padding = new Thickness(6),
  30. MaxHeight = 12,
  31. MaxWidth = 12,
  32. Child = content = new Border
  33. {
  34. Height = 0,
  35. Width = 0
  36. }
  37. };
  38. target.Arrange(new Rect(0, 0, 100, 100));
  39. Assert.Equal(new Rect(6, 6, 0, 0), content.Bounds);
  40. }
  41. public class UseLayoutRounding : ScopedTestBase
  42. {
  43. [Fact]
  44. public void Measure_Rounds_Padding()
  45. {
  46. var target = new Border
  47. {
  48. Padding = new Thickness(1),
  49. Child = new Canvas
  50. {
  51. Width = 101,
  52. Height = 101,
  53. }
  54. };
  55. var root = CreateRoot(1.5, target);
  56. root.LayoutManager.ExecuteInitialLayoutPass();
  57. // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666
  58. // - Size of 101 gets rounded up to 101.3333
  59. // - Desired size = 101.3333 + 2.6666 = 104
  60. Assert.Equal(new Size(104, 104), target.DesiredSize);
  61. }
  62. [Fact]
  63. public void Measure_Rounds_BorderThickness()
  64. {
  65. var target = new Border
  66. {
  67. BorderThickness = new Thickness(1),
  68. Child = new Canvas
  69. {
  70. Width = 101,
  71. Height = 101,
  72. }
  73. };
  74. var root = CreateRoot(1.5, target);
  75. root.LayoutManager.ExecuteInitialLayoutPass();
  76. // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666
  77. // - Size of 101 gets rounded up to 101.3333
  78. // - Desired size = 101.3333 + 2.6666 = 104
  79. Assert.Equal(new Size(104, 104), target.DesiredSize);
  80. }
  81. [Fact]
  82. public void Measure_Arranges_Child_To_Rounded_BorderThickness()
  83. {
  84. Canvas child;
  85. var target = new Border
  86. {
  87. BorderThickness = new Thickness(1),
  88. Width = 82,
  89. Height = 82,
  90. Child = child = new Canvas(),
  91. };
  92. var root = CreateRoot(1.5, target);
  93. root.LayoutManager.ExecuteInitialLayoutPass();
  94. // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666
  95. // - Size of 82 needs no rounding
  96. // - Minus border thickness, space for child is 82 - 2.6666 = 79.3333
  97. Assert.Equal(1.3333, child.Bounds.Left, 3);
  98. Assert.Equal(1.3333, child.Bounds.Top, 3);
  99. Assert.Equal(79.3333, child.Bounds.Width, 3);
  100. Assert.Equal(79.3333, child.Bounds.Height, 3);
  101. }
  102. [Fact]
  103. public void Measure_Arranges_Child_With_Rounded_Margin()
  104. {
  105. Border child;
  106. var target = new Border
  107. {
  108. Width = 220,
  109. Height = 220,
  110. Child = child = new Border
  111. {
  112. Margin = new Thickness(0, 25, 25, 25),
  113. },
  114. };
  115. var root = CreateRoot(1.5, target);
  116. root.LayoutManager.ExecuteInitialLayoutPass();
  117. // - 25 margin gets rounded up to 25.3333
  118. // - Size of 220 needs no rounding
  119. Assert.Equal(0, child.Bounds.Left, 3);
  120. Assert.Equal(25.3333, child.Bounds.Top, 3);
  121. Assert.Equal(194.6666, child.Bounds.Width, 3);
  122. Assert.Equal(169.3333, child.Bounds.Height, 3);
  123. }
  124. private static TestRoot CreateRoot(
  125. double scaling,
  126. Control child,
  127. Size? constraint = null)
  128. {
  129. return new TestRoot
  130. {
  131. LayoutScaling = scaling,
  132. UseLayoutRounding = true,
  133. Child = child,
  134. ClientSize = constraint ?? new Size(1000, 1000),
  135. };
  136. }
  137. }
  138. }
  139. }