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" |
11 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" | 11 #include "sync/internal_api/public/attachments/fake_attachment_downloader.h" |
12 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" | 12 #include "sync/internal_api/public/attachments/fake_attachment_uploader.h" |
13 #include "testing/gmock/include/gmock/gmock-matchers.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 | 14 |
16 namespace syncer { | 15 namespace syncer { |
17 | 16 |
18 class MockAttachmentStore : public AttachmentStore, | 17 class MockAttachmentStore : public AttachmentStore, |
19 public base::SupportsWeakPtr<MockAttachmentStore> { | 18 public base::SupportsWeakPtr<MockAttachmentStore> { |
20 public: | 19 public: |
21 MockAttachmentStore() {} | 20 MockAttachmentStore() {} |
22 | 21 |
23 virtual void Read(const AttachmentIdList& ids, | 22 virtual void Read(const AttachmentIdList& ids, |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 delegate)); | 189 delegate)); |
191 } | 190 } |
192 | 191 |
193 AttachmentService* attachment_service() { return attachment_service_.get(); } | 192 AttachmentService* attachment_service() { return attachment_service_.get(); } |
194 | 193 |
195 AttachmentService::GetOrDownloadCallback download_callback() { | 194 AttachmentService::GetOrDownloadCallback download_callback() { |
196 return base::Bind(&AttachmentServiceImplTest::DownloadDone, | 195 return base::Bind(&AttachmentServiceImplTest::DownloadDone, |
197 base::Unretained(this)); | 196 base::Unretained(this)); |
198 } | 197 } |
199 | 198 |
| 199 AttachmentService::StoreCallback store_callback() { |
| 200 return base::Bind(&AttachmentServiceImplTest::StoreDone, |
| 201 base::Unretained(this)); |
| 202 } |
| 203 |
200 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, | 204 void DownloadDone(const AttachmentService::GetOrDownloadResult& result, |
201 scoped_ptr<AttachmentMap> attachments) { | 205 scoped_ptr<AttachmentMap> attachments) { |
202 download_results_.push_back(result); | 206 download_results_.push_back(result); |
203 last_download_attachments_ = attachments.Pass(); | 207 last_download_attachments_ = attachments.Pass(); |
204 } | 208 } |
205 | 209 |
| 210 void StoreDone(const AttachmentService::StoreResult& result) { |
| 211 store_results_.push_back(result); |
| 212 } |
| 213 |
206 void RunLoop() { | 214 void RunLoop() { |
207 base::RunLoop run_loop; | 215 base::RunLoop run_loop; |
208 run_loop.RunUntilIdle(); | 216 run_loop.RunUntilIdle(); |
209 } | 217 } |
210 | 218 |
211 const std::vector<AttachmentService::GetOrDownloadResult>& | 219 const std::vector<AttachmentService::GetOrDownloadResult>& |
212 download_results() const { | 220 download_results() const { |
213 return download_results_; | 221 return download_results_; |
214 } | 222 } |
215 | 223 |
216 const AttachmentMap& last_download_attachments() const { | 224 const AttachmentMap& last_download_attachments() const { |
217 return *last_download_attachments_.get(); | 225 return *last_download_attachments_.get(); |
218 } | 226 } |
219 | 227 |
| 228 const std::vector<AttachmentService::StoreResult>& store_results() const { |
| 229 return store_results_; |
| 230 } |
| 231 |
220 MockAttachmentStore* store() { return attachment_store_.get(); } | 232 MockAttachmentStore* store() { return attachment_store_.get(); } |
221 | 233 |
222 MockAttachmentDownloader* downloader() { | 234 MockAttachmentDownloader* downloader() { |
223 return attachment_downloader_.get(); | 235 return attachment_downloader_.get(); |
224 } | 236 } |
225 | 237 |
226 MockAttachmentUploader* uploader() { | 238 MockAttachmentUploader* uploader() { |
227 return attachment_uploader_.get(); | 239 return attachment_uploader_.get(); |
228 } | 240 } |
229 | 241 |
230 const std::vector<AttachmentId>& on_attachment_uploaded_list() const { | 242 const std::vector<AttachmentId>& on_attachment_uploaded_list() const { |
231 return on_attachment_uploaded_list_; | 243 return on_attachment_uploaded_list_; |
232 } | 244 } |
233 | 245 |
234 private: | 246 private: |
235 base::MessageLoop message_loop_; | 247 base::MessageLoop message_loop_; |
236 base::WeakPtr<MockAttachmentStore> attachment_store_; | 248 base::WeakPtr<MockAttachmentStore> attachment_store_; |
237 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_; | 249 base::WeakPtr<MockAttachmentDownloader> attachment_downloader_; |
238 base::WeakPtr<MockAttachmentUploader> attachment_uploader_; | 250 base::WeakPtr<MockAttachmentUploader> attachment_uploader_; |
239 scoped_ptr<AttachmentService> attachment_service_; | 251 scoped_ptr<AttachmentService> attachment_service_; |
240 | 252 |
241 std::vector<AttachmentService::GetOrDownloadResult> download_results_; | 253 std::vector<AttachmentService::GetOrDownloadResult> download_results_; |
242 scoped_ptr<AttachmentMap> last_download_attachments_; | 254 scoped_ptr<AttachmentMap> last_download_attachments_; |
243 std::vector<AttachmentId> on_attachment_uploaded_list_; | 255 std::vector<AttachmentId> on_attachment_uploaded_list_; |
| 256 |
| 257 std::vector<AttachmentService::StoreResult> store_results_; |
244 }; | 258 }; |
245 | 259 |
246 TEST_F(AttachmentServiceImplTest, GetStore) { | |
247 EXPECT_EQ(store(), attachment_service()->GetStore()); | |
248 } | |
249 | |
250 TEST_F(AttachmentServiceImplTest, GetOrDownload_EmptyAttachmentList) { | 260 TEST_F(AttachmentServiceImplTest, GetOrDownload_EmptyAttachmentList) { |
251 AttachmentIdList attachment_ids; | 261 AttachmentIdList attachment_ids; |
252 attachment_service()->GetOrDownloadAttachments(attachment_ids, | 262 attachment_service()->GetOrDownloadAttachments(attachment_ids, |
253 download_callback()); | 263 download_callback()); |
254 store()->RespondToRead(AttachmentIdSet()); | 264 store()->RespondToRead(AttachmentIdSet()); |
255 | 265 |
256 RunLoop(); | 266 RunLoop(); |
257 EXPECT_EQ(1U, download_results().size()); | 267 EXPECT_EQ(1U, download_results().size()); |
258 EXPECT_EQ(0U, last_download_attachments().size()); | 268 EXPECT_EQ(0U, last_download_attachments().size()); |
259 } | 269 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 EXPECT_FALSE(store()->read_ids.empty()); | 341 EXPECT_FALSE(store()->read_ids.empty()); |
332 | 342 |
333 AttachmentIdSet local_attachments; | 343 AttachmentIdSet local_attachments; |
334 store()->RespondToRead(local_attachments); | 344 store()->RespondToRead(local_attachments); |
335 RunLoop(); | 345 RunLoop(); |
336 ASSERT_EQ(1U, download_results().size()); | 346 ASSERT_EQ(1U, download_results().size()); |
337 EXPECT_EQ(AttachmentService::GET_UNSPECIFIED_ERROR, download_results()[0]); | 347 EXPECT_EQ(AttachmentService::GET_UNSPECIFIED_ERROR, download_results()[0]); |
338 EXPECT_TRUE(last_download_attachments().empty()); | 348 EXPECT_TRUE(last_download_attachments().empty()); |
339 } | 349 } |
340 | 350 |
341 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success) { | 351 TEST_F(AttachmentServiceImplTest, StoreAttachments_Success) { |
342 AttachmentIdSet attachment_ids; | 352 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
343 const size_t num_attachments = 3; | 353 Attachment attachment(Attachment::Create(data)); |
344 for (unsigned i = 0; i < num_attachments; ++i) { | 354 AttachmentList attachments; |
345 attachment_ids.insert(AttachmentId::Create()); | 355 attachments.push_back(attachment); |
346 } | 356 attachment_service()->StoreAttachments(attachments, store_callback()); |
347 attachment_service()->UploadAttachments(attachment_ids); | 357 EXPECT_EQ(1U, store()->write_attachments.size()); |
| 358 EXPECT_EQ(1U, uploader()->upload_requests.size()); |
| 359 |
| 360 store()->RespondToWrite(AttachmentStore::SUCCESS); |
| 361 uploader()->RespondToUpload(attachment.GetId(), |
| 362 AttachmentUploader::UPLOAD_SUCCESS); |
348 RunLoop(); | 363 RunLoop(); |
349 // See that the service has issued reads for the attachments, but not yet | 364 ASSERT_EQ(1U, store_results().size()); |
350 // uploaded anything. | 365 EXPECT_EQ(AttachmentService::STORE_SUCCESS, store_results()[0]); |
351 EXPECT_EQ(num_attachments, store()->read_ids.size()); | 366 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); |
352 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 367 EXPECT_EQ(attachment.GetId(), on_attachment_uploaded_list()[0]); |
353 for (unsigned i = 0; i < num_attachments; ++i) { | |
354 store()->RespondToRead(attachment_ids); | |
355 } | |
356 | |
357 RunLoop(); | |
358 EXPECT_EQ(0U, store()->read_ids.size()); | |
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 } | |
365 RunLoop(); | |
366 | |
367 // See that all the attachments were uploaded. | |
368 ASSERT_EQ(attachment_ids.size(), on_attachment_uploaded_list().size()); | |
369 for (iter = attachment_ids.begin(); iter != end; ++iter) { | |
370 EXPECT_THAT(on_attachment_uploaded_list(), testing::Contains(*iter)); | |
371 } | |
372 } | 368 } |
373 | 369 |
374 TEST_F(AttachmentServiceImplTest, UploadAttachments_Success_NoDelegate) { | 370 TEST_F(AttachmentServiceImplTest, |
| 371 StoreAttachments_StoreFailsWithUnspecifiedError) { |
| 372 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
| 373 Attachment attachment(Attachment::Create(data)); |
| 374 AttachmentList attachments; |
| 375 attachments.push_back(attachment); |
| 376 attachment_service()->StoreAttachments(attachments, store_callback()); |
| 377 EXPECT_EQ(1U, store()->write_attachments.size()); |
| 378 EXPECT_EQ(1U, uploader()->upload_requests.size()); |
| 379 |
| 380 store()->RespondToWrite(AttachmentStore::UNSPECIFIED_ERROR); |
| 381 uploader()->RespondToUpload(attachment.GetId(), |
| 382 AttachmentUploader::UPLOAD_SUCCESS); |
| 383 RunLoop(); |
| 384 ASSERT_EQ(1U, store_results().size()); |
| 385 EXPECT_EQ(AttachmentService::STORE_UNSPECIFIED_ERROR, store_results()[0]); |
| 386 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); |
| 387 EXPECT_EQ(attachment.GetId(), on_attachment_uploaded_list()[0]); |
| 388 } |
| 389 |
| 390 TEST_F(AttachmentServiceImplTest, |
| 391 StoreAttachments_UploadFailsWithUnspecifiedError) { |
| 392 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
| 393 Attachment attachment(Attachment::Create(data)); |
| 394 AttachmentList attachments; |
| 395 attachments.push_back(attachment); |
| 396 attachment_service()->StoreAttachments(attachments, store_callback()); |
| 397 EXPECT_EQ(1U, store()->write_attachments.size()); |
| 398 EXPECT_EQ(1U, uploader()->upload_requests.size()); |
| 399 |
| 400 store()->RespondToWrite(AttachmentStore::SUCCESS); |
| 401 uploader()->RespondToUpload(attachment.GetId(), |
| 402 AttachmentUploader::UPLOAD_UNSPECIFIED_ERROR); |
| 403 RunLoop(); |
| 404 ASSERT_EQ(1U, store_results().size()); |
| 405 // Even though the upload failed, the Store operation is successful. |
| 406 EXPECT_EQ(AttachmentService::STORE_SUCCESS, store_results()[0]); |
| 407 EXPECT_TRUE(on_attachment_uploaded_list().empty()); |
| 408 } |
| 409 |
| 410 TEST_F(AttachmentServiceImplTest, StoreAttachments_NoDelegate) { |
375 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), | 411 InitializeAttachmentService(make_scoped_ptr(new MockAttachmentUploader()), |
376 make_scoped_ptr(new MockAttachmentDownloader()), | 412 make_scoped_ptr(new MockAttachmentDownloader()), |
377 NULL); // No delegate. | 413 NULL); // No delegate. |
378 | 414 |
379 AttachmentIdSet attachment_ids; | 415 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
380 attachment_ids.insert(AttachmentId::Create()); | 416 Attachment attachment(Attachment::Create(data)); |
381 attachment_service()->UploadAttachments(attachment_ids); | 417 AttachmentList attachments; |
382 RunLoop(); | 418 attachments.push_back(attachment); |
383 EXPECT_EQ(1U, store()->read_ids.size()); | 419 attachment_service()->StoreAttachments(attachments, store_callback()); |
384 EXPECT_EQ(0U, uploader()->upload_requests.size()); | 420 EXPECT_EQ(1U, store()->write_attachments.size()); |
385 store()->RespondToRead(attachment_ids); | |
386 RunLoop(); | |
387 EXPECT_EQ(0U, store()->read_ids.size()); | |
388 EXPECT_EQ(1U, uploader()->upload_requests.size()); | 421 EXPECT_EQ(1U, uploader()->upload_requests.size()); |
389 uploader()->RespondToUpload(*attachment_ids.begin(), | 422 |
| 423 store()->RespondToWrite(AttachmentStore::SUCCESS); |
| 424 uploader()->RespondToUpload(attachment.GetId(), |
390 AttachmentUploader::UPLOAD_SUCCESS); | 425 AttachmentUploader::UPLOAD_SUCCESS); |
391 RunLoop(); | 426 RunLoop(); |
392 ASSERT_TRUE(on_attachment_uploaded_list().empty()); | 427 ASSERT_EQ(1U, store_results().size()); |
| 428 EXPECT_EQ(AttachmentService::STORE_SUCCESS, store_results()[0]); |
| 429 EXPECT_TRUE(on_attachment_uploaded_list().empty()); |
393 } | 430 } |
394 | 431 |
395 TEST_F(AttachmentServiceImplTest, UploadAttachments_SomeMissingFromStore) { | 432 TEST_F(AttachmentServiceImplTest, StoreAttachments_NoUploader) { |
396 AttachmentIdSet attachment_ids; | 433 // No uploader. |
397 attachment_ids.insert(AttachmentId::Create()); | |
398 attachment_ids.insert(AttachmentId::Create()); | |
399 | |
400 attachment_service()->UploadAttachments(attachment_ids); | |
401 RunLoop(); | |
402 EXPECT_EQ(2U, store()->read_ids.size()); | |
403 EXPECT_EQ(0U, uploader()->upload_requests.size()); | |
404 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(); | |
410 | |
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, | |
414 AttachmentUploader::UPLOAD_SUCCESS); | |
415 RunLoop(); | |
416 | |
417 // See that the delegate was called for only one. | |
418 ASSERT_EQ(1U, on_attachment_uploaded_list().size()); | |
419 } | |
420 | |
421 TEST_F(AttachmentServiceImplTest, UploadAttachments_AllMissingFromStore) { | |
422 AttachmentIdSet attachment_ids; | |
423 attachment_ids.insert(AttachmentId::Create()); | |
424 attachment_ids.insert(AttachmentId::Create()); | |
425 | |
426 attachment_service()->UploadAttachments(attachment_ids); | |
427 RunLoop(); | |
428 EXPECT_EQ(2U, store()->read_ids.size()); | |
429 EXPECT_EQ(0U, uploader()->upload_requests.size()); | |
430 // None found! | |
431 store()->RespondToRead(AttachmentIdSet()); | |
432 store()->RespondToRead(AttachmentIdSet()); | |
433 EXPECT_EQ(0U, store()->read_ids.size()); | |
434 RunLoop(); | |
435 | |
436 // Nothing uploaded. | |
437 EXPECT_EQ(0U, uploader()->upload_requests.size()); | |
438 RunLoop(); | |
439 | |
440 // See that the delegate was never called. | |
441 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | |
442 } | |
443 | |
444 TEST_F(AttachmentServiceImplTest, UploadAttachments_NoUploader) { | |
445 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), | 434 InitializeAttachmentService(make_scoped_ptr<MockAttachmentUploader>(NULL), |
446 make_scoped_ptr(new MockAttachmentDownloader()), | 435 make_scoped_ptr(new MockAttachmentDownloader()), |
447 this); | 436 this); |
448 | 437 |
449 AttachmentIdSet attachment_ids; | 438 scoped_refptr<base::RefCountedString> data = new base::RefCountedString(); |
450 attachment_ids.insert(AttachmentId::Create()); | 439 Attachment attachment(Attachment::Create(data)); |
451 attachment_service()->UploadAttachments(attachment_ids); | 440 AttachmentList attachments; |
| 441 attachments.push_back(attachment); |
| 442 attachment_service()->StoreAttachments(attachments, store_callback()); |
| 443 EXPECT_EQ(1U, store()->write_attachments.size()); |
| 444 |
| 445 store()->RespondToWrite(AttachmentStore::SUCCESS); |
452 RunLoop(); | 446 RunLoop(); |
453 EXPECT_EQ(0U, store()->read_ids.size()); | 447 ASSERT_EQ(1U, store_results().size()); |
454 ASSERT_EQ(0U, on_attachment_uploaded_list().size()); | 448 EXPECT_EQ(AttachmentService::STORE_SUCCESS, store_results()[0]); |
455 } | 449 EXPECT_TRUE(on_attachment_uploaded_list().empty()); |
456 | |
457 // Upload three attachments. For one of them, server responds with error. | |
458 TEST_F(AttachmentServiceImplTest, UploadAttachments_OneUploadFails) { | |
459 AttachmentIdSet attachment_ids; | |
460 attachment_ids.insert(AttachmentId::Create()); | |
461 attachment_ids.insert(AttachmentId::Create()); | |
462 attachment_ids.insert(AttachmentId::Create()); | |
463 | |
464 attachment_service()->UploadAttachments(attachment_ids); | |
465 RunLoop(); | |
466 EXPECT_EQ(3U, store()->read_ids.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 } | 450 } |
487 | 451 |
488 } // namespace syncer | 452 } // namespace syncer |
OLD | NEW |