| 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 21 matching lines...) Expand all Loading... |
| 32 ~MockAttachmentStoreBackend() override {} | 32 ~MockAttachmentStoreBackend() override {} |
| 33 | 33 |
| 34 void Init(const AttachmentStore::InitCallback& callback) override {} | 34 void Init(const AttachmentStore::InitCallback& callback) override {} |
| 35 | 35 |
| 36 void Read(const AttachmentIdList& ids, | 36 void Read(const AttachmentIdList& ids, |
| 37 const AttachmentStore::ReadCallback& callback) override { | 37 const AttachmentStore::ReadCallback& callback) override { |
| 38 read_ids.push_back(ids); | 38 read_ids.push_back(ids); |
| 39 read_callbacks.push_back(callback); | 39 read_callbacks.push_back(callback); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void Write(AttachmentStore::AttachmentReferrer referrer, | 42 void Write(AttachmentStore::Component component, |
| 43 const AttachmentList& attachments, | 43 const AttachmentList& attachments, |
| 44 const AttachmentStore::WriteCallback& callback) override { | 44 const AttachmentStore::WriteCallback& callback) override { |
| 45 write_attachments.push_back(attachments); | 45 write_attachments.push_back(attachments); |
| 46 write_callbacks.push_back(callback); | 46 write_callbacks.push_back(callback); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void Drop(AttachmentStore::AttachmentReferrer referrer, | 49 void SetReference(AttachmentStore::Component component, |
| 50 const AttachmentIdList& ids, | 50 const AttachmentIdList& ids) override { |
| 51 const AttachmentStore::DropCallback& callback) override { | 51 set_reference_ids.push_back(ids); |
| 52 NOTREACHED(); | 52 } |
| 53 |
| 54 void DropReference(AttachmentStore::Component component, |
| 55 const AttachmentIdList& ids, |
| 56 const AttachmentStore::DropCallback& callback) override { |
| 57 ASSERT_EQ(AttachmentStore::SYNC, component); |
| 58 drop_ids.push_back(ids); |
| 53 } | 59 } |
| 54 | 60 |
| 55 void ReadMetadata( | 61 void ReadMetadata( |
| 56 const AttachmentIdList& ids, | 62 const AttachmentIdList& ids, |
| 57 const AttachmentStore::ReadMetadataCallback& callback) override { | 63 const AttachmentStore::ReadMetadataCallback& callback) override { |
| 58 NOTREACHED(); | 64 NOTREACHED(); |
| 59 } | 65 } |
| 60 | 66 |
| 61 void ReadAllMetadata( | 67 void ReadAllMetadata( |
| 62 AttachmentStore::AttachmentReferrer referrer, | 68 AttachmentStore::Component component, |
| 63 const AttachmentStore::ReadMetadataCallback& callback) override { | 69 const AttachmentStore::ReadMetadataCallback& callback) override { |
| 64 NOTREACHED(); | 70 NOTREACHED(); |
| 65 } | 71 } |
| 66 | 72 |
| 67 // Respond to Read request. Attachments found in local_attachments should be | 73 // Respond to Read request. Attachments found in local_attachments should be |
| 68 // returned, everything else should be reported unavailable. | 74 // returned, everything else should be reported unavailable. |
| 69 void RespondToRead(const AttachmentIdSet& local_attachments) { | 75 void RespondToRead(const AttachmentIdSet& local_attachments) { |
| 70 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); | 76 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
| 71 AttachmentStore::ReadCallback callback = read_callbacks.back(); | 77 AttachmentStore::ReadCallback callback = read_callbacks.back(); |
| 72 AttachmentIdList ids = read_ids.back(); | 78 AttachmentIdList ids = read_ids.back(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 93 FROM_HERE, | 99 FROM_HERE, |
| 94 base::Bind(callback, | 100 base::Bind(callback, |
| 95 result, | 101 result, |
| 96 base::Passed(&attachments), | 102 base::Passed(&attachments), |
| 97 base::Passed(&unavailable_attachments))); | 103 base::Passed(&unavailable_attachments))); |
| 98 } | 104 } |
| 99 | 105 |
| 100 // Respond to Write request with |result|. | 106 // Respond to Write request with |result|. |
| 101 void RespondToWrite(const AttachmentStore::Result& result) { | 107 void RespondToWrite(const AttachmentStore::Result& result) { |
| 102 AttachmentStore::WriteCallback callback = write_callbacks.back(); | 108 AttachmentStore::WriteCallback callback = write_callbacks.back(); |
| 103 AttachmentList attachments = write_attachments.back(); | |
| 104 write_callbacks.pop_back(); | 109 write_callbacks.pop_back(); |
| 105 write_attachments.pop_back(); | 110 write_attachments.pop_back(); |
| 106 base::MessageLoop::current()->PostTask(FROM_HERE, | 111 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 107 base::Bind(callback, result)); | 112 base::Bind(callback, result)); |
| 108 } | 113 } |
| 109 | 114 |
| 110 std::vector<AttachmentIdList> read_ids; | 115 std::vector<AttachmentIdList> read_ids; |
| 111 std::vector<AttachmentStore::ReadCallback> read_callbacks; | 116 std::vector<AttachmentStore::ReadCallback> read_callbacks; |
| 112 std::vector<AttachmentList> write_attachments; | 117 std::vector<AttachmentList> write_attachments; |
| 113 std::vector<AttachmentStore::WriteCallback> write_callbacks; | 118 std::vector<AttachmentStore::WriteCallback> write_callbacks; |
| 119 std::vector<AttachmentIdList> set_reference_ids; |
| 120 std::vector<AttachmentIdList> drop_ids; |
| 114 | 121 |
| 115 private: | 122 private: |
| 116 DISALLOW_COPY_AND_ASSIGN(MockAttachmentStoreBackend); | 123 DISALLOW_COPY_AND_ASSIGN(MockAttachmentStoreBackend); |
| 117 }; | 124 }; |
| 118 | 125 |
| 119 class MockAttachmentDownloader | 126 class MockAttachmentDownloader |
| 120 : public AttachmentDownloader, | 127 : public AttachmentDownloader, |
| 121 public base::SupportsWeakPtr<MockAttachmentDownloader> { | 128 public base::SupportsWeakPtr<MockAttachmentDownloader> { |
| 122 public: | 129 public: |
| 123 MockAttachmentDownloader() {} | 130 MockAttachmentDownloader() {} |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 AttachmentStore::CreateMockStoreForTest( | 223 AttachmentStore::CreateMockStoreForTest( |
| 217 attachment_store_backend.Pass()); | 224 attachment_store_backend.Pass()); |
| 218 | 225 |
| 219 if (uploader.get()) { | 226 if (uploader.get()) { |
| 220 attachment_uploader_ = uploader->AsWeakPtr(); | 227 attachment_uploader_ = uploader->AsWeakPtr(); |
| 221 } | 228 } |
| 222 if (downloader.get()) { | 229 if (downloader.get()) { |
| 223 attachment_downloader_ = downloader->AsWeakPtr(); | 230 attachment_downloader_ = downloader->AsWeakPtr(); |
| 224 } | 231 } |
| 225 attachment_service_.reset(new AttachmentServiceImpl( | 232 attachment_service_.reset(new AttachmentServiceImpl( |
| 226 attachment_store.Pass(), uploader.Pass(), downloader.Pass(), delegate, | 233 attachment_store->CreateAttachmentStoreForSync(), uploader.Pass(), |
| 227 base::TimeDelta::FromMinutes(1), base::TimeDelta::FromMinutes(8))); | 234 downloader.Pass(), delegate, base::TimeDelta::FromMinutes(1), |
| 235 base::TimeDelta::FromMinutes(8))); |
| 228 | 236 |
| 229 scoped_ptr<base::MockTimer> timer_to_pass( | 237 scoped_ptr<base::MockTimer> timer_to_pass( |
| 230 new base::MockTimer(false, false)); | 238 new base::MockTimer(false, false)); |
| 231 mock_timer_ = timer_to_pass.get(); | 239 mock_timer_ = timer_to_pass.get(); |
| 232 attachment_service_->SetTimerForTest(timer_to_pass.Pass()); | 240 attachment_service_->SetTimerForTest(timer_to_pass.Pass()); |
| 233 } | 241 } |
| 234 | 242 |
| 235 AttachmentService* attachment_service() { return attachment_service_.get(); } | 243 AttachmentService* attachment_service() { return attachment_service_.get(); } |
| 236 | 244 |
| 237 base::MockTimer* mock_timer() { return mock_timer_; } | 245 base::MockTimer* mock_timer() { return mock_timer_; } |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 EXPECT_TRUE(last_download_attachments().empty()); | 435 EXPECT_TRUE(last_download_attachments().empty()); |
| 428 } | 436 } |
| 429 | 437 |
| 430 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { | 438 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { |
| 431 AttachmentIdList attachment_ids; | 439 AttachmentIdList attachment_ids; |
| 432 const unsigned num_attachments = 3; | 440 const unsigned num_attachments = 3; |
| 433 for (unsigned i = 0; i < num_attachments; ++i) { | 441 for (unsigned i = 0; i < num_attachments; ++i) { |
| 434 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 442 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 435 } | 443 } |
| 436 attachment_service()->UploadAttachments(attachment_ids); | 444 attachment_service()->UploadAttachments(attachment_ids); |
| 437 | 445 RunLoop(); |
| 446 EXPECT_FALSE(store()->set_reference_ids.empty()); |
| 438 for (unsigned i = 0; i < num_attachments; ++i) { | 447 for (unsigned i = 0; i < num_attachments; ++i) { |
| 439 RunLoopAndFireTimer(); | 448 RunLoopAndFireTimer(); |
| 440 // See that the service has issued a read for at least one of the | 449 // See that the service has issued a read for at least one of the |
| 441 // attachments. | 450 // attachments. |
| 442 ASSERT_GE(store()->read_ids.size(), 1U); | 451 ASSERT_GE(store()->read_ids.size(), 1U); |
| 443 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); | 452 store()->RespondToRead(AttachmentIdSetFromList(attachment_ids)); |
| 444 RunLoop(); | 453 RunLoop(); |
| 445 ASSERT_GE(uploader()->upload_requests.size(), 1U); | 454 ASSERT_GE(uploader()->upload_requests.size(), 1U); |
| 446 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 455 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 447 AttachmentUploader::UPLOAD_SUCCESS); | 456 AttachmentUploader::UPLOAD_SUCCESS); |
| 448 } | 457 } |
| 449 RunLoop(); | 458 RunLoop(); |
| 450 ASSERT_EQ(0U, store()->read_ids.size()); | 459 ASSERT_EQ(0U, store()->read_ids.size()); |
| 451 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 460 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
| 452 | 461 |
| 453 // See that all the attachments were uploaded. | 462 // See that all the attachments were uploaded. |
| 454 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); | 463 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); |
| 455 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); | 464 for (auto iter = attachment_ids.begin(); iter != attachment_ids.end(); |
| 456 ++iter) { | 465 ++iter) { |
| 457 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); | 466 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); |
| 458 } | 467 } |
| 468 EXPECT_EQ(num_attachments, store()->drop_ids.size()); |
| 459 } | 469 } |
| 460 | 470 |
| 461 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { | 471 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { |
| 462 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 472 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), |
| 463 make_scoped_ptr(new MockAttachmentDownloader()), | 473 make_scoped_ptr(new MockAttachmentDownloader()), |
| 464 NULL); // No delegate. | 474 NULL); // No delegate. |
| 465 | 475 |
| 466 AttachmentIdList attachment_ids; | 476 AttachmentIdList attachment_ids; |
| 467 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 477 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 468 attachment_service()->UploadAttachments(attachment_ids); | 478 attachment_service()->UploadAttachments(attachment_ids); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 495 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 505 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 496 AttachmentUploader::UPLOAD_SUCCESS); | 506 AttachmentUploader::UPLOAD_SUCCESS); |
| 497 RunLoopAndFireTimer(); | 507 RunLoopAndFireTimer(); |
| 498 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); | 508 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); |
| 499 ASSERT_GE(store()->read_ids.size(), 1U); | 509 ASSERT_GE(store()->read_ids.size(), 1U); |
| 500 // Not found! | 510 // Not found! |
| 501 store()->RespondToRead(AttachmentIdSet()); | 511 store()->RespondToRead(AttachmentIdSet()); |
| 502 RunLoop(); | 512 RunLoop(); |
| 503 // No upload requests since the read failed. | 513 // No upload requests since the read failed. |
| 504 ASSERT_EQ(0U, uploader()->upload_requests.size()); | 514 ASSERT_EQ(0U, uploader()->upload_requests.size()); |
| 515 EXPECT_EQ(attachment_ids.size(), store()->drop_ids.size()); |
| 505 } | 516 } |
| 506 | 517 |
| 507 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { | 518 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { |
| 508 AttachmentIdList attachment_ids; | 519 AttachmentIdList attachment_ids; |
| 509 const unsigned num_attachments = 2; | 520 const unsigned num_attachments = 2; |
| 510 for (unsigned i = 0; i < num_attachments; ++i) { | 521 for (unsigned i = 0; i < num_attachments; ++i) { |
| 511 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 522 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 512 } | 523 } |
| 513 attachment_service()->UploadAttachments(attachment_ids); | 524 attachment_service()->UploadAttachments(attachment_ids); |
| 514 | 525 |
| 515 for (unsigned i = 0; i < num_attachments; ++i) { | 526 for (unsigned i = 0; i < num_attachments; ++i) { |
| 516 RunLoopAndFireTimer(); | 527 RunLoopAndFireTimer(); |
| 517 ASSERT_GE(store()->read_ids.size(), 1U); | 528 ASSERT_GE(store()->read_ids.size(), 1U); |
| 518 // None found! | 529 // None found! |
| 519 store()->RespondToRead(AttachmentIdSet()); | 530 store()->RespondToRead(AttachmentIdSet()); |
| 520 } | 531 } |
| 521 RunLoop(); | 532 RunLoop(); |
| 522 | 533 |
| 523 // Nothing uploaded. | 534 // Nothing uploaded. |
| 524 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 535 EXPECT_EQ(0U, uploader()->upload_requests.size()); |
| 525 // See that the delegate was never called. | 536 // See that the delegate was never called. |
| 526 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 537 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
| 538 EXPECT_EQ(num_attachments, store()->drop_ids.size()); |
| 527 } | 539 } |
| 528 | 540 |
| 529 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { | 541 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { |
| 530 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), | 542 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), |
| 531 make_scoped_ptr(new MockAttachmentDownloader()), | 543 make_scoped_ptr(new MockAttachmentDownloader()), |
| 532 this); | 544 this); |
| 533 | 545 |
| 534 AttachmentIdList attachment_ids; | 546 AttachmentIdList attachment_ids; |
| 535 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 547 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 536 attachment_service()->UploadAttachments(attachment_ids); | 548 attachment_service()->UploadAttachments(attachment_ids); |
| 537 RunLoop(); | 549 RunLoop(); |
| 538 EXPECT_EQ(0U, store()->read_ids.size()); | 550 EXPECT_EQ(0U, store()->read_ids.size()); |
| 539 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 551 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); |
| 552 EXPECT_EQ(0U, store()->drop_ids.size()); |
| 540 } | 553 } |
| 541 | 554 |
| 542 // Upload three attachments. For one of them, server responds with error. | 555 // Upload three attachments. For one of them, server responds with error. |
| 543 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { | 556 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { |
| 544 AttachmentIdList attachment_ids; | 557 AttachmentIdList attachment_ids; |
| 545 const unsigned num_attachments = 3; | 558 const unsigned num_attachments = 3; |
| 546 for (unsigned i = 0; i < num_attachments; ++i) { | 559 for (unsigned i = 0; i < num_attachments; ++i) { |
| 547 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 560 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 548 } | 561 } |
| 549 attachment_service()->UploadAttachments(attachment_ids); | 562 attachment_service()->UploadAttachments(attachment_ids); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 560 if (i == 2U) { | 573 if (i == 2U) { |
| 561 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR; | 574 result = AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR; |
| 562 } else { | 575 } else { |
| 563 result = AttachmentUploader::UPLOAD_SUCCESS; | 576 result = AttachmentUploader::UPLOAD_SUCCESS; |
| 564 } | 577 } |
| 565 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, | 578 uploader()->RespondToUpload(uploader()->upload_requests.begin()->first, |
| 566 result); | 579 result); |
| 567 RunLoop(); | 580 RunLoop(); |
| 568 } | 581 } |
| 569 ASSERT_EQ(2U, on_attachment_uploaded_list().size()); | 582 ASSERT_EQ(2U, on_attachment_uploaded_list().size()); |
| 583 EXPECT_EQ(num_attachments, store()->drop_ids.size()); |
| 570 } | 584 } |
| 571 | 585 |
| 572 // Attempt an upload, respond with transient error to trigger backoff, issue | 586 // Attempt an upload, respond with transient error to trigger backoff, issue |
| 573 // network disconnect/connect events and see that backoff is cleared. | 587 // network disconnect/connect events and see that backoff is cleared. |
| 574 TEST_F(AttachmentServiceImplTest, | 588 TEST_F(AttachmentServiceImplTest, |
| 575 UploadAttachments_ResetBackoffAfterNetworkChange) { | 589 UploadAttachments_ResetBackoffAfterNetworkChange) { |
| 576 AttachmentIdList attachment_ids; | 590 AttachmentIdList attachment_ids; |
| 577 attachment_ids.push_back(AttachmentId::Create(0, 0)); | 591 attachment_ids.push_back(AttachmentId::Create(0, 0)); |
| 578 attachment_service()->UploadAttachments(attachment_ids); | 592 attachment_service()->UploadAttachments(attachment_ids); |
| 579 | 593 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 604 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 618 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
| 605 net::NetworkChangeNotifier::CONNECTION_WIFI); | 619 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 606 RunLoop(); | 620 RunLoop(); |
| 607 | 621 |
| 608 // No longer in backoff. | 622 // No longer in backoff. |
| 609 ASSERT_TRUE(mock_timer()->IsRunning()); | 623 ASSERT_TRUE(mock_timer()->IsRunning()); |
| 610 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); | 624 ASSERT_EQ(base::TimeDelta(), mock_timer()->GetCurrentDelay()); |
| 611 } | 625 } |
| 612 | 626 |
| 613 } // namespace syncer | 627 } // namespace syncer |
| OLD | NEW |