BorderTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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
  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. [Fact]
  42. public void Changing_Background_Brush_Color_Should_Invalidate_Visual()
  43. {
  44. var target = new Border()
  45. {
  46. Background = new SolidColorBrush(Colors.Red),
  47. };
  48. var root = new TestRoot(target);
  49. var renderer = Mock.Get(root.Renderer);
  50. renderer.Invocations.Clear();
  51. ((SolidColorBrush)target.Background).Color = Colors.Green;
  52. renderer.Verify(x => x.AddDirty(target), Times.Once);
  53. }
  54. public class UseLayoutRounding
  55. {
  56. [Fact]
  57. public void Measure_Rounds_Padding()
  58. {
  59. var target = new Border
  60. {
  61. Padding = new Thickness(1),
  62. Child = new Canvas
  63. {
  64. Width = 101,
  65. Height = 101,
  66. }
  67. };
  68. var root = CreatedRoot(1.5, target);
  69. root.LayoutManager.ExecuteInitialLayoutPass();
  70. // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666
  71. // - Size of 101 gets rounded up to 101.3333
  72. // - Desired size = 101.3333 + 2.6666 = 104
  73. Assert.Equal(new Size(104, 104), target.DesiredSize);
  74. }
  75. [Fact]
  76. public void Measure_Rounds_BorderThickness()
  77. {
  78. var target = new Border
  79. {
  80. BorderThickness = new Thickness(1),
  81. Child = new Canvas
  82. {
  83. Width = 101,
  84. Height = 101,
  85. }
  86. };
  87. var root = CreatedRoot(1.5, target);
  88. root.LayoutManager.ExecuteInitialLayoutPass();
  89. // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666
  90. // - Size of 101 gets rounded up to 101.3333
  91. // - Desired size = 101.3333 + 2.6666 = 104
  92. Assert.Equal(new Size(104, 104), target.DesiredSize);
  93. }
  94. private static TestRoot CreatedRoot(
  95. double scaling,
  96. Control child,
  97. Size? constraint = null)
  98. {
  99. return new TestRoot
  100. {
  101. LayoutScaling = scaling,
  102. UseLayoutRounding = true,
  103. Child = child,
  104. ClientSize = constraint ?? new Size(1000, 1000),
  105. };
  106. }
  107. }
  108. }
  109. }