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