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