| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 namespace file_system_provider { | 23 namespace file_system_provider { |
| 24 namespace operations { | 24 namespace operations { |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 28 const char kFileSystemId[] = "testing-file-system"; | 28 const char kFileSystemId[] = "testing-file-system"; |
| 29 const char kMimeType[] = "text/plain"; | 29 const char kMimeType[] = "text/plain"; |
| 30 const int kRequestId = 2; | 30 const int kRequestId = 2; |
| 31 const base::FilePath::CharType kDirectoryPath[] = "/directory"; | 31 const base::FilePath::CharType kDirectoryPath[] = "/directory"; |
| 32 | 32 |
| 33 // URLs are case insensitive, so it should pass the sanity check. |
| 34 const char kThumbnail[] = "DaTa:ImAgE/pNg;base64,"; |
| 35 |
| 33 // Callback invocation logger. Acts as a fileapi end-point. | 36 // Callback invocation logger. Acts as a fileapi end-point. |
| 34 class CallbackLogger { | 37 class CallbackLogger { |
| 35 public: | 38 public: |
| 36 class Event { | 39 class Event { |
| 37 public: | 40 public: |
| 38 Event(const EntryMetadata& metadata, base::File::Error result) | 41 Event(const EntryMetadata& metadata, base::File::Error result) |
| 39 : metadata_(metadata), result_(result) {} | 42 : metadata_(metadata), result_(result) {} |
| 40 virtual ~Event() {} | 43 virtual ~Event() {} |
| 41 | 44 |
| 42 const EntryMetadata& metadata() { return metadata_; } | 45 const EntryMetadata& metadata() { return metadata_; } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 base::FilePath() /* mount_path */); | 84 base::FilePath() /* mount_path */); |
| 82 } | 85 } |
| 83 | 86 |
| 84 ProvidedFileSystemInfo file_system_info_; | 87 ProvidedFileSystemInfo file_system_info_; |
| 85 }; | 88 }; |
| 86 | 89 |
| 87 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute) { | 90 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute) { |
| 88 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 91 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 89 CallbackLogger callback_logger; | 92 CallbackLogger callback_logger; |
| 90 | 93 |
| 91 GetMetadata get_metadata(NULL, | 94 GetMetadata get_metadata( |
| 92 file_system_info_, | 95 NULL, |
| 93 base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 96 file_system_info_, |
| 94 base::Bind(&CallbackLogger::OnGetMetadata, | 97 base::FilePath::FromUTF8Unsafe(kDirectoryPath), |
| 95 base::Unretained(&callback_logger))); | 98 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, |
| 99 base::Bind(&CallbackLogger::OnGetMetadata, |
| 100 base::Unretained(&callback_logger))); |
| 96 get_metadata.SetDispatchEventImplForTesting( | 101 get_metadata.SetDispatchEventImplForTesting( |
| 97 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 102 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 98 base::Unretained(&dispatcher))); | 103 base::Unretained(&dispatcher))); |
| 99 | 104 |
| 100 EXPECT_TRUE(get_metadata.Execute(kRequestId)); | 105 EXPECT_TRUE(get_metadata.Execute(kRequestId)); |
| 101 | 106 |
| 102 ASSERT_EQ(1u, dispatcher.events().size()); | 107 ASSERT_EQ(1u, dispatcher.events().size()); |
| 103 extensions::Event* event = dispatcher.events()[0]; | 108 extensions::Event* event = dispatcher.events()[0]; |
| 104 EXPECT_EQ( | 109 EXPECT_EQ( |
| 105 extensions::api::file_system_provider::OnGetMetadataRequested::kEventName, | 110 extensions::api::file_system_provider::OnGetMetadataRequested::kEventName, |
| 106 event->event_name); | 111 event->event_name); |
| 107 base::ListValue* event_args = event->event_args.get(); | 112 base::ListValue* event_args = event->event_args.get(); |
| 108 ASSERT_EQ(1u, event_args->GetSize()); | 113 ASSERT_EQ(1u, event_args->GetSize()); |
| 109 | 114 |
| 110 base::DictionaryValue* options = NULL; | 115 base::DictionaryValue* options = NULL; |
| 111 ASSERT_TRUE(event_args->GetDictionary(0, &options)); | 116 ASSERT_TRUE(event_args->GetDictionary(0, &options)); |
| 112 | 117 |
| 113 std::string event_file_system_id; | 118 std::string event_file_system_id; |
| 114 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); | 119 EXPECT_TRUE(options->GetString("fileSystemId", &event_file_system_id)); |
| 115 EXPECT_EQ(kFileSystemId, event_file_system_id); | 120 EXPECT_EQ(kFileSystemId, event_file_system_id); |
| 116 | 121 |
| 117 int event_request_id = -1; | 122 int event_request_id = -1; |
| 118 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); | 123 EXPECT_TRUE(options->GetInteger("requestId", &event_request_id)); |
| 119 EXPECT_EQ(kRequestId, event_request_id); | 124 EXPECT_EQ(kRequestId, event_request_id); |
| 120 | 125 |
| 121 std::string event_entry_path; | 126 std::string event_entry_path; |
| 122 EXPECT_TRUE(options->GetString("entryPath", &event_entry_path)); | 127 EXPECT_TRUE(options->GetString("entryPath", &event_entry_path)); |
| 123 EXPECT_EQ(kDirectoryPath, event_entry_path); | 128 EXPECT_EQ(kDirectoryPath, event_entry_path); |
| 129 |
| 130 bool event_thumbnail; |
| 131 EXPECT_TRUE(options->GetBoolean("thumbnail", &event_thumbnail)); |
| 132 EXPECT_TRUE(event_thumbnail); |
| 124 } | 133 } |
| 125 | 134 |
| 126 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute_NoListener) { | 135 TEST_F(FileSystemProviderOperationsGetMetadataTest, Execute_NoListener) { |
| 127 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 136 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
| 128 CallbackLogger callback_logger; | 137 CallbackLogger callback_logger; |
| 129 | 138 |
| 130 GetMetadata get_metadata(NULL, | 139 GetMetadata get_metadata( |
| 131 file_system_info_, | 140 NULL, |
| 132 base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 141 file_system_info_, |
| 133 base::Bind(&CallbackLogger::OnGetMetadata, | 142 base::FilePath::FromUTF8Unsafe(kDirectoryPath), |
| 134 base::Unretained(&callback_logger))); | 143 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, |
| 144 base::Bind(&CallbackLogger::OnGetMetadata, |
| 145 base::Unretained(&callback_logger))); |
| 135 get_metadata.SetDispatchEventImplForTesting( | 146 get_metadata.SetDispatchEventImplForTesting( |
| 136 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 147 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 137 base::Unretained(&dispatcher))); | 148 base::Unretained(&dispatcher))); |
| 138 | 149 |
| 139 EXPECT_FALSE(get_metadata.Execute(kRequestId)); | 150 EXPECT_FALSE(get_metadata.Execute(kRequestId)); |
| 140 } | 151 } |
| 141 | 152 |
| 142 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnSuccess) { | 153 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnSuccess) { |
| 143 using extensions::api::file_system_provider_internal:: | 154 using extensions::api::file_system_provider_internal:: |
| 144 GetMetadataRequestedSuccess::Params; | 155 GetMetadataRequestedSuccess::Params; |
| 145 | 156 |
| 146 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 157 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 147 CallbackLogger callback_logger; | 158 CallbackLogger callback_logger; |
| 148 | 159 |
| 149 GetMetadata get_metadata(NULL, | 160 GetMetadata get_metadata( |
| 150 file_system_info_, | 161 NULL, |
| 151 base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 162 file_system_info_, |
| 152 base::Bind(&CallbackLogger::OnGetMetadata, | 163 base::FilePath::FromUTF8Unsafe(kDirectoryPath), |
| 153 base::Unretained(&callback_logger))); | 164 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, |
| 165 base::Bind(&CallbackLogger::OnGetMetadata, |
| 166 base::Unretained(&callback_logger))); |
| 154 get_metadata.SetDispatchEventImplForTesting( | 167 get_metadata.SetDispatchEventImplForTesting( |
| 155 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 168 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 156 base::Unretained(&dispatcher))); | 169 base::Unretained(&dispatcher))); |
| 157 | 170 |
| 158 EXPECT_TRUE(get_metadata.Execute(kRequestId)); | 171 EXPECT_TRUE(get_metadata.Execute(kRequestId)); |
| 159 | 172 |
| 160 // Sample input as JSON. Keep in sync with file_system_provider_api.idl. | 173 // Sample input as JSON. Keep in sync with file_system_provider_api.idl. |
| 161 // As for now, it is impossible to create *::Params class directly, not from | 174 // As for now, it is impossible to create *::Params class directly, not from |
| 162 // base::Value. | 175 // base::Value. |
| 163 const std::string input = | 176 const std::string input = |
| 164 "[\n" | 177 "[\n" |
| 165 " \"testing-file-system\",\n" // kFileSystemId | 178 " \"testing-file-system\",\n" // kFileSystemId |
| 166 " 2,\n" // kRequestId | 179 " 2,\n" // kRequestId |
| 167 " {\n" | 180 " {\n" |
| 168 " \"isDirectory\": false,\n" | 181 " \"isDirectory\": false,\n" |
| 169 " \"name\": \"blueberries.txt\",\n" | 182 " \"name\": \"blueberries.txt\",\n" |
| 170 " \"size\": 4096,\n" | 183 " \"size\": 4096,\n" |
| 171 " \"modificationTime\": {\n" | 184 " \"modificationTime\": {\n" |
| 172 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" | 185 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" |
| 173 " },\n" | 186 " },\n" |
| 174 " \"mimeType\": \"text/plain\"\n" // kMimeType | 187 " \"mimeType\": \"text/plain\",\n" // kMimeType |
| 188 " \"thumbnail\": \"DaTa:ImAgE/pNg;base64,\"\n" // kThumbnail |
| 175 " },\n" | 189 " },\n" |
| 176 " 0\n" // execution_time | 190 " 0\n" // execution_time |
| 177 "]\n"; | 191 "]\n"; |
| 178 | 192 |
| 179 int json_error_code; | 193 int json_error_code; |
| 180 std::string json_error_msg; | 194 std::string json_error_msg; |
| 181 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 195 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 182 input, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg)); | 196 input, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg)); |
| 183 ASSERT_TRUE(value.get()) << json_error_msg; | 197 ASSERT_TRUE(value.get()) << json_error_msg; |
| 184 | 198 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 198 EXPECT_EQ(base::File::FILE_OK, event->result()); | 212 EXPECT_EQ(base::File::FILE_OK, event->result()); |
| 199 | 213 |
| 200 const EntryMetadata& metadata = event->metadata(); | 214 const EntryMetadata& metadata = event->metadata(); |
| 201 EXPECT_FALSE(metadata.is_directory); | 215 EXPECT_FALSE(metadata.is_directory); |
| 202 EXPECT_EQ(4096, metadata.size); | 216 EXPECT_EQ(4096, metadata.size); |
| 203 base::Time expected_time; | 217 base::Time expected_time; |
| 204 EXPECT_TRUE( | 218 EXPECT_TRUE( |
| 205 base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", &expected_time)); | 219 base::Time::FromString("Thu Apr 24 00:46:52 UTC 2014", &expected_time)); |
| 206 EXPECT_EQ(expected_time, metadata.modification_time); | 220 EXPECT_EQ(expected_time, metadata.modification_time); |
| 207 EXPECT_EQ(kMimeType, metadata.mime_type); | 221 EXPECT_EQ(kMimeType, metadata.mime_type); |
| 222 EXPECT_EQ(kThumbnail, metadata.thumbnail); |
| 223 } |
| 224 |
| 225 TEST_F(FileSystemProviderOperationsGetMetadataTest, |
| 226 OnSuccess_InvalidThumbnail) { |
| 227 using extensions::api::file_system_provider_internal:: |
| 228 GetMetadataRequestedSuccess::Params; |
| 229 |
| 230 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 231 CallbackLogger callback_logger; |
| 232 |
| 233 GetMetadata get_metadata( |
| 234 NULL, |
| 235 file_system_info_, |
| 236 base::FilePath::FromUTF8Unsafe(kDirectoryPath), |
| 237 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, |
| 238 base::Bind(&CallbackLogger::OnGetMetadata, |
| 239 base::Unretained(&callback_logger))); |
| 240 get_metadata.SetDispatchEventImplForTesting( |
| 241 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 242 base::Unretained(&dispatcher))); |
| 243 |
| 244 EXPECT_TRUE(get_metadata.Execute(kRequestId)); |
| 245 |
| 246 // Sample input as JSON. Keep in sync with file_system_provider_api.idl. |
| 247 // As for now, it is impossible to create *::Params class directly, not from |
| 248 // base::Value. |
| 249 const std::string input = |
| 250 "[\n" |
| 251 " \"testing-file-system\",\n" // kFileSystemId |
| 252 " 2,\n" // kRequestId |
| 253 " {\n" |
| 254 " \"isDirectory\": false,\n" |
| 255 " \"name\": \"blueberries.txt\",\n" |
| 256 " \"size\": 4096,\n" |
| 257 " \"modificationTime\": {\n" |
| 258 " \"value\": \"Thu Apr 24 00:46:52 UTC 2014\"\n" |
| 259 " },\n" |
| 260 " \"mimeType\": \"text/plain\",\n" // kMimeType |
| 261 " \"thumbnail\": \"http://www.foobar.com/evil\"\n" // kThumbnail |
| 262 " },\n" |
| 263 " 0\n" // execution_time |
| 264 "]\n"; |
| 265 |
| 266 int json_error_code; |
| 267 std::string json_error_msg; |
| 268 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 269 input, base::JSON_PARSE_RFC, &json_error_code, &json_error_msg)); |
| 270 ASSERT_TRUE(value.get()) << json_error_msg; |
| 271 |
| 272 base::ListValue* value_as_list; |
| 273 ASSERT_TRUE(value->GetAsList(&value_as_list)); |
| 274 scoped_ptr<Params> params(Params::Create(*value_as_list)); |
| 275 ASSERT_TRUE(params.get()); |
| 276 scoped_ptr<RequestValue> request_value( |
| 277 RequestValue::CreateForGetMetadataSuccess(params.Pass())); |
| 278 ASSERT_TRUE(request_value.get()); |
| 279 |
| 280 const bool has_more = false; |
| 281 get_metadata.OnSuccess(kRequestId, request_value.Pass(), has_more); |
| 282 |
| 283 ASSERT_EQ(1u, callback_logger.events().size()); |
| 284 CallbackLogger::Event* event = callback_logger.events()[0]; |
| 285 EXPECT_EQ(base::File::FILE_ERROR_IO, event->result()); |
| 286 |
| 287 const EntryMetadata& metadata = event->metadata(); |
| 288 EXPECT_EQ("", metadata.thumbnail); |
| 208 } | 289 } |
| 209 | 290 |
| 210 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnError) { | 291 TEST_F(FileSystemProviderOperationsGetMetadataTest, OnError) { |
| 211 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 292 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 212 CallbackLogger callback_logger; | 293 CallbackLogger callback_logger; |
| 213 | 294 |
| 214 GetMetadata get_metadata(NULL, | 295 GetMetadata get_metadata( |
| 215 file_system_info_, | 296 NULL, |
| 216 base::FilePath::FromUTF8Unsafe(kDirectoryPath), | 297 file_system_info_, |
| 217 base::Bind(&CallbackLogger::OnGetMetadata, | 298 base::FilePath::FromUTF8Unsafe(kDirectoryPath), |
| 218 base::Unretained(&callback_logger))); | 299 ProvidedFileSystemInterface::METADATA_FIELD_THUMBNAIL, |
| 300 base::Bind(&CallbackLogger::OnGetMetadata, |
| 301 base::Unretained(&callback_logger))); |
| 219 get_metadata.SetDispatchEventImplForTesting( | 302 get_metadata.SetDispatchEventImplForTesting( |
| 220 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 303 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 221 base::Unretained(&dispatcher))); | 304 base::Unretained(&dispatcher))); |
| 222 | 305 |
| 223 EXPECT_TRUE(get_metadata.Execute(kRequestId)); | 306 EXPECT_TRUE(get_metadata.Execute(kRequestId)); |
| 224 | 307 |
| 225 get_metadata.OnError(kRequestId, | 308 get_metadata.OnError(kRequestId, |
| 226 scoped_ptr<RequestValue>(new RequestValue()), | 309 scoped_ptr<RequestValue>(new RequestValue()), |
| 227 base::File::FILE_ERROR_TOO_MANY_OPENED); | 310 base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 228 | 311 |
| 229 ASSERT_EQ(1u, callback_logger.events().size()); | 312 ASSERT_EQ(1u, callback_logger.events().size()); |
| 230 CallbackLogger::Event* event = callback_logger.events()[0]; | 313 CallbackLogger::Event* event = callback_logger.events()[0]; |
| 231 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); | 314 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, event->result()); |
| 232 } | 315 } |
| 233 | 316 |
| 234 } // namespace operations | 317 } // namespace operations |
| 235 } // namespace file_system_provider | 318 } // namespace file_system_provider |
| 236 } // namespace chromeos | 319 } // namespace chromeos |
| OLD | NEW |