LinearGradientBrushTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Avalonia.Controls;
  2. using Avalonia.Media;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. #if AVALONIA_SKIA
  10. namespace Avalonia.Skia.RenderTests
  11. #else
  12. namespace Avalonia.Direct2D1.RenderTests.Media
  13. #endif
  14. {
  15. public class LinearGradientBrushTests : TestBase
  16. {
  17. public LinearGradientBrushTests() : base(@"Media\LinearGradientBrush")
  18. {
  19. }
  20. [Fact]
  21. public async Task LinearGradientBrush_RedBlue_Horizontal_Fill()
  22. {
  23. Decorator target = new Decorator
  24. {
  25. Padding = new Thickness(8),
  26. Width = 200,
  27. Height = 200,
  28. Child = new Border
  29. {
  30. Background = new LinearGradientBrush
  31. {
  32. StartPoint = new RelativePoint(0, 0.5, RelativeUnit.Relative),
  33. EndPoint = new RelativePoint(1, 0.5, RelativeUnit.Relative),
  34. GradientStops =
  35. {
  36. new GradientStop { Color = Colors.Red, Offset = 0 },
  37. new GradientStop { Color = Colors.Blue, Offset = 1 }
  38. }
  39. }
  40. }
  41. };
  42. await RenderToFile(target);
  43. CompareImages();
  44. }
  45. [Fact]
  46. public async Task LinearGradientBrush_RedBlue_Vertical_Fill()
  47. {
  48. Decorator target = new Decorator
  49. {
  50. Padding = new Thickness(8),
  51. Width = 200,
  52. Height = 200,
  53. Child = new Border
  54. {
  55. Background = new LinearGradientBrush
  56. {
  57. StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Relative),
  58. EndPoint = new RelativePoint(0.5, 1, RelativeUnit.Relative),
  59. GradientStops =
  60. {
  61. new GradientStop { Color = Colors.Red, Offset = 0 },
  62. new GradientStop { Color = Colors.Blue, Offset = 1 }
  63. }
  64. }
  65. }
  66. };
  67. await RenderToFile(target);
  68. CompareImages();
  69. }
  70. [Fact]
  71. public async Task LinearGradientBrush_DrawingContext()
  72. {
  73. var brush = new LinearGradientBrush
  74. {
  75. StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative),
  76. EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative),
  77. GradientStops =
  78. {
  79. new GradientStop { Color = Colors.Red, Offset = 0 },
  80. new GradientStop { Color = Colors.Blue, Offset = 1 }
  81. }
  82. };
  83. Decorator target = new Decorator
  84. {
  85. Width = 200,
  86. Height = 200,
  87. Child = new DrawnControl(c =>
  88. {
  89. c.DrawRectangle(brush, null, new Rect(0, 0, 100, 100));
  90. using (c.PushTransform(Matrix.CreateTranslation(100, 100)))
  91. c.DrawRectangle(brush, null, new Rect(0, 0, 100, 100));
  92. }),
  93. };
  94. await RenderToFile(target);
  95. CompareImages();
  96. }
  97. private class DrawnControl : Control
  98. {
  99. private readonly Action<DrawingContext> _render;
  100. public DrawnControl(Action<DrawingContext> render) => _render = render;
  101. public override void Render(DrawingContext context) => _render(context);
  102. }
  103. }
  104. }