Update To 11.40.268.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 "storage/browser/fileapi/async_file_util.h"
20 #include "testing/gtest/include/gtest/gtest.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     MountOptions mount_options(kFileSystemId, "" /* display_name */);
41     mount_options.writable = true;
42     file_system_info_ =
43         ProvidedFileSystemInfo(kExtensionId, mount_options, base::FilePath());
44   }
45
46   ProvidedFileSystemInfo file_system_info_;
47 };
48
49 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute) {
50   using extensions::api::file_system_provider::DeleteEntryRequestedOptions;
51
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   const base::DictionaryValue* options_as_value = NULL;
75   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
76
77   DeleteEntryRequestedOptions options;
78   ASSERT_TRUE(
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);
84 }
85
86 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_NoListener) {
87   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88   util::StatusCallbackLog callback_log;
89
90   DeleteEntry delete_entry(NULL,
91                            file_system_info_,
92                            base::FilePath::FromUTF8Unsafe(kEntryPath),
93                            true /* recursive */,
94                            base::Bind(&util::LogStatusCallback, &callback_log));
95   delete_entry.SetDispatchEventImplForTesting(
96       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
97                  base::Unretained(&dispatcher)));
98
99   EXPECT_FALSE(delete_entry.Execute(kRequestId));
100 }
101
102 TEST_F(FileSystemProviderOperationsDeleteEntryTest, Execute_ReadOnly) {
103   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
104   util::StatusCallbackLog callback_log;
105
106   const ProvidedFileSystemInfo read_only_file_system_info(
107       kExtensionId,
108       MountOptions(kFileSystemId, "" /* display_name */),
109       base::FilePath() /* mount_path */);
110
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)));
119
120   EXPECT_FALSE(delete_entry.Execute(kRequestId));
121 }
122
123 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnSuccess) {
124   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125   util::StatusCallbackLog callback_log;
126
127   DeleteEntry delete_entry(NULL,
128                            file_system_info_,
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)));
135
136   EXPECT_TRUE(delete_entry.Execute(kRequestId));
137
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]);
143 }
144
145 TEST_F(FileSystemProviderOperationsDeleteEntryTest, OnError) {
146   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
147   util::StatusCallbackLog callback_log;
148
149   DeleteEntry delete_entry(NULL,
150                            file_system_info_,
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)));
157
158   EXPECT_TRUE(delete_entry.Execute(kRequestId));
159
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]);
165 }
166
167 }  // namespace operations
168 }  // namespace file_system_provider
169 }  // namespace chromeos