Upstream version 10.39.225.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_ =
81         ProvidedFileSystemInfo(kExtensionId,
82                                kFileSystemId,
83                                "" /* display_name */,
84                                false /* writable */,
85                                base::FilePath() /* mount_path */);
86     io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength));
87   }
88
89   ProvidedFileSystemInfo file_system_info_;
90   scoped_refptr<net::IOBuffer> io_buffer_;
91 };
92
93 TEST_F(FileSystemProviderOperationsReadFileTest, Execute) {
94   using extensions::api::file_system_provider::ReadFileRequestedOptions;
95
96   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
97   CallbackLogger callback_logger;
98
99   ReadFile read_file(NULL,
100                      file_system_info_,
101                      kFileHandle,
102                      io_buffer_.get(),
103                      kOffset,
104                      kLength,
105                      base::Bind(&CallbackLogger::OnReadFile,
106                                 base::Unretained(&callback_logger)));
107   read_file.SetDispatchEventImplForTesting(
108       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
109                  base::Unretained(&dispatcher)));
110
111   EXPECT_TRUE(read_file.Execute(kRequestId));
112
113   ASSERT_EQ(1u, dispatcher.events().size());
114   extensions::Event* event = dispatcher.events()[0];
115   EXPECT_EQ(
116       extensions::api::file_system_provider::OnReadFileRequested::kEventName,
117       event->event_name);
118   base::ListValue* event_args = event->event_args.get();
119   ASSERT_EQ(1u, event_args->GetSize());
120
121   const base::DictionaryValue* options_as_value = NULL;
122   ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value));
123
124   ReadFileRequestedOptions options;
125   ASSERT_TRUE(ReadFileRequestedOptions::Populate(*options_as_value, &options));
126   EXPECT_EQ(kFileSystemId, options.file_system_id);
127   EXPECT_EQ(kRequestId, options.request_id);
128   EXPECT_EQ(kFileHandle, options.open_request_id);
129   EXPECT_EQ(kOffset, static_cast<double>(options.offset));
130   EXPECT_EQ(kLength, options.length);
131 }
132
133 TEST_F(FileSystemProviderOperationsReadFileTest, Execute_NoListener) {
134   util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */);
135   CallbackLogger callback_logger;
136
137   ReadFile read_file(NULL,
138                      file_system_info_,
139                      kFileHandle,
140                      io_buffer_.get(),
141                      kOffset,
142                      kLength,
143                      base::Bind(&CallbackLogger::OnReadFile,
144                                 base::Unretained(&callback_logger)));
145   read_file.SetDispatchEventImplForTesting(
146       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
147                  base::Unretained(&dispatcher)));
148
149   EXPECT_FALSE(read_file.Execute(kRequestId));
150 }
151
152 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) {
153   using extensions::api::file_system_provider_internal::
154       ReadFileRequestedSuccess::Params;
155
156   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
157   CallbackLogger callback_logger;
158
159   ReadFile read_file(NULL,
160                      file_system_info_,
161                      kFileHandle,
162                      io_buffer_.get(),
163                      kOffset,
164                      kLength,
165                      base::Bind(&CallbackLogger::OnReadFile,
166                                 base::Unretained(&callback_logger)));
167   read_file.SetDispatchEventImplForTesting(
168       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
169                  base::Unretained(&dispatcher)));
170
171   EXPECT_TRUE(read_file.Execute(kRequestId));
172
173   const std::string data = "ABCDE";
174   const bool has_more = false;
175   const int execution_time = 0;
176
177   base::ListValue value_as_list;
178   value_as_list.Set(0, new base::StringValue(kFileSystemId));
179   value_as_list.Set(1, new base::FundamentalValue(kRequestId));
180   value_as_list.Set(
181       2, base::BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size()));
182   value_as_list.Set(3, new base::FundamentalValue(has_more));
183   value_as_list.Set(4, new base::FundamentalValue(execution_time));
184
185   scoped_ptr<Params> params(Params::Create(value_as_list));
186   ASSERT_TRUE(params.get());
187   scoped_ptr<RequestValue> request_value(
188       RequestValue::CreateForReadFileSuccess(params.Pass()));
189   ASSERT_TRUE(request_value.get());
190
191   read_file.OnSuccess(kRequestId, request_value.Pass(), has_more);
192
193   ASSERT_EQ(1u, callback_logger.events().size());
194   CallbackLogger::Event* event = callback_logger.events()[0];
195   EXPECT_EQ(kLength, event->chunk_length());
196   EXPECT_FALSE(event->has_more());
197   EXPECT_EQ(data, std::string(io_buffer_->data(), kLength));
198   EXPECT_EQ(base::File::FILE_OK, event->result());
199 }
200
201 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) {
202   util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */);
203   CallbackLogger callback_logger;
204
205   ReadFile read_file(NULL,
206                      file_system_info_,
207                      kFileHandle,
208                      io_buffer_.get(),
209                      kOffset,
210                      kLength,
211                      base::Bind(&CallbackLogger::OnReadFile,
212                                 base::Unretained(&callback_logger)));
213   read_file.SetDispatchEventImplForTesting(
214       base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl,
215                  base::Unretained(&dispatcher)));
216
217   EXPECT_TRUE(read_file.Execute(kRequestId));
218
219   read_file.OnError(kRequestId,
220                     scoped_ptr<RequestValue>(new RequestValue()),
221                     base::File::FILE_ERROR_TOO_MANY_OPENED);
222
223   ASSERT_EQ(1u, callback_logger.events().size());
224   CallbackLogger::Event* event = callback_logger.events()[0];
225   EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result());
226 }
227
228 }  // namespace operations
229 }  // namespace file_system_provider
230 }  // namespace chromeos