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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_cache_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 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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/json/json_file_value_serializer.h" 6 #include "base/json/json_file_value_serializer.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "chrome/browser/chromeos/cros/cros_library.h" 9 #include "chrome/browser/chromeos/cros/cros_library.h"
10 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" 10 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h"
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 ToCacheEntry(resource.cache_state).IsPresent() ? 330 ToCacheEntry(resource.cache_state).IsPresent() ?
331 base::PLATFORM_FILE_OK : 331 base::PLATFORM_FILE_OK :
332 base::PLATFORM_FILE_ERROR_NOT_FOUND, 332 base::PLATFORM_FILE_ERROR_NOT_FOUND,
333 resource.expected_file_extension); 333 resource.expected_file_extension);
334 EXPECT_EQ(1, num_callback_invocations_); 334 EXPECT_EQ(1, num_callback_invocations_);
335 335
336 // Verify cache state. 336 // Verify cache state.
337 std::string md5; 337 std::string md5;
338 if (ToCacheEntry(resource.cache_state).IsPresent()) 338 if (ToCacheEntry(resource.cache_state).IsPresent())
339 md5 = resource.md5; 339 md5 = resource.md5;
340 scoped_ptr<GDataCacheEntry> cache_entry = 340 GDataCacheEntry cache_entry;
341 GetCacheEntryFromOriginThread(resource.resource_id, md5); 341 ASSERT_TRUE(GetCacheEntryFromOriginThread(
342 ASSERT_TRUE(cache_entry.get()); 342 resource.resource_id, md5, &cache_entry));
343 EXPECT_EQ(resource.cache_state, cache_entry->cache_state()); 343 EXPECT_EQ(resource.cache_state, cache_entry.cache_state());
344 EXPECT_EQ(resource.expected_sub_dir_type, 344 EXPECT_EQ(resource.expected_sub_dir_type,
345 GDataCache::GetSubDirectoryType(*cache_entry)); 345 GDataCache::GetSubDirectoryType(cache_entry));
346 } 346 }
347 } 347 }
348 348
349 void TestGetFileFromCacheByResourceIdAndMd5( 349 void TestGetFileFromCacheByResourceIdAndMd5(
350 const std::string& resource_id, 350 const std::string& resource_id,
351 const std::string& md5, 351 const std::string& md5,
352 base::PlatformFileError expected_error, 352 base::PlatformFileError expected_error,
353 const std::string& expected_file_extension) { 353 const std::string& expected_file_extension) {
354 expected_error_ = expected_error; 354 expected_error_ = expected_error;
355 expected_file_extension_ = expected_file_extension; 355 expected_file_extension_ = expected_file_extension;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 } 417 }
418 418
419 void VerifyRemoveFromCache(base::PlatformFileError error, 419 void VerifyRemoveFromCache(base::PlatformFileError error,
420 const std::string& resource_id, 420 const std::string& resource_id,
421 const std::string& md5) { 421 const std::string& md5) {
422 ++num_callback_invocations_; 422 ++num_callback_invocations_;
423 423
424 EXPECT_EQ(expected_error_, error); 424 EXPECT_EQ(expected_error_, error);
425 425
426 // Verify cache map. 426 // Verify cache map.
427 scoped_ptr<GDataCacheEntry> cache_entry = 427 GDataCacheEntry cache_entry;
428 GetCacheEntryFromOriginThread(resource_id, md5); 428 const bool cache_entry_found =
429 if (cache_entry.get()) 429 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry);
430 EXPECT_TRUE(cache_entry->IsDirty()); 430 if (cache_entry_found)
431 EXPECT_TRUE(cache_entry.IsDirty());
431 432
432 // If entry doesn't exist, verify that: 433 // If entry doesn't exist, verify that:
433 // - no files with "<resource_id>.* exists in persistent and tmp dirs 434 // - no files with "<resource_id>.* exists in persistent and tmp dirs
434 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. 435 // - no "<resource_id>" symlink exists in pinned and outgoing dirs.
435 std::vector<PathToVerify> paths_to_verify; 436 std::vector<PathToVerify> paths_to_verify;
436 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. 437 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP.
437 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", 438 PathToVerify(cache_->GetCacheFilePath(resource_id, "*",
438 GDataCache::CACHE_TYPE_TMP, 439 GDataCache::CACHE_TYPE_TMP,
439 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); 440 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
440 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. 441 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT.
441 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", 442 PathToVerify(cache_->GetCacheFilePath(resource_id, "*",
442 GDataCache::CACHE_TYPE_PERSISTENT, 443 GDataCache::CACHE_TYPE_PERSISTENT,
443 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); 444 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
444 paths_to_verify.push_back( // Index 2: CACHE_TYPE_TMP, but STATE_PINNED. 445 paths_to_verify.push_back( // Index 2: CACHE_TYPE_TMP, but STATE_PINNED.
445 PathToVerify(cache_->GetCacheFilePath(resource_id, "", 446 PathToVerify(cache_->GetCacheFilePath(resource_id, "",
446 GDataCache::CACHE_TYPE_PINNED, 447 GDataCache::CACHE_TYPE_PINNED,
447 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); 448 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
448 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING. 449 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING.
449 PathToVerify(cache_->GetCacheFilePath(resource_id, "", 450 PathToVerify(cache_->GetCacheFilePath(resource_id, "",
450 GDataCache::CACHE_TYPE_OUTGOING, 451 GDataCache::CACHE_TYPE_OUTGOING,
451 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); 452 GDataCache::CACHED_FILE_FROM_SERVER), FilePath()));
452 if (!cache_entry.get()) { 453 if (!cache_entry_found) {
453 for (size_t i = 0; i < paths_to_verify.size(); ++i) { 454 for (size_t i = 0; i < paths_to_verify.size(); ++i) {
454 file_util::FileEnumerator enumerator( 455 file_util::FileEnumerator enumerator(
455 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, 456 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/,
456 static_cast<file_util::FileEnumerator::FileType>( 457 static_cast<file_util::FileEnumerator::FileType>(
457 file_util::FileEnumerator::FILES | 458 file_util::FileEnumerator::FILES |
458 file_util::FileEnumerator::SHOW_SYM_LINKS), 459 file_util::FileEnumerator::SHOW_SYM_LINKS),
459 paths_to_verify[i].path_to_scan.BaseName().value()); 460 paths_to_verify[i].path_to_scan.BaseName().value());
460 EXPECT_TRUE(enumerator.Next().empty()); 461 EXPECT_TRUE(enumerator.Next().empty());
461 } 462 }
462 } else { 463 } else {
(...skipping 10 matching lines...) Expand all
473 GDataCache::CACHE_TYPE_PERSISTENT, 474 GDataCache::CACHE_TYPE_PERSISTENT,
474 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); 475 GDataCache::CACHED_FILE_LOCALLY_MODIFIED);
475 476
476 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3). 477 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3).
477 paths_to_verify[3].expected_existing_path = 478 paths_to_verify[3].expected_existing_path =
478 GetCacheFilePath(resource_id, 479 GetCacheFilePath(resource_id,
479 std::string(), 480 std::string(),
480 GDataCache::CACHE_TYPE_OUTGOING, 481 GDataCache::CACHE_TYPE_OUTGOING,
481 GDataCache::CACHED_FILE_FROM_SERVER); 482 GDataCache::CACHED_FILE_FROM_SERVER);
482 483
483 if (cache_entry->IsPinned()) { 484 if (cache_entry.IsPinned()) {
484 // Change expected_existing_path of CACHE_TYPE_TMP but STATE_PINNED 485 // Change expected_existing_path of CACHE_TYPE_TMP but STATE_PINNED
485 // (index 2). 486 // (index 2).
486 paths_to_verify[2].expected_existing_path = 487 paths_to_verify[2].expected_existing_path =
487 GetCacheFilePath(resource_id, 488 GetCacheFilePath(resource_id,
488 std::string(), 489 std::string(),
489 GDataCache::CACHE_TYPE_PINNED, 490 GDataCache::CACHE_TYPE_PINNED,
490 GDataCache::CACHED_FILE_FROM_SERVER); 491 GDataCache::CACHED_FILE_FROM_SERVER);
491 } 492 }
492 493
493 for (size_t i = 0; i < paths_to_verify.size(); ++i) { 494 for (size_t i = 0; i < paths_to_verify.size(); ++i) {
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 } 686 }
686 687
687 void VerifyCacheFileState(base::PlatformFileError error, 688 void VerifyCacheFileState(base::PlatformFileError error,
688 const std::string& resource_id, 689 const std::string& resource_id,
689 const std::string& md5) { 690 const std::string& md5) {
690 ++num_callback_invocations_; 691 ++num_callback_invocations_;
691 692
692 EXPECT_EQ(expected_error_, error); 693 EXPECT_EQ(expected_error_, error);
693 694
694 // Verify cache map. 695 // Verify cache map.
695 scoped_ptr<GDataCacheEntry> cache_entry = 696 GDataCacheEntry cache_entry;
696 GetCacheEntryFromOriginThread(resource_id, md5); 697 const bool cache_entry_found =
698 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry);
697 if (ToCacheEntry(expected_cache_state_).IsPresent() || 699 if (ToCacheEntry(expected_cache_state_).IsPresent() ||
698 ToCacheEntry(expected_cache_state_).IsPinned()) { 700 ToCacheEntry(expected_cache_state_).IsPinned()) {
699 ASSERT_TRUE(cache_entry.get()); 701 ASSERT_TRUE(cache_entry_found);
700 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state()); 702 EXPECT_EQ(expected_cache_state_, cache_entry.cache_state());
701 EXPECT_EQ(expected_sub_dir_type_, 703 EXPECT_EQ(expected_sub_dir_type_,
702 GDataCache::GetSubDirectoryType(*cache_entry)); 704 GDataCache::GetSubDirectoryType(cache_entry));
703 } else { 705 } else {
704 EXPECT_FALSE(cache_entry.get()); 706 EXPECT_FALSE(cache_entry_found);
705 } 707 }
706 708
707 // Verify actual cache file. 709 // Verify actual cache file.
708 FilePath dest_path = cache_->GetCacheFilePath( 710 FilePath dest_path = cache_->GetCacheFilePath(
709 resource_id, 711 resource_id,
710 md5, 712 md5,
711 ToCacheEntry(expected_cache_state_).IsPinned() || 713 ToCacheEntry(expected_cache_state_).IsPinned() ||
712 ToCacheEntry(expected_cache_state_).IsDirty() ? 714 ToCacheEntry(expected_cache_state_).IsDirty() ?
713 GDataCache::CACHE_TYPE_PERSISTENT : 715 GDataCache::CACHE_TYPE_PERSISTENT :
714 GDataCache::CACHE_TYPE_TMP, 716 GDataCache::CACHE_TYPE_TMP,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
764 766
765 FilePath GetCacheFilePath(const std::string& resource_id, 767 FilePath GetCacheFilePath(const std::string& resource_id,
766 const std::string& md5, 768 const std::string& md5,
767 GDataCache::CacheSubDirectoryType sub_dir_type, 769 GDataCache::CacheSubDirectoryType sub_dir_type,
768 GDataCache::CachedFileOrigin file_origin) { 770 GDataCache::CachedFileOrigin file_origin) {
769 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, 771 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type,
770 file_origin); 772 file_origin);
771 } 773 }
772 774
773 // Helper function to call GetCacheEntry from origin thread. 775 // Helper function to call GetCacheEntry from origin thread.
774 scoped_ptr<GDataCacheEntry> GetCacheEntryFromOriginThread( 776 bool GetCacheEntryFromOriginThread(const std::string& resource_id,
775 const std::string& resource_id, 777 const std::string& md5,
776 const std::string& md5) { 778 GDataCacheEntry* cache_entry) {
777 scoped_ptr<GDataCacheEntry> cache_entry; 779 bool result = false;
778 content::BrowserThread::GetBlockingPool() 780 content::BrowserThread::GetBlockingPool()
779 ->GetSequencedTaskRunner(sequence_token_)->PostTask( 781 ->GetSequencedTaskRunner(sequence_token_)->PostTask(
780 FROM_HERE, 782 FROM_HERE,
781 base::Bind( 783 base::Bind(
782 &GDataCacheTest::GetCacheEntryFromOriginThreadInternal, 784 &GDataCacheTest::GetCacheEntryFromOriginThreadInternal,
783 base::Unretained(this), 785 base::Unretained(this),
784 resource_id, 786 resource_id,
785 md5, 787 md5,
786 &cache_entry)); 788 cache_entry,
789 &result));
787 test_util::RunBlockingPoolTask(); 790 test_util::RunBlockingPoolTask();
788 return cache_entry.Pass(); 791 return result;
789 } 792 }
790 793
791 // Used to implement GetCacheEntry. 794 // Used to implement GetCacheEntry.
792 void GetCacheEntryFromOriginThreadInternal( 795 void GetCacheEntryFromOriginThreadInternal(
793 const std::string& resource_id, 796 const std::string& resource_id,
794 const std::string& md5, 797 const std::string& md5,
795 scoped_ptr<GDataCacheEntry>* cache_entry) { 798 GDataCacheEntry* cache_entry,
796 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); 799 bool* result) {
800 *result = cache_->GetCacheEntry(resource_id, md5, cache_entry);
797 } 801 }
798 802
799 // Returns true if the cache entry exists for the given resource ID and MD5. 803 // Returns true if the cache entry exists for the given resource ID and MD5.
800 bool CacheEntryExists(const std::string& resource_id, 804 bool CacheEntryExists(const std::string& resource_id,
801 const std::string& md5) { 805 const std::string& md5) {
802 return GetCacheEntryFromOriginThread(resource_id, md5).get(); 806 GDataCacheEntry cache_entry;
807 return GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry);
803 } 808 }
804 809
805 void TestGetCacheFilePath(const std::string& resource_id, 810 void TestGetCacheFilePath(const std::string& resource_id,
806 const std::string& md5, 811 const std::string& md5,
807 const std::string& expected_filename) { 812 const std::string& expected_filename) {
808 FilePath actual_path = cache_->GetCacheFilePath( 813 FilePath actual_path = cache_->GetCacheFilePath(
809 resource_id, 814 resource_id,
810 md5, 815 md5,
811 GDataCache::CACHE_TYPE_TMP, 816 GDataCache::CACHE_TYPE_TMP,
812 GDataCache::CACHED_FILE_FROM_SERVER); 817 GDataCache::CACHED_FILE_FROM_SERVER);
(...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after
1580 EXPECT_EQ(1, num_callback_invocations_); 1585 EXPECT_EQ(1, num_callback_invocations_);
1581 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); 1586 EXPECT_TRUE(CacheEntryExists(resource_id, md5));
1582 1587
1583 // Try to remove the file. 1588 // Try to remove the file.
1584 num_callback_invocations_ = 0; 1589 num_callback_invocations_ = 0;
1585 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); 1590 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK);
1586 EXPECT_EQ(1, num_callback_invocations_); 1591 EXPECT_EQ(1, num_callback_invocations_);
1587 } 1592 }
1588 1593
1589 } // namespace gdata 1594 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698