Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / operations / open_file_unittest.cc
blob725054770b67f4e461bf199789269f592add961f
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"
7 #include <string>
8 #include <vector>
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_capabilities/file_system_provider_capabilities_handler.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 kFilePath[] =
32 FILE_PATH_LITERAL("/directory/blueberries.txt");
34 // Callback invocation logger. Acts as a fileapi end-point.
35 class CallbackLogger {
36 public:
37 class Event {
38 public:
39 Event(int file_handle, base::File::Error result)
40 : file_handle_(file_handle), result_(result) {}
41 virtual ~Event() {}
43 int file_handle() { return file_handle_; }
44 base::File::Error result() { return result_; }
46 private:
47 int file_handle_;
48 base::File::Error result_;
50 DISALLOW_COPY_AND_ASSIGN(Event);
53 CallbackLogger() {}
54 virtual ~CallbackLogger() {}
56 void OnOpenFile(int file_handle, base::File::Error result) {
57 events_.push_back(new Event(file_handle, result));
60 ScopedVector<Event>& events() { return events_; }
62 private:
63 ScopedVector<Event> events_;
65 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
68 } // namespace
70 class FileSystemProviderOperationsOpenFileTest : public testing::Test {
71 protected:
72 FileSystemProviderOperationsOpenFileTest() {}
73 ~FileSystemProviderOperationsOpenFileTest() override {}
75 void SetUp() override {
76 file_system_info_ = ProvidedFileSystemInfo(
77 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
78 base::FilePath(), false /* configurable */, true /* watchable */,
79 extensions::SOURCE_FILE);
82 ProvidedFileSystemInfo file_system_info_;
85 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute) {
86 using extensions::api::file_system_provider::OpenFileRequestedOptions;
88 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
89 CallbackLogger callback_logger;
91 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
92 OPEN_FILE_MODE_READ,
93 base::Bind(&CallbackLogger::OnOpenFile,
94 base::Unretained(&callback_logger)));
95 open_file.SetDispatchEventImplForTesting(
96 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
97 base::Unretained(&dispatcher)));
99 EXPECT_TRUE(open_file.Execute(kRequestId));
101 ASSERT_EQ(1u, dispatcher.events().size());
102 extensions::Event* event = dispatcher.events()[0];
103 EXPECT_EQ(
104 extensions::api::file_system_provider::OnOpenFileRequested::kEventName,
105 event->event_name);
106 base::ListValue* event_args = event->event_args.get();
107 ASSERT_EQ(1u, event_args->GetSize());
109 const base::DictionaryValue* options_as_value = NULL;
110 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
112 OpenFileRequestedOptions options;
113 ASSERT_TRUE(OpenFileRequestedOptions::Populate(*options_as_value, &options));
114 EXPECT_EQ(kFileSystemId, options.file_system_id);
115 EXPECT_EQ(kRequestId, options.request_id);
116 EXPECT_EQ(kFilePath, options.file_path);
117 EXPECT_EQ(extensions::api::file_system_provider::OPEN_FILE_MODE_READ,
118 options.mode);
121 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_NoListener) {
122 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
123 CallbackLogger callback_logger;
125 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
126 OPEN_FILE_MODE_READ,
127 base::Bind(&CallbackLogger::OnOpenFile,
128 base::Unretained(&callback_logger)));
129 open_file.SetDispatchEventImplForTesting(
130 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
131 base::Unretained(&dispatcher)));
133 EXPECT_FALSE(open_file.Execute(kRequestId));
136 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_ReadOnly) {
137 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
138 CallbackLogger callback_logger;
140 const ProvidedFileSystemInfo read_only_file_system_info(
141 kExtensionId, MountOptions(kFileSystemId, "" /* display_name */),
142 base::FilePath() /* mount_path */, false /* configurable */,
143 true /* watchable */, extensions::SOURCE_FILE);
145 // Opening for read on a read-only file system is allowed.
147 OpenFile open_file(NULL, read_only_file_system_info,
148 base::FilePath(kFilePath), OPEN_FILE_MODE_READ,
149 base::Bind(&CallbackLogger::OnOpenFile,
150 base::Unretained(&callback_logger)));
151 open_file.SetDispatchEventImplForTesting(
152 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
153 base::Unretained(&dispatcher)));
155 EXPECT_TRUE(open_file.Execute(kRequestId));
158 // Opening for write on a read-only file system is forbidden and must fail.
160 OpenFile open_file(NULL, read_only_file_system_info,
161 base::FilePath(kFilePath), OPEN_FILE_MODE_WRITE,
162 base::Bind(&CallbackLogger::OnOpenFile,
163 base::Unretained(&callback_logger)));
164 open_file.SetDispatchEventImplForTesting(
165 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
166 base::Unretained(&dispatcher)));
168 EXPECT_FALSE(open_file.Execute(kRequestId));
172 TEST_F(FileSystemProviderOperationsOpenFileTest, OnSuccess) {
173 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
174 CallbackLogger callback_logger;
176 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
177 OPEN_FILE_MODE_READ,
178 base::Bind(&CallbackLogger::OnOpenFile,
179 base::Unretained(&callback_logger)));
180 open_file.SetDispatchEventImplForTesting(
181 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
182 base::Unretained(&dispatcher)));
184 EXPECT_TRUE(open_file.Execute(kRequestId));
186 open_file.OnSuccess(kRequestId,
187 scoped_ptr<RequestValue>(new RequestValue()),
188 false /* has_more */);
189 ASSERT_EQ(1u, callback_logger.events().size());
190 CallbackLogger::Event* event = callback_logger.events()[0];
191 EXPECT_EQ(base::File::FILE_OK, event->result());
192 EXPECT_LT(0, event->file_handle());
195 TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) {
196 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
197 CallbackLogger callback_logger;
199 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
200 OPEN_FILE_MODE_READ,
201 base::Bind(&CallbackLogger::OnOpenFile,
202 base::Unretained(&callback_logger)));
203 open_file.SetDispatchEventImplForTesting(
204 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
205 base::Unretained(&dispatcher)));
207 EXPECT_TRUE(open_file.Execute(kRequestId));
209 open_file.OnError(kRequestId,
210 scoped_ptr<RequestValue>(new RequestValue()),
211 base::File::FILE_ERROR_TOO_MANY_OPENED);
212 ASSERT_EQ(1u, callback_logger.events().size());
213 CallbackLogger::Event* event = callback_logger.events()[0];
214 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
215 ASSERT_EQ(0, event->file_handle());
218 } // namespace operations
219 } // namespace file_system_provider
220 } // namespace chromeos