diff --git a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs deleted file mode 100644 index b05d83204..000000000 --- a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.IO; -using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.IO; -using Moq; -using Xunit; - -namespace SixLabors.ImageSharp.Tests -{ - /// - /// Tests the class. - /// - public class DiscoverImageFormatTests - { - private readonly Mock fileSystem; - private readonly string FilePath; - private readonly IImageFormatDetector localMimeTypeDetector; - private readonly Mock localImageFormatMock; - - public IImageFormat localImageFormat => this.localImageFormatMock.Object; - public Configuration LocalConfiguration { get; private set; } - public byte[] Marker { get; private set; } - public MemoryStream DataStream { get; private set; } - public byte[] DecodedData { get; private set; } - private const string localMimeType = "image/local"; - - public DiscoverImageFormatTests() - { - this.localImageFormatMock = new Mock(); - - this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); - - this.fileSystem = new Mock(); - - this.LocalConfiguration = new Configuration - { - FileSystem = this.fileSystem.Object - }; - - this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); - - this.Marker = Guid.NewGuid().ToByteArray(); - this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); - - this.FilePath = Guid.NewGuid().ToString(); - this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); - - TestFileSystem.RegisterGlobalTestFormat(); - TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); - } - - [Fact] - public void DiscoverImageFormatByteArray() - { - IImageFormat type = Image.DetectFormat(this.DataStream.ToArray()); - Assert.Equal(TestFormat.GlobalTestFormat, type); - } - - [Fact] - public void DiscoverImageFormatByteArray_WithConfig() - { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream.ToArray()); - Assert.Equal(this.localImageFormat, type); - } - - [Fact] - public void DiscoverImageFormatFile() - { - IImageFormat type = Image.DetectFormat(this.FilePath); - Assert.Equal(TestFormat.GlobalTestFormat, type); - } - - [Fact] - public void DiscoverImageFormatFilePath_WithConfig() - { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.FilePath); - Assert.Equal(this.localImageFormat, type); - } - - [Fact] - public void DiscoverImageFormatStream() - { - IImageFormat type = Image.DetectFormat(this.DataStream); - Assert.Equal(TestFormat.GlobalTestFormat, type); - } - - [Fact] - public void DiscoverImageFormatFileStream_WithConfig() - { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream); - Assert.Equal(this.localImageFormat, type); - } - - [Fact] - public void DiscoverImageFormatNoDetectorsRegisterdShouldReturnNull() - { - IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream); - Assert.Null(type); - } - } -} diff --git a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs new file mode 100644 index 000000000..c067bf29f --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs @@ -0,0 +1,90 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.IO; +using Moq; +using Xunit; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ImageTests + { + /// + /// Tests the class. + /// + public class DetectFormat : ImageLoadTestBase + { + private static readonly string ActualImagePath = TestFile.GetInputFileFullPath(TestImages.Bmp.F); + + private byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes; + + private ReadOnlySpan ActualImageSpan => this.ActualImageBytes.AsSpan(); + + private byte[] ByteArray => this.DataStream.ToArray(); + + private ReadOnlySpan ByteSpan => this.ByteArray.AsSpan(); + + private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; + + private static readonly IImageFormat ExpectedGlobalFormat = + Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); + + [Fact] + public void FromBytes_GlobalConfiguration() + { + IImageFormat type = Image.DetectFormat(this.ActualImageBytes); + + Assert.Equal(ExpectedGlobalFormat, type); + } + + [Fact] + public void FromBytes_CustomConfiguration() + { + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.ByteArray); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void FromFileSystemPath_GlobalConfiguration() + { + IImageFormat type = Image.DetectFormat(ActualImagePath); + Assert.Equal(ExpectedGlobalFormat, type); + } + + [Fact] + public void FromFileSystemPath_CustomConfiguration() + { + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.MockFilePath); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void FromStream_GlobalConfiguration() + { + using (var stream = new MemoryStream(this.ActualImageBytes)) + { + IImageFormat type = Image.DetectFormat(stream); + Assert.Equal(ExpectedGlobalFormat, type); + } + } + + [Fact] + public void FromStream_CustomConfiguration() + { + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void WhenNoMatchingFormatFound_ReturnsNull() + { + IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream); + Assert.Null(type); + } + } + } +} diff --git a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs index 8e7d56dde..983d478cc 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs @@ -21,6 +21,12 @@ protected Mock localImageFormatMock; + protected readonly string MockFilePath = Guid.NewGuid().ToString(); + + internal readonly Mock localFileSystemMock = new Mock(); + + protected readonly TestFileSystem topLevelFileSystem = new TestFileSystem(); + public Configuration LocalConfiguration { get; } public TestFormat TestFormat { get; } = new TestFormat(); @@ -67,8 +73,12 @@ this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = this.TestFormat.CreateStream(this.Marker); - + this.localFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(this.DataStream); + this.topLevelFileSystem.AddFile(this.MockFilePath, this.DataStream); + this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object; + this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; } + public void Dispose() { // clean up the global object; diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs index 70e8f729f..1a21d3d10 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs @@ -16,23 +16,10 @@ namespace SixLabors.ImageSharp.Tests { public class Load_FileSystemPath : ImageLoadTestBase { - private readonly string filePath = Guid.NewGuid().ToString(); - private readonly Mock localFileSystemMock = new Mock(); - private readonly TestFileSystem topLevelFileSystem = new TestFileSystem(); - - public Load_FileSystemPath() - { - this.localFileSystemMock.Setup(x => x.OpenRead(this.filePath)).Returns(this.DataStream); - - this.topLevelFileSystem.AddFile(this.filePath, this.DataStream); - this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object; - this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; - } - [Fact] public void BasicCase() { - var img = Image.Load(this.TopLevelConfiguration, this.filePath); + var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath); Assert.NotNull(img); @@ -42,7 +29,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void UseLocalConfiguration() { - var img = Image.Load(this.LocalConfiguration, this.filePath); + var img = Image.Load(this.LocalConfiguration, this.MockFilePath); Assert.NotNull(img); @@ -52,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void UseCustomDecoder() { - var img = Image.Load(this.TopLevelConfiguration, this.filePath, this.localDecoder.Object); + var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, this.DataStream)); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs index b1041a93d..b23c9d67c 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs @@ -95,9 +95,11 @@ namespace SixLabors.ImageSharp.Tests [InlineData(true)] public void UseGlobalConfiguration(bool useSpan) { - Image img = useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes); - - Assert.Equal(new Size(108, 202), img.Size()); + using (Image img = + useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes)) + { + Assert.Equal(new Size(108, 202), img.Size()); + } } [Theory] @@ -105,11 +107,12 @@ namespace SixLabors.ImageSharp.Tests [InlineData(true)] public void UseGlobalConfiguration_NonDefaultPixelType(bool useSpan) { - Image img = useSpan - ? Image.Load(this.ActualImageSpan) - : Image.Load(this.ActualImageBytes); - - Assert.Equal(new Size(108, 202), img.Size()); + using (Image img = useSpan + ? Image.Load(this.ActualImageSpan) + : Image.Load(this.ActualImageBytes)) + { + Assert.Equal(new Size(108, 202), img.Size()); + } } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs index 1b880a461..7664c88d9 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs @@ -11,6 +11,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { + using SixLabors.Primitives; + public partial class ImageTests { /// @@ -29,6 +31,18 @@ namespace SixLabors.ImageSharp.Tests this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); } + [Fact] + public void UseGlobalConfiguration() + { + byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes; + + using (var stream = new MemoryStream(data)) + using (var img = Image.Load(stream)) + { + Assert.Equal(new Size(108, 202), img.Size()); + } + } + [Fact] public void NonDefaultPixelTypeImage() {