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/drive/fake_drive_service.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
13 #include "base/run_loop.h"
14 #include "base/stl_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/drive/test_util.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "google_apis/drive/drive_api_parser.h"
20 #include "google_apis/drive/test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using google_apis::AboutResource
;
24 using google_apis::AppList
;
25 using google_apis::ChangeList
;
26 using google_apis::ChangeResource
;
27 using google_apis::FileList
;
28 using google_apis::FileResource
;
29 using google_apis::GDATA_NO_CONNECTION
;
30 using google_apis::GDATA_OTHER_ERROR
;
31 using google_apis::GDataErrorCode
;
32 using google_apis::GetContentCallback
;
33 using google_apis::HTTP_CREATED
;
34 using google_apis::HTTP_FORBIDDEN
;
35 using google_apis::HTTP_NOT_FOUND
;
36 using google_apis::HTTP_NO_CONTENT
;
37 using google_apis::HTTP_PRECONDITION
;
38 using google_apis::HTTP_RESUME_INCOMPLETE
;
39 using google_apis::HTTP_SUCCESS
;
40 using google_apis::ProgressCallback
;
41 using google_apis::UploadRangeResponse
;
47 using google_apis::test_util::AppendProgressCallbackResult
;
48 using google_apis::test_util::CreateCopyResultCallback
;
49 using google_apis::test_util::ProgressInfo
;
50 using google_apis::test_util::TestGetContentCallback
;
51 using google_apis::test_util::WriteStringToFile
;
53 } // namespace test_util
57 class FakeDriveServiceTest
: public testing::Test
{
59 // Returns the resource entry that matches |resource_id|.
60 scoped_ptr
<FileResource
> FindEntry(const std::string
& resource_id
) {
61 GDataErrorCode error
= GDATA_OTHER_ERROR
;
62 scoped_ptr
<FileResource
> entry
;
63 fake_service_
.GetFileResource(
64 resource_id
, test_util::CreateCopyResultCallback(&error
, &entry
));
65 base::RunLoop().RunUntilIdle();
69 // Returns true if the resource identified by |resource_id| exists.
70 bool Exists(const std::string
& resource_id
) {
71 scoped_ptr
<FileResource
> entry
= FindEntry(resource_id
);
72 return entry
&& !entry
->labels().is_trashed();
75 // Adds a new directory at |parent_resource_id| with the given name.
76 // Returns true on success.
77 bool AddNewDirectory(const std::string
& parent_resource_id
,
78 const std::string
& directory_title
) {
79 GDataErrorCode error
= GDATA_OTHER_ERROR
;
80 scoped_ptr
<FileResource
> entry
;
81 fake_service_
.AddNewDirectory(
84 DriveServiceInterface::AddNewDirectoryOptions(),
85 test_util::CreateCopyResultCallback(&error
, &entry
));
86 base::RunLoop().RunUntilIdle();
87 return error
== HTTP_CREATED
;
90 // Returns true if the resource identified by |resource_id| has a parent
91 // identified by |parent_id|.
92 bool HasParent(const std::string
& resource_id
, const std::string
& parent_id
) {
93 scoped_ptr
<FileResource
> entry
= FindEntry(resource_id
);
95 for (size_t i
= 0; i
< entry
->parents().size(); ++i
) {
96 if (entry
->parents()[i
].file_id() == parent_id
)
103 int64
GetLargestChangeByAboutResource() {
104 GDataErrorCode error
;
105 scoped_ptr
<AboutResource
> about_resource
;
106 fake_service_
.GetAboutResource(
107 test_util::CreateCopyResultCallback(&error
, &about_resource
));
108 base::RunLoop().RunUntilIdle();
109 return about_resource
->largest_change_id();
112 content::TestBrowserThreadBundle thread_bundle_
;
113 FakeDriveService fake_service_
;
116 TEST_F(FakeDriveServiceTest
, GetAllFileList
) {
117 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
119 GDataErrorCode error
= GDATA_OTHER_ERROR
;
120 scoped_ptr
<FileList
> file_list
;
121 fake_service_
.GetAllFileList(
122 test_util::CreateCopyResultCallback(&error
, &file_list
));
123 base::RunLoop().RunUntilIdle();
125 EXPECT_EQ(HTTP_SUCCESS
, error
);
126 ASSERT_TRUE(file_list
);
127 // Do some sanity check.
128 EXPECT_EQ(15U, file_list
->items().size());
129 EXPECT_EQ(1, fake_service_
.file_list_load_count());
132 TEST_F(FakeDriveServiceTest
, GetAllFileList_Offline
) {
133 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
134 fake_service_
.set_offline(true);
136 GDataErrorCode error
= GDATA_OTHER_ERROR
;
137 scoped_ptr
<FileList
> file_list
;
138 fake_service_
.GetAllFileList(
139 test_util::CreateCopyResultCallback(&error
, &file_list
));
140 base::RunLoop().RunUntilIdle();
142 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
143 EXPECT_FALSE(file_list
);
146 TEST_F(FakeDriveServiceTest
, GetFileListInDirectory_InRootDirectory
) {
147 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
149 GDataErrorCode error
= GDATA_OTHER_ERROR
;
150 scoped_ptr
<FileList
> file_list
;
151 fake_service_
.GetFileListInDirectory(
152 fake_service_
.GetRootResourceId(),
153 test_util::CreateCopyResultCallback(&error
, &file_list
));
154 base::RunLoop().RunUntilIdle();
156 EXPECT_EQ(HTTP_SUCCESS
, error
);
157 ASSERT_TRUE(file_list
);
158 // Do some sanity check. There are 8 entries in the root directory.
159 EXPECT_EQ(8U, file_list
->items().size());
160 EXPECT_EQ(1, fake_service_
.directory_load_count());
163 TEST_F(FakeDriveServiceTest
, GetFileListInDirectory_InNonRootDirectory
) {
164 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
166 GDataErrorCode error
= GDATA_OTHER_ERROR
;
167 scoped_ptr
<FileList
> file_list
;
168 fake_service_
.GetFileListInDirectory(
169 "1_folder_resource_id",
170 test_util::CreateCopyResultCallback(&error
, &file_list
));
171 base::RunLoop().RunUntilIdle();
173 EXPECT_EQ(HTTP_SUCCESS
, error
);
174 ASSERT_TRUE(file_list
);
175 // Do some sanity check. There is three entries in 1_folder_resource_id
177 EXPECT_EQ(3U, file_list
->items().size());
178 EXPECT_EQ(1, fake_service_
.directory_load_count());
181 TEST_F(FakeDriveServiceTest
, GetFileListInDirectory_Offline
) {
182 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
183 fake_service_
.set_offline(true);
185 GDataErrorCode error
= GDATA_OTHER_ERROR
;
186 scoped_ptr
<FileList
> file_list
;
187 fake_service_
.GetFileListInDirectory(
188 fake_service_
.GetRootResourceId(),
189 test_util::CreateCopyResultCallback(&error
, &file_list
));
190 base::RunLoop().RunUntilIdle();
192 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
193 EXPECT_FALSE(file_list
);
196 TEST_F(FakeDriveServiceTest
, Search
) {
197 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
199 GDataErrorCode error
= GDATA_OTHER_ERROR
;
200 scoped_ptr
<FileList
> file_list
;
201 fake_service_
.Search(
202 "File", // search_query
203 test_util::CreateCopyResultCallback(&error
, &file_list
));
204 base::RunLoop().RunUntilIdle();
206 EXPECT_EQ(HTTP_SUCCESS
, error
);
207 ASSERT_TRUE(file_list
);
208 // Do some sanity check. There are 4 entries that contain "File" in their
210 EXPECT_EQ(4U, file_list
->items().size());
213 TEST_F(FakeDriveServiceTest
, Search_WithAttribute
) {
214 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
216 GDataErrorCode error
= GDATA_OTHER_ERROR
;
217 scoped_ptr
<FileList
> file_list
;
218 fake_service_
.Search(
219 "title:1.txt", // search_query
220 test_util::CreateCopyResultCallback(&error
, &file_list
));
221 base::RunLoop().RunUntilIdle();
223 EXPECT_EQ(HTTP_SUCCESS
, error
);
224 ASSERT_TRUE(file_list
);
225 // Do some sanity check. There are 4 entries that contain "1.txt" in their
227 EXPECT_EQ(4U, file_list
->items().size());
230 TEST_F(FakeDriveServiceTest
, Search_MultipleQueries
) {
231 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
233 GDataErrorCode error
= GDATA_OTHER_ERROR
;
234 scoped_ptr
<FileList
> file_list
;
235 fake_service_
.Search(
236 "Directory 1", // search_query
237 test_util::CreateCopyResultCallback(&error
, &file_list
));
238 base::RunLoop().RunUntilIdle();
240 EXPECT_EQ(HTTP_SUCCESS
, error
);
241 ASSERT_TRUE(file_list
);
242 // There are 2 entries that contain both "Directory" and "1" in their titles.
243 EXPECT_EQ(2U, file_list
->items().size());
245 fake_service_
.Search(
246 "\"Directory 1\"", // search_query
247 test_util::CreateCopyResultCallback(&error
, &file_list
));
248 base::RunLoop().RunUntilIdle();
250 EXPECT_EQ(HTTP_SUCCESS
, error
);
251 ASSERT_TRUE(file_list
);
252 // There is 1 entry that contain "Directory 1" in its title.
253 EXPECT_EQ(1U, file_list
->items().size());
256 TEST_F(FakeDriveServiceTest
, Search_Offline
) {
257 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
258 fake_service_
.set_offline(true);
260 GDataErrorCode error
= GDATA_OTHER_ERROR
;
261 scoped_ptr
<FileList
> file_list
;
262 fake_service_
.Search(
263 "Directory 1", // search_query
264 test_util::CreateCopyResultCallback(&error
, &file_list
));
265 base::RunLoop().RunUntilIdle();
267 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
268 EXPECT_FALSE(file_list
);
271 TEST_F(FakeDriveServiceTest
, Search_Deleted
) {
272 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
274 GDataErrorCode error
= GDATA_OTHER_ERROR
;
275 fake_service_
.DeleteResource("2_file_resource_id",
276 std::string(), // etag
277 test_util::CreateCopyResultCallback(&error
));
278 base::RunLoop().RunUntilIdle();
279 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
281 error
= GDATA_OTHER_ERROR
;
282 scoped_ptr
<FileList
> file_list
;
283 fake_service_
.Search(
284 "File", // search_query
285 test_util::CreateCopyResultCallback(&error
, &file_list
));
286 base::RunLoop().RunUntilIdle();
288 EXPECT_EQ(HTTP_SUCCESS
, error
);
289 ASSERT_TRUE(file_list
);
290 // Do some sanity check. There are 4 entries that contain "File" in their
291 // titles and one of them is deleted.
292 EXPECT_EQ(3U, file_list
->items().size());
295 TEST_F(FakeDriveServiceTest
, Search_Trashed
) {
296 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
298 GDataErrorCode error
= GDATA_OTHER_ERROR
;
299 fake_service_
.TrashResource("2_file_resource_id",
300 test_util::CreateCopyResultCallback(&error
));
301 base::RunLoop().RunUntilIdle();
302 EXPECT_EQ(HTTP_SUCCESS
, error
);
304 error
= GDATA_OTHER_ERROR
;
305 scoped_ptr
<FileList
> file_list
;
306 fake_service_
.Search(
307 "File", // search_query
308 test_util::CreateCopyResultCallback(&error
, &file_list
));
309 base::RunLoop().RunUntilIdle();
311 EXPECT_EQ(HTTP_SUCCESS
, error
);
312 ASSERT_TRUE(file_list
);
313 // Do some sanity check. There are 4 entries that contain "File" in their
314 // titles and one of them is deleted.
315 EXPECT_EQ(3U, file_list
->items().size());
318 TEST_F(FakeDriveServiceTest
, SearchByTitle
) {
319 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
321 GDataErrorCode error
= GDATA_OTHER_ERROR
;
322 scoped_ptr
<FileList
> file_list
;
323 fake_service_
.SearchByTitle(
325 fake_service_
.GetRootResourceId(), // directory_resource_id
326 test_util::CreateCopyResultCallback(&error
, &file_list
));
327 base::RunLoop().RunUntilIdle();
329 EXPECT_EQ(HTTP_SUCCESS
, error
);
330 ASSERT_TRUE(file_list
);
331 // Do some sanity check. There are 2 entries that contain "1.txt" in their
332 // titles directly under the root directory.
333 EXPECT_EQ(2U, file_list
->items().size());
336 TEST_F(FakeDriveServiceTest
, SearchByTitle_EmptyDirectoryResourceId
) {
337 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
339 GDataErrorCode error
= GDATA_OTHER_ERROR
;
340 scoped_ptr
<FileList
> file_list
;
341 fake_service_
.SearchByTitle(
343 "", // directory resource id
344 test_util::CreateCopyResultCallback(&error
, &file_list
));
345 base::RunLoop().RunUntilIdle();
347 EXPECT_EQ(HTTP_SUCCESS
, error
);
348 ASSERT_TRUE(file_list
);
349 // Do some sanity check. There are 4 entries that contain "1.txt" in their
351 EXPECT_EQ(4U, file_list
->items().size());
354 TEST_F(FakeDriveServiceTest
, SearchByTitle_Offline
) {
355 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
356 fake_service_
.set_offline(true);
358 GDataErrorCode error
= GDATA_OTHER_ERROR
;
359 scoped_ptr
<FileList
> file_list
;
360 fake_service_
.SearchByTitle(
361 "Directory 1", // title
362 fake_service_
.GetRootResourceId(), // directory_resource_id
363 test_util::CreateCopyResultCallback(&error
, &file_list
));
364 base::RunLoop().RunUntilIdle();
366 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
367 EXPECT_FALSE(file_list
);
370 TEST_F(FakeDriveServiceTest
, GetChangeList_NoNewEntries
) {
371 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
373 GDataErrorCode error
= GDATA_OTHER_ERROR
;
374 scoped_ptr
<ChangeList
> change_list
;
375 fake_service_
.GetChangeList(
376 fake_service_
.about_resource().largest_change_id() + 1,
377 test_util::CreateCopyResultCallback(&error
, &change_list
));
378 base::RunLoop().RunUntilIdle();
380 EXPECT_EQ(HTTP_SUCCESS
, error
);
381 ASSERT_TRUE(change_list
);
382 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
383 change_list
->largest_change_id());
384 // This should be empty as the latest changestamp was passed to
385 // GetChangeList(), hence there should be no new entries.
386 EXPECT_EQ(0U, change_list
->items().size());
387 // It's considered loaded even if the result is empty.
388 EXPECT_EQ(1, fake_service_
.change_list_load_count());
391 TEST_F(FakeDriveServiceTest
, GetChangeList_WithNewEntry
) {
392 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
393 const int64 old_largest_change_id
=
394 fake_service_
.about_resource().largest_change_id();
396 // Add a new directory in the root directory.
397 ASSERT_TRUE(AddNewDirectory(
398 fake_service_
.GetRootResourceId(), "new directory"));
400 // Get the resource list newer than old_largest_change_id.
401 GDataErrorCode error
= GDATA_OTHER_ERROR
;
402 scoped_ptr
<ChangeList
> change_list
;
403 fake_service_
.GetChangeList(
404 old_largest_change_id
+ 1,
405 test_util::CreateCopyResultCallback(&error
, &change_list
));
406 base::RunLoop().RunUntilIdle();
408 EXPECT_EQ(HTTP_SUCCESS
, error
);
409 ASSERT_TRUE(change_list
);
410 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
411 change_list
->largest_change_id());
412 // The result should only contain the newly created directory.
413 ASSERT_EQ(1U, change_list
->items().size());
414 ASSERT_TRUE(change_list
->items()[0]->file());
415 EXPECT_EQ("new directory", change_list
->items()[0]->file()->title());
416 EXPECT_EQ(1, fake_service_
.change_list_load_count());
419 TEST_F(FakeDriveServiceTest
, GetChangeList_Offline
) {
420 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
421 fake_service_
.set_offline(true);
423 GDataErrorCode error
= GDATA_OTHER_ERROR
;
424 scoped_ptr
<ChangeList
> change_list
;
425 fake_service_
.GetChangeList(
426 654321, // start_changestamp
427 test_util::CreateCopyResultCallback(&error
, &change_list
));
428 base::RunLoop().RunUntilIdle();
430 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
431 EXPECT_FALSE(change_list
);
434 TEST_F(FakeDriveServiceTest
, GetChangeList_DeletedEntry
) {
435 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
436 ASSERT_TRUE(Exists("2_file_resource_id"));
437 const int64 old_largest_change_id
=
438 fake_service_
.about_resource().largest_change_id();
440 GDataErrorCode error
= GDATA_OTHER_ERROR
;
441 fake_service_
.DeleteResource("2_file_resource_id",
442 std::string(), // etag
443 test_util::CreateCopyResultCallback(&error
));
444 base::RunLoop().RunUntilIdle();
445 ASSERT_EQ(HTTP_NO_CONTENT
, error
);
446 ASSERT_FALSE(Exists("2_file_resource_id"));
448 // Get the resource list newer than old_largest_change_id.
449 error
= GDATA_OTHER_ERROR
;
450 scoped_ptr
<ChangeList
> change_list
;
451 fake_service_
.GetChangeList(
452 old_largest_change_id
+ 1,
453 test_util::CreateCopyResultCallback(&error
, &change_list
));
454 base::RunLoop().RunUntilIdle();
456 EXPECT_EQ(HTTP_SUCCESS
, error
);
457 ASSERT_TRUE(change_list
);
458 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
459 change_list
->largest_change_id());
460 // The result should only contain the deleted file.
461 ASSERT_EQ(1U, change_list
->items().size());
462 const ChangeResource
& item
= *change_list
->items()[0];
463 EXPECT_EQ("2_file_resource_id", item
.file_id());
464 EXPECT_FALSE(item
.file());
465 EXPECT_TRUE(item
.is_deleted());
466 EXPECT_EQ(1, fake_service_
.change_list_load_count());
469 TEST_F(FakeDriveServiceTest
, GetChangeList_TrashedEntry
) {
470 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
471 ASSERT_TRUE(Exists("2_file_resource_id"));
472 const int64 old_largest_change_id
=
473 fake_service_
.about_resource().largest_change_id();
475 GDataErrorCode error
= GDATA_OTHER_ERROR
;
476 fake_service_
.TrashResource("2_file_resource_id",
477 test_util::CreateCopyResultCallback(&error
));
478 base::RunLoop().RunUntilIdle();
479 ASSERT_EQ(HTTP_SUCCESS
, error
);
480 ASSERT_FALSE(Exists("2_file_resource_id"));
482 // Get the resource list newer than old_largest_change_id.
483 error
= GDATA_OTHER_ERROR
;
484 scoped_ptr
<ChangeList
> change_list
;
485 fake_service_
.GetChangeList(
486 old_largest_change_id
+ 1,
487 test_util::CreateCopyResultCallback(&error
, &change_list
));
488 base::RunLoop().RunUntilIdle();
490 EXPECT_EQ(HTTP_SUCCESS
, error
);
491 ASSERT_TRUE(change_list
);
492 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
493 change_list
->largest_change_id());
494 // The result should only contain the trashed file.
495 ASSERT_EQ(1U, change_list
->items().size());
496 const ChangeResource
& item
= *change_list
->items()[0];
497 EXPECT_EQ("2_file_resource_id", item
.file_id());
498 ASSERT_TRUE(item
.file());
499 EXPECT_TRUE(item
.file()->labels().is_trashed());
500 EXPECT_EQ(1, fake_service_
.change_list_load_count());
503 TEST_F(FakeDriveServiceTest
, GetRemainingFileList_GetAllFileList
) {
504 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
505 fake_service_
.set_default_max_results(6);
507 GDataErrorCode error
= GDATA_OTHER_ERROR
;
508 scoped_ptr
<FileList
> file_list
;
509 fake_service_
.GetAllFileList(
510 test_util::CreateCopyResultCallback(&error
, &file_list
));
511 base::RunLoop().RunUntilIdle();
512 EXPECT_EQ(HTTP_SUCCESS
, error
);
513 ASSERT_TRUE(file_list
);
515 // Do some sanity check.
516 // The number of results is 14 entries. Thus, it should split into three
517 // chunks: 6, 6, and then 2.
518 EXPECT_EQ(6U, file_list
->items().size());
519 EXPECT_EQ(1, fake_service_
.file_list_load_count());
521 // Second page loading.
522 // Keep the next url before releasing the |file_list|.
523 GURL
next_url(file_list
->next_link());
525 error
= GDATA_OTHER_ERROR
;
527 fake_service_
.GetRemainingFileList(
529 test_util::CreateCopyResultCallback(&error
, &file_list
));
530 base::RunLoop().RunUntilIdle();
532 EXPECT_EQ(HTTP_SUCCESS
, error
);
533 ASSERT_TRUE(file_list
);
535 EXPECT_EQ(6U, file_list
->items().size());
536 EXPECT_EQ(1, fake_service_
.file_list_load_count());
538 // Third page loading.
539 next_url
= file_list
->next_link();
541 error
= GDATA_OTHER_ERROR
;
543 fake_service_
.GetRemainingFileList(
545 test_util::CreateCopyResultCallback(&error
, &file_list
));
546 base::RunLoop().RunUntilIdle();
548 EXPECT_EQ(HTTP_SUCCESS
, error
);
549 ASSERT_TRUE(file_list
);
551 EXPECT_EQ(3U, file_list
->items().size());
552 EXPECT_EQ(1, fake_service_
.file_list_load_count());
555 TEST_F(FakeDriveServiceTest
, GetRemainingFileList_GetFileListInDirectory
) {
556 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
557 fake_service_
.set_default_max_results(3);
559 GDataErrorCode error
= GDATA_OTHER_ERROR
;
560 scoped_ptr
<FileList
> file_list
;
561 fake_service_
.GetFileListInDirectory(
562 fake_service_
.GetRootResourceId(),
563 test_util::CreateCopyResultCallback(&error
, &file_list
));
564 base::RunLoop().RunUntilIdle();
565 EXPECT_EQ(HTTP_SUCCESS
, error
);
566 ASSERT_TRUE(file_list
);
568 // Do some sanity check.
569 // The number of results is 8 entries. Thus, it should split into three
570 // chunks: 3, 3, and then 2.
571 EXPECT_EQ(3U, file_list
->items().size());
572 EXPECT_EQ(1, fake_service_
.directory_load_count());
574 // Second page loading.
575 // Keep the next url before releasing the |file_list|.
576 GURL next_url
= file_list
->next_link();
578 error
= GDATA_OTHER_ERROR
;
580 fake_service_
.GetRemainingFileList(
582 test_util::CreateCopyResultCallback(&error
, &file_list
));
583 base::RunLoop().RunUntilIdle();
585 EXPECT_EQ(HTTP_SUCCESS
, error
);
586 ASSERT_TRUE(file_list
);
588 EXPECT_EQ(3U, file_list
->items().size());
589 EXPECT_EQ(1, fake_service_
.directory_load_count());
591 // Third page loading.
592 next_url
= file_list
->next_link();
594 error
= GDATA_OTHER_ERROR
;
596 fake_service_
.GetRemainingFileList(
598 test_util::CreateCopyResultCallback(&error
, &file_list
));
599 base::RunLoop().RunUntilIdle();
601 EXPECT_EQ(HTTP_SUCCESS
, error
);
602 ASSERT_TRUE(file_list
);
604 EXPECT_EQ(2U, file_list
->items().size());
605 EXPECT_EQ(1, fake_service_
.directory_load_count());
608 TEST_F(FakeDriveServiceTest
, GetRemainingFileList_Search
) {
609 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
610 fake_service_
.set_default_max_results(2);
612 GDataErrorCode error
= GDATA_OTHER_ERROR
;
613 scoped_ptr
<FileList
> file_list
;
614 fake_service_
.Search(
615 "File", // search_query
616 test_util::CreateCopyResultCallback(&error
, &file_list
));
617 base::RunLoop().RunUntilIdle();
618 EXPECT_EQ(HTTP_SUCCESS
, error
);
619 ASSERT_TRUE(file_list
);
621 // Do some sanity check.
622 // The number of results is 4 entries. Thus, it should split into two
623 // chunks: 2, and then 2
624 EXPECT_EQ(2U, file_list
->items().size());
626 // Second page loading.
627 // Keep the next url before releasing the |file_list|.
628 GURL next_url
= file_list
->next_link();
630 error
= GDATA_OTHER_ERROR
;
632 fake_service_
.GetRemainingFileList(
634 test_util::CreateCopyResultCallback(&error
, &file_list
));
635 base::RunLoop().RunUntilIdle();
637 EXPECT_EQ(HTTP_SUCCESS
, error
);
638 ASSERT_TRUE(file_list
);
640 EXPECT_EQ(2U, file_list
->items().size());
643 TEST_F(FakeDriveServiceTest
, GetRemainingChangeList_GetChangeList
) {
644 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
645 fake_service_
.set_default_max_results(2);
646 const int64 old_largest_change_id
=
647 fake_service_
.about_resource().largest_change_id();
649 // Add 5 new directory in the root directory.
650 for (int i
= 0; i
< 5; ++i
) {
651 ASSERT_TRUE(AddNewDirectory(
652 fake_service_
.GetRootResourceId(),
653 base::StringPrintf("new directory %d", i
)));
656 GDataErrorCode error
= GDATA_OTHER_ERROR
;
657 scoped_ptr
<ChangeList
> change_list
;
658 fake_service_
.GetChangeList(
659 old_largest_change_id
+ 1, // start_changestamp
660 test_util::CreateCopyResultCallback(&error
, &change_list
));
661 base::RunLoop().RunUntilIdle();
662 EXPECT_EQ(HTTP_SUCCESS
, error
);
663 ASSERT_TRUE(change_list
);
665 // Do some sanity check.
666 // The number of results is 5 entries. Thus, it should split into three
667 // chunks: 2, 2 and then 1.
668 EXPECT_EQ(2U, change_list
->items().size());
669 EXPECT_EQ(1, fake_service_
.change_list_load_count());
671 // Second page loading.
672 // Keep the next url before releasing the |change_list|.
673 GURL next_url
= change_list
->next_link();
675 error
= GDATA_OTHER_ERROR
;
677 fake_service_
.GetRemainingChangeList(
679 test_util::CreateCopyResultCallback(&error
, &change_list
));
680 base::RunLoop().RunUntilIdle();
682 EXPECT_EQ(HTTP_SUCCESS
, error
);
683 ASSERT_TRUE(change_list
);
685 EXPECT_EQ(2U, change_list
->items().size());
686 EXPECT_EQ(1, fake_service_
.change_list_load_count());
688 // Third page loading.
689 next_url
= change_list
->next_link();
691 error
= GDATA_OTHER_ERROR
;
693 fake_service_
.GetRemainingChangeList(
695 test_util::CreateCopyResultCallback(&error
, &change_list
));
696 base::RunLoop().RunUntilIdle();
698 EXPECT_EQ(HTTP_SUCCESS
, error
);
699 ASSERT_TRUE(change_list
);
701 EXPECT_EQ(1U, change_list
->items().size());
702 EXPECT_EQ(1, fake_service_
.change_list_load_count());
705 TEST_F(FakeDriveServiceTest
, GetAboutResource
) {
706 GDataErrorCode error
= GDATA_OTHER_ERROR
;
707 scoped_ptr
<AboutResource
> about_resource
;
708 fake_service_
.GetAboutResource(
709 test_util::CreateCopyResultCallback(&error
, &about_resource
));
710 base::RunLoop().RunUntilIdle();
712 EXPECT_EQ(HTTP_SUCCESS
, error
);
714 ASSERT_TRUE(about_resource
);
715 // Do some sanity check.
716 EXPECT_EQ(fake_service_
.GetRootResourceId(),
717 about_resource
->root_folder_id());
718 EXPECT_EQ(1, fake_service_
.about_resource_load_count());
721 TEST_F(FakeDriveServiceTest
, GetAboutResource_Offline
) {
722 fake_service_
.set_offline(true);
724 GDataErrorCode error
= GDATA_OTHER_ERROR
;
725 scoped_ptr
<AboutResource
> about_resource
;
726 fake_service_
.GetAboutResource(
727 test_util::CreateCopyResultCallback(&error
, &about_resource
));
728 base::RunLoop().RunUntilIdle();
730 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
731 EXPECT_FALSE(about_resource
);
734 TEST_F(FakeDriveServiceTest
, GetAppList
) {
735 ASSERT_TRUE(fake_service_
.LoadAppListForDriveApi(
736 "drive/applist.json"));
738 GDataErrorCode error
= GDATA_OTHER_ERROR
;
739 scoped_ptr
<AppList
> app_list
;
740 fake_service_
.GetAppList(
741 test_util::CreateCopyResultCallback(&error
, &app_list
));
742 base::RunLoop().RunUntilIdle();
744 EXPECT_EQ(HTTP_SUCCESS
, error
);
746 ASSERT_TRUE(app_list
);
747 EXPECT_EQ(1, fake_service_
.app_list_load_count());
750 TEST_F(FakeDriveServiceTest
, GetAppList_Offline
) {
751 ASSERT_TRUE(fake_service_
.LoadAppListForDriveApi(
752 "drive/applist.json"));
753 fake_service_
.set_offline(true);
755 GDataErrorCode error
= GDATA_OTHER_ERROR
;
756 scoped_ptr
<AppList
> app_list
;
757 fake_service_
.GetAppList(
758 test_util::CreateCopyResultCallback(&error
, &app_list
));
759 base::RunLoop().RunUntilIdle();
761 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
762 EXPECT_FALSE(app_list
);
765 TEST_F(FakeDriveServiceTest
, GetFileResource_ExistingFile
) {
766 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
768 const std::string kResourceId
= "2_file_resource_id";
769 GDataErrorCode error
= GDATA_OTHER_ERROR
;
770 scoped_ptr
<FileResource
> entry
;
771 fake_service_
.GetFileResource(
772 kResourceId
, test_util::CreateCopyResultCallback(&error
, &entry
));
773 base::RunLoop().RunUntilIdle();
775 EXPECT_EQ(HTTP_SUCCESS
, error
);
777 // Do some sanity check.
778 EXPECT_EQ(kResourceId
, entry
->file_id());
781 TEST_F(FakeDriveServiceTest
, GetFileResource_NonexistingFile
) {
782 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
784 const std::string kResourceId
= "nonexisting_resource_id";
785 GDataErrorCode error
= GDATA_OTHER_ERROR
;
786 scoped_ptr
<FileResource
> entry
;
787 fake_service_
.GetFileResource(
788 kResourceId
, test_util::CreateCopyResultCallback(&error
, &entry
));
789 base::RunLoop().RunUntilIdle();
791 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
795 TEST_F(FakeDriveServiceTest
, GetFileResource_Offline
) {
796 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
797 fake_service_
.set_offline(true);
799 const std::string kResourceId
= "2_file_resource_id";
800 GDataErrorCode error
= GDATA_OTHER_ERROR
;
801 scoped_ptr
<FileResource
> entry
;
802 fake_service_
.GetFileResource(
803 kResourceId
, test_util::CreateCopyResultCallback(&error
, &entry
));
804 base::RunLoop().RunUntilIdle();
806 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
810 TEST_F(FakeDriveServiceTest
, GetShareUrl
) {
811 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
813 const std::string kResourceId
= "2_file_resource_id";
814 GDataErrorCode error
= GDATA_OTHER_ERROR
;
816 fake_service_
.GetShareUrl(
818 GURL(), // embed origin
819 test_util::CreateCopyResultCallback(&error
, &share_url
));
820 base::RunLoop().RunUntilIdle();
822 EXPECT_EQ(HTTP_SUCCESS
, error
);
823 EXPECT_FALSE(share_url
.is_empty());
826 TEST_F(FakeDriveServiceTest
, DeleteResource_ExistingFile
) {
827 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
829 // Resource "2_file_resource_id" should now exist.
830 ASSERT_TRUE(Exists("2_file_resource_id"));
832 GDataErrorCode error
= GDATA_OTHER_ERROR
;
833 fake_service_
.DeleteResource("2_file_resource_id",
834 std::string(), // etag
835 test_util::CreateCopyResultCallback(&error
));
836 base::RunLoop().RunUntilIdle();
838 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
839 // Resource "2_file_resource_id" should be gone now.
840 EXPECT_FALSE(Exists("2_file_resource_id"));
842 error
= GDATA_OTHER_ERROR
;
843 fake_service_
.DeleteResource("2_file_resource_id",
844 std::string(), // etag
845 test_util::CreateCopyResultCallback(&error
));
846 base::RunLoop().RunUntilIdle();
847 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
848 EXPECT_FALSE(Exists("2_file_resource_id"));
851 TEST_F(FakeDriveServiceTest
, DeleteResource_NonexistingFile
) {
852 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
854 GDataErrorCode error
= GDATA_OTHER_ERROR
;
855 fake_service_
.DeleteResource("nonexisting_resource_id",
856 std::string(), // etag
857 test_util::CreateCopyResultCallback(&error
));
858 base::RunLoop().RunUntilIdle();
860 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
863 TEST_F(FakeDriveServiceTest
, DeleteResource_ETagMatch
) {
864 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
866 // Resource "2_file_resource_id" should now exist.
867 scoped_ptr
<FileResource
> entry
= FindEntry("2_file_resource_id");
869 ASSERT_FALSE(entry
->labels().is_trashed());
870 ASSERT_FALSE(entry
->etag().empty());
872 GDataErrorCode error
= GDATA_OTHER_ERROR
;
873 fake_service_
.DeleteResource("2_file_resource_id",
874 entry
->etag() + "_mismatch",
875 test_util::CreateCopyResultCallback(&error
));
876 base::RunLoop().RunUntilIdle();
878 EXPECT_EQ(HTTP_PRECONDITION
, error
);
879 // Resource "2_file_resource_id" should still exist.
880 EXPECT_TRUE(Exists("2_file_resource_id"));
882 error
= GDATA_OTHER_ERROR
;
883 fake_service_
.DeleteResource("2_file_resource_id",
885 test_util::CreateCopyResultCallback(&error
));
886 base::RunLoop().RunUntilIdle();
887 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
888 // Resource "2_file_resource_id" should be gone now.
889 EXPECT_FALSE(Exists("2_file_resource_id"));
892 TEST_F(FakeDriveServiceTest
, DeleteResource_Offline
) {
893 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
894 fake_service_
.set_offline(true);
896 GDataErrorCode error
= GDATA_OTHER_ERROR
;
897 fake_service_
.DeleteResource("2_file_resource_id",
898 std::string(), // etag
899 test_util::CreateCopyResultCallback(&error
));
900 base::RunLoop().RunUntilIdle();
902 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
905 TEST_F(FakeDriveServiceTest
, DeleteResource_Forbidden
) {
906 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
908 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
909 "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER
));
911 GDataErrorCode error
= GDATA_OTHER_ERROR
;
912 fake_service_
.DeleteResource("2_file_resource_id",
913 std::string(), // etag
914 test_util::CreateCopyResultCallback(&error
));
915 base::RunLoop().RunUntilIdle();
917 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
920 TEST_F(FakeDriveServiceTest
, TrashResource_ExistingFile
) {
921 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
923 // Resource "2_file_resource_id" should now exist.
924 ASSERT_TRUE(Exists("2_file_resource_id"));
926 GDataErrorCode error
= GDATA_OTHER_ERROR
;
927 fake_service_
.TrashResource("2_file_resource_id",
928 test_util::CreateCopyResultCallback(&error
));
929 base::RunLoop().RunUntilIdle();
931 EXPECT_EQ(HTTP_SUCCESS
, error
);
932 // Resource "2_file_resource_id" should be gone now.
933 EXPECT_FALSE(Exists("2_file_resource_id"));
935 error
= GDATA_OTHER_ERROR
;
936 fake_service_
.TrashResource("2_file_resource_id",
937 test_util::CreateCopyResultCallback(&error
));
938 base::RunLoop().RunUntilIdle();
939 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
940 EXPECT_FALSE(Exists("2_file_resource_id"));
943 TEST_F(FakeDriveServiceTest
, TrashResource_NonexistingFile
) {
944 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
946 GDataErrorCode error
= GDATA_OTHER_ERROR
;
947 fake_service_
.TrashResource("nonexisting_resource_id",
948 test_util::CreateCopyResultCallback(&error
));
949 base::RunLoop().RunUntilIdle();
951 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
954 TEST_F(FakeDriveServiceTest
, TrashResource_Offline
) {
955 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
956 fake_service_
.set_offline(true);
958 GDataErrorCode error
= GDATA_OTHER_ERROR
;
959 fake_service_
.TrashResource("2_file_resource_id",
960 test_util::CreateCopyResultCallback(&error
));
961 base::RunLoop().RunUntilIdle();
963 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
966 TEST_F(FakeDriveServiceTest
, TrashResource_Forbidden
) {
967 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
969 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
970 "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER
));
972 GDataErrorCode error
= GDATA_OTHER_ERROR
;
973 fake_service_
.TrashResource("2_file_resource_id",
974 test_util::CreateCopyResultCallback(&error
));
975 base::RunLoop().RunUntilIdle();
977 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
980 TEST_F(FakeDriveServiceTest
, DownloadFile_ExistingFile
) {
981 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
983 base::ScopedTempDir temp_dir
;
984 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
986 std::vector
<test_util::ProgressInfo
> download_progress_values
;
988 const base::FilePath kOutputFilePath
=
989 temp_dir
.path().AppendASCII("whatever.txt");
990 GDataErrorCode error
= GDATA_OTHER_ERROR
;
991 base::FilePath output_file_path
;
992 test_util::TestGetContentCallback get_content_callback
;
993 fake_service_
.DownloadFile(
995 "2_file_resource_id",
996 test_util::CreateCopyResultCallback(&error
, &output_file_path
),
997 get_content_callback
.callback(),
998 base::Bind(&test_util::AppendProgressCallbackResult
,
999 &download_progress_values
));
1000 base::RunLoop().RunUntilIdle();
1002 EXPECT_EQ(HTTP_SUCCESS
, error
);
1003 EXPECT_EQ(output_file_path
, kOutputFilePath
);
1004 std::string content
;
1005 ASSERT_TRUE(base::ReadFileToString(output_file_path
, &content
));
1006 EXPECT_EQ("This is some test content.", content
);
1007 ASSERT_TRUE(!download_progress_values
.empty());
1008 EXPECT_TRUE(base::STLIsSorted(download_progress_values
));
1009 EXPECT_LE(0, download_progress_values
.front().first
);
1010 EXPECT_GE(26, download_progress_values
.back().first
);
1011 EXPECT_EQ(content
, get_content_callback
.GetConcatenatedData());
1014 TEST_F(FakeDriveServiceTest
, DownloadFile_NonexistingFile
) {
1015 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1017 base::ScopedTempDir temp_dir
;
1018 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1020 const base::FilePath kOutputFilePath
=
1021 temp_dir
.path().AppendASCII("whatever.txt");
1022 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1023 base::FilePath output_file_path
;
1024 fake_service_
.DownloadFile(
1026 "non_existent_file_resource_id",
1027 test_util::CreateCopyResultCallback(&error
, &output_file_path
),
1028 GetContentCallback(),
1029 ProgressCallback());
1030 base::RunLoop().RunUntilIdle();
1032 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1035 TEST_F(FakeDriveServiceTest
, DownloadFile_Offline
) {
1036 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1037 fake_service_
.set_offline(true);
1039 base::ScopedTempDir temp_dir
;
1040 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1042 const base::FilePath kOutputFilePath
=
1043 temp_dir
.path().AppendASCII("whatever.txt");
1044 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1045 base::FilePath output_file_path
;
1046 fake_service_
.DownloadFile(
1048 "2_file_resource_id",
1049 test_util::CreateCopyResultCallback(&error
, &output_file_path
),
1050 GetContentCallback(),
1051 ProgressCallback());
1052 base::RunLoop().RunUntilIdle();
1054 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1057 TEST_F(FakeDriveServiceTest
, CopyResource
) {
1058 const base::Time::Exploded kModifiedDate
= {2012, 7, 0, 19, 15, 59, 13, 123};
1060 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1062 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1064 const std::string kResourceId
= "2_file_resource_id";
1065 const std::string kParentResourceId
= "2_folder_resource_id";
1066 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1067 scoped_ptr
<FileResource
> entry
;
1068 fake_service_
.CopyResource(
1072 base::Time::FromUTCExploded(kModifiedDate
),
1073 test_util::CreateCopyResultCallback(&error
, &entry
));
1074 base::RunLoop().RunUntilIdle();
1076 EXPECT_EQ(HTTP_SUCCESS
, error
);
1078 // The copied entry should have the new resource ID and the title.
1079 EXPECT_NE(kResourceId
, entry
->file_id());
1080 EXPECT_EQ("new title", entry
->title());
1081 EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate
), entry
->modified_date());
1082 EXPECT_TRUE(HasParent(entry
->file_id(), kParentResourceId
));
1083 // Should be incremented as a new hosted document was created.
1084 EXPECT_EQ(old_largest_change_id
+ 1,
1085 fake_service_
.about_resource().largest_change_id());
1086 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1089 TEST_F(FakeDriveServiceTest
, CopyResource_NonExisting
) {
1090 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1092 const std::string kResourceId
= "nonexisting_resource_id";
1093 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1094 scoped_ptr
<FileResource
> entry
;
1095 fake_service_
.CopyResource(
1097 "1_folder_resource_id",
1100 test_util::CreateCopyResultCallback(&error
, &entry
));
1101 base::RunLoop().RunUntilIdle();
1103 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1106 TEST_F(FakeDriveServiceTest
, CopyResource_EmptyParentResourceId
) {
1107 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1109 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1111 const std::string kResourceId
= "2_file_resource_id";
1112 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1113 scoped_ptr
<FileResource
> entry
;
1114 fake_service_
.CopyResource(
1119 test_util::CreateCopyResultCallback(&error
, &entry
));
1120 base::RunLoop().RunUntilIdle();
1122 EXPECT_EQ(HTTP_SUCCESS
, error
);
1124 // The copied entry should have the new resource ID and the title.
1125 EXPECT_NE(kResourceId
, entry
->file_id());
1126 EXPECT_EQ("new title", entry
->title());
1127 EXPECT_TRUE(HasParent(kResourceId
, fake_service_
.GetRootResourceId()));
1128 // Should be incremented as a new hosted document was created.
1129 EXPECT_EQ(old_largest_change_id
+ 1,
1130 fake_service_
.about_resource().largest_change_id());
1131 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1134 TEST_F(FakeDriveServiceTest
, CopyResource_Offline
) {
1135 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1136 fake_service_
.set_offline(true);
1138 const std::string kResourceId
= "2_file_resource_id";
1139 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1140 scoped_ptr
<FileResource
> entry
;
1141 fake_service_
.CopyResource(
1143 "1_folder_resource_id",
1146 test_util::CreateCopyResultCallback(&error
, &entry
));
1147 base::RunLoop().RunUntilIdle();
1149 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1150 EXPECT_FALSE(entry
);
1153 TEST_F(FakeDriveServiceTest
, UpdateResource
) {
1154 const base::Time::Exploded kModifiedDate
= {2012, 7, 0, 19, 15, 59, 13, 123};
1155 const base::Time::Exploded kViewedDate
= {2013, 8, 1, 20, 16, 00, 14, 234};
1157 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1159 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1161 const std::string kResourceId
= "2_file_resource_id";
1162 const std::string kParentResourceId
= "2_folder_resource_id";
1163 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1164 scoped_ptr
<FileResource
> entry
;
1165 fake_service_
.UpdateResource(
1169 base::Time::FromUTCExploded(kModifiedDate
),
1170 base::Time::FromUTCExploded(kViewedDate
),
1171 test_util::CreateCopyResultCallback(&error
, &entry
));
1172 base::RunLoop().RunUntilIdle();
1174 EXPECT_EQ(HTTP_SUCCESS
, error
);
1176 // The updated entry should have the new title.
1177 EXPECT_EQ(kResourceId
, entry
->file_id());
1178 EXPECT_EQ("new title", entry
->title());
1179 EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate
),
1180 entry
->modified_date());
1181 EXPECT_EQ(base::Time::FromUTCExploded(kViewedDate
),
1182 entry
->last_viewed_by_me_date());
1183 EXPECT_TRUE(HasParent(kResourceId
, kParentResourceId
));
1184 // Should be incremented as a new hosted document was created.
1185 EXPECT_EQ(old_largest_change_id
+ 1,
1186 fake_service_
.about_resource().largest_change_id());
1187 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1190 TEST_F(FakeDriveServiceTest
, UpdateResource_NonExisting
) {
1191 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1193 const std::string kResourceId
= "nonexisting_resource_id";
1194 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1195 scoped_ptr
<FileResource
> entry
;
1196 fake_service_
.UpdateResource(
1198 "1_folder_resource_id",
1202 test_util::CreateCopyResultCallback(&error
, &entry
));
1203 base::RunLoop().RunUntilIdle();
1205 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1208 TEST_F(FakeDriveServiceTest
, UpdateResource_EmptyParentResourceId
) {
1209 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1211 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1213 const std::string kResourceId
= "2_file_resource_id";
1215 // Just make sure that the resource is under root.
1216 ASSERT_TRUE(HasParent(kResourceId
, "fake_root"));
1218 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1219 scoped_ptr
<FileResource
> entry
;
1220 fake_service_
.UpdateResource(
1226 test_util::CreateCopyResultCallback(&error
, &entry
));
1227 base::RunLoop().RunUntilIdle();
1229 EXPECT_EQ(HTTP_SUCCESS
, error
);
1231 // The updated entry should have the new title.
1232 EXPECT_EQ(kResourceId
, entry
->file_id());
1233 EXPECT_EQ("new title", entry
->title());
1234 EXPECT_TRUE(HasParent(kResourceId
, "fake_root"));
1235 // Should be incremented as a new hosted document was created.
1236 EXPECT_EQ(old_largest_change_id
+ 1,
1237 fake_service_
.about_resource().largest_change_id());
1238 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1241 TEST_F(FakeDriveServiceTest
, UpdateResource_Offline
) {
1242 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1243 fake_service_
.set_offline(true);
1245 const std::string kResourceId
= "2_file_resource_id";
1246 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1247 scoped_ptr
<FileResource
> entry
;
1248 fake_service_
.UpdateResource(
1254 test_util::CreateCopyResultCallback(&error
, &entry
));
1255 base::RunLoop().RunUntilIdle();
1257 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1258 EXPECT_FALSE(entry
);
1261 TEST_F(FakeDriveServiceTest
, UpdateResource_Forbidden
) {
1262 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1264 const std::string kResourceId
= "2_file_resource_id";
1265 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
1266 kResourceId
, google_apis::drive::PERMISSION_ROLE_READER
));
1268 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1269 scoped_ptr
<FileResource
> entry
;
1270 fake_service_
.UpdateResource(
1276 test_util::CreateCopyResultCallback(&error
, &entry
));
1277 base::RunLoop().RunUntilIdle();
1279 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
1280 EXPECT_FALSE(entry
);
1283 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_FileInRootDirectory
) {
1284 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1286 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1288 const std::string kResourceId
= "2_file_resource_id";
1289 const std::string kOldParentResourceId
= fake_service_
.GetRootResourceId();
1290 const std::string kNewParentResourceId
= "1_folder_resource_id";
1292 // Here's the original parent link.
1293 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1294 EXPECT_FALSE(HasParent(kResourceId
, kNewParentResourceId
));
1296 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1297 fake_service_
.AddResourceToDirectory(
1298 kNewParentResourceId
,
1300 test_util::CreateCopyResultCallback(&error
));
1301 base::RunLoop().RunUntilIdle();
1303 EXPECT_EQ(HTTP_SUCCESS
, error
);
1305 // The parent link should now be changed.
1306 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1307 EXPECT_TRUE(HasParent(kResourceId
, kNewParentResourceId
));
1308 // Should be incremented as a file was moved.
1309 EXPECT_EQ(old_largest_change_id
+ 1,
1310 fake_service_
.about_resource().largest_change_id());
1311 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1314 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_FileInNonRootDirectory
) {
1315 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1317 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1319 const std::string kResourceId
= "subdirectory_file_1_id";
1320 const std::string kOldParentResourceId
= "1_folder_resource_id";
1321 const std::string kNewParentResourceId
= "2_folder_resource_id";
1323 // Here's the original parent link.
1324 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1325 EXPECT_FALSE(HasParent(kResourceId
, kNewParentResourceId
));
1327 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1328 fake_service_
.AddResourceToDirectory(
1329 kNewParentResourceId
,
1331 test_util::CreateCopyResultCallback(&error
));
1332 base::RunLoop().RunUntilIdle();
1334 EXPECT_EQ(HTTP_SUCCESS
, error
);
1336 // The parent link should now be changed.
1337 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1338 EXPECT_TRUE(HasParent(kResourceId
, kNewParentResourceId
));
1339 // Should be incremented as a file was moved.
1340 EXPECT_EQ(old_largest_change_id
+ 1,
1341 fake_service_
.about_resource().largest_change_id());
1342 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1345 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_NonexistingFile
) {
1346 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1348 const std::string kResourceId
= "nonexisting_file";
1349 const std::string kNewParentResourceId
= "1_folder_resource_id";
1351 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1352 fake_service_
.AddResourceToDirectory(
1353 kNewParentResourceId
,
1355 test_util::CreateCopyResultCallback(&error
));
1356 base::RunLoop().RunUntilIdle();
1358 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1361 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_OrphanFile
) {
1362 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1364 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1366 const std::string kResourceId
= "1_orphanfile_resource_id";
1367 const std::string kNewParentResourceId
= "1_folder_resource_id";
1369 // The file does not belong to any directory, even to the root.
1370 EXPECT_FALSE(HasParent(kResourceId
, kNewParentResourceId
));
1371 EXPECT_FALSE(HasParent(kResourceId
, fake_service_
.GetRootResourceId()));
1373 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1374 fake_service_
.AddResourceToDirectory(
1375 kNewParentResourceId
,
1377 test_util::CreateCopyResultCallback(&error
));
1378 base::RunLoop().RunUntilIdle();
1380 EXPECT_EQ(HTTP_SUCCESS
, error
);
1382 // The parent link should now be changed.
1383 EXPECT_TRUE(HasParent(kResourceId
, kNewParentResourceId
));
1384 EXPECT_FALSE(HasParent(kResourceId
, fake_service_
.GetRootResourceId()));
1385 // Should be incremented as a file was moved.
1386 EXPECT_EQ(old_largest_change_id
+ 1,
1387 fake_service_
.about_resource().largest_change_id());
1388 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1391 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_Offline
) {
1392 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1393 fake_service_
.set_offline(true);
1395 const std::string kResourceId
= "2_file_resource_id";
1396 const std::string kNewParentResourceId
= "1_folder_resource_id";
1398 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1399 fake_service_
.AddResourceToDirectory(
1400 kNewParentResourceId
,
1402 test_util::CreateCopyResultCallback(&error
));
1403 base::RunLoop().RunUntilIdle();
1405 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1408 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_ExistingFile
) {
1409 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1411 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1413 const std::string kResourceId
= "subdirectory_file_1_id";
1414 const std::string kParentResourceId
= "1_folder_resource_id";
1416 scoped_ptr
<FileResource
> entry
= FindEntry(kResourceId
);
1418 // The entry should have a parent now.
1419 ASSERT_FALSE(entry
->parents().empty());
1421 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1422 fake_service_
.RemoveResourceFromDirectory(
1425 test_util::CreateCopyResultCallback(&error
));
1426 base::RunLoop().RunUntilIdle();
1428 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
1430 entry
= FindEntry(kResourceId
);
1432 // The entry should have no parent now.
1433 ASSERT_TRUE(entry
->parents().empty());
1434 // Should be incremented as a file was moved to the root directory.
1435 EXPECT_EQ(old_largest_change_id
+ 1,
1436 fake_service_
.about_resource().largest_change_id());
1437 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1440 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_NonexistingFile
) {
1441 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1443 const std::string kResourceId
= "nonexisting_file";
1444 const std::string kParentResourceId
= "1_folder_resource_id";
1446 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1447 fake_service_
.RemoveResourceFromDirectory(
1450 test_util::CreateCopyResultCallback(&error
));
1451 base::RunLoop().RunUntilIdle();
1453 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1456 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_OrphanFile
) {
1457 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1459 const std::string kResourceId
= "1_orphanfile_resource_id";
1460 const std::string kParentResourceId
= fake_service_
.GetRootResourceId();
1462 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1463 fake_service_
.RemoveResourceFromDirectory(
1466 test_util::CreateCopyResultCallback(&error
));
1467 base::RunLoop().RunUntilIdle();
1469 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1472 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_Offline
) {
1473 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1474 fake_service_
.set_offline(true);
1476 const std::string kResourceId
= "subdirectory_file_1_id";
1477 const std::string kParentResourceId
= "1_folder_resource_id";
1479 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1480 fake_service_
.RemoveResourceFromDirectory(
1483 test_util::CreateCopyResultCallback(&error
));
1484 base::RunLoop().RunUntilIdle();
1486 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1489 TEST_F(FakeDriveServiceTest
, AddNewDirectory_EmptyParent
) {
1490 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1492 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1494 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1495 scoped_ptr
<FileResource
> entry
;
1496 fake_service_
.AddNewDirectory(
1499 DriveServiceInterface::AddNewDirectoryOptions(),
1500 test_util::CreateCopyResultCallback(&error
, &entry
));
1501 base::RunLoop().RunUntilIdle();
1503 EXPECT_EQ(HTTP_CREATED
, error
);
1505 EXPECT_TRUE(entry
->IsDirectory());
1506 EXPECT_EQ("resource_id_1", entry
->file_id());
1507 EXPECT_EQ("new directory", entry
->title());
1508 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1509 // Should be incremented as a new directory was created.
1510 EXPECT_EQ(old_largest_change_id
+ 1,
1511 fake_service_
.about_resource().largest_change_id());
1512 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1515 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToRootDirectory
) {
1516 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1518 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1520 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1521 scoped_ptr
<FileResource
> entry
;
1522 fake_service_
.AddNewDirectory(
1523 fake_service_
.GetRootResourceId(),
1525 DriveServiceInterface::AddNewDirectoryOptions(),
1526 test_util::CreateCopyResultCallback(&error
, &entry
));
1527 base::RunLoop().RunUntilIdle();
1529 EXPECT_EQ(HTTP_CREATED
, error
);
1531 EXPECT_TRUE(entry
->IsDirectory());
1532 EXPECT_EQ("resource_id_1", entry
->file_id());
1533 EXPECT_EQ("new directory", entry
->title());
1534 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1535 // Should be incremented as a new directory was created.
1536 EXPECT_EQ(old_largest_change_id
+ 1,
1537 fake_service_
.about_resource().largest_change_id());
1538 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1541 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToRootDirectoryOnEmptyFileSystem
) {
1542 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1544 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1545 scoped_ptr
<FileResource
> entry
;
1546 fake_service_
.AddNewDirectory(
1547 fake_service_
.GetRootResourceId(),
1549 DriveServiceInterface::AddNewDirectoryOptions(),
1550 test_util::CreateCopyResultCallback(&error
, &entry
));
1551 base::RunLoop().RunUntilIdle();
1553 EXPECT_EQ(HTTP_CREATED
, error
);
1555 EXPECT_TRUE(entry
->IsDirectory());
1556 EXPECT_EQ("resource_id_1", entry
->file_id());
1557 EXPECT_EQ("new directory", entry
->title());
1558 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1559 // Should be incremented as a new directory was created.
1560 EXPECT_EQ(old_largest_change_id
+ 1,
1561 fake_service_
.about_resource().largest_change_id());
1562 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1565 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToNonRootDirectory
) {
1566 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1568 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1570 const std::string kParentResourceId
= "1_folder_resource_id";
1572 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1573 scoped_ptr
<FileResource
> entry
;
1574 fake_service_
.AddNewDirectory(
1577 DriveServiceInterface::AddNewDirectoryOptions(),
1578 test_util::CreateCopyResultCallback(&error
, &entry
));
1579 base::RunLoop().RunUntilIdle();
1581 EXPECT_EQ(HTTP_CREATED
, error
);
1583 EXPECT_TRUE(entry
->IsDirectory());
1584 EXPECT_EQ("resource_id_1", entry
->file_id());
1585 EXPECT_EQ("new directory", entry
->title());
1586 EXPECT_TRUE(HasParent(entry
->file_id(), kParentResourceId
));
1587 // Should be incremented as a new directory was created.
1588 EXPECT_EQ(old_largest_change_id
+ 1,
1589 fake_service_
.about_resource().largest_change_id());
1590 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1593 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToNonexistingDirectory
) {
1594 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1596 const std::string kParentResourceId
= "nonexisting_resource_id";
1598 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1599 scoped_ptr
<FileResource
> entry
;
1600 fake_service_
.AddNewDirectory(
1603 DriveServiceInterface::AddNewDirectoryOptions(),
1604 test_util::CreateCopyResultCallback(&error
, &entry
));
1605 base::RunLoop().RunUntilIdle();
1607 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1608 EXPECT_FALSE(entry
);
1611 TEST_F(FakeDriveServiceTest
, AddNewDirectory_Offline
) {
1612 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1613 fake_service_
.set_offline(true);
1615 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1616 scoped_ptr
<FileResource
> entry
;
1617 fake_service_
.AddNewDirectory(
1618 fake_service_
.GetRootResourceId(),
1620 DriveServiceInterface::AddNewDirectoryOptions(),
1621 test_util::CreateCopyResultCallback(&error
, &entry
));
1622 base::RunLoop().RunUntilIdle();
1624 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1625 EXPECT_FALSE(entry
);
1628 TEST_F(FakeDriveServiceTest
, InitiateUploadNewFile_Offline
) {
1629 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1630 fake_service_
.set_offline(true);
1632 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1633 GURL upload_location
;
1634 fake_service_
.InitiateUploadNewFile(
1637 "1_folder_resource_id",
1639 FakeDriveService::InitiateUploadNewFileOptions(),
1640 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1641 base::RunLoop().RunUntilIdle();
1643 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1644 EXPECT_TRUE(upload_location
.is_empty());
1647 TEST_F(FakeDriveServiceTest
, InitiateUploadNewFile_NotFound
) {
1648 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1650 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1651 GURL upload_location
;
1652 fake_service_
.InitiateUploadNewFile(
1657 FakeDriveService::InitiateUploadNewFileOptions(),
1658 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1659 base::RunLoop().RunUntilIdle();
1661 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1662 EXPECT_TRUE(upload_location
.is_empty());
1665 TEST_F(FakeDriveServiceTest
, InitiateUploadNewFile
) {
1666 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1668 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1669 GURL upload_location
;
1670 fake_service_
.InitiateUploadNewFile(
1673 "1_folder_resource_id",
1675 FakeDriveService::InitiateUploadNewFileOptions(),
1676 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1677 base::RunLoop().RunUntilIdle();
1679 EXPECT_EQ(HTTP_SUCCESS
, error
);
1680 EXPECT_FALSE(upload_location
.is_empty());
1681 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link?mode=newfile"),
1685 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_Offline
) {
1686 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1687 fake_service_
.set_offline(true);
1689 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1690 GURL upload_location
;
1691 fake_service_
.InitiateUploadExistingFile(
1694 "2_file_resource_id",
1695 FakeDriveService::InitiateUploadExistingFileOptions(),
1696 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1697 base::RunLoop().RunUntilIdle();
1699 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
1700 EXPECT_TRUE(upload_location
.is_empty());
1703 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_Forbidden
) {
1704 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1706 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
1707 "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER
));
1709 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1710 GURL upload_location
;
1711 fake_service_
.InitiateUploadExistingFile(
1714 "2_file_resource_id",
1715 FakeDriveService::InitiateUploadExistingFileOptions(),
1716 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1717 base::RunLoop().RunUntilIdle();
1719 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
1720 EXPECT_TRUE(upload_location
.is_empty());
1723 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_NotFound
) {
1724 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1726 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1727 GURL upload_location
;
1728 fake_service_
.InitiateUploadExistingFile(
1732 FakeDriveService::InitiateUploadExistingFileOptions(),
1733 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1734 base::RunLoop().RunUntilIdle();
1736 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1737 EXPECT_TRUE(upload_location
.is_empty());
1740 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_WrongETag
) {
1741 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1743 FakeDriveService::InitiateUploadExistingFileOptions options
;
1744 options
.etag
= "invalid_etag";
1746 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1747 GURL upload_location
;
1748 fake_service_
.InitiateUploadExistingFile(
1751 "2_file_resource_id",
1753 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1754 base::RunLoop().RunUntilIdle();
1756 EXPECT_EQ(HTTP_PRECONDITION
, error
);
1757 EXPECT_TRUE(upload_location
.is_empty());
1760 TEST_F(FakeDriveServiceTest
, InitiateUpload_ExistingFile
) {
1761 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1763 scoped_ptr
<FileResource
> entry
= FindEntry("2_file_resource_id");
1766 FakeDriveService::InitiateUploadExistingFileOptions options
;
1767 options
.etag
= entry
->etag();
1769 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1770 GURL upload_location
;
1771 fake_service_
.InitiateUploadExistingFile(
1774 "2_file_resource_id",
1776 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1777 base::RunLoop().RunUntilIdle();
1779 EXPECT_EQ(HTTP_SUCCESS
, error
);
1780 EXPECT_TRUE(upload_location
.is_valid());
1783 TEST_F(FakeDriveServiceTest
, ResumeUpload_Offline
) {
1784 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1786 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1787 GURL upload_location
;
1788 fake_service_
.InitiateUploadNewFile(
1791 "1_folder_resource_id",
1793 FakeDriveService::InitiateUploadNewFileOptions(),
1794 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1795 base::RunLoop().RunUntilIdle();
1797 EXPECT_EQ(HTTP_SUCCESS
, error
);
1798 EXPECT_FALSE(upload_location
.is_empty());
1799 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1802 fake_service_
.set_offline(true);
1804 UploadRangeResponse response
;
1805 scoped_ptr
<FileResource
> entry
;
1806 fake_service_
.ResumeUpload(
1808 0, 13, 15, "test/foo",
1810 test_util::CreateCopyResultCallback(&response
, &entry
),
1811 ProgressCallback());
1812 base::RunLoop().RunUntilIdle();
1814 EXPECT_EQ(GDATA_NO_CONNECTION
, response
.code
);
1815 EXPECT_FALSE(entry
.get());
1818 TEST_F(FakeDriveServiceTest
, ResumeUpload_NotFound
) {
1819 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1821 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1822 GURL upload_location
;
1823 fake_service_
.InitiateUploadNewFile(
1826 "1_folder_resource_id",
1828 FakeDriveService::InitiateUploadNewFileOptions(),
1829 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1830 base::RunLoop().RunUntilIdle();
1832 ASSERT_EQ(HTTP_SUCCESS
, error
);
1834 UploadRangeResponse response
;
1835 scoped_ptr
<FileResource
> entry
;
1836 fake_service_
.ResumeUpload(
1837 GURL("https://foo.com/"),
1838 0, 13, 15, "test/foo",
1840 test_util::CreateCopyResultCallback(&response
, &entry
),
1841 ProgressCallback());
1842 base::RunLoop().RunUntilIdle();
1844 EXPECT_EQ(HTTP_NOT_FOUND
, response
.code
);
1845 EXPECT_FALSE(entry
.get());
1848 TEST_F(FakeDriveServiceTest
, ResumeUpload_ExistingFile
) {
1849 base::ScopedTempDir temp_dir
;
1850 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1851 base::FilePath local_file_path
=
1852 temp_dir
.path().Append(FILE_PATH_LITERAL("File 1.txt"));
1853 std::string
contents("hogefugapiyo");
1854 ASSERT_TRUE(test_util::WriteStringToFile(local_file_path
, contents
));
1856 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1858 scoped_ptr
<FileResource
> entry
= FindEntry("2_file_resource_id");
1861 FakeDriveService::InitiateUploadExistingFileOptions options
;
1862 options
.etag
= entry
->etag();
1864 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1865 GURL upload_location
;
1866 fake_service_
.InitiateUploadExistingFile(
1869 "2_file_resource_id",
1871 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1872 base::RunLoop().RunUntilIdle();
1874 ASSERT_EQ(HTTP_SUCCESS
, error
);
1876 UploadRangeResponse response
;
1878 std::vector
<test_util::ProgressInfo
> upload_progress_values
;
1879 fake_service_
.ResumeUpload(
1881 0, contents
.size() / 2, contents
.size(), "text/plain",
1883 test_util::CreateCopyResultCallback(&response
, &entry
),
1884 base::Bind(&test_util::AppendProgressCallbackResult
,
1885 &upload_progress_values
));
1886 base::RunLoop().RunUntilIdle();
1888 EXPECT_EQ(HTTP_RESUME_INCOMPLETE
, response
.code
);
1889 EXPECT_FALSE(entry
.get());
1890 ASSERT_TRUE(!upload_progress_values
.empty());
1891 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1892 EXPECT_LE(0, upload_progress_values
.front().first
);
1893 EXPECT_GE(static_cast<int64
>(contents
.size() / 2),
1894 upload_progress_values
.back().first
);
1896 upload_progress_values
.clear();
1897 fake_service_
.ResumeUpload(
1899 contents
.size() / 2, contents
.size(), contents
.size(), "text/plain",
1901 test_util::CreateCopyResultCallback(&response
, &entry
),
1902 base::Bind(&test_util::AppendProgressCallbackResult
,
1903 &upload_progress_values
));
1904 base::RunLoop().RunUntilIdle();
1906 EXPECT_EQ(HTTP_SUCCESS
, response
.code
);
1907 EXPECT_TRUE(entry
.get());
1908 EXPECT_EQ(static_cast<int64
>(contents
.size()), entry
->file_size());
1909 EXPECT_TRUE(Exists(entry
->file_id()));
1910 ASSERT_TRUE(!upload_progress_values
.empty());
1911 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1912 EXPECT_LE(0, upload_progress_values
.front().first
);
1913 EXPECT_GE(static_cast<int64
>(contents
.size() - contents
.size() / 2),
1914 upload_progress_values
.back().first
);
1915 EXPECT_EQ(base::MD5String(contents
), entry
->md5_checksum());
1918 TEST_F(FakeDriveServiceTest
, ResumeUpload_NewFile
) {
1919 base::ScopedTempDir temp_dir
;
1920 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1921 base::FilePath local_file_path
=
1922 temp_dir
.path().Append(FILE_PATH_LITERAL("new file.foo"));
1923 std::string
contents("hogefugapiyo");
1924 ASSERT_TRUE(test_util::WriteStringToFile(local_file_path
, contents
));
1926 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1928 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1929 GURL upload_location
;
1930 fake_service_
.InitiateUploadNewFile(
1933 "1_folder_resource_id",
1935 FakeDriveService::InitiateUploadNewFileOptions(),
1936 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1937 base::RunLoop().RunUntilIdle();
1939 EXPECT_EQ(HTTP_SUCCESS
, error
);
1940 EXPECT_FALSE(upload_location
.is_empty());
1941 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1944 UploadRangeResponse response
;
1945 scoped_ptr
<FileResource
> entry
;
1946 std::vector
<test_util::ProgressInfo
> upload_progress_values
;
1947 fake_service_
.ResumeUpload(
1949 0, contents
.size() / 2, contents
.size(), "test/foo",
1951 test_util::CreateCopyResultCallback(&response
, &entry
),
1952 base::Bind(&test_util::AppendProgressCallbackResult
,
1953 &upload_progress_values
));
1954 base::RunLoop().RunUntilIdle();
1956 EXPECT_EQ(HTTP_RESUME_INCOMPLETE
, response
.code
);
1957 EXPECT_FALSE(entry
.get());
1958 ASSERT_TRUE(!upload_progress_values
.empty());
1959 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1960 EXPECT_LE(0, upload_progress_values
.front().first
);
1961 EXPECT_GE(static_cast<int64
>(contents
.size() / 2),
1962 upload_progress_values
.back().first
);
1964 upload_progress_values
.clear();
1965 fake_service_
.ResumeUpload(
1967 contents
.size() / 2, contents
.size(), contents
.size(), "test/foo",
1969 test_util::CreateCopyResultCallback(&response
, &entry
),
1970 base::Bind(&test_util::AppendProgressCallbackResult
,
1971 &upload_progress_values
));
1972 base::RunLoop().RunUntilIdle();
1974 EXPECT_EQ(HTTP_CREATED
, response
.code
);
1975 EXPECT_TRUE(entry
.get());
1976 EXPECT_EQ(static_cast<int64
>(contents
.size()), entry
->file_size());
1977 EXPECT_TRUE(Exists(entry
->file_id()));
1978 ASSERT_TRUE(!upload_progress_values
.empty());
1979 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1980 EXPECT_LE(0, upload_progress_values
.front().first
);
1981 EXPECT_GE(static_cast<int64
>(contents
.size() - contents
.size() / 2),
1982 upload_progress_values
.back().first
);
1983 EXPECT_EQ(base::MD5String(contents
), entry
->md5_checksum());
1986 TEST_F(FakeDriveServiceTest
, AddNewFile_ToRootDirectory
) {
1987 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1989 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1991 const std::string kContentType
= "text/plain";
1992 const std::string kContentData
= "This is some test content.";
1993 const std::string kTitle
= "new file";
1995 GDataErrorCode error
= GDATA_OTHER_ERROR
;
1996 scoped_ptr
<FileResource
> entry
;
1997 fake_service_
.AddNewFile(
2000 fake_service_
.GetRootResourceId(),
2002 false, // shared_with_me
2003 test_util::CreateCopyResultCallback(&error
, &entry
));
2004 base::RunLoop().RunUntilIdle();
2006 EXPECT_EQ(HTTP_CREATED
, error
);
2008 EXPECT_EQ(kContentType
, entry
->mime_type());
2009 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
2010 EXPECT_EQ("resource_id_1", entry
->file_id());
2011 EXPECT_EQ(kTitle
, entry
->title());
2012 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
2013 // Should be incremented as a new directory was created.
2014 EXPECT_EQ(old_largest_change_id
+ 1,
2015 fake_service_
.about_resource().largest_change_id());
2016 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
2017 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2020 TEST_F(FakeDriveServiceTest
, AddNewFile_ToRootDirectoryOnEmptyFileSystem
) {
2021 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
2023 const std::string kContentType
= "text/plain";
2024 const std::string kContentData
= "This is some test content.";
2025 const std::string kTitle
= "new file";
2027 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2028 scoped_ptr
<FileResource
> entry
;
2029 fake_service_
.AddNewFile(
2032 fake_service_
.GetRootResourceId(),
2034 false, // shared_with_me
2035 test_util::CreateCopyResultCallback(&error
, &entry
));
2036 base::RunLoop().RunUntilIdle();
2038 EXPECT_EQ(HTTP_CREATED
, error
);
2040 EXPECT_EQ(kContentType
, entry
->mime_type());
2041 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
2042 EXPECT_EQ("resource_id_1", entry
->file_id());
2043 EXPECT_EQ(kTitle
, entry
->title());
2044 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
2045 // Should be incremented as a new directory was created.
2046 EXPECT_EQ(old_largest_change_id
+ 1,
2047 fake_service_
.about_resource().largest_change_id());
2048 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
2049 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2052 TEST_F(FakeDriveServiceTest
, AddNewFile_ToNonRootDirectory
) {
2053 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2055 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
2057 const std::string kContentType
= "text/plain";
2058 const std::string kContentData
= "This is some test content.";
2059 const std::string kTitle
= "new file";
2060 const std::string kParentResourceId
= "1_folder_resource_id";
2062 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2063 scoped_ptr
<FileResource
> entry
;
2064 fake_service_
.AddNewFile(
2069 false, // shared_with_me
2070 test_util::CreateCopyResultCallback(&error
, &entry
));
2071 base::RunLoop().RunUntilIdle();
2073 EXPECT_EQ(HTTP_CREATED
, error
);
2075 EXPECT_EQ(kContentType
, entry
->mime_type());
2076 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
2077 EXPECT_EQ("resource_id_1", entry
->file_id());
2078 EXPECT_EQ(kTitle
, entry
->title());
2079 EXPECT_TRUE(HasParent(entry
->file_id(), kParentResourceId
));
2080 // Should be incremented as a new directory was created.
2081 EXPECT_EQ(old_largest_change_id
+ 1,
2082 fake_service_
.about_resource().largest_change_id());
2083 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
2084 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2087 TEST_F(FakeDriveServiceTest
, AddNewFile_ToNonexistingDirectory
) {
2088 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2090 const std::string kContentType
= "text/plain";
2091 const std::string kContentData
= "This is some test content.";
2092 const std::string kTitle
= "new file";
2093 const std::string kParentResourceId
= "nonexisting_resource_id";
2095 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2096 scoped_ptr
<FileResource
> entry
;
2097 fake_service_
.AddNewFile(
2102 false, // shared_with_me
2103 test_util::CreateCopyResultCallback(&error
, &entry
));
2104 base::RunLoop().RunUntilIdle();
2106 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
2107 EXPECT_FALSE(entry
);
2110 TEST_F(FakeDriveServiceTest
, AddNewFile_Offline
) {
2111 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2112 fake_service_
.set_offline(true);
2114 const std::string kContentType
= "text/plain";
2115 const std::string kContentData
= "This is some test content.";
2116 const std::string kTitle
= "new file";
2118 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2119 scoped_ptr
<FileResource
> entry
;
2120 fake_service_
.AddNewFile(
2123 fake_service_
.GetRootResourceId(),
2125 false, // shared_with_me
2126 test_util::CreateCopyResultCallback(&error
, &entry
));
2127 base::RunLoop().RunUntilIdle();
2129 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
2130 EXPECT_FALSE(entry
);
2133 TEST_F(FakeDriveServiceTest
, AddNewFile_SharedWithMeLabel
) {
2134 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2136 const std::string kContentType
= "text/plain";
2137 const std::string kContentData
= "This is some test content.";
2138 const std::string kTitle
= "new file";
2140 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
2142 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2143 scoped_ptr
<FileResource
> entry
;
2144 fake_service_
.AddNewFile(
2147 fake_service_
.GetRootResourceId(),
2149 true, // shared_with_me
2150 test_util::CreateCopyResultCallback(&error
, &entry
));
2151 base::RunLoop().RunUntilIdle();
2153 EXPECT_EQ(HTTP_CREATED
, error
);
2155 EXPECT_EQ(kContentType
, entry
->mime_type());
2156 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
2157 EXPECT_EQ("resource_id_1", entry
->file_id());
2158 EXPECT_EQ(kTitle
, entry
->title());
2159 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
2160 EXPECT_FALSE(entry
->shared_with_me_date().is_null());
2161 // Should be incremented as a new directory was created.
2162 EXPECT_EQ(old_largest_change_id
+ 1,
2163 fake_service_
.about_resource().largest_change_id());
2164 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
2165 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2168 TEST_F(FakeDriveServiceTest
, SetLastModifiedTime_ExistingFile
) {
2169 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2171 const std::string kResourceId
= "2_file_resource_id";
2173 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time
));
2175 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2176 scoped_ptr
<FileResource
> entry
;
2177 fake_service_
.SetLastModifiedTime(
2180 test_util::CreateCopyResultCallback(&error
, &entry
));
2181 base::RunLoop().RunUntilIdle();
2183 EXPECT_EQ(HTTP_SUCCESS
, error
);
2185 EXPECT_EQ(time
, entry
->modified_date());
2188 TEST_F(FakeDriveServiceTest
, SetLastModifiedTime_NonexistingFile
) {
2189 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2191 const std::string kResourceId
= "nonexisting_resource_id";
2193 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time
));
2195 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2196 scoped_ptr
<FileResource
> entry
;
2197 fake_service_
.SetLastModifiedTime(
2200 test_util::CreateCopyResultCallback(&error
, &entry
));
2201 base::RunLoop().RunUntilIdle();
2203 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
2204 EXPECT_FALSE(entry
);
2207 TEST_F(FakeDriveServiceTest
, SetLastModifiedTime_Offline
) {
2208 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2209 fake_service_
.set_offline(true);
2211 const std::string kResourceId
= "2_file_resource_id";
2213 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time
));
2215 GDataErrorCode error
= GDATA_OTHER_ERROR
;
2216 scoped_ptr
<FileResource
> entry
;
2217 fake_service_
.SetLastModifiedTime(
2220 test_util::CreateCopyResultCallback(&error
, &entry
));
2221 base::RunLoop().RunUntilIdle();
2223 EXPECT_EQ(GDATA_NO_CONNECTION
, error
);
2224 EXPECT_FALSE(entry
);
2229 } // namespace drive