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

Side by Side Diff: chrome/browser/chromeos/drive/file_system_unittest.cc

Issue 23606018: drive: Move some of FileSystemTest tests to lower layers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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 "chrome/browser/chromeos/drive/file_system.h" 5 #include "chrome/browser/chromeos/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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 76
77 } // namespace 77 } // namespace
78 78
79 class FileSystemTest : public testing::Test { 79 class FileSystemTest : public testing::Test {
80 protected: 80 protected:
81 virtual void SetUp() OVERRIDE { 81 virtual void SetUp() OVERRIDE {
82 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 82 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
83 pref_service_.reset(new TestingPrefServiceSimple); 83 pref_service_.reset(new TestingPrefServiceSimple);
84 test_util::RegisterDrivePrefs(pref_service_->registry()); 84 test_util::RegisterDrivePrefs(pref_service_->registry());
85 85
86 fake_network_change_notifier_.reset(
87 new test_util::FakeNetworkChangeNotifier);
88
89 fake_drive_service_.reset(new FakeDriveService); 86 fake_drive_service_.reset(new FakeDriveService);
90 fake_drive_service_->LoadResourceListForWapi( 87 fake_drive_service_->LoadResourceListForWapi(
91 "gdata/root_feed.json"); 88 "gdata/root_feed.json");
92 fake_drive_service_->LoadAccountMetadataForWapi( 89 fake_drive_service_->LoadAccountMetadataForWapi(
93 "gdata/account_metadata.json"); 90 "gdata/account_metadata.json");
94 91
95 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); 92 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
96 93
97 scheduler_.reset(new JobScheduler(pref_service_.get(), 94 scheduler_.reset(new JobScheduler(pref_service_.get(),
98 fake_drive_service_.get(), 95 fake_drive_service_.get(),
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 return entry.Pass(); 162 return entry.Pass();
166 } 163 }
167 164
168 // Gets directory info by path synchronously. 165 // Gets directory info by path synchronously.
169 scoped_ptr<ResourceEntryVector> ReadDirectoryByPathSync( 166 scoped_ptr<ResourceEntryVector> ReadDirectoryByPathSync(
170 const base::FilePath& file_path) { 167 const base::FilePath& file_path) {
171 FileError error = FILE_ERROR_FAILED; 168 FileError error = FILE_ERROR_FAILED;
172 scoped_ptr<ResourceEntryVector> entries; 169 scoped_ptr<ResourceEntryVector> entries;
173 file_system_->ReadDirectoryByPath( 170 file_system_->ReadDirectoryByPath(
174 file_path, 171 file_path,
175 google_apis::test_util::CreateCopyResultCallback( 172 google_apis::test_util::CreateCopyResultCallback(&error, &entries));
176 &error, &entries));
177 test_util::RunBlockingPoolTask(); 173 test_util::RunBlockingPoolTask();
178 174
179 return entries.Pass(); 175 return entries.Pass();
180 } 176 }
181 177
182 // Returns true if an entry exists at |file_path|. 178 // Returns true if an entry exists at |file_path|.
183 bool EntryExists(const base::FilePath& file_path) { 179 bool EntryExists(const base::FilePath& file_path) {
184 return GetResourceEntryByPathSync(file_path); 180 return GetResourceEntryByPathSync(file_path);
185 } 181 }
186 182
187 // Gets the resource ID of |file_path|. Returns an empty string if not found.
188 std::string GetResourceIdByPath(const base::FilePath& file_path) {
189 scoped_ptr<ResourceEntry> entry =
190 GetResourceEntryByPathSync(file_path);
191 if (entry)
192 return entry->resource_id();
193 else
194 return "";
195 }
196
197 // Flag for specifying the timestamp of the test filesystem cache. 183 // Flag for specifying the timestamp of the test filesystem cache.
198 enum SetUpTestFileSystemParam { 184 enum SetUpTestFileSystemParam {
199 USE_OLD_TIMESTAMP, 185 USE_OLD_TIMESTAMP,
200 USE_SERVER_TIMESTAMP, 186 USE_SERVER_TIMESTAMP,
201 }; 187 };
202 188
203 // Sets up a filesystem with directories: drive/root, drive/root/Dir1, 189 // Sets up a filesystem with directories: drive/root, drive/root/Dir1,
204 // drive/root/Dir1/SubDir2 and files drive/root/File1, drive/root/Dir1/File2, 190 // drive/root/Dir1/SubDir2 and files drive/root/File1, drive/root/Dir1/File2,
205 // drive/root/Dir1/SubDir2/File3. If |use_up_to_date_timestamp| is true, sets 191 // drive/root/Dir1/SubDir2/File3. If |use_up_to_date_timestamp| is true, sets
206 // the changestamp to 654321, equal to that of "account_metadata.json" test 192 // the changestamp to 654321, equal to that of "account_metadata.json" test
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 268
283 // Recreate resource metadata. 269 // Recreate resource metadata.
284 SetUpResourceMetadataAndFileSystem(); 270 SetUpResourceMetadataAndFileSystem();
285 } 271 }
286 272
287 content::TestBrowserThreadBundle thread_bundle_; 273 content::TestBrowserThreadBundle thread_bundle_;
288 base::ScopedTempDir temp_dir_; 274 base::ScopedTempDir temp_dir_;
289 // We don't use TestingProfile::GetPrefs() in favor of having less 275 // We don't use TestingProfile::GetPrefs() in favor of having less
290 // dependencies to Profile in general. 276 // dependencies to Profile in general.
291 scoped_ptr<TestingPrefServiceSimple> pref_service_; 277 scoped_ptr<TestingPrefServiceSimple> pref_service_;
292 scoped_ptr<test_util::FakeNetworkChangeNotifier>
293 fake_network_change_notifier_;
294 278
295 scoped_ptr<FakeDriveService> fake_drive_service_; 279 scoped_ptr<FakeDriveService> fake_drive_service_;
296 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; 280 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
297 scoped_ptr<JobScheduler> scheduler_; 281 scoped_ptr<JobScheduler> scheduler_;
298 scoped_ptr<MockDirectoryChangeObserver> mock_directory_observer_; 282 scoped_ptr<MockDirectoryChangeObserver> mock_directory_observer_;
299 283
300 scoped_ptr<internal::ResourceMetadataStorage, 284 scoped_ptr<internal::ResourceMetadataStorage,
301 test_util::DestroyHelperForTests> metadata_storage_; 285 test_util::DestroyHelperForTests> metadata_storage_;
302 scoped_ptr<internal::FileCache, test_util::DestroyHelperForTests> cache_; 286 scoped_ptr<internal::FileCache, test_util::DestroyHelperForTests> cache_;
303 scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests> 287 scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests>
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 EXPECT_EQ("document:5_document_resource_id", entry->resource_id()); 381 EXPECT_EQ("document:5_document_resource_id", entry->resource_id());
398 } 382 }
399 383
400 TEST_F(FileSystemTest, GetNonExistingFile) { 384 TEST_F(FileSystemTest, GetNonExistingFile) {
401 const base::FilePath kFilePath( 385 const base::FilePath kFilePath(
402 FILE_PATH_LITERAL("drive/root/nonexisting.file")); 386 FILE_PATH_LITERAL("drive/root/nonexisting.file"));
403 scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath); 387 scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath);
404 EXPECT_FALSE(entry); 388 EXPECT_FALSE(entry);
405 } 389 }
406 390
407 TEST_F(FileSystemTest, GetEncodedFileNames) {
408 const base::FilePath kFilePath1(
409 FILE_PATH_LITERAL("drive/root/Slash / in file 1.txt"));
410 scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath1);
411 ASSERT_FALSE(entry);
412
413 const base::FilePath kFilePath2 = base::FilePath::FromUTF8Unsafe(
414 "drive/root/Slash \xE2\x88\x95 in file 1.txt");
415 entry = GetResourceEntryByPathSync(kFilePath2);
416 ASSERT_TRUE(entry);
417 EXPECT_EQ("file:slash_file_resource_id", entry->resource_id());
418
419 const base::FilePath kFilePath3 = base::FilePath::FromUTF8Unsafe(
420 "drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt");
421 entry = GetResourceEntryByPathSync(kFilePath3);
422 ASSERT_TRUE(entry);
423 EXPECT_EQ("file:slash_subdir_file", entry->resource_id());
424 }
425
426 TEST_F(FileSystemTest, GetDuplicateNames) {
427 const base::FilePath kFilePath1(
428 FILE_PATH_LITERAL("drive/root/Duplicate Name.txt"));
429 scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath1);
430 ASSERT_TRUE(entry);
431 const std::string resource_id1 = entry->resource_id();
432
433 const base::FilePath kFilePath2(
434 FILE_PATH_LITERAL("drive/root/Duplicate Name (1).txt"));
435 entry = GetResourceEntryByPathSync(kFilePath2);
436 ASSERT_TRUE(entry);
437 const std::string resource_id2 = entry->resource_id();
438
439 // The entries are de-duped non-deterministically, so we shouldn't rely on the
440 // names matching specific resource ids.
441 const std::string file3_resource_id = "file:3_file_resource_id";
442 const std::string file4_resource_id = "file:4_file_resource_id";
443 EXPECT_TRUE(file3_resource_id == resource_id1 ||
444 file3_resource_id == resource_id2);
445 EXPECT_TRUE(file4_resource_id == resource_id1 ||
446 file4_resource_id == resource_id2);
447 }
448
449 TEST_F(FileSystemTest, GetExistingDirectory) { 391 TEST_F(FileSystemTest, GetExistingDirectory) {
450 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/Directory 1")); 392 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/Directory 1"));
451 scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath); 393 scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath);
452 ASSERT_TRUE(entry); 394 ASSERT_TRUE(entry);
453 ASSERT_EQ("folder:1_folder_resource_id", entry->resource_id()); 395 ASSERT_EQ("folder:1_folder_resource_id", entry->resource_id());
454 396
455 // The changestamp should be propagated to the directory. 397 // The changestamp should be propagated to the directory.
456 EXPECT_EQ(fake_drive_service_->largest_changestamp(), 398 EXPECT_EQ(fake_drive_service_->largest_changestamp(),
457 entry->directory_specific_info().changestamp()); 399 entry->directory_specific_info().changestamp());
458 } 400 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 test_util::RunBlockingPoolTask(); 476 test_util::RunBlockingPoolTask();
535 EXPECT_EQ(2, fake_drive_service_->about_resource_load_count()); 477 EXPECT_EQ(2, fake_drive_service_->about_resource_load_count());
536 } 478 }
537 479
538 TEST_F(FileSystemTest, LoadFileSystemFromCacheWhileOffline) { 480 TEST_F(FileSystemTest, LoadFileSystemFromCacheWhileOffline) {
539 ASSERT_NO_FATAL_FAILURE(SetUpTestFileSystem(USE_OLD_TIMESTAMP)); 481 ASSERT_NO_FATAL_FAILURE(SetUpTestFileSystem(USE_OLD_TIMESTAMP));
540 482
541 // Make GetResourceList fail for simulating offline situation. This will 483 // Make GetResourceList fail for simulating offline situation. This will
542 // leave the file system "loaded from cache, but not synced with server" 484 // leave the file system "loaded from cache, but not synced with server"
543 // state. 485 // state.
544 fake_network_change_notifier_->SetConnectionType(
545 net::NetworkChangeNotifier::CONNECTION_NONE);
546 fake_drive_service_->set_offline(true); 486 fake_drive_service_->set_offline(true);
547 487
548 // Load the root. 488 // Load the root.
549 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveGrandRootPath())); 489 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveGrandRootPath()));
550 // Loading of about resource should not happen as it's offline. 490 // Loading of about resource should not happen as it's offline.
551 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); 491 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count());
552 492
553 // Load "My Drive". 493 // Load "My Drive".
554 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveMyDriveRootPath())); 494 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveMyDriveRootPath()));
555 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); 495 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count());
556 496
557 // Tests that cached data can be loaded even if the server is not reachable. 497 // Tests that cached data can be loaded even if the server is not reachable.
558 EXPECT_TRUE(EntryExists(base::FilePath( 498 EXPECT_TRUE(EntryExists(base::FilePath(
559 FILE_PATH_LITERAL("drive/root/File1")))); 499 FILE_PATH_LITERAL("drive/root/File1"))));
560 EXPECT_TRUE(EntryExists(base::FilePath( 500 EXPECT_TRUE(EntryExists(base::FilePath(
561 FILE_PATH_LITERAL("drive/root/Dir1")))); 501 FILE_PATH_LITERAL("drive/root/Dir1"))));
562 EXPECT_TRUE( 502 EXPECT_TRUE(EntryExists(base::FilePath(
563 EntryExists(base::FilePath(FILE_PATH_LITERAL("drive/root/Dir1/File2")))); 503 FILE_PATH_LITERAL("drive/root/Dir1/File2"))));
564 EXPECT_TRUE(EntryExists(base::FilePath( 504 EXPECT_TRUE(EntryExists(base::FilePath(
565 FILE_PATH_LITERAL("drive/root/Dir1/SubDir2")))); 505 FILE_PATH_LITERAL("drive/root/Dir1/SubDir2"))));
566 EXPECT_TRUE(EntryExists( 506 EXPECT_TRUE(EntryExists(base::FilePath(
567 base::FilePath(FILE_PATH_LITERAL("drive/root/Dir1/SubDir2/File3")))); 507 FILE_PATH_LITERAL("drive/root/Dir1/SubDir2/File3"))));
568 508
569 // Since the file system has at least succeeded to load cached snapshot, 509 // Since the file system has at least succeeded to load cached snapshot,
570 // the file system should be able to start periodic refresh. 510 // the file system should be able to start periodic refresh.
571 // To test it, call CheckForUpdates and verify it does try to check 511 // To test it, call CheckForUpdates and verify it does try to check
572 // updates, which will cause directory changes. 512 // updates, which will cause directory changes.
573 fake_network_change_notifier_->SetConnectionType(
574 net::NetworkChangeNotifier::CONNECTION_WIFI);
575 fake_drive_service_->set_offline(false); 513 fake_drive_service_->set_offline(false);
576 514
577 file_system_->CheckForUpdates(); 515 file_system_->CheckForUpdates();
578 516
579 test_util::RunBlockingPoolTask(); 517 test_util::RunBlockingPoolTask();
580 EXPECT_EQ(1, fake_drive_service_->about_resource_load_count()); 518 EXPECT_EQ(1, fake_drive_service_->about_resource_load_count());
581 EXPECT_EQ(1, fake_drive_service_->change_list_load_count()); 519 EXPECT_EQ(1, fake_drive_service_->change_list_load_count());
582 520
583 ASSERT_LE(1u, mock_directory_observer_->changed_directories().size()); 521 ASSERT_LE(1u, mock_directory_observer_->changed_directories().size());
584 } 522 }
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 int64 bytes_total; 647 int64 bytes_total;
710 int64 bytes_used; 648 int64 bytes_used;
711 file_system_->GetAvailableSpace( 649 file_system_->GetAvailableSpace(
712 google_apis::test_util::CreateCopyResultCallback( 650 google_apis::test_util::CreateCopyResultCallback(
713 &error, &bytes_total, &bytes_used)); 651 &error, &bytes_total, &bytes_used));
714 test_util::RunBlockingPoolTask(); 652 test_util::RunBlockingPoolTask();
715 EXPECT_EQ(GG_LONGLONG(6789012345), bytes_used); 653 EXPECT_EQ(GG_LONGLONG(6789012345), bytes_used);
716 EXPECT_EQ(GG_LONGLONG(9876543210), bytes_total); 654 EXPECT_EQ(GG_LONGLONG(9876543210), bytes_total);
717 } 655 }
718 656
719 TEST_F(FileSystemTest, OpenAndCloseFile) {
720 ASSERT_TRUE(LoadFullResourceList());
721
722 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt"));
723 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot));
724 const std::string& file_resource_id = entry->resource_id();
725
726 // Open kFileInRoot ("drive/root/File 1.txt").
727 FileError error = FILE_ERROR_FAILED;
728 base::FilePath file_path;
729 base::Closure close_callback;
730 file_system_->OpenFile(
731 kFileInRoot,
732 OPEN_FILE,
733 std::string(), // mime_type
734 google_apis::test_util::CreateCopyResultCallback(
735 &error, &file_path, &close_callback));
736 test_util::RunBlockingPoolTask();
737 const base::FilePath opened_file_path = file_path;
738
739 // Verify that the file was properly opened.
740 EXPECT_EQ(FILE_ERROR_OK, error);
741
742 // The opened file is downloaded, which means the file is available
743 // offline. The directory change should be notified so Files.app can change
744 // the offline availability status of the file.
745 ASSERT_EQ(1u, mock_directory_observer_->changed_directories().size());
746 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("drive/root")),
747 mock_directory_observer_->changed_directories()[0]);
748
749 // Verify that the file contents match the expected contents.
750 const std::string kExpectedContent = "This is some test content.";
751 std::string cache_file_data;
752 EXPECT_TRUE(base::ReadFileToString(opened_file_path, &cache_file_data));
753 EXPECT_EQ(kExpectedContent, cache_file_data);
754
755 FileCacheEntry cache_entry;
756 EXPECT_TRUE(cache_->GetCacheEntry(file_resource_id, &cache_entry));
757 EXPECT_TRUE(cache_entry.is_present());
758 EXPECT_TRUE(cache_entry.is_dirty());
759
760 base::FilePath cache_file_path;
761 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(file_resource_id, &cache_file_path));
762 EXPECT_EQ(cache_file_path, opened_file_path);
763
764 // Write a new content.
765 const std::string kNewContent = kExpectedContent + kExpectedContent;
766 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path,
767 kNewContent));
768
769 // Close kFileInRoot ("drive/root/File 1.txt").
770 ASSERT_FALSE(close_callback.is_null());
771 close_callback.Run();
772 test_util::RunBlockingPoolTask();
773
774 // Verify that the file was properly closed.
775 EXPECT_EQ(FILE_ERROR_OK, error);
776
777 // Verify that the file was synced as expected.
778 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_FILE_ERROR;
779 scoped_ptr<google_apis::ResourceEntry> gdata_entry;
780 fake_drive_service_->GetResourceEntry(
781 file_resource_id,
782 google_apis::test_util::CreateCopyResultCallback(
783 &gdata_error, &gdata_entry));
784 test_util::RunBlockingPoolTask();
785 EXPECT_EQ(gdata_error, google_apis::HTTP_SUCCESS);
786 ASSERT_TRUE(gdata_entry);
787 EXPECT_EQ(static_cast<int>(kNewContent.size()), gdata_entry->file_size());
788
789 // The modified file is uploaded. The directory change should be notified
790 // so Files.app can show new metadata of the modified file.
791 ASSERT_EQ(2u, mock_directory_observer_->changed_directories().size());
792 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("drive/root")),
793 mock_directory_observer_->changed_directories()[1]);
794 }
795
796 TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) { 657 TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) {
797 ASSERT_TRUE(LoadFullResourceList()); 658 ASSERT_TRUE(LoadFullResourceList());
798 659
799 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); 660 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
800 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root)); 661 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
801 ASSERT_TRUE(entry); 662 ASSERT_TRUE(entry);
802 663
803 // Write to cache. 664 // Write to cache.
804 ASSERT_EQ(FILE_ERROR_OK, cache_->Store( 665 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(
805 entry->resource_id(), 666 entry->resource_id(),
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 kEmbedOrigin, 728 kEmbedOrigin,
868 google_apis::test_util::CreateCopyResultCallback(&error, &share_url)); 729 google_apis::test_util::CreateCopyResultCallback(&error, &share_url));
869 test_util::RunBlockingPoolTask(); 730 test_util::RunBlockingPoolTask();
870 731
871 // Verify the error and the share url, which should be empty. 732 // Verify the error and the share url, which should be empty.
872 EXPECT_EQ(FILE_ERROR_FAILED, error); 733 EXPECT_EQ(FILE_ERROR_FAILED, error);
873 EXPECT_TRUE(share_url.is_empty()); 734 EXPECT_TRUE(share_url.is_empty());
874 } 735 }
875 736
876 } // namespace drive 737 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698