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

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

Issue 23258006: drive: Move RefreshDirectory from ResourceMetadata to ChangeListLoader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fix Created 7 years, 4 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/resource_metadata.h" 5 #include "chrome/browser/chromeos/drive/resource_metadata.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 resource_metadata_->RenameEntryOnUIThread( 530 resource_metadata_->RenameEntryOnUIThread(
531 base::FilePath::FromUTF8Unsafe("drive/root/dir2/file11"), 531 base::FilePath::FromUTF8Unsafe("drive/root/dir2/file11"),
532 "file11", 532 "file11",
533 google_apis::test_util::CreateCopyResultCallback( 533 google_apis::test_util::CreateCopyResultCallback(
534 &error, &drive_file_path)); 534 &error, &drive_file_path));
535 test_util::RunBlockingPoolTask(); 535 test_util::RunBlockingPoolTask();
536 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); 536 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
537 EXPECT_EQ(base::FilePath(), drive_file_path); 537 EXPECT_EQ(base::FilePath(), drive_file_path);
538 } 538 }
539 539
540 TEST_F(ResourceMetadataTestOnUIThread, RefreshDirectory_EmptyMap) {
541 base::FilePath kDirectoryPath(FILE_PATH_LITERAL("drive/root/dir1"));
542 const int64 kNewChangestamp = kTestChangestamp + 1;
543
544 // Read the directory.
545 FileError error = FILE_ERROR_FAILED;
546 scoped_ptr<ResourceEntryVector> entries;
547 entries = ReadDirectoryByPathSync(base::FilePath(kDirectoryPath));
548 ASSERT_TRUE(entries.get());
549 // "file4", "file5", "dir3" should exist in drive/dir1.
550 ASSERT_EQ(3U, entries->size());
551 std::vector<std::string> base_names = GetSortedBaseNames(*entries);
552 EXPECT_EQ("dir3", base_names[0]);
553 EXPECT_EQ("file4", base_names[1]);
554 EXPECT_EQ("file5", base_names[2]);
555
556 // Get the directory.
557 scoped_ptr<ResourceEntry> dir1_proto;
558 dir1_proto = GetResourceEntryByPathSync(kDirectoryPath);
559 ASSERT_TRUE(dir1_proto.get());
560 // The changestamp should be initially kTestChangestamp.
561 EXPECT_EQ(kTestChangestamp,
562 dir1_proto->directory_specific_info().changestamp());
563
564 // Update the directory with an empty map.
565 base::FilePath file_path;
566 ResourceEntryMap entry_map;
567 resource_metadata_->RefreshDirectoryOnUIThread(
568 DirectoryFetchInfo(dir1_proto->resource_id(), kNewChangestamp),
569 entry_map,
570 google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
571 test_util::RunBlockingPoolTask();
572 EXPECT_EQ(FILE_ERROR_OK, error);
573 EXPECT_EQ(kDirectoryPath, file_path);
574
575 // Get the directory again.
576 dir1_proto = GetResourceEntryByPathSync(kDirectoryPath);
577 ASSERT_TRUE(dir1_proto.get());
578 // The new changestamp should be set.
579 EXPECT_EQ(kNewChangestamp,
580 dir1_proto->directory_specific_info().changestamp());
581
582 // Read the directory again.
583 entries = ReadDirectoryByPathSync(base::FilePath(kDirectoryPath));
584 ASSERT_TRUE(entries.get());
585 }
586
587 TEST_F(ResourceMetadataTestOnUIThread, RefreshDirectory_NonEmptyMap) {
588 base::FilePath kDirectoryPath(FILE_PATH_LITERAL("drive/root/dir1"));
589 const int64 kNewChangestamp = kTestChangestamp + 1;
590
591 // Read the directory.
592 FileError error = FILE_ERROR_FAILED;
593 scoped_ptr<ResourceEntryVector> entries;
594 entries = ReadDirectoryByPathSync(kDirectoryPath);
595 ASSERT_TRUE(entries.get());
596 // "file4", "file5", "dir3" should exist in drive/dir1.
597 ASSERT_EQ(3U, entries->size());
598 std::vector<std::string> base_names = GetSortedBaseNames(*entries);
599 EXPECT_EQ("dir3", base_names[0]);
600 EXPECT_EQ("file4", base_names[1]);
601 EXPECT_EQ("file5", base_names[2]);
602
603 // Get the directory dir1.
604 scoped_ptr<ResourceEntry> dir1_proto;
605 dir1_proto = GetResourceEntryByPathSync(kDirectoryPath);
606 ASSERT_TRUE(dir1_proto.get());
607 // The changestamp should be initially kTestChangestamp.
608 EXPECT_EQ(kTestChangestamp,
609 dir1_proto->directory_specific_info().changestamp());
610
611 // Get the directory dir2 (existing non-child directory).
612 // This directory will be moved to "drive/dir1/dir2".
613 scoped_ptr<ResourceEntry> dir2_proto;
614 dir2_proto = GetResourceEntryByPathSync(
615 base::FilePath::FromUTF8Unsafe("drive/root/dir2"));
616 ASSERT_TRUE(dir2_proto.get());
617 EXPECT_EQ(kTestChangestamp,
618 dir2_proto->directory_specific_info().changestamp());
619 // Change the parent resource ID, as dir2 will be moved to "drive/dir1/dir2".
620 dir2_proto->set_parent_local_id(dir1_proto->resource_id());
621
622 // Get the directory dir3 (existing child directory).
623 // This directory will remain as "drive/dir1/dir3".
624 scoped_ptr<ResourceEntry> dir3_proto;
625 dir3_proto = GetResourceEntryByPathSync(
626 base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"));
627 ASSERT_TRUE(dir3_proto.get());
628 EXPECT_EQ(kTestChangestamp,
629 dir3_proto->directory_specific_info().changestamp());
630
631 // Create a map.
632 ResourceEntryMap entry_map;
633
634 // Add a new file to the map.
635 ResourceEntry new_file;
636 new_file.set_title("new_file");
637 new_file.set_resource_id("new_file_id");
638 new_file.set_parent_local_id(dir1_proto->resource_id());
639 entry_map["new_file_id"] = new_file;
640
641 // Add a new directory to the map.
642 ResourceEntry new_directory;
643 new_directory.set_title("new_directory");
644 new_directory.set_resource_id("new_directory_id");
645 new_directory.set_parent_local_id(dir1_proto->resource_id());
646 new_directory.mutable_file_info()->set_is_directory(true);
647 entry_map["new_directory_id"] = new_directory;
648
649 // Add dir2 and dir3 as well.
650 entry_map[dir2_proto->resource_id()] = *dir2_proto;
651 entry_map[dir3_proto->resource_id()] = *dir3_proto;
652
653 // Update the directory with the map.
654 base::FilePath file_path;
655 resource_metadata_->RefreshDirectoryOnUIThread(
656 DirectoryFetchInfo(dir1_proto->resource_id(), kNewChangestamp),
657 entry_map,
658 google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
659 test_util::RunBlockingPoolTask();
660 EXPECT_EQ(FILE_ERROR_OK, error);
661 EXPECT_EQ(kDirectoryPath, file_path);
662
663 // Get the directory again.
664 dir1_proto = GetResourceEntryByPathSync(kDirectoryPath);
665 ASSERT_TRUE(dir1_proto.get());
666 // The new changestamp should be set.
667 EXPECT_EQ(kNewChangestamp,
668 dir1_proto->directory_specific_info().changestamp());
669
670 // Read the directory again.
671 entries = ReadDirectoryByPathSync(kDirectoryPath);
672 ASSERT_TRUE(entries.get());
673 // "new_file", "new_directory", "dir2" should now be added.
674 base_names = GetSortedBaseNames(*entries);
675 EXPECT_EQ(1, std::count(base_names.begin(), base_names.end(), "dir2"));
676 EXPECT_EQ(1,
677 std::count(base_names.begin(), base_names.end(), "new_directory"));
678 EXPECT_EQ(1,
679 std::count(base_names.begin(), base_names.end(), "new_file"));
680
681 // Get the new directory.
682 scoped_ptr<ResourceEntry> new_directory_proto;
683 new_directory_proto = GetResourceEntryByPathSync(
684 base::FilePath::FromUTF8Unsafe("drive/root/dir1/new_directory"));
685 ASSERT_TRUE(new_directory_proto.get());
686 // The changestamp should be 0 for a new directory.
687 EXPECT_EQ(0, new_directory_proto->directory_specific_info().changestamp());
688
689 // Get the directory dir3 (existing child directory) again.
690 dir3_proto = GetResourceEntryByPathSync(
691 base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"));
692 ASSERT_TRUE(dir3_proto.get());
693 // The changestamp should not be changed.
694 EXPECT_EQ(kTestChangestamp,
695 dir3_proto->directory_specific_info().changestamp());
696
697 // Read the directory dir3. The contents should remain.
698 // See the comment at Init() for the contents of the dir3.
699 entries = ReadDirectoryByPathSync(
700 base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"));
701 ASSERT_TRUE(entries.get());
702 ASSERT_EQ(2U, entries->size());
703
704 // Get the directory dir2 (existing non-child directory) again using the
705 // old path. This should fail, as dir2 is now moved to drive/dir1/dir2.
706 dir2_proto = GetResourceEntryByPathSync(
707 base::FilePath::FromUTF8Unsafe("drive/root/dir2"));
708 ASSERT_FALSE(dir2_proto.get());
709
710 // Get the directory dir2 (existing non-child directory) again using the
711 // new path. This should succeed.
712 dir2_proto = GetResourceEntryByPathSync(
713 base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir2"));
714 ASSERT_TRUE(dir2_proto.get());
715 // The changestamp should not be changed.
716 EXPECT_EQ(kTestChangestamp,
717 dir2_proto->directory_specific_info().changestamp());
718
719 // Read the directory dir2. The contents should remain.
720 // See the comment at Init() for the contents of the dir2.
721 entries = ReadDirectoryByPathSync(
722 base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir2"));
723 ASSERT_TRUE(entries.get());
724 ASSERT_EQ(3U, entries->size());
725 }
726
727 TEST_F(ResourceMetadataTestOnUIThread, RefreshDirectory_WrongParentResourceId) {
728 base::FilePath kDirectoryPath(FILE_PATH_LITERAL("drive/root/dir1"));
729 const int64 kNewChangestamp = kTestChangestamp + 1;
730
731 // Get the directory dir1.
732 scoped_ptr<ResourceEntry> dir1_proto;
733 dir1_proto = GetResourceEntryByPathSync(kDirectoryPath);
734 ASSERT_TRUE(dir1_proto.get());
735
736 // Create a map and add a new file to it.
737 ResourceEntryMap entry_map;
738 ResourceEntry new_file;
739 new_file.set_title("new_file");
740 new_file.set_resource_id("new_file_id");
741 // Set a random parent resource ID. This entry should not be added because
742 // the parent resource ID does not match dir1_proto->resource_id().
743 new_file.set_parent_local_id("some-random-resource-id");
744 entry_map["new_file_id"] = new_file;
745
746 // Update the directory with the map.
747 base::FilePath file_path;
748 FileError error = FILE_ERROR_FAILED;
749 resource_metadata_->RefreshDirectoryOnUIThread(
750 DirectoryFetchInfo(dir1_proto->resource_id(), kNewChangestamp),
751 entry_map,
752 google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
753 test_util::RunBlockingPoolTask();
754 EXPECT_EQ(FILE_ERROR_OK, error);
755 EXPECT_EQ(kDirectoryPath, file_path);
756
757 // Read the directory. Confirm that the new file is not added.
758 scoped_ptr<ResourceEntryVector> entries;
759 entries = ReadDirectoryByPathSync(kDirectoryPath);
760 ASSERT_TRUE(entries.get());
761 std::vector<std::string> base_names = GetSortedBaseNames(*entries);
762 EXPECT_EQ(0, std::count(base_names.begin(), base_names.end(), "new_file"));
763 }
764
765 TEST_F(ResourceMetadataTestOnUIThread, AddEntry) { 540 TEST_F(ResourceMetadataTestOnUIThread, AddEntry) {
766 FileError error = FILE_ERROR_FAILED; 541 FileError error = FILE_ERROR_FAILED;
767 base::FilePath drive_file_path; 542 base::FilePath drive_file_path;
768 543
769 // Add a file to dir3. 544 // Add a file to dir3.
770 ResourceEntry file_entry = CreateFileEntry("file100", "resource_id:dir3"); 545 ResourceEntry file_entry = CreateFileEntry("file100", "resource_id:dir3");
771 resource_metadata_->AddEntryOnUIThread( 546 resource_metadata_->AddEntryOnUIThread(
772 file_entry, 547 file_entry,
773 google_apis::test_util::CreateCopyResultCallback( 548 google_apis::test_util::CreateCopyResultCallback(
774 &error, &drive_file_path)); 549 &error, &drive_file_path));
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 else 853 else
1079 ++directory_count; 854 ++directory_count;
1080 } 855 }
1081 856
1082 EXPECT_EQ(7, file_count); 857 EXPECT_EQ(7, file_count);
1083 EXPECT_EQ(6, directory_count); 858 EXPECT_EQ(6, directory_count);
1084 } 859 }
1085 860
1086 } // namespace internal 861 } // namespace internal
1087 } // namespace drive 862 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/resource_metadata.cc ('k') | chrome/browser/chromeos/drive/sync_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698