| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/task.h" | 11 #include "base/task.h" |
| 11 #include "base/utf_string_conversions.h" // TODO(viettrungluu): remove | |
| 12 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" | 12 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 13 #include "chrome/browser/sync/engine/syncapi.h" | 13 #include "chrome/browser/sync/engine/syncapi.h" |
| 14 #include "chrome/browser/sync/glue/preference_change_processor.h" | 14 #include "chrome/browser/sync/glue/preference_change_processor.h" |
| 15 #include "chrome/browser/sync/glue/preference_data_type_controller.h" | 15 #include "chrome/browser/sync/glue/preference_data_type_controller.h" |
| 16 #include "chrome/browser/sync/glue/preference_model_associator.h" | 16 #include "chrome/browser/sync/glue/preference_model_associator.h" |
| 17 #include "chrome/browser/sync/glue/sync_backend_host.h" | 17 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| 18 #include "chrome/browser/sync/profile_sync_test_util.h" | 18 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 19 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" | 19 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
| 20 #include "chrome/browser/sync/syncable/model_type.h" | 20 #include "chrome/browser/sync/syncable/model_type.h" |
| 21 #include "chrome/browser/sync/test_profile_sync_service.h" | 21 #include "chrome/browser/sync/test_profile_sync_service.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 if (!node.InitByIdLookup(node_id)) | 110 if (!node.InitByIdLookup(node_id)) |
| 111 return NULL; | 111 return NULL; |
| 112 | 112 |
| 113 const sync_pb::PreferenceSpecifics& specifics( | 113 const sync_pb::PreferenceSpecifics& specifics( |
| 114 node.GetPreferenceSpecifics()); | 114 node.GetPreferenceSpecifics()); |
| 115 | 115 |
| 116 JSONReader reader; | 116 JSONReader reader; |
| 117 return reader.JsonToValue(specifics.value(), false, false); | 117 return reader.JsonToValue(specifics.value(), false, false); |
| 118 } | 118 } |
| 119 | 119 |
| 120 int64 WriteSyncedValue(sync_api::WriteNode& node, |
| 121 const std::string& name, |
| 122 const Value& value) { |
| 123 if (!PreferenceModelAssociator::WritePreferenceToNode(name, value, &node)) |
| 124 return sync_api::kInvalidId; |
| 125 return node.GetId(); |
| 126 } |
| 127 |
| 120 int64 SetSyncedValue(const std::string& name, const Value& value) { | 128 int64 SetSyncedValue(const std::string& name, const Value& value) { |
| 121 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); | 129 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); |
| 122 sync_api::ReadNode root(&trans); | 130 sync_api::ReadNode root(&trans); |
| 123 if (!root.InitByTagLookup(browser_sync::kPreferencesTag)) | 131 if (!root.InitByTagLookup(browser_sync::kPreferencesTag)) |
| 124 return sync_api::kInvalidId; | 132 return sync_api::kInvalidId; |
| 125 | 133 |
| 134 sync_api::WriteNode tag_node(&trans); |
| 126 sync_api::WriteNode node(&trans); | 135 sync_api::WriteNode node(&trans); |
| 127 | 136 |
| 128 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); | 137 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); |
| 129 if (node_id == sync_api::kInvalidId) { | 138 if (node_id == sync_api::kInvalidId) { |
| 130 if (!node.InitUniqueByCreation(syncable::PREFERENCES, | 139 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name)) |
| 131 root, | 140 return WriteSyncedValue(tag_node, name, value); |
| 132 name)) { | 141 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name)) |
| 133 return sync_api::kInvalidId; | 142 return WriteSyncedValue(node, name, value); |
| 134 } | 143 } else if (node.InitByIdLookup(node_id)) { |
| 135 } else { | 144 return WriteSyncedValue(node, name, value); |
| 136 if (!node.InitByIdLookup(node_id)) { | |
| 137 return sync_api::kInvalidId; | |
| 138 } | |
| 139 } | 145 } |
| 140 | 146 return sync_api::kInvalidId; |
| 141 std::string serialized; | |
| 142 JSONStringValueSerializer json(&serialized); | |
| 143 EXPECT_TRUE(json.Serialize(value)); | |
| 144 | |
| 145 sync_pb::PreferenceSpecifics preference; | |
| 146 preference.set_name(name); | |
| 147 preference.set_value(serialized); | |
| 148 node.SetPreferenceSpecifics(preference); | |
| 149 // TODO(viettrungluu): remove conversion and header | |
| 150 node.SetTitle(UTF8ToWide(name)); | |
| 151 | |
| 152 return node.GetId(); | |
| 153 } | 147 } |
| 154 | 148 |
| 155 SyncManager::ChangeRecord* MakeChangeRecord(const std::string& name, | 149 SyncManager::ChangeRecord* MakeChangeRecord(const std::string& name, |
| 156 SyncManager::ChangeRecord) { | 150 SyncManager::ChangeRecord) { |
| 157 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); | 151 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); |
| 158 SyncManager::ChangeRecord* record = new SyncManager::ChangeRecord(); | 152 SyncManager::ChangeRecord* record = new SyncManager::ChangeRecord(); |
| 159 record->action = SyncManager::ChangeRecord::ACTION_UPDATE; | 153 record->action = SyncManager::ChangeRecord::ACTION_UPDATE; |
| 160 record->id = node_id; | 154 record->id = node_id; |
| 161 return record; | 155 return record; |
| 162 } | 156 } |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 record->id = node_id; | 434 record->id = node_id; |
| 441 { | 435 { |
| 442 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); | 436 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); |
| 443 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); | 437 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
| 444 } | 438 } |
| 445 EXPECT_TRUE(managed_value->Equals( | 439 EXPECT_TRUE(managed_value->Equals( |
| 446 prefs_->GetManagedPref(prefs::kHomePage))); | 440 prefs_->GetManagedPref(prefs::kHomePage))); |
| 447 EXPECT_TRUE(user_value->Equals( | 441 EXPECT_TRUE(user_value->Equals( |
| 448 prefs_->GetUserPref(prefs::kHomePage))); | 442 prefs_->GetUserPref(prefs::kHomePage))); |
| 449 } | 443 } |
| 444 |
| 445 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) { |
| 446 CreateRootTask task(this, syncable::PREFERENCES); |
| 447 ASSERT_TRUE(StartSyncService(&task, false)); |
| 448 ASSERT_TRUE(task.success()); |
| 449 |
| 450 scoped_ptr<Value> initial_value( |
| 451 Value::CreateStringValue("http://example.com/initial")); |
| 452 profile_->GetPrefs()->Set(prefs::kHomePage, *initial_value); |
| 453 scoped_ptr<const Value> actual(GetSyncedValue(prefs::kHomePage)); |
| 454 EXPECT_TRUE(initial_value->Equals(actual.get())); |
| 455 |
| 456 // Switch kHomePage to managed and set a different value. |
| 457 scoped_ptr<Value> managed_value( |
| 458 Value::CreateStringValue("http://example.com/managed")); |
| 459 profile_->GetPrefs()->SetManagedPref(prefs::kHomePage, |
| 460 managed_value->DeepCopy()); |
| 461 |
| 462 // Sync node should be gone. |
| 463 EXPECT_EQ(sync_api::kInvalidId, |
| 464 model_associator_->GetSyncIdFromChromeId(prefs::kHomePage)); |
| 465 |
| 466 // Change the sync value. |
| 467 scoped_ptr<Value> sync_value( |
| 468 Value::CreateStringValue("http://example.com/sync")); |
| 469 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); |
| 470 ASSERT_NE(node_id, sync_api::kInvalidId); |
| 471 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord); |
| 472 record->action = SyncManager::ChangeRecord::ACTION_ADD; |
| 473 record->id = node_id; |
| 474 { |
| 475 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); |
| 476 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); |
| 477 } |
| 478 |
| 479 // The pref value should still be the one dictated by policy. |
| 480 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); |
| 481 |
| 482 // Switch kHomePage back to unmanaged. |
| 483 profile_->GetPrefs()->RemoveManagedPref(prefs::kHomePage); |
| 484 |
| 485 // Sync value should be picked up. |
| 486 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); |
| 487 } |
| OLD | NEW |