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/google_apis/drive_api_parser.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/json/json_file_value_serializer.h"
10 #include "base/path_service.h"
11 #include "base/string16.h"
12 #include "base/time.h"
13 #include "base/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
16 #include "chrome/browser/google_apis/test_util.h"
17 #include "chrome/browser/google_apis/time_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 using base::DictionaryValue
;
22 using base::ListValue
;
24 namespace google_apis
{
26 // TODO(nhiroki): Make it possible to run these tests on any platforms after
27 // moving json files to out of 'chromeos' directory (http://crbug.com/149788).
28 #if defined(OS_CHROMEOS)
29 // Test about resource parsing.
30 TEST(DriveAPIParserTest
, AboutResourceParser
) {
32 scoped_ptr
<Value
> document
= test_util::LoadJSONFile(
33 "chromeos/drive/about.json");
34 ASSERT_TRUE(document
.get());
36 ASSERT_EQ(Value::TYPE_DICTIONARY
, document
->GetType());
37 scoped_ptr
<AboutResource
> resource(new AboutResource());
38 EXPECT_TRUE(resource
->Parse(*document
));
40 EXPECT_EQ("0AIv7G8yEYAWHUk9123", resource
->root_folder_id());
41 EXPECT_EQ(5368709120LL, resource
->quota_bytes_total());
42 EXPECT_EQ(1073741824LL, resource
->quota_bytes_used());
43 EXPECT_EQ(8177LL, resource
->largest_change_id());
46 TEST(DriveAPIParserTest
, AboutResourceFromAccountMetadata
) {
47 AccountMetadata account_metadata
;
48 // Set up AccountMetadata instance.
50 account_metadata
.set_quota_bytes_total(10000);
51 account_metadata
.set_quota_bytes_used(1000);
52 account_metadata
.set_largest_changestamp(100);
55 scoped_ptr
<AboutResource
> about_resource(
56 AboutResource::CreateFromAccountMetadata(account_metadata
,
59 EXPECT_EQ(10000, about_resource
->quota_bytes_total());
60 EXPECT_EQ(1000, about_resource
->quota_bytes_used());
61 EXPECT_EQ(100, about_resource
->largest_change_id());
62 EXPECT_EQ("dummy_root_id", about_resource
->root_folder_id());
65 // Test app list parsing.
66 TEST(DriveAPIParserTest
, AppListParser
) {
68 scoped_ptr
<Value
> document
= test_util::LoadJSONFile(
69 "chromeos/drive/applist.json");
70 ASSERT_TRUE(document
.get());
72 ASSERT_EQ(Value::TYPE_DICTIONARY
, document
->GetType());
73 scoped_ptr
<AppList
> applist(new AppList
);
74 EXPECT_TRUE(applist
->Parse(*document
));
76 EXPECT_EQ("\"Jm4BaSnCWNND-noZsHINRqj4ABC/tuqRBw0lvjUdPtc_2msA1tN4XYZ\"",
78 ASSERT_EQ(2U, applist
->items().size());
80 const AppResource
& app1
= *applist
->items()[0];
81 EXPECT_EQ("123456788192", app1
.application_id());
82 EXPECT_EQ("Drive app 1", app1
.name());
83 EXPECT_EQ("", app1
.object_type());
84 EXPECT_TRUE(app1
.supports_create());
85 EXPECT_TRUE(app1
.supports_import());
86 EXPECT_TRUE(app1
.is_installed());
87 EXPECT_FALSE(app1
.is_authorized());
88 EXPECT_EQ("https://chrome.google.com/webstore/detail/"
89 "abcdefghabcdefghabcdefghabcdefgh",
90 app1
.product_url().spec());
92 ASSERT_EQ(1U, app1
.primary_mimetypes().size());
93 EXPECT_EQ("application/vnd.google-apps.drive-sdk.123456788192",
94 *app1
.primary_mimetypes()[0]);
96 ASSERT_EQ(2U, app1
.secondary_mimetypes().size());
97 EXPECT_EQ("text/html", *app1
.secondary_mimetypes()[0]);
98 EXPECT_EQ("text/plain", *app1
.secondary_mimetypes()[1]);
100 ASSERT_EQ(2U, app1
.primary_file_extensions().size());
101 EXPECT_EQ("exe", *app1
.primary_file_extensions()[0]);
102 EXPECT_EQ("com", *app1
.primary_file_extensions()[1]);
104 EXPECT_EQ(0U, app1
.secondary_file_extensions().size());
106 ASSERT_EQ(6U, app1
.icons().size());
107 const DriveAppIcon
& icon1
= *app1
.icons()[0];
108 EXPECT_EQ(DriveAppIcon::APPLICATION
, icon1
.category());
109 EXPECT_EQ(10, icon1
.icon_side_length());
110 EXPECT_EQ("http://www.example.com/10.png", icon1
.icon_url().spec());
112 const DriveAppIcon
& icon6
= *app1
.icons()[5];
113 EXPECT_EQ(DriveAppIcon::SHARED_DOCUMENT
, icon6
.category());
114 EXPECT_EQ(16, icon6
.icon_side_length());
115 EXPECT_EQ("http://www.example.com/ds16.png", icon6
.icon_url().spec());
118 const AppResource
& app2
= *applist
->items()[1];
119 EXPECT_EQ("876543210000", app2
.application_id());
120 EXPECT_EQ("Drive app 2", app2
.name());
121 EXPECT_EQ("", app2
.object_type());
122 EXPECT_FALSE(app2
.supports_create());
123 EXPECT_FALSE(app2
.supports_import());
124 EXPECT_TRUE(app2
.is_installed());
125 EXPECT_FALSE(app2
.is_authorized());
126 EXPECT_EQ("https://chrome.google.com/webstore/detail/"
127 "hgfedcbahgfedcbahgfedcbahgfedcba",
128 app2
.product_url().spec());
130 ASSERT_EQ(3U, app2
.primary_mimetypes().size());
131 EXPECT_EQ("image/jpeg", *app2
.primary_mimetypes()[0]);
132 EXPECT_EQ("image/png", *app2
.primary_mimetypes()[1]);
133 EXPECT_EQ("application/vnd.google-apps.drive-sdk.876543210000",
134 *app2
.primary_mimetypes()[2]);
136 EXPECT_EQ(0U, app2
.secondary_mimetypes().size());
137 EXPECT_EQ(0U, app2
.primary_file_extensions().size());
138 EXPECT_EQ(0U, app2
.secondary_file_extensions().size());
140 ASSERT_EQ(3U, app2
.icons().size());
141 const DriveAppIcon
& icon2
= *app2
.icons()[1];
142 EXPECT_EQ(DriveAppIcon::DOCUMENT
, icon2
.category());
143 EXPECT_EQ(10, icon2
.icon_side_length());
144 EXPECT_EQ("http://www.example.com/d10.png", icon2
.icon_url().spec());
147 TEST(DriveAPIParserTest
, AppListFromAccountMetadata
) {
148 AccountMetadata account_metadata
;
149 // Set up AccountMetadata instance.
151 ScopedVector
<InstalledApp
> installed_apps
;
152 scoped_ptr
<InstalledApp
> installed_app(new InstalledApp
);
153 installed_app
->set_app_id("app_id");
154 installed_app
->set_app_name("name");
155 installed_app
->set_object_type("object_type");
156 installed_app
->set_supports_create(true);
159 ScopedVector
<Link
> links
;
160 scoped_ptr
<Link
> link(new Link
);
161 link
->set_type(Link::LINK_PRODUCT
);
162 link
->set_href(GURL("http://product/url"));
163 links
.push_back(link
.release());
164 installed_app
->set_links(&links
);
167 ScopedVector
<std::string
> primary_mimetypes
;
168 primary_mimetypes
.push_back(new std::string("primary_mimetype"));
169 installed_app
->set_primary_mimetypes(&primary_mimetypes
);
172 ScopedVector
<std::string
> secondary_mimetypes
;
173 secondary_mimetypes
.push_back(new std::string("secondary_mimetype"));
174 installed_app
->set_secondary_mimetypes(&secondary_mimetypes
);
177 ScopedVector
<std::string
> primary_extensions
;
178 primary_extensions
.push_back(new std::string("primary_extension"));
179 installed_app
->set_primary_extensions(&primary_extensions
);
182 ScopedVector
<std::string
> secondary_extensions
;
183 secondary_extensions
.push_back(new std::string("secondary_extension"));
184 installed_app
->set_secondary_extensions(&secondary_extensions
);
187 ScopedVector
<AppIcon
> app_icons
;
188 scoped_ptr
<AppIcon
> app_icon(new AppIcon
);
189 app_icon
->set_category(AppIcon::ICON_DOCUMENT
);
190 app_icon
->set_icon_side_length(10);
192 ScopedVector
<Link
> links
;
193 scoped_ptr
<Link
> link(new Link
);
194 link
->set_type(Link::LINK_ICON
);
195 link
->set_href(GURL("http://icon/url"));
196 links
.push_back(link
.release());
197 app_icon
->set_links(&links
);
199 app_icons
.push_back(app_icon
.release());
200 installed_app
->set_app_icons(&app_icons
);
203 installed_apps
.push_back(installed_app
.release());
204 account_metadata
.set_installed_apps(&installed_apps
);
207 scoped_ptr
<AppList
> app_list(
208 AppList::CreateFromAccountMetadata(account_metadata
));
209 const ScopedVector
<AppResource
>& items
= app_list
->items();
210 ASSERT_EQ(1U, items
.size());
212 const AppResource
& app_resource
= *items
[0];
213 EXPECT_EQ("app_id", app_resource
.application_id());
214 EXPECT_EQ("name", app_resource
.name());
215 EXPECT_EQ("object_type", app_resource
.object_type());
216 EXPECT_TRUE(app_resource
.supports_create());
217 EXPECT_EQ("http://product/url", app_resource
.product_url().spec());
218 const ScopedVector
<std::string
>& primary_mimetypes
=
219 app_resource
.primary_mimetypes();
220 ASSERT_EQ(1U, primary_mimetypes
.size());
221 EXPECT_EQ("primary_mimetype", *primary_mimetypes
[0]);
222 const ScopedVector
<std::string
>& secondary_mimetypes
=
223 app_resource
.secondary_mimetypes();
224 ASSERT_EQ(1U, secondary_mimetypes
.size());
225 EXPECT_EQ("secondary_mimetype", *secondary_mimetypes
[0]);
226 const ScopedVector
<std::string
>& primary_file_extensions
=
227 app_resource
.primary_file_extensions();
228 ASSERT_EQ(1U, primary_file_extensions
.size());
229 EXPECT_EQ("primary_extension", *primary_file_extensions
[0]);
230 const ScopedVector
<std::string
>& secondary_file_extensions
=
231 app_resource
.secondary_file_extensions();
232 ASSERT_EQ(1U, secondary_file_extensions
.size());
233 EXPECT_EQ("secondary_extension", *secondary_file_extensions
[0]);
235 const ScopedVector
<DriveAppIcon
>& icons
= app_resource
.icons();
236 ASSERT_EQ(1U, icons
.size());
237 const DriveAppIcon
& icon
= *icons
[0];
238 EXPECT_EQ(DriveAppIcon::DOCUMENT
, icon
.category());
239 EXPECT_EQ(10, icon
.icon_side_length());
240 EXPECT_EQ("http://icon/url", icon
.icon_url().spec());
243 // Test file list parsing.
244 TEST(DriveAPIParserTest
, FileListParser
) {
246 scoped_ptr
<Value
> document
= test_util::LoadJSONFile(
247 "chromeos/drive/filelist.json");
248 ASSERT_TRUE(document
.get());
250 ASSERT_EQ(Value::TYPE_DICTIONARY
, document
->GetType());
251 scoped_ptr
<FileList
> filelist(new FileList
);
252 EXPECT_TRUE(filelist
->Parse(*document
));
254 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/zyHTfoHpnRHovyi8bWpwK0DXABC\"",
256 EXPECT_EQ("EAIaggELEgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_Ry"
257 "NGJnridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJnr"
258 "idyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgart10AAAA"
259 "ABC", filelist
->next_page_token());
260 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files?pageToken=EAIaggEL"
261 "EgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_RyNGJ"
262 "nridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJ"
263 "nridyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgar"
264 "t10AAAAABC"), filelist
->next_link());
266 ASSERT_EQ(3U, filelist
->items().size());
267 // Check file 1 (a regular file)
268 const FileResource
& file1
= *filelist
->items()[0];
269 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", file1
.file_id());
270 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/MTM0MzM2NzgwMDIXYZ\"",
272 EXPECT_EQ("My first file data", file1
.title());
273 EXPECT_EQ("application/octet-stream", file1
.mime_type());
275 EXPECT_FALSE(file1
.labels().is_starred());
276 EXPECT_FALSE(file1
.labels().is_hidden());
277 EXPECT_FALSE(file1
.labels().is_trashed());
278 EXPECT_FALSE(file1
.labels().is_restricted());
279 EXPECT_TRUE(file1
.labels().is_viewed());
281 base::Time created_time
;
282 ASSERT_TRUE(util::GetTimeFromString("2012-07-24T08:51:16.570Z",
284 EXPECT_EQ(created_time
, file1
.created_date());
286 base::Time modified_time
;
287 ASSERT_TRUE(util::GetTimeFromString("2012-07-27T05:43:20.269Z",
289 EXPECT_EQ(modified_time
, file1
.modified_by_me_date());
291 ASSERT_EQ(1U, file1
.parents().size());
292 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", file1
.parents()[0]->file_id());
293 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
294 "0B4v7G8yEYAWHYW1OcExsUVZLABC"),
295 file1
.parents()[0]->parent_link());
296 EXPECT_FALSE(file1
.parents()[0]->is_root());
298 EXPECT_EQ(GURL("https://www.example.com/download"), file1
.download_url());
299 EXPECT_EQ("ext", file1
.file_extension());
300 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", file1
.md5_checksum());
301 EXPECT_EQ(1000U, file1
.file_size());
303 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
304 "0B4v7G8yEYAWHUmRrU2lMS2hLABC"),
306 EXPECT_EQ(GURL("https://docs.google.com/file/d/"
307 "0B4v7G8yEYAWHUmRrU2lMS2hLABC/edit"),
308 file1
.alternate_link());
309 EXPECT_EQ(GURL("https://docs.google.com/uc?"
310 "id=0B4v7G8yEYAWHUmRrU2lMS2hLABC&export=download"),
311 file1
.web_content_link());
313 // Check file 2 (a Google Document)
314 const FileResource
& file2
= *filelist
->items()[1];
315 EXPECT_EQ("Test Google Document", file2
.title());
316 EXPECT_EQ("application/vnd.google-apps.document", file2
.mime_type());
318 EXPECT_TRUE(file2
.labels().is_starred());
319 EXPECT_TRUE(file2
.labels().is_hidden());
320 EXPECT_TRUE(file2
.labels().is_trashed());
321 EXPECT_TRUE(file2
.labels().is_restricted());
322 EXPECT_TRUE(file2
.labels().is_viewed());
324 EXPECT_EQ(0U, file2
.file_size());
326 ASSERT_EQ(0U, file2
.parents().size());
328 EXPECT_EQ(GURL("https://docs.google.com/a/chromium.org/document/d/"
329 "1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC/preview"),
331 EXPECT_EQ(GURL("https://docs.google.com/feeds/vt?gd=true&"
332 "id=1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC&"
333 "v=3&s=AMedNnoAAAAAUBJyB0g8HbxZaLRnlztxefZPS24LiXYZ&sz=s220"),
334 file2
.thumbnail_link());
336 // Check file 3 (a folder)
337 const FileResource
& file3
= *filelist
->items()[2];
338 EXPECT_EQ(0U, file3
.file_size());
339 EXPECT_EQ("TestFolder", file3
.title());
340 EXPECT_EQ("application/vnd.google-apps.folder", file3
.mime_type());
341 ASSERT_TRUE(file3
.IsDirectory());
343 ASSERT_EQ(1U, file3
.parents().size());
344 EXPECT_EQ("0AIv7G8yEYAWHUk9ABC", file3
.parents()[0]->file_id());
345 EXPECT_TRUE(file3
.parents()[0]->is_root());
348 // Test change list parsing.
349 TEST(DriveAPIParserTest
, ChangeListParser
) {
351 scoped_ptr
<Value
> document
=
352 test_util::LoadJSONFile("chromeos/drive/changelist.json");
353 ASSERT_TRUE(document
.get());
355 ASSERT_EQ(Value::TYPE_DICTIONARY
, document
->GetType());
356 scoped_ptr
<ChangeList
> changelist(new ChangeList
);
357 EXPECT_TRUE(changelist
->Parse(*document
));
359 EXPECT_EQ("\"Lp2bjAtLP341hvGmYHhxjYyBPJ8/BWbu_eylt5f_aGtCN6mGRv9hABC\"",
361 EXPECT_EQ("8929", changelist
->next_page_token());
362 EXPECT_EQ("https://www.googleapis.com/drive/v2/changes?pageToken=8929",
363 changelist
->next_link().spec());
364 EXPECT_EQ(13664, changelist
->largest_change_id());
366 ASSERT_EQ(4U, changelist
->items().size());
368 const ChangeResource
& change1
= *changelist
->items()[0];
369 EXPECT_EQ(8421, change1
.change_id());
370 EXPECT_FALSE(change1
.is_deleted());
371 EXPECT_EQ("1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC", change1
.file_id());
372 EXPECT_EQ(change1
.file_id(), change1
.file().file_id());
374 scoped_ptr
<ResourceEntry
> entry1(
375 ResourceEntry::CreateFromChangeResource(change1
));
376 EXPECT_EQ(change1
.file_id(), entry1
->resource_id());
377 EXPECT_EQ(change1
.is_deleted(), entry1
->deleted());
379 const ChangeResource
& change2
= *changelist
->items()[1];
380 EXPECT_EQ(8424, change2
.change_id());
381 EXPECT_FALSE(change2
.is_deleted());
382 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", change2
.file_id());
383 EXPECT_EQ(change2
.file_id(), change2
.file().file_id());
385 scoped_ptr
<ResourceEntry
> entry2(
386 ResourceEntry::CreateFromChangeResource(change2
));
387 EXPECT_EQ(change2
.file_id(), entry2
->resource_id());
388 EXPECT_EQ(change2
.is_deleted(), entry2
->deleted());
390 const ChangeResource
& change3
= *changelist
->items()[2];
391 EXPECT_EQ(8429, change3
.change_id());
392 EXPECT_FALSE(change3
.is_deleted());
393 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", change3
.file_id());
394 EXPECT_EQ(change3
.file_id(), change3
.file().file_id());
396 scoped_ptr
<ResourceEntry
> entry3(
397 ResourceEntry::CreateFromChangeResource(change3
));
398 EXPECT_EQ(change3
.file_id(), entry3
->resource_id());
399 EXPECT_EQ(change3
.is_deleted(), entry3
->deleted());
402 const ChangeResource
& change4
= *changelist
->items()[3];
403 EXPECT_EQ(8430, change4
.change_id());
404 EXPECT_EQ("ABCv7G8yEYAWHc3Y5X0hMSkJYXYZ", change4
.file_id());
405 EXPECT_TRUE(change4
.is_deleted());
407 scoped_ptr
<ResourceEntry
> entry4(
408 ResourceEntry::CreateFromChangeResource(change4
));
409 EXPECT_EQ(change4
.file_id(), entry4
->resource_id());
410 EXPECT_EQ(change4
.is_deleted(), entry4
->deleted());
413 TEST(DriveAPIParserTest
, HasKind
) {
414 scoped_ptr
<base::Value
> change_list_json(
415 test_util::LoadJSONFile("chromeos/drive/changelist.json"));
416 scoped_ptr
<base::Value
> file_list_json(
417 test_util::LoadJSONFile("chromeos/drive/filelist.json"));
419 EXPECT_TRUE(ChangeList::HasChangeListKind(*change_list_json
));
420 EXPECT_FALSE(ChangeList::HasChangeListKind(*file_list_json
));
422 EXPECT_FALSE(FileList::HasFileListKind(*change_list_json
));
423 EXPECT_TRUE(FileList::HasFileListKind(*file_list_json
));
425 #endif // OS_CHROMEOS
427 } // namespace google_apis