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