summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_settings_sync_unittest.cc
blob: 07418de858434941837a55725d6e28d35556cf4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"

#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/scoped_temp_dir.h"
#include "base/task.h"
#include "chrome/browser/extensions/extension_settings_frontend.h"
#include "chrome/browser/extensions/extension_settings_storage_cache.h"
#include "chrome/browser/extensions/extension_settings_sync_util.h"
#include "chrome/browser/extensions/syncable_extension_settings_storage.h"
#include "chrome/browser/extensions/extension_settings_test_util.h"
#include "chrome/browser/sync/api/sync_change_processor.h"
#include "content/test/test_browser_thread.h"

// TODO(kalman): Integration tests for sync.

using namespace extension_settings_test_util;

namespace {

// Gets the pretty-printed JSON for a value.
static std::string GetJson(const Value& value) {
  std::string json;
  base::JSONWriter::Write(&value, true, &json);
  return json;
}

// Returns whether two Values are equal.
testing::AssertionResult ValuesEq(
    const char* expected_expr,
    const char* actual_expr,
    const Value* expected,
    const Value* actual) {
  if (expected == actual) {
    return testing::AssertionSuccess();
  }
  if (!expected && actual) {
    return testing::AssertionFailure() <<
        "Expected NULL, actual: " << GetJson(*actual);
  }
  if (expected && !actual) {
    return testing::AssertionFailure() <<
        "Expected: " << GetJson(*expected) << ", actual NULL";
  }
  if (!expected->Equals(actual)) {
    return testing::AssertionFailure() <<
        "Expected: " << GetJson(*expected) << ", actual: " << GetJson(*actual);
  }
  return testing::AssertionSuccess();
}

// Returns whether the result of a storage operation is an expected value.
// Logs when different.
testing::AssertionResult SettingsEq(
    const char* expected_expr,
    const char* actual_expr,
    const DictionaryValue* expected,
    const ExtensionSettingsStorage::Result actual) {
  if (actual.HasError()) {
    return testing::AssertionFailure() <<
        "Expected: " << GetJson(*expected) <<
        ", actual has error: " << actual.GetError();
  }
  return ValuesEq(
      expected_expr, actual_expr, expected, actual.GetSettings());
}

// SyncChangeProcessor which just records the changes made, accessed after
// being converted to the more useful ExtensionSettingSyncData via changes().
class MockSyncChangeProcessor : public SyncChangeProcessor {
 public:
  virtual SyncError ProcessSyncChanges(
      const tracked_objects::Location& from_here,
      const SyncChangeList& change_list) OVERRIDE {
    for (SyncChangeList::const_iterator it = change_list.begin();
        it != change_list.end(); ++it) {
      changes_.push_back(ExtensionSettingSyncData(*it));
    }
    return SyncError();
  }

  const ExtensionSettingSyncDataList& changes() { return changes_; }

  void ClearChanges() {
    changes_.clear();
  }

  // Returns the only change for a given extension setting.  If there is not
  // exactly 1 change for that key, a test assertion will fail.
  ExtensionSettingSyncData GetOnlyChange(
      const std::string& extension_id, const std::string& key) {
    ExtensionSettingSyncDataList matching_changes;
    for (ExtensionSettingSyncDataList::iterator it = changes_.begin();
        it != changes_.end(); ++it) {
      if (it->extension_id() == extension_id && it->key() == key) {
        matching_changes.push_back(*it);
      }
    }
    if (matching_changes.empty()) {
      ADD_FAILURE() << "No matching changes for " << extension_id << "/" <<
          key << " (out of " << changes_.size() << ")";
      return ExtensionSettingSyncData(
          SyncChange::ACTION_INVALID, "", "", new DictionaryValue());
    }
    if (matching_changes.size() != 1u) {
      ADD_FAILURE() << matching_changes.size() << " matching changes for " <<
           extension_id << "/" << key << " (out of " << changes_.size() << ")";
    }
    return matching_changes[0];
  }

 private:
  ExtensionSettingSyncDataList changes_;
};

void AssignSettingsService(SyncableService** dst, SyncableService* src) {
  *dst = src;
}

}  // namespace

class ExtensionSettingsSyncTest : public testing::Test {
 public:
  ExtensionSettingsSyncTest()
      : ui_thread_(BrowserThread::UI, MessageLoop::current()),
        file_thread_(BrowserThread::FILE, MessageLoop::current()) {}

  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    profile_.reset(new MockProfile(temp_dir_.path()));
    frontend_.reset(new ExtensionSettingsFrontend(profile_.get()));
  }

  virtual void TearDown() OVERRIDE {
    frontend_.reset();
    profile_.reset();
  }

 protected:
  // Adds a record of an extension or app to the extension service, then returns
  // its storage area.
  ExtensionSettingsStorage* AddExtensionAndGetStorage(
      const std::string& id, Extension::Type type) {
    profile_->GetMockExtensionService()->AddExtension(id, type);
    return GetStorage(id, frontend_.get());
  }

  // Gets the SyncableService for the given sync type.
  SyncableService* GetSyncableService(syncable::ModelType model_type) {
    SyncableService* settings_service = NULL;
    frontend_->RunWithSyncableService(
        model_type, base::Bind(&AssignSettingsService, &settings_service));
    MessageLoop::current()->RunAllPending();
    return settings_service;
  }

  // Gets all the sync data from the SyncableService for a sync type as a map
  // from extension id to its sync data.
  std::map<std::string, ExtensionSettingSyncDataList> GetAllSyncData(
      syncable::ModelType model_type) {
    SyncDataList as_list =
        GetSyncableService(model_type)->GetAllSyncData(model_type);
    std::map<std::string, ExtensionSettingSyncDataList> as_map;
    for (SyncDataList::iterator it = as_list.begin();
        it != as_list.end(); ++it) {
      ExtensionSettingSyncData sync_data(*it);
      as_map[sync_data.extension_id()].push_back(sync_data);
    }
    return as_map;
  }

  // Need these so that the DCHECKs for running on FILE or UI threads pass.
  MessageLoop message_loop_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread file_thread_;

  ScopedTempDir temp_dir_;
  MockSyncChangeProcessor sync_;
  scoped_ptr<MockProfile> profile_;
  scoped_ptr<ExtensionSettingsFrontend> frontend_;
};

// Get a semblance of coverage for both EXTENSION_SETTINGS and APP_SETTINGS
// sync by roughly alternative which one to test.

TEST_F(ExtensionSettingsSyncTest, NoDataDoesNotInvokeSync) {
  syncable::ModelType model_type = syncable::EXTENSION_SETTINGS;
  Extension::Type type = Extension::TYPE_EXTENSION;

  ASSERT_EQ(0u, GetAllSyncData(model_type).size());

  // Have one extension created before sync is set up, the other created after.
  AddExtensionAndGetStorage("s1", type);
  ASSERT_EQ(0u, GetAllSyncData(model_type).size());

  GetSyncableService(model_type)->MergeDataAndStartSyncing(
      model_type, SyncDataList(), &sync_);

  AddExtensionAndGetStorage("s2", type);
  ASSERT_EQ(0u, GetAllSyncData(model_type).size());

  GetSyncableService(model_type)->StopSyncing(model_type);

  ASSERT_EQ(0u, sync_.changes().size());
  ASSERT_EQ(0u, GetAllSyncData(model_type).size());
}

TEST_F(ExtensionSettingsSyncTest, InSyncDataDoesNotInvokeSync) {
  syncable::ModelType model_type = syncable::APP_SETTINGS;
  Extension::Type type = Extension::TYPE_PACKAGED_APP;

  StringValue value1("fooValue");
  ListValue value2;
  value2.Append(StringValue::CreateStringValue("barValue"));

  ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type);
  ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type);

  storage1->Set("foo", value1);
  storage2->Set("bar", value2);

  std::map<std::string, ExtensionSettingSyncDataList> all_sync_data =
      GetAllSyncData(model_type);
  ASSERT_EQ(2u, all_sync_data.size());
  ASSERT_EQ(1u, all_sync_data["s1"].size());
  ASSERT_PRED_FORMAT2(ValuesEq, &value1, &all_sync_data["s1"][0].value());
  ASSERT_EQ(1u, all_sync_data["s2"].size());
  ASSERT_PRED_FORMAT2(ValuesEq, &value2, &all_sync_data["s2"][0].value());

  SyncDataList sync_data;
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s1", "foo", value1));
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s2", "bar", value2));

  GetSyncableService(model_type)->MergeDataAndStartSyncing(
      model_type, sync_data, &sync_);

  // Already in sync, so no changes.
  ASSERT_EQ(0u, sync_.changes().size());

  // Regression test: not-changing the synced value shouldn't result in a sync
  // change, and changing the synced value should result in an update.
  storage1->Set("foo", value1);
  ASSERT_EQ(0u, sync_.changes().size());

  storage1->Set("foo", value2);
  ASSERT_EQ(1u, sync_.changes().size());
  ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo");
  ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
  ASSERT_TRUE(value2.Equals(&change.value()));

  GetSyncableService(model_type)->StopSyncing(model_type);
}

TEST_F(ExtensionSettingsSyncTest, LocalDataWithNoSyncDataIsPushedToSync) {
  syncable::ModelType model_type = syncable::EXTENSION_SETTINGS;
  Extension::Type type = Extension::TYPE_EXTENSION;

  StringValue value1("fooValue");
  ListValue value2;
  value2.Append(StringValue::CreateStringValue("barValue"));

  ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type);
  ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type);

  storage1->Set("foo", value1);
  storage2->Set("bar", value2);

  GetSyncableService(model_type)->MergeDataAndStartSyncing(
      model_type, SyncDataList(), &sync_);

  // All settings should have been pushed to sync.
  ASSERT_EQ(2u, sync_.changes().size());
  ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "foo");
  ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
  ASSERT_TRUE(value1.Equals(&change.value()));
  change = sync_.GetOnlyChange("s2", "bar");
  ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
  ASSERT_TRUE(value2.Equals(&change.value()));

  GetSyncableService(model_type)->StopSyncing(model_type);
}

TEST_F(ExtensionSettingsSyncTest, AnySyncDataOverwritesLocalData) {
  syncable::ModelType model_type = syncable::APP_SETTINGS;
  Extension::Type type = Extension::TYPE_PACKAGED_APP;

  StringValue value1("fooValue");
  ListValue value2;
  value2.Append(StringValue::CreateStringValue("barValue"));

  // Maintain dictionaries mirrored to the expected values of the settings in
  // each storage area.
  DictionaryValue expected1, expected2;

  // Pre-populate one of the storage areas.
  ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type);
  storage1->Set("overwriteMe", value1);

  SyncDataList sync_data;
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s1", "foo", value1));
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s2", "bar", value2));
  GetSyncableService(model_type)->MergeDataAndStartSyncing(
      model_type, sync_data, &sync_);
  expected1.Set("foo", value1.DeepCopy());
  expected2.Set("bar", value2.DeepCopy());

  ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type);

  // All changes should be local, so no sync changes.
  ASSERT_EQ(0u, sync_.changes().size());

  // Sync settings should have been pushed to local settings.
  ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
  ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());

  GetSyncableService(model_type)->StopSyncing(model_type);
}

TEST_F(ExtensionSettingsSyncTest, ProcessSyncChanges) {
  syncable::ModelType model_type = syncable::EXTENSION_SETTINGS;
  Extension::Type type = Extension::TYPE_EXTENSION;

  StringValue value1("fooValue");
  ListValue value2;
  value2.Append(StringValue::CreateStringValue("barValue"));

  // Maintain dictionaries mirrored to the expected values of the settings in
  // each storage area.
  DictionaryValue expected1, expected2;

  // Make storage1 initialised from local data, storage2 initialised from sync.
  ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type);
  ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type);

  storage1->Set("foo", value1);
  expected1.Set("foo", value1.DeepCopy());

  SyncDataList sync_data;
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s2", "bar", value2));

  GetSyncableService(model_type)->MergeDataAndStartSyncing(
      model_type, sync_data, &sync_);
  expected2.Set("bar", value2.DeepCopy());

  // Make sync add some settings.
  SyncChangeList change_list;
  change_list.push_back(extension_settings_sync_util::CreateAdd(
      "s1", "bar", value2));
  change_list.push_back(extension_settings_sync_util::CreateAdd(
      "s2", "foo", value1));
  GetSyncableService(model_type)->ProcessSyncChanges(FROM_HERE, change_list);
  expected1.Set("bar", value2.DeepCopy());
  expected2.Set("foo", value1.DeepCopy());

  ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
  ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());

  // Make sync update some settings, storage1 the new setting, storage2 the
  // initial setting.
  change_list.clear();
  change_list.push_back(extension_settings_sync_util::CreateUpdate(
      "s1", "bar", value2));
  change_list.push_back(extension_settings_sync_util::CreateUpdate(
      "s2", "bar", value1));
  GetSyncableService(model_type)->ProcessSyncChanges(FROM_HERE, change_list);
  expected1.Set("bar", value2.DeepCopy());
  expected2.Set("bar", value1.DeepCopy());

  ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
  ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());

  // Make sync remove some settings, storage1 the initial setting, storage2 the
  // new setting.
  change_list.clear();
  change_list.push_back(extension_settings_sync_util::CreateDelete(
      "s1", "foo"));
  change_list.push_back(extension_settings_sync_util::CreateDelete(
      "s2", "foo"));
  GetSyncableService(model_type)->ProcessSyncChanges(FROM_HERE, change_list);
  expected1.Remove("foo", NULL);
  expected2.Remove("foo", NULL);

  ASSERT_PRED_FORMAT2(SettingsEq, &expected1, storage1->Get());
  ASSERT_PRED_FORMAT2(SettingsEq, &expected2, storage2->Get());

  GetSyncableService(model_type)->StopSyncing(model_type);
}

TEST_F(ExtensionSettingsSyncTest, PushToSync) {
  syncable::ModelType model_type = syncable::APP_SETTINGS;
  Extension::Type type = Extension::TYPE_PACKAGED_APP;

  StringValue value1("fooValue");
  ListValue value2;
  value2.Append(StringValue::CreateStringValue("barValue"));

  // Make storage1/2 initialised from local data, storage3/4 initialised from
  // sync.
  ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage("s1", type);
  ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage("s2", type);
  ExtensionSettingsStorage* storage3 = AddExtensionAndGetStorage("s3", type);
  ExtensionSettingsStorage* storage4 = AddExtensionAndGetStorage("s4", type);

  storage1->Set("foo", value1);
  storage2->Set("foo", value1);

  SyncDataList sync_data;
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s3", "bar", value2));
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s4", "bar", value2));

  GetSyncableService(model_type)->MergeDataAndStartSyncing(
      model_type, sync_data, &sync_);

  // Add something locally.
  storage1->Set("bar", value2);
  storage2->Set("bar", value2);
  storage3->Set("foo", value1);
  storage4->Set("foo", value1);

  ExtensionSettingSyncData change = sync_.GetOnlyChange("s1", "bar");
    ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
    ASSERT_TRUE(value2.Equals(&change.value()));
  sync_.GetOnlyChange("s2", "bar");
    ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
    ASSERT_TRUE(value2.Equals(&change.value()));
  change = sync_.GetOnlyChange("s3", "foo");
    ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
    ASSERT_TRUE(value1.Equals(&change.value()));
  change = sync_.GetOnlyChange("s4", "foo");
    ASSERT_EQ(SyncChange::ACTION_ADD, change.change_type());
    ASSERT_TRUE(value1.Equals(&change.value()));

  // Change something locally, storage1/3 the new setting and storage2/4 the
  // initial setting, for all combinations of local vs sync intialisation and
  // new vs initial.
  sync_.ClearChanges();
  storage1->Set("bar", value1);
  storage2->Set("foo", value2);
  storage3->Set("bar", value1);
  storage4->Set("foo", value2);

  change = sync_.GetOnlyChange("s1", "bar");
    ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
    ASSERT_TRUE(value1.Equals(&change.value()));
  change = sync_.GetOnlyChange("s2", "foo");
    ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
    ASSERT_TRUE(value2.Equals(&change.value()));
  change = sync_.GetOnlyChange("s3", "bar");
    ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
    ASSERT_TRUE(value1.Equals(&change.value()));
  change = sync_.GetOnlyChange("s4", "foo");
    ASSERT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
    ASSERT_TRUE(value2.Equals(&change.value()));

  // Remove something locally, storage1/3 the new setting and storage2/4 the
  // initial setting, for all combinations of local vs sync intialisation and
  // new vs initial.
  sync_.ClearChanges();
  storage1->Remove("foo");
  storage2->Remove("bar");
  storage3->Remove("foo");
  storage4->Remove("bar");

  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s1", "foo").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s2", "bar").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s3", "foo").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s4", "bar").change_type());

  // Remove some nonexistent settings.
  sync_.ClearChanges();
  storage1->Remove("foo");
  storage2->Remove("bar");
  storage3->Remove("foo");
  storage4->Remove("bar");

  ASSERT_EQ(0u, sync_.changes().size());

  // Clear the rest of the settings.  Add the removed ones back first so that
  // more than one setting is cleared.
  storage1->Set("foo", value1);
  storage2->Set("bar", value2);
  storage3->Set("foo", value1);
  storage4->Set("bar", value2);

  sync_.ClearChanges();
  storage1->Clear();
  storage2->Clear();
  storage3->Clear();
  storage4->Clear();

  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s1", "foo").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s1", "bar").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s2", "foo").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s2", "bar").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s3", "foo").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s3", "bar").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s4", "foo").change_type());
  ASSERT_EQ(
      SyncChange::ACTION_DELETE,
      sync_.GetOnlyChange("s4", "bar").change_type());

  GetSyncableService(model_type)->StopSyncing(model_type);
}

TEST_F(ExtensionSettingsSyncTest, ExtensionAndAppSettingsSyncSeparately) {
  StringValue value1("fooValue");
  ListValue value2;
  value2.Append(StringValue::CreateStringValue("barValue"));

  // storage1 is an extension, storage2 is an app.
  ExtensionSettingsStorage* storage1 = AddExtensionAndGetStorage(
      "s1", Extension::TYPE_EXTENSION);
  ExtensionSettingsStorage* storage2 = AddExtensionAndGetStorage(
      "s2", Extension::TYPE_PACKAGED_APP);

  storage1->Set("foo", value1);
  storage2->Set("bar", value2);

  std::map<std::string, ExtensionSettingSyncDataList> extension_sync_data =
      GetAllSyncData(syncable::EXTENSION_SETTINGS);
  ASSERT_EQ(1u, extension_sync_data.size());
  ASSERT_EQ(1u, extension_sync_data["s1"].size());
  ASSERT_PRED_FORMAT2(ValuesEq, &value1, &extension_sync_data["s1"][0].value());

  std::map<std::string, ExtensionSettingSyncDataList> app_sync_data =
      GetAllSyncData(syncable::APP_SETTINGS);
  ASSERT_EQ(1u, app_sync_data.size());
  ASSERT_EQ(1u, app_sync_data["s2"].size());
  ASSERT_PRED_FORMAT2(ValuesEq, &value2, &app_sync_data["s2"][0].value());

  // Stop each separately, there should be no changes either time.
  SyncDataList sync_data;
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s1", "foo", value1));

  GetSyncableService(syncable::EXTENSION_SETTINGS)->
      MergeDataAndStartSyncing(syncable::EXTENSION_SETTINGS, sync_data, &sync_);
  GetSyncableService(syncable::EXTENSION_SETTINGS)->
      StopSyncing(syncable::EXTENSION_SETTINGS);
  ASSERT_EQ(0u, sync_.changes().size());

  sync_data.clear();
  sync_data.push_back(extension_settings_sync_util::CreateData(
      "s2", "bar", value2));

  GetSyncableService(syncable::APP_SETTINGS)->
      MergeDataAndStartSyncing(syncable::APP_SETTINGS, sync_data, &sync_);
  GetSyncableService(syncable::APP_SETTINGS)->
      StopSyncing(syncable::APP_SETTINGS);
  ASSERT_EQ(0u, sync_.changes().size());
}