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, -1, 11}; |
| 53 constexpr DocumentSpec kDirSpec = {"dir-id", "dir", kAndroidDirectoryMimeType, |
| 54 -1, 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, -1, 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 constexpr DocumentSpec kAllSpecs[] = {kRootSpec, kDirSpec, kPhotoSpec, |
| 66 kMusicSpec, kDupsSpec, kDup1Spec, |
| 67 kDup2Spec, kDup3Spec, kDup4Spec}; |
| 68 |
| 69 mojom::DocumentPtr MakeDocument(const DocumentSpec& spec) { |
| 70 mojom::DocumentPtr document = mojom::Document::New(); |
| 71 document->document_id = spec.document_id; |
| 72 document->display_name = spec.display_name; |
| 73 document->mime_type = spec.mime_type; |
| 74 document->size = spec.size; |
| 75 document->last_modified = spec.last_modified; |
| 76 return document; |
| 77 } |
| 78 |
| 79 class FileSystemInstanceTestImpl : public FakeFileSystemInstance { |
| 80 public: |
| 81 void GetChildDocuments(const std::string& authority, |
| 82 const std::string& document_id, |
| 83 const GetChildDocumentsCallback& callback) override { |
| 84 EXPECT_EQ(kAuthority, authority); |
| 85 base::Optional<std::vector<mojom::DocumentPtr>> result; |
| 86 if (document_id == kRootSpec.document_id) { |
| 87 result.emplace(); |
| 88 result.value().emplace_back(MakeDocument(kDirSpec)); |
| 89 result.value().emplace_back(MakeDocument(kDupsSpec)); |
| 90 } else if (document_id == kDirSpec.document_id) { |
| 91 result.emplace(); |
| 92 result.value().emplace_back(MakeDocument(kPhotoSpec)); |
| 93 result.value().emplace_back(MakeDocument(kMusicSpec)); |
| 94 } else if (document_id == kDupsSpec.document_id) { |
| 95 result.emplace(); |
| 96 // The order is intentionally shuffled. |
| 97 result.value().emplace_back(MakeDocument(kDup2Spec)); |
| 98 result.value().emplace_back(MakeDocument(kDup1Spec)); |
| 99 result.value().emplace_back(MakeDocument(kDup4Spec)); |
| 100 result.value().emplace_back(MakeDocument(kDup3Spec)); |
| 101 } |
| 102 callback.Run(std::move(result)); |
| 103 } |
| 104 |
| 105 void GetDocument(const std::string& authority, |
| 106 const std::string& document_id, |
| 107 const GetDocumentCallback& callback) override { |
| 108 EXPECT_EQ(kAuthority, authority); |
| 109 mojom::DocumentPtr result; |
| 110 for (const auto& spec : kAllSpecs) { |
| 111 if (document_id == spec.document_id) { |
| 112 result = MakeDocument(spec); |
| 113 break; |
| 114 } |
| 115 } |
| 116 callback.Run(std::move(result)); |
| 117 } |
| 118 }; |
| 119 |
| 120 void ExpectMatchesSpec(const base::File::Info& info, const DocumentSpec& spec) { |
| 121 EXPECT_EQ(spec.size, info.size); |
| 122 if (strcmp(spec.mime_type, kAndroidDirectoryMimeType) == 0) { |
| 123 EXPECT_TRUE(info.is_directory); |
| 124 } else { |
| 125 EXPECT_FALSE(info.is_directory); |
| 126 } |
| 127 EXPECT_FALSE(info.is_symbolic_link); |
| 128 EXPECT_EQ(spec.last_modified, |
| 129 static_cast<uint64_t>(info.last_modified.ToJavaTime())); |
| 130 EXPECT_EQ(spec.last_modified, |
| 131 static_cast<uint64_t>(info.last_accessed.ToJavaTime())); |
| 132 EXPECT_EQ(spec.last_modified, |
| 133 static_cast<uint64_t>(info.creation_time.ToJavaTime())); |
| 134 } |
| 135 |
| 136 class ArcDocumentsProviderRootTest : public testing::Test { |
| 137 public: |
| 138 ArcDocumentsProviderRootTest() |
| 139 : arc_service_manager_(base::MakeUnique<ArcServiceManager>(nullptr)), |
| 140 root_( |
| 141 base::MakeUnique<ArcDocumentsProviderRoot>(kAuthority, |
| 142 kRootSpec.document_id)) { |
| 143 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| 144 &file_system_); |
| 145 } |
| 146 |
| 147 ~ArcDocumentsProviderRootTest() override = default; |
| 148 |
| 149 protected: |
| 150 content::TestBrowserThreadBundle thread_bundle_; |
| 151 FileSystemInstanceTestImpl file_system_; |
| 152 std::unique_ptr<ArcServiceManager> arc_service_manager_; |
| 153 std::unique_ptr<ArcDocumentsProviderRoot> root_; |
| 154 |
| 155 private: |
| 156 DISALLOW_COPY_AND_ASSIGN(ArcDocumentsProviderRootTest); |
| 157 }; |
| 158 |
| 159 } // namespace |
| 160 |
| 161 TEST_F(ArcDocumentsProviderRootTest, GetFileInfo) { |
| 162 base::RunLoop run_loop; |
| 163 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir/photo.jpg")), |
| 164 base::Bind( |
| 165 [](base::RunLoop* run_loop, base::File::Error error, |
| 166 const base::File::Info& info) { |
| 167 EXPECT_EQ(base::File::FILE_OK, error); |
| 168 ExpectMatchesSpec(info, kPhotoSpec); |
| 169 run_loop->Quit(); |
| 170 }, |
| 171 &run_loop)); |
| 172 run_loop.Run(); |
| 173 } |
| 174 |
| 175 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDirectory) { |
| 176 base::RunLoop run_loop; |
| 177 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir")), |
| 178 base::Bind( |
| 179 [](base::RunLoop* run_loop, base::File::Error error, |
| 180 const base::File::Info& info) { |
| 181 EXPECT_EQ(base::File::FILE_OK, error); |
| 182 ExpectMatchesSpec(info, kDirSpec); |
| 183 run_loop->Quit(); |
| 184 }, |
| 185 &run_loop)); |
| 186 run_loop.Run(); |
| 187 } |
| 188 |
| 189 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoRoot) { |
| 190 base::RunLoop run_loop; |
| 191 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("")), |
| 192 base::Bind( |
| 193 [](base::RunLoop* run_loop, base::File::Error error, |
| 194 const base::File::Info& info) { |
| 195 EXPECT_EQ(base::File::FILE_OK, error); |
| 196 ExpectMatchesSpec(info, kRootSpec); |
| 197 run_loop->Quit(); |
| 198 }, |
| 199 &run_loop)); |
| 200 run_loop.Run(); |
| 201 } |
| 202 |
| 203 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoNoSuchFile) { |
| 204 base::RunLoop run_loop; |
| 205 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir/missing.jpg")), |
| 206 base::Bind( |
| 207 [](base::RunLoop* run_loop, base::File::Error error, |
| 208 const base::File::Info& info) { |
| 209 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); |
| 210 run_loop->Quit(); |
| 211 }, |
| 212 &run_loop)); |
| 213 run_loop.Run(); |
| 214 } |
| 215 |
| 216 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDups) { |
| 217 base::RunLoop run_loop; |
| 218 // "dup (2).mp4" should map to the 3rd instance of "dup.mp4" regardless of the |
| 219 // order returned from FileSystemInstance. |
| 220 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dups/dup (2).mp4")), |
| 221 base::Bind( |
| 222 [](base::RunLoop* run_loop, base::File::Error error, |
| 223 const base::File::Info& info) { |
| 224 EXPECT_EQ(base::File::FILE_OK, error); |
| 225 ExpectMatchesSpec(info, kDup3Spec); |
| 226 run_loop->Quit(); |
| 227 }, |
| 228 &run_loop)); |
| 229 run_loop.Run(); |
| 230 } |
| 231 |
| 232 TEST_F(ArcDocumentsProviderRootTest, ReadDirectory) { |
| 233 base::RunLoop run_loop; |
| 234 root_->ReadDirectory( |
| 235 base::FilePath(FILE_PATH_LITERAL("dir")), |
| 236 base::Bind( |
| 237 [](base::RunLoop* run_loop, base::File::Error error, |
| 238 const EntryList& file_list, bool has_more) { |
| 239 EXPECT_EQ(base::File::FILE_OK, error); |
| 240 ASSERT_EQ(2u, file_list.size()); |
| 241 EXPECT_EQ(FILE_PATH_LITERAL("music.bin.mp3"), file_list[0].name); |
| 242 EXPECT_FALSE(file_list[0].is_directory); |
| 243 EXPECT_EQ(FILE_PATH_LITERAL("photo.jpg"), file_list[1].name); |
| 244 EXPECT_FALSE(file_list[1].is_directory); |
| 245 EXPECT_FALSE(has_more); |
| 246 run_loop->Quit(); |
| 247 }, |
| 248 &run_loop)); |
| 249 run_loop.Run(); |
| 250 } |
| 251 |
| 252 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryRoot) { |
| 253 base::RunLoop run_loop; |
| 254 root_->ReadDirectory( |
| 255 base::FilePath(FILE_PATH_LITERAL("")), |
| 256 base::Bind( |
| 257 [](base::RunLoop* run_loop, base::File::Error error, |
| 258 const EntryList& file_list, bool has_more) { |
| 259 EXPECT_EQ(base::File::FILE_OK, error); |
| 260 ASSERT_EQ(2u, file_list.size()); |
| 261 EXPECT_EQ(FILE_PATH_LITERAL("dir"), file_list[0].name); |
| 262 EXPECT_TRUE(file_list[0].is_directory); |
| 263 EXPECT_EQ(FILE_PATH_LITERAL("dups"), file_list[1].name); |
| 264 EXPECT_TRUE(file_list[1].is_directory); |
| 265 EXPECT_FALSE(has_more); |
| 266 run_loop->Quit(); |
| 267 }, |
| 268 &run_loop)); |
| 269 run_loop.Run(); |
| 270 } |
| 271 |
| 272 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryNoSuchDirectory) { |
| 273 base::RunLoop run_loop; |
| 274 root_->ReadDirectory(base::FilePath(FILE_PATH_LITERAL("missing")), |
| 275 base::Bind( |
| 276 [](base::RunLoop* run_loop, base::File::Error error, |
| 277 const EntryList& file_list, bool has_more) { |
| 278 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); |
| 279 EXPECT_EQ(0u, file_list.size()); |
| 280 EXPECT_FALSE(has_more); |
| 281 run_loop->Quit(); |
| 282 }, |
| 283 &run_loop)); |
| 284 run_loop.Run(); |
| 285 } |
| 286 |
| 287 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryDups) { |
| 288 base::RunLoop run_loop; |
| 289 root_->ReadDirectory( |
| 290 base::FilePath(FILE_PATH_LITERAL("dups")), |
| 291 base::Bind( |
| 292 [](base::RunLoop* run_loop, base::File::Error error, |
| 293 const EntryList& file_list, bool has_more) { |
| 294 EXPECT_EQ(base::File::FILE_OK, error); |
| 295 ASSERT_EQ(4u, file_list.size()); |
| 296 // FiIles are sorted lexicographically. |
| 297 EXPECT_EQ(FILE_PATH_LITERAL("dup (1).mp4"), file_list[0].name); |
| 298 EXPECT_FALSE(file_list[0].is_directory); |
| 299 EXPECT_EQ(FILE_PATH_LITERAL("dup (2).mp4"), file_list[1].name); |
| 300 EXPECT_FALSE(file_list[1].is_directory); |
| 301 EXPECT_EQ(FILE_PATH_LITERAL("dup (3).mp4"), file_list[2].name); |
| 302 EXPECT_FALSE(file_list[2].is_directory); |
| 303 EXPECT_EQ(FILE_PATH_LITERAL("dup.mp4"), file_list[3].name); |
| 304 EXPECT_FALSE(file_list[3].is_directory); |
| 305 EXPECT_FALSE(has_more); |
| 306 run_loop->Quit(); |
| 307 }, |
| 308 &run_loop)); |
| 309 run_loop.Run(); |
| 310 } |
| 311 |
| 312 } // namespace arc |
OLD | NEW |