-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from kamsar/feature/avif
Avif support. Only works on async mode at the moment, some issue with…
- Loading branch information
Showing
44 changed files
with
1,968 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Specialized; | ||
using System.Web; | ||
using Dianoga.NextGenFormats; | ||
using FluentAssertions; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Dianoga.Tests.NextGenFormats | ||
{ | ||
public class HelpersTests | ||
{ | ||
[Fact] | ||
public void GetSupportedFormats_ShouldCallAndReturnValueFromdianogaGetSupportedFormatsPipeline() | ||
{ | ||
//Arrange | ||
var context = new Mock<HttpContextBase>(); | ||
var request = new Mock<HttpRequestBase>(); | ||
var acceptTypes = new[] {"image/webp"}; | ||
request.SetupGet(r => r.AcceptTypes).Returns(acceptTypes); | ||
context.Setup(ctx => ctx.Request).Returns(request.Object); | ||
var helpers = new Helpers(); | ||
var pipelineHelpers = new Mock<PipelineHelpers>(); | ||
pipelineHelpers.Setup(h => h.RunDianogaGetSupportedFormatsPipeline(It.IsAny<string[]>())).Returns("webp").Verifiable(); | ||
helpers.PipelineHelpers = pipelineHelpers.Object; | ||
|
||
//Act | ||
var result = helpers.GetSupportedFormats(context.Object); | ||
|
||
//Assert | ||
result.Should().Be("webp", ""); | ||
pipelineHelpers.Verify(m=>m.RunDianogaGetSupportedFormatsPipeline(acceptTypes), Times.Once()); | ||
} | ||
|
||
[Fact] | ||
public void GetSupportedFormats_ShouldNotCallDianogaGetSupportedFormatsPipeline_WhenNoAcceptTypes() | ||
{ | ||
//Arrange | ||
var context = new Mock<HttpContextBase>(); | ||
var request = new Mock<HttpRequestBase>(); | ||
context.Setup(ctx => ctx.Request).Returns(request.Object); | ||
var helpers = new Helpers(); | ||
var pipelineHelpers = new Mock<PipelineHelpers>(); | ||
helpers.PipelineHelpers = pipelineHelpers.Object; | ||
|
||
//Act | ||
var result = helpers.GetSupportedFormats(context.Object); | ||
|
||
//Assert | ||
result.Should().Be(String.Empty, ""); | ||
pipelineHelpers.Verify(m => m.RunDianogaGetSupportedFormatsPipeline(It.IsAny<string[]>()), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public void GetCustomOptions_ShouldTakeValueFromQueryString_WhenExtensionQueryStringIsPresent() | ||
{ | ||
//Arrange | ||
var context = new Mock<HttpContextBase>(); | ||
var request = new Mock<HttpRequestBase>(); | ||
context.Setup(ctx => ctx.Request).Returns(request.Object); | ||
var acceptTypes = new[] { "image/webp" }; | ||
request.SetupGet(r => r.AcceptTypes).Returns(acceptTypes); | ||
var queryString = new NameValueCollection(); | ||
queryString.Add("extension", "webp,avif"); | ||
request.SetupGet(r => r.QueryString).Returns(queryString); | ||
var helpers = new Helpers(); | ||
var pipelineHelpers = new Mock<PipelineHelpers>(); | ||
helpers.PipelineHelpers = pipelineHelpers.Object; | ||
|
||
//Act | ||
var result = helpers.GetCustomOptions(context.Object); | ||
|
||
//Assert | ||
result.Should().Be("webp,avif", ""); | ||
pipelineHelpers.Verify(m => m.RunDianogaGetSupportedFormatsPipeline(It.IsAny<string[]>()), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public void GetCustomOptions_ShouldTakeValueFromFromAcceptTypes_WhenExtensionQueryStringIsEmpty() | ||
{ | ||
//Arrange | ||
var context = new Mock<HttpContextBase>(); | ||
var request = new Mock<HttpRequestBase>(); | ||
context.Setup(ctx => ctx.Request).Returns(request.Object); | ||
var acceptTypes = new[] { "image/webp", "image/avif" }; | ||
request.SetupGet(r => r.AcceptTypes).Returns(acceptTypes); | ||
var queryString = new NameValueCollection(); | ||
queryString.Add("extension", ""); | ||
request.SetupGet(r => r.QueryString).Returns(queryString); | ||
var helpers = new Helpers(); | ||
var pipelineHelpers = new Mock<PipelineHelpers>(); | ||
pipelineHelpers.Setup(h => h.RunDianogaGetSupportedFormatsPipeline(It.IsAny<string[]>())).Returns("webp,avif").Verifiable(); | ||
helpers.PipelineHelpers = pipelineHelpers.Object; | ||
|
||
//Act | ||
var result = helpers.GetCustomOptions(context.Object); | ||
|
||
//Assert | ||
result.Should().Be("webp,avif", ""); | ||
pipelineHelpers.Verify(m => m.RunDianogaGetSupportedFormatsPipeline(It.IsAny<string[]>()), Times.Once); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Dianoga.Tests/NextGenFormats/Pipelines/DianogaGetSupportedFormats/CheckSupport.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Specialized; | ||
using System.Web; | ||
using Dianoga.NextGenFormats; | ||
using Dianoga.NextGenFormats.Pipelines.DianogaGetSupportedFormats; | ||
using Moq; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Dianoga.Tests.NextGenFormats | ||
{ | ||
public class CheckSupportTests | ||
{ | ||
[Fact] | ||
public void ShouldFindFormatSupportInAccepts_WhenItIsPresent() | ||
{ | ||
//Arrange | ||
var args = new SupportedFormatsArgs() | ||
{ | ||
Input = "image/avif,image/webp,image/apng,image/*,*/*;q=0.8", | ||
Prefix = "image/" | ||
}; | ||
|
||
var CheckSupport = new CheckSupport() | ||
{ | ||
Extension = "webp" | ||
}; | ||
|
||
//Act | ||
CheckSupport.Process(args); | ||
|
||
//Assert | ||
args.Extensions.Should().HaveCount(1); | ||
args.Extensions.Should().Contain("webp"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotFindFormatSupportInAccepts_WhenItIsAbsent() | ||
{ | ||
//Arrange | ||
var args = new SupportedFormatsArgs() | ||
{ | ||
Input = "image/avif,image/webp,image/apng,image/*,*/*;q=0.8", | ||
Prefix = "image/" | ||
}; | ||
|
||
var CheckSupport = new CheckSupport() | ||
{ | ||
Extension = "jxl" | ||
}; | ||
|
||
//Act | ||
CheckSupport.Process(args); | ||
|
||
//Assert | ||
args.Extensions.Should().HaveCount(0); | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
src/Dianoga.Tests/Optimizers/Pipelines/DianogaAvif/AvifOptimizerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using System.Diagnostics; | ||
using System.IO; | ||
using Dianoga.Optimizers; | ||
using Dianoga.Optimizers.Pipelines.DianogaAvif; | ||
using FluentAssertions; | ||
using FluentAssertions.Common; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Dianoga.Tests.Optimizers.Pipelines.DianogaAvif | ||
{ | ||
public class AvifOptimizerTests | ||
{ | ||
ITestOutputHelper output; | ||
public AvifOptimizerTests(ITestOutputHelper output) | ||
{ | ||
this.output = output; | ||
} | ||
|
||
[Fact] | ||
public void ShouldReturnOriginalStreamWhenOptimizedImageSizeIsGreater() | ||
{ | ||
Test(@"TestImages\small.jpg", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10 -l", out var args, out var startingSize); | ||
args.Stream.Length.Should().Be(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSquishLosslessSmallPng() | ||
{ | ||
Test(@"TestImages\small.png", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10 -l", out var args, out var startingSize); | ||
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSquishLosslessLargePngButBeTooBig() | ||
{ | ||
Test(@"TestImages\large.png", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10 -l", out var args, out var startingSize); | ||
args.Stream.Length.Should().Be(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSquishLossySmallJpegDefaults() | ||
{ | ||
Test(@"TestImages\small.jpg", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10", out var args, out var startingSize); | ||
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSquishLossyLargeJpegDefaults() | ||
{ | ||
Test(@"TestImages\large.jpg", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10", out var args, out var startingSize); | ||
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotSquishCorruptedJpegLossy() | ||
{ | ||
Test(@"TestImages\corrupted.jpg", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10", out var args, out var startingSize); | ||
args.Stream.Length.Should().IsSameOrEqualTo(startingSize); | ||
args.IsOptimized.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSquishLossySmallPngDefaults() | ||
{ | ||
Test(@"TestImages\small.png", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10", out var args, out var startingSize); | ||
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void ShouldSquishLossyLargePngDefaults() | ||
{ | ||
Test(@"TestImages\large.png", | ||
@"..\..\..\..\Dianoga\Dianoga Tools\avif\avifenc.exe", | ||
"-s 10", out var args, out var startingSize); | ||
args.Stream.Length.Should().BeLessThan(startingSize).And.BeGreaterThan(0); | ||
args.IsOptimized.Should().BeTrue(); | ||
} | ||
|
||
private void Test(string imagePath, string exePath, string exeArgs, out OptimizerArgs argsOut, out long startingSize) | ||
{ | ||
var inputStream = new MemoryStream(); | ||
|
||
using (var testJpeg = File.OpenRead(imagePath)) | ||
{ | ||
testJpeg.CopyTo(inputStream); | ||
} | ||
|
||
var sut = new AvifOptimizer(); | ||
sut.ExePath = exePath; | ||
sut.AdditionalToolArguments = exeArgs; | ||
|
||
var opts = new Sitecore.Resources.Media.MediaOptions(); | ||
opts.CustomOptions["extension"] = "avif"; | ||
var args = new OptimizerArgs(inputStream, opts, imagePath); | ||
|
||
startingSize = args.Stream.Length; | ||
|
||
var stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||
sut.Process(args); | ||
stopwatch.Stop(); | ||
output.WriteLine($"Time: {stopwatch.ElapsedMilliseconds}ms"); | ||
|
||
argsOut = args; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.