CustomRenderTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Threading.Tasks;
  3. using Avalonia.Controls;
  4. using Avalonia.Layout;
  5. using Avalonia.Media;
  6. using Xunit;
  7. namespace Avalonia.Skia.RenderTests
  8. {
  9. public class CustomRenderTests : TestBase
  10. {
  11. public CustomRenderTests()
  12. : base(@"Controls\CustomRender")
  13. {
  14. }
  15. [Fact]
  16. public async Task Clip()
  17. {
  18. Decorator target = new Decorator
  19. {
  20. Padding = new Thickness(8),
  21. Width = 200,
  22. Height = 200,
  23. Child = new CustomRenderer((control, context) =>
  24. {
  25. context.FillRectangle(
  26. Brushes.Red,
  27. new Rect(control.Bounds.Size),
  28. 4);
  29. using (context.PushClip(new Rect(control.Bounds.Size).Deflate(10)))
  30. {
  31. context.FillRectangle(
  32. Brushes.Blue,
  33. new Rect(control.Bounds.Size),
  34. 4);
  35. }
  36. }),
  37. };
  38. await RenderToFile(target);
  39. CompareImages();
  40. }
  41. [Fact]
  42. public async Task GeometryClip()
  43. {
  44. Decorator target = new Decorator
  45. {
  46. Padding = new Thickness(8),
  47. Width = 200,
  48. Height = 200,
  49. Child = new CustomRenderer((control, context) =>
  50. {
  51. var clip = new EllipseGeometry(new Rect(control.Bounds.Size));
  52. context.FillRectangle(
  53. Brushes.Red,
  54. new Rect(control.Bounds.Size),
  55. 4);
  56. using (context.PushGeometryClip(clip))
  57. {
  58. context.FillRectangle(
  59. Brushes.Blue,
  60. new Rect(control.Bounds.Size),
  61. 4);
  62. }
  63. }),
  64. };
  65. await RenderToFile(target);
  66. CompareImages();
  67. }
  68. [Fact]
  69. public async Task GeometryClip_With_Transform()
  70. {
  71. var target = new Border
  72. {
  73. Background = Brushes.White,
  74. Width = 200,
  75. Height = 200,
  76. Child = new CustomRenderer((control, context) =>
  77. {
  78. using (var transform = context.PushTransform(Matrix.CreateTranslation(100, 100)))
  79. using (var clip = context.PushClip(new Rect(0, 0, 100, 100)))
  80. {
  81. context.FillRectangle(Brushes.Blue, new Rect(0, 0, 200, 200));
  82. }
  83. context.FillRectangle(Brushes.Red, new Rect(0, 0, 100, 100));
  84. }),
  85. };
  86. await RenderToFile(target);
  87. CompareImages();
  88. }
  89. [Fact]
  90. public async Task Clip_With_Transform()
  91. {
  92. var target = new Border
  93. {
  94. Background = Brushes.White,
  95. Width = 200,
  96. Height = 200,
  97. Child = new CustomRenderer((control, context) =>
  98. {
  99. using (var transform = context.PushTransform(Matrix.CreateTranslation(100, 100)))
  100. using (var clip = context.PushClip(new Rect(0, 0, 100, 100)))
  101. {
  102. context.FillRectangle(Brushes.Blue, new Rect(0, 0, 200, 200));
  103. }
  104. context.FillRectangle(Brushes.Red, new Rect(0, 0, 100, 100));
  105. }),
  106. };
  107. await RenderToFile(target);
  108. CompareImages();
  109. }
  110. [Fact]
  111. public async Task Opacity()
  112. {
  113. Decorator target = new Decorator
  114. {
  115. Padding = new Thickness(8),
  116. Width = 200,
  117. Height = 200,
  118. Child = new CustomRenderer((control, context) =>
  119. {
  120. context.FillRectangle(
  121. Brushes.Red,
  122. new Rect(control.Bounds.Size),
  123. 4);
  124. using (context.PushOpacity(0.5))
  125. {
  126. context.FillRectangle(
  127. Brushes.Blue,
  128. new Rect(control.Bounds.Size).Deflate(10),
  129. 4);
  130. }
  131. }),
  132. };
  133. await RenderToFile(target);
  134. CompareImages();
  135. }
  136. [Fact]
  137. public async Task OpacityMask()
  138. {
  139. Decorator target = new Decorator
  140. {
  141. Padding = new Thickness(8),
  142. Width = 200,
  143. Height = 200,
  144. Child = new CustomRenderer((control, context) =>
  145. {
  146. var mask = new LinearGradientBrush
  147. {
  148. StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
  149. EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative),
  150. GradientStops =
  151. {
  152. new GradientStop(Color.FromUInt32(0xffffffff), 0),
  153. new GradientStop(Color.FromUInt32(0x00ffffff), 1)
  154. },
  155. };
  156. context.FillRectangle(
  157. Brushes.Red,
  158. new Rect(control.Bounds.Size),
  159. 4);
  160. using (context.PushOpacityMask(mask, new Rect(control.Bounds.Size)))
  161. {
  162. context.FillRectangle(
  163. Brushes.Blue,
  164. new Rect(control.Bounds.Size).Deflate(10),
  165. 4);
  166. }
  167. }),
  168. };
  169. await RenderToFile(target);
  170. CompareImages();
  171. }
  172. class CustomRenderer : Control
  173. {
  174. private Action<CustomRenderer, DrawingContext> _render;
  175. public CustomRenderer(Action<CustomRenderer, DrawingContext> render) => _render = render;
  176. public override void Render(DrawingContext context) => _render(this, context);
  177. }
  178. }
  179. }