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/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "components/sync_driver/data_type_error_handler_mock.h" | 11 #include "components/sync_driver/data_type_error_handler_mock.h" |
| 12 #include "components/sync_driver/sync_api_component_factory.h" |
12 #include "sync/api/attachments/attachment_service_impl.h" | 13 #include "sync/api/attachments/attachment_service_impl.h" |
13 #include "sync/api/fake_syncable_service.h" | 14 #include "sync/api/fake_syncable_service.h" |
14 #include "sync/api/sync_change.h" | 15 #include "sync/api/sync_change.h" |
15 #include "sync/api/sync_merge_result.h" | 16 #include "sync/api/sync_merge_result.h" |
16 #include "sync/internal_api/public/attachments/fake_attachment_store.h" | 17 #include "sync/internal_api/public/attachments/fake_attachment_store.h" |
17 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 18 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
18 #include "sync/internal_api/public/base/model_type.h" | 19 #include "sync/internal_api/public/base/model_type.h" |
19 #include "sync/internal_api/public/read_node.h" | 20 #include "sync/internal_api/public/read_node.h" |
20 #include "sync/internal_api/public/read_transaction.h" | 21 #include "sync/internal_api/public/read_transaction.h" |
21 #include "sync/internal_api/public/sync_encryption_handler.h" | 22 #include "sync/internal_api/public/sync_encryption_handler.h" |
(...skipping 20 matching lines...) Expand all Loading... |
42 | 43 |
43 private: | 44 private: |
44 std::vector<syncer::AttachmentList> attachment_lists_; | 45 std::vector<syncer::AttachmentList> attachment_lists_; |
45 }; | 46 }; |
46 | 47 |
47 MockAttachmentService::MockAttachmentService() | 48 MockAttachmentService::MockAttachmentService() |
48 : AttachmentServiceImpl( | 49 : AttachmentServiceImpl( |
49 scoped_ptr<syncer::AttachmentStore>(new syncer::FakeAttachmentStore( | 50 scoped_ptr<syncer::AttachmentStore>(new syncer::FakeAttachmentStore( |
50 base::MessageLoopProxy::current())), | 51 base::MessageLoopProxy::current())), |
51 scoped_ptr<syncer::AttachmentUploader>( | 52 scoped_ptr<syncer::AttachmentUploader>( |
52 new syncer::FakeAttachmentUploader)) { | 53 new syncer::FakeAttachmentUploader), |
| 54 NULL) { |
53 } | 55 } |
54 | 56 |
55 MockAttachmentService::~MockAttachmentService() { | 57 MockAttachmentService::~MockAttachmentService() { |
56 } | 58 } |
57 | 59 |
58 void MockAttachmentService::StoreAttachments( | 60 void MockAttachmentService::StoreAttachments( |
59 const syncer::AttachmentList& attachments, | 61 const syncer::AttachmentList& attachments, |
60 const StoreCallback& callback) { | 62 const StoreCallback& callback) { |
61 attachment_lists_.push_back(attachments); | 63 attachment_lists_.push_back(attachments); |
62 AttachmentServiceImpl::StoreAttachments(attachments, callback); | 64 AttachmentServiceImpl::StoreAttachments(attachments, callback); |
63 } | 65 } |
64 | 66 |
65 std::vector<syncer::AttachmentList>* MockAttachmentService::attachment_lists() { | 67 std::vector<syncer::AttachmentList>* MockAttachmentService::attachment_lists() { |
66 return &attachment_lists_; | 68 return &attachment_lists_; |
67 } | 69 } |
68 | 70 |
| 71 // MockSyncApiComponentFactory needed to initialize GenericChangeProcessor and |
| 72 // pass MockAttachmentService to it. |
| 73 class MockSyncApiComponentFactory : public SyncApiComponentFactory { |
| 74 public: |
| 75 MockSyncApiComponentFactory( |
| 76 scoped_ptr<syncer::AttachmentService> attachment_service) |
| 77 : attachment_service_(attachment_service.Pass()) {} |
| 78 |
| 79 virtual base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType( |
| 80 syncer::ModelType type) OVERRIDE { |
| 81 // Shouldn't be called for this test. |
| 82 NOTREACHED(); |
| 83 return base::WeakPtr<syncer::SyncableService>(); |
| 84 } |
| 85 |
| 86 virtual scoped_ptr<syncer::AttachmentService> CreateAttachmentService( |
| 87 syncer::AttachmentService::Delegate* delegate) OVERRIDE { |
| 88 EXPECT_TRUE(attachment_service_ != NULL); |
| 89 return attachment_service_.Pass(); |
| 90 } |
| 91 |
| 92 private: |
| 93 scoped_ptr<syncer::AttachmentService> attachment_service_; |
| 94 }; |
| 95 |
69 class SyncGenericChangeProcessorTest : public testing::Test { | 96 class SyncGenericChangeProcessorTest : public testing::Test { |
70 public: | 97 public: |
71 // It doesn't matter which type we use. Just pick one. | 98 // It doesn't matter which type we use. Just pick one. |
72 static const syncer::ModelType kType = syncer::PREFERENCES; | 99 static const syncer::ModelType kType = syncer::PREFERENCES; |
73 | 100 |
74 SyncGenericChangeProcessorTest() | 101 SyncGenericChangeProcessorTest() |
75 : sync_merge_result_(kType), | 102 : sync_merge_result_(kType), |
76 merge_result_ptr_factory_(&sync_merge_result_), | 103 merge_result_ptr_factory_(&sync_merge_result_), |
77 syncable_service_ptr_factory_(&fake_syncable_service_), | 104 syncable_service_ptr_factory_(&fake_syncable_service_), |
78 mock_attachment_service_(NULL) {} | 105 mock_attachment_service_(NULL) {} |
79 | 106 |
80 virtual void SetUp() OVERRIDE { | 107 virtual void SetUp() OVERRIDE { |
81 test_user_share_.SetUp(); | 108 test_user_share_.SetUp(); |
82 syncer::ModelTypeSet types = syncer::ProtocolTypes(); | 109 syncer::ModelTypeSet types = syncer::ProtocolTypes(); |
83 for (syncer::ModelTypeSet::Iterator iter = types.First(); iter.Good(); | 110 for (syncer::ModelTypeSet::Iterator iter = types.First(); iter.Good(); |
84 iter.Inc()) { | 111 iter.Inc()) { |
85 syncer::TestUserShare::CreateRoot(iter.Get(), | 112 syncer::TestUserShare::CreateRoot(iter.Get(), |
86 test_user_share_.user_share()); | 113 test_user_share_.user_share()); |
87 } | 114 } |
88 test_user_share_.encryption_handler()->Init(); | 115 test_user_share_.encryption_handler()->Init(); |
89 scoped_ptr<MockAttachmentService> mock_attachment_service( | 116 scoped_ptr<MockAttachmentService> mock_attachment_service( |
90 new MockAttachmentService); | 117 new MockAttachmentService); |
91 // GenericChangeProcessor takes ownership of the AttachmentService, but we | 118 // GenericChangeProcessor takes ownership of the AttachmentService, but we |
92 // need to have a pointer to it so we can see that it was used properly. | 119 // need to have a pointer to it so we can see that it was used properly. |
93 // Take a pointer and trust that GenericChangeProcessor does not prematurely | 120 // Take a pointer and trust that GenericChangeProcessor does not prematurely |
94 // destroy it. | 121 // destroy it. |
95 mock_attachment_service_ = mock_attachment_service.get(); | 122 mock_attachment_service_ = mock_attachment_service.get(); |
96 change_processor_.reset(new GenericChangeProcessor( | 123 sync_factory_.reset(new MockSyncApiComponentFactory( |
97 &data_type_error_handler_, | |
98 syncable_service_ptr_factory_.GetWeakPtr(), | |
99 merge_result_ptr_factory_.GetWeakPtr(), | |
100 test_user_share_.user_share(), | |
101 mock_attachment_service.PassAs<syncer::AttachmentService>())); | 124 mock_attachment_service.PassAs<syncer::AttachmentService>())); |
| 125 change_processor_.reset( |
| 126 new GenericChangeProcessor(&data_type_error_handler_, |
| 127 syncable_service_ptr_factory_.GetWeakPtr(), |
| 128 merge_result_ptr_factory_.GetWeakPtr(), |
| 129 test_user_share_.user_share(), |
| 130 sync_factory_.get())); |
102 } | 131 } |
103 | 132 |
104 virtual void TearDown() OVERRIDE { | 133 virtual void TearDown() OVERRIDE { |
105 mock_attachment_service_ = NULL; | 134 mock_attachment_service_ = NULL; |
106 test_user_share_.TearDown(); | 135 test_user_share_.TearDown(); |
107 } | 136 } |
108 | 137 |
109 void BuildChildNodes(int n) { | 138 void BuildChildNodes(int n) { |
110 syncer::WriteTransaction trans(FROM_HERE, user_share()); | 139 syncer::WriteTransaction trans(FROM_HERE, user_share()); |
111 syncer::ReadNode root(&trans); | 140 syncer::ReadNode root(&trans); |
(...skipping 23 matching lines...) Expand all Loading... |
135 syncer::SyncMergeResult sync_merge_result_; | 164 syncer::SyncMergeResult sync_merge_result_; |
136 base::WeakPtrFactory<syncer::SyncMergeResult> merge_result_ptr_factory_; | 165 base::WeakPtrFactory<syncer::SyncMergeResult> merge_result_ptr_factory_; |
137 | 166 |
138 syncer::FakeSyncableService fake_syncable_service_; | 167 syncer::FakeSyncableService fake_syncable_service_; |
139 base::WeakPtrFactory<syncer::FakeSyncableService> | 168 base::WeakPtrFactory<syncer::FakeSyncableService> |
140 syncable_service_ptr_factory_; | 169 syncable_service_ptr_factory_; |
141 | 170 |
142 DataTypeErrorHandlerMock data_type_error_handler_; | 171 DataTypeErrorHandlerMock data_type_error_handler_; |
143 syncer::TestUserShare test_user_share_; | 172 syncer::TestUserShare test_user_share_; |
144 MockAttachmentService* mock_attachment_service_; | 173 MockAttachmentService* mock_attachment_service_; |
| 174 scoped_ptr<SyncApiComponentFactory> sync_factory_; |
145 | 175 |
146 scoped_ptr<GenericChangeProcessor> change_processor_; | 176 scoped_ptr<GenericChangeProcessor> change_processor_; |
147 }; | 177 }; |
148 | 178 |
149 // Similar to above, but focused on the method that implements sync/api | 179 // Similar to above, but focused on the method that implements sync/api |
150 // interfaces and is hence exposed to datatypes directly. | 180 // interfaces and is hence exposed to datatypes directly. |
151 TEST_F(SyncGenericChangeProcessorTest, StressGetAllSyncData) { | 181 TEST_F(SyncGenericChangeProcessorTest, StressGetAllSyncData) { |
152 const int kNumChildNodes = 1000; | 182 const int kNumChildNodes = 1000; |
153 const int kRepeatCount = 1; | 183 const int kRepeatCount = 1; |
154 | 184 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); | 369 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
340 | 370 |
341 // Check that the AttachmentService received it. | 371 // Check that the AttachmentService received it. |
342 ASSERT_EQ(mock_attachment_service()->attachment_lists()->size(), 1U); | 372 ASSERT_EQ(mock_attachment_service()->attachment_lists()->size(), 1U); |
343 const syncer::AttachmentList& new_attachments_added = | 373 const syncer::AttachmentList& new_attachments_added = |
344 mock_attachment_service()->attachment_lists()->front(); | 374 mock_attachment_service()->attachment_lists()->front(); |
345 ASSERT_EQ(new_attachments_added.size(), 1U); | 375 ASSERT_EQ(new_attachments_added.size(), 1U); |
346 ASSERT_EQ(new_attachments_added[0].GetId(), new_attachments[0].GetId()); | 376 ASSERT_EQ(new_attachments_added[0].GetId(), new_attachments[0].GetId()); |
347 } | 377 } |
348 | 378 |
| 379 // Verify that after attachment is uploaded GenericChangeProcessor updates |
| 380 // corresponding entries |
| 381 TEST_F(SyncGenericChangeProcessorTest, AttachmentUploaded) { |
| 382 std::string tag = "client_tag"; |
| 383 std::string title = "client_title"; |
| 384 sync_pb::EntitySpecifics specifics; |
| 385 sync_pb::PreferenceSpecifics* pref_specifics = specifics.mutable_preference(); |
| 386 pref_specifics->set_name("test"); |
| 387 syncer::AttachmentList attachments; |
| 388 scoped_refptr<base::RefCountedString> attachment_data = |
| 389 new base::RefCountedString; |
| 390 attachment_data->data() = kTestData; |
| 391 attachments.push_back(syncer::Attachment::Create(attachment_data)); |
| 392 |
| 393 // Add a SyncData with two attachments. |
| 394 syncer::SyncChangeList change_list; |
| 395 change_list.push_back( |
| 396 syncer::SyncChange(FROM_HERE, |
| 397 syncer::SyncChange::ACTION_ADD, |
| 398 syncer::SyncData::CreateLocalDataWithAttachments( |
| 399 tag, title, specifics, attachments))); |
| 400 ASSERT_FALSE( |
| 401 change_processor()->ProcessSyncChanges(FROM_HERE, change_list).IsSet()); |
| 402 |
| 403 sync_pb::AttachmentIdProto attachment_id_proto = |
| 404 attachments[0].GetId().GetProto(); |
| 405 syncer::AttachmentId attachment_id = |
| 406 syncer::AttachmentId::CreateFromProto(attachment_id_proto); |
| 407 |
| 408 change_processor()->OnAttachmentUploaded(attachment_id); |
| 409 syncer::ReadTransaction read_transaction(FROM_HERE, user_share()); |
| 410 syncer::ReadNode node(&read_transaction); |
| 411 ASSERT_EQ(node.InitByClientTagLookup(syncer::PREFERENCES, tag), |
| 412 syncer::BaseNode::INIT_OK); |
| 413 syncer::AttachmentIdList attachment_ids = node.GetAttachmentIds(); |
| 414 EXPECT_EQ(1U, attachment_ids.size()); |
| 415 } |
| 416 |
349 } // namespace | 417 } // namespace |
350 | 418 |
351 } // namespace browser_sync | 419 } // namespace browser_sync |
OLD | NEW |