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/values.h"
16 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
17 #include "chrome/common/extensions/api/file_system_provider.h"
18 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.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;
47 file_system_info_
= ProvidedFileSystemInfo(
48 kExtensionId
, mount_options
, base::FilePath(), false /* configurable */,
49 true /* watchable */, extensions::SOURCE_FILE
);
50 io_buffer_
= make_scoped_refptr(new net::StringIOBuffer(kWriteData
));
53 ProvidedFileSystemInfo file_system_info_
;
54 scoped_refptr
<net::StringIOBuffer
> io_buffer_
;
57 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute
) {
58 using extensions::api::file_system_provider::WriteFileRequestedOptions
;
60 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
61 util::StatusCallbackLog callback_log
;
63 WriteFile
write_file(NULL
,
69 base::Bind(&util::LogStatusCallback
, &callback_log
));
70 write_file
.SetDispatchEventImplForTesting(
71 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
72 base::Unretained(&dispatcher
)));
74 EXPECT_TRUE(write_file
.Execute(kRequestId
));
76 ASSERT_EQ(1u, dispatcher
.events().size());
77 extensions::Event
* event
= dispatcher
.events()[0];
79 extensions::api::file_system_provider::OnWriteFileRequested::kEventName
,
81 base::ListValue
* event_args
= event
->event_args
.get();
82 ASSERT_EQ(1u, event_args
->GetSize());
84 const base::DictionaryValue
* options_as_value
= NULL
;
85 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
87 WriteFileRequestedOptions options
;
88 ASSERT_TRUE(WriteFileRequestedOptions::Populate(*options_as_value
, &options
));
89 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
90 EXPECT_EQ(kRequestId
, options
.request_id
);
91 EXPECT_EQ(kFileHandle
, options
.open_request_id
);
92 EXPECT_EQ(kOffset
, static_cast<double>(options
.offset
));
93 std::string
write_data(kWriteData
);
94 EXPECT_EQ(std::vector
<char>(write_data
.begin(), write_data
.end()),
98 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute_NoListener
) {
99 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
100 util::StatusCallbackLog callback_log
;
102 WriteFile
write_file(NULL
,
108 base::Bind(&util::LogStatusCallback
, &callback_log
));
109 write_file
.SetDispatchEventImplForTesting(
110 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
111 base::Unretained(&dispatcher
)));
113 EXPECT_FALSE(write_file
.Execute(kRequestId
));
116 TEST_F(FileSystemProviderOperationsWriteFileTest
, Execute_ReadOnly
) {
117 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
118 util::StatusCallbackLog callback_log
;
120 const ProvidedFileSystemInfo
read_only_file_system_info(
121 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
122 base::FilePath() /* mount_path */, false /* configurable */,
123 true /* watchable */, extensions::SOURCE_FILE
);
125 WriteFile
write_file(NULL
,
126 read_only_file_system_info
,
131 base::Bind(&util::LogStatusCallback
, &callback_log
));
132 write_file
.SetDispatchEventImplForTesting(
133 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
134 base::Unretained(&dispatcher
)));
136 EXPECT_FALSE(write_file
.Execute(kRequestId
));
139 TEST_F(FileSystemProviderOperationsWriteFileTest
, OnSuccess
) {
140 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
141 util::StatusCallbackLog callback_log
;
143 WriteFile
write_file(NULL
,
149 base::Bind(&util::LogStatusCallback
, &callback_log
));
150 write_file
.SetDispatchEventImplForTesting(
151 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
152 base::Unretained(&dispatcher
)));
154 EXPECT_TRUE(write_file
.Execute(kRequestId
));
156 write_file
.OnSuccess(kRequestId
,
157 scoped_ptr
<RequestValue
>(new RequestValue()),
158 false /* has_more */);
159 ASSERT_EQ(1u, callback_log
.size());
160 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
163 TEST_F(FileSystemProviderOperationsWriteFileTest
, OnError
) {
164 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
165 util::StatusCallbackLog callback_log
;
167 WriteFile
write_file(NULL
,
173 base::Bind(&util::LogStatusCallback
, &callback_log
));
174 write_file
.SetDispatchEventImplForTesting(
175 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
176 base::Unretained(&dispatcher
)));
178 EXPECT_TRUE(write_file
.Execute(kRequestId
));
180 write_file
.OnError(kRequestId
,
181 scoped_ptr
<RequestValue
>(new RequestValue()),
182 base::File::FILE_ERROR_TOO_MANY_OPENED
);
184 ASSERT_EQ(1u, callback_log
.size());
185 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
188 } // namespace operations
189 } // namespace file_system_provider
190 } // namespace chromeos