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 constexpr char kAuthority[] = "org.chromium.test"; |
| 39 constexpr DocumentSpec kRootDocumentSpec = {"root-id", "<root>", |
| 40 kAndroidDirectoryMimeType, -1, 0}; |
| 41 constexpr DocumentSpec kPetDocumentSpec = { |
| 42 "pet-id", "pet", kAndroidDirectoryMimeType, -1, 10000}; |
| 43 constexpr DocumentSpec kCatDocumentSpec = {"cat-id", "cat.png", "image/png", 28, |
| 44 20000}; |
| 45 constexpr DocumentSpec kDogDocumentSpec = {"dog-id", "dog.mp3", "audio/mp3", -1, |
| 46 30000}; |
| 47 |
| 48 static mojom::DocumentPtr MakeDocument(const DocumentSpec& spec) { |
| 49 mojom::DocumentPtr document = mojom::Document::New(); |
| 50 document->document_id = spec.document_id; |
| 51 document->display_name = spec.display_name; |
| 52 document->mime_type = spec.mime_type; |
| 53 document->size = spec.size; |
| 54 document->last_modified = spec.last_modified; |
| 55 return document; |
| 56 } |
| 57 |
| 58 class FakeFileSystemInstanceImpl : public FakeFileSystemInstance { |
| 59 public: |
| 60 void GetChildDocuments(const std::string& authority, |
| 61 const std::string& document_id, |
| 62 const GetChildDocumentsCallback& callback) override { |
| 63 EXPECT_EQ(kAuthority, authority); |
| 64 base::Optional<std::vector<mojom::DocumentPtr>> result; |
| 65 if (document_id == kRootDocumentSpec.document_id) { |
| 66 result.emplace(); |
| 67 result.value().emplace_back(MakeDocument(kPetDocumentSpec)); |
| 68 } else if (document_id == kPetDocumentSpec.document_id) { |
| 69 result.emplace(); |
| 70 result.value().emplace_back(MakeDocument(kCatDocumentSpec)); |
| 71 result.value().emplace_back(MakeDocument(kDogDocumentSpec)); |
| 72 } |
| 73 callback.Run(std::move(result)); |
| 74 } |
| 75 |
| 76 void GetDocument(const std::string& authority, |
| 77 const std::string& document_id, |
| 78 const GetDocumentCallback& callback) override { |
| 79 EXPECT_EQ(kAuthority, authority); |
| 80 mojom::DocumentPtr result; |
| 81 if (document_id == kRootDocumentSpec.document_id) { |
| 82 result = MakeDocument(kRootDocumentSpec); |
| 83 } else if (document_id == kPetDocumentSpec.document_id) { |
| 84 result = MakeDocument(kPetDocumentSpec); |
| 85 } else if (document_id == kCatDocumentSpec.document_id) { |
| 86 result = MakeDocument(kCatDocumentSpec); |
| 87 } else if (document_id == kDogDocumentSpec.document_id) { |
| 88 result = MakeDocument(kDogDocumentSpec); |
| 89 } |
| 90 callback.Run(std::move(result)); |
| 91 } |
| 92 }; |
| 93 |
| 94 void ExpectMatchesSpec(const base::File::Info& info, const DocumentSpec& spec) { |
| 95 EXPECT_EQ(info.size, spec.size); |
| 96 EXPECT_EQ(info.is_directory, |
| 97 strcmp(spec.mime_type, kAndroidDirectoryMimeType) == 0); |
| 98 EXPECT_EQ(info.is_symbolic_link, false); |
| 99 EXPECT_EQ(static_cast<uint64_t>(info.last_modified.ToJavaTime()), |
| 100 spec.last_modified); |
| 101 EXPECT_EQ(static_cast<uint64_t>(info.last_accessed.ToJavaTime()), |
| 102 spec.last_modified); |
| 103 EXPECT_EQ(static_cast<uint64_t>(info.creation_time.ToJavaTime()), |
| 104 spec.last_modified); |
| 105 } |
| 106 |
| 107 class ArcDocumentsProviderRootTest : public testing::Test { |
| 108 public: |
| 109 ArcDocumentsProviderRootTest() { |
| 110 arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); |
| 111 arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| 112 &file_system_); |
| 113 root_ = base::MakeUnique<ArcDocumentsProviderRoot>( |
| 114 kAuthority, kRootDocumentSpec.document_id); |
| 115 } |
| 116 |
| 117 ~ArcDocumentsProviderRootTest() override = default; |
| 118 |
| 119 protected: |
| 120 content::TestBrowserThreadBundle thread_bundle_; |
| 121 FakeFileSystemInstanceImpl file_system_; |
| 122 std::unique_ptr<ArcServiceManager> arc_service_manager_; |
| 123 std::unique_ptr<ArcDocumentsProviderRoot> root_; |
| 124 |
| 125 private: |
| 126 DISALLOW_COPY_AND_ASSIGN(ArcDocumentsProviderRootTest); |
| 127 }; |
| 128 |
| 129 } // namespace |
| 130 |
| 131 TEST_F(ArcDocumentsProviderRootTest, GetFileInfo) { |
| 132 base::RunLoop run_loop; |
| 133 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("pet/cat.png")), |
| 134 base::Bind( |
| 135 [](base::RunLoop* run_loop, base::File::Error error, |
| 136 const base::File::Info& info) { |
| 137 EXPECT_EQ(base::File::FILE_OK, error); |
| 138 ExpectMatchesSpec(info, kCatDocumentSpec); |
| 139 run_loop->Quit(); |
| 140 }, |
| 141 &run_loop)); |
| 142 run_loop.Run(); |
| 143 } |
| 144 |
| 145 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDirectory) { |
| 146 base::RunLoop run_loop; |
| 147 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("pet")), |
| 148 base::Bind( |
| 149 [](base::RunLoop* run_loop, base::File::Error error, |
| 150 const base::File::Info& info) { |
| 151 EXPECT_EQ(base::File::FILE_OK, error); |
| 152 ExpectMatchesSpec(info, kPetDocumentSpec); |
| 153 run_loop->Quit(); |
| 154 }, |
| 155 &run_loop)); |
| 156 run_loop.Run(); |
| 157 } |
| 158 |
| 159 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoRoot) { |
| 160 base::RunLoop run_loop; |
| 161 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("")), |
| 162 base::Bind( |
| 163 [](base::RunLoop* run_loop, base::File::Error error, |
| 164 const base::File::Info& info) { |
| 165 EXPECT_EQ(base::File::FILE_OK, error); |
| 166 ExpectMatchesSpec(info, kRootDocumentSpec); |
| 167 run_loop->Quit(); |
| 168 }, |
| 169 &run_loop)); |
| 170 run_loop.Run(); |
| 171 } |
| 172 |
| 173 TEST_F(ArcDocumentsProviderRootTest, GetFileInfoNoSuchFile) { |
| 174 base::RunLoop run_loop; |
| 175 root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("pet/fox.jpg")), |
| 176 base::Bind( |
| 177 [](base::RunLoop* run_loop, base::File::Error error, |
| 178 const base::File::Info& info) { |
| 179 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); |
| 180 run_loop->Quit(); |
| 181 }, |
| 182 &run_loop)); |
| 183 run_loop.Run(); |
| 184 } |
| 185 |
| 186 TEST_F(ArcDocumentsProviderRootTest, ReadDirectory) { |
| 187 base::RunLoop run_loop; |
| 188 root_->ReadDirectory( |
| 189 base::FilePath(FILE_PATH_LITERAL("pet")), |
| 190 base::Bind( |
| 191 [](base::RunLoop* run_loop, base::File::Error error, |
| 192 const EntryList& file_list, bool has_more) { |
| 193 EXPECT_EQ(base::File::FILE_OK, error); |
| 194 ASSERT_EQ(2u, file_list.size()); |
| 195 EXPECT_EQ(FILE_PATH_LITERAL("cat.png"), file_list[0].name); |
| 196 EXPECT_FALSE(file_list[0].is_directory); |
| 197 EXPECT_EQ(FILE_PATH_LITERAL("dog.mp3"), file_list[1].name); |
| 198 EXPECT_FALSE(file_list[1].is_directory); |
| 199 EXPECT_FALSE(has_more); |
| 200 run_loop->Quit(); |
| 201 }, |
| 202 &run_loop)); |
| 203 run_loop.Run(); |
| 204 } |
| 205 |
| 206 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryRoot) { |
| 207 base::RunLoop run_loop; |
| 208 root_->ReadDirectory(base::FilePath(FILE_PATH_LITERAL("")), |
| 209 base::Bind( |
| 210 [](base::RunLoop* run_loop, base::File::Error error, |
| 211 const EntryList& file_list, bool has_more) { |
| 212 EXPECT_EQ(base::File::FILE_OK, error); |
| 213 ASSERT_EQ(1u, file_list.size()); |
| 214 EXPECT_EQ(FILE_PATH_LITERAL("pet"), |
| 215 file_list[0].name); |
| 216 EXPECT_TRUE(file_list[0].is_directory); |
| 217 EXPECT_FALSE(has_more); |
| 218 run_loop->Quit(); |
| 219 }, |
| 220 &run_loop)); |
| 221 run_loop.Run(); |
| 222 } |
| 223 |
| 224 TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryNoSuchDirectory) { |
| 225 base::RunLoop run_loop; |
| 226 root_->ReadDirectory(base::FilePath(FILE_PATH_LITERAL("humans")), |
| 227 base::Bind( |
| 228 [](base::RunLoop* run_loop, base::File::Error error, |
| 229 const EntryList& file_list, bool has_more) { |
| 230 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); |
| 231 EXPECT_EQ(0u, file_list.size()); |
| 232 EXPECT_FALSE(has_more); |
| 233 run_loop->Quit(); |
| 234 }, |
| 235 &run_loop)); |
| 236 run_loop.Run(); |
| 237 } |
| 238 |
| 239 } // namespace arc |
OLD | NEW |