[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / extensions / common / manifest_test.cc
blob14e0aef0a0f8b0d7f9ef0d7e92d526f7c63671fd
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/string_util.h"
12 #include "base/values.h"
13 #include "extensions/common/extension_l10n_util.h"
14 #include "extensions/common/extension_paths.h"
15 #include "extensions/common/test_util.h"
16 #include "ui/base/l10n/l10n_util.h"
18 namespace extensions {
19 namespace {
21 // |manifest_path| is an absolute path to a manifest file.
22 base::DictionaryValue* LoadManifestFile(const base::FilePath& manifest_path,
23 std::string* error) {
24 base::FilePath extension_path = manifest_path.DirName();
26 EXPECT_TRUE(base::PathExists(manifest_path)) <<
27 "Couldn't find " << manifest_path.value();
29 JSONFileValueSerializer serializer(manifest_path);
30 base::DictionaryValue* manifest =
31 static_cast<base::DictionaryValue*>(serializer.Deserialize(NULL, error));
33 // Most unit tests don't need localization, and they'll fail if we try to
34 // localize them, since their manifests don't have a default_locale key.
35 // Only localize manifests that indicate they want to be localized.
36 // Calling LocalizeExtension at this point mirrors file_util::LoadExtension.
37 if (manifest &&
38 manifest_path.value().find(FILE_PATH_LITERAL("localized")) !=
39 std::string::npos)
40 extension_l10n_util::LocalizeExtension(extension_path, manifest, error);
42 return manifest;
45 } // namespace
47 ManifestTest::ManifestTest()
48 : enable_apps_(true) {
51 ManifestTest::~ManifestTest() {
54 // Helper class that simplifies creating methods that take either a filename
55 // to a manifest or the manifest itself.
56 ManifestTest::ManifestData::ManifestData(const char* name)
57 : name_(name), manifest_(NULL) {
60 ManifestTest::ManifestData::ManifestData(base::DictionaryValue* manifest,
61 const char* name)
62 : name_(name), manifest_(manifest) {
63 CHECK(manifest_) << "Manifest NULL";
66 ManifestTest::ManifestData::ManifestData(
67 scoped_ptr<base::DictionaryValue> manifest)
68 : manifest_(manifest.get()), manifest_holder_(manifest.Pass()) {
69 CHECK(manifest_) << "Manifest NULL";
72 ManifestTest::ManifestData::ManifestData(const ManifestData& m) {
73 NOTREACHED();
76 ManifestTest::ManifestData::~ManifestData() {
79 base::DictionaryValue* ManifestTest::ManifestData::GetManifest(
80 base::FilePath test_data_dir, std::string* error) const {
81 if (manifest_)
82 return manifest_;
84 base::FilePath manifest_path = test_data_dir.AppendASCII(name_);
85 manifest_ = LoadManifestFile(manifest_path, error);
86 manifest_holder_.reset(manifest_);
87 return manifest_;
90 std::string ManifestTest::GetTestExtensionID() const {
91 return std::string();
94 base::FilePath ManifestTest::GetTestDataDir() {
95 base::FilePath path;
96 PathService::Get(DIR_TEST_DATA, &path);
97 return path.AppendASCII("manifest_tests");
100 scoped_ptr<base::DictionaryValue> ManifestTest::LoadManifest(
101 char const* manifest_name, std::string* error) {
102 base::FilePath manifest_path = GetTestDataDir().AppendASCII(manifest_name);
103 return make_scoped_ptr(LoadManifestFile(manifest_path, error));
106 scoped_refptr<Extension> ManifestTest::LoadExtension(
107 const ManifestData& manifest,
108 std::string* error,
109 extensions::Manifest::Location location,
110 int flags) {
111 base::FilePath test_data_dir = GetTestDataDir();
112 base::DictionaryValue* value = manifest.GetManifest(test_data_dir, error);
113 if (!value)
114 return NULL;
115 return Extension::Create(test_data_dir.DirName(), location, *value, flags,
116 GetTestExtensionID(), error);
119 scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess(
120 const ManifestData& manifest,
121 extensions::Manifest::Location location,
122 int flags) {
123 std::string error;
124 scoped_refptr<Extension> extension =
125 LoadExtension(manifest, &error, location, flags);
126 EXPECT_TRUE(extension.get()) << manifest.name();
127 EXPECT_EQ("", error) << manifest.name();
128 return extension;
131 scoped_refptr<Extension> ManifestTest::LoadAndExpectSuccess(
132 char const* manifest_name,
133 extensions::Manifest::Location location,
134 int flags) {
135 return LoadAndExpectSuccess(ManifestData(manifest_name), location, flags);
138 scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning(
139 const ManifestData& manifest,
140 const std::string& expected_warning,
141 extensions::Manifest::Location location,
142 int flags) {
143 std::string error;
144 scoped_refptr<Extension> extension =
145 LoadExtension(manifest, &error, location, flags);
146 EXPECT_TRUE(extension.get()) << manifest.name();
147 EXPECT_EQ("", error) << manifest.name();
148 EXPECT_EQ(1u, extension->install_warnings().size());
149 EXPECT_EQ(expected_warning, extension->install_warnings()[0].message);
150 return extension;
153 scoped_refptr<Extension> ManifestTest::LoadAndExpectWarning(
154 char const* manifest_name,
155 const std::string& expected_warning,
156 extensions::Manifest::Location location,
157 int flags) {
158 return LoadAndExpectWarning(
159 ManifestData(manifest_name), expected_warning, location, flags);
162 void ManifestTest::VerifyExpectedError(
163 Extension* extension,
164 const std::string& name,
165 const std::string& error,
166 const std::string& expected_error) {
167 EXPECT_FALSE(extension) <<
168 "Expected failure loading extension '" << name <<
169 "', but didn't get one.";
170 EXPECT_TRUE(MatchPattern(error, expected_error)) << name <<
171 " expected '" << expected_error << "' but got '" << error << "'";
174 void ManifestTest::LoadAndExpectError(
175 const ManifestData& manifest,
176 const std::string& expected_error,
177 extensions::Manifest::Location location,
178 int flags) {
179 std::string error;
180 scoped_refptr<Extension> extension(
181 LoadExtension(manifest, &error, location, flags));
182 VerifyExpectedError(extension.get(), manifest.name(), error,
183 expected_error);
186 void ManifestTest::LoadAndExpectError(
187 char const* manifest_name,
188 const std::string& expected_error,
189 extensions::Manifest::Location location,
190 int flags) {
191 return LoadAndExpectError(
192 ManifestData(manifest_name), expected_error, location, flags);
195 void ManifestTest::AddPattern(extensions::URLPatternSet* extent,
196 const std::string& pattern) {
197 int schemes = URLPattern::SCHEME_ALL;
198 extent->AddPattern(URLPattern(schemes, pattern));
201 ManifestTest::Testcase::Testcase(
202 std::string manifest_filename,
203 std::string expected_error,
204 extensions::Manifest::Location location,
205 int flags)
206 : manifest_filename_(manifest_filename),
207 expected_error_(expected_error),
208 location_(location), flags_(flags) {
211 ManifestTest::Testcase::Testcase(std::string manifest_filename,
212 std::string expected_error)
213 : manifest_filename_(manifest_filename),
214 expected_error_(expected_error),
215 location_(extensions::Manifest::INTERNAL),
216 flags_(Extension::NO_FLAGS) {
219 ManifestTest::Testcase::Testcase(std::string manifest_filename)
220 : manifest_filename_(manifest_filename),
221 location_(extensions::Manifest::INTERNAL),
222 flags_(Extension::NO_FLAGS) {}
224 ManifestTest::Testcase::Testcase(
225 std::string manifest_filename,
226 extensions::Manifest::Location location,
227 int flags)
228 : manifest_filename_(manifest_filename),
229 location_(location),
230 flags_(flags) {}
232 void ManifestTest::RunTestcases(const Testcase* testcases,
233 size_t num_testcases,
234 ExpectType type) {
235 for (size_t i = 0; i < num_testcases; ++i)
236 RunTestcase(testcases[i], type);
239 void ManifestTest::RunTestcase(const Testcase& testcase,
240 ExpectType type) {
241 switch (type) {
242 case EXPECT_TYPE_ERROR:
243 LoadAndExpectError(testcase.manifest_filename_.c_str(),
244 testcase.expected_error_,
245 testcase.location_,
246 testcase.flags_);
247 break;
248 case EXPECT_TYPE_WARNING:
249 LoadAndExpectWarning(testcase.manifest_filename_.c_str(),
250 testcase.expected_error_,
251 testcase.location_,
252 testcase.flags_);
253 break;
254 case EXPECT_TYPE_SUCCESS:
255 LoadAndExpectSuccess(testcase.manifest_filename_.c_str(),
256 testcase.location_,
257 testcase.flags_);
258 break;
262 } // namespace extensions