Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string.h> | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h" | |
| 15 #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" | |
| 16 #include "components/arc/arc_bridge_service.h" | |
| 17 #include "components/arc/arc_service_manager.h" | |
| 18 #include "components/arc/test/fake_file_system_instance.h" | |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | |
| 20 #include "storage/common/fileapi/directory_entry.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using storage::DirectoryEntry; | |
| 24 using EntryList = storage::AsyncFileUtil::EntryList; | |
| 25 | |
| 26 namespace arc { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 struct DocumentSpec { | |
| 31 const char* document_id; | |
| 32 const char* display_name; | |
| 33 const char* mime_type; | |
| 34 int64_t size; | |
| 35 uint64_t last_modified; | |
| 36 }; | |
| 37 | |
| 38 // Fake file system hierarchy: | |
| 39 // | |
| 40 // <path> <type> <ID> | |
| 41 // (root)/ dir root-id | |
| 42 // dir/ dir dir-id | |
| 43 // photo.jpg image/jpeg photo-id | |
| 44 // music.bin audio/mp3 music-id | |
| 45 // dups/ dir dups-id | |
| 46 // dup.mp4 video/mp4 dup1-id | |
| 47 // dup.mp4 video/mp4 dup2-id | |
| 48 // dup.mp4 video/mp4 dup3-id | |
| 49 // dup.mp4 video/mp4 dup4-id | |
| 50 constexpr char kAuthority[] = "org.chromium.test"; | |
| 51 constexpr DocumentSpec kRootSpec = {"root-id", "(root)", | |
| 52 kAndroidDirectoryMimeType, 0, 11}; | |
|
hashimoto
2016/12/20 04:55:07
The Android side code returns -1 if the size is un
Shuhei Takahashi
2016/12/20 06:08:24
Done.
| |
| 53 constexpr DocumentSpec kDirSpec = {"dir-id", "dir", kAndroidDirectoryMimeType, | |
| 54 0, 22}; | |
| 55 constexpr DocumentSpec kPhotoSpec = {"photo-id", "photo.jpg", "image/jpeg", 3, | |
| 56 33}; | |
| 57 constexpr DocumentSpec kMusicSpec = {"music-id", "music.bin", "audio/mp3", 4, | |
| 58 44}; | |
| 59 constexpr DocumentSpec kDupsSpec = {"dups-id", "dups", | |
| 60 kAndroidDirectoryMimeType, 0, 55}; | |
| 61 constexpr DocumentSpec kDup1Spec = {"dup1-id", "dup.mp4", "video/mp4", 6, 66}; | |
| 62 constexpr DocumentSpec kDup2Spec = {"dup2-id", "dup.mp4", "video/mp4", 7, 77}; | |
| 63 constexpr DocumentSpec kDup3Spec = {"dup3-id", "dup.mp4", "video/mp4", 8, 88}; | |
| 64 constexpr DocumentSpec kDup4Spec = {"dup4-id", "dup.mp4", "video/mp4", 9, 99}; | |
| 65 | |
| 66 mojom::DocumentPtr MakeDocument(const DocumentSpec& spec) { | |
| 67 mojom::DocumentPtr document = mojom::Document::New(); | |
| 68 document->document_id = spec.document_id; | |
| 69 document->display_name = spec.display_name; | |
| 70 document->mime_type = spec.mime_type; | |
| 71 document->size = spec.size; | |
| 72 document->last_modified = spec.last_modified; | |
| 73 return document; | |
| 74 } | |
| 75 | |
| 76 class FakeFileSystemInstanceImpl : public FakeFileSystemInstance { | |
|
hashimoto
2016/12/20 04:55:07
"FakeFileSystemInstanceImpl" sounds weird, as "Fak
Shuhei Takahashi
2016/12/20 06:08:24
Renamed to FileSystemInstanceTestImpl. Does it sou
hashimoto
2016/12/20 06:24:18
sg
| |
| 77 public: | |
| 78 void GetChildDocuments(const std::string& authority, | |
| 79 const std::string& document_id, | |
| 80 const GetChildDocumentsCallback& callback) override { | |
| 81 EXPECT_EQ(kAuthority, authority); | |
| 82 base::Optional<std::vector<mojom::DocumentPtr>> result; | |
| 83 if (document_id == kRootSpec.document_id) { | |
| 84 result.emplace(); | |
| 85 result.value().emplace_back(MakeDocument(kDirSpec)); | |
| 86 result.value().emplace_back(MakeDocument(kDupsSpec)); | |
| 87 } else if (document_id == kDirSpec.document_id) { | |
| 88 result.emplace(); | |
| 89 result.value().emplace_back(MakeDocument(kPhotoSpec)); | |
| 90 result.value().emplace_back(MakeDocument(kMusicSpec)); | |
| 91 } else if (document_id == kDupsSpec.document_id) { | |
| 92 result.emplace(); | |
| 93 // The order is intentionally shuffled. | |
| 94 result.value().emplace_back(MakeDocument(kDup2Spec)); | |
| 95 result.value().emplace_back(MakeDocument(kDup1Spec)); | |
| 96 result.value().emplace_back(MakeDocument(kDup4Spec)); | |
| 97 result.value().emplace_back(MakeDocument(kDup3Spec)); | |
| 98 } | |
| 99 callback.Run(std::move(result)); | |
| 100 } | |
| 101 | |
| 102 void GetDocument(const std::string& authority, | |
| 103 const std::string& document_id, | |
| 104 const GetDocumentCallback& callback) override { | |
| 105 EXPECT_EQ(kAuthority, authority); | |
| 106 mojom::DocumentPtr result; | |
| 107 if (document_id == kRootSpec.document_id) { | |
| 108 result = MakeDocument(kRootSpec); | |
| 109 } else if (document_id == kDirSpec.document_id) { | |
| 110 result = MakeDocument(kDirSpec); | |
| 111 } else if (document_id == kPhotoSpec.document_id) { | |
| 112 result = MakeDocument(kPhotoSpec); | |
| 113 } else if (document_id == kMusicSpec.document_id) { | |
| 114 result = MakeDocument(kMusicSpec); | |
| 115 } else if (document_id == kDupsSpec.document_id) { | |
| 116 result = MakeDocument(kDupsSpec); | |
| 117 } else if (document_id == kDup1Spec.document_id) { | |
| 118 result = MakeDocument(kDup1Spec); | |
| 119 } else if (document_id == kDup2Spec.document_id) { | |
| 120 result = MakeDocument(kDup2Spec); | |
| 121 } else if (document_id == kDup3Spec.document_id) { | |
| 122 result = MakeDocument(kDup3Spec); | |
| 123 } else if (document_id == kDup4Spec.document_id) { | |
| 124 result = MakeDocument(kDup4Spec); | |
| 125 } | |
| 126 callback.Run(std::move(result)); | |
| 127 } | |
| 128 }; | |
| 129 | |
| 130 void ExpectMatchesSpec(const base::File::Info& info, const DocumentSpec& spec) { | |
| 131 EXPECT_EQ(spec.size, info.size); | |
| 132 EXPECT_EQ(strcmp(spec.mime_type, kAndroidDirectoryMimeType) == 0, | |
|
hashimoto
2016/12/20 04:55:07
How about something like this to make it more read
Shuhei Takahashi
2016/12/20 06:08:24
Done.
| |
| 133 info.is_directory); | |
| 134 EXPECT_EQ(false, info.is_symbolic_link); | |
|
hashimoto
2016/12/20 04:55:07
EXPECT_FALSE
Shuhei Takahashi
2016/12/20 06:08:24
Done.
| |
| 135 EXPECT_EQ(spec.last_modified, | |
| 136 static_cast<uint64_t>(info.last_modified.ToJavaTime())); | |
| 137 EXPECT_EQ(spec.last_modified, | |
| 138 static_cast<uint64_t>(info.last_accessed.ToJavaTime())); | |
| 139 EXPECT_EQ(spec.last_modified, | |
| 140 static_cast<uint64_t>(info.creation_time.ToJavaTime())); | |
| 141 } | |
| 142 | |
| 143 class ArcDocumentsProviderRootTest : public testing::Test { | |
| 144 public: | |
| 145 ArcDocumentsProviderRootTest() { | |
| 146 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); | |
|
hashimoto
2016/12/20 04:55:07
Can't this be done in the initializer list?
Shuhei Takahashi
2016/12/20 06:08:24
Done.
| |
| 147 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( | |
| 148 &file_system_); | |
| 149 root_ = base::MakeUnique<ArcDocumentsProviderRoot>(kAuthority, | |
|
hashimoto
2016/12/20 04:55:07
ditto.
Shuhei Takahashi
2016/12/20 06:08:24
Done.
| |
| 150 kRootSpec.document_id); | |
| 151 } | |
| 152 | |
| 153 ~ArcDocumentsProviderRootTest() override = default; | |
| 154 | |
| 155 protected: | |
| 156 content::TestBrowserThreadBundle thread_bundle_; | |
| 157 FakeFileSystemInstanceImpl file_system_; | |
| 158 std::unique_ptr<ArcServiceManager> arc_service_manager_; | |
| 159 std::unique_ptr<ArcDocumentsProviderRoot> root_; | |
| 160 | |
| 161 private: | |
| 162 DISALLOW_COPY_AND_ASSIGN(ArcDocumentsProviderRootTest); | |
| 163 }; | |
| 164 | |
| 165 } // namespace | |
| 166 | |
| 167 TEST_F(ArcDocumentsProviderRootTest, GetFileInfo) { | |
| 168 base::RunLoop run_loop; | |
| 169 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir/photo.jpg")), | |
| 170 base::Bind( | |
| 171 [](base::RunLoop* run_loop, base::File::Error error, | |
| 172 const base::File::Info& info) { | |
| 173 EXPECT_EQ(base::File::FILE_OK, error); | |
| 174 ExpectMatchesSpec(info, kPhotoSpec); | |
| 175 run_loop->Quit(); | |
| 176 }, | |
| 177 &run_loop)); | |
| 178 run_loop.Run(); | |
| 179 } | |
| 180 | |
| 181 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDirectory) { | |
| 182 base::RunLoop run_loop; | |
| 183 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir")), | |
| 184 base::Bind( | |
| 185 [](base::RunLoop* run_loop, base::File::Error error, | |
| 186 const base::File::Info& info) { | |
| 187 EXPECT_EQ(base::File::FILE_OK, error); | |
| 188 ExpectMatchesSpec(info, kDirSpec); | |
| 189 run_loop->Quit(); | |
| 190 }, | |
| 191 &run_loop)); | |
| 192 run_loop.Run(); | |
| 193 } | |
| 194 | |
| 195 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoRoot) { | |
| 196 base::RunLoop run_loop; | |
| 197 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("")), | |
| 198 base::Bind( | |
| 199 [](base::RunLoop* run_loop, base::File::Error error, | |
| 200 const base::File::Info& info) { | |
| 201 EXPECT_EQ(base::File::FILE_OK, error); | |
| 202 ExpectMatchesSpec(info, kRootSpec); | |
| 203 run_loop->Quit(); | |
| 204 }, | |
| 205 &run_loop)); | |
| 206 run_loop.Run(); | |
| 207 } | |
| 208 | |
| 209 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoNoSuchFile) { | |
| 210 base::RunLoop run_loop; | |
| 211 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir/missing.jpg")), | |
| 212 base::Bind( | |
| 213 [](base::RunLoop* run_loop, base::File::Error error, | |
| 214 const base::File::Info& info) { | |
| 215 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); | |
| 216 run_loop->Quit(); | |
| 217 }, | |
| 218 &run_loop)); | |
| 219 run_loop.Run(); | |
| 220 } | |
| 221 | |
| 222 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDups) { | |
| 223 base::RunLoop run_loop; | |
| 224 // "dup 2.mp4" should map to the 3rd instance of "dup.mp4" regardless of the | |
| 225 // order returned from FileSystemInstance. | |
| 226 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dups/dup 2.mp4")), | |
| 227 base::Bind( | |
| 228 [](base::RunLoop* run_loop, base::File::Error error, | |
| 229 const base::File::Info& info) { | |
| 230 EXPECT_EQ(base::File::FILE_OK, error); | |
| 231 ExpectMatchesSpec(info, kDup3Spec); | |
| 232 run_loop->Quit(); | |
| 233 }, | |
| 234 &run_loop)); | |
| 235 run_loop.Run(); | |
| 236 } | |
| 237 | |
| 238 TEST_F(ArcDocumentsProviderRootTest, ReadDirectory) { | |
| 239 base::RunLoop run_loop; | |
| 240 root_->ReadDirectory( | |
| 241 base::FilePath(FILE_PATH_LITERAL("dir")), | |
| 242 base::Bind( | |
| 243 [](base::RunLoop* run_loop, base::File::Error error, | |
| 244 const EntryList& file_list, bool has_more) { | |
| 245 EXPECT_EQ(base::File::FILE_OK, error); | |
| 246 ASSERT_EQ(2u, file_list.size()); | |
| 247 EXPECT_EQ(FILE_PATH_LITERAL("music.bin.mp3"), file_list[0].name); | |
| 248 EXPECT_FALSE(file_list[0].is_directory); | |
| 249 EXPECT_EQ(FILE_PATH_LITERAL("photo.jpg"), file_list[1].name); | |
| 250 EXPECT_FALSE(file_list[1].is_directory); | |
| 251 EXPECT_FALSE(has_more); | |
| 252 run_loop->Quit(); | |
| 253 }, | |
| 254 &run_loop)); | |
| 255 run_loop.Run(); | |
| 256 } | |
| 257 | |
| 258 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryRoot) { | |
| 259 base::RunLoop run_loop; | |
| 260 root_->ReadDirectory( | |
| 261 base::FilePath(FILE_PATH_LITERAL("")), | |
| 262 base::Bind( | |
| 263 [](base::RunLoop* run_loop, base::File::Error error, | |
| 264 const EntryList& file_list, bool has_more) { | |
| 265 EXPECT_EQ(base::File::FILE_OK, error); | |
| 266 ASSERT_EQ(2u, file_list.size()); | |
| 267 EXPECT_EQ(FILE_PATH_LITERAL("dir"), file_list[0].name); | |
| 268 EXPECT_TRUE(file_list[0].is_directory); | |
| 269 EXPECT_EQ(FILE_PATH_LITERAL("dups"), file_list[1].name); | |
| 270 EXPECT_TRUE(file_list[1].is_directory); | |
| 271 EXPECT_FALSE(has_more); | |
| 272 run_loop->Quit(); | |
| 273 }, | |
| 274 &run_loop)); | |
| 275 run_loop.Run(); | |
| 276 } | |
| 277 | |
| 278 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryNoSuchDirectory) { | |
| 279 base::RunLoop run_loop; | |
| 280 root_->ReadDirectory(base::FilePath(FILE_PATH_LITERAL("missing")), | |
| 281 base::Bind( | |
| 282 [](base::RunLoop* run_loop, base::File::Error error, | |
| 283 const EntryList& file_list, bool has_more) { | |
| 284 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); | |
| 285 EXPECT_EQ(0u, file_list.size()); | |
| 286 EXPECT_FALSE(has_more); | |
| 287 run_loop->Quit(); | |
| 288 }, | |
| 289 &run_loop)); | |
| 290 run_loop.Run(); | |
| 291 } | |
| 292 | |
| 293 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryDups) { | |
| 294 base::RunLoop run_loop; | |
| 295 root_->ReadDirectory( | |
| 296 base::FilePath(FILE_PATH_LITERAL("dups")), | |
| 297 base::Bind( | |
| 298 [](base::RunLoop* run_loop, base::File::Error error, | |
| 299 const EntryList& file_list, bool has_more) { | |
| 300 EXPECT_EQ(base::File::FILE_OK, error); | |
| 301 ASSERT_EQ(4u, file_list.size()); | |
| 302 // FiIles are sorted lexicographically. | |
| 303 EXPECT_EQ(FILE_PATH_LITERAL("dup 1.mp4"), file_list[0].name); | |
| 304 EXPECT_FALSE(file_list[0].is_directory); | |
| 305 EXPECT_EQ(FILE_PATH_LITERAL("dup 2.mp4"), file_list[1].name); | |
| 306 EXPECT_FALSE(file_list[1].is_directory); | |
| 307 EXPECT_EQ(FILE_PATH_LITERAL("dup 3.mp4"), file_list[2].name); | |
| 308 EXPECT_FALSE(file_list[2].is_directory); | |
| 309 EXPECT_EQ(FILE_PATH_LITERAL("dup.mp4"), file_list[3].name); | |
| 310 EXPECT_FALSE(file_list[3].is_directory); | |
| 311 EXPECT_FALSE(has_more); | |
| 312 run_loop->Quit(); | |
| 313 }, | |
| 314 &run_loop)); | |
| 315 run_loop.Run(); | |
| 316 } | |
| 317 | |
| 318 } // namespace arc | |
| OLD | NEW |