CustomRenderTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 System;
  4. using System.Threading.Tasks;
  5. using Avalonia.Controls;
  6. using Avalonia.Layout;
  7. using Avalonia.Media;
  8. using Xunit;
  9. #if AVALONIA_SKIA
  10. namespace Avalonia.Skia.RenderTests
  11. #else
  12. namespace Avalonia.Direct2D1.RenderTests.Controls
  13. #endif
  14. {
  15. public class CustomRenderTests : TestBase
  16. {
  17. public CustomRenderTests()
  18. : base(@"Controls\CustomRender")
  19. {
  20. }
  21. [Fact]
  22. public async Task Clip()
  23. {
  24. Decorator target = new Decorator
  25. {
  26. Padding = new Thickness(8),
  27. Width = 200,
  28. Height = 200,
  29. Child = new CustomRenderer((control, context) =>
  30. {
  31. context.FillRectangle(
  32. Brushes.Red,
  33. new Rect(control.Bounds.Size),
  34. 4);
  35. using (context.PushClip(new Rect(control.Bounds.Size).Deflate(20)))
  36. {
  37. context.FillRectangle(
  38. Brushes.Blue,
  39. new Rect(control.Bounds.Size),
  40. 4);
  41. }
  42. }),
  43. };
  44. await RenderToFile(target);
  45. CompareImages();
  46. }
  47. [Fact]
  48. public async Task GeometryClip()
  49. {
  50. Decorator target = new Decorator
  51. {
  52. Padding = new Thickness(8),
  53. Width = 200,
  54. Height = 200,
  55. Child = new CustomRenderer((control, context) =>
  56. {
  57. var clip = new EllipseGeometry(new Rect(control.Bounds.Size));
  58. context.FillRectangle(
  59. Brushes.Red,
  60. new Rect(control.Bounds.Size),
  61. 4);
  62. using (context.PushGeometryClip(clip))
  63. {
  64. context.FillRectangle(
  65. Brushes.Blue,
  66. new Rect(control.Bounds.Size),
  67. 4);
  68. }
  69. }),
  70. };
  71. await RenderToFile(target);
  72. CompareImages();
  73. }
  74. [Fact]
  75. public async Task Opacity()
  76. {
  77. Decorator target = new Decorator
  78. {
  79. Padding = new Thickness(8),
  80. Width = 200,
  81. Height = 200,
  82. Child = new CustomRenderer((control, context) =>
  83. {
  84. context.FillRectangle(
  85. Brushes.Red,
  86. new Rect(control.Bounds.Size),
  87. 4);
  88. using (context.PushOpacity(0.5))
  89. {
  90. context.FillRectangle(
  91. Brushes.Blue,
  92. new Rect(control.Bounds.Size).Deflate(20),
  93. 4);
  94. }
  95. }),
  96. };
  97. await RenderToFile(target);
  98. CompareImages();
  99. }
  100. [Fact]
  101. public async Task OpacityMask()
  102. {
  103. Decorator target = new Decorator
  104. {
  105. Padding = new Thickness(8),
  106. Width = 200,
  107. Height = 200,
  108. Child = new CustomRenderer((control, context) =>
  109. {
  110. var mask = new LinearGradientBrush
  111. {
  112. StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
  113. EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative),
  114. GradientStops = new[]
  115. {
  116. new GradientStop(Color.FromUInt32(0xffffffff), 0),
  117. new GradientStop(Color.FromUInt32(0x00ffffff), 1)
  118. },
  119. };
  120. context.FillRectangle(
  121. Brushes.Red,
  122. new Rect(control.Bounds.Size),
  123. 4);
  124. using (context.PushOpacityMask(mask, new Rect(control.Bounds.Size)))
  125. {
  126. context.FillRectangle(
  127. Brushes.Blue,
  128. new Rect(control.Bounds.Size).Deflate(20),
  129. 4);
  130. }
  131. }),
  132. };
  133. await RenderToFile(target);
  134. CompareImages();
  135. }
  136. class CustomRenderer : Control
  137. {
  138. private Action<CustomRenderer, DrawingContext> _render;
  139. public CustomRenderer(Action<CustomRenderer, DrawingContext> render) => _render = render;
  140. public override void Render(DrawingContext context) => _render(this, context);
  141. }
  142. }
  143. }