| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h" | 5 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/extensions/extension_l10n_util.h" | 13 #include "chrome/common/extensions/extension_l10n_util.h" |
| 14 #include "chrome/common/extensions/manifest_handler.h" | 14 #include "chrome/common/extensions/manifest_handler.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 16 | 16 |
| 17 using extensions::Extension; | 17 using extensions::Extension; |
| 18 | 18 |
| 19 ExtensionManifestTest::ExtensionManifestTest() | 19 namespace { |
| 20 : enable_apps_(true), | |
| 21 // UNKNOWN == trunk. | |
| 22 current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} | |
| 23 | 20 |
| 24 void ExtensionManifestTest::TearDown() { | 21 // If filename is a relative path, LoadManifestFile will treat it relative to |
| 25 extensions::ManifestHandler::ClearRegistryForTesting(); | 22 // the appropriate test directory. |
| 26 } | 23 DictionaryValue* LoadManifestFile( |
| 27 | 24 const base::FilePath& filename_path, std::string* error) { |
| 28 // static | |
| 29 DictionaryValue* ExtensionManifestTest::LoadManifestFile( | |
| 30 const std::string& filename, | |
| 31 std::string* error) { | |
| 32 base::FilePath filename_path(base::FilePath::FromUTF8Unsafe(filename)); | |
| 33 base::FilePath extension_path; | 25 base::FilePath extension_path; |
| 34 base::FilePath manifest_path; | 26 base::FilePath manifest_path; |
| 35 | 27 |
| 36 if (filename_path.IsAbsolute()) { | 28 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); |
| 37 extension_path = filename_path.DirName(); | 29 manifest_path = manifest_path.Append(filename_path); |
| 38 manifest_path = filename_path; | 30 extension_path = manifest_path.DirName(); |
| 39 } else { | |
| 40 PathService::Get(chrome::DIR_TEST_DATA, &extension_path); | |
| 41 extension_path = extension_path.AppendASCII("extensions") | |
| 42 .AppendASCII("manifest_tests"); | |
| 43 manifest_path = extension_path.AppendASCII(filename.c_str()); | |
| 44 } | |
| 45 | 31 |
| 46 EXPECT_TRUE(file_util::PathExists(manifest_path)) << | 32 EXPECT_TRUE(file_util::PathExists(manifest_path)) << |
| 47 "Couldn't find " << manifest_path.value(); | 33 "Couldn't find " << manifest_path.value(); |
| 48 | 34 |
| 49 JSONFileValueSerializer serializer(manifest_path); | 35 JSONFileValueSerializer serializer(manifest_path); |
| 50 DictionaryValue* manifest = | 36 DictionaryValue* manifest = |
| 51 static_cast<DictionaryValue*>(serializer.Deserialize(NULL, error)); | 37 static_cast<DictionaryValue*>(serializer.Deserialize(NULL, error)); |
| 52 | 38 |
| 53 // Most unit tests don't need localization, and they'll fail if we try to | 39 // Most unit tests don't need localization, and they'll fail if we try to |
| 54 // localize them, since their manifests don't have a default_locale key. | 40 // localize them, since their manifests don't have a default_locale key. |
| 55 // Only localize manifests that indicate they want to be localized. | 41 // Only localize manifests that indicate they want to be localized. |
| 56 // Calling LocalizeExtension at this point mirrors | 42 // Calling LocalizeExtension at this point mirrors |
| 57 // extension_file_util::LoadExtension. | 43 // extension_file_util::LoadExtension. |
| 58 if (manifest && filename.find("localized") != std::string::npos) | 44 if (manifest && |
| 45 filename_path.value().find(FILE_PATH_LITERAL("localized")) != |
| 46 std::string::npos) |
| 59 extension_l10n_util::LocalizeExtension(extension_path, manifest, error); | 47 extension_l10n_util::LocalizeExtension(extension_path, manifest, error); |
| 60 | 48 |
| 61 return manifest; | 49 return manifest; |
| 62 } | 50 } |
| 63 | 51 |
| 52 } // namespace |
| 53 |
| 54 ExtensionManifestTest::ExtensionManifestTest() |
| 55 : enable_apps_(true), |
| 56 // UNKNOWN == trunk. |
| 57 current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {} |
| 58 |
| 59 void ExtensionManifestTest::TearDown() { |
| 60 extensions::ManifestHandler::ClearRegistryForTesting(); |
| 61 } |
| 62 |
| 64 // Helper class that simplifies creating methods that take either a filename | 63 // Helper class that simplifies creating methods that take either a filename |
| 65 // to a manifest or the manifest itself. | 64 // to a manifest or the manifest itself. |
| 66 ExtensionManifestTest::Manifest::Manifest(const char* name) | 65 ExtensionManifestTest::Manifest::Manifest(const char* name) |
| 67 : name_(name), manifest_(NULL) { | 66 : name_(name), manifest_(NULL) { |
| 68 } | 67 } |
| 69 | 68 |
| 70 ExtensionManifestTest::Manifest::Manifest(DictionaryValue* manifest, | 69 ExtensionManifestTest::Manifest::Manifest(DictionaryValue* manifest, |
| 71 const char* name) | 70 const char* name) |
| 72 : name_(name), manifest_(manifest) { | 71 : name_(name), manifest_(manifest) { |
| 73 CHECK(manifest_) << "Manifest NULL"; | 72 CHECK(manifest_) << "Manifest NULL"; |
| 74 } | 73 } |
| 75 | 74 |
| 76 ExtensionManifestTest::Manifest::Manifest(const Manifest& m) { | 75 ExtensionManifestTest::Manifest::Manifest(const Manifest& m) { |
| 77 NOTREACHED(); | 76 NOTREACHED(); |
| 78 } | 77 } |
| 79 | 78 |
| 80 ExtensionManifestTest::Manifest::~Manifest() { | 79 ExtensionManifestTest::Manifest::~Manifest() { |
| 81 } | 80 } |
| 82 | 81 |
| 83 DictionaryValue* ExtensionManifestTest::Manifest::GetManifest( | 82 DictionaryValue* ExtensionManifestTest::Manifest::GetManifest( |
| 84 std::string* error) const { | 83 char const* test_data_dir, std::string* error) const { |
| 85 if (manifest_) | 84 if (manifest_) |
| 86 return manifest_; | 85 return manifest_; |
| 87 | 86 |
| 88 manifest_ = LoadManifestFile(name_, error); | 87 base::FilePath filename_path; |
| 88 filename_path = filename_path.AppendASCII("extensions") |
| 89 .AppendASCII(test_data_dir) |
| 90 .AppendASCII(name_); |
| 91 manifest_ = LoadManifestFile(filename_path, error); |
| 89 manifest_holder_.reset(manifest_); | 92 manifest_holder_.reset(manifest_); |
| 90 return manifest_; | 93 return manifest_; |
| 91 } | 94 } |
| 92 | 95 |
| 96 char const* ExtensionManifestTest::test_data_dir() { |
| 97 return "manifest_tests"; |
| 98 } |
| 99 |
| 100 scoped_ptr<DictionaryValue> ExtensionManifestTest::LoadManifest( |
| 101 char const* manifest_name, std::string* error) { |
| 102 base::FilePath filename_path; |
| 103 filename_path = filename_path.AppendASCII("extensions") |
| 104 .AppendASCII(test_data_dir()) |
| 105 .AppendASCII(manifest_name); |
| 106 return make_scoped_ptr(LoadManifestFile(filename_path, error)); |
| 107 } |
| 108 |
| 93 scoped_refptr<Extension> ExtensionManifestTest::LoadExtension( | 109 scoped_refptr<Extension> ExtensionManifestTest::LoadExtension( |
| 94 const Manifest& manifest, | 110 const Manifest& manifest, |
| 95 std::string* error, | 111 std::string* error, |
| 96 extensions::Manifest::Location location, | 112 extensions::Manifest::Location location, |
| 97 int flags) { | 113 int flags) { |
| 98 DictionaryValue* value = manifest.GetManifest(error); | 114 DictionaryValue* value = manifest.GetManifest(test_data_dir(), error); |
| 99 if (!value) | 115 if (!value) |
| 100 return NULL; | 116 return NULL; |
| 101 base::FilePath path; | 117 base::FilePath path; |
| 102 PathService::Get(chrome::DIR_TEST_DATA, &path); | 118 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 103 path = path.AppendASCII("extensions").AppendASCII("manifest_tests"); | 119 path = path.AppendASCII("extensions").AppendASCII(test_data_dir()); |
| 104 return Extension::Create(path.DirName(), location, *value, flags, error); | 120 return Extension::Create(path.DirName(), location, *value, flags, error); |
| 105 } | 121 } |
| 106 | 122 |
| 107 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess( | 123 scoped_refptr<Extension> ExtensionManifestTest::LoadAndExpectSuccess( |
| 108 const Manifest& manifest, | 124 const Manifest& manifest, |
| 109 extensions::Manifest::Location location, | 125 extensions::Manifest::Location location, |
| 110 int flags) { | 126 int flags) { |
| 111 std::string error; | 127 std::string error; |
| 112 scoped_refptr<Extension> extension = | 128 scoped_refptr<Extension> extension = |
| 113 LoadExtension(manifest, &error, location, flags); | 129 LoadExtension(manifest, &error, location, flags); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 break; | 259 break; |
| 244 case EXPECT_TYPE_SUCCESS: | 260 case EXPECT_TYPE_SUCCESS: |
| 245 for (size_t i = 0; i < num_testcases; ++i) { | 261 for (size_t i = 0; i < num_testcases; ++i) { |
| 246 LoadAndExpectSuccess(testcases[i].manifest_filename_.c_str(), | 262 LoadAndExpectSuccess(testcases[i].manifest_filename_.c_str(), |
| 247 testcases[i].location_, | 263 testcases[i].location_, |
| 248 testcases[i].flags_); | 264 testcases[i].flags_); |
| 249 } | 265 } |
| 250 break; | 266 break; |
| 251 } | 267 } |
| 252 } | 268 } |
| OLD | NEW |