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 "chrome/browser/chromeos/file_system_provider/operations/test_util.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
15 #include "chrome/common/extensions/api/file_system_provider.h"
16 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_system_provider_capabilities_handler.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 int kOperationRequestId
= 3;
34 class FileSystemProviderOperationsAbortTest
: public testing::Test
{
36 FileSystemProviderOperationsAbortTest() {}
37 ~FileSystemProviderOperationsAbortTest() override
{}
39 void SetUp() override
{
40 file_system_info_
= ProvidedFileSystemInfo(
41 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
42 base::FilePath(), false /* configurable */, true /* watchable */,
43 extensions::SOURCE_FILE
);
46 ProvidedFileSystemInfo file_system_info_
;
49 TEST_F(FileSystemProviderOperationsAbortTest
, Execute
) {
50 using extensions::api::file_system_provider::AbortRequestedOptions
;
52 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
53 util::StatusCallbackLog callback_log
;
58 base::Bind(&util::LogStatusCallback
, &callback_log
));
59 abort
.SetDispatchEventImplForTesting(
60 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
61 base::Unretained(&dispatcher
)));
63 EXPECT_TRUE(abort
.Execute(kRequestId
));
65 ASSERT_EQ(1u, dispatcher
.events().size());
66 extensions::Event
* event
= dispatcher
.events()[0];
67 EXPECT_EQ(extensions::api::file_system_provider::OnAbortRequested::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 AbortRequestedOptions options
;
76 ASSERT_TRUE(AbortRequestedOptions::Populate(*options_as_value
, &options
));
77 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
78 EXPECT_EQ(kRequestId
, options
.request_id
);
79 EXPECT_EQ(kOperationRequestId
, options
.operation_request_id
);
82 TEST_F(FileSystemProviderOperationsAbortTest
, Execute_NoListener
) {
83 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
84 util::StatusCallbackLog callback_log
;
89 base::Bind(&util::LogStatusCallback
, &callback_log
));
90 abort
.SetDispatchEventImplForTesting(
91 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
92 base::Unretained(&dispatcher
)));
94 EXPECT_FALSE(abort
.Execute(kRequestId
));
97 TEST_F(FileSystemProviderOperationsAbortTest
, OnSuccess
) {
98 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
99 util::StatusCallbackLog callback_log
;
104 base::Bind(&util::LogStatusCallback
, &callback_log
));
105 abort
.SetDispatchEventImplForTesting(
106 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
107 base::Unretained(&dispatcher
)));
109 EXPECT_TRUE(abort
.Execute(kRequestId
));
111 abort
.OnSuccess(kRequestId
,
112 scoped_ptr
<RequestValue
>(new RequestValue()),
113 false /* has_more */);
114 ASSERT_EQ(1u, callback_log
.size());
115 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
118 TEST_F(FileSystemProviderOperationsAbortTest
, OnError
) {
119 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
120 util::StatusCallbackLog callback_log
;
125 base::Bind(&util::LogStatusCallback
, &callback_log
));
126 abort
.SetDispatchEventImplForTesting(
127 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
128 base::Unretained(&dispatcher
)));
130 EXPECT_TRUE(abort
.Execute(kRequestId
));
132 abort
.OnError(kRequestId
,
133 scoped_ptr
<RequestValue
>(new RequestValue()),
134 base::File::FILE_ERROR_TOO_MANY_OPENED
);
135 ASSERT_EQ(1u, callback_log
.size());
136 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
139 } // namespace operations
140 } // namespace file_system_provider
141 } // namespace chromeos