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