Browse Source

ensure all save operations pass along IEncoderOptions

af/merge-core
Scott Williams 9 years ago
parent
commit
a5ef9520a2
  1. 2
      src/ImageSharp/Configuration.cs
  2. 10
      src/ImageSharp/IO/IFileSystem.cs
  3. 19
      src/ImageSharp/IO/LocalFileSystem.cs
  4. 14
      src/ImageSharp/Image/Image{TColor}.cs
  5. 2
      tests/ImageSharp.Tests/ConfigurationTests.cs
  6. 6
      tests/ImageSharp.Tests/IO/LocalFileSystem.cs
  7. 163
      tests/ImageSharp.Tests/Image/ImageSaveTests.cs
  8. 9
      tests/ImageSharp.Tests/Image/SaveWatchingImage.cs

2
src/ImageSharp/Configuration.cs

@ -56,7 +56,7 @@ namespace ImageSharp
#if !NETSTANDARD1_1
/// <summary>
/// Helper for accessing the local file system.
/// Gets or sets the fielsystem helper for accessing the local file system.
/// </summary>
internal IFileSystem FileSystem { get; set; } = new LocalFileSystem();
#endif

10
src/ImageSharp/IO/IFileSystem.cs

@ -1,13 +1,13 @@
// <copyright file="Endianness.cs" company="James Jackson-South">
// <copyright file="IFileSystem.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
using System.IO;
namespace ImageSharp.IO
{
#if !NETSTANDARD1_1
using System.IO;
#if !NETSTANDARD1_1
/// <summary>
/// A simple interface representing the filesystem.
/// </summary>
@ -25,7 +25,7 @@ namespace ImageSharp.IO
/// </summary>
/// <param name="path">Path to the file to open.</param>
/// <returns>A stream representing the file to open.</returns>
Stream OpenWrite(string path);
Stream Create(string path);
}
#endif
}

19
src/ImageSharp/IO/LocalFileSystem.cs

@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
// <copyright file="LocalFileSystem.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.IO
{
#if !NETSTANDARD1_1
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
#if !NETSTANDARD1_1
/// <summary>
/// A wrapper around the local File apis.
/// </summary>
@ -18,9 +23,9 @@ namespace ImageSharp.IO
}
/// <inheritdoc/>
public Stream OpenWrite(string path)
public Stream Create(string path)
{
return File.OpenWrite(path);
return File.Create(path);
}
}
#endif

14
src/ImageSharp/Image/Image{TColor}.cs

@ -168,7 +168,8 @@ namespace ImageSharp
: base(configuration)
{
Guard.NotNull(filePath, nameof(filePath));
using (Stream fs = File.OpenRead(filePath))
configuration = configuration ?? Configuration.Default;
using (Stream fs = configuration.FileSystem.OpenRead(filePath))
{
this.Load(fs, options);
}
@ -439,7 +440,7 @@ namespace ImageSharp
throw new InvalidOperationException($"No image formats have been registered for the file extension '{ext}'.");
}
return this.Save(filePath, format);
return this.Save(filePath, format, options);
}
/// <summary>
@ -465,10 +466,7 @@ namespace ImageSharp
public Image<TColor> Save(string filePath, IImageFormat format, IEncoderOptions options)
{
Guard.NotNull(format, nameof(format));
using (FileStream fs = File.Create(filePath))
{
return this.Save(fs, format);
}
return this.Save(filePath, format.Encoder, options);
}
/// <summary>
@ -494,9 +492,9 @@ namespace ImageSharp
public Image<TColor> Save(string filePath, IImageEncoder encoder, IEncoderOptions options)
{
Guard.NotNull(encoder, nameof(encoder));
using (FileStream fs = File.Create(filePath))
using (Stream fs = this.Configuration.FileSystem.Create(filePath))
{
return this.Save(fs, encoder);
return this.Save(fs, encoder, options);
}
}
#endif

2
tests/ImageSharp.Tests/ConfigurationTests.cs

@ -21,7 +21,7 @@ namespace ImageSharp.Tests
[Fact]
public void DefaultsToLocalFileSystem()
{
var configuration = Configuration.CreateDefaultInstance();
Configuration configuration = Configuration.CreateDefaultInstance();
ImageSharp.IO.IFileSystem fs = configuration.FileSystem;

6
tests/ImageSharp.Tests/IO/LocalFileSystem.cs

@ -22,7 +22,7 @@ namespace ImageSharp.Tests.IO
LocalFileSystem fs = new LocalFileSystem();
using (var r = new StreamReader(fs.OpenRead(path)))
using (StreamReader r = new StreamReader(fs.OpenRead(path)))
{
string data = r.ReadToEnd();
@ -33,13 +33,13 @@ namespace ImageSharp.Tests.IO
}
[Fact]
public void OpenWrite()
public void Create()
{
string path = Path.GetTempFileName();
string testData = Guid.NewGuid().ToString();
LocalFileSystem fs = new LocalFileSystem();
using (var r = new StreamWriter(fs.OpenWrite(path)))
using (StreamWriter r = new StreamWriter(fs.Create(path)))
{
r.Write(testData);
}

163
tests/ImageSharp.Tests/Image/ImageSaveTests.cs

@ -20,10 +20,12 @@ namespace ImageSharp.Tests
{
private readonly SaveWatchingImage Image;
private readonly Mock<IFileSystem> fileSystem;
private readonly IEncoderOptions encoderOptions;
public ImageSaveTests()
{
this.fileSystem = new Mock<IFileSystem>();
this.encoderOptions = new Mock<IEncoderOptions>().Object;
this.Image = new SaveWatchingImage(1, 1, this.fileSystem.Object);
}
@ -31,15 +33,170 @@ namespace ImageSharp.Tests
public void SavePath()
{
Stream stream = new MemoryStream();
this.fileSystem.Setup(x => x.OpenWrite("path")).Returns(stream);
this.Image.Save("path");
this.fileSystem.Setup(x => x.Create("path.png")).Returns(stream);
this.Image.Save("path.png");
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.Equal(this.Image.CurrentImageFormat.Encoder, operation.encoder);
Assert.IsType<PngEncoder>(operation.encoder);
Assert.Null(operation.options);
}
[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);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<JpegEncoder>(operation.encoder);
Assert.Equal(this.encoderOptions, operation.options);
}
[Fact]
public void SavePathWithEncoder()
{
Stream stream = new MemoryStream();
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream);
this.Image.Save("path.jpg", new BmpEncoder());
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<BmpEncoder>(operation.encoder);
Assert.Null(operation.options);
}
[Fact]
public void SavePathWithEncoderAndOptions()
{
Stream stream = new MemoryStream();
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream);
this.Image.Save("path.jpg", new BmpEncoder(), this.encoderOptions);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<BmpEncoder>(operation.encoder);
Assert.Equal(this.encoderOptions, operation.options);
}
[Fact]
public void SavePathWithFormat()
{
Stream stream = new MemoryStream();
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream);
this.Image.Save("path.jpg", new GifFormat());
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<GifEncoder>(operation.encoder);
Assert.Null(operation.options);
}
[Fact]
public void SavePathWithFormatAndOptions()
{
Stream stream = new MemoryStream();
this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream);
this.Image.Save("path.jpg", new BmpFormat(), this.encoderOptions);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<BmpEncoder>(operation.encoder);
Assert.Equal(this.encoderOptions, operation.options);
}
/// <summary>
/// /////////////////////////////////////////////////////////////
/// </summary>
///
[Fact]
public void SaveStream()
{
Stream stream = new MemoryStream();
this.Image.Save(stream);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType(this.Image.CurrentImageFormat.Encoder.GetType(), operation.encoder);
Assert.Null(operation.options);
}
[Fact]
public void SaveStreamWithOptions()
{
Stream stream = new MemoryStream();
this.Image.Save(stream, this.encoderOptions);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType(this.Image.CurrentImageFormat.Encoder.GetType(), operation.encoder);
Assert.Equal(this.encoderOptions, operation.options);
}
[Fact]
public void SaveStreamWithEncoder()
{
Stream stream = new MemoryStream();
this.Image.Save(stream, new BmpEncoder());
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<BmpEncoder>(operation.encoder);
Assert.Null(operation.options);
}
[Fact]
public void SaveStreamWithEncoderAndOptions()
{
Stream stream = new MemoryStream();
this.Image.Save(stream, new BmpEncoder(), this.encoderOptions);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<BmpEncoder>(operation.encoder);
Assert.Equal(this.encoderOptions, operation.options);
}
[Fact]
public void SaveStreamWithFormat()
{
Stream stream = new MemoryStream();
this.Image.Save(stream, new GifFormat());
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<GifEncoder>(operation.encoder);
Assert.Null(operation.options);
}
[Fact]
public void SaveStreamWithFormatAndOptions()
{
Stream stream = new MemoryStream();
this.Image.Save(stream, new BmpFormat(), this.encoderOptions);
SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single();
Assert.Equal(stream, operation.stream);
Assert.IsType<BmpEncoder>(operation.encoder);
Assert.Equal(this.encoderOptions, operation.options);
}
public void Dispose()
{
this.Image.Dispose();

9
tests/ImageSharp.Tests/Image/SaveWatchingImage.cs

@ -16,7 +16,7 @@ namespace ImageSharp.Tests
public class SaveWatchingImage : Image<Color>
{
public List<OperationDetails> Saves { get; } = new List<OperationDetails>();
public SaveWatchingImage(int width, int height, IFileSystem fs = null)
: base(width, height, Configuration.CreateDefaultInstance())
{
@ -26,7 +26,12 @@ namespace ImageSharp.Tests
internal override void SaveInternal(Stream stream, IImageEncoder encoder, IEncoderOptions options)
{
this.Saves.Add(new OperationDetails
{
encoder = encoder,
options = options,
stream = stream
});
}
public struct OperationDetails

Loading…
Cancel
Save