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

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

Issue 8477005: Add policies to specify an enterprise web store. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add tests for ComponentLoader. Created 9 years, 1 month 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "chrome/browser/extensions/component_loader.h"
8
9 #include "base/file_util.h"
10 #include "base/path_service.h"
11 #include "chrome/browser/extensions/test_extension_service.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 class StubExtensionService : public TestExtensionService {
Aaron Boodman 2011/11/15 17:34:47 Name this "MockExtensionService?" Also, it should
Patrick Dubroy 2011/11/15 19:19:16 Done and done. I named it that way because some p
19 private:
20 bool ready_;
21 ExtensionList extension_list_;
22
23 public:
24 StubExtensionService() : ready_(false) {
25 }
26
27 virtual void AddExtension(const Extension* extension) OVERRIDE {
28 // ExtensionService must become the owner of the extension object.
29 extension_list_.push_back(extension);
30 }
31
32 virtual void UnloadExtension(
33 const std::string& extension_id,
34 extension_misc::UnloadedExtensionReason reason) OVERRIDE {
35 // Remove the extension with the matching id.
36 for (ExtensionList::iterator it = extension_list_.begin();
37 it != extension_list_.end();
38 ++it) {
39 if ((*it)->id() == extension_id) {
40 extension_list_.erase(it);
41 return;
42 }
43 }
44 }
45
46 virtual bool is_ready() OVERRIDE {
47 return ready_;
48 }
49
50 virtual const ExtensionList* extensions() const OVERRIDE {
51 return &extension_list_;
52 }
53
54 void set_ready(bool ready) {
55 ready_ = ready;
56 }
57
58 void clear_extension_list() {
59 extension_list_.clear();
60 }
61 };
62
63 namespace extensions {
64
65 class ComponentLoaderTest : public testing::Test {
66 public:
67 ComponentLoaderTest() :
68 component_loader_(&extension_service_, &prefs_) {
69 }
70
71 void SetUp() {
72 FilePath test_data_dir;
73 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
74 extension_path_ =
75 test_data_dir.AppendASCII("extensions")
76 .AppendASCII("good")
77 .AppendASCII("Extensions")
78 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
79 .AppendASCII("1.0.0.0");
80
81 // Read in the extension manifest.
82 ASSERT_TRUE(file_util::ReadFileToString(
83 extension_path_.Append(Extension::kManifestFilename),
84 &manifest_contents_));
85
86 // Register the prefs that ComponentLoader will read.
87 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreURL, std::string());
88 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreName, std::string());
89 }
90
91 protected:
92 StubExtensionService extension_service_;
93 TestingPrefService prefs_;
94 ComponentLoader component_loader_;
95
96 // The root directory of the text extension.
97 FilePath extension_path_;
98
99 // The contents of the text extension's manifest file.
100 std::string manifest_contents_;
101 };
102
103 TEST_F(ComponentLoaderTest, ParseManifest) {
104 scoped_ptr<DictionaryValue> manifest;
105
106 // Test invalid JSON.
107 manifest.reset(
108 component_loader_.ParseManifest("{ 'test': 3 } invalid"));
109 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
110
111 // Test manifests that are valid JSON, but don't have an object literal
112 // at the root. ParseManifest() should always return NULL.
113
114 manifest.reset(component_loader_.ParseManifest(""));
115 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
116
117 manifest.reset(component_loader_.ParseManifest("[{ \"foo\": 3 }]"));
118 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
119
120 manifest.reset(component_loader_.ParseManifest("\"Test\""));
121 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
122
123 manifest.reset(component_loader_.ParseManifest("42"));
124 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
125
126 manifest.reset(component_loader_.ParseManifest("true"));
127 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
128
129 manifest.reset(component_loader_.ParseManifest("false"));
130 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
131
132 manifest.reset(component_loader_.ParseManifest("null"));
133 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
134
135 // Test parsing valid JSON.
136
137 int value;
138 manifest.reset(component_loader_.ParseManifest(
139 "{ \"test\": { \"one\": 1 }, \"two\": 2 }"));
140 ASSERT_NE(manifest.get(), (DictionaryValue*)NULL);
141 ASSERT_TRUE(manifest->GetInteger("test.one", &value));
142 ASSERT_EQ(1, value);
143 ASSERT_TRUE(manifest->GetInteger("two", &value));
144 ASSERT_EQ(2, value);
145
146 std::string string_value;
147 manifest.reset(component_loader_.ParseManifest(manifest_contents_));
148 ASSERT_TRUE(manifest->GetString("background_page", &string_value));
149 ASSERT_EQ("backgroundpage.html", string_value);
150 }
151
152 // Test that the extension isn't loaded if the extension service isn't ready.
153 TEST_F(ComponentLoaderTest, AddWhenNotReady) {
154 scoped_refptr<const Extension> extension;
155 extension_service_.set_ready(false);
156 extension = component_loader_.Add(manifest_contents_, extension_path_);
157 ASSERT_EQ((Extension*)NULL, extension.get());
158 ASSERT_EQ(0U, extension_service_.extensions()->size());
Aaron Boodman 2011/11/15 17:34:47 Nit: we use the lowercase "u" elsewhere in the cod
Patrick Dubroy 2011/11/15 19:19:16 Seems like we use both pretty heavily: /usr/local
159 }
160
161 // Test that it *is* loaded when the extension service *is* ready.
162 TEST_F(ComponentLoaderTest, AddWhenReady) {
163 scoped_refptr<const Extension> extension;
164 extension_service_.set_ready(true);
165 extension = component_loader_.Add(manifest_contents_, extension_path_);
166 ASSERT_NE((Extension*)NULL, extension.get());
167 ASSERT_EQ(1U, extension_service_.extensions()->size());
168 }
169
170 TEST_F(ComponentLoaderTest, Remove) {
171 extension_service_.set_ready(false);
172
173 // Removing an extension that was never added should be ok.
174 component_loader_.Remove(extension_path_);
175 ASSERT_EQ(0U, extension_service_.extensions()->size());
176
177 // Try adding and removing before LoadAll() is called.
178 component_loader_.Add(manifest_contents_, extension_path_);
179 component_loader_.Remove(extension_path_);
180 component_loader_.LoadAll();
181 ASSERT_EQ(0U, extension_service_.extensions()->size());
182
183 // Load an extension, and check that it's unloaded when Remove() is called.
184 scoped_refptr<const Extension> extension;
185 extension_service_.set_ready(true);
186 extension = component_loader_.Add(manifest_contents_, extension_path_);
187 ASSERT_NE((Extension*)NULL, extension.get());
188 component_loader_.Remove(extension_path_);
189 ASSERT_EQ(0U, extension_service_.extensions()->size());
190
191 // And after calling LoadAll(), it shouldn't get loaded.
192 component_loader_.LoadAll();
193 ASSERT_EQ(0U, extension_service_.extensions()->size());
194 }
195
196 TEST_F(ComponentLoaderTest, LoadAll) {
197 extension_service_.set_ready(false);
198
199 // No extensions should be loaded if none were added.
200 component_loader_.LoadAll();
201 ASSERT_EQ(0U, extension_service_.extensions()->size());
202
203 // Use LoadAll() to load the default extensions.
204 component_loader_.AddDefaultComponentExtensions();
205 component_loader_.LoadAll();
206 unsigned int default_count = extension_service_.extensions()->size();
207
208 // Clear the list of loaded extensions, and reload with one more.
209 extension_service_.clear_extension_list();
210 component_loader_.Add(manifest_contents_, extension_path_);
211 component_loader_.LoadAll();
212
213 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
214 }
215
216 TEST_F(ComponentLoaderTest, EnterpriseWebStore) {
217 component_loader_.AddDefaultComponentExtensions();
218 component_loader_.LoadAll();
219 unsigned int default_count = extension_service_.extensions()->size();
220
221 // Set the pref, and it should get loaded automatically.
222 extension_service_.set_ready(true);
223 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL,
224 Value::CreateStringValue("http://www.google.com"));
225 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
226
227 // Now that the pref is set, check if it's added by default.
228 extension_service_.set_ready(false);
229 extension_service_.clear_extension_list();
230 component_loader_.ClearAllRegisteredForTesting();
231 component_loader_.AddDefaultComponentExtensions();
232 component_loader_.LoadAll();
233 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
234
235 // Number of loaded extensions should be the same after changing the pref.
236 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL,
237 Value::CreateStringValue("http://www.google.de"));
238 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
239 }
240
241 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698