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/write_file.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/values.h"
17 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
18 #include "chrome/common/extensions/api/file_system_provider.h"
19 #include "chrome/common/extensions/api/file_system_provider_internal.h"
20 #include "extensions/browser/event_router.h"
21 #include "net/base/io_buffer.h"
22 #include "storage/browser/fileapi/async_file_util.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 namespace file_system_provider
{
27 namespace operations
{
30 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
31 const char kFileSystemId
[] = "testing-file-system";
32 const int kRequestId
= 2;
33 const int kFileHandle
= 3;
34 const char kWriteData
[] = "Welcome to my world!";
35 const int kOffset
= 10;
39 class FileSystemProviderOperationsWriteFileTest
: public testing::Test
{
41 FileSystemProviderOperationsWriteFileTest() {}
42 ~FileSystemProviderOperationsWriteFileTest() override
{}
44 void SetUp() override
{
45 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
46 mount_options
.writable
= true;
48 ProvidedFileSystemInfo(kExtensionId
, mount_options
, base::FilePath());
49 io_buffer_
= make_scoped_refptr(new net::StringIOBuffer(kWriteData
));
52 ProvidedFileSystemInfo file_system_info_
;
53 scoped_refptr
<net::StringIOBuffer
> io_buffer_
;
56 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute
) {
57 using extensions::api::file_system_provider::WriteFileRequestedOptions
;
59 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
60 util::StatusCallbackLog callback_log
;
62 WriteFile
write_file(NULL
,
68 base::Bind(&util::LogStatusCallback
, &callback_log
));
69 write_file
.SetDispatchEventImplForTesting(
70 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
71 base::Unretained(&dispatcher
)));
73 EXPECT_TRUE(write_file
.Execute(kRequestId
));
75 ASSERT_EQ(1u, dispatcher
.events().size());
76 extensions::Event
* event
= dispatcher
.events()[0];
78 extensions::api::file_system_provider::OnWriteFileRequested::kEventName
,
80 base::ListValue
* event_args
= event
->event_args
.get();
81 ASSERT_EQ(1u, event_args
->GetSize());
83 const base::DictionaryValue
* options_as_value
= NULL
;
84 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
86 WriteFileRequestedOptions options
;
87 ASSERT_TRUE(WriteFileRequestedOptions::Populate(*options_as_value
, &options
));
88 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
89 EXPECT_EQ(kRequestId
, options
.request_id
);
90 EXPECT_EQ(kFileHandle
, options
.open_request_id
);
91 EXPECT_EQ(kOffset
, static_cast<double>(options
.offset
));
92 std::string
write_data(kWriteData
);
93 EXPECT_EQ(std::vector
<char>(write_data
.begin(), write_data
.end()),
97 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute_NoListener
) {
98 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
99 util::StatusCallbackLog callback_log
;
101 WriteFile
write_file(NULL
,
107 base::Bind(&util::LogStatusCallback
, &callback_log
));
108 write_file
.SetDispatchEventImplForTesting(
109 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
110 base::Unretained(&dispatcher
)));
112 EXPECT_FALSE(write_file
.Execute(kRequestId
));
115 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute_ReadOnly
) {
116 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
117 util::StatusCallbackLog callback_log
;
119 const ProvidedFileSystemInfo
read_only_file_system_info(
121 MountOptions(kFileSystemId
, "" /* display_name */),
122 base::FilePath() /* mount_path */);
124 WriteFile
write_file(NULL
,
125 read_only_file_system_info
,
130 base::Bind(&util::LogStatusCallback
, &callback_log
));
131 write_file
.SetDispatchEventImplForTesting(
132 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
133 base::Unretained(&dispatcher
)));
135 EXPECT_FALSE(write_file
.Execute(kRequestId
));
138 TEST_F(FileSystemProviderOperationsWriteFileTest
, OnSuccess
) {
139 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
140 util::StatusCallbackLog callback_log
;
142 WriteFile
write_file(NULL
,
148 base::Bind(&util::LogStatusCallback
, &callback_log
));
149 write_file
.SetDispatchEventImplForTesting(
150 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
151 base::Unretained(&dispatcher
)));
153 EXPECT_TRUE(write_file
.Execute(kRequestId
));
155 write_file
.OnSuccess(kRequestId
,
156 scoped_ptr
<RequestValue
>(new RequestValue()),
157 false /* has_more */);
158 ASSERT_EQ(1u, callback_log
.size());
159 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
162 TEST_F(FileSystemProviderOperationsWriteFileTest
, OnError
) {
163 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
164 util::StatusCallbackLog callback_log
;
166 WriteFile
write_file(NULL
,
172 base::Bind(&util::LogStatusCallback
, &callback_log
));
173 write_file
.SetDispatchEventImplForTesting(
174 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
175 base::Unretained(&dispatcher
)));
177 EXPECT_TRUE(write_file
.Execute(kRequestId
));
179 write_file
.OnError(kRequestId
,
180 scoped_ptr
<RequestValue
>(new RequestValue()),
181 base::File::FILE_ERROR_TOO_MANY_OPENED
);
183 ASSERT_EQ(1u, callback_log
.size());
184 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
187 } // namespace operations
188 } // namespace file_system_provider
189 } // namespace chromeos