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

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

Issue 7775008: Enable sync for the settings from the Extension Settings API. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Review #6 Created 9 years, 3 months 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 "testing/gtest/include/gtest/gtest.h"
6
7 #include "base/bind.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/task.h"
14 #include "chrome/browser/extensions/extension_settings.h"
15 #include "chrome/browser/extensions/extension_settings_storage_cache.h"
16 #include "chrome/browser/extensions/extension_settings_noop_storage.h"
17 #include "chrome/browser/extensions/extension_settings_sync_util.h"
18 #include "chrome/browser/extensions/syncable_extension_settings_storage.h"
19 #include "chrome/browser/sync/api/sync_change_processor.h"
20 #include "content/browser/browser_thread.h"
21
22 // TODO(kalman): Integration tests for sync.
23
24 namespace {
25
26 // Gets the pretty-printed JSON for a value.
27 static std::string GetJson(const Value& value) {
28 std::string json;
29 base::JSONWriter::Write(&value, true, &json);
30 return json;
31 }
32
33 // Returns whether two Values are equal.
34 testing::AssertionResult ValuesEq(
35 const char* expected_expr,
36 const char* actual_expr,
37 const Value* expected,
38 const Value* actual) {
39 if (expected == actual) {
40 return testing::AssertionSuccess();
41 }
42 if (!expected && actual) {
43 return testing::AssertionFailure() <<
44 "Expected NULL, actual: " << GetJson(*actual);
45 }
46 if (expected && !actual) {
47 return testing::AssertionFailure() <<
48 "Expected: " << GetJson(*expected) << ", actual NULL";
49 }
50 if (!expected->Equals(actual)) {
51 return testing::AssertionFailure() <<
52 "Expected: " << GetJson(*expected) << ", actual: " << GetJson(*actual);
53 }
54 return testing::AssertionSuccess();
55 }
56
57 // Returns whether the result of a storage operation is an expected value.
58 // Logs when different.
59 testing::AssertionResult SettingsEq(
60 const char* expected_expr,
61 const char* actual_expr,
62 const DictionaryValue* expected,
63 const ExtensionSettingsStorage::Result actual) {
64 if (actual.HasError()) {
65 return testing::AssertionFailure() <<
66 "Expected: " << GetJson(*expected) <<
67 ", actual has error: " << actual.GetError();
68 }
69 return ValuesEq(
70 expected_expr, actual_expr, expected, actual.GetSettings());
71 }
72
73 // SyncChangeProcessor which just records the changes made, accessed after
74 // being converted to the more useful ExtensionSettingSyncData via changes().
75 class MockSyncChangeProcessor : public SyncChangeProcessor {
76 public:
77 virtual SyncError ProcessSyncChanges(
78 const tracked_objects::Location& from_here,
79 const SyncChangeList& change_list) OVERRIDE {
80 for (SyncChangeList::const_iterator it = change_list.begin();
81 it != change_list.end(); ++it) {
82 changes_.push_back(ExtensionSettingSyncData(*it));
83 }
84 return SyncError();
85 }
86
87 const ExtensionSettingSyncDataList& changes() { return changes_; }
88
89 void ClearChanges() {
90 changes_.clear();
91 }
92
93 // Returns the only change for a given extension setting. If there is not
94 // exactly 1 change for that key, a test assertion will fail.
95 ExtensionSettingSyncData GetOnlyChange(
96 const std::string& extension_id, const std::string& key) {
97 ExtensionSettingSyncDataList matching_changes;
98 for (ExtensionSettingSyncDataList::iterator it = changes_.begin();
99 it != changes_.end(); ++it) {
100 if (it->extension_id() == extension_id && it->key() == key) {
101 matching_changes.push_back(*it);
102 }
103 }
104 if (matching_changes.empty()) {
105 ADD_FAILURE() << "No matching changes for " << extension_id << "/" <<
106 key << " (out of " << changes_.size() << ")";
107 return ExtensionSettingSyncData(SyncChange::ACTION_INVALID, "", "", NULL);
108 }
109 if (matching_changes.size() != 1u) {
110 ADD_FAILURE() << matching_changes.size() << " matching changes for " <<
111 extension_id << "/" << key << " (out of " << changes_.size() << ")";
112 }
113 return matching_changes[0];
114 }
115
116 private:
117 ExtensionSettingSyncDataList changes_;
118 };
119
120 } // namespace
121
122 class ExtensionSettingsSyncTest : public testing::Test {
123 public:
124 ExtensionSettingsSyncTest()
125 : ui_thread_(BrowserThread::UI, MessageLoop::current()),
126 file_thread_(BrowserThread::FILE, MessageLoop::current()) {}
127
128 virtual void SetUp() OVERRIDE {
129 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
130 settings_.reset(new ExtensionSettings(temp_dir_.path()));
131 }
132
133 virtual void TearDown() OVERRIDE {
134 // Must do this explicitly here so that it's destroyed before the
135 // message loops are.
136 settings_.reset();
137 }
138
139 protected:
140 // Creates a new extension storage object and adds a record of the extension
141 // to the extension service.
142 SyncableExtensionSettingsStorage* GetStorage(
143 const std::string& extension_id) {
144 return static_cast<SyncableExtensionSettingsStorage*>(
145 settings_->GetStorage(extension_id));
146 }
147
148 MockSyncChangeProcessor sync_;
149 scoped_ptr<ExtensionSettings> settings_;
150
151 // Gets all the sync data from settings_ as a map from extension id to its
152 // sync data.
153 std::map<std::string, ExtensionSettingSyncDataList> GetAllSyncData() {
154 SyncDataList as_list =
155 settings_->GetAllSyncData(syncable::EXTENSION_SETTINGS);
156 std::map<std::string, ExtensionSettingSyncDataList> as_map;
157 for (SyncDataList::iterator it = as_list.begin();
158 it != as_list.end(); ++it) {
159 ExtensionSettingSyncData sync_data(*it);
160 as_map[sync_data.extension_id()].push_back(sync_data);
161 }
162 return as_map;
163 }
164
165 private:
166 ScopedTempDir temp_dir_;
167
168 // Need these so that the DCHECKs for running on FILE or UI threads pass.
169 MessageLoopForUI ui_message_loop_;
akalin 2011/09/21 05:41:53 i think this can just be MessageLoop message_loop_
not at google - send to devlin 2011/09/21 06:22:41 Done.
170 BrowserThread ui_thread_;
171 BrowserThread file_thread_;
172 };
173
174 TEST_F(ExtensionSettingsSyncTest, NoDataDoesNotInvokeSync) {
175 ASSERT_EQ(0u, GetAllSyncData().size());
176
177 // Have one extension created before sync is set up, the other created after.
178 GetStorage("s1");
179 ASSERT_EQ(0u, GetAllSyncData().size());
180
181 settings_->MergeDataAndStartSyncing(
182 syncable::EXTENSION_SETTINGS,
183 SyncDataList(),
184 &sync_);
185
186 GetStorage("s2");
187 ASSERT_EQ(0u, GetAllSyncData().size());
188
189 settings_->StopSyncing(syncable::EXTENSION_SETTINGS);
190
191 ASSERT_EQ(0u, sync_.changes().size());
192 ASSERT_EQ(0u, GetAllSyncData().size());
193 }
194
195 TEST_F(ExtensionSettingsSyncTest, InSyncDataDoesNotInvokeSync) {
196 StringValue value1("fooValue");
197 ListValue value2;
198 value2.Append(StringValue::CreateStringValue("barValue"));
199
200 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1");
201 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2");
202
203 storage1->Set("foo", value1);
204 storage2->Set("bar", value2);
205
206 std::map<std::string, ExtensionSettingSyncDataList> all_sync_data =
207 GetAllSyncData();
208 ASSERT_EQ(2u, all_sync_data.size());
209 ASSERT_EQ(1u, all_sync_data["s1"].size());
210 ASSERT_PRED_FORMAT2(ValuesEq, &value1, &all_sync_data["s1"][0].value());
211 ASSERT_EQ(1u, all_sync_data["s2"].size());
212 ASSERT_PRED_FORMAT2(ValuesEq, &value2, &all_sync_data["s2"][0].value());
213
214 SyncDataList sync_data;
215 sync_data.push_back(extension_settings_sync_util::CreateData(
216 "s1", "foo", value1));
217 sync_data.push_back(extension_settings_sync_util::CreateData(
218 "s2", "bar", value2));
219
220 settings_->MergeDataAndStartSyncing(
221 syncable::EXTENSION_SETTINGS, sync_data, &sync_);
222 settings_->StopSyncing(syncable::EXTENSION_SETTINGS);
223
224 // Already in sync, so no changes.
225 ASSERT_EQ(0u, sync_.changes().size());
226 }
227
228 TEST_F(ExtensionSettingsSyncTest, LocalDataWithNoSyncDataIsPushedToSync) {
229 StringValue value1("fooValue");
230 ListValue value2;
231 value2.Append(StringValue::CreateStringValue("barValue"));
232
233 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1");
234 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2");
235
236 storage1->Set("foo", value1);
237 storage2->Set("bar", value2);
238
239 settings_->MergeDataAndStartSyncing(
240 syncable::EXTENSION_SETTINGS, SyncDataList(), &sync_);
241
242 // All settings should have been pushed to sync.
243 ASSERT_EQ(2u, sync_.changes().size());
244 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo");
245 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
246 ASSERT_TRUE(value1.Equals(&change.value()));
247 change = sync_.GetOnlyChange("s2", "bar");
248 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
249 ASSERT_TRUE(value2.Equals(&change.value()));
250
251 settings_->StopSyncing(syncable::EXTENSION_SETTINGS);
252 }
253
254 TEST_F(ExtensionSettingsSyncTest, AnySyncDataOverwritesLocalData) {
255 StringValue value1("fooValue");
256 ListValue value2;
257 value2.Append(StringValue::CreateStringValue("barValue"));
258
259 // Maintain dictionaries mirrored to the expected values of the settings in
260 // each storage area.
261 DictionaryValue expected1, expected2;
262
263 // Pre-populate one of the storage areas.
264 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1");
265 storage1->Set("overwriteMe", value1);
266
267 SyncDataList sync_data;
268 sync_data.push_back(extension_settings_sync_util::CreateData(
269 "s1", "foo", value1));
270 sync_data.push_back(extension_settings_sync_util::CreateData(
271 "s2", "bar", value2));
272 settings_->MergeDataAndStartSyncing(
273 syncable::EXTENSION_SETTINGS, sync_data, &sync_);
274 expected1.Set("foo", value1.DeepCopy());
275 expected2.Set("bar", value2.DeepCopy());
276
277 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2");
278
279 // All changes should be local, so no sync changes.
280 ASSERT_EQ(0u, sync_.changes().size());
281
282 // Sync settings should have been pushed to local settings.
283 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
284 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());
285
286 settings_->StopSyncing(syncable::EXTENSION_SETTINGS);
287 }
288
289 TEST_F(ExtensionSettingsSyncTest, ProcessSyncChanges) {
290 StringValue value1("fooValue");
291 ListValue value2;
292 value2.Append(StringValue::CreateStringValue("barValue"));
293
294 // Maintain dictionaries mirrored to the expected values of the settings in
295 // each storage area.
296 DictionaryValue expected1, expected2;
297
298 // Make storage1 initialised from local data, storage2 initialised from sync.
299 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1");
300 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2");
301
302 storage1->Set("foo", value1);
303 expected1.Set("foo", value1.DeepCopy());
304
305 SyncDataList sync_data;
306 sync_data.push_back(extension_settings_sync_util::CreateData(
307 "s2", "bar", value2));
308
309 settings_->MergeDataAndStartSyncing(
310 syncable::EXTENSION_SETTINGS, sync_data, &sync_);
311 expected2.Set("bar", value2.DeepCopy());
312
313 // Make sync add some settings.
314 SyncChangeList change_list;
315 change_list.push_back(extension_settings_sync_util::CreateAdd(
316 "s1", "bar", value2));
317 change_list.push_back(extension_settings_sync_util::CreateAdd(
318 "s2", "foo", value1));
319 settings_->ProcessSyncChanges(FROM_HERE, change_list);
320 expected1.Set("bar", value2.DeepCopy());
321 expected2.Set("foo", value1.DeepCopy());
322
323 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
324 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());
325
326 // Make sync update some settings, storage1 the new setting, storage2 the
327 // initial setting.
328 change_list.clear();
329 change_list.push_back(extension_settings_sync_util::CreateUpdate(
330 "s1", "bar", value2));
331 change_list.push_back(extension_settings_sync_util::CreateUpdate(
332 "s2", "bar", value1));
333 settings_->ProcessSyncChanges(FROM_HERE, change_list);
334 expected1.Set("bar", value2.DeepCopy());
335 expected2.Set("bar", value1.DeepCopy());
336
337 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
338 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());
339
340 // Make sync remove some settings, storage1 the initial setting, storage2 the
341 // new setting.
342 change_list.clear();
343 change_list.push_back(extension_settings_sync_util::CreateDelete(
344 "s1", "foo"));
345 change_list.push_back(extension_settings_sync_util::CreateDelete(
346 "s2", "foo"));
347 settings_->ProcessSyncChanges(FROM_HERE, change_list);
348 expected1.Remove("foo", NULL);
349 expected2.Remove("foo", NULL);
350
351 ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
352 ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());
353
354 settings_->StopSyncing(syncable::EXTENSION_SETTINGS);
355 }
356
357 TEST_F(ExtensionSettingsSyncTest, PushToSync) {
358 StringValue value1("fooValue");
359 ListValue value2;
360 value2.Append(StringValue::CreateStringValue("barValue"));
361
362 // Make storage1/2 initialised from local data, storage3/4 initialised from
363 // sync.
364 SyncableExtensionSettingsStorage* storage1 = GetStorage("s1");
365 SyncableExtensionSettingsStorage* storage2 = GetStorage("s2");
366 SyncableExtensionSettingsStorage* storage3 = GetStorage("s3");
367 SyncableExtensionSettingsStorage* storage4 = GetStorage("s4");
368
369 storage1->Set("foo", value1);
370 storage2->Set("foo", value1);
371
372 SyncDataList sync_data;
373 sync_data.push_back(extension_settings_sync_util::CreateData(
374 "s3", "bar", value2));
375 sync_data.push_back(extension_settings_sync_util::CreateData(
376 "s4", "bar", value2));
377
378 settings_->MergeDataAndStartSyncing(
379 syncable::EXTENSION_SETTINGS, sync_data, &sync_);
380
381 // Add something locally.
382 storage1->Set("bar", value2);
383 storage2->Set("bar", value2);
384 storage3->Set("foo", value1);
385 storage4->Set("foo", value1);
386
387 ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "bar");
388 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
389 ASSERT_TRUE(value2.Equals(&change.value()));
390 sync_.GetOnlyChange("s2", "bar");
391 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
392 ASSERT_TRUE(value2.Equals(&change.value()));
393 change = sync_.GetOnlyChange("s3", "foo");
394 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
395 ASSERT_TRUE(value1.Equals(&change.value()));
396 change = sync_.GetOnlyChange("s4", "foo");
397 ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
398 ASSERT_TRUE(value1.Equals(&change.value()));
399
400 // Change something locally, storage1/3 the new setting and storage2/4 the
401 // initial setting, for all combinations of local vs sync intialisation and
402 // new vs initial.
403 sync_.ClearChanges();
404 storage1->Set("bar", value1);
405 storage2->Set("foo", value2);
406 storage3->Set("bar", value1);
407 storage4->Set("foo", value2);
408
409 change = sync_.GetOnlyChange("s1", "bar");
410 ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
411 ASSERT_TRUE(value1.Equals(&change.value()));
412 change = sync_.GetOnlyChange("s2", "foo");
413 ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
414 ASSERT_TRUE(value2.Equals(&change.value()));
415 change = sync_.GetOnlyChange("s3", "bar");
416 ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
417 ASSERT_TRUE(value1.Equals(&change.value()));
418 change = sync_.GetOnlyChange("s4", "foo");
419 ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
420 ASSERT_TRUE(value2.Equals(&change.value()));
421
422 // Remove something locally, storage1/3 the new setting and storage2/4 the
423 // initial setting, for all combinations of local vs sync intialisation and
424 // new vs initial.
425 sync_.ClearChanges();
426 storage1->Remove("foo");
427 storage2->Remove("bar");
428 storage3->Remove("foo");
429 storage4->Remove("bar");
430
431 ASSERT_EQ(
432 SyncChange::ACTION_DELETE,
433 sync_.GetOnlyChange("s1", "foo").change_type());
434 ASSERT_EQ(
435 SyncChange::ACTION_DELETE,
436 sync_.GetOnlyChange("s2", "bar").change_type());
437 ASSERT_EQ(
438 SyncChange::ACTION_DELETE,
439 sync_.GetOnlyChange("s3", "foo").change_type());
440 ASSERT_EQ(
441 SyncChange::ACTION_DELETE,
442 sync_.GetOnlyChange("s4", "bar").change_type());
443
444 // Remove some nonexistent settings.
445 sync_.ClearChanges();
446 storage1->Remove("foo");
447 storage2->Remove("bar");
448 storage3->Remove("foo");
449 storage4->Remove("bar");
450
451 ASSERT_EQ(0u, sync_.changes().size());
452
453 // Clear the rest of the settings. Add the removed ones back first so that
454 // more than one setting is cleared.
455 storage1->Set("foo", value1);
456 storage2->Set("bar", value2);
457 storage3->Set("foo", value1);
458 storage4->Set("bar", value2);
459
460 sync_.ClearChanges();
461 storage1->Clear();
462 storage2->Clear();
463 storage3->Clear();
464 storage4->Clear();
465
466 ASSERT_EQ(
467 SyncChange::ACTION_DELETE,
468 sync_.GetOnlyChange("s1", "foo").change_type());
469 ASSERT_EQ(
470 SyncChange::ACTION_DELETE,
471 sync_.GetOnlyChange("s1", "bar").change_type());
472 ASSERT_EQ(
473 SyncChange::ACTION_DELETE,
474 sync_.GetOnlyChange("s2", "foo").change_type());
475 ASSERT_EQ(
476 SyncChange::ACTION_DELETE,
477 sync_.GetOnlyChange("s2", "bar").change_type());
478 ASSERT_EQ(
479 SyncChange::ACTION_DELETE,
480 sync_.GetOnlyChange("s3", "foo").change_type());
481 ASSERT_EQ(
482 SyncChange::ACTION_DELETE,
483 sync_.GetOnlyChange("s3", "bar").change_type());
484 ASSERT_EQ(
485 SyncChange::ACTION_DELETE,
486 sync_.GetOnlyChange("s4", "foo").change_type());
487 ASSERT_EQ(
488 SyncChange::ACTION_DELETE,
489 sync_.GetOnlyChange("s4", "bar").change_type());
490
491 settings_->StopSyncing(syncable::EXTENSION_SETTINGS);
492 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698