mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
6.4 KiB
167 lines
6.4 KiB
// <copyright file="TestImageFactoryTests.cs" company="James Jackson-South">
|
|
// Copyright (c) James Jackson-South and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
// </copyright>
|
|
|
|
// ReSharper disable InconsistentNaming
|
|
namespace ImageSharp.Tests.TestUtilities
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
public class TestImageFactoryTests
|
|
{
|
|
|
|
public TestImageFactoryTests(ITestOutputHelper output)
|
|
{
|
|
this.Output = output;
|
|
}
|
|
|
|
private ITestOutputHelper Output { get; }
|
|
|
|
|
|
[Theory]
|
|
[WithBlankImages(42, 666, PixelTypes.Color | PixelTypes.Argb | PixelTypes.HalfSingle, "hello")]
|
|
public void Use_WithEmptyImageAttribute<TColor>(
|
|
TestImageFactory<TColor> factory,
|
|
string message)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
var img = factory.Create();
|
|
|
|
Assert.Equal(42, img.Width);
|
|
Assert.Equal(666, img.Height);
|
|
Assert.Equal("hello", message);
|
|
}
|
|
|
|
[Theory]
|
|
[WithBlankImages(42, 666, PixelTypes.All, "hello")]
|
|
public void Use_WithBlankImagesAttribute_WithAllPixelTypes<TColor>(
|
|
TestImageFactory<TColor> factory,
|
|
string message)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
var img = factory.Create();
|
|
|
|
Assert.Equal(42, img.Width);
|
|
Assert.Equal(666, img.Height);
|
|
Assert.Equal("hello", message);
|
|
}
|
|
|
|
// TODO: @dlemstra this works only with constant strings!
|
|
[Theory]
|
|
[WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)]
|
|
[WithFile(TestImages.Bmp.F, PixelTypes.All, 88)]
|
|
public void Use_WithFileAttribute<TColor>(TestImageFactory<TColor> factory, int yo)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
Assert.NotNull(factory.Utility.SourceFileOrDescription);
|
|
var img = factory.Create();
|
|
Assert.True(img.Width * img.Height > 0);
|
|
|
|
Assert.Equal(88, yo);
|
|
|
|
string fn = factory.Utility.GetTestOutputFileName("jpg");
|
|
this.Output.WriteLine(fn);
|
|
}
|
|
|
|
public static string[] AllBmpFiles => TestImages.Bmp.All;
|
|
|
|
[Theory]
|
|
[WithFileCollection(nameof(AllBmpFiles), PixelTypes.Color | PixelTypes.Argb)]
|
|
public void Use_WithFileCollection<TColor>(TestImageFactory<TColor> factory)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
Assert.NotNull(factory.Utility.SourceFileOrDescription);
|
|
var image = factory.Create();
|
|
factory.Utility.SaveTestOutputFile(image, "png");
|
|
}
|
|
|
|
[Theory]
|
|
[WithSolidFilledImages(10, 20, 255, 100, 50, 200, PixelTypes.Color | PixelTypes.Argb)]
|
|
public void Use_WithSolidFilledImagesAttribute<TColor>(TestImageFactory<TColor> factory)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
var img = factory.Create();
|
|
Assert.Equal(img.Width, 10);
|
|
Assert.Equal(img.Height, 20);
|
|
|
|
byte[] colors = new byte[4];
|
|
|
|
using (var pixels = img.Lock())
|
|
{
|
|
for (int y = 0; y < pixels.Height; y++)
|
|
{
|
|
for (int x = 0; x < pixels.Width; x++)
|
|
{
|
|
pixels[x, y].ToBytes(colors, 0, ComponentOrder.XYZW);
|
|
|
|
Assert.Equal(colors[0], 255);
|
|
Assert.Equal(colors[1], 100);
|
|
Assert.Equal(colors[2], 50);
|
|
Assert.Equal(colors[3], 200);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Image<TColor> TestMemberFactory<TColor>()
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
return new Image<TColor>(3, 3);
|
|
}
|
|
|
|
[Theory]
|
|
[WithMemberFactory(nameof(TestMemberFactory), PixelTypes.All)]
|
|
public void Use_WithMemberFactoryAttribute<TColor>(TestImageFactory<TColor> factory)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
var img = factory.Create();
|
|
Assert.Equal(img.Width, 3);
|
|
}
|
|
|
|
|
|
public static readonly TheoryData<ITestImageFactory> BasicData = new TheoryData<ITestImageFactory>()
|
|
{
|
|
TestImageFactory<Color>.Blank(10, 20),
|
|
TestImageFactory<HalfVector4>.Blank(
|
|
10,
|
|
20)
|
|
};
|
|
|
|
|
|
[Theory]
|
|
[MemberData(nameof(BasicData))]
|
|
public void Blank_MemberData<TColor>(TestImageFactory<TColor> factory)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
var img = factory.Create();
|
|
|
|
Assert.True(img.Width * img.Height > 0);
|
|
}
|
|
|
|
public static readonly TheoryData<ITestImageFactory> FileData = new TheoryData<ITestImageFactory>()
|
|
{
|
|
TestImageFactory<Color>.File(
|
|
TestImages.Bmp.Car),
|
|
TestImageFactory<HalfVector4>.File(
|
|
TestImages.Bmp.F)
|
|
};
|
|
|
|
[Theory]
|
|
[MemberData(nameof(FileData))]
|
|
public void File_MemberData<TColor>(TestImageFactory<TColor> factory)
|
|
where TColor : struct, IPackedPixel, IEquatable<TColor>
|
|
{
|
|
this.Output.WriteLine("SRC: " + factory.Utility.SourceFileOrDescription);
|
|
this.Output.WriteLine("OUT: " + factory.Utility.GetTestOutputFileName());
|
|
|
|
var img = factory.Create();
|
|
|
|
Assert.True(img.Width * img.Height > 0);
|
|
}
|
|
}
|
|
}
|