Files.app: Dispatch 'drive-connection-changed' event on initialization of VolumeManag...
[chromium-blink-merge.git] / extensions / common / manifest_test.cc
blob2dfdaaa5a147c0108b9c7e9cf4c7c02aafd96a1c
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(
204 std::string manifest_filename,
205 std::string expected_error,
206 extensions::Manifest::Location location,
207 int flags)
208 : manifest_filename_(manifest_filename),
209 expected_error_(expected_error),
210 location_(location), flags_(flags) {
213 ManifestTest::Testcase::Testcase(std::string manifest_filename,
214 std::string expected_error)
215 : manifest_filename_(manifest_filename),
216 expected_error_(expected_error),
217 location_(extensions::Manifest::INTERNAL),
218 flags_(Extension::NO_FLAGS) {
221 ManifestTest::Testcase::Testcase(std::string manifest_filename)
222 : manifest_filename_(manifest_filename),
223 location_(extensions::Manifest::INTERNAL),
224 flags_(Extension::NO_FLAGS) {}
226 ManifestTest::Testcase::Testcase(
227 std::string manifest_filename,
228 extensions::Manifest::Location location,
229 int flags)
230 : manifest_filename_(manifest_filename),
231 location_(location),
232 flags_(flags) {}
234 void ManifestTest::RunTestcases(const Testcase* testcases,
235 size_t num_testcases,
236 ExpectType type) {
237 for (size_t i = 0; i < num_testcases; ++i)
238 RunTestcase(testcases[i], type);
241 void ManifestTest::RunTestcase(const Testcase& testcase,
242 ExpectType type) {
243 switch (type) {
244 case EXPECT_TYPE_ERROR:
245 LoadAndExpectError(testcase.manifest_filename_.c_str(),
246 testcase.expected_error_,
247 testcase.location_,
248 testcase.flags_);
249 break;
250 case EXPECT_TYPE_WARNING:
251 LoadAndExpectWarning(testcase.manifest_filename_.c_str(),
252 testcase.expected_error_,
253 testcase.location_,
254 testcase.flags_);
255 break;
256 case EXPECT_TYPE_SUCCESS:
257 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(),
258 testcase.location_,
259 testcase.flags_);
260 break;
264 } // namespace extensions