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/copy_entry.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 base::FilePath::CharType kSourcePath
[] =
31 FILE_PATH_LITERAL("/bunny/and/bear/happy");
32 const base::FilePath::CharType kTargetPath
[] =
33 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
37 class FileSystemProviderOperationsCopyEntryTest
: public testing::Test
{
39 FileSystemProviderOperationsCopyEntryTest() {}
40 ~FileSystemProviderOperationsCopyEntryTest() override
{}
42 void SetUp() override
{
43 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
44 mount_options
.writable
= true;
45 file_system_info_
= ProvidedFileSystemInfo(
46 kExtensionId
, mount_options
, base::FilePath(), false /* configurable */,
47 true /* watchable */, extensions::SOURCE_FILE
);
50 ProvidedFileSystemInfo file_system_info_
;
53 TEST_F(FileSystemProviderOperationsCopyEntryTest
, Execute
) {
54 using extensions::api::file_system_provider::CopyEntryRequestedOptions
;
56 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
57 util::StatusCallbackLog callback_log
;
59 CopyEntry
copy_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
60 base::FilePath(kTargetPath
),
61 base::Bind(&util::LogStatusCallback
, &callback_log
));
62 copy_entry
.SetDispatchEventImplForTesting(
63 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
64 base::Unretained(&dispatcher
)));
66 EXPECT_TRUE(copy_entry
.Execute(kRequestId
));
68 ASSERT_EQ(1u, dispatcher
.events().size());
69 extensions::Event
* event
= dispatcher
.events()[0];
71 extensions::api::file_system_provider::OnCopyEntryRequested::kEventName
,
73 base::ListValue
* event_args
= event
->event_args
.get();
74 ASSERT_EQ(1u, event_args
->GetSize());
76 const base::DictionaryValue
* options_as_value
= NULL
;
77 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
79 CopyEntryRequestedOptions options
;
80 ASSERT_TRUE(CopyEntryRequestedOptions::Populate(*options_as_value
, &options
));
81 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
82 EXPECT_EQ(kRequestId
, options
.request_id
);
83 EXPECT_EQ(kSourcePath
, options
.source_path
);
84 EXPECT_EQ(kTargetPath
, options
.target_path
);
87 TEST_F(FileSystemProviderOperationsCopyEntryTest
, Execute_NoListener
) {
88 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
89 util::StatusCallbackLog callback_log
;
91 CopyEntry
copy_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
92 base::FilePath(kTargetPath
),
93 base::Bind(&util::LogStatusCallback
, &callback_log
));
94 copy_entry
.SetDispatchEventImplForTesting(
95 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
96 base::Unretained(&dispatcher
)));
98 EXPECT_FALSE(copy_entry
.Execute(kRequestId
));
101 TEST_F(FileSystemProviderOperationsCopyEntryTest
, 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 CopyEntry
copy_entry(NULL
, read_only_file_system_info
,
111 base::FilePath(kSourcePath
), base::FilePath(kTargetPath
),
112 base::Bind(&util::LogStatusCallback
, &callback_log
));
113 copy_entry
.SetDispatchEventImplForTesting(
114 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
115 base::Unretained(&dispatcher
)));
117 EXPECT_FALSE(copy_entry
.Execute(kRequestId
));
120 TEST_F(FileSystemProviderOperationsCopyEntryTest
, OnSuccess
) {
121 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
122 util::StatusCallbackLog callback_log
;
124 CopyEntry
copy_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
125 base::FilePath(kTargetPath
),
126 base::Bind(&util::LogStatusCallback
, &callback_log
));
127 copy_entry
.SetDispatchEventImplForTesting(
128 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
129 base::Unretained(&dispatcher
)));
131 EXPECT_TRUE(copy_entry
.Execute(kRequestId
));
133 copy_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(FileSystemProviderOperationsCopyEntryTest
, OnError
) {
141 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
142 util::StatusCallbackLog callback_log
;
144 CopyEntry
copy_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
145 base::FilePath(kTargetPath
),
146 base::Bind(&util::LogStatusCallback
, &callback_log
));
147 copy_entry
.SetDispatchEventImplForTesting(
148 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
149 base::Unretained(&dispatcher
)));
151 EXPECT_TRUE(copy_entry
.Execute(kRequestId
));
153 copy_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