Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / prefs / prefs_syncable_service_unittest.cc
blobaf8a057f06545b82d749e57ecf93f89488ba637a
1 // Copyright 2014 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.
5 #include "base/json/json_reader.h"
6 #include "base/json/json_string_value_serializer.h"
7 #include "base/json/json_writer.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/prefs/pref_model_associator.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/locale_settings.h"
15 #include "chrome/test/base/testing_pref_service_syncable.h"
16 #include "components/pref_registry/pref_registry_syncable.h"
17 #include "sync/api/attachments/attachment_id.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_data.h"
20 #include "sync/api/sync_error_factory_mock.h"
21 #include "sync/api/syncable_service.h"
22 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
23 #include "sync/protocol/preference_specifics.pb.h"
24 #include "sync/protocol/sync.pb.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/base/l10n/l10n_util.h"
28 using syncer::SyncChange;
29 using syncer::SyncData;
31 namespace {
32 const char kExampleUrl0[] = "http://example.com/0";
33 const char kExampleUrl1[] = "http://example.com/1";
34 const char kExampleUrl2[] = "http://example.com/2";
35 const char kUnsyncedPreferenceName[] = "nonsense_pref_name";
36 const char kUnsyncedPreferenceDefaultValue[] = "default";
37 const char kNonDefaultCharsetValue[] = "foo";
38 } // namespace
40 class TestSyncProcessorStub : public syncer::SyncChangeProcessor {
41 public:
42 explicit TestSyncProcessorStub(syncer::SyncChangeList* output)
43 : output_(output), fail_next_(false) {}
44 syncer::SyncError ProcessSyncChanges(
45 const tracked_objects::Location& from_here,
46 const syncer::SyncChangeList& change_list) override {
47 if (output_)
48 output_->insert(output_->end(), change_list.begin(), change_list.end());
49 if (fail_next_) {
50 fail_next_ = false;
51 return syncer::SyncError(
52 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, "Error",
53 syncer::PREFERENCES);
55 return syncer::SyncError();
58 void FailNextProcessSyncChanges() {
59 fail_next_ = true;
62 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override {
63 return syncer::SyncDataList();
65 private:
66 syncer::SyncChangeList* output_;
67 bool fail_next_;
70 class PrefsSyncableServiceTest : public testing::Test {
71 public:
72 PrefsSyncableServiceTest() :
73 pref_sync_service_(NULL),
74 test_processor_(NULL),
75 next_pref_remote_sync_node_id_(0) {}
77 void SetUp() override {
78 prefs_.registry()->RegisterStringPref(kUnsyncedPreferenceName,
79 kUnsyncedPreferenceDefaultValue);
80 prefs_.registry()->RegisterStringPref(
81 prefs::kHomePage,
82 std::string(),
83 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
84 prefs_.registry()->RegisterListPref(
85 prefs::kURLsToRestoreOnStartup,
86 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
87 prefs_.registry()->RegisterListPref(prefs::kURLsToRestoreOnStartupOld);
88 prefs_.registry()->RegisterStringPref(
89 prefs::kDefaultCharset,
90 l10n_util::GetStringUTF8(IDS_DEFAULT_ENCODING),
91 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
93 pref_sync_service_ = reinterpret_cast<PrefModelAssociator*>(
94 prefs_.GetSyncableService(syncer::PREFERENCES));
95 ASSERT_TRUE(pref_sync_service_);
96 next_pref_remote_sync_node_id_ = 0;
99 syncer::SyncChange MakeRemoteChange(
100 int64 id,
101 const std::string& name,
102 const base::Value& value,
103 SyncChange::SyncChangeType type) {
104 std::string serialized;
105 JSONStringValueSerializer json(&serialized);
106 if (!json.Serialize(value))
107 return syncer::SyncChange();
108 sync_pb::EntitySpecifics entity;
109 sync_pb::PreferenceSpecifics* pref_one = entity.mutable_preference();
110 pref_one->set_name(name);
111 pref_one->set_value(serialized);
112 return syncer::SyncChange(
113 FROM_HERE,
114 type,
115 syncer::SyncData::CreateRemoteData(
117 entity,
118 base::Time(),
119 syncer::AttachmentIdList(),
120 syncer::AttachmentServiceProxyForTest::Create()));
123 void AddToRemoteDataList(const std::string& name,
124 const base::Value& value,
125 syncer::SyncDataList* out) {
126 std::string serialized;
127 JSONStringValueSerializer json(&serialized);
128 ASSERT_TRUE(json.Serialize(value));
129 sync_pb::EntitySpecifics one;
130 sync_pb::PreferenceSpecifics* pref_one = one.mutable_preference();
131 pref_one->set_name(name);
132 pref_one->set_value(serialized);
133 out->push_back(SyncData::CreateRemoteData(
134 ++next_pref_remote_sync_node_id_,
135 one,
136 base::Time(),
137 syncer::AttachmentIdList(),
138 syncer::AttachmentServiceProxyForTest::Create()));
141 void InitWithSyncDataTakeOutput(const syncer::SyncDataList& initial_data,
142 syncer::SyncChangeList* output) {
143 test_processor_ = new TestSyncProcessorStub(output);
144 syncer::SyncMergeResult r = pref_sync_service_->MergeDataAndStartSyncing(
145 syncer::PREFERENCES, initial_data,
146 scoped_ptr<syncer::SyncChangeProcessor>(test_processor_),
147 scoped_ptr<syncer::SyncErrorFactory>(
148 new syncer::SyncErrorFactoryMock()));
149 EXPECT_FALSE(r.error().IsSet());
152 void InitWithNoSyncData() {
153 InitWithSyncDataTakeOutput(syncer::SyncDataList(), NULL);
156 const base::Value& GetPreferenceValue(const std::string& name) {
157 const PrefService::Preference* preference =
158 prefs_.FindPreference(name.c_str());
159 return *preference->GetValue();
162 scoped_ptr<base::Value> FindValue(const std::string& name,
163 const syncer::SyncChangeList& list) {
164 syncer::SyncChangeList::const_iterator it = list.begin();
165 for (; it != list.end(); ++it) {
166 if (syncer::SyncDataLocal(it->sync_data()).GetTag() == name) {
167 return make_scoped_ptr(base::JSONReader::DeprecatedRead(
168 it->sync_data().GetSpecifics().preference().value()));
171 return scoped_ptr<base::Value>();
174 bool IsSynced(const std::string& pref_name) {
175 return pref_sync_service_->registered_preferences().count(pref_name) > 0;
178 bool HasSyncData(const std::string& pref_name) {
179 return pref_sync_service_->IsPrefSynced(pref_name);
182 // Returns whether a given preference name is a new name of a migrated
183 // preference. Exposed here for testing.
184 static bool IsMigratedPreference(const char* preference_name) {
185 return PrefModelAssociator::IsMigratedPreference(preference_name);
187 static bool IsOldMigratedPreference(const char* old_preference_name) {
188 return PrefModelAssociator::IsOldMigratedPreference(old_preference_name);
191 PrefService* GetPrefs() { return &prefs_; }
192 TestingPrefServiceSyncable* GetTestingPrefService() { return &prefs_; }
194 protected:
195 TestingPrefServiceSyncable prefs_;
197 PrefModelAssociator* pref_sync_service_;
198 TestSyncProcessorStub* test_processor_;
200 // TODO(tim): Remove this by fixing AttachmentServiceProxyForTest.
201 base::MessageLoop loop_;
203 int next_pref_remote_sync_node_id_;
206 TEST_F(PrefsSyncableServiceTest, CreatePrefSyncData) {
207 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
209 const PrefService::Preference* pref =
210 prefs_.FindPreference(prefs::kHomePage);
211 syncer::SyncData sync_data;
212 EXPECT_TRUE(pref_sync_service_->CreatePrefSyncData(pref->name(),
213 *pref->GetValue(), &sync_data));
214 EXPECT_EQ(std::string(prefs::kHomePage),
215 syncer::SyncDataLocal(sync_data).GetTag());
216 const sync_pb::PreferenceSpecifics& specifics(sync_data.GetSpecifics().
217 preference());
218 EXPECT_EQ(std::string(prefs::kHomePage), specifics.name());
220 scoped_ptr<base::Value> value = base::JSONReader::Read(specifics.value());
221 EXPECT_TRUE(pref->GetValue()->Equals(value.get()));
224 TEST_F(PrefsSyncableServiceTest, ModelAssociationDoNotSyncDefaults) {
225 const PrefService::Preference* pref =
226 prefs_.FindPreference(prefs::kHomePage);
227 EXPECT_TRUE(pref->IsDefaultValue());
228 syncer::SyncChangeList out;
229 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
231 EXPECT_TRUE(IsSynced(prefs::kHomePage));
232 EXPECT_TRUE(pref->IsDefaultValue());
233 EXPECT_FALSE(FindValue(prefs::kHomePage, out).get());
236 TEST_F(PrefsSyncableServiceTest, ModelAssociationEmptyCloud) {
237 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
239 ListPrefUpdate update(GetPrefs(), prefs::kURLsToRestoreOnStartup);
240 base::ListValue* url_list = update.Get();
241 url_list->Append(new base::StringValue(kExampleUrl0));
242 url_list->Append(new base::StringValue(kExampleUrl1));
244 syncer::SyncChangeList out;
245 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
247 scoped_ptr<base::Value> value(FindValue(prefs::kHomePage, out));
248 ASSERT_TRUE(value.get());
249 EXPECT_TRUE(GetPreferenceValue(prefs::kHomePage).Equals(value.get()));
250 value = FindValue(prefs::kURLsToRestoreOnStartup, out).Pass();
251 ASSERT_TRUE(value.get());
252 EXPECT_TRUE(
253 GetPreferenceValue(prefs::kURLsToRestoreOnStartup).Equals(value.get()));
256 TEST_F(PrefsSyncableServiceTest, ModelAssociationCloudHasData) {
257 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
259 ListPrefUpdate update(GetPrefs(), prefs::kURLsToRestoreOnStartup);
260 base::ListValue* url_list = update.Get();
261 url_list->Append(new base::StringValue(kExampleUrl0));
262 url_list->Append(new base::StringValue(kExampleUrl1));
265 syncer::SyncDataList in;
266 syncer::SyncChangeList out;
267 AddToRemoteDataList(prefs::kHomePage, base::StringValue(kExampleUrl1), &in);
268 base::ListValue urls_to_restore;
269 urls_to_restore.Append(new base::StringValue(kExampleUrl1));
270 urls_to_restore.Append(new base::StringValue(kExampleUrl2));
271 AddToRemoteDataList(prefs::kURLsToRestoreOnStartup, urls_to_restore, &in);
272 AddToRemoteDataList(prefs::kDefaultCharset,
273 base::StringValue(kNonDefaultCharsetValue),
274 &in);
275 InitWithSyncDataTakeOutput(in, &out);
277 ASSERT_FALSE(FindValue(prefs::kHomePage, out).get());
278 ASSERT_FALSE(FindValue(prefs::kDefaultCharset, out).get());
280 EXPECT_EQ(kExampleUrl1, prefs_.GetString(prefs::kHomePage));
282 scoped_ptr<base::ListValue> expected_urls(new base::ListValue);
283 expected_urls->Append(new base::StringValue(kExampleUrl1));
284 expected_urls->Append(new base::StringValue(kExampleUrl2));
285 expected_urls->Append(new base::StringValue(kExampleUrl0));
286 scoped_ptr<base::Value> value(
287 FindValue(prefs::kURLsToRestoreOnStartup, out));
288 ASSERT_TRUE(value.get());
289 EXPECT_TRUE(value->Equals(expected_urls.get()));
290 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartup).
291 Equals(expected_urls.get()));
292 EXPECT_EQ(kNonDefaultCharsetValue,
293 prefs_.GetString(prefs::kDefaultCharset));
296 TEST_F(PrefsSyncableServiceTest, ModelAssociationMigrateOldData) {
297 ASSERT_TRUE(IsMigratedPreference(prefs::kURLsToRestoreOnStartup));
298 ASSERT_TRUE(IsOldMigratedPreference(prefs::kURLsToRestoreOnStartupOld));
300 syncer::SyncDataList in;
301 syncer::SyncChangeList out;
302 base::ListValue urls_to_restore;
303 urls_to_restore.Append(new base::StringValue(kExampleUrl1));
304 urls_to_restore.Append(new base::StringValue(kExampleUrl2));
305 AddToRemoteDataList(prefs::kURLsToRestoreOnStartupOld, urls_to_restore,
306 &in);
307 InitWithSyncDataTakeOutput(in, &out);
309 // Expect that the new preference data contains the old pref's values.
310 scoped_ptr<base::ListValue> expected_urls(new base::ListValue);
311 expected_urls->Append(new base::StringValue(kExampleUrl1));
312 expected_urls->Append(new base::StringValue(kExampleUrl2));
314 ASSERT_TRUE(HasSyncData(prefs::kURLsToRestoreOnStartup));
315 scoped_ptr<base::Value> value(
316 FindValue(prefs::kURLsToRestoreOnStartup, out));
317 ASSERT_TRUE(value.get());
318 EXPECT_TRUE(value->Equals(expected_urls.get()));
319 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartup).
320 Equals(expected_urls.get()));
322 // The old preference value should be the same.
323 expected_urls.reset(new base::ListValue);
324 ASSERT_FALSE(FindValue(prefs::kURLsToRestoreOnStartupOld, out).get());
325 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartupOld).
326 Equals(expected_urls.get()));
329 TEST_F(PrefsSyncableServiceTest,
330 ModelAssociationCloudHasOldMigratedData) {
331 ASSERT_TRUE(IsMigratedPreference(prefs::kURLsToRestoreOnStartup));
332 ASSERT_TRUE(IsOldMigratedPreference(prefs::kURLsToRestoreOnStartupOld));
333 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
335 ListPrefUpdate update(GetPrefs(), prefs::kURLsToRestoreOnStartup);
336 base::ListValue* url_list = update.Get();
337 url_list->Append(new base::StringValue(kExampleUrl0));
338 url_list->Append(new base::StringValue(kExampleUrl1));
341 syncer::SyncDataList in;
342 syncer::SyncChangeList out;
343 base::ListValue urls_to_restore;
344 urls_to_restore.Append(new base::StringValue(kExampleUrl1));
345 urls_to_restore.Append(new base::StringValue(kExampleUrl2));
346 AddToRemoteDataList(prefs::kURLsToRestoreOnStartupOld, urls_to_restore, &in);
347 AddToRemoteDataList(prefs::kHomePage, base::StringValue(kExampleUrl1), &in);
348 InitWithSyncDataTakeOutput(in, &out);
350 ASSERT_FALSE(FindValue(prefs::kHomePage, out).get());
352 // Expect that the new preference data contains the merged old prefs values.
353 scoped_ptr<base::ListValue> expected_urls(new base::ListValue);
354 expected_urls->Append(new base::StringValue(kExampleUrl1));
355 expected_urls->Append(new base::StringValue(kExampleUrl2));
356 expected_urls->Append(new base::StringValue(kExampleUrl0));
358 ASSERT_TRUE(HasSyncData(prefs::kURLsToRestoreOnStartup));
359 scoped_ptr<base::Value> value(
360 FindValue(prefs::kURLsToRestoreOnStartup, out));
361 ASSERT_TRUE(value.get());
362 EXPECT_TRUE(value->Equals(expected_urls.get()));
363 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartup).
364 Equals(expected_urls.get()));
366 expected_urls.reset(new base::ListValue);
367 value = FindValue(prefs::kURLsToRestoreOnStartupOld, out).Pass();
368 ASSERT_TRUE(value.get());
369 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartupOld).
370 Equals(expected_urls.get()));
373 TEST_F(PrefsSyncableServiceTest,
374 ModelAssociationCloudHasNewMigratedData) {
375 ASSERT_TRUE(IsMigratedPreference(prefs::kURLsToRestoreOnStartup));
376 ASSERT_TRUE(IsOldMigratedPreference(prefs::kURLsToRestoreOnStartupOld));
377 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
379 ListPrefUpdate update(GetPrefs(), prefs::kURLsToRestoreOnStartupOld);
380 base::ListValue* url_list = update.Get();
381 url_list->Append(new base::StringValue(kExampleUrl0));
382 url_list->Append(new base::StringValue(kExampleUrl1));
385 syncer::SyncDataList in;
386 syncer::SyncChangeList out;
387 base::ListValue urls_to_restore;
388 urls_to_restore.Append(new base::StringValue(kExampleUrl1));
389 urls_to_restore.Append(new base::StringValue(kExampleUrl2));
390 AddToRemoteDataList(prefs::kURLsToRestoreOnStartupOld, urls_to_restore, &in);
391 AddToRemoteDataList(prefs::kHomePage, base::StringValue(kExampleUrl1), &in);
392 InitWithSyncDataTakeOutput(in, &out);
394 scoped_ptr<base::Value> value(FindValue(prefs::kHomePage, out));
395 ASSERT_FALSE(value.get());
397 // Expect that the cloud data under the new migrated preference name sticks.
398 scoped_ptr<base::ListValue> expected_urls(new base::ListValue);
399 expected_urls->Append(new base::StringValue(kExampleUrl1));
400 expected_urls->Append(new base::StringValue(kExampleUrl2));
402 ASSERT_TRUE(HasSyncData(prefs::kURLsToRestoreOnStartup));
403 value = FindValue(prefs::kURLsToRestoreOnStartup, out).Pass();
404 ASSERT_TRUE(value.get());
405 EXPECT_TRUE(value->Equals(expected_urls.get()));
406 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartup).
407 Equals(expected_urls.get()));
409 // The old preference data should still be here, though not synced.
410 expected_urls.reset(new base::ListValue);
411 expected_urls->Append(new base::StringValue(kExampleUrl0));
412 expected_urls->Append(new base::StringValue(kExampleUrl1));
414 value = FindValue(prefs::kURLsToRestoreOnStartupOld, out).Pass();
415 ASSERT_FALSE(value.get());
416 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartupOld).
417 Equals(expected_urls.get()));
420 TEST_F(PrefsSyncableServiceTest,
421 ModelAssociationCloudAddsOldAndNewMigratedData) {
422 ASSERT_TRUE(IsMigratedPreference(prefs::kURLsToRestoreOnStartup));
423 ASSERT_TRUE(IsOldMigratedPreference(prefs::kURLsToRestoreOnStartupOld));
424 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
426 ListPrefUpdate update_old(GetPrefs(), prefs::kURLsToRestoreOnStartupOld);
427 base::ListValue* url_list_old = update_old.Get();
428 url_list_old->Append(new base::StringValue(kExampleUrl0));
429 url_list_old->Append(new base::StringValue(kExampleUrl1));
430 ListPrefUpdate update(GetPrefs(), prefs::kURLsToRestoreOnStartup);
431 base::ListValue* url_list = update.Get();
432 url_list->Append(new base::StringValue(kExampleUrl1));
433 url_list->Append(new base::StringValue(kExampleUrl2));
436 syncer::SyncDataList in;
437 syncer::SyncChangeList out;
438 AddToRemoteDataList(prefs::kHomePage, base::StringValue(kExampleUrl1), &in);
439 InitWithSyncDataTakeOutput(in, &out);
441 scoped_ptr<base::Value> value(FindValue(prefs::kHomePage, out));
442 ASSERT_FALSE(value.get());
444 // Expect that the cloud data under the new migrated preference name sticks.
445 scoped_ptr<base::ListValue> expected_urls(new base::ListValue);
446 expected_urls->Append(new base::StringValue(kExampleUrl1));
447 expected_urls->Append(new base::StringValue(kExampleUrl2));
449 ASSERT_TRUE(HasSyncData(prefs::kURLsToRestoreOnStartup));
450 value = FindValue(prefs::kURLsToRestoreOnStartup, out).Pass();
451 ASSERT_TRUE(value.get());
452 EXPECT_TRUE(value->Equals(expected_urls.get()));
453 EXPECT_TRUE(GetPreferenceValue(prefs::kURLsToRestoreOnStartup).
454 Equals(expected_urls.get()));
456 // Should not have synced in the old startup url values.
457 value = FindValue(prefs::kURLsToRestoreOnStartupOld, out).Pass();
458 ASSERT_FALSE(value.get());
459 EXPECT_FALSE(GetPreferenceValue(prefs::kURLsToRestoreOnStartupOld).
460 Equals(expected_urls.get()));
463 TEST_F(PrefsSyncableServiceTest, FailModelAssociation) {
464 syncer::SyncChangeList output;
465 TestSyncProcessorStub* stub = new TestSyncProcessorStub(&output);
466 stub->FailNextProcessSyncChanges();
467 syncer::SyncMergeResult r = pref_sync_service_->MergeDataAndStartSyncing(
468 syncer::PREFERENCES, syncer::SyncDataList(),
469 scoped_ptr<syncer::SyncChangeProcessor>(stub),
470 scoped_ptr<syncer::SyncErrorFactory>(
471 new syncer::SyncErrorFactoryMock()));
472 EXPECT_TRUE(r.error().IsSet());
475 TEST_F(PrefsSyncableServiceTest, UpdatedPreferenceWithDefaultValue) {
476 const PrefService::Preference* pref =
477 prefs_.FindPreference(prefs::kHomePage);
478 EXPECT_TRUE(pref->IsDefaultValue());
480 syncer::SyncChangeList out;
481 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
482 out.clear();
484 base::StringValue expected(kExampleUrl0);
485 GetPrefs()->Set(prefs::kHomePage, expected);
487 scoped_ptr<base::Value> actual(FindValue(prefs::kHomePage, out));
488 ASSERT_TRUE(actual.get());
489 EXPECT_TRUE(expected.Equals(actual.get()));
492 TEST_F(PrefsSyncableServiceTest, UpdatedPreferenceWithValue) {
493 GetPrefs()->SetString(prefs::kHomePage, kExampleUrl0);
494 syncer::SyncChangeList out;
495 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
496 out.clear();
498 base::StringValue expected(kExampleUrl1);
499 GetPrefs()->Set(prefs::kHomePage, expected);
501 scoped_ptr<base::Value> actual(FindValue(prefs::kHomePage, out));
502 ASSERT_TRUE(actual.get());
503 EXPECT_TRUE(expected.Equals(actual.get()));
506 TEST_F(PrefsSyncableServiceTest, UpdatedSyncNodeActionUpdate) {
507 GetPrefs()->SetString(prefs::kHomePage, kExampleUrl0);
508 InitWithNoSyncData();
510 base::StringValue expected(kExampleUrl1);
511 syncer::SyncChangeList list;
512 list.push_back(MakeRemoteChange(
513 1, prefs::kHomePage, expected, SyncChange::ACTION_UPDATE));
514 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
516 const base::Value& actual = GetPreferenceValue(prefs::kHomePage);
517 EXPECT_TRUE(expected.Equals(&actual));
520 TEST_F(PrefsSyncableServiceTest, UpdatedSyncNodeActionAdd) {
521 InitWithNoSyncData();
523 base::StringValue expected(kExampleUrl0);
524 syncer::SyncChangeList list;
525 list.push_back(MakeRemoteChange(
526 1, prefs::kHomePage, expected, SyncChange::ACTION_ADD));
527 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
529 const base::Value& actual = GetPreferenceValue(prefs::kHomePage);
530 EXPECT_TRUE(expected.Equals(&actual));
531 EXPECT_EQ(1U,
532 pref_sync_service_->registered_preferences().count(prefs::kHomePage));
535 TEST_F(PrefsSyncableServiceTest, UpdatedSyncNodeUnknownPreference) {
536 InitWithNoSyncData();
537 syncer::SyncChangeList list;
538 base::StringValue expected(kExampleUrl0);
539 list.push_back(MakeRemoteChange(
540 1, "unknown preference", expected, SyncChange::ACTION_UPDATE));
541 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
542 // Nothing interesting happens on the client when it gets an update
543 // of an unknown preference. We just should not crash.
546 TEST_F(PrefsSyncableServiceTest, ManagedPreferences) {
547 // Make the homepage preference managed.
548 base::StringValue managed_value("http://example.com");
549 prefs_.SetManagedPref(prefs::kHomePage, managed_value.DeepCopy());
551 syncer::SyncChangeList out;
552 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
553 out.clear();
555 // Changing the homepage preference should not sync anything.
556 base::StringValue user_value("http://chromium..com");
557 prefs_.SetUserPref(prefs::kHomePage, user_value.DeepCopy());
558 EXPECT_TRUE(out.empty());
560 // An incoming sync transaction should change the user value, not the managed
561 // value.
562 base::StringValue sync_value("http://crbug.com");
563 syncer::SyncChangeList list;
564 list.push_back(MakeRemoteChange(
565 1, prefs::kHomePage, sync_value, SyncChange::ACTION_UPDATE));
566 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
568 EXPECT_TRUE(managed_value.Equals(prefs_.GetManagedPref(prefs::kHomePage)));
569 EXPECT_TRUE(sync_value.Equals(prefs_.GetUserPref(prefs::kHomePage)));
572 // List preferences have special handling at association time due to our ability
573 // to merge the local and sync value. Make sure the merge logic doesn't merge
574 // managed preferences.
575 TEST_F(PrefsSyncableServiceTest, ManagedListPreferences) {
576 // Make the list of urls to restore on startup managed.
577 base::ListValue managed_value;
578 managed_value.Append(new base::StringValue(kExampleUrl0));
579 managed_value.Append(new base::StringValue(kExampleUrl1));
580 prefs_.SetManagedPref(prefs::kURLsToRestoreOnStartup,
581 managed_value.DeepCopy());
583 // Set a cloud version.
584 syncer::SyncDataList in;
585 syncer::SyncChangeList out;
586 base::ListValue urls_to_restore;
587 urls_to_restore.Append(new base::StringValue(kExampleUrl1));
588 urls_to_restore.Append(new base::StringValue(kExampleUrl2));
589 AddToRemoteDataList(prefs::kURLsToRestoreOnStartup, urls_to_restore, &in);
591 // Start sync and verify the synced value didn't get merged.
592 InitWithSyncDataTakeOutput(in, &out);
593 EXPECT_FALSE(FindValue(prefs::kURLsToRestoreOnStartup, out).get());
594 out.clear();
596 // Changing the user's urls to restore on startup pref should not sync
597 // anything.
598 base::ListValue user_value;
599 user_value.Append(new base::StringValue("http://chromium.org"));
600 prefs_.SetUserPref(prefs::kURLsToRestoreOnStartup, user_value.DeepCopy());
601 EXPECT_FALSE(FindValue(prefs::kURLsToRestoreOnStartup, out).get());
603 // An incoming sync transaction should change the user value, not the managed
604 // value.
605 base::ListValue sync_value;
606 sync_value.Append(new base::StringValue("http://crbug.com"));
607 syncer::SyncChangeList list;
608 list.push_back(MakeRemoteChange(
609 1, prefs::kURLsToRestoreOnStartup, sync_value,
610 SyncChange::ACTION_UPDATE));
611 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
613 EXPECT_TRUE(managed_value.Equals(
614 prefs_.GetManagedPref(prefs::kURLsToRestoreOnStartup)));
615 EXPECT_TRUE(sync_value.Equals(
616 prefs_.GetUserPref(prefs::kURLsToRestoreOnStartup)));
619 TEST_F(PrefsSyncableServiceTest, DynamicManagedPreferences) {
620 syncer::SyncChangeList out;
621 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
622 out.clear();
623 base::StringValue initial_value("http://example.com/initial");
624 GetPrefs()->Set(prefs::kHomePage, initial_value);
625 scoped_ptr<base::Value> actual(FindValue(prefs::kHomePage, out));
626 ASSERT_TRUE(actual.get());
627 EXPECT_TRUE(initial_value.Equals(actual.get()));
629 // Switch kHomePage to managed and set a different value.
630 base::StringValue managed_value("http://example.com/managed");
631 GetTestingPrefService()->SetManagedPref(prefs::kHomePage,
632 managed_value.DeepCopy());
634 // The pref value should be the one dictated by policy.
635 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(prefs::kHomePage)));
637 // Switch kHomePage back to unmanaged.
638 GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage);
640 // The original value should be picked up.
641 EXPECT_TRUE(initial_value.Equals(&GetPreferenceValue(prefs::kHomePage)));
644 TEST_F(PrefsSyncableServiceTest,
645 DynamicManagedPreferencesWithSyncChange) {
646 syncer::SyncChangeList out;
647 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
648 out.clear();
650 base::StringValue initial_value("http://example.com/initial");
651 GetPrefs()->Set(prefs::kHomePage, initial_value);
652 scoped_ptr<base::Value> actual(FindValue(prefs::kHomePage, out));
653 EXPECT_TRUE(initial_value.Equals(actual.get()));
655 // Switch kHomePage to managed and set a different value.
656 base::StringValue managed_value("http://example.com/managed");
657 GetTestingPrefService()->SetManagedPref(prefs::kHomePage,
658 managed_value.DeepCopy());
660 // Change the sync value.
661 base::StringValue sync_value("http://example.com/sync");
662 syncer::SyncChangeList list;
663 list.push_back(MakeRemoteChange(
664 1, prefs::kHomePage, sync_value, SyncChange::ACTION_UPDATE));
665 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
667 // The pref value should still be the one dictated by policy.
668 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(prefs::kHomePage)));
670 // Switch kHomePage back to unmanaged.
671 GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage);
673 // Sync value should be picked up.
674 EXPECT_TRUE(sync_value.Equals(&GetPreferenceValue(prefs::kHomePage)));
677 TEST_F(PrefsSyncableServiceTest, DynamicManagedDefaultPreferences) {
678 const PrefService::Preference* pref =
679 prefs_.FindPreference(prefs::kHomePage);
680 EXPECT_TRUE(pref->IsDefaultValue());
681 syncer::SyncChangeList out;
682 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out);
684 EXPECT_TRUE(IsSynced(prefs::kHomePage));
685 EXPECT_TRUE(pref->IsDefaultValue());
686 EXPECT_FALSE(FindValue(prefs::kHomePage, out).get());
687 out.clear();
689 // Switch kHomePage to managed and set a different value.
690 base::StringValue managed_value("http://example.com/managed");
691 GetTestingPrefService()->SetManagedPref(prefs::kHomePage,
692 managed_value.DeepCopy());
693 // The pref value should be the one dictated by policy.
694 EXPECT_TRUE(managed_value.Equals(&GetPreferenceValue(prefs::kHomePage)));
695 EXPECT_FALSE(pref->IsDefaultValue());
696 // There should be no synced value.
697 EXPECT_FALSE(FindValue(prefs::kHomePage, out).get());
698 // Switch kHomePage back to unmanaged.
699 GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage);
700 // The original value should be picked up.
701 EXPECT_TRUE(pref->IsDefaultValue());
702 // There should still be no synced value.
703 EXPECT_FALSE(FindValue(prefs::kHomePage, out).get());
706 TEST_F(PrefsSyncableServiceTest, DeletePreference) {
707 prefs_.SetString(prefs::kHomePage, kExampleUrl0);
708 const PrefService::Preference* pref =
709 prefs_.FindPreference(prefs::kHomePage);
710 EXPECT_FALSE(pref->IsDefaultValue());
712 InitWithNoSyncData();
714 scoped_ptr<base::Value> null_value = base::Value::CreateNullValue();
715 syncer::SyncChangeList list;
716 list.push_back(MakeRemoteChange(
717 1, prefs::kHomePage, *null_value, SyncChange::ACTION_DELETE));
718 pref_sync_service_->ProcessSyncChanges(FROM_HERE, list);
719 EXPECT_TRUE(pref->IsDefaultValue());