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
{
22 // |manifest_path| is an absolute path to a manifest file.
23 base::DictionaryValue
* LoadManifestFile(const base::FilePath
& manifest_path
,
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.
39 manifest_path
.value().find(FILE_PATH_LITERAL("localized")) !=
41 extension_l10n_util::LocalizeExtension(extension_path
, manifest
, error
);
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
,
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
) {
77 ManifestTest::ManifestData::~ManifestData() {
80 base::DictionaryValue
* ManifestTest::ManifestData::GetManifest(
81 base::FilePath test_data_dir
, std::string
* error
) const {
85 base::FilePath manifest_path
= test_data_dir
.AppendASCII(name_
);
86 manifest_
= LoadManifestFile(manifest_path
, error
);
87 manifest_holder_
.reset(manifest_
);
91 std::string
ManifestTest::GetTestExtensionID() const {
95 base::FilePath
ManifestTest::GetTestDataDir() {
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
,
110 extensions::Manifest::Location location
,
112 base::FilePath test_data_dir
= GetTestDataDir();
113 base::DictionaryValue
* value
= manifest
.GetManifest(test_data_dir
, error
);
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
,
125 scoped_refptr
<Extension
> extension
=
126 LoadExtension(manifest
, &error
, location
, flags
);
127 EXPECT_TRUE(extension
.get()) << manifest
.name();
128 EXPECT_EQ("", error
) << manifest
.name();
132 scoped_refptr
<Extension
> ManifestTest::LoadAndExpectSuccess(
133 char const* manifest_name
,
134 extensions::Manifest::Location location
,
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
,
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
);
154 scoped_refptr
<Extension
> ManifestTest::LoadAndExpectWarning(
155 char const* manifest_name
,
156 const std::string
& expected_warning
,
157 extensions::Manifest::Location location
,
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
176 void ManifestTest::LoadAndExpectError(
177 const ManifestData
& manifest
,
178 const std::string
& expected_error
,
179 extensions::Manifest::Location location
,
182 scoped_refptr
<Extension
> extension(
183 LoadExtension(manifest
, &error
, location
, flags
));
184 VerifyExpectedError(extension
.get(), manifest
.name(), error
,
188 void ManifestTest::LoadAndExpectError(
189 char const* manifest_name
,
190 const std::string
& expected_error
,
191 extensions::Manifest::Location location
,
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
,
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
,
230 : manifest_filename_(manifest_filename
),
234 void ManifestTest::RunTestcases(const Testcase
* testcases
,
235 size_t num_testcases
,
237 for (size_t i
= 0; i
< num_testcases
; ++i
)
238 RunTestcase(testcases
[i
], type
);
241 void ManifestTest::RunTestcase(const Testcase
& testcase
,
244 case EXPECT_TYPE_ERROR
:
245 LoadAndExpectError(testcase
.manifest_filename_
.c_str(),
246 testcase
.expected_error_
,
250 case EXPECT_TYPE_WARNING
:
251 LoadAndExpectWarning(testcase
.manifest_filename_
.c_str(),
252 testcase
.expected_error_
,
256 case EXPECT_TYPE_SUCCESS
:
257 LoadAndExpectSuccess(testcase
.manifest_filename_
.c_str(),
264 } // namespace extensions