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.
5 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "extensions/common/extension_l10n_util.h"
15 #include "extensions/common/test_util.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using extensions::Extension;
22 // If filename is a relative path, LoadManifestFile will treat it relative to
23 // the appropriate test directory.
24 base::DictionaryValue* LoadManifestFile(const base::FilePath& filename_path,
26 base::FilePath extension_path;
27 base::FilePath manifest_path;
29 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path);
30 manifest_path = manifest_path.Append(filename_path);
31 extension_path = manifest_path.DirName();
33 EXPECT_TRUE(base::PathExists(manifest_path)) <<
34 "Couldn't find " << manifest_path.value();
36 JSONFileValueSerializer serializer(manifest_path);
37 base::DictionaryValue* manifest =
38 static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, error));
40 // Most unit tests don't need localization, and they'll fail if we try to
41 // localize them, since their manifests don't have a default_locale key.
42 // Only localize manifests that indicate they want to be localized.
43 // Calling LocalizeExtension at this point mirrors file_util::LoadExtension.
45 filename_path.value().find(FILE_PATH_LITERAL("localized")) !=
47 extension_l10n_util::LocalizeExtension(extension_path, manifest, error);
54 ExtensionManifestTest::ExtensionManifestTest()
57 current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
59 // Helper class that simplifies creating methods that take either a filename
60 // to a manifest or the manifest itself.
61 ExtensionManifestTest::Manifest::Manifest(const char* name)
62 : name_(name), manifest_(NULL) {
65 ExtensionManifestTest::Manifest::Manifest(base::DictionaryValue* manifest,
67 : name_(name), manifest_(manifest) {
68 CHECK(manifest_) << "Manifest NULL";
71 ExtensionManifestTest::Manifest::Manifest(
72 scoped_ptr<base::DictionaryValue> manifest)
73 : manifest_(manifest.get()), manifest_holder_(manifest.Pass()) {
74 CHECK(manifest_) << "Manifest NULL";
77 ExtensionManifestTest::Manifest::Manifest(const Manifest& m) {
81 ExtensionManifestTest::Manifest::~Manifest() {
84 base::DictionaryValue* ExtensionManifestTest::Manifest::GetManifest(
85 char const* test_data_dir, std::string* error) const {
89 base::FilePath filename_path;
90 filename_path = filename_path.AppendASCII("extensions")
91 .AppendASCII(test_data_dir)
93 manifest_ = LoadManifestFile(filename_path, error);
94 manifest_holder_.reset(manifest_);
98 char const* ExtensionManifestTest::test_data_dir() {
99 return "manifest_tests";
102 scoped_ptr<base::DictionaryValue> ExtensionManifestTest::LoadManifest(
103 char const* manifest_name, std::string* error) {
104 base::FilePath filename_path;
105 filename_path = filename_path.AppendASCII("extensions")
106 .AppendASCII(test_data_dir())
107 .AppendASCII(manifest_name);
108 return make_scoped_ptr(LoadManifestFile(filename_path, error));
111 scoped_refptr<Extension> ExtensionManifestTest::LoadExtension(
112 const Manifest& manifest,
114 extensions::Manifest::Location location,
116 base::DictionaryValue* value = manifest.GetManifest(test_data_dir(), error);
120 PathService::Get(chrome::DIR_TEST_DATA, &path);
121 path = path.AppendASCII("extensions").AppendASCII(test_data_dir());
122 return Extension::Create(path.DirName(), location, *value, flags, error);
125 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess(
126 const Manifest& manifest,
127 extensions::Manifest::Location location,
130 scoped_refptr<Extension> extension =
131 LoadExtension(manifest, &error, location, flags);
132 EXPECT_TRUE(extension.get()) << manifest.name();
133 EXPECT_EQ("", error) << manifest.name();
137 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess(
138 char const* manifest_name,
139 extensions::Manifest::Location location,
141 return LoadAndExpectSuccess(Manifest(manifest_name), location, flags);
144 scoped_refptr<Extension> ExtensionManifestTest::LoadFromStringAndExpectSuccess(
145 char const* manifest_json) {
146 return LoadAndExpectSuccess(
147 Manifest(extensions::test_util::ParseJsonDictionaryWithSingleQuotes(
151 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectWarning(
152 const Manifest& manifest,
153 const std::string& expected_warning,
154 extensions::Manifest::Location location,
157 scoped_refptr<Extension> extension =
158 LoadExtension(manifest, &error, location, flags);
159 EXPECT_TRUE(extension.get()) << manifest.name();
160 EXPECT_EQ("", error) << manifest.name();
161 EXPECT_EQ(1u, extension->install_warnings().size());
162 EXPECT_EQ(expected_warning, extension->install_warnings()[0].message);
166 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectWarning(
167 char const* manifest_name,
168 const std::string& expected_warning,
169 extensions::Manifest::Location location,
171 return LoadAndExpectWarning(
172 Manifest(manifest_name), expected_warning, location, flags);
175 void ExtensionManifestTest::VerifyExpectedError(
176 Extension* extension,
177 const std::string& name,
178 const std::string& error,
179 const std::string& expected_error) {
180 EXPECT_FALSE(extension) <<
181 "Expected failure loading extension '" << name <<
182 "', but didn't get one.";
183 EXPECT_TRUE(MatchPattern(error, expected_error)) << name <<
184 " expected '" << expected_error << "' but got '" << error << "'";
187 void ExtensionManifestTest::LoadAndExpectError(
188 const Manifest& manifest,
189 const std::string& expected_error,
190 extensions::Manifest::Location location,
193 scoped_refptr<Extension> extension(
194 LoadExtension(manifest, &error, location, flags));
195 VerifyExpectedError(extension.get(), manifest.name(), error,
199 void ExtensionManifestTest::LoadAndExpectError(
200 char const* manifest_name,
201 const std::string& expected_error,
202 extensions::Manifest::Location location,
204 return LoadAndExpectError(
205 Manifest(manifest_name), expected_error, location, flags);
208 void ExtensionManifestTest::LoadFromStringAndExpectError(
209 char const* manifest_json,
210 const std::string& expected_error) {
211 return LoadAndExpectError(
212 Manifest(extensions::test_util::ParseJsonDictionaryWithSingleQuotes(
217 void ExtensionManifestTest::AddPattern(extensions::URLPatternSet* extent,
218 const std::string& pattern) {
219 int schemes = URLPattern::SCHEME_ALL;
220 extent->AddPattern(URLPattern(schemes, pattern));
223 ExtensionManifestTest::Testcase::Testcase(
224 std::string manifest_filename,
225 std::string expected_error,
226 extensions::Manifest::Location location,
228 : manifest_filename_(manifest_filename),
229 expected_error_(expected_error),
230 location_(location), flags_(flags) {
233 ExtensionManifestTest::Testcase::Testcase(std::string manifest_filename,
234 std::string expected_error)
235 : manifest_filename_(manifest_filename),
236 expected_error_(expected_error),
237 location_(extensions::Manifest::INTERNAL),
238 flags_(Extension::NO_FLAGS) {
241 ExtensionManifestTest::Testcase::Testcase(std::string manifest_filename)
242 : manifest_filename_(manifest_filename),
243 location_(extensions::Manifest::INTERNAL),
244 flags_(Extension::NO_FLAGS) {}
246 ExtensionManifestTest::Testcase::Testcase(
247 std::string manifest_filename,
248 extensions::Manifest::Location location,
250 : manifest_filename_(manifest_filename),
254 void ExtensionManifestTest::RunTestcases(const Testcase* testcases,
255 size_t num_testcases,
257 for (size_t i = 0; i < num_testcases; ++i)
258 RunTestcase(testcases[i], type);
261 void ExtensionManifestTest::RunTestcase(const Testcase& testcase,
264 case EXPECT_TYPE_ERROR:
265 LoadAndExpectError(testcase.manifest_filename_.c_str(),
266 testcase.expected_error_,
270 case EXPECT_TYPE_WARNING:
271 LoadAndExpectWarning(testcase.manifest_filename_.c_str(),
272 testcase.expected_error_,
276 case EXPECT_TYPE_SUCCESS:
277 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(),