Skip to content

Commit 18cc05f

Browse files
committed
SixLabors#542: refactor tests to follow the recommended pattern for drawing tests, as @antonfirsov suggested.
1 parent 2d2b3e1 commit 18cc05f

File tree

3 files changed

+329
-269
lines changed

3 files changed

+329
-269
lines changed

tests/ImageSharp.Tests/Drawing/FillEllipticGradientBrushTest.cs

Lines changed: 89 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,113 +12,135 @@
1212

1313
namespace SixLabors.ImageSharp.Tests.Drawing
1414
{
15+
[GroupOutput("Drawing/GradientBrushes")]
1516
public class FillEllipticGradientBrushTests : FileTestBase
1617
{
17-
[Fact]
18-
public void EllipticGradientBrushWithEqualColorsAndReturnsUnicolorImage()
18+
[Theory]
19+
[WithBlankImages(10, 10, PixelTypes.Rgba32)]
20+
public void EllipticGradientBrushWithEqualColorsAndReturnsUnicolorImage<TPixel>(
21+
TestImageProvider<TPixel> provider)
22+
where TPixel : struct, IPixel<TPixel>
1923
{
20-
string path = TestEnvironment.CreateOutputDirectory("Fill", "EllipticGradientBrush");
21-
using (var image = new Image<Rgba32>(10, 10))
24+
TPixel red = NamedColors<TPixel>.Red;
25+
26+
using (Image<TPixel> image = provider.GetImage())
2227
{
23-
EllipticGradientBrush<Rgba32> unicolorLinearGradientBrush =
24-
new EllipticGradientBrush<Rgba32>(
28+
EllipticGradientBrush<TPixel> unicolorLinearGradientBrush =
29+
new EllipticGradientBrush<TPixel>(
2530
new SixLabors.Primitives.Point(0, 0),
2631
new SixLabors.Primitives.Point(10, 0),
2732
1.0f,
2833
GradientRepetitionMode.None,
29-
new ColorStop<Rgba32>(0, Rgba32.Red),
30-
new ColorStop<Rgba32>(1, Rgba32.Red));
34+
new ColorStop<TPixel>(0, red),
35+
new ColorStop<TPixel>(1, red));
3136

3237
image.Mutate(x => x.Fill(unicolorLinearGradientBrush));
33-
image.Save($"{path}/UnicolorCircleGradient.png");
38+
image.DebugSave(provider);
3439

35-
using (PixelAccessor<Rgba32> sourcePixels = image.Lock())
40+
using (PixelAccessor<TPixel> sourcePixels = image.Lock())
3641
{
37-
Assert.Equal(Rgba32.Red, sourcePixels[0, 0]);
38-
Assert.Equal(Rgba32.Red, sourcePixels[9, 9]);
39-
Assert.Equal(Rgba32.Red, sourcePixels[5, 5]);
40-
Assert.Equal(Rgba32.Red, sourcePixels[3, 8]);
42+
Assert.Equal(red, sourcePixels[0, 0]);
43+
Assert.Equal(red, sourcePixels[9, 9]);
44+
Assert.Equal(red, sourcePixels[5, 5]);
45+
Assert.Equal(red, sourcePixels[3, 8]);
4146
}
47+
48+
image.CompareToReferenceOutput(provider);
4249
}
4350
}
4451

4552
[Theory]
46-
[InlineData(0.1)]
47-
[InlineData(0.4)]
48-
[InlineData(0.8)]
49-
[InlineData(1.0)]
50-
[InlineData(1.2)]
51-
[InlineData(1.6)]
52-
[InlineData(2.0)]
53-
public void EllipticGradientBrushProducesAxisParallelEllipsesWithDifferentRatio(
53+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.1)]
54+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.4)]
55+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.8)]
56+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.0)]
57+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.2)]
58+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.6)]
59+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 2.0)]
60+
public void EllipticGradientBrushProducesAxisParallelEllipsesWithDifferentRatio<TPixel>(
61+
TestImageProvider<TPixel> provider,
5462
float ratio)
63+
where TPixel : struct, IPixel<TPixel>
5564
{
56-
string path = TestEnvironment.CreateOutputDirectory("Fill", "EllipticGradientBrush");
57-
using (var image = new Image<Rgba32>(1000, 1000))
65+
TPixel yellow = NamedColors<TPixel>.Yellow;
66+
TPixel red = NamedColors<TPixel>.Red;
67+
TPixel black = NamedColors<TPixel>.Black;
68+
69+
using (var image = provider.GetImage())
5870
{
59-
EllipticGradientBrush<Rgba32> unicolorLinearGradientBrush =
60-
new EllipticGradientBrush<Rgba32>(
61-
new SixLabors.Primitives.Point(500, 500),
62-
new SixLabors.Primitives.Point(500, 750),
71+
EllipticGradientBrush<TPixel> unicolorLinearGradientBrush =
72+
new EllipticGradientBrush<TPixel>(
73+
new SixLabors.Primitives.Point(image.Width / 2, image.Height / 2),
74+
new SixLabors.Primitives.Point(image.Width / 2, (image.Width * 3) / 2),
6375
ratio,
6476
GradientRepetitionMode.None,
65-
new ColorStop<Rgba32>(0, Rgba32.Yellow),
66-
new ColorStop<Rgba32>(1, Rgba32.Red),
67-
new ColorStop<Rgba32>(1, Rgba32.Black));
77+
new ColorStop<TPixel>(0, yellow),
78+
new ColorStop<TPixel>(1, red),
79+
new ColorStop<TPixel>(1, black));
6880

6981
image.Mutate(x => x.Fill(unicolorLinearGradientBrush));
70-
image.Save($"{path}/Ellipsis{ratio}.png");
82+
image.DebugSave(provider, ratio);
83+
image.CompareToReferenceOutput(provider, ratio);
7184
}
7285
}
7386

7487
[Theory]
75-
[InlineData(0.1, 0)]
76-
[InlineData(0.4, 0)]
77-
[InlineData(0.8, 0)]
78-
[InlineData(1.0, 0)]
79-
80-
[InlineData(0.1, 45)]
81-
[InlineData(0.4, 45)]
82-
[InlineData(0.8, 45)]
83-
[InlineData(1.0, 45)]
84-
85-
[InlineData(0.1, 90)]
86-
[InlineData(0.4, 90)]
87-
[InlineData(0.8, 90)]
88-
[InlineData(1.0, 90)]
89-
90-
[InlineData(0.1, 30)]
91-
[InlineData(0.4, 30)]
92-
[InlineData(0.8, 30)]
93-
[InlineData(1.0, 30)]
94-
public void EllipticGradientBrushProducesRotatedEllipsesWithDifferentRatio(
88+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.1, 0)]
89+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.4, 0)]
90+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.8, 0)]
91+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.0, 0)]
92+
93+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.1, 45)]
94+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.4, 45)]
95+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.8, 45)]
96+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.0, 45)]
97+
98+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.1, 90)]
99+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.4, 90)]
100+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.8, 90)]
101+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.0, 90)]
102+
103+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.1, 30)]
104+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.4, 30)]
105+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 0.8, 30)]
106+
[WithBlankImages(1000, 1000, PixelTypes.Rgba32, 1.0, 30)]
107+
public void EllipticGradientBrushProducesRotatedEllipsesWithDifferentRatio<TPixel>(
108+
TestImageProvider<TPixel> provider,
95109
float ratio,
96110
float rotationInDegree)
111+
where TPixel: struct, IPixel<TPixel>
97112
{
98-
var center = new SixLabors.Primitives.Point(500, 500);
113+
string variant = $"{ratio}at{rotationInDegree}°";
114+
115+
using (var image = provider.GetImage())
116+
{
117+
TPixel yellow = NamedColors<TPixel>.Yellow;
118+
TPixel red = NamedColors<TPixel>.Red;
119+
TPixel black = NamedColors<TPixel>.Black;
99120

100-
var rotation = (Math.PI * rotationInDegree) / 180.0;
101-
var cos = Math.Cos(rotation);
102-
var sin = Math.Sin(rotation);
121+
var center = new SixLabors.Primitives.Point(image.Width / 2, image.Height / 2);
103122

104-
int axisX = (int)((center.X * cos) - (center.Y * sin));
105-
int axisY = (int)((center.X * sin) + (center.Y * cos));
123+
var rotation = (Math.PI * rotationInDegree) / 180.0;
124+
var cos = Math.Cos(rotation);
125+
var sin = Math.Sin(rotation);
106126

107-
string path = TestEnvironment.CreateOutputDirectory("Fill", "EllipticGradientBrush");
108-
using (var image = new Image<Rgba32>(1000, 1000))
109-
{
110-
EllipticGradientBrush<Rgba32> unicolorLinearGradientBrush =
111-
new EllipticGradientBrush<Rgba32>(
127+
int axisX = (int)((center.X * cos) - (center.Y * sin));
128+
int axisY = (int)((center.X * sin) + (center.Y * cos));
129+
130+
131+
EllipticGradientBrush<TPixel> unicolorLinearGradientBrush =
132+
new EllipticGradientBrush<TPixel>(
112133
center,
113134
new SixLabors.Primitives.Point(axisX, axisY),
114135
ratio,
115136
GradientRepetitionMode.None,
116-
new ColorStop<Rgba32>(0, Rgba32.Yellow),
117-
new ColorStop<Rgba32>(1, Rgba32.Red),
118-
new ColorStop<Rgba32>(1, Rgba32.Black));
137+
new ColorStop<TPixel>(0, yellow),
138+
new ColorStop<TPixel>(1, red),
139+
new ColorStop<TPixel>(1, black));
119140

120141
image.Mutate(x => x.Fill(unicolorLinearGradientBrush));
121-
image.Save($"{path}/Ellipsis{ratio}_rot{rotationInDegree}°.png");
142+
image.DebugSave(provider, variant);
143+
image.CompareToReferenceOutput(provider, variant);
122144
}
123145
}
124146
}

0 commit comments

Comments
 (0)