| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/tools/test_shell/image_decoder_unittest.h" | 5 #include "webkit/tools/test_shell/image_decoder_unittest.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/md5.h" | 9 #include "base/md5.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebData.h" | 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebData.h" |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebImage.h" | 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebImage.h" |
| 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebImageDecoder.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebImageDecoder.h" |
| 17 | 17 |
| 18 using base::Time; | 18 using base::Time; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const int kFirstFrameIndex = 0; | 22 const int kFirstFrameIndex = 0; |
| 23 | 23 |
| 24 // Determine if we should test with file specified by |path| based | 24 // Determine if we should test with file specified by |path| based |
| 25 // on |file_selection| and the |threshold| for the file size. | 25 // on |file_selection| and the |threshold| for the file size. |
| 26 bool ShouldSkipFile(const FilePath& path, | 26 bool ShouldSkipFile(const base::FilePath& path, |
| 27 ImageDecoderTestFileSelection file_selection, | 27 ImageDecoderTestFileSelection file_selection, |
| 28 const int64 threshold) { | 28 const int64 threshold) { |
| 29 if (file_selection == TEST_ALL) | 29 if (file_selection == TEST_ALL) |
| 30 return false; | 30 return false; |
| 31 | 31 |
| 32 int64 image_size = 0; | 32 int64 image_size = 0; |
| 33 file_util::GetFileSize(path, &image_size); | 33 file_util::GetFileSize(path, &image_size); |
| 34 return (file_selection == TEST_SMALLER) == (image_size > threshold); | 34 return (file_selection == TEST_SMALLER) == (image_size > threshold); |
| 35 } | 35 } |
| 36 | 36 |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 void ReadFileToVector(const FilePath& path, std::vector<char>* contents) { | 39 void ReadFileToVector(const base::FilePath& path, std::vector<char>* contents) { |
| 40 std::string raw_image_data; | 40 std::string raw_image_data; |
| 41 file_util::ReadFileToString(path, &raw_image_data); | 41 file_util::ReadFileToString(path, &raw_image_data); |
| 42 contents->resize(raw_image_data.size()); | 42 contents->resize(raw_image_data.size()); |
| 43 memcpy(&contents->at(0), raw_image_data.data(), raw_image_data.size()); | 43 memcpy(&contents->at(0), raw_image_data.data(), raw_image_data.size()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 FilePath GetMD5SumPath(const FilePath& path) { | 46 base::FilePath GetMD5SumPath(const base::FilePath& path) { |
| 47 static const FilePath::StringType kDecodedDataExtension( | 47 static const base::FilePath::StringType kDecodedDataExtension( |
| 48 FILE_PATH_LITERAL(".md5sum")); | 48 FILE_PATH_LITERAL(".md5sum")); |
| 49 return FilePath(path.value() + kDecodedDataExtension); | 49 return base::FilePath(path.value() + kDecodedDataExtension); |
| 50 } | 50 } |
| 51 | 51 |
| 52 #if defined(CALCULATE_MD5_SUMS) | 52 #if defined(CALCULATE_MD5_SUMS) |
| 53 void SaveMD5Sum(const FilePath& path, const WebKit::WebImage& web_image) { | 53 void SaveMD5Sum(const base::FilePath& path, const WebKit::WebImage& web_image) { |
| 54 // Calculate MD5 sum. | 54 // Calculate MD5 sum. |
| 55 base::MD5Digest digest; | 55 base::MD5Digest digest; |
| 56 web_image.getSkBitmap().lockPixels(); | 56 web_image.getSkBitmap().lockPixels(); |
| 57 base::MD5Sum(web_image.getSkBitmap().getPixels(), | 57 base::MD5Sum(web_image.getSkBitmap().getPixels(), |
| 58 web_image.getSkBitmap().width() * web_image.getSkBitmap().height() * | 58 web_image.getSkBitmap().width() * web_image.getSkBitmap().height() * |
| 59 sizeof(uint32_t), | 59 sizeof(uint32_t), |
| 60 &digest); | 60 &digest); |
| 61 | 61 |
| 62 // Write sum to disk. | 62 // Write sum to disk. |
| 63 int bytes_written = file_util::WriteFile(path, | 63 int bytes_written = file_util::WriteFile(path, |
| 64 reinterpret_cast<const char*>(&digest), sizeof digest); | 64 reinterpret_cast<const char*>(&digest), sizeof digest); |
| 65 ASSERT_EQ(sizeof digest, bytes_written); | 65 ASSERT_EQ(sizeof digest, bytes_written); |
| 66 web_image.getSkBitmap().unlockPixels(); | 66 web_image.getSkBitmap().unlockPixels(); |
| 67 } | 67 } |
| 68 #endif | 68 #endif |
| 69 | 69 |
| 70 #if !defined(CALCULATE_MD5_SUMS) | 70 #if !defined(CALCULATE_MD5_SUMS) |
| 71 void VerifyImage(const WebKit::WebImageDecoder& decoder, | 71 void VerifyImage(const WebKit::WebImageDecoder& decoder, |
| 72 const FilePath& path, | 72 const base::FilePath& path, |
| 73 const FilePath& md5_sum_path, | 73 const base::FilePath& md5_sum_path, |
| 74 size_t frame_index) { | 74 size_t frame_index) { |
| 75 // Make sure decoding can complete successfully. | 75 // Make sure decoding can complete successfully. |
| 76 EXPECT_TRUE(decoder.isSizeAvailable()) << path.value(); | 76 EXPECT_TRUE(decoder.isSizeAvailable()) << path.value(); |
| 77 EXPECT_GE(decoder.frameCount(), frame_index) << path.value(); | 77 EXPECT_GE(decoder.frameCount(), frame_index) << path.value(); |
| 78 EXPECT_TRUE(decoder.isFrameCompleteAtIndex(frame_index)) << path.value(); | 78 EXPECT_TRUE(decoder.isFrameCompleteAtIndex(frame_index)) << path.value(); |
| 79 EXPECT_FALSE(decoder.isFailed()); | 79 EXPECT_FALSE(decoder.isFailed()); |
| 80 | 80 |
| 81 // Calculate MD5 sum. | 81 // Calculate MD5 sum. |
| 82 base::MD5Digest actual_digest; | 82 base::MD5Digest actual_digest; |
| 83 WebKit::WebImage web_image = decoder.getFrameAtIndex(frame_index); | 83 WebKit::WebImage web_image = decoder.getFrameAtIndex(frame_index); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 96 | 96 |
| 97 // Verify that the sums are the same. | 97 // Verify that the sums are the same. |
| 98 EXPECT_EQ(0, | 98 EXPECT_EQ(0, |
| 99 memcmp(&expected_digest, &actual_digest, sizeof(base::MD5Digest))) | 99 memcmp(&expected_digest, &actual_digest, sizeof(base::MD5Digest))) |
| 100 << path.value(); | 100 << path.value(); |
| 101 web_image.getSkBitmap().unlockPixels(); | 101 web_image.getSkBitmap().unlockPixels(); |
| 102 } | 102 } |
| 103 #endif | 103 #endif |
| 104 | 104 |
| 105 void ImageDecoderTest::SetUp() { | 105 void ImageDecoderTest::SetUp() { |
| 106 FilePath data_dir; | 106 base::FilePath data_dir; |
| 107 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); | 107 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
| 108 data_dir_ = data_dir.AppendASCII("webkit"). | 108 data_dir_ = data_dir.AppendASCII("webkit"). |
| 109 AppendASCII("data"). | 109 AppendASCII("data"). |
| 110 AppendASCII(format_ + "_decoder"); | 110 AppendASCII(format_ + "_decoder"); |
| 111 ASSERT_TRUE(file_util::PathExists(data_dir_)) << data_dir_.value(); | 111 ASSERT_TRUE(file_util::PathExists(data_dir_)) << data_dir_.value(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 std::vector<FilePath> ImageDecoderTest::GetImageFiles() const { | 114 std::vector<base::FilePath> ImageDecoderTest::GetImageFiles() const { |
| 115 std::string pattern = "*." + format_; | 115 std::string pattern = "*." + format_; |
| 116 | 116 |
| 117 file_util::FileEnumerator enumerator(data_dir_, | 117 file_util::FileEnumerator enumerator(data_dir_, |
| 118 false, | 118 false, |
| 119 file_util::FileEnumerator::FILES); | 119 file_util::FileEnumerator::FILES); |
| 120 | 120 |
| 121 std::vector<FilePath> image_files; | 121 std::vector<base::FilePath> image_files; |
| 122 FilePath next_file_name; | 122 base::FilePath next_file_name; |
| 123 while (!(next_file_name = enumerator.Next()).empty()) { | 123 while (!(next_file_name = enumerator.Next()).empty()) { |
| 124 FilePath base_name = next_file_name.BaseName(); | 124 base::FilePath base_name = next_file_name.BaseName(); |
| 125 #if defined(OS_WIN) | 125 #if defined(OS_WIN) |
| 126 std::string base_name_ascii = WideToASCII(base_name.value()); | 126 std::string base_name_ascii = WideToASCII(base_name.value()); |
| 127 #else | 127 #else |
| 128 std::string base_name_ascii = base_name.value(); | 128 std::string base_name_ascii = base_name.value(); |
| 129 #endif | 129 #endif |
| 130 if (!MatchPattern(base_name_ascii, pattern)) | 130 if (!MatchPattern(base_name_ascii, pattern)) |
| 131 continue; | 131 continue; |
| 132 image_files.push_back(next_file_name); | 132 image_files.push_back(next_file_name); |
| 133 } | 133 } |
| 134 | 134 |
| 135 return image_files; | 135 return image_files; |
| 136 } | 136 } |
| 137 | 137 |
| 138 bool ImageDecoderTest::ShouldImageFail(const FilePath& path) const { | 138 bool ImageDecoderTest::ShouldImageFail(const base::FilePath& path) const { |
| 139 static const FilePath::StringType kBadSuffix(FILE_PATH_LITERAL(".bad.")); | 139 static const base::FilePath::StringType kBadSuffix(FILE_PATH_LITERAL(".bad."))
; |
| 140 return (path.value().length() > (kBadSuffix.length() + format_.length()) && | 140 return (path.value().length() > (kBadSuffix.length() + format_.length()) && |
| 141 !path.value().compare(path.value().length() - format_.length() - | 141 !path.value().compare(path.value().length() - format_.length() - |
| 142 kBadSuffix.length(), | 142 kBadSuffix.length(), |
| 143 kBadSuffix.length(), kBadSuffix)); | 143 kBadSuffix.length(), kBadSuffix)); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void ImageDecoderTest::TestDecoding( | 146 void ImageDecoderTest::TestDecoding( |
| 147 ImageDecoderTestFileSelection file_selection, | 147 ImageDecoderTestFileSelection file_selection, |
| 148 const int64 threshold) { | 148 const int64 threshold) { |
| 149 const std::vector<FilePath> image_files(GetImageFiles()); | 149 const std::vector<base::FilePath> image_files(GetImageFiles()); |
| 150 for (std::vector<FilePath>::const_iterator i = image_files.begin(); | 150 for (std::vector<base::FilePath>::const_iterator i = image_files.begin(); |
| 151 i != image_files.end(); ++i) { | 151 i != image_files.end(); ++i) { |
| 152 if (ShouldSkipFile(*i, file_selection, threshold)) | 152 if (ShouldSkipFile(*i, file_selection, threshold)) |
| 153 continue; | 153 continue; |
| 154 const FilePath md5_sum_path(GetMD5SumPath(*i)); | 154 const base::FilePath md5_sum_path(GetMD5SumPath(*i)); |
| 155 TestWebKitImageDecoder(*i, md5_sum_path, kFirstFrameIndex); | 155 TestWebKitImageDecoder(*i, md5_sum_path, kFirstFrameIndex); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 void ImageDecoderTest::TestWebKitImageDecoder(const FilePath& image_path, | 159 void ImageDecoderTest::TestWebKitImageDecoder(const base::FilePath& image_path, |
| 160 const FilePath& md5_sum_path, int desired_frame_index) const { | 160 const base::FilePath& md5_sum_path, int desired_frame_index) const { |
| 161 bool should_test_chunking = true; | 161 bool should_test_chunking = true; |
| 162 bool should_test_failed_images = true; | 162 bool should_test_failed_images = true; |
| 163 #ifdef CALCULATE_MD5_SUMS | 163 #ifdef CALCULATE_MD5_SUMS |
| 164 // Do not test anything just get the md5 sums. | 164 // Do not test anything just get the md5 sums. |
| 165 should_test_chunking = false; | 165 should_test_chunking = false; |
| 166 should_test_failed_images = false; | 166 should_test_failed_images = false; |
| 167 #endif | 167 #endif |
| 168 | 168 |
| 169 std::vector<char> image_contents; | 169 std::vector<char> image_contents; |
| 170 ReadFileToVector(image_path, &image_contents); | 170 ReadFileToVector(image_path, &image_contents); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // Since WebImage does not expose get data by frame, get the size | 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 | 208 // through decoder and pass it to fromData so that the closest |
| 209 // image dats to the size is returned. | 209 // image dats to the size is returned. |
| 210 WebKit::WebSize size(decoder->getImage(desired_frame_index).size()); | 210 WebKit::WebSize size(decoder->getImage(desired_frame_index).size()); |
| 211 const WebKit::WebImage& image = WebKit::WebImage::fromData(data, size); | 211 const WebKit::WebImage& image = WebKit::WebImage::fromData(data, size); |
| 212 SaveMD5Sum(md5_sum_path, image); | 212 SaveMD5Sum(md5_sum_path, image); |
| 213 #else | 213 #else |
| 214 VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index); | 214 VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index); |
| 215 #endif | 215 #endif |
| 216 } | 216 } |
| OLD | NEW |