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/move_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_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 kSourcePath
[] =
32 FILE_PATH_LITERAL("/bunny/and/bear/happy");
33 const base::FilePath::CharType kTargetPath
[] =
34 FILE_PATH_LITERAL("/kitty/and/puppy/happy");
38 class FileSystemProviderOperationsMoveEntryTest
: public testing::Test
{
40 FileSystemProviderOperationsMoveEntryTest() {}
41 ~FileSystemProviderOperationsMoveEntryTest() override
{}
43 void SetUp() override
{
44 MountOptions
mount_options(kFileSystemId
, "" /* display_name */);
45 mount_options
.writable
= true;
46 file_system_info_
= ProvidedFileSystemInfo(
47 kExtensionId
, mount_options
, base::FilePath(), false /* configurable */,
48 true /* watchable */, extensions::SOURCE_FILE
);
51 ProvidedFileSystemInfo file_system_info_
;
54 TEST_F(FileSystemProviderOperationsMoveEntryTest
, Execute
) {
55 using extensions::api::file_system_provider::MoveEntryRequestedOptions
;
57 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
58 util::StatusCallbackLog callback_log
;
60 MoveEntry
move_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
61 base::FilePath(kTargetPath
),
62 base::Bind(&util::LogStatusCallback
, &callback_log
));
63 move_entry
.SetDispatchEventImplForTesting(
64 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
65 base::Unretained(&dispatcher
)));
67 EXPECT_TRUE(move_entry
.Execute(kRequestId
));
69 ASSERT_EQ(1u, dispatcher
.events().size());
70 extensions::Event
* event
= dispatcher
.events()[0];
72 extensions::api::file_system_provider::OnMoveEntryRequested::kEventName
,
74 base::ListValue
* event_args
= event
->event_args
.get();
75 ASSERT_EQ(1u, event_args
->GetSize());
77 const base::DictionaryValue
* options_as_value
= NULL
;
78 ASSERT_TRUE(event_args
->GetDictionary(0, &options_as_value
));
80 MoveEntryRequestedOptions options
;
81 ASSERT_TRUE(MoveEntryRequestedOptions::Populate(*options_as_value
, &options
));
82 EXPECT_EQ(kFileSystemId
, options
.file_system_id
);
83 EXPECT_EQ(kRequestId
, options
.request_id
);
84 EXPECT_EQ(kSourcePath
, options
.source_path
);
85 EXPECT_EQ(kTargetPath
, options
.target_path
);
88 TEST_F(FileSystemProviderOperationsMoveEntryTest
, Execute_NoListener
) {
89 util::LoggingDispatchEventImpl
dispatcher(false /* dispatch_reply */);
90 util::StatusCallbackLog callback_log
;
92 MoveEntry
move_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
93 base::FilePath(kTargetPath
),
94 base::Bind(&util::LogStatusCallback
, &callback_log
));
95 move_entry
.SetDispatchEventImplForTesting(
96 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
97 base::Unretained(&dispatcher
)));
99 EXPECT_FALSE(move_entry
.Execute(kRequestId
));
102 TEST_F(FileSystemProviderOperationsMoveEntryTest
, Execute_ReadOnly
) {
103 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
104 util::StatusCallbackLog callback_log
;
106 const ProvidedFileSystemInfo
read_only_file_system_info(
107 kExtensionId
, MountOptions(kFileSystemId
, "" /* display_name */),
108 base::FilePath() /* mount_path */, false /* configurable */,
109 true /* watchable */, extensions::SOURCE_FILE
);
111 MoveEntry
move_entry(NULL
, read_only_file_system_info
,
112 base::FilePath(kSourcePath
), base::FilePath(kTargetPath
),
113 base::Bind(&util::LogStatusCallback
, &callback_log
));
114 move_entry
.SetDispatchEventImplForTesting(
115 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
116 base::Unretained(&dispatcher
)));
118 EXPECT_FALSE(move_entry
.Execute(kRequestId
));
121 TEST_F(FileSystemProviderOperationsMoveEntryTest
, OnSuccess
) {
122 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
123 util::StatusCallbackLog callback_log
;
125 MoveEntry
move_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
126 base::FilePath(kTargetPath
),
127 base::Bind(&util::LogStatusCallback
, &callback_log
));
128 move_entry
.SetDispatchEventImplForTesting(
129 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
130 base::Unretained(&dispatcher
)));
132 EXPECT_TRUE(move_entry
.Execute(kRequestId
));
134 move_entry
.OnSuccess(kRequestId
,
135 scoped_ptr
<RequestValue
>(new RequestValue()),
136 false /* has_more */);
137 ASSERT_EQ(1u, callback_log
.size());
138 EXPECT_EQ(base::File::FILE_OK
, callback_log
[0]);
141 TEST_F(FileSystemProviderOperationsMoveEntryTest
, OnError
) {
142 util::LoggingDispatchEventImpl
dispatcher(true /* dispatch_reply */);
143 util::StatusCallbackLog callback_log
;
145 MoveEntry
move_entry(NULL
, file_system_info_
, base::FilePath(kSourcePath
),
146 base::FilePath(kTargetPath
),
147 base::Bind(&util::LogStatusCallback
, &callback_log
));
148 move_entry
.SetDispatchEventImplForTesting(
149 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl
,
150 base::Unretained(&dispatcher
)));
152 EXPECT_TRUE(move_entry
.Execute(kRequestId
));
154 move_entry
.OnError(kRequestId
,
155 scoped_ptr
<RequestValue
>(new RequestValue()),
156 base::File::FILE_ERROR_TOO_MANY_OPENED
);
157 ASSERT_EQ(1u, callback_log
.size());
158 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED
, callback_log
[0]);
161 } // namespace operations
162 } // namespace file_system_provider
163 } // namespace chromeos