Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / manifest_test.cc
blob048e1838a2ec2f5e73f2b137ca5f11a92f3b82af
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 "extensions/common/manifest_test.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/path_service.h"
11 #include "base/strings/pattern.h"
12 #include "base/strings/string_util.h"
13 #include "base/values.h"
14 #include "extensions/common/extension_l10n_util.h"
15 #include "extensions/common/extension_paths.h"
16 #include "extensions/common/test_util.h"
17 #include "ui/base/l10n/l10n_util.h"
19 namespace extensions {
20 namespace {
22 // |manifest_path| is an absolute path to a manifest file.
23 base::DictionaryValue* LoadManifestFile(const base::FilePath& manifest_path,
24 std::string* error) {
25 base::FilePath extension_path = manifest_path.DirName();
27 EXPECT_TRUE(base::PathExists(manifest_path)) <<
28 "Couldn't find " << manifest_path.value();
30 JSONFileValueDeserializer deserializer(manifest_path);
31 base::DictionaryValue* manifest = static_cast<base::DictionaryValue*>(
32 deserializer.Deserialize(NULL, error));
34 // Most unit tests don't need localization, and they'll fail if we try to
35 // localize them, since their manifests don't have a default_locale key.
36 // Only localize manifests that indicate they want to be localized.
37 // Calling LocalizeExtension at this point mirrors file_util::LoadExtension.
38 if (manifest &&
39 manifest_path.value().find(FILE_PATH_LITERAL("localized")) !=
40 std::string::npos)
41 extension_l10n_util::LocalizeExtension(extension_path, manifest, error);
43 return manifest;
46 } // namespace
48 ManifestTest::ManifestTest()
49 : enable_apps_(true) {
52 ManifestTest::~ManifestTest() {
55 // Helper class that simplifies creating methods that take either a filename
56 // to a manifest or the manifest itself.
57 ManifestTest::ManifestData::ManifestData(const char* name)
58 : name_(name), manifest_(NULL) {
61 ManifestTest::ManifestData::ManifestData(base::DictionaryValue* manifest,
62 const char* name)
63 : name_(name), manifest_(manifest) {
64 CHECK(manifest_) << "Manifest NULL";
67 ManifestTest::ManifestData::ManifestData(
68 scoped_ptr<base::DictionaryValue> manifest)
69 : manifest_(manifest.get()), manifest_holder_(manifest.Pass()) {
70 CHECK(manifest_) << "Manifest NULL";
73 ManifestTest::ManifestData::ManifestData(const ManifestData& m) {
74 NOTREACHED();
77 ManifestTest::ManifestData::~ManifestData() {
80 base::DictionaryValue* ManifestTest::ManifestData::GetManifest(
81 base::FilePath test_data_dir, std::string* error) const {
82 if (manifest_)
83 return manifest_;
85 base::FilePath manifest_path = test_data_dir.AppendASCII(name_);
86 manifest_ = LoadManifestFile(manifest_path, error);
87 manifest_holder_.reset(manifest_);
88 return manifest_;
91 std::string ManifestTest::GetTestExtensionID() const {
92 return std::string();
95 base::FilePath ManifestTest::GetTestDataDir() {
96 base::FilePath path;
97 PathService::Get(DIR_TEST_DATA, &path);
98 return path.AppendASCII("manifest_tests");
101 scoped_ptr<base::DictionaryValue> ManifestTest::LoadManifest(
102 char const* manifest_name, std::string* error) {
103 base::FilePath manifest_path = GetTestDataDir().AppendASCII(manifest_name);
104 return make_scoped_ptr(LoadManifestFile(manifest_path, error));
107 scoped_refptr<Extension> ManifestTest::LoadExtension(
108 const ManifestData& manifest,
109 std::string* error,
110 extensions::Manifest::Location location,
111 int flags) {
112 base::FilePath test_data_dir = GetTestDataDir();
113 base::DictionaryValue* value = manifest.GetManifest(test_data_dir, error);
114 if (!value)
115 return NULL;
116 return Extension::Create(test_data_dir.DirName(), location, *value, flags,
117 GetTestExtensionID(), error);
120 scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess(
121 const ManifestData& manifest,
122 extensions::Manifest::Location location,
123 int flags) {
124 std::string error;
125 scoped_refptr<Extension> extension =
126 LoadExtension(manifest, &error, location, flags);
127 EXPECT_TRUE(extension.get()) << manifest.name();
128 EXPECT_EQ("", error) << manifest.name();
129 return extension;
132 scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess(
133 char const* manifest_name,
134 extensions::Manifest::Location location,
135 int flags) {
136 return LoadAndExpectSuccess(ManifestData(manifest_name), location, flags);
139 scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning(
140 const ManifestData& manifest,
141 const std::string& expected_warning,
142 extensions::Manifest::Location location,
143 int flags) {
144 std::string error;
145 scoped_refptr<Extension> extension =
146 LoadExtension(manifest, &error, location, flags);
147 EXPECT_TRUE(extension.get()) << manifest.name();
148 EXPECT_EQ("", error) << manifest.name();
149 EXPECT_EQ(1u, extension->install_warnings().size());
150 EXPECT_EQ(expected_warning, extension->install_warnings()[0].message);
151 return extension;
154 scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning(
155 char const* manifest_name,
156 const std::string& expected_warning,
157 extensions::Manifest::Location location,
158 int flags) {
159 return LoadAndExpectWarning(
160 ManifestData(manifest_name), expected_warning, location, flags);
163 void ManifestTest::VerifyExpectedError(
164 Extension* extension,
165 const std::string& name,
166 const std::string& error,
167 const std::string& expected_error) {
168 EXPECT_FALSE(extension) <<
169 "Expected failure loading extension '" << name <<
170 "', but didn't get one.";
171 EXPECT_TRUE(base::MatchPattern(error, expected_error))
172 << name << " expected '" << expected_error << "' but got '" << error
173 << "'";
176 void ManifestTest::LoadAndExpectError(
177 const ManifestData& manifest,
178 const std::string& expected_error,
179 extensions::Manifest::Location location,
180 int flags) {
181 std::string error;
182 scoped_refptr<Extension> extension(
183 LoadExtension(manifest, &error, location, flags));
184 VerifyExpectedError(extension.get(), manifest.name(), error,
185 expected_error);
188 void ManifestTest::LoadAndExpectError(
189 char const* manifest_name,
190 const std::string& expected_error,
191 extensions::Manifest::Location location,
192 int flags) {
193 return LoadAndExpectError(
194 ManifestData(manifest_name), expected_error, location, flags);
197 void ManifestTest::AddPattern(extensions::URLPatternSet* extent,
198 const std::string& pattern) {
199 int schemes = URLPattern::SCHEME_ALL;
200 extent->AddPattern(URLPattern(schemes, pattern));
203 ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
204 const std::string& expected_error,
205 extensions::Manifest::Location location,
206 int flags)
207 : manifest_filename_(manifest_filename),
208 expected_error_(expected_error),
209 location_(location),
210 flags_(flags) {}
212 ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
213 const std::string& expected_error)
214 : manifest_filename_(manifest_filename),
215 expected_error_(expected_error),
216 location_(extensions::Manifest::INTERNAL),
217 flags_(Extension::NO_FLAGS) {}
219 ManifestTest::Testcase::Testcase(const std::string& manifest_filename)
220 : manifest_filename_(manifest_filename),
221 location_(extensions::Manifest::INTERNAL),
222 flags_(Extension::NO_FLAGS) {}
224 ManifestTest::Testcase::Testcase(const std::string& manifest_filename,
225 extensions::Manifest::Location location,
226 int flags)
227 : manifest_filename_(manifest_filename),
228 location_(location),
229 flags_(flags) {}
231 void ManifestTest::RunTestcases(const Testcase* testcases,
232 size_t num_testcases,
233 ExpectType type) {
234 for (size_t i = 0; i < num_testcases; ++i)
235 RunTestcase(testcases[i], type);
238 void ManifestTest::RunTestcase(const Testcase& testcase,
239 ExpectType type) {
240 switch (type) {
241 case EXPECT_TYPE_ERROR:
242 LoadAndExpectError(testcase.manifest_filename_.c_str(),
243 testcase.expected_error_,
244 testcase.location_,
245 testcase.flags_);
246 break;
247 case EXPECT_TYPE_WARNING:
248 LoadAndExpectWarning(testcase.manifest_filename_.c_str(),
249 testcase.expected_error_,
250 testcase.location_,
251 testcase.flags_);
252 break;
253 case EXPECT_TYPE_SUCCESS:
254 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(),
255 testcase.location_,
256 testcase.flags_);
257 break;
261 } // namespace extensions