Update To 11.40.268.0
[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 "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 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     MountOptions mount_options(kFileSystemId, "" /* display_name */);
42     mount_options.writable = true;
43     file_system_info_ =
44         ProvidedFileSystemInfo(kExtensionId, mount_options, base::FilePath());
45   }
46
47   ProvidedFileSystemInfo file_system_info_;
48 };
49
50 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute) {
51   using extensions::api::file_system_provider::MoveEntryRequestedOptions;
52
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   const base::DictionaryValue* options_as_value = NULL;
76   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
77
78   MoveEntryRequestedOptions options;
79   ASSERT_TRUE(MoveEntryRequestedOptions::Populate(*options_as_value, &options));
80   EXPECT_EQ(kFileSystemId, options.file_system_id);
81   EXPECT_EQ(kRequestId, options.request_id);
82   EXPECT_EQ(kSourcePath, options.source_path);
83   EXPECT_EQ(kTargetPath, options.target_path);
84 }
85
86 TEST_F(FileSystemProviderOperationsMoveEntryTest, Execute_NoListener) {
87   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
88   util::StatusCallbackLog callback_log;
89
90   MoveEntry move_entry(NULL,
91                        file_system_info_,
92                        base::FilePath::FromUTF8Unsafe(kSourcePath),
93                        base::FilePath::FromUTF8Unsafe(kTargetPath),
94                        base::Bind(&util::LogStatusCallback, &callback_log));
95   move_entry.SetDispatchEventImplForTesting(
96       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
97                  base::Unretained(&dispatcher)));
98
99   EXPECT_FALSE(move_entry.Execute(kRequestId));
100 }
101
102 TEST_F(FileSystemProviderOperationsMoveEntryTest, 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   MoveEntry move_entry(NULL,
112                        read_only_file_system_info,
113                        base::FilePath::FromUTF8Unsafe(kSourcePath),
114                        base::FilePath::FromUTF8Unsafe(kTargetPath),
115                        base::Bind(&util::LogStatusCallback, &callback_log));
116   move_entry.SetDispatchEventImplForTesting(
117       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
118                  base::Unretained(&dispatcher)));
119
120   EXPECT_FALSE(move_entry.Execute(kRequestId));
121 }
122
123 TEST_F(FileSystemProviderOperationsMoveEntryTest, OnSuccess) {
124   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
125   util::StatusCallbackLog callback_log;
126
127   MoveEntry move_entry(NULL,
128                        file_system_info_,
129                        base::FilePath::FromUTF8Unsafe(kSourcePath),
130                        base::FilePath::FromUTF8Unsafe(kTargetPath),
131                        base::Bind(&util::LogStatusCallback, &callback_log));
132   move_entry.SetDispatchEventImplForTesting(
133       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
134                  base::Unretained(&dispatcher)));
135
136   EXPECT_TRUE(move_entry.Execute(kRequestId));
137
138   move_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(FileSystemProviderOperationsMoveEntryTest, OnError) {
146   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
147   util::StatusCallbackLog callback_log;
148
149   MoveEntry move_entry(NULL,
150                        file_system_info_,
151                        base::FilePath::FromUTF8Unsafe(kSourcePath),
152                        base::FilePath::FromUTF8Unsafe(kTargetPath),
153                        base::Bind(&util::LogStatusCallback, &callback_log));
154   move_entry.SetDispatchEventImplForTesting(
155       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
156                  base::Unretained(&dispatcher)));
157
158   EXPECT_TRUE(move_entry.Execute(kRequestId));
159
160   move_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