Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "components/sync_driver/generic_change_processor.h" | 5 #include "components/sync_driver/generic_change_processor.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "components/sync_driver/data_type_error_handler_mock.h" | 12 #include "components/sync_driver/data_type_error_handler_mock.h" |
| 12 #include "components/sync_driver/sync_api_component_factory.h" | 13 #include "components/sync_driver/sync_api_component_factory.h" |
| 13 #include "sync/api/attachments/fake_attachment_store.h" | 14 #include "sync/api/attachments/fake_attachment_store.h" |
| 14 #include "sync/api/fake_syncable_service.h" | 15 #include "sync/api/fake_syncable_service.h" |
| 15 #include "sync/api/sync_change.h" | 16 #include "sync/api/sync_change.h" |
| 16 #include "sync/api/sync_merge_result.h" | 17 #include "sync/api/sync_merge_result.h" |
| 17 #include "sync/internal_api/public/attachments/attachment_service_impl.h" | 18 #include "sync/internal_api/public/attachments/attachment_service_impl.h" |
| 18 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | 19 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" |
| 19 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 20 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
| 20 #include "sync/internal_api/public/base/model_type.h" | 21 #include "sync/internal_api/public/base/model_type.h" |
| 21 #include "sync/internal_api/public/read_node.h" | 22 #include "sync/internal_api/public/read_node.h" |
| 22 #include "sync/internal_api/public/read_transaction.h" | 23 #include "sync/internal_api/public/read_transaction.h" |
| 23 #include "sync/internal_api/public/sync_encryption_handler.h" | 24 #include "sync/internal_api/public/sync_encryption_handler.h" |
| 24 #include "sync/internal_api/public/test/test_user_share.h" | 25 #include "sync/internal_api/public/test/test_user_share.h" |
| 25 #include "sync/internal_api/public/user_share.h" | 26 #include "sync/internal_api/public/user_share.h" |
| 26 #include "sync/internal_api/public/write_node.h" | 27 #include "sync/internal_api/public/write_node.h" |
| 27 #include "sync/internal_api/public/write_transaction.h" | 28 #include "sync/internal_api/public/write_transaction.h" |
| 29 #include "testing/gmock/include/gmock/gmock-matchers.h" | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 31 |
| 30 namespace sync_driver { | 32 namespace sync_driver { |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 const char kTestData[] = "some data"; | 36 const char kTestData[] = "some data"; |
| 35 | 37 |
| 36 // A mock that keeps track of attachments passed to StoreAttachments. | 38 // A mock that keeps track of attachments passed to UploadAttachments. |
| 37 class MockAttachmentService : public syncer::AttachmentServiceImpl { | 39 class MockAttachmentService : public syncer::AttachmentServiceImpl { |
| 38 public: | 40 public: |
| 39 MockAttachmentService(); | 41 MockAttachmentService(); |
| 40 virtual ~MockAttachmentService(); | 42 virtual ~MockAttachmentService(); |
| 41 virtual void StoreAttachments(const syncer::AttachmentList& attachments, | 43 virtual void UploadAttachments( |
| 42 const StoreCallback& callback) OVERRIDE; | 44 const syncer::AttachmentIdSet& attachment_ids) OVERRIDE; |
| 43 std::vector<syncer::AttachmentList>* attachment_lists(); | 45 std::vector<syncer::AttachmentIdSet>* attachment_id_sets(); |
| 44 | 46 |
| 45 private: | 47 private: |
| 46 std::vector<syncer::AttachmentList> attachment_lists_; | 48 std::vector<syncer::AttachmentIdSet> attachment_id_sets_; |
| 47 }; | 49 }; |
| 48 | 50 |
| 49 MockAttachmentService::MockAttachmentService() | 51 MockAttachmentService::MockAttachmentService() |
| 50 : AttachmentServiceImpl( | 52 : AttachmentServiceImpl( |
| 51 scoped_ptr<syncer::AttachmentStore>(new syncer::FakeAttachmentStore( | 53 scoped_ptr<syncer::AttachmentStore>(new syncer::FakeAttachmentStore( |
| 52 base::MessageLoopProxy::current())), | 54 base::MessageLoopProxy::current())), |
| 53 scoped_ptr<syncer::AttachmentUploader>( | 55 scoped_ptr<syncer::AttachmentUploader>( |
| 54 new syncer::FakeAttachmentUploader), | 56 new syncer::FakeAttachmentUploader), |
| 55 scoped_ptr<syncer::AttachmentDownloader>( | 57 scoped_ptr<syncer::AttachmentDownloader>( |
| 56 new syncer::FakeAttachmentDownloader), | 58 new syncer::FakeAttachmentDownloader), |
| 57 NULL) { | 59 NULL) { |
| 58 } | 60 } |
| 59 | 61 |
| 60 MockAttachmentService::~MockAttachmentService() { | 62 MockAttachmentService::~MockAttachmentService() { |
| 61 } | 63 } |
| 62 | 64 |
| 63 void MockAttachmentService::StoreAttachments( | 65 void MockAttachmentService::UploadAttachments( |
| 64 const syncer::AttachmentList& attachments, | 66 const syncer::AttachmentIdSet& attachment_ids) { |
| 65 const StoreCallback& callback) { | 67 attachment_id_sets_.push_back(attachment_ids); |
| 66 attachment_lists_.push_back(attachments); | 68 AttachmentServiceImpl::UploadAttachments(attachment_ids); |
| 67 AttachmentServiceImpl::StoreAttachments(attachments, callback); | |
| 68 } | 69 } |
| 69 | 70 |
| 70 std::vector<syncer::AttachmentList>* MockAttachmentService::attachment_lists() { | 71 std::vector<syncer::AttachmentIdSet>* |
| 71 return &attachment_lists_; | 72 MockAttachmentService::attachment_id_sets() { |
| 73 return &attachment_id_sets_; | |
| 72 } | 74 } |
| 73 | 75 |
| 74 // MockSyncApiComponentFactory needed to initialize GenericChangeProcessor and | 76 // MockSyncApiComponentFactory needed to initialize GenericChangeProcessor and |
| 75 // pass MockAttachmentService to it. | 77 // pass MockAttachmentService to it. |
| 76 class MockSyncApiComponentFactory : public SyncApiComponentFactory { | 78 class MockSyncApiComponentFactory : public SyncApiComponentFactory { |
| 77 public: | 79 public: |
| 78 MockSyncApiComponentFactory( | 80 MockSyncApiComponentFactory( |
| 79 scoped_ptr<syncer::AttachmentService> attachment_service) | 81 scoped_ptr<syncer::AttachmentService> attachment_service) |
| 80 : attachment_service_(attachment_service.Pass()) {} | 82 : attachment_service_(attachment_service.Pass()) {} |
| 81 | 83 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 } | 156 } |
| 155 | 157 |
| 156 syncer::UserShare* user_share() { | 158 syncer::UserShare* user_share() { |
| 157 return test_user_share_.user_share(); | 159 return test_user_share_.user_share(); |
| 158 } | 160 } |
| 159 | 161 |
| 160 MockAttachmentService* mock_attachment_service() { | 162 MockAttachmentService* mock_attachment_service() { |
| 161 return mock_attachment_service_; | 163 return mock_attachment_service_; |
| 162 } | 164 } |
| 163 | 165 |
| 166 void RunLoop() { | |
| 167 base::RunLoop run_loop; | |
| 168 run_loop.RunUntilIdle(); | |
| 169 } | |
| 170 | |
| 164 private: | 171 private: |
| 165 base::MessageLoopForUI loop_; | 172 base::MessageLoopForUI loop_; |
| 166 | 173 |
| 167 syncer::SyncMergeResult sync_merge_result_; | 174 syncer::SyncMergeResult sync_merge_result_; |
| 168 base::WeakPtrFactory<syncer::SyncMergeResult> merge_result_ptr_factory_; | 175 base::WeakPtrFactory<syncer::SyncMergeResult> merge_result_ptr_factory_; |
| 169 | 176 |
| 170 syncer::FakeSyncableService fake_syncable_service_; | 177 syncer::FakeSyncableService fake_syncable_service_; |
| 171 base::WeakPtrFactory<syncer::FakeSyncableService> | 178 base::WeakPtrFactory<syncer::FakeSyncableService> |
| 172 syncable_service_ptr_factory_; | 179 syncable_service_ptr_factory_; |
| 173 | 180 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 | 349 |
| 343 // Add a SyncData with two attachments. | 350 // Add a SyncData with two attachments. |
| 344 syncer::SyncChangeList change_list; | 351 syncer::SyncChangeList change_list; |
| 345 change_list.push_back( | 352 change_list.push_back( |
| 346 syncer::SyncChange(FROM_HERE, | 353 syncer::SyncChange(FROM_HERE, |
| 347 syncer::SyncChange::ACTION_ADD, | 354 syncer::SyncChange::ACTION_ADD, |
| 348 syncer::SyncData::CreateLocalDataWithAttachments( | 355 syncer::SyncData::CreateLocalDataWithAttachments( |
| 349 tag, title, specifics, attachments))); | 356 tag, title, specifics, attachments))); |
| 350 ASSERT_FALSE( | 357 ASSERT_FALSE( |
| 351 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); | 358 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 359 RunLoop(); | |
| 352 | 360 |
| 353 // Check that the AttachmentService received the new attachments. | 361 // Check that the AttachmentService received the new attachments. |
| 354 ASSERT_EQ(mock_attachment_service()->attachment_lists()->size(), 1U); | 362 ASSERT_EQ(mock_attachment_service()->attachment_id_sets()->size(), 1U); |
| 355 const syncer::AttachmentList& attachments_added = | 363 const syncer::AttachmentIdSet& attachments_added = |
| 356 mock_attachment_service()->attachment_lists()->front(); | 364 mock_attachment_service()->attachment_id_sets()->front(); |
| 357 ASSERT_EQ(attachments_added.size(), 2U); | 365 ASSERT_EQ(attachments_added.size(), 2U); |
| 358 ASSERT_EQ(attachments_added[0].GetId(), attachments[0].GetId()); | 366 EXPECT_THAT(attachments_added, testing::Contains(attachments[0].GetId())); |
|
pavely
2014/09/04 17:44:37
(not asking to change): I learned today about Unor
maniscalco
2014/09/04 22:39:38
Neat. I hadn't heard of UnorderedElementsAre. It
| |
| 359 ASSERT_EQ(attachments_added[1].GetId(), attachments[1].GetId()); | 367 EXPECT_THAT(attachments_added, testing::Contains(attachments[1].GetId())); |
| 360 | 368 |
| 361 // Update the SyncData, replacing its two attachments with one new attachment. | 369 // Update the SyncData, replacing its two attachments with one new attachment. |
| 362 syncer::AttachmentList new_attachments; | 370 syncer::AttachmentList new_attachments; |
| 363 new_attachments.push_back(syncer::Attachment::Create(attachment_data)); | 371 new_attachments.push_back(syncer::Attachment::Create(attachment_data)); |
| 364 mock_attachment_service()->attachment_lists()->clear(); | 372 mock_attachment_service()->attachment_id_sets()->clear(); |
| 365 change_list.clear(); | 373 change_list.clear(); |
| 366 change_list.push_back( | 374 change_list.push_back( |
| 367 syncer::SyncChange(FROM_HERE, | 375 syncer::SyncChange(FROM_HERE, |
| 368 syncer::SyncChange::ACTION_UPDATE, | 376 syncer::SyncChange::ACTION_UPDATE, |
| 369 syncer::SyncData::CreateLocalDataWithAttachments( | 377 syncer::SyncData::CreateLocalDataWithAttachments( |
| 370 tag, title, specifics, new_attachments))); | 378 tag, title, specifics, new_attachments))); |
| 371 ASSERT_FALSE( | 379 ASSERT_FALSE( |
| 372 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); | 380 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 381 RunLoop(); | |
| 373 | 382 |
| 374 // Check that the AttachmentService received it. | 383 // Check that the AttachmentService received it. |
| 375 ASSERT_EQ(mock_attachment_service()->attachment_lists()->size(), 1U); | 384 ASSERT_EQ(mock_attachment_service()->attachment_id_sets()->size(), 1U); |
| 376 const syncer::AttachmentList& new_attachments_added = | 385 const syncer::AttachmentIdSet& new_attachments_added = |
| 377 mock_attachment_service()->attachment_lists()->front(); | 386 mock_attachment_service()->attachment_id_sets()->front(); |
| 378 ASSERT_EQ(new_attachments_added.size(), 1U); | 387 ASSERT_EQ(new_attachments_added.size(), 1U); |
| 379 ASSERT_EQ(new_attachments_added[0].GetId(), new_attachments[0].GetId()); | 388 EXPECT_THAT(new_attachments_added, |
| 389 testing::Contains(new_attachments[0].GetId())); | |
| 380 } | 390 } |
| 381 | 391 |
| 382 // Verify that after attachment is uploaded GenericChangeProcessor updates | 392 // Verify that after attachment is uploaded GenericChangeProcessor updates |
| 383 // corresponding entries | 393 // corresponding entries |
| 384 TEST_F(SyncGenericChangeProcessorTest, AttachmentUploaded) { | 394 TEST_F(SyncGenericChangeProcessorTest, AttachmentUploaded) { |
| 385 std::string tag = "client_tag"; | 395 std::string tag = "client_tag"; |
| 386 std::string title = "client_title"; | 396 std::string title = "client_title"; |
| 387 sync_pb::EntitySpecifics specifics; | 397 sync_pb::EntitySpecifics specifics; |
| 388 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); | 398 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); |
| 389 pref_specifics->set_name("test"); | 399 pref_specifics->set_name("test"); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 413 syncer::ReadNode node(&read_transaction); | 423 syncer::ReadNode node(&read_transaction); |
| 414 ASSERT_EQ(node.InitByClientTagLookup(syncer::PREFERENCES, tag), | 424 ASSERT_EQ(node.InitByClientTagLookup(syncer::PREFERENCES, tag), |
| 415 syncer::BaseNode::INIT_OK); | 425 syncer::BaseNode::INIT_OK); |
| 416 syncer::AttachmentIdList attachment_ids = node.GetAttachmentIds(); | 426 syncer::AttachmentIdList attachment_ids = node.GetAttachmentIds(); |
| 417 EXPECT_EQ(1U, attachment_ids.size()); | 427 EXPECT_EQ(1U, attachment_ids.size()); |
| 418 } | 428 } |
| 419 | 429 |
| 420 } // namespace | 430 } // namespace |
| 421 | 431 |
| 422 } // namespace sync_driver | 432 } // namespace sync_driver |
| OLD | NEW |