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/abort.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 int kOperationRequestId
= 3;
35 class FileSystemProviderOperationsAbortTest
: public testing::Test
{
37 FileSystemProviderOperationsAbortTest() {}
38 ~FileSystemProviderOperationsAbortTest() override
{}
40 void SetUp() override
{
41 file_system_info_
= ProvidedFileSystemInfo(
42 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
43 base::FilePath(), false /* configurable */, true /* watchable */,
44 extensions::SOURCE_FILE
);
47 ProvidedFileSystemInfo file_system_info_
;
50 TEST_F(FileSystemProviderOperationsAbortTest
, Execute
) {
51 using extensions::api::file_system_provider::AbortRequestedOptions
;
53 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
54 util::StatusCallbackLog callback_log
;
59 base::Bind(&util::LogStatusCallback
, &callback_log
));
60 abort
.SetDispatchEventImplForTesting(
61 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
62 base::Unretained(&dispatcher
)));
64 EXPECT_TRUE(abort
.Execute(kRequestId
));
66 ASSERT_EQ(1u, dispatcher
.events().size());
67 extensions::Event
* event
= dispatcher
.events()[0];
68 EXPECT_EQ(extensions::api::file_system_provider::OnAbortRequested::kEventName
,
70 base::ListValue
* event_args
= event
->event_args
.get();
71 ASSERT_EQ(1u, event_args
->GetSize());
73 const base::DictionaryValue
* options_as_value
= NULL
;
74 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
76 AbortRequestedOptions options
;
77 ASSERT_TRUE(AbortRequestedOptions::Populate(*options_as_value
, &options
));
78 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
79 EXPECT_EQ(kRequestId
, options
.request_id
);
80 EXPECT_EQ(kOperationRequestId
, options
.operation_request_id
);
83 TEST_F(FileSystemProviderOperationsAbortTest
, Execute_NoListener
) {
84 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
85 util::StatusCallbackLog callback_log
;
90 base::Bind(&util::LogStatusCallback
, &callback_log
));
91 abort
.SetDispatchEventImplForTesting(
92 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
93 base::Unretained(&dispatcher
)));
95 EXPECT_FALSE(abort
.Execute(kRequestId
));
98 TEST_F(FileSystemProviderOperationsAbortTest
, OnSuccess
) {
99 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
100 util::StatusCallbackLog callback_log
;
105 base::Bind(&util::LogStatusCallback
, &callback_log
));
106 abort
.SetDispatchEventImplForTesting(
107 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
108 base::Unretained(&dispatcher
)));
110 EXPECT_TRUE(abort
.Execute(kRequestId
));
112 abort
.OnSuccess(kRequestId
,
113 scoped_ptr
<RequestValue
>(new RequestValue()),
114 false /* has_more */);
115 ASSERT_EQ(1u, callback_log
.size());
116 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
119 TEST_F(FileSystemProviderOperationsAbortTest
, OnError
) {
120 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
121 util::StatusCallbackLog callback_log
;
126 base::Bind(&util::LogStatusCallback
, &callback_log
));
127 abort
.SetDispatchEventImplForTesting(
128 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
129 base::Unretained(&dispatcher
)));
131 EXPECT_TRUE(abort
.Execute(kRequestId
));
133 abort
.OnError(kRequestId
,
134 scoped_ptr
<RequestValue
>(new RequestValue()),
135 base::File::FILE_ERROR_TOO_MANY_OPENED
);
136 ASSERT_EQ(1u, callback_log
.size());
137 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
140 } // namespace operations
141 } // namespace file_system_provider
142 } // namespace chromeos