From 469d40494432d4143abb67f5e1234ad139233f64 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 18 Mar 2017 09:18:12 +0000 Subject: [PATCH] drop callbacks test by mocking encoder --- src/ImageSharp/Configuration.cs | 19 +++ src/ImageSharp/Image/IImageCallbacks.cs | 46 ------- src/ImageSharp/Image/ImageBase{TColor}.cs | 12 +- src/ImageSharp/Image/Image{TColor}.cs | 5 +- .../Drawing/Paths/ProcessorWatchingImage.cs | 13 +- .../ImageSharp.Tests/Image/ImageSaveTests.cs | 113 ++++++++---------- .../Image/SaveWatchingImage.cs | 53 -------- 7 files changed, 71 insertions(+), 190 deletions(-) delete mode 100644 src/ImageSharp/Image/IImageCallbacks.cs delete mode 100644 tests/ImageSharp.Tests/Image/SaveWatchingImage.cs diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index e0eb21865e..fa983d3557 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -34,6 +34,25 @@ namespace ImageSharp /// private readonly List imageFormatsList = new List(); + /// + /// Initializes a new instance of the class. + /// + public Configuration() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The inital set of image formats. + public Configuration(params IImageFormat[] providers) + { + foreach (IImageFormat p in providers) + { + this.AddImageFormat(p); + } + } + /// /// Gets the default instance. /// diff --git a/src/ImageSharp/Image/IImageCallbacks.cs b/src/ImageSharp/Image/IImageCallbacks.cs deleted file mode 100644 index 93267de057..0000000000 --- a/src/ImageSharp/Image/IImageCallbacks.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Text; - using ImageSharp.Processing; - - /// - /// Provides a set of methods that are called as part of the images lifetime/processes. - /// - internal interface IImageCallbacks - { - /// - /// Invoked before the image is saved. - /// - /// The color - /// The image - /// The destination stream - /// The encoder - /// The options - /// - /// return true if the processor should be applied otherwise false. - /// - bool OnSaving(ImageBase image, Stream stream, Formats.IImageEncoder encoder, IEncoderOptions options) - where TColor : struct, IPixel; - - /// - /// Invoked before the image is processed. - /// - /// The color - /// The image - /// The processor. - /// The rectangle. - /// - /// return true if the processor should be applied otherwise false. - /// - bool OnProcessing(ImageBase image, IImageProcessor processor, Rectangle rectangle) - where TColor : struct, IPixel; - } -} diff --git a/src/ImageSharp/Image/ImageBase{TColor}.cs b/src/ImageSharp/Image/ImageBase{TColor}.cs index f905123132..878ba09b39 100644 --- a/src/ImageSharp/Image/ImageBase{TColor}.cs +++ b/src/ImageSharp/Image/ImageBase{TColor}.cs @@ -116,11 +116,6 @@ namespace ImageSharp /// public Configuration Configuration { get; private set; } - /// - /// Gets or sets the callbacks item which will be called during the lifetime of the image being processed. - /// - internal IImageCallbacks Callbacks { get; set; } - /// /// Applies the processor. /// @@ -128,12 +123,7 @@ namespace ImageSharp /// The rectangle. public virtual void ApplyProcessor(IImageProcessor processor, Rectangle rectangle) { - // this will be null true or false, if its null or true then apply the processor - // thus is not false then apply the processors (allows for tests to save time and not actually run the prcessor is required) - if (this.Callbacks?.OnProcessing(this, processor, rectangle) != false) - { - processor.Apply(this, rectangle); - } + processor.Apply(this, rectangle); } /// diff --git a/src/ImageSharp/Image/Image{TColor}.cs b/src/ImageSharp/Image/Image{TColor}.cs index 8e33715bb8..3a76cf63ff 100644 --- a/src/ImageSharp/Image/Image{TColor}.cs +++ b/src/ImageSharp/Image/Image{TColor}.cs @@ -594,10 +594,7 @@ namespace ImageSharp Guard.NotNull(stream, nameof(stream)); Guard.NotNull(encoder, nameof(encoder)); - if (this.Callbacks?.OnSaving(this, stream, encoder, options) != false) - { - encoder.Encode(this, stream, options); - } + encoder.Encode(this, stream, options); } /// diff --git a/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs b/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs index ff4432fd16..2d3d2cc2b8 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs @@ -12,29 +12,22 @@ namespace ImageSharp.Tests.Drawing.Paths /// Watches but does not actually run the processors against the image. /// /// - public class ProcessorWatchingImage : Image, IImageCallbacks + public class ProcessorWatchingImage : Image { public List ProcessorApplications { get; } = new List(); public ProcessorWatchingImage(int width, int height) : base(width, height, Configuration.CreateDefaultInstance()) { - this.Callbacks = this; } - public bool OnSaving(ImageBase image, Stream stream, IImageEncoder encoder, IEncoderOptions options) where TColor : struct, IPixel - { - return true; - } - - public bool OnProcessing(ImageBase image, IImageProcessor processor, Rectangle rectangle) where TColor : struct, IPixel + public override void ApplyProcessor(IImageProcessor processor, Rectangle rectangle) { this.ProcessorApplications.Add(new ProcessorDetails { - processor = (IImageProcessor)processor, + processor = processor, rectangle = rectangle }); - return false;// do not really apply the processor to speed up testing } public struct ProcessorDetails diff --git a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs index 172a14dc2a..0d1c3e09b5 100644 --- a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs @@ -18,15 +18,38 @@ namespace ImageSharp.Tests /// public class ImageSaveTests : IDisposable { - private readonly SaveWatchingImage Image; + 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 SaveWatchingImage(1, 1, this.fileSystem.Object); + this.Image = new Image(1, 1, new Configuration(this.format.Object) { + FileSystem = this.fileSystem.Object + }); } [Fact] @@ -36,10 +59,7 @@ namespace ImageSharp.Tests 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.IsType(operation.encoder); - Assert.Null(operation.options); + this.encoder.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] @@ -49,11 +69,8 @@ namespace ImageSharp.Tests 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(operation.encoder); - Assert.Equal(this.encoderOptions, operation.options); + + this.encoder.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] @@ -62,12 +79,9 @@ namespace ImageSharp.Tests Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); - this.Image.Save("path.jpg", new BmpEncoder()); + this.Image.Save("path.jpg", this.encoderNotInFormat.Object); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Null(operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] @@ -76,12 +90,9 @@ namespace ImageSharp.Tests Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); - this.Image.Save("path.jpg", new BmpEncoder(), this.encoderOptions); + this.Image.Save("path.jpg", this.encoderNotInFormat.Object, this.encoderOptions); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Equal(this.encoderOptions, operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } @@ -92,12 +103,9 @@ namespace ImageSharp.Tests Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); - this.Image.Save("path.jpg", new GifFormat()); + this.Image.Save("path.jpg", this.encoderNotInFormat.Object); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Null(operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] @@ -106,29 +114,18 @@ namespace ImageSharp.Tests Stream stream = new MemoryStream(); this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); - this.Image.Save("path.jpg", new BmpFormat(), this.encoderOptions); + this.Image.Save("path.jpg", this.encoderNotInFormat.Object, this.encoderOptions); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Equal(this.encoderOptions, operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } - /// - /// ///////////////////////////////////////////////////////////// - /// - /// - [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); + + this.encoder.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] @@ -138,11 +135,7 @@ namespace ImageSharp.Tests 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); + this.encoder.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] @@ -150,12 +143,9 @@ namespace ImageSharp.Tests { Stream stream = new MemoryStream(); - this.Image.Save(stream, new BmpEncoder()); + this.Image.Save(stream, this.encoderNotInFormat.Object); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Null(operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] @@ -163,12 +153,9 @@ namespace ImageSharp.Tests { Stream stream = new MemoryStream(); - this.Image.Save(stream, new BmpEncoder(), this.encoderOptions); + this.Image.Save(stream, this.encoderNotInFormat.Object, this.encoderOptions); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Equal(this.encoderOptions, operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } [Fact] @@ -176,12 +163,9 @@ namespace ImageSharp.Tests { Stream stream = new MemoryStream(); - this.Image.Save(stream, new GifFormat()); + this.Image.Save(stream, this.formatNotRegistered.Object); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Null(operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, null)); } [Fact] @@ -189,12 +173,9 @@ namespace ImageSharp.Tests { Stream stream = new MemoryStream(); - this.Image.Save(stream, new BmpFormat(), this.encoderOptions); + this.Image.Save(stream, this.formatNotRegistered.Object, this.encoderOptions); - SaveWatchingImage.OperationDetails operation = this.Image.Saves.Single(); - Assert.Equal(stream, operation.stream); - Assert.IsType(operation.encoder); - Assert.Equal(this.encoderOptions, operation.options); + this.encoderNotInFormat.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } public void Dispose() diff --git a/tests/ImageSharp.Tests/Image/SaveWatchingImage.cs b/tests/ImageSharp.Tests/Image/SaveWatchingImage.cs deleted file mode 100644 index 0436146913..0000000000 --- a/tests/ImageSharp.Tests/Image/SaveWatchingImage.cs +++ /dev/null @@ -1,53 +0,0 @@ - -namespace ImageSharp.Tests -{ - using System; - using System.IO; - using ImageSharp; - using Processing; - using System.Collections.Generic; - using ImageSharp.Formats; - using ImageSharp.IO; - - /// - /// Watches but does not actually run the processors against the image. - /// - /// - public class SaveWatchingImage : Image, IImageCallbacks - { - public List Saves { get; } = new List(); - - public SaveWatchingImage(int width, int height, IFileSystem fs = null) - : base(width, height, Configuration.CreateDefaultInstance()) - { - //switch out the file system for tests - this.Configuration.FileSystem = fs ?? this.Configuration.FileSystem; - - this.Callbacks = this; - } - - public bool OnSaving(ImageBase image, Stream stream, IImageEncoder encoder, IEncoderOptions options) where TColor : struct, IPixel - { - this.Saves.Add(new OperationDetails - { - encoder = encoder, - options = options, - stream = stream - }); - - return false; - } - - public bool OnProcessing(ImageBase image, IImageProcessor processor, Rectangle rectangle) where TColor : struct, IPixel - { - return false; - } - - public struct OperationDetails - { - public Stream stream; - public IImageEncoder encoder; - public IEncoderOptions options; - } - } -}