summaryrefslogtreecommitdiffstats
path: root/webkit/tools
diff options
context:
space:
mode:
authorkkanetkar@chromium.org <kkanetkar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-10 01:49:47 +0000
committerkkanetkar@chromium.org <kkanetkar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-10 01:49:47 +0000
commit5ec41b8410e776699defd61afe439a7b8938fb03 (patch)
tree9f3d6c6e032f227ca9150a62d66e1945c9c9b073 /webkit/tools
parent39af4c8aa1caa19e46dff48b1951fcad5dafb212 (diff)
downloadchromium_src-5ec41b8410e776699defd61afe439a7b8938fb03.zip
chromium_src-5ec41b8410e776699defd61afe439a7b8938fb03.tar.gz
chromium_src-5ec41b8410e776699defd61afe439a7b8938fb03.tar.bz2
Revert 40951 - 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 TBR=yaar@chromium.org Review URL: http://codereview.chromium.org/802001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41112 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/tools')
-rw-r--r--webkit/tools/test_shell/image_decoder_unittest.cc203
-rw-r--r--webkit/tools/test_shell/image_decoder_unittest.h58
-rw-r--r--webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp21
-rw-r--r--webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp31
4 files changed, 192 insertions, 121 deletions
diff --git a/webkit/tools/test_shell/image_decoder_unittest.cc b/webkit/tools/test_shell/image_decoder_unittest.cc
index 917e1b3..7384ff30 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-2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2008 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,17 +12,12 @@
#include "base/path_service.h"
#include "base/scoped_ptr.h"
#include "base/string_util.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"
+#include "base/time.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,
@@ -38,11 +33,11 @@ bool ShouldSkipFile(const FilePath& path,
} // anonymous namespace
-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());
+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());
}
FilePath GetMD5SumPath(const FilePath& path) {
@@ -51,43 +46,48 @@ FilePath GetMD5SumPath(const FilePath& path) {
return FilePath(path.value() + kDecodedDataExtension);
}
-#if defined(CALCULATE_MD5_SUMS)
-void SaveMD5Sum(const std::wstring& path, const WebKit::WebImage& web_image) {
+#ifdef CALCULATE_MD5_SUMS
+void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) {
// Calculate MD5 sum.
MD5Digest digest;
- web_image.getSkBitmap().lockPixels();
- MD5Sum(web_image.getSkBitmap().getPixels(),
- web_image.getSkBitmap().width() * web_image.getSkBitmap().height() *
- sizeof(uint32_t),
- &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);
+ }
// 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();
}
-#endif
-
-#if !defined(CALCULATE_MD5_SUMS)
-void VerifyImage(const WebKit::WebImageDecoder& decoder,
+#else
+void VerifyImage(WebCore::ImageDecoder* 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();
- EXPECT_TRUE(decoder.isFrameCompleteAtIndex(frame_index)) << path.value();
- EXPECT_FALSE(decoder.isFailed());
+ 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();
// Calculate MD5 sum.
MD5Digest 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);
+ 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);
+ }
// Read the MD5 sum off disk.
std::string file_bytes;
@@ -97,9 +97,8 @@ void VerifyImage(const WebKit::WebImageDecoder& 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();
- web_image.getSkBitmap().unlockPixels();
+ EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) <<
+ path.value();
}
#endif
@@ -144,74 +143,94 @@ 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 int64 threshold) const {
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);
- }
-}
-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;
+ 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;
+ }
+
#ifdef CALCULATE_MD5_SUMS
- // Do not test anything just get the md5 sums.
- should_test_chunking = false;
- should_test_failed_images = false;
+ SaveMD5Sum(GetMD5SumPath(*i), decoder->frameBufferAtIndex(0));
+#else
+ VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0);
#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();
}
+}
- // 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);
+#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()));
- if (should_test_failed_images) {
- if (ShouldImageFail(image_path)) {
- EXPECT_FALSE(decoder->isFrameCompleteAtIndex(kFirstFrameIndex));
- EXPECT_TRUE(decoder->isFailed());
- return;
- }
- }
+ 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;
- EXPECT_FALSE(decoder->isFailed()) << image_path.value();
+ if (ShouldImageFail(*i))
+ continue;
-#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
+ scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, true));
+ VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0);
+ }
}
+#endif
diff --git a/webkit/tools/test_shell/image_decoder_unittest.h b/webkit/tools/test_shell/image_decoder_unittest.h
index da6aa22..760ac2c 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-2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2006-2008 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,16 +8,15 @@
#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.
//
@@ -30,7 +29,7 @@ namespace WebKit { class WebImageDecoder; }
// 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,
@@ -38,9 +37,24 @@ 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) { }
@@ -54,25 +68,37 @@ class ImageDecoderTest : public testing::Test {
// Returns true if the image is bogus and should not be successfully decoded.
bool ShouldImageFail(const FilePath& path) 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;
+ // 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;
// 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 int64 threshold) const;
- void TestDecoding() {
+ void TestDecoding() const {
TestDecoding(TEST_ALL, 0);
}
- // Creates WebKit API's decoder.
- virtual WebKit::WebImageDecoder* CreateWebKitImageDecoder() const = 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;
// 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 ea51d01..aa99023a 100644
--- a/webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp
+++ b/webkit/tools/webcore_unit_tests/BMPImageDecoder_unittest.cpp
@@ -1,4 +1,5 @@
-// 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
@@ -27,17 +28,17 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "config.h"
-
-#include "third_party/WebKit/WebKit/chromium/public/WebImageDecoder.h"
#include "webkit/tools/test_shell/image_decoder_unittest.h"
+#include "BMPImageDecoder.h"
+
class BMPImageDecoderTest : public ImageDecoderTest {
public:
BMPImageDecoderTest() : ImageDecoderTest("bmp") { }
protected:
- virtual WebKit::WebImageDecoder* CreateWebKitImageDecoder() const {
- return new WebKit::WebImageDecoder(WebKit::WebImageDecoder::TypeBMP);
+ virtual WebCore::ImageDecoder* CreateDecoder() const {
+ return new WebCore::BMPImageDecoder();
}
// The BMPImageDecoderTest tests are really slow under Valgrind.
@@ -54,3 +55,13 @@ 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 cce64dd..95c5b15 100644
--- a/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp
+++ b/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp
@@ -28,21 +28,20 @@
// 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 "third_party/WebKit/WebKit/chromium/public/WebImageDecoder.h"
+#include "base/scoped_ptr.h"
#include "webkit/tools/test_shell/image_decoder_unittest.h"
-using WebKit::WebImageDecoder;
+#include "ICOImageDecoder.h"
+#include "SharedBuffer.h"
class ICOImageDecoderTest : public ImageDecoderTest {
public:
ICOImageDecoderTest() : ImageDecoderTest("ico") { }
protected:
- virtual WebKit::WebImageDecoder* CreateWebKitImageDecoder() const {
- return new WebKit::WebImageDecoder(WebKit::WebImageDecoder::TypeICO);
+ virtual WebCore::ImageDecoder* CreateDecoder() const {
+ return new WebCore::ICOImageDecoder();
}
};
@@ -50,12 +49,28 @@ TEST_F(ICOImageDecoderTest, Decoding) {
TestDecoding();
}
-TEST_F(ICOImageDecoderTest, ImageNonZeroFrameIndex) {
+#ifndef CALCULATE_MD5_SUMS
+TEST_F(ICOImageDecoderTest, ChunkedDecoding) {
+ TestChunkedDecoding();
+}
+#endif
+
+TEST_F(ICOImageDecoderTest, FaviconSize) {
// 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;
- TestWebKitImageDecoder(multisize_icon_path, md5_sum_path, kDesiredFrameIndex);
+#ifdef CALCULATE_MD5_SUMS
+ SaveMD5Sum(md5_sum_path, decoder->frameBufferAtIndex(kDesiredFrameIndex));
+#else
+ VerifyImage(decoder.get(), multisize_icon_path, md5_sum_path,
+ kDesiredFrameIndex);
+#endif
}