OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "webkit/tools/test_shell/image_decoder_unittest.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/md5.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/path_service.h" | |
12 #include "base/string_util.h" | |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebData.h" | |
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebImage.h" | |
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebImageDecoder.h" | |
17 | |
18 using base::Time; | |
19 | |
20 namespace { | |
21 | |
22 const int kFirstFrameIndex = 0; | |
23 | |
24 // Determine if we should test with file specified by |path| based | |
25 // on |file_selection| and the |threshold| for the file size. | |
26 bool ShouldSkipFile(const base::FilePath& path, | |
27 ImageDecoderTestFileSelection file_selection, | |
28 const int64 threshold) { | |
29 if (file_selection == TEST_ALL) | |
30 return false; | |
31 | |
32 int64 image_size = 0; | |
33 file_util::GetFileSize(path, &image_size); | |
34 return (file_selection == TEST_SMALLER) == (image_size > threshold); | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 void ReadFileToVector(const base::FilePath& path, std::vector<char>* contents) { | |
40 std::string raw_image_data; | |
41 file_util::ReadFileToString(path, &raw_image_data); | |
42 contents->resize(raw_image_data.size()); | |
43 memcpy(&contents->at(0), raw_image_data.data(), raw_image_data.size()); | |
44 } | |
45 | |
46 base::FilePath GetMD5SumPath(const base::FilePath& path) { | |
47 static const base::FilePath::StringType kDecodedDataExtension( | |
48 FILE_PATH_LITERAL(".md5sum")); | |
49 return base::FilePath(path.value() + kDecodedDataExtension); | |
50 } | |
51 | |
52 #if defined(CALCULATE_MD5_SUMS) | |
53 void SaveMD5Sum(const base::FilePath& path, const WebKit::WebImage& web_image) { | |
54 // Calculate MD5 sum. | |
55 base::MD5Digest digest; | |
56 web_image.getSkBitmap().lockPixels(); | |
57 base::MD5Sum(web_image.getSkBitmap().getPixels(), | |
58 web_image.getSkBitmap().width() * web_image.getSkBitmap().height() * | |
59 sizeof(uint32_t), | |
60 &digest); | |
61 | |
62 // Write sum to disk. | |
63 int bytes_written = file_util::WriteFile(path, | |
64 reinterpret_cast<const char*>(&digest), sizeof digest); | |
65 ASSERT_EQ(sizeof digest, bytes_written); | |
66 web_image.getSkBitmap().unlockPixels(); | |
67 } | |
68 #endif | |
69 | |
70 #if !defined(CALCULATE_MD5_SUMS) | |
71 void VerifyImage(const WebKit::WebImageDecoder& decoder, | |
72 const base::FilePath& path, | |
73 const base::FilePath& md5_sum_path, | |
74 size_t frame_index) { | |
75 // Make sure decoding can complete successfully. | |
76 EXPECT_TRUE(decoder.isSizeAvailable()) << path.value(); | |
77 EXPECT_GE(decoder.frameCount(), frame_index) << path.value(); | |
78 EXPECT_TRUE(decoder.isFrameCompleteAtIndex(frame_index)) << path.value(); | |
79 EXPECT_FALSE(decoder.isFailed()); | |
80 | |
81 // Calculate MD5 sum. | |
82 base::MD5Digest actual_digest; | |
83 WebKit::WebImage web_image = decoder.getFrameAtIndex(frame_index); | |
84 web_image.getSkBitmap().lockPixels(); | |
85 base::MD5Sum(web_image.getSkBitmap().getPixels(), | |
86 web_image.getSkBitmap().width() * web_image.getSkBitmap().height() * | |
87 sizeof(uint32_t), | |
88 &actual_digest); | |
89 | |
90 // Read the MD5 sum off disk. | |
91 std::string file_bytes; | |
92 file_util::ReadFileToString(md5_sum_path, &file_bytes); | |
93 base::MD5Digest expected_digest; | |
94 ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path.value(); | |
95 memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest); | |
96 | |
97 // Verify that the sums are the same. | |
98 EXPECT_EQ(0, | |
99 memcmp(&expected_digest, &actual_digest, sizeof(base::MD5Digest))) | |
100 << path.value(); | |
101 web_image.getSkBitmap().unlockPixels(); | |
102 } | |
103 #endif | |
104 | |
105 void ImageDecoderTest::SetUp() { | |
106 base::FilePath data_dir; | |
107 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); | |
108 data_dir_ = data_dir.AppendASCII("webkit"). | |
109 AppendASCII("data"). | |
110 AppendASCII(format_ + "_decoder"); | |
111 ASSERT_TRUE(file_util::PathExists(data_dir_)) << data_dir_.value(); | |
112 } | |
113 | |
114 std::vector<base::FilePath> ImageDecoderTest::GetImageFiles() const { | |
115 std::string pattern = "*." + format_; | |
116 | |
117 file_util::FileEnumerator enumerator(data_dir_, | |
118 false, | |
119 file_util::FileEnumerator::FILES); | |
120 | |
121 std::vector<base::FilePath> image_files; | |
122 base::FilePath next_file_name; | |
123 while (!(next_file_name = enumerator.Next()).empty()) { | |
124 base::FilePath base_name = next_file_name.BaseName(); | |
125 #if defined(OS_WIN) | |
126 std::string base_name_ascii = WideToASCII(base_name.value()); | |
127 #else | |
128 std::string base_name_ascii = base_name.value(); | |
129 #endif | |
130 if (!MatchPattern(base_name_ascii, pattern)) | |
131 continue; | |
132 image_files.push_back(next_file_name); | |
133 } | |
134 | |
135 return image_files; | |
136 } | |
137 | |
138 bool ImageDecoderTest::ShouldImageFail(const base::FilePath& path) const { | |
139 static const base::FilePath::StringType kBadSuffix(FILE_PATH_LITERAL(".bad."))
; | |
140 return (path.value().length() > (kBadSuffix.length() + format_.length()) && | |
141 !path.value().compare(path.value().length() - format_.length() - | |
142 kBadSuffix.length(), | |
143 kBadSuffix.length(), kBadSuffix)); | |
144 } | |
145 | |
146 void ImageDecoderTest::TestDecoding( | |
147 ImageDecoderTestFileSelection file_selection, | |
148 const int64 threshold) { | |
149 const std::vector<base::FilePath> image_files(GetImageFiles()); | |
150 for (std::vector<base::FilePath>::const_iterator i = image_files.begin(); | |
151 i != image_files.end(); ++i) { | |
152 if (ShouldSkipFile(*i, file_selection, threshold)) | |
153 continue; | |
154 const base::FilePath md5_sum_path(GetMD5SumPath(*i)); | |
155 TestWebKitImageDecoder(*i, md5_sum_path, kFirstFrameIndex); | |
156 } | |
157 } | |
158 | |
159 void ImageDecoderTest::TestWebKitImageDecoder(const base::FilePath& image_path, | |
160 const base::FilePath& md5_sum_path, int desired_frame_index) const { | |
161 bool should_test_chunking = true; | |
162 bool should_test_failed_images = true; | |
163 #ifdef CALCULATE_MD5_SUMS | |
164 // Do not test anything just get the md5 sums. | |
165 should_test_chunking = false; | |
166 should_test_failed_images = false; | |
167 #endif | |
168 | |
169 std::vector<char> image_contents; | |
170 ReadFileToVector(image_path, &image_contents); | |
171 EXPECT_TRUE(image_contents.size()); | |
172 scoped_ptr<WebKit::WebImageDecoder> decoder(CreateWebKitImageDecoder()); | |
173 EXPECT_FALSE(decoder->isFailed()); | |
174 | |
175 if (should_test_chunking) { | |
176 // Test chunking file into half. | |
177 const int partial_size = image_contents.size()/2; | |
178 | |
179 WebKit::WebData partial_data( | |
180 reinterpret_cast<const char*>(&(image_contents.at(0))), partial_size); | |
181 | |
182 // Make Sure the image decoder doesn't fail when we ask for the frame | |
183 // buffer for this partial image. | |
184 // NOTE: We can't check that frame 0 is non-NULL, because if this is an | |
185 // ICO and we haven't yet supplied enough data to read the directory, | |
186 // there is no framecount and thus no first frame. | |
187 decoder->setData(const_cast<WebKit::WebData&>(partial_data), false); | |
188 EXPECT_FALSE(decoder->isFailed()) << image_path.value(); | |
189 } | |
190 | |
191 // Make sure passing the complete image results in successful decoding. | |
192 WebKit::WebData data(reinterpret_cast<const char*>(&(image_contents.at(0))), | |
193 image_contents.size()); | |
194 decoder->setData(const_cast<WebKit::WebData&>(data), true); | |
195 | |
196 if (should_test_failed_images) { | |
197 if (ShouldImageFail(image_path)) { | |
198 EXPECT_FALSE(decoder->isFrameCompleteAtIndex(kFirstFrameIndex)); | |
199 EXPECT_TRUE(decoder->isFailed()); | |
200 return; | |
201 } | |
202 } | |
203 | |
204 EXPECT_FALSE(decoder->isFailed()) << image_path.value(); | |
205 | |
206 #ifdef CALCULATE_MD5_SUMS | |
207 // Since WebImage does not expose get data by frame, get the size | |
208 // through decoder and pass it to fromData so that the closest | |
209 // image dats to the size is returned. | |
210 WebKit::WebSize size(decoder->getImage(desired_frame_index).size()); | |
211 const WebKit::WebImage& image = WebKit::WebImage::fromData(data, size); | |
212 SaveMD5Sum(md5_sum_path, image); | |
213 #else | |
214 VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index); | |
215 #endif | |
216 } | |
OLD | NEW |