Browse Source

Improve exclude filter test to also check presence of expected chunks

pull/1012/head
Brian Popow 6 years ago
parent
commit
f9c2f6ade6
  1. 2
      src/ImageSharp/Formats/Png/IPngEncoderOptions.cs
  2. 36
      tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.Chunks.cs

2
src/ImageSharp/Formats/Png/IPngEncoderOptions.cs

@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Png
PngInterlaceMode? InterlaceMethod { get; } PngInterlaceMode? InterlaceMethod { get; }
/// <summary> /// <summary>
/// Gets the optimize method. /// Gets chunk filter method.
/// </summary> /// </summary>
PngChunkFilter? ChunkFilter { get; } PngChunkFilter? ChunkFilter { get; }

36
tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.Chunks.cs

@ -158,22 +158,41 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
using Image<Rgba32> input = testFile.CreateRgba32Image(); using Image<Rgba32> input = testFile.CreateRgba32Image();
using var memStream = new MemoryStream(); using var memStream = new MemoryStream();
var encoder = new PngEncoder() { ChunkFilter = chunkFilter, TextCompressionThreshold = 8 }; var encoder = new PngEncoder() { ChunkFilter = chunkFilter, TextCompressionThreshold = 8 };
var expectedChunkTypes = new Dictionary<PngChunkType, bool>()
{
{ 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<PngChunkType>(); var excludedChunkTypes = new List<PngChunkType>();
switch (chunkFilter) switch (chunkFilter)
{ {
case PngChunkFilter.ExcludeGammaChunk: case PngChunkFilter.ExcludeGammaChunk:
excludedChunkTypes.Add(PngChunkType.Gamma); excludedChunkTypes.Add(PngChunkType.Gamma);
expectedChunkTypes.Remove(PngChunkType.Gamma);
break; break;
case PngChunkFilter.ExcludeExifChunk: case PngChunkFilter.ExcludeExifChunk:
excludedChunkTypes.Add(PngChunkType.Exif); excludedChunkTypes.Add(PngChunkType.Exif);
expectedChunkTypes.Remove(PngChunkType.Exif);
break; break;
case PngChunkFilter.ExcludePhysicalChunk: case PngChunkFilter.ExcludePhysicalChunk:
excludedChunkTypes.Add(PngChunkType.Physical); excludedChunkTypes.Add(PngChunkType.Physical);
expectedChunkTypes.Remove(PngChunkType.Physical);
break; break;
case PngChunkFilter.ExcludeTextChunks: case PngChunkFilter.ExcludeTextChunks:
excludedChunkTypes.Add(PngChunkType.Text); excludedChunkTypes.Add(PngChunkType.Text);
excludedChunkTypes.Add(PngChunkType.InternationalText); excludedChunkTypes.Add(PngChunkType.InternationalText);
excludedChunkTypes.Add(PngChunkType.CompressedText); excludedChunkTypes.Add(PngChunkType.CompressedText);
expectedChunkTypes.Remove(PngChunkType.Text);
expectedChunkTypes.Remove(PngChunkType.InternationalText);
expectedChunkTypes.Remove(PngChunkType.CompressedText);
break; break;
case PngChunkFilter.ExcludeAll: case PngChunkFilter.ExcludeAll:
excludedChunkTypes.Add(PngChunkType.Gamma); excludedChunkTypes.Add(PngChunkType.Gamma);
@ -182,6 +201,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
excludedChunkTypes.Add(PngChunkType.Text); excludedChunkTypes.Add(PngChunkType.Text);
excludedChunkTypes.Add(PngChunkType.InternationalText); excludedChunkTypes.Add(PngChunkType.InternationalText);
excludedChunkTypes.Add(PngChunkType.CompressedText); 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; break;
} }
@ -197,9 +222,19 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
int length = BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(0, 4)); int length = BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(0, 4));
var chunkType = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4)); var chunkType = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4));
Assert.False(excludedChunkTypes.Contains(chunkType), $"{chunkType} chunk should have been excluded"); 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); 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] [Fact]
@ -215,7 +250,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png
PngChunkType.Header, PngChunkType.Header,
PngChunkType.Gamma, PngChunkType.Gamma,
PngChunkType.Palette, PngChunkType.Palette,
PngChunkType.Transparency,
PngChunkType.InternationalText, PngChunkType.InternationalText,
PngChunkType.Text, PngChunkType.Text,
PngChunkType.CompressedText, PngChunkType.CompressedText,

Loading…
Cancel
Save