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

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: Rebasse. 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 namespace {
19
20 class MockExtensionService : public TestExtensionService {
21 private:
22 bool ready_;
23 ExtensionList extension_list_;
24
25 public:
26 MockExtensionService() : ready_(false) {
27 }
28
29 virtual void AddExtension(const Extension* extension) OVERRIDE {
30 // ExtensionService must become the owner of the extension object.
31 extension_list_.push_back(extension);
32 }
33
34 virtual void UnloadExtension(
35 const std::string& extension_id,
36 extension_misc::UnloadedExtensionReason reason) OVERRIDE {
37 // Remove the extension with the matching 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 }
46 }
47
48 virtual bool is_ready() OVERRIDE {
49 return ready_;
50 }
51
52 virtual const ExtensionList* extensions() const OVERRIDE {
53 return &extension_list_;
54 }
55
56 void set_ready(bool ready) {
57 ready_ = ready;
58 }
59
60 void clear_extension_list() {
61 extension_list_.clear();
62 }
63 };
64
65 } // namespace
66
67 namespace extensions {
68
69 class ComponentLoaderTest : public testing::Test {
70 public:
71 ComponentLoaderTest() :
72 // Note: we pass the same pref service here, to stand in for both
73 // user prefs and local state.
74 component_loader_(&extension_service_, &prefs_, &prefs_) {
75 }
76
77 void SetUp() {
78 FilePath test_data_dir;
79 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
80 extension_path_ =
81 test_data_dir.AppendASCII("extensions")
82 .AppendASCII("good")
83 .AppendASCII("Extensions")
84 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
85 .AppendASCII("1.0.0.0");
86
87 // Read in the extension manifest.
88 ASSERT_TRUE(file_util::ReadFileToString(
89 extension_path_.Append(Extension::kManifestFilename),
90 &manifest_contents_));
91
92 // Register the user prefs that ComponentLoader will read.
93 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreURL, std::string());
94 prefs_.RegisterStringPref(prefs::kEnterpriseWebStoreName, std::string());
95
96 // Register the local state prefs.
97 #if defined(OS_CHROMEOS)
98 prefs_.RegisterBooleanPref(prefs::kAccessibilityEnabled, false);
99 #endif
100 }
101
102 protected:
103 MockExtensionService extension_service_;
104 TestingPrefService prefs_;
105 ComponentLoader component_loader_;
106
107 // The root directory of the text extension.
108 FilePath extension_path_;
109
110 // The contents of the text extension's manifest file.
111 std::string manifest_contents_;
112 };
113
114 TEST_F(ComponentLoaderTest, ParseManifest) {
115 scoped_ptr<DictionaryValue> manifest;
116
117 // Test invalid JSON.
118 manifest.reset(
119 component_loader_.ParseManifest("{ 'test': 3 } invalid"));
120 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
121
122 // Test manifests that are valid JSON, but don't have an object literal
123 // at the root. ParseManifest() should always return NULL.
124
125 manifest.reset(component_loader_.ParseManifest(""));
126 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
127
128 manifest.reset(component_loader_.ParseManifest("[{ \"foo\": 3 }]"));
129 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
130
131 manifest.reset(component_loader_.ParseManifest("\"Test\""));
132 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
133
134 manifest.reset(component_loader_.ParseManifest("42"));
135 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
136
137 manifest.reset(component_loader_.ParseManifest("true"));
138 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
139
140 manifest.reset(component_loader_.ParseManifest("false"));
141 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
142
143 manifest.reset(component_loader_.ParseManifest("null"));
144 ASSERT_EQ((DictionaryValue*)NULL, manifest.get());
145
146 // Test parsing valid JSON.
147
148 int value;
149 manifest.reset(component_loader_.ParseManifest(
150 "{ \"test\": { \"one\": 1 }, \"two\": 2 }"));
151 ASSERT_NE(manifest.get(), (DictionaryValue*)NULL);
152 ASSERT_TRUE(manifest->GetInteger("test.one", &value));
153 ASSERT_EQ(1, value);
154 ASSERT_TRUE(manifest->GetInteger("two", &value));
155 ASSERT_EQ(2, value);
156
157 std::string string_value;
158 manifest.reset(component_loader_.ParseManifest(manifest_contents_));
159 ASSERT_TRUE(manifest->GetString("background_page", &string_value));
160 ASSERT_EQ("backgroundpage.html", string_value);
161 }
162
163 // Test that the extension isn't loaded if the extension service isn't ready.
164 TEST_F(ComponentLoaderTest, AddWhenNotReady) {
165 scoped_refptr<const Extension> extension;
166 extension_service_.set_ready(false);
167 extension = component_loader_.Add(manifest_contents_, extension_path_);
168 ASSERT_EQ((Extension*)NULL, extension.get());
169 ASSERT_EQ(0U, extension_service_.extensions()->size());
170 }
171
172 // Test that it *is* loaded when the extension service *is* ready.
173 TEST_F(ComponentLoaderTest, AddWhenReady) {
174 scoped_refptr<const Extension> extension;
175 extension_service_.set_ready(true);
176 extension = component_loader_.Add(manifest_contents_, extension_path_);
177 ASSERT_NE((Extension*)NULL, extension.get());
178 ASSERT_EQ(1U, extension_service_.extensions()->size());
179 }
180
181 TEST_F(ComponentLoaderTest, Remove) {
182 extension_service_.set_ready(false);
183
184 // Removing an extension that was never added should be ok.
185 component_loader_.Remove(extension_path_);
186 ASSERT_EQ(0U, extension_service_.extensions()->size());
187
188 // Try adding and removing before LoadAll() is called.
189 component_loader_.Add(manifest_contents_, extension_path_);
190 component_loader_.Remove(extension_path_);
191 component_loader_.LoadAll();
192 ASSERT_EQ(0U, extension_service_.extensions()->size());
193
194 // Load an extension, and check that it's unloaded when Remove() is called.
195 scoped_refptr<const Extension> extension;
196 extension_service_.set_ready(true);
197 extension = component_loader_.Add(manifest_contents_, extension_path_);
198 ASSERT_NE((Extension*)NULL, extension.get());
199 component_loader_.Remove(extension_path_);
200 ASSERT_EQ(0U, extension_service_.extensions()->size());
201
202 // And after calling LoadAll(), it shouldn't get loaded.
203 component_loader_.LoadAll();
204 ASSERT_EQ(0U, extension_service_.extensions()->size());
205 }
206
207 TEST_F(ComponentLoaderTest, LoadAll) {
208 extension_service_.set_ready(false);
209
210 // No extensions should be loaded if none were added.
211 component_loader_.LoadAll();
212 ASSERT_EQ(0U, extension_service_.extensions()->size());
213
214 // Use LoadAll() to load the default extensions.
215 component_loader_.AddDefaultComponentExtensions();
216 component_loader_.LoadAll();
217 unsigned int default_count = extension_service_.extensions()->size();
218
219 // Clear the list of loaded extensions, and reload with one more.
220 extension_service_.clear_extension_list();
221 component_loader_.Add(manifest_contents_, extension_path_);
222 component_loader_.LoadAll();
223
224 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
225 }
226
227 TEST_F(ComponentLoaderTest, EnterpriseWebStore) {
228 component_loader_.AddDefaultComponentExtensions();
229 component_loader_.LoadAll();
230 unsigned int default_count = extension_service_.extensions()->size();
231
232 // Set the pref, and it should get loaded automatically.
233 extension_service_.set_ready(true);
234 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL,
235 Value::CreateStringValue("http://www.google.com"));
236 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
237
238 // Now that the pref is set, check if it's added by default.
239 extension_service_.set_ready(false);
240 extension_service_.clear_extension_list();
241 component_loader_.ClearAllRegistered();
242 component_loader_.AddDefaultComponentExtensions();
243 component_loader_.LoadAll();
244 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
245
246 // Number of loaded extensions should be the same after changing the pref.
247 prefs_.SetUserPref(prefs::kEnterpriseWebStoreURL,
248 Value::CreateStringValue("http://www.google.de"));
249 ASSERT_EQ(default_count + 1, extension_service_.extensions()->size());
250 }
251
252 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698