Browse Source

Fix all failing tests

af/merge-core
James Jackson-South 8 years ago
parent
commit
528edf0ba2
  1. 65
      tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs
  2. 260
      tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs
  3. 32
      tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs
  4. 13
      tests/ImageSharp.Tests/Image/ImageLoadTests.cs
  5. 23
      tests/ImageSharp.Tests/Image/ImageSaveTests.cs
  6. 28
      tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs

65
tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs

@ -1,16 +1,14 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Pens;
using SixLabors.ImageSharp.Drawing.Processors;
using SixLabors.ImageSharp.PixelFormats;
using Moq;
using Xunit;
using SixLabors.ImageSharp.Drawing.Brushes;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Tests.Drawing
{
@ -25,18 +23,18 @@ namespace SixLabors.ImageSharp.Tests.Drawing
[InlineData(false, 16, 4)] // we always do 4 sub=pixels when antialising is off.
public void MinimumAntialiasSubpixelDepth(bool antialias, int antialiasSubpixelDepth, int expectedAntialiasSubpixelDepth)
{
SixLabors.Primitives.Rectangle bounds = new SixLabors.Primitives.Rectangle(0, 0, 1, 1);
var bounds = new SixLabors.Primitives.Rectangle(0, 0, 1, 1);
Mock<IBrush<Rgba32>> brush = new Mock<IBrush<Rgba32>>();
Mock<Region> region = new Mock<Region>();
var brush = new Mock<IBrush<Rgba32>>();
var region = new Mock<Region>();
region.Setup(x => x.Bounds).Returns(bounds);
GraphicsOptions options = new GraphicsOptions(antialias)
var options = new GraphicsOptions(antialias)
{
AntialiasSubpixelDepth = 1
};
FillRegionProcessor<Rgba32> processor = new FillRegionProcessor<Rgba32>(brush.Object, region.Object, options);
Image<Rgba32> img = new Image<Rgba32>(1, 1);
var processor = new FillRegionProcessor<Rgba32>(brush.Object, region.Object, options);
var img = new Image<Rgba32>(1, 1);
processor.Apply(img, bounds);
region.Verify(x => x.Scan(It.IsAny<float>(), It.IsAny<float[]>(), It.IsAny<int>()), Times.Exactly(4));
@ -45,31 +43,11 @@ namespace SixLabors.ImageSharp.Tests.Drawing
[Fact]
public void FillOffCanvas()
{
SixLabors.Primitives.Rectangle bounds = new SixLabors.Primitives.Rectangle(-100, -10, 10, 10);
Mock<IBrush<Rgba32>> brush = new Mock<IBrush<Rgba32>>();
Mock<Region> region = new Mock<Region>();
region.Setup(x => x.Bounds).Returns(bounds);
region.Setup(x => x.MaxIntersections).Returns(10);
region.Setup(x => x.Scan(It.IsAny<float>(), It.IsAny<float[]>(), It.IsAny<int>()))
.Returns<float, Span<float>>((y, span) =>
{
if (y < 5)
{
span[0] = -10f;
span[1] = 100f;
return 2;
}
return 0;
});
GraphicsOptions options = new GraphicsOptions(true)
{
};
FillRegionProcessor<Rgba32> processor = new FillRegionProcessor<Rgba32>(brush.Object, region.Object, options);
Image<Rgba32> img = new Image<Rgba32>(10, 10);
var bounds = new Rectangle(-100, -10, 10, 10);
var brush = new Mock<IBrush<Rgba32>>();
var options = new GraphicsOptions(true);
var processor = new FillRegionProcessor<Rgba32>(brush.Object, new MockRegion(), options);
var img = new Image<Rgba32>(10, 10);
processor.Apply(img, bounds);
}
@ -85,5 +63,24 @@ namespace SixLabors.ImageSharp.Tests.Drawing
}));
}
}
// Mocking the region throws an error in netcore2.0
private class MockRegion : Region
{
public override Rectangle Bounds => new Rectangle(-100, -10, 10, 10);
public override int MaxIntersections => 10;
public override int Scan(float y, float[] buffer, int offset)
{
if (y < 5)
{
buffer[0] = -10f;
buffer[1] = 100f;
return 2;
}
return 0;
}
}
}
}

260
tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs

@ -39,10 +39,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
[MemberData(nameof(CommonConversionData))]
public void ConvertFromYCbCrBasic(int inputBufferLength, int resultBufferLength, int seed)
{
ValidateConversion(new JpegColorConverter.FromYCbCrBasic(), 3, inputBufferLength, resultBufferLength, seed, ValidateYCbCr);
ValidateRgbToYCbCrConversion(
new JpegColorConverter.FromYCbCrBasic(),
3,
inputBufferLength,
resultBufferLength,
seed);
}
private static void ValidateYCbCr(JpegColorConverter.ComponentValues values, Span<Vector4> result, int i)
private static void ValidateYCbCr(JpegColorConverter.ComponentValues values, Vector4[] result, int i)
{
float y = values.Component0[i];
float cb = values.Component1[i];
@ -63,20 +68,27 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
[InlineData(8, 3)]
public void FromYCbCrSimd_ConvertCore(int size, int seed)
{
ValidateConversion(JpegColorConverter.FromYCbCrSimd.ConvertCore, 3, size, size, seed, ValidateYCbCr);
JpegColorConverter.ComponentValues values = CreateRandomValues(3, size, seed);
Vector4[] result = new Vector4[size];
JpegColorConverter.FromYCbCrSimd.ConvertCore(values, result);
for (int i = 0; i < size; i++)
{
ValidateYCbCr(values, result, i);
}
}
[Theory]
[MemberData(nameof(CommonConversionData))]
public void FromYCbCrSimd(int inputBufferLength, int resultBufferLength, int seed)
{
ValidateConversion(
ValidateRgbToYCbCrConversion(
new JpegColorConverter.FromYCbCrSimd(),
3,
inputBufferLength,
resultBufferLength,
seed,
ValidateYCbCr);
seed);
}
[Theory]
@ -91,13 +103,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
//JpegColorConverter.FromYCbCrSimdAvx2.LogPlz = s => this.Output.WriteLine(s);
ValidateConversion(
ValidateRgbToYCbCrConversion(
new JpegColorConverter.FromYCbCrSimdAvx2(),
3,
inputBufferLength,
resultBufferLength,
seed,
ValidateYCbCr);
seed);
}
@ -105,10 +116,15 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
[MemberData(nameof(CommonConversionData))]
public void ConvertFromYCbCr_WithDefaultConverter(int inputBufferLength, int resultBufferLength, int seed)
{
ValidateConversion(JpegColorSpace.YCbCr, 3, inputBufferLength, resultBufferLength, seed, ValidateYCbCr);
ValidateConversion(
JpegColorSpace.YCbCr,
3,
inputBufferLength,
resultBufferLength,
seed);
}
// Becnhmark, for local execution only
// Benchmark, for local execution only
//[Theory]
//[InlineData(false)]
//[InlineData(true)]
@ -120,11 +136,11 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
JpegColorConverter.ComponentValues values = CreateRandomValues(3, count, 1);
Vector4[] result = new Vector4[count];
JpegColorConverter converter = simd ? (JpegColorConverter)new JpegColorConverter.FromYCbCrSimd() : new JpegColorConverter.FromYCbCrBasic();
JpegColorConverter converter = simd ? (JpegColorConverter)new JpegColorConverter.FromYCbCrSimd() : new JpegColorConverter.FromYCbCrBasic();
// Warm up:
converter.ConvertToRGBA(values, result);
using (new MeasureGuard(this.Output, $"{converter.GetType().Name} x {times}"))
{
for (int i = 0; i < times; i++)
@ -141,79 +157,79 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var v = new Vector4(0, 0, 0, 1F);
var scale = new Vector4(1 / 255F, 1 / 255F, 1 / 255F, 1F);
ValidateConversion(
JpegColorSpace.Cmyk,
4,
inputBufferLength,
resultBufferLength,
seed,
(values, result, i) =>
{
float c = values.Component0[i];
float m = values.Component1[i];
float y = values.Component2[i];
float k = values.Component3[i] / 255F;
v.X = c * k;
v.Y = m * k;
v.Z = y * k;
v.W = 1F;
v *= scale;
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(v.X, v.Y, v.Z);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
});
var converter = JpegColorConverter.GetConverter(JpegColorSpace.Cmyk);
JpegColorConverter.ComponentValues values = CreateRandomValues(4, inputBufferLength, seed);
Vector4[] result = new Vector4[resultBufferLength];
converter.ConvertToRGBA(values, result);
for (int i = 0; i < resultBufferLength; i++)
{
float c = values.Component0[i];
float m = values.Component1[i];
float y = values.Component2[i];
float k = values.Component3[i] / 255F;
v.X = c * k;
v.Y = m * k;
v.Z = y * k;
v.W = 1F;
v *= scale;
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(v.X, v.Y, v.Z);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
}
}
[Theory]
[MemberData(nameof(CommonConversionData))]
public void ConvertFromGrayScale(int inputBufferLength, int resultBufferLength, int seed)
{
ValidateConversion(
JpegColorSpace.GrayScale,
1,
inputBufferLength,
resultBufferLength,
seed,
(values, result, i) =>
{
float y = values.Component0[i];
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(y / 255F, y / 255F, y / 255F);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
});
var converter = JpegColorConverter.GetConverter(JpegColorSpace.GrayScale);
JpegColorConverter.ComponentValues values = CreateRandomValues(1, inputBufferLength, seed);
Vector4[] result = new Vector4[resultBufferLength];
converter.ConvertToRGBA(values, result);
for (int i = 0; i < resultBufferLength; i++)
{
float y = values.Component0[i];
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(y / 255F, y / 255F, y / 255F);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
}
}
[Theory]
[MemberData(nameof(CommonConversionData))]
public void ConvertFromRgb(int inputBufferLength, int resultBufferLength, int seed)
{
ValidateConversion(
JpegColorSpace.RGB,
3,
inputBufferLength,
resultBufferLength,
seed,
(values, result, i) =>
{
float r = values.Component0[i];
float g = values.Component1[i];
float b = values.Component2[i];
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(r / 255F, g / 255F, b / 255F);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
});
var converter = JpegColorConverter.GetConverter(JpegColorSpace.RGB);
JpegColorConverter.ComponentValues values = CreateRandomValues(3, inputBufferLength, seed);
Vector4[] result = new Vector4[resultBufferLength];
converter.ConvertToRGBA(values, result);
for (int i = 0; i < resultBufferLength; i++)
{
float r = values.Component0[i];
float g = values.Component1[i];
float b = values.Component2[i];
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(r / 255F, g / 255F, b / 255F);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
}
}
[Theory]
@ -223,35 +239,35 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
var v = new Vector4(0, 0, 0, 1F);
var scale = new Vector4(1 / 255F, 1 / 255F, 1 / 255F, 1F);
ValidateConversion(
JpegColorSpace.Ycck,
4,
inputBufferLength,
resultBufferLength,
seed,
(values, result, i) =>
{
float y = values.Component0[i];
float cb = values.Component1[i] - 128F;
float cr = values.Component2[i] - 128F;
float k = values.Component3[i] / 255F;
v.X = (255F - (float)Math.Round(y + (1.402F * cr), MidpointRounding.AwayFromZero)) * k;
v.Y = (255F - (float)Math.Round(
y - (0.344136F * cb) - (0.714136F * cr),
MidpointRounding.AwayFromZero)) * k;
v.Z = (255F - (float)Math.Round(y + (1.772F * cb), MidpointRounding.AwayFromZero)) * k;
v.W = 1F;
v *= scale;
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(v.X, v.Y, v.Z);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
});
var converter = JpegColorConverter.GetConverter(JpegColorSpace.Ycck);
JpegColorConverter.ComponentValues values = CreateRandomValues(4, inputBufferLength, seed);
Vector4[] result = new Vector4[resultBufferLength];
converter.ConvertToRGBA(values, result);
for (int i = 0; i < resultBufferLength; i++)
{
float y = values.Component0[i];
float cb = values.Component1[i] - 128F;
float cr = values.Component2[i] - 128F;
float k = values.Component3[i] / 255F;
v.X = (255F - (float)Math.Round(y + (1.402F * cr), MidpointRounding.AwayFromZero)) * k;
v.Y = (255F - (float)Math.Round(
y - (0.344136F * cb) - (0.714136F * cr),
MidpointRounding.AwayFromZero)) * k;
v.Z = (255F - (float)Math.Round(y + (1.772F * cb), MidpointRounding.AwayFromZero)) * k;
v.W = 1F;
v *= scale;
Vector4 rgba = result[i];
var actual = new Rgb(rgba.X, rgba.Y, rgba.Z);
var expected = new Rgb(v.X, v.Y, v.Z);
Assert.True(actual.AlmostEquals(expected, Precision));
Assert.Equal(1, rgba.W);
}
}
private static JpegColorConverter.ComponentValues CreateRandomValues(
@ -269,7 +285,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
for (int j = 0; j < inputBufferLength; j++)
{
values[j] = (float)rnd.NextDouble() * (maxVal-minVal)+minVal;
values[j] = (float)rnd.NextDouble() * (maxVal - minVal) + minVal;
}
// no need to dispose when buffer is not array owner
@ -283,51 +299,31 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
int componentCount,
int inputBufferLength,
int resultBufferLength,
int seed,
Action<JpegColorConverter.ComponentValues, Span<Vector4>, int> validatePixelValue)
int seed)
{
ValidateConversion(
ValidateRgbToYCbCrConversion(
JpegColorConverter.GetConverter(colorSpace),
componentCount,
inputBufferLength,
resultBufferLength,
seed,
validatePixelValue);
seed);
}
private static void ValidateConversion(
private static void ValidateRgbToYCbCrConversion(
JpegColorConverter converter,
int componentCount,
int inputBufferLength,
int resultBufferLength,
int seed,
Action<JpegColorConverter.ComponentValues, Span<Vector4>, int> validatePixelValue)
{
ValidateConversion(
converter.ConvertToRGBA,
componentCount,
inputBufferLength,
resultBufferLength,
seed,
validatePixelValue);
}
private static void ValidateConversion(
Action<JpegColorConverter.ComponentValues, Span<Vector4>> doConvert,
int componentCount,
int inputBufferLength,
int resultBufferLength,
int seed,
Action<JpegColorConverter.ComponentValues, Span<Vector4>, int> validatePixelValue)
int seed)
{
JpegColorConverter.ComponentValues values = CreateRandomValues(componentCount, inputBufferLength, seed);
Vector4[] result = new Vector4[resultBufferLength];
doConvert(values, result);
converter.ConvertToRGBA(values, result);
for (int i = 0; i < resultBufferLength; i++)
{
validatePixelValue(values, result, i);
ValidateYCbCr(values, result, i);
}
}
}

32
tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs

@ -5,7 +5,6 @@ using System;
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;
using Moq;
using Xunit;
@ -18,10 +17,10 @@ namespace SixLabors.ImageSharp.Tests
{
private readonly Mock<IFileSystem> fileSystem;
private readonly string FilePath;
private readonly Mock<IImageFormatDetector> localMimeTypeDetector;
private readonly IImageFormatDetector localMimeTypeDetector;
private readonly Mock<IImageFormat> localImageFormatMock;
public IImageFormat localImageFormat => localImageFormatMock.Object;
public IImageFormat localImageFormat => this.localImageFormatMock.Object;
public Configuration LocalConfiguration { get; private set; }
public byte[] Marker { get; private set; }
public MemoryStream DataStream { get; private set; }
@ -32,9 +31,7 @@ namespace SixLabors.ImageSharp.Tests
{
this.localImageFormatMock = new Mock<IImageFormat>();
this.localMimeTypeDetector = new Mock<IImageFormatDetector>();
this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1);
this.localMimeTypeDetector.Setup(x => x.DetectFormat(It.IsAny<ReadOnlySpan<byte>>())).Returns(localImageFormatMock.Object);
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object);
this.fileSystem = new Mock<IFileSystem>();
@ -42,7 +39,8 @@ namespace SixLabors.ImageSharp.Tests
{
FileSystem = this.fileSystem.Object
};
this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector.Object);
this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector);
TestFormat.RegisterGlobalTestFormat();
this.Marker = Guid.NewGuid().ToByteArray();
@ -58,49 +56,49 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void DiscoverImageFormatByteArray()
{
var type = Image.DetectFormat(DataStream.ToArray());
IImageFormat type = Image.DetectFormat(this.DataStream.ToArray());
Assert.Equal(TestFormat.GlobalTestFormat, type);
}
[Fact]
public void DiscoverImageFormatByteArray_WithConfig()
{
var type = Image.DetectFormat(this.LocalConfiguration, DataStream.ToArray());
Assert.Equal(localImageFormat, type);
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream.ToArray());
Assert.Equal(this.localImageFormat, type);
}
[Fact]
public void DiscoverImageFormatFile()
{
var type = Image.DetectFormat(this.FilePath);
IImageFormat type = Image.DetectFormat(this.FilePath);
Assert.Equal(TestFormat.GlobalTestFormat, type);
}
[Fact]
public void DiscoverImageFormatFilePath_WithConfig()
{
var type = Image.DetectFormat(this.LocalConfiguration, FilePath);
Assert.Equal(localImageFormat, type);
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.FilePath);
Assert.Equal(this.localImageFormat, type);
}
[Fact]
public void DiscoverImageFormatStream()
{
var type = Image.DetectFormat(this.DataStream);
IImageFormat type = Image.DetectFormat(this.DataStream);
Assert.Equal(TestFormat.GlobalTestFormat, type);
}
[Fact]
public void DiscoverImageFormatFileStream_WithConfig()
{
var type = Image.DetectFormat(this.LocalConfiguration, DataStream);
Assert.Equal(localImageFormat, type);
IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream);
Assert.Equal(this.localImageFormat, type);
}
[Fact]
public void DiscoverImageFormatNoDetectorsRegisterdShouldReturnNull()
{
var type = Image.DetectFormat(new Configuration(), DataStream);
IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream);
Assert.Null(type);
}
}

13
tests/ImageSharp.Tests/Image/ImageLoadTests.cs

@ -14,13 +14,13 @@ namespace SixLabors.ImageSharp.Tests
/// <summary>
/// Tests the <see cref="Image"/> class.
/// </summary>
public class ImageLoadTests : IDisposable
public partial class ImageLoadTests : IDisposable
{
private readonly Mock<IFileSystem> fileSystem;
private Image<Rgba32> returnImage;
private Mock<IImageDecoder> localDecoder;
private readonly string FilePath;
private readonly Mock<IImageFormatDetector> localMimeTypeDetector;
private readonly IImageFormatDetector localMimeTypeDetector;
private readonly Mock<IImageFormat> localImageFormatMock;
public Configuration LocalConfiguration { get; private set; }
@ -35,10 +35,7 @@ namespace SixLabors.ImageSharp.Tests
this.localImageFormatMock = new Mock<IImageFormat>();
this.localDecoder = new Mock<IImageDecoder>();
this.localMimeTypeDetector = new Mock<IImageFormatDetector>();
this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1);
this.localMimeTypeDetector.Setup(x => x.DetectFormat(It.IsAny<ReadOnlySpan<byte>>())).Returns(localImageFormatMock.Object);
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object);
this.localDecoder.Setup(x => x.Decode<Rgba32>(It.IsAny<Configuration>(), It.IsAny<Stream>()))
.Callback<Configuration, Stream>((c, s) =>
@ -57,8 +54,8 @@ namespace SixLabors.ImageSharp.Tests
{
FileSystem = this.fileSystem.Object
};
this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector.Object);
this.LocalConfiguration.SetDecoder(localImageFormatMock.Object, this.localDecoder.Object);
this.LocalConfiguration.AddImageFormatDetector(this.localMimeTypeDetector);
this.LocalConfiguration.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object);
TestFormat.RegisterGlobalTestFormat();
this.Marker = Guid.NewGuid().ToByteArray();

23
tests/ImageSharp.Tests/Image/ImageSaveTests.cs

@ -24,17 +24,14 @@ namespace SixLabors.ImageSharp.Tests
private readonly Mock<IFileSystem> fileSystem;
private readonly Mock<IImageEncoder> encoder;
private readonly Mock<IImageEncoder> encoderNotInFormat;
private Mock<IImageFormatDetector> localMimeTypeDetector;
private IImageFormatDetector localMimeTypeDetector;
private Mock<IImageFormat> localImageFormat;
public ImageSaveTests()
{
this.localImageFormat = new Mock<IImageFormat>();
this.localImageFormat.Setup(x => x.FileExtensions).Returns(new[] { "png" });
this.localMimeTypeDetector = new Mock<IImageFormatDetector>();
this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1);
this.localMimeTypeDetector.Setup(x => x.DetectFormat(It.IsAny<Span<byte>>())).Returns(localImageFormat.Object);
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormat.Object);
this.encoder = new Mock<IImageEncoder>();
@ -45,8 +42,8 @@ namespace SixLabors.ImageSharp.Tests
{
FileSystem = this.fileSystem.Object
};
config.AddImageFormatDetector(this.localMimeTypeDetector.Object);
config.SetEncoder(localImageFormat.Object, this.encoder.Object);
config.AddImageFormatDetector(this.localMimeTypeDetector);
config.SetEncoder(this.localImageFormat.Object, this.encoder.Object);
this.Image = new Image<Rgba32>(config, 1, 1);
}
@ -57,7 +54,7 @@ namespace SixLabors.ImageSharp.Tests
{
using (Image<TPixel> image = provider.GetImage())
{
TPixel[] buffer = new TPixel[image.Width*image.Height];
TPixel[] buffer = new TPixel[image.Width * image.Height];
image.SavePixelData(buffer);
image.ComparePixelBufferTo(buffer);
@ -73,14 +70,14 @@ namespace SixLabors.ImageSharp.Tests
{
using (Image<TPixel> image = provider.GetImage())
{
byte[] buffer = new byte[image.Width*image.Height*Unsafe.SizeOf<TPixel>()];
byte[] buffer = new byte[image.Width * image.Height * Unsafe.SizeOf<TPixel>()];
image.SavePixelData(buffer);
image.ComparePixelBufferTo(buffer.AsSpan().NonPortableCast<byte, TPixel>());
}
}
[Fact]
public void SavePixelData_Rgba32_WhenBufferIsTooSmall_Throws()
{
@ -91,7 +88,7 @@ namespace SixLabors.ImageSharp.Tests
img[0, 1] = Rgba32.Red;
img[1, 1] = Rgba32.Blue;
var buffer = new byte[2 * 2]; // width * height * bytes per pixel
byte[] buffer = new byte[2 * 2]; // width * height * bytes per pixel
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
@ -125,7 +122,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void ToBase64String()
{
var str = this.Image.ToBase64String(localImageFormat.Object);
string str = this.Image.ToBase64String(this.localImageFormat.Object);
this.encoder.Verify(x => x.Encode<Rgba32>(this.Image, It.IsAny<Stream>()));
}
@ -134,7 +131,7 @@ namespace SixLabors.ImageSharp.Tests
public void SaveStreamWithMime()
{
Stream stream = new MemoryStream();
this.Image.Save(stream, localImageFormat.Object);
this.Image.Save(stream, this.localImageFormat.Object);
this.encoder.Verify(x => x.Encode<Rgba32>(this.Image, stream));
}

28
tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs

@ -0,0 +1,28 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.Tests
{
/// <summary>
/// You can't mock the "DetectFormat" method due to the ReadOnlySpan{byte} parameter.
/// </summary>
public class MockImageFormatDetector : IImageFormatDetector
{
private IImageFormat localImageFormatMock;
public MockImageFormatDetector(IImageFormat imageFormat)
{
this.localImageFormatMock = imageFormat;
}
public int HeaderSize => 1;
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
{
return this.localImageFormatMock;
}
}
}
Loading…
Cancel
Save