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/sync_file_system/drive_backend/fake_drive_service_helper.h"
16#include "chrome/browser/sync_file_system/drive_backend/fake_drive_uploader.h"
17#include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_util.h"
18#include "content/public/test/test_browser_thread.h"
19#include "content/public/test/test_browser_thread_bundle.h"
20#include "google_apis/drive/drive_api_parser.h"
21#include "google_apis/drive/gdata_errorcode.h"
22#include "google_apis/drive/test_util.h"
23#include "net/base/escape.h"
24#include "testing/gtest/include/gtest/gtest.h"
25
26#define FPL(x) FILE_PATH_LITERAL(x)
27
28using drive::DriveServiceInterface;
29using drive::DriveUploaderInterface;
30using google_apis::FileResource;
31using google_apis::GDataErrorCode;
32using google_apis::ResourceEntry;
33using google_apis::ResourceList;
34
35namespace sync_file_system {
36namespace drive_backend {
37
38namespace {
39
40const char kOrigin[] = "chrome-extension://example";
41const char kOriginDirectoryName[] = "example";
42
43struct Output {
44  GDataErrorCode error;
45  std::string resource_id;
46  std::string file_md5;
47  int64 largest_changestamp;
48
49  Output() : error(google_apis::GDATA_OTHER_ERROR),
50             largest_changestamp(-1) {
51  }
52};
53
54}  // namespace
55
56class APIUtilTest : public testing::Test {
57 public:
58  APIUtilTest() : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
59                  fake_drive_service_(NULL),
60                  fake_drive_uploader_(NULL) {}
61
62  virtual void SetUp() OVERRIDE {
63    fake_drive_service_ = new FakeDriveServiceWrapper;
64    fake_drive_uploader_ = new FakeDriveUploader(fake_drive_service_);
65
66    fake_drive_helper_.reset(new FakeDriveServiceHelper(
67        fake_drive_service_, fake_drive_uploader_,
68        APIUtil::GetSyncRootDirectoryName()));
69
70    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
71    api_util_ = APIUtil::CreateForTesting(
72        temp_dir_.path(),
73        scoped_ptr<DriveServiceInterface>(fake_drive_service_),
74        scoped_ptr<DriveUploaderInterface>(fake_drive_uploader_));
75  }
76
77  virtual void TearDown() OVERRIDE {
78    api_util_.reset();
79  }
80
81 protected:
82  std::string SetUpSyncRootDirectory() {
83    std::string sync_root_id;
84    EXPECT_EQ(google_apis::HTTP_CREATED,
85              fake_drive_helper_->AddOrphanedFolder(
86                  APIUtil::GetSyncRootDirectoryName(),
87                  &sync_root_id));
88    return sync_root_id;
89  }
90
91  std::string SetUpOriginRootDirectory(const std::string& sync_root_id) {
92    std::string origin_root_id;
93    EXPECT_EQ(google_apis::HTTP_CREATED,
94              fake_drive_helper_->AddFolder(
95                  sync_root_id,
96                  kOriginDirectoryName,
97                  &origin_root_id));
98    return origin_root_id;
99  }
100
101  void SetUpFile(const std::string& origin_root_id,
102                 const std::string& content_data,
103                 const std::string& title,
104                 scoped_ptr<FileResource>* entry) {
105    ASSERT_TRUE(entry);
106    std::string file_resource_id;
107    EXPECT_EQ(google_apis::HTTP_SUCCESS,
108              fake_drive_helper_->AddFile(
109                  origin_root_id,
110                  title,
111                  content_data,
112                  &file_resource_id));
113    EXPECT_EQ(google_apis::HTTP_SUCCESS,
114              fake_drive_helper_->GetFileResource(
115                  file_resource_id,
116                  entry));
117  }
118
119  void VerifyTitleUniqueness(const std::string& parent_resource_id,
120                             const std::string& title,
121                             const std::string& resource_id,
122                             google_apis::DriveEntryKind kind) {
123    ScopedVector<ResourceEntry> entries;
124    EXPECT_EQ(google_apis::HTTP_SUCCESS,
125              fake_drive_helper_->SearchByTitle(
126                  parent_resource_id, title, &entries));
127    ASSERT_EQ(1u, entries.size());
128    EXPECT_EQ(resource_id, entries[0]->resource_id());
129    EXPECT_EQ(kind, entries[0]->kind());
130  }
131
132  void VerifyFileDeletion(const std::string& parent_resource_id,
133                          const std::string& title) {
134    ScopedVector<ResourceEntry> entries;
135    EXPECT_EQ(google_apis::HTTP_SUCCESS,
136              fake_drive_helper_->SearchByTitle(
137                  parent_resource_id, title, &entries));
138    EXPECT_TRUE(entries.empty());
139  }
140
141  APIUtil* api_util() { return api_util_.get(); }
142
143  FakeDriveServiceWrapper* fake_drive_service() {
144    return fake_drive_service_;
145  }
146
147  FakeDriveUploader* fake_drive_uploader() {
148    return fake_drive_uploader_;
149  }
150
151  void TestGetSyncRoot();
152  void TestCreateSyncRoot();
153  void TestCreateSyncRoot_Conflict();
154  void TestGetOriginDirectory();
155  void TestCreateOriginDirectory();
156  void TestCreateOriginDirectory_Conflict();
157  void TestGetLargestChangeStamp();
158  void TestListFiles();
159  void TestListChanges();
160  void TestDownloadFile();
161  void TestDownloadFileInNotModified();
162  void TestUploadNewFile();
163  void TestUploadNewFile_ConflictWithFile();
164  void TestUploadExistingFile();
165  void TestUploadExistingFileInConflict();
166  void TestDeleteFile();
167  void TestDeleteFileInConflict();
168  void TestCreateDirectory();
169
170 private:
171  content::TestBrowserThreadBundle thread_bundle_;
172
173  base::ScopedTempDir temp_dir_;
174  scoped_ptr<APIUtil> api_util_;
175  FakeDriveServiceWrapper* fake_drive_service_;
176  FakeDriveUploader* fake_drive_uploader_;
177  scoped_ptr<FakeDriveServiceHelper> fake_drive_helper_;
178
179  DISALLOW_COPY_AND_ASSIGN(APIUtilTest);
180};
181
182void DidGetResourceID(Output* output,
183                      GDataErrorCode error,
184                      const std::string& resource_id) {
185  ASSERT_TRUE(output);
186  output->error = error;
187  output->resource_id = resource_id;
188}
189
190void DidGetLargestChangeStamp(Output* output,
191                              GDataErrorCode error,
192                              int64 largest_changestamp) {
193  ASSERT_TRUE(output);
194  output->error = error;
195  output->largest_changestamp = largest_changestamp;
196}
197
198void DidGetResourceList(GDataErrorCode* error_out,
199                        scoped_ptr<ResourceList>* document_feed_out,
200                        GDataErrorCode error,
201                        scoped_ptr<ResourceList> document_feed) {
202  ASSERT_TRUE(error_out);
203  ASSERT_TRUE(document_feed_out);
204  *error_out = error;
205  *document_feed_out = document_feed.Pass();
206}
207
208void DidDownloadFile(Output* output,
209                     GDataErrorCode error,
210                     const std::string& file_md5,
211                     int64 file_size,
212                     const base::Time& updated_time,
213                     webkit_blob::ScopedFile file) {
214  ASSERT_TRUE(output);
215  ASSERT_TRUE(base::PathExists(file.path()));
216  output->error = error;
217  output->file_md5 = file_md5;
218}
219
220void DidUploadFile(Output* output,
221                   GDataErrorCode error,
222                   const std::string& resource_id,
223                   const std::string& file_md5) {
224  ASSERT_TRUE(output);
225  output->error = error;
226  output->resource_id = resource_id;
227  output->file_md5 = file_md5;
228}
229
230void DidDeleteFile(GDataErrorCode* error_out,
231                   GDataErrorCode error) {
232  ASSERT_TRUE(error);
233  *error_out = error;
234}
235
236void APIUtilTest::TestGetSyncRoot() {
237  const std::string sync_root_id = SetUpSyncRootDirectory();
238
239  Output output;
240  api_util()->GetDriveDirectoryForSyncRoot(
241      base::Bind(&DidGetResourceID, &output));
242  base::MessageLoop::current()->RunUntilIdle();
243
244  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
245  EXPECT_EQ(sync_root_id, output.resource_id);
246}
247
248void APIUtilTest::TestCreateSyncRoot() {
249  Output output;
250  api_util()->GetDriveDirectoryForSyncRoot(
251      base::Bind(&DidGetResourceID, &output));
252  base::MessageLoop::current()->RunUntilIdle();
253
254  EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
255  EXPECT_FALSE(output.resource_id.empty());
256
257  VerifyTitleUniqueness(std::string(),  // directory_resource_id
258                        APIUtil::GetSyncRootDirectoryName(),
259                        output.resource_id,
260                        google_apis::ENTRY_KIND_FOLDER);
261}
262
263void APIUtilTest::TestCreateSyncRoot_Conflict() {
264  fake_drive_service()->set_make_directory_conflict(true);
265
266  Output output;
267  api_util()->GetDriveDirectoryForSyncRoot(
268      base::Bind(&DidGetResourceID, &output));
269  base::MessageLoop::current()->RunUntilIdle();
270
271  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
272  EXPECT_FALSE(output.resource_id.empty());
273
274  // Verify that there is no duplicated directory on the remote side.
275  VerifyTitleUniqueness(std::string(),  // directory_resource_id
276                        APIUtil::GetSyncRootDirectoryName(),
277                        output.resource_id,
278                        google_apis::ENTRY_KIND_FOLDER);
279}
280
281void APIUtilTest::TestGetOriginDirectory() {
282  const std::string sync_root_id = SetUpSyncRootDirectory();
283  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
284
285  Output output;
286  api_util()->GetDriveDirectoryForOrigin(
287      sync_root_id,
288      GURL(kOrigin),
289      base::Bind(&DidGetResourceID, &output));
290  base::MessageLoop::current()->RunUntilIdle();
291
292  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
293  EXPECT_EQ(origin_root_id, output.resource_id);
294}
295
296void APIUtilTest::TestCreateOriginDirectory() {
297  const std::string& sync_root_id = SetUpSyncRootDirectory();
298
299  Output output;
300  api_util()->GetDriveDirectoryForOrigin(
301      sync_root_id,
302      GURL(kOrigin),
303      base::Bind(&DidGetResourceID, &output));
304  base::MessageLoop::current()->RunUntilIdle();
305
306  EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
307  EXPECT_FALSE(output.resource_id.empty());
308
309  VerifyTitleUniqueness(sync_root_id,
310                        kOriginDirectoryName,
311                        output.resource_id,
312                        google_apis::ENTRY_KIND_FOLDER);
313}
314
315void APIUtilTest::TestCreateOriginDirectory_Conflict() {
316  fake_drive_service()->set_make_directory_conflict(true);
317  const std::string sync_root_id = SetUpSyncRootDirectory();
318
319  Output output;
320  api_util()->GetDriveDirectoryForOrigin(
321      sync_root_id,
322      GURL(kOrigin),
323      base::Bind(&DidGetResourceID, &output));
324  base::MessageLoop::current()->RunUntilIdle();
325
326  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
327  EXPECT_FALSE(output.resource_id.empty());
328
329  // Verify that there is no duplicated directory on the remote side.
330  VerifyTitleUniqueness(sync_root_id,
331                        kOriginDirectoryName,
332                        output.resource_id,
333                        google_apis::ENTRY_KIND_FOLDER);
334}
335
336void APIUtilTest::TestGetLargestChangeStamp() {
337  Output output;
338  api_util()->GetLargestChangeStamp(
339      base::Bind(&DidGetLargestChangeStamp, &output));
340  base::MessageLoop::current()->RunUntilIdle();
341
342  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
343  EXPECT_EQ(fake_drive_service()->about_resource().largest_change_id(),
344            output.largest_changestamp);
345}
346
347void APIUtilTest::TestListFiles() {
348  fake_drive_service()->set_default_max_results(3);
349  const std::string sync_root_id = SetUpSyncRootDirectory();
350  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
351
352  int kNumberOfFiles = 5;
353  for (int i = 0; i < kNumberOfFiles; ++i) {
354    scoped_ptr<FileResource> file;
355    std::string file_content = base::StringPrintf("test content %d", i);
356    std::string file_title = base::StringPrintf("test_%d.txt", i);
357    SetUpFile(origin_root_id, file_content, file_title, &file);
358  }
359
360  GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
361  scoped_ptr<ResourceList> document_feed;
362  api_util()->ListFiles(
363      origin_root_id,
364      base::Bind(&DidGetResourceList, &error, &document_feed));
365  base::MessageLoop::current()->RunUntilIdle();
366
367  EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
368  EXPECT_EQ(3U, document_feed->entries().size());
369
370  GURL feed_url;
371  ASSERT_TRUE(document_feed->GetNextFeedURL(&feed_url));
372
373  error = google_apis::GDATA_OTHER_ERROR;
374  document_feed.reset();
375
376  api_util()->ContinueListing(
377      feed_url,
378      base::Bind(&DidGetResourceList, &error, &document_feed));
379  base::MessageLoop::current()->RunUntilIdle();
380
381  EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
382  EXPECT_EQ(2U, document_feed->entries().size());
383}
384
385void APIUtilTest::TestListChanges() {
386  const int64 old_changestamp =
387      fake_drive_service()->about_resource().largest_change_id();
388  const std::string sync_root_id = SetUpSyncRootDirectory();
389  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
390
391  // Files should have changestamp #4+ since creating the sync root directory is
392  // #1, moving it out of 'My Drive' is #2, and creating the origin root
393  // directory is #3.
394  const int kNumberOfFiles = 5;
395  for (int i = 0; i < kNumberOfFiles; ++i) {
396    scoped_ptr<FileResource> file;
397    std::string file_content = base::StringPrintf("test content %d", i);
398    std::string file_title = base::StringPrintf("test_%d.txt", i);
399    SetUpFile(origin_root_id, file_content, file_title, &file);
400  }
401
402  GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
403  scoped_ptr<ResourceList> document_feed;
404  api_util()->ListFiles(
405      origin_root_id,
406      base::Bind(&DidGetResourceList, &error, &document_feed));
407  base::MessageLoop::current()->RunUntilIdle();
408
409  EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
410  EXPECT_EQ(5U, document_feed->entries().size());
411
412  error = google_apis::GDATA_OTHER_ERROR;
413  document_feed.reset();
414  api_util()->ListChanges(
415      old_changestamp + 6,
416      base::Bind(&DidGetResourceList, &error, &document_feed));
417  base::MessageLoop::current()->RunUntilIdle();
418
419  // There should be 3 files which have changestamp #6+.
420  EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
421  EXPECT_EQ(3U, document_feed->entries().size());
422}
423
424void APIUtilTest::TestDownloadFile() {
425  const std::string kFileContent = "test content";
426  const std::string kFileTitle = "test.txt";
427  const std::string sync_root_id = SetUpSyncRootDirectory();
428  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
429
430  scoped_ptr<FileResource> file;
431  SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
432
433  Output output;
434  api_util()->DownloadFile(
435      file->file_id(),
436      "",  // local_file_md5
437      base::Bind(&DidDownloadFile, &output));
438  base::MessageLoop::current()->RunUntilIdle();
439
440  EXPECT_EQ(file->md5_checksum(), output.file_md5);
441  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
442}
443
444void APIUtilTest::TestDownloadFileInNotModified() {
445  const std::string kFileContent = "test content";
446  const std::string kFileTitle = "test.txt";
447  const std::string sync_root_id = SetUpSyncRootDirectory();
448  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
449
450  scoped_ptr<FileResource> file;
451  SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
452
453  // Since local file's hash value is equal to remote file's one, it is expected
454  // to cancel download the file and to return NOT_MODIFIED status code.
455  Output output;
456  api_util()->DownloadFile(
457      file->file_id(),
458      file->md5_checksum(),
459      base::Bind(&DidDownloadFile, &output));
460  base::MessageLoop::current()->RunUntilIdle();
461
462  EXPECT_EQ(file->md5_checksum(), output.file_md5);
463  EXPECT_EQ(google_apis::HTTP_NOT_MODIFIED, output.error);
464}
465
466void APIUtilTest::TestUploadNewFile() {
467  const std::string kFileTitle = "test.txt";
468  const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
469  const std::string sync_root_id = SetUpSyncRootDirectory();
470  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
471
472  Output output;
473  api_util()->UploadNewFile(
474      origin_root_id,
475      kLocalFilePath,
476      kFileTitle,
477      base::Bind(&DidUploadFile, &output));
478  base::MessageLoop::current()->RunUntilIdle();
479
480  EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
481  EXPECT_TRUE(!output.resource_id.empty());
482
483  VerifyTitleUniqueness(origin_root_id,
484                        kFileTitle,
485                        output.resource_id,
486                        google_apis::ENTRY_KIND_FILE);
487}
488
489void APIUtilTest::TestUploadNewFile_ConflictWithFile() {
490  const std::string kFileTitle = "test.txt";
491  const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
492  const std::string sync_root_id = SetUpSyncRootDirectory();
493  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
494
495  fake_drive_uploader()->set_make_file_conflict(true);
496
497  Output output;
498  api_util()->UploadNewFile(
499      origin_root_id,
500      kLocalFilePath,
501      kFileTitle,
502      base::Bind(&DidUploadFile, &output));
503  base::MessageLoop::current()->RunUntilIdle();
504
505  // HTTP_CONFLICT error must be returned with empty resource_id.
506  EXPECT_EQ(google_apis::HTTP_CONFLICT, output.error);
507  EXPECT_TRUE(!output.resource_id.empty());
508
509  // Verify that there is no duplicated file on the remote side.
510  VerifyTitleUniqueness(origin_root_id,
511                        kFileTitle,
512                        output.resource_id,
513                        google_apis::ENTRY_KIND_FILE);
514}
515
516void APIUtilTest::TestUploadExistingFile() {
517  const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
518  const std::string kFileContent = "test content";
519  const std::string kFileTitle = "test.txt";
520  const std::string sync_root_id = SetUpSyncRootDirectory();
521  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
522
523  scoped_ptr<FileResource> file;
524  SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
525
526  Output output;
527  api_util()->UploadExistingFile(
528      file->file_id(),
529      file->md5_checksum(),
530      kLocalFilePath,
531      base::Bind(&DidUploadFile, &output));
532  base::MessageLoop::current()->RunUntilIdle();
533
534  EXPECT_EQ(google_apis::HTTP_SUCCESS, output.error);
535  EXPECT_EQ(file->file_id(), output.resource_id);
536
537  VerifyTitleUniqueness(origin_root_id,
538                        file->title(),
539                        file->file_id(),
540                        google_apis::ENTRY_KIND_FILE);
541}
542
543void APIUtilTest::TestUploadExistingFileInConflict() {
544  const base::FilePath kLocalFilePath(FPL("/tmp/dir/file"));
545  const std::string kFileContent = "test content";
546  const std::string kFileTitle = "test.txt";
547  const std::string sync_root_id = SetUpSyncRootDirectory();
548  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
549
550  scoped_ptr<FileResource> file;
551  SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
552
553  // Since remote file's hash value is different from the expected one, it is
554  // expected to cancel upload the file and to return CONFLICT status code.
555  const std::string kExpectedRemoteFileMD5 = "123456";
556
557  Output output;
558  api_util()->UploadExistingFile(
559      file->file_id(),
560      kExpectedRemoteFileMD5,
561      kLocalFilePath,
562      base::Bind(&DidUploadFile, &output));
563  base::MessageLoop::current()->RunUntilIdle();
564
565  EXPECT_EQ(google_apis::HTTP_CONFLICT, output.error);
566  EXPECT_TRUE(output.resource_id.empty());
567
568  // Verify that there is no duplicated file on the remote side.
569  VerifyTitleUniqueness(origin_root_id,
570                        file->title(),
571                        file->file_id(),
572                        google_apis::ENTRY_KIND_FILE);
573}
574
575void APIUtilTest::TestDeleteFile() {
576  const std::string kFileContent = "test content";
577  const std::string kFileTitle = "test.txt";
578  const std::string sync_root_id = SetUpSyncRootDirectory();
579  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
580
581  scoped_ptr<FileResource> file;
582  SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
583
584  GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
585  api_util()->DeleteFile(file->file_id(),
586                         file->md5_checksum(),
587                         base::Bind(&DidDeleteFile, &error));
588  base::MessageLoop::current()->RunUntilIdle();
589
590  EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
591
592  VerifyFileDeletion(origin_root_id, kFileTitle);
593}
594
595void APIUtilTest::TestDeleteFileInConflict() {
596  const std::string kFileContent = "test content";
597  const std::string kFileTitle = "test.txt";
598  const std::string sync_root_id = SetUpSyncRootDirectory();
599  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
600
601  scoped_ptr<FileResource> file;
602  SetUpFile(origin_root_id, kFileContent, kFileTitle, &file);
603
604  // Since remote file's hash value is different from the expected one, it is
605  // expected to cancel delete the file and to return CONFLICT status code.
606  const std::string kExpectedRemoteFileMD5 = "123456";
607
608  GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
609  api_util()->DeleteFile(file->file_id(),
610                         kExpectedRemoteFileMD5,
611                         base::Bind(&DidDeleteFile, &error));
612  base::MessageLoop::current()->RunUntilIdle();
613
614  EXPECT_EQ(google_apis::HTTP_CONFLICT, error);
615
616  // Verify that the conflict file was not deleted on the remote side.
617  VerifyTitleUniqueness(origin_root_id,
618                        file->title(),
619                        file->file_id(),
620                        google_apis::ENTRY_KIND_FILE);
621}
622
623void APIUtilTest::TestCreateDirectory() {
624  const std::string kDirectoryTitle("directory");
625  const std::string sync_root_id = SetUpSyncRootDirectory();
626  const std::string origin_root_id = SetUpOriginRootDirectory(sync_root_id);
627
628  Output output;
629  api_util()->CreateDirectory(
630      origin_root_id,
631      kDirectoryTitle,
632      base::Bind(&DidGetResourceID, &output));
633  base::MessageLoop::current()->RunUntilIdle();
634
635  EXPECT_EQ(google_apis::HTTP_CREATED, output.error);
636  EXPECT_FALSE(output.resource_id.empty());
637
638  VerifyTitleUniqueness(origin_root_id,
639                        kDirectoryTitle,
640                        output.resource_id,
641                        google_apis::ENTRY_KIND_FOLDER);
642}
643
644TEST_F(APIUtilTest, GetSyncRoot) {
645  ASSERT_FALSE(IsDriveAPIDisabled());
646  TestGetSyncRoot();
647}
648
649TEST_F(APIUtilTest, GetSyncRoot_WAPI) {
650  ScopedDisableDriveAPI disable_drive_api;
651  TestGetSyncRoot();
652}
653
654TEST_F(APIUtilTest, CreateSyncRoot) {
655  ASSERT_FALSE(IsDriveAPIDisabled());
656  TestCreateSyncRoot();
657}
658
659TEST_F(APIUtilTest, CreateSyncRoot_WAPI) {
660  ScopedDisableDriveAPI disable_drive_api;
661  TestCreateSyncRoot();
662}
663
664TEST_F(APIUtilTest, CreateSyncRoot_Conflict) {
665  ASSERT_FALSE(IsDriveAPIDisabled());
666  TestCreateSyncRoot_Conflict();
667}
668
669TEST_F(APIUtilTest, CreateSyncRoot_Conflict_WAPI) {
670  ScopedDisableDriveAPI disable_drive_api;
671  TestCreateSyncRoot_Conflict();
672}
673
674TEST_F(APIUtilTest, GetOriginDirectory) {
675  ASSERT_FALSE(IsDriveAPIDisabled());
676  TestGetOriginDirectory();
677}
678
679TEST_F(APIUtilTest, GetOriginDirectory_WAPI) {
680  ScopedDisableDriveAPI disable_drive_api;
681  TestGetOriginDirectory();
682}
683
684TEST_F(APIUtilTest, CreateOriginDirectory) {
685  ASSERT_FALSE(IsDriveAPIDisabled());
686  TestCreateOriginDirectory();
687}
688
689TEST_F(APIUtilTest, CreateOriginDirectory_WAPI) {
690  ScopedDisableDriveAPI disable_drive_api;
691  TestCreateOriginDirectory();
692}
693
694TEST_F(APIUtilTest, CreateOriginDirectory_Conflict) {
695  ASSERT_FALSE(IsDriveAPIDisabled());
696  TestCreateOriginDirectory_Conflict();
697}
698
699TEST_F(APIUtilTest, CreateOriginDirectory_Conflict_WAPI) {
700  ScopedDisableDriveAPI disable_drive_api;
701  TestCreateOriginDirectory_Conflict();
702}
703
704TEST_F(APIUtilTest, GetLargestChangeStamp) {
705  ASSERT_FALSE(IsDriveAPIDisabled());
706  TestGetLargestChangeStamp();
707}
708
709TEST_F(APIUtilTest, GetLargestChangeStamp_WAPI) {
710  ScopedDisableDriveAPI disable_drive_api;
711  TestGetLargestChangeStamp();
712}
713
714TEST_F(APIUtilTest, ListFiles) {
715  ASSERT_FALSE(IsDriveAPIDisabled());
716  TestListFiles();
717}
718
719TEST_F(APIUtilTest, ListFiles_WAPI) {
720  ScopedDisableDriveAPI disable_drive_api;
721  TestListFiles();
722}
723
724TEST_F(APIUtilTest, ListChanges) {
725  ASSERT_FALSE(IsDriveAPIDisabled());
726  TestListChanges();
727}
728
729TEST_F(APIUtilTest, ListChanges_WAPI) {
730  ScopedDisableDriveAPI disable_drive_api;
731  TestListChanges();
732}
733
734TEST_F(APIUtilTest, DownloadFile) {
735  ASSERT_FALSE(IsDriveAPIDisabled());
736  TestDownloadFile();
737}
738
739TEST_F(APIUtilTest, DownloadFile_WAPI) {
740  ScopedDisableDriveAPI disable_drive_api;
741  TestDownloadFile();
742}
743
744TEST_F(APIUtilTest, DownloadFileInNotModified) {
745  ASSERT_FALSE(IsDriveAPIDisabled());
746  TestDownloadFileInNotModified();
747}
748
749TEST_F(APIUtilTest, DownloadFileInNotModified_WAPI) {
750  ScopedDisableDriveAPI disable_drive_api;
751  TestDownloadFileInNotModified();
752}
753
754TEST_F(APIUtilTest, UploadNewFile) {
755  ASSERT_FALSE(IsDriveAPIDisabled());
756  TestUploadNewFile();
757}
758
759TEST_F(APIUtilTest, UploadNewFile_WAPI) {
760  ScopedDisableDriveAPI disable_drive_api;
761  TestUploadNewFile();
762}
763
764TEST_F(APIUtilTest, UploadNewFile_ConflictWithFile) {
765  ASSERT_FALSE(IsDriveAPIDisabled());
766  TestUploadNewFile_ConflictWithFile();
767}
768
769TEST_F(APIUtilTest, UploadNewFile_ConflictWithFile_WAPI) {
770  ScopedDisableDriveAPI disable_drive_api;
771  TestUploadNewFile_ConflictWithFile();
772}
773
774TEST_F(APIUtilTest, UploadExistingFile) {
775  ASSERT_FALSE(IsDriveAPIDisabled());
776  TestUploadExistingFile();
777}
778
779TEST_F(APIUtilTest, UploadExistingFile_WAPI) {
780  ScopedDisableDriveAPI disable_drive_api;
781  TestUploadExistingFile();
782}
783
784TEST_F(APIUtilTest, UploadExistingFileInConflict) {
785  ASSERT_FALSE(IsDriveAPIDisabled());
786  TestUploadExistingFileInConflict();
787}
788
789TEST_F(APIUtilTest, UploadExistingFileInConflict_WAPI) {
790  ScopedDisableDriveAPI disable_drive_api;
791  TestUploadExistingFileInConflict();
792}
793
794TEST_F(APIUtilTest, DeleteFile) {
795  ASSERT_FALSE(IsDriveAPIDisabled());
796  TestDeleteFile();
797}
798
799TEST_F(APIUtilTest, DeleteFile_WAPI) {
800  ScopedDisableDriveAPI disable_drive_api;
801  TestDeleteFile();
802}
803
804TEST_F(APIUtilTest, DeleteFileInConflict) {
805  ASSERT_FALSE(IsDriveAPIDisabled());
806  TestDeleteFileInConflict();
807}
808
809TEST_F(APIUtilTest, DeleteFileInConflict_WAPI) {
810  ScopedDisableDriveAPI disable_drive_api;
811  TestDeleteFileInConflict();
812}
813
814TEST_F(APIUtilTest, CreateDirectory) {
815  ASSERT_FALSE(IsDriveAPIDisabled());
816  TestCreateDirectory();
817}
818
819TEST_F(APIUtilTest, CreateDirectory_WAPI) {
820  ScopedDisableDriveAPI disable_drive_api;
821  TestCreateDirectory();
822}
823
824}  // namespace drive_backend
825}  // namespace sync_file_system
826