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

Side by Side Diff: chrome/browser/sync/glue/device_info_sync_service_unittest.cc

Issue 566623003: Refactor syncable DEVICE_INFO type from ChangeProcessor to SyncableService - part 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More CR feedback addressed in DeviceInfoSyncService. Created 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "base/message_loop/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 #include "chrome/browser/sync/glue/device_info_sync_service.h" 6 #include "chrome/browser/sync/glue/device_info_sync_service.h"
7 #include "chrome/browser/sync/glue/local_device_info_provider_mock.h" 7 #include "chrome/browser/sync/glue/local_device_info_provider_mock.h"
8 #include "content/public/test/test_browser_thread_bundle.h" 8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "sync/api/sync_change.h" 9 #include "sync/api/sync_change.h"
10 #include "sync/api/sync_change_processor.h" 10 #include "sync/api/sync_change_processor.h"
11 #include "sync/api/sync_change_processor_wrapper_for_test.h" 11 #include "sync/api/sync_change_processor_wrapper_for_test.h"
12 #include "sync/api/sync_error_factory_mock.h" 12 #include "sync/api/sync_error_factory_mock.h"
13 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test .h" 13 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test .h"
14 #include "sync/util/time.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 using syncer::AttachmentIdList; 17 using syncer::AttachmentIdList;
17 using syncer::AttachmentServiceProxyForTest; 18 using syncer::AttachmentServiceProxyForTest;
18 using syncer::ModelType; 19 using syncer::ModelType;
19 using syncer::SyncChange; 20 using syncer::SyncChange;
20 using syncer::SyncChangeList; 21 using syncer::SyncChangeList;
21 using syncer::SyncChangeProcessor; 22 using syncer::SyncChangeProcessor;
22 using syncer::SyncChangeProcessorWrapperForTest; 23 using syncer::SyncChangeProcessorWrapperForTest;
23 using syncer::SyncData; 24 using syncer::SyncData;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 scoped_ptr<SyncChangeProcessor> PassProcessor() { 106 scoped_ptr<SyncChangeProcessor> PassProcessor() {
106 return scoped_ptr<SyncChangeProcessor>( 107 return scoped_ptr<SyncChangeProcessor>(
107 new SyncChangeProcessorWrapperForTest(sync_processor_.get())); 108 new SyncChangeProcessorWrapperForTest(sync_processor_.get()));
108 } 109 }
109 110
110 scoped_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory() { 111 scoped_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory() {
111 return scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock()); 112 return scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock());
112 } 113 }
113 114
114 SyncData CreateRemoteData(const std::string& client_id, 115 SyncData CreateRemoteData(const std::string& client_id,
115 const std::string& client_name) { 116 const std::string& client_name,
117 int64 backup_timestamp = 0) {
116 sync_pb::EntitySpecifics entity; 118 sync_pb::EntitySpecifics entity;
117 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); 119 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info();
118 120
119 specifics.set_cache_guid(client_id); 121 specifics.set_cache_guid(client_id);
120 specifics.set_client_name(client_name); 122 specifics.set_client_name(client_name);
121 specifics.set_chrome_version("Chromium 10k"); 123 specifics.set_chrome_version("Chromium 10k");
122 specifics.set_sync_user_agent("Chrome 10k"); 124 specifics.set_sync_user_agent("Chrome 10k");
123 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX); 125 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
124 specifics.set_signin_scoped_device_id("device_id"); 126 specifics.set_signin_scoped_device_id("device_id");
125 127
128 if (backup_timestamp != 0) {
129 specifics.set_backup_timestamp(backup_timestamp);
130 }
131
126 return SyncData::CreateRemoteData(1, 132 return SyncData::CreateRemoteData(1,
127 entity, 133 entity,
128 base::Time(), 134 base::Time(),
129 AttachmentIdList(), 135 AttachmentIdList(),
130 AttachmentServiceProxyForTest::Create()); 136 AttachmentServiceProxyForTest::Create());
131 } 137 }
132 138
133 void AddInitialData(SyncDataList& sync_data_list, 139 void AddInitialData(SyncDataList& sync_data_list,
134 const std::string& client_id, 140 const std::string& client_id,
135 const std::string& client_name) { 141 const std::string& client_name) {
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // Unsubscribe observer before processing changes. 399 // Unsubscribe observer before processing changes.
394 sync_service_->RemoveObserver(this); 400 sync_service_->RemoveObserver(this);
395 401
396 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 402 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
397 EXPECT_FALSE(error.IsSet()); 403 EXPECT_FALSE(error.IsSet());
398 404
399 // The number of callback should still be zero. 405 // The number of callback should still be zero.
400 EXPECT_EQ(0, num_device_info_changed_callbacks_); 406 EXPECT_EQ(0, num_device_info_changed_callbacks_);
401 } 407 }
402 408
409 // Verifies setting backup timestamp after the initial sync.
410 TEST_F(DeviceInfoSyncServiceTest, UpdateLocalDeviceBackupTime) {
411 // Shouldn't have backuptime initially.
412 base::Time backup_time = sync_service_->GetLocalDeviceBackupTime();
413 EXPECT_TRUE(backup_time.is_null());
414
415 // Perform the initial sync with empty data.
416 SyncMergeResult merge_result =
417 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
418 SyncDataList(),
419 PassProcessor(),
420 CreateAndPassSyncErrorFactory());
421
422 // Should have local device after the initial sync.
423 EXPECT_EQ(1U, sync_processor_->change_list_size());
424 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0));
425
426 // Shouldn't have backup time initially.
427 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
428 EXPECT_FALSE(sync_processor_->device_info_at(0).has_backup_timestamp());
429
430 sync_service_->UpdateLocalDeviceBackupTime(base::Time::FromTimeT(1000));
431
432 // Should have local device info updated with the specified backup timestamp.
433 EXPECT_EQ(1U, sync_processor_->change_list_size());
434 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
435 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
436 EXPECT_TRUE(sync_processor_->device_info_at(0).has_backup_timestamp());
437
438 backup_time = syncer::ProtoTimeToTime(
439 sync_processor_->device_info_at(0).backup_timestamp());
440 EXPECT_EQ(1000, backup_time.ToTimeT());
441
442 // Also verify that we get the same backup time directly from the service.
443 backup_time = sync_service_->GetLocalDeviceBackupTime();
444 EXPECT_EQ(1000, backup_time.ToTimeT());
445 }
446
447 // Verifies setting backup timestamp prior to the initial sync.
448 TEST_F(DeviceInfoSyncServiceTest, UpdateLocalDeviceBackupTimeBeforeSync) {
449 // Set the backup timestamp.
450 sync_service_->UpdateLocalDeviceBackupTime(base::Time::FromTimeT(2000));
451 // Verify that we get it back.
452 base::Time backup_time = sync_service_->GetLocalDeviceBackupTime();
453 EXPECT_EQ(2000, backup_time.ToTimeT());
454
455 // Now perform the initial sync with empty data.
456 SyncMergeResult merge_result =
457 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
458 SyncDataList(),
459 PassProcessor(),
460 CreateAndPassSyncErrorFactory());
461
462 // Should have local device after the initial sync.
463 // Should have the backup timestamp set.
464 EXPECT_EQ(1U, sync_processor_->change_list_size());
465 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0));
466 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
467 EXPECT_TRUE(sync_processor_->device_info_at(0).has_backup_timestamp());
468
469 backup_time = syncer::ProtoTimeToTime(
470 sync_processor_->device_info_at(0).backup_timestamp());
471 EXPECT_EQ(2000, backup_time.ToTimeT());
472 }
473
474 // Verifies that the backup timestamp that comes in the intial sync data
475 // gets preserved when there are no changes to the local device.
476 TEST_F(DeviceInfoSyncServiceTest, PreserveBackupTimeWithMatchingLocalDevice) {
477 base::Time backup_time = base::Time::FromTimeT(3000);
478 SyncDataList sync_data;
479 sync_data.push_back(CreateRemoteData(
480 "guid_1", "client_1", syncer::TimeToProtoTime(backup_time)));
481
482 SyncMergeResult merge_result =
483 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
484 sync_data,
485 PassProcessor(),
486 CreateAndPassSyncErrorFactory());
487
488 // Everything is matching so there should be no updates.
489 EXPECT_EQ(0U, sync_processor_->change_list_size());
490
491 // Verify that we get back the same time.
492 backup_time = sync_service_->GetLocalDeviceBackupTime();
493 EXPECT_EQ(3000, backup_time.ToTimeT());
494 }
495
496 // Verifies that the backup timestamp that comes in the intial sync data
497 // gets merged with the local device data.
498 TEST_F(DeviceInfoSyncServiceTest, MergeBackupTimeWithMatchingLocalDevice) {
499 base::Time backup_time = base::Time::FromTimeT(4000);
500 SyncDataList sync_data;
501 sync_data.push_back(CreateRemoteData(
502 "guid_1", "foo_1", syncer::TimeToProtoTime(backup_time)));
503
504 SyncMergeResult merge_result =
505 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
506 sync_data,
507 PassProcessor(),
508 CreateAndPassSyncErrorFactory());
509
510 // Should be one change because of the client name mismatch.
511 // However the backup time passed in the initial data should be merged into
512 // the change.
513 EXPECT_EQ(1U, sync_processor_->change_list_size());
514
515 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
516 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
517 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
518
519 backup_time = syncer::ProtoTimeToTime(
520 sync_processor_->device_info_at(0).backup_timestamp());
521 EXPECT_EQ(4000, backup_time.ToTimeT());
522 }
523
524 // Verifies that mismatching backup timestamp generates an update even
525 // when the rest of local device data is matching.
526 TEST_F(DeviceInfoSyncServiceTest,
527 MergeMismatchingBackupTimeWithMatchingLocalDevice) {
528 base::Time backup_time = base::Time::FromTimeT(5000);
529 SyncDataList sync_data;
530 sync_data.push_back(CreateRemoteData(
531 "guid_1", "client_1", syncer::TimeToProtoTime(backup_time)));
532
533 // Set the backup timestamp different than the one in the sync data.
534 sync_service_->UpdateLocalDeviceBackupTime(base::Time::FromTimeT(6000));
535
536 SyncMergeResult merge_result =
537 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
538 sync_data,
539 PassProcessor(),
540 CreateAndPassSyncErrorFactory());
541
542 // Should generate and update due to timestamp mismatch.
543 // The locally set timestamp wins.
544 EXPECT_EQ(1U, sync_processor_->change_list_size());
545
546 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
547 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
548 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
549
550 backup_time = syncer::ProtoTimeToTime(
551 sync_processor_->device_info_at(0).backup_timestamp());
552 EXPECT_EQ(6000, backup_time.ToTimeT());
553 }
554
403 } // namespace 555 } // namespace
404 556
405 } // namespace browser_sync 557 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/device_info_sync_service.cc ('k') | chrome/browser/sync/glue/sync_backend_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698