Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / read_directory_unittest.cc
blob3855147b25fe13f04f7461a0f921f1290819dc28
1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/file_system_provider/operations/read_directory.h"
7 #include <string>
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h"
16 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
17 #include "chrome/common/extensions/api/file_system_provider.h"
18 #include "chrome/common/extensions/api/file_system_provider_internal.h"
19 #include "extensions/browser/event_router.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace chromeos {
24 namespace file_system_provider {
25 namespace operations {
26 namespace {
28 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
29 const char kFileSystemId[] = "testing-file-system";
30 const int kRequestId = 2;
31 const base::FilePath::CharType kDirectoryPath[] =
32 FILE_PATH_LITERAL("/directory");
34 // Callback invocation logger. Acts as a fileapi end-point.
35 class CallbackLogger {
36 public:
37 class Event {
38 public:
39 Event(base::File::Error result,
40 const storage::AsyncFileUtil::EntryList& entry_list,
41 bool has_more)
42 : result_(result), entry_list_(entry_list), has_more_(has_more) {}
43 virtual ~Event() {}
45 base::File::Error result() { return result_; }
46 const storage::AsyncFileUtil::EntryList& entry_list() {
47 return entry_list_;
49 bool has_more() { return has_more_; }
51 private:
52 base::File::Error result_;
53 storage::AsyncFileUtil::EntryList entry_list_;
54 bool has_more_;
56 DISALLOW_COPY_AND_ASSIGN(Event);
59 CallbackLogger() {}
60 virtual ~CallbackLogger() {}
62 void OnReadDirectory(base::File::Error result,
63 const storage::AsyncFileUtil::EntryList& entry_list,
64 bool has_more) {
65 events_.push_back(new Event(result, entry_list, has_more));
68 ScopedVector<Event>& events() { return events_; }
70 private:
71 ScopedVector<Event> events_;
73 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
76 // Returns the request value as |result| in case of successful parse.
77 void CreateRequestValueFromJSON(const std::string& json,
78 scoped_ptr<RequestValue>* result) {
79 using extensions::api::file_system_provider_internal::
80 ReadDirectoryRequestedSuccess::Params;
82 int json_error_code;
83 std::string json_error_msg;
84 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
85 json, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg));
86 ASSERT_TRUE(value.get()) << json_error_msg;
88 base::ListValue* value_as_list;
89 ASSERT_TRUE(value->GetAsList(&value_as_list));
90 scoped_ptr<Params> params(Params::Create(*value_as_list));
91 ASSERT_TRUE(params.get());
92 *result = RequestValue::CreateForReadDirectorySuccess(params.Pass());
93 ASSERT_TRUE(result->get());
96 } // namespace
98 class FileSystemProviderOperationsReadDirectoryTest : public testing::Test {
99 protected:
100 FileSystemProviderOperationsReadDirectoryTest() {}
101 ~FileSystemProviderOperationsReadDirectoryTest() override {}
103 void SetUp() override {
104 file_system_info_ = ProvidedFileSystemInfo(
105 kExtensionId,
106 MountOptions(kFileSystemId, "" /* display_name */),
107 base::FilePath());
110 ProvidedFileSystemInfo file_system_info_;
113 TEST_F(FileSystemProviderOperationsReadDirectoryTest, Execute) {
114 using extensions::api::file_system_provider::ReadDirectoryRequestedOptions;
116 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
117 CallbackLogger callback_logger;
119 ReadDirectory read_directory(NULL, file_system_info_,
120 base::FilePath(kDirectoryPath),
121 base::Bind(&CallbackLogger::OnReadDirectory,
122 base::Unretained(&callback_logger)));
123 read_directory.SetDispatchEventImplForTesting(
124 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
125 base::Unretained(&dispatcher)));
127 EXPECT_TRUE(read_directory.Execute(kRequestId));
129 ASSERT_EQ(1u, dispatcher.events().size());
130 extensions::Event* event = dispatcher.events()[0];
131 EXPECT_EQ(extensions::api::file_system_provider::OnReadDirectoryRequested::
132 kEventName,
133 event->event_name);
134 base::ListValue* event_args = event->event_args.get();
135 ASSERT_EQ(1u, event_args->GetSize());
137 const base::DictionaryValue* options_as_value = NULL;
138 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
140 ReadDirectoryRequestedOptions options;
141 ASSERT_TRUE(
142 ReadDirectoryRequestedOptions::Populate(*options_as_value, &options));
143 EXPECT_EQ(kFileSystemId, options.file_system_id);
144 EXPECT_EQ(kRequestId, options.request_id);
145 EXPECT_EQ(kDirectoryPath, options.directory_path);
148 TEST_F(FileSystemProviderOperationsReadDirectoryTest, Execute_NoListener) {
149 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
150 CallbackLogger callback_logger;
152 ReadDirectory read_directory(NULL, file_system_info_,
153 base::FilePath(kDirectoryPath),
154 base::Bind(&CallbackLogger::OnReadDirectory,
155 base::Unretained(&callback_logger)));
156 read_directory.SetDispatchEventImplForTesting(
157 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
158 base::Unretained(&dispatcher)));
160 EXPECT_FALSE(read_directory.Execute(kRequestId));
163 TEST_F(FileSystemProviderOperationsReadDirectoryTest, OnSuccess) {
164 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
165 CallbackLogger callback_logger;
167 ReadDirectory read_directory(NULL, file_system_info_,
168 base::FilePath(kDirectoryPath),
169 base::Bind(&CallbackLogger::OnReadDirectory,
170 base::Unretained(&callback_logger)));
171 read_directory.SetDispatchEventImplForTesting(
172 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
173 base::Unretained(&dispatcher)));
175 EXPECT_TRUE(read_directory.Execute(kRequestId));
177 // Sample input as JSON. Keep in sync with file_system_provider_api.idl.
178 // As for now, it is impossible to create *::Params class directly, not from
179 // base::Value.
180 const std::string input =
181 "[\n"
182 " \"testing-file-system\",\n" // kFileSystemId
183 " 2,\n" // kRequestId
184 " [\n"
185 " {\n"
186 " \"isDirectory\": false,\n"
187 " \"name\": \"blueberries.txt\",\n"
188 " \"size\": 4096,\n"
189 " \"modificationTime\": {\n"
190 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n"
191 " }\n"
192 " }\n"
193 " ],\n"
194 " false,\n" // has_more
195 " 0\n" // execution_time
196 "]\n";
197 scoped_ptr<RequestValue> request_value;
198 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value));
200 const bool has_more = false;
201 read_directory.OnSuccess(kRequestId, request_value.Pass(), has_more);
203 ASSERT_EQ(1u, callback_logger.events().size());
204 CallbackLogger::Event* event = callback_logger.events()[0];
205 EXPECT_EQ(base::File::FILE_OK, event->result());
207 ASSERT_EQ(1u, event->entry_list().size());
208 const storage::DirectoryEntry entry = event->entry_list()[0];
209 EXPECT_FALSE(entry.is_directory);
210 EXPECT_EQ("blueberries.txt", entry.name);
211 EXPECT_EQ(4096, entry.size);
212 base::Time expected_time;
213 EXPECT_TRUE(
214 base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", &expected_time));
215 EXPECT_EQ(expected_time, entry.last_modified_time);
218 TEST_F(FileSystemProviderOperationsReadDirectoryTest,
219 OnSuccess_InvalidMetadata) {
220 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
221 CallbackLogger callback_logger;
223 ReadDirectory read_directory(NULL, file_system_info_,
224 base::FilePath(kDirectoryPath),
225 base::Bind(&CallbackLogger::OnReadDirectory,
226 base::Unretained(&callback_logger)));
227 read_directory.SetDispatchEventImplForTesting(
228 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
229 base::Unretained(&dispatcher)));
231 EXPECT_TRUE(read_directory.Execute(kRequestId));
233 // Sample input as JSON. Keep in sync with file_system_provider_api.idl.
234 // As for now, it is impossible to create *::Params class directly, not from
235 // base::Value.
236 const std::string input =
237 "[\n"
238 " \"testing-file-system\",\n" // kFileSystemId
239 " 2,\n" // kRequestId
240 " [\n"
241 " {\n"
242 " \"isDirectory\": false,\n"
243 " \"name\": \"blue/berries.txt\",\n"
244 " \"size\": 4096,\n"
245 " \"modificationTime\": {\n"
246 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n"
247 " }\n"
248 " }\n"
249 " ],\n"
250 " false,\n" // has_more
251 " 0\n" // execution_time
252 "]\n";
253 scoped_ptr<RequestValue> request_value;
254 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value));
256 const bool has_more = false;
257 read_directory.OnSuccess(kRequestId, request_value.Pass(), has_more);
259 ASSERT_EQ(1u, callback_logger.events().size());
260 CallbackLogger::Event* event = callback_logger.events()[0];
261 EXPECT_EQ(base::File::FILE_ERROR_IO, event->result());
263 EXPECT_EQ(0u, event->entry_list().size());
266 TEST_F(FileSystemProviderOperationsReadDirectoryTest, OnError) {
267 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
268 CallbackLogger callback_logger;
270 ReadDirectory read_directory(NULL, file_system_info_,
271 base::FilePath(kDirectoryPath),
272 base::Bind(&CallbackLogger::OnReadDirectory,
273 base::Unretained(&callback_logger)));
274 read_directory.SetDispatchEventImplForTesting(
275 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
276 base::Unretained(&dispatcher)));
278 EXPECT_TRUE(read_directory.Execute(kRequestId));
280 read_directory.OnError(kRequestId,
281 scoped_ptr<RequestValue>(new RequestValue()),
282 base::File::FILE_ERROR_TOO_MANY_OPENED);
284 ASSERT_EQ(1u, callback_logger.events().size());
285 CallbackLogger::Event* event = callback_logger.events()[0];
286 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
287 ASSERT_EQ(0u, event->entry_list().size());
290 } // namespace operations
291 } // namespace file_system_provider
292 } // namespace chromeos