Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / operations / delete_entry_unittest.cc
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 "testing/gtest/include/gtest/gtest.h"
20 #include "webkit/browser/fileapi/async_file_util.h"
21
22 namespace chromeos {
23 namespace file_system_provider {
24 namespace operations {
25 namespace {
26
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";
31
32 }  // namespace
33
34 class 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
51 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute) {
52   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
53   util::StatusCallbackLog callback_log;
54
55   DeleteEntry delete_entry(NULL,
56                            file_system_info_,
57                            base::FilePath::FromUTF8Unsafe(kEntryPath),
58                            true /* recursive */,
59                            base::Bind(&util::LogStatusCallback, &callback_log));
60   delete_entry.SetDispatchEventImplForTesting(
61       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
62                  base::Unretained(&dispatcher)));
63
64   EXPECT_TRUE(delete_entry.Execute(kRequestId));
65
66   ASSERT_EQ(1u, dispatcher.events().size());
67   extensions::Event* event = dispatcher.events()[0];
68   EXPECT_EQ(
69       extensions::api::file_system_provider::OnDeleteEntryRequested::kEventName,
70       event->event_name);
71   base::ListValue* event_args = event->event_args.get();
72   ASSERT_EQ(1u, event_args->GetSize());
73
74   base::DictionaryValue* options = NULL;
75   ASSERT_TRUE(event_args->GetDictionary(0, &options));
76
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);
80
81   int event_request_id = -1;
82   EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
83   EXPECT_EQ(kRequestId, event_request_id);
84
85   std::string event_entry_path;
86   EXPECT_TRUE(options->GetString("entryPath", &event_entry_path));
87   EXPECT_EQ(kEntryPath, event_entry_path);
88
89   bool event_recursive;
90   EXPECT_TRUE(options->GetBoolean("recursive", &event_recursive));
91   EXPECT_TRUE(event_recursive);
92 }
93
94 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_NoListener) {
95   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
96   util::StatusCallbackLog callback_log;
97
98   DeleteEntry delete_entry(NULL,
99                            file_system_info_,
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)));
106
107   EXPECT_FALSE(delete_entry.Execute(kRequestId));
108 }
109
110 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_ReadOnly) {
111   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
112   util::StatusCallbackLog callback_log;
113
114   const ProvidedFileSystemInfo read_only_file_system_info(
115       kExtensionId,
116       kFileSystemId,
117       "" /* file_system_name */,
118       false /* writable */,
119       base::FilePath() /* mount_path */);
120
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)));
129
130   EXPECT_FALSE(delete_entry.Execute(kRequestId));
131 }
132
133 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnSuccess) {
134   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
135   util::StatusCallbackLog callback_log;
136
137   DeleteEntry delete_entry(NULL,
138                            file_system_info_,
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)));
145
146   EXPECT_TRUE(delete_entry.Execute(kRequestId));
147
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]);
153 }
154
155 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnError) {
156   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
157   util::StatusCallbackLog callback_log;
158
159   DeleteEntry delete_entry(NULL,
160                            file_system_info_,
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)));
167
168   EXPECT_TRUE(delete_entry.Execute(kRequestId));
169
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]);
175 }
176
177 }  // namespace operations
178 }  // namespace file_system_provider
179 }  // namespace chromeos