Home | History | Annotate | Download | only in test
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CONTENT_TEST_IMAGE_DECODER_TEST_H_
      6 #define CONTENT_TEST_IMAGE_DECODER_TEST_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/files/file_path.h"
     14 
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace blink { class WebImageDecoder; }
     18 
     19 // Decodes a handful of image files and compares their MD5 sums to the stored
     20 // sums on disk.  To recalculate the MD5 sums, uncomment the CALCULATE_MD5_SUMS
     21 // #define in the .cc file.
     22 //
     23 // The image files and corresponding MD5 sums live in the directory
     24 // chrome/test/data/*_decoder (where "*" is the format being tested).
     25 //
     26 // Note: The MD5 sums calculated in this test by little- and big-endian systems
     27 // will differ, since no endianness correction is done.  If we start compiling
     28 // for big endian machines this should be fixed.
     29 
     30 enum ImageDecoderTestFileSelection {
     31   TEST_ALL,
     32   TEST_SMALLER,
     33   TEST_BIGGER,
     34 };
     35 
     36 // Returns the path the decoded data is saved at.
     37 base::FilePath GetMD5SumPath(const base::FilePath& path);
     38 
     39 class ImageDecoderTest : public testing::Test {
     40  public:
     41   explicit ImageDecoderTest(const std::string& format) : format_(format) { }
     42 
     43  protected:
     44   virtual void SetUp() OVERRIDE;
     45 
     46   // Returns the vector of image files for testing.
     47   std::vector<base::FilePath> GetImageFiles() const;
     48 
     49   // Returns true if the image is bogus and should not be successfully decoded.
     50   bool ShouldImageFail(const base::FilePath& path) const;
     51 
     52   // Tests if decoder decodes image at image_path with underlying frame at
     53   // index desired_frame_index. The md5_sum_path is needed if the test is not
     54   // asked to generate one, i.e. if #define CALCULATE_MD5_SUMS is not set.
     55   void TestWebKitImageDecoder(const base::FilePath& image_path,
     56                               const base::FilePath& md5_sum_path,
     57                               int desired_frame_index) const;
     58 
     59   // Verifies each of the test image files is decoded correctly and matches the
     60   // expected state. |file_selection| and |threshold| can be used to select
     61   // files to test based on file size.
     62   // If just the MD5 sum is wanted, this skips chunking.
     63   void TestDecoding(ImageDecoderTestFileSelection file_selection,
     64                     const int64 threshold);
     65 
     66   void TestDecoding()  {
     67     TestDecoding(TEST_ALL, 0);
     68   }
     69 
     70   // Creates WebKit API's decoder.
     71   virtual blink::WebImageDecoder* CreateWebKitImageDecoder() const = 0;
     72 
     73   // The format to be decoded, like "bmp" or "ico".
     74   std::string format_;
     75 
     76  protected:
     77   const base::FilePath& data_dir() const { return data_dir_; }
     78 
     79  private:
     80   // Path to the test files.
     81   base::FilePath data_dir_;
     82 
     83   DISALLOW_COPY_AND_ASSIGN(ImageDecoderTest);
     84 };
     85 
     86 #endif  // CONTENT_TEST_IMAGE_DECODER_TEST_H_
     87