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 "sync/internal_api/public/attachments/attachment_service_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_service_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
18 #include "base/timer/mock_timer.h" | 19 #include "base/timer/mock_timer.h" |
19 #include "sync/api/attachments/attachment_store_backend.h" | 20 #include "sync/api/attachments/attachment_store_backend.h" |
20 #include "sync/internal_api/public/attachments/attachment_util.h" | 21 #include "sync/internal_api/public/attachments/attachment_util.h" |
21 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | 22 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" |
22 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 23 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
23 #include "testing/gmock/include/gmock/gmock-matchers.h" | 24 #include "testing/gmock/include/gmock/gmock-matchers.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 81 |
81 // Respond to Read request. Attachments found in local_attachments should be | 82 // Respond to Read request. Attachments found in local_attachments should be |
82 // returned, everything else should be reported unavailable. | 83 // returned, everything else should be reported unavailable. |
83 void RespondToRead(const AttachmentIdSet& local_attachments) { | 84 void RespondToRead(const AttachmentIdSet& local_attachments) { |
84 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); | 85 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
85 AttachmentStore::ReadCallback callback = read_callbacks.back(); | 86 AttachmentStore::ReadCallback callback = read_callbacks.back(); |
86 AttachmentIdList ids = read_ids.back(); | 87 AttachmentIdList ids = read_ids.back(); |
87 read_callbacks.pop_back(); | 88 read_callbacks.pop_back(); |
88 read_ids.pop_back(); | 89 read_ids.pop_back(); |
89 | 90 |
90 scoped_ptr<AttachmentMap> attachments(new AttachmentMap()); | 91 std::unique_ptr<AttachmentMap> attachments(new AttachmentMap()); |
91 scoped_ptr<AttachmentIdList> unavailable_attachments( | 92 std::unique_ptr<AttachmentIdList> unavailable_attachments( |
92 new AttachmentIdList()); | 93 new AttachmentIdList()); |
93 for (AttachmentIdList::const_iterator iter = ids.begin(); iter != ids.end(); | 94 for (AttachmentIdList::const_iterator iter = ids.begin(); iter != ids.end(); |
94 ++iter) { | 95 ++iter) { |
95 if (local_attachments.find(*iter) != local_attachments.end()) { | 96 if (local_attachments.find(*iter) != local_attachments.end()) { |
96 Attachment attachment = Attachment::CreateFromParts(*iter, data); | 97 Attachment attachment = Attachment::CreateFromParts(*iter, data); |
97 attachments->insert(std::make_pair(*iter, attachment)); | 98 attachments->insert(std::make_pair(*iter, attachment)); |
98 } else { | 99 } else { |
99 unavailable_attachments->push_back(*iter); | 100 unavailable_attachments->push_back(*iter); |
100 } | 101 } |
101 } | 102 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 void DownloadAttachment(const AttachmentId& id, | 142 void DownloadAttachment(const AttachmentId& id, |
142 const DownloadCallback& callback) override { | 143 const DownloadCallback& callback) override { |
143 ASSERT_TRUE(download_requests.find(id) == download_requests.end()); | 144 ASSERT_TRUE(download_requests.find(id) == download_requests.end()); |
144 download_requests.insert(std::make_pair(id, callback)); | 145 download_requests.insert(std::make_pair(id, callback)); |
145 } | 146 } |
146 | 147 |
147 // Multiple requests to download will be active at the same time. | 148 // Multiple requests to download will be active at the same time. |
148 // RespondToDownload should respond to only one of them. | 149 // RespondToDownload should respond to only one of them. |
149 void RespondToDownload(const AttachmentId& id, const DownloadResult& result) { | 150 void RespondToDownload(const AttachmentId& id, const DownloadResult& result) { |
150 ASSERT_TRUE(download_requests.find(id) != download_requests.end()); | 151 ASSERT_TRUE(download_requests.find(id) != download_requests.end()); |
151 scoped_ptr<Attachment> attachment; | 152 std::unique_ptr<Attachment> attachment; |
152 if (result == DOWNLOAD_SUCCESS) { | 153 if (result == DOWNLOAD_SUCCESS) { |
153 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); | 154 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
154 attachment.reset(new Attachment(Attachment::CreateFromParts(id, data))); | 155 attachment.reset(new Attachment(Attachment::CreateFromParts(id, data))); |
155 } | 156 } |
156 base::MessageLoop::current()->PostTask( | 157 base::MessageLoop::current()->PostTask( |
157 FROM_HERE, | 158 FROM_HERE, |
158 base::Bind(download_requests[id], result, base::Passed(&attachment))); | 159 base::Bind(download_requests[id], result, base::Passed(&attachment))); |
159 | 160 |
160 download_requests.erase(id); | 161 download_requests.erase(id); |
161 } | 162 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 196 |
196 } // namespace | 197 } // namespace |
197 | 198 |
198 class AttachmentServiceImplTest : public testing::Test, | 199 class AttachmentServiceImplTest : public testing::Test, |
199 public AttachmentService::Delegate { | 200 public AttachmentService::Delegate { |
200 protected: | 201 protected: |
201 AttachmentServiceImplTest() {} | 202 AttachmentServiceImplTest() {} |
202 | 203 |
203 void SetUp() override { | 204 void SetUp() override { |
204 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); | 205 network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); |
205 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 206 InitializeAttachmentService( |
206 make_scoped_ptr(new MockAttachmentDownloader()), | 207 base::WrapUnique(new MockAttachmentUploader()), |
207 this); | 208 base::WrapUnique(new MockAttachmentDownloader()), this); |
208 } | 209 } |
209 | 210 |
210 void TearDown() override { | 211 void TearDown() override { |
211 attachment_service_.reset(); | 212 attachment_service_.reset(); |
212 RunLoop(); | 213 RunLoop(); |
213 ASSERT_FALSE(attachment_store_backend_); | 214 ASSERT_FALSE(attachment_store_backend_); |
214 ASSERT_FALSE(attachment_uploader_); | 215 ASSERT_FALSE(attachment_uploader_); |
215 ASSERT_FALSE(attachment_downloader_); | 216 ASSERT_FALSE(attachment_downloader_); |
216 } | 217 } |
217 | 218 |
218 // AttachmentService::Delegate implementation. | 219 // AttachmentService::Delegate implementation. |
219 void OnAttachmentUploaded(const AttachmentId& attachment_id) override { | 220 void OnAttachmentUploaded(const AttachmentId& attachment_id) override { |
220 on_attachment_uploaded_list_.push_back(attachment_id); | 221 on_attachment_uploaded_list_.push_back(attachment_id); |
221 } | 222 } |
222 | 223 |
223 void InitializeAttachmentService( | 224 void InitializeAttachmentService( |
224 scoped_ptr<MockAttachmentUploader> uploader, | 225 std::unique_ptr<MockAttachmentUploader> uploader, |
225 scoped_ptr<MockAttachmentDownloader> downloader, | 226 std::unique_ptr<MockAttachmentDownloader> downloader, |
226 AttachmentService::Delegate* delegate) { | 227 AttachmentService::Delegate* delegate) { |
227 // Initialize mock attachment store | 228 // Initialize mock attachment store |
228 scoped_refptr<base::SingleThreadTaskRunner> runner = | 229 scoped_refptr<base::SingleThreadTaskRunner> runner = |
229 base::ThreadTaskRunnerHandle::Get(); | 230 base::ThreadTaskRunnerHandle::Get(); |
230 scoped_ptr<MockAttachmentStoreBackend> attachment_store_backend( | 231 std::unique_ptr<MockAttachmentStoreBackend> attachment_store_backend( |
231 new MockAttachmentStoreBackend(runner)); | 232 new MockAttachmentStoreBackend(runner)); |
232 attachment_store_backend_ = attachment_store_backend->AsWeakPtr(); | 233 attachment_store_backend_ = attachment_store_backend->AsWeakPtr(); |
233 scoped_ptr<AttachmentStore> attachment_store = | 234 std::unique_ptr<AttachmentStore> attachment_store = |
234 AttachmentStore::CreateMockStoreForTest( | 235 AttachmentStore::CreateMockStoreForTest( |
235 std::move(attachment_store_backend)); | 236 std::move(attachment_store_backend)); |
236 | 237 |
237 if (uploader.get()) { | 238 if (uploader.get()) { |
238 attachment_uploader_ = uploader->AsWeakPtr(); | 239 attachment_uploader_ = uploader->AsWeakPtr(); |
239 } | 240 } |
240 if (downloader.get()) { | 241 if (downloader.get()) { |
241 attachment_downloader_ = downloader->AsWeakPtr(); | 242 attachment_downloader_ = downloader->AsWeakPtr(); |
242 } | 243 } |
243 attachment_service_.reset(new AttachmentServiceImpl( | 244 attachment_service_.reset(new AttachmentServiceImpl( |
244 attachment_store->CreateAttachmentStoreForSync(), std::move(uploader), | 245 attachment_store->CreateAttachmentStoreForSync(), std::move(uploader), |
245 std::move(downloader), delegate, base::TimeDelta::FromMinutes(1), | 246 std::move(downloader), delegate, base::TimeDelta::FromMinutes(1), |
246 base::TimeDelta::FromMinutes(8))); | 247 base::TimeDelta::FromMinutes(8))); |
247 | 248 |
248 scoped_ptr<base::MockTimer> timer_to_pass( | 249 std::unique_ptr<base::MockTimer> timer_to_pass( |
249 new base::MockTimer(false, false)); | 250 new base::MockTimer(false, false)); |
250 mock_timer_ = timer_to_pass.get(); | 251 mock_timer_ = timer_to_pass.get(); |
251 attachment_service_->SetTimerForTest(std::move(timer_to_pass)); | 252 attachment_service_->SetTimerForTest(std::move(timer_to_pass)); |
252 } | 253 } |
253 | 254 |
254 AttachmentService* attachment_service() { return attachment_service_.get(); } | 255 AttachmentService* attachment_service() { return attachment_service_.get(); } |
255 | 256 |
256 base::MockTimer* mock_timer() { return mock_timer_; } | 257 base::MockTimer* mock_timer() { return mock_timer_; } |
257 | 258 |
258 AttachmentService::GetOrDownloadCallback download_callback() { | 259 AttachmentService::GetOrDownloadCallback download_callback() { |
259 return base::Bind(&AttachmentServiceImplTest::DownloadDone, | 260 return base::Bind(&AttachmentServiceImplTest::DownloadDone, |
260 base::Unretained(this)); | 261 base::Unretained(this)); |
261 } | 262 } |
262 | 263 |
263 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, | 264 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, |
264 scoped_ptr<AttachmentMap> attachments) { | 265 std::unique_ptr<AttachmentMap> attachments) { |
265 download_results_.push_back(result); | 266 download_results_.push_back(result); |
266 last_download_attachments_ = std::move(attachments); | 267 last_download_attachments_ = std::move(attachments); |
267 } | 268 } |
268 | 269 |
269 void RunLoop() { | 270 void RunLoop() { |
270 base::RunLoop run_loop; | 271 base::RunLoop run_loop; |
271 run_loop.RunUntilIdle(); | 272 run_loop.RunUntilIdle(); |
272 } | 273 } |
273 | 274 |
274 void RunLoopAndFireTimer() { | 275 void RunLoopAndFireTimer() { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 MockAttachmentUploader* uploader() { | 312 MockAttachmentUploader* uploader() { |
312 return attachment_uploader_.get(); | 313 return attachment_uploader_.get(); |
313 } | 314 } |
314 | 315 |
315 const std::vector<AttachmentId>& on_attachment_uploaded_list() const { | 316 const std::vector<AttachmentId>& on_attachment_uploaded_list() const { |
316 return on_attachment_uploaded_list_; | 317 return on_attachment_uploaded_list_; |
317 } | 318 } |
318 | 319 |
319 private: | 320 private: |
320 base::MessageLoop message_loop_; | 321 base::MessageLoop message_loop_; |
321 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 322 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
322 base::WeakPtr<MockAttachmentStoreBackend> attachment_store_backend_; | 323 base::WeakPtr<MockAttachmentStoreBackend> attachment_store_backend_; |
323 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_; | 324 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_; |
324 base::WeakPtr<MockAttachmentUploader> attachment_uploader_; | 325 base::WeakPtr<MockAttachmentUploader> attachment_uploader_; |
325 scoped_ptr<AttachmentServiceImpl> attachment_service_; | 326 std::unique_ptr<AttachmentServiceImpl> attachment_service_; |
326 base::MockTimer* mock_timer_; // not owned | 327 base::MockTimer* mock_timer_; // not owned |
327 | 328 |
328 std::vector<AttachmentService::GetOrDownloadResult> download_results_; | 329 std::vector<AttachmentService::GetOrDownloadResult> download_results_; |
329 scoped_ptr<AttachmentMap> last_download_attachments_; | 330 std::unique_ptr<AttachmentMap> last_download_attachments_; |
330 std::vector<AttachmentId> on_attachment_uploaded_list_; | 331 std::vector<AttachmentId> on_attachment_uploaded_list_; |
331 }; | 332 }; |
332 | 333 |
333 TEST_F(AttachmentServiceImplTest, GetOrDownload_EmptyAttachmentList) { | 334 TEST_F(AttachmentServiceImplTest, GetOrDownload_EmptyAttachmentList) { |
334 AttachmentIdList attachment_ids; | 335 AttachmentIdList attachment_ids; |
335 attachment_service()->GetOrDownloadAttachments(attachment_ids, | 336 attachment_service()->GetOrDownloadAttachments(attachment_ids, |
336 download_callback()); | 337 download_callback()); |
337 RunLoop(); | 338 RunLoop(); |
338 store()->RespondToRead(AttachmentIdSet()); | 339 store()->RespondToRead(AttachmentIdSet()); |
339 | 340 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 last_download_attachments().end()); | 423 last_download_attachments().end()); |
423 EXPECT_TRUE(last_download_attachments().find(attachment_ids[2]) == | 424 EXPECT_TRUE(last_download_attachments().find(attachment_ids[2]) == |
424 last_download_attachments().end()); | 425 last_download_attachments().end()); |
425 EXPECT_TRUE(last_download_attachments().find(attachment_ids[3]) == | 426 EXPECT_TRUE(last_download_attachments().find(attachment_ids[3]) == |
426 last_download_attachments().end()); | 427 last_download_attachments().end()); |
427 } | 428 } |
428 | 429 |
429 TEST_F(AttachmentServiceImplTest, GetOrDownload_NoDownloader) { | 430 TEST_F(AttachmentServiceImplTest, GetOrDownload_NoDownloader) { |
430 // No downloader. | 431 // No downloader. |
431 InitializeAttachmentService( | 432 InitializeAttachmentService( |
432 make_scoped_ptr<MockAttachmentUploader>(new MockAttachmentUploader()), | 433 base::WrapUnique<MockAttachmentUploader>(new MockAttachmentUploader()), |
433 make_scoped_ptr<MockAttachmentDownloader>(NULL), | 434 base::WrapUnique<MockAttachmentDownloader>(NULL), this); |
434 this); | |
435 | 435 |
436 AttachmentIdList attachment_ids; | 436 AttachmentIdList attachment_ids; |
437 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 437 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
438 attachment_service()->GetOrDownloadAttachments(attachment_ids, | 438 attachment_service()->GetOrDownloadAttachments(attachment_ids, |
439 download_callback()); | 439 download_callback()); |
440 RunLoop(); | 440 RunLoop(); |
441 EXPECT_FALSE(store()->read_ids.empty()); | 441 EXPECT_FALSE(store()->read_ids.empty()); |
442 | 442 |
443 AttachmentIdSet local_attachments; | 443 AttachmentIdSet local_attachments; |
444 store()->RespondToRead(local_attachments); | 444 store()->RespondToRead(local_attachments); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 // See that all the attachments were uploaded. | 476 // See that all the attachments were uploaded. |
477 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); | 477 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); |
478 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); | 478 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); |
479 ++iter) { | 479 ++iter) { |
480 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); | 480 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); |
481 } | 481 } |
482 EXPECT_EQ(num_attachments, store()->drop_ids.size()); | 482 EXPECT_EQ(num_attachments, store()->drop_ids.size()); |
483 } | 483 } |
484 | 484 |
485 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { | 485 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { |
486 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 486 InitializeAttachmentService(base::WrapUnique(new MockAttachmentUploader()), |
487 make_scoped_ptr(new MockAttachmentDownloader()), | 487 base::WrapUnique(new MockAttachmentDownloader()), |
488 NULL); // No delegate. | 488 NULL); // No delegate. |
489 | 489 |
490 AttachmentIdList attachment_ids; | 490 AttachmentIdList attachment_ids; |
491 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 491 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
492 attachment_service()->UploadAttachments(attachment_ids); | 492 attachment_service()->UploadAttachments(attachment_ids); |
493 RunLoopAndFireTimer(); | 493 RunLoopAndFireTimer(); |
494 ASSERT_EQ(1U, store()->read_ids.size()); | 494 ASSERT_EQ(1U, store()->read_ids.size()); |
495 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 495 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
496 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); | 496 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
497 RunLoop(); | 497 RunLoop(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 RunLoop(); | 546 RunLoop(); |
547 | 547 |
548 // Nothing uploaded. | 548 // Nothing uploaded. |
549 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 549 EXPECT_EQ(0U, uploader()->upload_requests.size()); |
550 // See that the delegate was never called. | 550 // See that the delegate was never called. |
551 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 551 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
552 EXPECT_EQ(num_attachments, store()->drop_ids.size()); | 552 EXPECT_EQ(num_attachments, store()->drop_ids.size()); |
553 } | 553 } |
554 | 554 |
555 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { | 555 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { |
556 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), | 556 InitializeAttachmentService(base::WrapUnique<MockAttachmentUploader>(NULL), |
557 make_scoped_ptr(new MockAttachmentDownloader()), | 557 base::WrapUnique(new MockAttachmentDownloader()), |
558 this); | 558 this); |
559 | 559 |
560 AttachmentIdList attachment_ids; | 560 AttachmentIdList attachment_ids; |
561 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 561 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
562 attachment_service()->UploadAttachments(attachment_ids); | 562 attachment_service()->UploadAttachments(attachment_ids); |
563 RunLoop(); | 563 RunLoop(); |
564 EXPECT_EQ(0U, store()->read_ids.size()); | 564 EXPECT_EQ(0U, store()->read_ids.size()); |
565 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 565 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
566 EXPECT_EQ(0U, store()->drop_ids.size()); | 566 EXPECT_EQ(0U, store()->drop_ids.size()); |
567 } | 567 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 632 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
633 net::NetworkChangeNotifier::CONNECTION_WIFI); | 633 net::NetworkChangeNotifier::CONNECTION_WIFI); |
634 RunLoop(); | 634 RunLoop(); |
635 | 635 |
636 // No longer in backoff. | 636 // No longer in backoff. |
637 ASSERT_TRUE(mock_timer()->IsRunning()); | 637 ASSERT_TRUE(mock_timer()->IsRunning()); |
638 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); | 638 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); |
639 } | 639 } |
640 | 640 |
641 } // namespace syncer | 641 } // namespace syncer |
OLD | NEW |