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 "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h
" | 5 #include "chrome/browser/chromeos/file_system_provider/operations/get_metadata.h
" |
6 | 6 |
| 7 #include <memory> |
7 #include <string> | 8 #include <string> |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/files/file.h" | 11 #include "base/files/file.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" | 17 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" |
18 #include "chrome/common/extensions/api/file_system_provider.h" | 18 #include "chrome/common/extensions/api/file_system_provider.h" |
19 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_sy
stem_provider_capabilities_handler.h" | 19 #include "chrome/common/extensions/api/file_system_provider_capabilities/file_sy
stem_provider_capabilities_handler.h" |
20 #include "chrome/common/extensions/api/file_system_provider_internal.h" | 20 #include "chrome/common/extensions/api/file_system_provider_internal.h" |
21 #include "extensions/browser/event_router.h" | 21 #include "extensions/browser/event_router.h" |
22 #include "storage/browser/fileapi/async_file_util.h" | 22 #include "storage/browser/fileapi/async_file_util.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 namespace chromeos { | 25 namespace chromeos { |
26 namespace file_system_provider { | 26 namespace file_system_provider { |
27 namespace operations { | 27 namespace operations { |
28 namespace { | 28 namespace { |
29 | 29 |
30 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 30 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
31 const char kFileSystemId[] = "testing-file-system"; | 31 const char kFileSystemId[] = "testing-file-system"; |
32 const char kMimeType[] = "text/plain"; | 32 const char kMimeType[] = "text/plain"; |
33 const int kRequestId = 2; | 33 const int kRequestId = 2; |
34 const base::FilePath::CharType kDirectoryPath[] = | 34 const base::FilePath::CharType kDirectoryPath[] = |
35 FILE_PATH_LITERAL("/directory"); | 35 FILE_PATH_LITERAL("/directory"); |
36 | 36 |
37 // URLs are case insensitive, so it should pass the sanity check. | 37 // URLs are case insensitive, so it should pass the sanity check. |
38 const char kThumbnail[] = "DaTa:ImAgE/pNg;base64,"; | 38 const char kThumbnail[] = "DaTa:ImAgE/pNg;base64,"; |
39 | 39 |
40 // Returns the request value as |result| in case of successful parse. | 40 // Returns the request value as |result| in case of successful parse. |
41 void CreateRequestValueFromJSON(const std::string& json, | 41 void CreateRequestValueFromJSON(const std::string& json, |
42 scoped_ptr<RequestValue>* result) { | 42 std::unique_ptr<RequestValue>* result) { |
43 using extensions::api::file_system_provider_internal:: | 43 using extensions::api::file_system_provider_internal:: |
44 GetMetadataRequestedSuccess::Params; | 44 GetMetadataRequestedSuccess::Params; |
45 | 45 |
46 int json_error_code; | 46 int json_error_code; |
47 std::string json_error_msg; | 47 std::string json_error_msg; |
48 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( | 48 std::unique_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( |
49 json, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg); | 49 json, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg); |
50 ASSERT_TRUE(value.get()) << json_error_msg; | 50 ASSERT_TRUE(value.get()) << json_error_msg; |
51 | 51 |
52 base::ListValue* value_as_list; | 52 base::ListValue* value_as_list; |
53 ASSERT_TRUE(value->GetAsList(&value_as_list)); | 53 ASSERT_TRUE(value->GetAsList(&value_as_list)); |
54 scoped_ptr<Params> params(Params::Create(*value_as_list)); | 54 std::unique_ptr<Params> params(Params::Create(*value_as_list)); |
55 ASSERT_TRUE(params.get()); | 55 ASSERT_TRUE(params.get()); |
56 *result = RequestValue::CreateForGetMetadataSuccess(std::move(params)); | 56 *result = RequestValue::CreateForGetMetadataSuccess(std::move(params)); |
57 ASSERT_TRUE(result->get()); | 57 ASSERT_TRUE(result->get()); |
58 } | 58 } |
59 | 59 |
60 // Callback invocation logger. Acts as a fileapi end-point. | 60 // Callback invocation logger. Acts as a fileapi end-point. |
61 class CallbackLogger { | 61 class CallbackLogger { |
62 public: | 62 public: |
63 class Event { | 63 class Event { |
64 public: | 64 public: |
65 Event(scoped_ptr<EntryMetadata> metadata, base::File::Error result) | 65 Event(std::unique_ptr<EntryMetadata> metadata, base::File::Error result) |
66 : metadata_(std::move(metadata)), result_(result) {} | 66 : metadata_(std::move(metadata)), result_(result) {} |
67 virtual ~Event() {} | 67 virtual ~Event() {} |
68 | 68 |
69 const EntryMetadata* metadata() const { return metadata_.get(); } | 69 const EntryMetadata* metadata() const { return metadata_.get(); } |
70 base::File::Error result() const { return result_; } | 70 base::File::Error result() const { return result_; } |
71 | 71 |
72 private: | 72 private: |
73 scoped_ptr<EntryMetadata> metadata_; | 73 std::unique_ptr<EntryMetadata> metadata_; |
74 base::File::Error result_; | 74 base::File::Error result_; |
75 | 75 |
76 DISALLOW_COPY_AND_ASSIGN(Event); | 76 DISALLOW_COPY_AND_ASSIGN(Event); |
77 }; | 77 }; |
78 | 78 |
79 CallbackLogger() {} | 79 CallbackLogger() {} |
80 virtual ~CallbackLogger() {} | 80 virtual ~CallbackLogger() {} |
81 | 81 |
82 void OnGetMetadata(scoped_ptr<EntryMetadata> metadata, | 82 void OnGetMetadata(std::unique_ptr<EntryMetadata> metadata, |
83 base::File::Error result) { | 83 base::File::Error result) { |
84 events_.push_back(new Event(std::move(metadata), result)); | 84 events_.push_back(new Event(std::move(metadata), result)); |
85 } | 85 } |
86 | 86 |
87 const ScopedVector<Event>& events() const { return events_; } | 87 const ScopedVector<Event>& events() const { return events_; } |
88 | 88 |
89 private: | 89 private: |
90 ScopedVector<Event> events_; | 90 ScopedVector<Event> events_; |
91 | 91 |
92 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); | 92 DISALLOW_COPY_AND_ASSIGN(CallbackLogger); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 " \"name\": \"blueberries.txt\",\n" | 304 " \"name\": \"blueberries.txt\",\n" |
305 " \"size\": 4096,\n" | 305 " \"size\": 4096,\n" |
306 " \"modificationTime\": {\n" | 306 " \"modificationTime\": {\n" |
307 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" | 307 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" |
308 " },\n" | 308 " },\n" |
309 " \"mimeType\": \"text/plain\",\n" // kMimeType | 309 " \"mimeType\": \"text/plain\",\n" // kMimeType |
310 " \"thumbnail\": \"DaTa:ImAgE/pNg;base64,\"\n" // kThumbnail | 310 " \"thumbnail\": \"DaTa:ImAgE/pNg;base64,\"\n" // kThumbnail |
311 " },\n" | 311 " },\n" |
312 " 0\n" // execution_time | 312 " 0\n" // execution_time |
313 "]\n"; | 313 "]\n"; |
314 scoped_ptr<RequestValue> request_value; | 314 std::unique_ptr<RequestValue> request_value; |
315 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value)); | 315 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value)); |
316 | 316 |
317 const bool has_more = false; | 317 const bool has_more = false; |
318 get_metadata.OnSuccess(kRequestId, std::move(request_value), has_more); | 318 get_metadata.OnSuccess(kRequestId, std::move(request_value), has_more); |
319 | 319 |
320 ASSERT_EQ(1u, callback_logger.events().size()); | 320 ASSERT_EQ(1u, callback_logger.events().size()); |
321 CallbackLogger::Event* event = callback_logger.events()[0]; | 321 CallbackLogger::Event* event = callback_logger.events()[0]; |
322 EXPECT_EQ(base::File::FILE_OK, event->result()); | 322 EXPECT_EQ(base::File::FILE_OK, event->result()); |
323 | 323 |
324 const EntryMetadata* metadata = event->metadata(); | 324 const EntryMetadata* metadata = event->metadata(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 " \"size\": 4096,\n" | 365 " \"size\": 4096,\n" |
366 " \"modificationTime\": {\n" | 366 " \"modificationTime\": {\n" |
367 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" | 367 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" |
368 " },\n" | 368 " },\n" |
369 " \"mimeType\": \"text/plain\",\n" // kMimeType | 369 " \"mimeType\": \"text/plain\",\n" // kMimeType |
370 " \"thumbnail\": \"http://www.foobar.com/evil\"\n" // kThumbnail | 370 " \"thumbnail\": \"http://www.foobar.com/evil\"\n" // kThumbnail |
371 " },\n" | 371 " },\n" |
372 " 0\n" // execution_time | 372 " 0\n" // execution_time |
373 "]\n"; | 373 "]\n"; |
374 | 374 |
375 scoped_ptr<RequestValue> request_value; | 375 std::unique_ptr<RequestValue> request_value; |
376 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value)); | 376 ASSERT_NO_FATAL_FAILURE(CreateRequestValueFromJSON(input, &request_value)); |
377 | 377 |
378 const bool has_more = false; | 378 const bool has_more = false; |
379 get_metadata.OnSuccess(kRequestId, std::move(request_value), has_more); | 379 get_metadata.OnSuccess(kRequestId, std::move(request_value), has_more); |
380 | 380 |
381 ASSERT_EQ(1u, callback_logger.events().size()); | 381 ASSERT_EQ(1u, callback_logger.events().size()); |
382 CallbackLogger::Event* event = callback_logger.events()[0]; | 382 CallbackLogger::Event* event = callback_logger.events()[0]; |
383 EXPECT_EQ(base::File::FILE_ERROR_IO, event->result()); | 383 EXPECT_EQ(base::File::FILE_ERROR_IO, event->result()); |
384 | 384 |
385 const EntryMetadata* metadata = event->metadata(); | 385 const EntryMetadata* metadata = event->metadata(); |
386 EXPECT_FALSE(metadata); | 386 EXPECT_FALSE(metadata); |
387 } | 387 } |
388 | 388 |
389 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnError) { | 389 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnError) { |
390 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 390 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
391 CallbackLogger callback_logger; | 391 CallbackLogger callback_logger; |
392 | 392 |
393 GetMetadata get_metadata( | 393 GetMetadata get_metadata( |
394 NULL, file_system_info_, base::FilePath(kDirectoryPath), | 394 NULL, file_system_info_, base::FilePath(kDirectoryPath), |
395 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, | 395 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, |
396 base::Bind(&CallbackLogger::OnGetMetadata, | 396 base::Bind(&CallbackLogger::OnGetMetadata, |
397 base::Unretained(&callback_logger))); | 397 base::Unretained(&callback_logger))); |
398 get_metadata.SetDispatchEventImplForTesting( | 398 get_metadata.SetDispatchEventImplForTesting( |
399 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 399 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
400 base::Unretained(&dispatcher))); | 400 base::Unretained(&dispatcher))); |
401 | 401 |
402 EXPECT_TRUE(get_metadata.Execute(kRequestId)); | 402 EXPECT_TRUE(get_metadata.Execute(kRequestId)); |
403 | 403 |
404 get_metadata.OnError(kRequestId, | 404 get_metadata.OnError(kRequestId, |
405 scoped_ptr<RequestValue>(new RequestValue()), | 405 std::unique_ptr<RequestValue>(new RequestValue()), |
406 base::File::FILE_ERROR_TOO_MANY_OPENED); | 406 base::File::FILE_ERROR_TOO_MANY_OPENED); |
407 | 407 |
408 ASSERT_EQ(1u, callback_logger.events().size()); | 408 ASSERT_EQ(1u, callback_logger.events().size()); |
409 CallbackLogger::Event* event = callback_logger.events()[0]; | 409 CallbackLogger::Event* event = callback_logger.events()[0]; |
410 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); | 410 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); |
411 } | 411 } |
412 | 412 |
413 } // namespace operations | 413 } // namespace operations |
414 } // namespace file_system_provider | 414 } // namespace file_system_provider |
415 } // namespace chromeos | 415 } // namespace chromeos |
OLD | NEW |