Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / fake_drive_service_helper.cc
blobb993415127a91ea862ff4c16ecdc9b5a0837fc03
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_service_helper.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
13 #include "chrome/browser/sync_file_system/sync_status_code.h"
14 #include "google_apis/drive/drive_api_parser.h"
15 #include "storage/browser/fileapi/file_system_url.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #define FPL(path) FILE_PATH_LITERAL(path)
20 using google_apis::AboutResource;
21 using google_apis::FileList;
22 using google_apis::FileResource;
23 using google_apis::DriveApiErrorCode;
25 namespace sync_file_system {
26 namespace drive_backend {
28 namespace {
30 void UploadResultCallback(DriveApiErrorCode* error_out,
31 scoped_ptr<FileResource>* entry_out,
32 DriveApiErrorCode error,
33 const GURL& upload_location,
34 scoped_ptr<FileResource> entry) {
35 ASSERT_TRUE(error_out);
36 ASSERT_TRUE(entry_out);
37 *error_out = error;
38 *entry_out = entry.Pass();
41 void DownloadResultCallback(DriveApiErrorCode* error_out,
42 DriveApiErrorCode error,
43 const base::FilePath& local_file) {
44 ASSERT_TRUE(error_out);
45 *error_out = error;
48 } // namespace
50 FakeDriveServiceHelper::FakeDriveServiceHelper(
51 drive::FakeDriveService* fake_drive_service,
52 drive::DriveUploaderInterface* drive_uploader,
53 const std::string& sync_root_folder_title)
54 : fake_drive_service_(fake_drive_service),
55 drive_uploader_(drive_uploader),
56 sync_root_folder_title_(sync_root_folder_title) {
57 Initialize();
60 FakeDriveServiceHelper::~FakeDriveServiceHelper() {
63 DriveApiErrorCode FakeDriveServiceHelper::AddOrphanedFolder(
64 const std::string& title,
65 std::string* folder_id) {
66 std::string root_folder_id = fake_drive_service_->GetRootResourceId();
67 DriveApiErrorCode error = AddFolder(root_folder_id, title, folder_id);
68 if (error != google_apis::HTTP_CREATED)
69 return error;
71 error = google_apis::DRIVE_OTHER_ERROR;
72 fake_drive_service_->RemoveResourceFromDirectory(
73 root_folder_id, *folder_id,
74 CreateResultReceiver(&error));
75 base::RunLoop().RunUntilIdle();
77 if (error != google_apis::HTTP_NO_CONTENT && folder_id)
78 return error;
79 return google_apis::HTTP_CREATED;
82 DriveApiErrorCode FakeDriveServiceHelper::AddFolder(
83 const std::string& parent_folder_id,
84 const std::string& title,
85 std::string* folder_id) {
86 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
87 scoped_ptr<FileResource> folder;
88 fake_drive_service_->AddNewDirectory(parent_folder_id, title,
89 drive::AddNewDirectoryOptions(),
90 CreateResultReceiver(&error, &folder));
91 base::RunLoop().RunUntilIdle();
93 if (error == google_apis::HTTP_CREATED && folder_id)
94 *folder_id = folder->file_id();
95 return error;
98 DriveApiErrorCode FakeDriveServiceHelper::AddFile(
99 const std::string& parent_folder_id,
100 const std::string& title,
101 const std::string& content,
102 std::string* file_id) {
103 base::FilePath temp_file = WriteToTempFile(content);
105 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
106 scoped_ptr<FileResource> file;
107 drive_uploader_->UploadNewFile(
108 parent_folder_id, temp_file, title, "application/octet-stream",
109 drive::UploadNewFileOptions(),
110 base::Bind(&UploadResultCallback, &error, &file),
111 google_apis::ProgressCallback());
112 base::RunLoop().RunUntilIdle();
114 if (error == google_apis::HTTP_SUCCESS && file_id)
115 *file_id = file->file_id();
116 return error;
119 DriveApiErrorCode FakeDriveServiceHelper::UpdateFile(
120 const std::string& file_id,
121 const std::string& content) {
122 base::FilePath temp_file = WriteToTempFile(content);
123 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
124 scoped_ptr<FileResource> file;
125 drive_uploader_->UploadExistingFile(
126 file_id, temp_file, "application/octet-stream",
127 drive::UploadExistingFileOptions(),
128 base::Bind(&UploadResultCallback, &error, &file),
129 google_apis::ProgressCallback());
130 base::RunLoop().RunUntilIdle();
131 return error;
134 DriveApiErrorCode FakeDriveServiceHelper::DeleteResource(
135 const std::string& file_id) {
136 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
137 fake_drive_service_->DeleteResource(
138 file_id,
139 std::string(), // etag
140 CreateResultReceiver(&error));
141 base::RunLoop().RunUntilIdle();
142 return error;
145 DriveApiErrorCode FakeDriveServiceHelper::TrashResource(
146 const std::string& file_id) {
147 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
148 fake_drive_service_->TrashResource(
149 file_id,
150 CreateResultReceiver(&error));
151 base::RunLoop().RunUntilIdle();
152 return error;
155 DriveApiErrorCode FakeDriveServiceHelper::UpdateModificationTime(
156 const std::string& file_id,
157 const base::Time& modification_time) {
158 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
159 scoped_ptr<FileResource> entry;
160 error = GetFileResource(file_id, &entry);
161 if (error != google_apis::HTTP_SUCCESS)
162 return error;
164 fake_drive_service_->UpdateResource(
165 file_id, std::string(), entry->title(), modification_time,
166 entry->last_viewed_by_me_date(), google_apis::drive::Properties(),
167 CreateResultReceiver(&error, &entry));
168 base::RunLoop().RunUntilIdle();
169 return error;
172 DriveApiErrorCode FakeDriveServiceHelper::RenameResource(
173 const std::string& file_id,
174 const std::string& new_title) {
175 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
176 scoped_ptr<FileResource> entry;
177 fake_drive_service_->UpdateResource(
178 file_id, std::string(), new_title, base::Time(), base::Time(),
179 google_apis::drive::Properties(), CreateResultReceiver(&error, &entry));
180 base::RunLoop().RunUntilIdle();
181 return error;
184 DriveApiErrorCode FakeDriveServiceHelper::AddResourceToDirectory(
185 const std::string& parent_folder_id,
186 const std::string& file_id) {
187 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
188 fake_drive_service_->AddResourceToDirectory(
189 parent_folder_id, file_id,
190 CreateResultReceiver(&error));
191 base::RunLoop().RunUntilIdle();
192 return error;
195 DriveApiErrorCode FakeDriveServiceHelper::RemoveResourceFromDirectory(
196 const std::string& parent_folder_id,
197 const std::string& file_id) {
198 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
199 fake_drive_service_->RemoveResourceFromDirectory(
200 parent_folder_id, file_id,
201 CreateResultReceiver(&error));
202 base::RunLoop().RunUntilIdle();
203 return error;
206 DriveApiErrorCode FakeDriveServiceHelper::GetSyncRootFolderID(
207 std::string* sync_root_folder_id) {
208 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
209 scoped_ptr<FileList> resource_list;
210 fake_drive_service_->SearchByTitle(
211 sync_root_folder_title_, std::string(),
212 CreateResultReceiver(&error, &resource_list));
213 base::RunLoop().RunUntilIdle();
214 if (error != google_apis::HTTP_SUCCESS)
215 return error;
217 const ScopedVector<FileResource>& items = resource_list->items();
218 for (ScopedVector<FileResource>::const_iterator itr = items.begin();
219 itr != items.end(); ++itr) {
220 const FileResource& item = **itr;
221 if (item.parents().empty()) {
222 *sync_root_folder_id = item.file_id();
223 return google_apis::HTTP_SUCCESS;
226 return google_apis::HTTP_NOT_FOUND;
229 DriveApiErrorCode FakeDriveServiceHelper::ListFilesInFolder(
230 const std::string& folder_id,
231 ScopedVector<FileResource>* entries) {
232 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
233 scoped_ptr<FileList> list;
234 fake_drive_service_->GetFileListInDirectory(
235 folder_id,
236 CreateResultReceiver(&error, &list));
237 base::RunLoop().RunUntilIdle();
238 if (error != google_apis::HTTP_SUCCESS)
239 return error;
241 return CompleteListing(list.Pass(), entries);
244 DriveApiErrorCode FakeDriveServiceHelper::SearchByTitle(
245 const std::string& folder_id,
246 const std::string& title,
247 ScopedVector<FileResource>* entries) {
248 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
249 scoped_ptr<FileList> list;
250 fake_drive_service_->SearchByTitle(
251 title, folder_id,
252 CreateResultReceiver(&error, &list));
253 base::RunLoop().RunUntilIdle();
254 if (error != google_apis::HTTP_SUCCESS)
255 return error;
257 return CompleteListing(list.Pass(), entries);
260 DriveApiErrorCode FakeDriveServiceHelper::GetFileResource(
261 const std::string& file_id,
262 scoped_ptr<FileResource>* entry) {
263 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
264 fake_drive_service_->GetFileResource(
265 file_id,
266 CreateResultReceiver(&error, entry));
267 base::RunLoop().RunUntilIdle();
268 return error;
271 DriveApiErrorCode FakeDriveServiceHelper::ReadFile(
272 const std::string& file_id,
273 std::string* file_content) {
274 scoped_ptr<google_apis::FileResource> file;
275 DriveApiErrorCode error = GetFileResource(file_id, &file);
276 if (error != google_apis::HTTP_SUCCESS)
277 return error;
278 if (!file)
279 return google_apis::DRIVE_PARSE_ERROR;
281 error = google_apis::DRIVE_OTHER_ERROR;
282 base::FilePath temp_file;
283 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_, &temp_file));
284 fake_drive_service_->DownloadFile(
285 temp_file, file->file_id(),
286 base::Bind(&DownloadResultCallback, &error),
287 google_apis::GetContentCallback(),
288 google_apis::ProgressCallback());
289 base::RunLoop().RunUntilIdle();
290 if (error != google_apis::HTTP_SUCCESS)
291 return error;
293 return base::ReadFileToString(temp_file, file_content)
294 ? google_apis::HTTP_SUCCESS : google_apis::DRIVE_FILE_ERROR;
297 DriveApiErrorCode FakeDriveServiceHelper::GetAboutResource(
298 scoped_ptr<AboutResource>* about_resource) {
299 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
300 fake_drive_service_->GetAboutResource(
301 CreateResultReceiver(&error, about_resource));
302 base::RunLoop().RunUntilIdle();
303 return error;
306 DriveApiErrorCode FakeDriveServiceHelper::CompleteListing(
307 scoped_ptr<FileList> list,
308 ScopedVector<FileResource>* entries) {
309 while (true) {
310 entries->reserve(entries->size() + list->items().size());
311 std::vector<FileResource*> tmp;
312 list->mutable_items()->release(&tmp);
313 for (std::vector<FileResource*>::const_iterator itr =
314 tmp.begin(); itr != tmp.end(); ++itr) {
315 entries->push_back(*itr);
318 GURL next_feed = list->next_link();
319 if (next_feed.is_empty())
320 return google_apis::HTTP_SUCCESS;
322 DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
323 list.reset();
324 fake_drive_service_->GetRemainingFileList(
325 next_feed,
326 CreateResultReceiver(&error, &list));
327 base::RunLoop().RunUntilIdle();
328 if (error != google_apis::HTTP_SUCCESS)
329 return error;
333 void FakeDriveServiceHelper::Initialize() {
334 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
335 temp_dir_ = base_dir_.path().Append(FPL("tmp"));
336 ASSERT_TRUE(base::CreateDirectory(temp_dir_));
339 base::FilePath FakeDriveServiceHelper::WriteToTempFile(
340 const std::string& content) {
341 base::FilePath temp_file;
342 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_, &temp_file));
343 EXPECT_EQ(static_cast<int>(content.size()),
344 base::WriteFile(temp_file, content.data(), content.size()));
345 return temp_file;
348 } // namespace drive_backend
349 } // namespace sync_file_system