Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_system_provider / operations / read_file_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/read_file.h"
6
7 #include <string>
8
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.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 "net/base/io_buffer.h"
20 #include "storage/browser/fileapi/async_file_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace chromeos {
24 namespace file_system_provider {
25 namespace operations {
26 namespace {
27
28 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
29 const char kFileSystemId[] = "testing-file-system";
30 const int kRequestId = 2;
31 const int kFileHandle = 3;
32 const int kOffset = 10;
33 const int kLength = 5;
34
35 // Callback invocation logger. Acts as a fileapi end-point.
36 class CallbackLogger {
37  public:
38   class Event {
39    public:
40     Event(int chunk_length, bool has_more, base::File::Error result)
41         : chunk_length_(chunk_length), has_more_(has_more), result_(result) {}
42     virtual ~Event() {}
43
44     int chunk_length() const { return chunk_length_; }
45     bool has_more() const { return has_more_; }
46     base::File::Error result() const { return result_; }
47
48    private:
49     int chunk_length_;
50     bool has_more_;
51     base::File::Error result_;
52
53     DISALLOW_COPY_AND_ASSIGN(Event);
54   };
55
56   CallbackLogger() {}
57   virtual ~CallbackLogger() {}
58
59   void OnReadFile(int chunk_length, bool has_more, base::File::Error result) {
60     events_.push_back(new Event(chunk_length, has_more, result));
61   }
62
63   ScopedVector<Event>& events() { return events_; }
64
65  private:
66   ScopedVector<Event> events_;
67   bool dispatch_reply_;
68
69   DISALLOW_COPY_AND_ASSIGN(CallbackLogger);
70 };
71
72 }  // namespace
73
74 class FileSystemProviderOperationsReadFileTest : public testing::Test {
75  protected:
76   FileSystemProviderOperationsReadFileTest() {}
77   virtual ~FileSystemProviderOperationsReadFileTest() {}
78
79   virtual void SetUp() override {
80     file_system_info_ = ProvidedFileSystemInfo(
81         kExtensionId,
82         MountOptions(kFileSystemId, "" /* display_name */),
83         base::FilePath());
84     io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength));
85   }
86
87   ProvidedFileSystemInfo file_system_info_;
88   scoped_refptr<net::IOBuffer> io_buffer_;
89 };
90
91 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) {
92   using extensions::api::file_system_provider::ReadFileRequestedOptions;
93
94   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
95   CallbackLogger callback_logger;
96
97   ReadFile read_file(NULL,
98                      file_system_info_,
99                      kFileHandle,
100                      io_buffer_.get(),
101                      kOffset,
102                      kLength,
103                      base::Bind(&CallbackLogger::OnReadFile,
104                                 base::Unretained(&callback_logger)));
105   read_file.SetDispatchEventImplForTesting(
106       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
107                  base::Unretained(&dispatcher)));
108
109   EXPECT_TRUE(read_file.Execute(kRequestId));
110
111   ASSERT_EQ(1u, dispatcher.events().size());
112   extensions::Event* event = dispatcher.events()[0];
113   EXPECT_EQ(
114       extensions::api::file_system_provider::OnReadFileRequested::kEventName,
115       event->event_name);
116   base::ListValue* event_args = event->event_args.get();
117   ASSERT_EQ(1u, event_args->GetSize());
118
119   const base::DictionaryValue* options_as_value = NULL;
120   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
121
122   ReadFileRequestedOptions options;
123   ASSERT_TRUE(ReadFileRequestedOptions::Populate(*options_as_value, &options));
124   EXPECT_EQ(kFileSystemId, options.file_system_id);
125   EXPECT_EQ(kRequestId, options.request_id);
126   EXPECT_EQ(kFileHandle, options.open_request_id);
127   EXPECT_EQ(kOffset, static_cast<double>(options.offset));
128   EXPECT_EQ(kLength, options.length);
129 }
130
131 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) {
132   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
133   CallbackLogger callback_logger;
134
135   ReadFile read_file(NULL,
136                      file_system_info_,
137                      kFileHandle,
138                      io_buffer_.get(),
139                      kOffset,
140                      kLength,
141                      base::Bind(&CallbackLogger::OnReadFile,
142                                 base::Unretained(&callback_logger)));
143   read_file.SetDispatchEventImplForTesting(
144       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
145                  base::Unretained(&dispatcher)));
146
147   EXPECT_FALSE(read_file.Execute(kRequestId));
148 }
149
150 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) {
151   using extensions::api::file_system_provider_internal::
152       ReadFileRequestedSuccess::Params;
153
154   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
155   CallbackLogger callback_logger;
156
157   ReadFile read_file(NULL,
158                      file_system_info_,
159                      kFileHandle,
160                      io_buffer_.get(),
161                      kOffset,
162                      kLength,
163                      base::Bind(&CallbackLogger::OnReadFile,
164                                 base::Unretained(&callback_logger)));
165   read_file.SetDispatchEventImplForTesting(
166       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
167                  base::Unretained(&dispatcher)));
168
169   EXPECT_TRUE(read_file.Execute(kRequestId));
170
171   const std::string data = "ABCDE";
172   const bool has_more = false;
173   const int execution_time = 0;
174
175   base::ListValue value_as_list;
176   value_as_list.Set(0, new base::StringValue(kFileSystemId));
177   value_as_list.Set(1, new base::FundamentalValue(kRequestId));
178   value_as_list.Set(
179       2, base::BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size()));
180   value_as_list.Set(3, new base::FundamentalValue(has_more));
181   value_as_list.Set(4, new base::FundamentalValue(execution_time));
182
183   scoped_ptr<Params> params(Params::Create(value_as_list));
184   ASSERT_TRUE(params.get());
185   scoped_ptr<RequestValue> request_value(
186       RequestValue::CreateForReadFileSuccess(params.Pass()));
187   ASSERT_TRUE(request_value.get());
188
189   read_file.OnSuccess(kRequestId, request_value.Pass(), has_more);
190
191   ASSERT_EQ(1u, callback_logger.events().size());
192   CallbackLogger::Event* event = callback_logger.events()[0];
193   EXPECT_EQ(kLength, event->chunk_length());
194   EXPECT_FALSE(event->has_more());
195   EXPECT_EQ(data, std::string(io_buffer_->data(), kLength));
196   EXPECT_EQ(base::File::FILE_OK, event->result());
197 }
198
199 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) {
200   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
201   CallbackLogger callback_logger;
202
203   ReadFile read_file(NULL,
204                      file_system_info_,
205                      kFileHandle,
206                      io_buffer_.get(),
207                      kOffset,
208                      kLength,
209                      base::Bind(&CallbackLogger::OnReadFile,
210                                 base::Unretained(&callback_logger)));
211   read_file.SetDispatchEventImplForTesting(
212       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
213                  base::Unretained(&dispatcher)));
214
215   EXPECT_TRUE(read_file.Execute(kRequestId));
216
217   read_file.OnError(kRequestId,
218                     scoped_ptr<RequestValue>(new RequestValue()),
219                     base::File::FILE_ERROR_TOO_MANY_OPENED);
220
221   ASSERT_EQ(1u, callback_logger.events().size());
222   CallbackLogger::Event* event = callback_logger.events()[0];
223   EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
224 }
225
226 }  // namespace operations
227 }  // namespace file_system_provider
228 }  // namespace chromeos