Browse Source

introduced PixelTypes.ColorWithDefaultImageClass

pull/57/head
antonfirsov 10 years ago
parent
commit
6da4ac1ed5
  1. 6
      tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs
  2. 4
      tests/ImageSharp.Tests/Image/PixelAccessorTests.cs
  3. 21
      tests/ImageSharp.Tests/TestFile.cs
  4. 41
      tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs
  5. 10
      tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs
  6. 7
      tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs
  7. 138
      tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs
  8. 39
      tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs
  9. 53
      tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs
  10. 44
      tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs
  11. 9
      tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs

6
tests/ImageSharp.Tests/Formats/Jpg/JpegTests.cs

@ -26,7 +26,7 @@ namespace ImageSharp.Tests.Formats.Jpg
// TODO: Turned off PixelTypes.All to be CI-friendly, what should be the practice? // TODO: Turned off PixelTypes.All to be CI-friendly, what should be the practice?
[Theory] [Theory]
//[WithFileCollection(nameof(AllJpegFiles), PixelTypes.All)] //[WithFileCollection(nameof(AllJpegFiles), PixelTypes.All)]
[WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.Argb)] [WithFileCollection(nameof(AllJpegFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb)]
public void OpenJpeg_SaveBmp<TColor>(TestImageProvider<TColor> provider) public void OpenJpeg_SaveBmp<TColor>(TestImageProvider<TColor> provider)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
@ -39,8 +39,8 @@ namespace ImageSharp.Tests.Formats.Jpg
public static IEnumerable<string> AllBmpFiles => TestImages.Bmp.All; public static IEnumerable<string> AllBmpFiles => TestImages.Bmp.All;
[Theory] [Theory]
[WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio420, 75)] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb, JpegSubsample.Ratio420, 75)]
[WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb, JpegSubsample.Ratio444, 75)] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.ColorWithDefaultImageClass | PixelTypes.Argb, JpegSubsample.Ratio444, 75)]
public void OpenBmp_SaveJpeg<TColor>(TestImageProvider<TColor> provider, JpegSubsample subSample, int quality) public void OpenBmp_SaveJpeg<TColor>(TestImageProvider<TColor> provider, JpegSubsample subSample, int quality)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {

4
tests/ImageSharp.Tests/Image/PixelAccessorTests.cs

@ -17,10 +17,10 @@ namespace ImageSharp.Tests
/// </summary> /// </summary>
public class PixelAccessorTests public class PixelAccessorTests
{ {
public static Image<TColor> CreateTestImage<TColor>() public static Image<TColor> CreateTestImage<TColor>(GenericFactory<TColor> factory)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
Image<TColor> image = new Image<TColor>(10, 10); Image<TColor> image = factory.CreateImage(10, 10);
using (var pixels = image.Lock()) using (var pixels = image.Lock())
{ {

21
tests/ImageSharp.Tests/TestFile.cs

@ -27,28 +27,19 @@ namespace ImageSharp.Tests
private readonly Image image; private readonly Image image;
private readonly string file; private readonly string file;
private TestFile(string file, bool decodeImage) private TestFile(string file)
{ {
this.file = file; this.file = file;
this.Bytes = File.ReadAllBytes(file); this.Bytes = File.ReadAllBytes(file);
if (decodeImage) this.image = new Image(this.Bytes);
{
this.image = new Image(this.Bytes);
}
} }
public static TestFile Create(string file) => CreateImpl(file, true);
// No need to decode the image when used by TestImageProvider!
internal static TestFile CreateWithoutImage(string file) => CreateImpl(file, false);
private static TestFile CreateImpl(string file, bool decodeImage) public static TestFile Create(string file)
{ {
return cache.GetOrAdd(file, (string fileName) => return cache.GetOrAdd(file, (string fileName) =>
{ {
return new TestFile(FormatsDirectory + fileName, decodeImage); return new TestFile(FormatsDirectory + fileName);
}); });
} }
@ -82,10 +73,6 @@ namespace ImageSharp.Tests
public Image CreateImage() public Image CreateImage()
{ {
if (this.image == null)
{
throw new InvalidOperationException("TestFile.CreateImage() is invalid because instance has been created with decodeImage = false!");
}
return new Image(this.image); return new Image(this.image);
} }
} }

41
tests/ImageSharp.Tests/TestUtilities/GenericFactory.cs

@ -0,0 +1,41 @@
// <copyright file="GenericFactory.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.TestUtilities
{
using System;
/// <summary>
/// Utility class to create specialized subclasses generic classes (eg. <see cref="Image"/>)
/// </summary>
public class GenericFactory<TColor>
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
public virtual Image<TColor> CreateImage(int width, int height)
{
return new Image<TColor>(width, height);
}
public virtual Image<TColor> CreateImage(byte[] bytes)
{
return new Image<TColor>(bytes);
}
public virtual PixelArea<TColor> CreatePixelArea(int width, int height, ComponentOrder componentOrder)
{
return new PixelArea<TColor>(width, height, componentOrder);
}
}
public class DefaultImageClassSpecificFactory : GenericFactory<Color>
{
public override Image<Color> CreateImage(byte[] bytes) => new Image(bytes);
public override Image<Color> CreateImage(int width, int height) => new Image(width, height);
public override PixelArea<Color> CreatePixelArea(int width, int height, ComponentOrder componentOrder)
=> new PixelArea<Color>(width, height, componentOrder);
}
}

10
tests/ImageSharp.Tests/TestUtilities/ImageDataAttributeBase.cs

@ -6,6 +6,7 @@ namespace ImageSharp.Tests.TestUtilities
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -35,15 +36,16 @@ namespace ImageSharp.Tests.TestUtilities
} }
else else
{ {
foreach (var pixelType in this.PixelTypes.ToTypes()) foreach (var kv in this.PixelTypes.ExpandAllTypes())
{ {
var factoryType = typeof(TestImageProvider<>).MakeGenericType(pixelType); var factoryType = typeof(TestImageProvider<>).MakeGenericType(kv.Value);
foreach (object[] originalFacoryMethodArgs in this.GetAllFactoryMethodArgs(testMethod, factoryType)) foreach (object[] originalFacoryMethodArgs in this.GetAllFactoryMethodArgs(testMethod, factoryType))
{ {
var actualFactoryMethodArgs = new object[originalFacoryMethodArgs.Length + 1]; var actualFactoryMethodArgs = new object[originalFacoryMethodArgs.Length + 2];
Array.Copy(originalFacoryMethodArgs, actualFactoryMethodArgs, originalFacoryMethodArgs.Length); Array.Copy(originalFacoryMethodArgs, actualFactoryMethodArgs, originalFacoryMethodArgs.Length);
actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 1] = testMethod; actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 2] = testMethod;
actualFactoryMethodArgs[actualFactoryMethodArgs.Length - 1] = kv.Key;
var factory = factoryType.GetMethod(this.GetFactoryMethodName(testMethod)) var factory = factoryType.GetMethod(this.GetFactoryMethodName(testMethod))
.Invoke(null, actualFactoryMethodArgs); .Invoke(null, actualFactoryMethodArgs);

7
tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs

@ -13,7 +13,7 @@ namespace ImageSharp.Tests.TestUtilities
[Flags] [Flags]
public enum PixelTypes : uint public enum PixelTypes : uint
{ {
None = 0, Undefined = 0,
Alpha8 = 1 << 0, Alpha8 = 1 << 0,
@ -48,6 +48,11 @@ namespace ImageSharp.Tests.TestUtilities
Short2 = 1 << 15, Short2 = 1 << 15,
Short4 = 1 << 16, Short4 = 1 << 16,
/// <summary>
/// Triggers instantiating the <see cref="Image"/> subclass of <see cref="Image{TColor}"/>
/// </summary>
ColorWithDefaultImageClass = 1 << 29,
// TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper // TODO: Add multi-flag entries by rules defined in PackedPixelConverterHelper

138
tests/ImageSharp.Tests/TestUtilities/TestImageProvider.cs

@ -16,10 +16,7 @@ namespace ImageSharp.Tests.TestUtilities
public abstract class TestImageProvider<TColor> : ITestImageFactory public abstract class TestImageProvider<TColor> : ITestImageFactory
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
/// <summary> public PixelTypes PixelType { get; private set; } = typeof(TColor).GetPixelType();
/// Returns an <see cref="Image{TColor}"/> instance to the test case with the necessary traits.
/// </summary>
public abstract Image<TColor> GetImage();
public virtual string SourceFileOrDescription => ""; public virtual string SourceFileOrDescription => "";
@ -28,8 +25,60 @@ namespace ImageSharp.Tests.TestUtilities
/// </summary> /// </summary>
public ImagingTestCaseUtility Utility { get; private set; } public ImagingTestCaseUtility Utility { get; private set; }
protected virtual TestImageProvider<TColor> InitUtility(MethodInfo testMethod) public GenericFactory<TColor> Factory { get; private set; } = new GenericFactory<TColor>();
public static TestImageProvider<TColor> Blank(
int width,
int height,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new BlankProvider(width, height).Init(testMethod, pixelTypeOverride);
public static TestImageProvider<TColor> File(
string filePath,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
{
return new FileProvider(filePath).Init(testMethod, pixelTypeOverride);
}
public static TestImageProvider<TColor> Lambda(
Func<GenericFactory<TColor>, Image<TColor>> func,
MethodInfo testMethod = null,
PixelTypes pixelTypeOverride = PixelTypes.Undefined)
=> new LambdaProvider(func).Init(testMethod, pixelTypeOverride);
public static TestImageProvider<TColor> 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);
}
/// <summary>
/// Returns an <see cref="Image{TColor}"/> instance to the test case with the necessary traits.
/// </summary>
public abstract Image<TColor> GetImage();
protected TestImageProvider<TColor> Init(MethodInfo testMethod, PixelTypes pixelTypeOverride)
{ {
if (pixelTypeOverride != PixelTypes.Undefined)
{
this.PixelType = pixelTypeOverride;
}
if (pixelTypeOverride == PixelTypes.ColorWithDefaultImageClass)
{
this.Factory = new DefaultImageClassSpecificFactory() as GenericFactory<TColor>;
}
this.Utility = new ImagingTestCaseUtility() this.Utility = new ImagingTestCaseUtility()
{ {
SourceFileOrDescription = this.SourceFileOrDescription, SourceFileOrDescription = this.SourceFileOrDescription,
@ -46,10 +95,6 @@ namespace ImageSharp.Tests.TestUtilities
private class BlankProvider : TestImageProvider<TColor> private class BlankProvider : TestImageProvider<TColor>
{ {
protected int Width { get; }
protected int Height { get; }
public BlankProvider(int width, int height) public BlankProvider(int width, int height)
{ {
this.Width = width; this.Width = width;
@ -58,28 +103,13 @@ namespace ImageSharp.Tests.TestUtilities
public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}"; public override string SourceFileOrDescription => $"Blank{this.Width}x{this.Height}";
public override Image<TColor> GetImage() => new Image<TColor>(this.Width, this.Height); protected int Height { get; }
}
public static TestImageProvider<TColor> Blank(int width, int height, MethodInfo testMethod = null)
=> new BlankProvider(width, height).InitUtility(testMethod);
private class LambdaProvider : TestImageProvider<TColor>
{
private readonly Func<Image<TColor>> creator;
public LambdaProvider(Func<Image<TColor>> creator) protected int Width { get; }
{
this.creator = creator;
}
public override Image<TColor> GetImage() => this.creator(); public override Image<TColor> GetImage() => this.Factory.CreateImage(this.Width, this.Height);
} }
public static TestImageProvider<TColor> Lambda(
Func<Image<TColor>> func,
MethodInfo testMethod = null) => new LambdaProvider(func).InitUtility(testMethod);
private class FileProvider : TestImageProvider<TColor> private class FileProvider : TestImageProvider<TColor>
{ {
private static ConcurrentDictionary<string, Image<TColor>> cache = private static ConcurrentDictionary<string, Image<TColor>> cache =
@ -100,37 +130,35 @@ namespace ImageSharp.Tests.TestUtilities
this.filePath, this.filePath,
fn => fn =>
{ {
var testFile = TestFile.CreateWithoutImage(this.filePath); var testFile = TestFile.Create(this.filePath);
return new Image<TColor>(testFile.Bytes); return this.Factory.CreateImage(testFile.Bytes);
}); });
return new Image<TColor>(cachedImage); return new Image<TColor>(cachedImage);
} }
} }
public static TestImageProvider<TColor> File(string filePath, MethodInfo testMethod = null) private class LambdaProvider : TestImageProvider<TColor>
{ {
return new FileProvider(filePath).InitUtility(testMethod); private readonly Func<GenericFactory<TColor>, Image<TColor>> creator;
public LambdaProvider(Func<GenericFactory<TColor>, Image<TColor>> creator)
{
this.creator = creator;
}
public override Image<TColor> GetImage() => this.creator(this.Factory);
} }
private class SolidProvider : BlankProvider private class SolidProvider : BlankProvider
{ {
private readonly byte r; private readonly byte a;
private readonly byte g;
private readonly byte b; private readonly byte b;
private readonly byte a; private readonly byte g;
public override Image<TColor> GetImage()
{
var image = base.GetImage();
TColor color = default(TColor);
color.PackFromBytes(this.r, this.g, this.b, this.a);
return image.Fill(color); private readonly byte r;
}
public SolidProvider(int width, int height, byte r, byte g, byte b, byte a) public SolidProvider(int width, int height, byte r, byte g, byte b, byte a)
: base(width, height) : base(width, height)
@ -140,18 +168,18 @@ namespace ImageSharp.Tests.TestUtilities
this.b = b; this.b = b;
this.a = a; this.a = a;
} }
}
public static TestImageProvider<TColor> Solid( public override string SourceFileOrDescription
int width, => $"Solid{this.Width}x{this.Height}_({this.r},{this.g},{this.b},{this.a})";
int height,
byte r, public override Image<TColor> GetImage()
byte g, {
byte b, var image = base.GetImage();
byte a = 255, TColor color = default(TColor);
MethodInfo testMethod = null) color.PackFromBytes(this.r, this.g, this.b, this.a);
{
return new SolidProvider(width, height, r, g, b, a).InitUtility(testMethod); return image.Fill(color);
}
} }
} }
@ -161,4 +189,4 @@ namespace ImageSharp.Tests.TestUtilities
public interface ITestImageFactory public interface ITestImageFactory
{ {
} }
} }

39
tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs

@ -2,6 +2,7 @@
// Copyright (c) James Jackson-South and contributors. // Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
// </copyright> // </copyright>
namespace ImageSharp.Tests.TestUtilities namespace ImageSharp.Tests.TestUtilities
{ {
using System; using System;
@ -15,27 +16,32 @@ namespace ImageSharp.Tests.TestUtilities
/// </summary> /// </summary>
public static class TestUtilityExtensions public static class TestUtilityExtensions
{ {
private static readonly Dictionary<Type, PixelTypes> ClrTypes2PixelTypes = new Dictionary<Type, PixelTypes>();
private static readonly Assembly ImageSharpAssembly = typeof(Color).GetTypeInfo().Assembly; private static readonly Assembly ImageSharpAssembly = typeof(Color).GetTypeInfo().Assembly;
private static readonly Dictionary<PixelTypes, Type> PixelTypes2ClrTypes = new Dictionary<PixelTypes, Type>(); private static readonly Dictionary<PixelTypes, Type> PixelTypes2ClrTypes = new Dictionary<PixelTypes, Type>();
private static readonly PixelTypes[] PixelTypesExpanded = private static readonly PixelTypes[] AllConcretePixelTypes = FlagsHelper<PixelTypes>
FlagsHelper<PixelTypes>.GetSortedValues().Where(t => t != PixelTypes.All && t != PixelTypes.None).ToArray(); .GetSortedValues()
.Except(new [] {PixelTypes.Undefined, PixelTypes.All })
.ToArray();
static TestUtilityExtensions() static TestUtilityExtensions()
{ {
Assembly assembly = typeof(Color).GetTypeInfo().Assembly;
string nameSpace = typeof(Color).FullName; string nameSpace = typeof(Color).FullName;
nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1); nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Color).Name.Length - 1);
foreach (PixelTypes pt in PixelTypesExpanded) foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.ColorWithDefaultImageClass))
{ {
string typeName = $"{nameSpace}.{FlagsHelper<PixelTypes>.ToString(pt)}"; string typeName = $"{nameSpace}.{FlagsHelper<PixelTypes>.ToString(pt)}";
var t = assembly.GetType(typeName); var t = ImageSharpAssembly.GetType(typeName);
if (t != null) if (t != null)
{ {
PixelTypes2ClrTypes[pt] = t; PixelTypes2ClrTypes[pt] = t;
ClrTypes2PixelTypes[t] = pt;
} }
} }
PixelTypes2ClrTypes[PixelTypes.ColorWithDefaultImageClass] = typeof(Color);
} }
public static Type GetPackedType(Type pixelType) public static Type GetPackedType(Type pixelType)
@ -46,13 +52,10 @@ namespace ImageSharp.Tests.TestUtilities
return intrfcType.GetGenericArguments().Single(); return intrfcType.GetGenericArguments().Single();
} }
public static bool HasFlag(this PixelTypes pixelTypes, PixelTypes flag) => (pixelTypes & flag) == flag; public static bool HasFlag(this PixelTypes pixelTypes, PixelTypes flag) => (pixelTypes & flag) == flag;
public static bool IsEquivalentTo<TColor>( public static bool IsEquivalentTo<TColor>(this Image<TColor> a, Image<TColor> b, bool compareAlpha = true)
this Image<TColor> a,
Image<TColor> b,
bool compareAlpha = true)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
if (a.Width != b.Width || a.Height != b.Height) if (a.Width != b.Width || a.Height != b.Height)
@ -101,19 +104,23 @@ namespace ImageSharp.Tests.TestUtilities
public static Type ToType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType]; public static Type ToType(this PixelTypes pixelType) => PixelTypes2ClrTypes[pixelType];
public static IEnumerable<Type> ToTypes(this PixelTypes pixelTypes) public static PixelTypes GetPixelType(this Type colorStructClrType) => ClrTypes2PixelTypes[colorStructClrType];
public static IEnumerable<KeyValuePair<PixelTypes, Type>> ExpandAllTypes(this PixelTypes pixelTypes)
{ {
if (pixelTypes == PixelTypes.None) if (pixelTypes == PixelTypes.Undefined)
{ {
return Enumerable.Empty<Type>(); return Enumerable.Empty<KeyValuePair<PixelTypes, Type>>();
} }
else if (pixelTypes == PixelTypes.All) else if (pixelTypes == PixelTypes.All)
{ {
// TODO: Need to return unknown types here without forcing CLR to load all types in ImageSharp assembly // TODO: Need to return unknown types here without forcing CLR to load all types in ImageSharp assembly
return PixelTypes2ClrTypes.Values; return PixelTypes2ClrTypes;
} }
return PixelTypesExpanded.Where(pt => pixelTypes.HasFlag(pt)).Select(pt => pt.ToType()); return AllConcretePixelTypes
.Where(pt => pixelTypes.HasFlag(pt))
.Select(pt => new KeyValuePair<PixelTypes, Type>(pt, pt.ToType()));
} }
} }
} }

53
tests/ImageSharp.Tests/TestUtilities/Tests/TestImageFactoryTests.cs → tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs

@ -4,6 +4,7 @@
// </copyright> // </copyright>
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
namespace ImageSharp.Tests.TestUtilities.Tests namespace ImageSharp.Tests.TestUtilities.Tests
{ {
using System; using System;
@ -11,22 +12,18 @@ namespace ImageSharp.Tests.TestUtilities.Tests
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
public class TestImageFactoryTests public class TestImageProviderTests
{ {
public TestImageProviderTests(ITestOutputHelper output)
public TestImageFactoryTests(ITestOutputHelper output)
{ {
this.Output = output; this.Output = output;
} }
private ITestOutputHelper Output { get; } private ITestOutputHelper Output { get; }
[Theory] [Theory]
[WithBlankImages(42, 666, PixelTypes.Color | PixelTypes.Argb | PixelTypes.HalfSingle, "hello")] [WithBlankImages(42, 666, PixelTypes.Color | PixelTypes.Argb | PixelTypes.HalfSingle, "hello")]
public void Use_WithEmptyImageAttribute<TColor>( public void Use_WithEmptyImageAttribute<TColor>(TestImageProvider<TColor> provider, string message)
TestImageProvider<TColor> provider,
string message)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
var img = provider.GetImage(); var img = provider.GetImage();
@ -50,6 +47,27 @@ namespace ImageSharp.Tests.TestUtilities.Tests
Assert.Equal("hello", message); Assert.Equal("hello", message);
} }
[Theory]
[WithBlankImages(1, 1, PixelTypes.Color, PixelTypes.Color)]
[WithBlankImages(1, 1, PixelTypes.Alpha8, PixelTypes.Alpha8)]
[WithBlankImages(1, 1, PixelTypes.ColorWithDefaultImageClass, PixelTypes.ColorWithDefaultImageClass)]
public void PixelType_PropertyValueIsCorrect<TColor>(TestImageProvider<TColor> provider, PixelTypes expected)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
Assert.Equal(expected, provider.PixelType);
}
[Theory]
[WithBlankImages(1, 1, PixelTypes.ColorWithDefaultImageClass)]
public void PixelTypes_ColorWithDefaultImageClass_TriggersCreatingTheNonGenericDerivedImageClass<TColor>(
TestImageProvider<TColor> provider)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
var img = provider.GetImage();
Assert.IsType<Image>(img);
}
// TODO: @dlemstra this works only with constant strings! // TODO: @dlemstra this works only with constant strings!
[Theory] [Theory]
[WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)] [WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)]
@ -78,7 +96,7 @@ namespace ImageSharp.Tests.TestUtilities.Tests
var image = provider.GetImage(); var image = provider.GetImage();
provider.Utility.SaveTestOutputFile(image, "png"); provider.Utility.SaveTestOutputFile(image, "png");
} }
[Theory] [Theory]
[WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Color | PixelTypes.Argb)] [WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Color | PixelTypes.Argb)]
public void Use_WithSolidFilledImagesAttribute<TColor>(TestImageProvider<TColor> provider) public void Use_WithSolidFilledImagesAttribute<TColor>(TestImageProvider<TColor> provider)
@ -107,21 +125,31 @@ namespace ImageSharp.Tests.TestUtilities.Tests
} }
} }
public static Image<TColor> TestMemberFactory<TColor>() /// <summary>
/// Need to us <see cref="GenericFactory{TColor}"/> to create instance of <see cref="Image"/> when pixelType is ColorWithDefaultImageClass
/// </summary>
/// <typeparam name="TColor"></typeparam>
/// <param name="factory"></param>
/// <returns></returns>
public static Image<TColor> CreateTestImage<TColor>(GenericFactory<TColor> factory)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
return new Image<TColor>(3, 3); return factory.CreateImage(3, 3);
} }
[Theory] [Theory]
[WithMemberFactory(nameof(TestMemberFactory), PixelTypes.All)] [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All)]
public void Use_WithMemberFactoryAttribute<TColor>(TestImageProvider<TColor> provider) public void Use_WithMemberFactoryAttribute<TColor>(TestImageProvider<TColor> provider)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
var img = provider.GetImage(); var img = provider.GetImage();
Assert.Equal(img.Width, 3); Assert.Equal(img.Width, 3);
} if (provider.PixelType == PixelTypes.ColorWithDefaultImageClass)
{
Assert.IsType<Image>(img);
}
}
public static readonly TheoryData<ITestImageFactory> BasicData = new TheoryData<ITestImageFactory>() public static readonly TheoryData<ITestImageFactory> BasicData = new TheoryData<ITestImageFactory>()
{ {
@ -131,7 +159,6 @@ namespace ImageSharp.Tests.TestUtilities.Tests
20) 20)
}; };
[Theory] [Theory]
[MemberData(nameof(BasicData))] [MemberData(nameof(BasicData))]
public void Blank_MemberData<TColor>(TestImageProvider<TColor> provider) public void Blank_MemberData<TColor>(TestImageProvider<TColor> provider)

44
tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs

@ -7,6 +7,7 @@
namespace ImageSharp.Tests.TestUtilities.Tests namespace ImageSharp.Tests.TestUtilities.Tests
{ {
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using System.Reflection; using System.Reflection;
@ -23,10 +24,10 @@ namespace ImageSharp.Tests.TestUtilities.Tests
private ITestOutputHelper Output { get; } private ITestOutputHelper Output { get; }
public static Image<TColor> CreateTestImage<TColor>() public static Image<TColor> CreateTestImage<TColor>(GenericFactory<TColor> factory)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
Image<TColor> image = new Image<TColor>(10, 10); Image<TColor> image = factory.CreateImage(10, 10);
using (var pixels = image.Lock()) using (var pixels = image.Lock())
{ {
@ -93,31 +94,52 @@ namespace ImageSharp.Tests.TestUtilities.Tests
[InlineData(PixelTypes.Color, typeof(Color))] [InlineData(PixelTypes.Color, typeof(Color))]
[InlineData(PixelTypes.Argb, typeof(Argb))] [InlineData(PixelTypes.Argb, typeof(Argb))]
[InlineData(PixelTypes.HalfVector4, typeof(HalfVector4))] [InlineData(PixelTypes.HalfVector4, typeof(HalfVector4))]
[InlineData(PixelTypes.ColorWithDefaultImageClass, typeof(Color))]
public void ToType(PixelTypes pt, Type expectedType) public void ToType(PixelTypes pt, Type expectedType)
{ {
Assert.Equal(pt.ToType(), expectedType); Assert.Equal(pt.ToType(), expectedType);
} }
[Theory]
[InlineData(typeof(Color), PixelTypes.Color)]
[InlineData(typeof(Argb), PixelTypes.Argb)]
public void GetPixelType(Type clrType, PixelTypes expectedPixelType)
{
Assert.Equal(expectedPixelType, clrType.GetPixelType());
}
private static void AssertContainsPixelType<T>(
PixelTypes pt,
IEnumerable<KeyValuePair<PixelTypes, Type>> pixelTypesExp)
{
Assert.Contains(new KeyValuePair<PixelTypes, Type>(pt, typeof(T)), pixelTypesExp);
}
[Fact] [Fact]
public void ToTypes() public void ToTypes()
{ {
PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2; PixelTypes pixelTypes = PixelTypes.Alpha8 | PixelTypes.Bgr565 | PixelTypes.Color | PixelTypes.HalfVector2 | PixelTypes.ColorWithDefaultImageClass;
var clrTypes = pixelTypes.ToTypes().ToArray(); var expanded = pixelTypes.ExpandAllTypes();
Assert.Equal(clrTypes.Length, 4); Assert.Equal(expanded.Count(), 5);
Assert.Contains(typeof(Alpha8), clrTypes);
Assert.Contains(typeof(Bgr565), clrTypes); AssertContainsPixelType<Alpha8>(PixelTypes.Alpha8, expanded);
Assert.Contains(typeof(Color), clrTypes); AssertContainsPixelType<Bgr565>(PixelTypes.Bgr565, expanded);
Assert.Contains(typeof(HalfVector2), clrTypes); AssertContainsPixelType<Color>(PixelTypes.Color, expanded);
AssertContainsPixelType<HalfVector2>(PixelTypes.HalfVector2, expanded);
AssertContainsPixelType<Color>(PixelTypes.ColorWithDefaultImageClass, expanded);
} }
[Fact] [Fact]
public void ToTypes_All() public void ToTypes_All()
{ {
var clrTypes = PixelTypes.All.ToTypes().ToArray(); var expanded = PixelTypes.All.ExpandAllTypes().ToArray();
Assert.True(clrTypes.Length >= FlagsHelper<PixelTypes>.GetSortedValues().Length - 2); Assert.True(expanded.Length >= FlagsHelper<PixelTypes>.GetSortedValues().Length - 2);
AssertContainsPixelType<Color>(PixelTypes.Color, expanded);
AssertContainsPixelType<Color>(PixelTypes.ColorWithDefaultImageClass, expanded);
} }
} }
} }

9
tests/ImageSharp.Tests/TestUtilities/WithMemberFactoryAttribute.cs

@ -6,6 +6,7 @@
namespace ImageSharp.Tests.TestUtilities namespace ImageSharp.Tests.TestUtilities
{ {
using System; using System;
using System.Linq;
using System.Reflection; using System.Reflection;
/// <summary> /// <summary>
@ -34,8 +35,12 @@ namespace ImageSharp.Tests.TestUtilities
var m = testMethod.DeclaringType.GetMethod(this.memberMethodName); var m = testMethod.DeclaringType.GetMethod(this.memberMethodName);
var args = factoryType.GetGenericArguments(); var args = factoryType.GetGenericArguments();
var imgType = typeof(Image<>).MakeGenericType(args); var colorType = args.Single();
var funcType = typeof(Func<>).MakeGenericType(imgType);
var imgType = typeof(Image<>).MakeGenericType(colorType);
var genericFactoryType = (typeof(GenericFactory<>)).MakeGenericType(colorType);
var funcType = typeof(Func<,>).MakeGenericType(genericFactoryType, imgType);
var genericMethod = m.MakeGenericMethod(args); var genericMethod = m.MakeGenericMethod(args);

Loading…
Cancel
Save