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/delete_entry.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 kEntryPath
[] =
32 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
36 class FileSystemProviderOperationsDeleteEntryTest
: public testing::Test
{
38 FileSystemProviderOperationsDeleteEntryTest() {}
39 ~FileSystemProviderOperationsDeleteEntryTest() 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(FileSystemProviderOperationsDeleteEntryTest
, Execute
) {
53 using extensions::api::file_system_provider::DeleteEntryRequestedOptions
;
55 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
56 util::StatusCallbackLog callback_log
;
58 DeleteEntry
delete_entry(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
60 base::Bind(&util::LogStatusCallback
, &callback_log
));
61 delete_entry
.SetDispatchEventImplForTesting(
62 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
63 base::Unretained(&dispatcher
)));
65 EXPECT_TRUE(delete_entry
.Execute(kRequestId
));
67 ASSERT_EQ(1u, dispatcher
.events().size());
68 extensions::Event
* event
= dispatcher
.events()[0];
70 extensions::api::file_system_provider::OnDeleteEntryRequested::kEventName
,
72 base::ListValue
* event_args
= event
->event_args
.get();
73 ASSERT_EQ(1u, event_args
->GetSize());
75 const base::DictionaryValue
* options_as_value
= NULL
;
76 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
78 DeleteEntryRequestedOptions options
;
80 DeleteEntryRequestedOptions::Populate(*options_as_value
, &options
));
81 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
82 EXPECT_EQ(kRequestId
, options
.request_id
);
83 EXPECT_EQ(kEntryPath
, options
.entry_path
);
84 EXPECT_TRUE(options
.recursive
);
87 TEST_F(FileSystemProviderOperationsDeleteEntryTest
, Execute_NoListener
) {
88 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
89 util::StatusCallbackLog callback_log
;
91 DeleteEntry
delete_entry(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
93 base::Bind(&util::LogStatusCallback
, &callback_log
));
94 delete_entry
.SetDispatchEventImplForTesting(
95 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
96 base::Unretained(&dispatcher
)));
98 EXPECT_FALSE(delete_entry
.Execute(kRequestId
));
101 TEST_F(FileSystemProviderOperationsDeleteEntryTest
, Execute_ReadOnly
) {
102 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
103 util::StatusCallbackLog callback_log
;
105 const ProvidedFileSystemInfo
read_only_file_system_info(
106 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
107 base::FilePath() /* mount_path */, false /* configurable */,
108 true /* watchable */, extensions::SOURCE_FILE
);
110 DeleteEntry
delete_entry(NULL
, read_only_file_system_info
,
111 base::FilePath(kEntryPath
), true /* recursive */,
112 base::Bind(&util::LogStatusCallback
, &callback_log
));
113 delete_entry
.SetDispatchEventImplForTesting(
114 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
115 base::Unretained(&dispatcher
)));
117 EXPECT_FALSE(delete_entry
.Execute(kRequestId
));
120 TEST_F(FileSystemProviderOperationsDeleteEntryTest
, OnSuccess
) {
121 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
122 util::StatusCallbackLog callback_log
;
124 DeleteEntry
delete_entry(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
125 true /* recursive */,
126 base::Bind(&util::LogStatusCallback
, &callback_log
));
127 delete_entry
.SetDispatchEventImplForTesting(
128 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
129 base::Unretained(&dispatcher
)));
131 EXPECT_TRUE(delete_entry
.Execute(kRequestId
));
133 delete_entry
.OnSuccess(kRequestId
,
134 scoped_ptr
<RequestValue
>(new RequestValue()),
135 false /* has_more */);
136 ASSERT_EQ(1u, callback_log
.size());
137 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
140 TEST_F(FileSystemProviderOperationsDeleteEntryTest
, OnError
) {
141 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
142 util::StatusCallbackLog callback_log
;
144 DeleteEntry
delete_entry(NULL
, file_system_info_
, base::FilePath(kEntryPath
),
145 true /* recursive */,
146 base::Bind(&util::LogStatusCallback
, &callback_log
));
147 delete_entry
.SetDispatchEventImplForTesting(
148 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
149 base::Unretained(&dispatcher
)));
151 EXPECT_TRUE(delete_entry
.Execute(kRequestId
));
153 delete_entry
.OnError(kRequestId
,
154 scoped_ptr
<RequestValue
>(new RequestValue()),
155 base::File::FILE_ERROR_TOO_MANY_OPENED
);
156 ASSERT_EQ(1u, callback_log
.size());
157 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
160 } // namespace operations
161 } // namespace file_system_provider
162 } // namespace chromeos