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/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 23 matching lines...) Expand all Loading... |
34 std::vector<std::string> GetSortedBaseNames( | 34 std::vector<std::string> GetSortedBaseNames( |
35 const ResourceEntryVector& entries) { | 35 const ResourceEntryVector& entries) { |
36 std::vector<std::string> base_names; | 36 std::vector<std::string> base_names; |
37 for (size_t i = 0; i < entries.size(); ++i) | 37 for (size_t i = 0; i < entries.size(); ++i) |
38 base_names.push_back(entries[i].base_name()); | 38 base_names.push_back(entries[i].base_name()); |
39 std::sort(base_names.begin(), base_names.end()); | 39 std::sort(base_names.begin(), base_names.end()); |
40 | 40 |
41 return base_names; | 41 return base_names; |
42 } | 42 } |
43 | 43 |
44 // Creates a ResourceEntry for a directory. | 44 // Creates a ResourceEntry for a directory with explicitly set resource_id. |
45 ResourceEntry CreateDirectoryEntry(const std::string& title, | 45 ResourceEntry CreateDirectoryEntryWithResourceId( |
46 const std::string& parent_local_id) { | 46 const std::string& title, |
| 47 const std::string& resource_id, |
| 48 const std::string& parent_local_id) { |
47 ResourceEntry entry; | 49 ResourceEntry entry; |
48 entry.set_title(title); | 50 entry.set_title(title); |
49 entry.set_resource_id("id:" + title); | 51 entry.set_resource_id(resource_id); |
50 entry.set_parent_local_id(parent_local_id); | 52 entry.set_parent_local_id(parent_local_id); |
51 entry.mutable_file_info()->set_is_directory(true); | 53 entry.mutable_file_info()->set_is_directory(true); |
52 entry.mutable_directory_specific_info()->set_changestamp(kTestChangestamp); | 54 entry.mutable_directory_specific_info()->set_changestamp(kTestChangestamp); |
53 return entry; | 55 return entry; |
54 } | 56 } |
55 | 57 |
56 // Creates a ResourceEntry for a file. | 58 // Creates a ResourceEntry for a directory. |
57 ResourceEntry CreateFileEntry(const std::string& title, | 59 ResourceEntry CreateDirectoryEntry(const std::string& title, |
58 const std::string& parent_local_id) { | 60 const std::string& parent_local_id) { |
| 61 return CreateDirectoryEntryWithResourceId( |
| 62 title, "id:" + title, parent_local_id); |
| 63 } |
| 64 |
| 65 // Creates a ResourceEntry for a file with explicitly set resource_id. |
| 66 ResourceEntry CreateFileEntryWithResourceId( |
| 67 const std::string& title, |
| 68 const std::string& resource_id, |
| 69 const std::string& parent_local_id) { |
59 ResourceEntry entry; | 70 ResourceEntry entry; |
60 entry.set_title(title); | 71 entry.set_title(title); |
61 entry.set_resource_id("id:" + title); | 72 entry.set_resource_id(resource_id); |
62 entry.set_parent_local_id(parent_local_id); | 73 entry.set_parent_local_id(parent_local_id); |
63 entry.mutable_file_info()->set_is_directory(false); | 74 entry.mutable_file_info()->set_is_directory(false); |
64 entry.mutable_file_info()->set_size(1024); | 75 entry.mutable_file_info()->set_size(1024); |
65 entry.mutable_file_specific_info()->set_md5("md5:" + title); | 76 entry.mutable_file_specific_info()->set_md5("md5:" + title); |
66 return entry; | 77 return entry; |
67 } | 78 } |
68 | 79 |
| 80 // Creates a ResourceEntry for a file. |
| 81 ResourceEntry CreateFileEntry(const std::string& title, |
| 82 const std::string& parent_local_id) { |
| 83 return CreateFileEntryWithResourceId(title, "id:" + title, parent_local_id); |
| 84 } |
| 85 |
69 // Creates the following files/directories | 86 // Creates the following files/directories |
70 // drive/root/dir1/ | 87 // drive/root/dir1/ |
71 // drive/root/dir2/ | 88 // drive/root/dir2/ |
72 // drive/root/dir1/dir3/ | 89 // drive/root/dir1/dir3/ |
73 // drive/root/dir1/file4 | 90 // drive/root/dir1/file4 |
74 // drive/root/dir1/file5 | 91 // drive/root/dir1/file5 |
75 // drive/root/dir2/file6 | 92 // drive/root/dir2/file6 |
76 // drive/root/dir2/file7 | 93 // drive/root/dir2/file7 |
77 // drive/root/dir2/file8 | 94 // drive/root/dir2/file8 |
78 // drive/root/dir1/dir3/file9 | 95 // drive/root/dir1/dir3/file9 |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 if (!it->GetValue().file_info().is_directory()) | 661 if (!it->GetValue().file_info().is_directory()) |
645 ++file_count; | 662 ++file_count; |
646 else | 663 else |
647 ++directory_count; | 664 ++directory_count; |
648 } | 665 } |
649 | 666 |
650 EXPECT_EQ(7, file_count); | 667 EXPECT_EQ(7, file_count); |
651 EXPECT_EQ(6, directory_count); | 668 EXPECT_EQ(6, directory_count); |
652 } | 669 } |
653 | 670 |
| 671 TEST_F(ResourceMetadataTest, DuplicatedNames) { |
| 672 ResourceEntry entry; |
| 673 |
| 674 // When multiple entries with the same title are added in a single directory, |
| 675 // their base_names are de-duped. |
| 676 // - drive/root/foo |
| 677 // - drive/root/foo (1) |
| 678 std::string dir_id_0; |
| 679 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 680 CreateDirectoryEntryWithResourceId( |
| 681 "foo", "foo0", kTestRootResourceId), &dir_id_0)); |
| 682 std::string dir_id_1; |
| 683 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 684 CreateDirectoryEntryWithResourceId( |
| 685 "foo", "foo1", kTestRootResourceId), &dir_id_1)); |
| 686 |
| 687 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 688 dir_id_0, &entry)); |
| 689 EXPECT_EQ("foo", entry.base_name()); |
| 690 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 691 dir_id_1, &entry)); |
| 692 EXPECT_EQ("foo (1)", entry.base_name()); |
| 693 |
| 694 // - drive/root/foo/bar.txt |
| 695 // - drive/root/foo/bar (1).txt |
| 696 // - drive/root/foo/bar (2).txt |
| 697 std::string file_id_0; |
| 698 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 699 CreateFileEntryWithResourceId( |
| 700 "bar.txt", "bar0", dir_id_0), &file_id_0)); |
| 701 std::string file_id_1; |
| 702 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 703 CreateFileEntryWithResourceId( |
| 704 "bar.txt", "bar1", dir_id_0), &file_id_1)); |
| 705 std::string file_id_2; |
| 706 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 707 CreateFileEntryWithResourceId( |
| 708 "bar.txt", "bar2", dir_id_0), &file_id_2)); |
| 709 |
| 710 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 711 file_id_0, &entry)); |
| 712 EXPECT_EQ("bar.txt", entry.base_name()); |
| 713 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 714 file_id_1, &entry)); |
| 715 EXPECT_EQ("bar (1).txt", entry.base_name()); |
| 716 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 717 file_id_2, &entry)); |
| 718 EXPECT_EQ("bar (2).txt", entry.base_name()); |
| 719 |
| 720 // Same name but different parent. No renaming. |
| 721 // - drive/root/foo (1)/bar.txt |
| 722 std::string file_id_3; |
| 723 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 724 CreateFileEntryWithResourceId( |
| 725 "bar.txt", "bar3", dir_id_1), &file_id_3)); |
| 726 |
| 727 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 728 file_id_3, &entry)); |
| 729 EXPECT_EQ("bar.txt", entry.base_name()); |
| 730 |
| 731 // Checks that the entries can be looked up by the de-duped paths. |
| 732 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryByPath( |
| 733 base::FilePath::FromUTF8Unsafe("drive/root/foo/bar (2).txt"), &entry)); |
| 734 EXPECT_EQ("bar2", entry.resource_id()); |
| 735 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryByPath( |
| 736 base::FilePath::FromUTF8Unsafe("drive/root/foo (1)/bar.txt"), &entry)); |
| 737 EXPECT_EQ("bar3", entry.resource_id()); |
| 738 } |
| 739 |
| 740 TEST_F(ResourceMetadataTest, EncodedNames) { |
| 741 ResourceEntry entry; |
| 742 |
| 743 std::string dir_id; |
| 744 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 745 CreateDirectoryEntry("\\(^o^)/", kTestRootResourceId), &dir_id)); |
| 746 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 747 dir_id, &entry)); |
| 748 EXPECT_EQ("\\(^o^)\xE2\x88\x95", entry.base_name()); |
| 749 |
| 750 std::string file_id; |
| 751 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry( |
| 752 CreateFileEntryWithResourceId("Slash /.txt", "myfile", dir_id), |
| 753 &file_id)); |
| 754 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById( |
| 755 file_id, &entry)); |
| 756 EXPECT_EQ("Slash \xE2\x88\x95.txt", entry.base_name()); |
| 757 |
| 758 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryByPath( |
| 759 base::FilePath::FromUTF8Unsafe( |
| 760 "drive/root/\\(^o^)\xE2\x88\x95/Slash \xE2\x88\x95.txt"), |
| 761 &entry)); |
| 762 EXPECT_EQ("myfile", entry.resource_id()); |
| 763 } |
| 764 |
654 } // namespace internal | 765 } // namespace internal |
655 } // namespace drive | 766 } // namespace drive |
OLD | NEW |