// // Copyright (c) James Jackson-South and contributors. // 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; public class TestImageProviderTests { public TestImageProviderTests(ITestOutputHelper output) { this.Output = output; } private ITestOutputHelper Output { get; } [Theory] [WithBlankImages(42, 666, PixelTypes.Rgba32 | PixelTypes.Argb32 | PixelTypes.HalfSingle, "hello")] public void Use_WithEmptyImageAttribute(TestImageProvider provider, string message) where TPixel : struct, IPixel { Image img = provider.GetImage(); 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( TestImageProvider provider, string message) where TPixel : struct, IPixel { Image img = provider.GetImage(); Assert.Equal(42, img.Width); Assert.Equal(666, img.Height); Assert.Equal("hello", message); } [Theory] [WithBlankImages(1, 1, PixelTypes.Rgba32, PixelTypes.Rgba32)] [WithBlankImages(1, 1, PixelTypes.Alpha8, PixelTypes.Alpha8)] [WithBlankImages(1, 1, PixelTypes.Rgba32, PixelTypes.Rgba32)] public void PixelType_PropertyValueIsCorrect(TestImageProvider provider, PixelTypes expected) where TPixel : struct, IPixel { Assert.Equal(expected, provider.PixelType); } [Theory] [WithBlankImages(1, 1, PixelTypes.Rgba32)] [WithFile(TestImages.Bmp.F, PixelTypes.Rgba32)] public void PixelTypes_ColorWithDefaultImageClass_TriggersCreatingTheNonGenericDerivedImageClass( TestImageProvider provider) where TPixel : struct, IPixel { Image img = provider.GetImage(); Assert.IsType>(img); } [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.All, 88)] [WithFile(TestImages.Bmp.F, PixelTypes.All, 88)] public void Use_WithFileAttribute(TestImageProvider provider, int yo) where TPixel : struct, IPixel { Assert.NotNull(provider.Utility.SourceFileOrDescription); Image img = provider.GetImage(); Assert.True(img.Width * img.Height > 0); Assert.Equal(88, yo); string fn = provider.Utility.GetTestOutputFileName("jpg"); 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] [WithFileCollection(nameof(AllBmpFiles), PixelTypes.Rgba32 | PixelTypes.Argb32)] public void Use_WithFileCollection(TestImageProvider provider) where TPixel : struct, IPixel { Assert.NotNull(provider.Utility.SourceFileOrDescription); 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) where TPixel : struct, IPixel { Image img = provider.GetImage(); Assert.Equal(10, img.Width); Assert.Equal(20, img.Height); byte[] colors = new byte[4]; using (PixelAccessor pixels = img.Lock()) { for (int y = 0; y < pixels.Height; y++) { for (int x = 0; x < pixels.Width; x++) { pixels[x, y].ToXyzwBytes(colors, 0); Assert.Equal(255, colors[0]); Assert.Equal(100, colors[1]); Assert.Equal(50, colors[2]); Assert.Equal(200, colors[3]); } } } } /// /// Need to us to create instance of when pixelType is StandardImageClass /// /// /// /// public static Image CreateTestImage() where TPixel : struct, IPixel { return new Image(3, 3); } [Theory] [WithMemberFactory(nameof(CreateTestImage), PixelTypes.All)] public void Use_WithMemberFactoryAttribute(TestImageProvider provider) where TPixel : struct, IPixel { Image img = provider.GetImage(); Assert.Equal(3, img.Width); if (provider.PixelType == PixelTypes.Rgba32) { Assert.IsType>(img); } } public static readonly TheoryData BasicData = new TheoryData() { TestImageProvider.Blank(10, 20), TestImageProvider.Blank( 10, 20), }; [Theory] [MemberData(nameof(BasicData))] public void Blank_MemberData(TestImageProvider provider) where TPixel : struct, IPixel { Image img = provider.GetImage(); Assert.True(img.Width * img.Height > 0); } public static readonly TheoryData FileData = new TheoryData() { TestImageProvider.File( TestImages.Bmp.Car), TestImageProvider.File( TestImages.Bmp.F) }; [Theory] [MemberData(nameof(FileData))] public void File_MemberData(TestImageProvider provider) where TPixel : struct, IPixel { this.Output.WriteLine("SRC: " + provider.Utility.SourceFileOrDescription); this.Output.WriteLine("OUT: " + provider.Utility.GetTestOutputFileName()); Image img = provider.GetImage(); Assert.True(img.Width * img.Height > 0); } } }