| 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/browser/extensions/extension_service_unittest.h" | 5 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 127 |
| 128 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 128 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 129 int schemes = URLPattern::SCHEME_ALL; | 129 int schemes = URLPattern::SCHEME_ALL; |
| 130 extent->AddPattern(URLPattern(schemes, pattern)); | 130 extent->AddPattern(URLPattern(schemes, pattern)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 } // namespace | 133 } // namespace |
| 134 | 134 |
| 135 class MockExtensionProvider : public ExternalExtensionProviderInterface { | 135 class MockExtensionProvider : public ExternalExtensionProviderInterface { |
| 136 public: | 136 public: |
| 137 explicit MockExtensionProvider( | 137 MockExtensionProvider( |
| 138 VisitorInterface* visitor, | 138 VisitorInterface* visitor, |
| 139 Extension::Location location) | 139 Extension::Location location) |
| 140 : location_(location), visitor_(visitor), visit_count_(0) { | 140 : location_(location), |
| 141 visitor_(visitor), |
| 142 visit_count_(0), |
| 143 creation_flags_(Extension::NO_FLAGS) { |
| 141 } | 144 } |
| 145 |
| 146 MockExtensionProvider( |
| 147 VisitorInterface* visitor, |
| 148 Extension::Location location, |
| 149 int creation_flags) |
| 150 : location_(location), |
| 151 visitor_(visitor), |
| 152 visit_count_(0), |
| 153 creation_flags_(creation_flags) { |
| 154 } |
| 155 |
| 142 virtual ~MockExtensionProvider() {} | 156 virtual ~MockExtensionProvider() {} |
| 143 | 157 |
| 144 void UpdateOrAddExtension(const std::string& id, | 158 void UpdateOrAddExtension(const std::string& id, |
| 145 const std::string& version, | 159 const std::string& version, |
| 146 const FilePath& path) { | 160 const FilePath& path) { |
| 147 extension_map_[id] = std::make_pair(version, path); | 161 extension_map_[id] = std::make_pair(version, path); |
| 148 } | 162 } |
| 149 | 163 |
| 150 void RemoveExtension(const std::string& id) { | 164 void RemoveExtension(const std::string& id) { |
| 151 extension_map_.erase(id); | 165 extension_map_.erase(id); |
| 152 } | 166 } |
| 153 | 167 |
| 154 // ExternalExtensionProvider implementation: | 168 // ExternalExtensionProvider implementation: |
| 155 virtual void VisitRegisteredExtension() { | 169 virtual void VisitRegisteredExtension() OVERRIDE { |
| 156 visit_count_++; | 170 visit_count_++; |
| 157 for (DataMap::const_iterator i = extension_map_.begin(); | 171 for (DataMap::const_iterator i = extension_map_.begin(); |
| 158 i != extension_map_.end(); ++i) { | 172 i != extension_map_.end(); ++i) { |
| 159 scoped_ptr<Version> version; | 173 scoped_ptr<Version> version; |
| 160 version.reset(Version::GetVersionFromString(i->second.first)); | 174 version.reset(Version::GetVersionFromString(i->second.first)); |
| 161 | 175 |
| 162 visitor_->OnExternalExtensionFileFound( | 176 visitor_->OnExternalExtensionFileFound( |
| 163 i->first, version.get(), i->second.second, location_, | 177 i->first, version.get(), i->second.second, location_, |
| 164 Extension::NO_FLAGS, false); | 178 creation_flags_, false); |
| 165 } | 179 } |
| 166 visitor_->OnExternalProviderReady(this); | 180 visitor_->OnExternalProviderReady(this); |
| 167 } | 181 } |
| 168 | 182 |
| 169 virtual bool HasExtension(const std::string& id) const { | 183 virtual bool HasExtension(const std::string& id) const OVERRIDE { |
| 170 return extension_map_.find(id) != extension_map_.end(); | 184 return extension_map_.find(id) != extension_map_.end(); |
| 171 } | 185 } |
| 172 | 186 |
| 173 virtual bool GetExtensionDetails(const std::string& id, | 187 virtual bool GetExtensionDetails( |
| 174 Extension::Location* location, | 188 const std::string& id, |
| 175 scoped_ptr<Version>* version) const { | 189 Extension::Location* location, |
| 190 scoped_ptr<Version>* version) const OVERRIDE { |
| 176 DataMap::const_iterator it = extension_map_.find(id); | 191 DataMap::const_iterator it = extension_map_.find(id); |
| 177 if (it == extension_map_.end()) | 192 if (it == extension_map_.end()) |
| 178 return false; | 193 return false; |
| 179 | 194 |
| 180 if (version) | 195 if (version) |
| 181 version->reset(Version::GetVersionFromString(it->second.first)); | 196 version->reset(Version::GetVersionFromString(it->second.first)); |
| 182 | 197 |
| 183 if (location) | 198 if (location) |
| 184 *location = location_; | 199 *location = location_; |
| 185 | 200 |
| 186 return true; | 201 return true; |
| 187 } | 202 } |
| 188 | 203 |
| 189 virtual bool IsReady() const { | 204 virtual bool IsReady() const OVERRIDE { |
| 190 return true; | 205 return true; |
| 191 } | 206 } |
| 192 | 207 |
| 193 virtual void ServiceShutdown() { | 208 virtual int GetCreationFlags() const OVERRIDE { |
| 209 return creation_flags_; |
| 210 } |
| 211 |
| 212 virtual void ServiceShutdown() OVERRIDE { |
| 194 } | 213 } |
| 195 | 214 |
| 196 int visit_count() const { return visit_count_; } | 215 int visit_count() const { return visit_count_; } |
| 197 void set_visit_count(int visit_count) { | 216 void set_visit_count(int visit_count) { |
| 198 visit_count_ = visit_count; | 217 visit_count_ = visit_count; |
| 199 } | 218 } |
| 200 | 219 |
| 201 private: | 220 private: |
| 202 typedef std::map< std::string, std::pair<std::string, FilePath> > DataMap; | 221 typedef std::map< std::string, std::pair<std::string, FilePath> > DataMap; |
| 203 DataMap extension_map_; | 222 DataMap extension_map_; |
| 204 Extension::Location location_; | 223 Extension::Location location_; |
| 205 VisitorInterface* visitor_; | 224 VisitorInterface* visitor_; |
| 206 | 225 |
| 207 // visit_count_ tracks the number of calls to VisitRegisteredExtension(). | 226 // visit_count_ tracks the number of calls to VisitRegisteredExtension(). |
| 208 // Mutable because it must be incremented on each call to | 227 // Mutable because it must be incremented on each call to |
| 209 // VisitRegisteredExtension(), which must be a const method to inherit | 228 // VisitRegisteredExtension(), which must be a const method to inherit |
| 210 // from the class being mocked. | 229 // from the class being mocked. |
| 211 mutable int visit_count_; | 230 mutable int visit_count_; |
| 212 | 231 |
| 232 int creation_flags_; |
| 233 |
| 213 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); | 234 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); |
| 214 }; | 235 }; |
| 215 | 236 |
| 216 class MockProviderVisitor | 237 class MockProviderVisitor |
| 217 : public ExternalExtensionProviderInterface::VisitorInterface { | 238 : public ExternalExtensionProviderInterface::VisitorInterface { |
| 218 public: | 239 public: |
| 219 | 240 |
| 220 // The provider will return |fake_base_path| from | 241 // The provider will return |fake_base_path| from |
| 221 // GetBaseCrxFilePath(). User can test the behavior with | 242 // GetBaseCrxFilePath(). User can test the behavior with |
| 222 // and without an empty path using this parameter. | 243 // and without an empty path using this parameter. |
| (...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1259 InstallCRX(path, INSTALL_FAILED); | 1280 InstallCRX(path, INSTALL_FAILED); |
| 1260 ValidatePrefKeyCount(pref_count); | 1281 ValidatePrefKeyCount(pref_count); |
| 1261 | 1282 |
| 1262 // TODO(erikkay): add more tests for many of the failure cases. | 1283 // TODO(erikkay): add more tests for many of the failure cases. |
| 1263 // TODO(erikkay): add tests for upgrade cases. | 1284 // TODO(erikkay): add tests for upgrade cases. |
| 1264 } | 1285 } |
| 1265 | 1286 |
| 1266 // Tests that flags passed to OnExternalExtensionFileFound() make it to the | 1287 // Tests that flags passed to OnExternalExtensionFileFound() make it to the |
| 1267 // extension object. | 1288 // extension object. |
| 1268 TEST_F(ExtensionServiceTest, InstallingExternalExtensionWithFlags) { | 1289 TEST_F(ExtensionServiceTest, InstallingExternalExtensionWithFlags) { |
| 1290 const char kPrefFromBookmark[] = "from_bookmark"; |
| 1291 |
| 1269 InitializeEmptyExtensionService(); | 1292 InitializeEmptyExtensionService(); |
| 1270 | 1293 |
| 1271 FilePath path = data_dir_.AppendASCII("good.crx"); | 1294 FilePath path = data_dir_.AppendASCII("good.crx"); |
| 1272 set_extensions_enabled(true); | 1295 set_extensions_enabled(true); |
| 1273 | 1296 |
| 1274 scoped_ptr<Version> version; | 1297 // Register and install an external extension. |
| 1275 version.reset(Version::GetVersionFromString("1.0.0.0")); | 1298 MockExtensionProvider* provider = |
| 1276 // Install an external extension. | 1299 new MockExtensionProvider(service_, |
| 1277 service_->OnExternalExtensionFileFound(good_crx, version.get(), | 1300 Extension::EXTERNAL_POLICY_DOWNLOAD, |
| 1278 path, Extension::EXTERNAL_PREF, | 1301 Extension::FROM_BOOKMARK); |
| 1279 Extension::FROM_BOOKMARK, false); | 1302 AddMockExternalProvider(provider); |
| 1303 provider->UpdateOrAddExtension(good_crx, "1.0.0.0", |
| 1304 data_dir_.AppendASCII("good.crx")); |
| 1305 service_->CheckForExternalUpdates(); |
| 1280 loop_.RunAllPending(); | 1306 loop_.RunAllPending(); |
| 1281 | 1307 |
| 1282 const Extension* extension = service_->GetExtensionById(good_crx, false); | 1308 const Extension* extension = service_->GetExtensionById(good_crx, false); |
| 1283 ASSERT_TRUE(extension); | 1309 ASSERT_TRUE(extension); |
| 1284 ASSERT_EQ(Extension::FROM_BOOKMARK, | 1310 ASSERT_TRUE(extension->from_bookmark()); |
| 1285 Extension::FROM_BOOKMARK & extension->creation_flags()); | 1311 ValidateBooleanPref(good_crx, kPrefFromBookmark, true); |
| 1312 |
| 1313 // Upgrade to version 2.0, the flag should be preserved. |
| 1314 path = data_dir_.AppendASCII("good2.crx"); |
| 1315 UpdateExtension(good_crx, path, ENABLED); |
| 1316 ValidateBooleanPref(good_crx, kPrefFromBookmark, true); |
| 1317 extension = service_->GetExtensionById(good_crx, false); |
| 1318 ASSERT_TRUE(extension); |
| 1319 ASSERT_TRUE(extension->from_bookmark()); |
| 1320 |
| 1321 // Somehow, the "from bookmark pref" gets reset (simulating |
| 1322 // http://crbug.com/109791). |
| 1323 SetPrefBool(extension->id(), kPrefFromBookmark, false); |
| 1324 service_->ReloadExtensions(); |
| 1325 extension = service_->GetExtensionById(good_crx, false); |
| 1326 ASSERT_TRUE(extension); |
| 1327 ASSERT_FALSE(extension->from_bookmark()); |
| 1328 ValidateBooleanPref(good_crx, kPrefFromBookmark, false); |
| 1329 |
| 1330 // If the app gets updated again, we'll reset the "from bookmark" pref if |
| 1331 // the external extension provider is still serving that extension. |
| 1332 UpdateExtension(good_crx, path, ENABLED); |
| 1333 ValidateBooleanPref(good_crx, kPrefFromBookmark, true); |
| 1334 extension = service_->GetExtensionById(good_crx, false); |
| 1335 ASSERT_TRUE(extension); |
| 1336 ASSERT_TRUE(extension->from_bookmark()); |
| 1286 } | 1337 } |
| 1287 | 1338 |
| 1288 // Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED | 1339 // Test the handling of Extension::EXTERNAL_EXTENSION_UNINSTALLED |
| 1289 TEST_F(ExtensionServiceTest, UninstallingExternalExtensions) { | 1340 TEST_F(ExtensionServiceTest, UninstallingExternalExtensions) { |
| 1290 InitializeEmptyExtensionService(); | 1341 InitializeEmptyExtensionService(); |
| 1291 | 1342 |
| 1292 FilePath path = data_dir_.AppendASCII("good.crx"); | 1343 FilePath path = data_dir_.AppendASCII("good.crx"); |
| 1293 set_extensions_enabled(true); | 1344 set_extensions_enabled(true); |
| 1294 | 1345 |
| 1295 scoped_ptr<Version> version; | 1346 scoped_ptr<Version> version; |
| (...skipping 3316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4612 ASSERT_FALSE(AddPendingSyncInstall()); | 4663 ASSERT_FALSE(AddPendingSyncInstall()); |
| 4613 | 4664 |
| 4614 // Wait for the external source to install. | 4665 // Wait for the external source to install. |
| 4615 WaitForCrxInstall(crx_path_, INSTALL_NEW); | 4666 WaitForCrxInstall(crx_path_, INSTALL_NEW); |
| 4616 ASSERT_TRUE(IsCrxInstalled()); | 4667 ASSERT_TRUE(IsCrxInstalled()); |
| 4617 | 4668 |
| 4618 // Now that the extension is installed, sync request should fail | 4669 // Now that the extension is installed, sync request should fail |
| 4619 // because the extension is already installed. | 4670 // because the extension is already installed. |
| 4620 ASSERT_FALSE(AddPendingSyncInstall()); | 4671 ASSERT_FALSE(AddPendingSyncInstall()); |
| 4621 } | 4672 } |
| OLD | NEW |