RenderTests_Culling.cs 6.0 KB

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