CustomRenderTests.cs 5.0 KB

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