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.
4
5 #include "google_apis/drive/drive_api_parser.h"
6
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"
13
14 namespace google_apis {
15
16 // Test about resource parsing.
TEST(DriveAPIParserTest,AboutResourceParser)17 TEST(DriveAPIParserTest, AboutResourceParser) {
18 std::string error;
19 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
20 "drive/about.json");
21 ASSERT_TRUE(document.get());
22
23 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
24 scoped_ptr<AboutResource> resource(new AboutResource());
25 EXPECT_TRUE(resource->Parse(*document));
26
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());
31 }
32
33 // Test app list parsing.
TEST(DriveAPIParserTest,AppListParser)34 TEST(DriveAPIParserTest, AppListParser) {
35 std::string error;
36 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
37 "drive/applist.json");
38 ASSERT_TRUE(document.get());
39
40 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
41 scoped_ptr<AppList> applist(new AppList);
42 EXPECT_TRUE(applist->Parse(*document));
43
44 EXPECT_EQ("\"Jm4BaSnCWNND-noZsHINRqj4ABC/tuqRBw0lvjUdPtc_2msA1tN4XYZ\"",
45 applist->etag());
46 ASSERT_EQ(2U, applist->items().size());
47 // Check Drive app 1
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());
59
60 ASSERT_EQ(1U, app1.primary_mimetypes().size());
61 EXPECT_EQ("application/vnd.google-apps.drive-sdk.123456788192",
62 *app1.primary_mimetypes()[0]);
63
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]);
67
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]);
71
72 EXPECT_EQ(0U, app1.secondary_file_extensions().size());
73
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());
79
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());
84
85 // Check Drive app 2
86 const AppResource& app2 = *applist->items()[1];
87 EXPECT_EQ("876543210000", app2.application_id());
88 EXPECT_EQ("Drive app 2", app2.name());
89 EXPECT_EQ("", app2.object_type());
90 EXPECT_FALSE(app2.supports_create());
91 EXPECT_FALSE(app2.supports_import());
92 EXPECT_TRUE(app2.is_installed());
93 EXPECT_FALSE(app2.is_authorized());
94 EXPECT_EQ("https://chrome.google.com/webstore/detail/"
95 "hgfedcbahgfedcbahgfedcbahgfedcba",
96 app2.product_url().spec());
97
98 ASSERT_EQ(3U, app2.primary_mimetypes().size());
99 EXPECT_EQ("image/jpeg", *app2.primary_mimetypes()[0]);
100 EXPECT_EQ("image/png", *app2.primary_mimetypes()[1]);
101 EXPECT_EQ("application/vnd.google-apps.drive-sdk.876543210000",
102 *app2.primary_mimetypes()[2]);
103
104 EXPECT_EQ(0U, app2.secondary_mimetypes().size());
105 EXPECT_EQ(0U, app2.primary_file_extensions().size());
106 EXPECT_EQ(0U, app2.secondary_file_extensions().size());
107
108 ASSERT_EQ(3U, app2.icons().size());
109 const DriveAppIcon& icon2 = *app2.icons()[1];
110 EXPECT_EQ(DriveAppIcon::DOCUMENT, icon2.category());
111 EXPECT_EQ(10, icon2.icon_side_length());
112 EXPECT_EQ("http://www.example.com/d10.png", icon2.icon_url().spec());
113 }
114
115 // Test file list parsing.
TEST(DriveAPIParserTest,FileListParser)116 TEST(DriveAPIParserTest, FileListParser) {
117 std::string error;
118 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
119 "drive/filelist.json");
120 ASSERT_TRUE(document.get());
121
122 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
123 scoped_ptr<FileList> filelist(new FileList);
124 EXPECT_TRUE(filelist->Parse(*document));
125
126 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/zyHTfoHpnRHovyi8bWpwK0DXABC\"",
127 filelist->etag());
128 EXPECT_EQ("EAIaggELEgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_Ry"
129 "NGJnridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJnr"
130 "idyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgart10AAAA"
131 "ABC", filelist->next_page_token());
132 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files?pageToken=EAIaggEL"
133 "EgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_RyNGJ"
134 "nridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJ"
135 "nridyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgar"
136 "t10AAAAABC"), filelist->next_link());
137
138 ASSERT_EQ(3U, filelist->items().size());
139 // Check file 1 (a regular file)
140 const FileResource& file1 = *filelist->items()[0];
141 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", file1.file_id());
142 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/MTM0MzM2NzgwMDIXYZ\"",
143 file1.etag());
144 EXPECT_EQ("My first file data", file1.title());
145 EXPECT_EQ("application/octet-stream", file1.mime_type());
146
147 EXPECT_FALSE(file1.labels().is_starred());
148 EXPECT_FALSE(file1.labels().is_hidden());
149 EXPECT_FALSE(file1.labels().is_trashed());
150 EXPECT_FALSE(file1.labels().is_restricted());
151 EXPECT_TRUE(file1.labels().is_viewed());
152 EXPECT_FALSE(file1.shared());
153
154 EXPECT_EQ(640, file1.image_media_metadata().width());
155 EXPECT_EQ(480, file1.image_media_metadata().height());
156 EXPECT_EQ(90, file1.image_media_metadata().rotation());
157
158 base::Time created_time;
159 ASSERT_TRUE(
160 util::GetTimeFromString("2012-07-24T08:51:16.570Z", &created_time));
161 EXPECT_EQ(created_time, file1.created_date());
162
163 base::Time modified_time;
164 ASSERT_TRUE(
165 util::GetTimeFromString("2012-07-27T05:43:20.269Z", &modified_time));
166 EXPECT_EQ(modified_time, file1.modified_date());
167 EXPECT_EQ(modified_time, file1.modified_by_me_date());
168
169 ASSERT_EQ(1U, file1.parents().size());
170 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", file1.parents()[0]->file_id());
171 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
172 "0B4v7G8yEYAWHYW1OcExsUVZLABC"),
173 file1.parents()[0]->parent_link());
174 EXPECT_FALSE(file1.parents()[0]->is_root());
175
176 EXPECT_EQ(GURL("https://www.example.com/download"), file1.download_url());
177 EXPECT_EQ("ext", file1.file_extension());
178 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", file1.md5_checksum());
179 EXPECT_EQ(1000U, file1.file_size());
180
181 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
182 "0B4v7G8yEYAWHUmRrU2lMS2hLABC"),
183 file1.self_link());
184 EXPECT_EQ(GURL("https://docs.google.com/file/d/"
185 "0B4v7G8yEYAWHUmRrU2lMS2hLABC/edit"),
186 file1.alternate_link());
187 EXPECT_EQ(GURL("https://docs.google.com/uc?"
188 "id=0B4v7G8yEYAWHUmRrU2lMS2hLABC&export=download"),
189 file1.web_content_link());
190 ASSERT_EQ(1U, file1.open_with_links().size());
191 EXPECT_EQ("1234567890", file1.open_with_links()[0].app_id);
192 EXPECT_EQ(GURL("http://open_with_link/url"),
193 file1.open_with_links()[0].open_url);
194
195 // Check file 2 (a Google Document)
196 const FileResource& file2 = *filelist->items()[1];
197 EXPECT_EQ("Test Google Document", file2.title());
198 EXPECT_EQ("application/vnd.google-apps.document", file2.mime_type());
199
200 EXPECT_TRUE(file2.labels().is_starred());
201 EXPECT_TRUE(file2.labels().is_hidden());
202 EXPECT_TRUE(file2.labels().is_trashed());
203 EXPECT_TRUE(file2.labels().is_restricted());
204 EXPECT_TRUE(file2.labels().is_viewed());
205 EXPECT_TRUE(file2.shared());
206
207 EXPECT_EQ(-1, file2.image_media_metadata().width());
208 EXPECT_EQ(-1, file2.image_media_metadata().height());
209 EXPECT_EQ(-1, file2.image_media_metadata().rotation());
210
211 base::Time shared_with_me_time;
212 ASSERT_TRUE(util::GetTimeFromString("2012-07-27T04:54:11.030Z",
213 &shared_with_me_time));
214 EXPECT_EQ(shared_with_me_time, file2.shared_with_me_date());
215
216 EXPECT_EQ(0U, file2.file_size());
217
218 ASSERT_EQ(0U, file2.parents().size());
219
220 EXPECT_EQ(GURL("https://docs.google.com/a/chromium.org/document/d/"
221 "1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC/preview"),
222 file2.embed_link());
223 EXPECT_EQ(GURL("https://docs.google.com/feeds/vt?gd=true&"
224 "id=1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC&"
225 "v=3&s=AMedNnoAAAAAUBJyB0g8HbxZaLRnlztxefZPS24LiXYZ&sz=s220"),
226 file2.thumbnail_link());
227 EXPECT_EQ(0U, file2.open_with_links().size());
228
229 // Check file 3 (a folder)
230 const FileResource& file3 = *filelist->items()[2];
231 EXPECT_EQ(0U, file3.file_size());
232 EXPECT_EQ("TestFolder", file3.title());
233 EXPECT_EQ("application/vnd.google-apps.folder", file3.mime_type());
234 ASSERT_TRUE(file3.IsDirectory());
235 EXPECT_FALSE(file3.shared());
236
237 ASSERT_EQ(1U, file3.parents().size());
238 EXPECT_EQ("0AIv7G8yEYAWHUk9ABC", file3.parents()[0]->file_id());
239 EXPECT_TRUE(file3.parents()[0]->is_root());
240 EXPECT_EQ(0U, file3.open_with_links().size());
241 }
242
243 // Test change list parsing.
TEST(DriveAPIParserTest,ChangeListParser)244 TEST(DriveAPIParserTest, ChangeListParser) {
245 std::string error;
246 scoped_ptr<base::Value> document =
247 test_util::LoadJSONFile("drive/changelist.json");
248 ASSERT_TRUE(document.get());
249
250 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
251 scoped_ptr<ChangeList> changelist(new ChangeList);
252 EXPECT_TRUE(changelist->Parse(*document));
253
254 EXPECT_EQ("\"Lp2bjAtLP341hvGmYHhxjYyBPJ8/BWbu_eylt5f_aGtCN6mGRv9hABC\"",
255 changelist->etag());
256 EXPECT_EQ("8929", changelist->next_page_token());
257 EXPECT_EQ("https://www.googleapis.com/drive/v2/changes?pageToken=8929",
258 changelist->next_link().spec());
259 EXPECT_EQ(13664, changelist->largest_change_id());
260
261 ASSERT_EQ(4U, changelist->items().size());
262
263 const ChangeResource& change1 = *changelist->items()[0];
264 EXPECT_EQ(8421, change1.change_id());
265 EXPECT_FALSE(change1.is_deleted());
266 EXPECT_EQ("1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC", change1.file_id());
267 EXPECT_EQ(change1.file_id(), change1.file()->file_id());
268 EXPECT_FALSE(change1.file()->shared());
269
270 const ChangeResource& change2 = *changelist->items()[1];
271 EXPECT_EQ(8424, change2.change_id());
272 EXPECT_FALSE(change2.is_deleted());
273 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", change2.file_id());
274 EXPECT_EQ(change2.file_id(), change2.file()->file_id());
275 EXPECT_TRUE(change2.file()->shared());
276
277 const ChangeResource& change3 = *changelist->items()[2];
278 EXPECT_EQ(8429, change3.change_id());
279 EXPECT_FALSE(change3.is_deleted());
280 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", change3.file_id());
281 EXPECT_EQ(change3.file_id(), change3.file()->file_id());
282 EXPECT_FALSE(change3.file()->shared());
283
284 // Deleted entry.
285 const ChangeResource& change4 = *changelist->items()[3];
286 EXPECT_EQ(8430, change4.change_id());
287 EXPECT_EQ("ABCv7G8yEYAWHc3Y5X0hMSkJYXYZ", change4.file_id());
288 EXPECT_TRUE(change4.is_deleted());
289 }
290
TEST(DriveAPIParserTest,HasKind)291 TEST(DriveAPIParserTest, HasKind) {
292 scoped_ptr<base::Value> change_list_json(
293 test_util::LoadJSONFile("drive/changelist.json"));
294 scoped_ptr<base::Value> file_list_json(
295 test_util::LoadJSONFile("drive/filelist.json"));
296
297 EXPECT_TRUE(ChangeList::HasChangeListKind(*change_list_json));
298 EXPECT_FALSE(ChangeList::HasChangeListKind(*file_list_json));
299
300 EXPECT_FALSE(FileList::HasFileListKind(*change_list_json));
301 EXPECT_TRUE(FileList::HasFileListKind(*file_list_json));
302 }
303
304 } // namespace google_apis
305