OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 #include "chrome/browser/prefs/pref_model_associator.h" | 12 #include "chrome/browser/prefs/pref_model_associator.h" |
13 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
14 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" | 14 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 15 #include "chrome/browser/sync/api/sync_data.h" |
15 #include "chrome/browser/sync/engine/syncapi.h" | 16 #include "chrome/browser/sync/engine/syncapi.h" |
16 #include "chrome/browser/sync/glue/generic_change_processor.h" | 17 #include "chrome/browser/sync/glue/generic_change_processor.h" |
17 #include "chrome/browser/sync/glue/preference_data_type_controller.h" | 18 #include "chrome/browser/sync/glue/preference_data_type_controller.h" |
18 #include "chrome/browser/sync/glue/sync_backend_host.h" | 19 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| 20 #include "chrome/browser/sync/glue/syncable_service_adapter.h" |
19 #include "chrome/browser/sync/profile_sync_test_util.h" | 21 #include "chrome/browser/sync/profile_sync_test_util.h" |
20 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" | 22 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
21 #include "chrome/browser/sync/syncable/model_type.h" | 23 #include "chrome/browser/sync/syncable/model_type.h" |
22 #include "chrome/browser/sync/test_profile_sync_service.h" | 24 #include "chrome/browser/sync/test_profile_sync_service.h" |
23 #include "chrome/common/net/gaia/gaia_constants.h" | 25 #include "chrome/common/net/gaia/gaia_constants.h" |
24 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
25 #include "chrome/test/testing_pref_service.h" | 27 #include "chrome/test/testing_pref_service.h" |
26 #include "chrome/test/testing_profile.h" | 28 #include "chrome/test/testing_profile.h" |
27 #include "content/common/json_value_serializer.h" | 29 #include "content/common/json_value_serializer.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 | 32 |
31 using base::JSONReader; | 33 using base::JSONReader; |
32 using browser_sync::GenericChangeProcessor; | 34 using browser_sync::GenericChangeProcessor; |
33 using browser_sync::PreferenceDataTypeController; | 35 using browser_sync::PreferenceDataTypeController; |
34 using browser_sync::SyncBackendHost; | 36 using browser_sync::SyncBackendHost; |
| 37 using browser_sync::SyncableServiceAdapter; |
35 using sync_api::SyncManager; | 38 using sync_api::SyncManager; |
36 using testing::_; | 39 using testing::_; |
| 40 using testing::Invoke; |
37 using testing::Return; | 41 using testing::Return; |
38 | 42 |
39 typedef std::map<const std::string, const Value*> PreferenceValues; | 43 typedef std::map<const std::string, const Value*> PreferenceValues; |
40 | 44 |
| 45 ACTION_P4(BuildPrefSyncComponents, profile_sync_service, pref_sync_service, |
| 46 model_associator_ptr, change_processor_ptr) { |
| 47 sync_api::UserShare* user_share = profile_sync_service->GetUserShare(); |
| 48 *change_processor_ptr = new GenericChangeProcessor(pref_sync_service, |
| 49 profile_sync_service, |
| 50 user_share); |
| 51 *model_associator_ptr = new browser_sync::SyncableServiceAdapter( |
| 52 syncable::PREFERENCES, |
| 53 pref_sync_service, |
| 54 *change_processor_ptr); |
| 55 return ProfileSyncFactory::SyncComponents(*model_associator_ptr, |
| 56 *change_processor_ptr); |
| 57 } |
| 58 |
| 59 // TODO(zea): Refactor to remove the ProfileSyncService usage. |
41 class ProfileSyncServicePreferenceTest | 60 class ProfileSyncServicePreferenceTest |
42 : public AbstractProfileSyncServiceTest { | 61 : public AbstractProfileSyncServiceTest { |
43 protected: | 62 protected: |
44 ProfileSyncServicePreferenceTest() | 63 ProfileSyncServicePreferenceTest() |
45 : example_url0_("http://example.com/0"), | 64 : example_url0_("http://example.com/0"), |
46 example_url1_("http://example.com/1"), | 65 example_url1_("http://example.com/1"), |
47 example_url2_("http://example.com/2"), | 66 example_url2_("http://example.com/2"), |
48 not_synced_preference_name_("nonsense_pref_name"), | 67 not_synced_preference_name_("nonsense_pref_name"), |
49 not_synced_preference_default_value_("default"), | 68 not_synced_preference_default_value_("default"), |
50 non_default_charset_value_("foo") {} | 69 non_default_charset_value_("foo") {} |
(...skipping 14 matching lines...) Expand all Loading... |
65 profile_.reset(); | 84 profile_.reset(); |
66 AbstractProfileSyncServiceTest::TearDown(); | 85 AbstractProfileSyncServiceTest::TearDown(); |
67 } | 86 } |
68 | 87 |
69 bool StartSyncService(Task* task, bool will_fail_association) { | 88 bool StartSyncService(Task* task, bool will_fail_association) { |
70 if (service_.get()) | 89 if (service_.get()) |
71 return false; | 90 return false; |
72 | 91 |
73 service_.reset(new TestProfileSyncService( | 92 service_.reset(new TestProfileSyncService( |
74 &factory_, profile_.get(), "test", false, task)); | 93 &factory_, profile_.get(), "test", false, task)); |
75 model_associator_ = | 94 pref_sync_service_ = reinterpret_cast<PrefModelAssociator*>( |
76 reinterpret_cast<PrefModelAssociator*>(prefs_->GetSyncableService()); | 95 prefs_->GetSyncableService()); |
77 change_processor_ = new GenericChangeProcessor(model_associator_, | 96 if (!pref_sync_service_) |
78 service_.get()); | 97 return false; |
79 EXPECT_CALL(factory_, CreatePreferenceSyncComponents(_, _)). | 98 EXPECT_CALL(factory_, CreatePreferenceSyncComponents(_, _)). |
80 WillOnce(Return(ProfileSyncFactory::SyncComponents( | 99 WillOnce(BuildPrefSyncComponents(service_.get(), |
81 model_associator_, change_processor_))); | 100 pref_sync_service_, |
| 101 &model_associator_, |
| 102 &change_processor_)); |
82 | 103 |
83 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). | 104 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). |
84 WillOnce(ReturnNewDataTypeManager()); | 105 WillOnce(ReturnNewDataTypeManager()); |
85 | 106 |
86 dtc_ = new PreferenceDataTypeController(&factory_, | 107 dtc_ = new PreferenceDataTypeController(&factory_, |
87 profile_.get(), | 108 profile_.get(), |
88 service_.get()); | 109 service_.get()); |
89 service_->RegisterDataTypeController(dtc_); | 110 service_->RegisterDataTypeController(dtc_); |
90 profile_->GetTokenService()->IssueAuthTokenForTest( | 111 profile_->GetTokenService()->IssueAuthTokenForTest( |
91 GaiaConstants::kSyncService, "token"); | 112 GaiaConstants::kSyncService, "token"); |
92 | 113 |
93 service_->Initialize(); | 114 service_->Initialize(); |
94 MessageLoop::current()->Run(); | 115 MessageLoop::current()->Run(); |
95 return true; | 116 return true; |
96 } | 117 } |
97 | 118 |
98 const Value& GetPreferenceValue(const std::string& name) { | 119 const Value& GetPreferenceValue(const std::string& name) { |
99 const PrefService::Preference* preference = | 120 const PrefService::Preference* preference = |
100 prefs_->FindPreference(name.c_str()); | 121 prefs_->FindPreference(name.c_str()); |
101 return *preference->GetValue(); | 122 return *preference->GetValue(); |
102 } | 123 } |
103 | 124 |
104 // Caller gets ownership of the returned value. | 125 // Caller gets ownership of the returned value. |
105 const Value* GetSyncedValue(const std::string& name) { | 126 const Value* GetSyncedValue(const std::string& name) { |
106 sync_api::ReadTransaction trans(service_->GetUserShare()); | 127 sync_api::ReadTransaction trans(service_->GetUserShare()); |
107 sync_api::ReadNode node(&trans); | 128 sync_api::ReadNode node(&trans); |
108 | 129 |
109 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); | 130 if (!node.InitByClientTagLookup(syncable::PREFERENCES, name)) |
110 if (node_id == sync_api::kInvalidId) | |
111 return NULL; | |
112 if (!node.InitByIdLookup(node_id)) | |
113 return NULL; | 131 return NULL; |
114 | 132 |
115 const sync_pb::PreferenceSpecifics& specifics( | 133 const sync_pb::PreferenceSpecifics& specifics( |
116 node.GetPreferenceSpecifics()); | 134 node.GetEntitySpecifics().GetExtension(sync_pb::preference)); |
117 | 135 |
118 JSONReader reader; | 136 JSONReader reader; |
119 return reader.JsonToValue(specifics.value(), false, false); | 137 return reader.JsonToValue(specifics.value(), false, false); |
120 } | 138 } |
121 | 139 |
122 int64 WriteSyncedValue(const std::string& name, | 140 int64 WriteSyncedValue(const std::string& name, |
123 const Value& value, | 141 const Value& value, |
124 sync_api::WriteNode* node) { | 142 sync_api::WriteNode* node) { |
125 if (!PrefModelAssociator::WritePreferenceToNode(name, value, node)) | 143 SyncData sync_data; |
| 144 if (!PrefModelAssociator::CreatePrefSyncData(name, |
| 145 value, |
| 146 &sync_data)) { |
126 return sync_api::kInvalidId; | 147 return sync_api::kInvalidId; |
| 148 } |
| 149 node->SetEntitySpecifics(sync_data.GetSpecifics()); |
127 return node->GetId(); | 150 return node->GetId(); |
128 } | 151 } |
129 | 152 |
130 int64 SetSyncedValue(const std::string& name, const Value& value) { | 153 int64 SetSyncedValue(const std::string& name, const Value& value) { |
131 sync_api::WriteTransaction trans(service_->GetUserShare()); | 154 sync_api::WriteTransaction trans(service_->GetUserShare()); |
132 sync_api::ReadNode root(&trans); | 155 sync_api::ReadNode root(&trans); |
133 if (!root.InitByTagLookup( | 156 if (!root.InitByTagLookup( |
134 syncable::ModelTypeToRootTag(syncable::PREFERENCES))) { | 157 syncable::ModelTypeToRootTag(syncable::PREFERENCES))) { |
135 return sync_api::kInvalidId; | 158 return sync_api::kInvalidId; |
136 } | 159 } |
137 | 160 |
138 sync_api::WriteNode tag_node(&trans); | 161 sync_api::WriteNode tag_node(&trans); |
139 sync_api::WriteNode node(&trans); | 162 sync_api::WriteNode node(&trans); |
140 | 163 |
141 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); | 164 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name)) |
142 if (node_id == sync_api::kInvalidId) { | 165 return WriteSyncedValue(name, value, &tag_node); |
143 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name)) | 166 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name)) |
144 return WriteSyncedValue(name, value, &tag_node); | |
145 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name)) | |
146 return WriteSyncedValue(name, value, &node); | |
147 } else if (node.InitByIdLookup(node_id)) { | |
148 return WriteSyncedValue(name, value, &node); | 167 return WriteSyncedValue(name, value, &node); |
149 } | 168 |
150 return sync_api::kInvalidId; | 169 return sync_api::kInvalidId; |
151 } | 170 } |
152 | 171 |
153 SyncManager::ChangeRecord* MakeChangeRecord(const std::string& name, | 172 SyncManager::ChangeRecord* MakeChangeRecord(int64 node_id, |
154 SyncManager::ChangeRecord) { | 173 SyncManager::ChangeRecord::Action action) { |
155 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); | |
156 SyncManager::ChangeRecord* record = new SyncManager::ChangeRecord(); | 174 SyncManager::ChangeRecord* record = new SyncManager::ChangeRecord(); |
157 record->action = SyncManager::ChangeRecord::ACTION_UPDATE; | 175 record->action = action; |
158 record->id = node_id; | 176 record->id = node_id; |
159 return record; | 177 return record; |
160 } | 178 } |
161 | 179 |
162 bool IsSynced(const std::string& pref_name) { | 180 bool IsSynced(const std::string& pref_name) { |
163 return model_associator_->synced_preferences().count(pref_name) > 0; | 181 return pref_sync_service_->registered_preferences().count(pref_name) > 0; |
164 } | 182 } |
165 | 183 |
166 std::string ValueString(const Value& value) { | 184 std::string ValueString(const Value& value) { |
167 std::string serialized; | 185 std::string serialized; |
168 JSONStringValueSerializer json(&serialized); | 186 JSONStringValueSerializer json(&serialized); |
169 json.Serialize(value); | 187 json.Serialize(value); |
170 return serialized; | 188 return serialized; |
171 } | 189 } |
172 | 190 |
173 friend class AddPreferenceEntriesTask; | 191 friend class AddPreferenceEntriesTask; |
174 | 192 |
175 scoped_ptr<TestingProfile> profile_; | 193 scoped_ptr<TestingProfile> profile_; |
176 TestingPrefService* prefs_; | 194 TestingPrefService* prefs_; |
177 | 195 |
178 PreferenceDataTypeController* dtc_; | 196 PreferenceDataTypeController* dtc_; |
179 PrefModelAssociator* model_associator_; | 197 PrefModelAssociator* pref_sync_service_; |
| 198 SyncableServiceAdapter* model_associator_; |
180 GenericChangeProcessor* change_processor_; | 199 GenericChangeProcessor* change_processor_; |
181 | 200 |
182 std::string example_url0_; | 201 std::string example_url0_; |
183 std::string example_url1_; | 202 std::string example_url1_; |
184 std::string example_url2_; | 203 std::string example_url2_; |
185 std::string not_synced_preference_name_; | 204 std::string not_synced_preference_name_; |
186 std::string not_synced_preference_default_value_; | 205 std::string not_synced_preference_default_value_; |
187 std::string non_default_charset_value_; | 206 std::string non_default_charset_value_; |
188 }; | 207 }; |
189 | 208 |
(...skipping 16 matching lines...) Expand all Loading... |
206 } | 225 } |
207 | 226 |
208 bool success() { return success_; } | 227 bool success() { return success_; } |
209 | 228 |
210 private: | 229 private: |
211 ProfileSyncServicePreferenceTest* test_; | 230 ProfileSyncServicePreferenceTest* test_; |
212 const PreferenceValues& entries_; | 231 const PreferenceValues& entries_; |
213 bool success_; | 232 bool success_; |
214 }; | 233 }; |
215 | 234 |
216 TEST_F(ProfileSyncServicePreferenceTest, WritePreferenceToNode) { | 235 TEST_F(ProfileSyncServicePreferenceTest, CreatePrefSyncData) { |
217 prefs_->SetString(prefs::kHomePage, example_url0_); | 236 prefs_->SetString(prefs::kHomePage, example_url0_); |
218 CreateRootTask task(this, syncable::PREFERENCES); | 237 CreateRootTask task(this, syncable::PREFERENCES); |
219 ASSERT_TRUE(StartSyncService(&task, false)); | 238 ASSERT_TRUE(StartSyncService(&task, false)); |
220 ASSERT_TRUE(task.success()); | 239 ASSERT_TRUE(task.success()); |
221 | 240 |
222 const PrefService::Preference* pref = | 241 const PrefService::Preference* pref = |
223 prefs_->FindPreference(prefs::kHomePage); | 242 prefs_->FindPreference(prefs::kHomePage); |
224 sync_api::WriteTransaction trans(service_->GetUserShare()); | 243 SyncData sync_data; |
225 sync_api::WriteNode node(&trans); | 244 EXPECT_TRUE(PrefModelAssociator::CreatePrefSyncData(pref->name(), |
226 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES, | 245 *pref->GetValue(), &sync_data)); |
227 prefs::kHomePage)); | 246 EXPECT_EQ(std::string(prefs::kHomePage), sync_data.GetTag()); |
228 | 247 const sync_pb::PreferenceSpecifics& specifics(sync_data.GetSpecifics(). |
229 EXPECT_TRUE(PrefModelAssociator::WritePreferenceToNode( | 248 GetExtension(sync_pb::preference)); |
230 pref->name(), *pref->GetValue(), &node)); | |
231 EXPECT_EQ(UTF8ToWide(prefs::kHomePage), node.GetTitle()); | |
232 const sync_pb::PreferenceSpecifics& specifics(node.GetPreferenceSpecifics()); | |
233 EXPECT_EQ(std::string(prefs::kHomePage), specifics.name()); | 249 EXPECT_EQ(std::string(prefs::kHomePage), specifics.name()); |
234 | 250 |
235 base::JSONReader reader; | 251 base::JSONReader reader; |
236 scoped_ptr<Value> value(reader.JsonToValue(specifics.value(), false, false)); | 252 scoped_ptr<Value> value(reader.JsonToValue(specifics.value(), false, false)); |
237 EXPECT_TRUE(pref->GetValue()->Equals(value.get())); | 253 EXPECT_TRUE(pref->GetValue()->Equals(value.get())); |
238 } | 254 } |
239 | 255 |
240 TEST_F(ProfileSyncServicePreferenceTest, ModelAssociationDoNotSyncDefaults) { | 256 TEST_F(ProfileSyncServicePreferenceTest, ModelAssociationDoNotSyncDefaults) { |
241 const PrefService::Preference* pref = | 257 const PrefService::Preference* pref = |
242 prefs_->FindPreference(prefs::kHomePage); | 258 prefs_->FindPreference(prefs::kHomePage); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 EXPECT_TRUE(expected->Equals(actual.get())); | 373 EXPECT_TRUE(expected->Equals(actual.get())); |
358 } | 374 } |
359 | 375 |
360 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionUpdate) { | 376 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionUpdate) { |
361 profile_->GetPrefs()->SetString(prefs::kHomePage, example_url0_); | 377 profile_->GetPrefs()->SetString(prefs::kHomePage, example_url0_); |
362 CreateRootTask task(this, syncable::PREFERENCES); | 378 CreateRootTask task(this, syncable::PREFERENCES); |
363 ASSERT_TRUE(StartSyncService(&task, false)); | 379 ASSERT_TRUE(StartSyncService(&task, false)); |
364 ASSERT_TRUE(task.success()); | 380 ASSERT_TRUE(task.success()); |
365 | 381 |
366 scoped_ptr<Value> expected(Value::CreateStringValue(example_url1_)); | 382 scoped_ptr<Value> expected(Value::CreateStringValue(example_url1_)); |
367 ASSERT_NE(SetSyncedValue(prefs::kHomePage, *expected), sync_api::kInvalidId); | 383 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); |
368 int64 node_id = model_associator_->GetSyncIdFromChromeId(prefs::kHomePage); | 384 ASSERT_NE(node_id, sync_api::kInvalidId); |
369 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord); | 385 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( |
370 record->action = SyncManager::ChangeRecord::ACTION_UPDATE; | 386 node_id, SyncManager::ChangeRecord::ACTION_UPDATE)); |
371 record->id = node_id; | |
372 { | 387 { |
373 sync_api::WriteTransaction trans(service_->GetUserShare()); | 388 sync_api::WriteTransaction trans(service_->GetUserShare()); |
374 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 389 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
375 } | 390 } |
| 391 change_processor_->CommitChangesFromSyncModel(); |
376 | 392 |
377 const Value& actual = GetPreferenceValue(prefs::kHomePage); | 393 const Value& actual = GetPreferenceValue(prefs::kHomePage); |
378 EXPECT_TRUE(expected->Equals(&actual)); | 394 EXPECT_TRUE(expected->Equals(&actual)); |
379 } | 395 } |
380 | 396 |
381 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionAdd) { | 397 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionAdd) { |
382 CreateRootTask task(this, syncable::PREFERENCES); | 398 CreateRootTask task(this, syncable::PREFERENCES); |
383 ASSERT_TRUE(StartSyncService(&task, false)); | 399 ASSERT_TRUE(StartSyncService(&task, false)); |
384 ASSERT_TRUE(task.success()); | 400 ASSERT_TRUE(task.success()); |
385 | 401 |
386 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); | 402 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); |
387 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); | 403 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); |
388 ASSERT_NE(node_id, sync_api::kInvalidId); | 404 ASSERT_NE(node_id, sync_api::kInvalidId); |
389 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord); | 405 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( |
390 record->action = SyncManager::ChangeRecord::ACTION_ADD; | 406 node_id, SyncManager::ChangeRecord::ACTION_ADD)); |
391 record->id = node_id; | |
392 { | 407 { |
393 sync_api::WriteTransaction trans(service_->GetUserShare()); | 408 sync_api::WriteTransaction trans(service_->GetUserShare()); |
394 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 409 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
395 } | 410 } |
| 411 change_processor_->CommitChangesFromSyncModel(); |
396 | 412 |
397 const Value& actual = GetPreferenceValue(prefs::kHomePage); | 413 const Value& actual = GetPreferenceValue(prefs::kHomePage); |
398 EXPECT_TRUE(expected->Equals(&actual)); | 414 EXPECT_TRUE(expected->Equals(&actual)); |
399 EXPECT_EQ(node_id, | 415 EXPECT_EQ(1U, |
400 model_associator_->GetSyncIdFromChromeId(prefs::kHomePage)); | 416 pref_sync_service_->synced_preferences().count(prefs::kHomePage)); |
401 } | 417 } |
402 | 418 |
403 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeUnknownPreference) { | 419 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeUnknownPreference) { |
404 CreateRootTask task(this, syncable::PREFERENCES); | 420 CreateRootTask task(this, syncable::PREFERENCES); |
405 ASSERT_TRUE(StartSyncService(&task, false)); | 421 ASSERT_TRUE(StartSyncService(&task, false)); |
406 ASSERT_TRUE(task.success()); | 422 ASSERT_TRUE(task.success()); |
407 | 423 |
408 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); | 424 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); |
409 int64 node_id = SetSyncedValue("unknown preference", *expected); | 425 int64 node_id = SetSyncedValue("unknown preference", *expected); |
410 ASSERT_NE(node_id, sync_api::kInvalidId); | 426 ASSERT_NE(node_id, sync_api::kInvalidId); |
411 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord); | 427 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( |
412 record->action = SyncManager::ChangeRecord::ACTION_ADD; | 428 node_id, SyncManager::ChangeRecord::ACTION_UPDATE)); |
413 record->id = node_id; | |
414 { | 429 { |
415 sync_api::WriteTransaction trans(service_->GetUserShare()); | 430 sync_api::WriteTransaction trans(service_->GetUserShare()); |
416 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 431 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
417 } | 432 } |
| 433 change_processor_->CommitChangesFromSyncModel(); |
418 | 434 |
419 // Nothing interesting happens on the client when it gets an update | 435 // Nothing interesting happens on the client when it gets an update |
420 // of an unknown preference. We just should not crash. | 436 // of an unknown preference. We just should not crash. |
421 } | 437 } |
422 | 438 |
423 TEST_F(ProfileSyncServicePreferenceTest, ManagedPreferences) { | 439 TEST_F(ProfileSyncServicePreferenceTest, ManagedPreferences) { |
424 // Make the homepage preference managed. | 440 // Make the homepage preference managed. |
425 scoped_ptr<Value> managed_value( | 441 scoped_ptr<Value> managed_value( |
426 Value::CreateStringValue("http://example.com")); | 442 Value::CreateStringValue("http://example.com")); |
427 prefs_->SetManagedPref(prefs::kHomePage, managed_value->DeepCopy()); | 443 prefs_->SetManagedPref(prefs::kHomePage, managed_value->DeepCopy()); |
428 | 444 |
429 CreateRootTask task(this, syncable::PREFERENCES); | 445 CreateRootTask task(this, syncable::PREFERENCES); |
430 ASSERT_TRUE(StartSyncService(&task, false)); | 446 ASSERT_TRUE(StartSyncService(&task, false)); |
431 ASSERT_TRUE(task.success()); | 447 ASSERT_TRUE(task.success()); |
432 | 448 |
433 // Changing the homepage preference should not sync anything. | 449 // Changing the homepage preference should not sync anything. |
434 scoped_ptr<Value> user_value( | 450 scoped_ptr<Value> user_value( |
435 Value::CreateStringValue("http://chromium..com")); | 451 Value::CreateStringValue("http://chromium..com")); |
436 prefs_->SetUserPref(prefs::kHomePage, user_value->DeepCopy()); | 452 prefs_->SetUserPref(prefs::kHomePage, user_value->DeepCopy()); |
437 EXPECT_EQ(NULL, GetSyncedValue(prefs::kHomePage)); | 453 EXPECT_EQ(NULL, GetSyncedValue(prefs::kHomePage)); |
438 | 454 |
439 // An incoming sync transaction shouldn't change the user value. | 455 // An incoming sync transaction shouldn't change the user value. |
440 scoped_ptr<Value> sync_value( | 456 scoped_ptr<Value> sync_value( |
441 Value::CreateStringValue("http://crbug.com")); | 457 Value::CreateStringValue("http://crbug.com")); |
442 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); | 458 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); |
443 ASSERT_NE(node_id, sync_api::kInvalidId); | 459 ASSERT_NE(node_id, sync_api::kInvalidId); |
444 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord); | 460 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( |
445 record->action = SyncManager::ChangeRecord::ACTION_UPDATE; | 461 node_id, SyncManager::ChangeRecord::ACTION_UPDATE)); |
446 record->id = node_id; | |
447 { | 462 { |
448 sync_api::WriteTransaction trans(service_->GetUserShare()); | 463 sync_api::WriteTransaction trans(service_->GetUserShare()); |
449 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 464 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
450 } | 465 } |
| 466 change_processor_->CommitChangesFromSyncModel(); |
| 467 |
451 EXPECT_TRUE(managed_value->Equals( | 468 EXPECT_TRUE(managed_value->Equals( |
452 prefs_->GetManagedPref(prefs::kHomePage))); | 469 prefs_->GetManagedPref(prefs::kHomePage))); |
453 EXPECT_TRUE(user_value->Equals( | 470 EXPECT_TRUE(user_value->Equals( |
454 prefs_->GetUserPref(prefs::kHomePage))); | 471 prefs_->GetUserPref(prefs::kHomePage))); |
455 } | 472 } |
456 | 473 |
457 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) { | 474 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) { |
458 CreateRootTask task(this, syncable::PREFERENCES); | 475 CreateRootTask task(this, syncable::PREFERENCES); |
459 ASSERT_TRUE(StartSyncService(&task, false)); | 476 ASSERT_TRUE(StartSyncService(&task, false)); |
460 ASSERT_TRUE(task.success()); | 477 ASSERT_TRUE(task.success()); |
461 | 478 |
462 scoped_ptr<Value> initial_value( | 479 scoped_ptr<Value> initial_value( |
463 Value::CreateStringValue("http://example.com/initial")); | 480 Value::CreateStringValue("http://example.com/initial")); |
464 profile_->GetPrefs()->Set(prefs::kHomePage, *initial_value); | 481 profile_->GetPrefs()->Set(prefs::kHomePage, *initial_value); |
465 scoped_ptr<const Value> actual(GetSyncedValue(prefs::kHomePage)); | 482 scoped_ptr<const Value> actual(GetSyncedValue(prefs::kHomePage)); |
466 EXPECT_TRUE(initial_value->Equals(actual.get())); | 483 EXPECT_TRUE(initial_value->Equals(actual.get())); |
467 | 484 |
468 // Switch kHomePage to managed and set a different value. | 485 // Switch kHomePage to managed and set a different value. |
469 scoped_ptr<Value> managed_value( | 486 scoped_ptr<Value> managed_value( |
470 Value::CreateStringValue("http://example.com/managed")); | 487 Value::CreateStringValue("http://example.com/managed")); |
471 profile_->GetTestingPrefService()->SetManagedPref( | 488 profile_->GetTestingPrefService()->SetManagedPref( |
472 prefs::kHomePage, managed_value->DeepCopy()); | 489 prefs::kHomePage, managed_value->DeepCopy()); |
473 | 490 |
474 // Sync node should be gone. | 491 // Sync node should be gone. |
475 EXPECT_EQ(sync_api::kInvalidId, | 492 EXPECT_EQ(0U, |
476 model_associator_->GetSyncIdFromChromeId(prefs::kHomePage)); | 493 pref_sync_service_->synced_preferences().count(prefs::kHomePage)); |
477 | 494 |
478 // Change the sync value. | 495 // Change the sync value. |
479 scoped_ptr<Value> sync_value( | 496 scoped_ptr<Value> sync_value( |
480 Value::CreateStringValue("http://example.com/sync")); | 497 Value::CreateStringValue("http://example.com/sync")); |
481 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); | 498 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); |
482 ASSERT_NE(node_id, sync_api::kInvalidId); | 499 ASSERT_NE(node_id, sync_api::kInvalidId); |
483 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord); | 500 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( |
484 record->action = SyncManager::ChangeRecord::ACTION_ADD; | 501 node_id, SyncManager::ChangeRecord::ACTION_ADD)); |
485 record->id = node_id; | |
486 { | 502 { |
487 sync_api::WriteTransaction trans(service_->GetUserShare()); | 503 sync_api::WriteTransaction trans(service_->GetUserShare()); |
488 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 504 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
489 } | 505 } |
| 506 change_processor_->CommitChangesFromSyncModel(); |
490 | 507 |
491 // The pref value should still be the one dictated by policy. | 508 // The pref value should still be the one dictated by policy. |
492 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); | 509 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); |
493 | 510 |
494 // Switch kHomePage back to unmanaged. | 511 // Switch kHomePage back to unmanaged. |
495 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); | 512 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); |
496 | 513 |
497 // Sync value should be picked up. | 514 // Sync value should be picked up. |
498 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); | 515 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); |
499 } | 516 } |
OLD | NEW |