| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/component_loader.h" | 7 #include "chrome/browser/extensions/component_loader.h" |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "chrome/browser/extensions/test_extension_service.h" | 11 #include "chrome/browser/extensions/test_extension_service.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/extensions/extension_set.h" | |
| 15 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/testing_pref_service.h" | 15 #include "chrome/test/base/testing_pref_service.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 class MockExtensionService : public TestExtensionService { | 20 class MockExtensionService : public TestExtensionService { |
| 22 private: | 21 private: |
| 23 bool ready_; | 22 bool ready_; |
| 24 ExtensionSet extension_set_; | 23 ExtensionList extension_list_; |
| 25 | 24 |
| 26 public: | 25 public: |
| 27 MockExtensionService() : ready_(false) { | 26 MockExtensionService() : ready_(false) { |
| 28 } | 27 } |
| 29 | 28 |
| 30 virtual void AddExtension(const Extension* extension) OVERRIDE { | 29 virtual void AddExtension(const Extension* extension) OVERRIDE { |
| 31 // ExtensionService must become the owner of the extension object. | 30 // ExtensionService must become the owner of the extension object. |
| 32 extension_set_.Insert(extension); | 31 extension_list_.push_back(extension); |
| 33 } | 32 } |
| 34 | 33 |
| 35 virtual void UnloadExtension( | 34 virtual void UnloadExtension( |
| 36 const std::string& extension_id, | 35 const std::string& extension_id, |
| 37 extension_misc::UnloadedExtensionReason reason) OVERRIDE { | 36 extension_misc::UnloadedExtensionReason reason) OVERRIDE { |
| 38 // Remove the extension with the matching id. | 37 // Remove the extension with the matching id. |
| 39 extension_set_.Remove(extension_id); | 38 for (ExtensionList::iterator it = extension_list_.begin(); |
| 39 it != extension_list_.end(); |
| 40 ++it) { |
| 41 if ((*it)->id() == extension_id) { |
| 42 extension_list_.erase(it); |
| 43 return; |
| 44 } |
| 45 } |
| 40 } | 46 } |
| 41 | 47 |
| 42 virtual bool is_ready() OVERRIDE { | 48 virtual bool is_ready() OVERRIDE { |
| 43 return ready_; | 49 return ready_; |
| 44 } | 50 } |
| 45 | 51 |
| 46 virtual const ExtensionSet* extensions() const OVERRIDE { | 52 virtual const ExtensionList* extensions() const OVERRIDE { |
| 47 return &extension_set_; | 53 return &extension_list_; |
| 48 } | 54 } |
| 49 | 55 |
| 50 void set_ready(bool ready) { | 56 void set_ready(bool ready) { |
| 51 ready_ = ready; | 57 ready_ = ready; |
| 52 } | 58 } |
| 53 | 59 |
| 54 void clear_extensions() { | 60 void clear_extension_list() { |
| 55 extension_set_.Clear(); | 61 extension_list_.clear(); |
| 56 } | 62 } |
| 57 }; | 63 }; |
| 58 | 64 |
| 59 } // namespace | 65 } // namespace |
| 60 | 66 |
| 61 namespace extensions { | 67 namespace extensions { |
| 62 | 68 |
| 63 class ComponentLoaderTest : public testing::Test { | 69 class ComponentLoaderTest : public testing::Test { |
| 64 public: | 70 public: |
| 65 ComponentLoaderTest() : | 71 ComponentLoaderTest() : |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 // No extensions should be loaded if none were added. | 210 // No extensions should be loaded if none were added. |
| 205 component_loader_.LoadAll(); | 211 component_loader_.LoadAll(); |
| 206 ASSERT_EQ(0U, extension_service_.extensions()->size()); | 212 ASSERT_EQ(0U, extension_service_.extensions()->size()); |
| 207 | 213 |
| 208 // Use LoadAll() to load the default extensions. | 214 // Use LoadAll() to load the default extensions. |
| 209 component_loader_.AddDefaultComponentExtensions(); | 215 component_loader_.AddDefaultComponentExtensions(); |
| 210 component_loader_.LoadAll(); | 216 component_loader_.LoadAll(); |
| 211 unsigned int default_count = extension_service_.extensions()->size(); | 217 unsigned int default_count = extension_service_.extensions()->size(); |
| 212 | 218 |
| 213 // Clear the list of loaded extensions, and reload with one more. | 219 // Clear the list of loaded extensions, and reload with one more. |
| 214 extension_service_.clear_extensions(); | 220 extension_service_.clear_extension_list(); |
| 215 component_loader_.Add(manifest_contents_, extension_path_); | 221 component_loader_.Add(manifest_contents_, extension_path_); |
| 216 component_loader_.LoadAll(); | 222 component_loader_.LoadAll(); |
| 217 | 223 |
| 218 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | 224 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); |
| 219 } | 225 } |
| 220 | 226 |
| 221 TEST_F(ComponentLoaderTest, EnterpriseWebStore) { | 227 TEST_F(ComponentLoaderTest, EnterpriseWebStore) { |
| 222 component_loader_.AddDefaultComponentExtensions(); | 228 component_loader_.AddDefaultComponentExtensions(); |
| 223 component_loader_.LoadAll(); | 229 component_loader_.LoadAll(); |
| 224 unsigned int default_count = extension_service_.extensions()->size(); | 230 unsigned int default_count = extension_service_.extensions()->size(); |
| 225 | 231 |
| 226 // Set the pref, and it should get loaded automatically. | 232 // Set the pref, and it should get loaded automatically. |
| 227 extension_service_.set_ready(true); | 233 extension_service_.set_ready(true); |
| 228 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, | 234 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, |
| 229 Value::CreateStringValue("http://www.google.com")); | 235 Value::CreateStringValue("http://www.google.com")); |
| 230 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | 236 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); |
| 231 | 237 |
| 232 // Now that the pref is set, check if it's added by default. | 238 // Now that the pref is set, check if it's added by default. |
| 233 extension_service_.set_ready(false); | 239 extension_service_.set_ready(false); |
| 234 extension_service_.clear_extensions(); | 240 extension_service_.clear_extension_list(); |
| 235 component_loader_.ClearAllRegistered(); | 241 component_loader_.ClearAllRegistered(); |
| 236 component_loader_.AddDefaultComponentExtensions(); | 242 component_loader_.AddDefaultComponentExtensions(); |
| 237 component_loader_.LoadAll(); | 243 component_loader_.LoadAll(); |
| 238 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | 244 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); |
| 239 | 245 |
| 240 // Number of loaded extensions should be the same after changing the pref. | 246 // Number of loaded extensions should be the same after changing the pref. |
| 241 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, | 247 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, |
| 242 Value::CreateStringValue("http://www.google.de")); | 248 Value::CreateStringValue("http://www.google.de")); |
| 243 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | 249 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); |
| 244 } | 250 } |
| 245 | 251 |
| 246 } // namespace extensions | 252 } // namespace extensions |
| OLD | NEW |