diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-27 20:41:27 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-27 20:41:27 +0000 |
commit | 497e342c070df49e9cbe1e9f8c39e31477fb11ff (patch) | |
tree | 7b8e200c4bd6c6599e9229fde271d9af1ffdb872 /webkit | |
parent | aa15ca70b58a09192d3367070f6c7a14fac0887c (diff) | |
download | chromium_src-497e342c070df49e9cbe1e9f8c39e31477fb11ff.zip chromium_src-497e342c070df49e9cbe1e9f8c39e31477fb11ff.tar.gz chromium_src-497e342c070df49e9cbe1e9f8c39e31477fb11ff.tar.bz2 |
Refactor the "load and decode" portion of the image decoder unittest into a helper function, so that it will be easier for the ICO favicon test to ask for a particular frame.
Right now this isn't necessary; that test still wants frame 0. After I land some patches upstream, however, this will change (and all the "testing_favicon_size_" crap will disappear, it's just a temporary hack).
BUG=none
TEST=Covered by existing unittests
Review URL: http://codereview.chromium.org/160123
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21692 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r-- | webkit/tools/test_shell/image_decoder_unittest.cc | 92 | ||||
-rw-r--r-- | webkit/tools/test_shell/image_decoder_unittest.h | 10 | ||||
-rw-r--r-- | webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp | 31 |
3 files changed, 77 insertions, 56 deletions
diff --git a/webkit/tools/test_shell/image_decoder_unittest.cc b/webkit/tools/test_shell/image_decoder_unittest.cc index 0c767e8..7ce7ebb 100644 --- a/webkit/tools/test_shell/image_decoder_unittest.cc +++ b/webkit/tools/test_shell/image_decoder_unittest.cc @@ -64,10 +64,13 @@ void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) { #else void VerifyImage(WebCore::ImageDecoder* decoder, const std::wstring& path, - const std::wstring& md5_sum_path) { + const std::wstring& md5_sum_path, + size_t frame_index) { // Make sure decoding can complete successfully. EXPECT_TRUE(decoder->isSizeAvailable()) << path; - WebCore::RGBA32Buffer* image_buffer = decoder->frameBufferAtIndex(0); + EXPECT_GE(static_cast<size_t>(decoder->frameCount()), frame_index) << path; + WebCore::RGBA32Buffer* image_buffer = + decoder->frameBufferAtIndex(frame_index); ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << path; EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) << path; @@ -132,6 +135,42 @@ bool ImageDecoderTest::ShouldImageFail(const std::wstring& path) const { kBadSuffix.length(), kBadSuffix)); } +WebCore::ImageDecoder* ImageDecoderTest::SetupDecoder( + const std::wstring& path, + bool split_at_random) const { + Vector<char> image_contents; + ReadFileToVector(path, &image_contents); + + WebCore::ImageDecoder* decoder = CreateDecoder(); + RefPtr<WebCore::SharedBuffer> shared_contents( + WebCore::SharedBuffer::create()); + + if (split_at_random) { + // Split the file at an arbitrary point. + const int partial_size = static_cast<int>( + (static_cast<double>(rand()) / RAND_MAX) * image_contents.size()); + shared_contents->append(image_contents.data(), partial_size); + + // Make sure the image decoder doesn't fail when we ask for the frame buffer + // for this partial image. + decoder->setData(shared_contents.get(), false); + EXPECT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), + decoder->frameBufferAtIndex(0)) << path; + EXPECT_FALSE(decoder->failed()) << path; + + // Make sure passing the complete image results in successful decoding. + shared_contents->append( + &image_contents.data()[partial_size], + static_cast<int>(image_contents.size() - partial_size)); + } else { + shared_contents->append(image_contents.data(), + static_cast<int>(image_contents.size())); + } + + decoder->setData(shared_contents.get(), true); + return decoder; +} + void ImageDecoderTest::TestDecoding( ImageDecoderTestFileSelection file_selection, const int64 threshold) const { @@ -141,25 +180,16 @@ void ImageDecoderTest::TestDecoding( if (ShouldSkipFile(*i, file_selection, threshold)) continue; - Vector<char> image_contents; - ReadFileToVector(*i, &image_contents); - - scoped_ptr<WebCore::ImageDecoder> decoder(CreateDecoder()); - RefPtr<WebCore::SharedBuffer> shared_contents(WebCore::SharedBuffer::create()); - shared_contents->append(image_contents.data(), - static_cast<int>(image_contents.size())); - decoder->setData(shared_contents.get(), true); - + scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, false)); if (ShouldImageFail(*i)) { - // We should always get a non-NULL frame buffer, but when the decoder - // tries to produce it, it should fail, and the frame buffer shouldn't - // complete. + // We may get a non-NULL frame buffer, but it should be incomplete, and + // the decoder should have failed. WebCore::RGBA32Buffer* const image_buffer = decoder->frameBufferAtIndex(0); - ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << - (*i); - EXPECT_NE(image_buffer->status(), WebCore::RGBA32Buffer::FrameComplete) << - (*i); + if (image_buffer) { + EXPECT_NE(image_buffer->status(), + WebCore::RGBA32Buffer::FrameComplete) << (*i); + } EXPECT_TRUE(decoder->failed()) << (*i); continue; } @@ -167,7 +197,7 @@ void ImageDecoderTest::TestDecoding( #ifdef CALCULATE_MD5_SUMS SaveMD5Sum(GetMD5SumPath(*i), decoder->frameBufferAtIndex(0)); #else - VerifyImage(decoder.get(), *i, GetMD5SumPath(*i)); + VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); #endif } } @@ -190,28 +220,8 @@ void ImageDecoderTest::TestChunkedDecoding( if (ShouldImageFail(*i)) continue; - // Read the file and split it at an arbitrary point. - Vector<char> image_contents; - ReadFileToVector(*i, &image_contents); - const int partial_size = static_cast<int>( - (static_cast<double>(rand()) / RAND_MAX) * image_contents.size()); - RefPtr<WebCore::SharedBuffer> partial_contents(WebCore::SharedBuffer::create()); - partial_contents->append(image_contents.data(), partial_size); - - // Make sure the image decoder doesn't fail when we ask for the frame buffer - // for this partial image. - scoped_ptr<WebCore::ImageDecoder> decoder(CreateDecoder()); - decoder->setData(partial_contents.get(), false); - EXPECT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), - decoder->frameBufferAtIndex(0)) << (*i); - EXPECT_FALSE(decoder->failed()) << (*i); - - // Make sure passing the complete image results in successful decoding. - partial_contents->append( - &image_contents.data()[partial_size], - static_cast<int>(image_contents.size() - partial_size)); - decoder->setData(partial_contents.get(), true); - VerifyImage(decoder.get(), *i, GetMD5SumPath(*i)); + scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(*i, true)); + VerifyImage(decoder.get(), *i, GetMD5SumPath(*i), 0); } } #endif diff --git a/webkit/tools/test_shell/image_decoder_unittest.h b/webkit/tools/test_shell/image_decoder_unittest.h index 33c99d1..4b1a5d9 100644 --- a/webkit/tools/test_shell/image_decoder_unittest.h +++ b/webkit/tools/test_shell/image_decoder_unittest.h @@ -46,9 +46,11 @@ std::wstring GetMD5SumPath(const std::wstring& path); void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer); #else // Verifies the image. |path| identifies the path the image was loaded from. +// |frame_index| indicates which index from the decoder we should examine. void VerifyImage(WebCore::ImageDecoder* decoder, const std::wstring& path, - const std::wstring& md5_sum_path); + const std::wstring& md5_sum_path, + size_t frame_index); #endif class ImageDecoderTest : public testing::Test { @@ -64,6 +66,12 @@ class ImageDecoderTest : public testing::Test { // Returns true if the image is bogus and should not be successfully decoded. bool ShouldImageFail(const std::wstring& path) const; + // Creates and returns an ImageDecoder for the file at the given |path|. If + // |split_at_random| is true, also verifies that breaking the data supplied to + // the decoder into two random pieces does not cause problems. + WebCore::ImageDecoder* SetupDecoder(const std::wstring& path, + bool split_at_random) const; + // Verifies each of the test image files is decoded correctly and matches the // expected state. |file_selection| and |threshold| can be used to select // files to test based on file size. diff --git a/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp b/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp index 3d6b0f9..b3eb5f2 100644 --- a/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp +++ b/webkit/tools/webcore_unit_tests/ICOImageDecoder_unittest.cpp @@ -39,12 +39,18 @@ static const int kFavIconSize = 16; class ICOImageDecoderTest : public ImageDecoderTest { public: - ICOImageDecoderTest() : ImageDecoderTest(L"ico") { } + ICOImageDecoderTest() : ImageDecoderTest(L"ico"), + testing_favicon_size_(false) { } protected: virtual WebCore::ImageDecoder* CreateDecoder() const { - return new WebCore::ICOImageDecoder(WebCore::IntSize()); + WebCore::IntSize desired_size; + if (testing_favicon_size_) + desired_size = WebCore::IntSize(kFavIconSize, kFavIconSize); + return new WebCore::ICOImageDecoder(desired_size); } + + bool testing_favicon_size_; }; TEST_F(ICOImageDecoderTest, Decoding) { @@ -59,26 +65,23 @@ TEST_F(ICOImageDecoderTest, ChunkedDecoding) { TEST_F(ICOImageDecoderTest, FaviconSize) { // Test that the decoder decodes a preferred size when specified. + testing_favicon_size_ = true; // Load an icon that has both favicon-size and larger entries. std::wstring multisize_icon_path(data_dir_); file_util::AppendToPath(&multisize_icon_path, L"yahoo.ico"); - Vector<char> image_contents; - ReadFileToVector(multisize_icon_path, &image_contents); - - // Decode, preferring the favicon size. - scoped_ptr<WebCore::ImageDecoder> decoder(new WebCore::ICOImageDecoder( - WebCore::IntSize(kFavIconSize, kFavIconSize))); - RefPtr<WebCore::SharedBuffer> shared_contents(WebCore::SharedBuffer::create()); - shared_contents->append(image_contents.data(), - static_cast<int>(image_contents.size())); - decoder->setData(shared_contents.get(), true); + scoped_ptr<WebCore::ImageDecoder> decoder(SetupDecoder(multisize_icon_path, + false)); // Verify the decoding. const std::wstring md5_sum_path(GetMD5SumPath(multisize_icon_path) + L"2"); + static const int kDesiredFrameIndex = 0; #ifdef CALCULATE_MD5_SUMS - SaveMD5Sum(md5_sum_path, decoder->frameBufferAtIndex(0)); + SaveMD5Sum(md5_sum_path, decoder->frameBufferAtIndex(kDesiredFrameIndex)); #else - VerifyImage(decoder.get(), multisize_icon_path, md5_sum_path); + VerifyImage(decoder.get(), multisize_icon_path, md5_sum_path, + kDesiredFrameIndex); #endif + + testing_favicon_size_ = false; } |