mirror of https://github.com/SixLabors/ImageSharp
6 changed files with 129 additions and 129 deletions
@ -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 |
|||
{ |
|||
/// <summary>
|
|||
/// Tests the <see cref="Image"/> class.
|
|||
/// </summary>
|
|||
public class DiscoverImageFormatTests |
|||
{ |
|||
private readonly Mock<IFileSystem> fileSystem; |
|||
private readonly string FilePath; |
|||
private readonly IImageFormatDetector localMimeTypeDetector; |
|||
private readonly Mock<IImageFormat> 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<IImageFormat>(); |
|||
|
|||
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); |
|||
|
|||
this.fileSystem = new Mock<IFileSystem>(); |
|||
|
|||
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); |
|||
} |
|||
} |
|||
} |
|||
@ -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 |
|||
{ |
|||
/// <summary>
|
|||
/// Tests the <see cref="Image"/> class.
|
|||
/// </summary>
|
|||
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<byte> ActualImageSpan => this.ActualImageBytes.AsSpan(); |
|||
|
|||
private byte[] ByteArray => this.DataStream.ToArray(); |
|||
|
|||
private ReadOnlySpan<byte> 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); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue