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