| 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 "content/test/image_decoder_test.h" | 5 #include "content/test/image_decoder_test.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 base::FilePath GetMD5SumPath(const base::FilePath& path) { | 54 base::FilePath GetMD5SumPath(const base::FilePath& path) { |
| 55 static const base::FilePath::StringType kDecodedDataExtension( | 55 static const base::FilePath::StringType kDecodedDataExtension( |
| 56 FILE_PATH_LITERAL(".md5sum")); | 56 FILE_PATH_LITERAL(".md5sum")); |
| 57 return base::FilePath(path.value() + kDecodedDataExtension); | 57 return base::FilePath(path.value() + kDecodedDataExtension); |
| 58 } | 58 } |
| 59 | 59 |
| 60 #if defined(CALCULATE_MD5_SUMS) | 60 #if defined(CALCULATE_MD5_SUMS) |
| 61 void SaveMD5Sum(const base::FilePath& path, const blink::WebImage& web_image) { | 61 void SaveMD5Sum(const base::FilePath& path, const blink::WebImage& web_image) { |
| 62 // Calculate MD5 sum. | 62 // Calculate MD5 sum. |
| 63 base::MD5Digest digest; | 63 base::MD5Digest digest; |
| 64 web_image.getSkBitmap().lockPixels(); | |
| 65 base::MD5Sum(web_image.getSkBitmap().getPixels(), | 64 base::MD5Sum(web_image.getSkBitmap().getPixels(), |
| 66 web_image.getSkBitmap().width() * | 65 web_image.getSkBitmap().width() * |
| 67 web_image.getSkBitmap().height() * sizeof(uint32_t), | 66 web_image.getSkBitmap().height() * sizeof(uint32_t), |
| 68 &digest); | 67 &digest); |
| 69 | 68 |
| 70 // Write sum to disk. | 69 // Write sum to disk. |
| 71 int bytes_written = base::WriteFile( | 70 int bytes_written = base::WriteFile( |
| 72 path, reinterpret_cast<const char*>(&digest), sizeof digest); | 71 path, reinterpret_cast<const char*>(&digest), sizeof digest); |
| 73 ASSERT_EQ(sizeof digest, bytes_written); | 72 ASSERT_EQ(sizeof digest, bytes_written); |
| 74 web_image.getSkBitmap().unlockPixels(); | |
| 75 } | 73 } |
| 76 #endif | 74 #endif |
| 77 | 75 |
| 78 #if !defined(CALCULATE_MD5_SUMS) | 76 #if !defined(CALCULATE_MD5_SUMS) |
| 79 void VerifyImage(const blink::WebImageDecoder& decoder, | 77 void VerifyImage(const blink::WebImageDecoder& decoder, |
| 80 const base::FilePath& path, | 78 const base::FilePath& path, |
| 81 const base::FilePath& md5_sum_path, | 79 const base::FilePath& md5_sum_path, |
| 82 size_t frame_index) { | 80 size_t frame_index) { |
| 83 // Make sure decoding can complete successfully. | 81 // Make sure decoding can complete successfully. |
| 84 EXPECT_TRUE(decoder.IsSizeAvailable()) << path.value(); | 82 EXPECT_TRUE(decoder.IsSizeAvailable()) << path.value(); |
| 85 EXPECT_GE(decoder.FrameCount(), frame_index) << path.value(); | 83 EXPECT_GE(decoder.FrameCount(), frame_index) << path.value(); |
| 86 EXPECT_TRUE(decoder.IsFrameCompleteAtIndex(frame_index)) << path.value(); | 84 EXPECT_TRUE(decoder.IsFrameCompleteAtIndex(frame_index)) << path.value(); |
| 87 EXPECT_FALSE(decoder.IsFailed()); | 85 EXPECT_FALSE(decoder.IsFailed()); |
| 88 | 86 |
| 89 // Calculate MD5 sum. | 87 // Calculate MD5 sum. |
| 90 base::MD5Digest actual_digest; | 88 base::MD5Digest actual_digest; |
| 91 blink::WebImage web_image = decoder.GetFrameAtIndex(frame_index); | 89 blink::WebImage web_image = decoder.GetFrameAtIndex(frame_index); |
| 92 web_image.GetSkBitmap().lockPixels(); | |
| 93 base::MD5Sum(web_image.GetSkBitmap().getPixels(), | 90 base::MD5Sum(web_image.GetSkBitmap().getPixels(), |
| 94 web_image.GetSkBitmap().width() * | 91 web_image.GetSkBitmap().width() * |
| 95 web_image.GetSkBitmap().height() * sizeof(uint32_t), | 92 web_image.GetSkBitmap().height() * sizeof(uint32_t), |
| 96 &actual_digest); | 93 &actual_digest); |
| 97 | 94 |
| 98 // Read the MD5 sum off disk. | 95 // Read the MD5 sum off disk. |
| 99 std::string file_bytes; | 96 std::string file_bytes; |
| 100 base::ReadFileToString(md5_sum_path, &file_bytes); | 97 base::ReadFileToString(md5_sum_path, &file_bytes); |
| 101 base::MD5Digest expected_digest; | 98 base::MD5Digest expected_digest; |
| 102 ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path.value(); | 99 ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path.value(); |
| 103 memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest); | 100 memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest); |
| 104 | 101 |
| 105 // Verify that the sums are the same. | 102 // Verify that the sums are the same. |
| 106 EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof actual_digest)) | 103 EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof actual_digest)) |
| 107 << path.value(); | 104 << path.value(); |
| 108 web_image.GetSkBitmap().unlockPixels(); | |
| 109 } | 105 } |
| 110 #endif | 106 #endif |
| 111 | 107 |
| 112 void ImageDecoderTest::SetUp() { | 108 void ImageDecoderTest::SetUp() { |
| 113 base::FilePath data_dir; | 109 base::FilePath data_dir; |
| 114 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); | 110 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir)); |
| 115 data_dir_ = data_dir.AppendASCII("webkit").AppendASCII("data").AppendASCII( | 111 data_dir_ = data_dir.AppendASCII("webkit").AppendASCII("data").AppendASCII( |
| 116 format_ + "_decoder"); | 112 format_ + "_decoder"); |
| 117 if (!base::PathExists(data_dir_)) { | 113 if (!base::PathExists(data_dir_)) { |
| 118 const testing::TestInfo* const test_info = | 114 const testing::TestInfo* const test_info = |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 EXPECT_TRUE(decoder->IsFailed()); | 202 EXPECT_TRUE(decoder->IsFailed()); |
| 207 } else { | 203 } else { |
| 208 EXPECT_FALSE(decoder->IsFailed()) << image_path.value(); | 204 EXPECT_FALSE(decoder->IsFailed()) << image_path.value(); |
| 209 #if defined(CALCULATE_MD5_SUMS) | 205 #if defined(CALCULATE_MD5_SUMS) |
| 210 SaveMD5Sum(md5_sum_path, decoder->getFrameAtIndex(desired_frame_index)); | 206 SaveMD5Sum(md5_sum_path, decoder->getFrameAtIndex(desired_frame_index)); |
| 211 #else | 207 #else |
| 212 VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index); | 208 VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index); |
| 213 #endif | 209 #endif |
| 214 } | 210 } |
| 215 } | 211 } |
| OLD | NEW |