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