Browse Source

TestImageProvider can use custom decoder now

af/merge-core
Anton Firszov 9 years ago
parent
commit
3b8e4b024d
  1. 4
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  2. 2
      tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs
  3. 3
      tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs
  4. 28
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs
  5. 6
      tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs
  6. 36
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

4
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

@ -15,7 +15,7 @@ namespace ImageSharp.Tests
using Xunit; using Xunit;
public class JpegDecoderTests : TestBase public class JpegDecoderTests
{ {
public static string[] BaselineTestJpegs = public static string[] BaselineTestJpegs =
{ {
@ -29,7 +29,7 @@ namespace ImageSharp.Tests
// TODO: We should make this comparer less tolerant ... // TODO: We should make this comparer less tolerant ...
private static readonly ImageComparer VeryTolerantJpegComparer = private static readonly ImageComparer VeryTolerantJpegComparer =
ImageComparer.Tolerant(0.005f, pixelThresholdInPixelByteSum: 4); ImageComparer.Tolerant(0.005f, pixelThresholdInPixelByteSum: 4);
[Theory] [Theory]
[WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32 | PixelTypes.Rgba32 | PixelTypes.Argb32)] [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32 | PixelTypes.Rgba32 | PixelTypes.Argb32)]
public void DecodeBaselineJpeg<TPixel>(TestImageProvider<TPixel> provider) public void DecodeBaselineJpeg<TPixel>(TestImageProvider<TPixel> provider)

2
tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs

@ -10,6 +10,8 @@ namespace ImageSharp.Tests
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
/// <summary> /// <summary>
/// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here!
///
/// Utility class to create specialized subclasses of generic classes (eg. <see cref="Image"/>) /// Utility class to create specialized subclasses of generic classes (eg. <see cref="Image"/>)
/// Used as parameter for <see cref="WithMemberFactoryAttribute"/> -based factory methods /// Used as parameter for <see cref="WithMemberFactoryAttribute"/> -based factory methods
/// </summary> /// </summary>

3
tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs

@ -7,6 +7,9 @@ namespace ImageSharp.Tests
{ {
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
/// <summary>
/// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here!
/// </summary>
public class ImageFactory : GenericFactory<Rgba32> public class ImageFactory : GenericFactory<Rgba32>
{ {
public override Image<Rgba32> CreateImage(byte[] bytes) => Image.Load<Rgba32>(bytes); public override Image<Rgba32> CreateImage(byte[] bytes) => Image.Load<Rgba32>(bytes);

28
tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs

@ -8,6 +8,7 @@ namespace ImageSharp.Tests
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
using Xunit.Abstractions; using Xunit.Abstractions;
@ -19,10 +20,10 @@ namespace ImageSharp.Tests
{ {
// Need PixelTypes in the dictionary key, because result images of TestImageProvider<TPixel>.FileProvider // Need PixelTypes in the dictionary key, because result images of TestImageProvider<TPixel>.FileProvider
// are shared between PixelTypes.Color & PixelTypes.Rgba32 // are shared between PixelTypes.Color & PixelTypes.Rgba32
private class Key : Tuple<PixelTypes, string> private class Key : Tuple<PixelTypes, string, Type>
{ {
public Key(PixelTypes item1, string item2) public Key(PixelTypes pixelType, string filePath, Type customDecoderType = null)
: base(item1, item2) : base(pixelType, filePath, customDecoderType)
{ {
} }
} }
@ -51,10 +52,27 @@ namespace ImageSharp.Tests
fn => fn =>
{ {
TestFile testFile = TestFile.Create(this.FilePath); TestFile testFile = TestFile.Create(this.FilePath);
return this.Factory.CreateImage(testFile.Bytes); return Image.Load<TPixel>(testFile.Bytes);
}); });
return this.Factory.CreateImage(cachedImage); return cachedImage.Clone();
}
public override Image<TPixel> GetImage(IImageDecoder decoder)
{
Guard.NotNull(decoder, nameof(decoder));
Key key = new Key(this.PixelType, this.FilePath, decoder.GetType());
Image<TPixel> cachedImage = cache.GetOrAdd(
key,
fn =>
{
TestFile testFile = TestFile.Create(this.FilePath);
return Image.Load<TPixel>(testFile.Bytes, decoder);
});
return cachedImage.Clone();
} }
public override void Deserialize(IXunitSerializationInfo info) public override void Deserialize(IXunitSerializationInfo info)

6
tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs

@ -8,6 +8,7 @@ namespace ImageSharp.Tests
using System; using System;
using System.Reflection; using System.Reflection;
using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
using Xunit.Abstractions; using Xunit.Abstractions;
@ -82,6 +83,11 @@ namespace ImageSharp.Tests
/// </summary> /// </summary>
public abstract Image<TPixel> GetImage(); public abstract Image<TPixel> GetImage();
public virtual Image<TPixel> GetImage(IImageDecoder decoder)
{
throw new NotSupportedException($"Decoder specific GetImage() is not supported with {this.GetType().Name}!");
}
/// <summary> /// <summary>
/// Returns an <see cref="Image{TPixel}"/> instance to the test case with the necessary traits. /// Returns an <see cref="Image{TPixel}"/> instance to the test case with the necessary traits.
/// </summary> /// </summary>

36
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

@ -3,12 +3,17 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
// ReSharper disable InconsistentNaming
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using System; using System;
using System.IO;
using ImageSharp.Formats;
using ImageSharp.PixelFormats; using ImageSharp.PixelFormats;
using Moq;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
@ -85,6 +90,35 @@ namespace ImageSharp.Tests
this.Output.WriteLine(fn); this.Output.WriteLine(fn);
} }
private class TestDecoder : IImageDecoder
{
public int InvocationCount { get; private set; } = 0;
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>
{
this.InvocationCount++;
return new Image<TPixel>(42, 42);
}
}
[Theory]
[WithFile(TestImages.Bmp.F, PixelTypes.Rgba32)]
public void GetImage_WithCustomDecoder_ShouldUtilizeCache<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
Assert.NotNull(provider.Utility.SourceFileOrDescription);
var decoder = new TestDecoder();
provider.GetImage(decoder);
Assert.Equal(1, decoder.InvocationCount);
provider.GetImage(decoder);
Assert.Equal(1, decoder.InvocationCount);
}
public static string[] AllBmpFiles => TestImages.Bmp.All; public static string[] AllBmpFiles => TestImages.Bmp.All;
[Theory] [Theory]
@ -96,7 +130,7 @@ namespace ImageSharp.Tests
Image<TPixel> image = provider.GetImage(); Image<TPixel> image = provider.GetImage();
provider.Utility.SaveTestOutputFile(image, "png"); provider.Utility.SaveTestOutputFile(image, "png");
} }
[Theory] [Theory]
[WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Rgba32 | PixelTypes.Argb32)] [WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Rgba32 | PixelTypes.Argb32)]
public void Use_WithSolidFilledImagesAttribute<TPixel>(TestImageProvider<TPixel> provider) public void Use_WithSolidFilledImagesAttribute<TPixel>(TestImageProvider<TPixel> provider)

Loading…
Cancel
Save