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/create_file.h"
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"
24 namespace file_system_provider
{
25 namespace operations
{
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("/kitty/and/puppy/happy");
36 class FileSystemProviderOperationsCreateFileTest
: public testing::Test
{
38 FileSystemProviderOperationsCreateFileTest() {}
39 ~FileSystemProviderOperationsCreateFileTest() override
{}
41 void SetUp() override
{
42 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
43 mount_options
.writable
= true;
44 file_system_info_
= ProvidedFileSystemInfo(
45 kExtensionId
, mount_options
, base::FilePath(), false /* configurable */,
46 true /* watchable */, extensions::SOURCE_FILE
);
49 ProvidedFileSystemInfo file_system_info_
;
52 TEST_F(FileSystemProviderOperationsCreateFileTest
, Execute
) {
53 using extensions::api::file_system_provider::CreateFileRequestedOptions
;
55 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
56 util::StatusCallbackLog callback_log
;
58 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
59 base::Bind(&util::LogStatusCallback
, &callback_log
));
60 create_file
.SetDispatchEventImplForTesting(
61 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
62 base::Unretained(&dispatcher
)));
64 EXPECT_TRUE(create_file
.Execute(kRequestId
));
66 ASSERT_EQ(1u, dispatcher
.events().size());
67 extensions::Event
* event
= dispatcher
.events()[0];
69 extensions::api::file_system_provider::OnCreateFileRequested::kEventName
,
71 base::ListValue
* event_args
= event
->event_args
.get();
72 ASSERT_EQ(1u, event_args
->GetSize());
74 const base::DictionaryValue
* options_as_value
= NULL
;
75 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
77 CreateFileRequestedOptions options
;
79 CreateFileRequestedOptions::Populate(*options_as_value
, &options
));
80 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
81 EXPECT_EQ(kRequestId
, options
.request_id
);
82 EXPECT_EQ(kFilePath
, options
.file_path
);
85 TEST_F(FileSystemProviderOperationsCreateFileTest
, Execute_NoListener
) {
86 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
87 util::StatusCallbackLog callback_log
;
89 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
90 base::Bind(&util::LogStatusCallback
, &callback_log
));
91 create_file
.SetDispatchEventImplForTesting(
92 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
93 base::Unretained(&dispatcher
)));
95 EXPECT_FALSE(create_file
.Execute(kRequestId
));
98 TEST_F(FileSystemProviderOperationsCreateFileTest
, Execute_ReadOnly
) {
99 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
100 util::StatusCallbackLog callback_log
;
102 const ProvidedFileSystemInfo
read_only_file_system_info(
103 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
104 base::FilePath() /* mount_path */, false /* configurable */,
105 true /* watchable */, extensions::SOURCE_FILE
);
107 CreateFile
create_file(NULL
, read_only_file_system_info
,
108 base::FilePath(kFilePath
),
109 base::Bind(&util::LogStatusCallback
, &callback_log
));
110 create_file
.SetDispatchEventImplForTesting(
111 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
112 base::Unretained(&dispatcher
)));
114 EXPECT_FALSE(create_file
.Execute(kRequestId
));
117 TEST_F(FileSystemProviderOperationsCreateFileTest
, OnSuccess
) {
118 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
119 util::StatusCallbackLog callback_log
;
121 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
122 base::Bind(&util::LogStatusCallback
, &callback_log
));
123 create_file
.SetDispatchEventImplForTesting(
124 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
125 base::Unretained(&dispatcher
)));
127 EXPECT_TRUE(create_file
.Execute(kRequestId
));
129 create_file
.OnSuccess(kRequestId
,
130 scoped_ptr
<RequestValue
>(new RequestValue()),
131 false /* has_more */);
132 ASSERT_EQ(1u, callback_log
.size());
133 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
136 TEST_F(FileSystemProviderOperationsCreateFileTest
, OnError
) {
137 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
138 util::StatusCallbackLog callback_log
;
140 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
141 base::Bind(&util::LogStatusCallback
, &callback_log
));
142 create_file
.SetDispatchEventImplForTesting(
143 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
144 base::Unretained(&dispatcher
)));
146 EXPECT_TRUE(create_file
.Execute(kRequestId
));
148 create_file
.OnError(kRequestId
,
149 scoped_ptr
<RequestValue
>(new RequestValue()),
150 base::File::FILE_ERROR_TOO_MANY_OPENED
);
151 ASSERT_EQ(1u, callback_log
.size());
152 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
155 } // namespace operations
156 } // namespace file_system_provider
157 } // namespace chromeos