| 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_prefetcher.h" | 5 #include "chrome/browser/chromeos/drive/drive_prefetcher.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 17 matching lines...) Expand all Loading... |
| 28 enum TestEntryType { | 28 enum TestEntryType { |
| 29 TYPE_DIRECTORY, | 29 TYPE_DIRECTORY, |
| 30 TYPE_REGULAR_FILE, | 30 TYPE_REGULAR_FILE, |
| 31 TYPE_HOSTED_FILE, | 31 TYPE_HOSTED_FILE, |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 // TestEntry represents a dummy entry for mocking a filesystem. | 34 // TestEntry represents a dummy entry for mocking a filesystem. |
| 35 struct TestEntry { | 35 struct TestEntry { |
| 36 const FilePath::CharType* path; | 36 const FilePath::CharType* path; |
| 37 TestEntryType entry_type; | 37 TestEntryType entry_type; |
| 38 int64 last_access; | 38 int64 last_accessed; |
| 39 int64 last_modified; |
| 39 const char* resource_id; | 40 const char* resource_id; |
| 40 int64 file_size; | 41 int64 file_size; |
| 41 | 42 |
| 42 // Checks whether this TestEntry is the direct content of the |directory|. | 43 // Checks whether this TestEntry is the direct content of the |directory|. |
| 43 bool IsDirectChildOf(const FilePath& directory) const { | 44 bool IsDirectChildOf(const FilePath& directory) const { |
| 44 return FilePath(path).DirName() == directory; | 45 return FilePath(path).DirName() == directory; |
| 45 } | 46 } |
| 46 | 47 |
| 47 // Converts this TestEntry to DriveEntryProto, which is the real data | 48 // Converts this TestEntry to DriveEntryProto, which is the real data |
| 48 // structure used in DriveFileSystem. | 49 // structure used in DriveFileSystem. |
| 49 DriveEntryProto ToDriveEntryProto() const { | 50 DriveEntryProto ToDriveEntryProto() const { |
| 50 DriveEntryProto entry; | 51 DriveEntryProto entry; |
| 51 entry.set_base_name(FilePath(path).BaseName().value()); | 52 entry.set_base_name(FilePath(path).BaseName().value()); |
| 52 entry.mutable_file_info()->set_is_directory(entry_type == TYPE_DIRECTORY); | 53 entry.mutable_file_info()->set_is_directory(entry_type == TYPE_DIRECTORY); |
| 53 if (entry_type != TYPE_DIRECTORY) { | 54 if (entry_type != TYPE_DIRECTORY) { |
| 54 entry.mutable_file_specific_info()->set_is_hosted_document( | 55 entry.mutable_file_specific_info()->set_is_hosted_document( |
| 55 entry_type == TYPE_HOSTED_FILE); | 56 entry_type == TYPE_HOSTED_FILE); |
| 56 entry.mutable_file_info()->set_size(file_size); | 57 entry.mutable_file_info()->set_size(file_size); |
| 57 } | 58 } |
| 58 entry.mutable_file_info()->set_last_accessed(last_access); | 59 entry.mutable_file_info()->set_last_accessed(last_accessed); |
| 60 entry.mutable_file_info()->set_last_modified(last_modified); |
| 59 entry.set_resource_id(resource_id); | 61 entry.set_resource_id(resource_id); |
| 60 return entry; | 62 return entry; |
| 61 } | 63 } |
| 62 }; | 64 }; |
| 63 | 65 |
| 64 // Mocks DriveFileSystem::GetFileByResourceId. It records the requested | 66 // Mocks DriveFileSystem::GetFileByResourceId. It records the requested |
| 65 // resource_id (arg0) to |fetched_list|, and calls back a successful completion. | 67 // resource_id (arg0) to |fetched_list|, and calls back a successful completion. |
| 66 ACTION_P(MockGetFile, fetched_list) { | 68 ACTION_P(MockGetFile, fetched_list) { |
| 67 fetched_list->push_back(arg0); | 69 fetched_list->push_back(arg0); |
| 68 arg1.Run(DRIVE_FILE_OK, FilePath(), std::string(), REGULAR_FILE); | 70 arg1.Run(DRIVE_FILE_OK, FilePath(), std::string(), REGULAR_FILE); |
| 69 } | 71 } |
| 70 | 72 |
| 71 // Mocks DriveFileSystem::ReadDirectory. It holds the flat list of all entries | 73 // Mocks DriveFileSystem::ReadDirectory. It holds the flat list of all entries |
| 72 // in the mock filesystem in |test_entries|, and when it is called to read a | 74 // in the mock filesystem in |test_entries|, and when it is called to read a |
| 73 // |directory|, it selects only the direct children of the directory. | 75 // |directory|, it selects only the direct children of the directory. |
| 74 ACTION_P(MockReadDirectory, test_entries) { | 76 ACTION_P(MockReadDirectory, test_entries) { |
| 75 const FilePath& directory = arg0; | 77 const FilePath& directory = arg0; |
| 76 const ReadDirectoryWithSettingCallback& callback = arg1; | 78 const ReadDirectoryWithSettingCallback& callback = arg1; |
| 77 | 79 |
| 78 scoped_ptr<DriveEntryProtoVector> entries(new DriveEntryProtoVector); | 80 scoped_ptr<DriveEntryProtoVector> entries(new DriveEntryProtoVector); |
| 79 for (size_t i = 0; i < test_entries.size(); ++i) { | 81 for (size_t i = 0; i < test_entries.size(); ++i) { |
| 80 if (test_entries[i].IsDirectChildOf(directory)) | 82 if (test_entries[i].IsDirectChildOf(directory)) |
| 81 entries->push_back(test_entries[i].ToDriveEntryProto()); | 83 entries->push_back(test_entries[i].ToDriveEntryProto()); |
| 82 } | 84 } |
| 83 callback.Run(DRIVE_FILE_OK, false /* hide_hosted_document */, entries.Pass()); | 85 callback.Run(DRIVE_FILE_OK, false /* hide_hosted_document */, entries.Pass()); |
| 84 } | 86 } |
| 85 | 87 |
| 86 const TestEntry kEmptyDrive[] = { | 88 const TestEntry kEmptyDrive[] = { |
| 87 { FILE_PATH_LITERAL("drive"), TYPE_DIRECTORY, 0, "id:drive" }, | 89 { FILE_PATH_LITERAL("drive"), TYPE_DIRECTORY, 0, 0, "id:drive" }, |
| 88 }; | 90 }; |
| 89 | 91 |
| 90 const TestEntry kOneFileDrive[] = { | 92 const TestEntry kOneFileDrive[] = { |
| 91 { FILE_PATH_LITERAL("drive"), TYPE_DIRECTORY, 0, "id:drive" }, | 93 { FILE_PATH_LITERAL("drive"), TYPE_DIRECTORY, 0, 0, "id:drive" }, |
| 92 { FILE_PATH_LITERAL("drive/abc.txt"), TYPE_REGULAR_FILE, 1, "id:abc" }, | 94 { FILE_PATH_LITERAL("drive/abc.txt"), TYPE_REGULAR_FILE, 1, 0, "id:abc" }, |
| 93 }; | 95 }; |
| 94 | 96 |
| 95 const char* kExpectedOneFile[] = { "id:abc" }; | 97 const char* kExpectedOneFile[] = { "id:abc" }; |
| 96 | 98 |
| 97 const TestEntry kComplexDrive[] = { | 99 const TestEntry kComplexDrive[] = { |
| 98 { FILE_PATH_LITERAL("drive"), TYPE_DIRECTORY, 0, "id:root" }, | 100 // Path Type Access Modify ID |
| 99 { FILE_PATH_LITERAL("drive/a"), TYPE_DIRECTORY, 0, "id:a" }, | 101 { FILE_PATH_LITERAL("drive"), TYPE_DIRECTORY, 0, 0, "id:root" }, |
| 100 { FILE_PATH_LITERAL("drive/a/foo.txt"), TYPE_REGULAR_FILE, 3, "id:foo1" }, | 102 { FILE_PATH_LITERAL("drive/a"), TYPE_DIRECTORY, 0, 0, "id:a" }, |
| 101 { FILE_PATH_LITERAL("drive/a/b"), TYPE_DIRECTORY, 8, "id:b" }, | 103 { FILE_PATH_LITERAL("drive/a/foo.txt"), TYPE_REGULAR_FILE, 3, 2, "id:foo1" }, |
| 102 { FILE_PATH_LITERAL("drive/a/b/bar.jpg"), TYPE_REGULAR_FILE, 5, "id:bar1", | 104 { FILE_PATH_LITERAL("drive/a/b"), TYPE_DIRECTORY, 8, 0, "id:b" }, |
| 103 999 }, | 105 { FILE_PATH_LITERAL("drive/a/bar.jpg"), TYPE_REGULAR_FILE, 5, 0, "id:bar1", |
| 104 { FILE_PATH_LITERAL("drive/a/b/new.gdoc"), TYPE_HOSTED_FILE, 7, "id:new" }, | 106 999 }, |
| 105 { FILE_PATH_LITERAL("drive/a/buz.zip"), TYPE_REGULAR_FILE, 4, "id:buz1" }, | 107 { FILE_PATH_LITERAL("drive/a/b/x.gdoc"), TYPE_HOSTED_FILE, 7, 0, "id:new" }, |
| 106 { FILE_PATH_LITERAL("drive/a/old.gdoc"), TYPE_HOSTED_FILE, 1, "id:old" }, | 108 { FILE_PATH_LITERAL("drive/a/buz.zip"), TYPE_REGULAR_FILE, 4, 0, "id:buz1" }, |
| 107 { FILE_PATH_LITERAL("drive/c"), TYPE_DIRECTORY, 0, "id:c" }, | 109 { FILE_PATH_LITERAL("drive/a/old.gdoc"), TYPE_HOSTED_FILE, 1, 0, "id:old" }, |
| 108 { FILE_PATH_LITERAL("drive/c/foo.txt"), TYPE_REGULAR_FILE, 2, "id:foo2" }, | 110 { FILE_PATH_LITERAL("drive/c"), TYPE_DIRECTORY, 0, 0, "id:c" }, |
| 109 { FILE_PATH_LITERAL("drive/c/buz.zip"), TYPE_REGULAR_FILE, 1, "id:buz2" }, | 111 { FILE_PATH_LITERAL("drive/c/foo.txt"), TYPE_REGULAR_FILE, 3, 1, "id:foo2" }, |
| 110 { FILE_PATH_LITERAL("drive/bar.jpg"), TYPE_REGULAR_FILE, 6, "id:bar2" }, | 112 { FILE_PATH_LITERAL("drive/c/buz.zip"), TYPE_REGULAR_FILE, 1, 0, "id:buz2" }, |
| 113 { FILE_PATH_LITERAL("drive/bar.jpg"), TYPE_REGULAR_FILE, 6, 0, "id:bar2" }, |
| 111 }; | 114 }; |
| 112 | 115 |
| 113 const char* kTop3Files[] = { | 116 const char* kTop3Files[] = { |
| 114 "id:bar2", // The file with the largest timestamp | 117 "id:bar2", // The file with the largest timestamp |
| 115 // "bar1" is the second latest, but its file size is over limit. | 118 // "bar1" is the second latest, but its file size is over limit. |
| 116 "id:buz1", // The third latest file. | 119 "id:buz1", // The third latest file. |
| 117 "id:foo1" // 4th. | 120 "id:foo1" // 4th. Has same access time with id:foo2, so the one with the |
| 121 // newer modified time wins. |
| 118 }; | 122 }; |
| 119 | 123 |
| 120 const char* kAllRegularFiles[] = { | 124 const char* kAllRegularFiles[] = { |
| 121 "id:bar2", "id:bar1", "id:buz1", "id:foo1", "id:foo2", "id:buz2", | 125 "id:bar2", "id:bar1", "id:buz1", "id:foo1", "id:foo2", "id:buz2", |
| 122 }; | 126 }; |
| 123 | 127 |
| 124 } // namespace | 128 } // namespace |
| 125 | 129 |
| 126 class DrivePrefetcherTest : public testing::Test { | 130 class DrivePrefetcherTest : public testing::Test { |
| 127 public: | 131 public: |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // This is checked by setting the fetch limit larger than the number of files. | 216 // This is checked by setting the fetch limit larger than the number of files. |
| 213 InitPrefetcher(100, 99999999); | 217 InitPrefetcher(100, 99999999); |
| 214 VerifyFullScan( | 218 VerifyFullScan( |
| 215 std::vector<TestEntry>(kComplexDrive, | 219 std::vector<TestEntry>(kComplexDrive, |
| 216 kComplexDrive + arraysize(kComplexDrive)), | 220 kComplexDrive + arraysize(kComplexDrive)), |
| 217 std::vector<std::string>(kAllRegularFiles, | 221 std::vector<std::string>(kAllRegularFiles, |
| 218 kAllRegularFiles + arraysize(kAllRegularFiles))); | 222 kAllRegularFiles + arraysize(kAllRegularFiles))); |
| 219 } | 223 } |
| 220 | 224 |
| 221 } // namespace drive | 225 } // namespace drive |
| OLD | NEW |