| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/extensions/extensions_service_unittest.h" | 5 #include "chrome/browser/extensions/extensions_service_unittest.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 const std::string& version, | 133 const std::string& version, |
| 134 const FilePath& path) { | 134 const FilePath& path) { |
| 135 extension_map_[id] = std::make_pair(version, path); | 135 extension_map_[id] = std::make_pair(version, path); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void RemoveExtension(const std::string& id) { | 138 void RemoveExtension(const std::string& id) { |
| 139 extension_map_.erase(id); | 139 extension_map_.erase(id); |
| 140 } | 140 } |
| 141 | 141 |
| 142 // ExternalExtensionProvider implementation: | 142 // ExternalExtensionProvider implementation: |
| 143 virtual void VisitRegisteredExtension( | 143 virtual void VisitRegisteredExtension(Visitor* visitor) const { |
| 144 Visitor* visitor, const std::set<std::string>& ids_to_ignore) const { | |
| 145 visit_count_++; | 144 visit_count_++; |
| 146 for (DataMap::const_iterator i = extension_map_.begin(); | 145 for (DataMap::const_iterator i = extension_map_.begin(); |
| 147 i != extension_map_.end(); ++i) { | 146 i != extension_map_.end(); ++i) { |
| 148 if (ids_to_ignore.find(i->first) != ids_to_ignore.end()) | |
| 149 continue; | |
| 150 scoped_ptr<Version> version; | 147 scoped_ptr<Version> version; |
| 151 version.reset(Version::GetVersionFromString(i->second.first)); | 148 version.reset(Version::GetVersionFromString(i->second.first)); |
| 152 | 149 |
| 153 visitor->OnExternalExtensionFileFound( | 150 visitor->OnExternalExtensionFileFound( |
| 154 i->first, version.get(), i->second.second, location_); | 151 i->first, version.get(), i->second.second, location_); |
| 155 } | 152 } |
| 156 } | 153 } |
| 157 | 154 |
| 158 virtual bool HasExtension(const std::string& id) const { | 155 virtual bool HasExtension(const std::string& id) const { |
| 159 return extension_map_.find(id) != extension_map_.end(); | 156 return extension_map_.find(id) != extension_map_.end(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 mutable int visit_count_; | 188 mutable int visit_count_; |
| 192 | 189 |
| 193 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); | 190 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); |
| 194 }; | 191 }; |
| 195 | 192 |
| 196 class MockProviderVisitor : public ExternalExtensionProvider::Visitor { | 193 class MockProviderVisitor : public ExternalExtensionProvider::Visitor { |
| 197 public: | 194 public: |
| 198 MockProviderVisitor() : ids_found_(0) { | 195 MockProviderVisitor() : ids_found_(0) { |
| 199 } | 196 } |
| 200 | 197 |
| 201 int Visit(const std::string& json_data, | 198 int Visit(const std::string& json_data) { |
| 202 const std::set<std::string>& ignore_list) { | |
| 203 // Give the test json file to the provider for parsing. | 199 // Give the test json file to the provider for parsing. |
| 204 provider_.reset(new ExternalPrefExtensionProvider()); | 200 provider_.reset(new ExternalPrefExtensionProvider()); |
| 205 provider_->SetPreferencesForTesting(json_data); | 201 provider_->SetPreferencesForTesting(json_data); |
| 206 | 202 |
| 207 // We also parse the file into a dictionary to compare what we get back | 203 // We also parse the file into a dictionary to compare what we get back |
| 208 // from the provider. | 204 // from the provider. |
| 209 JSONStringValueSerializer serializer(json_data); | 205 JSONStringValueSerializer serializer(json_data); |
| 210 Value* json_value = serializer.Deserialize(NULL, NULL); | 206 Value* json_value = serializer.Deserialize(NULL, NULL); |
| 211 | 207 |
| 212 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) { | 208 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) { |
| 213 NOTREACHED() << "Unable to deserialize json data"; | 209 NOTREACHED() << "Unable to deserialize json data"; |
| 214 return -1; | 210 return -1; |
| 215 } else { | 211 } else { |
| 216 DictionaryValue* external_extensions = | 212 DictionaryValue* external_extensions = |
| 217 static_cast<DictionaryValue*>(json_value); | 213 static_cast<DictionaryValue*>(json_value); |
| 218 prefs_.reset(external_extensions); | 214 prefs_.reset(external_extensions); |
| 219 } | 215 } |
| 220 | 216 |
| 221 // Reset our counter. | 217 // Reset our counter. |
| 222 ids_found_ = 0; | 218 ids_found_ = 0; |
| 223 // Ask the provider to look up all extensions (and return the ones | 219 // Ask the provider to look up all extensions and return them. |
| 224 // found (that are not on the ignore list). | 220 provider_->VisitRegisteredExtension(this); |
| 225 provider_->VisitRegisteredExtension(this, ignore_list); | |
| 226 | 221 |
| 227 return ids_found_; | 222 return ids_found_; |
| 228 } | 223 } |
| 229 | 224 |
| 230 virtual void OnExternalExtensionFileFound(const std::string& id, | 225 virtual void OnExternalExtensionFileFound(const std::string& id, |
| 231 const Version* version, | 226 const Version* version, |
| 232 const FilePath& path, | 227 const FilePath& path, |
| 233 Extension::Location unused) { | 228 Extension::Location unused) { |
| 234 ++ids_found_; | 229 ++ids_found_; |
| 235 DictionaryValue* pref; | 230 DictionaryValue* pref; |
| (...skipping 2584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2820 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" | 2815 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" |
| 2821 " \"external_crx\": \"RandomExtension2.crx\"," | 2816 " \"external_crx\": \"RandomExtension2.crx\"," |
| 2822 " \"external_version\": \"2.0\"" | 2817 " \"external_version\": \"2.0\"" |
| 2823 " }," | 2818 " }," |
| 2824 " \"cccccccccccccccccccccccccccccccc\": {" | 2819 " \"cccccccccccccccccccccccccccccccc\": {" |
| 2825 " \"external_update_url\": \"http:\\\\foo.com/update\"" | 2820 " \"external_update_url\": \"http:\\\\foo.com/update\"" |
| 2826 " }" | 2821 " }" |
| 2827 "}"; | 2822 "}"; |
| 2828 | 2823 |
| 2829 MockProviderVisitor visitor; | 2824 MockProviderVisitor visitor; |
| 2830 std::set<std::string> ignore_list; | |
| 2831 EXPECT_EQ(3, visitor.Visit(json_data, ignore_list)); | |
| 2832 ignore_list.insert("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); | |
| 2833 EXPECT_EQ(2, visitor.Visit(json_data, ignore_list)); | |
| 2834 ignore_list.insert("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); | |
| 2835 EXPECT_EQ(1, visitor.Visit(json_data, ignore_list)); | |
| 2836 ignore_list.insert("cccccccccccccccccccccccccccccccc"); | |
| 2837 EXPECT_EQ(0, visitor.Visit(json_data, ignore_list)); | |
| 2838 | 2825 |
| 2839 // Simulate an external_extensions.json file that contains seven invalid | 2826 // Simulate an external_extensions.json file that contains seven invalid |
| 2840 // extensions: | 2827 // extensions: |
| 2841 // - One that is missing the 'external_crx' key. | 2828 // - One that is missing the 'external_crx' key. |
| 2842 // - One that is missing the 'external_version' key. | 2829 // - One that is missing the 'external_version' key. |
| 2843 // - One that is specifying .. in the path. | 2830 // - One that is specifying .. in the path. |
| 2844 // - One that specifies both a file and update URL. | 2831 // - One that specifies both a file and update URL. |
| 2845 // - One that specifies no file or update URL. | 2832 // - One that specifies no file or update URL. |
| 2846 // - One that has an update URL that is not well formed. | 2833 // - One that has an update URL that is not well formed. |
| 2847 // - One that contains a malformed version. | 2834 // - One that contains a malformed version. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 2871 " }," | 2858 " }," |
| 2872 " \"gggggggggggggggggggggggggggggggg\": {" | 2859 " \"gggggggggggggggggggggggggggggggg\": {" |
| 2873 " \"external_crx\": \"RandomExtension3.crx\"," | 2860 " \"external_crx\": \"RandomExtension3.crx\"," |
| 2874 " \"external_version\": \"This is not a valid version!\"" | 2861 " \"external_version\": \"This is not a valid version!\"" |
| 2875 " }," | 2862 " }," |
| 2876 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": {" | 2863 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": {" |
| 2877 " \"external_crx\": \"RandomValidExtension.crx\"," | 2864 " \"external_crx\": \"RandomValidExtension.crx\"," |
| 2878 " \"external_version\": \"1.0\"" | 2865 " \"external_version\": \"1.0\"" |
| 2879 " }" | 2866 " }" |
| 2880 "}"; | 2867 "}"; |
| 2881 ignore_list.clear(); | 2868 EXPECT_EQ(1, visitor.Visit(json_data)); |
| 2882 EXPECT_EQ(1, visitor.Visit(json_data, ignore_list)); | |
| 2883 } | 2869 } |
| 2884 | 2870 |
| 2885 // Test loading good extensions from the profile directory. | 2871 // Test loading good extensions from the profile directory. |
| 2886 TEST_F(ExtensionsServiceTest, LoadAndRelocalizeExtensions) { | 2872 TEST_F(ExtensionsServiceTest, LoadAndRelocalizeExtensions) { |
| 2887 // Initialize the test dir with a good Preferences/extensions. | 2873 // Initialize the test dir with a good Preferences/extensions. |
| 2888 FilePath source_install_dir; | 2874 FilePath source_install_dir; |
| 2889 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_install_dir)); | 2875 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_install_dir)); |
| 2890 source_install_dir = source_install_dir | 2876 source_install_dir = source_install_dir |
| 2891 .AppendASCII("extensions") | 2877 .AppendASCII("extensions") |
| 2892 .AppendASCII("l10n"); | 2878 .AppendASCII("l10n"); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3097 // Component extensions shouldn't get recourded in the prefs. | 3083 // Component extensions shouldn't get recourded in the prefs. |
| 3098 ValidatePrefKeyCount(0); | 3084 ValidatePrefKeyCount(0); |
| 3099 | 3085 |
| 3100 // Reload all extensions, and make sure it comes back. | 3086 // Reload all extensions, and make sure it comes back. |
| 3101 std::string extension_id = service_->extensions()->at(0)->id(); | 3087 std::string extension_id = service_->extensions()->at(0)->id(); |
| 3102 loaded_.clear(); | 3088 loaded_.clear(); |
| 3103 service_->ReloadExtensions(); | 3089 service_->ReloadExtensions(); |
| 3104 ASSERT_EQ(1u, service_->extensions()->size()); | 3090 ASSERT_EQ(1u, service_->extensions()->size()); |
| 3105 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); | 3091 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); |
| 3106 } | 3092 } |
| OLD | NEW |