| 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.h" | 9 #include "base/stl_util.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 "base/tracked.h" | 12 #include "base/tracked.h" |
| 13 #include "chrome/browser/prefs/pref_model_associator.h" | 13 #include "chrome/browser/prefs/pref_model_associator.h" |
| 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 15 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" | 15 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 16 #include "chrome/browser/sync/api/sync_data.h" | 16 #include "chrome/browser/sync/api/sync_data.h" |
| 17 #include "chrome/browser/sync/glue/generic_change_processor.h" | 17 #include "chrome/browser/sync/glue/generic_change_processor.h" |
| 18 #include "chrome/browser/sync/glue/preference_data_type_controller.h" | 18 #include "chrome/browser/sync/glue/preference_data_type_controller.h" |
| 19 #include "chrome/browser/sync/glue/syncable_service_adapter.h" | 19 #include "chrome/browser/sync/glue/syncable_service_adapter.h" |
| 20 #include "chrome/browser/sync/glue/sync_backend_host.h" | 20 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| 21 #include "chrome/browser/sync/internal_api/change_record.h" |
| 21 #include "chrome/browser/sync/internal_api/read_node.h" | 22 #include "chrome/browser/sync/internal_api/read_node.h" |
| 22 #include "chrome/browser/sync/internal_api/read_transaction.h" | 23 #include "chrome/browser/sync/internal_api/read_transaction.h" |
| 23 #include "chrome/browser/sync/internal_api/sync_manager.h" | |
| 24 #include "chrome/browser/sync/internal_api/write_node.h" | 24 #include "chrome/browser/sync/internal_api/write_node.h" |
| 25 #include "chrome/browser/sync/internal_api/write_transaction.h" | 25 #include "chrome/browser/sync/internal_api/write_transaction.h" |
| 26 #include "chrome/browser/sync/profile_sync_test_util.h" | 26 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 27 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" | 27 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
| 28 #include "chrome/browser/sync/syncable/model_type.h" | 28 #include "chrome/browser/sync/syncable/model_type.h" |
| 29 #include "chrome/browser/sync/test_profile_sync_service.h" | 29 #include "chrome/browser/sync/test_profile_sync_service.h" |
| 30 #include "chrome/common/net/gaia/gaia_constants.h" | 30 #include "chrome/common/net/gaia/gaia_constants.h" |
| 31 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 32 #include "chrome/test/base/testing_pref_service.h" | 32 #include "chrome/test/base/testing_pref_service.h" |
| 33 #include "chrome/test/base/testing_profile.h" | 33 #include "chrome/test/base/testing_profile.h" |
| 34 #include "content/common/json_value_serializer.h" | 34 #include "content/common/json_value_serializer.h" |
| 35 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 37 | 37 |
| 38 using base::JSONReader; | 38 using base::JSONReader; |
| 39 using browser_sync::GenericChangeProcessor; | 39 using browser_sync::GenericChangeProcessor; |
| 40 using browser_sync::PreferenceDataTypeController; | 40 using browser_sync::PreferenceDataTypeController; |
| 41 using browser_sync::SyncBackendHost; | 41 using browser_sync::SyncBackendHost; |
| 42 using browser_sync::SyncableServiceAdapter; | 42 using browser_sync::SyncableServiceAdapter; |
| 43 using sync_api::SyncManager; | 43 using sync_api::ChangeRecord; |
| 44 using testing::_; | 44 using testing::_; |
| 45 using testing::Invoke; | 45 using testing::Invoke; |
| 46 using testing::Return; | 46 using testing::Return; |
| 47 | 47 |
| 48 typedef std::map<const std::string, const Value*> PreferenceValues; | 48 typedef std::map<const std::string, const Value*> PreferenceValues; |
| 49 | 49 |
| 50 ACTION_P4(BuildPrefSyncComponents, profile_sync_service, pref_sync_service, | 50 ACTION_P4(BuildPrefSyncComponents, profile_sync_service, pref_sync_service, |
| 51 model_associator_ptr, change_processor_ptr) { | 51 model_associator_ptr, change_processor_ptr) { |
| 52 sync_api::UserShare* user_share = profile_sync_service->GetUserShare(); | 52 sync_api::UserShare* user_share = profile_sync_service->GetUserShare(); |
| 53 *change_processor_ptr = new GenericChangeProcessor(pref_sync_service, | 53 *change_processor_ptr = new GenericChangeProcessor(pref_sync_service, |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 sync_api::WriteNode node(&trans); | 167 sync_api::WriteNode node(&trans); |
| 168 | 168 |
| 169 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name)) | 169 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name)) |
| 170 return WriteSyncedValue(name, value, &tag_node); | 170 return WriteSyncedValue(name, value, &tag_node); |
| 171 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name)) | 171 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name)) |
| 172 return WriteSyncedValue(name, value, &node); | 172 return WriteSyncedValue(name, value, &node); |
| 173 | 173 |
| 174 return sync_api::kInvalidId; | 174 return sync_api::kInvalidId; |
| 175 } | 175 } |
| 176 | 176 |
| 177 SyncManager::ChangeRecord* MakeChangeRecord(int64 node_id, | |
| 178 SyncManager::ChangeRecord::Action action) { | |
| 179 SyncManager::ChangeRecord* record = new SyncManager::ChangeRecord(); | |
| 180 record->action = action; | |
| 181 record->id = node_id; | |
| 182 return record; | |
| 183 } | |
| 184 | |
| 185 bool IsSynced(const std::string& pref_name) { | 177 bool IsSynced(const std::string& pref_name) { |
| 186 return pref_sync_service_->registered_preferences().count(pref_name) > 0; | 178 return pref_sync_service_->registered_preferences().count(pref_name) > 0; |
| 187 } | 179 } |
| 188 | 180 |
| 189 std::string ValueString(const Value& value) { | 181 std::string ValueString(const Value& value) { |
| 190 std::string serialized; | 182 std::string serialized; |
| 191 JSONStringValueSerializer json(&serialized); | 183 JSONStringValueSerializer json(&serialized); |
| 192 json.Serialize(value); | 184 json.Serialize(value); |
| 193 return serialized; | 185 return serialized; |
| 194 } | 186 } |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 | 372 |
| 381 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionUpdate) { | 373 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionUpdate) { |
| 382 profile_->GetPrefs()->SetString(prefs::kHomePage, example_url0_); | 374 profile_->GetPrefs()->SetString(prefs::kHomePage, example_url0_); |
| 383 CreateRootTask task(this, syncable::PREFERENCES); | 375 CreateRootTask task(this, syncable::PREFERENCES); |
| 384 ASSERT_TRUE(StartSyncService(&task, false)); | 376 ASSERT_TRUE(StartSyncService(&task, false)); |
| 385 ASSERT_TRUE(task.success()); | 377 ASSERT_TRUE(task.success()); |
| 386 | 378 |
| 387 scoped_ptr<Value> expected(Value::CreateStringValue(example_url1_)); | 379 scoped_ptr<Value> expected(Value::CreateStringValue(example_url1_)); |
| 388 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); | 380 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); |
| 389 ASSERT_NE(node_id, sync_api::kInvalidId); | 381 ASSERT_NE(node_id, sync_api::kInvalidId); |
| 390 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( | |
| 391 node_id, SyncManager::ChangeRecord::ACTION_UPDATE)); | |
| 392 { | 382 { |
| 393 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); | 383 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); |
| 394 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 384 change_processor_->ApplyChangesFromSyncModel( |
| 385 &trans, |
| 386 ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList( |
| 387 node_id, ChangeRecord::ACTION_UPDATE)); |
| 395 } | 388 } |
| 396 change_processor_->CommitChangesFromSyncModel(); | 389 change_processor_->CommitChangesFromSyncModel(); |
| 397 | 390 |
| 398 const Value& actual = GetPreferenceValue(prefs::kHomePage); | 391 const Value& actual = GetPreferenceValue(prefs::kHomePage); |
| 399 EXPECT_TRUE(expected->Equals(&actual)); | 392 EXPECT_TRUE(expected->Equals(&actual)); |
| 400 } | 393 } |
| 401 | 394 |
| 402 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionAdd) { | 395 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionAdd) { |
| 403 CreateRootTask task(this, syncable::PREFERENCES); | 396 CreateRootTask task(this, syncable::PREFERENCES); |
| 404 ASSERT_TRUE(StartSyncService(&task, false)); | 397 ASSERT_TRUE(StartSyncService(&task, false)); |
| 405 ASSERT_TRUE(task.success()); | 398 ASSERT_TRUE(task.success()); |
| 406 | 399 |
| 407 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); | 400 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); |
| 408 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); | 401 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); |
| 409 ASSERT_NE(node_id, sync_api::kInvalidId); | 402 ASSERT_NE(node_id, sync_api::kInvalidId); |
| 410 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( | |
| 411 node_id, SyncManager::ChangeRecord::ACTION_ADD)); | |
| 412 { | 403 { |
| 413 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); | 404 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); |
| 414 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 405 change_processor_->ApplyChangesFromSyncModel( |
| 406 &trans, |
| 407 ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList( |
| 408 node_id, ChangeRecord::ACTION_ADD)); |
| 415 } | 409 } |
| 416 change_processor_->CommitChangesFromSyncModel(); | 410 change_processor_->CommitChangesFromSyncModel(); |
| 417 | 411 |
| 418 const Value& actual = GetPreferenceValue(prefs::kHomePage); | 412 const Value& actual = GetPreferenceValue(prefs::kHomePage); |
| 419 EXPECT_TRUE(expected->Equals(&actual)); | 413 EXPECT_TRUE(expected->Equals(&actual)); |
| 420 EXPECT_EQ(1U, | 414 EXPECT_EQ(1U, |
| 421 pref_sync_service_->registered_preferences().count(prefs::kHomePage)); | 415 pref_sync_service_->registered_preferences().count(prefs::kHomePage)); |
| 422 } | 416 } |
| 423 | 417 |
| 424 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeUnknownPreference) { | 418 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeUnknownPreference) { |
| 425 CreateRootTask task(this, syncable::PREFERENCES); | 419 CreateRootTask task(this, syncable::PREFERENCES); |
| 426 ASSERT_TRUE(StartSyncService(&task, false)); | 420 ASSERT_TRUE(StartSyncService(&task, false)); |
| 427 ASSERT_TRUE(task.success()); | 421 ASSERT_TRUE(task.success()); |
| 428 | 422 |
| 429 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); | 423 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); |
| 430 int64 node_id = SetSyncedValue("unknown preference", *expected); | 424 int64 node_id = SetSyncedValue("unknown preference", *expected); |
| 431 ASSERT_NE(node_id, sync_api::kInvalidId); | 425 ASSERT_NE(node_id, sync_api::kInvalidId); |
| 432 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( | |
| 433 node_id, SyncManager::ChangeRecord::ACTION_UPDATE)); | |
| 434 { | 426 { |
| 435 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); | 427 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); |
| 436 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 428 change_processor_->ApplyChangesFromSyncModel( |
| 429 &trans, |
| 430 ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList( |
| 431 node_id, ChangeRecord::ACTION_UPDATE)); |
| 437 } | 432 } |
| 438 change_processor_->CommitChangesFromSyncModel(); | 433 change_processor_->CommitChangesFromSyncModel(); |
| 439 | 434 |
| 440 // Nothing interesting happens on the client when it gets an update | 435 // Nothing interesting happens on the client when it gets an update |
| 441 // of an unknown preference. We just should not crash. | 436 // of an unknown preference. We just should not crash. |
| 442 } | 437 } |
| 443 | 438 |
| 444 TEST_F(ProfileSyncServicePreferenceTest, ManagedPreferences) { | 439 TEST_F(ProfileSyncServicePreferenceTest, ManagedPreferences) { |
| 445 // Make the homepage preference managed. | 440 // Make the homepage preference managed. |
| 446 scoped_ptr<Value> managed_value( | 441 scoped_ptr<Value> managed_value( |
| 447 Value::CreateStringValue("http://example.com")); | 442 Value::CreateStringValue("http://example.com")); |
| 448 prefs_->SetManagedPref(prefs::kHomePage, managed_value->DeepCopy()); | 443 prefs_->SetManagedPref(prefs::kHomePage, managed_value->DeepCopy()); |
| 449 | 444 |
| 450 CreateRootTask task(this, syncable::PREFERENCES); | 445 CreateRootTask task(this, syncable::PREFERENCES); |
| 451 ASSERT_TRUE(StartSyncService(&task, false)); | 446 ASSERT_TRUE(StartSyncService(&task, false)); |
| 452 ASSERT_TRUE(task.success()); | 447 ASSERT_TRUE(task.success()); |
| 453 | 448 |
| 454 // Changing the homepage preference should not sync anything. | 449 // Changing the homepage preference should not sync anything. |
| 455 scoped_ptr<Value> user_value( | 450 scoped_ptr<Value> user_value( |
| 456 Value::CreateStringValue("http://chromium..com")); | 451 Value::CreateStringValue("http://chromium..com")); |
| 457 prefs_->SetUserPref(prefs::kHomePage, user_value->DeepCopy()); | 452 prefs_->SetUserPref(prefs::kHomePage, user_value->DeepCopy()); |
| 458 EXPECT_EQ(NULL, GetSyncedValue(prefs::kHomePage)); | 453 EXPECT_EQ(NULL, GetSyncedValue(prefs::kHomePage)); |
| 459 | 454 |
| 460 // An incoming sync transaction should change the user value, not the managed | 455 // An incoming sync transaction should change the user value, not the managed |
| 461 // value. | 456 // value. |
| 462 scoped_ptr<Value> sync_value( | 457 scoped_ptr<Value> sync_value( |
| 463 Value::CreateStringValue("http://crbug.com")); | 458 Value::CreateStringValue("http://crbug.com")); |
| 464 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); | 459 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); |
| 465 ASSERT_NE(node_id, sync_api::kInvalidId); | 460 ASSERT_NE(node_id, sync_api::kInvalidId); |
| 466 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( | |
| 467 node_id, SyncManager::ChangeRecord::ACTION_UPDATE)); | |
| 468 { | 461 { |
| 469 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); | 462 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); |
| 470 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 463 change_processor_->ApplyChangesFromSyncModel( |
| 464 &trans, |
| 465 ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList( |
| 466 node_id, ChangeRecord::ACTION_UPDATE)); |
| 471 } | 467 } |
| 472 change_processor_->CommitChangesFromSyncModel(); | 468 change_processor_->CommitChangesFromSyncModel(); |
| 473 | 469 |
| 474 EXPECT_TRUE(managed_value->Equals(prefs_->GetManagedPref(prefs::kHomePage))); | 470 EXPECT_TRUE(managed_value->Equals(prefs_->GetManagedPref(prefs::kHomePage))); |
| 475 EXPECT_TRUE(sync_value->Equals(prefs_->GetUserPref(prefs::kHomePage))); | 471 EXPECT_TRUE(sync_value->Equals(prefs_->GetUserPref(prefs::kHomePage))); |
| 476 } | 472 } |
| 477 | 473 |
| 478 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) { | 474 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) { |
| 479 CreateRootTask task(this, syncable::PREFERENCES); | 475 CreateRootTask task(this, syncable::PREFERENCES); |
| 480 ASSERT_TRUE(StartSyncService(&task, false)); | 476 ASSERT_TRUE(StartSyncService(&task, false)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 scoped_ptr<Value> managed_value( | 514 scoped_ptr<Value> managed_value( |
| 519 Value::CreateStringValue("http://example.com/managed")); | 515 Value::CreateStringValue("http://example.com/managed")); |
| 520 profile_->GetTestingPrefService()->SetManagedPref( | 516 profile_->GetTestingPrefService()->SetManagedPref( |
| 521 prefs::kHomePage, managed_value->DeepCopy()); | 517 prefs::kHomePage, managed_value->DeepCopy()); |
| 522 | 518 |
| 523 // Change the sync value. | 519 // Change the sync value. |
| 524 scoped_ptr<Value> sync_value( | 520 scoped_ptr<Value> sync_value( |
| 525 Value::CreateStringValue("http://example.com/sync")); | 521 Value::CreateStringValue("http://example.com/sync")); |
| 526 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); | 522 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); |
| 527 ASSERT_NE(node_id, sync_api::kInvalidId); | 523 ASSERT_NE(node_id, sync_api::kInvalidId); |
| 528 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord( | |
| 529 node_id, SyncManager::ChangeRecord::ACTION_ADD)); | |
| 530 { | 524 { |
| 531 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); | 525 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); |
| 532 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 526 change_processor_->ApplyChangesFromSyncModel( |
| 527 &trans, |
| 528 ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList( |
| 529 node_id, ChangeRecord::ACTION_ADD)); |
| 533 } | 530 } |
| 534 change_processor_->CommitChangesFromSyncModel(); | 531 change_processor_->CommitChangesFromSyncModel(); |
| 535 | 532 |
| 536 // The pref value should still be the one dictated by policy. | 533 // The pref value should still be the one dictated by policy. |
| 537 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); | 534 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); |
| 538 | 535 |
| 539 // Switch kHomePage back to unmanaged. | 536 // Switch kHomePage back to unmanaged. |
| 540 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); | 537 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); |
| 541 | 538 |
| 542 // Sync value should be picked up. | 539 // Sync value should be picked up. |
| 543 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); | 540 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); |
| 544 } | 541 } |
| OLD | NEW |