// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests.TestUtilities { using System; using System.Collections.Concurrent; using System.Reflection; /// /// Provides instances for parametric unit tests. /// /// The pixel format of the image public abstract class TestImageFactory : ITestImageFactory where TColor : struct, IPackedPixel, IEquatable { public abstract Image Create(); public virtual string SourceFileOrDescription => ""; /// /// Utility instance to provide informations about the test image & manage input/output /// public ImagingTestCaseUtility Utility { get; private set; } protected TestImageFactory() { } protected virtual TestImageFactory InitUtility(MethodInfo testMethod) { this.Utility = new ImagingTestCaseUtility() { SourceFileOrDescription = this.SourceFileOrDescription, PixelTypeName = typeof(TColor).Name }; if (testMethod != null) { this.Utility.Init(testMethod); } return this; } private class BlankFactory : TestImageFactory { protected int Width { get; } protected int Height { get; } public BlankFactory(int width, int height) { this.Width = width; this.Height = height; } public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; public override Image Create() => new Image(this.Width, this.Height); } public static TestImageFactory Blank(int width, int height, MethodInfo testMethod = null) => new BlankFactory(width, height).InitUtility(testMethod); private class LambdaFactory : TestImageFactory { private readonly Func> creator; public LambdaFactory(Func> creator) { this.creator = creator; } public override Image Create() => this.creator(); } public static TestImageFactory Lambda( Func> func, MethodInfo testMethod = null) => new LambdaFactory(func).InitUtility(testMethod); private class FileFactory : TestImageFactory { private static ConcurrentDictionary> cache = new ConcurrentDictionary>(); private string filePath; public FileFactory(string filePath) { this.filePath = filePath; } public override string SourceFileOrDescription => this.filePath; public override Image Create() { var cachedImage = cache.GetOrAdd( this.filePath, fn => { var testFile = TestFile.CreateWithoutImage(this.filePath); return new Image(testFile.Bytes); }); return new Image(cachedImage); } } public static TestImageFactory File(string filePath, MethodInfo testMethod = null) { return new FileFactory(filePath).InitUtility(testMethod); } private class SolidFactory : BlankFactory { private readonly byte r; private readonly byte g; private readonly byte b; private readonly byte a; public override Image Create() { var image = base.Create(); TColor color = default(TColor); color.PackFromBytes(this.r, this.g, this.b, this.a); return image.Fill(color); } public SolidFactory(int width, int height, byte r, byte g, byte b, byte a) : base(width, height) { this.r = r; this.g = g; this.b = b; this.a = a; } } public static TestImageFactory Solid( int width, int height, byte r, byte g, byte b, byte a = 255, MethodInfo testMethod = null) { return new SolidFactory(width, height, r, g, b, a).InitUtility(testMethod); } } /// /// Marker /// public interface ITestImageFactory { } }