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.
4
5#include "chrome/browser/chromeos/file_system_provider/operations/delete_entry.h"
6
7#include <string>
8#include <vector>
9
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"
21
22namespace chromeos {
23namespace file_system_provider {
24namespace operations {
25namespace {
26
27const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
28const char kFileSystemId[] = "testing-file-system";
29const int kRequestId = 2;
30const base::FilePath::CharType kEntryPath[] = "/kitty/and/puppy/happy";
31
32}  // namespace
33
34class FileSystemProviderOperationsDeleteEntryTest : public testing::Test {
35 protected:
36  FileSystemProviderOperationsDeleteEntryTest() {}
37  virtual ~FileSystemProviderOperationsDeleteEntryTest() {}
38
39  virtual void SetUp() OVERRIDE {
40    file_system_info_ =
41        ProvidedFileSystemInfo(kExtensionId,
42                               kFileSystemId,
43                               "" /* file_system_name */,
44                               true /* writable */,
45                               base::FilePath() /* mount_path */);
46  }
47
48  ProvidedFileSystemInfo file_system_info_;
49};
50
51TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute) {
52  using extensions::api::file_system_provider::DeleteEntryRequestedOptions;
53
54  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
55  util::StatusCallbackLog callback_log;
56
57  DeleteEntry delete_entry(NULL,
58                           file_system_info_,
59                           base::FilePath::FromUTF8Unsafe(kEntryPath),
60                           true /* recursive */,
61                           base::Bind(&util::LogStatusCallback, &callback_log));
62  delete_entry.SetDispatchEventImplForTesting(
63      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
64                 base::Unretained(&dispatcher)));
65
66  EXPECT_TRUE(delete_entry.Execute(kRequestId));
67
68  ASSERT_EQ(1u, dispatcher.events().size());
69  extensions::Event* event = dispatcher.events()[0];
70  EXPECT_EQ(
71      extensions::api::file_system_provider::OnDeleteEntryRequested::kEventName,
72      event->event_name);
73  base::ListValue* event_args = event->event_args.get();
74  ASSERT_EQ(1u, event_args->GetSize());
75
76  const base::DictionaryValue* options_as_value = NULL;
77  ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
78
79  DeleteEntryRequestedOptions options;
80  ASSERT_TRUE(
81      DeleteEntryRequestedOptions::Populate(*options_as_value, &options));
82  EXPECT_EQ(kFileSystemId, options.file_system_id);
83  EXPECT_EQ(kRequestId, options.request_id);
84  EXPECT_EQ(kEntryPath, options.entry_path);
85  EXPECT_TRUE(options.recursive);
86}
87
88TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_NoListener) {
89  util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
90  util::StatusCallbackLog callback_log;
91
92  DeleteEntry delete_entry(NULL,
93                           file_system_info_,
94                           base::FilePath::FromUTF8Unsafe(kEntryPath),
95                           true /* recursive */,
96                           base::Bind(&util::LogStatusCallback, &callback_log));
97  delete_entry.SetDispatchEventImplForTesting(
98      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
99                 base::Unretained(&dispatcher)));
100
101  EXPECT_FALSE(delete_entry.Execute(kRequestId));
102}
103
104TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_ReadOnly) {
105  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
106  util::StatusCallbackLog callback_log;
107
108  const ProvidedFileSystemInfo read_only_file_system_info(
109      kExtensionId,
110      kFileSystemId,
111      "" /* file_system_name */,
112      false /* writable */,
113      base::FilePath() /* mount_path */);
114
115  DeleteEntry delete_entry(NULL,
116                           read_only_file_system_info,
117                           base::FilePath::FromUTF8Unsafe(kEntryPath),
118                           true /* recursive */,
119                           base::Bind(&util::LogStatusCallback, &callback_log));
120  delete_entry.SetDispatchEventImplForTesting(
121      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
122                 base::Unretained(&dispatcher)));
123
124  EXPECT_FALSE(delete_entry.Execute(kRequestId));
125}
126
127TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnSuccess) {
128  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
129  util::StatusCallbackLog callback_log;
130
131  DeleteEntry delete_entry(NULL,
132                           file_system_info_,
133                           base::FilePath::FromUTF8Unsafe(kEntryPath),
134                           true /* recursive */,
135                           base::Bind(&util::LogStatusCallback, &callback_log));
136  delete_entry.SetDispatchEventImplForTesting(
137      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
138                 base::Unretained(&dispatcher)));
139
140  EXPECT_TRUE(delete_entry.Execute(kRequestId));
141
142  delete_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]);
147}
148
149TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnError) {
150  util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
151  util::StatusCallbackLog callback_log;
152
153  DeleteEntry delete_entry(NULL,
154                           file_system_info_,
155                           base::FilePath::FromUTF8Unsafe(kEntryPath),
156                           true /* recursive */,
157                           base::Bind(&util::LogStatusCallback, &callback_log));
158  delete_entry.SetDispatchEventImplForTesting(
159      base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
160                 base::Unretained(&dispatcher)));
161
162  EXPECT_TRUE(delete_entry.Execute(kRequestId));
163
164  delete_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]);
169}
170
171}  // namespace operations
172}  // namespace file_system_provider
173}  // namespace chromeos
174