Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: components/arc/test/fake_file_system_instance.cc

Issue 2651883003: Clean up ARC file system unit tests. (Closed)
Patch Set: Addressed comments. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/arc/test/fake_file_system_instance.h" 5 #include "components/arc/test/fake_file_system_instance.h"
6 6
7 #include <string.h>
8 #include <unistd.h>
9
7 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/files/scoped_file.h"
8 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/logging.h"
9 #include "base/optional.h" 17 #include "base/optional.h"
10 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
11 #include "mojo/public/cpp/system/handle.h" 19 #include "mojo/edk/embedder/embedder.h"
12 20
13 namespace arc { 21 namespace arc {
14 22
15 FakeFileSystemInstance::FakeFileSystemInstance() = default; 23 namespace {
16 24
17 FakeFileSystemInstance::~FakeFileSystemInstance() = default; 25 base::ScopedFD CreateRegularFileDescriptor(const char* content,
26 const base::FilePath& temp_dir) {
27 base::FilePath path;
28 bool create_success = base::CreateTemporaryFileInDir(temp_dir, &path);
29 DCHECK(create_success);
30 int written_size = base::WriteFile(path, content, strlen(content));
31 DCHECK_EQ(static_cast<int>(strlen(content)), written_size);
32 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
33 DCHECK(file.IsValid());
34 return base::ScopedFD(file.TakePlatformFile());
35 }
18 36
19 void FakeFileSystemInstance::GetChildDocuments( 37 base::ScopedFD CreateStreamFileDescriptor(const char* content) {
20 const std::string& authority, 38 int fds[2];
21 const std::string& document_id, 39 int ret = pipe(fds);
22 const GetChildDocumentsCallback& callback) { 40 DCHECK_EQ(0, ret);
41 base::ScopedFD fd_read(fds[0]);
42 base::ScopedFD fd_write(fds[1]);
43 bool write_success =
44 base::WriteFileDescriptor(fd_write.get(), content, strlen(content));
45 DCHECK(write_success);
46 return fd_read;
47 }
48
49 mojom::DocumentPtr MakeDocument(
50 const FakeFileSystemInstance::DocumentSpec& spec) {
51 mojom::DocumentPtr document = mojom::Document::New();
52 document->document_id = spec.document_id;
53 document->display_name = spec.display_name;
54 document->mime_type = spec.mime_type;
55 document->size = spec.size;
56 document->last_modified = spec.last_modified;
57 return document;
58 }
59
60 } // namespace
61
62 FakeFileSystemInstance::FakeFileSystemInstance() {
63 DCHECK(thread_checker_.CalledOnValidThread());
hidehiko 2017/01/27 15:50:00 This looks redundant, because |thread_checker_| is
Shuhei Takahashi 2017/01/30 02:27:35 Done.
64 bool temp_dir_created = temp_dir_.CreateUniqueTempDir();
65 DCHECK(temp_dir_created);
66 }
67
68 FakeFileSystemInstance::~FakeFileSystemInstance() {
69 DCHECK(thread_checker_.CalledOnValidThread());
70 }
71
72 void FakeFileSystemInstance::AddFile(const FileSpec& spec) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 DCHECK_EQ(0u, files_.count(std::string(spec.url)));
75 files_[std::string(spec.url)] = spec;
76 }
77
78 void FakeFileSystemInstance::AddDocument(const DocumentSpec& spec) {
79 DCHECK(thread_checker_.CalledOnValidThread());
80 DocumentKey key(spec.authority, spec.document_id);
81 DCHECK_EQ(0u, documents_.count(key));
82 documents_[key] = spec;
83 child_documents_[key]; // Allocate a vector.
84 if (spec.parent_document_id) {
85 DocumentKey parent_key(spec.authority, spec.parent_document_id);
86 DCHECK_EQ(1u, documents_.count(parent_key));
87 child_documents_[parent_key].push_back(key);
88 }
89 }
90
91 void FakeFileSystemInstance::GetFileSize(const std::string& url,
92 const GetFileSizeCallback& callback) {
93 DCHECK(thread_checker_.CalledOnValidThread());
94 auto iter = files_.find(url);
95 if (iter == files_.end()) {
96 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
97 base::Bind(callback, -1));
98 return;
99 }
100 const FileSpec& spec = iter->second;
23 base::ThreadTaskRunnerHandle::Get()->PostTask( 101 base::ThreadTaskRunnerHandle::Get()->PostTask(
24 FROM_HERE, base::Bind(callback, base::nullopt)); 102 FROM_HERE, base::Bind(callback, strlen(spec.content)));
103 }
104
105 void FakeFileSystemInstance::OpenFileToRead(
106 const std::string& url,
107 const OpenFileToReadCallback& callback) {
108 DCHECK(thread_checker_.CalledOnValidThread());
109 auto iter = files_.find(url);
110 if (iter == files_.end()) {
111 base::ThreadTaskRunnerHandle::Get()->PostTask(
112 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle())));
113 return;
114 }
115 const FileSpec& spec = iter->second;
116 base::ScopedFD fd =
117 spec.seekable == FileSpec::SEEKABLE
118 ? CreateRegularFileDescriptor(spec.content, temp_dir_.GetPath())
119 : CreateStreamFileDescriptor(spec.content);
120 mojo::edk::ScopedPlatformHandle platform_handle(
121 mojo::edk::PlatformHandle(fd.release()));
122 MojoHandle wrapped_handle;
123 MojoResult result = mojo::edk::CreatePlatformHandleWrapper(
124 std::move(platform_handle), &wrapped_handle);
125 DCHECK_EQ(MOJO_RESULT_OK, result);
126 base::ThreadTaskRunnerHandle::Get()->PostTask(
127 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle(
128 mojo::Handle(wrapped_handle)))));
25 } 129 }
26 130
27 void FakeFileSystemInstance::GetDocument(const std::string& authority, 131 void FakeFileSystemInstance::GetDocument(const std::string& authority,
28 const std::string& document_id, 132 const std::string& document_id,
29 const GetDocumentCallback& callback) { 133 const GetDocumentCallback& callback) {
134 DCHECK(thread_checker_.CalledOnValidThread());
135 auto iter = documents_.find(DocumentKey(authority, document_id));
136 if (iter == documents_.end()) {
137 base::ThreadTaskRunnerHandle::Get()->PostTask(
138 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr())));
139 return;
140 }
30 base::ThreadTaskRunnerHandle::Get()->PostTask( 141 base::ThreadTaskRunnerHandle::Get()->PostTask(
31 FROM_HERE, base::Bind(callback, base::Passed(mojom::DocumentPtr()))); 142 FROM_HERE,
143 base::Bind(callback, base::Passed(MakeDocument(iter->second))));
32 } 144 }
33 145
34 void FakeFileSystemInstance::GetFileSize(const std::string& url, 146 void FakeFileSystemInstance::GetChildDocuments(
35 const GetFileSizeCallback& callback) { 147 const std::string& authority,
36 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 148 const std::string& parent_document_id,
37 base::Bind(callback, -1)); 149 const GetChildDocumentsCallback& callback) {
38 } 150 DCHECK(thread_checker_.CalledOnValidThread());
39 151 auto iter = child_documents_.find(DocumentKey(authority, parent_document_id));
40 void FakeFileSystemInstance::OpenFileToRead( 152 if (iter == child_documents_.end()) {
41 const std::string& url, 153 base::ThreadTaskRunnerHandle::Get()->PostTask(
42 const OpenFileToReadCallback& callback) { 154 FROM_HERE, base::Bind(callback, base::nullopt));
155 return;
156 }
157 std::vector<mojom::DocumentPtr> children;
158 for (const auto& child_key : iter->second) {
159 children.emplace_back(MakeDocument(documents_[child_key]));
160 }
43 base::ThreadTaskRunnerHandle::Get()->PostTask( 161 base::ThreadTaskRunnerHandle::Get()->PostTask(
44 FROM_HERE, base::Bind(callback, base::Passed(mojo::ScopedHandle()))); 162 FROM_HERE,
163 base::Bind(callback,
164 base::Passed(base::make_optional(std::move(children)))));
45 } 165 }
46 166
47 void FakeFileSystemInstance::RequestMediaScan( 167 void FakeFileSystemInstance::RequestMediaScan(
48 const std::vector<std::string>& paths) { 168 const std::vector<std::string>& paths) {
169 DCHECK(thread_checker_.CalledOnValidThread());
49 // Do nothing and pretend we scaned them. 170 // Do nothing and pretend we scaned them.
50 } 171 }
51 172
52 } // namespace arc 173 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698