| OLD | NEW |
| 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 "chrome/browser/chromeos/drive/drive_file_system.h" | 5 #include "chrome/browser/chromeos/drive/drive_file_system.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 mock_directory_observer_.reset(new StrictMock<MockDirectoryChangeObserver>); | 135 mock_directory_observer_.reset(new StrictMock<MockDirectoryChangeObserver>); |
| 136 | 136 |
| 137 cache_->RequestInitializeForTesting(); | 137 cache_->RequestInitializeForTesting(); |
| 138 google_apis::test_util::RunBlockingPoolTask(); | 138 google_apis::test_util::RunBlockingPoolTask(); |
| 139 | 139 |
| 140 SetUpResourceMetadataAndFileSystem(); | 140 SetUpResourceMetadataAndFileSystem(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void SetUpResourceMetadataAndFileSystem() { | 143 void SetUpResourceMetadataAndFileSystem() { |
| 144 resource_metadata_.reset(new DriveResourceMetadata( | 144 resource_metadata_.reset(new DriveResourceMetadata( |
| 145 fake_drive_service_->GetRootResourceId(), | |
| 146 cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_META), | 145 cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_META), |
| 147 blocking_task_runner_)); | 146 blocking_task_runner_)); |
| 148 | 147 |
| 149 file_system_.reset(new DriveFileSystem(profile_.get(), | 148 file_system_.reset(new DriveFileSystem(profile_.get(), |
| 150 cache_.get(), | 149 cache_.get(), |
| 151 fake_drive_service_.get(), | 150 fake_drive_service_.get(), |
| 152 drive_webapps_registry_.get(), | 151 drive_webapps_registry_.get(), |
| 153 resource_metadata_.get(), | 152 resource_metadata_.get(), |
| 154 blocking_task_runner_)); | 153 blocking_task_runner_)); |
| 155 file_system_->AddObserver(mock_directory_observer_.get()); | 154 file_system_->AddObserver(mock_directory_observer_.get()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 // equal to that of "account_metadata.json" test data, indicating the cache is | 311 // equal to that of "account_metadata.json" test data, indicating the cache is |
| 313 // holding the latest file system info. | 312 // holding the latest file system info. |
| 314 bool SaveTestFileSystem(SaveTestFileSystemParam param) { | 313 bool SaveTestFileSystem(SaveTestFileSystemParam param) { |
| 315 // Destroy the existing resource metadata to close DB. | 314 // Destroy the existing resource metadata to close DB. |
| 316 resource_metadata_.reset(); | 315 resource_metadata_.reset(); |
| 317 | 316 |
| 318 const std::string root_resource_id = | 317 const std::string root_resource_id = |
| 319 fake_drive_service_->GetRootResourceId(); | 318 fake_drive_service_->GetRootResourceId(); |
| 320 scoped_ptr<DriveResourceMetadata, test_util::DestroyHelperForTests> | 319 scoped_ptr<DriveResourceMetadata, test_util::DestroyHelperForTests> |
| 321 resource_metadata(new DriveResourceMetadata( | 320 resource_metadata(new DriveResourceMetadata( |
| 322 root_resource_id, | |
| 323 cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_META), | 321 cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_META), |
| 324 blocking_task_runner_)); | 322 blocking_task_runner_)); |
| 325 | 323 |
| 326 DriveFileError error = DRIVE_FILE_ERROR_FAILED; | 324 DriveFileError error = DRIVE_FILE_ERROR_FAILED; |
| 327 resource_metadata->Initialize( | 325 resource_metadata->Initialize( |
| 328 google_apis::test_util::CreateCopyResultCallback(&error)); | 326 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 329 google_apis::test_util::RunBlockingPoolTask(); | 327 google_apis::test_util::RunBlockingPoolTask(); |
| 330 if (error != DRIVE_FILE_OK) | 328 if (error != DRIVE_FILE_OK) |
| 331 return false; | 329 return false; |
| 332 | 330 |
| 333 resource_metadata->SetLargestChangestamp( | 331 resource_metadata->SetLargestChangestamp( |
| 334 param == USE_SERVER_TIMESTAMP ? 654321 : 1, | 332 param == USE_SERVER_TIMESTAMP ? 654321 : 1, |
| 335 google_apis::test_util::CreateCopyResultCallback(&error)); | 333 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 336 google_apis::test_util::RunBlockingPoolTask(); | 334 google_apis::test_util::RunBlockingPoolTask(); |
| 337 if (error != DRIVE_FILE_OK) | 335 if (error != DRIVE_FILE_OK) |
| 338 return false; | 336 return false; |
| 339 | 337 |
| 340 // drive/root is already prepared by DriveResourceMetadata. | 338 // drive/root is already prepared by DriveResourceMetadata. |
| 341 // TODO(haruki): Create drive/root here when we start creating it in | |
| 342 // ChangeListLoader. | |
| 343 base::FilePath file_path; | 339 base::FilePath file_path; |
| 344 | 340 |
| 341 // drive/root |
| 342 resource_metadata->AddEntry( |
| 343 util::CreateMyDriveRootEntry(root_resource_id), |
| 344 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); |
| 345 google_apis::test_util::RunBlockingPoolTask(); |
| 346 if (error != DRIVE_FILE_OK) |
| 347 return false; |
| 348 |
| 345 // drive/root/File1 | 349 // drive/root/File1 |
| 346 DriveEntryProto file1; | 350 DriveEntryProto file1; |
| 347 file1.set_title("File1"); | 351 file1.set_title("File1"); |
| 348 file1.set_resource_id("resource_id:File1"); | 352 file1.set_resource_id("resource_id:File1"); |
| 349 file1.set_parent_resource_id(root_resource_id); | 353 file1.set_parent_resource_id(root_resource_id); |
| 350 file1.mutable_file_specific_info()->set_file_md5("md5"); | 354 file1.mutable_file_specific_info()->set_file_md5("md5"); |
| 351 file1.mutable_file_info()->set_is_directory(false); | 355 file1.mutable_file_info()->set_is_directory(false); |
| 352 file1.mutable_file_info()->set_size(1048576); | 356 file1.mutable_file_info()->set_size(1048576); |
| 353 resource_metadata->AddEntry( | 357 resource_metadata->AddEntry( |
| 354 file1, | 358 file1, |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; | 463 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
| 460 scoped_ptr<StrictMock<MockDriveCacheObserver> > mock_cache_observer_; | 464 scoped_ptr<StrictMock<MockDriveCacheObserver> > mock_cache_observer_; |
| 461 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; | 465 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; |
| 462 | 466 |
| 463 bool expected_success_; | 467 bool expected_success_; |
| 464 std::string expected_file_extension_; | 468 std::string expected_file_extension_; |
| 465 int root_feed_changestamp_; | 469 int root_feed_changestamp_; |
| 466 }; | 470 }; |
| 467 | 471 |
| 468 void AsyncInitializationCallback( | 472 void AsyncInitializationCallback( |
| 469 int* counter, | 473 int* counter, int expected_counter, MessageLoop* message_loop, |
| 470 int expected_counter, | 474 DriveFileError error, scoped_ptr<DriveEntryProto> entry) { |
| 471 MessageLoop* message_loop, | 475 if (error != DRIVE_FILE_OK || !entry) { |
| 472 DriveFileError error, | 476 // If we hit an error case, quit the message loop immediately. |
| 473 bool hide_hosted_documents, | 477 // Then the expectation in the test case can find it because the actual |
| 474 scoped_ptr<DriveEntryProtoVector> entries) { | 478 // value of |counter| is different from the expected one. |
| 475 ASSERT_EQ(DRIVE_FILE_OK, error); | 479 message_loop->Quit(); |
| 476 ASSERT_TRUE(entries.get()); | 480 return; |
| 477 ASSERT_FALSE(entries->empty()); | 481 } |
| 478 | 482 |
| 479 (*counter)++; | 483 (*counter)++; |
| 480 if (*counter >= expected_counter) | 484 if (*counter >= expected_counter) |
| 481 message_loop->Quit(); | 485 message_loop->Quit(); |
| 482 } | 486 } |
| 483 | 487 |
| 484 TEST_F(DriveFileSystemTest, DuplicatedAsyncInitialization) { | 488 TEST_F(DriveFileSystemTest, DuplicatedAsyncInitialization) { |
| 485 // The root directory will be loaded that triggers the event. | 489 int counter = 0; |
| 486 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( | 490 const GetEntryInfoCallback& callback = base::Bind( |
| 487 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1); | 491 &AsyncInitializationCallback, &counter, 2, &message_loop_); |
| 488 | 492 |
| 489 int counter = 0; | 493 file_system_->GetEntryInfoByPath( |
| 490 ReadDirectoryWithSettingCallback callback = base::Bind( | |
| 491 &AsyncInitializationCallback, | |
| 492 &counter, | |
| 493 2, | |
| 494 &message_loop_); | |
| 495 | |
| 496 file_system_->ReadDirectoryByPath( | |
| 497 base::FilePath(FILE_PATH_LITERAL("drive/root")), callback); | 494 base::FilePath(FILE_PATH_LITERAL("drive/root")), callback); |
| 498 file_system_->ReadDirectoryByPath( | 495 file_system_->GetEntryInfoByPath( |
| 499 base::FilePath(FILE_PATH_LITERAL("drive/root")), callback); | 496 base::FilePath(FILE_PATH_LITERAL("drive/root")), callback); |
| 500 message_loop_.Run(); // Wait to get our result | 497 message_loop_.Run(); // Wait to get our result |
| 501 EXPECT_EQ(2, counter); | 498 EXPECT_EQ(2, counter); |
| 502 | 499 |
| 503 // ReadDirectoryByPath() was called twice, but the account metadata should | 500 // Although GetEntryInfoByPath() was called twice, the account metadata |
| 504 // only be loaded once. In the past, there was a bug that caused it to be | 501 // should only be loaded once. In the past, there was a bug that caused |
| 505 // loaded twice. | 502 // it to be loaded twice. |
| 506 EXPECT_EQ(1, fake_drive_service_->about_resource_load_count()); | 503 EXPECT_EQ(1, fake_drive_service_->about_resource_load_count()); |
| 507 // On the other hand, the resource list could be loaded twice. One for | 504 EXPECT_EQ(1, fake_drive_service_->resource_list_load_count()); |
| 508 // just the directory contents, and one for the entire resource list. | |
| 509 // | |
| 510 // The |callback| function gets called back soon after the directory content | |
| 511 // is loaded, and the full resource load is done in background asynchronously. | |
| 512 // So it depends on timing whether we receive the full resource load request | |
| 513 // at this point. | |
| 514 EXPECT_TRUE(fake_drive_service_->resource_list_load_count() == 1 || | |
| 515 fake_drive_service_->resource_list_load_count() == 2) | |
| 516 << ": " << fake_drive_service_->resource_list_load_count(); | |
| 517 } | 505 } |
| 518 | 506 |
| 519 TEST_F(DriveFileSystemTest, GetGrandRootEntry) { | 507 TEST_F(DriveFileSystemTest, GetGrandRootEntry) { |
| 520 const base::FilePath kFilePath = | 508 const base::FilePath kFilePath = |
| 521 base::FilePath(FILE_PATH_LITERAL("drive")); | 509 base::FilePath(FILE_PATH_LITERAL("drive")); |
| 522 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); | 510 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); |
| 523 ASSERT_TRUE(entry.get()); | 511 ASSERT_TRUE(entry.get()); |
| 524 EXPECT_EQ(util::kDriveGrandRootSpecialResourceId, entry->resource_id()); | 512 EXPECT_EQ(util::kDriveGrandRootSpecialResourceId, entry->resource_id()); |
| 525 | 513 |
| 526 // Getting the grand root entry should not cause the resource load to happen. | 514 // Getting the grand root entry should not cause the resource load to happen. |
| 527 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); | 515 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); |
| 528 EXPECT_EQ(0, fake_drive_service_->resource_list_load_count()); | 516 EXPECT_EQ(0, fake_drive_service_->resource_list_load_count()); |
| 529 } | 517 } |
| 530 | 518 |
| 531 TEST_F(DriveFileSystemTest, GetMyDriveRootEntry) { | |
| 532 const base::FilePath kFilePath = | |
| 533 base::FilePath(FILE_PATH_LITERAL("drive/root")); | |
| 534 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); | |
| 535 ASSERT_TRUE(entry.get()); | |
| 536 EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id()); | |
| 537 | |
| 538 // Getting the "My Drive" root entry should not cause the resource load to | |
| 539 // happen. | |
| 540 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); | |
| 541 EXPECT_EQ(0, fake_drive_service_->resource_list_load_count()); | |
| 542 } | |
| 543 | |
| 544 TEST_F(DriveFileSystemTest, GetOtherDirEntry) { | 519 TEST_F(DriveFileSystemTest, GetOtherDirEntry) { |
| 545 const base::FilePath kFilePath = | 520 const base::FilePath kFilePath = |
| 546 base::FilePath(FILE_PATH_LITERAL("drive/other")); | 521 base::FilePath(FILE_PATH_LITERAL("drive/other")); |
| 547 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); | 522 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); |
| 548 ASSERT_TRUE(entry.get()); | 523 ASSERT_TRUE(entry.get()); |
| 549 EXPECT_EQ(util::kDriveOtherDirSpecialResourceId, entry->resource_id()); | 524 EXPECT_EQ(util::kDriveOtherDirSpecialResourceId, entry->resource_id()); |
| 550 | 525 |
| 551 // Getting the "other" directory entry should not cause the resource load to | 526 // Getting the "other" directory entry should not cause the resource load to |
| 552 // happen. | 527 // happen. |
| 553 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); | 528 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 718 | 693 |
| 719 const base::FilePath kFilePath = base::FilePath( | 694 const base::FilePath kFilePath = base::FilePath( |
| 720 FILE_PATH_LITERAL("drive/other/Orphan File 1.txt")); | 695 FILE_PATH_LITERAL("drive/other/Orphan File 1.txt")); |
| 721 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); | 696 scoped_ptr<DriveEntryProto> entry = GetEntryInfoByPathSync(kFilePath); |
| 722 ASSERT_TRUE(entry.get()); | 697 ASSERT_TRUE(entry.get()); |
| 723 EXPECT_EQ("file:1_orphanfile_resource_id", entry->resource_id()); | 698 EXPECT_EQ("file:1_orphanfile_resource_id", entry->resource_id()); |
| 724 } | 699 } |
| 725 | 700 |
| 726 TEST_F(DriveFileSystemTest, ReadDirectoryByPath_Root) { | 701 TEST_F(DriveFileSystemTest, ReadDirectoryByPath_Root) { |
| 727 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( | 702 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( |
| 728 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1); | 703 Eq(base::FilePath(FILE_PATH_LITERAL("drive"))))).Times(1); |
| 729 | 704 |
| 730 // ReadDirectoryByPath() should kick off the resource list loading. | 705 // ReadDirectoryByPath() should kick off the resource list loading. |
| 731 scoped_ptr<DriveEntryProtoVector> entries( | 706 scoped_ptr<DriveEntryProtoVector> entries( |
| 732 ReadDirectoryByPathSync(base::FilePath::FromUTF8Unsafe("drive/root"))); | 707 ReadDirectoryByPathSync(base::FilePath::FromUTF8Unsafe("drive"))); |
| 733 // The root directory should be read correctly. | 708 // The root directory should be read correctly. |
| 734 ASSERT_TRUE(entries.get()); | 709 ASSERT_TRUE(entries.get()); |
| 735 EXPECT_EQ(8U, entries->size()); | 710 ASSERT_EQ(2U, entries->size()); |
| 711 |
| 712 // The found two directories shouold be /drive/root and /drive/other. |
| 713 bool found_other = false; |
| 714 bool found_my_drive = false; |
| 715 for (size_t i = 0; i < entries->size(); ++i) { |
| 716 const base::FilePath title = |
| 717 base::FilePath::FromUTF8Unsafe((*entries)[i].title()); |
| 718 if (title == base::FilePath(util::kDriveOtherDirName)) { |
| 719 found_other = true; |
| 720 } else if (title == base::FilePath(util::kDriveMyDriveRootDirName)) { |
| 721 found_my_drive = true; |
| 722 } |
| 723 } |
| 724 |
| 725 EXPECT_TRUE(found_other); |
| 726 EXPECT_TRUE(found_my_drive); |
| 736 } | 727 } |
| 737 | 728 |
| 738 TEST_F(DriveFileSystemTest, ReadDirectoryByPath_NonRootDirectory) { | 729 TEST_F(DriveFileSystemTest, ReadDirectoryByPath_NonRootDirectory) { |
| 739 // ReadDirectoryByPath() should kick off the resource list loading. | 730 // ReadDirectoryByPath() should kick off the resource list loading. |
| 740 scoped_ptr<DriveEntryProtoVector> entries( | 731 scoped_ptr<DriveEntryProtoVector> entries( |
| 741 ReadDirectoryByPathSync( | 732 ReadDirectoryByPathSync( |
| 742 base::FilePath::FromUTF8Unsafe("drive/root/Directory 1"))); | 733 base::FilePath::FromUTF8Unsafe("drive/root/Directory 1"))); |
| 743 // The non root directory should also be read correctly. | 734 // The non root directory should also be read correctly. |
| 744 // There was a bug (crbug.com/181487), which broke this behavior. | 735 // There was a bug (crbug.com/181487), which broke this behavior. |
| 745 // Make sure this is fixed. | 736 // Make sure this is fixed. |
| (...skipping 1440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2186 | 2177 |
| 2187 // An app for foo.exe should now be found, as the registry was loaded. | 2178 // An app for foo.exe should now be found, as the registry was loaded. |
| 2188 drive_webapps_registry_->GetWebAppsForFile( | 2179 drive_webapps_registry_->GetWebAppsForFile( |
| 2189 base::FilePath(FILE_PATH_LITERAL("foo.exe")), | 2180 base::FilePath(FILE_PATH_LITERAL("foo.exe")), |
| 2190 "" /* mime_type */, | 2181 "" /* mime_type */, |
| 2191 &apps); | 2182 &apps); |
| 2192 EXPECT_EQ(1U, apps.size()); | 2183 EXPECT_EQ(1U, apps.size()); |
| 2193 } | 2184 } |
| 2194 | 2185 |
| 2195 } // namespace drive | 2186 } // namespace drive |
| OLD | NEW |