Fix build break
[chromium-blink-merge.git] / chrome / browser / google_apis / fake_drive_service_unittest.cc
blob1000c98f1c9f03288f164b7269e29e6169199686
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/google_apis/fake_drive_service.h"
7 #include <string>
8 #include <vector>
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop.h"
13 #include "base/stl_util.h"
14 #include "base/stringprintf.h"
15 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/google_apis/drive_api_parser.h"
17 #include "chrome/browser/google_apis/gdata_wapi_operations.h"
18 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
19 #include "chrome/browser/google_apis/test_util.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/test/test_browser_thread.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace google_apis {
26 namespace {
28 class FakeDriveServiceTest : public testing::Test {
29 protected:
30 FakeDriveServiceTest()
31 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
34 // Returns the resource entry that matches |resource_id|.
35 scoped_ptr<ResourceEntry> FindEntry(const std::string& resource_id) {
36 GDataErrorCode error = GDATA_OTHER_ERROR;
37 scoped_ptr<ResourceEntry> resource_entry;
38 fake_service_.GetResourceEntry(
39 resource_id,
40 test_util::CreateCopyResultCallback(&error, &resource_entry));
41 message_loop_.RunUntilIdle();
42 return resource_entry.Pass();
45 // Returns true if the resource identified by |resource_id| exists.
46 bool Exists(const std::string& resource_id) {
47 scoped_ptr<ResourceEntry> resource_entry = FindEntry(resource_id);
48 return resource_entry;
51 // Adds a new directory at |parent_resource_id| with the given name.
52 // Returns true on success.
53 bool AddNewDirectory(const std::string& parent_resource_id,
54 const std::string& directory_name) {
55 GDataErrorCode error = GDATA_OTHER_ERROR;
56 scoped_ptr<ResourceEntry> resource_entry;
57 fake_service_.AddNewDirectory(
58 parent_resource_id,
59 directory_name,
60 test_util::CreateCopyResultCallback(&error, &resource_entry));
61 message_loop_.RunUntilIdle();
62 return error == HTTP_CREATED;
65 // Returns true if the resource identified by |resource_id| has a parent
66 // identified by |parent_id|.
67 bool HasParent(const std::string& resource_id, const std::string& parent_id) {
68 const GURL parent_url = FakeDriveService::GetFakeLinkUrl(parent_id);
69 scoped_ptr<ResourceEntry> resource_entry = FindEntry(resource_id);
70 if (resource_entry.get()) {
71 for (size_t i = 0; i < resource_entry->links().size(); ++i) {
72 if (resource_entry->links()[i]->type() == Link::LINK_PARENT &&
73 resource_entry->links()[i]->href() == parent_url)
74 return true;
77 return false;
80 int64 GetLargestChangeByAboutResource() {
81 GDataErrorCode error;
82 scoped_ptr<AboutResource> about_resource;
83 fake_service_.GetAboutResource(
84 test_util::CreateCopyResultCallback(&error, &about_resource));
85 message_loop_.RunUntilIdle();
86 return about_resource->largest_change_id();
89 MessageLoopForUI message_loop_;
90 content::TestBrowserThread ui_thread_;
91 FakeDriveService fake_service_;
94 void AppendGetContentCallbackResult(
95 std::vector<std::string>* values,
96 GDataErrorCode error,
97 scoped_ptr<std::string> content) {
98 DCHECK_EQ(error, HTTP_SUCCESS); // Should always HTTP_SUCCESS.
99 values->push_back(*content);
102 void AppendProgressCallbackResult(std::vector<int64>* values, int64 progress) {
103 values->push_back(progress);
106 TEST_F(FakeDriveServiceTest, GetAllResourceList) {
107 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
108 "chromeos/gdata/root_feed.json"));
110 GDataErrorCode error = GDATA_OTHER_ERROR;
111 scoped_ptr<ResourceList> resource_list;
112 fake_service_.GetAllResourceList(
113 test_util::CreateCopyResultCallback(&error, &resource_list));
114 message_loop_.RunUntilIdle();
116 EXPECT_EQ(HTTP_SUCCESS, error);
117 ASSERT_TRUE(resource_list);
118 // Do some sanity check.
119 EXPECT_EQ(14U, resource_list->entries().size());
120 EXPECT_EQ(1, fake_service_.resource_list_load_count());
123 TEST_F(FakeDriveServiceTest, GetAllResourceList_Offline) {
124 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
125 "chromeos/gdata/root_feed.json"));
126 fake_service_.set_offline(true);
128 GDataErrorCode error = GDATA_OTHER_ERROR;
129 scoped_ptr<ResourceList> resource_list;
130 fake_service_.GetAllResourceList(
131 test_util::CreateCopyResultCallback(&error, &resource_list));
132 message_loop_.RunUntilIdle();
134 EXPECT_EQ(GDATA_NO_CONNECTION, error);
135 EXPECT_FALSE(resource_list);
138 TEST_F(FakeDriveServiceTest, GetResourceListInDirectory_InRootDirectory) {
139 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
140 "chromeos/gdata/root_feed.json"));
142 GDataErrorCode error = GDATA_OTHER_ERROR;
143 scoped_ptr<ResourceList> resource_list;
144 fake_service_.GetResourceListInDirectory(
145 fake_service_.GetRootResourceId(),
146 test_util::CreateCopyResultCallback(&error, &resource_list));
147 message_loop_.RunUntilIdle();
149 EXPECT_EQ(HTTP_SUCCESS, error);
150 ASSERT_TRUE(resource_list);
151 // Do some sanity check. There are 8 entries in the root directory.
152 EXPECT_EQ(8U, resource_list->entries().size());
153 EXPECT_EQ(1, fake_service_.resource_list_load_count());
156 TEST_F(FakeDriveServiceTest, GetResourceListInDirectory_InNonRootDirectory) {
157 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
158 "chromeos/gdata/root_feed.json"));
160 GDataErrorCode error = GDATA_OTHER_ERROR;
161 scoped_ptr<ResourceList> resource_list;
162 fake_service_.GetResourceListInDirectory(
163 "folder:1_folder_resource_id",
164 test_util::CreateCopyResultCallback(&error, &resource_list));
165 message_loop_.RunUntilIdle();
167 EXPECT_EQ(HTTP_SUCCESS, error);
168 ASSERT_TRUE(resource_list);
169 // Do some sanity check. There is three entries in 1_folder_resource_id
170 // directory.
171 EXPECT_EQ(3U, resource_list->entries().size());
172 EXPECT_EQ(1, fake_service_.resource_list_load_count());
175 TEST_F(FakeDriveServiceTest, GetResourceListInDirectory_Offline) {
176 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
177 "chromeos/gdata/root_feed.json"));
178 fake_service_.set_offline(true);
180 GDataErrorCode error = GDATA_OTHER_ERROR;
181 scoped_ptr<ResourceList> resource_list;
182 fake_service_.GetResourceListInDirectory(
183 fake_service_.GetRootResourceId(),
184 test_util::CreateCopyResultCallback(&error, &resource_list));
185 message_loop_.RunUntilIdle();
187 EXPECT_EQ(GDATA_NO_CONNECTION, error);
188 EXPECT_FALSE(resource_list);
191 TEST_F(FakeDriveServiceTest, Search) {
192 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
193 "chromeos/gdata/root_feed.json"));
195 GDataErrorCode error = GDATA_OTHER_ERROR;
196 scoped_ptr<ResourceList> resource_list;
197 fake_service_.Search(
198 "File", // search_query
199 test_util::CreateCopyResultCallback(&error, &resource_list));
200 message_loop_.RunUntilIdle();
202 EXPECT_EQ(HTTP_SUCCESS, error);
203 ASSERT_TRUE(resource_list);
204 // Do some sanity check. There are 4 entries that contain "File" in their
205 // titles.
206 EXPECT_EQ(4U, resource_list->entries().size());
207 EXPECT_EQ(1, fake_service_.resource_list_load_count());
210 TEST_F(FakeDriveServiceTest, Search_WithAttribute) {
211 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
212 "chromeos/gdata/root_feed.json"));
214 GDataErrorCode error = GDATA_OTHER_ERROR;
215 scoped_ptr<ResourceList> resource_list;
216 fake_service_.Search(
217 "title:1.txt", // search_query
218 test_util::CreateCopyResultCallback(&error, &resource_list));
219 message_loop_.RunUntilIdle();
221 EXPECT_EQ(HTTP_SUCCESS, error);
222 ASSERT_TRUE(resource_list);
223 // Do some sanity check. There are 4 entries that contain "1.txt" in their
224 // titles.
225 EXPECT_EQ(4U, resource_list->entries().size());
226 EXPECT_EQ(1, fake_service_.resource_list_load_count());
229 TEST_F(FakeDriveServiceTest, Search_MultipleQueries) {
230 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
231 "chromeos/gdata/root_feed.json"));
233 GDataErrorCode error = GDATA_OTHER_ERROR;
234 scoped_ptr<ResourceList> resource_list;
235 fake_service_.Search(
236 "Directory 1", // search_query
237 test_util::CreateCopyResultCallback(&error, &resource_list));
238 message_loop_.RunUntilIdle();
240 EXPECT_EQ(HTTP_SUCCESS, error);
241 ASSERT_TRUE(resource_list);
242 // There are 2 entries that contain both "Directory" and "1" in their titles.
243 EXPECT_EQ(2U, resource_list->entries().size());
245 fake_service_.Search(
246 "\"Directory 1\"", // search_query
247 test_util::CreateCopyResultCallback(&error, &resource_list));
248 message_loop_.RunUntilIdle();
250 EXPECT_EQ(HTTP_SUCCESS, error);
251 ASSERT_TRUE(resource_list);
252 // There is 1 entry that contain "Directory 1" in its title.
253 EXPECT_EQ(1U, resource_list->entries().size());
254 EXPECT_EQ(2, fake_service_.resource_list_load_count());
257 TEST_F(FakeDriveServiceTest, Search_Offline) {
258 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
259 "chromeos/gdata/root_feed.json"));
260 fake_service_.set_offline(true);
262 GDataErrorCode error = GDATA_OTHER_ERROR;
263 scoped_ptr<ResourceList> resource_list;
264 fake_service_.Search(
265 "Directory 1", // search_query
266 test_util::CreateCopyResultCallback(&error, &resource_list));
267 message_loop_.RunUntilIdle();
269 EXPECT_EQ(GDATA_NO_CONNECTION, error);
270 EXPECT_FALSE(resource_list);
273 TEST_F(FakeDriveServiceTest, SearchInDirectory) {
274 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
275 "chromeos/gdata/root_feed.json"));
277 GDataErrorCode error = GDATA_OTHER_ERROR;
278 scoped_ptr<ResourceList> resource_list;
279 fake_service_.SearchInDirectory(
280 "File", // search_query
281 fake_service_.GetRootResourceId(), // directory_resource_id
282 test_util::CreateCopyResultCallback(&error, &resource_list));
283 message_loop_.RunUntilIdle();
285 EXPECT_EQ(HTTP_SUCCESS, error);
286 ASSERT_TRUE(resource_list);
287 // Do some sanity check. There is entry that contain "File" in their
288 // titles directly under the root directory.
289 EXPECT_EQ(1U, resource_list->entries().size());
290 EXPECT_EQ(1, fake_service_.resource_list_load_count());
293 TEST_F(FakeDriveServiceTest, SearchInDirectory_WithAttribute) {
294 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
295 "chromeos/gdata/root_feed.json"));
297 GDataErrorCode error = GDATA_OTHER_ERROR;
298 scoped_ptr<ResourceList> resource_list;
299 fake_service_.SearchInDirectory(
300 "title:1.txt", // search_query
301 fake_service_.GetRootResourceId(), // directory_resource_id
302 test_util::CreateCopyResultCallback(&error, &resource_list));
303 message_loop_.RunUntilIdle();
305 EXPECT_EQ(HTTP_SUCCESS, error);
306 ASSERT_TRUE(resource_list);
307 // Do some sanity check. There are 2 entries that contain "1.txt" in their
308 // titles directly under the root directory.
309 EXPECT_EQ(2U, resource_list->entries().size());
310 EXPECT_EQ(1, fake_service_.resource_list_load_count());
313 TEST_F(FakeDriveServiceTest, SearchInDirectory_MultipleQueries) {
314 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
315 "chromeos/gdata/root_feed.json"));
317 GDataErrorCode error = GDATA_OTHER_ERROR;
318 scoped_ptr<ResourceList> resource_list;
319 fake_service_.SearchInDirectory(
320 "Directory 1", // search_query
321 fake_service_.GetRootResourceId(), // directory_resource_id
322 test_util::CreateCopyResultCallback(&error, &resource_list));
323 message_loop_.RunUntilIdle();
325 EXPECT_EQ(HTTP_SUCCESS, error);
326 ASSERT_TRUE(resource_list);
327 // There is one entry that contains both "Directory" and "1" in its title
328 // directly under the root directory.
329 EXPECT_EQ(1U, resource_list->entries().size());
331 fake_service_.SearchInDirectory(
332 "\"Directory 1\"", // search_query
333 fake_service_.GetRootResourceId(), // directory_resource_id
334 test_util::CreateCopyResultCallback(&error, &resource_list));
335 message_loop_.RunUntilIdle();
337 EXPECT_EQ(HTTP_SUCCESS, error);
338 ASSERT_TRUE(resource_list);
339 // There is one entry that contains "Directory 1" in its title
340 // directly under the root directory.
341 EXPECT_EQ(1U, resource_list->entries().size());
342 EXPECT_EQ(2, fake_service_.resource_list_load_count());
345 TEST_F(FakeDriveServiceTest, SearchInDirectory_Offline) {
346 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
347 "chromeos/gdata/root_feed.json"));
348 fake_service_.set_offline(true);
350 GDataErrorCode error = GDATA_OTHER_ERROR;
351 scoped_ptr<ResourceList> resource_list;
352 fake_service_.SearchInDirectory(
353 "Directory 1", // search_query
354 fake_service_.GetRootResourceId(), // directory_resource_id
355 test_util::CreateCopyResultCallback(&error, &resource_list));
356 message_loop_.RunUntilIdle();
358 EXPECT_EQ(GDATA_NO_CONNECTION, error);
359 EXPECT_FALSE(resource_list);
362 TEST_F(FakeDriveServiceTest, GetChangeList_NoNewEntries) {
363 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
364 "chromeos/gdata/root_feed.json"));
365 // Load the account_metadata.json as well to add the largest changestamp
366 // (654321) to the existing entries.
367 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
368 "chromeos/gdata/account_metadata.json"));
370 GDataErrorCode error = GDATA_OTHER_ERROR;
371 scoped_ptr<ResourceList> resource_list;
372 fake_service_.GetChangeList(
373 654321 + 1, // start_changestamp
374 test_util::CreateCopyResultCallback(&error, &resource_list));
375 message_loop_.RunUntilIdle();
377 EXPECT_EQ(HTTP_SUCCESS, error);
378 ASSERT_TRUE(resource_list);
379 // This should be empty as the latest changestamp was passed to
380 // GetResourceList(), hence there should be no new entries.
381 EXPECT_EQ(0U, resource_list->entries().size());
382 // It's considered loaded even if the result is empty.
383 EXPECT_EQ(1, fake_service_.resource_list_load_count());
386 TEST_F(FakeDriveServiceTest, GetChangeList_WithNewEntry) {
387 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
388 "chromeos/gdata/root_feed.json"));
389 // Load the account_metadata.json as well to add the largest changestamp
390 // (654321) to the existing entries.
391 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
392 "chromeos/gdata/account_metadata.json"));
393 // Add a new directory in the root directory. The new directory will have
394 // the changestamp of 654322.
395 ASSERT_TRUE(AddNewDirectory(
396 fake_service_.GetRootResourceId(), "new directory"));
398 // Get the resource list newer than 654321.
399 GDataErrorCode error = GDATA_OTHER_ERROR;
400 scoped_ptr<ResourceList> resource_list;
401 fake_service_.GetChangeList(
402 654321 + 1, // start_changestamp
403 test_util::CreateCopyResultCallback(&error, &resource_list));
404 message_loop_.RunUntilIdle();
406 EXPECT_EQ(HTTP_SUCCESS, error);
407 ASSERT_TRUE(resource_list);
408 // The result should only contain the newly created directory.
409 ASSERT_EQ(1U, resource_list->entries().size());
410 EXPECT_EQ("new directory", resource_list->entries()[0]->title());
411 EXPECT_EQ(1, fake_service_.resource_list_load_count());
414 TEST_F(FakeDriveServiceTest, GetChangeList_Offline) {
415 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
416 "chromeos/gdata/root_feed.json"));
417 fake_service_.set_offline(true);
419 GDataErrorCode error = GDATA_OTHER_ERROR;
420 scoped_ptr<ResourceList> resource_list;
421 fake_service_.GetChangeList(
422 654321, // start_changestamp
423 test_util::CreateCopyResultCallback(&error, &resource_list));
424 message_loop_.RunUntilIdle();
426 EXPECT_EQ(GDATA_NO_CONNECTION, error);
427 EXPECT_FALSE(resource_list);
430 TEST_F(FakeDriveServiceTest, ContinueGetResourceList_GetAllResourceList) {
431 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
432 "chromeos/gdata/root_feed.json"));
433 fake_service_.set_default_max_results(6);
435 GDataErrorCode error = GDATA_OTHER_ERROR;
436 scoped_ptr<ResourceList> resource_list;
437 fake_service_.GetAllResourceList(
438 test_util::CreateCopyResultCallback(&error, &resource_list));
439 message_loop_.RunUntilIdle();
440 EXPECT_EQ(HTTP_SUCCESS, error);
441 ASSERT_TRUE(resource_list);
443 // Do some sanity check.
444 // The number of results is 14 entries. Thus, it should split into three
445 // chunks: 6, 6, and then 2.
446 EXPECT_EQ(6U, resource_list->entries().size());
447 EXPECT_EQ(1, fake_service_.resource_list_load_count());
449 // Second page loading.
450 const google_apis::Link* next_link =
451 resource_list->GetLinkByType(Link::LINK_NEXT);
452 ASSERT_TRUE(next_link);
453 // Keep the next url before releasing the |resource_list|.
454 GURL next_url(next_link->href());
456 error = GDATA_OTHER_ERROR;
457 resource_list.reset();
458 fake_service_.ContinueGetResourceList(
459 next_url,
460 test_util::CreateCopyResultCallback(&error, &resource_list));
461 message_loop_.RunUntilIdle();
463 EXPECT_EQ(HTTP_SUCCESS, error);
464 ASSERT_TRUE(resource_list);
466 EXPECT_EQ(6U, resource_list->entries().size());
467 EXPECT_EQ(2, fake_service_.resource_list_load_count());
469 // Third page loading.
470 next_link = resource_list->GetLinkByType(Link::LINK_NEXT);
471 ASSERT_TRUE(next_link);
472 next_url = GURL(next_link->href());
474 error = GDATA_OTHER_ERROR;
475 resource_list.reset();
476 fake_service_.ContinueGetResourceList(
477 next_url,
478 test_util::CreateCopyResultCallback(&error, &resource_list));
479 message_loop_.RunUntilIdle();
481 EXPECT_EQ(HTTP_SUCCESS, error);
482 ASSERT_TRUE(resource_list);
484 EXPECT_EQ(2U, resource_list->entries().size());
485 EXPECT_EQ(3, fake_service_.resource_list_load_count());
488 TEST_F(FakeDriveServiceTest,
489 ContinueGetResourceList_GetResourceListInDirectory) {
490 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
491 "chromeos/gdata/root_feed.json"));
492 fake_service_.set_default_max_results(3);
494 GDataErrorCode error = GDATA_OTHER_ERROR;
495 scoped_ptr<ResourceList> resource_list;
496 fake_service_.GetResourceListInDirectory(
497 fake_service_.GetRootResourceId(),
498 test_util::CreateCopyResultCallback(&error, &resource_list));
499 message_loop_.RunUntilIdle();
500 EXPECT_EQ(HTTP_SUCCESS, error);
501 ASSERT_TRUE(resource_list);
503 // Do some sanity check.
504 // The number of results is 8 entries. Thus, it should split into three
505 // chunks: 3, 3, and then 2.
506 EXPECT_EQ(3U, resource_list->entries().size());
507 EXPECT_EQ(1, fake_service_.resource_list_load_count());
509 // Second page loading.
510 const google_apis::Link* next_link =
511 resource_list->GetLinkByType(Link::LINK_NEXT);
512 ASSERT_TRUE(next_link);
513 // Keep the next url before releasing the |resource_list|.
514 GURL next_url(next_link->href());
516 error = GDATA_OTHER_ERROR;
517 resource_list.reset();
518 fake_service_.ContinueGetResourceList(
519 next_url,
520 test_util::CreateCopyResultCallback(&error, &resource_list));
521 message_loop_.RunUntilIdle();
523 EXPECT_EQ(HTTP_SUCCESS, error);
524 ASSERT_TRUE(resource_list);
526 EXPECT_EQ(3U, resource_list->entries().size());
527 EXPECT_EQ(2, fake_service_.resource_list_load_count());
529 // Third page loading.
530 next_link = resource_list->GetLinkByType(Link::LINK_NEXT);
531 ASSERT_TRUE(next_link);
532 next_url = GURL(next_link->href());
534 error = GDATA_OTHER_ERROR;
535 resource_list.reset();
536 fake_service_.ContinueGetResourceList(
537 next_url,
538 test_util::CreateCopyResultCallback(&error, &resource_list));
539 message_loop_.RunUntilIdle();
541 EXPECT_EQ(HTTP_SUCCESS, error);
542 ASSERT_TRUE(resource_list);
544 EXPECT_EQ(2U, resource_list->entries().size());
545 EXPECT_EQ(3, fake_service_.resource_list_load_count());
548 TEST_F(FakeDriveServiceTest, ContinueGetResourceList_Search) {
549 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
550 "chromeos/gdata/root_feed.json"));
551 fake_service_.set_default_max_results(2);
553 GDataErrorCode error = GDATA_OTHER_ERROR;
554 scoped_ptr<ResourceList> resource_list;
555 fake_service_.Search(
556 "File", // search_query
557 test_util::CreateCopyResultCallback(&error, &resource_list));
558 message_loop_.RunUntilIdle();
559 EXPECT_EQ(HTTP_SUCCESS, error);
560 ASSERT_TRUE(resource_list);
562 // Do some sanity check.
563 // The number of results is 4 entries. Thus, it should split into two
564 // chunks: 2, and then 2
565 EXPECT_EQ(2U, resource_list->entries().size());
566 EXPECT_EQ(1, fake_service_.resource_list_load_count());
568 // Second page loading.
569 const google_apis::Link* next_link =
570 resource_list->GetLinkByType(Link::LINK_NEXT);
571 ASSERT_TRUE(next_link);
572 // Keep the next url before releasing the |resource_list|.
573 GURL next_url(next_link->href());
575 error = GDATA_OTHER_ERROR;
576 resource_list.reset();
577 fake_service_.ContinueGetResourceList(
578 next_url,
579 test_util::CreateCopyResultCallback(&error, &resource_list));
580 message_loop_.RunUntilIdle();
582 EXPECT_EQ(HTTP_SUCCESS, error);
583 ASSERT_TRUE(resource_list);
585 EXPECT_EQ(2U, resource_list->entries().size());
586 EXPECT_EQ(2, fake_service_.resource_list_load_count());
589 TEST_F(FakeDriveServiceTest, ContinueGetResourceList_GetChangeList) {
590 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
591 "chromeos/gdata/root_feed.json"));
592 fake_service_.set_default_max_results(2);
594 // Load the account_metadata.json as well to add the largest changestamp
595 // (654321) to the existing entries.
596 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
597 "chromeos/gdata/account_metadata.json"));
598 // Add 5 new directory in the root directory. The new directory will have
599 // the changestamp of 654326.
600 for (int i = 0; i < 5; ++i) {
601 ASSERT_TRUE(AddNewDirectory(
602 fake_service_.GetRootResourceId(),
603 base::StringPrintf("new directory %d", i)));
606 GDataErrorCode error = GDATA_OTHER_ERROR;
607 scoped_ptr<ResourceList> resource_list;
608 fake_service_.GetChangeList(
609 654321 + 1, // start_changestamp
610 test_util::CreateCopyResultCallback(&error, &resource_list));
611 message_loop_.RunUntilIdle();
612 EXPECT_EQ(HTTP_SUCCESS, error);
613 ASSERT_TRUE(resource_list);
615 // Do some sanity check.
616 // The number of results is 5 entries. Thus, it should split into three
617 // chunks: 2, 2 and then 1.
618 EXPECT_EQ(2U, resource_list->entries().size());
619 EXPECT_EQ(1, fake_service_.resource_list_load_count());
621 // Second page loading.
622 const google_apis::Link* next_link =
623 resource_list->GetLinkByType(Link::LINK_NEXT);
624 ASSERT_TRUE(next_link);
625 // Keep the next url before releasing the |resource_list|.
626 GURL next_url(next_link->href());
628 error = GDATA_OTHER_ERROR;
629 resource_list.reset();
630 fake_service_.ContinueGetResourceList(
631 next_url,
632 test_util::CreateCopyResultCallback(&error, &resource_list));
633 message_loop_.RunUntilIdle();
635 EXPECT_EQ(HTTP_SUCCESS, error);
636 ASSERT_TRUE(resource_list);
638 EXPECT_EQ(2U, resource_list->entries().size());
639 EXPECT_EQ(2, fake_service_.resource_list_load_count());
641 // Third page loading.
642 next_link = resource_list->GetLinkByType(Link::LINK_NEXT);
643 ASSERT_TRUE(next_link);
644 next_url = GURL(next_link->href());
646 error = GDATA_OTHER_ERROR;
647 resource_list.reset();
648 fake_service_.ContinueGetResourceList(
649 next_url,
650 test_util::CreateCopyResultCallback(&error, &resource_list));
651 message_loop_.RunUntilIdle();
653 EXPECT_EQ(HTTP_SUCCESS, error);
654 ASSERT_TRUE(resource_list);
656 EXPECT_EQ(1U, resource_list->entries().size());
657 EXPECT_EQ(3, fake_service_.resource_list_load_count());
660 TEST_F(FakeDriveServiceTest, GetAccountMetadata) {
661 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
662 "chromeos/gdata/account_metadata.json"));
664 GDataErrorCode error = GDATA_OTHER_ERROR;
665 scoped_ptr<AccountMetadata> account_metadata;
666 fake_service_.GetAccountMetadata(
667 test_util::CreateCopyResultCallback(&error, &account_metadata));
668 message_loop_.RunUntilIdle();
670 EXPECT_EQ(HTTP_SUCCESS, error);
672 ASSERT_TRUE(account_metadata);
673 // Do some sanity check.
674 EXPECT_EQ(2U, account_metadata->installed_apps().size());
675 EXPECT_EQ(1, fake_service_.account_metadata_load_count());
678 TEST_F(FakeDriveServiceTest, GetAccountMetadata_Offline) {
679 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
680 "chromeos/gdata/account_metadata.json"));
681 fake_service_.set_offline(true);
683 GDataErrorCode error = GDATA_OTHER_ERROR;
684 scoped_ptr<AccountMetadata> account_metadata;
685 fake_service_.GetAccountMetadata(
686 test_util::CreateCopyResultCallback(&error, &account_metadata));
687 message_loop_.RunUntilIdle();
689 EXPECT_EQ(GDATA_NO_CONNECTION, error);
690 EXPECT_FALSE(account_metadata);
693 TEST_F(FakeDriveServiceTest, GetAboutResource) {
694 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
695 "chromeos/gdata/account_metadata.json"));
697 GDataErrorCode error = GDATA_OTHER_ERROR;
698 scoped_ptr<AboutResource> about_resource;
699 fake_service_.GetAboutResource(
700 test_util::CreateCopyResultCallback(&error, &about_resource));
701 message_loop_.RunUntilIdle();
703 EXPECT_EQ(HTTP_SUCCESS, error);
705 ASSERT_TRUE(about_resource);
706 // Do some sanity check.
707 EXPECT_EQ(fake_service_.GetRootResourceId(),
708 about_resource->root_folder_id());
709 EXPECT_EQ(1, fake_service_.about_resource_load_count());
712 TEST_F(FakeDriveServiceTest, GetAboutResource_Offline) {
713 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
714 "chromeos/gdata/account_metadata.json"));
715 fake_service_.set_offline(true);
717 GDataErrorCode error = GDATA_OTHER_ERROR;
718 scoped_ptr<AboutResource> about_resource;
719 fake_service_.GetAboutResource(
720 test_util::CreateCopyResultCallback(&error, &about_resource));
721 message_loop_.RunUntilIdle();
723 EXPECT_EQ(GDATA_NO_CONNECTION, error);
724 EXPECT_FALSE(about_resource);
727 TEST_F(FakeDriveServiceTest, GetAppList) {
728 ASSERT_TRUE(fake_service_.LoadAppListForDriveApi(
729 "chromeos/drive/applist.json"));
731 GDataErrorCode error = GDATA_OTHER_ERROR;
732 scoped_ptr<AppList> app_list;
733 fake_service_.GetAppList(
734 test_util::CreateCopyResultCallback(&error, &app_list));
735 message_loop_.RunUntilIdle();
737 EXPECT_EQ(HTTP_SUCCESS, error);
739 ASSERT_TRUE(app_list);
742 TEST_F(FakeDriveServiceTest, GetAppList_Offline) {
743 ASSERT_TRUE(fake_service_.LoadAppListForDriveApi(
744 "chromeos/drive/applist.json"));
745 fake_service_.set_offline(true);
747 GDataErrorCode error = GDATA_OTHER_ERROR;
748 scoped_ptr<AppList> app_list;
749 fake_service_.GetAppList(
750 test_util::CreateCopyResultCallback(&error, &app_list));
751 message_loop_.RunUntilIdle();
753 EXPECT_EQ(GDATA_NO_CONNECTION, error);
754 EXPECT_FALSE(app_list);
757 TEST_F(FakeDriveServiceTest, GetResourceEntry_ExistingFile) {
758 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
759 "chromeos/gdata/root_feed.json"));
761 const std::string kResourceId = "file:2_file_resource_id";
762 GDataErrorCode error = GDATA_OTHER_ERROR;
763 scoped_ptr<ResourceEntry> resource_entry;
764 fake_service_.GetResourceEntry(
765 kResourceId,
766 test_util::CreateCopyResultCallback(&error, &resource_entry));
767 message_loop_.RunUntilIdle();
769 EXPECT_EQ(HTTP_SUCCESS, error);
770 ASSERT_TRUE(resource_entry);
771 // Do some sanity check.
772 EXPECT_EQ(kResourceId, resource_entry->resource_id());
775 TEST_F(FakeDriveServiceTest, GetResourceEntry_NonexistingFile) {
776 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
777 "chromeos/gdata/root_feed.json"));
779 const std::string kResourceId = "file:nonexisting_resource_id";
780 GDataErrorCode error = GDATA_OTHER_ERROR;
781 scoped_ptr<ResourceEntry> resource_entry;
782 fake_service_.GetResourceEntry(
783 kResourceId,
784 test_util::CreateCopyResultCallback(&error, &resource_entry));
785 message_loop_.RunUntilIdle();
787 EXPECT_EQ(HTTP_NOT_FOUND, error);
788 ASSERT_FALSE(resource_entry);
791 TEST_F(FakeDriveServiceTest, GetResourceEntry_Offline) {
792 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
793 "chromeos/gdata/root_feed.json"));
794 fake_service_.set_offline(true);
796 const std::string kResourceId = "file:2_file_resource_id";
797 GDataErrorCode error = GDATA_OTHER_ERROR;
798 scoped_ptr<ResourceEntry> resource_entry;
799 fake_service_.GetResourceEntry(
800 kResourceId,
801 test_util::CreateCopyResultCallback(&error, &resource_entry));
802 message_loop_.RunUntilIdle();
804 EXPECT_EQ(GDATA_NO_CONNECTION, error);
805 EXPECT_FALSE(resource_entry);
808 TEST_F(FakeDriveServiceTest, DeleteResource_ExistingFile) {
809 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
810 "chromeos/gdata/root_feed.json"));
812 // Resource "file:2_file_resource_id" should now exist.
813 ASSERT_TRUE(Exists("file:2_file_resource_id"));
815 GDataErrorCode error = GDATA_OTHER_ERROR;
816 fake_service_.DeleteResource("file:2_file_resource_id",
817 std::string(), // etag
818 test_util::CreateCopyResultCallback(&error));
819 message_loop_.RunUntilIdle();
821 EXPECT_EQ(HTTP_SUCCESS, error);
822 // Resource "file:2_file_resource_id" should be gone now.
823 EXPECT_FALSE(Exists("file:2_file_resource_id"));
826 TEST_F(FakeDriveServiceTest, DeleteResource_NonexistingFile) {
827 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
828 "chromeos/gdata/root_feed.json"));
830 GDataErrorCode error = GDATA_OTHER_ERROR;
831 fake_service_.DeleteResource("file:nonexisting_resource_id",
832 std::string(), // etag
833 test_util::CreateCopyResultCallback(&error));
834 message_loop_.RunUntilIdle();
836 EXPECT_EQ(HTTP_NOT_FOUND, error);
839 TEST_F(FakeDriveServiceTest, DeleteResource_Offline) {
840 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
841 "chromeos/gdata/root_feed.json"));
842 fake_service_.set_offline(true);
844 GDataErrorCode error = GDATA_OTHER_ERROR;
845 fake_service_.DeleteResource("file:2_file_resource_id",
846 std::string(), // etag
847 test_util::CreateCopyResultCallback(&error));
848 message_loop_.RunUntilIdle();
850 EXPECT_EQ(GDATA_NO_CONNECTION, error);
853 TEST_F(FakeDriveServiceTest, DownloadFile_ExistingFile) {
854 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
855 "chromeos/gdata/root_feed.json"));
857 base::ScopedTempDir temp_dir;
858 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
860 std::vector<test_util::ProgressInfo> download_progress_values;
862 const GURL kContentUrl("https://file_content_url/");
863 const base::FilePath kOutputFilePath =
864 temp_dir.path().AppendASCII("whatever.txt");
865 GDataErrorCode error = GDATA_OTHER_ERROR;
866 base::FilePath output_file_path;
867 std::vector<std::string> content_buffer;
868 fake_service_.DownloadFile(
869 base::FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path
870 kOutputFilePath,
871 kContentUrl,
872 test_util::CreateCopyResultCallback(&error, &output_file_path),
873 base::Bind(&AppendGetContentCallbackResult, &content_buffer),
874 base::Bind(&test_util::AppendProgressCallbackResult,
875 &download_progress_values));
876 message_loop_.RunUntilIdle();
878 EXPECT_EQ(HTTP_SUCCESS, error);
879 EXPECT_EQ(output_file_path, kOutputFilePath);
880 std::string content;
881 ASSERT_TRUE(file_util::ReadFileToString(output_file_path, &content));
882 // The content is "x"s of the file size specified in root_feed.json.
883 EXPECT_EQ("xxxxxxxxxx", content);
884 ASSERT_TRUE(!download_progress_values.empty());
885 EXPECT_TRUE(base::STLIsSorted(download_progress_values));
886 EXPECT_GE(download_progress_values.front().first, 0);
887 EXPECT_LE(download_progress_values.back().first, 10);
889 std::string concatenated_content;
890 for (size_t i = 0; i < content_buffer.size(); ++i) {
891 concatenated_content += content_buffer[i];
893 EXPECT_EQ(content, concatenated_content);
896 TEST_F(FakeDriveServiceTest, DownloadFile_NonexistingFile) {
897 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
898 "chromeos/gdata/root_feed.json"));
900 base::ScopedTempDir temp_dir;
901 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
903 const GURL kContentUrl("https://non_existing_content_url/");
904 const base::FilePath kOutputFilePath =
905 temp_dir.path().AppendASCII("whatever.txt");
906 GDataErrorCode error = GDATA_OTHER_ERROR;
907 base::FilePath output_file_path;
908 fake_service_.DownloadFile(
909 base::FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path
910 kOutputFilePath,
911 kContentUrl,
912 test_util::CreateCopyResultCallback(&error, &output_file_path),
913 GetContentCallback(),
914 ProgressCallback());
915 message_loop_.RunUntilIdle();
917 EXPECT_EQ(HTTP_NOT_FOUND, error);
920 TEST_F(FakeDriveServiceTest, DownloadFile_Offline) {
921 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
922 "chromeos/gdata/root_feed.json"));
923 fake_service_.set_offline(true);
925 base::ScopedTempDir temp_dir;
926 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
928 const GURL kContentUrl("https://file_content_url/");
929 const base::FilePath kOutputFilePath =
930 temp_dir.path().AppendASCII("whatever.txt");
931 GDataErrorCode error = GDATA_OTHER_ERROR;
932 base::FilePath output_file_path;
933 fake_service_.DownloadFile(
934 base::FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path
935 kOutputFilePath,
936 kContentUrl,
937 test_util::CreateCopyResultCallback(&error, &output_file_path),
938 GetContentCallback(),
939 ProgressCallback());
940 message_loop_.RunUntilIdle();
942 EXPECT_EQ(GDATA_NO_CONNECTION, error);
945 TEST_F(FakeDriveServiceTest, CopyHostedDocument_ExistingHostedDocument) {
946 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
947 "chromeos/gdata/root_feed.json"));
948 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
949 "chromeos/gdata/account_metadata.json"));
951 int64 old_largest_change_id = GetLargestChangeByAboutResource();
953 const std::string kResourceId = "document:5_document_resource_id";
954 GDataErrorCode error = GDATA_OTHER_ERROR;
955 scoped_ptr<ResourceEntry> resource_entry;
956 fake_service_.CopyHostedDocument(
957 kResourceId,
958 "new name",
959 test_util::CreateCopyResultCallback(&error, &resource_entry));
960 message_loop_.RunUntilIdle();
962 EXPECT_EQ(HTTP_SUCCESS, error);
963 ASSERT_TRUE(resource_entry);
964 // The copied entry should have the new resource ID and the title.
965 EXPECT_EQ(kResourceId + "_copied", resource_entry->resource_id());
966 EXPECT_EQ("new name", resource_entry->title());
967 // Should be incremented as a new hosted document was created.
968 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
969 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
972 TEST_F(FakeDriveServiceTest, CopyHostedDocument_NonexistingHostedDocument) {
973 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
974 "chromeos/gdata/root_feed.json"));
976 const std::string kResourceId = "document:nonexisting_resource_id";
977 GDataErrorCode error = GDATA_OTHER_ERROR;
978 scoped_ptr<ResourceEntry> resource_entry;
979 fake_service_.CopyHostedDocument(
980 kResourceId,
981 "new name",
982 test_util::CreateCopyResultCallback(&error, &resource_entry));
983 message_loop_.RunUntilIdle();
985 EXPECT_EQ(HTTP_NOT_FOUND, error);
988 TEST_F(FakeDriveServiceTest, CopyHostedDocument_ExistingRegularFile) {
989 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
990 "chromeos/gdata/root_feed.json"));
992 const std::string kResourceId = "file:2_file_resource_id";
993 GDataErrorCode error = GDATA_OTHER_ERROR;
994 scoped_ptr<ResourceEntry> resource_entry;
995 fake_service_.CopyHostedDocument(
996 kResourceId,
997 "new name",
998 test_util::CreateCopyResultCallback(&error, &resource_entry));
999 message_loop_.RunUntilIdle();
1001 // The copy should fail as this is not a hosted document.
1002 EXPECT_EQ(HTTP_NOT_FOUND, error);
1003 EXPECT_FALSE(resource_entry);
1006 TEST_F(FakeDriveServiceTest, CopyHostedDocument_Offline) {
1007 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1008 "chromeos/gdata/root_feed.json"));
1009 fake_service_.set_offline(true);
1011 const std::string kResourceId = "document:5_document_resource_id";
1012 GDataErrorCode error = GDATA_OTHER_ERROR;
1013 scoped_ptr<ResourceEntry> resource_entry;
1014 fake_service_.CopyHostedDocument(
1015 kResourceId,
1016 "new name",
1017 test_util::CreateCopyResultCallback(&error, &resource_entry));
1018 message_loop_.RunUntilIdle();
1020 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1021 EXPECT_FALSE(resource_entry);
1024 TEST_F(FakeDriveServiceTest, RenameResource_ExistingFile) {
1025 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1026 "chromeos/gdata/root_feed.json"));
1027 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1028 "chromeos/gdata/account_metadata.json"));
1030 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1032 const std::string kResourceId = "file:2_file_resource_id";
1034 GDataErrorCode error = GDATA_OTHER_ERROR;
1035 fake_service_.RenameResource(kResourceId,
1036 "new name",
1037 test_util::CreateCopyResultCallback(&error));
1038 message_loop_.RunUntilIdle();
1040 EXPECT_EQ(HTTP_SUCCESS, error);
1042 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId);
1043 ASSERT_TRUE(resource_entry);
1044 EXPECT_EQ("new name", resource_entry->title());
1045 // Should be incremented as a file was renamed.
1046 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1047 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1050 TEST_F(FakeDriveServiceTest, RenameResource_NonexistingFile) {
1051 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1052 "chromeos/gdata/root_feed.json"));
1054 const std::string kResourceId = "file:nonexisting_file";
1056 GDataErrorCode error = GDATA_OTHER_ERROR;
1057 fake_service_.RenameResource(kResourceId,
1058 "new name",
1059 test_util::CreateCopyResultCallback(&error));
1060 message_loop_.RunUntilIdle();
1062 EXPECT_EQ(HTTP_NOT_FOUND, error);
1065 TEST_F(FakeDriveServiceTest, RenameResource_Offline) {
1066 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1067 "chromeos/gdata/root_feed.json"));
1068 fake_service_.set_offline(true);
1070 const std::string kResourceId = "file:2_file_resource_id";
1072 GDataErrorCode error = GDATA_OTHER_ERROR;
1073 fake_service_.RenameResource(kResourceId,
1074 "new name",
1075 test_util::CreateCopyResultCallback(&error));
1076 message_loop_.RunUntilIdle();
1078 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1081 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInRootDirectory) {
1082 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1083 "chromeos/gdata/root_feed.json"));
1084 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1085 "chromeos/gdata/account_metadata.json"));
1087 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1089 const std::string kResourceId = "file:2_file_resource_id";
1090 const std::string kOldParentResourceId = fake_service_.GetRootResourceId();
1091 const std::string kNewParentResourceId = "folder:1_folder_resource_id";
1093 // Here's the original parent link.
1094 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1095 EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
1097 GDataErrorCode error = GDATA_OTHER_ERROR;
1098 fake_service_.AddResourceToDirectory(
1099 kNewParentResourceId,
1100 kResourceId,
1101 test_util::CreateCopyResultCallback(&error));
1102 message_loop_.RunUntilIdle();
1104 EXPECT_EQ(HTTP_SUCCESS, error);
1106 // The parent link should now be changed.
1107 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1108 EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
1109 // Should be incremented as a file was moved.
1110 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1111 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1114 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInNonRootDirectory) {
1115 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1116 "chromeos/gdata/root_feed.json"));
1117 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1118 "chromeos/gdata/account_metadata.json"));
1120 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1122 const std::string kResourceId = "file:subdirectory_file_1_id";
1123 const std::string kOldParentResourceId = "folder:1_folder_resource_id";
1124 const std::string kNewParentResourceId = "folder:2_folder_resource_id";
1126 // Here's the original parent link.
1127 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1128 EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
1130 GDataErrorCode error = GDATA_OTHER_ERROR;
1131 fake_service_.AddResourceToDirectory(
1132 kNewParentResourceId,
1133 kResourceId,
1134 test_util::CreateCopyResultCallback(&error));
1135 message_loop_.RunUntilIdle();
1137 EXPECT_EQ(HTTP_SUCCESS, error);
1139 // The parent link should now be changed.
1140 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1141 EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
1142 // Should be incremented as a file was moved.
1143 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1144 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1147 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_NonexistingFile) {
1148 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1149 "chromeos/gdata/root_feed.json"));
1151 const std::string kResourceId = "file:nonexisting_file";
1152 const std::string kNewParentResourceId = "folder:1_folder_resource_id";
1154 GDataErrorCode error = GDATA_OTHER_ERROR;
1155 fake_service_.AddResourceToDirectory(
1156 kNewParentResourceId,
1157 kResourceId,
1158 test_util::CreateCopyResultCallback(&error));
1159 message_loop_.RunUntilIdle();
1161 EXPECT_EQ(HTTP_NOT_FOUND, error);
1164 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_OrphanFile) {
1165 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1166 "chromeos/gdata/root_feed.json"));
1167 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1168 "chromeos/gdata/account_metadata.json"));
1170 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1172 const std::string kResourceId = "file:1_orphanfile_resource_id";
1173 const std::string kNewParentResourceId = "folder:1_folder_resource_id";
1175 // The file does not belong to any directory, even to the root.
1176 EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
1177 EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
1179 GDataErrorCode error = GDATA_OTHER_ERROR;
1180 fake_service_.AddResourceToDirectory(
1181 kNewParentResourceId,
1182 kResourceId,
1183 test_util::CreateCopyResultCallback(&error));
1184 message_loop_.RunUntilIdle();
1186 EXPECT_EQ(HTTP_SUCCESS, error);
1188 // The parent link should now be changed.
1189 EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
1190 EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
1191 // Should be incremented as a file was moved.
1192 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1193 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1196 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_Offline) {
1197 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1198 "chromeos/gdata/root_feed.json"));
1199 fake_service_.set_offline(true);
1201 const std::string kResourceId = "file:2_file_resource_id";
1202 const std::string kNewParentResourceId = "folder:1_folder_resource_id";
1204 GDataErrorCode error = GDATA_OTHER_ERROR;
1205 fake_service_.AddResourceToDirectory(
1206 kNewParentResourceId,
1207 kResourceId,
1208 test_util::CreateCopyResultCallback(&error));
1209 message_loop_.RunUntilIdle();
1211 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1214 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_ExistingFile) {
1215 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1216 "chromeos/gdata/root_feed.json"));
1217 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1218 "chromeos/gdata/account_metadata.json"));
1220 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1222 const std::string kResourceId = "file:subdirectory_file_1_id";
1223 const std::string kParentResourceId = "folder:1_folder_resource_id";
1225 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId);
1226 ASSERT_TRUE(resource_entry);
1227 // The parent link should exist now.
1228 const google_apis::Link* parent_link =
1229 resource_entry->GetLinkByType(Link::LINK_PARENT);
1230 ASSERT_TRUE(parent_link);
1232 GDataErrorCode error = GDATA_OTHER_ERROR;
1233 fake_service_.RemoveResourceFromDirectory(
1234 kParentResourceId,
1235 kResourceId,
1236 test_util::CreateCopyResultCallback(&error));
1237 message_loop_.RunUntilIdle();
1239 EXPECT_EQ(HTTP_SUCCESS, error);
1241 resource_entry = FindEntry(kResourceId);
1242 ASSERT_TRUE(resource_entry);
1243 // The parent link should be gone now.
1244 parent_link = resource_entry->GetLinkByType(Link::LINK_PARENT);
1245 ASSERT_FALSE(parent_link);
1246 // Should be incremented as a file was moved to the root directory.
1247 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1248 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1251 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_NonexistingFile) {
1252 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1253 "chromeos/gdata/root_feed.json"));
1255 const std::string kResourceId = "file:nonexisting_file";
1256 const std::string kParentResourceId = "folder:1_folder_resource_id";
1258 GDataErrorCode error = GDATA_OTHER_ERROR;
1259 fake_service_.RemoveResourceFromDirectory(
1260 kParentResourceId,
1261 kResourceId,
1262 test_util::CreateCopyResultCallback(&error));
1263 message_loop_.RunUntilIdle();
1265 EXPECT_EQ(HTTP_NOT_FOUND, error);
1268 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_OrphanFile) {
1269 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1270 "chromeos/gdata/root_feed.json"));
1272 const std::string kResourceId = "file:1_orphanfile_resource_id";
1273 const std::string kParentResourceId = fake_service_.GetRootResourceId();
1275 GDataErrorCode error = GDATA_OTHER_ERROR;
1276 fake_service_.RemoveResourceFromDirectory(
1277 kParentResourceId,
1278 kResourceId,
1279 test_util::CreateCopyResultCallback(&error));
1280 message_loop_.RunUntilIdle();
1282 EXPECT_EQ(HTTP_NOT_FOUND, error);
1285 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_Offline) {
1286 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1287 "chromeos/gdata/root_feed.json"));
1288 fake_service_.set_offline(true);
1290 const std::string kResourceId = "file:subdirectory_file_1_id";
1291 const std::string kParentResourceId = "folder:1_folder_resource_id";
1293 GDataErrorCode error = GDATA_OTHER_ERROR;
1294 fake_service_.RemoveResourceFromDirectory(
1295 kParentResourceId,
1296 kResourceId,
1297 test_util::CreateCopyResultCallback(&error));
1298 message_loop_.RunUntilIdle();
1300 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1303 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectory) {
1304 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1305 "chromeos/gdata/root_feed.json"));
1306 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1307 "chromeos/gdata/account_metadata.json"));
1309 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1311 GDataErrorCode error = GDATA_OTHER_ERROR;
1312 scoped_ptr<ResourceEntry> resource_entry;
1313 fake_service_.AddNewDirectory(
1314 fake_service_.GetRootResourceId(),
1315 "new directory",
1316 test_util::CreateCopyResultCallback(&error, &resource_entry));
1317 message_loop_.RunUntilIdle();
1319 EXPECT_EQ(HTTP_CREATED, error);
1320 ASSERT_TRUE(resource_entry);
1321 EXPECT_TRUE(resource_entry->is_folder());
1322 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
1323 EXPECT_EQ("new directory", resource_entry->title());
1324 EXPECT_TRUE(HasParent(resource_entry->resource_id(),
1325 fake_service_.GetRootResourceId()));
1326 // Should be incremented as a new directory was created.
1327 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1328 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1331 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectoryOnEmptyFileSystem) {
1332 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1333 "chromeos/gdata/empty_feed.json"));
1334 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1335 "chromeos/gdata/account_metadata.json"));
1337 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1339 GDataErrorCode error = GDATA_OTHER_ERROR;
1340 scoped_ptr<ResourceEntry> resource_entry;
1341 fake_service_.AddNewDirectory(
1342 fake_service_.GetRootResourceId(),
1343 "new directory",
1344 test_util::CreateCopyResultCallback(&error, &resource_entry));
1345 message_loop_.RunUntilIdle();
1347 EXPECT_EQ(HTTP_CREATED, error);
1348 ASSERT_TRUE(resource_entry);
1349 EXPECT_TRUE(resource_entry->is_folder());
1350 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
1351 EXPECT_EQ("new directory", resource_entry->title());
1352 EXPECT_TRUE(HasParent(resource_entry->resource_id(),
1353 fake_service_.GetRootResourceId()));
1354 // Should be incremented as a new directory was created.
1355 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1356 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1359 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonRootDirectory) {
1360 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1361 "chromeos/gdata/root_feed.json"));
1362 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1363 "chromeos/gdata/account_metadata.json"));
1365 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1367 const std::string kParentResourceId = "folder:1_folder_resource_id";
1369 GDataErrorCode error = GDATA_OTHER_ERROR;
1370 scoped_ptr<ResourceEntry> resource_entry;
1371 fake_service_.AddNewDirectory(
1372 kParentResourceId,
1373 "new directory",
1374 test_util::CreateCopyResultCallback(&error, &resource_entry));
1375 message_loop_.RunUntilIdle();
1377 EXPECT_EQ(HTTP_CREATED, error);
1378 ASSERT_TRUE(resource_entry);
1379 EXPECT_TRUE(resource_entry->is_folder());
1380 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
1381 EXPECT_EQ("new directory", resource_entry->title());
1382 EXPECT_TRUE(HasParent(resource_entry->resource_id(), kParentResourceId));
1383 // Should be incremented as a new directory was created.
1384 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1385 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1388 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonexistingDirectory) {
1389 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1390 "chromeos/gdata/root_feed.json"));
1392 const std::string kParentResourceId = "folder:nonexisting_resource_id";
1394 GDataErrorCode error = GDATA_OTHER_ERROR;
1395 scoped_ptr<ResourceEntry> resource_entry;
1396 fake_service_.AddNewDirectory(
1397 kParentResourceId,
1398 "new directory",
1399 test_util::CreateCopyResultCallback(&error, &resource_entry));
1400 message_loop_.RunUntilIdle();
1402 EXPECT_EQ(HTTP_NOT_FOUND, error);
1403 EXPECT_FALSE(resource_entry);
1406 TEST_F(FakeDriveServiceTest, AddNewDirectory_Offline) {
1407 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1408 "chromeos/gdata/root_feed.json"));
1409 fake_service_.set_offline(true);
1411 GDataErrorCode error = GDATA_OTHER_ERROR;
1412 scoped_ptr<ResourceEntry> resource_entry;
1413 fake_service_.AddNewDirectory(
1414 fake_service_.GetRootResourceId(),
1415 "new directory",
1416 test_util::CreateCopyResultCallback(&error, &resource_entry));
1417 message_loop_.RunUntilIdle();
1419 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1420 EXPECT_FALSE(resource_entry);
1423 TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_Offline) {
1424 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1425 "chromeos/gdata/root_feed.json"));
1426 fake_service_.set_offline(true);
1428 GDataErrorCode error = GDATA_OTHER_ERROR;
1429 GURL upload_location;
1430 fake_service_.InitiateUploadNewFile(
1431 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
1432 "test/foo",
1434 "folder:1_folder_resource_id",
1435 "new file.foo",
1436 test_util::CreateCopyResultCallback(&error, &upload_location));
1437 message_loop_.RunUntilIdle();
1439 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1440 EXPECT_TRUE(upload_location.is_empty());
1443 TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_NotFound) {
1444 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1445 "chromeos/gdata/root_feed.json"));
1447 GDataErrorCode error = GDATA_OTHER_ERROR;
1448 GURL upload_location;
1449 fake_service_.InitiateUploadNewFile(
1450 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
1451 "test/foo",
1453 "non_existent",
1454 "new file.foo",
1455 test_util::CreateCopyResultCallback(&error, &upload_location));
1456 message_loop_.RunUntilIdle();
1458 EXPECT_EQ(HTTP_NOT_FOUND, error);
1459 EXPECT_TRUE(upload_location.is_empty());
1462 TEST_F(FakeDriveServiceTest, InitiateUploadNewFile) {
1463 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1464 "chromeos/gdata/root_feed.json"));
1466 GDataErrorCode error = GDATA_OTHER_ERROR;
1467 GURL upload_location;
1468 fake_service_.InitiateUploadNewFile(
1469 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1")),
1470 "test/foo",
1472 "folder:1_folder_resource_id",
1473 "new file.foo",
1474 test_util::CreateCopyResultCallback(&error, &upload_location));
1475 message_loop_.RunUntilIdle();
1477 EXPECT_EQ(HTTP_SUCCESS, error);
1478 EXPECT_FALSE(upload_location.is_empty());
1479 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1480 upload_location);
1483 TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Offline) {
1484 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1485 "chromeos/gdata/root_feed.json"));
1486 fake_service_.set_offline(true);
1488 GDataErrorCode error = GDATA_OTHER_ERROR;
1489 GURL upload_location;
1490 fake_service_.InitiateUploadExistingFile(
1491 base::FilePath(FILE_PATH_LITERAL("drive/File 1")),
1492 "test/foo",
1494 "file:2_file_resource_id",
1495 std::string(), // etag
1496 test_util::CreateCopyResultCallback(&error, &upload_location));
1497 message_loop_.RunUntilIdle();
1499 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1500 EXPECT_TRUE(upload_location.is_empty());
1503 TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_NotFound) {
1504 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1505 "chromeos/gdata/root_feed.json"));
1507 GDataErrorCode error = GDATA_OTHER_ERROR;
1508 GURL upload_location;
1509 fake_service_.InitiateUploadExistingFile(
1510 base::FilePath(FILE_PATH_LITERAL("drive/File 1")),
1511 "test/foo",
1513 "non_existent",
1514 std::string(), // etag
1515 test_util::CreateCopyResultCallback(&error, &upload_location));
1516 message_loop_.RunUntilIdle();
1518 EXPECT_EQ(HTTP_NOT_FOUND, error);
1519 EXPECT_TRUE(upload_location.is_empty());
1522 TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_WrongETag) {
1523 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1524 "chromeos/gdata/root_feed.json"));
1526 GDataErrorCode error = GDATA_OTHER_ERROR;
1527 GURL upload_location;
1528 fake_service_.InitiateUploadExistingFile(
1529 base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
1530 "text/plain",
1532 "file:2_file_resource_id",
1533 "invalid_etag",
1534 test_util::CreateCopyResultCallback(&error, &upload_location));
1535 message_loop_.RunUntilIdle();
1537 EXPECT_EQ(HTTP_PRECONDITION, error);
1538 EXPECT_TRUE(upload_location.is_empty());
1541 TEST_F(FakeDriveServiceTest, InitiateUpload_ExistingFile) {
1542 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1543 "chromeos/gdata/root_feed.json"));
1545 GDataErrorCode error = GDATA_OTHER_ERROR;
1546 GURL upload_location;
1547 fake_service_.InitiateUploadExistingFile(
1548 base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
1549 "text/plain",
1551 "file:2_file_resource_id",
1552 "\"HhMOFgxXHit7ImBr\"",
1553 test_util::CreateCopyResultCallback(&error, &upload_location));
1554 message_loop_.RunUntilIdle();
1556 EXPECT_EQ(HTTP_SUCCESS, error);
1557 EXPECT_EQ(GURL("https://2_file_link_resumable_create_media"),
1558 upload_location);
1561 TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) {
1562 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1563 "chromeos/gdata/root_feed.json"));
1565 GDataErrorCode error = GDATA_OTHER_ERROR;
1566 GURL upload_location;
1567 fake_service_.InitiateUploadNewFile(
1568 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1569 "test/foo",
1571 "folder:1_folder_resource_id",
1572 "new file.foo",
1573 test_util::CreateCopyResultCallback(&error, &upload_location));
1574 message_loop_.RunUntilIdle();
1576 EXPECT_EQ(HTTP_SUCCESS, error);
1577 EXPECT_FALSE(upload_location.is_empty());
1578 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1579 upload_location);
1581 fake_service_.set_offline(true);
1583 UploadRangeResponse response;
1584 scoped_ptr<ResourceEntry> entry;
1585 fake_service_.ResumeUpload(
1586 UPLOAD_NEW_FILE,
1587 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1588 upload_location,
1589 0, 13, 15, "test/foo",
1590 scoped_refptr<net::IOBuffer>(),
1591 test_util::CreateCopyResultCallback(&response, &entry),
1592 ProgressCallback());
1593 message_loop_.RunUntilIdle();
1595 EXPECT_EQ(GDATA_NO_CONNECTION, response.code);
1596 EXPECT_FALSE(entry.get());
1599 TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) {
1600 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1601 "chromeos/gdata/root_feed.json"));
1603 GDataErrorCode error = GDATA_OTHER_ERROR;
1604 GURL upload_location;
1605 fake_service_.InitiateUploadNewFile(
1606 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1607 "test/foo",
1609 "folder:1_folder_resource_id",
1610 "new file.foo",
1611 test_util::CreateCopyResultCallback(&error, &upload_location));
1612 message_loop_.RunUntilIdle();
1614 ASSERT_EQ(HTTP_SUCCESS, error);
1616 UploadRangeResponse response;
1617 scoped_ptr<ResourceEntry> entry;
1618 fake_service_.ResumeUpload(
1619 UPLOAD_NEW_FILE,
1620 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1621 GURL("https://foo.com/"),
1622 0, 13, 15, "test/foo",
1623 scoped_refptr<net::IOBuffer>(),
1624 test_util::CreateCopyResultCallback(&response, &entry),
1625 ProgressCallback());
1626 message_loop_.RunUntilIdle();
1628 EXPECT_EQ(HTTP_NOT_FOUND, response.code);
1629 EXPECT_FALSE(entry.get());
1632 TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) {
1633 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1634 "chromeos/gdata/root_feed.json"));
1636 GDataErrorCode error = GDATA_OTHER_ERROR;
1637 GURL upload_location;
1638 fake_service_.InitiateUploadExistingFile(
1639 base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
1640 "text/plain",
1642 "file:2_file_resource_id",
1643 "\"HhMOFgxXHit7ImBr\"",
1644 test_util::CreateCopyResultCallback(&error, &upload_location));
1645 message_loop_.RunUntilIdle();
1647 ASSERT_EQ(HTTP_SUCCESS, error);
1649 UploadRangeResponse response;
1650 scoped_ptr<ResourceEntry> entry;
1651 std::vector<test_util::ProgressInfo> upload_progress_values;
1652 fake_service_.ResumeUpload(
1653 UPLOAD_EXISTING_FILE,
1654 base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
1655 upload_location,
1656 0, 13, 15, "text/plain",
1657 scoped_refptr<net::IOBuffer>(),
1658 test_util::CreateCopyResultCallback(&response, &entry),
1659 base::Bind(&test_util::AppendProgressCallbackResult,
1660 &upload_progress_values));
1661 message_loop_.RunUntilIdle();
1663 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1664 EXPECT_FALSE(entry.get());
1665 ASSERT_TRUE(!upload_progress_values.empty());
1666 EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1667 EXPECT_GE(upload_progress_values.front().first, 0);
1668 EXPECT_LE(upload_progress_values.back().first, 13);
1670 upload_progress_values.clear();
1671 fake_service_.ResumeUpload(
1672 UPLOAD_EXISTING_FILE,
1673 base::FilePath(FILE_PATH_LITERAL("drive/File 1.txt")),
1674 upload_location,
1675 13, 15, 15, "text/plain",
1676 scoped_refptr<net::IOBuffer>(),
1677 test_util::CreateCopyResultCallback(&response, &entry),
1678 base::Bind(&test_util::AppendProgressCallbackResult,
1679 &upload_progress_values));
1680 message_loop_.RunUntilIdle();
1682 EXPECT_EQ(HTTP_SUCCESS, response.code);
1683 EXPECT_TRUE(entry.get());
1684 EXPECT_EQ(15L, entry->file_size());
1685 EXPECT_TRUE(Exists(entry->resource_id()));
1686 ASSERT_TRUE(!upload_progress_values.empty());
1687 EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1688 EXPECT_GE(upload_progress_values.front().first, 0);
1689 EXPECT_LE(upload_progress_values.back().first, 2);
1692 TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) {
1693 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1694 "chromeos/gdata/root_feed.json"));
1696 GDataErrorCode error = GDATA_OTHER_ERROR;
1697 GURL upload_location;
1698 fake_service_.InitiateUploadNewFile(
1699 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1700 "test/foo",
1702 "folder:1_folder_resource_id",
1703 "new file.foo",
1704 test_util::CreateCopyResultCallback(&error, &upload_location));
1705 message_loop_.RunUntilIdle();
1707 EXPECT_EQ(HTTP_SUCCESS, error);
1708 EXPECT_FALSE(upload_location.is_empty());
1709 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1710 upload_location);
1712 UploadRangeResponse response;
1713 scoped_ptr<ResourceEntry> entry;
1714 std::vector<test_util::ProgressInfo> upload_progress_values;
1715 fake_service_.ResumeUpload(
1716 UPLOAD_NEW_FILE,
1717 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1718 upload_location,
1719 0, 13, 15, "test/foo",
1720 scoped_refptr<net::IOBuffer>(),
1721 test_util::CreateCopyResultCallback(&response, &entry),
1722 base::Bind(&test_util::AppendProgressCallbackResult,
1723 &upload_progress_values));
1724 message_loop_.RunUntilIdle();
1726 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1727 EXPECT_FALSE(entry.get());
1728 ASSERT_TRUE(!upload_progress_values.empty());
1729 EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1730 EXPECT_GE(upload_progress_values.front().first, 0);
1731 EXPECT_LE(upload_progress_values.back().first, 13);
1733 upload_progress_values.clear();
1734 fake_service_.ResumeUpload(
1735 UPLOAD_NEW_FILE,
1736 base::FilePath(FILE_PATH_LITERAL("drive/Directory 1/new file.foo")),
1737 upload_location,
1738 13, 15, 15, "test/foo",
1739 scoped_refptr<net::IOBuffer>(),
1740 test_util::CreateCopyResultCallback(&response, &entry),
1741 base::Bind(&test_util::AppendProgressCallbackResult,
1742 &upload_progress_values));
1743 message_loop_.RunUntilIdle();
1745 EXPECT_EQ(HTTP_CREATED, response.code);
1746 EXPECT_TRUE(entry.get());
1747 EXPECT_EQ(15L, entry->file_size());
1748 EXPECT_TRUE(Exists(entry->resource_id()));
1749 ASSERT_TRUE(!upload_progress_values.empty());
1750 EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1751 EXPECT_GE(upload_progress_values.front().first, 0);
1752 EXPECT_LE(upload_progress_values.back().first, 2);
1755 TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectory) {
1756 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1757 "chromeos/gdata/root_feed.json"));
1758 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1759 "chromeos/gdata/account_metadata.json"));
1761 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1763 const std::string kContentType = "text/plain";
1764 const int64 kContentSize = 123;
1765 const std::string kTitle = "new file";
1767 GDataErrorCode error = GDATA_OTHER_ERROR;
1768 scoped_ptr<ResourceEntry> resource_entry;
1769 fake_service_.AddNewFile(
1770 kContentType,
1771 kContentSize,
1772 fake_service_.GetRootResourceId(),
1773 kTitle,
1774 test_util::CreateCopyResultCallback(&error, &resource_entry));
1775 message_loop_.RunUntilIdle();
1777 EXPECT_EQ(HTTP_CREATED, error);
1778 ASSERT_TRUE(resource_entry);
1779 EXPECT_TRUE(resource_entry->is_file());
1780 EXPECT_EQ(kContentType, resource_entry->content_mime_type());
1781 EXPECT_EQ(kContentSize, resource_entry->file_size());
1782 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
1783 EXPECT_EQ(kTitle, resource_entry->title());
1784 EXPECT_TRUE(HasParent(resource_entry->resource_id(),
1785 fake_service_.GetRootResourceId()));
1786 // Should be incremented as a new directory was created.
1787 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1788 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1791 TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectoryOnEmptyFileSystem) {
1792 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1793 "chromeos/gdata/empty_feed.json"));
1794 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1795 "chromeos/gdata/account_metadata.json"));
1797 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1799 const std::string kContentType = "text/plain";
1800 const int64 kContentSize = 123;
1801 const std::string kTitle = "new file";
1803 GDataErrorCode error = GDATA_OTHER_ERROR;
1804 scoped_ptr<ResourceEntry> resource_entry;
1805 fake_service_.AddNewFile(
1806 kContentType,
1807 kContentSize,
1808 fake_service_.GetRootResourceId(),
1809 kTitle,
1810 test_util::CreateCopyResultCallback(&error, &resource_entry));
1811 message_loop_.RunUntilIdle();
1813 EXPECT_EQ(HTTP_CREATED, error);
1814 ASSERT_TRUE(resource_entry);
1815 EXPECT_TRUE(resource_entry->is_file());
1816 EXPECT_EQ(kContentType, resource_entry->content_mime_type());
1817 EXPECT_EQ(kContentSize, resource_entry->file_size());
1818 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
1819 EXPECT_EQ(kTitle, resource_entry->title());
1820 EXPECT_TRUE(HasParent(resource_entry->resource_id(),
1821 fake_service_.GetRootResourceId()));
1822 // Should be incremented as a new directory was created.
1823 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1824 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1827 TEST_F(FakeDriveServiceTest, AddNewFile_ToNonRootDirectory) {
1828 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1829 "chromeos/gdata/root_feed.json"));
1830 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi(
1831 "chromeos/gdata/account_metadata.json"));
1833 int64 old_largest_change_id = GetLargestChangeByAboutResource();
1835 const std::string kContentType = "text/plain";
1836 const int64 kContentSize = 123;
1837 const std::string kTitle = "new file";
1838 const std::string kParentResourceId = "folder:1_folder_resource_id";
1840 GDataErrorCode error = GDATA_OTHER_ERROR;
1841 scoped_ptr<ResourceEntry> resource_entry;
1842 fake_service_.AddNewFile(
1843 kContentType,
1844 kContentSize,
1845 kParentResourceId,
1846 kTitle,
1847 test_util::CreateCopyResultCallback(&error, &resource_entry));
1848 message_loop_.RunUntilIdle();
1850 EXPECT_EQ(HTTP_CREATED, error);
1851 ASSERT_TRUE(resource_entry);
1852 EXPECT_TRUE(resource_entry->is_file());
1853 EXPECT_EQ(kContentType, resource_entry->content_mime_type());
1854 EXPECT_EQ(kContentSize, resource_entry->file_size());
1855 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
1856 EXPECT_EQ(kTitle, resource_entry->title());
1857 EXPECT_TRUE(HasParent(resource_entry->resource_id(), kParentResourceId));
1858 // Should be incremented as a new directory was created.
1859 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp());
1860 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1863 TEST_F(FakeDriveServiceTest, AddNewFile_ToNonexistingDirectory) {
1864 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1865 "chromeos/gdata/root_feed.json"));
1867 const std::string kContentType = "text/plain";
1868 const int64 kContentSize = 123;
1869 const std::string kTitle = "new file";
1870 const std::string kParentResourceId = "folder:nonexisting_resource_id";
1872 GDataErrorCode error = GDATA_OTHER_ERROR;
1873 scoped_ptr<ResourceEntry> resource_entry;
1874 fake_service_.AddNewFile(
1875 kContentType,
1876 kContentSize,
1877 kParentResourceId,
1878 kTitle,
1879 test_util::CreateCopyResultCallback(&error, &resource_entry));
1880 message_loop_.RunUntilIdle();
1882 EXPECT_EQ(HTTP_NOT_FOUND, error);
1883 EXPECT_FALSE(resource_entry);
1886 TEST_F(FakeDriveServiceTest, AddNewFile_Offline) {
1887 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1888 "chromeos/gdata/root_feed.json"));
1889 fake_service_.set_offline(true);
1891 const std::string kContentType = "text/plain";
1892 const int64 kContentSize = 123;
1893 const std::string kTitle = "new file";
1895 GDataErrorCode error = GDATA_OTHER_ERROR;
1896 scoped_ptr<ResourceEntry> resource_entry;
1897 fake_service_.AddNewFile(
1898 kContentType,
1899 kContentSize,
1900 fake_service_.GetRootResourceId(),
1901 kTitle,
1902 test_util::CreateCopyResultCallback(&error, &resource_entry));
1903 message_loop_.RunUntilIdle();
1905 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1906 EXPECT_FALSE(resource_entry);
1909 TEST_F(FakeDriveServiceTest, SetLastModifiedTime_ExistingFile) {
1910 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1911 "chromeos/gdata/root_feed.json"));
1913 const std::string kResourceId = "file:2_file_resource_id";
1914 base::Time time;
1915 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
1917 GDataErrorCode error = GDATA_OTHER_ERROR;
1918 scoped_ptr<ResourceEntry> resource_entry;
1919 fake_service_.SetLastModifiedTime(
1920 kResourceId,
1921 time,
1922 test_util::CreateCopyResultCallback(&error, &resource_entry));
1923 message_loop_.RunUntilIdle();
1925 EXPECT_EQ(HTTP_SUCCESS, error);
1926 ASSERT_TRUE(resource_entry);
1927 EXPECT_EQ(time, resource_entry->updated_time());
1930 TEST_F(FakeDriveServiceTest, SetLastModifiedTime_NonexistingFile) {
1931 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1932 "chromeos/gdata/root_feed.json"));
1934 const std::string kResourceId = "file:nonexisting_resource_id";
1935 base::Time time;
1936 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
1938 GDataErrorCode error = GDATA_OTHER_ERROR;
1939 scoped_ptr<ResourceEntry> resource_entry;
1940 fake_service_.SetLastModifiedTime(
1941 kResourceId,
1942 time,
1943 test_util::CreateCopyResultCallback(&error, &resource_entry));
1944 message_loop_.RunUntilIdle();
1946 EXPECT_EQ(HTTP_NOT_FOUND, error);
1947 EXPECT_FALSE(resource_entry);
1950 TEST_F(FakeDriveServiceTest, SetLastModifiedTime_Offline) {
1951 ASSERT_TRUE(fake_service_.LoadResourceListForWapi(
1952 "chromeos/gdata/root_feed.json"));
1953 fake_service_.set_offline(true);
1955 const std::string kResourceId = "file:2_file_resource_id";
1956 base::Time time;
1957 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
1959 GDataErrorCode error = GDATA_OTHER_ERROR;
1960 scoped_ptr<ResourceEntry> resource_entry;
1961 fake_service_.SetLastModifiedTime(
1962 kResourceId,
1963 time,
1964 test_util::CreateCopyResultCallback(&error, &resource_entry));
1965 message_loop_.RunUntilIdle();
1967 EXPECT_EQ(GDATA_NO_CONNECTION, error);
1968 EXPECT_FALSE(resource_entry);
1971 } // namespace
1973 } // namespace google_apis