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