Skip to content

Commit

Permalink
Fix ImageDecoder tests to allow MD5 sum calculation to compile again.
Browse files Browse the repository at this point in the history
This also fixes a variety of style errors (e.g. incorrect wrapping/indenting or
use of protected data members), and cleans up the code some (e.g. by bailing
early for invalid images in MD5 sum generation mode).

BUG=none
TEST=none
R=darin@chromium.org

Review URL: https://codereview.chromium.org/213543003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@259940 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
pkasting@chromium.org committed Mar 27, 2014
1 parent 84fd4d9 commit 7b9fc9c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 85 deletions.
2 changes: 1 addition & 1 deletion content/renderer/bmp_image_decoder_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST_F(BMPImageDecoderTest, DecodingFast) {
}

#if defined(THREAD_SANITIZER)
// BMPImageDecoderTest.DecodingSlow als times out under ThreadSanitizer v2.
// BMPImageDecoderTest.DecodingSlow always times out under ThreadSanitizer v2.
#define MAYBE_DecodingSlow DISABLED_DecodingSlow
#else
#define MAYBE_DecodingSlow DecodingSlow
Expand Down
4 changes: 2 additions & 2 deletions content/renderer/ico_image_decoder_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ TEST_F(ICOImageDecoderTest, Decoding) {
}

TEST_F(ICOImageDecoderTest, ImageNonZeroFrameIndex) {
if (data_dir_.empty())
if (data_dir().empty())
return;
// Test that the decoder decodes multiple sizes of icons which have them.
// Load an icon that has both favicon-size and larger entries.
base::FilePath multisize_icon_path(data_dir_.AppendASCII("yahoo.ico"));
base::FilePath multisize_icon_path(data_dir().AppendASCII("yahoo.ico"));
const base::FilePath md5_sum_path(
GetMD5SumPath(multisize_icon_path).value() + FILE_PATH_LITERAL("2"));
static const int kDesiredFrameIndex = 3;
Expand Down
129 changes: 56 additions & 73 deletions content/test/image_decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#include "third_party/WebKit/public/platform/WebSize.h"
#include "third_party/WebKit/public/web/WebImageDecoder.h"

using base::Time;
// Uncomment this to recalculate the MD5 sums; see header comments.
// #define CALCULATE_MD5_SUMS

namespace {

Expand All @@ -42,7 +43,7 @@ void ReadFileToVector(const base::FilePath& path, std::vector<char>* contents) {
std::string raw_image_data;
base::ReadFileToString(path, &raw_image_data);
contents->resize(raw_image_data.size());
memcpy(&contents->at(0), raw_image_data.data(), raw_image_data.size());
memcpy(&contents->front(), raw_image_data.data(), raw_image_data.size());
}

base::FilePath GetMD5SumPath(const base::FilePath& path) {
Expand All @@ -57,13 +58,13 @@ void SaveMD5Sum(const base::FilePath& path, const blink::WebImage& web_image) {
base::MD5Digest digest;
web_image.getSkBitmap().lockPixels();
base::MD5Sum(web_image.getSkBitmap().getPixels(),
web_image.getSkBitmap().width() * web_image.getSkBitmap().height() *
sizeof(uint32_t),
&digest);
web_image.getSkBitmap().width() *
web_image.getSkBitmap().height() * sizeof(uint32_t),
&digest);

// Write sum to disk.
int bytes_written = base::WriteFile(path,
reinterpret_cast<const char*>(&digest), sizeof digest);
int bytes_written = base::WriteFile(
path, reinterpret_cast<const char*>(&digest), sizeof digest);
ASSERT_EQ(sizeof digest, bytes_written);
web_image.getSkBitmap().unlockPixels();
}
Expand All @@ -85,9 +86,9 @@ void VerifyImage(const blink::WebImageDecoder& decoder,
blink::WebImage web_image = decoder.getFrameAtIndex(frame_index);
web_image.getSkBitmap().lockPixels();
base::MD5Sum(web_image.getSkBitmap().getPixels(),
web_image.getSkBitmap().width() * web_image.getSkBitmap().height() *
sizeof(uint32_t),
&actual_digest);
web_image.getSkBitmap().width() *
web_image.getSkBitmap().height() * sizeof(uint32_t),
&actual_digest);

// Read the MD5 sum off disk.
std::string file_bytes;
Expand All @@ -97,48 +98,42 @@ void VerifyImage(const blink::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(base::MD5Digest)))
<< path.value();
EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof actual_digest))
<< path.value();
web_image.getSkBitmap().unlockPixels();
}
#endif

void ImageDecoderTest::SetUp() {
base::FilePath data_dir;
ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir));
data_dir_ = data_dir.AppendASCII("webkit").
AppendASCII("data").
AppendASCII(format_ + "_decoder");
data_dir_ = data_dir.AppendASCII("webkit").AppendASCII("data").AppendASCII(
format_ + "_decoder");
if (!base::PathExists(data_dir_)) {
const testing::TestInfo* const test_info =
testing::UnitTest::GetInstance()->current_test_info();
VLOG(0) << test_info->name() <<
" not running because test data wasn't found.";
VLOG(0) << test_info->name()
<< " not running because test data wasn't found.";
data_dir_.clear();
return;
}
}

std::vector<base::FilePath> ImageDecoderTest::GetImageFiles() const {
std::string pattern = "*." + format_;

base::FileEnumerator enumerator(data_dir_,
false,
base::FileEnumerator enumerator(data_dir_, false,
base::FileEnumerator::FILES);

std::vector<base::FilePath> image_files;
base::FilePath next_file_name;
while (!(next_file_name = enumerator.Next()).empty()) {
for (base::FilePath next_file_name = enumerator.Next();
!next_file_name.empty(); next_file_name = enumerator.Next()) {
base::FilePath base_name = next_file_name.BaseName();
#if defined(OS_WIN)
std::string base_name_ascii = base::UTF16ToASCII(base_name.value());
#else
std::string base_name_ascii = base_name.value();
#endif
if (!MatchPattern(base_name_ascii, pattern))
continue;
image_files.push_back(next_file_name);
if (MatchPattern(base_name_ascii, pattern))
image_files.push_back(next_file_name);
}

return image_files;
Expand All @@ -160,21 +155,19 @@ void ImageDecoderTest::TestDecoding(
const std::vector<base::FilePath> image_files(GetImageFiles());
for (std::vector<base::FilePath>::const_iterator i = image_files.begin();
i != image_files.end(); ++i) {
if (ShouldSkipFile(*i, file_selection, threshold))
continue;
const base::FilePath md5_sum_path(GetMD5SumPath(*i));
TestWebKitImageDecoder(*i, md5_sum_path, kFirstFrameIndex);
if (!ShouldSkipFile(*i, file_selection, threshold))
TestWebKitImageDecoder(*i, GetMD5SumPath(*i), kFirstFrameIndex);
}
}

void ImageDecoderTest::TestWebKitImageDecoder(const base::FilePath& image_path,
const base::FilePath& md5_sum_path, int desired_frame_index) const {
bool should_test_chunking = true;
bool should_test_failed_images = true;
#ifdef CALCULATE_MD5_SUMS
// Do not test anything just get the md5 sums.
should_test_chunking = false;
should_test_failed_images = false;
void ImageDecoderTest::TestWebKitImageDecoder(
const base::FilePath& image_path,
const base::FilePath& md5_sum_path,
int desired_frame_index) const {
#if defined(CALCULATE_MD5_SUMS)
// If we're just calculating the MD5 sums, skip failing images quickly.
if (ShouldImageFail(image_path))
return;
#endif

std::vector<char> image_contents;
Expand All @@ -183,45 +176,35 @@ void ImageDecoderTest::TestWebKitImageDecoder(const base::FilePath& image_path,
scoped_ptr<blink::WebImageDecoder> decoder(CreateWebKitImageDecoder());
EXPECT_FALSE(decoder->isFailed());

if (should_test_chunking) {
// Test chunking file into half.
const int partial_size = image_contents.size()/2;

blink::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<blink::WebData&>(partial_data), false);
EXPECT_FALSE(decoder->isFailed()) << image_path.value();
}
#if !defined(CALCULATE_MD5_SUMS)
// Test chunking file into half.
const int partial_size = image_contents.size()/2;

blink::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<blink::WebData&>(partial_data), false);
EXPECT_FALSE(decoder->isFailed()) << image_path.value();
#endif

// Make sure passing the complete image results in successful decoding.
blink::WebData data(reinterpret_cast<const char*>(&(image_contents.at(0))),
image_contents.size());
decoder->setData(const_cast<blink::WebData&>(data), true);

if (should_test_failed_images) {
if (ShouldImageFail(image_path)) {
EXPECT_FALSE(decoder->isFrameCompleteAtIndex(kFirstFrameIndex));
EXPECT_TRUE(decoder->isFailed());
return;
}
}

EXPECT_FALSE(decoder->isFailed()) << image_path.value();

#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.
blink::WebSize size(decoder->getImage(desired_frame_index).size());
const blink::WebImage& image = blink::WebImage::fromData(data, size);
SaveMD5Sum(md5_sum_path, image);
if (ShouldImageFail(image_path)) {
EXPECT_FALSE(decoder->isFrameCompleteAtIndex(kFirstFrameIndex));
EXPECT_TRUE(decoder->isFailed());
} else {
EXPECT_FALSE(decoder->isFailed()) << image_path.value();
#if defined(CALCULATE_MD5_SUMS)
SaveMD5Sum(md5_sum_path, decoder->getFrameAtIndex(desired_frame_index));
#else
VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index);
VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index);
#endif
}
}
18 changes: 9 additions & 9 deletions content/test/image_decoder_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

namespace blink { 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.
//
// To recalculate the MD5 sums, uncommment CALCULATE_MD5_SUMS.
// Decodes a handful of image files and compares their MD5 sums to the stored
// sums on disk. To recalculate the MD5 sums, uncomment the CALCULATE_MD5_SUMS
// #define in the .cc file.
//
// The image files and corresponding MD5 sums live in the directory
// chrome/test/data/*_decoder (where "*" is the format being tested).
Expand All @@ -28,8 +27,6 @@ namespace blink { 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

enum ImageDecoderTestFileSelection {
TEST_ALL,
TEST_SMALLER,
Expand All @@ -54,9 +51,10 @@ class ImageDecoderTest : public testing::Test {

// 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.
// asked to generate one, i.e. if #define CALCULATE_MD5_SUMS is not set.
void TestWebKitImageDecoder(const base::FilePath& image_path,
const base::FilePath& md5_sum_path, int desired_frame_index) const;
const base::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
Expand All @@ -76,10 +74,12 @@ class ImageDecoderTest : public testing::Test {
std::string format_;

protected:
const base::FilePath& data_dir() const { return data_dir_; }

private:
// Path to the test files.
base::FilePath data_dir_;

private:
DISALLOW_COPY_AND_ASSIGN(ImageDecoderTest);
};

Expand Down

0 comments on commit 7b9fc9c

Please sign in to comment.