summaryrefslogtreecommitdiffstats
path: root/webkit/tools/test_shell/image_decoder_unittest.cc
blob: 9e640fde371632d7b1f244e06b001abc9fdb5a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "config.h"

#include <windows.h>

#include "base/file_util.h"
#include "base/md5.h"
#include "base/path_service.h"
#include "base/scoped_handle.h"
#include "base/scoped_ptr.h"
#include "base/time.h"
#include "webkit/tools/test_shell/image_decoder_unittest.h"

void ReadFileToVector(const std::wstring& path, Vector<char>* contents) {
  std::string contents_str;
  file_util::ReadFileToString(path, &contents_str);
  contents->resize(contents_str.size());
  memcpy(&contents->first(), contents_str.data(), contents_str.size());
}

std::wstring GetMD5SumPath(const std::wstring& path) {
  static const std::wstring kDecodedDataExtension(L".md5sum");
  return path + kDecodedDataExtension;
}

#ifdef CALCULATE_MD5_SUMS
void SaveMD5Sum(const std::wstring& path, WebCore::RGBA32Buffer* buffer) {
  // Create the file to write.
  ScopedHandle handle(CreateFile(path.c_str(), GENERIC_WRITE, 0,
                                 NULL, CREATE_ALWAYS,
                                 FILE_ATTRIBUTE_NORMAL, NULL));
  ASSERT_TRUE(handle.IsValid());

  // Calculate MD5 sum.
  MD5Digest digest;
  SkAutoLockPixels bmp_lock(buffer->bitmap()); 
  MD5Sum(buffer->bitmap().getPixels(),
         buffer->rect().width() * buffer->rect().height() * sizeof(unsigned),
         &digest);

  // Write sum to disk.
  DWORD bytes_written;
  ASSERT_TRUE(WriteFile(handle, &digest, sizeof digest, &bytes_written,
                        NULL));
  ASSERT_EQ(sizeof digest, bytes_written);
}
#else
void VerifyImage(WebCore::ImageDecoder* decoder,
                 const std::wstring& path,
                 const std::wstring& md5_sum_path) {
  // Make sure decoding can complete successfully.
  EXPECT_TRUE(decoder->isSizeAvailable()) << path;
  WebCore::RGBA32Buffer* image_buffer = decoder->frameBufferAtIndex(0);
  ASSERT_NE(static_cast<WebCore::RGBA32Buffer*>(NULL), image_buffer) << path;
  EXPECT_EQ(WebCore::RGBA32Buffer::FrameComplete, image_buffer->status()) <<
      path;
  EXPECT_FALSE(decoder->failed()) << path;

  // Calculate MD5 sum.
  MD5Digest actual_digest;
  SkAutoLockPixels bmp_lock(image_buffer->bitmap()); 
  MD5Sum(image_buffer->bitmap().getPixels(), image_buffer->rect().width() *
      image_buffer->rect().height() * sizeof(unsigned), &actual_digest);

  // Read the MD5 sum off disk.
  std::string file_bytes;
  file_util::ReadFileToString(md5_sum_path, &file_bytes);
  MD5Digest expected_digest;
  ASSERT_EQ(sizeof expected_digest, file_bytes.size()) << path;
  memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest);

  // Verify that the sums are the same.
  EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof(MD5Digest))) <<
      path;
}
#endif

void ImageDecoderTest::SetUp() {
  ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir_));
  file_util::AppendToPath(&data_dir_, L"webkit");
  file_util::AppendToPath(&data_dir_, L"data");
  file_util::AppendToPath(&data_dir_, format_ + L"_decoder");
  ASSERT_TRUE(file_util::PathExists(data_dir_));
}

std::vector<std::wstring> ImageDecoderTest::GetImageFiles() const {
  std::wstring find_string(data_dir_);
  file_util::AppendToPath(&find_string, L"*." + format_);
  WIN32_FIND_DATA find_data;
  ScopedFindFileHandle handle(FindFirstFile(find_string.c_str(),
                                            &find_data));
  EXPECT_TRUE(handle.IsValid());

  std::vector<std::wstring> image_files;
  do {
    std::wstring image_path = data_dir_;
    file_util::AppendToPath(&image_path, find_data.cFileName);
    image_files.push_back(image_path);
  } while (FindNextFile(handle, &find_data));

  return image_files;
}

bool ImageDecoderTest::ShouldImageFail(const std::wstring& path) const {
  static const std::wstring kBadSuffix(L".bad.");
  return (path.length() > (kBadSuffix.length() + format_.length()) &&
          !path.compare(path.length() - format_.length() - kBadSuffix.length(),
                        kBadSuffix.length(), kBadSuffix));
}

void ImageDecoderTest::TestDecoding() const {
  const std::vector<std::wstring> image_files(GetImageFiles());
  for (std::vector<std::wstring>::const_iterator i(image_files.begin());
       i != image_files.end(); ++i) {
    Vector<char> image_contents;
    ReadFileToVector(*i, &image_contents);

    scoped_ptr<WebCore::ImageDecoder> decoder(CreateDecoder());
    RefPtr<WebCore::SharedBuffer> shared_contents(new WebCore::SharedBuffer);
    shared_contents->append(image_contents.data(),
                            static_cast<int>(image_contents.size()));
    decoder->setData(shared_contents.get(), true);

    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.
      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);
      EXPECT_TRUE(decoder->failed()) << (*i);
      continue;
    }

#ifdef CALCULATE_MD5_SUMS
    SaveMD5Sum(GetMD5SumPath(*i), decoder->frameBufferAtIndex(0));
#else
    VerifyImage(decoder.get(), *i, GetMD5SumPath(*i));
#endif
  }
}

#ifndef CALCULATE_MD5_SUMS
void ImageDecoderTest::TestChunkedDecoding() const {
  // Init random number generator with current day, so a failing case will fail
  // consistently over the course of a whole day.
  const Time today = Time::Now().LocalMidnight();
  srand(static_cast<unsigned int>(today.ToInternalValue()));

  const std::vector<std::wstring> image_files(GetImageFiles());
  for (std::vector<std::wstring>::const_iterator i(image_files.begin());
       i != image_files.end(); ++i) {
    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(new WebCore::SharedBuffer);
    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));
  }
}
#endif