Chromium Code Reviews| 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/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/testing_pref_service.h" | 15 #include "chrome/test/base/testing_pref_service.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class MockExtensionService : public TestExtensionService { | 20 class MockExtensionService : public TestExtensionService { |
| 21 private: | 21 private: |
| 22 bool ready_; | 22 bool ready_; |
| 23 ExtensionList extension_list_; | 23 ExtensionList extension_list_; |
| 24 size_t unloaded_; | |
|
Aaron Boodman
2011/12/05 22:11:26
This is not a good name. It should be unloaded_cou
SeRya
2011/12/06 08:19:27
Done.
| |
| 25 | |
| 26 bool find(std::string id) { | |
|
Aaron Boodman
2011/12/05 22:11:26
Methods are supposed to be ordered before fields.
SeRya
2011/12/06 08:19:27
Done.
| |
| 27 for (ExtensionList::iterator it = extension_list_.begin(); | |
| 28 it != extension_list_.end(); | |
| 29 ++it) | |
| 30 if ((*it)->id() == id) | |
| 31 return true; | |
| 32 | |
| 33 return false; | |
| 34 } | |
| 24 | 35 |
| 25 public: | 36 public: |
| 26 MockExtensionService() : ready_(false) { | 37 MockExtensionService() : ready_(false), unloaded_(0) { |
| 27 } | 38 } |
| 28 | 39 |
| 29 virtual void AddExtension(const Extension* extension) OVERRIDE { | 40 virtual void AddExtension(const Extension* extension) OVERRIDE { |
| 41 ASSERT_FALSE(find(extension->id())); | |
| 30 // ExtensionService must become the owner of the extension object. | 42 // ExtensionService must become the owner of the extension object. |
| 31 extension_list_.push_back(extension); | 43 extension_list_.push_back(extension); |
| 32 } | 44 } |
| 33 | 45 |
| 34 virtual void UnloadExtension( | 46 virtual void UnloadExtension( |
| 35 const std::string& extension_id, | 47 const std::string& extension_id, |
| 36 extension_misc::UnloadedExtensionReason reason) OVERRIDE { | 48 extension_misc::UnloadedExtensionReason reason) OVERRIDE { |
| 49 ASSERT_TRUE(find(extension_id)); | |
| 37 // Remove the extension with the matching id. | 50 // Remove the extension with the matching id. |
| 38 for (ExtensionList::iterator it = extension_list_.begin(); | 51 for (ExtensionList::iterator it = extension_list_.begin(); |
| 39 it != extension_list_.end(); | 52 it != extension_list_.end(); |
| 40 ++it) { | 53 ++it) { |
| 41 if ((*it)->id() == extension_id) { | 54 if ((*it)->id() == extension_id) { |
| 42 extension_list_.erase(it); | 55 extension_list_.erase(it); |
| 56 unloaded_++; | |
| 43 return; | 57 return; |
| 44 } | 58 } |
| 45 } | 59 } |
| 46 } | 60 } |
| 47 | 61 |
| 48 virtual bool is_ready() OVERRIDE { | 62 virtual bool is_ready() OVERRIDE { |
| 49 return ready_; | 63 return ready_; |
| 50 } | 64 } |
| 51 | 65 |
| 52 virtual const ExtensionList* extensions() const OVERRIDE { | 66 virtual const ExtensionList* extensions() const OVERRIDE { |
| 53 return &extension_list_; | 67 return &extension_list_; |
| 54 } | 68 } |
| 55 | 69 |
| 56 void set_ready(bool ready) { | 70 void set_ready(bool ready) { |
| 57 ready_ = ready; | 71 ready_ = ready; |
| 58 } | 72 } |
| 59 | 73 |
| 74 size_t get_unloaded_count() const { | |
|
Aaron Boodman
2011/12/05 22:11:26
getters in Chrome do not have the 'get_' prefix.
SeRya
2011/12/06 08:19:27
Done.
| |
| 75 return unloaded_; | |
| 76 } | |
| 77 | |
| 60 void clear_extension_list() { | 78 void clear_extension_list() { |
| 61 extension_list_.clear(); | 79 extension_list_.clear(); |
| 62 } | 80 } |
| 63 }; | 81 }; |
| 64 | 82 |
| 65 } // namespace | 83 } // namespace |
| 66 | 84 |
| 67 namespace extensions { | 85 namespace extensions { |
| 68 | 86 |
| 69 class ComponentLoaderTest : public testing::Test { | 87 class ComponentLoaderTest : public testing::Test { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 protected: | 120 protected: |
| 103 MockExtensionService extension_service_; | 121 MockExtensionService extension_service_; |
| 104 TestingPrefService prefs_; | 122 TestingPrefService prefs_; |
| 105 ComponentLoader component_loader_; | 123 ComponentLoader component_loader_; |
| 106 | 124 |
| 107 // The root directory of the text extension. | 125 // The root directory of the text extension. |
| 108 FilePath extension_path_; | 126 FilePath extension_path_; |
| 109 | 127 |
| 110 // The contents of the text extension's manifest file. | 128 // The contents of the text extension's manifest file. |
| 111 std::string manifest_contents_; | 129 std::string manifest_contents_; |
| 130 | |
| 131 FilePath get_extension_path(std::string name) { | |
|
Aaron Boodman
2011/12/05 22:11:26
Since this does significant work, it should be nam
SeRya
2011/12/06 08:19:27
Done.
| |
| 132 FilePath test_data_dir; | |
| 133 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | |
| 134 return | |
| 135 test_data_dir.AppendASCII("extensions") | |
|
Aaron Boodman
2011/12/05 22:11:26
Line 135 looks like it can fit with line 134.
SeRya
2011/12/06 08:19:27
Done.
| |
| 136 .AppendASCII("good") | |
| 137 .AppendASCII("Extensions") | |
| 138 .AppendASCII(name.c_str()); | |
| 139 | |
| 140 } | |
| 112 }; | 141 }; |
| 113 | 142 |
| 114 TEST_F(ComponentLoaderTest, ParseManifest) { | 143 TEST_F(ComponentLoaderTest, ParseManifest) { |
| 115 scoped_ptr<DictionaryValue> manifest; | 144 scoped_ptr<DictionaryValue> manifest; |
| 116 | 145 |
| 117 // Test invalid JSON. | 146 // Test invalid JSON. |
| 118 manifest.reset( | 147 manifest.reset( |
| 119 component_loader_.ParseManifest("{ 'test': 3 } invalid")); | 148 component_loader_.ParseManifest("{ 'test': 3 } invalid")); |
| 120 ASSERT_EQ((DictionaryValue*)NULL, manifest.get()); | 149 ASSERT_EQ((DictionaryValue*)NULL, manifest.get()); |
| 121 | 150 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 component_loader_.AddDefaultComponentExtensions(); | 271 component_loader_.AddDefaultComponentExtensions(); |
| 243 component_loader_.LoadAll(); | 272 component_loader_.LoadAll(); |
| 244 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | 273 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); |
| 245 | 274 |
| 246 // Number of loaded extensions should be the same after changing the pref. | 275 // Number of loaded extensions should be the same after changing the pref. |
| 247 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, | 276 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, |
| 248 Value::CreateStringValue("http://www.google.de")); | 277 Value::CreateStringValue("http://www.google.de")); |
| 249 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | 278 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); |
| 250 } | 279 } |
| 251 | 280 |
| 281 TEST_F(ComponentLoaderTest, AddOrReplace) { | |
| 282 ASSERT_EQ(0U, component_loader_.GetRegisteredExtensionsCount()); | |
|
Aaron Boodman
2011/12/05 22:11:26
Chrome uses lower-case 'u'.
SeRya
2011/12/06 08:19:27
Done.
| |
| 283 component_loader_.AddDefaultComponentExtensions(); | |
| 284 size_t const default_count = component_loader_.GetRegisteredExtensionsCount(); | |
| 285 | |
| 286 // Replace a default component extension. | |
| 287 component_loader_.AddOrReplace(get_extension_path("hhaomjibdihmijegdhdafkllkbg gdgoj")); | |
|
Aaron Boodman
2011/12/05 22:11:26
line length
SeRya
2011/12/06 08:19:27
Done.
| |
| 288 ASSERT_EQ(default_count, | |
| 289 component_loader_.GetRegisteredExtensionsCount()); | |
| 290 | |
| 291 // Add a new component extension. | |
| 292 component_loader_.AddOrReplace( | |
| 293 get_extension_path("hpiknbiabeeppbpihjehijgoemciehgk/2")); | |
| 294 ASSERT_EQ(default_count + 1, | |
| 295 component_loader_.GetRegisteredExtensionsCount()); | |
| 296 | |
| 297 extension_service_.set_ready(true); | |
| 298 component_loader_.LoadAll(); | |
| 299 | |
| 300 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | |
| 301 ASSERT_EQ(0U, extension_service_.get_unloaded_count()); | |
| 302 | |
| 303 // replace loaded component extension. | |
| 304 component_loader_.AddOrReplace(get_extension_path("hhaomjibdihmijegdhdafkllkbg gdgoj")); | |
|
Aaron Boodman
2011/12/05 22:11:26
line length
SeRya
2011/12/06 08:19:27
Done.
| |
| 305 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); | |
| 306 ASSERT_EQ(1U, extension_service_.get_unloaded_count()); | |
| 307 } | |
| 308 | |
| 252 } // namespace extensions | 309 } // namespace extensions |
| OLD | NEW |