Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_file_system_unittest.cc

Issue 10581038: chromeos: Stop returning scoped_ptr from GDataCache::GetCacheEntry (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase and review fix Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <errno.h> 5 #include <errno.h>
6 #include <string> 6 #include <string>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 408
409 FilePath GetCacheFilePath(const std::string& resource_id, 409 FilePath GetCacheFilePath(const std::string& resource_id,
410 const std::string& md5, 410 const std::string& md5,
411 GDataCache::CacheSubDirectoryType sub_dir_type, 411 GDataCache::CacheSubDirectoryType sub_dir_type,
412 GDataCache::CachedFileOrigin file_origin) { 412 GDataCache::CachedFileOrigin file_origin) {
413 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, 413 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type,
414 file_origin); 414 file_origin);
415 } 415 }
416 416
417 // Helper function to call GetCacheEntry from origin thread. 417 // Helper function to call GetCacheEntry from origin thread.
418 scoped_ptr<GDataCache::CacheEntry> GetCacheEntryFromOriginThread( 418 bool GetCacheEntryFromOriginThread(const std::string& resource_id,
419 const std::string& resource_id, 419 const std::string& md5,
420 const std::string& md5) { 420 GDataCache::CacheEntry* cache_entry) {
421 scoped_ptr<GDataCache::CacheEntry> cache_entry; 421 bool result = false;
422 content::BrowserThread::GetBlockingPool() 422 content::BrowserThread::GetBlockingPool()
423 ->GetSequencedTaskRunner(sequence_token_)->PostTask( 423 ->GetSequencedTaskRunner(sequence_token_)->PostTask(
424 FROM_HERE, 424 FROM_HERE,
425 base::Bind( 425 base::Bind(
426 &GDataFileSystemTest::GetCacheEntryFromOriginThreadInternal, 426 &GDataFileSystemTest::GetCacheEntryFromOriginThreadInternal,
427 base::Unretained(this), 427 base::Unretained(this),
428 resource_id, 428 resource_id,
429 md5, 429 md5,
430 &cache_entry)); 430 cache_entry,
431 &result));
431 test_util::RunBlockingPoolTask(); 432 test_util::RunBlockingPoolTask();
432 return cache_entry.Pass(); 433 return result;
433 } 434 }
434 435
435 // Used to implement GetCacheEntry. 436 // Used to implement GetCacheEntry.
436 void GetCacheEntryFromOriginThreadInternal( 437 void GetCacheEntryFromOriginThreadInternal(
437 const std::string& resource_id, 438 const std::string& resource_id,
438 const std::string& md5, 439 const std::string& md5,
439 scoped_ptr<GDataCache::CacheEntry>* cache_entry) { 440 GDataCache::CacheEntry* cache_entry,
440 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); 441 bool* result) {
442 *result = cache_->GetCacheEntry(resource_id, md5, cache_entry);
441 } 443 }
442 444
443 // Returns true if the cache entry exists for the given resource ID and MD5. 445 // Returns true if the cache entry exists for the given resource ID and MD5.
444 bool CacheEntryExists(const std::string& resource_id, 446 bool CacheEntryExists(const std::string& resource_id,
445 const std::string& md5) { 447 const std::string& md5) {
446 return GetCacheEntryFromOriginThread(resource_id, md5).get(); 448 GDataCache::CacheEntry cache_entry;
449 return GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry);
447 } 450 }
448 451
449 // Returns true if the cache file exists for the given resource ID and MD5. 452 // Returns true if the cache file exists for the given resource ID and MD5.
450 bool CacheFileExists(const std::string& resource_id, 453 bool CacheFileExists(const std::string& resource_id,
451 const std::string& md5) { 454 const std::string& md5) {
452 const FilePath file_path = cache_->GetCacheFilePath( 455 const FilePath file_path = cache_->GetCacheFilePath(
453 resource_id, 456 resource_id,
454 md5, 457 md5,
455 GDataCache::CACHE_TYPE_TMP, 458 GDataCache::CACHE_TYPE_TMP,
456 GDataCache::CACHED_FILE_FROM_SERVER); 459 GDataCache::CACHED_FILE_FROM_SERVER);
(...skipping 13 matching lines...) Expand all
470 473
471 cache_->StoreOnUIThread( 474 cache_->StoreOnUIThread(
472 resource_id, md5, source_path, 475 resource_id, md5, source_path,
473 GDataCache::FILE_OPERATION_COPY, 476 GDataCache::FILE_OPERATION_COPY,
474 base::Bind(&GDataFileSystemTest::VerifyCacheFileState, 477 base::Bind(&GDataFileSystemTest::VerifyCacheFileState,
475 base::Unretained(this))); 478 base::Unretained(this)));
476 479
477 test_util::RunBlockingPoolTask(); 480 test_util::RunBlockingPoolTask();
478 } 481 }
479 482
480 void TestRemoveFromCache(const std::string& resource_id,
achuithb 2012/07/11 19:09:59 Did this code move elsewhere or is it not needed a
hashimoto 2012/07/12 05:15:19 Sorry for missing description. The tests which wer
satorux1 2012/07/12 07:58:46 Thank you for removing this! BTW, you could've rem
481 base::PlatformFileError expected_error) {
482 expected_error_ = expected_error;
483
484 cache_->RemoveOnUIThread(
485 resource_id,
486 base::Bind(&GDataFileSystemTest::VerifyRemoveFromCache,
487 base::Unretained(this)));
488
489 test_util::RunBlockingPoolTask();
490 }
491
492 void VerifyRemoveFromCache(base::PlatformFileError error,
493 const std::string& resource_id,
494 const std::string& md5) {
495 ++num_callback_invocations_;
496
497 EXPECT_EQ(expected_error_, error);
498
499 // Verify cache map.
500 scoped_ptr<GDataCache::CacheEntry> cache_entry =
501 GetCacheEntryFromOriginThread(resource_id, md5);
502 if (cache_entry.get())
503 EXPECT_TRUE(cache_entry->IsDirty());
504
505 // If entry doesn't exist, verify that:
506 // - no files with "<resource_id>.* exists in persistent and tmp dirs
507 // - no "<resource_id>" symlink exists in pinned and outgoing dirs.
508 std::vector<PathToVerify> paths_to_verify;
509 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP.
510 PathToVerify(cache_->GetCacheFilePath(resource_id, "*",
511 GDataCache::CACHE_TYPE_TMP,
512 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
513 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT.
514 PathToVerify(cache_->GetCacheFilePath(resource_id, "*",
515 GDataCache::CACHE_TYPE_PERSISTENT,
516 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
517 paths_to_verify.push_back( // Index 2: CACHE_TYPE_PINNED.
518 PathToVerify(cache_->GetCacheFilePath(resource_id, "",
519 GDataCache::CACHE_TYPE_PINNED,
520 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
521 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING.
522 PathToVerify(cache_->GetCacheFilePath(resource_id, "",
523 GDataCache::CACHE_TYPE_OUTGOING,
524 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
525 if (!cache_entry.get()) {
526 for (size_t i = 0; i < paths_to_verify.size(); ++i) {
527 file_util::FileEnumerator enumerator(
528 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/,
529 static_cast<file_util::FileEnumerator::FileType>(
530 file_util::FileEnumerator::FILES |
531 file_util::FileEnumerator::SHOW_SYM_LINKS),
532 paths_to_verify[i].path_to_scan.BaseName().value());
533 EXPECT_TRUE(enumerator.Next().empty());
534 }
535 } else {
536 // Entry is dirty, verify that:
537 // - no files with "<resource_id>.*" exist in tmp dir
538 // - only 1 "<resource_id>.local" exists in persistent dir
539 // - only 1 <resource_id> exists in outgoing dir
540 // - if entry is pinned, only 1 <resource_id> exists in pinned dir.
541
542 // Change expected_existing_path of CACHE_TYPE_PERSISTENT (index 1).
543 paths_to_verify[1].expected_existing_path =
544 GetCacheFilePath(resource_id,
545 std::string(),
546 GDataCache::CACHE_TYPE_PERSISTENT,
547 GDataCache::CACHED_FILE_LOCALLY_MODIFIED);
548
549 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3).
550 paths_to_verify[3].expected_existing_path =
551 GetCacheFilePath(resource_id,
552 std::string(),
553 GDataCache::CACHE_TYPE_OUTGOING,
554 GDataCache::CACHED_FILE_FROM_SERVER);
555
556 if (cache_entry->IsPinned()) {
557 // Change expected_existing_path of CACHE_TYPE_PINNED (index 2).
558 paths_to_verify[2].expected_existing_path =
559 GetCacheFilePath(resource_id,
560 std::string(),
561 GDataCache::CACHE_TYPE_PINNED,
562 GDataCache::CACHED_FILE_FROM_SERVER);
563 }
564
565 for (size_t i = 0; i < paths_to_verify.size(); ++i) {
566 const struct PathToVerify& verify = paths_to_verify[i];
567 file_util::FileEnumerator enumerator(
568 verify.path_to_scan.DirName(), false /* not recursive*/,
569 static_cast<file_util::FileEnumerator::FileType>(
570 file_util::FileEnumerator::FILES |
571 file_util::FileEnumerator::SHOW_SYM_LINKS),
572 verify.path_to_scan.BaseName().value());
573 size_t num_files_found = 0;
574 for (FilePath current = enumerator.Next(); !current.empty();
575 current = enumerator.Next()) {
576 ++num_files_found;
577 EXPECT_EQ(verify.expected_existing_path, current);
578 }
579 if (verify.expected_existing_path.empty())
580 EXPECT_EQ(0U, num_files_found);
581 else
582 EXPECT_EQ(1U, num_files_found);
583 }
584 }
585 }
586
587 void TestPin( 483 void TestPin(
588 const std::string& resource_id, 484 const std::string& resource_id,
589 const std::string& md5, 485 const std::string& md5,
590 base::PlatformFileError expected_error, 486 base::PlatformFileError expected_error,
591 int expected_cache_state, 487 int expected_cache_state,
592 GDataCache::CacheSubDirectoryType expected_sub_dir_type) { 488 GDataCache::CacheSubDirectoryType expected_sub_dir_type) {
593 expected_error_ = expected_error; 489 expected_error_ = expected_error;
594 expected_cache_state_ = expected_cache_state; 490 expected_cache_state_ = expected_cache_state;
595 expected_sub_dir_type_ = expected_sub_dir_type; 491 expected_sub_dir_type_ = expected_sub_dir_type;
596 492
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 } 586 }
691 587
692 void VerifyCacheFileState(base::PlatformFileError error, 588 void VerifyCacheFileState(base::PlatformFileError error,
693 const std::string& resource_id, 589 const std::string& resource_id,
694 const std::string& md5) { 590 const std::string& md5) {
695 ++num_callback_invocations_; 591 ++num_callback_invocations_;
696 592
697 EXPECT_EQ(expected_error_, error); 593 EXPECT_EQ(expected_error_, error);
698 594
699 // Verify cache map. 595 // Verify cache map.
700 scoped_ptr<GDataCache::CacheEntry> cache_entry = 596 GDataCache::CacheEntry cache_entry;
701 GetCacheEntryFromOriginThread(resource_id, md5); 597 const bool cache_entry_found =
598 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry);
702 if (GDataCache::IsCachePresent(expected_cache_state_) || 599 if (GDataCache::IsCachePresent(expected_cache_state_) ||
703 GDataCache::IsCachePinned(expected_cache_state_)) { 600 GDataCache::IsCachePinned(expected_cache_state_)) {
704 ASSERT_TRUE(cache_entry.get()); 601 ASSERT_TRUE(cache_entry_found);
705 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state); 602 EXPECT_EQ(expected_cache_state_, cache_entry.cache_state);
706 EXPECT_EQ(expected_sub_dir_type_, cache_entry->GetSubDirectoryType()); 603 EXPECT_EQ(expected_sub_dir_type_, cache_entry.GetSubDirectoryType());
707 } else { 604 } else {
708 EXPECT_FALSE(cache_entry.get()); 605 EXPECT_FALSE(cache_entry_found);
709 } 606 }
710 607
711 // Verify actual cache file. 608 // Verify actual cache file.
712 FilePath dest_path = cache_->GetCacheFilePath( 609 FilePath dest_path = cache_->GetCacheFilePath(
713 resource_id, 610 resource_id,
714 md5, 611 md5,
715 GDataCache::IsCachePinned(expected_cache_state_) || 612 GDataCache::IsCachePinned(expected_cache_state_) ||
716 GDataCache::IsCacheDirty(expected_cache_state_) ? 613 GDataCache::IsCacheDirty(expected_cache_state_) ?
717 GDataCache::CACHE_TYPE_PERSISTENT : 614 GDataCache::CACHE_TYPE_PERSISTENT :
718 GDataCache::CACHE_TYPE_TMP, 615 GDataCache::CACHE_TYPE_TMP,
(...skipping 1907 matching lines...) Expand 10 before | Expand all | Expand 10 after
2626 2523
2627 // Try to close the same file twice. 2524 // Try to close the same file twice.
2628 file_system_->CloseFile(kFileInRoot, close_file_callback); 2525 file_system_->CloseFile(kFileInRoot, close_file_callback);
2629 message_loop_.Run(); 2526 message_loop_.Run();
2630 2527
2631 // It must fail. 2528 // It must fail.
2632 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, callback_helper_->last_error_); 2529 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, callback_helper_->last_error_);
2633 } 2530 }
2634 2531
2635 } // namespace gdata 2532 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698