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 {
41 MountOptions mount_options(kFileSystemId, "" /* display_name */);
42 mount_options.writable = true;
44 ProvidedFileSystemInfo(kExtensionId, mount_options, base::FilePath());
47 ProvidedFileSystemInfo file_system_info_;
50 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute) {
51 using extensions::api::file_system_provider::MoveEntryRequestedOptions;
53 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
54 util::StatusCallbackLog callback_log;
56 MoveEntry move_entry(NULL,
58 base::FilePath::FromUTF8Unsafe(kSourcePath),
59 base::FilePath::FromUTF8Unsafe(kTargetPath),
60 base::Bind(&util::LogStatusCallback, &callback_log));
61 move_entry.SetDispatchEventImplForTesting(
62 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63 base::Unretained(&dispatcher)));
65 EXPECT_TRUE(move_entry.Execute(kRequestId));
67 ASSERT_EQ(1u, dispatcher.events().size());
68 extensions::Event* event = dispatcher.events()[0];
70 extensions::api::file_system_provider::OnMoveEntryRequested::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 MoveEntryRequestedOptions options;
79 ASSERT_TRUE(MoveEntryRequestedOptions::Populate(*options_as_value, &options));
80 EXPECT_EQ(kFileSystemId, options.file_system_id);
81 EXPECT_EQ(kRequestId, options.request_id);
82 EXPECT_EQ(kSourcePath, options.source_path);
83 EXPECT_EQ(kTargetPath, options.target_path);
86 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute_NoListener) {
87 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88 util::StatusCallbackLog callback_log;
90 MoveEntry move_entry(NULL,
92 base::FilePath::FromUTF8Unsafe(kSourcePath),
93 base::FilePath::FromUTF8Unsafe(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(
108 MountOptions(kFileSystemId, "" /* display_name */),
109 base::FilePath() /* mount_path */);
111 MoveEntry move_entry(NULL,
112 read_only_file_system_info,
113 base::FilePath::FromUTF8Unsafe(kSourcePath),
114 base::FilePath::FromUTF8Unsafe(kTargetPath),
115 base::Bind(&util::LogStatusCallback, &callback_log));
116 move_entry.SetDispatchEventImplForTesting(
117 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
118 base::Unretained(&dispatcher)));
120 EXPECT_FALSE(move_entry.Execute(kRequestId));
123 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnSuccess) {
124 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125 util::StatusCallbackLog callback_log;
127 MoveEntry move_entry(NULL,
129 base::FilePath::FromUTF8Unsafe(kSourcePath),
130 base::FilePath::FromUTF8Unsafe(kTargetPath),
131 base::Bind(&util::LogStatusCallback, &callback_log));
132 move_entry.SetDispatchEventImplForTesting(
133 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
134 base::Unretained(&dispatcher)));
136 EXPECT_TRUE(move_entry.Execute(kRequestId));
138 move_entry.OnSuccess(kRequestId,
139 scoped_ptr<RequestValue>(new RequestValue()),
140 false /* has_more */);
141 ASSERT_EQ(1u, callback_log.size());
142 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
145 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnError) {
146 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
147 util::StatusCallbackLog callback_log;
149 MoveEntry move_entry(NULL,
151 base::FilePath::FromUTF8Unsafe(kSourcePath),
152 base::FilePath::FromUTF8Unsafe(kTargetPath),
153 base::Bind(&util::LogStatusCallback, &callback_log));
154 move_entry.SetDispatchEventImplForTesting(
155 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
156 base::Unretained(&dispatcher)));
158 EXPECT_TRUE(move_entry.Execute(kRequestId));
160 move_entry.OnError(kRequestId,
161 scoped_ptr<RequestValue>(new RequestValue()),
162 base::File::FILE_ERROR_TOO_MANY_OPENED);
163 ASSERT_EQ(1u, callback_log.size());
164 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
167 } // namespace operations
168 } // namespace file_system_provider
169 } // namespace chromeos