OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 | 6 |
7 #include "webkit/tools/test_shell/image_decoder_unittest.h" | 7 #include "webkit/tools/test_shell/image_decoder_unittest.h" |
8 | 8 |
| 9 #include "base/file_path.h" |
9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
10 #include "base/md5.h" | 11 #include "base/md5.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/scoped_ptr.h" | 13 #include "base/scoped_ptr.h" |
13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
14 #include "base/time.h" | 15 #include "base/time.h" |
15 | 16 |
16 using base::Time; | 17 using base::Time; |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // Determine if we should test with file specified by |path| based | 21 // Determine if we should test with file specified by |path| based |
21 // on |file_selection| and the |threshold| for the file size. | 22 // on |file_selection| and the |threshold| for the file size. |
22 bool ShouldSkipFile(const std::wstring& path, | 23 bool ShouldSkipFile(const FilePath& path, |
23 ImageDecoderTestFileSelection file_selection, | 24 ImageDecoderTestFileSelection file_selection, |
24 const int64 threshold) { | 25 const int64 threshold) { |
25 if (file_selection == TEST_ALL) | 26 if (file_selection == TEST_ALL) |
26 return false; | 27 return false; |
27 | 28 |
28 int64 image_size = 0; | 29 int64 image_size = 0; |
29 file_util::GetFileSize(path, &image_size); | 30 file_util::GetFileSize(path, &image_size); |
30 return (file_selection == TEST_SMALLER) == (image_size > threshold); | 31 return (file_selection == TEST_SMALLER) == (image_size > threshold); |
31 } | 32 } |
32 | 33 |
33 } // anonymous namespace | 34 } // anonymous namespace |
34 | 35 |
35 void ReadFileToVector(const std::wstring& path, Vector<char>* contents) { | 36 void ReadFileToVector(const FilePath& path, Vector<char>* contents) { |
36 std::string contents_str; | 37 std::string contents_str; |
37 file_util::ReadFileToString(path, &contents_str); | 38 file_util::ReadFileToString(path, &contents_str); |
38 contents->resize(contents_str.size()); | 39 contents->resize(contents_str.size()); |
39 memcpy(&contents->first(), contents_str.data(), contents_str.size()); | 40 memcpy(&contents->first(), contents_str.data(), contents_str.size()); |
40 } | 41 } |
41 | 42 |
42 std::wstring GetMD5SumPath(const std::wstring& path) { | 43 FilePath GetMD5SumPath(const FilePath& path) { |
43 static const std::wstring kDecodedDataExtension(L".md5sum"); | 44 static const FilePath::StringType kDecodedDataExtension( |
44 return path + kDecodedDataExtension; | 45 FILE_PATH_LITERAL(".md5sum")); |
| 46 return FilePath(path.value() + kDecodedDataExtension); |
45 } | 47 } |
46 | 48 |
47 #ifdef CALCULATE_MD5_SUMS | 49 #ifdef CALCULATE_MD5_SUMS |
48 void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) { | 50 void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) { |
49 // Calculate MD5 sum. | 51 // Calculate MD5 sum. |
50 MD5Digest digest; | 52 MD5Digest digest; |
51 scoped_ptr<NativeImageSkia> image_data(buffer->asNewNativeImage()); | 53 scoped_ptr<NativeImageSkia> image_data(buffer->asNewNativeImage()); |
52 { | 54 { |
53 SkAutoLockPixels bmp_lock(*image_data); | 55 SkAutoLockPixels bmp_lock(*image_data); |
54 MD5Sum(image_data->getPixels(), | 56 MD5Sum(image_data->getPixels(), |
55 image_data->width() * image_data->height() * sizeof(uint32_t), | 57 image_data->width() * image_data->height() * sizeof(uint32_t), |
56 &digest); | 58 &digest); |
57 } | 59 } |
58 | 60 |
59 // Write sum to disk. | 61 // Write sum to disk. |
60 int bytes_written = file_util::WriteFile(path, | 62 int bytes_written = file_util::WriteFile(path, |
61 reinterpret_cast<const char*>(&digest), sizeof digest); | 63 reinterpret_cast<const char*>(&digest), sizeof digest); |
62 ASSERT_EQ(sizeof digest, bytes_written); | 64 ASSERT_EQ(sizeof digest, bytes_written); |
63 } | 65 } |
64 #else | 66 #else |
65 void VerifyImage(WebCore::ImageDecoder* decoder, | 67 void VerifyImage(WebCore::ImageDecoder* decoder, |
66 const std::wstring& path, | 68 const FilePath& path, |
67 const std::wstring& md5_sum_path, | 69 const FilePath& md5_sum_path, |
68 size_t frame_index) { | 70 size_t frame_index) { |
69 // Make sure decoding can complete successfully. | 71 // Make sure decoding can complete successfully. |
70 EXPECT_TRUE(decoder->isSizeAvailable()) << path; | 72 EXPECT_TRUE(decoder->isSizeAvailable()) << path.value(); |
71 EXPECT_GE(decoder->frameCount(), frame_index) << path; | 73 EXPECT_GE(decoder->frameCount(), frame_index) << path.value(); |
72 WebCore::RGBA32Buffer* image_buffer = | 74 WebCore::RGBA32Buffer* image_buffer = |
73 decoder->frameBufferAtIndex(frame_index); | 75 decoder->frameBufferAtIndex(frame_index); |
74 ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << path; | 76 ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << |
| 77 path.value(); |
75 EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) << | 78 EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) << |
76 path; | 79 path.value(); |
77 EXPECT_FALSE(decoder->failed()) << path; | 80 EXPECT_FALSE(decoder->failed()) << path.value(); |
78 | 81 |
79 // Calculate MD5 sum. | 82 // Calculate MD5 sum. |
80 MD5Digest actual_digest; | 83 MD5Digest actual_digest; |
81 scoped_ptr<NativeImageSkia> image_data(image_buffer->asNewNativeImage()); | 84 scoped_ptr<NativeImageSkia> image_data(image_buffer->asNewNativeImage()); |
82 { | 85 { |
83 SkAutoLockPixels bmp_lock(*image_data); | 86 SkAutoLockPixels bmp_lock(*image_data); |
84 MD5Sum(image_data->getPixels(), | 87 MD5Sum(image_data->getPixels(), |
85 image_data->width() * image_data->height() * sizeof(uint32_t), | 88 image_data->width() * image_data->height() * sizeof(uint32_t), |
86 &actual_digest); | 89 &actual_digest); |
87 } | 90 } |
88 | 91 |
89 // Read the MD5 sum off disk. | 92 // Read the MD5 sum off disk. |
90 std::string file_bytes; | 93 std::string file_bytes; |
91 file_util::ReadFileToString(md5_sum_path, &file_bytes); | 94 file_util::ReadFileToString(md5_sum_path, &file_bytes); |
92 MD5Digest expected_digest; | 95 MD5Digest expected_digest; |
93 ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path; | 96 ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path.value(); |
94 memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest); | 97 memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest); |
95 | 98 |
96 // Verify that the sums are the same. | 99 // Verify that the sums are the same. |
97 EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) << | 100 EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) << |
98 path; | 101 path.value(); |
99 } | 102 } |
100 #endif | 103 #endif |
101 | 104 |
102 void ImageDecoderTest::SetUp() { | 105 void ImageDecoderTest::SetUp() { |
103 FilePath data_dir; | 106 FilePath data_dir; |
104 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); | 107 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
105 data_dir_ = data_dir.ToWStringHack(); | 108 data_dir_ = data_dir.AppendASCII("webkit"). |
106 file_util::AppendToPath(&data_dir_, L"webkit"); | 109 AppendASCII("data"). |
107 file_util::AppendToPath(&data_dir_, L"data"); | 110 AppendASCII(format_ + "_decoder"); |
108 file_util::AppendToPath(&data_dir_, format_ + L"_decoder"); | 111 ASSERT_TRUE(file_util::PathExists(data_dir_)) << data_dir_.value(); |
109 ASSERT_TRUE(file_util::PathExists(data_dir_)) << data_dir_; | |
110 } | 112 } |
111 | 113 |
112 std::vector<std::wstring> ImageDecoderTest::GetImageFiles() const { | 114 std::vector<FilePath> ImageDecoderTest::GetImageFiles() const { |
113 std::wstring pattern = L"*." + format_; | 115 #if defined(OS_WIN) |
| 116 std::wstring pattern = ASCIIToWide("*." + format_); |
| 117 #else |
| 118 std::string pattern = "*." + format_; |
| 119 #endif |
114 | 120 |
115 file_util::FileEnumerator enumerator(FilePath::FromWStringHack(data_dir_), | 121 file_util::FileEnumerator enumerator(data_dir_, |
116 false, | 122 false, |
117 file_util::FileEnumerator::FILES); | 123 file_util::FileEnumerator::FILES); |
118 | 124 |
119 std::vector<std::wstring> image_files; | 125 std::vector<FilePath> image_files; |
120 std::wstring next_file_name; | 126 FilePath next_file_name; |
121 while ((next_file_name = enumerator.Next().ToWStringHack()) != L"") { | 127 while (!(next_file_name = enumerator.Next()).empty()) { |
122 if (!MatchPattern(next_file_name, pattern)) { | 128 if (!MatchPattern(next_file_name.value(), pattern)) { |
123 continue; | 129 continue; |
124 } | 130 } |
125 image_files.push_back(next_file_name); | 131 image_files.push_back(next_file_name); |
126 } | 132 } |
127 | 133 |
128 return image_files; | 134 return image_files; |
129 } | 135 } |
130 | 136 |
131 bool ImageDecoderTest::ShouldImageFail(const std::wstring& path) const { | 137 bool ImageDecoderTest::ShouldImageFail(const FilePath& path) const { |
132 static const std::wstring kBadSuffix(L".bad."); | 138 static const FilePath::StringType kBadSuffix(FILE_PATH_LITERAL(".bad.")); |
133 return (path.length() > (kBadSuffix.length() + format_.length()) && | 139 return (path.value().length() > (kBadSuffix.length() + format_.length()) && |
134 !path.compare(path.length() - format_.length() - kBadSuffix.length(), | 140 !path.value().compare(path.value().length() - format_.length() - |
135 kBadSuffix.length(), kBadSuffix)); | 141 kBadSuffix.length(), |
| 142 kBadSuffix.length(), kBadSuffix)); |
136 } | 143 } |
137 | 144 |
138 WebCore::ImageDecoder* ImageDecoderTest::SetupDecoder( | 145 WebCore::ImageDecoder* ImageDecoderTest::SetupDecoder( |
139 const std::wstring& path, | 146 const FilePath& path, |
140 bool split_at_random) const { | 147 bool split_at_random) const { |
141 Vector<char> image_contents; | 148 Vector<char> image_contents; |
142 ReadFileToVector(path, &image_contents); | 149 ReadFileToVector(path, &image_contents); |
143 | 150 |
144 WebCore::ImageDecoder* decoder = CreateDecoder(); | 151 WebCore::ImageDecoder* decoder = CreateDecoder(); |
145 RefPtr<WebCore::SharedBuffer> shared_contents( | 152 RefPtr<WebCore::SharedBuffer> shared_contents( |
146 WebCore::SharedBuffer::create()); | 153 WebCore::SharedBuffer::create()); |
147 | 154 |
148 if (split_at_random) { | 155 if (split_at_random) { |
149 // Split the file at an arbitrary point. | 156 // Split the file at an arbitrary point. |
150 const int partial_size = static_cast<int>( | 157 const int partial_size = static_cast<int>( |
151 (static_cast<double>(rand()) / RAND_MAX) * image_contents.size()); | 158 (static_cast<double>(rand()) / RAND_MAX) * image_contents.size()); |
152 shared_contents->append(image_contents.data(), partial_size); | 159 shared_contents->append(image_contents.data(), partial_size); |
153 | 160 |
154 // Make sure the image decoder doesn't fail when we ask for the frame buffer | 161 // Make sure the image decoder doesn't fail when we ask for the frame buffer |
155 // for this partial image. | 162 // for this partial image. |
156 decoder->setData(shared_contents.get(), false); | 163 decoder->setData(shared_contents.get(), false); |
157 EXPECT_FALSE(decoder->failed()) << path; | 164 EXPECT_FALSE(decoder->failed()) << path.value(); |
158 // NOTE: We can't check that frame 0 is non-NULL, because if this is an ICO | 165 // NOTE: We can't check that frame 0 is non-NULL, because if this is an ICO |
159 // and we haven't yet supplied enough data to read the directory, there is | 166 // and we haven't yet supplied enough data to read the directory, there is |
160 // no framecount and thus no first frame. | 167 // no framecount and thus no first frame. |
161 | 168 |
162 // Make sure passing the complete image results in successful decoding. | 169 // Make sure passing the complete image results in successful decoding. |
163 shared_contents->append( | 170 shared_contents->append( |
164 &image_contents.data()[partial_size], | 171 &image_contents.data()[partial_size], |
165 static_cast<int>(image_contents.size() - partial_size)); | 172 static_cast<int>(image_contents.size() - partial_size)); |
166 } else { | 173 } else { |
167 shared_contents->append(image_contents.data(), | 174 shared_contents->append(image_contents.data(), |
168 static_cast<int>(image_contents.size())); | 175 static_cast<int>(image_contents.size())); |
169 } | 176 } |
170 | 177 |
171 decoder->setData(shared_contents.get(), true); | 178 decoder->setData(shared_contents.get(), true); |
172 return decoder; | 179 return decoder; |
173 } | 180 } |
174 | 181 |
175 void ImageDecoderTest::TestDecoding( | 182 void ImageDecoderTest::TestDecoding( |
176 ImageDecoderTestFileSelection file_selection, | 183 ImageDecoderTestFileSelection file_selection, |
177 const int64 threshold) const { | 184 const int64 threshold) const { |
178 const std::vector<std::wstring> image_files(GetImageFiles()); | 185 const std::vector<FilePath> image_files(GetImageFiles()); |
179 for (std::vector<std::wstring>::const_iterator i(image_files.begin()); | 186 for (std::vector<FilePath>::const_iterator i = image_files.begin(); |
180 i != image_files.end(); ++i) { | 187 i != image_files.end(); ++i) { |
181 if (ShouldSkipFile(*i, file_selection, threshold)) | 188 if (ShouldSkipFile(*i, file_selection, threshold)) |
182 continue; | 189 continue; |
183 | 190 |
184 scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, false)); | 191 scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, false)); |
185 if (ShouldImageFail(*i)) { | 192 if (ShouldImageFail(*i)) { |
186 // We may get a non-NULL frame buffer, but it should be incomplete, and | 193 // We may get a non-NULL frame buffer, but it should be incomplete, and |
187 // the decoder should have failed. | 194 // the decoder should have failed. |
188 WebCore::RGBA32Buffer* const image_buffer = | 195 WebCore::RGBA32Buffer* const image_buffer = |
189 decoder->frameBufferAtIndex(0); | 196 decoder->frameBufferAtIndex(0); |
190 if (image_buffer) { | 197 if (image_buffer) { |
191 EXPECT_NE(image_buffer->status(), | 198 EXPECT_NE(image_buffer->status(), |
192 WebCore::RGBA32Buffer::FrameComplete) << (*i); | 199 WebCore::RGBA32Buffer::FrameComplete) << i->value(); |
193 } | 200 } |
194 EXPECT_TRUE(decoder->failed()) << (*i); | 201 EXPECT_TRUE(decoder->failed()) << i->value(); |
195 continue; | 202 continue; |
196 } | 203 } |
197 | 204 |
198 #ifdef CALCULATE_MD5_SUMS | 205 #ifdef CALCULATE_MD5_SUMS |
199 SaveMD5Sum(GetMD5SumPath(*i), decoder->frameBufferAtIndex(0)); | 206 SaveMD5Sum(GetMD5SumPath(*i), decoder->frameBufferAtIndex(0)); |
200 #else | 207 #else |
201 VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); | 208 VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); |
202 #endif | 209 #endif |
203 } | 210 } |
204 } | 211 } |
205 | 212 |
206 #ifndef CALCULATE_MD5_SUMS | 213 #ifndef CALCULATE_MD5_SUMS |
207 void ImageDecoderTest::TestChunkedDecoding( | 214 void ImageDecoderTest::TestChunkedDecoding( |
208 ImageDecoderTestFileSelection file_selection, | 215 ImageDecoderTestFileSelection file_selection, |
209 const int64 threshold) const { | 216 const int64 threshold) const { |
210 // Init random number generator with current day, so a failing case will fail | 217 // Init random number generator with current day, so a failing case will fail |
211 // consistently over the course of a whole day. | 218 // consistently over the course of a whole day. |
212 const Time today = Time::Now().LocalMidnight(); | 219 const Time today = Time::Now().LocalMidnight(); |
213 srand(static_cast<unsigned int>(today.ToInternalValue())); | 220 srand(static_cast<unsigned int>(today.ToInternalValue())); |
214 | 221 |
215 const std::vector<std::wstring> image_files(GetImageFiles()); | 222 const std::vector<FilePath> image_files(GetImageFiles()); |
216 for (std::vector<std::wstring>::const_iterator i(image_files.begin()); | 223 for (std::vector<FilePath>::const_iterator i = image_files.begin(); |
217 i != image_files.end(); ++i) { | 224 i != image_files.end(); ++i) { |
218 if (ShouldSkipFile(*i, file_selection, threshold)) | 225 if (ShouldSkipFile(*i, file_selection, threshold)) |
219 continue; | 226 continue; |
220 | 227 |
221 if (ShouldImageFail(*i)) | 228 if (ShouldImageFail(*i)) |
222 continue; | 229 continue; |
223 | 230 |
224 scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, true)); | 231 scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, true)); |
225 VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); | 232 VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); |
226 } | 233 } |
227 } | 234 } |
228 #endif | 235 #endif |
OLD | NEW |