1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
7 #include "base/files/file_path.h"
8 #include "base/time/time.h"
9 #include "base/values.h"
10 #include "chrome/browser/chromeos/drive/drive.pb.h"
11 #include "chrome/browser/chromeos/drive/test_util.h"
12 #include "google_apis/drive/gdata_wapi_parser.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(ResourceEntryConversionTest
, ConvertToResourceEntry_File
) {
18 scoped_ptr
<base::Value
> value
=
19 google_apis::test_util::LoadJSONFile("gdata/file_entry.json");
20 ASSERT_TRUE(value
.get());
22 scoped_ptr
<google_apis::ResourceEntry
> gdata_resource_entry(
23 google_apis::ResourceEntry::ExtractAndParse(*value
));
24 ASSERT_TRUE(gdata_resource_entry
.get());
27 std::string parent_resource_id
;
28 EXPECT_TRUE(ConvertToResourceEntry(*gdata_resource_entry
, &entry
,
29 &parent_resource_id
));
31 EXPECT_EQ("File 1.mp3", entry
.title());
32 EXPECT_EQ("File 1.mp3", entry
.base_name());
33 EXPECT_EQ("file:2_file_resource_id", entry
.resource_id());
34 EXPECT_EQ("", parent_resource_id
);
36 EXPECT_FALSE(entry
.deleted());
37 EXPECT_FALSE(entry
.shared_with_me());
38 EXPECT_FALSE(entry
.shared());
40 base::Time expected_creation_time
;
41 base::Time expected_modified_time
;
44 // 2011-12-14T00:40:47.330Z
45 base::Time::Exploded exploded
;
48 exploded
.day_of_month
= 13;
49 exploded
.day_of_week
= 2; // Tuesday
53 exploded
.millisecond
= 330;
54 expected_creation_time
= base::Time::FromUTCExploded(exploded
);
58 // 2011-12-13T00:40:47.330Z
59 base::Time::Exploded exploded
;
62 exploded
.day_of_month
= 14;
63 exploded
.day_of_week
= 3; // Wednesday
67 exploded
.millisecond
= 330;
68 expected_modified_time
= base::Time::FromUTCExploded(exploded
);
71 EXPECT_EQ(expected_modified_time
.ToInternalValue(),
72 entry
.file_info().last_modified());
73 // Last accessed value equal to 0 means that the file has never been viewed.
74 EXPECT_EQ(0, entry
.file_info().last_accessed());
75 EXPECT_EQ(expected_creation_time
.ToInternalValue(),
76 entry
.file_info().creation_time());
78 EXPECT_EQ("audio/mpeg",
79 entry
.file_specific_info().content_mime_type());
80 EXPECT_FALSE(entry
.file_specific_info().is_hosted_document());
81 EXPECT_EQ("https://file_link_alternate/",
82 entry
.file_specific_info().alternate_url());
84 // Regular file specific fields.
85 EXPECT_EQ(892721, entry
.file_info().size());
86 EXPECT_EQ("3b4382ebefec6e743578c76bbd0575ce",
87 entry
.file_specific_info().md5());
88 EXPECT_FALSE(entry
.file_info().is_directory());
91 TEST(ResourceEntryConversionTest
,
92 ConvertToResourceEntry_HostedDocument
) {
93 scoped_ptr
<base::Value
> value
=
94 google_apis::test_util::LoadJSONFile(
95 "gdata/hosted_document_entry.json");
96 ASSERT_TRUE(value
.get());
98 scoped_ptr
<google_apis::ResourceEntry
> gdata_resource_entry(
99 google_apis::ResourceEntry::ExtractAndParse(*value
));
100 ASSERT_TRUE(gdata_resource_entry
.get());
103 std::string parent_resource_id
;
104 EXPECT_TRUE(ConvertToResourceEntry(*gdata_resource_entry
, &entry
,
105 &parent_resource_id
));
107 EXPECT_EQ("Document 1", entry
.title());
108 EXPECT_EQ("Document 1.gdoc", entry
.base_name()); // The suffix added.
109 EXPECT_EQ(".gdoc", entry
.file_specific_info().document_extension());
110 EXPECT_EQ("document:5_document_resource_id", entry
.resource_id());
111 EXPECT_EQ("", parent_resource_id
);
113 EXPECT_FALSE(entry
.deleted());
114 EXPECT_FALSE(entry
.shared_with_me());
115 EXPECT_FALSE(entry
.shared());
117 // 2011-12-12T23:28:52.783Z
118 base::Time::Exploded exploded
;
119 exploded
.year
= 2011;
121 exploded
.day_of_month
= 12;
122 exploded
.day_of_week
= 1; // Monday
124 exploded
.minute
= 28;
125 exploded
.second
= 52;
126 exploded
.millisecond
= 783;
127 const base::Time expected_last_modified_time
=
128 base::Time::FromUTCExploded(exploded
);
130 // 2011-12-12T23:28:46.686Z
131 exploded
.year
= 2011;
133 exploded
.day_of_month
= 12;
134 exploded
.day_of_week
= 1; // Monday
136 exploded
.minute
= 28;
137 exploded
.second
= 46;
138 exploded
.millisecond
= 686;
139 const base::Time expected_creation_time
=
140 base::Time::FromUTCExploded(exploded
);
142 // 2011-12-13T02:12:18.527Z
143 exploded
.year
= 2011;
145 exploded
.day_of_month
= 13;
146 exploded
.day_of_week
= 2; // Tuesday
148 exploded
.minute
= 12;
149 exploded
.second
= 18;
150 exploded
.millisecond
= 527;
151 const base::Time expected_last_accessed_time
=
152 base::Time::FromUTCExploded(exploded
);
154 EXPECT_EQ(expected_last_modified_time
.ToInternalValue(),
155 entry
.file_info().last_modified());
156 EXPECT_EQ(expected_last_accessed_time
.ToInternalValue(),
157 entry
.file_info().last_accessed());
158 EXPECT_EQ(expected_creation_time
.ToInternalValue(),
159 entry
.file_info().creation_time());
161 EXPECT_EQ("text/html",
162 entry
.file_specific_info().content_mime_type());
163 EXPECT_TRUE(entry
.file_specific_info().is_hosted_document());
164 EXPECT_EQ("https://3_document_alternate_link/",
165 entry
.file_specific_info().alternate_url());
167 // The size should be 0 for a hosted document.
168 EXPECT_EQ(0, entry
.file_info().size());
169 EXPECT_FALSE(entry
.file_info().is_directory());
172 TEST(ResourceEntryConversionTest
,
173 ConvertToResourceEntry_Directory
) {
174 scoped_ptr
<base::Value
> value
=
175 google_apis::test_util::LoadJSONFile(
176 "gdata/directory_entry.json");
177 ASSERT_TRUE(value
.get());
179 scoped_ptr
<google_apis::ResourceEntry
> gdata_resource_entry(
180 google_apis::ResourceEntry::ExtractAndParse(*value
));
181 ASSERT_TRUE(gdata_resource_entry
.get());
184 std::string parent_resource_id
;
185 EXPECT_TRUE(ConvertToResourceEntry(*gdata_resource_entry
, &entry
,
186 &parent_resource_id
));
188 EXPECT_EQ("Sub Directory Folder", entry
.title());
189 EXPECT_EQ("Sub Directory Folder", entry
.base_name());
190 EXPECT_EQ("folder:sub_dir_folder_resource_id", entry
.resource_id());
191 // The parent resource ID should be obtained as this is a sub directory
192 // under a non-root directory.
193 EXPECT_EQ("folder:1_folder_resource_id", parent_resource_id
);
195 EXPECT_FALSE(entry
.deleted());
196 EXPECT_FALSE(entry
.shared_with_me());
197 EXPECT_FALSE(entry
.shared());
199 // 2011-04-01T18:34:08.234Z
200 base::Time::Exploded exploded
;
201 exploded
.year
= 2011;
203 exploded
.day_of_month
= 01;
204 exploded
.day_of_week
= 5; // Friday
206 exploded
.minute
= 34;
208 exploded
.millisecond
= 234;
209 const base::Time expected_last_modified_time
=
210 base::Time::FromUTCExploded(exploded
);
212 // 2010-11-07T05:03:54.719Z
213 exploded
.year
= 2010;
215 exploded
.day_of_month
= 7;
216 exploded
.day_of_week
= 0; // Sunday
219 exploded
.second
= 54;
220 exploded
.millisecond
= 719;
221 const base::Time expected_creation_time
=
222 base::Time::FromUTCExploded(exploded
);
224 // 2011-11-02T04:37:38.469Z
225 exploded
.year
= 2011;
227 exploded
.day_of_month
= 2;
228 exploded
.day_of_week
= 2; // Tuesday
230 exploded
.minute
= 37;
231 exploded
.second
= 38;
232 exploded
.millisecond
= 469;
233 const base::Time expected_last_accessed_time
=
234 base::Time::FromUTCExploded(exploded
);
236 EXPECT_EQ(expected_last_modified_time
.ToInternalValue(),
237 entry
.file_info().last_modified());
238 EXPECT_EQ(expected_last_accessed_time
.ToInternalValue(),
239 entry
.file_info().last_accessed());
240 EXPECT_EQ(expected_creation_time
.ToInternalValue(),
241 entry
.file_info().creation_time());
243 EXPECT_TRUE(entry
.file_info().is_directory());
246 TEST(ResourceEntryConversionTest
,
247 ConvertToResourceEntry_DeletedHostedDocument
) {
248 scoped_ptr
<base::Value
> value
=
249 google_apis::test_util::LoadJSONFile(
250 "gdata/deleted_hosted_document_entry.json");
251 ASSERT_TRUE(value
.get());
253 scoped_ptr
<google_apis::ResourceEntry
> gdata_resource_entry(
254 google_apis::ResourceEntry::ExtractAndParse(*value
));
255 ASSERT_TRUE(gdata_resource_entry
.get());
258 std::string parent_resource_id
;
259 EXPECT_TRUE(ConvertToResourceEntry(*gdata_resource_entry
, &entry
,
260 &parent_resource_id
));
262 EXPECT_EQ("Deleted document", entry
.title());
263 EXPECT_EQ("Deleted document.gdoc", entry
.base_name());
264 EXPECT_EQ("document:deleted_in_root_id", entry
.resource_id());
265 EXPECT_EQ("", parent_resource_id
);
267 EXPECT_TRUE(entry
.deleted()); // The document was deleted.
268 EXPECT_FALSE(entry
.shared_with_me());
269 EXPECT_FALSE(entry
.shared());
271 // 2012-04-10T22:50:55.797Z
272 base::Time::Exploded exploded
;
273 exploded
.year
= 2012;
275 exploded
.day_of_month
= 10;
276 exploded
.day_of_week
= 2; // Tuesday
278 exploded
.minute
= 50;
279 exploded
.second
= 55;
280 exploded
.millisecond
= 797;
281 const base::Time expected_last_modified_time
=
282 base::Time::FromUTCExploded(exploded
);
284 // 2012-04-10T22:50:53.237Z
285 exploded
.year
= 2012;
287 exploded
.day_of_month
= 10;
288 exploded
.day_of_week
= 2; // Tuesday
290 exploded
.minute
= 50;
291 exploded
.second
= 53;
292 exploded
.millisecond
= 237;
293 const base::Time expected_creation_time
=
294 base::Time::FromUTCExploded(exploded
);
296 // 2012-04-10T22:50:55.797Z
297 exploded
.year
= 2012;
299 exploded
.day_of_month
= 10;
300 exploded
.day_of_week
= 2; // Tuesday
302 exploded
.minute
= 50;
303 exploded
.second
= 55;
304 exploded
.millisecond
= 797;
305 const base::Time expected_last_accessed_time
=
306 base::Time::FromUTCExploded(exploded
);
308 EXPECT_EQ(expected_last_modified_time
.ToInternalValue(),
309 entry
.file_info().last_modified());
310 EXPECT_EQ(expected_last_accessed_time
.ToInternalValue(),
311 entry
.file_info().last_accessed());
312 EXPECT_EQ(expected_creation_time
.ToInternalValue(),
313 entry
.file_info().creation_time());
315 EXPECT_EQ("text/html",
316 entry
.file_specific_info().content_mime_type());
317 EXPECT_TRUE(entry
.file_specific_info().is_hosted_document());
318 EXPECT_EQ("https://alternate/document%3Adeleted_in_root_id/edit",
319 entry
.file_specific_info().alternate_url());
321 // The size should be 0 for a hosted document.
322 EXPECT_EQ(0, entry
.file_info().size());
325 TEST(ResourceEntryConversionTest
,
326 ConvertToResourceEntry_SharedWithMeEntry
) {
327 scoped_ptr
<base::Value
> value
= google_apis::test_util::LoadJSONFile(
328 "gdata/shared_with_me_entry.json");
329 ASSERT_TRUE(value
.get());
331 scoped_ptr
<google_apis::ResourceEntry
> gdata_resource_entry(
332 google_apis::ResourceEntry::ExtractAndParse(*value
));
333 ASSERT_TRUE(gdata_resource_entry
.get());
336 std::string parent_resource_id
;
337 EXPECT_TRUE(ConvertToResourceEntry(*gdata_resource_entry
, &entry
,
338 &parent_resource_id
));
339 EXPECT_TRUE(entry
.shared_with_me());
340 EXPECT_TRUE(entry
.shared());
343 TEST(ResourceEntryConversionTest
, ToPlatformFileInfo
) {
345 entry
.mutable_file_info()->set_size(12345);
346 entry
.mutable_file_info()->set_is_directory(true);
347 entry
.mutable_file_info()->set_is_symbolic_link(true);
348 entry
.mutable_file_info()->set_creation_time(999);
349 entry
.mutable_file_info()->set_last_modified(123456789);
350 entry
.mutable_file_info()->set_last_accessed(987654321);
352 base::PlatformFileInfo file_info
;
353 ConvertResourceEntryToPlatformFileInfo(entry
, &file_info
);
354 EXPECT_EQ(entry
.file_info().size(), file_info
.size
);
355 EXPECT_EQ(entry
.file_info().is_directory(), file_info
.is_directory
);
356 EXPECT_EQ(entry
.file_info().is_symbolic_link(), file_info
.is_symbolic_link
);
357 EXPECT_EQ(base::Time::FromInternalValue(entry
.file_info().creation_time()),
358 file_info
.creation_time
);
359 EXPECT_EQ(base::Time::FromInternalValue(entry
.file_info().last_modified()),
360 file_info
.last_modified
);
361 EXPECT_EQ(base::Time::FromInternalValue(entry
.file_info().last_accessed()),
362 file_info
.last_accessed
);
365 TEST(ResourceEntryConversionTest
, FromPlatformFileInfo
) {
366 base::PlatformFileInfo file_info
;
367 file_info
.size
= 12345;
368 file_info
.is_directory
= true;
369 file_info
.is_symbolic_link
= true;
370 file_info
.last_modified
=
371 base::Time::UnixEpoch() + base::TimeDelta::FromDays(999);
372 file_info
.last_accessed
=
373 base::Time::UnixEpoch() + base::TimeDelta::FromDays(12345);
374 file_info
.creation_time
=
375 base::Time::UnixEpoch() + base::TimeDelta::FromDays(54321);
378 SetPlatformFileInfoToResourceEntry(file_info
, &entry
);
380 EXPECT_EQ(file_info
.size
, entry
.file_info().size());
381 EXPECT_EQ(file_info
.is_directory
, entry
.file_info().is_directory());
382 EXPECT_EQ(file_info
.is_symbolic_link
, entry
.file_info().is_symbolic_link());
383 EXPECT_EQ(file_info
.creation_time
,
384 base::Time::FromInternalValue(entry
.file_info().creation_time()));
385 EXPECT_EQ(file_info
.last_modified
,
386 base::Time::FromInternalValue(entry
.file_info().last_modified()));
387 EXPECT_EQ(file_info
.last_accessed
,
388 base::Time::FromInternalValue(entry
.file_info().last_accessed()));
391 TEST(ResourceEntryConversionTest
, ConvertToResourceEntry_ImageMediaMetadata
) {
392 google_apis::ResourceEntry entry_all_fields
;
393 google_apis::ResourceEntry entry_zero_fields
;
394 google_apis::ResourceEntry entry_no_fields
;
396 entry_all_fields
.set_image_width(640);
397 entry_all_fields
.set_image_height(480);
398 entry_all_fields
.set_image_rotation(90);
399 entry_all_fields
.set_kind(google_apis::ENTRY_KIND_FILE
);
401 entry_zero_fields
.set_image_width(0);
402 entry_zero_fields
.set_image_height(0);
403 entry_zero_fields
.set_image_rotation(0);
404 entry_zero_fields
.set_kind(google_apis::ENTRY_KIND_FILE
);
406 entry_no_fields
.set_kind(google_apis::ENTRY_KIND_FILE
);
410 std::string parent_resource_id
;
411 EXPECT_TRUE(ConvertToResourceEntry(entry_all_fields
, &entry
,
412 &parent_resource_id
));
413 EXPECT_EQ(640, entry
.file_specific_info().image_width());
414 EXPECT_EQ(480, entry
.file_specific_info().image_height());
415 EXPECT_EQ(90, entry
.file_specific_info().image_rotation());
419 std::string parent_resource_id
;
420 EXPECT_TRUE(ConvertToResourceEntry(entry_zero_fields
, &entry
,
421 &parent_resource_id
));
422 EXPECT_TRUE(entry
.file_specific_info().has_image_width());
423 EXPECT_TRUE(entry
.file_specific_info().has_image_height());
424 EXPECT_TRUE(entry
.file_specific_info().has_image_rotation());
425 EXPECT_EQ(0, entry
.file_specific_info().image_width());
426 EXPECT_EQ(0, entry
.file_specific_info().image_height());
427 EXPECT_EQ(0, entry
.file_specific_info().image_rotation());
431 std::string parent_resource_id
;
432 EXPECT_TRUE(ConvertToResourceEntry(entry_no_fields
, &entry
,
433 &parent_resource_id
));
434 EXPECT_FALSE(entry
.file_specific_info().has_image_width());
435 EXPECT_FALSE(entry
.file_specific_info().has_image_height());
436 EXPECT_FALSE(entry
.file_specific_info().has_image_rotation());