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_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"
23 namespace file_system_provider
{
24 namespace operations
{
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("/kitty/and/puppy/happy");
35 class FileSystemProviderOperationsCreateFileTest
: public testing::Test
{
37 FileSystemProviderOperationsCreateFileTest() {}
38 ~FileSystemProviderOperationsCreateFileTest() override
{}
40 void SetUp() override
{
41 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
42 mount_options
.writable
= true;
44 ProvidedFileSystemInfo(kExtensionId
, mount_options
, base::FilePath());
47 ProvidedFileSystemInfo file_system_info_
;
50 TEST_F(FileSystemProviderOperationsCreateFileTest
, Execute
) {
51 using extensions::api::file_system_provider::CreateFileRequestedOptions
;
53 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
54 util::StatusCallbackLog callback_log
;
56 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
57 base::Bind(&util::LogStatusCallback
, &callback_log
));
58 create_file
.SetDispatchEventImplForTesting(
59 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
60 base::Unretained(&dispatcher
)));
62 EXPECT_TRUE(create_file
.Execute(kRequestId
));
64 ASSERT_EQ(1u, dispatcher
.events().size());
65 extensions::Event
* event
= dispatcher
.events()[0];
67 extensions::api::file_system_provider::OnCreateFileRequested::kEventName
,
69 base::ListValue
* event_args
= event
->event_args
.get();
70 ASSERT_EQ(1u, event_args
->GetSize());
72 const base::DictionaryValue
* options_as_value
= NULL
;
73 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
75 CreateFileRequestedOptions options
;
77 CreateFileRequestedOptions::Populate(*options_as_value
, &options
));
78 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
79 EXPECT_EQ(kRequestId
, options
.request_id
);
80 EXPECT_EQ(kFilePath
, options
.file_path
);
83 TEST_F(FileSystemProviderOperationsCreateFileTest
, Execute_NoListener
) {
84 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
85 util::StatusCallbackLog callback_log
;
87 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
88 base::Bind(&util::LogStatusCallback
, &callback_log
));
89 create_file
.SetDispatchEventImplForTesting(
90 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
91 base::Unretained(&dispatcher
)));
93 EXPECT_FALSE(create_file
.Execute(kRequestId
));
96 TEST_F(FileSystemProviderOperationsCreateFileTest
, Execute_ReadOnly
) {
97 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
98 util::StatusCallbackLog callback_log
;
100 const ProvidedFileSystemInfo
read_only_file_system_info(
102 MountOptions(kFileSystemId
, "" /* display_name */),
103 base::FilePath() /* mount_path */);
105 CreateFile
create_file(NULL
, read_only_file_system_info
,
106 base::FilePath(kFilePath
),
107 base::Bind(&util::LogStatusCallback
, &callback_log
));
108 create_file
.SetDispatchEventImplForTesting(
109 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
110 base::Unretained(&dispatcher
)));
112 EXPECT_FALSE(create_file
.Execute(kRequestId
));
115 TEST_F(FileSystemProviderOperationsCreateFileTest
, OnSuccess
) {
116 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
117 util::StatusCallbackLog callback_log
;
119 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
120 base::Bind(&util::LogStatusCallback
, &callback_log
));
121 create_file
.SetDispatchEventImplForTesting(
122 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
123 base::Unretained(&dispatcher
)));
125 EXPECT_TRUE(create_file
.Execute(kRequestId
));
127 create_file
.OnSuccess(kRequestId
,
128 scoped_ptr
<RequestValue
>(new RequestValue()),
129 false /* has_more */);
130 ASSERT_EQ(1u, callback_log
.size());
131 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
134 TEST_F(FileSystemProviderOperationsCreateFileTest
, OnError
) {
135 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
136 util::StatusCallbackLog callback_log
;
138 CreateFile
create_file(NULL
, file_system_info_
, base::FilePath(kFilePath
),
139 base::Bind(&util::LogStatusCallback
, &callback_log
));
140 create_file
.SetDispatchEventImplForTesting(
141 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
142 base::Unretained(&dispatcher
)));
144 EXPECT_TRUE(create_file
.Execute(kRequestId
));
146 create_file
.OnError(kRequestId
,
147 scoped_ptr
<RequestValue
>(new RequestValue()),
148 base::File::FILE_ERROR_TOO_MANY_OPENED
);
149 ASSERT_EQ(1u, callback_log
.size());
150 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
153 } // namespace operations
154 } // namespace file_system_provider
155 } // namespace chromeos