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..80bdacd0b14465c7e77e2dbf01ee10f7a3f9c351 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root_unittest.cc |
| @@ -0,0 +1,239 @@ |
| +// 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; |
| +}; |
| + |
| +constexpr char kAuthority[] = "org.chromium.test"; |
| +constexpr DocumentSpec kRootDocumentSpec = {"root-id", "<root>", |
| + kAndroidDirectoryMimeType, -1, 0}; |
| +constexpr DocumentSpec kPetDocumentSpec = { |
| + "pet-id", "pet", kAndroidDirectoryMimeType, -1, 10000}; |
| +constexpr DocumentSpec kCatDocumentSpec = {"cat-id", "cat.png", "image/png", 28, |
| + 20000}; |
| +constexpr DocumentSpec kDogDocumentSpec = {"dog-id", "dog.mp3", "audio/mp3", -1, |
| + 30000}; |
| + |
| +static 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 { |
| + 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 == kRootDocumentSpec.document_id) { |
| + result.emplace(); |
| + result.value().emplace_back(MakeDocument(kPetDocumentSpec)); |
| + } else if (document_id == kPetDocumentSpec.document_id) { |
| + result.emplace(); |
| + result.value().emplace_back(MakeDocument(kCatDocumentSpec)); |
| + result.value().emplace_back(MakeDocument(kDogDocumentSpec)); |
| + } |
| + 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 == kRootDocumentSpec.document_id) { |
| + result = MakeDocument(kRootDocumentSpec); |
| + } else if (document_id == kPetDocumentSpec.document_id) { |
| + result = MakeDocument(kPetDocumentSpec); |
| + } else if (document_id == kCatDocumentSpec.document_id) { |
| + result = MakeDocument(kCatDocumentSpec); |
| + } else if (document_id == kDogDocumentSpec.document_id) { |
| + result = MakeDocument(kDogDocumentSpec); |
| + } |
| + callback.Run(std::move(result)); |
| + } |
| +}; |
| + |
| +void ExpectMatchesSpec(const base::File::Info& info, const DocumentSpec& spec) { |
| + EXPECT_EQ(info.size, spec.size); |
| + EXPECT_EQ(info.is_directory, |
| + strcmp(spec.mime_type, kAndroidDirectoryMimeType) == 0); |
| + EXPECT_EQ(info.is_symbolic_link, false); |
| + EXPECT_EQ(static_cast<uint64_t>(info.last_modified.ToJavaTime()), |
| + spec.last_modified); |
| + EXPECT_EQ(static_cast<uint64_t>(info.last_accessed.ToJavaTime()), |
| + spec.last_modified); |
| + EXPECT_EQ(static_cast<uint64_t>(info.creation_time.ToJavaTime()), |
| + spec.last_modified); |
| +} |
| + |
| +class ArcDocumentsProviderRootTest : public testing::Test { |
| + public: |
| + ArcDocumentsProviderRootTest() { |
| + arc_service_manager_ = base::MakeUnique<ArcServiceManager>(nullptr); |
| + arc_service_manager_->arc_bridge_service()->file_system()->SetInstance( |
| + &file_system_); |
| + root_ = base::MakeUnique<ArcDocumentsProviderRoot>( |
| + kAuthority, kRootDocumentSpec.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("pet/cat.png")), |
|
Luis Héctor Chávez
2016/12/15 23:54:26
Won't all of these DCHECK since you're running the
Shuhei Takahashi
2016/12/16 05:47:14
TestBrowserThreadBundle runs UI/IO/etc. threads as
|
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ExpectMatchesSpec(info, kCatDocumentSpec); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfoDirectory) { |
| + base::RunLoop run_loop; |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("pet")), |
| + base::Bind( |
| + [](base::RunLoop* run_loop, base::File::Error error, |
| + const base::File::Info& info) { |
| + EXPECT_EQ(base::File::FILE_OK, error); |
| + ExpectMatchesSpec(info, kPetDocumentSpec); |
| + 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, kRootDocumentSpec); |
| + run_loop->Quit(); |
| + }, |
| + &run_loop)); |
| + run_loop.Run(); |
| +} |
| + |
| +TEST_F(ArcDocumentsProviderRootTest, GetFileInfoNoSuchFile) { |
| + base::RunLoop run_loop; |
| + root_->GetFileInfo(base::FilePath(FILE_PATH_LITERAL("pet/fox.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, ReadDirectory) { |
| + base::RunLoop run_loop; |
| + root_->ReadDirectory( |
| + base::FilePath(FILE_PATH_LITERAL("pet")), |
| + 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("cat.png"), file_list[0].name); |
| + EXPECT_FALSE(file_list[0].is_directory); |
| + EXPECT_EQ(FILE_PATH_LITERAL("dog.mp3"), 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(1u, file_list.size()); |
| + EXPECT_EQ(FILE_PATH_LITERAL("pet"), |
| + file_list[0].name); |
| + EXPECT_TRUE(file_list[0].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("humans")), |
| + 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(); |
| +} |
| + |
| +} // namespace arc |