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 "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_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[] = "/bunny/and/bear/happy";
31 const base::FilePath::CharType kTargetPath[] = "/kitty/and/puppy/happy";
35 class FileSystemProviderOperationsMoveEntryTest : public testing::Test {
37 FileSystemProviderOperationsMoveEntryTest() {}
38 virtual ~FileSystemProviderOperationsMoveEntryTest() {}
40 virtual void SetUp() OVERRIDE {
42 ProvidedFileSystemInfo(kExtensionId,
44 "" /* file_system_name */,
46 base::FilePath() /* mount_path */);
49 ProvidedFileSystemInfo file_system_info_;
52 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute) {
53 using extensions::api::file_system_provider::MoveEntryRequestedOptions;
55 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
56 util::StatusCallbackLog callback_log;
58 MoveEntry move_entry(NULL,
60 base::FilePath::FromUTF8Unsafe(kSourcePath),
61 base::FilePath::FromUTF8Unsafe(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,
94 base::FilePath::FromUTF8Unsafe(kSourcePath),
95 base::FilePath::FromUTF8Unsafe(kTargetPath),
96 base::Bind(&util::LogStatusCallback, &callback_log));
97 move_entry.SetDispatchEventImplForTesting(
98 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
99 base::Unretained(&dispatcher)));
101 EXPECT_FALSE(move_entry.Execute(kRequestId));
104 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute_ReadOnly) {
105 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
106 util::StatusCallbackLog callback_log;
108 const ProvidedFileSystemInfo read_only_file_system_info(
111 "" /* file_system_name */,
112 false /* writable */,
113 base::FilePath() /* mount_path */);
115 MoveEntry move_entry(NULL,
116 read_only_file_system_info,
117 base::FilePath::FromUTF8Unsafe(kSourcePath),
118 base::FilePath::FromUTF8Unsafe(kTargetPath),
119 base::Bind(&util::LogStatusCallback, &callback_log));
120 move_entry.SetDispatchEventImplForTesting(
121 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
122 base::Unretained(&dispatcher)));
124 EXPECT_FALSE(move_entry.Execute(kRequestId));
127 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnSuccess) {
128 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
129 util::StatusCallbackLog callback_log;
131 MoveEntry move_entry(NULL,
133 base::FilePath::FromUTF8Unsafe(kSourcePath),
134 base::FilePath::FromUTF8Unsafe(kTargetPath),
135 base::Bind(&util::LogStatusCallback, &callback_log));
136 move_entry.SetDispatchEventImplForTesting(
137 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
138 base::Unretained(&dispatcher)));
140 EXPECT_TRUE(move_entry.Execute(kRequestId));
142 move_entry.OnSuccess(kRequestId,
143 scoped_ptr<RequestValue>(new RequestValue()),
144 false /* has_more */);
145 ASSERT_EQ(1u, callback_log.size());
146 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
149 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnError) {
150 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
151 util::StatusCallbackLog callback_log;
153 MoveEntry move_entry(NULL,
155 base::FilePath::FromUTF8Unsafe(kSourcePath),
156 base::FilePath::FromUTF8Unsafe(kTargetPath),
157 base::Bind(&util::LogStatusCallback, &callback_log));
158 move_entry.SetDispatchEventImplForTesting(
159 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
160 base::Unretained(&dispatcher)));
162 EXPECT_TRUE(move_entry.Execute(kRequestId));
164 move_entry.OnError(kRequestId,
165 scoped_ptr<RequestValue>(new RequestValue()),
166 base::File::FILE_ERROR_TOO_MANY_OPENED);
167 ASSERT_EQ(1u, callback_log.size());
168 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
171 } // namespace operations
172 } // namespace file_system_provider
173 } // namespace chromeos