| 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/run_loop.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "sync/internal_api/public/user_share.h" | 26 #include "sync/internal_api/public/user_share.h" |
| 27 #include "sync/internal_api/public/write_node.h" | 27 #include "sync/internal_api/public/write_node.h" |
| 28 #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" | 29 #include "testing/gmock/include/gmock/gmock-matchers.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 31 |
| 32 namespace sync_driver { | 32 namespace sync_driver { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const char kTestData[] = "some data"; | |
| 37 | |
| 38 // A mock that keeps track of attachments passed to UploadAttachments. | 36 // A mock that keeps track of attachments passed to UploadAttachments. |
| 39 class MockAttachmentService : public syncer::AttachmentServiceImpl { | 37 class MockAttachmentService : public syncer::AttachmentServiceImpl { |
| 40 public: | 38 public: |
| 41 MockAttachmentService( | 39 MockAttachmentService( |
| 42 const scoped_refptr<syncer::AttachmentStore>& attachment_store); | 40 const scoped_refptr<syncer::AttachmentStore>& attachment_store); |
| 43 virtual ~MockAttachmentService(); | 41 virtual ~MockAttachmentService(); |
| 44 virtual void UploadAttachments( | 42 virtual void UploadAttachments( |
| 45 const syncer::AttachmentIdSet& attachment_ids) OVERRIDE; | 43 const syncer::AttachmentIdSet& attachment_ids) OVERRIDE; |
| 46 std::vector<syncer::AttachmentIdSet>* attachment_id_sets(); | 44 std::vector<syncer::AttachmentIdSet>* attachment_id_sets(); |
| 47 | 45 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 | 338 |
| 341 // Verify that attachments on newly added or updated SyncData are passed to the | 339 // Verify that attachments on newly added or updated SyncData are passed to the |
| 342 // AttachmentService. | 340 // AttachmentService. |
| 343 TEST_F(SyncGenericChangeProcessorTest, | 341 TEST_F(SyncGenericChangeProcessorTest, |
| 344 ProcessSyncChanges_AddUpdateWithAttachment) { | 342 ProcessSyncChanges_AddUpdateWithAttachment) { |
| 345 std::string tag = "client_tag"; | 343 std::string tag = "client_tag"; |
| 346 std::string title = "client_title"; | 344 std::string title = "client_title"; |
| 347 sync_pb::EntitySpecifics specifics; | 345 sync_pb::EntitySpecifics specifics; |
| 348 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); | 346 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); |
| 349 pref_specifics->set_name("test"); | 347 pref_specifics->set_name("test"); |
| 350 syncer::AttachmentList attachments; | 348 |
| 351 scoped_refptr<base::RefCountedString> attachment_data = | 349 syncer::AttachmentIdList attachment_ids; |
| 352 new base::RefCountedString; | 350 attachment_ids.push_back(syncer::AttachmentId::Create()); |
| 353 attachment_data->data() = kTestData; | 351 attachment_ids.push_back(syncer::AttachmentId::Create()); |
| 354 attachments.push_back(syncer::Attachment::Create(attachment_data)); | |
| 355 attachments.push_back(syncer::Attachment::Create(attachment_data)); | |
| 356 | 352 |
| 357 // Add a SyncData with two attachments. | 353 // Add a SyncData with two attachments. |
| 358 syncer::SyncChangeList change_list; | 354 syncer::SyncChangeList change_list; |
| 359 change_list.push_back( | 355 change_list.push_back( |
| 360 syncer::SyncChange(FROM_HERE, | 356 syncer::SyncChange(FROM_HERE, |
| 361 syncer::SyncChange::ACTION_ADD, | 357 syncer::SyncChange::ACTION_ADD, |
| 362 syncer::SyncData::CreateLocalDataWithAttachments( | 358 syncer::SyncData::CreateLocalDataWithAttachments( |
| 363 tag, title, specifics, attachments))); | 359 tag, title, specifics, attachment_ids))); |
| 364 ASSERT_FALSE( | 360 ASSERT_FALSE( |
| 365 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); | 361 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 366 RunLoop(); | 362 RunLoop(); |
| 367 | 363 |
| 368 // Check that the AttachmentService received the new attachments. | 364 // Check that the AttachmentService received the new attachments. |
| 369 ASSERT_EQ(mock_attachment_service()->attachment_id_sets()->size(), 1U); | 365 ASSERT_EQ(mock_attachment_service()->attachment_id_sets()->size(), 1U); |
| 370 const syncer::AttachmentIdSet& attachments_added = | 366 const syncer::AttachmentIdSet& attachments_added = |
| 371 mock_attachment_service()->attachment_id_sets()->front(); | 367 mock_attachment_service()->attachment_id_sets()->front(); |
| 372 ASSERT_THAT(attachments_added, | 368 ASSERT_THAT( |
| 373 testing::UnorderedElementsAre(attachments[0].GetId(), | 369 attachments_added, |
| 374 attachments[1].GetId())); | 370 testing::UnorderedElementsAre(attachment_ids[0], attachment_ids[1])); |
| 375 | 371 |
| 376 // Update the SyncData, replacing its two attachments with one new attachment. | 372 // Update the SyncData, replacing its two attachments with one new attachment. |
| 377 syncer::AttachmentList new_attachments; | 373 syncer::AttachmentIdList new_attachment_ids; |
| 378 new_attachments.push_back(syncer::Attachment::Create(attachment_data)); | 374 new_attachment_ids.push_back(syncer::AttachmentId::Create()); |
| 379 mock_attachment_service()->attachment_id_sets()->clear(); | 375 mock_attachment_service()->attachment_id_sets()->clear(); |
| 380 change_list.clear(); | 376 change_list.clear(); |
| 381 change_list.push_back( | 377 change_list.push_back( |
| 382 syncer::SyncChange(FROM_HERE, | 378 syncer::SyncChange(FROM_HERE, |
| 383 syncer::SyncChange::ACTION_UPDATE, | 379 syncer::SyncChange::ACTION_UPDATE, |
| 384 syncer::SyncData::CreateLocalDataWithAttachments( | 380 syncer::SyncData::CreateLocalDataWithAttachments( |
| 385 tag, title, specifics, new_attachments))); | 381 tag, title, specifics, new_attachment_ids))); |
| 386 ASSERT_FALSE( | 382 ASSERT_FALSE( |
| 387 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); | 383 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 388 RunLoop(); | 384 RunLoop(); |
| 389 | 385 |
| 390 // Check that the AttachmentService received it. | 386 // Check that the AttachmentService received it. |
| 391 ASSERT_EQ(mock_attachment_service()->attachment_id_sets()->size(), 1U); | 387 ASSERT_EQ(mock_attachment_service()->attachment_id_sets()->size(), 1U); |
| 392 const syncer::AttachmentIdSet& new_attachments_added = | 388 const syncer::AttachmentIdSet& new_attachments_added = |
| 393 mock_attachment_service()->attachment_id_sets()->front(); | 389 mock_attachment_service()->attachment_id_sets()->front(); |
| 394 ASSERT_THAT(new_attachments_added, | 390 ASSERT_THAT(new_attachments_added, |
| 395 testing::UnorderedElementsAre(new_attachments[0].GetId())); | 391 testing::UnorderedElementsAre(new_attachment_ids[0])); |
| 396 } | 392 } |
| 397 | 393 |
| 398 // Verify that after attachment is uploaded GenericChangeProcessor updates | 394 // Verify that after attachment is uploaded GenericChangeProcessor updates |
| 399 // corresponding entries | 395 // corresponding entries |
| 400 TEST_F(SyncGenericChangeProcessorTest, AttachmentUploaded) { | 396 TEST_F(SyncGenericChangeProcessorTest, AttachmentUploaded) { |
| 401 std::string tag = "client_tag"; | 397 std::string tag = "client_tag"; |
| 402 std::string title = "client_title"; | 398 std::string title = "client_title"; |
| 403 sync_pb::EntitySpecifics specifics; | 399 sync_pb::EntitySpecifics specifics; |
| 404 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); | 400 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); |
| 405 pref_specifics->set_name("test"); | 401 pref_specifics->set_name("test"); |
| 406 syncer::AttachmentList attachments; | 402 |
| 407 scoped_refptr<base::RefCountedString> attachment_data = | 403 syncer::AttachmentIdList attachment_ids; |
| 408 new base::RefCountedString; | 404 attachment_ids.push_back(syncer::AttachmentId::Create()); |
| 409 attachment_data->data() = kTestData; | |
| 410 attachments.push_back(syncer::Attachment::Create(attachment_data)); | |
| 411 | 405 |
| 412 // Add a SyncData with two attachments. | 406 // Add a SyncData with two attachments. |
| 413 syncer::SyncChangeList change_list; | 407 syncer::SyncChangeList change_list; |
| 414 change_list.push_back( | 408 change_list.push_back( |
| 415 syncer::SyncChange(FROM_HERE, | 409 syncer::SyncChange(FROM_HERE, |
| 416 syncer::SyncChange::ACTION_ADD, | 410 syncer::SyncChange::ACTION_ADD, |
| 417 syncer::SyncData::CreateLocalDataWithAttachments( | 411 syncer::SyncData::CreateLocalDataWithAttachments( |
| 418 tag, title, specifics, attachments))); | 412 tag, title, specifics, attachment_ids))); |
| 419 ASSERT_FALSE( | 413 ASSERT_FALSE( |
| 420 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); | 414 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 421 | 415 |
| 422 sync_pb::AttachmentIdProto attachment_id_proto = | 416 sync_pb::AttachmentIdProto attachment_id_proto = attachment_ids[0].GetProto(); |
| 423 attachments[0].GetId().GetProto(); | |
| 424 syncer::AttachmentId attachment_id = | 417 syncer::AttachmentId attachment_id = |
| 425 syncer::AttachmentId::CreateFromProto(attachment_id_proto); | 418 syncer::AttachmentId::CreateFromProto(attachment_id_proto); |
| 426 | 419 |
| 427 change_processor()->OnAttachmentUploaded(attachment_id); | 420 change_processor()->OnAttachmentUploaded(attachment_id); |
| 428 syncer::ReadTransaction read_transaction(FROM_HERE, user_share()); | 421 syncer::ReadTransaction read_transaction(FROM_HERE, user_share()); |
| 429 syncer::ReadNode node(&read_transaction); | 422 syncer::ReadNode node(&read_transaction); |
| 430 ASSERT_EQ(node.InitByClientTagLookup(syncer::PREFERENCES, tag), | 423 ASSERT_EQ(node.InitByClientTagLookup(syncer::PREFERENCES, tag), |
| 431 syncer::BaseNode::INIT_OK); | 424 syncer::BaseNode::INIT_OK); |
| 432 syncer::AttachmentIdList attachment_ids = node.GetAttachmentIds(); | 425 attachment_ids = node.GetAttachmentIds(); |
| 433 EXPECT_EQ(1U, attachment_ids.size()); | 426 EXPECT_EQ(1U, attachment_ids.size()); |
| 434 } | 427 } |
| 435 | 428 |
| 436 } // namespace | 429 } // namespace |
| 437 | 430 |
| 438 } // namespace sync_driver | 431 } // namespace sync_driver |
| OLD | NEW |