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 "components/drive/resource_entry_conversion.h"
7 #include "base/time/time.h"
8 #include "components/drive/drive.pb.h"
9 #include "components/drive/drive_api_util.h"
10 #include "google_apis/drive/drive_api_parser.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 base::Time
GetTestTime() {
18 // 2011-12-14-T00:40:47.330Z
19 base::Time::Exploded exploded
;
22 exploded
.day_of_month
= 14;
23 exploded
.day_of_week
= 2; // Tuesday
27 exploded
.millisecond
= 330;
28 return base::Time::FromUTCExploded(exploded
);
33 TEST(ResourceEntryConversionTest
, ConvertToResourceEntry_File
) {
34 google_apis::FileResource file_resource
;
35 file_resource
.set_title("File 1.mp3");
36 file_resource
.set_file_id("resource_id");
37 file_resource
.set_created_date(GetTestTime());
38 file_resource
.set_modified_date(
39 GetTestTime() + base::TimeDelta::FromSeconds(10));
40 file_resource
.set_mime_type("audio/mpeg");
41 file_resource
.set_alternate_link(GURL("https://file_link_alternate"));
42 file_resource
.set_file_size(892721);
43 file_resource
.set_md5_checksum("3b4382ebefec6e743578c76bbd0575ce");
46 std::string parent_resource_id
;
47 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
48 file_resource
, &entry
, &parent_resource_id
));
50 EXPECT_EQ(file_resource
.title(), entry
.title());
51 EXPECT_EQ(file_resource
.title(), entry
.base_name());
52 EXPECT_EQ(file_resource
.file_id(), entry
.resource_id());
53 EXPECT_EQ("", parent_resource_id
);
55 EXPECT_FALSE(entry
.deleted());
56 EXPECT_FALSE(entry
.shared_with_me());
57 EXPECT_FALSE(entry
.shared());
59 EXPECT_EQ(file_resource
.modified_date().ToInternalValue(),
60 entry
.file_info().last_modified());
61 // Last accessed value equal to 0 means that the file has never been viewed.
62 EXPECT_EQ(0, entry
.file_info().last_accessed());
63 EXPECT_EQ(file_resource
.created_date().ToInternalValue(),
64 entry
.file_info().creation_time());
66 EXPECT_EQ(file_resource
.mime_type(),
67 entry
.file_specific_info().content_mime_type());
68 EXPECT_FALSE(entry
.file_specific_info().is_hosted_document());
69 EXPECT_EQ(file_resource
.alternate_link().spec(),
70 entry
.file_specific_info().alternate_url());
72 // Regular file specific fields.
73 EXPECT_EQ(file_resource
.file_size(), entry
.file_info().size());
74 EXPECT_EQ(file_resource
.md5_checksum(), entry
.file_specific_info().md5());
75 EXPECT_FALSE(entry
.file_info().is_directory());
78 TEST(ResourceEntryConversionTest
,
79 ConvertFileResourceToResourceEntry_HostedDocument
) {
80 google_apis::FileResource file_resource
;
81 file_resource
.set_title("Document 1");
82 file_resource
.set_file_id("resource_id");
83 file_resource
.set_created_date(GetTestTime());
84 file_resource
.set_modified_date(
85 GetTestTime() + base::TimeDelta::FromSeconds(10));
86 file_resource
.set_last_viewed_by_me_date(
87 GetTestTime() + base::TimeDelta::FromSeconds(20));
88 file_resource
.set_mime_type(util::kGoogleDocumentMimeType
);
89 file_resource
.set_alternate_link(GURL("https://file_link_alternate"));
90 // Do not set file size to represent a hosted document.
93 std::string parent_resource_id
;
94 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
95 file_resource
, &entry
, &parent_resource_id
));
97 EXPECT_EQ(file_resource
.title(), entry
.title());
98 EXPECT_EQ(file_resource
.title() + ".gdoc",
99 entry
.base_name()); // The suffix added.
100 EXPECT_EQ(".gdoc", entry
.file_specific_info().document_extension());
101 EXPECT_EQ(file_resource
.file_id(), entry
.resource_id());
102 EXPECT_EQ("", parent_resource_id
);
104 EXPECT_FALSE(entry
.deleted());
105 EXPECT_FALSE(entry
.shared_with_me());
106 EXPECT_FALSE(entry
.shared());
108 EXPECT_EQ(file_resource
.modified_date().ToInternalValue(),
109 entry
.file_info().last_modified());
110 EXPECT_EQ(file_resource
.last_viewed_by_me_date().ToInternalValue(),
111 entry
.file_info().last_accessed());
112 EXPECT_EQ(file_resource
.created_date().ToInternalValue(),
113 entry
.file_info().creation_time());
115 EXPECT_EQ(file_resource
.mime_type(),
116 entry
.file_specific_info().content_mime_type());
117 EXPECT_TRUE(entry
.file_specific_info().is_hosted_document());
118 EXPECT_EQ(file_resource
.alternate_link().spec(),
119 entry
.file_specific_info().alternate_url());
121 // The size should be 0 for a hosted document.
122 EXPECT_EQ(0, entry
.file_info().size());
123 EXPECT_FALSE(entry
.file_info().is_directory());
126 TEST(ResourceEntryConversionTest
,
127 ConvertFileResourceToResourceEntry_Directory
) {
128 google_apis::FileResource file_resource
;
129 file_resource
.set_title("Folder");
130 file_resource
.set_file_id("resource_id");
131 file_resource
.set_created_date(GetTestTime());
132 file_resource
.set_modified_date(
133 GetTestTime() + base::TimeDelta::FromSeconds(10));
134 file_resource
.set_last_viewed_by_me_date(
135 GetTestTime() + base::TimeDelta::FromSeconds(20));
136 file_resource
.set_mime_type(util::kDriveFolderMimeType
);
138 google_apis::ParentReference parent
;
139 parent
.set_file_id("parent_resource_id");
140 file_resource
.mutable_parents()->push_back(parent
);
143 std::string parent_resource_id
;
144 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
145 file_resource
, &entry
, &parent_resource_id
));
147 EXPECT_EQ(file_resource
.title(), entry
.title());
148 EXPECT_EQ(file_resource
.title(), entry
.base_name());
149 EXPECT_EQ(file_resource
.file_id(), entry
.resource_id());
150 // The parent resource ID should be obtained as this is a sub directory
151 // under a non-root directory.
152 EXPECT_EQ(parent
.file_id(), parent_resource_id
);
154 EXPECT_FALSE(entry
.deleted());
155 EXPECT_FALSE(entry
.shared_with_me());
156 EXPECT_FALSE(entry
.shared());
158 EXPECT_EQ(file_resource
.modified_date().ToInternalValue(),
159 entry
.file_info().last_modified());
160 EXPECT_EQ(file_resource
.last_viewed_by_me_date().ToInternalValue(),
161 entry
.file_info().last_accessed());
162 EXPECT_EQ(file_resource
.created_date().ToInternalValue(),
163 entry
.file_info().creation_time());
165 EXPECT_TRUE(entry
.file_info().is_directory());
168 TEST(ResourceEntryConversionTest
,
169 ConvertFileResourceToResourceEntry_DeletedHostedDocument
) {
170 google_apis::FileResource file_resource
;
171 file_resource
.set_title("Document 1");
172 file_resource
.set_file_id("resource_id");
173 file_resource
.set_created_date(GetTestTime());
174 file_resource
.set_modified_date(
175 GetTestTime() + base::TimeDelta::FromSeconds(10));
176 file_resource
.set_last_viewed_by_me_date(
177 GetTestTime() + base::TimeDelta::FromSeconds(20));
178 file_resource
.set_mime_type(util::kGoogleDocumentMimeType
);
179 file_resource
.set_alternate_link(GURL("https://file_link_alternate"));
180 file_resource
.mutable_labels()->set_trashed(true);
183 std::string parent_resource_id
;
184 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
185 file_resource
, &entry
, &parent_resource_id
));
187 EXPECT_EQ(file_resource
.title(), entry
.title());
188 EXPECT_EQ(file_resource
.title() + ".gdoc", entry
.base_name());
189 EXPECT_EQ(file_resource
.file_id(), entry
.resource_id());
190 EXPECT_EQ("", parent_resource_id
);
192 EXPECT_TRUE(entry
.deleted()); // The document was deleted.
193 EXPECT_FALSE(entry
.shared_with_me());
194 EXPECT_FALSE(entry
.shared());
196 EXPECT_EQ(file_resource
.modified_date().ToInternalValue(),
197 entry
.file_info().last_modified());
198 EXPECT_EQ(file_resource
.last_viewed_by_me_date().ToInternalValue(),
199 entry
.file_info().last_accessed());
200 EXPECT_EQ(file_resource
.created_date().ToInternalValue(),
201 entry
.file_info().creation_time());
203 EXPECT_EQ(file_resource
.mime_type(),
204 entry
.file_specific_info().content_mime_type());
205 EXPECT_TRUE(entry
.file_specific_info().is_hosted_document());
206 EXPECT_EQ(file_resource
.alternate_link().spec(),
207 entry
.file_specific_info().alternate_url());
209 // The size should be 0 for a hosted document.
210 EXPECT_EQ(0, entry
.file_info().size());
213 TEST(ResourceEntryConversionTest
, ConvertChangeResourceToResourceEntry
) {
214 google_apis::ChangeResource change_resource
;
215 change_resource
.set_file(make_scoped_ptr(new google_apis::FileResource
));
216 change_resource
.set_file_id("resource_id");
217 change_resource
.set_modification_date(GetTestTime());
219 google_apis::FileResource
* file_resource
= change_resource
.mutable_file();
220 file_resource
->set_title("File 1.mp3");
221 file_resource
->set_file_id("resource_id");
222 // Set dummy file size to declare that this is a regular file.
223 file_resource
->set_file_size(12345);
226 std::string parent_resource_id
;
227 EXPECT_TRUE(ConvertChangeResourceToResourceEntry(
228 change_resource
, &entry
, &parent_resource_id
));
230 EXPECT_EQ(change_resource
.file_id(), entry
.resource_id());
231 EXPECT_EQ(change_resource
.modification_date().ToInternalValue(),
232 entry
.modification_date());
234 EXPECT_EQ(file_resource
->title(), entry
.title());
235 EXPECT_EQ(file_resource
->title(), entry
.base_name());
236 EXPECT_EQ("", parent_resource_id
);
238 EXPECT_FALSE(entry
.deleted());
241 TEST(ResourceEntryConversionTest
,
242 ConvertChangeResourceToResourceEntry_Trashed
) {
243 google_apis::ChangeResource change_resource
;
244 change_resource
.set_file(make_scoped_ptr(new google_apis::FileResource
));
245 change_resource
.set_file_id("resource_id");
246 change_resource
.set_modification_date(GetTestTime());
248 google_apis::FileResource
* file_resource
= change_resource
.mutable_file();
249 file_resource
->set_title("File 1.mp3");
250 file_resource
->set_file_id("resource_id");
251 // Set dummy file size to declare that this is a regular file.
252 file_resource
->set_file_size(12345);
253 file_resource
->mutable_labels()->set_trashed(true);
256 std::string parent_resource_id
;
257 EXPECT_TRUE(ConvertChangeResourceToResourceEntry(
258 change_resource
, &entry
, &parent_resource_id
));
260 EXPECT_EQ(change_resource
.file_id(), entry
.resource_id());
261 EXPECT_EQ(change_resource
.modification_date().ToInternalValue(),
262 entry
.modification_date());
264 EXPECT_EQ(file_resource
->title(), entry
.title());
265 EXPECT_EQ(file_resource
->title(), entry
.base_name());
266 EXPECT_EQ("", parent_resource_id
);
268 EXPECT_TRUE(entry
.deleted());
271 TEST(ResourceEntryConversionTest
,
272 ConvertChangeResourceToResourceEntry_Deleted
) {
273 google_apis::ChangeResource change_resource
;
274 change_resource
.set_deleted(true);
275 change_resource
.set_file_id("resource_id");
276 change_resource
.set_modification_date(GetTestTime());
279 std::string parent_resource_id
;
280 EXPECT_TRUE(ConvertChangeResourceToResourceEntry(
281 change_resource
, &entry
, &parent_resource_id
));
283 EXPECT_EQ(change_resource
.file_id(), entry
.resource_id());
284 EXPECT_EQ("", parent_resource_id
);
286 EXPECT_TRUE(entry
.deleted());
288 EXPECT_EQ(change_resource
.modification_date().ToInternalValue(),
289 entry
.modification_date());
292 TEST(ResourceEntryConversionTest
,
293 ConvertFileResourceToResourceEntry_SharedWithMeEntry
) {
294 google_apis::FileResource file_resource
;
295 file_resource
.set_shared(true);
296 file_resource
.set_shared_with_me_date(GetTestTime());
299 std::string parent_resource_id
;
300 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
301 file_resource
, &entry
, &parent_resource_id
));
302 EXPECT_TRUE(entry
.shared_with_me());
303 EXPECT_TRUE(entry
.shared());
306 TEST(ResourceEntryConversionTest
, ToPlatformFileInfo
) {
308 entry
.mutable_file_info()->set_size(12345);
309 entry
.mutable_file_info()->set_is_directory(true);
310 entry
.mutable_file_info()->set_is_symbolic_link(true);
311 entry
.mutable_file_info()->set_creation_time(999);
312 entry
.mutable_file_info()->set_last_modified(123456789);
313 entry
.mutable_file_info()->set_last_accessed(987654321);
315 base::File::Info file_info
;
316 ConvertResourceEntryToFileInfo(entry
, &file_info
);
317 EXPECT_EQ(entry
.file_info().size(), file_info
.size
);
318 EXPECT_EQ(entry
.file_info().is_directory(), file_info
.is_directory
);
319 EXPECT_EQ(entry
.file_info().is_symbolic_link(), file_info
.is_symbolic_link
);
320 EXPECT_EQ(base::Time::FromInternalValue(entry
.file_info().creation_time()),
321 file_info
.creation_time
);
322 EXPECT_EQ(base::Time::FromInternalValue(entry
.file_info().last_modified()),
323 file_info
.last_modified
);
324 EXPECT_EQ(base::Time::FromInternalValue(entry
.file_info().last_accessed()),
325 file_info
.last_accessed
);
328 TEST(ResourceEntryConversionTest
,
329 ConvertFileResourceToResourceEntry_ImageMediaMetadata
) {
330 google_apis::FileResource entry_all_fields
;
331 google_apis::FileResource entry_zero_fields
;
332 google_apis::FileResource entry_no_fields
;
334 entry_all_fields
.mutable_image_media_metadata()->set_width(640);
335 entry_all_fields
.mutable_image_media_metadata()->set_height(480);
336 entry_all_fields
.mutable_image_media_metadata()->set_rotation(90);
338 entry_zero_fields
.mutable_image_media_metadata()->set_width(0);
339 entry_zero_fields
.mutable_image_media_metadata()->set_height(0);
340 entry_zero_fields
.mutable_image_media_metadata()->set_rotation(0);
344 std::string parent_resource_id
;
345 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
346 entry_all_fields
, &entry
, &parent_resource_id
));
347 EXPECT_EQ(640, entry
.file_specific_info().image_width());
348 EXPECT_EQ(480, entry
.file_specific_info().image_height());
349 EXPECT_EQ(90, entry
.file_specific_info().image_rotation());
353 std::string parent_resource_id
;
354 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
355 entry_zero_fields
, &entry
, &parent_resource_id
));
356 EXPECT_TRUE(entry
.file_specific_info().has_image_width());
357 EXPECT_TRUE(entry
.file_specific_info().has_image_height());
358 EXPECT_TRUE(entry
.file_specific_info().has_image_rotation());
359 EXPECT_EQ(0, entry
.file_specific_info().image_width());
360 EXPECT_EQ(0, entry
.file_specific_info().image_height());
361 EXPECT_EQ(0, entry
.file_specific_info().image_rotation());
365 std::string parent_resource_id
;
366 EXPECT_TRUE(ConvertFileResourceToResourceEntry(
367 entry_no_fields
, &entry
, &parent_resource_id
));
368 EXPECT_FALSE(entry
.file_specific_info().has_image_width());
369 EXPECT_FALSE(entry
.file_specific_info().has_image_height());
370 EXPECT_FALSE(entry
.file_specific_info().has_image_rotation());