1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/drive/fake_drive_service.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/file_util.h" 11 #include "base/files/scoped_temp_dir.h" 12 #include "base/md5.h" 13 #include "base/run_loop.h" 14 #include "base/stl_util.h" 15 #include "base/strings/stringprintf.h" 16 #include "base/strings/utf_string_conversions.h" 17 #include "content/public/test/test_browser_thread_bundle.h" 18 #include "google_apis/drive/drive_api_parser.h" 19 #include "google_apis/drive/gdata_wapi_parser.h" 20 #include "google_apis/drive/gdata_wapi_requests.h" 21 #include "google_apis/drive/test_util.h" 22 #include "testing/gtest/include/gtest/gtest.h" 23 24 using google_apis::AboutResource; 25 using google_apis::AppList; 26 using google_apis::GDataErrorCode; 27 using google_apis::GDATA_NO_CONNECTION; 28 using google_apis::GDATA_OTHER_ERROR; 29 using google_apis::GetContentCallback; 30 using google_apis::HTTP_CREATED; 31 using google_apis::HTTP_NOT_FOUND; 32 using google_apis::HTTP_NO_CONTENT; 33 using google_apis::HTTP_PRECONDITION; 34 using google_apis::HTTP_RESUME_INCOMPLETE; 35 using google_apis::HTTP_SUCCESS; 36 using google_apis::Link; 37 using google_apis::ProgressCallback; 38 using google_apis::ResourceEntry; 39 using google_apis::ResourceList; 40 using google_apis::UploadRangeResponse; 41 namespace test_util = google_apis::test_util; 42 43 namespace drive { 44 45 namespace { 46 47 class FakeDriveServiceTest : public testing::Test { 48 protected: 49 // Returns the resource entry that matches |resource_id|. 50 scoped_ptr<ResourceEntry> FindEntry(const std::string& resource_id) { 51 GDataErrorCode error = GDATA_OTHER_ERROR; 52 scoped_ptr<ResourceEntry> resource_entry; 53 fake_service_.GetResourceEntry( 54 resource_id, 55 test_util::CreateCopyResultCallback(&error, &resource_entry)); 56 base::RunLoop().RunUntilIdle(); 57 return resource_entry.Pass(); 58 } 59 60 // Returns true if the resource identified by |resource_id| exists. 61 bool Exists(const std::string& resource_id) { 62 scoped_ptr<ResourceEntry> resource_entry = FindEntry(resource_id); 63 return resource_entry && !resource_entry->deleted(); 64 } 65 66 // Adds a new directory at |parent_resource_id| with the given name. 67 // Returns true on success. 68 bool AddNewDirectory(const std::string& parent_resource_id, 69 const std::string& directory_title) { 70 GDataErrorCode error = GDATA_OTHER_ERROR; 71 scoped_ptr<ResourceEntry> resource_entry; 72 fake_service_.AddNewDirectory( 73 parent_resource_id, 74 directory_title, 75 test_util::CreateCopyResultCallback(&error, &resource_entry)); 76 base::RunLoop().RunUntilIdle(); 77 return error == HTTP_CREATED; 78 } 79 80 // Returns true if the resource identified by |resource_id| has a parent 81 // identified by |parent_id|. 82 bool HasParent(const std::string& resource_id, const std::string& parent_id) { 83 const GURL parent_url = FakeDriveService::GetFakeLinkUrl(parent_id); 84 scoped_ptr<ResourceEntry> resource_entry = FindEntry(resource_id); 85 if (resource_entry.get()) { 86 for (size_t i = 0; i < resource_entry->links().size(); ++i) { 87 if (resource_entry->links()[i]->type() == Link::LINK_PARENT && 88 resource_entry->links()[i]->href() == parent_url) 89 return true; 90 } 91 } 92 return false; 93 } 94 95 int64 GetLargestChangeByAboutResource() { 96 GDataErrorCode error; 97 scoped_ptr<AboutResource> about_resource; 98 fake_service_.GetAboutResource( 99 test_util::CreateCopyResultCallback(&error, &about_resource)); 100 base::RunLoop().RunUntilIdle(); 101 return about_resource->largest_change_id(); 102 } 103 104 content::TestBrowserThreadBundle thread_bundle_; 105 FakeDriveService fake_service_; 106 }; 107 108 TEST_F(FakeDriveServiceTest, GetAllResourceList) { 109 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 110 "gdata/root_feed.json")); 111 112 GDataErrorCode error = GDATA_OTHER_ERROR; 113 scoped_ptr<ResourceList> resource_list; 114 fake_service_.GetAllResourceList( 115 test_util::CreateCopyResultCallback(&error, &resource_list)); 116 base::RunLoop().RunUntilIdle(); 117 118 EXPECT_EQ(HTTP_SUCCESS, error); 119 ASSERT_TRUE(resource_list); 120 // Do some sanity check. 121 EXPECT_EQ(14U, resource_list->entries().size()); 122 EXPECT_EQ(1, fake_service_.resource_list_load_count()); 123 } 124 125 TEST_F(FakeDriveServiceTest, GetAllResourceList_Offline) { 126 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 127 "gdata/root_feed.json")); 128 fake_service_.set_offline(true); 129 130 GDataErrorCode error = GDATA_OTHER_ERROR; 131 scoped_ptr<ResourceList> resource_list; 132 fake_service_.GetAllResourceList( 133 test_util::CreateCopyResultCallback(&error, &resource_list)); 134 base::RunLoop().RunUntilIdle(); 135 136 EXPECT_EQ(GDATA_NO_CONNECTION, error); 137 EXPECT_FALSE(resource_list); 138 } 139 140 TEST_F(FakeDriveServiceTest, GetResourceListInDirectory_InRootDirectory) { 141 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 142 "gdata/root_feed.json")); 143 144 GDataErrorCode error = GDATA_OTHER_ERROR; 145 scoped_ptr<ResourceList> resource_list; 146 fake_service_.GetResourceListInDirectory( 147 fake_service_.GetRootResourceId(), 148 test_util::CreateCopyResultCallback(&error, &resource_list)); 149 base::RunLoop().RunUntilIdle(); 150 151 EXPECT_EQ(HTTP_SUCCESS, error); 152 ASSERT_TRUE(resource_list); 153 // Do some sanity check. There are 8 entries in the root directory. 154 EXPECT_EQ(8U, resource_list->entries().size()); 155 EXPECT_EQ(1, fake_service_.directory_load_count()); 156 } 157 158 TEST_F(FakeDriveServiceTest, GetResourceListInDirectory_InNonRootDirectory) { 159 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 160 "gdata/root_feed.json")); 161 162 GDataErrorCode error = GDATA_OTHER_ERROR; 163 scoped_ptr<ResourceList> resource_list; 164 fake_service_.GetResourceListInDirectory( 165 "folder:1_folder_resource_id", 166 test_util::CreateCopyResultCallback(&error, &resource_list)); 167 base::RunLoop().RunUntilIdle(); 168 169 EXPECT_EQ(HTTP_SUCCESS, error); 170 ASSERT_TRUE(resource_list); 171 // Do some sanity check. There is three entries in 1_folder_resource_id 172 // directory. 173 EXPECT_EQ(3U, resource_list->entries().size()); 174 EXPECT_EQ(1, fake_service_.directory_load_count()); 175 } 176 177 TEST_F(FakeDriveServiceTest, GetResourceListInDirectory_Offline) { 178 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 179 "gdata/root_feed.json")); 180 fake_service_.set_offline(true); 181 182 GDataErrorCode error = GDATA_OTHER_ERROR; 183 scoped_ptr<ResourceList> resource_list; 184 fake_service_.GetResourceListInDirectory( 185 fake_service_.GetRootResourceId(), 186 test_util::CreateCopyResultCallback(&error, &resource_list)); 187 base::RunLoop().RunUntilIdle(); 188 189 EXPECT_EQ(GDATA_NO_CONNECTION, error); 190 EXPECT_FALSE(resource_list); 191 } 192 193 TEST_F(FakeDriveServiceTest, Search) { 194 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 195 "gdata/root_feed.json")); 196 197 GDataErrorCode error = GDATA_OTHER_ERROR; 198 scoped_ptr<ResourceList> resource_list; 199 fake_service_.Search( 200 "File", // search_query 201 test_util::CreateCopyResultCallback(&error, &resource_list)); 202 base::RunLoop().RunUntilIdle(); 203 204 EXPECT_EQ(HTTP_SUCCESS, error); 205 ASSERT_TRUE(resource_list); 206 // Do some sanity check. There are 4 entries that contain "File" in their 207 // titles. 208 EXPECT_EQ(4U, resource_list->entries().size()); 209 } 210 211 TEST_F(FakeDriveServiceTest, Search_WithAttribute) { 212 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 213 "gdata/root_feed.json")); 214 215 GDataErrorCode error = GDATA_OTHER_ERROR; 216 scoped_ptr<ResourceList> resource_list; 217 fake_service_.Search( 218 "title:1.txt", // search_query 219 test_util::CreateCopyResultCallback(&error, &resource_list)); 220 base::RunLoop().RunUntilIdle(); 221 222 EXPECT_EQ(HTTP_SUCCESS, error); 223 ASSERT_TRUE(resource_list); 224 // Do some sanity check. There are 4 entries that contain "1.txt" in their 225 // titles. 226 EXPECT_EQ(4U, resource_list->entries().size()); 227 } 228 229 TEST_F(FakeDriveServiceTest, Search_MultipleQueries) { 230 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 231 "gdata/root_feed.json")); 232 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 base::RunLoop().RunUntilIdle(); 239 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()); 244 245 fake_service_.Search( 246 "\"Directory 1\"", // search_query 247 test_util::CreateCopyResultCallback(&error, &resource_list)); 248 base::RunLoop().RunUntilIdle(); 249 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 } 255 256 TEST_F(FakeDriveServiceTest, Search_Offline) { 257 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 258 "gdata/root_feed.json")); 259 fake_service_.set_offline(true); 260 261 GDataErrorCode error = GDATA_OTHER_ERROR; 262 scoped_ptr<ResourceList> resource_list; 263 fake_service_.Search( 264 "Directory 1", // search_query 265 test_util::CreateCopyResultCallback(&error, &resource_list)); 266 base::RunLoop().RunUntilIdle(); 267 268 EXPECT_EQ(GDATA_NO_CONNECTION, error); 269 EXPECT_FALSE(resource_list); 270 } 271 272 TEST_F(FakeDriveServiceTest, Search_Deleted) { 273 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 274 "gdata/root_feed.json")); 275 276 GDataErrorCode error = GDATA_OTHER_ERROR; 277 fake_service_.DeleteResource("file:2_file_resource_id", 278 std::string(), // etag 279 test_util::CreateCopyResultCallback(&error)); 280 base::RunLoop().RunUntilIdle(); 281 EXPECT_EQ(HTTP_NO_CONTENT, error); 282 283 error = GDATA_OTHER_ERROR; 284 scoped_ptr<ResourceList> resource_list; 285 fake_service_.Search( 286 "File", // search_query 287 test_util::CreateCopyResultCallback(&error, &resource_list)); 288 base::RunLoop().RunUntilIdle(); 289 290 EXPECT_EQ(HTTP_SUCCESS, error); 291 ASSERT_TRUE(resource_list); 292 // Do some sanity check. There are 4 entries that contain "File" in their 293 // titles and one of them is deleted. 294 EXPECT_EQ(3U, resource_list->entries().size()); 295 } 296 297 TEST_F(FakeDriveServiceTest, Search_Trashed) { 298 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 299 "gdata/root_feed.json")); 300 301 GDataErrorCode error = GDATA_OTHER_ERROR; 302 fake_service_.TrashResource("file:2_file_resource_id", 303 test_util::CreateCopyResultCallback(&error)); 304 base::RunLoop().RunUntilIdle(); 305 EXPECT_EQ(HTTP_SUCCESS, error); 306 307 error = GDATA_OTHER_ERROR; 308 scoped_ptr<ResourceList> resource_list; 309 fake_service_.Search( 310 "File", // search_query 311 test_util::CreateCopyResultCallback(&error, &resource_list)); 312 base::RunLoop().RunUntilIdle(); 313 314 EXPECT_EQ(HTTP_SUCCESS, error); 315 ASSERT_TRUE(resource_list); 316 // Do some sanity check. There are 4 entries that contain "File" in their 317 // titles and one of them is deleted. 318 EXPECT_EQ(3U, resource_list->entries().size()); 319 } 320 321 TEST_F(FakeDriveServiceTest, SearchByTitle) { 322 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 323 "gdata/root_feed.json")); 324 325 GDataErrorCode error = GDATA_OTHER_ERROR; 326 scoped_ptr<ResourceList> resource_list; 327 fake_service_.SearchByTitle( 328 "1.txt", // title 329 fake_service_.GetRootResourceId(), // directory_resource_id 330 test_util::CreateCopyResultCallback(&error, &resource_list)); 331 base::RunLoop().RunUntilIdle(); 332 333 EXPECT_EQ(HTTP_SUCCESS, error); 334 ASSERT_TRUE(resource_list); 335 // Do some sanity check. There are 2 entries that contain "1.txt" in their 336 // titles directly under the root directory. 337 EXPECT_EQ(2U, resource_list->entries().size()); 338 } 339 340 TEST_F(FakeDriveServiceTest, SearchByTitle_EmptyDirectoryResourceId) { 341 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 342 "gdata/root_feed.json")); 343 344 GDataErrorCode error = GDATA_OTHER_ERROR; 345 scoped_ptr<ResourceList> resource_list; 346 fake_service_.SearchByTitle( 347 "1.txt", // title 348 "", // directory resource id 349 test_util::CreateCopyResultCallback(&error, &resource_list)); 350 base::RunLoop().RunUntilIdle(); 351 352 EXPECT_EQ(HTTP_SUCCESS, error); 353 ASSERT_TRUE(resource_list); 354 // Do some sanity check. There are 4 entries that contain "1.txt" in their 355 // titles. 356 EXPECT_EQ(4U, resource_list->entries().size()); 357 } 358 359 TEST_F(FakeDriveServiceTest, SearchByTitle_Offline) { 360 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 361 "gdata/root_feed.json")); 362 fake_service_.set_offline(true); 363 364 GDataErrorCode error = GDATA_OTHER_ERROR; 365 scoped_ptr<ResourceList> resource_list; 366 fake_service_.SearchByTitle( 367 "Directory 1", // title 368 fake_service_.GetRootResourceId(), // directory_resource_id 369 test_util::CreateCopyResultCallback(&error, &resource_list)); 370 base::RunLoop().RunUntilIdle(); 371 372 EXPECT_EQ(GDATA_NO_CONNECTION, error); 373 EXPECT_FALSE(resource_list); 374 } 375 376 TEST_F(FakeDriveServiceTest, GetChangeList_NoNewEntries) { 377 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 378 "gdata/root_feed.json")); 379 // Load the account_metadata.json as well to add the largest changestamp 380 // (654321) to the existing entries. 381 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 382 "gdata/account_metadata.json")); 383 384 GDataErrorCode error = GDATA_OTHER_ERROR; 385 scoped_ptr<ResourceList> resource_list; 386 fake_service_.GetChangeList( 387 654321 + 1, // start_changestamp 388 test_util::CreateCopyResultCallback(&error, &resource_list)); 389 base::RunLoop().RunUntilIdle(); 390 391 EXPECT_EQ(HTTP_SUCCESS, error); 392 ASSERT_TRUE(resource_list); 393 EXPECT_EQ(fake_service_.largest_changestamp(), 394 resource_list->largest_changestamp()); 395 // This should be empty as the latest changestamp was passed to 396 // GetResourceList(), hence there should be no new entries. 397 EXPECT_EQ(0U, resource_list->entries().size()); 398 // It's considered loaded even if the result is empty. 399 EXPECT_EQ(1, fake_service_.change_list_load_count()); 400 } 401 402 TEST_F(FakeDriveServiceTest, GetChangeList_WithNewEntry) { 403 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 404 "gdata/root_feed.json")); 405 // Load the account_metadata.json as well to add the largest changestamp 406 // (654321) to the existing entries. 407 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 408 "gdata/account_metadata.json")); 409 // Add a new directory in the root directory. The new directory will have 410 // the changestamp of 654322. 411 ASSERT_TRUE(AddNewDirectory( 412 fake_service_.GetRootResourceId(), "new directory")); 413 414 // Get the resource list newer than 654321. 415 GDataErrorCode error = GDATA_OTHER_ERROR; 416 scoped_ptr<ResourceList> resource_list; 417 fake_service_.GetChangeList( 418 654321 + 1, // start_changestamp 419 test_util::CreateCopyResultCallback(&error, &resource_list)); 420 base::RunLoop().RunUntilIdle(); 421 422 EXPECT_EQ(HTTP_SUCCESS, error); 423 ASSERT_TRUE(resource_list); 424 EXPECT_EQ(fake_service_.largest_changestamp(), 425 resource_list->largest_changestamp()); 426 // The result should only contain the newly created directory. 427 ASSERT_EQ(1U, resource_list->entries().size()); 428 EXPECT_EQ("new directory", resource_list->entries()[0]->title()); 429 EXPECT_EQ(1, fake_service_.change_list_load_count()); 430 } 431 432 TEST_F(FakeDriveServiceTest, GetChangeList_Offline) { 433 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 434 "gdata/root_feed.json")); 435 fake_service_.set_offline(true); 436 437 GDataErrorCode error = GDATA_OTHER_ERROR; 438 scoped_ptr<ResourceList> resource_list; 439 fake_service_.GetChangeList( 440 654321, // start_changestamp 441 test_util::CreateCopyResultCallback(&error, &resource_list)); 442 base::RunLoop().RunUntilIdle(); 443 444 EXPECT_EQ(GDATA_NO_CONNECTION, error); 445 EXPECT_FALSE(resource_list); 446 } 447 448 TEST_F(FakeDriveServiceTest, GetChangeList_DeletedEntry) { 449 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 450 "gdata/root_feed.json")); 451 // Load the account_metadata.json as well to add the largest changestamp 452 // (654321) to the existing entries. 453 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 454 "gdata/account_metadata.json")); 455 ASSERT_TRUE(Exists("file:2_file_resource_id")); 456 457 GDataErrorCode error = GDATA_OTHER_ERROR; 458 fake_service_.DeleteResource("file:2_file_resource_id", 459 std::string(), // etag 460 test_util::CreateCopyResultCallback(&error)); 461 base::RunLoop().RunUntilIdle(); 462 ASSERT_EQ(HTTP_NO_CONTENT, error); 463 ASSERT_FALSE(Exists("file:2_file_resource_id")); 464 465 // Get the resource list newer than 654321. 466 error = GDATA_OTHER_ERROR; 467 scoped_ptr<ResourceList> resource_list; 468 fake_service_.GetChangeList( 469 654321 + 1, // start_changestamp 470 test_util::CreateCopyResultCallback(&error, &resource_list)); 471 base::RunLoop().RunUntilIdle(); 472 473 EXPECT_EQ(HTTP_SUCCESS, error); 474 ASSERT_TRUE(resource_list); 475 EXPECT_EQ(fake_service_.largest_changestamp(), 476 resource_list->largest_changestamp()); 477 // The result should only contain the deleted file. 478 ASSERT_EQ(1U, resource_list->entries().size()); 479 const ResourceEntry& entry = *resource_list->entries()[0]; 480 EXPECT_EQ("file:2_file_resource_id", entry.resource_id()); 481 EXPECT_TRUE(entry.title().empty()); 482 EXPECT_TRUE(entry.deleted()); 483 EXPECT_EQ(1, fake_service_.change_list_load_count()); 484 } 485 486 TEST_F(FakeDriveServiceTest, GetChangeList_TrashedEntry) { 487 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 488 "gdata/root_feed.json")); 489 // Load the account_metadata.json as well to add the largest changestamp 490 // (654321) to the existing entries. 491 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 492 "gdata/account_metadata.json")); 493 ASSERT_TRUE(Exists("file:2_file_resource_id")); 494 495 GDataErrorCode error = GDATA_OTHER_ERROR; 496 fake_service_.TrashResource("file:2_file_resource_id", 497 test_util::CreateCopyResultCallback(&error)); 498 base::RunLoop().RunUntilIdle(); 499 ASSERT_EQ(HTTP_SUCCESS, error); 500 ASSERT_FALSE(Exists("file:2_file_resource_id")); 501 502 // Get the resource list newer than 654321. 503 error = GDATA_OTHER_ERROR; 504 scoped_ptr<ResourceList> resource_list; 505 fake_service_.GetChangeList( 506 654321 + 1, // start_changestamp 507 test_util::CreateCopyResultCallback(&error, &resource_list)); 508 base::RunLoop().RunUntilIdle(); 509 510 EXPECT_EQ(HTTP_SUCCESS, error); 511 ASSERT_TRUE(resource_list); 512 EXPECT_EQ(fake_service_.largest_changestamp(), 513 resource_list->largest_changestamp()); 514 // The result should only contain the trashed file. 515 ASSERT_EQ(1U, resource_list->entries().size()); 516 const ResourceEntry& entry = *resource_list->entries()[0]; 517 EXPECT_EQ("file:2_file_resource_id", entry.resource_id()); 518 EXPECT_TRUE(entry.deleted()); 519 EXPECT_EQ(1, fake_service_.change_list_load_count()); 520 } 521 522 TEST_F(FakeDriveServiceTest, GetRemainingChangeList_GetAllResourceList) { 523 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 524 "gdata/root_feed.json")); 525 fake_service_.set_default_max_results(6); 526 527 GDataErrorCode error = GDATA_OTHER_ERROR; 528 scoped_ptr<ResourceList> resource_list; 529 fake_service_.GetAllResourceList( 530 test_util::CreateCopyResultCallback(&error, &resource_list)); 531 base::RunLoop().RunUntilIdle(); 532 EXPECT_EQ(HTTP_SUCCESS, error); 533 ASSERT_TRUE(resource_list); 534 535 // Do some sanity check. 536 // The number of results is 14 entries. Thus, it should split into three 537 // chunks: 6, 6, and then 2. 538 EXPECT_EQ(6U, resource_list->entries().size()); 539 EXPECT_EQ(1, fake_service_.resource_list_load_count()); 540 541 // Second page loading. 542 const google_apis::Link* next_link = 543 resource_list->GetLinkByType(Link::LINK_NEXT); 544 ASSERT_TRUE(next_link); 545 // Keep the next url before releasing the |resource_list|. 546 GURL next_url(next_link->href()); 547 548 error = GDATA_OTHER_ERROR; 549 resource_list.reset(); 550 fake_service_.GetRemainingChangeList( 551 next_url, 552 test_util::CreateCopyResultCallback(&error, &resource_list)); 553 base::RunLoop().RunUntilIdle(); 554 555 EXPECT_EQ(HTTP_SUCCESS, error); 556 ASSERT_TRUE(resource_list); 557 558 EXPECT_EQ(6U, resource_list->entries().size()); 559 EXPECT_EQ(1, fake_service_.resource_list_load_count()); 560 561 // Third page loading. 562 next_link = resource_list->GetLinkByType(Link::LINK_NEXT); 563 ASSERT_TRUE(next_link); 564 next_url = GURL(next_link->href()); 565 566 error = GDATA_OTHER_ERROR; 567 resource_list.reset(); 568 fake_service_.GetRemainingChangeList( 569 next_url, 570 test_util::CreateCopyResultCallback(&error, &resource_list)); 571 base::RunLoop().RunUntilIdle(); 572 573 EXPECT_EQ(HTTP_SUCCESS, error); 574 ASSERT_TRUE(resource_list); 575 576 EXPECT_EQ(2U, resource_list->entries().size()); 577 EXPECT_EQ(1, fake_service_.resource_list_load_count()); 578 } 579 580 TEST_F(FakeDriveServiceTest, 581 GetRemainingFileList_GetResourceListInDirectory) { 582 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 583 "gdata/root_feed.json")); 584 fake_service_.set_default_max_results(3); 585 586 GDataErrorCode error = GDATA_OTHER_ERROR; 587 scoped_ptr<ResourceList> resource_list; 588 fake_service_.GetResourceListInDirectory( 589 fake_service_.GetRootResourceId(), 590 test_util::CreateCopyResultCallback(&error, &resource_list)); 591 base::RunLoop().RunUntilIdle(); 592 EXPECT_EQ(HTTP_SUCCESS, error); 593 ASSERT_TRUE(resource_list); 594 595 // Do some sanity check. 596 // The number of results is 8 entries. Thus, it should split into three 597 // chunks: 3, 3, and then 2. 598 EXPECT_EQ(3U, resource_list->entries().size()); 599 EXPECT_EQ(1, fake_service_.directory_load_count()); 600 601 // Second page loading. 602 const google_apis::Link* next_link = 603 resource_list->GetLinkByType(Link::LINK_NEXT); 604 ASSERT_TRUE(next_link); 605 // Keep the next url before releasing the |resource_list|. 606 GURL next_url(next_link->href()); 607 608 error = GDATA_OTHER_ERROR; 609 resource_list.reset(); 610 fake_service_.GetRemainingFileList( 611 next_url, 612 test_util::CreateCopyResultCallback(&error, &resource_list)); 613 base::RunLoop().RunUntilIdle(); 614 615 EXPECT_EQ(HTTP_SUCCESS, error); 616 ASSERT_TRUE(resource_list); 617 618 EXPECT_EQ(3U, resource_list->entries().size()); 619 EXPECT_EQ(1, fake_service_.directory_load_count()); 620 621 // Third page loading. 622 next_link = resource_list->GetLinkByType(Link::LINK_NEXT); 623 ASSERT_TRUE(next_link); 624 next_url = GURL(next_link->href()); 625 626 error = GDATA_OTHER_ERROR; 627 resource_list.reset(); 628 fake_service_.GetRemainingFileList( 629 next_url, 630 test_util::CreateCopyResultCallback(&error, &resource_list)); 631 base::RunLoop().RunUntilIdle(); 632 633 EXPECT_EQ(HTTP_SUCCESS, error); 634 ASSERT_TRUE(resource_list); 635 636 EXPECT_EQ(2U, resource_list->entries().size()); 637 EXPECT_EQ(1, fake_service_.directory_load_count()); 638 } 639 640 TEST_F(FakeDriveServiceTest, GetRemainingFileList_Search) { 641 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 642 "gdata/root_feed.json")); 643 fake_service_.set_default_max_results(2); 644 645 GDataErrorCode error = GDATA_OTHER_ERROR; 646 scoped_ptr<ResourceList> resource_list; 647 fake_service_.Search( 648 "File", // search_query 649 test_util::CreateCopyResultCallback(&error, &resource_list)); 650 base::RunLoop().RunUntilIdle(); 651 EXPECT_EQ(HTTP_SUCCESS, error); 652 ASSERT_TRUE(resource_list); 653 654 // Do some sanity check. 655 // The number of results is 4 entries. Thus, it should split into two 656 // chunks: 2, and then 2 657 EXPECT_EQ(2U, resource_list->entries().size()); 658 659 // Second page loading. 660 const google_apis::Link* next_link = 661 resource_list->GetLinkByType(Link::LINK_NEXT); 662 ASSERT_TRUE(next_link); 663 // Keep the next url before releasing the |resource_list|. 664 GURL next_url(next_link->href()); 665 666 error = GDATA_OTHER_ERROR; 667 resource_list.reset(); 668 fake_service_.GetRemainingFileList( 669 next_url, 670 test_util::CreateCopyResultCallback(&error, &resource_list)); 671 base::RunLoop().RunUntilIdle(); 672 673 EXPECT_EQ(HTTP_SUCCESS, error); 674 ASSERT_TRUE(resource_list); 675 676 EXPECT_EQ(2U, resource_list->entries().size()); 677 } 678 679 TEST_F(FakeDriveServiceTest, GetRemainingChangeList_GetChangeList) { 680 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 681 "gdata/root_feed.json")); 682 fake_service_.set_default_max_results(2); 683 684 // Load the account_metadata.json as well to add the largest changestamp 685 // (654321) to the existing entries. 686 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 687 "gdata/account_metadata.json")); 688 // Add 5 new directory in the root directory. The new directory will have 689 // the changestamp of 654326. 690 for (int i = 0; i < 5; ++i) { 691 ASSERT_TRUE(AddNewDirectory( 692 fake_service_.GetRootResourceId(), 693 base::StringPrintf("new directory %d", i))); 694 } 695 696 GDataErrorCode error = GDATA_OTHER_ERROR; 697 scoped_ptr<ResourceList> resource_list; 698 fake_service_.GetChangeList( 699 654321 + 1, // start_changestamp 700 test_util::CreateCopyResultCallback(&error, &resource_list)); 701 base::RunLoop().RunUntilIdle(); 702 EXPECT_EQ(HTTP_SUCCESS, error); 703 ASSERT_TRUE(resource_list); 704 705 // Do some sanity check. 706 // The number of results is 5 entries. Thus, it should split into three 707 // chunks: 2, 2 and then 1. 708 EXPECT_EQ(2U, resource_list->entries().size()); 709 EXPECT_EQ(1, fake_service_.change_list_load_count()); 710 711 // Second page loading. 712 const google_apis::Link* next_link = 713 resource_list->GetLinkByType(Link::LINK_NEXT); 714 ASSERT_TRUE(next_link); 715 // Keep the next url before releasing the |resource_list|. 716 GURL next_url(next_link->href()); 717 718 error = GDATA_OTHER_ERROR; 719 resource_list.reset(); 720 fake_service_.GetRemainingChangeList( 721 next_url, 722 test_util::CreateCopyResultCallback(&error, &resource_list)); 723 base::RunLoop().RunUntilIdle(); 724 725 EXPECT_EQ(HTTP_SUCCESS, error); 726 ASSERT_TRUE(resource_list); 727 728 EXPECT_EQ(2U, resource_list->entries().size()); 729 EXPECT_EQ(1, fake_service_.change_list_load_count()); 730 731 // Third page loading. 732 next_link = resource_list->GetLinkByType(Link::LINK_NEXT); 733 ASSERT_TRUE(next_link); 734 next_url = GURL(next_link->href()); 735 736 error = GDATA_OTHER_ERROR; 737 resource_list.reset(); 738 fake_service_.GetRemainingChangeList( 739 next_url, 740 test_util::CreateCopyResultCallback(&error, &resource_list)); 741 base::RunLoop().RunUntilIdle(); 742 743 EXPECT_EQ(HTTP_SUCCESS, error); 744 ASSERT_TRUE(resource_list); 745 746 EXPECT_EQ(1U, resource_list->entries().size()); 747 EXPECT_EQ(1, fake_service_.change_list_load_count()); 748 } 749 750 TEST_F(FakeDriveServiceTest, GetAboutResource) { 751 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 752 "gdata/account_metadata.json")); 753 754 GDataErrorCode error = GDATA_OTHER_ERROR; 755 scoped_ptr<AboutResource> about_resource; 756 fake_service_.GetAboutResource( 757 test_util::CreateCopyResultCallback(&error, &about_resource)); 758 base::RunLoop().RunUntilIdle(); 759 760 EXPECT_EQ(HTTP_SUCCESS, error); 761 762 ASSERT_TRUE(about_resource); 763 // Do some sanity check. 764 EXPECT_EQ(fake_service_.GetRootResourceId(), 765 about_resource->root_folder_id()); 766 EXPECT_EQ(1, fake_service_.about_resource_load_count()); 767 } 768 769 TEST_F(FakeDriveServiceTest, GetAboutResource_Offline) { 770 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 771 "gdata/account_metadata.json")); 772 fake_service_.set_offline(true); 773 774 GDataErrorCode error = GDATA_OTHER_ERROR; 775 scoped_ptr<AboutResource> about_resource; 776 fake_service_.GetAboutResource( 777 test_util::CreateCopyResultCallback(&error, &about_resource)); 778 base::RunLoop().RunUntilIdle(); 779 780 EXPECT_EQ(GDATA_NO_CONNECTION, error); 781 EXPECT_FALSE(about_resource); 782 } 783 784 TEST_F(FakeDriveServiceTest, GetAppList) { 785 ASSERT_TRUE(fake_service_.LoadAppListForDriveApi( 786 "drive/applist.json")); 787 788 GDataErrorCode error = GDATA_OTHER_ERROR; 789 scoped_ptr<AppList> app_list; 790 fake_service_.GetAppList( 791 test_util::CreateCopyResultCallback(&error, &app_list)); 792 base::RunLoop().RunUntilIdle(); 793 794 EXPECT_EQ(HTTP_SUCCESS, error); 795 796 ASSERT_TRUE(app_list); 797 EXPECT_EQ(1, fake_service_.app_list_load_count()); 798 } 799 800 TEST_F(FakeDriveServiceTest, GetAppList_Offline) { 801 ASSERT_TRUE(fake_service_.LoadAppListForDriveApi( 802 "drive/applist.json")); 803 fake_service_.set_offline(true); 804 805 GDataErrorCode error = GDATA_OTHER_ERROR; 806 scoped_ptr<AppList> app_list; 807 fake_service_.GetAppList( 808 test_util::CreateCopyResultCallback(&error, &app_list)); 809 base::RunLoop().RunUntilIdle(); 810 811 EXPECT_EQ(GDATA_NO_CONNECTION, error); 812 EXPECT_FALSE(app_list); 813 } 814 815 TEST_F(FakeDriveServiceTest, GetResourceEntry_ExistingFile) { 816 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 817 "gdata/root_feed.json")); 818 819 const std::string kResourceId = "file:2_file_resource_id"; 820 GDataErrorCode error = GDATA_OTHER_ERROR; 821 scoped_ptr<ResourceEntry> resource_entry; 822 fake_service_.GetResourceEntry( 823 kResourceId, 824 test_util::CreateCopyResultCallback(&error, &resource_entry)); 825 base::RunLoop().RunUntilIdle(); 826 827 EXPECT_EQ(HTTP_SUCCESS, error); 828 ASSERT_TRUE(resource_entry); 829 // Do some sanity check. 830 EXPECT_EQ(kResourceId, resource_entry->resource_id()); 831 } 832 833 TEST_F(FakeDriveServiceTest, GetResourceEntry_NonexistingFile) { 834 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 835 "gdata/root_feed.json")); 836 837 const std::string kResourceId = "file:nonexisting_resource_id"; 838 GDataErrorCode error = GDATA_OTHER_ERROR; 839 scoped_ptr<ResourceEntry> resource_entry; 840 fake_service_.GetResourceEntry( 841 kResourceId, 842 test_util::CreateCopyResultCallback(&error, &resource_entry)); 843 base::RunLoop().RunUntilIdle(); 844 845 EXPECT_EQ(HTTP_NOT_FOUND, error); 846 ASSERT_FALSE(resource_entry); 847 } 848 849 TEST_F(FakeDriveServiceTest, GetResourceEntry_Offline) { 850 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 851 "gdata/root_feed.json")); 852 fake_service_.set_offline(true); 853 854 const std::string kResourceId = "file:2_file_resource_id"; 855 GDataErrorCode error = GDATA_OTHER_ERROR; 856 scoped_ptr<ResourceEntry> resource_entry; 857 fake_service_.GetResourceEntry( 858 kResourceId, 859 test_util::CreateCopyResultCallback(&error, &resource_entry)); 860 base::RunLoop().RunUntilIdle(); 861 862 EXPECT_EQ(GDATA_NO_CONNECTION, error); 863 EXPECT_FALSE(resource_entry); 864 } 865 866 TEST_F(FakeDriveServiceTest, GetShareUrl) { 867 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 868 "gdata/root_feed.json")); 869 870 const std::string kResourceId = "file:2_file_resource_id"; 871 GDataErrorCode error = GDATA_OTHER_ERROR; 872 GURL share_url; 873 fake_service_.GetShareUrl( 874 kResourceId, 875 GURL(), // embed origin 876 test_util::CreateCopyResultCallback(&error, &share_url)); 877 base::RunLoop().RunUntilIdle(); 878 879 EXPECT_EQ(HTTP_SUCCESS, error); 880 EXPECT_FALSE(share_url.is_empty()); 881 } 882 883 TEST_F(FakeDriveServiceTest, DeleteResource_ExistingFile) { 884 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 885 "gdata/root_feed.json")); 886 887 // Resource "file:2_file_resource_id" should now exist. 888 ASSERT_TRUE(Exists("file:2_file_resource_id")); 889 890 GDataErrorCode error = GDATA_OTHER_ERROR; 891 fake_service_.DeleteResource("file:2_file_resource_id", 892 std::string(), // etag 893 test_util::CreateCopyResultCallback(&error)); 894 base::RunLoop().RunUntilIdle(); 895 896 EXPECT_EQ(HTTP_NO_CONTENT, error); 897 // Resource "file:2_file_resource_id" should be gone now. 898 EXPECT_FALSE(Exists("file:2_file_resource_id")); 899 900 error = GDATA_OTHER_ERROR; 901 fake_service_.DeleteResource("file:2_file_resource_id", 902 std::string(), // etag 903 test_util::CreateCopyResultCallback(&error)); 904 base::RunLoop().RunUntilIdle(); 905 EXPECT_EQ(HTTP_NOT_FOUND, error); 906 EXPECT_FALSE(Exists("file:2_file_resource_id")); 907 } 908 909 TEST_F(FakeDriveServiceTest, DeleteResource_NonexistingFile) { 910 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 911 "gdata/root_feed.json")); 912 913 GDataErrorCode error = GDATA_OTHER_ERROR; 914 fake_service_.DeleteResource("file:nonexisting_resource_id", 915 std::string(), // etag 916 test_util::CreateCopyResultCallback(&error)); 917 base::RunLoop().RunUntilIdle(); 918 919 EXPECT_EQ(HTTP_NOT_FOUND, error); 920 } 921 922 TEST_F(FakeDriveServiceTest, DeleteResource_ETagMatch) { 923 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 924 "gdata/root_feed.json")); 925 926 // Resource "file:2_file_resource_id" should now exist. 927 scoped_ptr<ResourceEntry> entry = FindEntry("file:2_file_resource_id"); 928 ASSERT_TRUE(entry); 929 ASSERT_FALSE(entry->deleted()); 930 ASSERT_FALSE(entry->etag().empty()); 931 932 GDataErrorCode error = GDATA_OTHER_ERROR; 933 fake_service_.DeleteResource("file:2_file_resource_id", 934 entry->etag() + "_mismatch", 935 test_util::CreateCopyResultCallback(&error)); 936 base::RunLoop().RunUntilIdle(); 937 938 EXPECT_EQ(HTTP_PRECONDITION, error); 939 // Resource "file:2_file_resource_id" should still exist. 940 EXPECT_TRUE(Exists("file:2_file_resource_id")); 941 942 error = GDATA_OTHER_ERROR; 943 fake_service_.DeleteResource("file:2_file_resource_id", 944 entry->etag(), 945 test_util::CreateCopyResultCallback(&error)); 946 base::RunLoop().RunUntilIdle(); 947 EXPECT_EQ(HTTP_NO_CONTENT, error); 948 // Resource "file:2_file_resource_id" should be gone now. 949 EXPECT_FALSE(Exists("file:2_file_resource_id")); 950 } 951 952 TEST_F(FakeDriveServiceTest, DeleteResource_Offline) { 953 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 954 "gdata/root_feed.json")); 955 fake_service_.set_offline(true); 956 957 GDataErrorCode error = GDATA_OTHER_ERROR; 958 fake_service_.DeleteResource("file:2_file_resource_id", 959 std::string(), // etag 960 test_util::CreateCopyResultCallback(&error)); 961 base::RunLoop().RunUntilIdle(); 962 963 EXPECT_EQ(GDATA_NO_CONNECTION, error); 964 } 965 966 TEST_F(FakeDriveServiceTest, TrashResource_ExistingFile) { 967 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 968 "gdata/root_feed.json")); 969 970 // Resource "file:2_file_resource_id" should now exist. 971 ASSERT_TRUE(Exists("file:2_file_resource_id")); 972 973 GDataErrorCode error = GDATA_OTHER_ERROR; 974 fake_service_.TrashResource("file:2_file_resource_id", 975 test_util::CreateCopyResultCallback(&error)); 976 base::RunLoop().RunUntilIdle(); 977 978 EXPECT_EQ(HTTP_SUCCESS, error); 979 // Resource "file:2_file_resource_id" should be gone now. 980 EXPECT_FALSE(Exists("file:2_file_resource_id")); 981 982 error = GDATA_OTHER_ERROR; 983 fake_service_.TrashResource("file:2_file_resource_id", 984 test_util::CreateCopyResultCallback(&error)); 985 base::RunLoop().RunUntilIdle(); 986 EXPECT_EQ(HTTP_NOT_FOUND, error); 987 EXPECT_FALSE(Exists("file:2_file_resource_id")); 988 } 989 990 TEST_F(FakeDriveServiceTest, TrashResource_NonexistingFile) { 991 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 992 "gdata/root_feed.json")); 993 994 GDataErrorCode error = GDATA_OTHER_ERROR; 995 fake_service_.TrashResource("file:nonexisting_resource_id", 996 test_util::CreateCopyResultCallback(&error)); 997 base::RunLoop().RunUntilIdle(); 998 999 EXPECT_EQ(HTTP_NOT_FOUND, error); 1000 } 1001 1002 TEST_F(FakeDriveServiceTest, TrashResource_Offline) { 1003 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1004 "gdata/root_feed.json")); 1005 fake_service_.set_offline(true); 1006 1007 GDataErrorCode error = GDATA_OTHER_ERROR; 1008 fake_service_.TrashResource("file:2_file_resource_id", 1009 test_util::CreateCopyResultCallback(&error)); 1010 base::RunLoop().RunUntilIdle(); 1011 1012 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1013 } 1014 1015 TEST_F(FakeDriveServiceTest, DownloadFile_ExistingFile) { 1016 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1017 "gdata/root_feed.json")); 1018 1019 base::ScopedTempDir temp_dir; 1020 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1021 1022 std::vector<test_util::ProgressInfo> download_progress_values; 1023 1024 const base::FilePath kOutputFilePath = 1025 temp_dir.path().AppendASCII("whatever.txt"); 1026 GDataErrorCode error = GDATA_OTHER_ERROR; 1027 base::FilePath output_file_path; 1028 test_util::TestGetContentCallback get_content_callback; 1029 fake_service_.DownloadFile( 1030 kOutputFilePath, 1031 "file:2_file_resource_id", 1032 test_util::CreateCopyResultCallback(&error, &output_file_path), 1033 get_content_callback.callback(), 1034 base::Bind(&test_util::AppendProgressCallbackResult, 1035 &download_progress_values)); 1036 base::RunLoop().RunUntilIdle(); 1037 1038 EXPECT_EQ(HTTP_SUCCESS, error); 1039 EXPECT_EQ(output_file_path, kOutputFilePath); 1040 std::string content; 1041 ASSERT_TRUE(base::ReadFileToString(output_file_path, &content)); 1042 // The content is "x"s of the file size specified in root_feed.json. 1043 EXPECT_EQ("This is some test content.", content); 1044 ASSERT_TRUE(!download_progress_values.empty()); 1045 EXPECT_TRUE(base::STLIsSorted(download_progress_values)); 1046 EXPECT_LE(0, download_progress_values.front().first); 1047 EXPECT_GE(26, download_progress_values.back().first); 1048 EXPECT_EQ(content, get_content_callback.GetConcatenatedData()); 1049 } 1050 1051 TEST_F(FakeDriveServiceTest, DownloadFile_NonexistingFile) { 1052 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1053 "gdata/root_feed.json")); 1054 1055 base::ScopedTempDir temp_dir; 1056 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1057 1058 const base::FilePath kOutputFilePath = 1059 temp_dir.path().AppendASCII("whatever.txt"); 1060 GDataErrorCode error = GDATA_OTHER_ERROR; 1061 base::FilePath output_file_path; 1062 fake_service_.DownloadFile( 1063 kOutputFilePath, 1064 "file:non_existent_file_resource_id", 1065 test_util::CreateCopyResultCallback(&error, &output_file_path), 1066 GetContentCallback(), 1067 ProgressCallback()); 1068 base::RunLoop().RunUntilIdle(); 1069 1070 EXPECT_EQ(HTTP_NOT_FOUND, error); 1071 } 1072 1073 TEST_F(FakeDriveServiceTest, DownloadFile_Offline) { 1074 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1075 "gdata/root_feed.json")); 1076 fake_service_.set_offline(true); 1077 1078 base::ScopedTempDir temp_dir; 1079 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1080 1081 const base::FilePath kOutputFilePath = 1082 temp_dir.path().AppendASCII("whatever.txt"); 1083 GDataErrorCode error = GDATA_OTHER_ERROR; 1084 base::FilePath output_file_path; 1085 fake_service_.DownloadFile( 1086 kOutputFilePath, 1087 "file:2_file_resource_id", 1088 test_util::CreateCopyResultCallback(&error, &output_file_path), 1089 GetContentCallback(), 1090 ProgressCallback()); 1091 base::RunLoop().RunUntilIdle(); 1092 1093 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1094 } 1095 1096 TEST_F(FakeDriveServiceTest, CopyResource) { 1097 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123}; 1098 1099 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1100 "gdata/root_feed.json")); 1101 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1102 "gdata/account_metadata.json")); 1103 1104 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1105 1106 const std::string kResourceId = "file:2_file_resource_id"; 1107 const std::string kParentResourceId = "folder:2_folder_resource_id"; 1108 GDataErrorCode error = GDATA_OTHER_ERROR; 1109 scoped_ptr<ResourceEntry> resource_entry; 1110 fake_service_.CopyResource( 1111 kResourceId, 1112 kParentResourceId, 1113 "new title", 1114 base::Time::FromUTCExploded(kModifiedDate), 1115 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1116 base::RunLoop().RunUntilIdle(); 1117 1118 EXPECT_EQ(HTTP_SUCCESS, error); 1119 ASSERT_TRUE(resource_entry); 1120 // The copied entry should have the new resource ID and the title. 1121 EXPECT_EQ(kResourceId + "_copied", resource_entry->resource_id()); 1122 EXPECT_EQ("new title", resource_entry->title()); 1123 EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate), 1124 resource_entry->updated_time()); 1125 EXPECT_TRUE(HasParent(resource_entry->resource_id(), kParentResourceId)); 1126 // Should be incremented as a new hosted document was created. 1127 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1128 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1129 } 1130 1131 TEST_F(FakeDriveServiceTest, CopyResource_NonExisting) { 1132 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1133 "gdata/root_feed.json")); 1134 1135 const std::string kResourceId = "document:nonexisting_resource_id"; 1136 GDataErrorCode error = GDATA_OTHER_ERROR; 1137 scoped_ptr<ResourceEntry> resource_entry; 1138 fake_service_.CopyResource( 1139 kResourceId, 1140 "folder:1_folder_resource_id", 1141 "new title", 1142 base::Time(), 1143 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1144 base::RunLoop().RunUntilIdle(); 1145 1146 EXPECT_EQ(HTTP_NOT_FOUND, error); 1147 } 1148 1149 TEST_F(FakeDriveServiceTest, CopyResource_EmptyParentResourceId) { 1150 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1151 "gdata/root_feed.json")); 1152 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1153 "gdata/account_metadata.json")); 1154 1155 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1156 1157 const std::string kResourceId = "file:2_file_resource_id"; 1158 GDataErrorCode error = GDATA_OTHER_ERROR; 1159 scoped_ptr<ResourceEntry> resource_entry; 1160 fake_service_.CopyResource( 1161 kResourceId, 1162 std::string(), 1163 "new title", 1164 base::Time(), 1165 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1166 base::RunLoop().RunUntilIdle(); 1167 1168 EXPECT_EQ(HTTP_SUCCESS, error); 1169 ASSERT_TRUE(resource_entry); 1170 // The copied entry should have the new resource ID and the title. 1171 EXPECT_EQ(kResourceId + "_copied", resource_entry->resource_id()); 1172 EXPECT_EQ("new title", resource_entry->title()); 1173 EXPECT_TRUE(HasParent(kResourceId, fake_service_.GetRootResourceId())); 1174 // Should be incremented as a new hosted document was created. 1175 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1176 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1177 } 1178 1179 TEST_F(FakeDriveServiceTest, CopyResource_Offline) { 1180 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1181 "gdata/root_feed.json")); 1182 fake_service_.set_offline(true); 1183 1184 const std::string kResourceId = "file:2_file_resource_id"; 1185 GDataErrorCode error = GDATA_OTHER_ERROR; 1186 scoped_ptr<ResourceEntry> resource_entry; 1187 fake_service_.CopyResource( 1188 kResourceId, 1189 "folder:1_folder_resource_id", 1190 "new title", 1191 base::Time(), 1192 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1193 base::RunLoop().RunUntilIdle(); 1194 1195 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1196 EXPECT_FALSE(resource_entry); 1197 } 1198 1199 TEST_F(FakeDriveServiceTest, UpdateResource) { 1200 const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123}; 1201 const base::Time::Exploded kViewedDate = {2013, 8, 1, 20, 16, 00, 14, 234}; 1202 1203 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1204 "gdata/root_feed.json")); 1205 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1206 "gdata/account_metadata.json")); 1207 1208 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1209 1210 const std::string kResourceId = "file:2_file_resource_id"; 1211 const std::string kParentResourceId = "folder:2_folder_resource_id"; 1212 GDataErrorCode error = GDATA_OTHER_ERROR; 1213 scoped_ptr<ResourceEntry> resource_entry; 1214 fake_service_.UpdateResource( 1215 kResourceId, 1216 kParentResourceId, 1217 "new title", 1218 base::Time::FromUTCExploded(kModifiedDate), 1219 base::Time::FromUTCExploded(kViewedDate), 1220 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1221 base::RunLoop().RunUntilIdle(); 1222 1223 EXPECT_EQ(HTTP_SUCCESS, error); 1224 ASSERT_TRUE(resource_entry); 1225 // The copied entry should have the new resource ID and the title. 1226 EXPECT_EQ(kResourceId, resource_entry->resource_id()); 1227 EXPECT_EQ("new title", resource_entry->title()); 1228 EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate), 1229 resource_entry->updated_time()); 1230 EXPECT_EQ(base::Time::FromUTCExploded(kViewedDate), 1231 resource_entry->last_viewed_time()); 1232 EXPECT_TRUE(HasParent(kResourceId, kParentResourceId)); 1233 // Should be incremented as a new hosted document was created. 1234 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1235 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1236 } 1237 1238 TEST_F(FakeDriveServiceTest, UpdateResource_NonExisting) { 1239 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1240 "gdata/root_feed.json")); 1241 1242 const std::string kResourceId = "document:nonexisting_resource_id"; 1243 GDataErrorCode error = GDATA_OTHER_ERROR; 1244 scoped_ptr<ResourceEntry> resource_entry; 1245 fake_service_.UpdateResource( 1246 kResourceId, 1247 "folder:1_folder_resource_id", 1248 "new title", 1249 base::Time(), 1250 base::Time(), 1251 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1252 base::RunLoop().RunUntilIdle(); 1253 1254 EXPECT_EQ(HTTP_NOT_FOUND, error); 1255 } 1256 1257 TEST_F(FakeDriveServiceTest, UpdateResource_EmptyParentResourceId) { 1258 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1259 "gdata/root_feed.json")); 1260 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1261 "gdata/account_metadata.json")); 1262 1263 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1264 1265 const std::string kResourceId = "file:2_file_resource_id"; 1266 1267 // Just make sure that the resource is under root. 1268 ASSERT_TRUE(HasParent(kResourceId, "fake_root")); 1269 1270 GDataErrorCode error = GDATA_OTHER_ERROR; 1271 scoped_ptr<ResourceEntry> resource_entry; 1272 fake_service_.UpdateResource( 1273 kResourceId, 1274 std::string(), 1275 "new title", 1276 base::Time(), 1277 base::Time(), 1278 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1279 base::RunLoop().RunUntilIdle(); 1280 1281 EXPECT_EQ(HTTP_SUCCESS, error); 1282 ASSERT_TRUE(resource_entry); 1283 // The copied entry should have the new resource ID and the title. 1284 EXPECT_EQ(kResourceId, resource_entry->resource_id()); 1285 EXPECT_EQ("new title", resource_entry->title()); 1286 EXPECT_TRUE(HasParent(kResourceId, "fake_root")); 1287 // Should be incremented as a new hosted document was created. 1288 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1289 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1290 } 1291 1292 TEST_F(FakeDriveServiceTest, UpdateResource_Offline) { 1293 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1294 "gdata/root_feed.json")); 1295 fake_service_.set_offline(true); 1296 1297 const std::string kResourceId = "file:2_file_resource_id"; 1298 GDataErrorCode error = GDATA_OTHER_ERROR; 1299 scoped_ptr<ResourceEntry> resource_entry; 1300 fake_service_.UpdateResource( 1301 kResourceId, 1302 std::string(), 1303 "new title", 1304 base::Time(), 1305 base::Time(), 1306 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1307 base::RunLoop().RunUntilIdle(); 1308 1309 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1310 EXPECT_FALSE(resource_entry); 1311 } 1312 1313 TEST_F(FakeDriveServiceTest, RenameResource_ExistingFile) { 1314 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1315 "gdata/root_feed.json")); 1316 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1317 "gdata/account_metadata.json")); 1318 1319 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1320 1321 const std::string kResourceId = "file:2_file_resource_id"; 1322 1323 GDataErrorCode error = GDATA_OTHER_ERROR; 1324 fake_service_.RenameResource(kResourceId, 1325 "new title", 1326 test_util::CreateCopyResultCallback(&error)); 1327 base::RunLoop().RunUntilIdle(); 1328 1329 EXPECT_EQ(HTTP_SUCCESS, error); 1330 1331 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId); 1332 ASSERT_TRUE(resource_entry); 1333 EXPECT_EQ("new title", resource_entry->title()); 1334 // Should be incremented as a file was renamed. 1335 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1336 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1337 } 1338 1339 TEST_F(FakeDriveServiceTest, RenameResource_NonexistingFile) { 1340 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1341 "gdata/root_feed.json")); 1342 1343 const std::string kResourceId = "file:nonexisting_file"; 1344 1345 GDataErrorCode error = GDATA_OTHER_ERROR; 1346 fake_service_.RenameResource(kResourceId, 1347 "new title", 1348 test_util::CreateCopyResultCallback(&error)); 1349 base::RunLoop().RunUntilIdle(); 1350 1351 EXPECT_EQ(HTTP_NOT_FOUND, error); 1352 } 1353 1354 TEST_F(FakeDriveServiceTest, RenameResource_Offline) { 1355 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1356 "gdata/root_feed.json")); 1357 fake_service_.set_offline(true); 1358 1359 const std::string kResourceId = "file:2_file_resource_id"; 1360 1361 GDataErrorCode error = GDATA_OTHER_ERROR; 1362 fake_service_.RenameResource(kResourceId, 1363 "new title", 1364 test_util::CreateCopyResultCallback(&error)); 1365 base::RunLoop().RunUntilIdle(); 1366 1367 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1368 } 1369 1370 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInRootDirectory) { 1371 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1372 "gdata/root_feed.json")); 1373 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1374 "gdata/account_metadata.json")); 1375 1376 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1377 1378 const std::string kResourceId = "file:2_file_resource_id"; 1379 const std::string kOldParentResourceId = fake_service_.GetRootResourceId(); 1380 const std::string kNewParentResourceId = "folder:1_folder_resource_id"; 1381 1382 // Here's the original parent link. 1383 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId)); 1384 EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId)); 1385 1386 GDataErrorCode error = GDATA_OTHER_ERROR; 1387 fake_service_.AddResourceToDirectory( 1388 kNewParentResourceId, 1389 kResourceId, 1390 test_util::CreateCopyResultCallback(&error)); 1391 base::RunLoop().RunUntilIdle(); 1392 1393 EXPECT_EQ(HTTP_SUCCESS, error); 1394 1395 // The parent link should now be changed. 1396 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId)); 1397 EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId)); 1398 // Should be incremented as a file was moved. 1399 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1400 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1401 } 1402 1403 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInNonRootDirectory) { 1404 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1405 "gdata/root_feed.json")); 1406 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1407 "gdata/account_metadata.json")); 1408 1409 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1410 1411 const std::string kResourceId = "file:subdirectory_file_1_id"; 1412 const std::string kOldParentResourceId = "folder:1_folder_resource_id"; 1413 const std::string kNewParentResourceId = "folder:2_folder_resource_id"; 1414 1415 // Here's the original parent link. 1416 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId)); 1417 EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId)); 1418 1419 GDataErrorCode error = GDATA_OTHER_ERROR; 1420 fake_service_.AddResourceToDirectory( 1421 kNewParentResourceId, 1422 kResourceId, 1423 test_util::CreateCopyResultCallback(&error)); 1424 base::RunLoop().RunUntilIdle(); 1425 1426 EXPECT_EQ(HTTP_SUCCESS, error); 1427 1428 // The parent link should now be changed. 1429 EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId)); 1430 EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId)); 1431 // Should be incremented as a file was moved. 1432 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1433 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1434 } 1435 1436 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_NonexistingFile) { 1437 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1438 "gdata/root_feed.json")); 1439 1440 const std::string kResourceId = "file:nonexisting_file"; 1441 const std::string kNewParentResourceId = "folder:1_folder_resource_id"; 1442 1443 GDataErrorCode error = GDATA_OTHER_ERROR; 1444 fake_service_.AddResourceToDirectory( 1445 kNewParentResourceId, 1446 kResourceId, 1447 test_util::CreateCopyResultCallback(&error)); 1448 base::RunLoop().RunUntilIdle(); 1449 1450 EXPECT_EQ(HTTP_NOT_FOUND, error); 1451 } 1452 1453 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_OrphanFile) { 1454 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1455 "gdata/root_feed.json")); 1456 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1457 "gdata/account_metadata.json")); 1458 1459 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1460 1461 const std::string kResourceId = "file:1_orphanfile_resource_id"; 1462 const std::string kNewParentResourceId = "folder:1_folder_resource_id"; 1463 1464 // The file does not belong to any directory, even to the root. 1465 EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId)); 1466 EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId())); 1467 1468 GDataErrorCode error = GDATA_OTHER_ERROR; 1469 fake_service_.AddResourceToDirectory( 1470 kNewParentResourceId, 1471 kResourceId, 1472 test_util::CreateCopyResultCallback(&error)); 1473 base::RunLoop().RunUntilIdle(); 1474 1475 EXPECT_EQ(HTTP_SUCCESS, error); 1476 1477 // The parent link should now be changed. 1478 EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId)); 1479 EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId())); 1480 // Should be incremented as a file was moved. 1481 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1482 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1483 } 1484 1485 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_Offline) { 1486 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1487 "gdata/root_feed.json")); 1488 fake_service_.set_offline(true); 1489 1490 const std::string kResourceId = "file:2_file_resource_id"; 1491 const std::string kNewParentResourceId = "folder:1_folder_resource_id"; 1492 1493 GDataErrorCode error = GDATA_OTHER_ERROR; 1494 fake_service_.AddResourceToDirectory( 1495 kNewParentResourceId, 1496 kResourceId, 1497 test_util::CreateCopyResultCallback(&error)); 1498 base::RunLoop().RunUntilIdle(); 1499 1500 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1501 } 1502 1503 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_ExistingFile) { 1504 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1505 "gdata/root_feed.json")); 1506 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1507 "gdata/account_metadata.json")); 1508 1509 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1510 1511 const std::string kResourceId = "file:subdirectory_file_1_id"; 1512 const std::string kParentResourceId = "folder:1_folder_resource_id"; 1513 1514 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId); 1515 ASSERT_TRUE(resource_entry); 1516 // The parent link should exist now. 1517 const google_apis::Link* parent_link = 1518 resource_entry->GetLinkByType(Link::LINK_PARENT); 1519 ASSERT_TRUE(parent_link); 1520 1521 GDataErrorCode error = GDATA_OTHER_ERROR; 1522 fake_service_.RemoveResourceFromDirectory( 1523 kParentResourceId, 1524 kResourceId, 1525 test_util::CreateCopyResultCallback(&error)); 1526 base::RunLoop().RunUntilIdle(); 1527 1528 EXPECT_EQ(HTTP_NO_CONTENT, error); 1529 1530 resource_entry = FindEntry(kResourceId); 1531 ASSERT_TRUE(resource_entry); 1532 // The parent link should be gone now. 1533 parent_link = resource_entry->GetLinkByType(Link::LINK_PARENT); 1534 ASSERT_FALSE(parent_link); 1535 // Should be incremented as a file was moved to the root directory. 1536 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1537 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1538 } 1539 1540 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_NonexistingFile) { 1541 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1542 "gdata/root_feed.json")); 1543 1544 const std::string kResourceId = "file:nonexisting_file"; 1545 const std::string kParentResourceId = "folder:1_folder_resource_id"; 1546 1547 GDataErrorCode error = GDATA_OTHER_ERROR; 1548 fake_service_.RemoveResourceFromDirectory( 1549 kParentResourceId, 1550 kResourceId, 1551 test_util::CreateCopyResultCallback(&error)); 1552 base::RunLoop().RunUntilIdle(); 1553 1554 EXPECT_EQ(HTTP_NOT_FOUND, error); 1555 } 1556 1557 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_OrphanFile) { 1558 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1559 "gdata/root_feed.json")); 1560 1561 const std::string kResourceId = "file:1_orphanfile_resource_id"; 1562 const std::string kParentResourceId = fake_service_.GetRootResourceId(); 1563 1564 GDataErrorCode error = GDATA_OTHER_ERROR; 1565 fake_service_.RemoveResourceFromDirectory( 1566 kParentResourceId, 1567 kResourceId, 1568 test_util::CreateCopyResultCallback(&error)); 1569 base::RunLoop().RunUntilIdle(); 1570 1571 EXPECT_EQ(HTTP_NOT_FOUND, error); 1572 } 1573 1574 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_Offline) { 1575 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1576 "gdata/root_feed.json")); 1577 fake_service_.set_offline(true); 1578 1579 const std::string kResourceId = "file:subdirectory_file_1_id"; 1580 const std::string kParentResourceId = "folder:1_folder_resource_id"; 1581 1582 GDataErrorCode error = GDATA_OTHER_ERROR; 1583 fake_service_.RemoveResourceFromDirectory( 1584 kParentResourceId, 1585 kResourceId, 1586 test_util::CreateCopyResultCallback(&error)); 1587 base::RunLoop().RunUntilIdle(); 1588 1589 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1590 } 1591 1592 TEST_F(FakeDriveServiceTest, AddNewDirectory_EmptyParent) { 1593 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1594 "gdata/root_feed.json")); 1595 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1596 "gdata/account_metadata.json")); 1597 1598 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1599 1600 GDataErrorCode error = GDATA_OTHER_ERROR; 1601 scoped_ptr<ResourceEntry> resource_entry; 1602 fake_service_.AddNewDirectory( 1603 std::string(), 1604 "new directory", 1605 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1606 base::RunLoop().RunUntilIdle(); 1607 1608 EXPECT_EQ(HTTP_CREATED, error); 1609 ASSERT_TRUE(resource_entry); 1610 EXPECT_TRUE(resource_entry->is_folder()); 1611 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 1612 EXPECT_EQ("new directory", resource_entry->title()); 1613 EXPECT_TRUE(HasParent(resource_entry->resource_id(), 1614 fake_service_.GetRootResourceId())); 1615 // Should be incremented as a new directory was created. 1616 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1617 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1618 } 1619 1620 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectory) { 1621 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1622 "gdata/root_feed.json")); 1623 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1624 "gdata/account_metadata.json")); 1625 1626 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1627 1628 GDataErrorCode error = GDATA_OTHER_ERROR; 1629 scoped_ptr<ResourceEntry> resource_entry; 1630 fake_service_.AddNewDirectory( 1631 fake_service_.GetRootResourceId(), 1632 "new directory", 1633 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1634 base::RunLoop().RunUntilIdle(); 1635 1636 EXPECT_EQ(HTTP_CREATED, error); 1637 ASSERT_TRUE(resource_entry); 1638 EXPECT_TRUE(resource_entry->is_folder()); 1639 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 1640 EXPECT_EQ("new directory", resource_entry->title()); 1641 EXPECT_TRUE(HasParent(resource_entry->resource_id(), 1642 fake_service_.GetRootResourceId())); 1643 // Should be incremented as a new directory was created. 1644 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1645 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1646 } 1647 1648 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectoryOnEmptyFileSystem) { 1649 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1650 "gdata/empty_feed.json")); 1651 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1652 "gdata/account_metadata.json")); 1653 1654 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1655 1656 GDataErrorCode error = GDATA_OTHER_ERROR; 1657 scoped_ptr<ResourceEntry> resource_entry; 1658 fake_service_.AddNewDirectory( 1659 fake_service_.GetRootResourceId(), 1660 "new directory", 1661 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1662 base::RunLoop().RunUntilIdle(); 1663 1664 EXPECT_EQ(HTTP_CREATED, error); 1665 ASSERT_TRUE(resource_entry); 1666 EXPECT_TRUE(resource_entry->is_folder()); 1667 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 1668 EXPECT_EQ("new directory", resource_entry->title()); 1669 EXPECT_TRUE(HasParent(resource_entry->resource_id(), 1670 fake_service_.GetRootResourceId())); 1671 // Should be incremented as a new directory was created. 1672 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1673 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1674 } 1675 1676 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonRootDirectory) { 1677 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1678 "gdata/root_feed.json")); 1679 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 1680 "gdata/account_metadata.json")); 1681 1682 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 1683 1684 const std::string kParentResourceId = "folder:1_folder_resource_id"; 1685 1686 GDataErrorCode error = GDATA_OTHER_ERROR; 1687 scoped_ptr<ResourceEntry> resource_entry; 1688 fake_service_.AddNewDirectory( 1689 kParentResourceId, 1690 "new directory", 1691 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1692 base::RunLoop().RunUntilIdle(); 1693 1694 EXPECT_EQ(HTTP_CREATED, error); 1695 ASSERT_TRUE(resource_entry); 1696 EXPECT_TRUE(resource_entry->is_folder()); 1697 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 1698 EXPECT_EQ("new directory", resource_entry->title()); 1699 EXPECT_TRUE(HasParent(resource_entry->resource_id(), kParentResourceId)); 1700 // Should be incremented as a new directory was created. 1701 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 1702 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 1703 } 1704 1705 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonexistingDirectory) { 1706 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1707 "gdata/root_feed.json")); 1708 1709 const std::string kParentResourceId = "folder:nonexisting_resource_id"; 1710 1711 GDataErrorCode error = GDATA_OTHER_ERROR; 1712 scoped_ptr<ResourceEntry> resource_entry; 1713 fake_service_.AddNewDirectory( 1714 kParentResourceId, 1715 "new directory", 1716 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1717 base::RunLoop().RunUntilIdle(); 1718 1719 EXPECT_EQ(HTTP_NOT_FOUND, error); 1720 EXPECT_FALSE(resource_entry); 1721 } 1722 1723 TEST_F(FakeDriveServiceTest, AddNewDirectory_Offline) { 1724 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1725 "gdata/root_feed.json")); 1726 fake_service_.set_offline(true); 1727 1728 GDataErrorCode error = GDATA_OTHER_ERROR; 1729 scoped_ptr<ResourceEntry> resource_entry; 1730 fake_service_.AddNewDirectory( 1731 fake_service_.GetRootResourceId(), 1732 "new directory", 1733 test_util::CreateCopyResultCallback(&error, &resource_entry)); 1734 base::RunLoop().RunUntilIdle(); 1735 1736 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1737 EXPECT_FALSE(resource_entry); 1738 } 1739 1740 TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_Offline) { 1741 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1742 "gdata/root_feed.json")); 1743 fake_service_.set_offline(true); 1744 1745 GDataErrorCode error = GDATA_OTHER_ERROR; 1746 GURL upload_location; 1747 fake_service_.InitiateUploadNewFile( 1748 "test/foo", 1749 13, 1750 "folder:1_folder_resource_id", 1751 "new file.foo", 1752 test_util::CreateCopyResultCallback(&error, &upload_location)); 1753 base::RunLoop().RunUntilIdle(); 1754 1755 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1756 EXPECT_TRUE(upload_location.is_empty()); 1757 } 1758 1759 TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_NotFound) { 1760 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1761 "gdata/root_feed.json")); 1762 1763 GDataErrorCode error = GDATA_OTHER_ERROR; 1764 GURL upload_location; 1765 fake_service_.InitiateUploadNewFile( 1766 "test/foo", 1767 13, 1768 "non_existent", 1769 "new file.foo", 1770 test_util::CreateCopyResultCallback(&error, &upload_location)); 1771 base::RunLoop().RunUntilIdle(); 1772 1773 EXPECT_EQ(HTTP_NOT_FOUND, error); 1774 EXPECT_TRUE(upload_location.is_empty()); 1775 } 1776 1777 TEST_F(FakeDriveServiceTest, InitiateUploadNewFile) { 1778 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1779 "gdata/root_feed.json")); 1780 1781 GDataErrorCode error = GDATA_OTHER_ERROR; 1782 GURL upload_location; 1783 fake_service_.InitiateUploadNewFile( 1784 "test/foo", 1785 13, 1786 "folder:1_folder_resource_id", 1787 "new file.foo", 1788 test_util::CreateCopyResultCallback(&error, &upload_location)); 1789 base::RunLoop().RunUntilIdle(); 1790 1791 EXPECT_EQ(HTTP_SUCCESS, error); 1792 EXPECT_FALSE(upload_location.is_empty()); 1793 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link?mode=newfile"), 1794 upload_location); 1795 } 1796 1797 TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Offline) { 1798 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1799 "gdata/root_feed.json")); 1800 fake_service_.set_offline(true); 1801 1802 GDataErrorCode error = GDATA_OTHER_ERROR; 1803 GURL upload_location; 1804 fake_service_.InitiateUploadExistingFile( 1805 "test/foo", 1806 13, 1807 "file:2_file_resource_id", 1808 std::string(), // etag 1809 test_util::CreateCopyResultCallback(&error, &upload_location)); 1810 base::RunLoop().RunUntilIdle(); 1811 1812 EXPECT_EQ(GDATA_NO_CONNECTION, error); 1813 EXPECT_TRUE(upload_location.is_empty()); 1814 } 1815 1816 TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_NotFound) { 1817 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1818 "gdata/root_feed.json")); 1819 1820 GDataErrorCode error = GDATA_OTHER_ERROR; 1821 GURL upload_location; 1822 fake_service_.InitiateUploadExistingFile( 1823 "test/foo", 1824 13, 1825 "non_existent", 1826 std::string(), // etag 1827 test_util::CreateCopyResultCallback(&error, &upload_location)); 1828 base::RunLoop().RunUntilIdle(); 1829 1830 EXPECT_EQ(HTTP_NOT_FOUND, error); 1831 EXPECT_TRUE(upload_location.is_empty()); 1832 } 1833 1834 TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_WrongETag) { 1835 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1836 "gdata/root_feed.json")); 1837 1838 GDataErrorCode error = GDATA_OTHER_ERROR; 1839 GURL upload_location; 1840 fake_service_.InitiateUploadExistingFile( 1841 "text/plain", 1842 13, 1843 "file:2_file_resource_id", 1844 "invalid_etag", 1845 test_util::CreateCopyResultCallback(&error, &upload_location)); 1846 base::RunLoop().RunUntilIdle(); 1847 1848 EXPECT_EQ(HTTP_PRECONDITION, error); 1849 EXPECT_TRUE(upload_location.is_empty()); 1850 } 1851 1852 TEST_F(FakeDriveServiceTest, InitiateUpload_ExistingFile) { 1853 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1854 "gdata/root_feed.json")); 1855 1856 GDataErrorCode error = GDATA_OTHER_ERROR; 1857 GURL upload_location; 1858 fake_service_.InitiateUploadExistingFile( 1859 "text/plain", 1860 13, 1861 "file:2_file_resource_id", 1862 "\"HhMOFgxXHit7ImBr\"", 1863 test_util::CreateCopyResultCallback(&error, &upload_location)); 1864 base::RunLoop().RunUntilIdle(); 1865 1866 EXPECT_EQ(HTTP_SUCCESS, error); 1867 EXPECT_TRUE(upload_location.is_valid()); 1868 } 1869 1870 TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) { 1871 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1872 "gdata/root_feed.json")); 1873 1874 GDataErrorCode error = GDATA_OTHER_ERROR; 1875 GURL upload_location; 1876 fake_service_.InitiateUploadNewFile( 1877 "test/foo", 1878 15, 1879 "folder:1_folder_resource_id", 1880 "new file.foo", 1881 test_util::CreateCopyResultCallback(&error, &upload_location)); 1882 base::RunLoop().RunUntilIdle(); 1883 1884 EXPECT_EQ(HTTP_SUCCESS, error); 1885 EXPECT_FALSE(upload_location.is_empty()); 1886 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"), 1887 upload_location); 1888 1889 fake_service_.set_offline(true); 1890 1891 UploadRangeResponse response; 1892 scoped_ptr<ResourceEntry> entry; 1893 fake_service_.ResumeUpload( 1894 upload_location, 1895 0, 13, 15, "test/foo", 1896 base::FilePath(), 1897 test_util::CreateCopyResultCallback(&response, &entry), 1898 ProgressCallback()); 1899 base::RunLoop().RunUntilIdle(); 1900 1901 EXPECT_EQ(GDATA_NO_CONNECTION, response.code); 1902 EXPECT_FALSE(entry.get()); 1903 } 1904 1905 TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) { 1906 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1907 "gdata/root_feed.json")); 1908 1909 GDataErrorCode error = GDATA_OTHER_ERROR; 1910 GURL upload_location; 1911 fake_service_.InitiateUploadNewFile( 1912 "test/foo", 1913 15, 1914 "folder:1_folder_resource_id", 1915 "new file.foo", 1916 test_util::CreateCopyResultCallback(&error, &upload_location)); 1917 base::RunLoop().RunUntilIdle(); 1918 1919 ASSERT_EQ(HTTP_SUCCESS, error); 1920 1921 UploadRangeResponse response; 1922 scoped_ptr<ResourceEntry> entry; 1923 fake_service_.ResumeUpload( 1924 GURL("https://foo.com/"), 1925 0, 13, 15, "test/foo", 1926 base::FilePath(), 1927 test_util::CreateCopyResultCallback(&response, &entry), 1928 ProgressCallback()); 1929 base::RunLoop().RunUntilIdle(); 1930 1931 EXPECT_EQ(HTTP_NOT_FOUND, response.code); 1932 EXPECT_FALSE(entry.get()); 1933 } 1934 1935 TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) { 1936 base::ScopedTempDir temp_dir; 1937 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 1938 base::FilePath local_file_path = 1939 temp_dir.path().Append(FILE_PATH_LITERAL("File 1.txt")); 1940 std::string contents("hogefugapiyo"); 1941 ASSERT_TRUE(test_util::WriteStringToFile(local_file_path, contents)); 1942 1943 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 1944 "gdata/root_feed.json")); 1945 1946 GDataErrorCode error = GDATA_OTHER_ERROR; 1947 GURL upload_location; 1948 fake_service_.InitiateUploadExistingFile( 1949 "text/plain", 1950 contents.size(), 1951 "file:2_file_resource_id", 1952 "\"HhMOFgxXHit7ImBr\"", 1953 test_util::CreateCopyResultCallback(&error, &upload_location)); 1954 base::RunLoop().RunUntilIdle(); 1955 1956 ASSERT_EQ(HTTP_SUCCESS, error); 1957 1958 UploadRangeResponse response; 1959 scoped_ptr<ResourceEntry> entry; 1960 std::vector<test_util::ProgressInfo> upload_progress_values; 1961 fake_service_.ResumeUpload( 1962 upload_location, 1963 0, contents.size() / 2, contents.size(), "text/plain", 1964 local_file_path, 1965 test_util::CreateCopyResultCallback(&response, &entry), 1966 base::Bind(&test_util::AppendProgressCallbackResult, 1967 &upload_progress_values)); 1968 base::RunLoop().RunUntilIdle(); 1969 1970 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); 1971 EXPECT_FALSE(entry.get()); 1972 ASSERT_TRUE(!upload_progress_values.empty()); 1973 EXPECT_TRUE(base::STLIsSorted(upload_progress_values)); 1974 EXPECT_LE(0, upload_progress_values.front().first); 1975 EXPECT_GE(static_cast<int64>(contents.size() / 2), 1976 upload_progress_values.back().first); 1977 1978 upload_progress_values.clear(); 1979 fake_service_.ResumeUpload( 1980 upload_location, 1981 contents.size() / 2, contents.size(), contents.size(), "text/plain", 1982 local_file_path, 1983 test_util::CreateCopyResultCallback(&response, &entry), 1984 base::Bind(&test_util::AppendProgressCallbackResult, 1985 &upload_progress_values)); 1986 base::RunLoop().RunUntilIdle(); 1987 1988 EXPECT_EQ(HTTP_SUCCESS, response.code); 1989 EXPECT_TRUE(entry.get()); 1990 EXPECT_EQ(static_cast<int64>(contents.size()), 1991 entry->file_size()); 1992 EXPECT_TRUE(Exists(entry->resource_id())); 1993 ASSERT_TRUE(!upload_progress_values.empty()); 1994 EXPECT_TRUE(base::STLIsSorted(upload_progress_values)); 1995 EXPECT_LE(0, upload_progress_values.front().first); 1996 EXPECT_GE(static_cast<int64>(contents.size() - contents.size() / 2), 1997 upload_progress_values.back().first); 1998 EXPECT_EQ(base::MD5String(contents), entry->file_md5()); 1999 } 2000 2001 TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) { 2002 base::ScopedTempDir temp_dir; 2003 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 2004 base::FilePath local_file_path = 2005 temp_dir.path().Append(FILE_PATH_LITERAL("new file.foo")); 2006 std::string contents("hogefugapiyo"); 2007 ASSERT_TRUE(test_util::WriteStringToFile(local_file_path, contents)); 2008 2009 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2010 "gdata/root_feed.json")); 2011 2012 GDataErrorCode error = GDATA_OTHER_ERROR; 2013 GURL upload_location; 2014 fake_service_.InitiateUploadNewFile( 2015 "test/foo", 2016 contents.size(), 2017 "folder:1_folder_resource_id", 2018 "new file.foo", 2019 test_util::CreateCopyResultCallback(&error, &upload_location)); 2020 base::RunLoop().RunUntilIdle(); 2021 2022 EXPECT_EQ(HTTP_SUCCESS, error); 2023 EXPECT_FALSE(upload_location.is_empty()); 2024 EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"), 2025 upload_location); 2026 2027 UploadRangeResponse response; 2028 scoped_ptr<ResourceEntry> entry; 2029 std::vector<test_util::ProgressInfo> upload_progress_values; 2030 fake_service_.ResumeUpload( 2031 upload_location, 2032 0, contents.size() / 2, contents.size(), "test/foo", 2033 local_file_path, 2034 test_util::CreateCopyResultCallback(&response, &entry), 2035 base::Bind(&test_util::AppendProgressCallbackResult, 2036 &upload_progress_values)); 2037 base::RunLoop().RunUntilIdle(); 2038 2039 EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code); 2040 EXPECT_FALSE(entry.get()); 2041 ASSERT_TRUE(!upload_progress_values.empty()); 2042 EXPECT_TRUE(base::STLIsSorted(upload_progress_values)); 2043 EXPECT_LE(0, upload_progress_values.front().first); 2044 EXPECT_GE(static_cast<int64>(contents.size() / 2), 2045 upload_progress_values.back().first); 2046 2047 upload_progress_values.clear(); 2048 fake_service_.ResumeUpload( 2049 upload_location, 2050 contents.size() / 2, contents.size(), contents.size(), "test/foo", 2051 local_file_path, 2052 test_util::CreateCopyResultCallback(&response, &entry), 2053 base::Bind(&test_util::AppendProgressCallbackResult, 2054 &upload_progress_values)); 2055 base::RunLoop().RunUntilIdle(); 2056 2057 EXPECT_EQ(HTTP_CREATED, response.code); 2058 EXPECT_TRUE(entry.get()); 2059 EXPECT_EQ(static_cast<int64>(contents.size()), entry->file_size()); 2060 EXPECT_TRUE(Exists(entry->resource_id())); 2061 ASSERT_TRUE(!upload_progress_values.empty()); 2062 EXPECT_TRUE(base::STLIsSorted(upload_progress_values)); 2063 EXPECT_LE(0, upload_progress_values.front().first); 2064 EXPECT_GE(static_cast<int64>(contents.size() - contents.size() / 2), 2065 upload_progress_values.back().first); 2066 EXPECT_EQ(base::MD5String(contents), entry->file_md5()); 2067 } 2068 2069 TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectory) { 2070 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2071 "gdata/root_feed.json")); 2072 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 2073 "gdata/account_metadata.json")); 2074 2075 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 2076 2077 const std::string kContentType = "text/plain"; 2078 const std::string kContentData = "This is some test content."; 2079 const std::string kTitle = "new file"; 2080 2081 GDataErrorCode error = GDATA_OTHER_ERROR; 2082 scoped_ptr<ResourceEntry> resource_entry; 2083 fake_service_.AddNewFile( 2084 kContentType, 2085 kContentData, 2086 fake_service_.GetRootResourceId(), 2087 kTitle, 2088 false, // shared_with_me 2089 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2090 base::RunLoop().RunUntilIdle(); 2091 2092 EXPECT_EQ(HTTP_CREATED, error); 2093 ASSERT_TRUE(resource_entry); 2094 EXPECT_TRUE(resource_entry->is_file()); 2095 EXPECT_EQ(kContentType, resource_entry->content_mime_type()); 2096 EXPECT_EQ(static_cast<int64>(kContentData.size()), 2097 resource_entry->file_size()); 2098 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 2099 EXPECT_EQ(kTitle, resource_entry->title()); 2100 EXPECT_TRUE(HasParent(resource_entry->resource_id(), 2101 fake_service_.GetRootResourceId())); 2102 // Should be incremented as a new directory was created. 2103 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 2104 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 2105 EXPECT_EQ(base::MD5String(kContentData), resource_entry->file_md5()); 2106 } 2107 2108 TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectoryOnEmptyFileSystem) { 2109 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2110 "gdata/empty_feed.json")); 2111 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 2112 "gdata/account_metadata.json")); 2113 2114 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 2115 2116 const std::string kContentType = "text/plain"; 2117 const std::string kContentData = "This is some test content."; 2118 const std::string kTitle = "new file"; 2119 2120 GDataErrorCode error = GDATA_OTHER_ERROR; 2121 scoped_ptr<ResourceEntry> resource_entry; 2122 fake_service_.AddNewFile( 2123 kContentType, 2124 kContentData, 2125 fake_service_.GetRootResourceId(), 2126 kTitle, 2127 false, // shared_with_me 2128 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2129 base::RunLoop().RunUntilIdle(); 2130 2131 EXPECT_EQ(HTTP_CREATED, error); 2132 ASSERT_TRUE(resource_entry); 2133 EXPECT_TRUE(resource_entry->is_file()); 2134 EXPECT_EQ(kContentType, resource_entry->content_mime_type()); 2135 EXPECT_EQ(static_cast<int64>(kContentData.size()), 2136 resource_entry->file_size()); 2137 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 2138 EXPECT_EQ(kTitle, resource_entry->title()); 2139 EXPECT_TRUE(HasParent(resource_entry->resource_id(), 2140 fake_service_.GetRootResourceId())); 2141 // Should be incremented as a new directory was created. 2142 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 2143 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 2144 EXPECT_EQ(base::MD5String(kContentData), resource_entry->file_md5()); 2145 } 2146 2147 TEST_F(FakeDriveServiceTest, AddNewFile_ToNonRootDirectory) { 2148 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2149 "gdata/root_feed.json")); 2150 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 2151 "gdata/account_metadata.json")); 2152 2153 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 2154 2155 const std::string kContentType = "text/plain"; 2156 const std::string kContentData = "This is some test content."; 2157 const std::string kTitle = "new file"; 2158 const std::string kParentResourceId = "folder:1_folder_resource_id"; 2159 2160 GDataErrorCode error = GDATA_OTHER_ERROR; 2161 scoped_ptr<ResourceEntry> resource_entry; 2162 fake_service_.AddNewFile( 2163 kContentType, 2164 kContentData, 2165 kParentResourceId, 2166 kTitle, 2167 false, // shared_with_me 2168 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2169 base::RunLoop().RunUntilIdle(); 2170 2171 EXPECT_EQ(HTTP_CREATED, error); 2172 ASSERT_TRUE(resource_entry); 2173 EXPECT_TRUE(resource_entry->is_file()); 2174 EXPECT_EQ(kContentType, resource_entry->content_mime_type()); 2175 EXPECT_EQ(static_cast<int64>(kContentData.size()), 2176 resource_entry->file_size()); 2177 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 2178 EXPECT_EQ(kTitle, resource_entry->title()); 2179 EXPECT_TRUE(HasParent(resource_entry->resource_id(), kParentResourceId)); 2180 // Should be incremented as a new directory was created. 2181 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 2182 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 2183 EXPECT_EQ(base::MD5String(kContentData), resource_entry->file_md5()); 2184 } 2185 2186 TEST_F(FakeDriveServiceTest, AddNewFile_ToNonexistingDirectory) { 2187 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2188 "gdata/root_feed.json")); 2189 2190 const std::string kContentType = "text/plain"; 2191 const std::string kContentData = "This is some test content."; 2192 const std::string kTitle = "new file"; 2193 const std::string kParentResourceId = "folder:nonexisting_resource_id"; 2194 2195 GDataErrorCode error = GDATA_OTHER_ERROR; 2196 scoped_ptr<ResourceEntry> resource_entry; 2197 fake_service_.AddNewFile( 2198 kContentType, 2199 kContentData, 2200 kParentResourceId, 2201 kTitle, 2202 false, // shared_with_me 2203 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2204 base::RunLoop().RunUntilIdle(); 2205 2206 EXPECT_EQ(HTTP_NOT_FOUND, error); 2207 EXPECT_FALSE(resource_entry); 2208 } 2209 2210 TEST_F(FakeDriveServiceTest, AddNewFile_Offline) { 2211 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2212 "gdata/root_feed.json")); 2213 fake_service_.set_offline(true); 2214 2215 const std::string kContentType = "text/plain"; 2216 const std::string kContentData = "This is some test content."; 2217 const std::string kTitle = "new file"; 2218 2219 GDataErrorCode error = GDATA_OTHER_ERROR; 2220 scoped_ptr<ResourceEntry> resource_entry; 2221 fake_service_.AddNewFile( 2222 kContentType, 2223 kContentData, 2224 fake_service_.GetRootResourceId(), 2225 kTitle, 2226 false, // shared_with_me 2227 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2228 base::RunLoop().RunUntilIdle(); 2229 2230 EXPECT_EQ(GDATA_NO_CONNECTION, error); 2231 EXPECT_FALSE(resource_entry); 2232 } 2233 2234 TEST_F(FakeDriveServiceTest, AddNewFile_SharedWithMeLabel) { 2235 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2236 "gdata/root_feed.json")); 2237 ASSERT_TRUE(fake_service_.LoadAccountMetadataForWapi( 2238 "gdata/account_metadata.json")); 2239 2240 const std::string kContentType = "text/plain"; 2241 const std::string kContentData = "This is some test content."; 2242 const std::string kTitle = "new file"; 2243 2244 int64 old_largest_change_id = GetLargestChangeByAboutResource(); 2245 2246 GDataErrorCode error = GDATA_OTHER_ERROR; 2247 scoped_ptr<ResourceEntry> resource_entry; 2248 fake_service_.AddNewFile( 2249 kContentType, 2250 kContentData, 2251 fake_service_.GetRootResourceId(), 2252 kTitle, 2253 true, // shared_with_me 2254 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2255 base::RunLoop().RunUntilIdle(); 2256 2257 EXPECT_EQ(HTTP_CREATED, error); 2258 ASSERT_TRUE(resource_entry); 2259 EXPECT_TRUE(resource_entry->is_file()); 2260 EXPECT_EQ(kContentType, resource_entry->content_mime_type()); 2261 EXPECT_EQ(static_cast<int64>(kContentData.size()), 2262 resource_entry->file_size()); 2263 EXPECT_EQ("resource_id_1", resource_entry->resource_id()); 2264 EXPECT_EQ(kTitle, resource_entry->title()); 2265 EXPECT_TRUE(HasParent(resource_entry->resource_id(), 2266 fake_service_.GetRootResourceId())); 2267 ASSERT_EQ(1U, resource_entry->labels().size()); 2268 EXPECT_EQ("shared-with-me", resource_entry->labels()[0]); 2269 // Should be incremented as a new directory was created. 2270 EXPECT_EQ(old_largest_change_id + 1, fake_service_.largest_changestamp()); 2271 EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource()); 2272 EXPECT_EQ(base::MD5String(kContentData), resource_entry->file_md5()); 2273 } 2274 2275 TEST_F(FakeDriveServiceTest, SetLastModifiedTime_ExistingFile) { 2276 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2277 "gdata/root_feed.json")); 2278 2279 const std::string kResourceId = "file:2_file_resource_id"; 2280 base::Time time; 2281 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time)); 2282 2283 GDataErrorCode error = GDATA_OTHER_ERROR; 2284 scoped_ptr<ResourceEntry> resource_entry; 2285 fake_service_.SetLastModifiedTime( 2286 kResourceId, 2287 time, 2288 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2289 base::RunLoop().RunUntilIdle(); 2290 2291 EXPECT_EQ(HTTP_SUCCESS, error); 2292 ASSERT_TRUE(resource_entry); 2293 EXPECT_EQ(time, resource_entry->updated_time()); 2294 } 2295 2296 TEST_F(FakeDriveServiceTest, SetLastModifiedTime_NonexistingFile) { 2297 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2298 "gdata/root_feed.json")); 2299 2300 const std::string kResourceId = "file:nonexisting_resource_id"; 2301 base::Time time; 2302 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time)); 2303 2304 GDataErrorCode error = GDATA_OTHER_ERROR; 2305 scoped_ptr<ResourceEntry> resource_entry; 2306 fake_service_.SetLastModifiedTime( 2307 kResourceId, 2308 time, 2309 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2310 base::RunLoop().RunUntilIdle(); 2311 2312 EXPECT_EQ(HTTP_NOT_FOUND, error); 2313 EXPECT_FALSE(resource_entry); 2314 } 2315 2316 TEST_F(FakeDriveServiceTest, SetLastModifiedTime_Offline) { 2317 ASSERT_TRUE(fake_service_.LoadResourceListForWapi( 2318 "gdata/root_feed.json")); 2319 fake_service_.set_offline(true); 2320 2321 const std::string kResourceId = "file:2_file_resource_id"; 2322 base::Time time; 2323 ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time)); 2324 2325 GDataErrorCode error = GDATA_OTHER_ERROR; 2326 scoped_ptr<ResourceEntry> resource_entry; 2327 fake_service_.SetLastModifiedTime( 2328 kResourceId, 2329 time, 2330 test_util::CreateCopyResultCallback(&error, &resource_entry)); 2331 base::RunLoop().RunUntilIdle(); 2332 2333 EXPECT_EQ(GDATA_NO_CONNECTION, error); 2334 EXPECT_FALSE(resource_entry); 2335 } 2336 2337 } // namespace 2338 2339 } // namespace drive 2340