Home | History | Annotate | Download | only in common
      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 EXTENSIONS_COMMON_MANIFEST_TEST_H_
      6 #define EXTENSIONS_COMMON_MANIFEST_TEST_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/values.h"
     11 #include "extensions/common/extension.h"
     12 #include "extensions/common/manifest.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace base {
     16 class FilePath;
     17 }
     18 
     19 namespace extensions {
     20 
     21 // Base class for tests that parse a manifest file.
     22 class ManifestTest : public testing::Test {
     23  public:
     24   ManifestTest();
     25   virtual ~ManifestTest();
     26 
     27  protected:
     28   // Helper class that simplifies creating methods that take either a filename
     29   // to a manifest or the manifest itself.
     30   class ManifestData {
     31    public:
     32     explicit ManifestData(const char* name);
     33     ManifestData(base::DictionaryValue* manifest, const char* name);
     34     explicit ManifestData(scoped_ptr<base::DictionaryValue> manifest);
     35     // C++98 requires the copy constructor for a type to be visible if you
     36     // take a const-ref of a temporary for that type.  Since Manifest
     37     // contains a scoped_ptr, its implicit copy constructor is declared
     38     // Manifest(Manifest&) according to spec 12.8.5.  This breaks the first
     39     // requirement and thus you cannot use it with LoadAndExpectError() or
     40     // LoadAndExpectSuccess() easily.
     41     //
     42     // To get around this spec pedantry, we declare the copy constructor
     43     // explicitly.  It will never get invoked.
     44     ManifestData(const ManifestData& m);
     45 
     46     ~ManifestData();
     47 
     48     const std::string& name() const { return name_; };
     49 
     50     base::DictionaryValue* GetManifest(base::FilePath manifest_path,
     51                                        std::string* error) const;
     52 
     53    private:
     54     const std::string name_;
     55     mutable base::DictionaryValue* manifest_;
     56     mutable scoped_ptr<base::DictionaryValue> manifest_holder_;
     57   };
     58 
     59   // Returns the path in which to find test manifest data files, for example
     60   // extensions/test/data/manifest_tests.
     61   virtual base::FilePath GetTestDataDir();
     62 
     63   scoped_ptr<base::DictionaryValue> LoadManifest(
     64       char const* manifest_name,
     65       std::string* error);
     66 
     67   scoped_refptr<extensions::Extension> LoadExtension(
     68       const ManifestData& manifest,
     69       std::string* error,
     70       extensions::Manifest::Location location =
     71           extensions::Manifest::INTERNAL,
     72       int flags = extensions::Extension::NO_FLAGS);
     73 
     74   scoped_refptr<extensions::Extension> LoadAndExpectSuccess(
     75       const ManifestData& manifest,
     76       extensions::Manifest::Location location =
     77           extensions::Manifest::INTERNAL,
     78       int flags = extensions::Extension::NO_FLAGS);
     79 
     80   scoped_refptr<extensions::Extension> LoadAndExpectSuccess(
     81       char const* manifest_name,
     82       extensions::Manifest::Location location =
     83           extensions::Manifest::INTERNAL,
     84       int flags = extensions::Extension::NO_FLAGS);
     85 
     86   scoped_refptr<extensions::Extension> LoadAndExpectWarning(
     87       const ManifestData& manifest,
     88       const std::string& expected_error,
     89       extensions::Manifest::Location location =
     90           extensions::Manifest::INTERNAL,
     91       int flags = extensions::Extension::NO_FLAGS);
     92 
     93   scoped_refptr<extensions::Extension> LoadAndExpectWarning(
     94       char const* manifest_name,
     95       const std::string& expected_error,
     96       extensions::Manifest::Location location =
     97           extensions::Manifest::INTERNAL,
     98       int flags = extensions::Extension::NO_FLAGS);
     99 
    100   void VerifyExpectedError(extensions::Extension* extension,
    101                            const std::string& name,
    102                            const std::string& error,
    103                            const std::string& expected_error);
    104 
    105   void LoadAndExpectError(char const* manifest_name,
    106                           const std::string& expected_error,
    107                           extensions::Manifest::Location location =
    108                               extensions::Manifest::INTERNAL,
    109                           int flags = extensions::Extension::NO_FLAGS);
    110 
    111   void LoadAndExpectError(const ManifestData& manifest,
    112                           const std::string& expected_error,
    113                           extensions::Manifest::Location location =
    114                               extensions::Manifest::INTERNAL,
    115                           int flags = extensions::Extension::NO_FLAGS);
    116 
    117   void AddPattern(extensions::URLPatternSet* extent,
    118                   const std::string& pattern);
    119 
    120   // used to differentiate between calls to LoadAndExpectError,
    121   // LoadAndExpectWarning and LoadAndExpectSuccess via function RunTestcases.
    122   enum ExpectType {
    123     EXPECT_TYPE_ERROR,
    124     EXPECT_TYPE_WARNING,
    125     EXPECT_TYPE_SUCCESS
    126   };
    127 
    128   struct Testcase {
    129     std::string manifest_filename_;
    130     std::string expected_error_; // only used for ExpectedError tests
    131     extensions::Manifest::Location location_;
    132     int flags_;
    133 
    134     Testcase(std::string manifest_filename, std::string expected_error,
    135         extensions::Manifest::Location location, int flags);
    136 
    137     Testcase(std::string manifest_filename, std::string expected_error);
    138 
    139     explicit Testcase(std::string manifest_filename);
    140 
    141     Testcase(std::string manifest_filename,
    142              extensions::Manifest::Location location,
    143              int flags);
    144   };
    145 
    146   void RunTestcases(const Testcase* testcases,
    147                     size_t num_testcases,
    148                     ExpectType type);
    149 
    150   void RunTestcase(const Testcase& testcase, ExpectType type);
    151 
    152   bool enable_apps_;
    153 
    154  private:
    155   DISALLOW_COPY_AND_ASSIGN(ManifestTest);
    156 };
    157 
    158 }  // namespace extensions
    159 
    160 #endif  // EXTENSIONS_COMMON_MANIFEST_TEST_H_
    161