//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Tests
{
using System;
using System.Reflection;
///
/// Provides instances for parametric unit tests.
///
/// The pixel format of the image
public abstract partial class TestImageProvider
where TColor : struct, IPackedPixel, IEquatable
{
public PixelTypes PixelType { get; private set; } = typeof(TColor).GetPixelType();
public virtual string SourceFileOrDescription => "";
///
/// Utility instance to provide informations about the test image & manage input/output
///
public ImagingTestCaseUtility Utility { get; private set; }
public GenericFactory Factory { get; private set; } = new GenericFactory();
public static TestImageProvider Blank(
int width,
int height,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new BlankProvider(width, height).Init(testMethod, pixelTypeOverride);
public static TestImageProvider File(
string filePath,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
{
return new FileProvider(filePath).Init(testMethod, pixelTypeOverride);
}
public static TestImageProvider Lambda(
Func, Image> func,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new LambdaProvider(func).Init(testMethod, pixelTypeOverride);
public static TestImageProvider Solid(
int width,
int height,
byte r,
byte g,
byte b,
byte a = 255,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
{
return new SolidProvider(width, height, r, g, b, a).Init(testMethod, pixelTypeOverride);
}
///
/// Returns an instance to the test case with the necessary traits.
///
public abstract Image GetImage();
protected TestImageProvider Init(MethodInfo testMethod, PixelTypes pixelTypeOverride)
{
if (pixelTypeOverride != PixelTypes.Undefined)
{
this.PixelType = pixelTypeOverride;
}
if (pixelTypeOverride == PixelTypes.StandardImageClass)
{
this.Factory = new ImageFactory() as GenericFactory;
}
this.Utility = new ImagingTestCaseUtility()
{
SourceFileOrDescription = this.SourceFileOrDescription,
PixelTypeName = this.PixelType.ToString()
};
if (testMethod != null)
{
this.Utility.Init(testMethod);
}
return this;
}
public override string ToString()
{
string provName = this.GetType().Name.Replace("Provider", "");
return $"{this.SourceFileOrDescription}[{this.PixelType}]";
}
}
}