From fb5c908bbf9270f125f6cd8bdb616e911e775e6e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Wed, 16 Aug 2017 15:53:26 +0200 Subject: [PATCH] TestImageProvider can use custom decoder now --- .../Formats/Jpg/JpegDecoderTests.cs | 4 +-- .../TestUtilities/Factories/GenericFactory.cs | 2 ++ .../TestUtilities/Factories/ImageFactory.cs | 3 ++ .../ImageProviders/FileProvider.cs | 28 ++++++++++++--- .../ImageProviders/TestImageProvider.cs | 6 ++++ .../Tests/TestImageProviderTests.cs | 36 ++++++++++++++++++- 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index ff78f12e6f..e14375aabc 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests using Xunit; - public class JpegDecoderTests : TestBase + public class JpegDecoderTests { public static string[] BaselineTestJpegs = { @@ -29,7 +29,7 @@ namespace ImageSharp.Tests // TODO: We should make this comparer less tolerant ... private static readonly ImageComparer VeryTolerantJpegComparer = ImageComparer.Tolerant(0.005f, pixelThresholdInPixelByteSum: 4); - + [Theory] [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32 | PixelTypes.Rgba32 | PixelTypes.Argb32)] public void DecodeBaselineJpeg(TestImageProvider provider) diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs index bfa70a2a50..efe548ee88 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs @@ -10,6 +10,8 @@ namespace ImageSharp.Tests using ImageSharp.PixelFormats; /// + /// 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. ) /// Used as parameter for -based factory methods /// diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs index 052a4c774f..efa08c736f 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs @@ -7,6 +7,9 @@ namespace ImageSharp.Tests { using ImageSharp.PixelFormats; + /// + /// TODO: Non-generic 'Image' class has been removed. We no longer need the factory pattern here! + /// public class ImageFactory : GenericFactory { public override Image CreateImage(byte[] bytes) => Image.Load(bytes); diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index 362924de59..7ae103cd29 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Tests using System; using System.Collections.Concurrent; + using ImageSharp.Formats; using ImageSharp.PixelFormats; using Xunit.Abstractions; @@ -19,10 +20,10 @@ namespace ImageSharp.Tests { // Need PixelTypes in the dictionary key, because result images of TestImageProvider.FileProvider // are shared between PixelTypes.Color & PixelTypes.Rgba32 - private class Key : Tuple + private class Key : Tuple { - public Key(PixelTypes item1, string item2) - : base(item1, item2) + public Key(PixelTypes pixelType, string filePath, Type customDecoderType = null) + : base(pixelType, filePath, customDecoderType) { } } @@ -51,10 +52,27 @@ namespace ImageSharp.Tests fn => { TestFile testFile = TestFile.Create(this.FilePath); - return this.Factory.CreateImage(testFile.Bytes); + return Image.Load(testFile.Bytes); }); - return this.Factory.CreateImage(cachedImage); + return cachedImage.Clone(); + } + + public override Image GetImage(IImageDecoder decoder) + { + Guard.NotNull(decoder, nameof(decoder)); + + Key key = new Key(this.PixelType, this.FilePath, decoder.GetType()); + + Image cachedImage = cache.GetOrAdd( + key, + fn => + { + TestFile testFile = TestFile.Create(this.FilePath); + return Image.Load(testFile.Bytes, decoder); + }); + + return cachedImage.Clone(); } public override void Deserialize(IXunitSerializationInfo info) diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs index 8681bb334a..99d1125d72 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestImageProvider.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Tests using System; using System.Reflection; + using ImageSharp.Formats; using ImageSharp.PixelFormats; using Xunit.Abstractions; @@ -82,6 +83,11 @@ namespace ImageSharp.Tests /// public abstract Image GetImage(); + public virtual Image GetImage(IImageDecoder decoder) + { + throw new NotSupportedException($"Decoder specific GetImage() is not supported with {this.GetType().Name}!"); + } + /// /// Returns an instance to the test case with the necessary traits. /// diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index c01babb632..e9c4fc3d3d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -3,12 +3,17 @@ // Licensed under the Apache License, Version 2.0. // +// ReSharper disable InconsistentNaming namespace ImageSharp.Tests { using System; + using System.IO; + using ImageSharp.Formats; using ImageSharp.PixelFormats; + using Moq; + using Xunit; using Xunit.Abstractions; @@ -85,6 +90,35 @@ namespace ImageSharp.Tests this.Output.WriteLine(fn); } + private class TestDecoder : IImageDecoder + { + public int InvocationCount { get; private set; } = 0; + + public Image Decode(Configuration configuration, Stream stream) + where TPixel : struct, IPixel + { + this.InvocationCount++; + return new Image(42, 42); + } + } + + + [Theory] + [WithFile(TestImages.Bmp.F, PixelTypes.Rgba32)] + public void GetImage_WithCustomDecoder_ShouldUtilizeCache(TestImageProvider provider) + where TPixel : struct, IPixel + { + 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; [Theory] @@ -96,7 +130,7 @@ namespace ImageSharp.Tests Image image = provider.GetImage(); provider.Utility.SaveTestOutputFile(image, "png"); } - + [Theory] [WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Rgba32 | PixelTypes.Argb32)] public void Use_WithSolidFilledImagesAttribute(TestImageProvider provider)