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 "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 kEntryPath[] = "/kitty/and/puppy/happy";
34 class FileSystemProviderOperationsDeleteEntryTest : public testing::Test {
36 FileSystemProviderOperationsDeleteEntryTest() {}
37 virtual ~FileSystemProviderOperationsDeleteEntryTest() {}
39 virtual void SetUp() override {
40 MountOptions mount_options(kFileSystemId, "" /* display_name */);
41 mount_options.writable = true;
43 ProvidedFileSystemInfo(kExtensionId, mount_options, base::FilePath());
46 ProvidedFileSystemInfo file_system_info_;
49 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute) {
50 using extensions::api::file_system_provider::DeleteEntryRequestedOptions;
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 const base::DictionaryValue* options_as_value = NULL;
75 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
77 DeleteEntryRequestedOptions options;
79 DeleteEntryRequestedOptions::Populate(*options_as_value, &options));
80 EXPECT_EQ(kFileSystemId, options.file_system_id);
81 EXPECT_EQ(kRequestId, options.request_id);
82 EXPECT_EQ(kEntryPath, options.entry_path);
83 EXPECT_TRUE(options.recursive);
86 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_NoListener) {
87 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88 util::StatusCallbackLog callback_log;
90 DeleteEntry delete_entry(NULL,
92 base::FilePath::FromUTF8Unsafe(kEntryPath),
94 base::Bind(&util::LogStatusCallback, &callback_log));
95 delete_entry.SetDispatchEventImplForTesting(
96 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
97 base::Unretained(&dispatcher)));
99 EXPECT_FALSE(delete_entry.Execute(kRequestId));
102 TEST_F(FileSystemProviderOperationsDeleteEntryTest, 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 DeleteEntry delete_entry(NULL,
112 read_only_file_system_info,
113 base::FilePath::FromUTF8Unsafe(kEntryPath),
114 true /* recursive */,
115 base::Bind(&util::LogStatusCallback, &callback_log));
116 delete_entry.SetDispatchEventImplForTesting(
117 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
118 base::Unretained(&dispatcher)));
120 EXPECT_FALSE(delete_entry.Execute(kRequestId));
123 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnSuccess) {
124 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125 util::StatusCallbackLog callback_log;
127 DeleteEntry delete_entry(NULL,
129 base::FilePath::FromUTF8Unsafe(kEntryPath),
130 true /* recursive */,
131 base::Bind(&util::LogStatusCallback, &callback_log));
132 delete_entry.SetDispatchEventImplForTesting(
133 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
134 base::Unretained(&dispatcher)));
136 EXPECT_TRUE(delete_entry.Execute(kRequestId));
138 delete_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(FileSystemProviderOperationsDeleteEntryTest, OnError) {
146 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
147 util::StatusCallbackLog callback_log;
149 DeleteEntry delete_entry(NULL,
151 base::FilePath::FromUTF8Unsafe(kEntryPath),
152 true /* recursive */,
153 base::Bind(&util::LogStatusCallback, &callback_log));
154 delete_entry.SetDispatchEventImplForTesting(
155 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
156 base::Unretained(&dispatcher)));
158 EXPECT_TRUE(delete_entry.Execute(kRequestId));
160 delete_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