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
3 // found in the LICENSE file.
5 #include "base/message_loop/message_loop.h"
6 #include "chrome/browser/sync/glue/local_device_info_provider_mock.h"
7 #include "components/sync_driver/device_info_sync_service.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "sync/api/sync_change.h"
10 #include "sync/api/sync_change_processor.h"
11 #include "sync/api/sync_change_processor_wrapper_for_test.h"
12 #include "sync/api/sync_error_factory_mock.h"
13 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
14 #include "sync/util/time.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using sync_driver::DeviceInfoSyncService;
18 using sync_driver::DeviceInfoTracker;
19 using syncer::AttachmentIdList;
20 using syncer::AttachmentServiceProxyForTest;
21 using syncer::ModelType;
22 using syncer::SyncChange;
23 using syncer::SyncChangeList;
24 using syncer::SyncChangeProcessor;
25 using syncer::SyncChangeProcessorWrapperForTest;
26 using syncer::SyncData;
27 using syncer::SyncDataList;
28 using syncer::SyncError;
29 using syncer::SyncErrorFactory;
30 using syncer::SyncErrorFactoryMock;
31 using syncer::SyncMergeResult;
33 namespace browser_sync {
37 class TestChangeProcessor : public SyncChangeProcessor {
39 TestChangeProcessor() {}
40 virtual ~TestChangeProcessor() {}
42 // SyncChangeProcessor implementation.
43 // Store a copy of all the changes passed in so we can examine them later.
44 virtual SyncError ProcessSyncChanges(
45 const tracked_objects::Location& from_here,
46 const SyncChangeList& change_list) OVERRIDE {
47 change_list_ = change_list;
51 // This method isn't used in these tests.
52 virtual SyncDataList GetAllSyncData(ModelType type) const OVERRIDE {
53 return SyncDataList();
56 size_t change_list_size() const { return change_list_.size(); }
58 SyncChange::SyncChangeType change_type_at(size_t index) const {
59 CHECK_LT(index, change_list_size());
60 return change_list_[index].change_type();
63 const sync_pb::DeviceInfoSpecifics& device_info_at(size_t index) const {
64 CHECK_LT(index, change_list_size());
65 return change_list_[index].sync_data().GetSpecifics().device_info();
68 const std::string& cache_guid_at(size_t index) const {
69 return device_info_at(index).cache_guid();
72 const std::string& client_name_at(size_t index) const {
73 return device_info_at(index).client_name();
77 SyncChangeList change_list_;
80 class DeviceInfoSyncServiceTest : public testing::Test,
81 public DeviceInfoTracker::Observer {
83 DeviceInfoSyncServiceTest() : num_device_info_changed_callbacks_(0) {}
84 virtual ~DeviceInfoSyncServiceTest() {}
86 virtual void SetUp() OVERRIDE {
87 local_device_.reset(new LocalDeviceInfoProviderMock(
92 sync_pb::SyncEnums_DeviceType_TYPE_LINUX,
94 sync_service_.reset(new DeviceInfoSyncService(local_device_.get()));
95 sync_processor_.reset(new TestChangeProcessor());
97 sync_service_->AddObserver(this);
100 virtual void TearDown() OVERRIDE {
101 sync_service_->RemoveObserver(this);
104 virtual void OnDeviceInfoChange() OVERRIDE {
105 num_device_info_changed_callbacks_++;
108 scoped_ptr<SyncChangeProcessor> PassProcessor() {
109 return scoped_ptr<SyncChangeProcessor>(
110 new SyncChangeProcessorWrapperForTest(sync_processor_.get()));
113 scoped_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory() {
114 return scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock());
117 SyncData CreateRemoteData(const std::string& client_id,
118 const std::string& client_name,
119 int64 backup_timestamp = 0) {
120 sync_pb::EntitySpecifics entity;
121 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info();
123 specifics.set_cache_guid(client_id);
124 specifics.set_client_name(client_name);
125 specifics.set_chrome_version("Chromium 10k");
126 specifics.set_sync_user_agent("Chrome 10k");
127 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
128 specifics.set_signin_scoped_device_id("device_id");
130 if (backup_timestamp != 0) {
131 specifics.set_backup_timestamp(backup_timestamp);
134 return SyncData::CreateRemoteData(1,
138 AttachmentServiceProxyForTest::Create());
141 void AddInitialData(SyncDataList& sync_data_list,
142 const std::string& client_id,
143 const std::string& client_name) {
144 SyncData sync_data = CreateRemoteData(client_id, client_name);
145 sync_data_list.push_back(sync_data);
148 void AddChange(SyncChangeList& change_list,
149 SyncChange::SyncChangeType change_type,
150 const std::string& client_id,
151 const std::string& client_name) {
152 SyncData sync_data = CreateRemoteData(client_id, client_name);
153 SyncChange sync_change(FROM_HERE, change_type, sync_data);
154 change_list.push_back(sync_change);
158 int num_device_info_changed_callbacks_;
159 scoped_ptr<LocalDeviceInfoProviderMock> local_device_;
160 scoped_ptr<DeviceInfoSyncService> sync_service_;
161 scoped_ptr<TestChangeProcessor> sync_processor_;
162 content::TestBrowserThreadBundle thread_bundle_;
165 // Sync with empty initial data.
166 TEST_F(DeviceInfoSyncServiceTest, StartSyncEmptyInitialData) {
167 SyncMergeResult merge_result =
168 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
171 CreateAndPassSyncErrorFactory());
173 EXPECT_EQ(0, merge_result.num_items_added());
174 EXPECT_EQ(0, merge_result.num_items_modified());
175 EXPECT_EQ(0, merge_result.num_items_deleted());
176 EXPECT_EQ(1, merge_result.num_items_before_association());
177 EXPECT_EQ(1, merge_result.num_items_after_association());
178 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0));
180 EXPECT_EQ(1U, sync_processor_->change_list_size());
181 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
183 // Should have one device info corresponding to local device info.
184 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size());
185 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
186 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
187 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
190 // Sync with initial data matching the local device data.
191 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingInitialData) {
192 SyncDataList sync_data;
193 AddInitialData(sync_data, "guid_1", "client_1");
195 SyncMergeResult merge_result =
196 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
199 CreateAndPassSyncErrorFactory());
200 EXPECT_EQ(0, merge_result.num_items_added());
201 EXPECT_EQ(0, merge_result.num_items_modified());
202 EXPECT_EQ(0, merge_result.num_items_deleted());
203 EXPECT_EQ(1, merge_result.num_items_before_association());
204 EXPECT_EQ(1, merge_result.num_items_after_association());
206 // No changes expected because the device info matches.
207 EXPECT_EQ(0U, sync_processor_->change_list_size());
209 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size());
210 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
211 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
212 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
215 // Sync with misc initial data.
216 TEST_F(DeviceInfoSyncServiceTest, StartSync) {
217 SyncDataList sync_data;
218 AddInitialData(sync_data, "guid_2", "foo");
219 AddInitialData(sync_data, "guid_3", "bar");
220 // This guid matches the local device but the client name is different.
221 AddInitialData(sync_data, "guid_1", "baz");
223 SyncMergeResult merge_result =
224 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
227 CreateAndPassSyncErrorFactory());
229 EXPECT_EQ(2, merge_result.num_items_added());
230 EXPECT_EQ(1, merge_result.num_items_modified());
231 EXPECT_EQ(0, merge_result.num_items_deleted());
232 EXPECT_EQ(1, merge_result.num_items_before_association());
233 EXPECT_EQ(3, merge_result.num_items_after_association());
235 EXPECT_EQ(1U, sync_processor_->change_list_size());
236 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
237 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
239 EXPECT_EQ(3U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size());
240 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size());
241 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
242 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2"));
243 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_3"));
244 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
247 // Process sync change with ACTION_ADD.
249 TEST_F(DeviceInfoSyncServiceTest, ProcessAddChange) {
250 EXPECT_EQ(0, num_device_info_changed_callbacks_);
252 // Start with an empty initial data.
253 SyncMergeResult merge_result =
254 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
257 CreateAndPassSyncErrorFactory());
258 // There should be only one item corresponding to the local device
259 EXPECT_EQ(1, merge_result.num_items_after_association());
260 EXPECT_EQ(1, num_device_info_changed_callbacks_);
262 // Add a new device info with a non-matching guid.
263 SyncChangeList change_list;
264 AddChange(change_list, SyncChange::ACTION_ADD, "guid_2", "foo");
266 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
267 EXPECT_FALSE(error.IsSet());
268 EXPECT_EQ(2, num_device_info_changed_callbacks_);
270 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size());
272 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
273 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2"));
274 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
277 // Process multiple sync change with ACTION_UPDATE and ACTION_ADD.
278 // Verify that callback is called multiple times.
279 TEST_F(DeviceInfoSyncServiceTest, ProcessMultipleChanges) {
280 SyncDataList sync_data;
281 AddInitialData(sync_data, "guid_2", "foo");
282 AddInitialData(sync_data, "guid_3", "bar");
284 SyncMergeResult merge_result =
285 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
288 CreateAndPassSyncErrorFactory());
289 EXPECT_EQ(3, merge_result.num_items_after_association());
290 // reset callbacks counter
291 num_device_info_changed_callbacks_ = 0;
293 SyncChangeList change_list;
294 AddChange(change_list, SyncChange::ACTION_UPDATE, "guid_2", "foo_2");
296 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
297 EXPECT_FALSE(error.IsSet());
299 EXPECT_EQ(1, num_device_info_changed_callbacks_);
300 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size());
301 EXPECT_EQ("foo_2", sync_service_->GetDeviceInfo("guid_2")->client_name());
304 AddChange(change_list, SyncChange::ACTION_UPDATE, "guid_3", "bar_3");
305 AddChange(change_list, SyncChange::ACTION_ADD, "guid_4", "baz_4");
307 error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
308 EXPECT_FALSE(error.IsSet());
310 EXPECT_EQ(2, num_device_info_changed_callbacks_);
311 EXPECT_EQ(4U, sync_service_->GetAllDeviceInfo().size());
312 EXPECT_EQ("bar_3", sync_service_->GetDeviceInfo("guid_3")->client_name());
313 EXPECT_EQ("baz_4", sync_service_->GetDeviceInfo("guid_4")->client_name());
316 // Process update to the local device info and verify that it is ignored.
317 TEST_F(DeviceInfoSyncServiceTest, ProcessUpdateChangeMatchingLocalDevice) {
318 SyncMergeResult merge_result =
319 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
322 CreateAndPassSyncErrorFactory());
323 EXPECT_EQ(1, merge_result.num_items_after_association());
324 // reset callbacks counter
325 num_device_info_changed_callbacks_ = 0;
327 SyncChangeList change_list;
328 AddChange(change_list, SyncChange::ACTION_UPDATE, "guid_1", "foo_1");
330 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
331 EXPECT_FALSE(error.IsSet());
332 // Callback shouldn't be sent in this case.
333 EXPECT_EQ(0, num_device_info_changed_callbacks_);
334 // Should still have the old local device Info.
335 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
336 EXPECT_EQ("client_1", sync_service_->GetDeviceInfo("guid_1")->client_name());
339 // Process sync change with ACTION_DELETE.
340 TEST_F(DeviceInfoSyncServiceTest, ProcessDeleteChange) {
341 SyncDataList sync_data;
342 AddInitialData(sync_data, "guid_2", "foo");
343 AddInitialData(sync_data, "guid_3", "bar");
345 SyncMergeResult merge_result =
346 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
349 CreateAndPassSyncErrorFactory());
350 EXPECT_EQ(3, merge_result.num_items_after_association());
351 // reset callbacks counter
352 num_device_info_changed_callbacks_ = 0;
354 SyncChangeList change_list;
355 AddChange(change_list, SyncChange::ACTION_DELETE, "guid_2", "foo_2");
357 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
358 EXPECT_FALSE(error.IsSet());
360 EXPECT_EQ(1, num_device_info_changed_callbacks_);
361 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size());
362 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_2"));
365 // Process sync change with unexpected action.
366 TEST_F(DeviceInfoSyncServiceTest, ProcessInvalidChange) {
367 SyncMergeResult merge_result =
368 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
371 CreateAndPassSyncErrorFactory());
372 EXPECT_EQ(1, merge_result.num_items_after_association());
373 // reset callbacks counter
374 num_device_info_changed_callbacks_ = 0;
376 SyncChangeList change_list;
377 AddChange(change_list, (SyncChange::SyncChangeType)100, "guid_2", "foo_2");
379 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
380 EXPECT_TRUE(error.IsSet());
382 // The number of callback should still be zero.
383 EXPECT_EQ(0, num_device_info_changed_callbacks_);
384 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
387 // Process sync change after unsubscribing from notifications.
388 TEST_F(DeviceInfoSyncServiceTest, ProcessChangesAfterUnsubscribing) {
389 SyncMergeResult merge_result =
390 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
393 CreateAndPassSyncErrorFactory());
394 EXPECT_EQ(1, merge_result.num_items_after_association());
395 // reset callbacks counter
396 num_device_info_changed_callbacks_ = 0;
398 SyncChangeList change_list;
399 AddChange(change_list, SyncChange::ACTION_ADD, "guid_2", "foo_2");
401 // Unsubscribe observer before processing changes.
402 sync_service_->RemoveObserver(this);
404 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
405 EXPECT_FALSE(error.IsSet());
407 // The number of callback should still be zero.
408 EXPECT_EQ(0, num_device_info_changed_callbacks_);
411 // Verifies setting backup timestamp after the initial sync.
412 TEST_F(DeviceInfoSyncServiceTest, UpdateLocalDeviceBackupTime) {
413 // Shouldn't have backuptime initially.
414 base::Time backup_time = sync_service_->GetLocalDeviceBackupTime();
415 EXPECT_TRUE(backup_time.is_null());
417 // Perform the initial sync with empty data.
418 SyncMergeResult merge_result =
419 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
422 CreateAndPassSyncErrorFactory());
424 // Should have local device after the initial sync.
425 EXPECT_EQ(1U, sync_processor_->change_list_size());
426 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0));
428 // Shouldn't have backup time initially.
429 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
430 EXPECT_FALSE(sync_processor_->device_info_at(0).has_backup_timestamp());
432 sync_service_->UpdateLocalDeviceBackupTime(base::Time::FromTimeT(1000));
434 // Should have local device info updated with the specified backup timestamp.
435 EXPECT_EQ(1U, sync_processor_->change_list_size());
436 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
437 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
438 EXPECT_TRUE(sync_processor_->device_info_at(0).has_backup_timestamp());
440 backup_time = syncer::ProtoTimeToTime(
441 sync_processor_->device_info_at(0).backup_timestamp());
442 EXPECT_EQ(1000, backup_time.ToTimeT());
444 // Also verify that we get the same backup time directly from the service.
445 backup_time = sync_service_->GetLocalDeviceBackupTime();
446 EXPECT_EQ(1000, backup_time.ToTimeT());
449 // Verifies setting backup timestamp prior to the initial sync.
450 TEST_F(DeviceInfoSyncServiceTest, UpdateLocalDeviceBackupTimeBeforeSync) {
451 // Set the backup timestamp.
452 sync_service_->UpdateLocalDeviceBackupTime(base::Time::FromTimeT(2000));
453 // Verify that we get it back.
454 base::Time backup_time = sync_service_->GetLocalDeviceBackupTime();
455 EXPECT_EQ(2000, backup_time.ToTimeT());
457 // Now perform the initial sync with empty data.
458 SyncMergeResult merge_result =
459 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
462 CreateAndPassSyncErrorFactory());
464 // Should have local device after the initial sync.
465 // Should have the backup timestamp set.
466 EXPECT_EQ(1U, sync_processor_->change_list_size());
467 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0));
468 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
469 EXPECT_TRUE(sync_processor_->device_info_at(0).has_backup_timestamp());
471 backup_time = syncer::ProtoTimeToTime(
472 sync_processor_->device_info_at(0).backup_timestamp());
473 EXPECT_EQ(2000, backup_time.ToTimeT());
476 // Verifies that the backup timestamp that comes in the intial sync data
477 // gets preserved when there are no changes to the local device.
478 TEST_F(DeviceInfoSyncServiceTest, PreserveBackupTimeWithMatchingLocalDevice) {
479 base::Time backup_time = base::Time::FromTimeT(3000);
480 SyncDataList sync_data;
481 sync_data.push_back(CreateRemoteData(
482 "guid_1", "client_1", syncer::TimeToProtoTime(backup_time)));
484 SyncMergeResult merge_result =
485 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
488 CreateAndPassSyncErrorFactory());
490 // Everything is matching so there should be no updates.
491 EXPECT_EQ(0U, sync_processor_->change_list_size());
493 // Verify that we get back the same time.
494 backup_time = sync_service_->GetLocalDeviceBackupTime();
495 EXPECT_EQ(3000, backup_time.ToTimeT());
498 // Verifies that the backup timestamp that comes in the intial sync data
499 // gets merged with the local device data.
500 TEST_F(DeviceInfoSyncServiceTest, MergeBackupTimeWithMatchingLocalDevice) {
501 base::Time backup_time = base::Time::FromTimeT(4000);
502 SyncDataList sync_data;
503 sync_data.push_back(CreateRemoteData(
504 "guid_1", "foo_1", syncer::TimeToProtoTime(backup_time)));
506 SyncMergeResult merge_result =
507 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
510 CreateAndPassSyncErrorFactory());
512 // Should be one change because of the client name mismatch.
513 // However the backup time passed in the initial data should be merged into
515 EXPECT_EQ(1U, sync_processor_->change_list_size());
517 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
518 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
519 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
521 backup_time = syncer::ProtoTimeToTime(
522 sync_processor_->device_info_at(0).backup_timestamp());
523 EXPECT_EQ(4000, backup_time.ToTimeT());
526 // Verifies that mismatching backup timestamp generates an update even
527 // when the rest of local device data is matching.
528 TEST_F(DeviceInfoSyncServiceTest,
529 MergeMismatchingBackupTimeWithMatchingLocalDevice) {
530 base::Time backup_time = base::Time::FromTimeT(5000);
531 SyncDataList sync_data;
532 sync_data.push_back(CreateRemoteData(
533 "guid_1", "client_1", syncer::TimeToProtoTime(backup_time)));
535 // Set the backup timestamp different than the one in the sync data.
536 sync_service_->UpdateLocalDeviceBackupTime(base::Time::FromTimeT(6000));
538 SyncMergeResult merge_result =
539 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO,
542 CreateAndPassSyncErrorFactory());
544 // Should generate and update due to timestamp mismatch.
545 // The locally set timestamp wins.
546 EXPECT_EQ(1U, sync_processor_->change_list_size());
548 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
549 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
550 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
552 backup_time = syncer::ProtoTimeToTime(
553 sync_processor_->device_info_at(0).backup_timestamp());
554 EXPECT_EQ(6000, backup_time.ToTimeT());
559 } // namespace browser_sync