Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1031)

Side by Side Diff: chrome/browser/sync/profile_sync_service_preference_unittest.cc

Issue 7918001: [Sync] Move ChangeRecord into its own file (change_record.{h,cc}) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync to head Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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, 177 sync_api::ImmutableChangeRecordList MakeSingletonChangeRecordList(
Nicolas Zea 2011/09/16 21:20:26 Maybe pull this into test_profile_sync_service.h?
akalin 2011/09/16 21:50:36 Moved into ProfileSyncServiceTestHelper in abstrac
178 SyncManager::ChangeRecord::Action action) { 178 int64 node_id, ChangeRecord::Action action) {
179 SyncManager::ChangeRecord* record = new SyncManager::ChangeRecord(); 179 ChangeRecord record;
180 record->action = action; 180 record.action = action;
181 record->id = node_id; 181 record.id = node_id;
182 return record; 182 sync_api::ChangeRecordList records(1, record);
183 return sync_api::ImmutableChangeRecordList(&records);
183 } 184 }
184 185
185 bool IsSynced(const std::string& pref_name) { 186 bool IsSynced(const std::string& pref_name) {
186 return pref_sync_service_->registered_preferences().count(pref_name) > 0; 187 return pref_sync_service_->registered_preferences().count(pref_name) > 0;
187 } 188 }
188 189
189 std::string ValueString(const Value& value) { 190 std::string ValueString(const Value& value) {
190 std::string serialized; 191 std::string serialized;
191 JSONStringValueSerializer json(&serialized); 192 JSONStringValueSerializer json(&serialized);
192 json.Serialize(value); 193 json.Serialize(value);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 381
381 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionUpdate) { 382 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionUpdate) {
382 profile_->GetPrefs()->SetString(prefs::kHomePage, example_url0_); 383 profile_->GetPrefs()->SetString(prefs::kHomePage, example_url0_);
383 CreateRootTask task(this, syncable::PREFERENCES); 384 CreateRootTask task(this, syncable::PREFERENCES);
384 ASSERT_TRUE(StartSyncService(&task, false)); 385 ASSERT_TRUE(StartSyncService(&task, false));
385 ASSERT_TRUE(task.success()); 386 ASSERT_TRUE(task.success());
386 387
387 scoped_ptr<Value> expected(Value::CreateStringValue(example_url1_)); 388 scoped_ptr<Value> expected(Value::CreateStringValue(example_url1_));
388 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); 389 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected);
389 ASSERT_NE(node_id, sync_api::kInvalidId); 390 ASSERT_NE(node_id, sync_api::kInvalidId);
390 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord(
391 node_id, SyncManager::ChangeRecord::ACTION_UPDATE));
392 { 391 {
393 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); 392 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare());
394 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); 393 change_processor_->ApplyChangesFromSyncModel(
394 &trans,
395 MakeSingletonChangeRecordList(node_id, ChangeRecord::ACTION_UPDATE));
395 } 396 }
396 change_processor_->CommitChangesFromSyncModel(); 397 change_processor_->CommitChangesFromSyncModel();
397 398
398 const Value& actual = GetPreferenceValue(prefs::kHomePage); 399 const Value& actual = GetPreferenceValue(prefs::kHomePage);
399 EXPECT_TRUE(expected->Equals(&actual)); 400 EXPECT_TRUE(expected->Equals(&actual));
400 } 401 }
401 402
402 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionAdd) { 403 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeActionAdd) {
403 CreateRootTask task(this, syncable::PREFERENCES); 404 CreateRootTask task(this, syncable::PREFERENCES);
404 ASSERT_TRUE(StartSyncService(&task, false)); 405 ASSERT_TRUE(StartSyncService(&task, false));
405 ASSERT_TRUE(task.success()); 406 ASSERT_TRUE(task.success());
406 407
407 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); 408 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_));
408 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected); 409 int64 node_id = SetSyncedValue(prefs::kHomePage, *expected);
409 ASSERT_NE(node_id, sync_api::kInvalidId); 410 ASSERT_NE(node_id, sync_api::kInvalidId);
410 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord(
411 node_id, SyncManager::ChangeRecord::ACTION_ADD));
412 { 411 {
413 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); 412 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare());
414 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); 413 change_processor_->ApplyChangesFromSyncModel(
414 &trans,
415 MakeSingletonChangeRecordList(node_id, ChangeRecord::ACTION_ADD));
415 } 416 }
416 change_processor_->CommitChangesFromSyncModel(); 417 change_processor_->CommitChangesFromSyncModel();
417 418
418 const Value& actual = GetPreferenceValue(prefs::kHomePage); 419 const Value& actual = GetPreferenceValue(prefs::kHomePage);
419 EXPECT_TRUE(expected->Equals(&actual)); 420 EXPECT_TRUE(expected->Equals(&actual));
420 EXPECT_EQ(1U, 421 EXPECT_EQ(1U,
421 pref_sync_service_->registered_preferences().count(prefs::kHomePage)); 422 pref_sync_service_->registered_preferences().count(prefs::kHomePage));
422 } 423 }
423 424
424 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeUnknownPreference) { 425 TEST_F(ProfileSyncServicePreferenceTest, UpdatedSyncNodeUnknownPreference) {
425 CreateRootTask task(this, syncable::PREFERENCES); 426 CreateRootTask task(this, syncable::PREFERENCES);
426 ASSERT_TRUE(StartSyncService(&task, false)); 427 ASSERT_TRUE(StartSyncService(&task, false));
427 ASSERT_TRUE(task.success()); 428 ASSERT_TRUE(task.success());
428 429
429 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_)); 430 scoped_ptr<Value> expected(Value::CreateStringValue(example_url0_));
430 int64 node_id = SetSyncedValue("unknown preference", *expected); 431 int64 node_id = SetSyncedValue("unknown preference", *expected);
431 ASSERT_NE(node_id, sync_api::kInvalidId); 432 ASSERT_NE(node_id, sync_api::kInvalidId);
432 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord(
433 node_id, SyncManager::ChangeRecord::ACTION_UPDATE));
434 { 433 {
435 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); 434 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare());
436 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); 435 change_processor_->ApplyChangesFromSyncModel(
436 &trans,
437 MakeSingletonChangeRecordList(node_id, ChangeRecord::ACTION_UPDATE));
437 } 438 }
438 change_processor_->CommitChangesFromSyncModel(); 439 change_processor_->CommitChangesFromSyncModel();
439 440
440 // Nothing interesting happens on the client when it gets an update 441 // Nothing interesting happens on the client when it gets an update
441 // of an unknown preference. We just should not crash. 442 // of an unknown preference. We just should not crash.
442 } 443 }
443 444
444 TEST_F(ProfileSyncServicePreferenceTest, ManagedPreferences) { 445 TEST_F(ProfileSyncServicePreferenceTest, ManagedPreferences) {
445 // Make the homepage preference managed. 446 // Make the homepage preference managed.
446 scoped_ptr<Value> managed_value( 447 scoped_ptr<Value> managed_value(
447 Value::CreateStringValue("http://example.com")); 448 Value::CreateStringValue("http://example.com"));
448 prefs_->SetManagedPref(prefs::kHomePage, managed_value->DeepCopy()); 449 prefs_->SetManagedPref(prefs::kHomePage, managed_value->DeepCopy());
449 450
450 CreateRootTask task(this, syncable::PREFERENCES); 451 CreateRootTask task(this, syncable::PREFERENCES);
451 ASSERT_TRUE(StartSyncService(&task, false)); 452 ASSERT_TRUE(StartSyncService(&task, false));
452 ASSERT_TRUE(task.success()); 453 ASSERT_TRUE(task.success());
453 454
454 // Changing the homepage preference should not sync anything. 455 // Changing the homepage preference should not sync anything.
455 scoped_ptr<Value> user_value( 456 scoped_ptr<Value> user_value(
456 Value::CreateStringValue("http://chromium..com")); 457 Value::CreateStringValue("http://chromium..com"));
457 prefs_->SetUserPref(prefs::kHomePage, user_value->DeepCopy()); 458 prefs_->SetUserPref(prefs::kHomePage, user_value->DeepCopy());
458 EXPECT_EQ(NULL, GetSyncedValue(prefs::kHomePage)); 459 EXPECT_EQ(NULL, GetSyncedValue(prefs::kHomePage));
459 460
460 // An incoming sync transaction should change the user value, not the managed 461 // An incoming sync transaction should change the user value, not the managed
461 // value. 462 // value.
462 scoped_ptr<Value> sync_value( 463 scoped_ptr<Value> sync_value(
463 Value::CreateStringValue("http://crbug.com")); 464 Value::CreateStringValue("http://crbug.com"));
464 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); 465 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value);
465 ASSERT_NE(node_id, sync_api::kInvalidId); 466 ASSERT_NE(node_id, sync_api::kInvalidId);
466 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord(
467 node_id, SyncManager::ChangeRecord::ACTION_UPDATE));
468 { 467 {
469 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); 468 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare());
470 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); 469 change_processor_->ApplyChangesFromSyncModel(
470 &trans,
471 MakeSingletonChangeRecordList(node_id, ChangeRecord::ACTION_UPDATE));
471 } 472 }
472 change_processor_->CommitChangesFromSyncModel(); 473 change_processor_->CommitChangesFromSyncModel();
473 474
474 EXPECT_TRUE(managed_value->Equals(prefs_->GetManagedPref(prefs::kHomePage))); 475 EXPECT_TRUE(managed_value->Equals(prefs_->GetManagedPref(prefs::kHomePage)));
475 EXPECT_TRUE(sync_value->Equals(prefs_->GetUserPref(prefs::kHomePage))); 476 EXPECT_TRUE(sync_value->Equals(prefs_->GetUserPref(prefs::kHomePage)));
476 } 477 }
477 478
478 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) { 479 TEST_F(ProfileSyncServicePreferenceTest, DynamicManagedPreferences) {
479 CreateRootTask task(this, syncable::PREFERENCES); 480 CreateRootTask task(this, syncable::PREFERENCES);
480 ASSERT_TRUE(StartSyncService(&task, false)); 481 ASSERT_TRUE(StartSyncService(&task, false));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 scoped_ptr<Value> managed_value( 519 scoped_ptr<Value> managed_value(
519 Value::CreateStringValue("http://example.com/managed")); 520 Value::CreateStringValue("http://example.com/managed"));
520 profile_->GetTestingPrefService()->SetManagedPref( 521 profile_->GetTestingPrefService()->SetManagedPref(
521 prefs::kHomePage, managed_value->DeepCopy()); 522 prefs::kHomePage, managed_value->DeepCopy());
522 523
523 // Change the sync value. 524 // Change the sync value.
524 scoped_ptr<Value> sync_value( 525 scoped_ptr<Value> sync_value(
525 Value::CreateStringValue("http://example.com/sync")); 526 Value::CreateStringValue("http://example.com/sync"));
526 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value); 527 int64 node_id = SetSyncedValue(prefs::kHomePage, *sync_value);
527 ASSERT_NE(node_id, sync_api::kInvalidId); 528 ASSERT_NE(node_id, sync_api::kInvalidId);
528 scoped_ptr<SyncManager::ChangeRecord> record(MakeChangeRecord(
529 node_id, SyncManager::ChangeRecord::ACTION_ADD));
530 { 529 {
531 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare()); 530 sync_api::WriteTransaction trans(FROM_HERE, service_->GetUserShare());
532 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1); 531 change_processor_->ApplyChangesFromSyncModel(
532 &trans,
533 MakeSingletonChangeRecordList(node_id, ChangeRecord::ACTION_ADD));
533 } 534 }
534 change_processor_->CommitChangesFromSyncModel(); 535 change_processor_->CommitChangesFromSyncModel();
535 536
536 // The pref value should still be the one dictated by policy. 537 // The pref value should still be the one dictated by policy.
537 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); 538 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage)));
538 539
539 // Switch kHomePage back to unmanaged. 540 // Switch kHomePage back to unmanaged.
540 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); 541 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage);
541 542
542 // Sync value should be picked up. 543 // Sync value should be picked up.
543 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); 544 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage)));
544 } 545 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698