// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System; using System.IO; using System.Threading; using Moq; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Tests { public partial class ImageTests { public abstract class ImageLoadTestBase : IDisposable { private Lazy dataStreamLazy; protected Image localStreamReturnImageRgba32; protected Image localStreamReturnImageAgnostic; protected Mock localDecoder; protected IImageFormatDetector localMimeTypeDetector; protected Mock localImageFormatMock; protected Mock localImageInfoMock; 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(); /// /// Gets the top-level configuration in the context of this test case. /// It has registered. /// public Configuration TopLevelConfiguration { get; } public byte[] Marker { get; } public Stream DataStream => this.dataStreamLazy.Value; public byte[] DecodedData { get; private set; } protected byte[] ByteArray => ((MemoryStream)this.DataStream).ToArray(); protected ImageLoadTestBase() { this.localStreamReturnImageRgba32 = new Image(1, 1); this.localStreamReturnImageAgnostic = new Image(1, 1); this.localImageInfoMock = new Mock(); this.localImageFormatMock = new Mock(); var detector = new Mock(); detector.Setup(x => x.Identify(It.IsAny(), It.IsAny())).Returns(this.localImageInfoMock.Object); detector.Setup(x => x.IdentifyAsync(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(this.localImageInfoMock.Object); this.localDecoder = detector.As(); this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) .Callback((c, s) => { using (var ms = new MemoryStream()) { s.CopyTo(ms); this.DecodedData = ms.ToArray(); } }) .Returns(this.localStreamReturnImageRgba32); this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) .Callback((c, s) => { using (var ms = new MemoryStream()) { s.CopyTo(ms); this.DecodedData = ms.ToArray(); } }) .Returns(this.localStreamReturnImageAgnostic); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.LocalConfiguration = new Configuration(); this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); this.LocalConfiguration.ImageFormatsManager.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); this.TopLevelConfiguration = new Configuration(this.TestFormat); this.Marker = Guid.NewGuid().ToByteArray(); this.dataStreamLazy = new Lazy(this.CreateStream); Stream StreamFactory() => this.DataStream; this.LocalFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(StreamFactory); this.topLevelFileSystem.AddFile(this.MockFilePath, StreamFactory); this.LocalConfiguration.FileSystem = this.LocalFileSystemMock.Object; this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; } public void Dispose() { // Clean up the global object; this.localStreamReturnImageRgba32?.Dispose(); this.localStreamReturnImageAgnostic?.Dispose(); } protected virtual Stream CreateStream() => this.TestFormat.CreateStream(this.Marker); } } }