1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/test/image_decoder_test.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/strings/pattern.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "third_party/WebKit/public/platform/WebData.h"
17 #include "third_party/WebKit/public/platform/WebImage.h"
18 #include "third_party/WebKit/public/platform/WebSize.h"
19 #include "third_party/WebKit/public/web/WebImageDecoder.h"
21 // Uncomment this to recalculate the MD5 sums; see header comments.
22 // #define CALCULATE_MD5_SUMS
26 const int kFirstFrameIndex
= 0;
28 // Determine if we should test with file specified by |path| based
29 // on |file_selection| and the |threshold| for the file size.
30 bool ShouldSkipFile(const base::FilePath
& path
,
31 ImageDecoderTestFileSelection file_selection
,
32 const int64 threshold
) {
33 if (file_selection
== TEST_ALL
)
37 base::GetFileSize(path
, &image_size
);
38 return (file_selection
== TEST_SMALLER
) == (image_size
> threshold
);
43 void ReadFileToVector(const base::FilePath
& path
, std::vector
<char>* contents
) {
44 std::string raw_image_data
;
45 base::ReadFileToString(path
, &raw_image_data
);
46 contents
->resize(raw_image_data
.size());
47 memcpy(&contents
->front(), raw_image_data
.data(), raw_image_data
.size());
50 base::FilePath
GetMD5SumPath(const base::FilePath
& path
) {
51 static const base::FilePath::StringType
kDecodedDataExtension(
52 FILE_PATH_LITERAL(".md5sum"));
53 return base::FilePath(path
.value() + kDecodedDataExtension
);
56 #if defined(CALCULATE_MD5_SUMS)
57 void SaveMD5Sum(const base::FilePath
& path
, const blink::WebImage
& web_image
) {
59 base::MD5Digest digest
;
60 web_image
.getSkBitmap().lockPixels();
61 base::MD5Sum(web_image
.getSkBitmap().getPixels(),
62 web_image
.getSkBitmap().width() *
63 web_image
.getSkBitmap().height() * sizeof(uint32_t),
67 int bytes_written
= base::WriteFile(
68 path
, reinterpret_cast<const char*>(&digest
), sizeof digest
);
69 ASSERT_EQ(sizeof digest
, bytes_written
);
70 web_image
.getSkBitmap().unlockPixels();
74 #if !defined(CALCULATE_MD5_SUMS)
75 void VerifyImage(const blink::WebImageDecoder
& decoder
,
76 const base::FilePath
& path
,
77 const base::FilePath
& md5_sum_path
,
79 // Make sure decoding can complete successfully.
80 EXPECT_TRUE(decoder
.isSizeAvailable()) << path
.value();
81 EXPECT_GE(decoder
.frameCount(), frame_index
) << path
.value();
82 EXPECT_TRUE(decoder
.isFrameCompleteAtIndex(frame_index
)) << path
.value();
83 EXPECT_FALSE(decoder
.isFailed());
86 base::MD5Digest actual_digest
;
87 blink::WebImage web_image
= decoder
.getFrameAtIndex(frame_index
);
88 web_image
.getSkBitmap().lockPixels();
89 base::MD5Sum(web_image
.getSkBitmap().getPixels(),
90 web_image
.getSkBitmap().width() *
91 web_image
.getSkBitmap().height() * sizeof(uint32_t),
94 // Read the MD5 sum off disk.
95 std::string file_bytes
;
96 base::ReadFileToString(md5_sum_path
, &file_bytes
);
97 base::MD5Digest expected_digest
;
98 ASSERT_EQ(sizeof expected_digest
, file_bytes
.size()) << path
.value();
99 memcpy(&expected_digest
, file_bytes
.data(), sizeof expected_digest
);
101 // Verify that the sums are the same.
102 EXPECT_EQ(0, memcmp(&expected_digest
, &actual_digest
, sizeof actual_digest
))
104 web_image
.getSkBitmap().unlockPixels();
108 void ImageDecoderTest::SetUp() {
109 base::FilePath data_dir
;
110 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &data_dir
));
111 data_dir_
= data_dir
.AppendASCII("webkit").AppendASCII("data").AppendASCII(
112 format_
+ "_decoder");
113 if (!base::PathExists(data_dir_
)) {
114 const testing::TestInfo
* const test_info
=
115 testing::UnitTest::GetInstance()->current_test_info();
116 VLOG(0) << test_info
->name()
117 << " not running because test data wasn't found.";
123 std::vector
<base::FilePath
> ImageDecoderTest::GetImageFiles() const {
124 std::string pattern
= "*." + format_
;
125 base::FileEnumerator
enumerator(data_dir_
, false,
126 base::FileEnumerator::FILES
);
127 std::vector
<base::FilePath
> image_files
;
128 for (base::FilePath next_file_name
= enumerator
.Next();
129 !next_file_name
.empty(); next_file_name
= enumerator
.Next()) {
130 base::FilePath base_name
= next_file_name
.BaseName();
132 std::string base_name_ascii
= base::UTF16ToASCII(base_name
.value());
134 std::string base_name_ascii
= base_name
.value();
136 if (base::MatchPattern(base_name_ascii
, pattern
))
137 image_files
.push_back(next_file_name
);
143 bool ImageDecoderTest::ShouldImageFail(const base::FilePath
& path
) const {
144 const base::FilePath::StringType
kBadSuffix(FILE_PATH_LITERAL(".bad."));
145 return (path
.value().length() > (kBadSuffix
.length() + format_
.length()) &&
146 !path
.value().compare(path
.value().length() - format_
.length() -
148 kBadSuffix
.length(), kBadSuffix
));
151 void ImageDecoderTest::TestDecoding(
152 ImageDecoderTestFileSelection file_selection
,
153 const int64 threshold
) {
154 if (data_dir_
.empty())
156 const std::vector
<base::FilePath
> image_files(GetImageFiles());
157 for (std::vector
<base::FilePath
>::const_iterator i
= image_files
.begin();
158 i
!= image_files
.end(); ++i
) {
159 if (!ShouldSkipFile(*i
, file_selection
, threshold
))
160 TestWebKitImageDecoder(*i
, GetMD5SumPath(*i
), kFirstFrameIndex
);
164 void ImageDecoderTest::TestWebKitImageDecoder(
165 const base::FilePath
& image_path
,
166 const base::FilePath
& md5_sum_path
,
167 int desired_frame_index
) const {
168 #if defined(CALCULATE_MD5_SUMS)
169 // If we're just calculating the MD5 sums, skip failing images quickly.
170 if (ShouldImageFail(image_path
))
174 std::vector
<char> image_contents
;
175 ReadFileToVector(image_path
, &image_contents
);
176 EXPECT_TRUE(image_contents
.size());
177 scoped_ptr
<blink::WebImageDecoder
> decoder(CreateWebKitImageDecoder());
178 EXPECT_FALSE(decoder
->isFailed());
180 #if !defined(CALCULATE_MD5_SUMS)
181 // Test chunking file into half.
182 const int partial_size
= image_contents
.size()/2;
184 blink::WebData
partial_data(
185 reinterpret_cast<const char*>(&(image_contents
.at(0))), partial_size
);
187 // Make Sure the image decoder doesn't fail when we ask for the frame
188 // buffer for this partial image.
189 // NOTE: We can't check that frame 0 is non-NULL, because if this is an
190 // ICO and we haven't yet supplied enough data to read the directory,
191 // there is no framecount and thus no first frame.
192 decoder
->setData(const_cast<blink::WebData
&>(partial_data
), false);
193 EXPECT_FALSE(decoder
->isFailed()) << image_path
.value();
196 // Make sure passing the complete image results in successful decoding.
197 blink::WebData
data(reinterpret_cast<const char*>(&(image_contents
.at(0))),
198 image_contents
.size());
199 decoder
->setData(const_cast<blink::WebData
&>(data
), true);
200 if (ShouldImageFail(image_path
)) {
201 EXPECT_FALSE(decoder
->isFrameCompleteAtIndex(kFirstFrameIndex
));
202 EXPECT_TRUE(decoder
->isFailed());
204 EXPECT_FALSE(decoder
->isFailed()) << image_path
.value();
205 #if defined(CALCULATE_MD5_SUMS)
206 SaveMD5Sum(md5_sum_path
, decoder
->getFrameAtIndex(desired_frame_index
));
208 VerifyImage(*decoder
, image_path
, md5_sum_path
, desired_frame_index
);