Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/common/extensions/manifest_tests/extension_manifest_test.cc

Issue 574173002: Reland: Refactor ExtensionManifestTest to allow usage in src/extensions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: (manifest-test) add ScopedChannel Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
6
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/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"
17
18 using extensions::Extension;
19
20 namespace {
21
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,
25 std::string* error) {
26 base::FilePath extension_path;
27 base::FilePath manifest_path;
28
29 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path);
30 manifest_path = manifest_path.Append(filename_path);
31 extension_path = manifest_path.DirName();
32
33 EXPECT_TRUE(base::PathExists(manifest_path)) <<
34 "Couldn't find " << manifest_path.value();
35
36 JSONFileValueSerializer serializer(manifest_path);
37 base::DictionaryValue* manifest =
38 static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, error));
39
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.
44 if (manifest &&
45 filename_path.value().find(FILE_PATH_LITERAL("localized")) !=
46 std::string::npos)
47 extension_l10n_util::LocalizeExtension(extension_path, manifest, error);
48
49 return manifest;
50 }
51
52 } // namespace
53
54 ExtensionManifestTest::ExtensionManifestTest()
55 : enable_apps_(true),
56 // UNKNOWN == trunk.
57 current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
58
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) {
63 }
64
65 ExtensionManifestTest::Manifest::Manifest(base::DictionaryValue* manifest,
66 const char* name)
67 : name_(name), manifest_(manifest) {
68 CHECK(manifest_) << "Manifest NULL";
69 }
70
71 ExtensionManifestTest::Manifest::Manifest(
72 scoped_ptr<base::DictionaryValue> manifest)
73 : manifest_(manifest.get()), manifest_holder_(manifest.Pass()) {
74 CHECK(manifest_) << "Manifest NULL";
75 }
76
77 ExtensionManifestTest::Manifest::Manifest(const Manifest& m) {
78 NOTREACHED();
79 }
80
81 ExtensionManifestTest::Manifest::~Manifest() {
82 }
83
84 base::DictionaryValue* ExtensionManifestTest::Manifest::GetManifest(
85 char const* test_data_dir, std::string* error) const {
86 if (manifest_)
87 return manifest_;
88
89 base::FilePath filename_path;
90 filename_path = filename_path.AppendASCII("extensions")
91 .AppendASCII(test_data_dir)
92 .AppendASCII(name_);
93 manifest_ = LoadManifestFile(filename_path, error);
94 manifest_holder_.reset(manifest_);
95 return manifest_;
96 }
97
98 char const* ExtensionManifestTest::test_data_dir() {
99 return "manifest_tests";
100 }
101
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));
109 }
110
111 scoped_refptr<Extension> ExtensionManifestTest::LoadExtension(
112 const Manifest& manifest,
113 std::string* error,
114 extensions::Manifest::Location location,
115 int flags) {
116 base::DictionaryValue* value = manifest.GetManifest(test_data_dir(), error);
117 if (!value)
118 return NULL;
119 base::FilePath path;
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);
123 }
124
125 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess(
126 const Manifest& manifest,
127 extensions::Manifest::Location location,
128 int flags) {
129 std::string error;
130 scoped_refptr<Extension> extension =
131 LoadExtension(manifest, &error, location, flags);
132 EXPECT_TRUE(extension.get()) << manifest.name();
133 EXPECT_EQ("", error) << manifest.name();
134 return extension;
135 }
136
137 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess(
138 char const* manifest_name,
139 extensions::Manifest::Location location,
140 int flags) {
141 return LoadAndExpectSuccess(Manifest(manifest_name), location, flags);
142 }
143
144 scoped_refptr<Extension> ExtensionManifestTest::LoadFromStringAndExpectSuccess(
145 char const* manifest_json) {
146 return LoadAndExpectSuccess(
147 Manifest(extensions::test_util::ParseJsonDictionaryWithSingleQuotes(
148 manifest_json)));
149 }
150
151 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectWarning(
152 const Manifest& manifest,
153 const std::string& expected_warning,
154 extensions::Manifest::Location location,
155 int flags) {
156 std::string error;
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);
163 return extension;
164 }
165
166 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectWarning(
167 char const* manifest_name,
168 const std::string& expected_warning,
169 extensions::Manifest::Location location,
170 int flags) {
171 return LoadAndExpectWarning(
172 Manifest(manifest_name), expected_warning, location, flags);
173 }
174
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 << "'";
185 }
186
187 void ExtensionManifestTest::LoadAndExpectError(
188 const Manifest& manifest,
189 const std::string& expected_error,
190 extensions::Manifest::Location location,
191 int flags) {
192 std::string error;
193 scoped_refptr<Extension> extension(
194 LoadExtension(manifest, &error, location, flags));
195 VerifyExpectedError(extension.get(), manifest.name(), error,
196 expected_error);
197 }
198
199 void ExtensionManifestTest::LoadAndExpectError(
200 char const* manifest_name,
201 const std::string& expected_error,
202 extensions::Manifest::Location location,
203 int flags) {
204 return LoadAndExpectError(
205 Manifest(manifest_name), expected_error, location, flags);
206 }
207
208 void ExtensionManifestTest::LoadFromStringAndExpectError(
209 char const* manifest_json,
210 const std::string& expected_error) {
211 return LoadAndExpectError(
212 Manifest(extensions::test_util::ParseJsonDictionaryWithSingleQuotes(
213 manifest_json)),
214 expected_error);
215 }
216
217 void ExtensionManifestTest::AddPattern(extensions::URLPatternSet* extent,
218 const std::string& pattern) {
219 int schemes = URLPattern::SCHEME_ALL;
220 extent->AddPattern(URLPattern(schemes, pattern));
221 }
222
223 ExtensionManifestTest::Testcase::Testcase(
224 std::string manifest_filename,
225 std::string expected_error,
226 extensions::Manifest::Location location,
227 int flags)
228 : manifest_filename_(manifest_filename),
229 expected_error_(expected_error),
230 location_(location), flags_(flags) {
231 }
232
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) {
239 }
240
241 ExtensionManifestTest::Testcase::Testcase(std::string manifest_filename)
242 : manifest_filename_(manifest_filename),
243 location_(extensions::Manifest::INTERNAL),
244 flags_(Extension::NO_FLAGS) {}
245
246 ExtensionManifestTest::Testcase::Testcase(
247 std::string manifest_filename,
248 extensions::Manifest::Location location,
249 int flags)
250 : manifest_filename_(manifest_filename),
251 location_(location),
252 flags_(flags) {}
253
254 void ExtensionManifestTest::RunTestcases(const Testcase* testcases,
255 size_t num_testcases,
256 ExpectType type) {
257 for (size_t i = 0; i < num_testcases; ++i)
258 RunTestcase(testcases[i], type);
259 }
260
261 void ExtensionManifestTest::RunTestcase(const Testcase& testcase,
262 ExpectType type) {
263 switch (type) {
264 case EXPECT_TYPE_ERROR:
265 LoadAndExpectError(testcase.manifest_filename_.c_str(),
266 testcase.expected_error_,
267 testcase.location_,
268 testcase.flags_);
269 break;
270 case EXPECT_TYPE_WARNING:
271 LoadAndExpectWarning(testcase.manifest_filename_.c_str(),
272 testcase.expected_error_,
273 testcase.location_,
274 testcase.flags_);
275 break;
276 case EXPECT_TYPE_SUCCESS:
277 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(),
278 testcase.location_,
279 testcase.flags_);
280 break;
281 }
282 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698