// // 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 Moq; using Xunit; /// /// Tests the class. /// public class ImageSaveTests : IDisposable { private readonly Image Image; private readonly Mock fileSystem; private readonly Mock format; private readonly Mock formatNotRegistered; private readonly Mock encoder; private readonly Mock encoderNotInFormat; private readonly IEncoderOptions encoderOptions; public ImageSaveTests() { this.encoder = new Mock(); this.format = new Mock(); this.format.Setup(x => x.Encoder).Returns(this.encoder.Object); this.format.Setup(x => x.Decoder).Returns(new Mock().Object); this.format.Setup(x => x.MimeType).Returns("img/test"); this.format.Setup(x => x.Extension).Returns("png"); this.format.Setup(x => x.SupportedExtensions).Returns(new string[] { "png", "jpg" }); this.encoderNotInFormat = new Mock(); this.formatNotRegistered = new Mock(); this.formatNotRegistered.Setup(x => x.Encoder).Returns(this.encoderNotInFormat.Object); this.formatNotRegistered.Setup(x => x.Decoder).Returns(new Mock().Object); this.formatNotRegistered.Setup(x => x.MimeType).Returns("img/test"); this.formatNotRegistered.Setup(x => x.Extension).Returns("png"); this.formatNotRegistered.Setup(x => x.SupportedExtensions).Returns(new string[] { "png", "jpg" }); this.fileSystem = new Mock(); this.encoderOptions = new Mock().Object; this.Image = new Image(1, 1, new Configuration(this.format.Object) { FileSystem = this.fileSystem.Object }); } [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, null)); } [Fact] public void SavePathWithOptions() { Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); this.Image.Save("path.jpg", this.encoderOptions); this.encoder.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [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, null)); } [Fact] public void SavePathWithEncoderAndOptions() { Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); this.Image.Save("path.jpg", this.encoderNotInFormat.Object, this.encoderOptions); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] public void SavePathWithFormat() { 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, null)); } [Fact] public void SavePathWithFormatAndOptions() { Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); this.Image.Save("path.jpg", this.encoderNotInFormat.Object, this.encoderOptions); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] public void SaveStream() { Stream stream = new MemoryStream(); this.Image.Save(stream); this.encoder.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] public void SaveStreamWithOptions() { Stream stream = new MemoryStream(); this.Image.Save(stream, this.encoderOptions); this.encoder.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] public void SaveStreamWithEncoder() { Stream stream = new MemoryStream(); this.Image.Save(stream, this.encoderNotInFormat.Object); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] public void SaveStreamWithEncoderAndOptions() { Stream stream = new MemoryStream(); this.Image.Save(stream, this.encoderNotInFormat.Object, this.encoderOptions); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] public void SaveStreamWithFormat() { Stream stream = new MemoryStream(); this.Image.Save(stream, this.formatNotRegistered.Object); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] public void SaveStreamWithFormatAndOptions() { Stream stream = new MemoryStream(); this.Image.Save(stream, this.formatNotRegistered.Object, this.encoderOptions); this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } public void Dispose() { this.Image.Dispose(); } } }