857cbf4fbb35fc91b083b32e7c81448c16290566
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / operations / move_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/move_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 kSourcePath[] = "/bunny/and/bear/happy";
31 const base::FilePath::CharType kTargetPath[] = "/kitty/and/puppy/happy";
32
33 }  // namespace
34
35 class FileSystemProviderOperationsMoveEntryTest : public testing::Test {
36  protected:
37   FileSystemProviderOperationsMoveEntryTest() {}
38   virtual ~FileSystemProviderOperationsMoveEntryTest() {}
39
40   virtual void SetUp() OVERRIDE {
41     file_system_info_ =
42         ProvidedFileSystemInfo(kExtensionId,
43                                kFileSystemId,
44                                "" /* file_system_name */,
45                                true /* writable */,
46                                base::FilePath() /* mount_path */);
47   }
48
49   ProvidedFileSystemInfo file_system_info_;
50 };
51
52 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute) {
53   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
54   util::StatusCallbackLog callback_log;
55
56   MoveEntry move_entry(NULL,
57                        file_system_info_,
58                        base::FilePath::FromUTF8Unsafe(kSourcePath),
59                        base::FilePath::FromUTF8Unsafe(kTargetPath),
60                        base::Bind(&util::LogStatusCallback, &callback_log));
61   move_entry.SetDispatchEventImplForTesting(
62       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
63                  base::Unretained(&dispatcher)));
64
65   EXPECT_TRUE(move_entry.Execute(kRequestId));
66
67   ASSERT_EQ(1u, dispatcher.events().size());
68   extensions::Event* event = dispatcher.events()[0];
69   EXPECT_EQ(
70       extensions::api::file_system_provider::OnMoveEntryRequested::kEventName,
71       event->event_name);
72   base::ListValue* event_args = event->event_args.get();
73   ASSERT_EQ(1u, event_args->GetSize());
74
75   base::DictionaryValue* options = NULL;
76   ASSERT_TRUE(event_args->GetDictionary(0, &options));
77
78   std::string event_file_system_id;
79   EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id));
80   EXPECT_EQ(kFileSystemId, event_file_system_id);
81
82   int event_request_id = -1;
83   EXPECT_TRUE(options->GetInteger("requestId", &event_request_id));
84   EXPECT_EQ(kRequestId, event_request_id);
85
86   std::string event_source_path;
87   EXPECT_TRUE(options->GetString("sourcePath", &event_source_path));
88   EXPECT_EQ(kSourcePath, event_source_path);
89
90   std::string event_target_path;
91   EXPECT_TRUE(options->GetString("targetPath", &event_target_path));
92   EXPECT_EQ(kTargetPath, event_target_path);
93 }
94
95 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute_NoListener) {
96   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
97   util::StatusCallbackLog callback_log;
98
99   MoveEntry move_entry(NULL,
100                        file_system_info_,
101                        base::FilePath::FromUTF8Unsafe(kSourcePath),
102                        base::FilePath::FromUTF8Unsafe(kTargetPath),
103                        base::Bind(&util::LogStatusCallback, &callback_log));
104   move_entry.SetDispatchEventImplForTesting(
105       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
106                  base::Unretained(&dispatcher)));
107
108   EXPECT_FALSE(move_entry.Execute(kRequestId));
109 }
110
111 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute_ReadOnly) {
112   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
113   util::StatusCallbackLog callback_log;
114
115   const ProvidedFileSystemInfo read_only_file_system_info(
116       kExtensionId,
117       kFileSystemId,
118       "" /* file_system_name */,
119       false /* writable */,
120       base::FilePath() /* mount_path */);
121
122   MoveEntry move_entry(NULL,
123                        read_only_file_system_info,
124                        base::FilePath::FromUTF8Unsafe(kSourcePath),
125                        base::FilePath::FromUTF8Unsafe(kTargetPath),
126                        base::Bind(&util::LogStatusCallback, &callback_log));
127   move_entry.SetDispatchEventImplForTesting(
128       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
129                  base::Unretained(&dispatcher)));
130
131   EXPECT_FALSE(move_entry.Execute(kRequestId));
132 }
133
134 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnSuccess) {
135   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
136   util::StatusCallbackLog callback_log;
137
138   MoveEntry move_entry(NULL,
139                        file_system_info_,
140                        base::FilePath::FromUTF8Unsafe(kSourcePath),
141                        base::FilePath::FromUTF8Unsafe(kTargetPath),
142                        base::Bind(&util::LogStatusCallback, &callback_log));
143   move_entry.SetDispatchEventImplForTesting(
144       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
145                  base::Unretained(&dispatcher)));
146
147   EXPECT_TRUE(move_entry.Execute(kRequestId));
148
149   move_entry.OnSuccess(kRequestId,
150                        scoped_ptr<RequestValue>(new RequestValue()),
151                        false /* has_more */);
152   ASSERT_EQ(1u, callback_log.size());
153   EXPECT_EQ(base::File::FILE_OK, callback_log[0]);
154 }
155
156 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnError) {
157   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
158   util::StatusCallbackLog callback_log;
159
160   MoveEntry move_entry(NULL,
161                        file_system_info_,
162                        base::FilePath::FromUTF8Unsafe(kSourcePath),
163                        base::FilePath::FromUTF8Unsafe(kTargetPath),
164                        base::Bind(&util::LogStatusCallback, &callback_log));
165   move_entry.SetDispatchEventImplForTesting(
166       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
167                  base::Unretained(&dispatcher)));
168
169   EXPECT_TRUE(move_entry.Execute(kRequestId));
170
171   move_entry.OnError(kRequestId,
172                      scoped_ptr<RequestValue>(new RequestValue()),
173                      base::File::FILE_ERROR_TOO_MANY_OPENED);
174   ASSERT_EQ(1u, callback_log.size());
175   EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]);
176 }
177
178 }  // namespace operations
179 }  // namespace file_system_provider
180 }  // namespace chromeos