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

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

Issue 5213002: Fix for Bug 50726 "Save extension list and "winning" prefs from extensions" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Addressed comments, moved ExtensionPrefStore reference to Profile Created 10 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
(Empty)
1 // Copyright (c) 2010 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 #include <vector>
7
8 #include "base/scoped_ptr.h"
9 #include "base/scoped_temp_dir.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_pref_store.h"
12 #include "chrome/browser/prefs/in_memory_pref_store.h"
13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/prefs/pref_value_store.h"
15 #include "chrome/common/extensions/extension.h"
16 #include "chrome/test/testing_pref_service.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace keys = extension_manifest_keys;
21
22 namespace {
23
24 class TestExtensionPrefStore : public ExtensionPrefStore {
25 public:
26 TestExtensionPrefStore()
27 : ExtensionPrefStore(NULL, PrefNotifier::EXTENSION_STORE),
28 ext1(NULL),
29 ext2(NULL),
30 ext3(NULL),
31 pref_service_(NULL) {
32 // Can't use ASSERT_TRUE here because a constructor can't return a value.
33 if (!temp_dir_.CreateUniqueTempDir()) {
34 ADD_FAILURE() << "Failed to create temp dir";
35 return;
36 }
37 DictionaryValue simple_dict;
38 std::string error;
39
40 simple_dict.SetString(keys::kVersion, "1.0.0.0");
41 simple_dict.SetString(keys::kName, "unused");
42
43 ext1_scoped_ = Extension::Create(
44 temp_dir_.path().AppendASCII("ext1"), Extension::INVALID,
45 simple_dict, false, &error);
46 ext2_scoped_ = Extension::Create(
47 temp_dir_.path().AppendASCII("ext2"), Extension::INVALID,
48 simple_dict, false, &error);
49 ext3_scoped_ = Extension::Create(
50 temp_dir_.path().AppendASCII("ext3"), Extension::INVALID,
51 simple_dict, false, &error);
52
53 ext1 = ext1_scoped_.get();
54 ext2 = ext2_scoped_.get();
55 ext3 = ext3_scoped_.get();
56 }
57
58 typedef std::vector<std::string> ExtensionIDs;
59 void GetExtensionIDList(ExtensionIDs* result) {
60 GetExtensionIDs(result);
61 }
62
63 void SetPrefService(PrefService* pref_service) {
64 pref_service_ = pref_service;
65 }
66
67 // Overridden from ExtensionPrefStore.
68 virtual PrefService* GetPrefService() {
69 return pref_service_;
70 }
71
72 // Weak references, for convenience.
73 Extension* ext1;
74 Extension* ext2;
75 Extension* ext3;
76
77 private:
78 ScopedTempDir temp_dir_;
79
80 scoped_refptr<Extension> ext1_scoped_;
81 scoped_refptr<Extension> ext2_scoped_;
82 scoped_refptr<Extension> ext3_scoped_;
83
84 // Weak reference.
85 PrefService* pref_service_;
86 };
87
88 // Mock PrefNotifier that allows the notifications to be tracked.
89 class MockPrefNotifier : public PrefNotifier {
90 public:
91 MockPrefNotifier(PrefService* service, PrefValueStore* value_store)
92 : PrefNotifier(service, value_store) {}
93
94 virtual ~MockPrefNotifier() {}
95
96 MOCK_METHOD1(FireObservers, void(const char* path));
97 };
98
99 // Mock PrefService that allows the PrefNotifier to be injected.
100 class MockPrefService : public PrefService {
101 public:
102 explicit MockPrefService(PrefValueStore* pref_value_store)
103 : PrefService(pref_value_store) {
104 }
105
106 void SetPrefNotifier(MockPrefNotifier* notifier) {
107 pref_notifier_.reset(notifier);
108 }
109 };
110
111 // Use constants to avoid confusing std::map with hard-coded strings.
112 const char kPref1[] = "path1.subpath";
113 const char kPref2[] = "path2";
114 const char kPref3[] = "path3";
115 const char kPref4[] = "path4";
116
117 } // namespace
118
119 TEST(ExtensionPrefStoreTest, InstallOneExtension) {
120 TestExtensionPrefStore eps;
121 ASSERT_TRUE(eps.ext1 != NULL);
122 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
123
124 TestExtensionPrefStore::ExtensionIDs ids;
125 eps.GetExtensionIDList(&ids);
126 EXPECT_EQ(1u, ids.size());
127 EXPECT_EQ(eps.ext1->id(), ids[0]);
128
129 DictionaryValue* prefs = eps.prefs();
130 ASSERT_EQ(1u, prefs->size());
131 std::string actual;
132 ASSERT_TRUE(prefs->GetString(kPref1, &actual));
133 EXPECT_EQ("val1", actual);
134 }
135
136 // Make sure the last-installed extension wins.
137 TEST(ExtensionPrefStoreTest, InstallMultipleExtensions) {
138 TestExtensionPrefStore eps;
139 ASSERT_TRUE(eps.ext1 != NULL);
140 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
141 eps.InstallExtensionPref(eps.ext2, kPref1, Value::CreateStringValue("val2"));
142 eps.InstallExtensionPref(eps.ext3, kPref1, Value::CreateStringValue("val3"));
143
144 TestExtensionPrefStore::ExtensionIDs ids;
145 eps.GetExtensionIDList(&ids);
146 EXPECT_EQ(3u, ids.size());
147 EXPECT_EQ(eps.ext3->id(), ids[0]);
148 EXPECT_EQ(eps.ext2->id(), ids[1]);
149 EXPECT_EQ(eps.ext1->id(), ids[2]);
150
151 DictionaryValue* prefs = eps.prefs();
152 ASSERT_EQ(1u, prefs->size());
153 std::string actual;
154 ASSERT_TRUE(prefs->GetString(kPref1, &actual));
155 EXPECT_EQ("val3", actual);
156 }
157
158 // Make sure the last-installed extension wins for each preference.
159 TEST(ExtensionPrefStoreTest, InstallOverwrittenExtensions) {
160 TestExtensionPrefStore eps;
161 ASSERT_TRUE(eps.ext1 != NULL);
162 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
163 eps.InstallExtensionPref(eps.ext2, kPref1, Value::CreateStringValue("val2"));
164 eps.InstallExtensionPref(eps.ext3, kPref1, Value::CreateStringValue("val3"));
165
166 eps.InstallExtensionPref(eps.ext1, kPref2, Value::CreateStringValue("val4"));
167 eps.InstallExtensionPref(eps.ext2, kPref2, Value::CreateStringValue("val5"));
168
169 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val6"));
170 eps.InstallExtensionPref(eps.ext1, kPref2, Value::CreateStringValue("val7"));
171 eps.InstallExtensionPref(eps.ext1, kPref3, Value::CreateStringValue("val8"));
172
173 TestExtensionPrefStore::ExtensionIDs ids;
174 eps.GetExtensionIDList(&ids);
175 EXPECT_EQ(3u, ids.size());
176 EXPECT_EQ(eps.ext3->id(), ids[0]);
177 EXPECT_EQ(eps.ext2->id(), ids[1]);
178 EXPECT_EQ(eps.ext1->id(), ids[2]);
179
180 DictionaryValue* prefs = eps.prefs();
181 ASSERT_EQ(3u, prefs->size());
182 std::string actual;
183 EXPECT_TRUE(prefs->GetString(kPref1, &actual));
184 EXPECT_EQ("val3", actual);
185 EXPECT_TRUE(prefs->GetString(kPref2, &actual));
186 EXPECT_EQ("val5", actual);
187 EXPECT_TRUE(prefs->GetString(kPref3, &actual));
188 EXPECT_EQ("val8", actual);
189 }
190
191 // Make sure the last-installed extension wins even if other extensions set
192 // the same or different preferences later.
193 TEST(ExtensionPrefStoreTest, InstallInterleavedExtensions) {
194 TestExtensionPrefStore eps;
195 ASSERT_TRUE(eps.ext1 != NULL);
196 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
197 eps.InstallExtensionPref(eps.ext2, kPref2, Value::CreateStringValue("val2"));
198 eps.InstallExtensionPref(eps.ext3, kPref3, Value::CreateStringValue("val3"));
199
200 eps.InstallExtensionPref(eps.ext3, kPref3, Value::CreateStringValue("val4"));
201 eps.InstallExtensionPref(eps.ext2, kPref3, Value::CreateStringValue("val5"));
202 eps.InstallExtensionPref(eps.ext1, kPref3, Value::CreateStringValue("val6"));
203
204 eps.InstallExtensionPref(eps.ext3, kPref1, Value::CreateStringValue("val7"));
205
206 TestExtensionPrefStore::ExtensionIDs ids;
207 eps.GetExtensionIDList(&ids);
208 EXPECT_EQ(3u, ids.size());
209 EXPECT_EQ(eps.ext3->id(), ids[0]);
210 EXPECT_EQ(eps.ext2->id(), ids[1]);
211 EXPECT_EQ(eps.ext1->id(), ids[2]);
212
213 DictionaryValue* prefs = eps.prefs();
214 ASSERT_EQ(3u, prefs->size());
215 std::string actual;
216 EXPECT_TRUE(prefs->GetString(kPref1, &actual));
217 EXPECT_EQ("val7", actual);
218 EXPECT_TRUE(prefs->GetString(kPref2, &actual));
219 EXPECT_EQ("val2", actual);
220 EXPECT_TRUE(prefs->GetString(kPref3, &actual));
221 EXPECT_EQ("val4", actual);
222 }
223
224 TEST(ExtensionPrefStoreTest, UninstallOnlyExtension) {
225 TestExtensionPrefStore eps;
226 ASSERT_TRUE(eps.ext1 != NULL);
227 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
228 eps.InstallExtensionPref(eps.ext1, kPref2, Value::CreateStringValue("val2"));
229
230 // No need to check the state here; the Install* tests cover that.
231 eps.UninstallExtension(eps.ext1);
232
233 TestExtensionPrefStore::ExtensionIDs ids;
234 eps.GetExtensionIDList(&ids);
235 EXPECT_EQ(0u, ids.size());
236
237 DictionaryValue* prefs = eps.prefs();
238 std::string actual;
239 // "path1.name" has been removed, but an empty "path1" dictionary is still
240 // present.
241 ASSERT_EQ(1u, prefs->size());
242 EXPECT_FALSE(prefs->GetString(kPref1, &actual));
243 EXPECT_FALSE(prefs->GetString(kPref2, &actual));
244 }
245
246 // Tests uninstalling an extension that wasn't winning for any preferences.
247 TEST(ExtensionPrefStoreTest, UninstallIrrelevantExtension) {
248 TestExtensionPrefStore eps;
249 ASSERT_TRUE(eps.ext1 != NULL);
250 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
251 eps.InstallExtensionPref(eps.ext2, kPref1, Value::CreateStringValue("val2"));
252
253 eps.InstallExtensionPref(eps.ext1, kPref2, Value::CreateStringValue("val3"));
254 eps.InstallExtensionPref(eps.ext2, kPref2, Value::CreateStringValue("val4"));
255
256 eps.UninstallExtension(eps.ext1);
257
258 TestExtensionPrefStore::ExtensionIDs ids;
259 eps.GetExtensionIDList(&ids);
260 EXPECT_EQ(1u, ids.size());
261 EXPECT_EQ(eps.ext2->id(), ids[0]);
262
263 DictionaryValue* prefs = eps.prefs();
264 ASSERT_EQ(2u, prefs->size());
265 std::string actual;
266 EXPECT_TRUE(prefs->GetString(kPref1, &actual));
267 EXPECT_EQ("val2", actual);
268 EXPECT_TRUE(prefs->GetString(kPref2, &actual));
269 EXPECT_EQ("val4", actual);
270 }
271
272 // Tests uninstalling an extension that was winning for all preferences.
273 TEST(ExtensionPrefStoreTest, UninstallExtensionFromTop) {
274 TestExtensionPrefStore eps;
275 ASSERT_TRUE(eps.ext1 != NULL);
276 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
277 eps.InstallExtensionPref(eps.ext2, kPref1, Value::CreateStringValue("val2"));
278 eps.InstallExtensionPref(eps.ext3, kPref1, Value::CreateStringValue("val3"));
279
280 eps.InstallExtensionPref(eps.ext1, kPref2, Value::CreateStringValue("val4"));
281 eps.InstallExtensionPref(eps.ext3, kPref2, Value::CreateStringValue("val5"));
282
283 eps.UninstallExtension(eps.ext3);
284
285 TestExtensionPrefStore::ExtensionIDs ids;
286 eps.GetExtensionIDList(&ids);
287 EXPECT_EQ(2u, ids.size());
288 EXPECT_EQ(eps.ext2->id(), ids[0]);
289 EXPECT_EQ(eps.ext1->id(), ids[1]);
290
291 DictionaryValue* prefs = eps.prefs();
292 ASSERT_EQ(2u, prefs->size());
293 std::string actual;
294 EXPECT_TRUE(prefs->GetString(kPref1, &actual));
295 EXPECT_EQ("val2", actual);
296 EXPECT_TRUE(prefs->GetString(kPref2, &actual));
297 EXPECT_EQ("val4", actual);
298 }
299
300 // Tests uninstalling an extension that was winning for only some preferences.
301 TEST(ExtensionPrefStoreTest, UninstallExtensionFromMiddle) {
302 TestExtensionPrefStore eps;
303 ASSERT_TRUE(eps.ext1 != NULL);
304 eps.InstallExtensionPref(eps.ext1, kPref1, Value::CreateStringValue("val1"));
305 eps.InstallExtensionPref(eps.ext2, kPref1, Value::CreateStringValue("val2"));
306 eps.InstallExtensionPref(eps.ext3, kPref1, Value::CreateStringValue("val3"));
307
308 eps.InstallExtensionPref(eps.ext1, kPref2, Value::CreateStringValue("val4"));
309 eps.InstallExtensionPref(eps.ext2, kPref2, Value::CreateStringValue("val5"));
310
311 eps.InstallExtensionPref(eps.ext1, kPref3, Value::CreateStringValue("val6"));
312
313 eps.InstallExtensionPref(eps.ext2, kPref4, Value::CreateStringValue("val7"));
314
315 eps.UninstallExtension(eps.ext2);
316
317 TestExtensionPrefStore::ExtensionIDs ids;
318 eps.GetExtensionIDList(&ids);
319 EXPECT_EQ(2u, ids.size());
320 EXPECT_EQ(eps.ext3->id(), ids[0]);
321 EXPECT_EQ(eps.ext1->id(), ids[1]);
322
323 DictionaryValue* prefs = eps.prefs();
324 ASSERT_EQ(3u, prefs->size());
325 std::string actual;
326 EXPECT_TRUE(prefs->GetString(kPref1, &actual));
327 EXPECT_EQ("val3", actual);
328 EXPECT_TRUE(prefs->GetString(kPref2, &actual));
329 EXPECT_EQ("val4", actual);
330 EXPECT_TRUE(prefs->GetString(kPref3, &actual));
331 EXPECT_EQ("val6", actual);
332 EXPECT_FALSE(prefs->GetString(kPref4, &actual));
333 }
334
335 TEST(ExtensionPrefStoreTest, NotifyWhenNeeded) {
336 using testing::Mock;
337
338 TestExtensionPrefStore* eps = new TestExtensionPrefStore;
339 InMemoryPrefStore* dps = new InMemoryPrefStore;
340 ASSERT_TRUE(eps->ext1 != NULL);
341
342 // The PrefValueStore takes ownership of the PrefStores; in this case, that's
343 // only an ExtensionPrefStore. Likewise, the PrefService takes ownership of
344 // the PrefValueStore and PrefNotifier.
345 PrefValueStore* value_store = new TestingPrefService::TestingPrefValueStore(
346 NULL, NULL, eps, NULL, NULL, NULL, dps);
347 scoped_ptr<MockPrefService> pref_service(new MockPrefService(value_store));
348 MockPrefNotifier* pref_notifier = new MockPrefNotifier(pref_service.get(),
349 value_store);
350 pref_service->SetPrefNotifier(pref_notifier);
351
352 eps->SetPrefService(pref_service.get());
353 pref_service->RegisterStringPref(kPref1, std::string());
354
355 EXPECT_CALL(*pref_notifier, FireObservers(kPref1));
356 eps->InstallExtensionPref(eps->ext1, kPref1,
357 Value::CreateStringValue("https://www.chromium.org"));
358 Mock::VerifyAndClearExpectations(pref_notifier);
359
360 EXPECT_CALL(*pref_notifier, FireObservers(kPref1)).Times(0);
361 eps->InstallExtensionPref(eps->ext1, kPref1,
362 Value::CreateStringValue("https://www.chromium.org"));
363 Mock::VerifyAndClearExpectations(pref_notifier);
364
365 EXPECT_CALL(*pref_notifier, FireObservers(kPref1)).Times(2);
366 eps->InstallExtensionPref(eps->ext1, kPref1,
367 Value::CreateStringValue("chrome://newtab"));
368 eps->UninstallExtension(eps->ext1);
369 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698