Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / fake_drive_uploader.cc
blob98d6d3ed46398f87a04d50163f4c56d849e94d3d
1 // Copyright 2014 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/fake_drive_uploader.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "google_apis/drive/drive_api_parser.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using drive::FakeDriveService;
16 using drive::UploadCompletionCallback;
17 using google_apis::CancelCallback;
18 using google_apis::FileResource;
19 using google_apis::FileResourceCallback;
20 using google_apis::DriveApiErrorCode;
21 using google_apis::ProgressCallback;
23 namespace sync_file_system {
24 namespace drive_backend {
26 namespace {
28 void DidAddFileOrDirectoryForMakingConflict(DriveApiErrorCode error,
29 scoped_ptr<FileResource> entry) {
30 ASSERT_EQ(google_apis::HTTP_CREATED, error);
31 ASSERT_TRUE(entry);
34 void DidAddFileForUploadNew(
35 const UploadCompletionCallback& callback,
36 DriveApiErrorCode error,
37 scoped_ptr<FileResource> entry) {
38 ASSERT_EQ(google_apis::HTTP_CREATED, error);
39 ASSERT_TRUE(entry);
40 base::ThreadTaskRunnerHandle::Get()->PostTask(
41 FROM_HERE,
42 base::Bind(callback,
43 google_apis::HTTP_SUCCESS,
44 GURL(),
45 base::Passed(&entry)));
48 void DidGetFileResourceForUploadExisting(
49 const UploadCompletionCallback& callback,
50 DriveApiErrorCode error,
51 scoped_ptr<FileResource> entry) {
52 base::ThreadTaskRunnerHandle::Get()->PostTask(
53 FROM_HERE,
54 base::Bind(callback,
55 error,
56 GURL(),
57 base::Passed(&entry)));
60 } // namespace
62 FakeDriveServiceWrapper::FakeDriveServiceWrapper()
63 : make_directory_conflict_(false) {}
65 FakeDriveServiceWrapper::~FakeDriveServiceWrapper() {}
67 CancelCallback FakeDriveServiceWrapper::AddNewDirectory(
68 const std::string& parent_resource_id,
69 const std::string& directory_name,
70 const drive::AddNewDirectoryOptions& options,
71 const FileResourceCallback& callback) {
72 if (make_directory_conflict_) {
73 FakeDriveService::AddNewDirectory(
74 parent_resource_id,
75 directory_name,
76 options,
77 base::Bind(&DidAddFileOrDirectoryForMakingConflict));
79 return FakeDriveService::AddNewDirectory(
80 parent_resource_id, directory_name, options, callback);
83 FakeDriveUploader::FakeDriveUploader(
84 FakeDriveServiceWrapper* fake_drive_service)
85 : fake_drive_service_(fake_drive_service),
86 make_file_conflict_(false) {}
88 FakeDriveUploader::~FakeDriveUploader() {}
90 void FakeDriveUploader::StartBatchProcessing() {
93 void FakeDriveUploader::StopBatchProcessing() {
96 CancelCallback FakeDriveUploader::UploadNewFile(
97 const std::string& parent_resource_id,
98 const base::FilePath& local_file_path,
99 const std::string& title,
100 const std::string& content_type,
101 const drive::UploadNewFileOptions& options,
102 const UploadCompletionCallback& callback,
103 const ProgressCallback& progress_callback) {
104 DCHECK(!callback.is_null());
105 const std::string kFileContent = "test content";
107 if (make_file_conflict_) {
108 fake_drive_service_->AddNewFile(
109 content_type,
110 kFileContent,
111 parent_resource_id,
112 title,
113 false, // shared_with_me
114 base::Bind(&DidAddFileOrDirectoryForMakingConflict));
117 fake_drive_service_->AddNewFile(
118 content_type,
119 kFileContent,
120 parent_resource_id,
121 title,
122 false, // shared_with_me
123 base::Bind(&DidAddFileForUploadNew, callback));
124 base::MessageLoop::current()->RunUntilIdle();
126 return CancelCallback();
129 CancelCallback FakeDriveUploader::UploadExistingFile(
130 const std::string& resource_id,
131 const base::FilePath& local_file_path,
132 const std::string& content_type,
133 const drive::UploadExistingFileOptions& options,
134 const UploadCompletionCallback& callback,
135 const ProgressCallback& progress_callback) {
136 DCHECK(!callback.is_null());
137 return fake_drive_service_->GetFileResource(
138 resource_id,
139 base::Bind(&DidGetFileResourceForUploadExisting, callback));
142 CancelCallback FakeDriveUploader::ResumeUploadFile(
143 const GURL& upload_location,
144 const base::FilePath& local_file_path,
145 const std::string& content_type,
146 const drive::UploadCompletionCallback& callback,
147 const ProgressCallback& progress_callback) {
148 // At the moment, sync file system doesn't support resuming of the uploading.
149 // So this method shouldn't be reached.
150 NOTREACHED();
151 return CancelCallback();
154 } // namespace drive_backend
155 } // namespace sync_file_system