// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using System; using System.IO; using System.Linq; using ImageSharp.Formats; using ImageSharp.IO; using ImageSharp.PixelFormats; using Moq; using Xunit; /// /// Tests the class. /// public class ImageSaveTests : IDisposable { private readonly Image Image; private readonly Mock fileSystem; private readonly Mock encoder; private readonly Mock encoderNotInFormat; private Mock localMimeTypeDetector; public ImageSaveTests() { this.localMimeTypeDetector = new Mock(); this.localMimeTypeDetector.Setup(x => x.HeaderSize).Returns(1); this.localMimeTypeDetector.Setup(x => x.DetectMimeType(It.IsAny>())).Returns("img/test"); this.encoder = new Mock(); this.encoderNotInFormat = new Mock(); this.fileSystem = new Mock(); var config = new Configuration() { FileSystem = this.fileSystem.Object }; config.AddMimeTypeDetector(this.localMimeTypeDetector.Object); config.SetMimeTypeEncoder("img/test", this.encoder.Object); config.SetFileExtensionToMimeTypeMapping("png", "img/test"); this.Image = new Image(config, 1, 1); } [Fact] public void SavePath() { Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.png")).Returns(stream); this.Image.Save("path.png"); this.encoder.Verify(x => x.Encode(this.Image, stream)); } [Fact] public void SavePathWithEncoder() { Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); this.Image.Save("path.jpg", this.encoderNotInFormat.Object); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream)); } [Fact] public void ToBase64String() { var str = this.Image.ToBase64String("img/test"); this.encoder.Verify(x => x.Encode(this.Image, It.IsAny())); } [Fact] public void SaveStreamWithMime() { Stream stream = new MemoryStream(); this.Image.Save(stream, "img/test"); this.encoder.Verify(x => x.Encode(this.Image, stream)); } [Fact] public void SaveStreamWithEncoder() { Stream stream = new MemoryStream(); this.Image.Save(stream, this.encoderNotInFormat.Object); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream)); } public void Dispose() { this.Image.Dispose(); } } }