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/open_file.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
16 #include "chrome/common/extensions/api/file_system_provider.h"
17 #include "chrome/common/extensions/api/file_system_provider_internal.h"
18 #include "extensions/browser/event_router.h"
19 #include "storage/browser/fileapi/async_file_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 namespace file_system_provider {
24 namespace operations {
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const base::FilePath::CharType kFilePath[] = "/directory/blueberries.txt";
32 // Callback invocation logger. Acts as a fileapi end-point.
33 class CallbackLogger {
37 Event(int file_handle, base::File::Error result)
38 : file_handle_(file_handle), result_(result) {}
41 int file_handle() { return file_handle_; }
42 base::File::Error result() { return result_; }
46 base::File::Error result_;
48 DISALLOW_COPY_AND_ASSIGN(Event);
52 virtual ~CallbackLogger() {}
54 void OnOpenFile(int file_handle, base::File::Error result) {
55 events_.push_back(new Event(file_handle, result));
58 ScopedVector<Event>& events() { return events_; }
61 ScopedVector<Event> events_;
64 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
69 class FileSystemProviderOperationsOpenFileTest : public testing::Test {
71 FileSystemProviderOperationsOpenFileTest() {}
72 virtual ~FileSystemProviderOperationsOpenFileTest() {}
74 virtual void SetUp() OVERRIDE {
76 ProvidedFileSystemInfo(kExtensionId,
78 "" /* display_name */,
80 base::FilePath() /* mount_path */);
83 ProvidedFileSystemInfo file_system_info_;
86 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute) {
87 using extensions::api::file_system_provider::OpenFileRequestedOptions;
89 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
90 CallbackLogger callback_logger;
92 OpenFile open_file(NULL,
94 base::FilePath::FromUTF8Unsafe(kFilePath),
95 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
96 base::Bind(&CallbackLogger::OnOpenFile,
97 base::Unretained(&callback_logger)));
98 open_file.SetDispatchEventImplForTesting(
99 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
100 base::Unretained(&dispatcher)));
102 EXPECT_TRUE(open_file.Execute(kRequestId));
104 ASSERT_EQ(1u, dispatcher.events().size());
105 extensions::Event* event = dispatcher.events()[0];
107 extensions::api::file_system_provider::OnOpenFileRequested::kEventName,
109 base::ListValue* event_args = event->event_args.get();
110 ASSERT_EQ(1u, event_args->GetSize());
112 const base::DictionaryValue* options_as_value = NULL;
113 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
115 OpenFileRequestedOptions options;
116 ASSERT_TRUE(OpenFileRequestedOptions::Populate(*options_as_value, &options));
117 EXPECT_EQ(kFileSystemId, options.file_system_id);
118 EXPECT_EQ(kRequestId, options.request_id);
119 EXPECT_EQ(kFilePath, options.file_path);
120 EXPECT_EQ(extensions::api::file_system_provider::OPEN_FILE_MODE_READ,
124 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_NoListener) {
125 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
126 CallbackLogger callback_logger;
128 OpenFile open_file(NULL,
130 base::FilePath::FromUTF8Unsafe(kFilePath),
131 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
132 base::Bind(&CallbackLogger::OnOpenFile,
133 base::Unretained(&callback_logger)));
134 open_file.SetDispatchEventImplForTesting(
135 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
136 base::Unretained(&dispatcher)));
138 EXPECT_FALSE(open_file.Execute(kRequestId));
141 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_ReadOnly) {
142 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
143 CallbackLogger callback_logger;
145 const ProvidedFileSystemInfo read_only_file_system_info(
148 "" /* file_system_name */,
149 false /* writable */,
150 base::FilePath() /* mount_path */);
152 // Opening for read on a read-only file system is allowed.
154 OpenFile open_file(NULL,
155 read_only_file_system_info,
156 base::FilePath::FromUTF8Unsafe(kFilePath),
157 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
158 base::Bind(&CallbackLogger::OnOpenFile,
159 base::Unretained(&callback_logger)));
160 open_file.SetDispatchEventImplForTesting(
161 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
162 base::Unretained(&dispatcher)));
164 EXPECT_TRUE(open_file.Execute(kRequestId));
167 // Opening for write on a read-only file system is forbidden and must fail.
169 OpenFile open_file(NULL,
170 read_only_file_system_info,
171 base::FilePath::FromUTF8Unsafe(kFilePath),
172 ProvidedFileSystemInterface::OPEN_FILE_MODE_WRITE,
173 base::Bind(&CallbackLogger::OnOpenFile,
174 base::Unretained(&callback_logger)));
175 open_file.SetDispatchEventImplForTesting(
176 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
177 base::Unretained(&dispatcher)));
179 EXPECT_FALSE(open_file.Execute(kRequestId));
183 TEST_F(FileSystemProviderOperationsOpenFileTest, OnSuccess) {
184 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
185 CallbackLogger callback_logger;
187 OpenFile open_file(NULL,
189 base::FilePath::FromUTF8Unsafe(kFilePath),
190 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
191 base::Bind(&CallbackLogger::OnOpenFile,
192 base::Unretained(&callback_logger)));
193 open_file.SetDispatchEventImplForTesting(
194 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
195 base::Unretained(&dispatcher)));
197 EXPECT_TRUE(open_file.Execute(kRequestId));
199 open_file.OnSuccess(kRequestId,
200 scoped_ptr<RequestValue>(new RequestValue()),
201 false /* has_more */);
202 ASSERT_EQ(1u, callback_logger.events().size());
203 CallbackLogger::Event* event = callback_logger.events()[0];
204 EXPECT_EQ(base::File::FILE_OK, event->result());
205 EXPECT_LT(0, event->file_handle());
208 TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) {
209 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
210 CallbackLogger callback_logger;
212 OpenFile open_file(NULL,
214 base::FilePath::FromUTF8Unsafe(kFilePath),
215 ProvidedFileSystemInterface::OPEN_FILE_MODE_READ,
216 base::Bind(&CallbackLogger::OnOpenFile,
217 base::Unretained(&callback_logger)));
218 open_file.SetDispatchEventImplForTesting(
219 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
220 base::Unretained(&dispatcher)));
222 EXPECT_TRUE(open_file.Execute(kRequestId));
224 open_file.OnError(kRequestId,
225 scoped_ptr<RequestValue>(new RequestValue()),
226 base::File::FILE_ERROR_TOO_MANY_OPENED);
227 ASSERT_EQ(1u, callback_logger.events().size());
228 CallbackLogger::Event* event = callback_logger.events()[0];
229 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
230 ASSERT_EQ(0, event->file_handle());
233 } // namespace operations
234 } // namespace file_system_provider
235 } // namespace chromeos