RenderTests_Culling.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using Avalonia.Controls;
  2. using Avalonia.Media;
  3. using Avalonia.Platform;
  4. using Avalonia.Rendering;
  5. using Avalonia.UnitTests;
  6. using Moq;
  7. using Xunit;
  8. namespace Avalonia.Base.UnitTests
  9. {
  10. public class RenderTests_Culling
  11. {
  12. [Fact]
  13. public void In_Bounds_Control_Should_Be_Rendered()
  14. {
  15. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  16. {
  17. TestControl target;
  18. var container = new Canvas
  19. {
  20. Width = 100,
  21. Height = 100,
  22. ClipToBounds = true,
  23. Children =
  24. {
  25. (target = new TestControl
  26. {
  27. Width = 10, Height = 10, [Canvas.LeftProperty] = 98, [Canvas.TopProperty] = 98,
  28. })
  29. }
  30. };
  31. Render(container);
  32. Assert.True(target.Rendered);
  33. }
  34. }
  35. [Fact]
  36. public void Out_Of_Bounds_Control_Should_Not_Be_Rendered()
  37. {
  38. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  39. {
  40. TestControl target;
  41. var container = new Canvas
  42. {
  43. Width = 100,
  44. Height = 100,
  45. ClipToBounds = true,
  46. Children =
  47. {
  48. (target = new TestControl
  49. {
  50. Width = 10,
  51. Height = 10,
  52. ClipToBounds = true,
  53. [Canvas.LeftProperty] = 110,
  54. [Canvas.TopProperty] = 110,
  55. })
  56. }
  57. };
  58. Render(container);
  59. Assert.False(target.Rendered);
  60. }
  61. }
  62. [Fact]
  63. public void Out_Of_Bounds_Child_Control_Should_Not_Be_Rendered()
  64. {
  65. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  66. {
  67. TestControl target;
  68. var container = new Canvas
  69. {
  70. Width = 100,
  71. Height = 100,
  72. ClipToBounds = true,
  73. Children =
  74. {
  75. new Canvas
  76. {
  77. Width = 100,
  78. Height = 100,
  79. [Canvas.LeftProperty] = 50,
  80. [Canvas.TopProperty] = 50,
  81. Children =
  82. {
  83. (target = new TestControl
  84. {
  85. Width = 10,
  86. Height = 10,
  87. ClipToBounds = true,
  88. [Canvas.LeftProperty] = 50,
  89. [Canvas.TopProperty] = 50,
  90. })
  91. }
  92. }
  93. }
  94. };
  95. Render(container);
  96. Assert.False(target.Rendered);
  97. }
  98. }
  99. [Fact]
  100. public void RenderTransform_Should_Be_Respected()
  101. {
  102. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  103. {
  104. TestControl target;
  105. var container = new Canvas
  106. {
  107. Width = 100,
  108. Height = 100,
  109. ClipToBounds = true,
  110. Children =
  111. {
  112. (target = new TestControl
  113. {
  114. Width = 10,
  115. Height = 10,
  116. [Canvas.LeftProperty] = 110,
  117. [Canvas.TopProperty] = 110,
  118. RenderTransform = new TranslateTransform(-100, -100),
  119. })
  120. }
  121. };
  122. Render(container);
  123. Assert.True(target.Rendered);
  124. }
  125. }
  126. [Fact]
  127. public void Negative_Margin_Should_Be_Respected()
  128. {
  129. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  130. {
  131. TestControl target;
  132. var container = new Canvas
  133. {
  134. Width = 100,
  135. Height = 100,
  136. ClipToBounds = true,
  137. Children =
  138. {
  139. new Border
  140. {
  141. Margin = new Thickness(100, 100, 0, 0),
  142. Child = target = new TestControl
  143. {
  144. Width = 10, Height = 10, Margin = new Thickness(-100, -100, 0, 0),
  145. }
  146. }
  147. }
  148. };
  149. Render(container);
  150. Assert.True(target.Rendered);
  151. }
  152. }
  153. private void Render(Control control)
  154. {
  155. var ctx = CreateDrawingContext();
  156. control.Measure(Size.Infinity);
  157. control.Arrange(new Rect(control.DesiredSize));
  158. ImmediateRenderer.Render(control, ctx);
  159. }
  160. private DrawingContext CreateDrawingContext()
  161. {
  162. return new PlatformDrawingContext(Mock.Of<IDrawingContextImpl>());
  163. }
  164. private class TestControl : Control
  165. {
  166. public bool Rendered { get; private set; }
  167. public override void Render(DrawingContext context)
  168. {
  169. Rendered = true;
  170. }
  171. }
  172. }
  173. }