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 "google_apis/drive/drive_api_parser.h"
7 #include "base/time/time.h"
8 #include "base/values.h"
9 #include "google_apis/drive/gdata_wapi_parser.h"
10 #include "google_apis/drive/test_util.h"
11 #include "google_apis/drive/time_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace google_apis {
16 // Test about resource parsing.
17 TEST(DriveAPIParserTest, AboutResourceParser) {
19 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
21 ASSERT_TRUE(document.get());
23 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
24 scoped_ptr<AboutResource> resource(new AboutResource());
25 EXPECT_TRUE(resource->Parse(*document));
27 EXPECT_EQ("0AIv7G8yEYAWHUk9123", resource->root_folder_id());
28 EXPECT_EQ(5368709120LL, resource->quota_bytes_total());
29 EXPECT_EQ(1073741824LL, resource->quota_bytes_used());
30 EXPECT_EQ(8177LL, resource->largest_change_id());
33 // Test app list parsing.
34 TEST(DriveAPIParserTest, AppListParser) {
36 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
37 "drive/applist.json");
38 ASSERT_TRUE(document.get());
40 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
41 scoped_ptr<AppList> applist(new AppList);
42 EXPECT_TRUE(applist->Parse(*document));
44 EXPECT_EQ("\"Jm4BaSnCWNND-noZsHINRqj4ABC/tuqRBw0lvjUdPtc_2msA1tN4XYZ\"",
46 ASSERT_EQ(2U, applist->items().size());
48 const AppResource& app1 = *applist->items()[0];
49 EXPECT_EQ("123456788192", app1.application_id());
50 EXPECT_EQ("Drive app 1", app1.name());
51 EXPECT_EQ("", app1.object_type());
52 EXPECT_TRUE(app1.supports_create());
53 EXPECT_TRUE(app1.supports_import());
54 EXPECT_TRUE(app1.is_installed());
55 EXPECT_FALSE(app1.is_authorized());
56 EXPECT_EQ("https://chrome.google.com/webstore/detail/"
57 "abcdefghabcdefghabcdefghabcdefgh",
58 app1.product_url().spec());
60 ASSERT_EQ(1U, app1.primary_mimetypes().size());
61 EXPECT_EQ("application/vnd.google-apps.drive-sdk.123456788192",
62 *app1.primary_mimetypes()[0]);
64 ASSERT_EQ(2U, app1.secondary_mimetypes().size());
65 EXPECT_EQ("text/html", *app1.secondary_mimetypes()[0]);
66 EXPECT_EQ("text/plain", *app1.secondary_mimetypes()[1]);
68 ASSERT_EQ(2U, app1.primary_file_extensions().size());
69 EXPECT_EQ("exe", *app1.primary_file_extensions()[0]);
70 EXPECT_EQ("com", *app1.primary_file_extensions()[1]);
72 EXPECT_EQ(0U, app1.secondary_file_extensions().size());
74 ASSERT_EQ(6U, app1.icons().size());
75 const DriveAppIcon& icon1 = *app1.icons()[0];
76 EXPECT_EQ(DriveAppIcon::APPLICATION, icon1.category());
77 EXPECT_EQ(10, icon1.icon_side_length());
78 EXPECT_EQ("http://www.example.com/10.png", icon1.icon_url().spec());
80 const DriveAppIcon& icon6 = *app1.icons()[5];
81 EXPECT_EQ(DriveAppIcon::SHARED_DOCUMENT, icon6.category());
82 EXPECT_EQ(16, icon6.icon_side_length());
83 EXPECT_EQ("http://www.example.com/ds16.png", icon6.icon_url().spec());
85 EXPECT_EQ("https://www.example.com/createForApp1", app1.create_url().spec());
88 const AppResource& app2 = *applist->items()[1];
89 EXPECT_EQ("876543210000", app2.application_id());
90 EXPECT_EQ("Drive app 2", app2.name());
91 EXPECT_EQ("", app2.object_type());
92 EXPECT_FALSE(app2.supports_create());
93 EXPECT_FALSE(app2.supports_import());
94 EXPECT_TRUE(app2.is_installed());
95 EXPECT_FALSE(app2.is_authorized());
96 EXPECT_EQ("https://chrome.google.com/webstore/detail/"
97 "hgfedcbahgfedcbahgfedcbahgfedcba",
98 app2.product_url().spec());
100 ASSERT_EQ(3U, app2.primary_mimetypes().size());
101 EXPECT_EQ("image/jpeg", *app2.primary_mimetypes()[0]);
102 EXPECT_EQ("image/png", *app2.primary_mimetypes()[1]);
103 EXPECT_EQ("application/vnd.google-apps.drive-sdk.876543210000",
104 *app2.primary_mimetypes()[2]);
106 EXPECT_EQ(0U, app2.secondary_mimetypes().size());
107 EXPECT_EQ(0U, app2.primary_file_extensions().size());
108 EXPECT_EQ(0U, app2.secondary_file_extensions().size());
110 ASSERT_EQ(3U, app2.icons().size());
111 const DriveAppIcon& icon2 = *app2.icons()[1];
112 EXPECT_EQ(DriveAppIcon::DOCUMENT, icon2.category());
113 EXPECT_EQ(10, icon2.icon_side_length());
114 EXPECT_EQ("http://www.example.com/d10.png", icon2.icon_url().spec());
116 EXPECT_EQ("https://www.example.com/createForApp2", app2.create_url().spec());
119 // Test file list parsing.
120 TEST(DriveAPIParserTest, FileListParser) {
122 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
123 "drive/filelist.json");
124 ASSERT_TRUE(document.get());
126 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
127 scoped_ptr<FileList> filelist(new FileList);
128 EXPECT_TRUE(filelist->Parse(*document));
130 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/zyHTfoHpnRHovyi8bWpwK0DXABC\"",
132 EXPECT_EQ("EAIaggELEgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_Ry"
133 "NGJnridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJnr"
134 "idyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgart10AAAA"
135 "ABC", filelist->next_page_token());
136 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files?pageToken=EAIaggEL"
137 "EgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_RyNGJ"
138 "nridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJ"
139 "nridyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgar"
140 "t10AAAAABC"), filelist->next_link());
142 ASSERT_EQ(3U, filelist->items().size());
143 // Check file 1 (a regular file)
144 const FileResource& file1 = *filelist->items()[0];
145 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", file1.file_id());
146 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/MTM0MzM2NzgwMDIXYZ\"",
148 EXPECT_EQ("My first file data", file1.title());
149 EXPECT_EQ("application/octet-stream", file1.mime_type());
151 EXPECT_FALSE(file1.labels().is_starred());
152 EXPECT_FALSE(file1.labels().is_hidden());
153 EXPECT_FALSE(file1.labels().is_trashed());
154 EXPECT_FALSE(file1.labels().is_restricted());
155 EXPECT_TRUE(file1.labels().is_viewed());
156 EXPECT_FALSE(file1.shared());
158 EXPECT_EQ(640, file1.image_media_metadata().width());
159 EXPECT_EQ(480, file1.image_media_metadata().height());
160 EXPECT_EQ(90, file1.image_media_metadata().rotation());
162 base::Time created_time;
164 util::GetTimeFromString("2012-07-24T08:51:16.570Z", &created_time));
165 EXPECT_EQ(created_time, file1.created_date());
167 base::Time modified_time;
169 util::GetTimeFromString("2012-07-27T05:43:20.269Z", &modified_time));
170 EXPECT_EQ(modified_time, file1.modified_date());
171 EXPECT_EQ(modified_time, file1.modified_by_me_date());
173 ASSERT_EQ(1U, file1.parents().size());
174 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", file1.parents()[0]->file_id());
175 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
176 "0B4v7G8yEYAWHYW1OcExsUVZLABC"),
177 file1.parents()[0]->parent_link());
178 EXPECT_FALSE(file1.parents()[0]->is_root());
180 EXPECT_EQ(GURL("https://www.example.com/download"), file1.download_url());
181 EXPECT_EQ("ext", file1.file_extension());
182 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", file1.md5_checksum());
183 EXPECT_EQ(1000U, file1.file_size());
185 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
186 "0B4v7G8yEYAWHUmRrU2lMS2hLABC"),
188 EXPECT_EQ(GURL("https://docs.google.com/file/d/"
189 "0B4v7G8yEYAWHUmRrU2lMS2hLABC/edit"),
190 file1.alternate_link());
191 EXPECT_EQ(GURL("https://docs.google.com/uc?"
192 "id=0B4v7G8yEYAWHUmRrU2lMS2hLABC&export=download"),
193 file1.web_content_link());
194 ASSERT_EQ(1U, file1.open_with_links().size());
195 EXPECT_EQ("1234567890", file1.open_with_links()[0].app_id);
196 EXPECT_EQ(GURL("http://open_with_link/url"),
197 file1.open_with_links()[0].open_url);
199 // Check file 2 (a Google Document)
200 const FileResource& file2 = *filelist->items()[1];
201 EXPECT_EQ("Test Google Document", file2.title());
202 EXPECT_EQ("application/vnd.google-apps.document", file2.mime_type());
204 EXPECT_TRUE(file2.labels().is_starred());
205 EXPECT_TRUE(file2.labels().is_hidden());
206 EXPECT_TRUE(file2.labels().is_trashed());
207 EXPECT_TRUE(file2.labels().is_restricted());
208 EXPECT_TRUE(file2.labels().is_viewed());
209 EXPECT_TRUE(file2.shared());
211 EXPECT_EQ(-1, file2.image_media_metadata().width());
212 EXPECT_EQ(-1, file2.image_media_metadata().height());
213 EXPECT_EQ(-1, file2.image_media_metadata().rotation());
215 base::Time shared_with_me_time;
216 ASSERT_TRUE(util::GetTimeFromString("2012-07-27T04:54:11.030Z",
217 &shared_with_me_time));
218 EXPECT_EQ(shared_with_me_time, file2.shared_with_me_date());
220 EXPECT_EQ(0U, file2.file_size());
222 ASSERT_EQ(0U, file2.parents().size());
224 EXPECT_EQ(GURL("https://docs.google.com/a/chromium.org/document/d/"
225 "1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC/preview"),
227 EXPECT_EQ(GURL("https://docs.google.com/feeds/vt?gd=true&"
228 "id=1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC&"
229 "v=3&s=AMedNnoAAAAAUBJyB0g8HbxZaLRnlztxefZPS24LiXYZ&sz=s220"),
230 file2.thumbnail_link());
231 EXPECT_EQ(0U, file2.open_with_links().size());
233 // Check file 3 (a folder)
234 const FileResource& file3 = *filelist->items()[2];
235 EXPECT_EQ(0U, file3.file_size());
236 EXPECT_EQ("TestFolder", file3.title());
237 EXPECT_EQ("application/vnd.google-apps.folder", file3.mime_type());
238 ASSERT_TRUE(file3.IsDirectory());
239 EXPECT_FALSE(file3.shared());
241 ASSERT_EQ(1U, file3.parents().size());
242 EXPECT_EQ("0AIv7G8yEYAWHUk9ABC", file3.parents()[0]->file_id());
243 EXPECT_TRUE(file3.parents()[0]->is_root());
244 EXPECT_EQ(0U, file3.open_with_links().size());
247 // Test change list parsing.
248 TEST(DriveAPIParserTest, ChangeListParser) {
250 scoped_ptr<base::Value> document =
251 test_util::LoadJSONFile("drive/changelist.json");
252 ASSERT_TRUE(document.get());
254 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
255 scoped_ptr<ChangeList> changelist(new ChangeList);
256 EXPECT_TRUE(changelist->Parse(*document));
258 EXPECT_EQ("\"Lp2bjAtLP341hvGmYHhxjYyBPJ8/BWbu_eylt5f_aGtCN6mGRv9hABC\"",
260 EXPECT_EQ("8929", changelist->next_page_token());
261 EXPECT_EQ("https://www.googleapis.com/drive/v2/changes?pageToken=8929",
262 changelist->next_link().spec());
263 EXPECT_EQ(13664, changelist->largest_change_id());
265 ASSERT_EQ(4U, changelist->items().size());
267 const ChangeResource& change1 = *changelist->items()[0];
268 EXPECT_EQ(8421, change1.change_id());
269 EXPECT_FALSE(change1.is_deleted());
270 EXPECT_EQ("1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC", change1.file_id());
271 EXPECT_EQ(change1.file_id(), change1.file()->file_id());
272 EXPECT_FALSE(change1.file()->shared());
274 const ChangeResource& change2 = *changelist->items()[1];
275 EXPECT_EQ(8424, change2.change_id());
276 EXPECT_FALSE(change2.is_deleted());
277 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", change2.file_id());
278 EXPECT_EQ(change2.file_id(), change2.file()->file_id());
279 EXPECT_TRUE(change2.file()->shared());
281 const ChangeResource& change3 = *changelist->items()[2];
282 EXPECT_EQ(8429, change3.change_id());
283 EXPECT_FALSE(change3.is_deleted());
284 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", change3.file_id());
285 EXPECT_EQ(change3.file_id(), change3.file()->file_id());
286 EXPECT_FALSE(change3.file()->shared());
289 const ChangeResource& change4 = *changelist->items()[3];
290 EXPECT_EQ(8430, change4.change_id());
291 EXPECT_EQ("ABCv7G8yEYAWHc3Y5X0hMSkJYXYZ", change4.file_id());
292 EXPECT_TRUE(change4.is_deleted());
295 TEST(DriveAPIParserTest, HasKind) {
296 scoped_ptr<base::Value> change_list_json(
297 test_util::LoadJSONFile("drive/changelist.json"));
298 scoped_ptr<base::Value> file_list_json(
299 test_util::LoadJSONFile("drive/filelist.json"));
301 EXPECT_TRUE(ChangeList::HasChangeListKind(*change_list_json));
302 EXPECT_FALSE(ChangeList::HasChangeListKind(*file_list_json));
304 EXPECT_FALSE(FileList::HasFileListKind(*change_list_json));
305 EXPECT_TRUE(FileList::HasFileListKind(*file_list_json));
308 } // namespace google_apis