Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Side by Side Diff: chrome/browser/extensions/component_loader_unittest.cc

Issue 8659002: Adding the --load-component-extension flag. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding comment to ScopedAllowIO.wq Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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" 14 #include "chrome/common/extensions/extension_set.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_pref_service.h" 16 #include "chrome/test/base/testing_pref_service.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 namespace { 19 namespace {
20 20
21 class MockExtensionService : public TestExtensionService { 21 class MockExtensionService : public TestExtensionService {
22 private: 22 private:
23 bool ready_; 23 bool ready_;
24 size_t unloaded_count_;
24 ExtensionSet extension_set_; 25 ExtensionSet extension_set_;
25 26
26 public: 27 public:
27 MockExtensionService() : ready_(false) { 28 MockExtensionService() : ready_(false), unloaded_count_(0) {
28 } 29 }
29 30
30 virtual void AddExtension(const Extension* extension) OVERRIDE { 31 virtual void AddExtension(const Extension* extension) OVERRIDE {
32 ASSERT_FALSE(extension_set_.Contains(extension->id()));
31 // ExtensionService must become the owner of the extension object. 33 // ExtensionService must become the owner of the extension object.
32 extension_set_.Insert(extension); 34 extension_set_.Insert(extension);
33 } 35 }
34 36
35 virtual void UnloadExtension( 37 virtual void UnloadExtension(
36 const std::string& extension_id, 38 const std::string& extension_id,
37 extension_misc::UnloadedExtensionReason reason) OVERRIDE { 39 extension_misc::UnloadedExtensionReason reason) OVERRIDE {
40 ASSERT_TRUE(extension_set_.Contains(extension_id));
38 // Remove the extension with the matching id. 41 // Remove the extension with the matching id.
39 extension_set_.Remove(extension_id); 42 extension_set_.Remove(extension_id);
43 unloaded_count_++;
40 } 44 }
41 45
42 virtual bool is_ready() OVERRIDE { 46 virtual bool is_ready() OVERRIDE {
43 return ready_; 47 return ready_;
44 } 48 }
45 49
46 virtual const ExtensionSet* extensions() const OVERRIDE { 50 virtual const ExtensionSet* extensions() const OVERRIDE {
47 return &extension_set_; 51 return &extension_set_;
48 } 52 }
49 53
50 void set_ready(bool ready) { 54 void set_ready(bool ready) {
51 ready_ = ready; 55 ready_ = ready;
52 } 56 }
53 57
58 size_t unloaded_count() const {
59 return unloaded_count_;
60 }
61
54 void clear_extensions() { 62 void clear_extensions() {
55 extension_set_.Clear(); 63 extension_set_.Clear();
56 } 64 }
57 }; 65 };
58 66
59 } // namespace 67 } // namespace
60 68
61 namespace extensions { 69 namespace extensions {
62 70
63 class ComponentLoaderTest : public testing::Test { 71 class ComponentLoaderTest : public testing::Test {
64 public: 72 public:
65 ComponentLoaderTest() : 73 ComponentLoaderTest() :
66 // Note: we pass the same pref service here, to stand in for both 74 // Note: we pass the same pref service here, to stand in for both
67 // user prefs and local state. 75 // user prefs and local state.
68 component_loader_(&extension_service_, &prefs_, &prefs_) { 76 component_loader_(&extension_service_, &prefs_, &prefs_) {
69 } 77 }
70 78
71 void SetUp() { 79 void SetUp() {
72 FilePath test_data_dir;
73 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
74 extension_path_ = 80 extension_path_ =
75 test_data_dir.AppendASCII("extensions") 81 GetBasePath().AppendASCII("good")
76 .AppendASCII("good")
77 .AppendASCII("Extensions") 82 .AppendASCII("Extensions")
78 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") 83 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
79 .AppendASCII("1.0.0.0"); 84 .AppendASCII("1.0.0.0");
80 85
81 // Read in the extension manifest. 86 // Read in the extension manifest.
82 ASSERT_TRUE(file_util::ReadFileToString( 87 ASSERT_TRUE(file_util::ReadFileToString(
83 extension_path_.Append(Extension::kManifestFilename), 88 extension_path_.Append(Extension::kManifestFilename),
84 &manifest_contents_)); 89 &manifest_contents_));
85 90
86 // Register the user prefs that ComponentLoader will read. 91 // Register the user prefs that ComponentLoader will read.
87 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreURL, std::string()); 92 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreURL, std::string());
88 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreName, std::string()); 93 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreName, std::string());
89 94
90 // Register the local state prefs. 95 // Register the local state prefs.
91 #if defined(OS_CHROMEOS) 96 #if defined(OS_CHROMEOS)
92 prefs_.RegisterBooleanPref(prefs::kAccessibilityEnabled, false); 97 prefs_.RegisterBooleanPref(prefs::kAccessibilityEnabled, false);
93 #endif 98 #endif
94 } 99 }
95 100
96 protected: 101 protected:
97 MockExtensionService extension_service_; 102 MockExtensionService extension_service_;
98 TestingPrefService prefs_; 103 TestingPrefService prefs_;
99 ComponentLoader component_loader_; 104 ComponentLoader component_loader_;
100 105
101 // The root directory of the text extension. 106 // The root directory of the text extension.
102 FilePath extension_path_; 107 FilePath extension_path_;
103 108
104 // The contents of the text extension's manifest file. 109 // The contents of the text extension's manifest file.
105 std::string manifest_contents_; 110 std::string manifest_contents_;
111
112 FilePath GetBasePath() {
113 FilePath test_data_dir;
114 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
115 return test_data_dir.AppendASCII("extensions");
116 }
106 }; 117 };
107 118
108 TEST_F(ComponentLoaderTest, ParseManifest) { 119 TEST_F(ComponentLoaderTest, ParseManifest) {
109 scoped_ptr<DictionaryValue> manifest; 120 scoped_ptr<DictionaryValue> manifest;
110 121
111 // Test invalid JSON. 122 // Test invalid JSON.
112 manifest.reset( 123 manifest.reset(
113 component_loader_.ParseManifest("{ 'test': 3 } invalid")); 124 component_loader_.ParseManifest("{ 'test': 3 } invalid"));
114 ASSERT_EQ((DictionaryValue*)NULL, manifest.get()); 125 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
115 126
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 ASSERT_TRUE(manifest->GetString("background_page", &string_value)); 164 ASSERT_TRUE(manifest->GetString("background_page", &string_value));
154 ASSERT_EQ("backgroundpage.html", string_value); 165 ASSERT_EQ("backgroundpage.html", string_value);
155 } 166 }
156 167
157 // Test that the extension isn't loaded if the extension service isn't ready. 168 // Test that the extension isn't loaded if the extension service isn't ready.
158 TEST_F(ComponentLoaderTest, AddWhenNotReady) { 169 TEST_F(ComponentLoaderTest, AddWhenNotReady) {
159 scoped_refptr<const Extension> extension; 170 scoped_refptr<const Extension> extension;
160 extension_service_.set_ready(false); 171 extension_service_.set_ready(false);
161 extension = component_loader_.Add(manifest_contents_, extension_path_); 172 extension = component_loader_.Add(manifest_contents_, extension_path_);
162 ASSERT_EQ((Extension*)NULL, extension.get()); 173 ASSERT_EQ((Extension*)NULL, extension.get());
163 ASSERT_EQ(0U, extension_service_.extensions()->size()); 174 ASSERT_EQ(0u, extension_service_.extensions()->size());
164 } 175 }
165 176
166 // Test that it *is* loaded when the extension service *is* ready. 177 // Test that it *is* loaded when the extension service *is* ready.
167 TEST_F(ComponentLoaderTest, AddWhenReady) { 178 TEST_F(ComponentLoaderTest, AddWhenReady) {
168 scoped_refptr<const Extension> extension; 179 scoped_refptr<const Extension> extension;
169 extension_service_.set_ready(true); 180 extension_service_.set_ready(true);
170 extension = component_loader_.Add(manifest_contents_, extension_path_); 181 extension = component_loader_.Add(manifest_contents_, extension_path_);
171 ASSERT_NE((Extension*)NULL, extension.get()); 182 ASSERT_NE((Extension*)NULL, extension.get());
172 ASSERT_EQ(1U, extension_service_.extensions()->size()); 183 ASSERT_EQ(1u, extension_service_.extensions()->size());
173 } 184 }
174 185
175 TEST_F(ComponentLoaderTest, Remove) { 186 TEST_F(ComponentLoaderTest, Remove) {
176 extension_service_.set_ready(false); 187 extension_service_.set_ready(false);
177 188
178 // Removing an extension that was never added should be ok. 189 // Removing an extension that was never added should be ok.
179 component_loader_.Remove(extension_path_); 190 component_loader_.Remove(extension_path_);
180 ASSERT_EQ(0U, extension_service_.extensions()->size()); 191 ASSERT_EQ(0u, extension_service_.extensions()->size());
181 192
182 // Try adding and removing before LoadAll() is called. 193 // Try adding and removing before LoadAll() is called.
183 component_loader_.Add(manifest_contents_, extension_path_); 194 component_loader_.Add(manifest_contents_, extension_path_);
184 component_loader_.Remove(extension_path_); 195 component_loader_.Remove(extension_path_);
185 component_loader_.LoadAll(); 196 component_loader_.LoadAll();
186 ASSERT_EQ(0U, extension_service_.extensions()->size()); 197 ASSERT_EQ(0u, extension_service_.extensions()->size());
187 198
188 // Load an extension, and check that it's unloaded when Remove() is called. 199 // Load an extension, and check that it's unloaded when Remove() is called.
189 scoped_refptr<const Extension> extension; 200 scoped_refptr<const Extension> extension;
190 extension_service_.set_ready(true); 201 extension_service_.set_ready(true);
191 extension = component_loader_.Add(manifest_contents_, extension_path_); 202 extension = component_loader_.Add(manifest_contents_, extension_path_);
192 ASSERT_NE((Extension*)NULL, extension.get()); 203 ASSERT_NE((Extension*)NULL, extension.get());
193 component_loader_.Remove(extension_path_); 204 component_loader_.Remove(extension_path_);
194 ASSERT_EQ(0U, extension_service_.extensions()->size()); 205 ASSERT_EQ(0u, extension_service_.extensions()->size());
195 206
196 // And after calling LoadAll(), it shouldn't get loaded. 207 // And after calling LoadAll(), it shouldn't get loaded.
197 component_loader_.LoadAll(); 208 component_loader_.LoadAll();
198 ASSERT_EQ(0U, extension_service_.extensions()->size()); 209 ASSERT_EQ(0u, extension_service_.extensions()->size());
199 } 210 }
200 211
201 TEST_F(ComponentLoaderTest, LoadAll) { 212 TEST_F(ComponentLoaderTest, LoadAll) {
202 extension_service_.set_ready(false); 213 extension_service_.set_ready(false);
203 214
204 // No extensions should be loaded if none were added. 215 // No extensions should be loaded if none were added.
205 component_loader_.LoadAll(); 216 component_loader_.LoadAll();
206 ASSERT_EQ(0U, extension_service_.extensions()->size()); 217 ASSERT_EQ(0u, extension_service_.extensions()->size());
207 218
208 // Use LoadAll() to load the default extensions. 219 // Use LoadAll() to load the default extensions.
209 component_loader_.AddDefaultComponentExtensions(); 220 component_loader_.AddDefaultComponentExtensions();
210 component_loader_.LoadAll(); 221 component_loader_.LoadAll();
211 unsigned int default_count = extension_service_.extensions()->size(); 222 unsigned int default_count = extension_service_.extensions()->size();
212 223
213 // Clear the list of loaded extensions, and reload with one more. 224 // Clear the list of loaded extensions, and reload with one more.
214 extension_service_.clear_extensions(); 225 extension_service_.clear_extensions();
215 component_loader_.Add(manifest_contents_, extension_path_); 226 component_loader_.Add(manifest_contents_, extension_path_);
216 component_loader_.LoadAll(); 227 component_loader_.LoadAll();
(...skipping 19 matching lines...) Expand all
236 component_loader_.AddDefaultComponentExtensions(); 247 component_loader_.AddDefaultComponentExtensions();
237 component_loader_.LoadAll(); 248 component_loader_.LoadAll();
238 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); 249 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
239 250
240 // Number of loaded extensions should be the same after changing the pref. 251 // Number of loaded extensions should be the same after changing the pref.
241 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, 252 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL,
242 Value::CreateStringValue("http://www.google.de")); 253 Value::CreateStringValue("http://www.google.de"));
243 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); 254 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
244 } 255 }
245 256
257 TEST_F(ComponentLoaderTest, AddOrReplace) {
258 ASSERT_EQ(0u, component_loader_.registered_extensions_count());
259 component_loader_.AddDefaultComponentExtensions();
260 size_t const default_count = component_loader_.registered_extensions_count();
261 FilePath known_extension = GetBasePath()
262 .AppendASCII("override_component_extension");
263 FilePath unknow_extension = extension_path_;
264
265 // Replace a default component extension.
266 component_loader_.AddOrReplace(known_extension);
267 ASSERT_EQ(default_count,
268 component_loader_.registered_extensions_count());
269
270 // Add a new component extension.
271 component_loader_.AddOrReplace(unknow_extension);
272 ASSERT_EQ(default_count + 1,
273 component_loader_.registered_extensions_count());
274
275 extension_service_.set_ready(true);
276 component_loader_.LoadAll();
277
278 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
279 ASSERT_EQ(0u, extension_service_.unloaded_count());
280
281 // replace loaded component extension.
282 component_loader_.AddOrReplace(known_extension);
283 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
284 ASSERT_EQ(1u, extension_service_.unloaded_count());
285 }
286
246 } // namespace extensions 287 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/component_loader.cc ('k') | chrome/browser/extensions/extension_file_browser_private_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698