diff options
author | yaar@chromium.org <yaar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-08 22:11:41 +0000 |
---|---|---|
committer | yaar@chromium.org <yaar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-08 22:11:41 +0000 |
commit | 76d316196deedfa00a81d089c3116cf47110395f (patch) | |
tree | 1e88dae921a10bcfe4be63e2470e46ab4d20e32f | |
parent | 7a766a21fc455ee5350117895f320f54470d4653 (diff) | |
download | chromium_src-76d316196deedfa00a81d089c3116cf47110395f.zip chromium_src-76d316196deedfa00a81d089c3116cf47110395f.tar.gz chromium_src-76d316196deedfa00a81d089c3116cf47110395f.tar.bz2 |
UpStreamimg *image_decoder_unittest. Used WebKit API's ImageDecoder to
decouple WebCore deps. Corresponding WebKit Bug 35415.
BUG=28063
TEST=Run unit tests.
Review URL: http://codereview.chromium.org/661231
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40951 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | skia/ext/vector_canvas_unittest.cc | 11 | ||||
-rw-r--r-- | webkit/tools/test_shell/image_decoder_unittest.cc | 203 | ||||
-rw-r--r-- | webkit/tools/test_shell/image_decoder_unittest.h | 58 | ||||
-rw-r--r-- | webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp | 21 | ||||
-rw-r--r-- | webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp | 31 |
5 files changed, 130 insertions, 194 deletions
diff --git a/skia/ext/vector_canvas_unittest.cc b/skia/ext/vector_canvas_unittest.cc index 11c6f41..328de22 100644 --- a/skia/ext/vector_canvas_unittest.cc +++ b/skia/ext/vector_canvas_unittest.cc @@ -5,14 +5,14 @@ #include "config.h" #include "build/build_config.h" -#include "webkit/tools/test_shell/image_decoder_unittest.h" - #if !defined(OS_WIN) #include <unistd.h> #endif #include "PNGImageDecoder.h" +#undef LOG + #include "app/gfx/codec/png_codec.h" #include "base/command_line.h" #include "base/file_util.h" @@ -28,6 +28,13 @@ namespace { const wchar_t* const kGenerateSwitch = L"vector-canvas-generate"; +void ReadFileToVector(const FilePath& path, Vector<char>* contents) { + std::string contents_str; + file_util::ReadFileToString(path, &contents_str); + contents->resize(contents_str.size()); + memcpy(&contents->first(), contents_str.data(), contents_str.size()); +} + // Lightweight HDC management. class Context { public: diff --git a/webkit/tools/test_shell/image_decoder_unittest.cc b/webkit/tools/test_shell/image_decoder_unittest.cc index 7384ff30..917e1b3 100644 --- a/webkit/tools/test_shell/image_decoder_unittest.cc +++ b/webkit/tools/test_shell/image_decoder_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,12 +12,17 @@ #include "base/path_service.h" #include "base/scoped_ptr.h" #include "base/string_util.h" -#include "base/time.h" +#include "third_party/WebKit/WebKit/chromium/public/WebData.h" +#include "third_party/WebKit/WebKit/chromium/public/WebImage.h" +#include "third_party/WebKit/WebKit/chromium/public/WebImageDecoder.h" +#include "third_party/WebKit/WebKit/chromium/public/WebSize.h" using base::Time; namespace { +const int kFirstFrameIndex = 0; + // Determine if we should test with file specified by |path| based // on |file_selection| and the |threshold| for the file size. bool ShouldSkipFile(const FilePath& path, @@ -33,11 +38,11 @@ bool ShouldSkipFile(const FilePath& path, } // anonymous namespace -void ReadFileToVector(const FilePath& path, Vector<char>* contents) { - std::string contents_str; - file_util::ReadFileToString(path, &contents_str); - contents->resize(contents_str.size()); - memcpy(&contents->first(), contents_str.data(), contents_str.size()); +void ReadFileToVector(const FilePath& path, std::vector<char>* contents) { + std::string raw_image_data; + file_util::ReadFileToString(path, &raw_image_data); + contents->resize(raw_image_data.size()); + memcpy(&contents->at(0), raw_image_data.data(), raw_image_data.size()); } FilePath GetMD5SumPath(const FilePath& path) { @@ -46,48 +51,43 @@ FilePath GetMD5SumPath(const FilePath& path) { return FilePath(path.value() + kDecodedDataExtension); } -#ifdef CALCULATE_MD5_SUMS -void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) { +#if defined(CALCULATE_MD5_SUMS) +void SaveMD5Sum(const std::wstring& path, const WebKit::WebImage& web_image) { // Calculate MD5 sum. MD5Digest digest; - scoped_ptr<NativeImageSkia> image_data(buffer->asNewNativeImage()); - { - SkAutoLockPixels bmp_lock(*image_data); - MD5Sum(image_data->getPixels(), - image_data->width() * image_data->height() * sizeof(uint32_t), - &digest); - } + web_image.getSkBitmap().lockPixels(); + MD5Sum(web_image.getSkBitmap().getPixels(), + web_image.getSkBitmap().width() * web_image.getSkBitmap().height() * + sizeof(uint32_t), + &digest); // Write sum to disk. int bytes_written = file_util::WriteFile(path, reinterpret_cast<const char*>(&digest), sizeof digest); ASSERT_EQ(sizeof digest, bytes_written); + web_image.getSkBitmap().unlockPixels(); } -#else -void VerifyImage(WebCore::ImageDecoder* decoder, +#endif + +#if !defined(CALCULATE_MD5_SUMS) +void VerifyImage(const WebKit::WebImageDecoder& decoder, const FilePath& path, const FilePath& md5_sum_path, size_t frame_index) { // Make sure decoding can complete successfully. - EXPECT_TRUE(decoder->isSizeAvailable()) << path.value(); - EXPECT_GE(decoder->frameCount(), frame_index) << path.value(); - WebCore::RGBA32Buffer* image_buffer = - decoder->frameBufferAtIndex(frame_index); - ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << - path.value(); - EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) << - path.value(); - EXPECT_FALSE(decoder->failed()) << path.value(); + EXPECT_TRUE(decoder.isSizeAvailable()) << path.value(); + EXPECT_GE(decoder.frameCount(), frame_index) << path.value(); + EXPECT_TRUE(decoder.isFrameCompleteAtIndex(frame_index)) << path.value(); + EXPECT_FALSE(decoder.isFailed()); // Calculate MD5 sum. MD5Digest actual_digest; - scoped_ptr<NativeImageSkia> image_data(image_buffer->asNewNativeImage()); - { - SkAutoLockPixels bmp_lock(*image_data); - MD5Sum(image_data->getPixels(), - image_data->width() * image_data->height() * sizeof(uint32_t), - &actual_digest); - } + const WebKit::WebImage& web_image = decoder.getFrameAtIndex(frame_index); + web_image.getSkBitmap().lockPixels(); + MD5Sum(web_image.getSkBitmap().getPixels(), + web_image.getSkBitmap().width() * web_image.getSkBitmap().height() * + sizeof(uint32_t), + &actual_digest); // Read the MD5 sum off disk. std::string file_bytes; @@ -97,8 +97,9 @@ void VerifyImage(WebCore::ImageDecoder* decoder, memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest); // Verify that the sums are the same. - EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) << - path.value(); + EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) + << path.value(); + web_image.getSkBitmap().unlockPixels(); } #endif @@ -143,94 +144,74 @@ bool ImageDecoderTest::ShouldImageFail(const FilePath& path) const { kBadSuffix.length(), kBadSuffix)); } -WebCore::ImageDecoder* ImageDecoderTest::SetupDecoder( - const FilePath& path, - bool split_at_random) const { - Vector<char> image_contents; - ReadFileToVector(path, &image_contents); - - WebCore::ImageDecoder* decoder = CreateDecoder(); - RefPtr<WebCore::SharedBuffer> shared_contents( - WebCore::SharedBuffer::create()); - - if (split_at_random) { - // Split the file at an arbitrary point. - const int partial_size = static_cast<int>( - (static_cast<double>(rand()) / RAND_MAX) * image_contents.size()); - shared_contents->append(image_contents.data(), partial_size); - - // Make sure the image decoder doesn't fail when we ask for the frame buffer - // for this partial image. - decoder->setData(shared_contents.get(), false); - EXPECT_FALSE(decoder->failed()) << path.value(); - // NOTE: We can't check that frame 0 is non-NULL, because if this is an ICO - // and we haven't yet supplied enough data to read the directory, there is - // no framecount and thus no first frame. - - // Make sure passing the complete image results in successful decoding. - shared_contents->append( - &image_contents.data()[partial_size], - static_cast<int>(image_contents.size() - partial_size)); - } else { - shared_contents->append(image_contents.data(), - static_cast<int>(image_contents.size())); - } - - decoder->setData(shared_contents.get(), true); - return decoder; -} - void ImageDecoderTest::TestDecoding( ImageDecoderTestFileSelection file_selection, - const int64 threshold) const { + const int64 threshold) { const std::vector<FilePath> image_files(GetImageFiles()); for (std::vector<FilePath>::const_iterator i = image_files.begin(); i != image_files.end(); ++i) { if (ShouldSkipFile(*i, file_selection, threshold)) continue; + const FilePath md5_sum_path(GetMD5SumPath(*i)); + TestWebKitImageDecoder(*i, md5_sum_path, kFirstFrameIndex); + } +} - scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, false)); - if (ShouldImageFail(*i)) { - // We may get a non-NULL frame buffer, but it should be incomplete, and - // the decoder should have failed. - WebCore::RGBA32Buffer* const image_buffer = - decoder->frameBufferAtIndex(0); - if (image_buffer) { - EXPECT_NE(image_buffer->status(), - WebCore::RGBA32Buffer::FrameComplete) << i->value(); - } - EXPECT_TRUE(decoder->failed()) << i->value(); - continue; - } - +void ImageDecoderTest::TestWebKitImageDecoder(const FilePath& image_path, + const FilePath& md5_sum_path, int desired_frame_index) const { + bool should_test_chunking = true; + bool should_test_failed_images = true; #ifdef CALCULATE_MD5_SUMS - SaveMD5Sum(GetMD5SumPath(*i), decoder->frameBufferAtIndex(0)); -#else - VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); + // Do not test anything just get the md5 sums. + should_test_chunking = false; + should_test_failed_images = false; #endif + + std::vector<char> image_contents; + ReadFileToVector(image_path, &image_contents); + EXPECT_TRUE(image_contents.size()); + scoped_ptr<WebKit::WebImageDecoder> decoder(CreateWebKitImageDecoder()); + EXPECT_FALSE(decoder->isFailed()); + + if (should_test_chunking) { + // Test chunking file into half. + const int partial_size = image_contents.size()/2; + + WebKit::WebData partial_data( + reinterpret_cast<const char*>(&(image_contents.at(0))), partial_size); + + // Make Sure the image decoder doesn't fail when we ask for the frame + // buffer for this partial image. + // NOTE: We can't check that frame 0 is non-NULL, because if this is an + // ICO and we haven't yet supplied enough data to read the directory, + // there is no framecount and thus no first frame. + decoder->setData(const_cast<WebKit::WebData&>(partial_data), false); + EXPECT_FALSE(decoder->isFailed()) << image_path.value(); } -} -#ifndef CALCULATE_MD5_SUMS -void ImageDecoderTest::TestChunkedDecoding( - ImageDecoderTestFileSelection file_selection, - const int64 threshold) const { - // Init random number generator with current day, so a failing case will fail - // consistently over the course of a whole day. - const Time today = Time::Now().LocalMidnight(); - srand(static_cast<unsigned int>(today.ToInternalValue())); + // Make sure passing the complete image results in successful decoding. + WebKit::WebData data(reinterpret_cast<const char*>(&(image_contents.at(0))), + image_contents.size()); + decoder->setData(const_cast<WebKit::WebData&>(data), true); - const std::vector<FilePath> image_files(GetImageFiles()); - for (std::vector<FilePath>::const_iterator i = image_files.begin(); - i != image_files.end(); ++i) { - if (ShouldSkipFile(*i, file_selection, threshold)) - continue; + if (should_test_failed_images) { + if (ShouldImageFail(image_path)) { + EXPECT_FALSE(decoder->isFrameCompleteAtIndex(kFirstFrameIndex)); + EXPECT_TRUE(decoder->isFailed()); + return; + } + } - if (ShouldImageFail(*i)) - continue; + EXPECT_FALSE(decoder->isFailed()) << image_path.value(); - scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, true)); - VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); - } -} +#ifdef CALCULATE_MD5_SUMS + // Since WebImage does not expose get data by frame, get the size + // through decoder and pass it to fromData so that the closest + // image dats to the size is returned. + WebKit::WebSize size(decoder->getImage(desired_frame_index).size()); + const WebKit::WebImage& image = WebKit::WebImage::fromData(data, size); + SaveMD5Sum(md5_sum_path.value(), image); +#else + VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index); #endif +} diff --git a/webkit/tools/test_shell/image_decoder_unittest.h b/webkit/tools/test_shell/image_decoder_unittest.h index 760ac2c..da6aa22 100644 --- a/webkit/tools/test_shell/image_decoder_unittest.h +++ b/webkit/tools/test_shell/image_decoder_unittest.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,15 +8,16 @@ #include <string> #include <vector> -#include "Vector.h" -#include "ImageDecoder.h" - #undef LOG #include "base/basictypes.h" #include "base/file_path.h" +#include "base/scoped_ptr.h" + #include "testing/gtest/include/gtest/gtest.h" +namespace WebKit { class WebImageDecoder; } + // If CALCULATE_MD5_SUMS is not defined, then this test decodes a handful of // image files and compares their MD5 sums to the stored sums on disk. // @@ -29,7 +30,7 @@ // will differ, since no endianness correction is done. If we start compiling // for big endian machines this should be fixed. -//#define CALCULATE_MD5_SUMS +// #define CALCULATE_MD5_SUMS enum ImageDecoderTestFileSelection { TEST_ALL, @@ -37,24 +38,9 @@ enum ImageDecoderTestFileSelection { TEST_BIGGER, }; -// Reads the contents of the specified file into the specified vector. -void ReadFileToVector(const FilePath& path, Vector<char>* contents); - // Returns the path the decoded data is saved at. FilePath GetMD5SumPath(const FilePath& path); -#ifdef CALCULATE_MD5_SUMS -// Saves the MD5 sum to the specified file. -void SaveMD5Sum(const FilePath& path, WebCore::RGBA32Buffer* buffer); -#else -// Verifies the image. |path| identifies the path the image was loaded from. -// |frame_index| indicates which index from the decoder we should examine. -void VerifyImage(WebCore::ImageDecoder* decoder, - const FilePath& path, - const FilePath& md5_sum_path, - size_t frame_index); -#endif - class ImageDecoderTest : public testing::Test { public: explicit ImageDecoderTest(const std::string& format) : format_(format) { } @@ -68,37 +54,25 @@ class ImageDecoderTest : public testing::Test { // Returns true if the image is bogus and should not be successfully decoded. bool ShouldImageFail(const FilePath& path) const; - // Creates and returns an ImageDecoder for the file at the given |path|. If - // |split_at_random| is true, also verifies that breaking the data supplied to - // the decoder into two random pieces does not cause problems. - WebCore::ImageDecoder* SetupDecoder(const FilePath& path, - bool split_at_random) const; + // Tests if decoder decodes image at image_path with underlying frame at + // index desired_frame_index. The md5_sum_path is needed if the test is not + // asked to generate one i.e. if # #define CALCULATE_MD5_SUMS is set. + void TestWebKitImageDecoder(const FilePath& image_path, + const FilePath& md5_sum_path, int desired_frame_index) const; // Verifies each of the test image files is decoded correctly and matches the // expected state. |file_selection| and |threshold| can be used to select // files to test based on file size. + // If just the MD5 sum is wanted, this skips chunking. void TestDecoding(ImageDecoderTestFileSelection file_selection, - const int64 threshold) const; + const int64 threshold); - void TestDecoding() const { + void TestDecoding() { TestDecoding(TEST_ALL, 0); } -#ifndef CALCULATE_MD5_SUMS - // Verifies that decoding still works correctly when the files are split into - // pieces at a random point. |file_selection| and |threshold| can be used to - // select files to test based on file size. - void TestChunkedDecoding(ImageDecoderTestFileSelection file_selection, - const int64 threshold) const; - - void TestChunkedDecoding() const { - TestChunkedDecoding(TEST_ALL, 0); - } - -#endif - - // Returns the correct type of image decoder for this test. - virtual WebCore::ImageDecoder* CreateDecoder() const = 0; + // Creates WebKit API's decoder. + virtual WebKit::WebImageDecoder* CreateWebKitImageDecoder() const = 0; // The format to be decoded, like "bmp" or "ico". std::string format_; diff --git a/webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp b/webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp index aa99023a..ea51d01 100644 --- a/webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp +++ b/webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp @@ -1,5 +1,4 @@ -// Copyright (c) 2008, Google Inc. -// All rights reserved. +// Copyright (c) 2008, Google Inc. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are @@ -28,17 +27,17 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "config.h" -#include "webkit/tools/test_shell/image_decoder_unittest.h" -#include "BMPImageDecoder.h" +#include "third_party/WebKit/WebKit/chromium/public/WebImageDecoder.h" +#include "webkit/tools/test_shell/image_decoder_unittest.h" class BMPImageDecoderTest : public ImageDecoderTest { public: BMPImageDecoderTest() : ImageDecoderTest("bmp") { } protected: - virtual WebCore::ImageDecoder* CreateDecoder() const { - return new WebCore::BMPImageDecoder(); + virtual WebKit::WebImageDecoder* CreateWebKitImageDecoder() const { + return new WebKit::WebImageDecoder(WebKit::WebImageDecoder::TypeBMP); } // The BMPImageDecoderTest tests are really slow under Valgrind. @@ -55,13 +54,3 @@ TEST_F(BMPImageDecoderTest, DecodingFast) { TEST_F(BMPImageDecoderTest, DecodingSlow) { TestDecoding(TEST_BIGGER, kThresholdSize); } - -#ifndef CALCULATE_MD5_SUMS -TEST_F(BMPImageDecoderTest, ChunkedDecodingFast) { - TestChunkedDecoding(TEST_SMALLER, kThresholdSize); -} - -TEST_F(BMPImageDecoderTest, ChunkedDecodingSlow) { - TestChunkedDecoding(TEST_BIGGER, kThresholdSize); -} -#endif diff --git a/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp b/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp index 95c5b15..cce64dd 100644 --- a/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp +++ b/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp @@ -28,20 +28,21 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "config.h" + +#include "base/file_path.h" #include "base/file_util.h" -#include "base/scoped_ptr.h" +#include "third_party/WebKit/WebKit/chromium/public/WebImageDecoder.h" #include "webkit/tools/test_shell/image_decoder_unittest.h" -#include "ICOImageDecoder.h" -#include "SharedBuffer.h" +using WebKit::WebImageDecoder; class ICOImageDecoderTest : public ImageDecoderTest { public: ICOImageDecoderTest() : ImageDecoderTest("ico") { } protected: - virtual WebCore::ImageDecoder* CreateDecoder() const { - return new WebCore::ICOImageDecoder(); + virtual WebKit::WebImageDecoder* CreateWebKitImageDecoder() const { + return new WebKit::WebImageDecoder(WebKit::WebImageDecoder::TypeICO); } }; @@ -49,28 +50,12 @@ TEST_F(ICOImageDecoderTest, Decoding) { TestDecoding(); } -#ifndef CALCULATE_MD5_SUMS -TEST_F(ICOImageDecoderTest, ChunkedDecoding) { - TestChunkedDecoding(); -} -#endif - -TEST_F(ICOImageDecoderTest, FaviconSize) { +TEST_F(ICOImageDecoderTest, ImageNonZeroFrameIndex) { // Test that the decoder decodes multiple sizes of icons which have them. - // Load an icon that has both favicon-size and larger entries. FilePath multisize_icon_path(data_dir_.AppendASCII("yahoo.ico")); - scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(multisize_icon_path, - false)); - - // Verify the decoding. const FilePath md5_sum_path( GetMD5SumPath(multisize_icon_path).value() + FILE_PATH_LITERAL("2")); static const int kDesiredFrameIndex = 3; -#ifdef CALCULATE_MD5_SUMS - SaveMD5Sum(md5_sum_path, decoder->frameBufferAtIndex(kDesiredFrameIndex)); -#else - VerifyImage(decoder.get(), multisize_icon_path, md5_sum_path, - kDesiredFrameIndex); -#endif + TestWebKitImageDecoder(multisize_icon_path, md5_sum_path, kDesiredFrameIndex); } |