Bug 1191114 (Part 4) - Add tests for metadata decoding, including that we always deliver HAS_TRANSPARENCY during the metadata decode. r=tn

This commit is contained in:
Seth Fowler 2015-08-12 10:41:11 -07:00
parent 15e076ac81
commit 6a2b326a41
10 changed files with 242 additions and 3 deletions

View File

@ -136,7 +136,10 @@ ImageTestCase GreenBMPTestCase()
ImageTestCase GreenICOTestCase()
{
return ImageTestCase("green.ico", "image/x-icon", IntSize(100, 100));
// This ICO contains a 32-bit BMP, and we use a BMP's alpha data by default
// when the BMP is embedded in an ICO, so it's transparent.
return ImageTestCase("green.ico", "image/x-icon", IntSize(100, 100),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase GreenFirstFrameAnimatedGIFTestCase()
@ -146,12 +149,53 @@ ImageTestCase GreenFirstFrameAnimatedGIFTestCase()
ImageTestCase GreenFirstFrameAnimatedPNGTestCase()
{
return ImageTestCase("first-frame-green.png", "image/png", IntSize(100, 100));
return ImageTestCase("first-frame-green.png", "image/png", IntSize(100, 100),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase CorruptTestCase()
{
return ImageTestCase("corrupt.jpg", "image/jpeg", IntSize(100, 100));
return ImageTestCase("corrupt.jpg", "image/jpeg", IntSize(100, 100),
TEST_CASE_HAS_ERROR);
}
ImageTestCase TransparentPNGTestCase()
{
return ImageTestCase("transparent.png", "image/png", IntSize(32, 32),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase TransparentGIFTestCase()
{
return ImageTestCase("transparent.gif", "image/gif", IntSize(16, 16),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase FirstFramePaddingGIFTestCase()
{
return ImageTestCase("transparent.gif", "image/gif", IntSize(16, 16),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase TransparentBMPWhenBMPAlphaEnabledTestCase()
{
// Note that we only decode this test case as transparent when the BMP decoder
// is set to use alpha data. (That's not the default, which is why it's not marked
// TEST_CASE_IS_TRANSPARENT; tests that want to treat this testcase as
// transparent need to handle this case manually.)
return ImageTestCase("transparent.bmp", "image/bmp", IntSize(32, 32));
}
ImageTestCase RLE4BMPTestCase()
{
return ImageTestCase("rle4.bmp", "image/bmp", IntSize(320, 240),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase RLE8BMPTestCase()
{
return ImageTestCase("rle8.bmp", "image/bmp", IntSize(32, 32),
TEST_CASE_IS_TRANSPARENT);
}
} // namespace mozilla

View File

@ -94,6 +94,14 @@ ImageTestCase GreenFirstFrameAnimatedPNGTestCase();
ImageTestCase CorruptTestCase();
ImageTestCase TransparentPNGTestCase();
ImageTestCase TransparentGIFTestCase();
ImageTestCase FirstFramePaddingGIFTestCase();
ImageTestCase TransparentBMPWhenBMPAlphaEnabledTestCase();
ImageTestCase RLE4BMPTestCase();
ImageTestCase RLE8BMPTestCase();
} // namespace mozilla
#endif // mozilla_image_test_gtest_Common_h

View File

@ -0,0 +1,180 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "Common.h"
#include "Decoder.h"
#include "DecoderFactory.h"
#include "decoders/nsBMPDecoder.h"
#include "imgIContainer.h"
#include "imgITools.h"
#include "mozilla/gfx/2D.h"
#include "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsIInputStream.h"
#include "nsIRunnable.h"
#include "nsIThread.h"
#include "mozilla/nsRefPtr.h"
#include "nsStreamUtils.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "ProgressTracker.h"
#include "SourceBuffer.h"
using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::image;
TEST(ImageMetadata, ImageModuleAvailable)
{
// We can run into problems if XPCOM modules get initialized in the wrong
// order. It's important that this test run first, both as a sanity check and
// to ensure we get the module initialization order we want.
nsCOMPtr<imgITools> imgTools =
do_CreateInstance("@mozilla.org/image/tools;1");
EXPECT_TRUE(imgTools != nullptr);
}
static void
CheckMetadata(const ImageTestCase& aTestCase, bool aEnableBMPAlpha = false)
{
nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
ASSERT_TRUE(inputStream != nullptr);
// Prepare the input stream.
nsresult rv;
if (!NS_InputStreamIsBuffered(inputStream)) {
nsCOMPtr<nsIInputStream> bufStream;
rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream),
inputStream, 1024);
if (NS_SUCCEEDED(rv)) {
inputStream = bufStream;
}
}
// Figure out how much data we have.
uint64_t length;
rv = inputStream->Available(&length);
ASSERT_TRUE(NS_SUCCEEDED(rv));
// Write the data into a SourceBuffer.
nsRefPtr<SourceBuffer> sourceBuffer = new SourceBuffer();
sourceBuffer->ExpectLength(length);
rv = sourceBuffer->AppendFromInputStream(inputStream, length);
ASSERT_TRUE(NS_SUCCEEDED(rv));
sourceBuffer->Complete(NS_OK);
// Create a metadata decoder.
DecoderType decoderType =
DecoderFactory::GetDecoderType(aTestCase.mMimeType);
nsRefPtr<Decoder> decoder =
DecoderFactory::CreateAnonymousMetadataDecoder(decoderType, sourceBuffer);
ASSERT_TRUE(decoder != nullptr);
if (aEnableBMPAlpha) {
static_cast<nsBMPDecoder*>(decoder.get())->SetUseAlphaData(true);
}
// Run the metadata decoder synchronously.
decoder->Decode();
// Ensure that the metadata decoder didn't make progress it shouldn't have
// (which would indicate that it decoded past the header of the image).
Progress metadataProgress = decoder->TakeProgress();
EXPECT_TRUE(0 == (metadataProgress & ~(FLAG_SIZE_AVAILABLE |
FLAG_HAS_TRANSPARENCY)));
// If the test case is corrupt, assert what we can and return early.
if (aTestCase.mFlags & TEST_CASE_HAS_ERROR) {
EXPECT_TRUE(decoder->GetDecodeDone());
EXPECT_TRUE(decoder->HasError());
return;
}
EXPECT_TRUE(decoder->GetDecodeDone() && !decoder->HasError());
// Check that we got the expected metadata.
EXPECT_TRUE(metadataProgress & FLAG_SIZE_AVAILABLE);
IntSize metadataSize = decoder->GetSize();
EXPECT_EQ(aTestCase.mSize.width, metadataSize.width);
EXPECT_EQ(aTestCase.mSize.height, metadataSize.height);
bool expectTransparency = aEnableBMPAlpha
? true
: bool(aTestCase.mFlags & TEST_CASE_IS_TRANSPARENT);
EXPECT_EQ(expectTransparency, bool(metadataProgress & FLAG_HAS_TRANSPARENCY));
// Create a full decoder, so we can compare the result.
decoder =
DecoderFactory::CreateAnonymousDecoder(decoderType, sourceBuffer,
imgIContainer::DECODE_FLAGS_DEFAULT);
ASSERT_TRUE(decoder != nullptr);
if (aEnableBMPAlpha) {
static_cast<nsBMPDecoder*>(decoder.get())->SetUseAlphaData(true);
}
// Run the full decoder synchronously.
decoder->Decode();
EXPECT_TRUE(decoder->GetDecodeDone() && !decoder->HasError());
Progress fullProgress = decoder->TakeProgress();
// If the metadata decoder set a progress bit, the full decoder should also
// have set the same bit.
EXPECT_EQ(fullProgress, metadataProgress | fullProgress);
// The full decoder and the metadata decoder should agree on the image's size.
IntSize fullSize = decoder->GetSize();
EXPECT_EQ(metadataSize.width, fullSize.width);
EXPECT_EQ(metadataSize.height, fullSize.height);
// We should not discover transparency during the full decode that we didn't
// discover during the metadata decode, unless the image is animated.
EXPECT_TRUE(!(fullProgress & FLAG_HAS_TRANSPARENCY) ||
(metadataProgress & FLAG_HAS_TRANSPARENCY) ||
(fullProgress & FLAG_IS_ANIMATED));
}
TEST(ImageMetadata, PNG) { CheckMetadata(GreenPNGTestCase()); }
TEST(ImageMetadata, TransparentPNG) { CheckMetadata(TransparentPNGTestCase()); }
TEST(ImageMetadata, GIF) { CheckMetadata(GreenGIFTestCase()); }
TEST(ImageMetadata, TransparentGIF) { CheckMetadata(TransparentGIFTestCase()); }
TEST(ImageMetadata, JPG) { CheckMetadata(GreenJPGTestCase()); }
TEST(ImageMetadata, BMP) { CheckMetadata(GreenBMPTestCase()); }
TEST(ImageMetadata, ICO) { CheckMetadata(GreenICOTestCase()); }
TEST(ImageMetadata, AnimatedGIF)
{
CheckMetadata(GreenFirstFrameAnimatedGIFTestCase());
}
TEST(ImageMetadata, AnimatedPNG)
{
CheckMetadata(GreenFirstFrameAnimatedPNGTestCase());
}
TEST(ImageMetadata, FirstFramePaddingGIF)
{
CheckMetadata(FirstFramePaddingGIFTestCase());
}
TEST(ImageMetadata, TransparentBMPWithBMPAlphaOff)
{
CheckMetadata(TransparentBMPWhenBMPAlphaEnabledTestCase(),
/* aEnableBMPAlpha = */ false);
}
TEST(ImageMetadata, TransparentBMPWithBMPAlphaOn)
{
CheckMetadata(TransparentBMPWhenBMPAlphaEnabledTestCase(),
/* aEnableBMPAlpha = */ true);
}
TEST(ImageMetadata, RLE4BMP) { CheckMetadata(RLE4BMPTestCase()); }
TEST(ImageMetadata, RLE8BMP) { CheckMetadata(RLE8BMPTestCase()); }
TEST(ImageMetadata, Corrupt) { CheckMetadata(CorruptTestCase()); }

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 B

View File

@ -11,17 +11,24 @@ FAIL_ON_WARNINGS = True
UNIFIED_SOURCES = [
'Common.cpp',
'TestDecodeToSurface.cpp',
'TestMetadata.cpp',
]
TEST_HARNESS_FILES.gtest += [
'corrupt.jpg',
'first-frame-green.gif',
'first-frame-green.png',
'first-frame-padding.gif',
'green.bmp',
'green.gif',
'green.ico',
'green.jpg',
'green.png',
'rle4.bmp',
'rle8.bmp',
'transparent.bmp',
'transparent.gif',
'transparent.png',
]
LOCAL_INCLUDES += [

BIN
image/test/gtest/rle4.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

BIN
image/test/gtest/rle8.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 B