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 / open_file_unittest.cc
blob8cc3088c110de41ccbd6ab8ac320648d18277a19
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_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"
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28 const char kFileSystemId[] = "testing-file-system";
29 const int kRequestId = 2;
30 const base::FilePath::CharType kFilePath[] =
31 FILE_PATH_LITERAL("/directory/blueberries.txt");
33 // Callback invocation logger. Acts as a fileapi end-point.
34 class CallbackLogger {
35 public:
36 class Event {
37 public:
38 Event(int file_handle, base::File::Error result)
39 : file_handle_(file_handle), result_(result) {}
40 virtual ~Event() {}
42 int file_handle() { return file_handle_; }
43 base::File::Error result() { return result_; }
45 private:
46 int file_handle_;
47 base::File::Error result_;
49 DISALLOW_COPY_AND_ASSIGN(Event);
52 CallbackLogger() {}
53 virtual ~CallbackLogger() {}
55 void OnOpenFile(int file_handle, base::File::Error result) {
56 events_.push_back(new Event(file_handle, result));
59 ScopedVector<Event>& events() { return events_; }
61 private:
62 ScopedVector<Event> events_;
64 DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
67 } // namespace
69 class FileSystemProviderOperationsOpenFileTest : public testing::Test {
70 protected:
71 FileSystemProviderOperationsOpenFileTest() {}
72 ~FileSystemProviderOperationsOpenFileTest() override {}
74 void SetUp() override {
75 file_system_info_ = ProvidedFileSystemInfo(
76 kExtensionId,
77 MountOptions(kFileSystemId, "" /* display_name */),
78 base::FilePath());
81 ProvidedFileSystemInfo file_system_info_;
84 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute) {
85 using extensions::api::file_system_provider::OpenFileRequestedOptions;
87 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
88 CallbackLogger callback_logger;
90 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
91 OPEN_FILE_MODE_READ,
92 base::Bind(&CallbackLogger::OnOpenFile,
93 base::Unretained(&callback_logger)));
94 open_file.SetDispatchEventImplForTesting(
95 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
96 base::Unretained(&dispatcher)));
98 EXPECT_TRUE(open_file.Execute(kRequestId));
100 ASSERT_EQ(1u, dispatcher.events().size());
101 extensions::Event* event = dispatcher.events()[0];
102 EXPECT_EQ(
103 extensions::api::file_system_provider::OnOpenFileRequested::kEventName,
104 event->event_name);
105 base::ListValue* event_args = event->event_args.get();
106 ASSERT_EQ(1u, event_args->GetSize());
108 const base::DictionaryValue* options_as_value = NULL;
109 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
111 OpenFileRequestedOptions options;
112 ASSERT_TRUE(OpenFileRequestedOptions::Populate(*options_as_value, &options));
113 EXPECT_EQ(kFileSystemId, options.file_system_id);
114 EXPECT_EQ(kRequestId, options.request_id);
115 EXPECT_EQ(kFilePath, options.file_path);
116 EXPECT_EQ(extensions::api::file_system_provider::OPEN_FILE_MODE_READ,
117 options.mode);
120 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_NoListener) {
121 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
122 CallbackLogger callback_logger;
124 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
125 OPEN_FILE_MODE_READ,
126 base::Bind(&CallbackLogger::OnOpenFile,
127 base::Unretained(&callback_logger)));
128 open_file.SetDispatchEventImplForTesting(
129 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
130 base::Unretained(&dispatcher)));
132 EXPECT_FALSE(open_file.Execute(kRequestId));
135 TEST_F(FileSystemProviderOperationsOpenFileTest, Execute_ReadOnly) {
136 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
137 CallbackLogger callback_logger;
139 const ProvidedFileSystemInfo read_only_file_system_info(
140 kExtensionId,
141 MountOptions(kFileSystemId, "" /* display_name */),
142 base::FilePath() /* mount_path */);
144 // Opening for read on a read-only file system is allowed.
146 OpenFile open_file(NULL, read_only_file_system_info,
147 base::FilePath(kFilePath), OPEN_FILE_MODE_READ,
148 base::Bind(&CallbackLogger::OnOpenFile,
149 base::Unretained(&callback_logger)));
150 open_file.SetDispatchEventImplForTesting(
151 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
152 base::Unretained(&dispatcher)));
154 EXPECT_TRUE(open_file.Execute(kRequestId));
157 // Opening for write on a read-only file system is forbidden and must fail.
159 OpenFile open_file(NULL, read_only_file_system_info,
160 base::FilePath(kFilePath), OPEN_FILE_MODE_WRITE,
161 base::Bind(&CallbackLogger::OnOpenFile,
162 base::Unretained(&callback_logger)));
163 open_file.SetDispatchEventImplForTesting(
164 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
165 base::Unretained(&dispatcher)));
167 EXPECT_FALSE(open_file.Execute(kRequestId));
171 TEST_F(FileSystemProviderOperationsOpenFileTest, OnSuccess) {
172 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
173 CallbackLogger callback_logger;
175 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
176 OPEN_FILE_MODE_READ,
177 base::Bind(&CallbackLogger::OnOpenFile,
178 base::Unretained(&callback_logger)));
179 open_file.SetDispatchEventImplForTesting(
180 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
181 base::Unretained(&dispatcher)));
183 EXPECT_TRUE(open_file.Execute(kRequestId));
185 open_file.OnSuccess(kRequestId,
186 scoped_ptr<RequestValue>(new RequestValue()),
187 false /* has_more */);
188 ASSERT_EQ(1u, callback_logger.events().size());
189 CallbackLogger::Event* event = callback_logger.events()[0];
190 EXPECT_EQ(base::File::FILE_OK, event->result());
191 EXPECT_LT(0, event->file_handle());
194 TEST_F(FileSystemProviderOperationsOpenFileTest, OnError) {
195 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
196 CallbackLogger callback_logger;
198 OpenFile open_file(NULL, file_system_info_, base::FilePath(kFilePath),
199 OPEN_FILE_MODE_READ,
200 base::Bind(&CallbackLogger::OnOpenFile,
201 base::Unretained(&callback_logger)));
202 open_file.SetDispatchEventImplForTesting(
203 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
204 base::Unretained(&dispatcher)));
206 EXPECT_TRUE(open_file.Execute(kRequestId));
208 open_file.OnError(kRequestId,
209 scoped_ptr<RequestValue>(new RequestValue()),
210 base::File::FILE_ERROR_TOO_MANY_OPENED);
211 ASSERT_EQ(1u, callback_logger.events().size());
212 CallbackLogger::Event* event = callback_logger.events()[0];
213 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
214 ASSERT_EQ(0, event->file_handle());
217 } // namespace operations
218 } // namespace file_system_provider
219 } // namespace chromeos