RenderTests_Culling.cs 5.3 KB

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