CustomRenderTests.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Opacity()
  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. context.FillRectangle(
  60. Brushes.Red,
  61. new Rect(control.Bounds.Size),
  62. 4);
  63. using (context.PushOpacity(0.5))
  64. {
  65. context.FillRectangle(
  66. Brushes.Blue,
  67. new Rect(control.Bounds.Size).Deflate(20),
  68. 4);
  69. }
  70. }),
  71. };
  72. await RenderToFile(target);
  73. CompareImages();
  74. }
  75. class CustomRenderer : Control
  76. {
  77. private Action<CustomRenderer, DrawingContext> _render;
  78. public CustomRenderer(Action<CustomRenderer, DrawingContext> render) => _render = render;
  79. public override void Render(DrawingContext context) => _render(this, context);
  80. }
  81. }
  82. }