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 19 matching lines...) Expand all Loading... |
30 #include "testing/gmock/include/gmock/gmock-matchers.h" | 30 #include "testing/gmock/include/gmock/gmock-matchers.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 | 32 |
33 namespace sync_driver { | 33 namespace sync_driver { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // A mock that keeps track of attachments passed to UploadAttachments. | 37 // A mock that keeps track of attachments passed to UploadAttachments. |
38 class MockAttachmentService : public syncer::AttachmentServiceImpl { | 38 class MockAttachmentService : public syncer::AttachmentServiceImpl { |
39 public: | 39 public: |
40 MockAttachmentService( | 40 MockAttachmentService(scoped_ptr<syncer::AttachmentStore> attachment_store); |
41 const scoped_refptr<syncer::AttachmentStore>& attachment_store); | |
42 ~MockAttachmentService() override; | 41 ~MockAttachmentService() override; |
43 void UploadAttachments( | 42 void UploadAttachments( |
44 const syncer::AttachmentIdSet& attachment_ids) override; | 43 const syncer::AttachmentIdSet& attachment_ids) override; |
45 std::vector<syncer::AttachmentIdSet>* attachment_id_sets(); | 44 std::vector<syncer::AttachmentIdSet>* attachment_id_sets(); |
46 | 45 |
47 private: | 46 private: |
48 std::vector<syncer::AttachmentIdSet> attachment_id_sets_; | 47 std::vector<syncer::AttachmentIdSet> attachment_id_sets_; |
49 }; | 48 }; |
50 | 49 |
51 MockAttachmentService::MockAttachmentService( | 50 MockAttachmentService::MockAttachmentService( |
52 const scoped_refptr<syncer::AttachmentStore>& attachment_store) | 51 scoped_ptr<syncer::AttachmentStore> attachment_store) |
53 : AttachmentServiceImpl(attachment_store, | 52 : AttachmentServiceImpl(attachment_store.Pass(), |
54 scoped_ptr<syncer::AttachmentUploader>( | 53 scoped_ptr<syncer::AttachmentUploader>( |
55 new syncer::FakeAttachmentUploader), | 54 new syncer::FakeAttachmentUploader), |
56 scoped_ptr<syncer::AttachmentDownloader>( | 55 scoped_ptr<syncer::AttachmentDownloader>( |
57 new syncer::FakeAttachmentDownloader), | 56 new syncer::FakeAttachmentDownloader), |
58 NULL, | 57 NULL, |
59 base::TimeDelta(), | 58 base::TimeDelta(), |
60 base::TimeDelta()) { | 59 base::TimeDelta()) { |
61 } | 60 } |
62 | 61 |
63 MockAttachmentService::~MockAttachmentService() { | 62 MockAttachmentService::~MockAttachmentService() { |
64 } | 63 } |
65 | 64 |
66 void MockAttachmentService::UploadAttachments( | 65 void MockAttachmentService::UploadAttachments( |
67 const syncer::AttachmentIdSet& attachment_ids) { | 66 const syncer::AttachmentIdSet& attachment_ids) { |
68 attachment_id_sets_.push_back(attachment_ids); | 67 attachment_id_sets_.push_back(attachment_ids); |
69 AttachmentServiceImpl::UploadAttachments(attachment_ids); | 68 AttachmentServiceImpl::UploadAttachments(attachment_ids); |
70 } | 69 } |
71 | 70 |
72 std::vector<syncer::AttachmentIdSet>* | 71 std::vector<syncer::AttachmentIdSet>* |
73 MockAttachmentService::attachment_id_sets() { | 72 MockAttachmentService::attachment_id_sets() { |
74 return &attachment_id_sets_; | 73 return &attachment_id_sets_; |
75 } | 74 } |
76 | 75 |
77 // MockSyncApiComponentFactory needed to initialize GenericChangeProcessor and | 76 // MockSyncApiComponentFactory needed to initialize GenericChangeProcessor and |
78 // pass MockAttachmentService to it. | 77 // pass MockAttachmentService to it. |
79 class MockSyncApiComponentFactory : public SyncApiComponentFactory { | 78 class MockSyncApiComponentFactory : public SyncApiComponentFactory { |
80 public: | 79 public: |
81 MockSyncApiComponentFactory( | 80 MockSyncApiComponentFactory() {} |
82 scoped_ptr<syncer::AttachmentService> attachment_service) | |
83 : attachment_service_(attachment_service.Pass()) {} | |
84 | 81 |
85 base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType( | 82 base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType( |
86 syncer::ModelType type) override { | 83 syncer::ModelType type) override { |
87 // Shouldn't be called for this test. | 84 // Shouldn't be called for this test. |
88 NOTREACHED(); | 85 NOTREACHED(); |
89 return base::WeakPtr<syncer::SyncableService>(); | 86 return base::WeakPtr<syncer::SyncableService>(); |
90 } | 87 } |
91 | 88 |
92 scoped_ptr<syncer::AttachmentService> CreateAttachmentService( | 89 scoped_ptr<syncer::AttachmentService> CreateAttachmentService( |
93 const scoped_refptr<syncer::AttachmentStore>& attachment_store, | 90 scoped_ptr<syncer::AttachmentStore> attachment_store, |
94 const syncer::UserShare& user_share, | 91 const syncer::UserShare& user_share, |
95 const std::string& store_birthday, | 92 const std::string& store_birthday, |
96 syncer::ModelType model_type, | 93 syncer::ModelType model_type, |
97 syncer::AttachmentService::Delegate* delegate) override { | 94 syncer::AttachmentService::Delegate* delegate) override { |
98 EXPECT_TRUE(attachment_service_ != NULL); | 95 scoped_ptr<MockAttachmentService> attachment_service( |
99 return attachment_service_.Pass(); | 96 new MockAttachmentService(attachment_store.Pass())); |
| 97 // GenericChangeProcessor takes ownership of the AttachmentService, but we |
| 98 // need to have a pointer to it so we can see that it was used properly. |
| 99 // Take a pointer and trust that GenericChangeProcessor does not prematurely |
| 100 // destroy it. |
| 101 mock_attachment_service_ = attachment_service.get(); |
| 102 return attachment_service.Pass(); |
| 103 } |
| 104 |
| 105 MockAttachmentService* GetMockAttachmentService() { |
| 106 return mock_attachment_service_; |
100 } | 107 } |
101 | 108 |
102 private: | 109 private: |
103 scoped_ptr<syncer::AttachmentService> attachment_service_; | 110 MockAttachmentService* mock_attachment_service_; |
104 }; | 111 }; |
105 | 112 |
106 class SyncGenericChangeProcessorTest : public testing::Test { | 113 class SyncGenericChangeProcessorTest : public testing::Test { |
107 public: | 114 public: |
108 // Most test cases will use this type. For those that need a | 115 // Most test cases will use this type. For those that need a |
109 // GenericChangeProcessor for a different type, use |InitializeForType|. | 116 // GenericChangeProcessor for a different type, use |InitializeForType|. |
110 static const syncer::ModelType kType = syncer::PREFERENCES; | 117 static const syncer::ModelType kType = syncer::PREFERENCES; |
111 | 118 |
112 SyncGenericChangeProcessorTest() | 119 SyncGenericChangeProcessorTest() |
113 : syncable_service_ptr_factory_(&fake_syncable_service_), | 120 : syncable_service_ptr_factory_(&fake_syncable_service_), |
(...skipping 28 matching lines...) Expand all Loading... |
142 for (syncer::ModelTypeSet::Iterator iter = types.First(); iter.Good(); | 149 for (syncer::ModelTypeSet::Iterator iter = types.First(); iter.Good(); |
143 iter.Inc()) { | 150 iter.Inc()) { |
144 syncer::TestUserShare::CreateRoot(iter.Get(), | 151 syncer::TestUserShare::CreateRoot(iter.Get(), |
145 test_user_share_->user_share()); | 152 test_user_share_->user_share()); |
146 } | 153 } |
147 test_user_share_->encryption_handler()->Init(); | 154 test_user_share_->encryption_handler()->Init(); |
148 ConstructGenericChangeProcessor(type); | 155 ConstructGenericChangeProcessor(type); |
149 } | 156 } |
150 | 157 |
151 void ConstructGenericChangeProcessor(syncer::ModelType type) { | 158 void ConstructGenericChangeProcessor(syncer::ModelType type) { |
152 scoped_refptr<syncer::AttachmentStore> attachment_store = | 159 MockSyncApiComponentFactory sync_factory; |
| 160 scoped_ptr<syncer::AttachmentStore> attachment_store = |
153 syncer::AttachmentStore::CreateInMemoryStore(); | 161 syncer::AttachmentStore::CreateInMemoryStore(); |
154 scoped_ptr<MockAttachmentService> mock_attachment_service( | 162 change_processor_.reset(new GenericChangeProcessor( |
155 new MockAttachmentService(attachment_store)); | 163 type, &data_type_error_handler_, |
156 // GenericChangeProcessor takes ownership of the AttachmentService, but we | 164 syncable_service_ptr_factory_.GetWeakPtr(), |
157 // need to have a pointer to it so we can see that it was used properly. | 165 merge_result_ptr_factory_->GetWeakPtr(), test_user_share_->user_share(), |
158 // Take a pointer and trust that GenericChangeProcessor does not prematurely | 166 &sync_factory, attachment_store.Pass())); |
159 // destroy it. | 167 mock_attachment_service_ = sync_factory.GetMockAttachmentService(); |
160 mock_attachment_service_ = mock_attachment_service.get(); | |
161 sync_factory_.reset( | |
162 new MockSyncApiComponentFactory(mock_attachment_service.Pass())); | |
163 change_processor_.reset( | |
164 new GenericChangeProcessor(type, | |
165 &data_type_error_handler_, | |
166 syncable_service_ptr_factory_.GetWeakPtr(), | |
167 merge_result_ptr_factory_->GetWeakPtr(), | |
168 test_user_share_->user_share(), | |
169 sync_factory_.get(), | |
170 attachment_store)); | |
171 } | 168 } |
172 | 169 |
173 void BuildChildNodes(syncer::ModelType type, int n) { | 170 void BuildChildNodes(syncer::ModelType type, int n) { |
174 syncer::WriteTransaction trans(FROM_HERE, user_share()); | 171 syncer::WriteTransaction trans(FROM_HERE, user_share()); |
175 syncer::ReadNode root(&trans); | 172 syncer::ReadNode root(&trans); |
176 ASSERT_EQ(syncer::BaseNode::INIT_OK, root.InitTypeRoot(type)); | 173 ASSERT_EQ(syncer::BaseNode::INIT_OK, root.InitTypeRoot(type)); |
177 for (int i = 0; i < n; ++i) { | 174 for (int i = 0; i < n; ++i) { |
178 syncer::WriteNode node(&trans); | 175 syncer::WriteNode node(&trans); |
179 node.InitUniqueByCreation(type, root, base::StringPrintf("node%05d", i)); | 176 node.InitUniqueByCreation(type, root, base::StringPrintf("node%05d", i)); |
180 } | 177 } |
(...skipping 23 matching lines...) Expand all Loading... |
204 scoped_ptr<base::WeakPtrFactory<syncer::SyncMergeResult> > | 201 scoped_ptr<base::WeakPtrFactory<syncer::SyncMergeResult> > |
205 merge_result_ptr_factory_; | 202 merge_result_ptr_factory_; |
206 | 203 |
207 syncer::FakeSyncableService fake_syncable_service_; | 204 syncer::FakeSyncableService fake_syncable_service_; |
208 base::WeakPtrFactory<syncer::FakeSyncableService> | 205 base::WeakPtrFactory<syncer::FakeSyncableService> |
209 syncable_service_ptr_factory_; | 206 syncable_service_ptr_factory_; |
210 | 207 |
211 DataTypeErrorHandlerMock data_type_error_handler_; | 208 DataTypeErrorHandlerMock data_type_error_handler_; |
212 scoped_ptr<syncer::TestUserShare> test_user_share_; | 209 scoped_ptr<syncer::TestUserShare> test_user_share_; |
213 MockAttachmentService* mock_attachment_service_; | 210 MockAttachmentService* mock_attachment_service_; |
214 scoped_ptr<SyncApiComponentFactory> sync_factory_; | |
215 | 211 |
216 scoped_ptr<GenericChangeProcessor> change_processor_; | 212 scoped_ptr<GenericChangeProcessor> change_processor_; |
217 }; | 213 }; |
218 | 214 |
219 // Similar to above, but focused on the method that implements sync/api | 215 // Similar to above, but focused on the method that implements sync/api |
220 // interfaces and is hence exposed to datatypes directly. | 216 // interfaces and is hence exposed to datatypes directly. |
221 TEST_F(SyncGenericChangeProcessorTest, StressGetAllSyncData) { | 217 TEST_F(SyncGenericChangeProcessorTest, StressGetAllSyncData) { |
222 const int kNumChildNodes = 1000; | 218 const int kNumChildNodes = 1000; |
223 const int kRepeatCount = 1; | 219 const int kRepeatCount = 1; |
224 | 220 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 // AttachmentService to upload id1 only. | 474 // AttachmentService to upload id1 only. |
479 ConstructGenericChangeProcessor(kType); | 475 ConstructGenericChangeProcessor(kType); |
480 ASSERT_EQ(1U, mock_attachment_service()->attachment_id_sets()->size()); | 476 ASSERT_EQ(1U, mock_attachment_service()->attachment_id_sets()->size()); |
481 ASSERT_THAT(mock_attachment_service()->attachment_id_sets()->front(), | 477 ASSERT_THAT(mock_attachment_service()->attachment_id_sets()->front(), |
482 testing::UnorderedElementsAre(id1)); | 478 testing::UnorderedElementsAre(id1)); |
483 } | 479 } |
484 | 480 |
485 } // namespace | 481 } // namespace |
486 | 482 |
487 } // namespace sync_driver | 483 } // namespace sync_driver |
OLD | NEW |