OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/json/json_reader.h" | |
10 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/values.h" |
13 #include "chrome/browser/chromeos/file_system_provider/operations/read_file.h" | 13 #include "chrome/browser/chromeos/file_system_provider/operations/read_file.h" |
14 #include "chrome/common/extensions/api/file_system_provider.h" | 14 #include "chrome/common/extensions/api/file_system_provider.h" |
15 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 15 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
16 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "webkit/browser/fileapi/async_file_util.h" | 19 #include "webkit/browser/fileapi/async_file_util.h" |
20 | 20 |
21 namespace chromeos { | 21 namespace chromeos { |
22 namespace file_system_provider { | 22 namespace file_system_provider { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 kLength, | 174 kLength, |
175 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); | 175 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); |
176 read_file.SetDispatchEventImplForTesting( | 176 read_file.SetDispatchEventImplForTesting( |
177 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 177 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
178 base::Unretained(&dispatcher))); | 178 base::Unretained(&dispatcher))); |
179 | 179 |
180 EXPECT_FALSE(read_file.Execute(kRequestId)); | 180 EXPECT_FALSE(read_file.Execute(kRequestId)); |
181 } | 181 } |
182 | 182 |
183 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) { | 183 TEST_F(FileSystemProviderOperationsReadFileTest, OnSuccess) { |
184 using extensions::api::file_system_provider::EntryMetadata; | |
185 using extensions::api::file_system_provider_internal:: | 184 using extensions::api::file_system_provider_internal:: |
186 ReadFileRequestedSuccess::Params; | 185 ReadFileRequestedSuccess::Params; |
187 | 186 |
188 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 187 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
189 CallbackLogger callback_logger; | 188 CallbackLogger callback_logger; |
190 | 189 |
191 ReadFile read_file( | 190 ReadFile read_file( |
192 NULL, | 191 NULL, |
193 file_system_info_, | 192 file_system_info_, |
194 kFileHandle, | 193 kFileHandle, |
195 io_buffer_.get(), | 194 io_buffer_.get(), |
196 kOffset, | 195 kOffset, |
197 kLength, | 196 kLength, |
198 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); | 197 base::Bind(&CallbackLogger::OnReadFile, callback_logger.GetWeakPtr())); |
199 read_file.SetDispatchEventImplForTesting( | 198 read_file.SetDispatchEventImplForTesting( |
200 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, | 199 base::Bind(&LoggingDispatchEventImpl::OnDispatchEventImpl, |
201 base::Unretained(&dispatcher))); | 200 base::Unretained(&dispatcher))); |
202 | 201 |
203 EXPECT_TRUE(read_file.Execute(kRequestId)); | 202 EXPECT_TRUE(read_file.Execute(kRequestId)); |
204 | 203 |
205 // Sample input as JSON. Keep in sync with file_system_provider_api.idl. | 204 const std::string data = "ABCDE"; |
206 // As for now, it is impossible to create *::Params class directly, not from | 205 const bool has_next = false; |
207 // base::Value. | |
208 const std::string input = | |
209 "[\n" | |
210 " 1,\n" // kFileSystemId | |
211 " 2,\n" // kRequestId | |
212 " \"ABCDE\",\n" // 5 bytes | |
213 " false\n" // has_next | |
214 "]\n"; | |
215 | 206 |
216 int json_error_code; | 207 base::ListValue value_as_list; |
217 std::string json_error_msg; | 208 value_as_list.Set(0, new base::FundamentalValue(kFileSystemId)); |
218 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 209 value_as_list.Set(1, new base::FundamentalValue(kRequestId)); |
219 input, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg)); | 210 value_as_list.Set( |
220 ASSERT_TRUE(value.get()) << json_error_msg; | 211 2, base::BinaryValue::CreateWithCopiedBuffer(data.c_str(), data.size())); |
| 212 value_as_list.Set(3, new base::FundamentalValue(has_next)); |
221 | 213 |
222 base::ListValue* value_as_list; | 214 scoped_ptr<Params> params(Params::Create(value_as_list)); |
223 ASSERT_TRUE(value->GetAsList(&value_as_list)); | |
224 scoped_ptr<Params> params(Params::Create(*value_as_list)); | |
225 ASSERT_TRUE(params.get()); | 215 ASSERT_TRUE(params.get()); |
226 scoped_ptr<RequestValue> request_value( | 216 scoped_ptr<RequestValue> request_value( |
227 RequestValue::CreateForReadFileSuccess(params.Pass())); | 217 RequestValue::CreateForReadFileSuccess(params.Pass())); |
228 ASSERT_TRUE(request_value.get()); | 218 ASSERT_TRUE(request_value.get()); |
229 | 219 |
230 const bool has_next = false; | |
231 read_file.OnSuccess(kRequestId, request_value.Pass(), has_next); | 220 read_file.OnSuccess(kRequestId, request_value.Pass(), has_next); |
232 | 221 |
233 ASSERT_EQ(1u, callback_logger.events().size()); | 222 ASSERT_EQ(1u, callback_logger.events().size()); |
234 CallbackLogger::Event* event = callback_logger.events()[0]; | 223 CallbackLogger::Event* event = callback_logger.events()[0]; |
235 EXPECT_EQ(kLength, event->chunk_length()); | 224 EXPECT_EQ(kLength, event->chunk_length()); |
236 EXPECT_FALSE(event->has_next()); | 225 EXPECT_FALSE(event->has_next()); |
237 EXPECT_EQ("ABCDE", std::string(io_buffer_->data() + kOffset, kLength)); | 226 EXPECT_EQ(data, std::string(io_buffer_->data(), kLength)); |
238 EXPECT_EQ(base::File::FILE_OK, event->result()); | 227 EXPECT_EQ(base::File::FILE_OK, event->result()); |
239 } | 228 } |
240 | 229 |
241 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) { | 230 TEST_F(FileSystemProviderOperationsReadFileTest, OnError) { |
242 using extensions::api::file_system_provider::EntryMetadata; | |
243 using extensions::api::file_system_provider_internal::ReadFileRequestedError:: | 231 using extensions::api::file_system_provider_internal::ReadFileRequestedError:: |
244 Params; | 232 Params; |
245 | 233 |
246 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 234 LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
247 CallbackLogger callback_logger; | 235 CallbackLogger callback_logger; |
248 | 236 |
249 ReadFile read_file( | 237 ReadFile read_file( |
250 NULL, | 238 NULL, |
251 file_system_info_, | 239 file_system_info_, |
252 kFileHandle, | 240 kFileHandle, |
(...skipping 10 matching lines...) Expand all Loading... |
263 read_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED); | 251 read_file.OnError(kRequestId, base::File::FILE_ERROR_TOO_MANY_OPENED); |
264 | 252 |
265 ASSERT_EQ(1u, callback_logger.events().size()); | 253 ASSERT_EQ(1u, callback_logger.events().size()); |
266 CallbackLogger::Event* event = callback_logger.events()[0]; | 254 CallbackLogger::Event* event = callback_logger.events()[0]; |
267 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); | 255 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); |
268 } | 256 } |
269 | 257 |
270 } // namespace operations | 258 } // namespace operations |
271 } // namespace file_system_provider | 259 } // namespace file_system_provider |
272 } // namespace chromeos | 260 } // namespace chromeos |
OLD | NEW |