Chromium Code Reviews| Index: chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root_unittest.cc |
| diff --git a/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root_unittest.cc b/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cb1467e272ad772f306a8af923d2ab725fce0e68 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root_unittest.cc |
| @@ -0,0 +1,318 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string.h> |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/files/file_path.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h" |
| +#include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "components/arc/arc_service_manager.h" |
| +#include "components/arc/test/fake_file_system_instance.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "storage/common/fileapi/directory_entry.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using storage::DirectoryEntry; |
| +using EntryList = storage::AsyncFileUtil::EntryList; |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +struct DocumentSpec { |
| + const char* document_id; |
| + const char* display_name; |
| + const char* mime_type; |
| + int64_t size; |
| + uint64_t last_modified; |
| +}; |
| + |
| +// Fake file system hierarchy: |
| +// |
| +// <path> <type> <ID> |
| +// (root)/ dir root-id |
| +// dir/ dir dir-id |
| +// photo.jpg image/jpeg photo-id |
| +// music.bin audio/mp3 music-id |
| +// dups/ dir dups-id |
| +// dup.mp4 video/mp4 dup1-id |
| +// dup.mp4 video/mp4 dup2-id |
| +// dup.mp4 video/mp4 dup3-id |
| +// dup.mp4 video/mp4 dup4-id |
| +constexpr char kAuthority[] = "org.chromium.test"; |
| +constexpr DocumentSpec kRootSpec = {"root-id", "(root)", |
| + 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.
|
| +constexpr DocumentSpec kDirSpec = {"dir-id", "dir", kAndroidDirectoryMimeType, |
| + 0, 22}; |
| +constexpr DocumentSpec kPhotoSpec = {"photo-id", "photo.jpg", "image/jpeg", 3, |
| + 33}; |
| +constexpr DocumentSpec kMusicSpec = {"music-id", "music.bin", "audio/mp3", 4, |
| + 44}; |
| +constexpr DocumentSpec kDupsSpec = {"dups-id", "dups", |
| + kAndroidDirectoryMimeType, 0, 55}; |
| +constexpr DocumentSpec kDup1Spec = {"dup1-id", "dup.mp4", "video/mp4", 6, 66}; |
| +constexpr DocumentSpec kDup2Spec = {"dup2-id", "dup.mp4", "video/mp4", 7, 77}; |
| +constexpr DocumentSpec kDup3Spec = {"dup3-id", "dup.mp4", "video/mp4", 8, 88}; |
| +constexpr DocumentSpec kDup4Spec = {"dup4-id", "dup.mp4", "video/mp4", 9, 99}; |
| + |
| +mojom::DocumentPtr MakeDocument(const DocumentSpec& spec) { |
| + mojom::DocumentPtr document = mojom::Document::New(); |
| + document->document_id = spec.document_id; |
| + document->display_name = spec.display_name; |
| + document->mime_type = spec.mime_type; |
| + document->size = spec.size; |
| + document->last_modified = spec.last_modified; |
| + return document; |
| +} |
| + |
| +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
|
| + public: |
| + void GetChildDocuments(const std::string& authority, |
| + const std::string& document_id, |
| + const GetChildDocumentsCallback& callback) override { |
| + EXPECT_EQ(kAuthority, authority); |
| + base::Optional<std::vector<mojom::DocumentPtr>> result; |
| + if (document_id == kRootSpec.document_id) { |
| + result.emplace(); |
| + result.value().emplace_back(MakeDocument(kDirSpec)); |
| + result.value().emplace_back(MakeDocument(kDupsSpec)); |
| + } else if (document_id == kDirSpec.document_id) { |
| + result.emplace(); |
| + result.value().emplace_back(MakeDocument(kPhotoSpec)); |
| + result.value().emplace_back(MakeDocument(kMusicSpec)); |
| + } else if (document_id == kDupsSpec.document_id) { |
| + result.emplace(); |
| + // The order is intentionally shuffled. |
| + result.value().emplace_back(MakeDocument(kDup2Spec)); |
| + result.value().emplace_back(MakeDocument(kDup1Spec)); |
| + result.value().emplace_back(MakeDocument(kDup4Spec)); |
| + result.value().emplace_back(MakeDocument(kDup3Spec)); |
| + } |
| + callback.Run(std::move(result)); |
| + } |
| + |
| + void GetDocument(const std::string& authority, |
| + const std::string& document_id, |
| + const GetDocumentCallback& callback) override { |
| + EXPECT_EQ(kAuthority, authority); |
| + mojom::DocumentPtr result; |
| + if (document_id == kRootSpec.document_id) { |
| + result = MakeDocument(kRootSpec); |
| + } else if (document_id == kDirSpec.document_id) { |
| + result = MakeDocument(kDirSpec); |
| + } else if (document_id == kPhotoSpec.document_id) { |
| + result = MakeDocument(kPhotoSpec); |
| + } else if (document_id == kMusicSpec.document_id) { |
| + result = MakeDocument(kMusicSpec); |
| + } else if (document_id == kDupsSpec.document_id) { |
| + result = MakeDocument(kDupsSpec); |
| + } else if (document_id == kDup1Spec.document_id) { |
| + result = MakeDocument(kDup1Spec); |
| + } else if (document_id == kDup2Spec.document_id) { |
| + result = MakeDocument(kDup2Spec); |
| + } else if (document_id == kDup3Spec.document_id) { |
| + result = MakeDocument(kDup3Spec); |
| + } else if (document_id == kDup4Spec.document_id) { |
| + result = MakeDocument(kDup4Spec); |
| + } |
| + callback.Run(std::move(result)); |
| + } |
| +}; |
| + |
| +void ExpectMatchesSpec(const base::File::Info& info, const DocumentSpec& spec) { |
| + EXPECT_EQ(spec.size, info.size); |
| + 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.
|
| + info.is_directory); |
| + 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.
|
| + EXPECT_EQ(spec.last_modified, |
| + static_cast<uint64_t>(info.last_modified.ToJavaTime())); |
| + EXPECT_EQ(spec.last_modified, |
| + static_cast<uint64_t>(info.last_accessed.ToJavaTime())); |
| + EXPECT_EQ(spec.last_modified, |
| + static_cast<uint64_t>(info.creation_time.ToJavaTime())); |
| +} |
| + |
| +class ArcDocumentsProviderRootTest : public testing::Test { |
| + public: |
| + ArcDocumentsProviderRootTest() { |
| + 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.
|
| + arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| + &file_system_); |
| + root_ = base::MakeUnique<ArcDocumentsProviderRoot>(kAuthority, |
|
hashimoto
2016/12/20 04:55:07
ditto.
Shuhei Takahashi
2016/12/20 06:08:24
Done.
|
| + kRootSpec.document_id); |
| + } |
| + |
| + ~ArcDocumentsProviderRootTest() override = default; |
| + |
| + protected: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + FakeFileSystemInstanceImpl file_system_; |
| + std::unique_ptr<ArcServiceManager> arc_service_manager_; |
| + std::unique_ptr<ArcDocumentsProviderRoot> root_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ArcDocumentsProviderRootTest); |
| +}; |
| + |
| +} // namespace |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfo) { |
| + base::RunLoop run_loop; |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir/photo.jpg")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ExpectMatchesSpec(info, kPhotoSpec); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDirectory) { |
| + base::RunLoop run_loop; |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ExpectMatchesSpec(info, kDirSpec); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfoRoot) { |
| + base::RunLoop run_loop; |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ExpectMatchesSpec(info, kRootSpec); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfoNoSuchFile) { |
| + base::RunLoop run_loop; |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dir/missing.jpg")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDups) { |
| + base::RunLoop run_loop; |
| + // "dup 2.mp4" should map to the 3rd instance of "dup.mp4" regardless of the |
| + // order returned from FileSystemInstance. |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("dups/dup 2.mp4")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ExpectMatchesSpec(info, kDup3Spec); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, ReadDirectory) { |
| + base::RunLoop run_loop; |
| + root_->ReadDirectory( |
| + base::FilePath(FILE_PATH_LITERAL("dir")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const EntryList& file_list, bool has_more) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ASSERT_EQ(2u, file_list.size()); |
| + EXPECT_EQ(FILE_PATH_LITERAL("music.bin.mp3"), file_list[0].name); |
| + EXPECT_FALSE(file_list[0].is_directory); |
| + EXPECT_EQ(FILE_PATH_LITERAL("photo.jpg"), file_list[1].name); |
| + EXPECT_FALSE(file_list[1].is_directory); |
| + EXPECT_FALSE(has_more); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryRoot) { |
| + base::RunLoop run_loop; |
| + root_->ReadDirectory( |
| + base::FilePath(FILE_PATH_LITERAL("")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const EntryList& file_list, bool has_more) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ASSERT_EQ(2u, file_list.size()); |
| + EXPECT_EQ(FILE_PATH_LITERAL("dir"), file_list[0].name); |
| + EXPECT_TRUE(file_list[0].is_directory); |
| + EXPECT_EQ(FILE_PATH_LITERAL("dups"), file_list[1].name); |
| + EXPECT_TRUE(file_list[1].is_directory); |
| + EXPECT_FALSE(has_more); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryNoSuchDirectory) { |
| + base::RunLoop run_loop; |
| + root_->ReadDirectory(base::FilePath(FILE_PATH_LITERAL("missing")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const EntryList& file_list, bool has_more) { |
| + EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, error); |
| + EXPECT_EQ(0u, file_list.size()); |
| + EXPECT_FALSE(has_more); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, ReadDirectoryDups) { |
| + base::RunLoop run_loop; |
| + root_->ReadDirectory( |
| + base::FilePath(FILE_PATH_LITERAL("dups")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const EntryList& file_list, bool has_more) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ASSERT_EQ(4u, file_list.size()); |
| + // FiIles are sorted lexicographically. |
| + EXPECT_EQ(FILE_PATH_LITERAL("dup 1.mp4"), file_list[0].name); |
| + EXPECT_FALSE(file_list[0].is_directory); |
| + EXPECT_EQ(FILE_PATH_LITERAL("dup 2.mp4"), file_list[1].name); |
| + EXPECT_FALSE(file_list[1].is_directory); |
| + EXPECT_EQ(FILE_PATH_LITERAL("dup 3.mp4"), file_list[2].name); |
| + EXPECT_FALSE(file_list[2].is_directory); |
| + EXPECT_EQ(FILE_PATH_LITERAL("dup.mp4"), file_list[3].name); |
| + EXPECT_FALSE(file_list[3].is_directory); |
| + EXPECT_FALSE(has_more); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +} // namespace arc |