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 "components/drive/service/fake_drive_service.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/stl_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "components/drive/service/test_util.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::DRIVE_NO_CONNECTION
;
30 using google_apis::DRIVE_OTHER_ERROR
;
31 using google_apis::DriveApiErrorCode
;
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 DriveApiErrorCode error
= DRIVE_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 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
80 scoped_ptr
<FileResource
> entry
;
81 fake_service_
.AddNewDirectory(
82 parent_resource_id
, directory_title
, AddNewDirectoryOptions(),
83 test_util::CreateCopyResultCallback(&error
, &entry
));
84 base::RunLoop().RunUntilIdle();
85 return error
== HTTP_CREATED
;
88 // Returns true if the resource identified by |resource_id| has a parent
89 // identified by |parent_id|.
90 bool HasParent(const std::string
& resource_id
, const std::string
& parent_id
) {
91 scoped_ptr
<FileResource
> entry
= FindEntry(resource_id
);
93 for (size_t i
= 0; i
< entry
->parents().size(); ++i
) {
94 if (entry
->parents()[i
].file_id() == parent_id
)
101 int64
GetLargestChangeByAboutResource() {
102 DriveApiErrorCode error
;
103 scoped_ptr
<AboutResource
> about_resource
;
104 fake_service_
.GetAboutResource(
105 test_util::CreateCopyResultCallback(&error
, &about_resource
));
106 base::RunLoop().RunUntilIdle();
107 return about_resource
->largest_change_id();
110 base::MessageLoop message_loop_
;
111 FakeDriveService fake_service_
;
114 TEST_F(FakeDriveServiceTest
, GetAllFileList
) {
115 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
117 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
118 scoped_ptr
<FileList
> file_list
;
119 fake_service_
.GetAllFileList(
120 test_util::CreateCopyResultCallback(&error
, &file_list
));
121 base::RunLoop().RunUntilIdle();
123 EXPECT_EQ(HTTP_SUCCESS
, error
);
124 ASSERT_TRUE(file_list
);
125 // Do some sanity check.
126 EXPECT_EQ(15U, file_list
->items().size());
127 EXPECT_EQ(1, fake_service_
.file_list_load_count());
130 TEST_F(FakeDriveServiceTest
, GetAllFileList_Offline
) {
131 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
132 fake_service_
.set_offline(true);
134 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
135 scoped_ptr
<FileList
> file_list
;
136 fake_service_
.GetAllFileList(
137 test_util::CreateCopyResultCallback(&error
, &file_list
));
138 base::RunLoop().RunUntilIdle();
140 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
141 EXPECT_FALSE(file_list
);
144 TEST_F(FakeDriveServiceTest
, GetFileListInDirectory_InRootDirectory
) {
145 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
147 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
148 scoped_ptr
<FileList
> file_list
;
149 fake_service_
.GetFileListInDirectory(
150 fake_service_
.GetRootResourceId(),
151 test_util::CreateCopyResultCallback(&error
, &file_list
));
152 base::RunLoop().RunUntilIdle();
154 EXPECT_EQ(HTTP_SUCCESS
, error
);
155 ASSERT_TRUE(file_list
);
156 // Do some sanity check. There are 8 entries in the root directory.
157 EXPECT_EQ(8U, file_list
->items().size());
158 EXPECT_EQ(1, fake_service_
.directory_load_count());
161 TEST_F(FakeDriveServiceTest
, GetFileListInDirectory_InNonRootDirectory
) {
162 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
164 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
165 scoped_ptr
<FileList
> file_list
;
166 fake_service_
.GetFileListInDirectory(
167 "1_folder_resource_id",
168 test_util::CreateCopyResultCallback(&error
, &file_list
));
169 base::RunLoop().RunUntilIdle();
171 EXPECT_EQ(HTTP_SUCCESS
, error
);
172 ASSERT_TRUE(file_list
);
173 // Do some sanity check. There is three entries in 1_folder_resource_id
175 EXPECT_EQ(3U, file_list
->items().size());
176 EXPECT_EQ(1, fake_service_
.directory_load_count());
179 TEST_F(FakeDriveServiceTest
, GetFileListInDirectory_Offline
) {
180 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
181 fake_service_
.set_offline(true);
183 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
184 scoped_ptr
<FileList
> file_list
;
185 fake_service_
.GetFileListInDirectory(
186 fake_service_
.GetRootResourceId(),
187 test_util::CreateCopyResultCallback(&error
, &file_list
));
188 base::RunLoop().RunUntilIdle();
190 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
191 EXPECT_FALSE(file_list
);
194 TEST_F(FakeDriveServiceTest
, Search
) {
195 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
197 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
198 scoped_ptr
<FileList
> file_list
;
199 fake_service_
.Search(
200 "File", // search_query
201 test_util::CreateCopyResultCallback(&error
, &file_list
));
202 base::RunLoop().RunUntilIdle();
204 EXPECT_EQ(HTTP_SUCCESS
, error
);
205 ASSERT_TRUE(file_list
);
206 // Do some sanity check. There are 4 entries that contain "File" in their
208 EXPECT_EQ(4U, file_list
->items().size());
211 TEST_F(FakeDriveServiceTest
, Search_WithAttribute
) {
212 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
214 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
215 scoped_ptr
<FileList
> file_list
;
216 fake_service_
.Search(
217 "title:1.txt", // search_query
218 test_util::CreateCopyResultCallback(&error
, &file_list
));
219 base::RunLoop().RunUntilIdle();
221 EXPECT_EQ(HTTP_SUCCESS
, error
);
222 ASSERT_TRUE(file_list
);
223 // Do some sanity check. There are 4 entries that contain "1.txt" in their
225 EXPECT_EQ(4U, file_list
->items().size());
228 TEST_F(FakeDriveServiceTest
, Search_MultipleQueries
) {
229 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
231 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
232 scoped_ptr
<FileList
> file_list
;
233 fake_service_
.Search(
234 "Directory 1", // search_query
235 test_util::CreateCopyResultCallback(&error
, &file_list
));
236 base::RunLoop().RunUntilIdle();
238 EXPECT_EQ(HTTP_SUCCESS
, error
);
239 ASSERT_TRUE(file_list
);
240 // There are 2 entries that contain both "Directory" and "1" in their titles.
241 EXPECT_EQ(2U, file_list
->items().size());
243 fake_service_
.Search(
244 "\"Directory 1\"", // search_query
245 test_util::CreateCopyResultCallback(&error
, &file_list
));
246 base::RunLoop().RunUntilIdle();
248 EXPECT_EQ(HTTP_SUCCESS
, error
);
249 ASSERT_TRUE(file_list
);
250 // There is 1 entry that contain "Directory 1" in its title.
251 EXPECT_EQ(1U, file_list
->items().size());
254 TEST_F(FakeDriveServiceTest
, Search_Offline
) {
255 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
256 fake_service_
.set_offline(true);
258 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
259 scoped_ptr
<FileList
> file_list
;
260 fake_service_
.Search(
261 "Directory 1", // search_query
262 test_util::CreateCopyResultCallback(&error
, &file_list
));
263 base::RunLoop().RunUntilIdle();
265 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
266 EXPECT_FALSE(file_list
);
269 TEST_F(FakeDriveServiceTest
, Search_Deleted
) {
270 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
272 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
273 fake_service_
.DeleteResource("2_file_resource_id",
274 std::string(), // etag
275 test_util::CreateCopyResultCallback(&error
));
276 base::RunLoop().RunUntilIdle();
277 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
279 error
= DRIVE_OTHER_ERROR
;
280 scoped_ptr
<FileList
> file_list
;
281 fake_service_
.Search(
282 "File", // search_query
283 test_util::CreateCopyResultCallback(&error
, &file_list
));
284 base::RunLoop().RunUntilIdle();
286 EXPECT_EQ(HTTP_SUCCESS
, error
);
287 ASSERT_TRUE(file_list
);
288 // Do some sanity check. There are 4 entries that contain "File" in their
289 // titles and one of them is deleted.
290 EXPECT_EQ(3U, file_list
->items().size());
293 TEST_F(FakeDriveServiceTest
, Search_Trashed
) {
294 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
296 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
297 fake_service_
.TrashResource("2_file_resource_id",
298 test_util::CreateCopyResultCallback(&error
));
299 base::RunLoop().RunUntilIdle();
300 EXPECT_EQ(HTTP_SUCCESS
, error
);
302 error
= DRIVE_OTHER_ERROR
;
303 scoped_ptr
<FileList
> file_list
;
304 fake_service_
.Search(
305 "File", // search_query
306 test_util::CreateCopyResultCallback(&error
, &file_list
));
307 base::RunLoop().RunUntilIdle();
309 EXPECT_EQ(HTTP_SUCCESS
, error
);
310 ASSERT_TRUE(file_list
);
311 // Do some sanity check. There are 4 entries that contain "File" in their
312 // titles and one of them is deleted.
313 EXPECT_EQ(3U, file_list
->items().size());
316 TEST_F(FakeDriveServiceTest
, SearchByTitle
) {
317 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
319 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
320 scoped_ptr
<FileList
> file_list
;
321 fake_service_
.SearchByTitle(
323 fake_service_
.GetRootResourceId(), // directory_resource_id
324 test_util::CreateCopyResultCallback(&error
, &file_list
));
325 base::RunLoop().RunUntilIdle();
327 EXPECT_EQ(HTTP_SUCCESS
, error
);
328 ASSERT_TRUE(file_list
);
329 // Do some sanity check. There are 2 entries that contain "1.txt" in their
330 // titles directly under the root directory.
331 EXPECT_EQ(2U, file_list
->items().size());
334 TEST_F(FakeDriveServiceTest
, SearchByTitle_EmptyDirectoryResourceId
) {
335 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
337 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
338 scoped_ptr
<FileList
> file_list
;
339 fake_service_
.SearchByTitle(
341 "", // directory resource id
342 test_util::CreateCopyResultCallback(&error
, &file_list
));
343 base::RunLoop().RunUntilIdle();
345 EXPECT_EQ(HTTP_SUCCESS
, error
);
346 ASSERT_TRUE(file_list
);
347 // Do some sanity check. There are 4 entries that contain "1.txt" in their
349 EXPECT_EQ(4U, file_list
->items().size());
352 TEST_F(FakeDriveServiceTest
, SearchByTitle_Offline
) {
353 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
354 fake_service_
.set_offline(true);
356 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
357 scoped_ptr
<FileList
> file_list
;
358 fake_service_
.SearchByTitle(
359 "Directory 1", // title
360 fake_service_
.GetRootResourceId(), // directory_resource_id
361 test_util::CreateCopyResultCallback(&error
, &file_list
));
362 base::RunLoop().RunUntilIdle();
364 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
365 EXPECT_FALSE(file_list
);
368 TEST_F(FakeDriveServiceTest
, GetChangeList_NoNewEntries
) {
369 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
371 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
372 scoped_ptr
<ChangeList
> change_list
;
373 fake_service_
.GetChangeList(
374 fake_service_
.about_resource().largest_change_id() + 1,
375 test_util::CreateCopyResultCallback(&error
, &change_list
));
376 base::RunLoop().RunUntilIdle();
378 EXPECT_EQ(HTTP_SUCCESS
, error
);
379 ASSERT_TRUE(change_list
);
380 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
381 change_list
->largest_change_id());
382 // This should be empty as the latest changestamp was passed to
383 // GetChangeList(), hence there should be no new entries.
384 EXPECT_EQ(0U, change_list
->items().size());
385 // It's considered loaded even if the result is empty.
386 EXPECT_EQ(1, fake_service_
.change_list_load_count());
389 TEST_F(FakeDriveServiceTest
, GetChangeList_WithNewEntry
) {
390 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
391 const int64 old_largest_change_id
=
392 fake_service_
.about_resource().largest_change_id();
394 // Add a new directory in the root directory.
395 ASSERT_TRUE(AddNewDirectory(
396 fake_service_
.GetRootResourceId(), "new directory"));
398 // Get the resource list newer than old_largest_change_id.
399 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
400 scoped_ptr
<ChangeList
> change_list
;
401 fake_service_
.GetChangeList(
402 old_largest_change_id
+ 1,
403 test_util::CreateCopyResultCallback(&error
, &change_list
));
404 base::RunLoop().RunUntilIdle();
406 EXPECT_EQ(HTTP_SUCCESS
, error
);
407 ASSERT_TRUE(change_list
);
408 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
409 change_list
->largest_change_id());
410 // The result should only contain the newly created directory.
411 ASSERT_EQ(1U, change_list
->items().size());
412 ASSERT_TRUE(change_list
->items()[0]->file());
413 EXPECT_EQ("new directory", change_list
->items()[0]->file()->title());
414 EXPECT_EQ(1, fake_service_
.change_list_load_count());
417 TEST_F(FakeDriveServiceTest
, GetChangeList_Offline
) {
418 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
419 fake_service_
.set_offline(true);
421 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
422 scoped_ptr
<ChangeList
> change_list
;
423 fake_service_
.GetChangeList(
424 654321, // start_changestamp
425 test_util::CreateCopyResultCallback(&error
, &change_list
));
426 base::RunLoop().RunUntilIdle();
428 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
429 EXPECT_FALSE(change_list
);
432 TEST_F(FakeDriveServiceTest
, GetChangeList_DeletedEntry
) {
433 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
434 ASSERT_TRUE(Exists("2_file_resource_id"));
435 const int64 old_largest_change_id
=
436 fake_service_
.about_resource().largest_change_id();
438 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
439 fake_service_
.DeleteResource("2_file_resource_id",
440 std::string(), // etag
441 test_util::CreateCopyResultCallback(&error
));
442 base::RunLoop().RunUntilIdle();
443 ASSERT_EQ(HTTP_NO_CONTENT
, error
);
444 ASSERT_FALSE(Exists("2_file_resource_id"));
446 // Get the resource list newer than old_largest_change_id.
447 error
= DRIVE_OTHER_ERROR
;
448 scoped_ptr
<ChangeList
> change_list
;
449 fake_service_
.GetChangeList(
450 old_largest_change_id
+ 1,
451 test_util::CreateCopyResultCallback(&error
, &change_list
));
452 base::RunLoop().RunUntilIdle();
454 EXPECT_EQ(HTTP_SUCCESS
, error
);
455 ASSERT_TRUE(change_list
);
456 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
457 change_list
->largest_change_id());
458 // The result should only contain the deleted file.
459 ASSERT_EQ(1U, change_list
->items().size());
460 const ChangeResource
& item
= *change_list
->items()[0];
461 EXPECT_EQ("2_file_resource_id", item
.file_id());
462 EXPECT_FALSE(item
.file());
463 EXPECT_TRUE(item
.is_deleted());
464 EXPECT_EQ(1, fake_service_
.change_list_load_count());
467 TEST_F(FakeDriveServiceTest
, GetChangeList_TrashedEntry
) {
468 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
469 ASSERT_TRUE(Exists("2_file_resource_id"));
470 const int64 old_largest_change_id
=
471 fake_service_
.about_resource().largest_change_id();
473 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
474 fake_service_
.TrashResource("2_file_resource_id",
475 test_util::CreateCopyResultCallback(&error
));
476 base::RunLoop().RunUntilIdle();
477 ASSERT_EQ(HTTP_SUCCESS
, error
);
478 ASSERT_FALSE(Exists("2_file_resource_id"));
480 // Get the resource list newer than old_largest_change_id.
481 error
= DRIVE_OTHER_ERROR
;
482 scoped_ptr
<ChangeList
> change_list
;
483 fake_service_
.GetChangeList(
484 old_largest_change_id
+ 1,
485 test_util::CreateCopyResultCallback(&error
, &change_list
));
486 base::RunLoop().RunUntilIdle();
488 EXPECT_EQ(HTTP_SUCCESS
, error
);
489 ASSERT_TRUE(change_list
);
490 EXPECT_EQ(fake_service_
.about_resource().largest_change_id(),
491 change_list
->largest_change_id());
492 // The result should only contain the trashed file.
493 ASSERT_EQ(1U, change_list
->items().size());
494 const ChangeResource
& item
= *change_list
->items()[0];
495 EXPECT_EQ("2_file_resource_id", item
.file_id());
496 ASSERT_TRUE(item
.file());
497 EXPECT_TRUE(item
.file()->labels().is_trashed());
498 EXPECT_EQ(1, fake_service_
.change_list_load_count());
501 TEST_F(FakeDriveServiceTest
, GetRemainingFileList_GetAllFileList
) {
502 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
503 fake_service_
.set_default_max_results(6);
505 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
506 scoped_ptr
<FileList
> file_list
;
507 fake_service_
.GetAllFileList(
508 test_util::CreateCopyResultCallback(&error
, &file_list
));
509 base::RunLoop().RunUntilIdle();
510 EXPECT_EQ(HTTP_SUCCESS
, error
);
511 ASSERT_TRUE(file_list
);
513 // Do some sanity check.
514 // The number of results is 14 entries. Thus, it should split into three
515 // chunks: 6, 6, and then 2.
516 EXPECT_EQ(6U, file_list
->items().size());
517 EXPECT_EQ(1, fake_service_
.file_list_load_count());
519 // Second page loading.
520 // Keep the next url before releasing the |file_list|.
521 GURL
next_url(file_list
->next_link());
523 error
= DRIVE_OTHER_ERROR
;
525 fake_service_
.GetRemainingFileList(
527 test_util::CreateCopyResultCallback(&error
, &file_list
));
528 base::RunLoop().RunUntilIdle();
530 EXPECT_EQ(HTTP_SUCCESS
, error
);
531 ASSERT_TRUE(file_list
);
533 EXPECT_EQ(6U, file_list
->items().size());
534 EXPECT_EQ(1, fake_service_
.file_list_load_count());
536 // Third page loading.
537 next_url
= file_list
->next_link();
539 error
= DRIVE_OTHER_ERROR
;
541 fake_service_
.GetRemainingFileList(
543 test_util::CreateCopyResultCallback(&error
, &file_list
));
544 base::RunLoop().RunUntilIdle();
546 EXPECT_EQ(HTTP_SUCCESS
, error
);
547 ASSERT_TRUE(file_list
);
549 EXPECT_EQ(3U, file_list
->items().size());
550 EXPECT_EQ(1, fake_service_
.file_list_load_count());
553 TEST_F(FakeDriveServiceTest
, GetRemainingFileList_GetFileListInDirectory
) {
554 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
555 fake_service_
.set_default_max_results(3);
557 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
558 scoped_ptr
<FileList
> file_list
;
559 fake_service_
.GetFileListInDirectory(
560 fake_service_
.GetRootResourceId(),
561 test_util::CreateCopyResultCallback(&error
, &file_list
));
562 base::RunLoop().RunUntilIdle();
563 EXPECT_EQ(HTTP_SUCCESS
, error
);
564 ASSERT_TRUE(file_list
);
566 // Do some sanity check.
567 // The number of results is 8 entries. Thus, it should split into three
568 // chunks: 3, 3, and then 2.
569 EXPECT_EQ(3U, file_list
->items().size());
570 EXPECT_EQ(1, fake_service_
.directory_load_count());
572 // Second page loading.
573 // Keep the next url before releasing the |file_list|.
574 GURL next_url
= file_list
->next_link();
576 error
= DRIVE_OTHER_ERROR
;
578 fake_service_
.GetRemainingFileList(
580 test_util::CreateCopyResultCallback(&error
, &file_list
));
581 base::RunLoop().RunUntilIdle();
583 EXPECT_EQ(HTTP_SUCCESS
, error
);
584 ASSERT_TRUE(file_list
);
586 EXPECT_EQ(3U, file_list
->items().size());
587 EXPECT_EQ(1, fake_service_
.directory_load_count());
589 // Third page loading.
590 next_url
= file_list
->next_link();
592 error
= DRIVE_OTHER_ERROR
;
594 fake_service_
.GetRemainingFileList(
596 test_util::CreateCopyResultCallback(&error
, &file_list
));
597 base::RunLoop().RunUntilIdle();
599 EXPECT_EQ(HTTP_SUCCESS
, error
);
600 ASSERT_TRUE(file_list
);
602 EXPECT_EQ(2U, file_list
->items().size());
603 EXPECT_EQ(1, fake_service_
.directory_load_count());
606 TEST_F(FakeDriveServiceTest
, GetRemainingFileList_Search
) {
607 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
608 fake_service_
.set_default_max_results(2);
610 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
611 scoped_ptr
<FileList
> file_list
;
612 fake_service_
.Search(
613 "File", // search_query
614 test_util::CreateCopyResultCallback(&error
, &file_list
));
615 base::RunLoop().RunUntilIdle();
616 EXPECT_EQ(HTTP_SUCCESS
, error
);
617 ASSERT_TRUE(file_list
);
619 // Do some sanity check.
620 // The number of results is 4 entries. Thus, it should split into two
621 // chunks: 2, and then 2
622 EXPECT_EQ(2U, file_list
->items().size());
624 // Second page loading.
625 // Keep the next url before releasing the |file_list|.
626 GURL next_url
= file_list
->next_link();
628 error
= DRIVE_OTHER_ERROR
;
630 fake_service_
.GetRemainingFileList(
632 test_util::CreateCopyResultCallback(&error
, &file_list
));
633 base::RunLoop().RunUntilIdle();
635 EXPECT_EQ(HTTP_SUCCESS
, error
);
636 ASSERT_TRUE(file_list
);
638 EXPECT_EQ(2U, file_list
->items().size());
641 TEST_F(FakeDriveServiceTest
, GetRemainingChangeList_GetChangeList
) {
642 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
643 fake_service_
.set_default_max_results(2);
644 const int64 old_largest_change_id
=
645 fake_service_
.about_resource().largest_change_id();
647 // Add 5 new directory in the root directory.
648 for (int i
= 0; i
< 5; ++i
) {
649 ASSERT_TRUE(AddNewDirectory(
650 fake_service_
.GetRootResourceId(),
651 base::StringPrintf("new directory %d", i
)));
654 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
655 scoped_ptr
<ChangeList
> change_list
;
656 fake_service_
.GetChangeList(
657 old_largest_change_id
+ 1, // start_changestamp
658 test_util::CreateCopyResultCallback(&error
, &change_list
));
659 base::RunLoop().RunUntilIdle();
660 EXPECT_EQ(HTTP_SUCCESS
, error
);
661 ASSERT_TRUE(change_list
);
663 // Do some sanity check.
664 // The number of results is 5 entries. Thus, it should split into three
665 // chunks: 2, 2 and then 1.
666 EXPECT_EQ(2U, change_list
->items().size());
667 EXPECT_EQ(1, fake_service_
.change_list_load_count());
669 // Second page loading.
670 // Keep the next url before releasing the |change_list|.
671 GURL next_url
= change_list
->next_link();
673 error
= DRIVE_OTHER_ERROR
;
675 fake_service_
.GetRemainingChangeList(
677 test_util::CreateCopyResultCallback(&error
, &change_list
));
678 base::RunLoop().RunUntilIdle();
680 EXPECT_EQ(HTTP_SUCCESS
, error
);
681 ASSERT_TRUE(change_list
);
683 EXPECT_EQ(2U, change_list
->items().size());
684 EXPECT_EQ(1, fake_service_
.change_list_load_count());
686 // Third page loading.
687 next_url
= change_list
->next_link();
689 error
= DRIVE_OTHER_ERROR
;
691 fake_service_
.GetRemainingChangeList(
693 test_util::CreateCopyResultCallback(&error
, &change_list
));
694 base::RunLoop().RunUntilIdle();
696 EXPECT_EQ(HTTP_SUCCESS
, error
);
697 ASSERT_TRUE(change_list
);
699 EXPECT_EQ(1U, change_list
->items().size());
700 EXPECT_EQ(1, fake_service_
.change_list_load_count());
703 TEST_F(FakeDriveServiceTest
, GetAboutResource
) {
704 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
705 scoped_ptr
<AboutResource
> about_resource
;
706 fake_service_
.GetAboutResource(
707 test_util::CreateCopyResultCallback(&error
, &about_resource
));
708 base::RunLoop().RunUntilIdle();
710 EXPECT_EQ(HTTP_SUCCESS
, error
);
712 ASSERT_TRUE(about_resource
);
713 // Do some sanity check.
714 EXPECT_EQ(fake_service_
.GetRootResourceId(),
715 about_resource
->root_folder_id());
716 EXPECT_EQ(1, fake_service_
.about_resource_load_count());
719 TEST_F(FakeDriveServiceTest
, GetAboutResource_Offline
) {
720 fake_service_
.set_offline(true);
722 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
723 scoped_ptr
<AboutResource
> about_resource
;
724 fake_service_
.GetAboutResource(
725 test_util::CreateCopyResultCallback(&error
, &about_resource
));
726 base::RunLoop().RunUntilIdle();
728 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
729 EXPECT_FALSE(about_resource
);
732 TEST_F(FakeDriveServiceTest
, GetAppList
) {
733 ASSERT_TRUE(fake_service_
.LoadAppListForDriveApi(
734 "drive/applist.json"));
736 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
737 scoped_ptr
<AppList
> app_list
;
738 fake_service_
.GetAppList(
739 test_util::CreateCopyResultCallback(&error
, &app_list
));
740 base::RunLoop().RunUntilIdle();
742 EXPECT_EQ(HTTP_SUCCESS
, error
);
744 ASSERT_TRUE(app_list
);
745 EXPECT_EQ(1, fake_service_
.app_list_load_count());
748 TEST_F(FakeDriveServiceTest
, GetAppList_Offline
) {
749 ASSERT_TRUE(fake_service_
.LoadAppListForDriveApi(
750 "drive/applist.json"));
751 fake_service_
.set_offline(true);
753 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
754 scoped_ptr
<AppList
> app_list
;
755 fake_service_
.GetAppList(
756 test_util::CreateCopyResultCallback(&error
, &app_list
));
757 base::RunLoop().RunUntilIdle();
759 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
760 EXPECT_FALSE(app_list
);
763 TEST_F(FakeDriveServiceTest
, GetFileResource_ExistingFile
) {
764 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
766 const std::string kResourceId
= "2_file_resource_id";
767 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
768 scoped_ptr
<FileResource
> entry
;
769 fake_service_
.GetFileResource(
770 kResourceId
, test_util::CreateCopyResultCallback(&error
, &entry
));
771 base::RunLoop().RunUntilIdle();
773 EXPECT_EQ(HTTP_SUCCESS
, error
);
775 // Do some sanity check.
776 EXPECT_EQ(kResourceId
, entry
->file_id());
779 TEST_F(FakeDriveServiceTest
, GetFileResource_NonexistingFile
) {
780 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
782 const std::string kResourceId
= "nonexisting_resource_id";
783 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
784 scoped_ptr
<FileResource
> entry
;
785 fake_service_
.GetFileResource(
786 kResourceId
, test_util::CreateCopyResultCallback(&error
, &entry
));
787 base::RunLoop().RunUntilIdle();
789 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
793 TEST_F(FakeDriveServiceTest
, GetFileResource_Offline
) {
794 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
795 fake_service_
.set_offline(true);
797 const std::string kResourceId
= "2_file_resource_id";
798 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
799 scoped_ptr
<FileResource
> entry
;
800 fake_service_
.GetFileResource(
801 kResourceId
, test_util::CreateCopyResultCallback(&error
, &entry
));
802 base::RunLoop().RunUntilIdle();
804 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
808 TEST_F(FakeDriveServiceTest
, GetShareUrl
) {
809 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
811 const std::string kResourceId
= "2_file_resource_id";
812 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
814 fake_service_
.GetShareUrl(
816 GURL(), // embed origin
817 test_util::CreateCopyResultCallback(&error
, &share_url
));
818 base::RunLoop().RunUntilIdle();
820 EXPECT_EQ(HTTP_SUCCESS
, error
);
821 EXPECT_FALSE(share_url
.is_empty());
824 TEST_F(FakeDriveServiceTest
, DeleteResource_ExistingFile
) {
825 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
827 // Resource "2_file_resource_id" should now exist.
828 ASSERT_TRUE(Exists("2_file_resource_id"));
830 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
831 fake_service_
.DeleteResource("2_file_resource_id",
832 std::string(), // etag
833 test_util::CreateCopyResultCallback(&error
));
834 base::RunLoop().RunUntilIdle();
836 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
837 // Resource "2_file_resource_id" should be gone now.
838 EXPECT_FALSE(Exists("2_file_resource_id"));
840 error
= DRIVE_OTHER_ERROR
;
841 fake_service_
.DeleteResource("2_file_resource_id",
842 std::string(), // etag
843 test_util::CreateCopyResultCallback(&error
));
844 base::RunLoop().RunUntilIdle();
845 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
846 EXPECT_FALSE(Exists("2_file_resource_id"));
849 TEST_F(FakeDriveServiceTest
, DeleteResource_NonexistingFile
) {
850 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
852 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
853 fake_service_
.DeleteResource("nonexisting_resource_id",
854 std::string(), // etag
855 test_util::CreateCopyResultCallback(&error
));
856 base::RunLoop().RunUntilIdle();
858 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
861 TEST_F(FakeDriveServiceTest
, DeleteResource_ETagMatch
) {
862 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
864 // Resource "2_file_resource_id" should now exist.
865 scoped_ptr
<FileResource
> entry
= FindEntry("2_file_resource_id");
867 ASSERT_FALSE(entry
->labels().is_trashed());
868 ASSERT_FALSE(entry
->etag().empty());
870 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
871 fake_service_
.DeleteResource("2_file_resource_id",
872 entry
->etag() + "_mismatch",
873 test_util::CreateCopyResultCallback(&error
));
874 base::RunLoop().RunUntilIdle();
876 EXPECT_EQ(HTTP_PRECONDITION
, error
);
877 // Resource "2_file_resource_id" should still exist.
878 EXPECT_TRUE(Exists("2_file_resource_id"));
880 error
= DRIVE_OTHER_ERROR
;
881 fake_service_
.DeleteResource("2_file_resource_id",
883 test_util::CreateCopyResultCallback(&error
));
884 base::RunLoop().RunUntilIdle();
885 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
886 // Resource "2_file_resource_id" should be gone now.
887 EXPECT_FALSE(Exists("2_file_resource_id"));
890 TEST_F(FakeDriveServiceTest
, DeleteResource_Offline
) {
891 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
892 fake_service_
.set_offline(true);
894 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
895 fake_service_
.DeleteResource("2_file_resource_id",
896 std::string(), // etag
897 test_util::CreateCopyResultCallback(&error
));
898 base::RunLoop().RunUntilIdle();
900 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
903 TEST_F(FakeDriveServiceTest
, DeleteResource_Forbidden
) {
904 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
906 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
907 "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER
));
909 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
910 fake_service_
.DeleteResource("2_file_resource_id",
911 std::string(), // etag
912 test_util::CreateCopyResultCallback(&error
));
913 base::RunLoop().RunUntilIdle();
915 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
918 TEST_F(FakeDriveServiceTest
, TrashResource_ExistingFile
) {
919 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
921 // Resource "2_file_resource_id" should now exist.
922 ASSERT_TRUE(Exists("2_file_resource_id"));
924 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
925 fake_service_
.TrashResource("2_file_resource_id",
926 test_util::CreateCopyResultCallback(&error
));
927 base::RunLoop().RunUntilIdle();
929 EXPECT_EQ(HTTP_SUCCESS
, error
);
930 // Resource "2_file_resource_id" should be gone now.
931 EXPECT_FALSE(Exists("2_file_resource_id"));
933 error
= DRIVE_OTHER_ERROR
;
934 fake_service_
.TrashResource("2_file_resource_id",
935 test_util::CreateCopyResultCallback(&error
));
936 base::RunLoop().RunUntilIdle();
937 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
938 EXPECT_FALSE(Exists("2_file_resource_id"));
941 TEST_F(FakeDriveServiceTest
, TrashResource_NonexistingFile
) {
942 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
944 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
945 fake_service_
.TrashResource("nonexisting_resource_id",
946 test_util::CreateCopyResultCallback(&error
));
947 base::RunLoop().RunUntilIdle();
949 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
952 TEST_F(FakeDriveServiceTest
, TrashResource_Offline
) {
953 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
954 fake_service_
.set_offline(true);
956 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
957 fake_service_
.TrashResource("2_file_resource_id",
958 test_util::CreateCopyResultCallback(&error
));
959 base::RunLoop().RunUntilIdle();
961 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
964 TEST_F(FakeDriveServiceTest
, TrashResource_Forbidden
) {
965 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
967 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
968 "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER
));
970 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
971 fake_service_
.TrashResource("2_file_resource_id",
972 test_util::CreateCopyResultCallback(&error
));
973 base::RunLoop().RunUntilIdle();
975 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
978 TEST_F(FakeDriveServiceTest
, DownloadFile_ExistingFile
) {
979 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
981 base::ScopedTempDir temp_dir
;
982 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
984 std::vector
<test_util::ProgressInfo
> download_progress_values
;
986 const base::FilePath kOutputFilePath
=
987 temp_dir
.path().AppendASCII("whatever.txt");
988 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
989 base::FilePath output_file_path
;
990 test_util::TestGetContentCallback get_content_callback
;
991 fake_service_
.DownloadFile(
993 "2_file_resource_id",
994 test_util::CreateCopyResultCallback(&error
, &output_file_path
),
995 get_content_callback
.callback(),
996 base::Bind(&test_util::AppendProgressCallbackResult
,
997 &download_progress_values
));
998 base::RunLoop().RunUntilIdle();
1000 EXPECT_EQ(HTTP_SUCCESS
, error
);
1001 EXPECT_EQ(output_file_path
, kOutputFilePath
);
1002 std::string content
;
1003 ASSERT_TRUE(base::ReadFileToString(output_file_path
, &content
));
1004 EXPECT_EQ("This is some test content.", content
);
1005 ASSERT_TRUE(!download_progress_values
.empty());
1006 EXPECT_TRUE(base::STLIsSorted(download_progress_values
));
1007 EXPECT_LE(0, download_progress_values
.front().first
);
1008 EXPECT_GE(26, download_progress_values
.back().first
);
1009 EXPECT_EQ(content
, get_content_callback
.GetConcatenatedData());
1012 TEST_F(FakeDriveServiceTest
, DownloadFile_NonexistingFile
) {
1013 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1015 base::ScopedTempDir temp_dir
;
1016 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1018 const base::FilePath kOutputFilePath
=
1019 temp_dir
.path().AppendASCII("whatever.txt");
1020 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1021 base::FilePath output_file_path
;
1022 fake_service_
.DownloadFile(
1024 "non_existent_file_resource_id",
1025 test_util::CreateCopyResultCallback(&error
, &output_file_path
),
1026 GetContentCallback(),
1027 ProgressCallback());
1028 base::RunLoop().RunUntilIdle();
1030 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1033 TEST_F(FakeDriveServiceTest
, DownloadFile_Offline
) {
1034 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1035 fake_service_
.set_offline(true);
1037 base::ScopedTempDir temp_dir
;
1038 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1040 const base::FilePath kOutputFilePath
=
1041 temp_dir
.path().AppendASCII("whatever.txt");
1042 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1043 base::FilePath output_file_path
;
1044 fake_service_
.DownloadFile(
1046 "2_file_resource_id",
1047 test_util::CreateCopyResultCallback(&error
, &output_file_path
),
1048 GetContentCallback(),
1049 ProgressCallback());
1050 base::RunLoop().RunUntilIdle();
1052 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1055 TEST_F(FakeDriveServiceTest
, CopyResource
) {
1056 const base::Time::Exploded kModifiedDate
= {2012, 7, 0, 19, 15, 59, 13, 123};
1058 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1060 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1062 const std::string kResourceId
= "2_file_resource_id";
1063 const std::string kParentResourceId
= "2_folder_resource_id";
1064 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1065 scoped_ptr
<FileResource
> entry
;
1066 fake_service_
.CopyResource(
1070 base::Time::FromUTCExploded(kModifiedDate
),
1071 test_util::CreateCopyResultCallback(&error
, &entry
));
1072 base::RunLoop().RunUntilIdle();
1074 EXPECT_EQ(HTTP_SUCCESS
, error
);
1076 // The copied entry should have the new resource ID and the title.
1077 EXPECT_NE(kResourceId
, entry
->file_id());
1078 EXPECT_EQ("new title", entry
->title());
1079 EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate
), entry
->modified_date());
1080 EXPECT_TRUE(HasParent(entry
->file_id(), kParentResourceId
));
1081 // Should be incremented as a new hosted document was created.
1082 EXPECT_EQ(old_largest_change_id
+ 1,
1083 fake_service_
.about_resource().largest_change_id());
1084 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1087 TEST_F(FakeDriveServiceTest
, CopyResource_NonExisting
) {
1088 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1090 const std::string kResourceId
= "nonexisting_resource_id";
1091 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1092 scoped_ptr
<FileResource
> entry
;
1093 fake_service_
.CopyResource(
1095 "1_folder_resource_id",
1098 test_util::CreateCopyResultCallback(&error
, &entry
));
1099 base::RunLoop().RunUntilIdle();
1101 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1104 TEST_F(FakeDriveServiceTest
, CopyResource_EmptyParentResourceId
) {
1105 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1107 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1109 const std::string kResourceId
= "2_file_resource_id";
1110 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1111 scoped_ptr
<FileResource
> entry
;
1112 fake_service_
.CopyResource(
1117 test_util::CreateCopyResultCallback(&error
, &entry
));
1118 base::RunLoop().RunUntilIdle();
1120 EXPECT_EQ(HTTP_SUCCESS
, error
);
1122 // The copied entry should have the new resource ID and the title.
1123 EXPECT_NE(kResourceId
, entry
->file_id());
1124 EXPECT_EQ("new title", entry
->title());
1125 EXPECT_TRUE(HasParent(kResourceId
, fake_service_
.GetRootResourceId()));
1126 // Should be incremented as a new hosted document was created.
1127 EXPECT_EQ(old_largest_change_id
+ 1,
1128 fake_service_
.about_resource().largest_change_id());
1129 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1132 TEST_F(FakeDriveServiceTest
, CopyResource_Offline
) {
1133 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1134 fake_service_
.set_offline(true);
1136 const std::string kResourceId
= "2_file_resource_id";
1137 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1138 scoped_ptr
<FileResource
> entry
;
1139 fake_service_
.CopyResource(
1141 "1_folder_resource_id",
1144 test_util::CreateCopyResultCallback(&error
, &entry
));
1145 base::RunLoop().RunUntilIdle();
1147 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1148 EXPECT_FALSE(entry
);
1151 TEST_F(FakeDriveServiceTest
, UpdateResource
) {
1152 const base::Time::Exploded kModifiedDate
= {2012, 7, 0, 19, 15, 59, 13, 123};
1153 const base::Time::Exploded kViewedDate
= {2013, 8, 1, 20, 16, 00, 14, 234};
1155 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1157 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1159 const std::string kResourceId
= "2_file_resource_id";
1160 const std::string kParentResourceId
= "2_folder_resource_id";
1161 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1162 scoped_ptr
<FileResource
> entry
;
1163 fake_service_
.UpdateResource(
1164 kResourceId
, kParentResourceId
, "new title",
1165 base::Time::FromUTCExploded(kModifiedDate
),
1166 base::Time::FromUTCExploded(kViewedDate
),
1167 google_apis::drive::Properties(),
1168 test_util::CreateCopyResultCallback(&error
, &entry
));
1169 base::RunLoop().RunUntilIdle();
1171 EXPECT_EQ(HTTP_SUCCESS
, error
);
1173 // The updated entry should have the new title.
1174 EXPECT_EQ(kResourceId
, entry
->file_id());
1175 EXPECT_EQ("new title", entry
->title());
1176 EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate
),
1177 entry
->modified_date());
1178 EXPECT_EQ(base::Time::FromUTCExploded(kViewedDate
),
1179 entry
->last_viewed_by_me_date());
1180 EXPECT_TRUE(HasParent(kResourceId
, kParentResourceId
));
1181 // Should be incremented as a new hosted document was created.
1182 EXPECT_EQ(old_largest_change_id
+ 1,
1183 fake_service_
.about_resource().largest_change_id());
1184 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1187 TEST_F(FakeDriveServiceTest
, UpdateResource_NonExisting
) {
1188 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1190 const std::string kResourceId
= "nonexisting_resource_id";
1191 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1192 scoped_ptr
<FileResource
> entry
;
1193 fake_service_
.UpdateResource(
1194 kResourceId
, "1_folder_resource_id", "new title", base::Time(),
1195 base::Time(), google_apis::drive::Properties(),
1196 test_util::CreateCopyResultCallback(&error
, &entry
));
1197 base::RunLoop().RunUntilIdle();
1199 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1202 TEST_F(FakeDriveServiceTest
, UpdateResource_EmptyParentResourceId
) {
1203 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1205 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1207 const std::string kResourceId
= "2_file_resource_id";
1209 // Just make sure that the resource is under root.
1210 ASSERT_TRUE(HasParent(kResourceId
, "fake_root"));
1212 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1213 scoped_ptr
<FileResource
> entry
;
1214 fake_service_
.UpdateResource(
1215 kResourceId
, std::string(), "new title", base::Time(), base::Time(),
1216 google_apis::drive::Properties(),
1217 test_util::CreateCopyResultCallback(&error
, &entry
));
1218 base::RunLoop().RunUntilIdle();
1220 EXPECT_EQ(HTTP_SUCCESS
, error
);
1222 // The updated entry should have the new title.
1223 EXPECT_EQ(kResourceId
, entry
->file_id());
1224 EXPECT_EQ("new title", entry
->title());
1225 EXPECT_TRUE(HasParent(kResourceId
, "fake_root"));
1226 // Should be incremented as a new hosted document was created.
1227 EXPECT_EQ(old_largest_change_id
+ 1,
1228 fake_service_
.about_resource().largest_change_id());
1229 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1232 TEST_F(FakeDriveServiceTest
, UpdateResource_Offline
) {
1233 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1234 fake_service_
.set_offline(true);
1236 const std::string kResourceId
= "2_file_resource_id";
1237 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1238 scoped_ptr
<FileResource
> entry
;
1239 fake_service_
.UpdateResource(
1240 kResourceId
, std::string(), "new title", base::Time(), base::Time(),
1241 google_apis::drive::Properties(),
1242 test_util::CreateCopyResultCallback(&error
, &entry
));
1243 base::RunLoop().RunUntilIdle();
1245 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1246 EXPECT_FALSE(entry
);
1249 TEST_F(FakeDriveServiceTest
, UpdateResource_Forbidden
) {
1250 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1252 const std::string kResourceId
= "2_file_resource_id";
1253 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
1254 kResourceId
, google_apis::drive::PERMISSION_ROLE_READER
));
1256 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1257 scoped_ptr
<FileResource
> entry
;
1258 fake_service_
.UpdateResource(
1259 kResourceId
, std::string(), "new title", base::Time(), base::Time(),
1260 google_apis::drive::Properties(),
1261 test_util::CreateCopyResultCallback(&error
, &entry
));
1262 base::RunLoop().RunUntilIdle();
1264 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
1265 EXPECT_FALSE(entry
);
1268 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_FileInRootDirectory
) {
1269 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1271 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1273 const std::string kResourceId
= "2_file_resource_id";
1274 const std::string kOldParentResourceId
= fake_service_
.GetRootResourceId();
1275 const std::string kNewParentResourceId
= "1_folder_resource_id";
1277 // Here's the original parent link.
1278 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1279 EXPECT_FALSE(HasParent(kResourceId
, kNewParentResourceId
));
1281 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1282 fake_service_
.AddResourceToDirectory(
1283 kNewParentResourceId
,
1285 test_util::CreateCopyResultCallback(&error
));
1286 base::RunLoop().RunUntilIdle();
1288 EXPECT_EQ(HTTP_SUCCESS
, error
);
1290 // The parent link should now be changed.
1291 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1292 EXPECT_TRUE(HasParent(kResourceId
, kNewParentResourceId
));
1293 // Should be incremented as a file was moved.
1294 EXPECT_EQ(old_largest_change_id
+ 1,
1295 fake_service_
.about_resource().largest_change_id());
1296 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1299 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_FileInNonRootDirectory
) {
1300 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1302 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1304 const std::string kResourceId
= "subdirectory_file_1_id";
1305 const std::string kOldParentResourceId
= "1_folder_resource_id";
1306 const std::string kNewParentResourceId
= "2_folder_resource_id";
1308 // Here's the original parent link.
1309 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1310 EXPECT_FALSE(HasParent(kResourceId
, kNewParentResourceId
));
1312 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1313 fake_service_
.AddResourceToDirectory(
1314 kNewParentResourceId
,
1316 test_util::CreateCopyResultCallback(&error
));
1317 base::RunLoop().RunUntilIdle();
1319 EXPECT_EQ(HTTP_SUCCESS
, error
);
1321 // The parent link should now be changed.
1322 EXPECT_TRUE(HasParent(kResourceId
, kOldParentResourceId
));
1323 EXPECT_TRUE(HasParent(kResourceId
, kNewParentResourceId
));
1324 // Should be incremented as a file was moved.
1325 EXPECT_EQ(old_largest_change_id
+ 1,
1326 fake_service_
.about_resource().largest_change_id());
1327 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1330 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_NonexistingFile
) {
1331 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1333 const std::string kResourceId
= "nonexisting_file";
1334 const std::string kNewParentResourceId
= "1_folder_resource_id";
1336 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1337 fake_service_
.AddResourceToDirectory(
1338 kNewParentResourceId
,
1340 test_util::CreateCopyResultCallback(&error
));
1341 base::RunLoop().RunUntilIdle();
1343 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1346 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_OrphanFile
) {
1347 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1349 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1351 const std::string kResourceId
= "1_orphanfile_resource_id";
1352 const std::string kNewParentResourceId
= "1_folder_resource_id";
1354 // The file does not belong to any directory, even to the root.
1355 EXPECT_FALSE(HasParent(kResourceId
, kNewParentResourceId
));
1356 EXPECT_FALSE(HasParent(kResourceId
, fake_service_
.GetRootResourceId()));
1358 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1359 fake_service_
.AddResourceToDirectory(
1360 kNewParentResourceId
,
1362 test_util::CreateCopyResultCallback(&error
));
1363 base::RunLoop().RunUntilIdle();
1365 EXPECT_EQ(HTTP_SUCCESS
, error
);
1367 // The parent link should now be changed.
1368 EXPECT_TRUE(HasParent(kResourceId
, kNewParentResourceId
));
1369 EXPECT_FALSE(HasParent(kResourceId
, fake_service_
.GetRootResourceId()));
1370 // Should be incremented as a file was moved.
1371 EXPECT_EQ(old_largest_change_id
+ 1,
1372 fake_service_
.about_resource().largest_change_id());
1373 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1376 TEST_F(FakeDriveServiceTest
, AddResourceToDirectory_Offline
) {
1377 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1378 fake_service_
.set_offline(true);
1380 const std::string kResourceId
= "2_file_resource_id";
1381 const std::string kNewParentResourceId
= "1_folder_resource_id";
1383 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1384 fake_service_
.AddResourceToDirectory(
1385 kNewParentResourceId
,
1387 test_util::CreateCopyResultCallback(&error
));
1388 base::RunLoop().RunUntilIdle();
1390 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1393 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_ExistingFile
) {
1394 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1396 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1398 const std::string kResourceId
= "subdirectory_file_1_id";
1399 const std::string kParentResourceId
= "1_folder_resource_id";
1401 scoped_ptr
<FileResource
> entry
= FindEntry(kResourceId
);
1403 // The entry should have a parent now.
1404 ASSERT_FALSE(entry
->parents().empty());
1406 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1407 fake_service_
.RemoveResourceFromDirectory(
1410 test_util::CreateCopyResultCallback(&error
));
1411 base::RunLoop().RunUntilIdle();
1413 EXPECT_EQ(HTTP_NO_CONTENT
, error
);
1415 entry
= FindEntry(kResourceId
);
1417 // The entry should have no parent now.
1418 ASSERT_TRUE(entry
->parents().empty());
1419 // Should be incremented as a file was moved to the root directory.
1420 EXPECT_EQ(old_largest_change_id
+ 1,
1421 fake_service_
.about_resource().largest_change_id());
1422 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1425 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_NonexistingFile
) {
1426 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1428 const std::string kResourceId
= "nonexisting_file";
1429 const std::string kParentResourceId
= "1_folder_resource_id";
1431 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1432 fake_service_
.RemoveResourceFromDirectory(
1435 test_util::CreateCopyResultCallback(&error
));
1436 base::RunLoop().RunUntilIdle();
1438 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1441 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_OrphanFile
) {
1442 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1444 const std::string kResourceId
= "1_orphanfile_resource_id";
1445 const std::string kParentResourceId
= fake_service_
.GetRootResourceId();
1447 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1448 fake_service_
.RemoveResourceFromDirectory(
1451 test_util::CreateCopyResultCallback(&error
));
1452 base::RunLoop().RunUntilIdle();
1454 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1457 TEST_F(FakeDriveServiceTest
, RemoveResourceFromDirectory_Offline
) {
1458 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1459 fake_service_
.set_offline(true);
1461 const std::string kResourceId
= "subdirectory_file_1_id";
1462 const std::string kParentResourceId
= "1_folder_resource_id";
1464 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1465 fake_service_
.RemoveResourceFromDirectory(
1468 test_util::CreateCopyResultCallback(&error
));
1469 base::RunLoop().RunUntilIdle();
1471 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1474 TEST_F(FakeDriveServiceTest
, AddNewDirectory_EmptyParent
) {
1475 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1477 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1479 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1480 scoped_ptr
<FileResource
> entry
;
1481 fake_service_
.AddNewDirectory(
1482 std::string(), "new directory", AddNewDirectoryOptions(),
1483 test_util::CreateCopyResultCallback(&error
, &entry
));
1484 base::RunLoop().RunUntilIdle();
1486 EXPECT_EQ(HTTP_CREATED
, error
);
1488 EXPECT_TRUE(entry
->IsDirectory());
1489 EXPECT_EQ("resource_id_1", entry
->file_id());
1490 EXPECT_EQ("new directory", entry
->title());
1491 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1492 // Should be incremented as a new directory was created.
1493 EXPECT_EQ(old_largest_change_id
+ 1,
1494 fake_service_
.about_resource().largest_change_id());
1495 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1498 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToRootDirectory
) {
1499 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1501 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1503 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1504 scoped_ptr
<FileResource
> entry
;
1505 fake_service_
.AddNewDirectory(
1506 fake_service_
.GetRootResourceId(), "new directory",
1507 AddNewDirectoryOptions(),
1508 test_util::CreateCopyResultCallback(&error
, &entry
));
1509 base::RunLoop().RunUntilIdle();
1511 EXPECT_EQ(HTTP_CREATED
, error
);
1513 EXPECT_TRUE(entry
->IsDirectory());
1514 EXPECT_EQ("resource_id_1", entry
->file_id());
1515 EXPECT_EQ("new directory", entry
->title());
1516 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1517 // Should be incremented as a new directory was created.
1518 EXPECT_EQ(old_largest_change_id
+ 1,
1519 fake_service_
.about_resource().largest_change_id());
1520 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1523 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToRootDirectoryOnEmptyFileSystem
) {
1524 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1526 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1527 scoped_ptr
<FileResource
> entry
;
1528 fake_service_
.AddNewDirectory(
1529 fake_service_
.GetRootResourceId(), "new directory",
1530 AddNewDirectoryOptions(),
1531 test_util::CreateCopyResultCallback(&error
, &entry
));
1532 base::RunLoop().RunUntilIdle();
1534 EXPECT_EQ(HTTP_CREATED
, error
);
1536 EXPECT_TRUE(entry
->IsDirectory());
1537 EXPECT_EQ("resource_id_1", entry
->file_id());
1538 EXPECT_EQ("new directory", entry
->title());
1539 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1540 // Should be incremented as a new directory was created.
1541 EXPECT_EQ(old_largest_change_id
+ 1,
1542 fake_service_
.about_resource().largest_change_id());
1543 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1546 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToNonRootDirectory
) {
1547 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1549 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1551 const std::string kParentResourceId
= "1_folder_resource_id";
1553 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1554 scoped_ptr
<FileResource
> entry
;
1555 fake_service_
.AddNewDirectory(
1556 kParentResourceId
, "new directory", AddNewDirectoryOptions(),
1557 test_util::CreateCopyResultCallback(&error
, &entry
));
1558 base::RunLoop().RunUntilIdle();
1560 EXPECT_EQ(HTTP_CREATED
, error
);
1562 EXPECT_TRUE(entry
->IsDirectory());
1563 EXPECT_EQ("resource_id_1", entry
->file_id());
1564 EXPECT_EQ("new directory", entry
->title());
1565 EXPECT_TRUE(HasParent(entry
->file_id(), kParentResourceId
));
1566 // Should be incremented as a new directory was created.
1567 EXPECT_EQ(old_largest_change_id
+ 1,
1568 fake_service_
.about_resource().largest_change_id());
1569 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1572 TEST_F(FakeDriveServiceTest
, AddNewDirectory_ToNonexistingDirectory
) {
1573 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1575 const std::string kParentResourceId
= "nonexisting_resource_id";
1577 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1578 scoped_ptr
<FileResource
> entry
;
1579 fake_service_
.AddNewDirectory(
1580 kParentResourceId
, "new directory", AddNewDirectoryOptions(),
1581 test_util::CreateCopyResultCallback(&error
, &entry
));
1582 base::RunLoop().RunUntilIdle();
1584 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1585 EXPECT_FALSE(entry
);
1588 TEST_F(FakeDriveServiceTest
, AddNewDirectory_Offline
) {
1589 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1590 fake_service_
.set_offline(true);
1592 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1593 scoped_ptr
<FileResource
> entry
;
1594 fake_service_
.AddNewDirectory(
1595 fake_service_
.GetRootResourceId(), "new directory",
1596 AddNewDirectoryOptions(),
1597 test_util::CreateCopyResultCallback(&error
, &entry
));
1598 base::RunLoop().RunUntilIdle();
1600 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1601 EXPECT_FALSE(entry
);
1604 TEST_F(FakeDriveServiceTest
, InitiateUploadNewFile_Offline
) {
1605 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1606 fake_service_
.set_offline(true);
1608 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1609 GURL upload_location
;
1610 fake_service_
.InitiateUploadNewFile(
1611 "test/foo", 13, "1_folder_resource_id", "new file.foo",
1612 UploadNewFileOptions(),
1613 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1614 base::RunLoop().RunUntilIdle();
1616 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1617 EXPECT_TRUE(upload_location
.is_empty());
1620 TEST_F(FakeDriveServiceTest
, InitiateUploadNewFile_NotFound
) {
1621 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1623 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1624 GURL upload_location
;
1625 fake_service_
.InitiateUploadNewFile(
1626 "test/foo", 13, "non_existent", "new file.foo", UploadNewFileOptions(),
1627 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1628 base::RunLoop().RunUntilIdle();
1630 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1631 EXPECT_TRUE(upload_location
.is_empty());
1634 TEST_F(FakeDriveServiceTest
, InitiateUploadNewFile
) {
1635 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1637 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1638 GURL upload_location
;
1639 fake_service_
.InitiateUploadNewFile(
1640 "test/foo", 13, "1_folder_resource_id", "new file.foo",
1641 UploadNewFileOptions(),
1642 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1643 base::RunLoop().RunUntilIdle();
1645 EXPECT_EQ(HTTP_SUCCESS
, error
);
1646 EXPECT_FALSE(upload_location
.is_empty());
1647 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link?mode=newfile"),
1651 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_Offline
) {
1652 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1653 fake_service_
.set_offline(true);
1655 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1656 GURL upload_location
;
1657 fake_service_
.InitiateUploadExistingFile(
1658 "test/foo", 13, "2_file_resource_id", UploadExistingFileOptions(),
1659 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1660 base::RunLoop().RunUntilIdle();
1662 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
1663 EXPECT_TRUE(upload_location
.is_empty());
1666 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_Forbidden
) {
1667 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1669 EXPECT_EQ(HTTP_SUCCESS
, fake_service_
.SetUserPermission(
1670 "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER
));
1672 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1673 GURL upload_location
;
1674 fake_service_
.InitiateUploadExistingFile(
1675 "test/foo", 13, "2_file_resource_id", UploadExistingFileOptions(),
1676 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1677 base::RunLoop().RunUntilIdle();
1679 EXPECT_EQ(HTTP_FORBIDDEN
, error
);
1680 EXPECT_TRUE(upload_location
.is_empty());
1683 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_NotFound
) {
1684 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1686 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1687 GURL upload_location
;
1688 fake_service_
.InitiateUploadExistingFile(
1689 "test/foo", 13, "non_existent", UploadExistingFileOptions(),
1690 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1691 base::RunLoop().RunUntilIdle();
1693 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
1694 EXPECT_TRUE(upload_location
.is_empty());
1697 TEST_F(FakeDriveServiceTest
, InitiateUploadExistingFile_WrongETag
) {
1698 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1700 UploadExistingFileOptions options
;
1701 options
.etag
= "invalid_etag";
1703 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1704 GURL upload_location
;
1705 fake_service_
.InitiateUploadExistingFile(
1708 "2_file_resource_id",
1710 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1711 base::RunLoop().RunUntilIdle();
1713 EXPECT_EQ(HTTP_PRECONDITION
, error
);
1714 EXPECT_TRUE(upload_location
.is_empty());
1717 TEST_F(FakeDriveServiceTest
, InitiateUpload_ExistingFile
) {
1718 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1720 scoped_ptr
<FileResource
> entry
= FindEntry("2_file_resource_id");
1723 UploadExistingFileOptions options
;
1724 options
.etag
= entry
->etag();
1726 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1727 GURL upload_location
;
1728 fake_service_
.InitiateUploadExistingFile(
1731 "2_file_resource_id",
1733 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1734 base::RunLoop().RunUntilIdle();
1736 EXPECT_EQ(HTTP_SUCCESS
, error
);
1737 EXPECT_TRUE(upload_location
.is_valid());
1740 TEST_F(FakeDriveServiceTest
, ResumeUpload_Offline
) {
1741 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1743 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1744 GURL upload_location
;
1745 fake_service_
.InitiateUploadNewFile(
1746 "test/foo", 15, "1_folder_resource_id", "new file.foo",
1747 UploadNewFileOptions(),
1748 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1749 base::RunLoop().RunUntilIdle();
1751 EXPECT_EQ(HTTP_SUCCESS
, error
);
1752 EXPECT_FALSE(upload_location
.is_empty());
1753 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1756 fake_service_
.set_offline(true);
1758 UploadRangeResponse response
;
1759 scoped_ptr
<FileResource
> entry
;
1760 fake_service_
.ResumeUpload(
1762 0, 13, 15, "test/foo",
1764 test_util::CreateCopyResultCallback(&response
, &entry
),
1765 ProgressCallback());
1766 base::RunLoop().RunUntilIdle();
1768 EXPECT_EQ(DRIVE_NO_CONNECTION
, response
.code
);
1769 EXPECT_FALSE(entry
.get());
1772 TEST_F(FakeDriveServiceTest
, ResumeUpload_NotFound
) {
1773 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1775 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1776 GURL upload_location
;
1777 fake_service_
.InitiateUploadNewFile(
1778 "test/foo", 15, "1_folder_resource_id", "new file.foo",
1779 UploadNewFileOptions(),
1780 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1781 base::RunLoop().RunUntilIdle();
1783 ASSERT_EQ(HTTP_SUCCESS
, error
);
1785 UploadRangeResponse response
;
1786 scoped_ptr
<FileResource
> entry
;
1787 fake_service_
.ResumeUpload(
1788 GURL("https://foo.com/"),
1789 0, 13, 15, "test/foo",
1791 test_util::CreateCopyResultCallback(&response
, &entry
),
1792 ProgressCallback());
1793 base::RunLoop().RunUntilIdle();
1795 EXPECT_EQ(HTTP_NOT_FOUND
, response
.code
);
1796 EXPECT_FALSE(entry
.get());
1799 TEST_F(FakeDriveServiceTest
, ResumeUpload_ExistingFile
) {
1800 base::ScopedTempDir temp_dir
;
1801 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1802 base::FilePath local_file_path
=
1803 temp_dir
.path().Append(FILE_PATH_LITERAL("File 1.txt"));
1804 std::string
contents("hogefugapiyo");
1805 ASSERT_TRUE(test_util::WriteStringToFile(local_file_path
, contents
));
1807 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1809 scoped_ptr
<FileResource
> entry
= FindEntry("2_file_resource_id");
1812 UploadExistingFileOptions options
;
1813 options
.etag
= entry
->etag();
1815 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1816 GURL upload_location
;
1817 fake_service_
.InitiateUploadExistingFile(
1820 "2_file_resource_id",
1822 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1823 base::RunLoop().RunUntilIdle();
1825 ASSERT_EQ(HTTP_SUCCESS
, error
);
1827 UploadRangeResponse response
;
1829 std::vector
<test_util::ProgressInfo
> upload_progress_values
;
1830 fake_service_
.ResumeUpload(
1832 0, contents
.size() / 2, contents
.size(), "text/plain",
1834 test_util::CreateCopyResultCallback(&response
, &entry
),
1835 base::Bind(&test_util::AppendProgressCallbackResult
,
1836 &upload_progress_values
));
1837 base::RunLoop().RunUntilIdle();
1839 EXPECT_EQ(HTTP_RESUME_INCOMPLETE
, response
.code
);
1840 EXPECT_FALSE(entry
.get());
1841 ASSERT_TRUE(!upload_progress_values
.empty());
1842 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1843 EXPECT_LE(0, upload_progress_values
.front().first
);
1844 EXPECT_GE(static_cast<int64
>(contents
.size() / 2),
1845 upload_progress_values
.back().first
);
1847 upload_progress_values
.clear();
1848 fake_service_
.ResumeUpload(
1850 contents
.size() / 2, contents
.size(), contents
.size(), "text/plain",
1852 test_util::CreateCopyResultCallback(&response
, &entry
),
1853 base::Bind(&test_util::AppendProgressCallbackResult
,
1854 &upload_progress_values
));
1855 base::RunLoop().RunUntilIdle();
1857 EXPECT_EQ(HTTP_SUCCESS
, response
.code
);
1858 EXPECT_TRUE(entry
.get());
1859 EXPECT_EQ(static_cast<int64
>(contents
.size()), entry
->file_size());
1860 EXPECT_TRUE(Exists(entry
->file_id()));
1861 ASSERT_TRUE(!upload_progress_values
.empty());
1862 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1863 EXPECT_LE(0, upload_progress_values
.front().first
);
1864 EXPECT_GE(static_cast<int64
>(contents
.size() - contents
.size() / 2),
1865 upload_progress_values
.back().first
);
1866 EXPECT_EQ(base::MD5String(contents
), entry
->md5_checksum());
1869 TEST_F(FakeDriveServiceTest
, ResumeUpload_NewFile
) {
1870 base::ScopedTempDir temp_dir
;
1871 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
1872 base::FilePath local_file_path
=
1873 temp_dir
.path().Append(FILE_PATH_LITERAL("new file.foo"));
1874 std::string
contents("hogefugapiyo");
1875 ASSERT_TRUE(test_util::WriteStringToFile(local_file_path
, contents
));
1877 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1879 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1880 GURL upload_location
;
1881 fake_service_
.InitiateUploadNewFile(
1882 "test/foo", contents
.size(), "1_folder_resource_id", "new file.foo",
1883 UploadNewFileOptions(),
1884 test_util::CreateCopyResultCallback(&error
, &upload_location
));
1885 base::RunLoop().RunUntilIdle();
1887 EXPECT_EQ(HTTP_SUCCESS
, error
);
1888 EXPECT_FALSE(upload_location
.is_empty());
1889 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1892 UploadRangeResponse response
;
1893 scoped_ptr
<FileResource
> entry
;
1894 std::vector
<test_util::ProgressInfo
> upload_progress_values
;
1895 fake_service_
.ResumeUpload(
1897 0, contents
.size() / 2, contents
.size(), "test/foo",
1899 test_util::CreateCopyResultCallback(&response
, &entry
),
1900 base::Bind(&test_util::AppendProgressCallbackResult
,
1901 &upload_progress_values
));
1902 base::RunLoop().RunUntilIdle();
1904 EXPECT_EQ(HTTP_RESUME_INCOMPLETE
, response
.code
);
1905 EXPECT_FALSE(entry
.get());
1906 ASSERT_TRUE(!upload_progress_values
.empty());
1907 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1908 EXPECT_LE(0, upload_progress_values
.front().first
);
1909 EXPECT_GE(static_cast<int64
>(contents
.size() / 2),
1910 upload_progress_values
.back().first
);
1912 upload_progress_values
.clear();
1913 fake_service_
.ResumeUpload(
1915 contents
.size() / 2, contents
.size(), contents
.size(), "test/foo",
1917 test_util::CreateCopyResultCallback(&response
, &entry
),
1918 base::Bind(&test_util::AppendProgressCallbackResult
,
1919 &upload_progress_values
));
1920 base::RunLoop().RunUntilIdle();
1922 EXPECT_EQ(HTTP_CREATED
, response
.code
);
1923 EXPECT_TRUE(entry
.get());
1924 EXPECT_EQ(static_cast<int64
>(contents
.size()), entry
->file_size());
1925 EXPECT_TRUE(Exists(entry
->file_id()));
1926 ASSERT_TRUE(!upload_progress_values
.empty());
1927 EXPECT_TRUE(base::STLIsSorted(upload_progress_values
));
1928 EXPECT_LE(0, upload_progress_values
.front().first
);
1929 EXPECT_GE(static_cast<int64
>(contents
.size() - contents
.size() / 2),
1930 upload_progress_values
.back().first
);
1931 EXPECT_EQ(base::MD5String(contents
), entry
->md5_checksum());
1934 TEST_F(FakeDriveServiceTest
, AddNewFile_ToRootDirectory
) {
1935 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
1937 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1939 const std::string kContentType
= "text/plain";
1940 const std::string kContentData
= "This is some test content.";
1941 const std::string kTitle
= "new file";
1943 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1944 scoped_ptr
<FileResource
> entry
;
1945 fake_service_
.AddNewFile(
1948 fake_service_
.GetRootResourceId(),
1950 false, // shared_with_me
1951 test_util::CreateCopyResultCallback(&error
, &entry
));
1952 base::RunLoop().RunUntilIdle();
1954 EXPECT_EQ(HTTP_CREATED
, error
);
1956 EXPECT_EQ(kContentType
, entry
->mime_type());
1957 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
1958 EXPECT_EQ("resource_id_1", entry
->file_id());
1959 EXPECT_EQ(kTitle
, entry
->title());
1960 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1961 // Should be incremented as a new directory was created.
1962 EXPECT_EQ(old_largest_change_id
+ 1,
1963 fake_service_
.about_resource().largest_change_id());
1964 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1965 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
1968 TEST_F(FakeDriveServiceTest
, AddNewFile_ToRootDirectoryOnEmptyFileSystem
) {
1969 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
1971 const std::string kContentType
= "text/plain";
1972 const std::string kContentData
= "This is some test content.";
1973 const std::string kTitle
= "new file";
1975 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
1976 scoped_ptr
<FileResource
> entry
;
1977 fake_service_
.AddNewFile(
1980 fake_service_
.GetRootResourceId(),
1982 false, // shared_with_me
1983 test_util::CreateCopyResultCallback(&error
, &entry
));
1984 base::RunLoop().RunUntilIdle();
1986 EXPECT_EQ(HTTP_CREATED
, error
);
1988 EXPECT_EQ(kContentType
, entry
->mime_type());
1989 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
1990 EXPECT_EQ("resource_id_1", entry
->file_id());
1991 EXPECT_EQ(kTitle
, entry
->title());
1992 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
1993 // Should be incremented as a new directory was created.
1994 EXPECT_EQ(old_largest_change_id
+ 1,
1995 fake_service_
.about_resource().largest_change_id());
1996 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
1997 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2000 TEST_F(FakeDriveServiceTest
, AddNewFile_ToNonRootDirectory
) {
2001 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2003 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
2005 const std::string kContentType
= "text/plain";
2006 const std::string kContentData
= "This is some test content.";
2007 const std::string kTitle
= "new file";
2008 const std::string kParentResourceId
= "1_folder_resource_id";
2010 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2011 scoped_ptr
<FileResource
> entry
;
2012 fake_service_
.AddNewFile(
2017 false, // shared_with_me
2018 test_util::CreateCopyResultCallback(&error
, &entry
));
2019 base::RunLoop().RunUntilIdle();
2021 EXPECT_EQ(HTTP_CREATED
, error
);
2023 EXPECT_EQ(kContentType
, entry
->mime_type());
2024 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
2025 EXPECT_EQ("resource_id_1", entry
->file_id());
2026 EXPECT_EQ(kTitle
, entry
->title());
2027 EXPECT_TRUE(HasParent(entry
->file_id(), kParentResourceId
));
2028 // Should be incremented as a new directory was created.
2029 EXPECT_EQ(old_largest_change_id
+ 1,
2030 fake_service_
.about_resource().largest_change_id());
2031 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
2032 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2035 TEST_F(FakeDriveServiceTest
, AddNewFile_ToNonexistingDirectory
) {
2036 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2038 const std::string kContentType
= "text/plain";
2039 const std::string kContentData
= "This is some test content.";
2040 const std::string kTitle
= "new file";
2041 const std::string kParentResourceId
= "nonexisting_resource_id";
2043 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2044 scoped_ptr
<FileResource
> entry
;
2045 fake_service_
.AddNewFile(
2050 false, // shared_with_me
2051 test_util::CreateCopyResultCallback(&error
, &entry
));
2052 base::RunLoop().RunUntilIdle();
2054 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
2055 EXPECT_FALSE(entry
);
2058 TEST_F(FakeDriveServiceTest
, AddNewFile_Offline
) {
2059 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2060 fake_service_
.set_offline(true);
2062 const std::string kContentType
= "text/plain";
2063 const std::string kContentData
= "This is some test content.";
2064 const std::string kTitle
= "new file";
2066 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2067 scoped_ptr
<FileResource
> entry
;
2068 fake_service_
.AddNewFile(
2071 fake_service_
.GetRootResourceId(),
2073 false, // shared_with_me
2074 test_util::CreateCopyResultCallback(&error
, &entry
));
2075 base::RunLoop().RunUntilIdle();
2077 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
2078 EXPECT_FALSE(entry
);
2081 TEST_F(FakeDriveServiceTest
, AddNewFile_SharedWithMeLabel
) {
2082 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2084 const std::string kContentType
= "text/plain";
2085 const std::string kContentData
= "This is some test content.";
2086 const std::string kTitle
= "new file";
2088 int64 old_largest_change_id
= GetLargestChangeByAboutResource();
2090 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2091 scoped_ptr
<FileResource
> entry
;
2092 fake_service_
.AddNewFile(
2095 fake_service_
.GetRootResourceId(),
2097 true, // shared_with_me
2098 test_util::CreateCopyResultCallback(&error
, &entry
));
2099 base::RunLoop().RunUntilIdle();
2101 EXPECT_EQ(HTTP_CREATED
, error
);
2103 EXPECT_EQ(kContentType
, entry
->mime_type());
2104 EXPECT_EQ(static_cast<int64
>(kContentData
.size()), entry
->file_size());
2105 EXPECT_EQ("resource_id_1", entry
->file_id());
2106 EXPECT_EQ(kTitle
, entry
->title());
2107 EXPECT_TRUE(HasParent(entry
->file_id(), fake_service_
.GetRootResourceId()));
2108 EXPECT_FALSE(entry
->shared_with_me_date().is_null());
2109 // Should be incremented as a new directory was created.
2110 EXPECT_EQ(old_largest_change_id
+ 1,
2111 fake_service_
.about_resource().largest_change_id());
2112 EXPECT_EQ(old_largest_change_id
+ 1, GetLargestChangeByAboutResource());
2113 EXPECT_EQ(base::MD5String(kContentData
), entry
->md5_checksum());
2116 TEST_F(FakeDriveServiceTest
, SetLastModifiedTime_ExistingFile
) {
2117 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2119 const std::string kResourceId
= "2_file_resource_id";
2121 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time
));
2123 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2124 scoped_ptr
<FileResource
> entry
;
2125 fake_service_
.SetLastModifiedTime(
2128 test_util::CreateCopyResultCallback(&error
, &entry
));
2129 base::RunLoop().RunUntilIdle();
2131 EXPECT_EQ(HTTP_SUCCESS
, error
);
2133 EXPECT_EQ(time
, entry
->modified_date());
2136 TEST_F(FakeDriveServiceTest
, SetLastModifiedTime_NonexistingFile
) {
2137 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2139 const std::string kResourceId
= "nonexisting_resource_id";
2141 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time
));
2143 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2144 scoped_ptr
<FileResource
> entry
;
2145 fake_service_
.SetLastModifiedTime(
2148 test_util::CreateCopyResultCallback(&error
, &entry
));
2149 base::RunLoop().RunUntilIdle();
2151 EXPECT_EQ(HTTP_NOT_FOUND
, error
);
2152 EXPECT_FALSE(entry
);
2155 TEST_F(FakeDriveServiceTest
, SetLastModifiedTime_Offline
) {
2156 ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_
));
2157 fake_service_
.set_offline(true);
2159 const std::string kResourceId
= "2_file_resource_id";
2161 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time
));
2163 DriveApiErrorCode error
= DRIVE_OTHER_ERROR
;
2164 scoped_ptr
<FileResource
> entry
;
2165 fake_service_
.SetLastModifiedTime(
2168 test_util::CreateCopyResultCallback(&error
, &entry
));
2169 base::RunLoop().RunUntilIdle();
2171 EXPECT_EQ(DRIVE_NO_CONNECTION
, error
);
2172 EXPECT_FALSE(entry
);
2177 } // namespace drive