1 // Copyright 2015 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/get_actions.h"
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.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 "storage/browser/fileapi/async_file_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
25 namespace file_system_provider
{
26 namespace operations
{
29 const char kExtensionId
[] = "mbflcebpggnecokmikipoihdbecnjfoj";
30 const char kFileSystemId
[] = "testing-file-system";
31 const int kRequestId
= 2;
32 const base::FilePath::CharType kEntryPath
[] = FILE_PATH_LITERAL("/directory");
34 // Callback invocation logger. Acts as a fileapi end-point.
35 class CallbackLogger
{
39 Event(const Actions
& actions
, base::File::Error result
)
40 : actions_(actions
), result_(result
) {}
43 const Actions
& actions() const { return actions_
; }
44 base::File::Error
result() const { return result_
; }
48 base::File::Error result_
;
50 DISALLOW_COPY_AND_ASSIGN(Event
);
54 virtual ~CallbackLogger() {}
56 void OnGetActions(const Actions
& actions
, base::File::Error result
) {
57 events_
.push_back(new Event(actions
, result
));
60 const ScopedVector
<Event
>& events() const { return events_
; }
63 ScopedVector
<Event
> events_
;
65 DISALLOW_COPY_AND_ASSIGN(CallbackLogger
);
68 // Returns the request value as |result| in case of successful parse.
69 void CreateRequestValueFromJSON(const std::string
& json
,
70 scoped_ptr
<RequestValue
>* result
) {
71 using extensions::api::file_system_provider_internal::
72 GetActionsRequestedSuccess::Params
;
75 std::string json_error_msg
;
76 scoped_ptr
<base::Value
> value
= base::JSONReader::ReadAndReturnError(
77 json
, base::JSON_PARSE_RFC
, &json_error_code
, &json_error_msg
);
78 ASSERT_TRUE(value
.get()) << json_error_msg
;
80 base::ListValue
* value_as_list
;
81 ASSERT_TRUE(value
->GetAsList(&value_as_list
));
82 scoped_ptr
<Params
> params(Params::Create(*value_as_list
));
83 ASSERT_TRUE(params
.get());
84 *result
= RequestValue::CreateForGetActionsSuccess(params
.Pass());
85 ASSERT_TRUE(result
->get());
90 class FileSystemProviderOperationsGetActionsTest
: public testing::Test
{
92 FileSystemProviderOperationsGetActionsTest() {}
93 ~FileSystemProviderOperationsGetActionsTest() override
{}
95 void SetUp() override
{
96 file_system_info_
= ProvidedFileSystemInfo(
97 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
98 base::FilePath(), false /* configurable */, true /* watchable */,
99 extensions::SOURCE_FILE
);
102 ProvidedFileSystemInfo file_system_info_
;
105 TEST_F(FileSystemProviderOperationsGetActionsTest
, Execute
) {
106 using extensions::api::file_system_provider::GetActionsRequestedOptions
;
108 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
109 CallbackLogger callback_logger
;
111 GetActions
get_actions(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
112 base::Bind(&CallbackLogger::OnGetActions
,
113 base::Unretained(&callback_logger
)));
114 get_actions
.SetDispatchEventImplForTesting(
115 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
116 base::Unretained(&dispatcher
)));
118 EXPECT_TRUE(get_actions
.Execute(kRequestId
));
120 ASSERT_EQ(1u, dispatcher
.events().size());
121 extensions::Event
* event
= dispatcher
.events()[0];
123 extensions::api::file_system_provider::OnGetActionsRequested::kEventName
,
125 base::ListValue
* event_args
= event
->event_args
.get();
126 ASSERT_EQ(1u, event_args
->GetSize());
128 const base::DictionaryValue
* options_as_value
= NULL
;
129 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
131 GetActionsRequestedOptions options
;
133 GetActionsRequestedOptions::Populate(*options_as_value
, &options
));
134 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
135 EXPECT_EQ(kRequestId
, options
.request_id
);
136 EXPECT_EQ(kEntryPath
, options
.entry_path
);
139 TEST_F(FileSystemProviderOperationsGetActionsTest
, Execute_NoListener
) {
140 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
141 CallbackLogger callback_logger
;
143 GetActions
get_actions(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
144 base::Bind(&CallbackLogger::OnGetActions
,
145 base::Unretained(&callback_logger
)));
146 get_actions
.SetDispatchEventImplForTesting(
147 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
148 base::Unretained(&dispatcher
)));
150 EXPECT_FALSE(get_actions
.Execute(kRequestId
));
153 TEST_F(FileSystemProviderOperationsGetActionsTest
, OnSuccess
) {
154 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
155 CallbackLogger callback_logger
;
157 GetActions
get_actions(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
158 base::Bind(&CallbackLogger::OnGetActions
,
159 base::Unretained(&callback_logger
)));
160 get_actions
.SetDispatchEventImplForTesting(
161 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
162 base::Unretained(&dispatcher
)));
164 EXPECT_TRUE(get_actions
.Execute(kRequestId
));
166 // Sample input as JSON. Keep in sync with file_system_provider_api.idl.
167 // As for now, it is impossible to create *::Params class directly, not from
169 const std::string input
=
171 " \"testing-file-system\",\n" // kFileSystemId
172 " 2,\n" // kRequestId
175 " \"id\": \"SAVE_FOR_OFFLINE\"\n"
178 " \"id\": \"OFFLINE_NOT_NECESSARY\",\n"
179 " \"title\": \"Ignored title.\"\n"
182 " \"id\": \"SomeCustomActionId\",\n"
183 " \"title\": \"Custom action.\"\n"
186 " 0\n" // execution_time
188 scoped_ptr
<RequestValue
> request_value
;
189 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input
, &request_value
));
191 const bool has_more
= false;
192 get_actions
.OnSuccess(kRequestId
, request_value
.Pass(), has_more
);
194 ASSERT_EQ(1u, callback_logger
.events().size());
195 CallbackLogger::Event
* event
= callback_logger
.events()[0];
196 EXPECT_EQ(base::File::FILE_OK
, event
->result());
198 ASSERT_EQ(3u, event
->actions().size());
199 const Action action_share
= event
->actions()[0];
200 EXPECT_EQ("SAVE_FOR_OFFLINE", action_share
.id
);
201 EXPECT_EQ("", action_share
.title
);
203 const Action action_pin_toggle
= event
->actions()[1];
204 EXPECT_EQ("OFFLINE_NOT_NECESSARY", action_pin_toggle
.id
);
205 EXPECT_EQ("Ignored title.", action_pin_toggle
.title
);
207 const Action action_custom
= event
->actions()[2];
208 EXPECT_EQ("SomeCustomActionId", action_custom
.id
);
209 EXPECT_EQ("Custom action.", action_custom
.title
);
212 TEST_F(FileSystemProviderOperationsGetActionsTest
, OnError
) {
213 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
214 CallbackLogger callback_logger
;
216 GetActions
get_actions(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
217 base::Bind(&CallbackLogger::OnGetActions
,
218 base::Unretained(&callback_logger
)));
219 get_actions
.SetDispatchEventImplForTesting(
220 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
221 base::Unretained(&dispatcher
)));
223 EXPECT_TRUE(get_actions
.Execute(kRequestId
));
225 get_actions
.OnError(kRequestId
, scoped_ptr
<RequestValue
>(new RequestValue()),
226 base::File::FILE_ERROR_TOO_MANY_OPENED
);
228 ASSERT_EQ(1u, callback_logger
.events().size());
229 CallbackLogger::Event
* event
= callback_logger
.events()[0];
230 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, event
->result());
231 ASSERT_EQ(0u, event
->actions().size());
234 } // namespace operations
235 } // namespace file_system_provider
236 } // namespace chromeos