- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend_v1 / api_util_unittest.cc
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.
4
5 #include "chrome/browser/sync_file_system/drive_backend_v1/api_util.h"
6
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"
24
25 #define FPL(x) FILE_PATH_LITERAL(x)
26
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;
35
36 namespace sync_file_system {
37 namespace drive_backend {
38
39 namespace {
40
41 const char kOrigin[] = "chrome-extension://example";
42 const char kOriginDirectoryName[] = "example";
43
44 struct Output {
45   GDataErrorCode error;
46   std::string resource_id;
47   std::string file_md5;
48   int64 largest_changestamp;
49
50   Output() : error(google_apis::GDATA_OTHER_ERROR),
51              largest_changestamp(-1) {
52   }
53 };
54
55 void DidAddFileOrDirectoryForMakingConflict(GDataErrorCode error,
56                                             scoped_ptr<ResourceEntry> entry) {
57   ASSERT_EQ(google_apis::HTTP_CREATED, error);
58   ASSERT_TRUE(entry);
59 }
60
61 void DidAddFileForUploadNew(
62     const UploadCompletionCallback& callback,
63     GDataErrorCode error,
64     scoped_ptr<ResourceEntry> entry) {
65   ASSERT_EQ(google_apis::HTTP_CREATED, error);
66   ASSERT_TRUE(entry);
67   base::MessageLoopProxy::current()->PostTask(
68       FROM_HERE,
69       base::Bind(callback,
70                  google_apis::HTTP_SUCCESS,
71                  GURL(),
72                  base::Passed(&entry)));
73 }
74
75 void DidGetResourceEntryForUploadExisting(
76     const UploadCompletionCallback& callback,
77     GDataErrorCode error,
78     scoped_ptr<ResourceEntry> entry) {
79   ASSERT_EQ(google_apis::HTTP_SUCCESS, error);
80   ASSERT_TRUE(entry);
81   base::MessageLoopProxy::current()->PostTask(
82       FROM_HERE,
83       base::Bind(callback,
84                  google_apis::HTTP_SUCCESS,
85                  GURL(),
86                  base::Passed(&entry)));
87 }
88
89 class FakeDriveServiceWrapper : public FakeDriveService {
90  public:
91   FakeDriveServiceWrapper() : make_directory_conflict_(false) {}
92   virtual ~FakeDriveServiceWrapper() {}
93
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(
101           parent_resource_id,
102           directory_name,
103           base::Bind(&DidAddFileOrDirectoryForMakingConflict));
104     }
105     return FakeDriveService::AddNewDirectory(
106         parent_resource_id, directory_name, callback);
107   }
108
109   void set_make_directory_conflict(bool enable) {
110     make_directory_conflict_ = enable;
111   }
112
113  private:
114   bool make_directory_conflict_;
115
116   DISALLOW_COPY_AND_ASSIGN(FakeDriveServiceWrapper);
117 };
118
119 // A fake implementation of DriveUploaderInterface, which provides fake
120 // behaviors for file uploading.
121 class FakeDriveUploader : public DriveUploaderInterface {
122  public:
123   explicit FakeDriveUploader(FakeDriveServiceWrapper* fake_drive_service)
124       : fake_drive_service_(fake_drive_service),
125         make_file_conflict_(false) {}
126   virtual ~FakeDriveUploader() {}
127
128   // DriveUploaderInterface overrides.
129
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";
141
142     if (make_file_conflict_) {
143       fake_drive_service_->AddNewFile(
144           content_type,
145           kFileContent,
146           parent_resource_id,
147           title,
148           false,  // shared_with_me
149           base::Bind(&DidAddFileOrDirectoryForMakingConflict));
150     }
151
152     fake_drive_service_->AddNewFile(
153         content_type,
154         kFileContent,
155         parent_resource_id,
156         title,
157         false,  // shared_with_me
158         base::Bind(&DidAddFileForUploadNew, callback));
159     base::MessageLoop::current()->RunUntilIdle();
160
161     return google_apis::CancelCallback();
162   }
163
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(
175         resource_id,
176         base::Bind(&DidGetResourceEntryForUploadExisting, callback));
177   }
178
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 {
187     NOTREACHED();
188     return google_apis::CancelCallback();
189   }
190
191   void set_make_file_conflict(bool enable) {
192     make_file_conflict_ = enable;
193   }
194
195  private:
196   FakeDriveServiceWrapper* fake_drive_service_;
197   bool make_file_conflict_;
198
199   DISALLOW_COPY_AND_ASSIGN(FakeDriveUploader);
200 };
201
202 }  // namespace
203
204 class APIUtilTest : public testing::Test {
205  public:
206   APIUtilTest() : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
207                   fake_drive_service_(NULL),
208                   fake_drive_uploader_(NULL) {}
209
210   virtual void SetUp() OVERRIDE {
211     fake_drive_service_ = new FakeDriveServiceWrapper;
212     fake_drive_uploader_ = new FakeDriveUploader(fake_drive_service_);
213
214     fake_drive_helper_.reset(new FakeDriveServiceHelper(
215         fake_drive_service_, fake_drive_uploader_));
216
217     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
218     api_util_ = APIUtil::CreateForTesting(
219         temp_dir_.path(),
220         scoped_ptr<DriveServiceInterface>(fake_drive_service_),
221         scoped_ptr<DriveUploaderInterface>(fake_drive_uploader_));
222
223     fake_drive_service_->LoadResourceListForWapi(
224         "sync_file_system/initialize.json");
225   }
226
227   virtual void TearDown() OVERRIDE {
228     api_util_.reset();
229   }
230
231  protected:
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(),
237                   &sync_root_id));
238     return sync_root_id;
239   }
240
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(
245                   sync_root_id,
246                   kOriginDirectoryName,
247                   &origin_root_id));
248     return origin_root_id;
249   }
250
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) {
255     ASSERT_TRUE(entry);
256     std::string file_resource_id;
257     EXPECT_EQ(google_apis::HTTP_SUCCESS,
258               fake_drive_helper_->AddFile(
259                   origin_root_id,
260                   title,
261                   content_data,
262                   &file_resource_id));
263     EXPECT_EQ(google_apis::HTTP_SUCCESS,
264               fake_drive_helper_->GetResourceEntry(
265                   file_resource_id,
266                   entry));
267   }
268
269   void LoadAccountMetadata() {
270     fake_drive_service_->LoadAccountMetadataForWapi(
271         "sync_file_system/account_metadata.json");
272   }
273
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());
285   }
286
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());
294   }
295
296   APIUtil* api_util() { return api_util_.get(); }
297
298   FakeDriveServiceWrapper* fake_drive_service() {
299     return fake_drive_service_;
300   }
301
302   FakeDriveUploader* fake_drive_uploader() {
303     return fake_drive_uploader_;
304   }
305
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();
324
325  private:
326   content::TestBrowserThreadBundle thread_bundle_;
327
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_;
333
334   DISALLOW_COPY_AND_ASSIGN(APIUtilTest);
335 };
336
337 void DidGetResourceID(Output* output,
338                       GDataErrorCode error,
339                       const std::string& resource_id) {
340   ASSERT_TRUE(output);
341   output->error = error;
342   output->resource_id = resource_id;
343 }
344
345 void DidGetLargestChangeStamp(Output* output,
346                               GDataErrorCode error,
347                               int64 largest_changestamp) {
348   ASSERT_TRUE(output);
349   output->error = error;
350   output->largest_changestamp = largest_changestamp;
351 }
352
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);
359   *error_out = error;
360   *document_feed_out = document_feed.Pass();
361 }
362
363 void DidDownloadFile(Output* output,
364                      GDataErrorCode error,
365                      const std::string& file_md5,
366                      int64 file_size,
367                      const base::Time& updated_time,
368                      webkit_blob::ScopedFile file) {
369   ASSERT_TRUE(output);
370   ASSERT_TRUE(base::PathExists(file.path()));
371   output->error = error;
372   output->file_md5 = file_md5;
373 }
374
375 void DidUploadFile(Output* output,
376                    GDataErrorCode error,
377                    const std::string& resource_id,
378                    const std::string& file_md5) {
379   ASSERT_TRUE(output);
380   output->error = error;
381   output->resource_id = resource_id;
382   output->file_md5 = file_md5;
383 }
384
385 void DidDeleteFile(GDataErrorCode* error_out,
386                    GDataErrorCode error) {
387   ASSERT_TRUE(error);
388   *error_out = error;
389 }
390
391 void APIUtilTest::TestGetSyncRoot() {
392   LoadAccountMetadata();
393   const std::string sync_root_id = SetUpSyncRootDirectory();
394
395   Output output;
396   api_util()->GetDriveDirectoryForSyncRoot(
397       base::Bind(&DidGetResourceID, &output));
398   base::MessageLoop::current()->RunUntilIdle();
399
400   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
401   EXPECT_EQ(sync_root_id, output.resource_id);
402 }
403
404 void APIUtilTest::TestCreateSyncRoot() {
405   LoadAccountMetadata();
406
407   Output output;
408   api_util()->GetDriveDirectoryForSyncRoot(
409       base::Bind(&DidGetResourceID, &output));
410   base::MessageLoop::current()->RunUntilIdle();
411
412   EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
413   EXPECT_FALSE(output.resource_id.empty());
414
415   VerifyTitleUniqueness(std::string(),  // directory_resource_id
416                         APIUtil::GetSyncRootDirectoryName(),
417                         output.resource_id,
418                         google_apis::ENTRY_KIND_FOLDER);
419 }
420
421 void APIUtilTest::TestCreateSyncRoot_Conflict() {
422   LoadAccountMetadata();
423   fake_drive_service()->set_make_directory_conflict(true);
424
425   Output output;
426   api_util()->GetDriveDirectoryForSyncRoot(
427       base::Bind(&DidGetResourceID, &output));
428   base::MessageLoop::current()->RunUntilIdle();
429
430   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
431   EXPECT_FALSE(output.resource_id.empty());
432
433   // Verify that there is no duplicated directory on the remote side.
434   VerifyTitleUniqueness(std::string(),  // directory_resource_id
435                         APIUtil::GetSyncRootDirectoryName(),
436                         output.resource_id,
437                         google_apis::ENTRY_KIND_FOLDER);
438 }
439
440 void APIUtilTest::TestGetOriginDirectory() {
441   const std::string sync_root_id = SetUpSyncRootDirectory();
442   const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
443
444   Output output;
445   api_util()->GetDriveDirectoryForOrigin(
446       sync_root_id,
447       GURL(kOrigin),
448       base::Bind(&DidGetResourceID, &output));
449   base::MessageLoop::current()->RunUntilIdle();
450
451   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
452   EXPECT_EQ(origin_root_id, output.resource_id);
453 }
454
455 void APIUtilTest::TestCreateOriginDirectory() {
456   const std::string& sync_root_id = SetUpSyncRootDirectory();
457
458   Output output;
459   api_util()->GetDriveDirectoryForOrigin(
460       sync_root_id,
461       GURL(kOrigin),
462       base::Bind(&DidGetResourceID, &output));
463   base::MessageLoop::current()->RunUntilIdle();
464
465   EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
466   EXPECT_FALSE(output.resource_id.empty());
467
468   VerifyTitleUniqueness(sync_root_id,
469                         kOriginDirectoryName,
470                         output.resource_id,
471                         google_apis::ENTRY_KIND_FOLDER);
472 }
473
474 void APIUtilTest::TestCreateOriginDirectory_Conflict() {
475   fake_drive_service()->set_make_directory_conflict(true);
476   const std::string sync_root_id = SetUpSyncRootDirectory();
477
478   Output output;
479   api_util()->GetDriveDirectoryForOrigin(
480       sync_root_id,
481       GURL(kOrigin),
482       base::Bind(&DidGetResourceID, &output));
483   base::MessageLoop::current()->RunUntilIdle();
484
485   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
486   EXPECT_FALSE(output.resource_id.empty());
487
488   // Verify that there is no duplicated directory on the remote side.
489   VerifyTitleUniqueness(sync_root_id,
490                         kOriginDirectoryName,
491                         output.resource_id,
492                         google_apis::ENTRY_KIND_FOLDER);
493 }
494
495 void APIUtilTest::TestGetLargestChangeStamp() {
496   LoadAccountMetadata();
497
498   Output output;
499   api_util()->GetLargestChangeStamp(
500       base::Bind(&DidGetLargestChangeStamp, &output));
501   base::MessageLoop::current()->RunUntilIdle();
502
503   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
504   EXPECT_EQ(654321, output.largest_changestamp);
505 }
506
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);
511
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);
518   }
519
520   GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
521   scoped_ptr<ResourceList> document_feed;
522   api_util()->ListFiles(
523       origin_root_id,
524       base::Bind(&DidGetResourceList, &error, &document_feed));
525   base::MessageLoop::current()->RunUntilIdle();
526
527   EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
528   EXPECT_EQ(3U, document_feed->entries().size());
529
530   GURL feed_url;
531   ASSERT_TRUE(document_feed->GetNextFeedURL(&feed_url));
532
533   error = google_apis::GDATA_OTHER_ERROR;
534   document_feed.reset();
535
536   api_util()->ContinueListing(
537       feed_url,
538       base::Bind(&DidGetResourceList, &error, &document_feed));
539   base::MessageLoop::current()->RunUntilIdle();
540
541   EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
542   EXPECT_EQ(2U, document_feed->entries().size());
543 }
544
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);
549
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
552   // directory is #3.
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);
559   }
560
561   GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
562   scoped_ptr<ResourceList> document_feed;
563   api_util()->ListFiles(
564       origin_root_id,
565       base::Bind(&DidGetResourceList, &error, &document_feed));
566   base::MessageLoop::current()->RunUntilIdle();
567
568   EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
569   EXPECT_EQ(5U, document_feed->entries().size());
570
571   error = google_apis::GDATA_OTHER_ERROR;
572   document_feed.reset();
573   api_util()->ListChanges(
574       kStartChangestamp,
575       base::Bind(&DidGetResourceList, &error, &document_feed));
576   base::MessageLoop::current()->RunUntilIdle();
577
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());
581 }
582
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);
588
589   scoped_ptr<ResourceEntry> file;
590   SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
591
592   Output output;
593   api_util()->DownloadFile(
594       file->resource_id(),
595       "",  // local_file_md5
596       base::Bind(&DidDownloadFile, &output));
597   base::MessageLoop::current()->RunUntilIdle();
598
599   EXPECT_EQ(file->file_md5(), output.file_md5);
600   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
601 }
602
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);
608
609   scoped_ptr<ResourceEntry> file;
610   SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
611
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.
614   Output output;
615   api_util()->DownloadFile(
616       file->resource_id(),
617       file->file_md5(),
618       base::Bind(&DidDownloadFile, &output));
619   base::MessageLoop::current()->RunUntilIdle();
620
621   EXPECT_EQ(file->file_md5(), output.file_md5);
622   EXPECT_EQ(google_apis::HTTP_NOT_MODIFIED, output.error);
623 }
624
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);
630
631   Output output;
632   api_util()->UploadNewFile(
633       origin_root_id,
634       kLocalFilePath,
635       kFileTitle,
636       base::Bind(&DidUploadFile, &output));
637   base::MessageLoop::current()->RunUntilIdle();
638
639   EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
640   EXPECT_TRUE(!output.resource_id.empty());
641
642   VerifyTitleUniqueness(origin_root_id,
643                         kFileTitle,
644                         output.resource_id,
645                         google_apis::ENTRY_KIND_FILE);
646 }
647
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);
653
654   fake_drive_uploader()->set_make_file_conflict(true);
655
656   Output output;
657   api_util()->UploadNewFile(
658       origin_root_id,
659       kLocalFilePath,
660       kFileTitle,
661       base::Bind(&DidUploadFile, &output));
662   base::MessageLoop::current()->RunUntilIdle();
663
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());
667
668   // Verify that there is no duplicated file on the remote side.
669   VerifyTitleUniqueness(origin_root_id,
670                         kFileTitle,
671                         output.resource_id,
672                         google_apis::ENTRY_KIND_FILE);
673 }
674
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);
681
682   scoped_ptr<ResourceEntry> file;
683   SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
684
685   Output output;
686   api_util()->UploadExistingFile(
687       file->resource_id(),
688       file->file_md5(),
689       kLocalFilePath,
690       base::Bind(&DidUploadFile, &output));
691   base::MessageLoop::current()->RunUntilIdle();
692
693   EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
694   EXPECT_EQ(file->resource_id(), output.resource_id);
695
696   VerifyTitleUniqueness(origin_root_id,
697                         file->title(),
698                         file->resource_id(),
699                         file->kind());
700 }
701
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);
708
709   scoped_ptr<ResourceEntry> file;
710   SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
711
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";
715
716   Output output;
717   api_util()->UploadExistingFile(
718       file->resource_id(),
719       kExpectedRemoteFileMD5,
720       kLocalFilePath,
721       base::Bind(&DidUploadFile, &output));
722   base::MessageLoop::current()->RunUntilIdle();
723
724   EXPECT_EQ(google_apis::HTTP_CONFLICT, output.error);
725   EXPECT_TRUE(output.resource_id.empty());
726
727   // Verify that there is no duplicated file on the remote side.
728   VerifyTitleUniqueness(origin_root_id,
729                         file->title(),
730                         file->resource_id(),
731                         file->kind());
732 }
733
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);
739
740   scoped_ptr<ResourceEntry> file;
741   SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
742
743   GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
744   api_util()->DeleteFile(file->resource_id(),
745                          file->file_md5(),
746                          base::Bind(&DidDeleteFile, &error));
747   base::MessageLoop::current()->RunUntilIdle();
748
749   EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
750
751   VerifyFileDeletion(origin_root_id, kFileTitle);
752 }
753
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);
759
760   scoped_ptr<ResourceEntry> file;
761   SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
762
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";
766
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();
772
773   EXPECT_EQ(google_apis::HTTP_CONFLICT, error);
774
775   // Verify that the conflict file was not deleted on the remote side.
776   VerifyTitleUniqueness(origin_root_id,
777                         file->title(),
778                         file->resource_id(),
779                         file->kind());
780 }
781
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);
786
787   Output output;
788   api_util()->CreateDirectory(
789       origin_root_id,
790       kDirectoryTitle,
791       base::Bind(&DidGetResourceID, &output));
792   base::MessageLoop::current()->RunUntilIdle();
793
794   EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
795   EXPECT_FALSE(output.resource_id.empty());
796
797   VerifyTitleUniqueness(origin_root_id,
798                         kDirectoryTitle,
799                         output.resource_id,
800                         google_apis::ENTRY_KIND_FOLDER);
801 }
802
803 TEST_F(APIUtilTest, GetSyncRoot) {
804   ASSERT_FALSE(IsDriveAPIDisabled());
805   TestGetSyncRoot();
806 }
807
808 TEST_F(APIUtilTest, GetSyncRoot_WAPI) {
809   ScopedDisableDriveAPI disable_drive_api;
810   TestGetSyncRoot();
811 }
812
813 TEST_F(APIUtilTest, CreateSyncRoot) {
814   ASSERT_FALSE(IsDriveAPIDisabled());
815   TestCreateSyncRoot();
816 }
817
818 TEST_F(APIUtilTest, CreateSyncRoot_WAPI) {
819   ScopedDisableDriveAPI disable_drive_api;
820   TestCreateSyncRoot();
821 }
822
823 TEST_F(APIUtilTest, CreateSyncRoot_Conflict) {
824   ASSERT_FALSE(IsDriveAPIDisabled());
825   TestCreateSyncRoot_Conflict();
826 }
827
828 TEST_F(APIUtilTest, CreateSyncRoot_Conflict_WAPI) {
829   ScopedDisableDriveAPI disable_drive_api;
830   TestCreateSyncRoot_Conflict();
831 }
832
833 TEST_F(APIUtilTest, GetOriginDirectory) {
834   ASSERT_FALSE(IsDriveAPIDisabled());
835   TestGetOriginDirectory();
836 }
837
838 TEST_F(APIUtilTest, GetOriginDirectory_WAPI) {
839   ScopedDisableDriveAPI disable_drive_api;
840   TestGetOriginDirectory();
841 }
842
843 TEST_F(APIUtilTest, CreateOriginDirectory) {
844   ASSERT_FALSE(IsDriveAPIDisabled());
845   TestCreateOriginDirectory();
846 }
847
848 TEST_F(APIUtilTest, CreateOriginDirectory_WAPI) {
849   ScopedDisableDriveAPI disable_drive_api;
850   TestCreateOriginDirectory();
851 }
852
853 TEST_F(APIUtilTest, CreateOriginDirectory_Conflict) {
854   ASSERT_FALSE(IsDriveAPIDisabled());
855   TestCreateOriginDirectory_Conflict();
856 }
857
858 TEST_F(APIUtilTest, CreateOriginDirectory_Conflict_WAPI) {
859   ScopedDisableDriveAPI disable_drive_api;
860   TestCreateOriginDirectory_Conflict();
861 }
862
863 TEST_F(APIUtilTest, GetLargestChangeStamp) {
864   ASSERT_FALSE(IsDriveAPIDisabled());
865   TestGetLargestChangeStamp();
866 }
867
868 TEST_F(APIUtilTest, GetLargestChangeStamp_WAPI) {
869   ScopedDisableDriveAPI disable_drive_api;
870   TestGetLargestChangeStamp();
871 }
872
873 TEST_F(APIUtilTest, ListFiles) {
874   ASSERT_FALSE(IsDriveAPIDisabled());
875   TestListFiles();
876 }
877
878 TEST_F(APIUtilTest, ListFiles_WAPI) {
879   ScopedDisableDriveAPI disable_drive_api;
880   TestListFiles();
881 }
882
883 TEST_F(APIUtilTest, ListChanges) {
884   ASSERT_FALSE(IsDriveAPIDisabled());
885   TestListChanges();
886 }
887
888 TEST_F(APIUtilTest, ListChanges_WAPI) {
889   ScopedDisableDriveAPI disable_drive_api;
890   TestListChanges();
891 }
892
893 TEST_F(APIUtilTest, DownloadFile) {
894   ASSERT_FALSE(IsDriveAPIDisabled());
895   TestDownloadFile();
896 }
897
898 TEST_F(APIUtilTest, DownloadFile_WAPI) {
899   ScopedDisableDriveAPI disable_drive_api;
900   TestDownloadFile();
901 }
902
903 TEST_F(APIUtilTest, DownloadFileInNotModified) {
904   ASSERT_FALSE(IsDriveAPIDisabled());
905   TestDownloadFileInNotModified();
906 }
907
908 TEST_F(APIUtilTest, DownloadFileInNotModified_WAPI) {
909   ScopedDisableDriveAPI disable_drive_api;
910   TestDownloadFileInNotModified();
911 }
912
913 TEST_F(APIUtilTest, UploadNewFile) {
914   ASSERT_FALSE(IsDriveAPIDisabled());
915   TestUploadNewFile();
916 }
917
918 TEST_F(APIUtilTest, UploadNewFile_WAPI) {
919   ScopedDisableDriveAPI disable_drive_api;
920   TestUploadNewFile();
921 }
922
923 TEST_F(APIUtilTest, UploadNewFile_ConflictWithFile) {
924   ASSERT_FALSE(IsDriveAPIDisabled());
925   TestUploadNewFile_ConflictWithFile();
926 }
927
928 TEST_F(APIUtilTest, UploadNewFile_ConflictWithFile_WAPI) {
929   ScopedDisableDriveAPI disable_drive_api;
930   TestUploadNewFile_ConflictWithFile();
931 }
932
933 TEST_F(APIUtilTest, UploadExistingFile) {
934   ASSERT_FALSE(IsDriveAPIDisabled());
935   TestUploadExistingFile();
936 }
937
938 TEST_F(APIUtilTest, UploadExistingFile_WAPI) {
939   ScopedDisableDriveAPI disable_drive_api;
940   TestUploadExistingFile();
941 }
942
943 TEST_F(APIUtilTest, UploadExistingFileInConflict) {
944   ASSERT_FALSE(IsDriveAPIDisabled());
945   TestUploadExistingFileInConflict();
946 }
947
948 TEST_F(APIUtilTest, UploadExistingFileInConflict_WAPI) {
949   ScopedDisableDriveAPI disable_drive_api;
950   TestUploadExistingFileInConflict();
951 }
952
953 TEST_F(APIUtilTest, DeleteFile) {
954   ASSERT_FALSE(IsDriveAPIDisabled());
955   TestDeleteFile();
956 }
957
958 TEST_F(APIUtilTest, DeleteFile_WAPI) {
959   ScopedDisableDriveAPI disable_drive_api;
960   TestDeleteFile();
961 }
962
963 TEST_F(APIUtilTest, DeleteFileInConflict) {
964   ASSERT_FALSE(IsDriveAPIDisabled());
965   TestDeleteFileInConflict();
966 }
967
968 TEST_F(APIUtilTest, DeleteFileInConflict_WAPI) {
969   ScopedDisableDriveAPI disable_drive_api;
970   TestDeleteFileInConflict();
971 }
972
973 TEST_F(APIUtilTest, CreateDirectory) {
974   ASSERT_FALSE(IsDriveAPIDisabled());
975   TestCreateDirectory();
976 }
977
978 TEST_F(APIUtilTest, CreateDirectory_WAPI) {
979   ScopedDisableDriveAPI disable_drive_api;
980   TestCreateDirectory();
981 }
982
983 }  // namespace drive_backend
984 }  // namespace sync_file_system