From f9c2f6ade67a4f6f2a9e9eeb92d3205ec832730e Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Thu, 30 Apr 2020 11:20:29 +0200 Subject: [PATCH] Improve exclude filter test to also check presence of expected chunks --- .../Formats/Png/IPngEncoderOptions.cs | 2 +- .../Formats/Png/PngEncoderTests.Chunks.cs | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs b/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs index 48fd7447d..e5d0b09cf 100644 --- a/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs +++ b/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Png PngInterlaceMode? InterlaceMethod { get; } /// - /// Gets the optimize method. + /// Gets chunk filter method. /// PngChunkFilter? ChunkFilter { get; } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.Chunks.cs b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.Chunks.cs index 9d28fd89b..fa1544816 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.Chunks.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.Chunks.cs @@ -158,22 +158,41 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png using Image input = testFile.CreateRgba32Image(); using var memStream = new MemoryStream(); var encoder = new PngEncoder() { ChunkFilter = chunkFilter, TextCompressionThreshold = 8 }; + var expectedChunkTypes = new Dictionary() + { + { PngChunkType.Header, false }, + { PngChunkType.Gamma, false }, + { PngChunkType.Palette, false }, + { PngChunkType.InternationalText, false }, + { PngChunkType.Text, false }, + { PngChunkType.CompressedText, false }, + { PngChunkType.Exif, false }, + { PngChunkType.Physical, false }, + { PngChunkType.Data, false }, + { PngChunkType.End, false } + }; var excludedChunkTypes = new List(); switch (chunkFilter) { case PngChunkFilter.ExcludeGammaChunk: excludedChunkTypes.Add(PngChunkType.Gamma); + expectedChunkTypes.Remove(PngChunkType.Gamma); break; case PngChunkFilter.ExcludeExifChunk: excludedChunkTypes.Add(PngChunkType.Exif); + expectedChunkTypes.Remove(PngChunkType.Exif); break; case PngChunkFilter.ExcludePhysicalChunk: excludedChunkTypes.Add(PngChunkType.Physical); + expectedChunkTypes.Remove(PngChunkType.Physical); break; case PngChunkFilter.ExcludeTextChunks: excludedChunkTypes.Add(PngChunkType.Text); excludedChunkTypes.Add(PngChunkType.InternationalText); excludedChunkTypes.Add(PngChunkType.CompressedText); + expectedChunkTypes.Remove(PngChunkType.Text); + expectedChunkTypes.Remove(PngChunkType.InternationalText); + expectedChunkTypes.Remove(PngChunkType.CompressedText); break; case PngChunkFilter.ExcludeAll: excludedChunkTypes.Add(PngChunkType.Gamma); @@ -182,6 +201,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png excludedChunkTypes.Add(PngChunkType.Text); excludedChunkTypes.Add(PngChunkType.InternationalText); excludedChunkTypes.Add(PngChunkType.CompressedText); + expectedChunkTypes.Remove(PngChunkType.Gamma); + expectedChunkTypes.Remove(PngChunkType.Exif); + expectedChunkTypes.Remove(PngChunkType.Physical); + expectedChunkTypes.Remove(PngChunkType.Text); + expectedChunkTypes.Remove(PngChunkType.InternationalText); + expectedChunkTypes.Remove(PngChunkType.CompressedText); break; } @@ -197,9 +222,19 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png int length = BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(0, 4)); var chunkType = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4)); Assert.False(excludedChunkTypes.Contains(chunkType), $"{chunkType} chunk should have been excluded"); + if (expectedChunkTypes.ContainsKey(chunkType)) + { + expectedChunkTypes[chunkType] = true; + } bytesSpan = bytesSpan.Slice(4 + 4 + length + 4); } + + // all expected chunk types should have been seen at least once. + foreach (PngChunkType chunkType in expectedChunkTypes.Keys) + { + Assert.True(expectedChunkTypes[chunkType], $"We expect {chunkType} chunk to be present at least once"); + } } [Fact] @@ -215,7 +250,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png PngChunkType.Header, PngChunkType.Gamma, PngChunkType.Palette, - PngChunkType.Transparency, PngChunkType.InternationalText, PngChunkType.Text, PngChunkType.CompressedText,