| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/sync_driver/device_info_service.h" | 5 #include "components/sync_driver/device_info_service.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 128 |
| 129 void Delete(const std::string& client_tag, | 129 void Delete(const std::string& client_tag, |
| 130 MetadataChangeList* metadata_changes) override { | 130 MetadataChangeList* metadata_changes) override { |
| 131 delete_set_.insert(client_tag); | 131 delete_set_.insert(client_tag); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void OnMetadataLoaded(scoped_ptr<MetadataBatch> batch) override { | 134 void OnMetadataLoaded(scoped_ptr<MetadataBatch> batch) override { |
| 135 std::swap(metadata_, batch); | 135 std::swap(metadata_, batch); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void OnSyncStarting(const StartCallback& callback) override {} |
| 139 |
| 138 const std::map<std::string, scoped_ptr<EntityData>>& put_map() const { | 140 const std::map<std::string, scoped_ptr<EntityData>>& put_map() const { |
| 139 return put_map_; | 141 return put_map_; |
| 140 } | 142 } |
| 141 const std::set<std::string>& delete_set() const { return delete_set_; } | 143 const std::set<std::string>& delete_set() const { return delete_set_; } |
| 142 const MetadataBatch* metadata() const { return metadata_.get(); } | 144 const MetadataBatch* metadata() const { return metadata_.get(); } |
| 143 | 145 |
| 144 private: | 146 private: |
| 145 std::map<std::string, scoped_ptr<EntityData>> put_map_; | 147 std::map<std::string, scoped_ptr<EntityData>> put_map_; |
| 146 std::set<std::string> delete_set_; | 148 std::set<std::string> delete_set_; |
| 147 scoped_ptr<MetadataBatch> metadata_; | 149 scoped_ptr<MetadataBatch> metadata_; |
| 148 }; | 150 }; |
| 149 | 151 |
| 150 } // namespace | 152 } // namespace |
| 151 | 153 |
| 152 class DeviceInfoServiceTest : public testing::Test, | 154 class DeviceInfoServiceTest : public testing::Test, |
| 153 public DeviceInfoTracker::Observer { | 155 public DeviceInfoTracker::Observer { |
| 154 protected: | 156 protected: |
| 155 ~DeviceInfoServiceTest() override { | 157 ~DeviceInfoServiceTest() override { |
| 156 // Some tests may never initialize the service. | 158 // Some tests may never initialize the service. |
| 157 if (service_) | 159 if (service_) |
| 158 service_->RemoveObserver(this); | 160 service_->RemoveObserver(this); |
| 159 | 161 |
| 160 // Force all remaining (store) tasks to execute so we don't leak memory. | 162 // Force all remaining (store) tasks to execute so we don't leak memory. |
| 161 base::RunLoop().RunUntilIdle(); | 163 base::RunLoop().RunUntilIdle(); |
| 162 } | 164 } |
| 163 | 165 |
| 164 void OnDeviceInfoChange() override { change_count_++; } | 166 void OnDeviceInfoChange() override { change_count_++; } |
| 165 | 167 |
| 166 protected: | 168 scoped_ptr<ModelTypeChangeProcessor> CreateModelTypeChangeProcessor( |
| 169 syncer::ModelType type, |
| 170 ModelTypeService* service) { |
| 171 processor_ = new FakeModelTypeChangeProcessor(); |
| 172 return make_scoped_ptr(processor_); |
| 173 } |
| 174 |
| 167 DeviceInfoServiceTest() | 175 DeviceInfoServiceTest() |
| 168 : change_count_(0), | 176 : change_count_(0), |
| 169 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), | 177 store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), |
| 170 local_device_(new LocalDeviceInfoProviderMock( | 178 local_device_(new LocalDeviceInfoProviderMock( |
| 171 "guid_1", | 179 "guid_1", |
| 172 "client_1", | 180 "client_1", |
| 173 "Chromium 10k", | 181 "Chromium 10k", |
| 174 "Chrome 10k", | 182 "Chrome 10k", |
| 175 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | 183 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, |
| 176 "device_id")) {} | 184 "device_id")) {} |
| 177 | 185 |
| 178 // Initialized the service based on the current local device and store. Can | 186 // Initialized the service based on the current local device and store. Can |
| 179 // only be called once per run, as it passes |store_|. | 187 // only be called once per run, as it passes |store_|. |
| 180 void InitializeService() { | 188 void InitializeService() { |
| 181 ASSERT_TRUE(store_); | 189 ASSERT_TRUE(store_); |
| 182 service_.reset(new DeviceInfoService( | 190 service_.reset(new DeviceInfoService( |
| 183 local_device_.get(), | 191 local_device_.get(), |
| 184 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, | 192 base::Bind(&ModelTypeStoreTestUtil::MoveStoreToCallback, |
| 185 base::Passed(&store_)))); | 193 base::Passed(&store_)), |
| 194 base::Bind(&DeviceInfoServiceTest::CreateModelTypeChangeProcessor, |
| 195 base::Unretained(this)))); |
| 186 service_->AddObserver(this); | 196 service_->AddObserver(this); |
| 187 } | 197 } |
| 188 | 198 |
| 189 // Creates the service and runs any outstanding tasks. This will typically | 199 // Creates the service and runs any outstanding tasks. This will typically |
| 190 // cause all initialization callbacks between the sevice and store to fire. | 200 // cause all initialization callbacks between the sevice and store to fire. |
| 191 void InitializeAndPump() { | 201 void InitializeAndPump() { |
| 192 InitializeService(); | 202 InitializeService(); |
| 193 base::RunLoop().RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
| 194 } | 204 } |
| 195 | 205 |
| 196 // Creates a new processor and sets it on the service. We typically need to | 206 // Creates a new processor and sets it on the service. We typically need to |
| 197 // pump in this scenario because metadata is going to need to be loading from | 207 // pump in this scenario because metadata is going to need to be loading from |
| 198 // the store and given to the processor, which is async. | 208 // the store and given to the processor, which is async. |
| 199 void SetProcessorAndPump() { | 209 void CreateProcessorAndPump() { |
| 200 processor_ = new FakeModelTypeChangeProcessor(); | 210 // TODO(skym): Shouldn't need to directly force processor creation anymore. |
| 201 service()->set_change_processor(make_scoped_ptr(processor_)); | 211 EXPECT_EQ(processor_, service_->GetOrCreateChangeProcessor()); |
| 212 ASSERT_TRUE(processor_); |
| 202 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
| 203 } | 214 } |
| 204 | 215 |
| 205 // Generates a specifics object with slightly differing values. Will generate | 216 // Generates a specifics object with slightly differing values. Will generate |
| 206 // the same values on each run of a test because a simple counter is used to | 217 // the same values on each run of a test because a simple counter is used to |
| 207 // vary field values. | 218 // vary field values. |
| 208 DeviceInfoSpecifics GenerateTestSpecifics() { | 219 DeviceInfoSpecifics GenerateTestSpecifics() { |
| 209 int label = ++generated_count_; | 220 int label = ++generated_count_; |
| 210 DeviceInfoSpecifics specifics; | 221 DeviceInfoSpecifics specifics; |
| 211 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); | 222 specifics.set_cache_guid(base::StringPrintf("cache guid %d", label)); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 384 |
| 374 InitializeAndPump(); | 385 InitializeAndPump(); |
| 375 | 386 |
| 376 // Verify that we have data. We do this because we're testing that the service | 387 // Verify that we have data. We do this because we're testing that the service |
| 377 // may sometimes come up after our store init is fully completed. | 388 // may sometimes come up after our store init is fully completed. |
| 378 ScopedVector<DeviceInfo> all_device_info(service()->GetAllDeviceInfo()); | 389 ScopedVector<DeviceInfo> all_device_info(service()->GetAllDeviceInfo()); |
| 379 ASSERT_EQ(1u, all_device_info.size()); | 390 ASSERT_EQ(1u, all_device_info.size()); |
| 380 AssertEqual(specifics, *all_device_info[0]); | 391 AssertEqual(specifics, *all_device_info[0]); |
| 381 AssertEqual(specifics, *service()->GetDeviceInfo("tag").get()); | 392 AssertEqual(specifics, *service()->GetDeviceInfo("tag").get()); |
| 382 | 393 |
| 383 SetProcessorAndPump(); | 394 CreateProcessorAndPump(); |
| 384 ASSERT_TRUE(processor()->metadata()); | 395 ASSERT_TRUE(processor()->metadata()); |
| 385 ASSERT_EQ(state.encryption_key_name(), | 396 ASSERT_EQ(state.encryption_key_name(), |
| 386 processor()->metadata()->GetDataTypeState().encryption_key_name()); | 397 processor()->metadata()->GetDataTypeState().encryption_key_name()); |
| 387 } | 398 } |
| 388 | 399 |
| 389 TEST_F(DeviceInfoServiceTest, TestInitProcBeforeStoreFinishes) { | 400 TEST_F(DeviceInfoServiceTest, TestInitProcBeforeStoreFinishes) { |
| 390 scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 401 scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 391 DeviceInfoSpecifics specifics(GenerateTestSpecifics()); | 402 DeviceInfoSpecifics specifics(GenerateTestSpecifics()); |
| 392 store()->WriteData(batch.get(), "tag", specifics.SerializeAsString()); | 403 store()->WriteData(batch.get(), "tag", specifics.SerializeAsString()); |
| 393 DataTypeState state; | 404 DataTypeState state; |
| 394 state.set_encryption_key_name("ekn"); | 405 state.set_encryption_key_name("ekn"); |
| 395 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); | 406 store()->WriteGlobalMetadata(batch.get(), state.SerializeAsString()); |
| 396 store()->CommitWriteBatch(std::move(batch), | 407 store()->CommitWriteBatch(std::move(batch), |
| 397 base::Bind(&AssertResultIsSuccess)); | 408 base::Bind(&AssertResultIsSuccess)); |
| 398 | 409 |
| 399 InitializeService(); | 410 InitializeService(); |
| 400 // Verify we have _NO_ data yet, to verify that we're testing when the | 411 // Verify we have _NO_ data yet, to verify that we're testing when the |
| 401 // processor is attached and ready before our store init is fully completed. | 412 // processor is attached and ready before our store init is fully completed. |
| 402 ASSERT_EQ(0u, service()->GetAllDeviceInfo().size()); | 413 ASSERT_EQ(0u, service()->GetAllDeviceInfo().size()); |
| 403 | 414 |
| 404 SetProcessorAndPump(); | 415 CreateProcessorAndPump(); |
| 405 ASSERT_TRUE(processor()->metadata()); | 416 ASSERT_TRUE(processor()->metadata()); |
| 406 ASSERT_EQ(state.encryption_key_name(), | 417 ASSERT_EQ(state.encryption_key_name(), |
| 407 processor()->metadata()->GetDataTypeState().encryption_key_name()); | 418 processor()->metadata()->GetDataTypeState().encryption_key_name()); |
| 408 } | 419 } |
| 409 | 420 |
| 410 TEST_F(DeviceInfoServiceTest, GetData) { | 421 TEST_F(DeviceInfoServiceTest, GetData) { |
| 411 scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 422 scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 412 DeviceInfoSpecifics specifics1(GenerateTestSpecifics()); | 423 DeviceInfoSpecifics specifics1(GenerateTestSpecifics()); |
| 413 DeviceInfoSpecifics specifics3(GenerateTestSpecifics()); | 424 DeviceInfoSpecifics specifics3(GenerateTestSpecifics()); |
| 414 store()->WriteData(batch.get(), "tag1", specifics1.SerializeAsString()); | 425 store()->WriteData(batch.get(), "tag1", specifics1.SerializeAsString()); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 | 538 |
| 528 const SyncError error = | 539 const SyncError error = |
| 529 service()->ApplySyncChanges(std::move(metadata_changes), data_changes); | 540 service()->ApplySyncChanges(std::move(metadata_changes), data_changes); |
| 530 EXPECT_FALSE(error.IsSet()); | 541 EXPECT_FALSE(error.IsSet()); |
| 531 EXPECT_EQ(1, change_count()); | 542 EXPECT_EQ(1, change_count()); |
| 532 | 543 |
| 533 // Force write/commit tasks to finish before shutdown. | 544 // Force write/commit tasks to finish before shutdown. |
| 534 base::RunLoop().RunUntilIdle(); | 545 base::RunLoop().RunUntilIdle(); |
| 535 ShutdownService(); | 546 ShutdownService(); |
| 536 InitializeAndPump(); | 547 InitializeAndPump(); |
| 537 SetProcessorAndPump(); | 548 CreateProcessorAndPump(); |
| 538 | 549 |
| 539 scoped_ptr<DeviceInfo> info = service()->GetDeviceInfo(tag); | 550 scoped_ptr<DeviceInfo> info = service()->GetDeviceInfo(tag); |
| 540 ASSERT_TRUE(info); | 551 ASSERT_TRUE(info); |
| 541 AssertEqual(specifics, *info.get()); | 552 AssertEqual(specifics, *info.get()); |
| 542 EXPECT_TRUE(processor()->metadata()); | 553 EXPECT_TRUE(processor()->metadata()); |
| 543 // TODO(skym): Uncomment once SimpleMetadataChangeList::TransferChanges is | 554 // TODO(skym): Uncomment once SimpleMetadataChangeList::TransferChanges is |
| 544 // implemented. | 555 // implemented. |
| 545 // EXPECT_EQ(state.encryption_key_name(), | 556 // EXPECT_EQ(state.encryption_key_name(), |
| 546 // processor()->metadata()->GetDataTypeState().encryption_key_name()); | 557 // processor()->metadata()->GetDataTypeState().encryption_key_name()); |
| 547 } | 558 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 578 TEST_F(DeviceInfoServiceTest, MergeBeforeInit) { | 589 TEST_F(DeviceInfoServiceTest, MergeBeforeInit) { |
| 579 InitializeService(); | 590 InitializeService(); |
| 580 const SyncError error = service()->MergeSyncData( | 591 const SyncError error = service()->MergeSyncData( |
| 581 service()->CreateMetadataChangeList(), EntityDataMap()); | 592 service()->CreateMetadataChangeList(), EntityDataMap()); |
| 582 EXPECT_TRUE(error.IsSet()); | 593 EXPECT_TRUE(error.IsSet()); |
| 583 EXPECT_EQ(0, change_count()); | 594 EXPECT_EQ(0, change_count()); |
| 584 } | 595 } |
| 585 | 596 |
| 586 TEST_F(DeviceInfoServiceTest, MergeEmpty) { | 597 TEST_F(DeviceInfoServiceTest, MergeEmpty) { |
| 587 InitializeAndPump(); | 598 InitializeAndPump(); |
| 588 SetProcessorAndPump(); | 599 CreateProcessorAndPump(); |
| 589 const SyncError error = service()->MergeSyncData( | 600 const SyncError error = service()->MergeSyncData( |
| 590 service()->CreateMetadataChangeList(), EntityDataMap()); | 601 service()->CreateMetadataChangeList(), EntityDataMap()); |
| 591 EXPECT_FALSE(error.IsSet()); | 602 EXPECT_FALSE(error.IsSet()); |
| 592 EXPECT_EQ(0, change_count()); | 603 EXPECT_EQ(0, change_count()); |
| 593 } | 604 } |
| 594 | 605 |
| 595 TEST_F(DeviceInfoServiceTest, MergeWithData) { | 606 TEST_F(DeviceInfoServiceTest, MergeWithData) { |
| 596 const DeviceInfoSpecifics unique_local(GenerateTestSpecifics("unique_local")); | 607 const DeviceInfoSpecifics unique_local(GenerateTestSpecifics("unique_local")); |
| 597 const DeviceInfoSpecifics conflict_local(GenerateTestSpecifics("conflict")); | 608 const DeviceInfoSpecifics conflict_local(GenerateTestSpecifics("conflict")); |
| 598 DeviceInfoSpecifics conflict_remote(GenerateTestSpecifics("conflict")); | 609 DeviceInfoSpecifics conflict_remote(GenerateTestSpecifics("conflict")); |
| 599 DeviceInfoSpecifics unique_remote(GenerateTestSpecifics("unique_remote")); | 610 DeviceInfoSpecifics unique_remote(GenerateTestSpecifics("unique_remote")); |
| 600 | 611 |
| 601 scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); | 612 scoped_ptr<WriteBatch> batch = store()->CreateWriteBatch(); |
| 602 store()->WriteData(batch.get(), unique_local.cache_guid(), | 613 store()->WriteData(batch.get(), unique_local.cache_guid(), |
| 603 unique_local.SerializeAsString()); | 614 unique_local.SerializeAsString()); |
| 604 store()->WriteData(batch.get(), conflict_local.cache_guid(), | 615 store()->WriteData(batch.get(), conflict_local.cache_guid(), |
| 605 conflict_local.SerializeAsString()); | 616 conflict_local.SerializeAsString()); |
| 606 store()->CommitWriteBatch(std::move(batch), | 617 store()->CommitWriteBatch(std::move(batch), |
| 607 base::Bind(&AssertResultIsSuccess)); | 618 base::Bind(&AssertResultIsSuccess)); |
| 608 | 619 |
| 609 InitializeAndPump(); | 620 InitializeAndPump(); |
| 610 SetProcessorAndPump(); | 621 CreateProcessorAndPump(); |
| 611 | 622 |
| 612 EntityDataMap remote_input; | 623 EntityDataMap remote_input; |
| 613 remote_input[conflict_remote.cache_guid()] = | 624 remote_input[conflict_remote.cache_guid()] = |
| 614 SpecificsToEntity(conflict_remote); | 625 SpecificsToEntity(conflict_remote); |
| 615 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote); | 626 remote_input[unique_remote.cache_guid()] = SpecificsToEntity(unique_remote); |
| 616 | 627 |
| 617 DataTypeState state; | 628 DataTypeState state; |
| 618 state.set_encryption_key_name("ekn"); | 629 state.set_encryption_key_name("ekn"); |
| 619 scoped_ptr<MetadataChangeList> metadata_changes( | 630 scoped_ptr<MetadataChangeList> metadata_changes( |
| 620 service()->CreateMetadataChangeList()); | 631 service()->CreateMetadataChangeList()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 639 AssertEqual(unique_local, it->second->specifics.device_info()); | 650 AssertEqual(unique_local, it->second->specifics.device_info()); |
| 640 | 651 |
| 641 // TODO(skym): Uncomment once SimpleMetadataChangeList::TransferChanges is | 652 // TODO(skym): Uncomment once SimpleMetadataChangeList::TransferChanges is |
| 642 // implemented. | 653 // implemented. |
| 643 // ASSERT_EQ(state.encryption_key_name(), | 654 // ASSERT_EQ(state.encryption_key_name(), |
| 644 // processor()->metadata()->GetDataTypeState().encryption_key_name()); | 655 // processor()->metadata()->GetDataTypeState().encryption_key_name()); |
| 645 } | 656 } |
| 646 | 657 |
| 647 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { | 658 TEST_F(DeviceInfoServiceTest, MergeLocalGuid) { |
| 648 InitializeAndPump(); | 659 InitializeAndPump(); |
| 649 SetProcessorAndPump(); | 660 CreateProcessorAndPump(); |
| 650 | 661 |
| 651 // Service should ignore this because it uses the local device's guid. | 662 // Service should ignore this because it uses the local device's guid. |
| 652 DeviceInfoSpecifics specifics( | 663 DeviceInfoSpecifics specifics( |
| 653 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid())); | 664 GenerateTestSpecifics(local_device()->GetLocalDeviceInfo()->guid())); |
| 654 EntityDataMap remote_input; | 665 EntityDataMap remote_input; |
| 655 remote_input[specifics.cache_guid()] = SpecificsToEntity(specifics); | 666 remote_input[specifics.cache_guid()] = SpecificsToEntity(specifics); |
| 656 | 667 |
| 657 const SyncError error = service()->MergeSyncData( | 668 const SyncError error = service()->MergeSyncData( |
| 658 service()->CreateMetadataChangeList(), remote_input); | 669 service()->CreateMetadataChangeList(), remote_input); |
| 659 EXPECT_FALSE(error.IsSet()); | 670 EXPECT_FALSE(error.IsSet()); |
| 660 EXPECT_EQ(0, change_count()); | 671 EXPECT_EQ(0, change_count()); |
| 661 EXPECT_TRUE(service()->GetAllDeviceInfo().empty()); | 672 EXPECT_TRUE(service()->GetAllDeviceInfo().empty()); |
| 662 EXPECT_TRUE(processor()->delete_set().empty()); | 673 EXPECT_TRUE(processor()->delete_set().empty()); |
| 663 EXPECT_TRUE(processor()->put_map().empty()); | 674 EXPECT_TRUE(processor()->put_map().empty()); |
| 664 } | 675 } |
| 665 | 676 |
| 666 } // namespace | 677 } // namespace |
| 667 | 678 |
| 668 } // namespace sync_driver_v2 | 679 } // namespace sync_driver_v2 |
| OLD | NEW |