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

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: Code review fix. 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/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 find(std::string id) {
Aaron Boodman 2011/12/06 19:43:35 There is already GetExtensionById in TestExtension
SeRya 2011/12/06 22:19:37 Irrelevant after merge.
Aaron Boodman 2011/12/07 00:09:02 What I mean is that the TestExtensionService base
23 for (ExtensionList::iterator it = extension_list_.begin();
24 it != extension_list_.end();
25 ++it)
26 if ((*it)->id() == id)
27 return true;
28
29 return false;
30 }
31
22 bool ready_; 32 bool ready_;
23 ExtensionList extension_list_; 33 ExtensionList extension_list_;
34 size_t unloaded_count_;
24 35
25 public: 36 public:
26 MockExtensionService() : ready_(false) { 37 MockExtensionService() : ready_(false), unloaded_count_(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_count_++;
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 unloaded_count() const {
75 return unloaded_count_;
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 {
70 public: 88 public:
71 ComponentLoaderTest() : 89 ComponentLoaderTest() :
72 // Note: we pass the same pref service here, to stand in for both 90 // Note: we pass the same pref service here, to stand in for both
73 // user prefs and local state. 91 // user prefs and local state.
74 component_loader_(&extension_service_, &prefs_, &prefs_) { 92 component_loader_(&extension_service_, &prefs_, &prefs_) {
75 } 93 }
76 94
77 void SetUp() { 95 void SetUp() {
78 FilePath test_data_dir;
79 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
80 extension_path_ = 96 extension_path_ =
81 test_data_dir.AppendASCII("extensions") 97 GetBasePath().AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
82 .AppendASCII("good")
83 .AppendASCII("Extensions")
84 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
85 .AppendASCII("1.0.0.0"); 98 .AppendASCII("1.0.0.0");
86 99
87 // Read in the extension manifest. 100 // Read in the extension manifest.
88 ASSERT_TRUE(file_util::ReadFileToString( 101 ASSERT_TRUE(file_util::ReadFileToString(
89 extension_path_.Append(Extension::kManifestFilename), 102 extension_path_.Append(Extension::kManifestFilename),
90 &manifest_contents_)); 103 &manifest_contents_));
91 104
92 // Register the user prefs that ComponentLoader will read. 105 // Register the user prefs that ComponentLoader will read.
93 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreURL, std::string()); 106 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreURL, std::string());
94 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreName, std::string()); 107 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreName, std::string());
95 108
96 // Register the local state prefs. 109 // Register the local state prefs.
97 #if defined(OS_CHROMEOS) 110 #if defined(OS_CHROMEOS)
98 prefs_.RegisterBooleanPref(prefs::kAccessibilityEnabled, false); 111 prefs_.RegisterBooleanPref(prefs::kAccessibilityEnabled, false);
99 #endif 112 #endif
100 } 113 }
101 114
102 protected: 115 protected:
103 MockExtensionService extension_service_; 116 MockExtensionService extension_service_;
104 TestingPrefService prefs_; 117 TestingPrefService prefs_;
105 ComponentLoader component_loader_; 118 ComponentLoader component_loader_;
106 119
107 // The root directory of the text extension. 120 // The root directory of the text extension.
108 FilePath extension_path_; 121 FilePath extension_path_;
109 122
110 // The contents of the text extension's manifest file. 123 // The contents of the text extension's manifest file.
111 std::string manifest_contents_; 124 std::string manifest_contents_;
125
126 FilePath GetBasePath() {
127 FilePath test_data_dir;
128 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
129 return test_data_dir.AppendASCII("extensions")
130 .AppendASCII("good")
131 .AppendASCII("Extensions");
132 }
112 }; 133 };
113 134
114 TEST_F(ComponentLoaderTest, ParseManifest) { 135 TEST_F(ComponentLoaderTest, ParseManifest) {
115 scoped_ptr<DictionaryValue> manifest; 136 scoped_ptr<DictionaryValue> manifest;
116 137
117 // Test invalid JSON. 138 // Test invalid JSON.
118 manifest.reset( 139 manifest.reset(
119 component_loader_.ParseManifest("{ 'test': 3 } invalid")); 140 component_loader_.ParseManifest("{ 'test': 3 } invalid"));
120 ASSERT_EQ((DictionaryValue*)NULL, manifest.get()); 141 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
121 142
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 ASSERT_TRUE(manifest->GetString("background_page", &string_value)); 180 ASSERT_TRUE(manifest->GetString("background_page", &string_value));
160 ASSERT_EQ("backgroundpage.html", string_value); 181 ASSERT_EQ("backgroundpage.html", string_value);
161 } 182 }
162 183
163 // Test that the extension isn't loaded if the extension service isn't ready. 184 // Test that the extension isn't loaded if the extension service isn't ready.
164 TEST_F(ComponentLoaderTest, AddWhenNotReady) { 185 TEST_F(ComponentLoaderTest, AddWhenNotReady) {
165 scoped_refptr<const Extension> extension; 186 scoped_refptr<const Extension> extension;
166 extension_service_.set_ready(false); 187 extension_service_.set_ready(false);
167 extension = component_loader_.Add(manifest_contents_, extension_path_); 188 extension = component_loader_.Add(manifest_contents_, extension_path_);
168 ASSERT_EQ((Extension*)NULL, extension.get()); 189 ASSERT_EQ((Extension*)NULL, extension.get());
169 ASSERT_EQ(0U, extension_service_.extensions()->size()); 190 ASSERT_EQ(0u, extension_service_.extensions()->size());
170 } 191 }
171 192
172 // Test that it *is* loaded when the extension service *is* ready. 193 // Test that it *is* loaded when the extension service *is* ready.
173 TEST_F(ComponentLoaderTest, AddWhenReady) { 194 TEST_F(ComponentLoaderTest, AddWhenReady) {
174 scoped_refptr<const Extension> extension; 195 scoped_refptr<const Extension> extension;
175 extension_service_.set_ready(true); 196 extension_service_.set_ready(true);
176 extension = component_loader_.Add(manifest_contents_, extension_path_); 197 extension = component_loader_.Add(manifest_contents_, extension_path_);
177 ASSERT_NE((Extension*)NULL, extension.get()); 198 ASSERT_NE((Extension*)NULL, extension.get());
178 ASSERT_EQ(1U, extension_service_.extensions()->size()); 199 ASSERT_EQ(1u, extension_service_.extensions()->size());
179 } 200 }
180 201
181 TEST_F(ComponentLoaderTest, Remove) { 202 TEST_F(ComponentLoaderTest, Remove) {
182 extension_service_.set_ready(false); 203 extension_service_.set_ready(false);
183 204
184 // Removing an extension that was never added should be ok. 205 // Removing an extension that was never added should be ok.
185 component_loader_.Remove(extension_path_); 206 component_loader_.Remove(extension_path_);
186 ASSERT_EQ(0U, extension_service_.extensions()->size()); 207 ASSERT_EQ(0u, extension_service_.extensions()->size());
187 208
188 // Try adding and removing before LoadAll() is called. 209 // Try adding and removing before LoadAll() is called.
189 component_loader_.Add(manifest_contents_, extension_path_); 210 component_loader_.Add(manifest_contents_, extension_path_);
190 component_loader_.Remove(extension_path_); 211 component_loader_.Remove(extension_path_);
191 component_loader_.LoadAll(); 212 component_loader_.LoadAll();
192 ASSERT_EQ(0U, extension_service_.extensions()->size()); 213 ASSERT_EQ(0u, extension_service_.extensions()->size());
193 214
194 // Load an extension, and check that it's unloaded when Remove() is called. 215 // Load an extension, and check that it's unloaded when Remove() is called.
195 scoped_refptr<const Extension> extension; 216 scoped_refptr<const Extension> extension;
196 extension_service_.set_ready(true); 217 extension_service_.set_ready(true);
197 extension = component_loader_.Add(manifest_contents_, extension_path_); 218 extension = component_loader_.Add(manifest_contents_, extension_path_);
198 ASSERT_NE((Extension*)NULL, extension.get()); 219 ASSERT_NE((Extension*)NULL, extension.get());
199 component_loader_.Remove(extension_path_); 220 component_loader_.Remove(extension_path_);
200 ASSERT_EQ(0U, extension_service_.extensions()->size()); 221 ASSERT_EQ(0u, extension_service_.extensions()->size());
201 222
202 // And after calling LoadAll(), it shouldn't get loaded. 223 // And after calling LoadAll(), it shouldn't get loaded.
203 component_loader_.LoadAll(); 224 component_loader_.LoadAll();
204 ASSERT_EQ(0U, extension_service_.extensions()->size()); 225 ASSERT_EQ(0u, extension_service_.extensions()->size());
205 } 226 }
206 227
207 TEST_F(ComponentLoaderTest, LoadAll) { 228 TEST_F(ComponentLoaderTest, LoadAll) {
208 extension_service_.set_ready(false); 229 extension_service_.set_ready(false);
209 230
210 // No extensions should be loaded if none were added. 231 // No extensions should be loaded if none were added.
211 component_loader_.LoadAll(); 232 component_loader_.LoadAll();
212 ASSERT_EQ(0U, extension_service_.extensions()->size()); 233 ASSERT_EQ(0u, extension_service_.extensions()->size());
Aaron Boodman 2011/12/06 19:43:35 thank-you!
213 234
214 // Use LoadAll() to load the default extensions. 235 // Use LoadAll() to load the default extensions.
215 component_loader_.AddDefaultComponentExtensions(); 236 component_loader_.AddDefaultComponentExtensions();
216 component_loader_.LoadAll(); 237 component_loader_.LoadAll();
217 unsigned int default_count = extension_service_.extensions()->size(); 238 unsigned int default_count = extension_service_.extensions()->size();
218 239
219 // Clear the list of loaded extensions, and reload with one more. 240 // Clear the list of loaded extensions, and reload with one more.
220 extension_service_.clear_extension_list(); 241 extension_service_.clear_extension_list();
221 component_loader_.Add(manifest_contents_, extension_path_); 242 component_loader_.Add(manifest_contents_, extension_path_);
222 component_loader_.LoadAll(); 243 component_loader_.LoadAll();
(...skipping 19 matching lines...) Expand all
242 component_loader_.AddDefaultComponentExtensions(); 263 component_loader_.AddDefaultComponentExtensions();
243 component_loader_.LoadAll(); 264 component_loader_.LoadAll();
244 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); 265 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
245 266
246 // Number of loaded extensions should be the same after changing the pref. 267 // Number of loaded extensions should be the same after changing the pref.
247 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL, 268 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL,
248 Value::CreateStringValue("http://www.google.de")); 269 Value::CreateStringValue("http://www.google.de"));
249 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size()); 270 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
250 } 271 }
251 272
273 TEST_F(ComponentLoaderTest, AddOrReplace) {
274 ASSERT_EQ(0u, component_loader_.GetRegisteredExtensionsCount());
275 component_loader_.AddDefaultComponentExtensions();
276 size_t const default_count = component_loader_.GetRegisteredExtensionsCount();
277 FilePath known_extension = GetBasePath()
278 .AppendASCII("mndnfokpggljbaajbnioimlmbfngpief");
279 FilePath unknow_extension = GetBasePath()
280 .AppendASCII("hpiknbiabeeppbpihjehijgoemciehgk")
281 .AppendASCII("2");
282
283 // Replace a default component extension.
284 component_loader_.AddOrReplace(known_extension);
285 ASSERT_EQ(default_count,
286 component_loader_.GetRegisteredExtensionsCount());
287
288 // Add a new component extension.
289 component_loader_.AddOrReplace(unknow_extension);
290 ASSERT_EQ(default_count + 1,
291 component_loader_.GetRegisteredExtensionsCount());
292
293 extension_service_.set_ready(true);
294 component_loader_.LoadAll();
295
296 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
297 ASSERT_EQ(0u, extension_service_.unloaded_count());
298
299 // replace loaded component extension.
300 component_loader_.AddOrReplace(known_extension);
301 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
302 ASSERT_EQ(1u, extension_service_.unloaded_count());
303 }
304
252 } // namespace extensions 305 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698