1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/sync_file_system/drive_backend_v1/api_util.h"
7 #include "base/file_util.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "chrome/browser/drive/drive_uploader.h"
14 #include "chrome/browser/drive/fake_drive_service.h"
15 #include "chrome/browser/google_apis/drive_api_parser.h"
16 #include "chrome/browser/google_apis/gdata_errorcode.h"
17 #include "chrome/browser/google_apis/test_util.h"
18 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_util.h"
19 #include "chrome/browser/sync_file_system/drive_backend_v1/fake_drive_service_helper.h"
20 #include "content/public/test/test_browser_thread.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "net/base/escape.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 #define FPL(x) FILE_PATH_LITERAL(x)
27 using drive::DriveServiceInterface;
28 using drive::DriveUploaderInterface;
29 using drive::FakeDriveService;
30 using drive::UploadCompletionCallback;
31 using google_apis::GDataErrorCode;
32 using google_apis::ProgressCallback;
33 using google_apis::ResourceEntry;
34 using google_apis::ResourceList;
36 namespace sync_file_system {
37 namespace drive_backend {
41 const char kOrigin[] = "chrome-extension://example";
42 const char kOriginDirectoryName[] = "example";
46 std::string resource_id;
48 int64 largest_changestamp;
50 Output() : error(google_apis::GDATA_OTHER_ERROR),
51 largest_changestamp(-1) {
55 void DidAddFileOrDirectoryForMakingConflict(GDataErrorCode error,
56 scoped_ptr<ResourceEntry> entry) {
57 ASSERT_EQ(google_apis::HTTP_CREATED, error);
61 void DidAddFileForUploadNew(
62 const UploadCompletionCallback& callback,
64 scoped_ptr<ResourceEntry> entry) {
65 ASSERT_EQ(google_apis::HTTP_CREATED, error);
67 base::MessageLoopProxy::current()->PostTask(
70 google_apis::HTTP_SUCCESS,
72 base::Passed(&entry)));
75 void DidGetResourceEntryForUploadExisting(
76 const UploadCompletionCallback& callback,
78 scoped_ptr<ResourceEntry> entry) {
79 ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
81 base::MessageLoopProxy::current()->PostTask(
84 google_apis::HTTP_SUCCESS,
86 base::Passed(&entry)));
89 class FakeDriveServiceWrapper : public FakeDriveService {
91 FakeDriveServiceWrapper() : make_directory_conflict_(false) {}
92 virtual ~FakeDriveServiceWrapper() {}
94 // DriveServiceInterface overrides.
95 virtual google_apis::CancelCallback AddNewDirectory(
96 const std::string& parent_resource_id,
97 const std::string& directory_name,
98 const google_apis::GetResourceEntryCallback& callback) OVERRIDE {
99 if (make_directory_conflict_) {
100 FakeDriveService::AddNewDirectory(
103 base::Bind(&DidAddFileOrDirectoryForMakingConflict));
105 return FakeDriveService::AddNewDirectory(
106 parent_resource_id, directory_name, callback);
109 void set_make_directory_conflict(bool enable) {
110 make_directory_conflict_ = enable;
114 bool make_directory_conflict_;
116 DISALLOW_COPY_AND_ASSIGN(FakeDriveServiceWrapper);
119 // A fake implementation of DriveUploaderInterface, which provides fake
120 // behaviors for file uploading.
121 class FakeDriveUploader : public DriveUploaderInterface {
123 explicit FakeDriveUploader(FakeDriveServiceWrapper* fake_drive_service)
124 : fake_drive_service_(fake_drive_service),
125 make_file_conflict_(false) {}
126 virtual ~FakeDriveUploader() {}
128 // DriveUploaderInterface overrides.
130 // Proxies a request to upload a new file to FakeDriveService, and returns the
131 // resource entry to the caller.
132 virtual google_apis::CancelCallback UploadNewFile(
133 const std::string& parent_resource_id,
134 const base::FilePath& local_file_path,
135 const std::string& title,
136 const std::string& content_type,
137 const UploadCompletionCallback& callback,
138 const ProgressCallback& progress_callback) OVERRIDE {
139 DCHECK(!callback.is_null());
140 const std::string kFileContent = "test content";
142 if (make_file_conflict_) {
143 fake_drive_service_->AddNewFile(
148 false, // shared_with_me
149 base::Bind(&DidAddFileOrDirectoryForMakingConflict));
152 fake_drive_service_->AddNewFile(
157 false, // shared_with_me
158 base::Bind(&DidAddFileForUploadNew, callback));
159 base::MessageLoop::current()->RunUntilIdle();
161 return google_apis::CancelCallback();
164 // Pretends that an existing file |resource_id| was uploaded successfully, and
165 // returns a resource entry to the caller.
166 virtual google_apis::CancelCallback UploadExistingFile(
167 const std::string& resource_id,
168 const base::FilePath& local_file_path,
169 const std::string& content_type,
170 const std::string& etag,
171 const UploadCompletionCallback& callback,
172 const ProgressCallback& progress_callback) OVERRIDE {
173 DCHECK(!callback.is_null());
174 return fake_drive_service_->GetResourceEntry(
176 base::Bind(&DidGetResourceEntryForUploadExisting, callback));
179 // At the moment, sync file system doesn't support resuming of the uploading.
180 // So this method shouldn't be reached.
181 virtual google_apis::CancelCallback ResumeUploadFile(
182 const GURL& upload_location,
183 const base::FilePath& local_file_path,
184 const std::string& content_type,
185 const UploadCompletionCallback& callback,
186 const ProgressCallback& progress_callback) OVERRIDE {
188 return google_apis::CancelCallback();
191 void set_make_file_conflict(bool enable) {
192 make_file_conflict_ = enable;
196 FakeDriveServiceWrapper* fake_drive_service_;
197 bool make_file_conflict_;
199 DISALLOW_COPY_AND_ASSIGN(FakeDriveUploader);
204 class APIUtilTest : public testing::Test {
206 APIUtilTest() : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
207 fake_drive_service_(NULL),
208 fake_drive_uploader_(NULL) {}
210 virtual void SetUp() OVERRIDE {
211 fake_drive_service_ = new FakeDriveServiceWrapper;
212 fake_drive_uploader_ = new FakeDriveUploader(fake_drive_service_);
214 fake_drive_helper_.reset(new FakeDriveServiceHelper(
215 fake_drive_service_, fake_drive_uploader_));
217 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
218 api_util_ = APIUtil::CreateForTesting(
220 scoped_ptr<DriveServiceInterface>(fake_drive_service_),
221 scoped_ptr<DriveUploaderInterface>(fake_drive_uploader_));
223 fake_drive_service_->LoadResourceListForWapi(
224 "sync_file_system/initialize.json");
227 virtual void TearDown() OVERRIDE {
232 std::string SetUpSyncRootDirectory() {
233 std::string sync_root_id;
234 EXPECT_EQ(google_apis::HTTP_CREATED,
235 fake_drive_helper_->AddOrphanedFolder(
236 APIUtil::GetSyncRootDirectoryName(),
241 std::string SetUpOriginRootDirectory(const std::string& sync_root_id) {
242 std::string origin_root_id;
243 EXPECT_EQ(google_apis::HTTP_CREATED,
244 fake_drive_helper_->AddFolder(
246 kOriginDirectoryName,
248 return origin_root_id;
251 void SetUpFile(const std::string& origin_root_id,
252 const std::string& content_data,
253 const std::string& title,
254 scoped_ptr<ResourceEntry>* entry) {
256 std::string file_resource_id;
257 EXPECT_EQ(google_apis::HTTP_SUCCESS,
258 fake_drive_helper_->AddFile(
263 EXPECT_EQ(google_apis::HTTP_SUCCESS,
264 fake_drive_helper_->GetResourceEntry(
269 void LoadAccountMetadata() {
270 fake_drive_service_->LoadAccountMetadataForWapi(
271 "sync_file_system/account_metadata.json");
274 void VerifyTitleUniqueness(const std::string& parent_resource_id,
275 const std::string& title,
276 const std::string& resource_id,
277 google_apis::DriveEntryKind kind) {
278 ScopedVector<ResourceEntry> entries;
279 EXPECT_EQ(google_apis::HTTP_SUCCESS,
280 fake_drive_helper_->SearchByTitle(
281 parent_resource_id, title, &entries));
282 ASSERT_EQ(1u, entries.size());
283 EXPECT_EQ(resource_id, entries[0]->resource_id());
284 EXPECT_EQ(kind, entries[0]->kind());
287 void VerifyFileDeletion(const std::string& parent_resource_id,
288 const std::string& title) {
289 ScopedVector<ResourceEntry> entries;
290 EXPECT_EQ(google_apis::HTTP_SUCCESS,
291 fake_drive_helper_->SearchByTitle(
292 parent_resource_id, title, &entries));
293 EXPECT_TRUE(entries.empty());
296 APIUtil* api_util() { return api_util_.get(); }
298 FakeDriveServiceWrapper* fake_drive_service() {
299 return fake_drive_service_;
302 FakeDriveUploader* fake_drive_uploader() {
303 return fake_drive_uploader_;
306 void TestGetSyncRoot();
307 void TestCreateSyncRoot();
308 void TestCreateSyncRoot_Conflict();
309 void TestGetOriginDirectory();
310 void TestCreateOriginDirectory();
311 void TestCreateOriginDirectory_Conflict();
312 void TestGetLargestChangeStamp();
313 void TestListFiles();
314 void TestListChanges();
315 void TestDownloadFile();
316 void TestDownloadFileInNotModified();
317 void TestUploadNewFile();
318 void TestUploadNewFile_ConflictWithFile();
319 void TestUploadExistingFile();
320 void TestUploadExistingFileInConflict();
321 void TestDeleteFile();
322 void TestDeleteFileInConflict();
323 void TestCreateDirectory();
326 content::TestBrowserThreadBundle thread_bundle_;
328 base::ScopedTempDir temp_dir_;
329 scoped_ptr<APIUtil> api_util_;
330 FakeDriveServiceWrapper* fake_drive_service_;
331 FakeDriveUploader* fake_drive_uploader_;
332 scoped_ptr<FakeDriveServiceHelper> fake_drive_helper_;
334 DISALLOW_COPY_AND_ASSIGN(APIUtilTest);
337 void DidGetResourceID(Output* output,
338 GDataErrorCode error,
339 const std::string& resource_id) {
341 output->error = error;
342 output->resource_id = resource_id;
345 void DidGetLargestChangeStamp(Output* output,
346 GDataErrorCode error,
347 int64 largest_changestamp) {
349 output->error = error;
350 output->largest_changestamp = largest_changestamp;
353 void DidGetResourceList(GDataErrorCode* error_out,
354 scoped_ptr<ResourceList>* document_feed_out,
355 GDataErrorCode error,
356 scoped_ptr<ResourceList> document_feed) {
357 ASSERT_TRUE(error_out);
358 ASSERT_TRUE(document_feed_out);
360 *document_feed_out = document_feed.Pass();
363 void DidDownloadFile(Output* output,
364 GDataErrorCode error,
365 const std::string& file_md5,
367 const base::Time& updated_time,
368 webkit_blob::ScopedFile file) {
370 ASSERT_TRUE(base::PathExists(file.path()));
371 output->error = error;
372 output->file_md5 = file_md5;
375 void DidUploadFile(Output* output,
376 GDataErrorCode error,
377 const std::string& resource_id,
378 const std::string& file_md5) {
380 output->error = error;
381 output->resource_id = resource_id;
382 output->file_md5 = file_md5;
385 void DidDeleteFile(GDataErrorCode* error_out,
386 GDataErrorCode error) {
391 void APIUtilTest::TestGetSyncRoot() {
392 LoadAccountMetadata();
393 const std::string sync_root_id = SetUpSyncRootDirectory();
396 api_util()->GetDriveDirectoryForSyncRoot(
397 base::Bind(&DidGetResourceID, &output));
398 base::MessageLoop::current()->RunUntilIdle();
400 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
401 EXPECT_EQ(sync_root_id, output.resource_id);
404 void APIUtilTest::TestCreateSyncRoot() {
405 LoadAccountMetadata();
408 api_util()->GetDriveDirectoryForSyncRoot(
409 base::Bind(&DidGetResourceID, &output));
410 base::MessageLoop::current()->RunUntilIdle();
412 EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
413 EXPECT_FALSE(output.resource_id.empty());
415 VerifyTitleUniqueness(std::string(), // directory_resource_id
416 APIUtil::GetSyncRootDirectoryName(),
418 google_apis::ENTRY_KIND_FOLDER);
421 void APIUtilTest::TestCreateSyncRoot_Conflict() {
422 LoadAccountMetadata();
423 fake_drive_service()->set_make_directory_conflict(true);
426 api_util()->GetDriveDirectoryForSyncRoot(
427 base::Bind(&DidGetResourceID, &output));
428 base::MessageLoop::current()->RunUntilIdle();
430 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
431 EXPECT_FALSE(output.resource_id.empty());
433 // Verify that there is no duplicated directory on the remote side.
434 VerifyTitleUniqueness(std::string(), // directory_resource_id
435 APIUtil::GetSyncRootDirectoryName(),
437 google_apis::ENTRY_KIND_FOLDER);
440 void APIUtilTest::TestGetOriginDirectory() {
441 const std::string sync_root_id = SetUpSyncRootDirectory();
442 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
445 api_util()->GetDriveDirectoryForOrigin(
448 base::Bind(&DidGetResourceID, &output));
449 base::MessageLoop::current()->RunUntilIdle();
451 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
452 EXPECT_EQ(origin_root_id, output.resource_id);
455 void APIUtilTest::TestCreateOriginDirectory() {
456 const std::string& sync_root_id = SetUpSyncRootDirectory();
459 api_util()->GetDriveDirectoryForOrigin(
462 base::Bind(&DidGetResourceID, &output));
463 base::MessageLoop::current()->RunUntilIdle();
465 EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
466 EXPECT_FALSE(output.resource_id.empty());
468 VerifyTitleUniqueness(sync_root_id,
469 kOriginDirectoryName,
471 google_apis::ENTRY_KIND_FOLDER);
474 void APIUtilTest::TestCreateOriginDirectory_Conflict() {
475 fake_drive_service()->set_make_directory_conflict(true);
476 const std::string sync_root_id = SetUpSyncRootDirectory();
479 api_util()->GetDriveDirectoryForOrigin(
482 base::Bind(&DidGetResourceID, &output));
483 base::MessageLoop::current()->RunUntilIdle();
485 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
486 EXPECT_FALSE(output.resource_id.empty());
488 // Verify that there is no duplicated directory on the remote side.
489 VerifyTitleUniqueness(sync_root_id,
490 kOriginDirectoryName,
492 google_apis::ENTRY_KIND_FOLDER);
495 void APIUtilTest::TestGetLargestChangeStamp() {
496 LoadAccountMetadata();
499 api_util()->GetLargestChangeStamp(
500 base::Bind(&DidGetLargestChangeStamp, &output));
501 base::MessageLoop::current()->RunUntilIdle();
503 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
504 EXPECT_EQ(654321, output.largest_changestamp);
507 void APIUtilTest::TestListFiles() {
508 fake_drive_service()->set_default_max_results(3);
509 const std::string sync_root_id = SetUpSyncRootDirectory();
510 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
512 int kNumberOfFiles = 5;
513 for (int i = 0; i < kNumberOfFiles; ++i) {
514 scoped_ptr<ResourceEntry> file;
515 std::string file_content = base::StringPrintf("test content %d", i);
516 std::string file_title = base::StringPrintf("test_%d.txt", i);
517 SetUpFile(origin_root_id, file_content, file_title, &file);
520 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
521 scoped_ptr<ResourceList> document_feed;
522 api_util()->ListFiles(
524 base::Bind(&DidGetResourceList, &error, &document_feed));
525 base::MessageLoop::current()->RunUntilIdle();
527 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
528 EXPECT_EQ(3U, document_feed->entries().size());
531 ASSERT_TRUE(document_feed->GetNextFeedURL(&feed_url));
533 error = google_apis::GDATA_OTHER_ERROR;
534 document_feed.reset();
536 api_util()->ContinueListing(
538 base::Bind(&DidGetResourceList, &error, &document_feed));
539 base::MessageLoop::current()->RunUntilIdle();
541 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
542 EXPECT_EQ(2U, document_feed->entries().size());
545 void APIUtilTest::TestListChanges() {
546 const int64 kStartChangestamp = 6;
547 const std::string sync_root_id = SetUpSyncRootDirectory();
548 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
550 // Files should have changestamp #4+ since creating the sync root directory is
551 // #1, moving it out of 'My Drive' is #2, and creating the origin root
553 const int kNumberOfFiles = 5;
554 for (int i = 0; i < kNumberOfFiles; ++i) {
555 scoped_ptr<ResourceEntry> file;
556 std::string file_content = base::StringPrintf("test content %d", i);
557 std::string file_title = base::StringPrintf("test_%d.txt", i);
558 SetUpFile(origin_root_id, file_content, file_title, &file);
561 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
562 scoped_ptr<ResourceList> document_feed;
563 api_util()->ListFiles(
565 base::Bind(&DidGetResourceList, &error, &document_feed));
566 base::MessageLoop::current()->RunUntilIdle();
568 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
569 EXPECT_EQ(5U, document_feed->entries().size());
571 error = google_apis::GDATA_OTHER_ERROR;
572 document_feed.reset();
573 api_util()->ListChanges(
575 base::Bind(&DidGetResourceList, &error, &document_feed));
576 base::MessageLoop::current()->RunUntilIdle();
578 // There should be 3 files which have changestamp #6+.
579 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
580 EXPECT_EQ(3U, document_feed->entries().size());
583 void APIUtilTest::TestDownloadFile() {
584 const std::string kFileContent = "test content";
585 const std::string kFileTitle = "test.txt";
586 const std::string sync_root_id = SetUpSyncRootDirectory();
587 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
589 scoped_ptr<ResourceEntry> file;
590 SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
593 api_util()->DownloadFile(
595 "", // local_file_md5
596 base::Bind(&DidDownloadFile, &output));
597 base::MessageLoop::current()->RunUntilIdle();
599 EXPECT_EQ(file->file_md5(), output.file_md5);
600 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
603 void APIUtilTest::TestDownloadFileInNotModified() {
604 const std::string kFileContent = "test content";
605 const std::string kFileTitle = "test.txt";
606 const std::string sync_root_id = SetUpSyncRootDirectory();
607 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
609 scoped_ptr<ResourceEntry> file;
610 SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
612 // Since local file's hash value is equal to remote file's one, it is expected
613 // to cancel download the file and to return NOT_MODIFIED status code.
615 api_util()->DownloadFile(
618 base::Bind(&DidDownloadFile, &output));
619 base::MessageLoop::current()->RunUntilIdle();
621 EXPECT_EQ(file->file_md5(), output.file_md5);
622 EXPECT_EQ(google_apis::HTTP_NOT_MODIFIED, output.error);
625 void APIUtilTest::TestUploadNewFile() {
626 const std::string kFileTitle = "test.txt";
627 const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
628 const std::string sync_root_id = SetUpSyncRootDirectory();
629 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
632 api_util()->UploadNewFile(
636 base::Bind(&DidUploadFile, &output));
637 base::MessageLoop::current()->RunUntilIdle();
639 EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
640 EXPECT_TRUE(!output.resource_id.empty());
642 VerifyTitleUniqueness(origin_root_id,
645 google_apis::ENTRY_KIND_FILE);
648 void APIUtilTest::TestUploadNewFile_ConflictWithFile() {
649 const std::string kFileTitle = "test.txt";
650 const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
651 const std::string sync_root_id = SetUpSyncRootDirectory();
652 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
654 fake_drive_uploader()->set_make_file_conflict(true);
657 api_util()->UploadNewFile(
661 base::Bind(&DidUploadFile, &output));
662 base::MessageLoop::current()->RunUntilIdle();
664 // HTTP_CONFLICT error must be returned with empty resource_id.
665 EXPECT_EQ(google_apis::HTTP_CONFLICT, output.error);
666 EXPECT_TRUE(!output.resource_id.empty());
668 // Verify that there is no duplicated file on the remote side.
669 VerifyTitleUniqueness(origin_root_id,
672 google_apis::ENTRY_KIND_FILE);
675 void APIUtilTest::TestUploadExistingFile() {
676 const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
677 const std::string kFileContent = "test content";
678 const std::string kFileTitle = "test.txt";
679 const std::string sync_root_id = SetUpSyncRootDirectory();
680 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
682 scoped_ptr<ResourceEntry> file;
683 SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
686 api_util()->UploadExistingFile(
690 base::Bind(&DidUploadFile, &output));
691 base::MessageLoop::current()->RunUntilIdle();
693 EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
694 EXPECT_EQ(file->resource_id(), output.resource_id);
696 VerifyTitleUniqueness(origin_root_id,
702 void APIUtilTest::TestUploadExistingFileInConflict() {
703 const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
704 const std::string kFileContent = "test content";
705 const std::string kFileTitle = "test.txt";
706 const std::string sync_root_id = SetUpSyncRootDirectory();
707 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
709 scoped_ptr<ResourceEntry> file;
710 SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
712 // Since remote file's hash value is different from the expected one, it is
713 // expected to cancel upload the file and to return CONFLICT status code.
714 const std::string kExpectedRemoteFileMD5 = "123456";
717 api_util()->UploadExistingFile(
719 kExpectedRemoteFileMD5,
721 base::Bind(&DidUploadFile, &output));
722 base::MessageLoop::current()->RunUntilIdle();
724 EXPECT_EQ(google_apis::HTTP_CONFLICT, output.error);
725 EXPECT_TRUE(output.resource_id.empty());
727 // Verify that there is no duplicated file on the remote side.
728 VerifyTitleUniqueness(origin_root_id,
734 void APIUtilTest::TestDeleteFile() {
735 const std::string kFileContent = "test content";
736 const std::string kFileTitle = "test.txt";
737 const std::string sync_root_id = SetUpSyncRootDirectory();
738 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
740 scoped_ptr<ResourceEntry> file;
741 SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
743 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
744 api_util()->DeleteFile(file->resource_id(),
746 base::Bind(&DidDeleteFile, &error));
747 base::MessageLoop::current()->RunUntilIdle();
749 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
751 VerifyFileDeletion(origin_root_id, kFileTitle);
754 void APIUtilTest::TestDeleteFileInConflict() {
755 const std::string kFileContent = "test content";
756 const std::string kFileTitle = "test.txt";
757 const std::string sync_root_id = SetUpSyncRootDirectory();
758 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
760 scoped_ptr<ResourceEntry> file;
761 SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
763 // Since remote file's hash value is different from the expected one, it is
764 // expected to cancel delete the file and to return CONFLICT status code.
765 const std::string kExpectedRemoteFileMD5 = "123456";
767 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
768 api_util()->DeleteFile(file->resource_id(),
769 kExpectedRemoteFileMD5,
770 base::Bind(&DidDeleteFile, &error));
771 base::MessageLoop::current()->RunUntilIdle();
773 EXPECT_EQ(google_apis::HTTP_CONFLICT, error);
775 // Verify that the conflict file was not deleted on the remote side.
776 VerifyTitleUniqueness(origin_root_id,
782 void APIUtilTest::TestCreateDirectory() {
783 const std::string kDirectoryTitle("directory");
784 const std::string sync_root_id = SetUpSyncRootDirectory();
785 const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
788 api_util()->CreateDirectory(
791 base::Bind(&DidGetResourceID, &output));
792 base::MessageLoop::current()->RunUntilIdle();
794 EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
795 EXPECT_FALSE(output.resource_id.empty());
797 VerifyTitleUniqueness(origin_root_id,
800 google_apis::ENTRY_KIND_FOLDER);
803 TEST_F(APIUtilTest, GetSyncRoot) {
804 ASSERT_FALSE(IsDriveAPIDisabled());
808 TEST_F(APIUtilTest, GetSyncRoot_WAPI) {
809 ScopedDisableDriveAPI disable_drive_api;
813 TEST_F(APIUtilTest, CreateSyncRoot) {
814 ASSERT_FALSE(IsDriveAPIDisabled());
815 TestCreateSyncRoot();
818 TEST_F(APIUtilTest, CreateSyncRoot_WAPI) {
819 ScopedDisableDriveAPI disable_drive_api;
820 TestCreateSyncRoot();
823 TEST_F(APIUtilTest, CreateSyncRoot_Conflict) {
824 ASSERT_FALSE(IsDriveAPIDisabled());
825 TestCreateSyncRoot_Conflict();
828 TEST_F(APIUtilTest, CreateSyncRoot_Conflict_WAPI) {
829 ScopedDisableDriveAPI disable_drive_api;
830 TestCreateSyncRoot_Conflict();
833 TEST_F(APIUtilTest, GetOriginDirectory) {
834 ASSERT_FALSE(IsDriveAPIDisabled());
835 TestGetOriginDirectory();
838 TEST_F(APIUtilTest, GetOriginDirectory_WAPI) {
839 ScopedDisableDriveAPI disable_drive_api;
840 TestGetOriginDirectory();
843 TEST_F(APIUtilTest, CreateOriginDirectory) {
844 ASSERT_FALSE(IsDriveAPIDisabled());
845 TestCreateOriginDirectory();
848 TEST_F(APIUtilTest, CreateOriginDirectory_WAPI) {
849 ScopedDisableDriveAPI disable_drive_api;
850 TestCreateOriginDirectory();
853 TEST_F(APIUtilTest, CreateOriginDirectory_Conflict) {
854 ASSERT_FALSE(IsDriveAPIDisabled());
855 TestCreateOriginDirectory_Conflict();
858 TEST_F(APIUtilTest, CreateOriginDirectory_Conflict_WAPI) {
859 ScopedDisableDriveAPI disable_drive_api;
860 TestCreateOriginDirectory_Conflict();
863 TEST_F(APIUtilTest, GetLargestChangeStamp) {
864 ASSERT_FALSE(IsDriveAPIDisabled());
865 TestGetLargestChangeStamp();
868 TEST_F(APIUtilTest, GetLargestChangeStamp_WAPI) {
869 ScopedDisableDriveAPI disable_drive_api;
870 TestGetLargestChangeStamp();
873 TEST_F(APIUtilTest, ListFiles) {
874 ASSERT_FALSE(IsDriveAPIDisabled());
878 TEST_F(APIUtilTest, ListFiles_WAPI) {
879 ScopedDisableDriveAPI disable_drive_api;
883 TEST_F(APIUtilTest, ListChanges) {
884 ASSERT_FALSE(IsDriveAPIDisabled());
888 TEST_F(APIUtilTest, ListChanges_WAPI) {
889 ScopedDisableDriveAPI disable_drive_api;
893 TEST_F(APIUtilTest, DownloadFile) {
894 ASSERT_FALSE(IsDriveAPIDisabled());
898 TEST_F(APIUtilTest, DownloadFile_WAPI) {
899 ScopedDisableDriveAPI disable_drive_api;
903 TEST_F(APIUtilTest, DownloadFileInNotModified) {
904 ASSERT_FALSE(IsDriveAPIDisabled());
905 TestDownloadFileInNotModified();
908 TEST_F(APIUtilTest, DownloadFileInNotModified_WAPI) {
909 ScopedDisableDriveAPI disable_drive_api;
910 TestDownloadFileInNotModified();
913 TEST_F(APIUtilTest, UploadNewFile) {
914 ASSERT_FALSE(IsDriveAPIDisabled());
918 TEST_F(APIUtilTest, UploadNewFile_WAPI) {
919 ScopedDisableDriveAPI disable_drive_api;
923 TEST_F(APIUtilTest, UploadNewFile_ConflictWithFile) {
924 ASSERT_FALSE(IsDriveAPIDisabled());
925 TestUploadNewFile_ConflictWithFile();
928 TEST_F(APIUtilTest, UploadNewFile_ConflictWithFile_WAPI) {
929 ScopedDisableDriveAPI disable_drive_api;
930 TestUploadNewFile_ConflictWithFile();
933 TEST_F(APIUtilTest, UploadExistingFile) {
934 ASSERT_FALSE(IsDriveAPIDisabled());
935 TestUploadExistingFile();
938 TEST_F(APIUtilTest, UploadExistingFile_WAPI) {
939 ScopedDisableDriveAPI disable_drive_api;
940 TestUploadExistingFile();
943 TEST_F(APIUtilTest, UploadExistingFileInConflict) {
944 ASSERT_FALSE(IsDriveAPIDisabled());
945 TestUploadExistingFileInConflict();
948 TEST_F(APIUtilTest, UploadExistingFileInConflict_WAPI) {
949 ScopedDisableDriveAPI disable_drive_api;
950 TestUploadExistingFileInConflict();
953 TEST_F(APIUtilTest, DeleteFile) {
954 ASSERT_FALSE(IsDriveAPIDisabled());
958 TEST_F(APIUtilTest, DeleteFile_WAPI) {
959 ScopedDisableDriveAPI disable_drive_api;
963 TEST_F(APIUtilTest, DeleteFileInConflict) {
964 ASSERT_FALSE(IsDriveAPIDisabled());
965 TestDeleteFileInConflict();
968 TEST_F(APIUtilTest, DeleteFileInConflict_WAPI) {
969 ScopedDisableDriveAPI disable_drive_api;
970 TestDeleteFileInConflict();
973 TEST_F(APIUtilTest, CreateDirectory) {
974 ASSERT_FALSE(IsDriveAPIDisabled());
975 TestCreateDirectory();
978 TEST_F(APIUtilTest, CreateDirectory_WAPI) {
979 ScopedDisableDriveAPI disable_drive_api;
980 TestCreateDirectory();
983 } // namespace drive_backend
984 } // namespace sync_file_system