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_internal.h"
18 #include "extensions/browser/event_router.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "webkit/browser/fileapi/async_file_util.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 kEntryPath[] = "/kitty/and/puppy/happy";
34 class FileSystemProviderOperationsDeleteEntryTest : public testing::Test {
36 FileSystemProviderOperationsDeleteEntryTest() {}
37 virtual ~FileSystemProviderOperationsDeleteEntryTest() {}
39 virtual void SetUp() OVERRIDE {
41 ProvidedFileSystemInfo(kExtensionId,
43 "" /* file_system_name */,
45 base::FilePath() /* mount_path */);
48 ProvidedFileSystemInfo file_system_info_;
51 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute) {
52 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
53 util::StatusCallbackLog callback_log;
55 DeleteEntry delete_entry(NULL,
57 base::FilePath::FromUTF8Unsafe(kEntryPath),
59 base::Bind(&util::LogStatusCallback, &callback_log));
60 delete_entry.SetDispatchEventImplForTesting(
61 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
62 base::Unretained(&dispatcher)));
64 EXPECT_TRUE(delete_entry.Execute(kRequestId));
66 ASSERT_EQ(1u, dispatcher.events().size());
67 extensions::Event* event = dispatcher.events()[0];
69 extensions::api::file_system_provider::OnDeleteEntryRequested::kEventName,
71 base::ListValue* event_args = event->event_args.get();
72 ASSERT_EQ(1u, event_args->GetSize());
74 base::DictionaryValue* options = NULL;
75 ASSERT_TRUE(event_args->GetDictionary(0, &options));
77 std::string event_file_system_id;
78 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
79 EXPECT_EQ(kFileSystemId, event_file_system_id);
81 int event_request_id = -1;
82 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
83 EXPECT_EQ(kRequestId, event_request_id);
85 std::string event_entry_path;
86 EXPECT_TRUE(options->GetString("entryPath", &event_entry_path));
87 EXPECT_EQ(kEntryPath, event_entry_path);
90 EXPECT_TRUE(options->GetBoolean("recursive", &event_recursive));
91 EXPECT_TRUE(event_recursive);
94 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_NoListener) {
95 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
96 util::StatusCallbackLog callback_log;
98 DeleteEntry delete_entry(NULL,
100 base::FilePath::FromUTF8Unsafe(kEntryPath),
101 true /* recursive */,
102 base::Bind(&util::LogStatusCallback, &callback_log));
103 delete_entry.SetDispatchEventImplForTesting(
104 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
105 base::Unretained(&dispatcher)));
107 EXPECT_FALSE(delete_entry.Execute(kRequestId));
110 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_ReadOnly) {
111 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
112 util::StatusCallbackLog callback_log;
114 const ProvidedFileSystemInfo read_only_file_system_info(
117 "" /* file_system_name */,
118 false /* writable */,
119 base::FilePath() /* mount_path */);
121 DeleteEntry delete_entry(NULL,
122 read_only_file_system_info,
123 base::FilePath::FromUTF8Unsafe(kEntryPath),
124 true /* recursive */,
125 base::Bind(&util::LogStatusCallback, &callback_log));
126 delete_entry.SetDispatchEventImplForTesting(
127 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
128 base::Unretained(&dispatcher)));
130 EXPECT_FALSE(delete_entry.Execute(kRequestId));
133 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnSuccess) {
134 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
135 util::StatusCallbackLog callback_log;
137 DeleteEntry delete_entry(NULL,
139 base::FilePath::FromUTF8Unsafe(kEntryPath),
140 true /* recursive */,
141 base::Bind(&util::LogStatusCallback, &callback_log));
142 delete_entry.SetDispatchEventImplForTesting(
143 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
144 base::Unretained(&dispatcher)));
146 EXPECT_TRUE(delete_entry.Execute(kRequestId));
148 delete_entry.OnSuccess(kRequestId,
149 scoped_ptr<RequestValue>(new RequestValue()),
150 false /* has_more */);
151 ASSERT_EQ(1u, callback_log.size());
152 EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
155 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnError) {
156 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
157 util::StatusCallbackLog callback_log;
159 DeleteEntry delete_entry(NULL,
161 base::FilePath::FromUTF8Unsafe(kEntryPath),
162 true /* recursive */,
163 base::Bind(&util::LogStatusCallback, &callback_log));
164 delete_entry.SetDispatchEventImplForTesting(
165 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
166 base::Unretained(&dispatcher)));
168 EXPECT_TRUE(delete_entry.Execute(kRequestId));
170 delete_entry.OnError(kRequestId,
171 scoped_ptr<RequestValue>(new RequestValue()),
172 base::File::FILE_ERROR_TOO_MANY_OPENED);
173 ASSERT_EQ(1u, callback_log.size());
174 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
177 } // namespace operations
178 } // namespace file_system_provider
179 } // namespace chromeos