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_capabilities/file_system_provider_capabilities_handler.h"
20 #include "chrome/common/extensions/api/file_system_provider_internal.h"
21 #include "extensions/browser/event_router.h"
22 #include "net/base/io_buffer.h"
23 #include "storage/browser/fileapi/async_file_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
27 namespace file_system_provider
{
28 namespace operations
{
31 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
32 const char kFileSystemId
[] = "testing-file-system";
33 const int kRequestId
= 2;
34 const int kFileHandle
= 3;
35 const char kWriteData
[] = "Welcome to my world!";
36 const int kOffset
= 10;
40 class FileSystemProviderOperationsWriteFileTest
: public testing::Test
{
42 FileSystemProviderOperationsWriteFileTest() {}
43 ~FileSystemProviderOperationsWriteFileTest() override
{}
45 void SetUp() override
{
46 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
47 mount_options
.writable
= true;
48 file_system_info_
= ProvidedFileSystemInfo(
49 kExtensionId
, mount_options
, base::FilePath(), false /* configurable */,
50 true /* watchable */, extensions::SOURCE_FILE
);
51 io_buffer_
= make_scoped_refptr(new net::StringIOBuffer(kWriteData
));
54 ProvidedFileSystemInfo file_system_info_
;
55 scoped_refptr
<net::StringIOBuffer
> io_buffer_
;
58 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute
) {
59 using extensions::api::file_system_provider::WriteFileRequestedOptions
;
61 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
62 util::StatusCallbackLog callback_log
;
64 WriteFile
write_file(NULL
,
70 base::Bind(&util::LogStatusCallback
, &callback_log
));
71 write_file
.SetDispatchEventImplForTesting(
72 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
73 base::Unretained(&dispatcher
)));
75 EXPECT_TRUE(write_file
.Execute(kRequestId
));
77 ASSERT_EQ(1u, dispatcher
.events().size());
78 extensions::Event
* event
= dispatcher
.events()[0];
80 extensions::api::file_system_provider::OnWriteFileRequested::kEventName
,
82 base::ListValue
* event_args
= event
->event_args
.get();
83 ASSERT_EQ(1u, event_args
->GetSize());
85 const base::DictionaryValue
* options_as_value
= NULL
;
86 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
88 WriteFileRequestedOptions options
;
89 ASSERT_TRUE(WriteFileRequestedOptions::Populate(*options_as_value
, &options
));
90 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
91 EXPECT_EQ(kRequestId
, options
.request_id
);
92 EXPECT_EQ(kFileHandle
, options
.open_request_id
);
93 EXPECT_EQ(kOffset
, static_cast<double>(options
.offset
));
94 std::string
write_data(kWriteData
);
95 EXPECT_EQ(std::vector
<char>(write_data
.begin(), write_data
.end()),
99 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute_NoListener
) {
100 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
101 util::StatusCallbackLog callback_log
;
103 WriteFile
write_file(NULL
,
109 base::Bind(&util::LogStatusCallback
, &callback_log
));
110 write_file
.SetDispatchEventImplForTesting(
111 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
112 base::Unretained(&dispatcher
)));
114 EXPECT_FALSE(write_file
.Execute(kRequestId
));
117 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute_ReadOnly
) {
118 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
119 util::StatusCallbackLog callback_log
;
121 const ProvidedFileSystemInfo
read_only_file_system_info(
122 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
123 base::FilePath() /* mount_path */, false /* configurable */,
124 true /* watchable */, extensions::SOURCE_FILE
);
126 WriteFile
write_file(NULL
,
127 read_only_file_system_info
,
132 base::Bind(&util::LogStatusCallback
, &callback_log
));
133 write_file
.SetDispatchEventImplForTesting(
134 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
135 base::Unretained(&dispatcher
)));
137 EXPECT_FALSE(write_file
.Execute(kRequestId
));
140 TEST_F(FileSystemProviderOperationsWriteFileTest
, OnSuccess
) {
141 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
142 util::StatusCallbackLog callback_log
;
144 WriteFile
write_file(NULL
,
150 base::Bind(&util::LogStatusCallback
, &callback_log
));
151 write_file
.SetDispatchEventImplForTesting(
152 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
153 base::Unretained(&dispatcher
)));
155 EXPECT_TRUE(write_file
.Execute(kRequestId
));
157 write_file
.OnSuccess(kRequestId
,
158 scoped_ptr
<RequestValue
>(new RequestValue()),
159 false /* has_more */);
160 ASSERT_EQ(1u, callback_log
.size());
161 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
164 TEST_F(FileSystemProviderOperationsWriteFileTest
, OnError
) {
165 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
166 util::StatusCallbackLog callback_log
;
168 WriteFile
write_file(NULL
,
174 base::Bind(&util::LogStatusCallback
, &callback_log
));
175 write_file
.SetDispatchEventImplForTesting(
176 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
177 base::Unretained(&dispatcher
)));
179 EXPECT_TRUE(write_file
.Execute(kRequestId
));
181 write_file
.OnError(kRequestId
,
182 scoped_ptr
<RequestValue
>(new RequestValue()),
183 base::File::FILE_ERROR_TOO_MANY_OPENED
);
185 ASSERT_EQ(1u, callback_log
.size());
186 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
189 } // namespace operations
190 } // namespace file_system_provider
191 } // namespace chromeos