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 "base/bind.h" | 7 #include "base/bind.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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if (uploader.get()) { | 180 if (uploader.get()) { |
181 attachment_uploader_ = uploader->AsWeakPtr(); | 181 attachment_uploader_ = uploader->AsWeakPtr(); |
182 } | 182 } |
183 if (downloader.get()) { | 183 if (downloader.get()) { |
184 attachment_downloader_ = downloader->AsWeakPtr(); | 184 attachment_downloader_ = downloader->AsWeakPtr(); |
185 } | 185 } |
186 attachment_service_.reset( | 186 attachment_service_.reset( |
187 new AttachmentServiceImpl(attachment_store.PassAs<AttachmentStore>(), | 187 new AttachmentServiceImpl(attachment_store.PassAs<AttachmentStore>(), |
188 uploader.PassAs<AttachmentUploader>(), | 188 uploader.PassAs<AttachmentUploader>(), |
189 downloader.PassAs<AttachmentDownloader>(), | 189 downloader.PassAs<AttachmentDownloader>(), |
190 delegate)); | 190 delegate, |
| 191 base::TimeDelta(), |
| 192 base::TimeDelta())); |
191 } | 193 } |
192 | 194 |
193 AttachmentService* attachment_service() { return attachment_service_.get(); } | 195 AttachmentService* attachment_service() { return attachment_service_.get(); } |
194 | 196 |
195 AttachmentService::GetOrDownloadCallback download_callback() { | 197 AttachmentService::GetOrDownloadCallback download_callback() { |
196 return base::Bind(&AttachmentServiceImplTest::DownloadDone, | 198 return base::Bind(&AttachmentServiceImplTest::DownloadDone, |
197 base::Unretained(this)); | 199 base::Unretained(this)); |
198 } | 200 } |
199 | 201 |
200 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, | 202 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 AttachmentIdSet local_attachments; | 335 AttachmentIdSet local_attachments; |
334 store()->RespondToRead(local_attachments); | 336 store()->RespondToRead(local_attachments); |
335 RunLoop(); | 337 RunLoop(); |
336 ASSERT_EQ(1U, download_results().size()); | 338 ASSERT_EQ(1U, download_results().size()); |
337 EXPECT_EQ(AttachmentService::GET_UNSPECIFIED_ERROR, download_results()[0]); | 339 EXPECT_EQ(AttachmentService::GET_UNSPECIFIED_ERROR, download_results()[0]); |
338 EXPECT_TRUE(last_download_attachments().empty()); | 340 EXPECT_TRUE(last_download_attachments().empty()); |
339 } | 341 } |
340 | 342 |
341 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { | 343 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { |
342 AttachmentIdSet attachment_ids; | 344 AttachmentIdSet attachment_ids; |
343 const size_t num_attachments = 3; | 345 const unsigned num_attachments = 3; |
344 for (unsigned i = 0; i < num_attachments; ++i) { | 346 for (unsigned i = 0; i < num_attachments; ++i) { |
345 attachment_ids.insert(AttachmentId::Create()); | 347 attachment_ids.insert(AttachmentId::Create()); |
346 } | 348 } |
347 attachment_service()->UploadAttachments(attachment_ids); | 349 attachment_service()->UploadAttachments(attachment_ids); |
348 RunLoop(); | 350 |
349 // See that the service has issued reads for the attachments, but not yet | |
350 // uploaded anything. | |
351 EXPECT_EQ(num_attachments, store()->read_ids.size()); | |
352 EXPECT_EQ(0U, uploader()->upload_requests.size()); | |
353 for (unsigned i = 0; i < num_attachments; ++i) { | 351 for (unsigned i = 0; i < num_attachments; ++i) { |
| 352 RunLoop(); |
| 353 // See that the service has issued a read for at least one of the |
| 354 // attachments. |
| 355 ASSERT_GE(1U, store()->read_ids.size()); |
354 store()->RespondToRead(attachment_ids); | 356 store()->RespondToRead(attachment_ids); |
355 } | 357 RunLoop(); |
356 | 358 ASSERT_GE(1U, uploader()->upload_requests.size()); |
357 RunLoop(); | 359 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
358 EXPECT_EQ(0U, store()->read_ids.size()); | 360 AttachmentUploader::UPLOAD_SUCCESS); |
359 EXPECT_EQ(num_attachments, uploader()->upload_requests.size()); | |
360 AttachmentIdSet::const_iterator iter = attachment_ids.begin(); | |
361 const AttachmentIdSet::const_iterator end = attachment_ids.end(); | |
362 for (; iter != end; ++iter) { | |
363 uploader()->RespondToUpload(*iter, AttachmentUploader::UPLOAD_SUCCESS); | |
364 } | 361 } |
365 RunLoop(); | 362 RunLoop(); |
| 363 ASSERT_EQ(0U, store()->read_ids.size()); |
| 364 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
366 | 365 |
367 // See that all the attachments were uploaded. | 366 // See that all the attachments were uploaded. |
368 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); | 367 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); |
| 368 AttachmentIdSet::const_iterator iter = attachment_ids.begin(); |
| 369 const AttachmentIdSet::const_iterator end = attachment_ids.end(); |
369 for (iter = attachment_ids.begin(); iter != end; ++iter) { | 370 for (iter = attachment_ids.begin(); iter != end; ++iter) { |
370 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); | 371 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); |
371 } | 372 } |
372 } | 373 } |
373 | 374 |
374 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { | 375 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { |
375 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 376 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), |
376 make_scoped_ptr(new MockAttachmentDownloader()), | 377 make_scoped_ptr(new MockAttachmentDownloader()), |
377 NULL); // No delegate. | 378 NULL); // No delegate. |
378 | 379 |
(...skipping 10 matching lines...) Expand all Loading... |
389 uploader()->RespondToUpload(*attachment_ids.begin(), | 390 uploader()->RespondToUpload(*attachment_ids.begin(), |
390 AttachmentUploader::UPLOAD_SUCCESS); | 391 AttachmentUploader::UPLOAD_SUCCESS); |
391 RunLoop(); | 392 RunLoop(); |
392 ASSERT_TRUE(on_attachment_uploaded_list().empty()); | 393 ASSERT_TRUE(on_attachment_uploaded_list().empty()); |
393 } | 394 } |
394 | 395 |
395 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) { | 396 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) { |
396 AttachmentIdSet attachment_ids; | 397 AttachmentIdSet attachment_ids; |
397 attachment_ids.insert(AttachmentId::Create()); | 398 attachment_ids.insert(AttachmentId::Create()); |
398 attachment_ids.insert(AttachmentId::Create()); | 399 attachment_ids.insert(AttachmentId::Create()); |
399 | |
400 attachment_service()->UploadAttachments(attachment_ids); | 400 attachment_service()->UploadAttachments(attachment_ids); |
401 RunLoop(); | 401 RunLoop(); |
402 EXPECT_EQ(2U, store()->read_ids.size()); | 402 ASSERT_GE(1U, store()->read_ids.size()); |
403 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 403 |
| 404 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
404 store()->RespondToRead(attachment_ids); | 405 store()->RespondToRead(attachment_ids); |
405 EXPECT_EQ(1U, store()->read_ids.size()); | |
406 // Not found! | |
407 store()->RespondToRead(AttachmentIdSet()); | |
408 EXPECT_EQ(0U, store()->read_ids.size()); | |
409 RunLoop(); | 406 RunLoop(); |
| 407 ASSERT_EQ(1U, uploader()->upload_requests.size()); |
410 | 408 |
411 // One attachment went missing so we should see only one upload request. | |
412 EXPECT_EQ(1U, uploader()->upload_requests.size()); | |
413 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 409 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
414 AttachmentUploader::UPLOAD_SUCCESS); | 410 AttachmentUploader::UPLOAD_SUCCESS); |
415 RunLoop(); | 411 RunLoop(); |
416 | |
417 // See that the delegate was called for only one. | |
418 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); | 412 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); |
| 413 ASSERT_GE(1U, store()->read_ids.size()); |
| 414 // Not found! |
| 415 store()->RespondToRead(AttachmentIdSet()); |
| 416 RunLoop(); |
| 417 // No upload requests since the read failed. |
| 418 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
419 } | 419 } |
420 | 420 |
421 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { | 421 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { |
422 AttachmentIdSet attachment_ids; | 422 AttachmentIdSet attachment_ids; |
423 attachment_ids.insert(AttachmentId::Create()); | 423 const unsigned num_attachments = 2; |
424 attachment_ids.insert(AttachmentId::Create()); | 424 for (unsigned i = 0; i < num_attachments; ++i) { |
| 425 attachment_ids.insert(AttachmentId::Create()); |
| 426 } |
| 427 attachment_service()->UploadAttachments(attachment_ids); |
425 | 428 |
426 attachment_service()->UploadAttachments(attachment_ids); | 429 for (unsigned i = 0; i < num_attachments; ++i) { |
427 RunLoop(); | 430 RunLoop(); |
428 EXPECT_EQ(2U, store()->read_ids.size()); | 431 ASSERT_GE(1U, store()->read_ids.size()); |
429 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 432 // None found! |
430 // None found! | 433 store()->RespondToRead(AttachmentIdSet()); |
431 store()->RespondToRead(AttachmentIdSet()); | 434 } |
432 store()->RespondToRead(AttachmentIdSet()); | |
433 EXPECT_EQ(0U, store()->read_ids.size()); | |
434 RunLoop(); | 435 RunLoop(); |
435 | 436 |
436 // Nothing uploaded. | 437 // Nothing uploaded. |
437 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 438 EXPECT_EQ(0U, uploader()->upload_requests.size()); |
438 RunLoop(); | |
439 | |
440 // See that the delegate was never called. | 439 // See that the delegate was never called. |
441 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 440 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
442 } | 441 } |
443 | 442 |
444 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { | 443 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { |
445 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), | 444 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), |
446 make_scoped_ptr(new MockAttachmentDownloader()), | 445 make_scoped_ptr(new MockAttachmentDownloader()), |
447 this); | 446 this); |
448 | 447 |
449 AttachmentIdSet attachment_ids; | 448 AttachmentIdSet attachment_ids; |
450 attachment_ids.insert(AttachmentId::Create()); | 449 attachment_ids.insert(AttachmentId::Create()); |
451 attachment_service()->UploadAttachments(attachment_ids); | 450 attachment_service()->UploadAttachments(attachment_ids); |
452 RunLoop(); | 451 RunLoop(); |
453 EXPECT_EQ(0U, store()->read_ids.size()); | 452 EXPECT_EQ(0U, store()->read_ids.size()); |
454 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 453 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
455 } | 454 } |
456 | 455 |
457 // Upload three attachments. For one of them, server responds with error. | 456 // Upload three attachments. For one of them, server responds with error. |
458 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { | 457 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { |
459 AttachmentIdSet attachment_ids; | 458 AttachmentIdSet attachment_ids; |
460 attachment_ids.insert(AttachmentId::Create()); | 459 const unsigned num_attachments = 3; |
461 attachment_ids.insert(AttachmentId::Create()); | 460 for (unsigned i = 0; i < num_attachments; ++i) { |
462 attachment_ids.insert(AttachmentId::Create()); | 461 attachment_ids.insert(AttachmentId::Create()); |
| 462 } |
| 463 attachment_service()->UploadAttachments(attachment_ids); |
463 | 464 |
464 attachment_service()->UploadAttachments(attachment_ids); | 465 for (unsigned i = 0; i < num_attachments; ++i) { |
| 466 RunLoop(); |
| 467 ASSERT_GE(1U, store()->read_ids.size()); |
| 468 store()->RespondToRead(attachment_ids); |
| 469 RunLoop(); |
| 470 ASSERT_EQ(1U, uploader()->upload_requests.size()); |
| 471 AttachmentUploader::UploadResult result = |
| 472 AttachmentUploader::UPLOAD_SUCCESS; |
| 473 // Fail the 2nd one. |
| 474 if (i == 2U) { |
| 475 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR; |
| 476 } else { |
| 477 result = AttachmentUploader::UPLOAD_SUCCESS; |
| 478 } |
| 479 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 480 result); |
| 481 } |
465 RunLoop(); | 482 RunLoop(); |
466 EXPECT_EQ(3U, store()->read_ids.size()); | 483 ASSERT_EQ(2U, on_attachment_uploaded_list().size()); |
467 EXPECT_EQ(0U, uploader()->upload_requests.size()); | |
468 | |
469 // All attachments found. | |
470 store()->RespondToRead(attachment_ids); | |
471 store()->RespondToRead(attachment_ids); | |
472 store()->RespondToRead(attachment_ids); | |
473 RunLoop(); | |
474 | |
475 EXPECT_EQ(3U, uploader()->upload_requests.size()); | |
476 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | |
477 AttachmentUploader::UPLOAD_SUCCESS); | |
478 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | |
479 AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR); | |
480 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | |
481 AttachmentUploader::UPLOAD_SUCCESS); | |
482 EXPECT_EQ(0U, uploader()->upload_requests.size()); | |
483 RunLoop(); | |
484 | |
485 EXPECT_EQ(2U, on_attachment_uploaded_list().size()); | |
486 } | 484 } |
487 | 485 |
488 } // namespace syncer | 486 } // namespace syncer |
OLD | NEW |