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.is_removable());
54 EXPECT_EQ("abcdefghabcdefghabcdefghabcdefgh", app1.product_id());
55
56 ASSERT_EQ(1U, app1.primary_mimetypes().size());
57 EXPECT_EQ("application/vnd.google-apps.drive-sdk.123456788192",
58 *app1.primary_mimetypes()[0]);
59
60 ASSERT_EQ(2U, app1.secondary_mimetypes().size());
61 EXPECT_EQ("text/html", *app1.secondary_mimetypes()[0]);
62 EXPECT_EQ("text/plain", *app1.secondary_mimetypes()[1]);
63
64 ASSERT_EQ(2U, app1.primary_file_extensions().size());
65 EXPECT_EQ("exe", *app1.primary_file_extensions()[0]);
66 EXPECT_EQ("com", *app1.primary_file_extensions()[1]);
67
68 EXPECT_EQ(0U, app1.secondary_file_extensions().size());
69
70 ASSERT_EQ(6U, app1.icons().size());
71 const DriveAppIcon& icon1 = *app1.icons()[0];
72 EXPECT_EQ(DriveAppIcon::APPLICATION, icon1.category());
73 EXPECT_EQ(10, icon1.icon_side_length());
74 EXPECT_EQ("http://www.example.com/10.png", icon1.icon_url().spec());
75
76 const DriveAppIcon& icon6 = *app1.icons()[5];
77 EXPECT_EQ(DriveAppIcon::SHARED_DOCUMENT, icon6.category());
78 EXPECT_EQ(16, icon6.icon_side_length());
79 EXPECT_EQ("http://www.example.com/ds16.png", icon6.icon_url().spec());
80
81 EXPECT_EQ("https://www.example.com/createForApp1", app1.create_url().spec());
82
83 // Check Drive app 2
84 const AppResource& app2 = *applist->items()[1];
85 EXPECT_EQ("876543210000", app2.application_id());
86 EXPECT_EQ("Drive app 2", app2.name());
87 EXPECT_EQ("", app2.object_type());
88 EXPECT_FALSE(app2.supports_create());
89 EXPECT_FALSE(app2.is_removable());
90 EXPECT_EQ("hgfedcbahgfedcbahgfedcbahgfedcba", app2.product_id());
91
92 ASSERT_EQ(3U, app2.primary_mimetypes().size());
93 EXPECT_EQ("image/jpeg", *app2.primary_mimetypes()[0]);
94 EXPECT_EQ("image/png", *app2.primary_mimetypes()[1]);
95 EXPECT_EQ("application/vnd.google-apps.drive-sdk.876543210000",
96 *app2.primary_mimetypes()[2]);
97
98 EXPECT_EQ(0U, app2.secondary_mimetypes().size());
99 EXPECT_EQ(0U, app2.primary_file_extensions().size());
100 EXPECT_EQ(0U, app2.secondary_file_extensions().size());
101
102 ASSERT_EQ(3U, app2.icons().size());
103 const DriveAppIcon& icon2 = *app2.icons()[1];
104 EXPECT_EQ(DriveAppIcon::DOCUMENT, icon2.category());
105 EXPECT_EQ(10, icon2.icon_side_length());
106 EXPECT_EQ("http://www.example.com/d10.png", icon2.icon_url().spec());
107
108 EXPECT_EQ("https://www.example.com/createForApp2", app2.create_url().spec());
109 }
110
111 // Test file list parsing.
TEST(DriveAPIParserTest,FileListParser)112 TEST(DriveAPIParserTest, FileListParser) {
113 std::string error;
114 scoped_ptr<base::Value> document = test_util::LoadJSONFile(
115 "drive/filelist.json");
116 ASSERT_TRUE(document.get());
117
118 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
119 scoped_ptr<FileList> filelist(new FileList);
120 EXPECT_TRUE(filelist->Parse(*document));
121
122 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files?pageToken=EAIaggEL"
123 "EgA6egpi96It9mH_____f_8AAP__AAD_okhU-cHLz83KzszMxsjMzs_RyNGJ"
124 "nridyrbHs7u9tv8AAP__AP7__n__AP8AokhU-cHLz83KzszMxsjMzs_RyNGJ"
125 "nridyrbHs7u9tv8A__4QZCEiXPTi_wtIgTkAAAAAngnSXUgCDEAAIgsJPgar"
126 "t10AAAAABC"), filelist->next_link());
127
128 ASSERT_EQ(3U, filelist->items().size());
129 // Check file 1 (a regular file)
130 const FileResource& file1 = *filelist->items()[0];
131 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", file1.file_id());
132 EXPECT_EQ("\"WtRjAPZWbDA7_fkFjc5ojsEvDEF/MTM0MzM2NzgwMDIXYZ\"",
133 file1.etag());
134 EXPECT_EQ("My first file data", file1.title());
135 EXPECT_EQ("application/octet-stream", file1.mime_type());
136
137 EXPECT_FALSE(file1.labels().is_trashed());
138 EXPECT_FALSE(file1.shared());
139
140 EXPECT_EQ(640, file1.image_media_metadata().width());
141 EXPECT_EQ(480, file1.image_media_metadata().height());
142 EXPECT_EQ(90, file1.image_media_metadata().rotation());
143
144 base::Time created_time;
145 ASSERT_TRUE(
146 util::GetTimeFromString("2012-07-24T08:51:16.570Z", &created_time));
147 EXPECT_EQ(created_time, file1.created_date());
148
149 base::Time modified_time;
150 ASSERT_TRUE(
151 util::GetTimeFromString("2012-07-27T05:43:20.269Z", &modified_time));
152 EXPECT_EQ(modified_time, file1.modified_date());
153
154 ASSERT_EQ(1U, file1.parents().size());
155 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", file1.parents()[0].file_id());
156 EXPECT_EQ(GURL("https://www.googleapis.com/drive/v2/files/"
157 "0B4v7G8yEYAWHYW1OcExsUVZLABC"),
158 file1.parents()[0].parent_link());
159
160 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", file1.md5_checksum());
161 EXPECT_EQ(1000U, file1.file_size());
162
163 EXPECT_EQ(GURL("https://docs.google.com/file/d/"
164 "0B4v7G8yEYAWHUmRrU2lMS2hLABC/edit"),
165 file1.alternate_link());
166 ASSERT_EQ(1U, file1.open_with_links().size());
167 EXPECT_EQ("1234567890", file1.open_with_links()[0].app_id);
168 EXPECT_EQ(GURL("http://open_with_link/url"),
169 file1.open_with_links()[0].open_url);
170
171 // Check file 2 (a Google Document)
172 const FileResource& file2 = *filelist->items()[1];
173 EXPECT_EQ("Test Google Document", file2.title());
174 EXPECT_EQ("application/vnd.google-apps.document", file2.mime_type());
175
176 EXPECT_TRUE(file2.labels().is_trashed());
177 EXPECT_TRUE(file2.shared());
178
179 EXPECT_EQ(-1, file2.image_media_metadata().width());
180 EXPECT_EQ(-1, file2.image_media_metadata().height());
181 EXPECT_EQ(-1, file2.image_media_metadata().rotation());
182
183 base::Time shared_with_me_time;
184 ASSERT_TRUE(util::GetTimeFromString("2012-07-27T04:54:11.030Z",
185 &shared_with_me_time));
186 EXPECT_EQ(shared_with_me_time, file2.shared_with_me_date());
187
188 EXPECT_EQ(0U, file2.file_size());
189
190 ASSERT_EQ(0U, file2.parents().size());
191
192 EXPECT_EQ(0U, file2.open_with_links().size());
193
194 // Check file 3 (a folder)
195 const FileResource& file3 = *filelist->items()[2];
196 EXPECT_EQ(0U, file3.file_size());
197 EXPECT_EQ("TestFolder", file3.title());
198 EXPECT_EQ("application/vnd.google-apps.folder", file3.mime_type());
199 ASSERT_TRUE(file3.IsDirectory());
200 EXPECT_FALSE(file3.shared());
201
202 ASSERT_EQ(1U, file3.parents().size());
203 EXPECT_EQ("0AIv7G8yEYAWHUk9ABC", file3.parents()[0].file_id());
204 EXPECT_EQ(0U, file3.open_with_links().size());
205 }
206
207 // Test change list parsing.
TEST(DriveAPIParserTest,ChangeListParser)208 TEST(DriveAPIParserTest, ChangeListParser) {
209 std::string error;
210 scoped_ptr<base::Value> document =
211 test_util::LoadJSONFile("drive/changelist.json");
212 ASSERT_TRUE(document.get());
213
214 ASSERT_EQ(base::Value::TYPE_DICTIONARY, document->GetType());
215 scoped_ptr<ChangeList> changelist(new ChangeList);
216 EXPECT_TRUE(changelist->Parse(*document));
217
218 EXPECT_EQ("https://www.googleapis.com/drive/v2/changes?pageToken=8929",
219 changelist->next_link().spec());
220 EXPECT_EQ(13664, changelist->largest_change_id());
221
222 ASSERT_EQ(4U, changelist->items().size());
223
224 const ChangeResource& change1 = *changelist->items()[0];
225 EXPECT_EQ(8421, change1.change_id());
226 EXPECT_FALSE(change1.is_deleted());
227 EXPECT_EQ("1Pc8jzfU1ErbN_eucMMqdqzY3eBm0v8sxXm_1CtLxABC", change1.file_id());
228 EXPECT_EQ(change1.file_id(), change1.file()->file_id());
229 EXPECT_FALSE(change1.file()->shared());
230 EXPECT_EQ(change1.file()->modified_date(), change1.modification_date());
231
232 const ChangeResource& change2 = *changelist->items()[1];
233 EXPECT_EQ(8424, change2.change_id());
234 EXPECT_FALSE(change2.is_deleted());
235 EXPECT_EQ("0B4v7G8yEYAWHUmRrU2lMS2hLABC", change2.file_id());
236 EXPECT_EQ(change2.file_id(), change2.file()->file_id());
237 EXPECT_TRUE(change2.file()->shared());
238 EXPECT_EQ(change2.file()->modified_date(), change2.modification_date());
239
240 const ChangeResource& change3 = *changelist->items()[2];
241 EXPECT_EQ(8429, change3.change_id());
242 EXPECT_FALSE(change3.is_deleted());
243 EXPECT_EQ("0B4v7G8yEYAWHYW1OcExsUVZLABC", change3.file_id());
244 EXPECT_EQ(change3.file_id(), change3.file()->file_id());
245 EXPECT_FALSE(change3.file()->shared());
246 EXPECT_EQ(change3.file()->modified_date(), change3.modification_date());
247
248 // Deleted entry.
249 const ChangeResource& change4 = *changelist->items()[3];
250 EXPECT_EQ(8430, change4.change_id());
251 EXPECT_EQ("ABCv7G8yEYAWHc3Y5X0hMSkJYXYZ", change4.file_id());
252 EXPECT_TRUE(change4.is_deleted());
253 base::Time modification_time;
254 ASSERT_TRUE(util::GetTimeFromString("2012-07-27T12:34:56.789Z",
255 &modification_time));
256 EXPECT_EQ(modification_time, change4.modification_date());
257 }
258
TEST(DriveAPIParserTest,HasKind)259 TEST(DriveAPIParserTest, HasKind) {
260 scoped_ptr<base::Value> change_list_json(
261 test_util::LoadJSONFile("drive/changelist.json"));
262 scoped_ptr<base::Value> file_list_json(
263 test_util::LoadJSONFile("drive/filelist.json"));
264
265 EXPECT_TRUE(ChangeList::HasChangeListKind(*change_list_json));
266 EXPECT_FALSE(ChangeList::HasChangeListKind(*file_list_json));
267
268 EXPECT_FALSE(FileList::HasFileListKind(*change_list_json));
269 EXPECT_TRUE(FileList::HasFileListKind(*file_list_json));
270 }
271
272 } // namespace google_apis
273