2015-08-12 20:41:11 +03:00
|
|
|
/* 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"
|
2016-06-29 23:43:19 +03:00
|
|
|
#include "IDecodingTask.h"
|
2015-08-12 20:41:11 +03:00
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "imgITools.h"
|
2015-08-14 10:37:19 +03:00
|
|
|
#include "ImageFactory.h"
|
2015-08-12 20:41:11 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIThread.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-08-12 20:41:11 +03:00
|
|
|
#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;
|
|
|
|
|
2015-10-14 07:20:10 +03:00
|
|
|
enum class BMPWithinICO
|
2015-08-14 10:37:19 +03:00
|
|
|
{
|
2015-10-14 07:20:10 +03:00
|
|
|
NO,
|
|
|
|
YES
|
2015-08-14 10:37:19 +03:00
|
|
|
};
|
|
|
|
|
2015-08-12 20:41:11 +03:00
|
|
|
static void
|
2015-08-14 10:37:19 +03:00
|
|
|
CheckMetadata(const ImageTestCase& aTestCase,
|
2015-10-14 07:20:10 +03:00
|
|
|
BMPWithinICO aBMPWithinICO = BMPWithinICO::NO)
|
2015-08-12 20:41:11 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream = LoadFile(aTestCase.mPath);
|
|
|
|
ASSERT_TRUE(inputStream != nullptr);
|
|
|
|
|
|
|
|
// Figure out how much data we have.
|
|
|
|
uint64_t length;
|
2015-08-14 10:37:16 +03:00
|
|
|
nsresult rv = inputStream->Available(&length);
|
2015-08-12 20:41:11 +03:00
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Write the data into a SourceBuffer.
|
2016-06-27 07:50:43 +03:00
|
|
|
NotNull<RefPtr<SourceBuffer>> sourceBuffer = WrapNotNull(new SourceBuffer());
|
2015-08-12 20:41:11 +03:00
|
|
|
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);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Decoder> decoder =
|
2015-08-12 20:41:11 +03:00
|
|
|
DecoderFactory::CreateAnonymousMetadataDecoder(decoderType, sourceBuffer);
|
|
|
|
ASSERT_TRUE(decoder != nullptr);
|
2016-06-29 23:43:19 +03:00
|
|
|
RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder));
|
2015-08-12 20:41:11 +03:00
|
|
|
|
2015-10-14 07:20:10 +03:00
|
|
|
if (aBMPWithinICO == BMPWithinICO::YES) {
|
|
|
|
static_cast<nsBMPDecoder*>(decoder.get())->SetIsWithinICO();
|
2015-08-12 20:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the metadata decoder synchronously.
|
2016-06-29 23:43:19 +03:00
|
|
|
task->Run();
|
2015-10-14 07:20:10 +03:00
|
|
|
|
2015-08-12 20:41:11 +03:00
|
|
|
// 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 |
|
2015-08-14 10:37:19 +03:00
|
|
|
FLAG_HAS_TRANSPARENCY |
|
|
|
|
FLAG_IS_ANIMATED)));
|
2015-08-12 20:41:11 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-07-30 00:11:19 +03:00
|
|
|
IntSize metadataSize = decoder->Size();
|
2015-08-12 20:41:11 +03:00
|
|
|
EXPECT_EQ(aTestCase.mSize.width, metadataSize.width);
|
|
|
|
EXPECT_EQ(aTestCase.mSize.height, metadataSize.height);
|
|
|
|
|
2015-10-14 07:20:10 +03:00
|
|
|
bool expectTransparency = aBMPWithinICO == BMPWithinICO::YES
|
2015-08-12 20:41:11 +03:00
|
|
|
? true
|
|
|
|
: bool(aTestCase.mFlags & TEST_CASE_IS_TRANSPARENT);
|
|
|
|
EXPECT_EQ(expectTransparency, bool(metadataProgress & FLAG_HAS_TRANSPARENCY));
|
|
|
|
|
2015-08-14 10:37:19 +03:00
|
|
|
EXPECT_EQ(bool(aTestCase.mFlags & TEST_CASE_IS_ANIMATED),
|
|
|
|
bool(metadataProgress & FLAG_IS_ANIMATED));
|
|
|
|
|
2015-08-12 20:41:11 +03:00
|
|
|
// Create a full decoder, so we can compare the result.
|
|
|
|
decoder =
|
2016-05-27 22:59:19 +03:00
|
|
|
DecoderFactory::CreateAnonymousDecoder(decoderType, sourceBuffer, Nothing(),
|
2015-08-15 03:56:44 +03:00
|
|
|
DefaultSurfaceFlags());
|
2015-08-12 20:41:11 +03:00
|
|
|
ASSERT_TRUE(decoder != nullptr);
|
2016-06-29 23:43:19 +03:00
|
|
|
task = new AnonymousDecodingTask(WrapNotNull(decoder));
|
2015-08-12 20:41:11 +03:00
|
|
|
|
2015-10-14 07:20:10 +03:00
|
|
|
if (aBMPWithinICO == BMPWithinICO::YES) {
|
|
|
|
static_cast<nsBMPDecoder*>(decoder.get())->SetIsWithinICO();
|
2015-08-12 20:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the full decoder synchronously.
|
2016-06-29 23:43:19 +03:00
|
|
|
task->Run();
|
2015-10-14 07:20:10 +03:00
|
|
|
|
2015-08-12 20:41:11 +03:00
|
|
|
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.
|
2016-07-30 00:11:19 +03:00
|
|
|
IntSize fullSize = decoder->Size();
|
2015-08-12 20:41:11 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
class ImageDecoderMetadata : public ::testing::Test
|
|
|
|
{
|
2016-07-12 10:38:32 +03:00
|
|
|
protected:
|
|
|
|
AutoInitializeImageLib mInit;
|
2016-06-23 21:13:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ImageDecoderMetadata, PNG) { CheckMetadata(GreenPNGTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, TransparentPNG) { CheckMetadata(TransparentPNGTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, GIF) { CheckMetadata(GreenGIFTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, TransparentGIF) { CheckMetadata(TransparentGIFTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, JPG) { CheckMetadata(GreenJPGTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, BMP) { CheckMetadata(GreenBMPTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, ICO) { CheckMetadata(GreenICOTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, Icon) { CheckMetadata(GreenIconTestCase()); }
|
2015-08-12 20:41:11 +03:00
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, AnimatedGIF)
|
2015-08-12 20:41:11 +03:00
|
|
|
{
|
|
|
|
CheckMetadata(GreenFirstFrameAnimatedGIFTestCase());
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, AnimatedPNG)
|
2015-08-12 20:41:11 +03:00
|
|
|
{
|
|
|
|
CheckMetadata(GreenFirstFrameAnimatedPNGTestCase());
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, FirstFramePaddingGIF)
|
2015-08-12 20:41:11 +03:00
|
|
|
{
|
|
|
|
CheckMetadata(FirstFramePaddingGIFTestCase());
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, TransparentIfWithinICOBMPNotWithinICO)
|
2015-08-12 20:41:11 +03:00
|
|
|
{
|
2015-10-14 07:20:10 +03:00
|
|
|
CheckMetadata(TransparentIfWithinICOBMPTestCase(TEST_CASE_DEFAULT_FLAGS),
|
|
|
|
BMPWithinICO::NO);
|
2015-08-12 20:41:11 +03:00
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, TransparentIfWithinICOBMPWithinICO)
|
2015-08-12 20:41:11 +03:00
|
|
|
{
|
2015-10-14 07:20:10 +03:00
|
|
|
CheckMetadata(TransparentIfWithinICOBMPTestCase(TEST_CASE_IS_TRANSPARENT),
|
|
|
|
BMPWithinICO::YES);
|
2015-08-12 20:41:11 +03:00
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, RLE4BMP) { CheckMetadata(RLE4BMPTestCase()); }
|
|
|
|
TEST_F(ImageDecoderMetadata, RLE8BMP) { CheckMetadata(RLE8BMPTestCase()); }
|
2015-08-12 20:41:11 +03:00
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, Corrupt) { CheckMetadata(CorruptTestCase()); }
|
2015-08-14 10:37:19 +03:00
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, NoFrameDelayGIF)
|
2015-08-14 10:37:19 +03:00
|
|
|
{
|
|
|
|
CheckMetadata(NoFrameDelayGIFTestCase());
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:13:14 +03:00
|
|
|
TEST_F(ImageDecoderMetadata, NoFrameDelayGIFFullDecode)
|
2015-08-14 10:37:19 +03:00
|
|
|
{
|
|
|
|
ImageTestCase testCase = NoFrameDelayGIFTestCase();
|
|
|
|
|
|
|
|
// The previous test (NoFrameDelayGIF) verifies that we *don't* detect that
|
|
|
|
// this test case is animated, because it has a zero frame delay for the first
|
|
|
|
// frame. This test verifies that when we do a full decode, we detect the
|
|
|
|
// animation at that point and successfully decode all the frames.
|
|
|
|
|
|
|
|
// Create an image.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image =
|
2016-05-09 20:10:09 +03:00
|
|
|
ImageFactory::CreateAnonymousImage(nsDependentCString(testCase.mMimeType));
|
2015-08-14 10:37:19 +03:00
|
|
|
ASSERT_TRUE(!image->HasError());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputStream = LoadFile(testCase.mPath);
|
|
|
|
ASSERT_TRUE(inputStream != nullptr);
|
|
|
|
|
|
|
|
// Figure out how much data we have.
|
|
|
|
uint64_t length;
|
|
|
|
nsresult rv = inputStream->Available(&length);
|
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Write the data into the image.
|
|
|
|
rv = image->OnImageDataAvailable(nullptr, nullptr, inputStream, 0,
|
|
|
|
static_cast<uint32_t>(length));
|
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Let the image know we've sent all the data.
|
|
|
|
rv = image->OnImageDataComplete(nullptr, nullptr, NS_OK, true);
|
|
|
|
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
|
2015-08-14 10:37:19 +03:00
|
|
|
tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
|
|
|
|
|
|
|
|
// Use GetFrame() to force a sync decode of the image.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface =
|
2015-08-14 10:37:19 +03:00
|
|
|
image->GetFrame(imgIContainer::FRAME_CURRENT,
|
|
|
|
imgIContainer::FLAG_SYNC_DECODE);
|
|
|
|
|
|
|
|
// Ensure that the image's metadata meets our expectations.
|
|
|
|
IntSize imageSize(0, 0);
|
|
|
|
rv = image->GetWidth(&imageSize.width);
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
rv = image->GetHeight(&imageSize.height);
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
EXPECT_EQ(testCase.mSize.width, imageSize.width);
|
|
|
|
EXPECT_EQ(testCase.mSize.height, imageSize.height);
|
|
|
|
|
|
|
|
Progress imageProgress = tracker->GetProgress();
|
|
|
|
|
|
|
|
EXPECT_TRUE(bool(imageProgress & FLAG_HAS_TRANSPARENCY) == false);
|
|
|
|
EXPECT_TRUE(bool(imageProgress & FLAG_IS_ANIMATED) == true);
|
|
|
|
|
|
|
|
// Ensure that we decoded both frames of the image.
|
2016-08-18 10:06:41 +03:00
|
|
|
LookupResult result =
|
2015-08-14 10:37:19 +03:00
|
|
|
SurfaceCache::Lookup(ImageKey(image.get()),
|
|
|
|
RasterSurfaceKey(imageSize,
|
2015-08-15 03:56:44 +03:00
|
|
|
DefaultSurfaceFlags(),
|
2016-08-18 10:06:41 +03:00
|
|
|
PlaybackType::eAnimated));
|
|
|
|
ASSERT_EQ(MatchType::EXACT, result.Type());
|
|
|
|
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(result.Surface().Seek(0)));
|
|
|
|
EXPECT_TRUE(bool(result.Surface()));
|
|
|
|
|
|
|
|
EXPECT_TRUE(NS_SUCCEEDED(result.Surface().Seek(1)));
|
|
|
|
EXPECT_TRUE(bool(result.Surface()));
|
2015-08-14 10:37:19 +03:00
|
|
|
}
|