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"
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
{
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
);
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
);
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
) {
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
)
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
)
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 drive::AddNewDirectoryOptions options
;
89 options
.visibility
= google_apis::drive::FILE_VISIBILITY_PRIVATE
;
90 fake_drive_service_
->AddNewDirectory(parent_folder_id
, title
, options
,
91 CreateResultReceiver(&error
, &folder
));
92 base::RunLoop().RunUntilIdle();
94 if (error
== google_apis::HTTP_CREATED
&& folder_id
)
95 *folder_id
= folder
->file_id();
99 DriveApiErrorCode
FakeDriveServiceHelper::AddFile(
100 const std::string
& parent_folder_id
,
101 const std::string
& title
,
102 const std::string
& content
,
103 std::string
* file_id
) {
104 base::FilePath temp_file
= WriteToTempFile(content
);
106 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
107 scoped_ptr
<FileResource
> file
;
108 drive_uploader_
->UploadNewFile(
109 parent_folder_id
, temp_file
, title
, "application/octet-stream",
110 drive::UploadNewFileOptions(),
111 base::Bind(&UploadResultCallback
, &error
, &file
),
112 google_apis::ProgressCallback());
113 base::RunLoop().RunUntilIdle();
115 if (error
== google_apis::HTTP_SUCCESS
&& file_id
)
116 *file_id
= file
->file_id();
120 DriveApiErrorCode
FakeDriveServiceHelper::UpdateFile(
121 const std::string
& file_id
,
122 const std::string
& content
) {
123 base::FilePath temp_file
= WriteToTempFile(content
);
124 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
125 scoped_ptr
<FileResource
> file
;
126 drive_uploader_
->UploadExistingFile(
127 file_id
, temp_file
, "application/octet-stream",
128 drive::UploadExistingFileOptions(),
129 base::Bind(&UploadResultCallback
, &error
, &file
),
130 google_apis::ProgressCallback());
131 base::RunLoop().RunUntilIdle();
135 DriveApiErrorCode
FakeDriveServiceHelper::DeleteResource(
136 const std::string
& file_id
) {
137 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
138 fake_drive_service_
->DeleteResource(
140 std::string(), // etag
141 CreateResultReceiver(&error
));
142 base::RunLoop().RunUntilIdle();
146 DriveApiErrorCode
FakeDriveServiceHelper::TrashResource(
147 const std::string
& file_id
) {
148 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
149 fake_drive_service_
->TrashResource(
151 CreateResultReceiver(&error
));
152 base::RunLoop().RunUntilIdle();
156 DriveApiErrorCode
FakeDriveServiceHelper::UpdateModificationTime(
157 const std::string
& file_id
,
158 const base::Time
& modification_time
) {
159 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
160 scoped_ptr
<FileResource
> entry
;
161 error
= GetFileResource(file_id
, &entry
);
162 if (error
!= google_apis::HTTP_SUCCESS
)
165 fake_drive_service_
->UpdateResource(
166 file_id
, std::string(), entry
->title(), modification_time
,
167 entry
->last_viewed_by_me_date(), google_apis::drive::Properties(),
168 CreateResultReceiver(&error
, &entry
));
169 base::RunLoop().RunUntilIdle();
173 DriveApiErrorCode
FakeDriveServiceHelper::RenameResource(
174 const std::string
& file_id
,
175 const std::string
& new_title
) {
176 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
177 scoped_ptr
<FileResource
> entry
;
178 fake_drive_service_
->UpdateResource(
179 file_id
, std::string(), new_title
, base::Time(), base::Time(),
180 google_apis::drive::Properties(), CreateResultReceiver(&error
, &entry
));
181 base::RunLoop().RunUntilIdle();
185 DriveApiErrorCode
FakeDriveServiceHelper::AddResourceToDirectory(
186 const std::string
& parent_folder_id
,
187 const std::string
& file_id
) {
188 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
189 fake_drive_service_
->AddResourceToDirectory(
190 parent_folder_id
, file_id
,
191 CreateResultReceiver(&error
));
192 base::RunLoop().RunUntilIdle();
196 DriveApiErrorCode
FakeDriveServiceHelper::RemoveResourceFromDirectory(
197 const std::string
& parent_folder_id
,
198 const std::string
& file_id
) {
199 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
200 fake_drive_service_
->RemoveResourceFromDirectory(
201 parent_folder_id
, file_id
,
202 CreateResultReceiver(&error
));
203 base::RunLoop().RunUntilIdle();
207 DriveApiErrorCode
FakeDriveServiceHelper::GetSyncRootFolderID(
208 std::string
* sync_root_folder_id
) {
209 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
210 scoped_ptr
<FileList
> resource_list
;
211 fake_drive_service_
->SearchByTitle(
212 sync_root_folder_title_
, std::string(),
213 CreateResultReceiver(&error
, &resource_list
));
214 base::RunLoop().RunUntilIdle();
215 if (error
!= google_apis::HTTP_SUCCESS
)
218 const ScopedVector
<FileResource
>& items
= resource_list
->items();
219 for (ScopedVector
<FileResource
>::const_iterator itr
= items
.begin();
220 itr
!= items
.end(); ++itr
) {
221 const FileResource
& item
= **itr
;
222 if (item
.parents().empty()) {
223 *sync_root_folder_id
= item
.file_id();
224 return google_apis::HTTP_SUCCESS
;
227 return google_apis::HTTP_NOT_FOUND
;
230 DriveApiErrorCode
FakeDriveServiceHelper::ListFilesInFolder(
231 const std::string
& folder_id
,
232 ScopedVector
<FileResource
>* entries
) {
233 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
234 scoped_ptr
<FileList
> list
;
235 fake_drive_service_
->GetFileListInDirectory(
237 CreateResultReceiver(&error
, &list
));
238 base::RunLoop().RunUntilIdle();
239 if (error
!= google_apis::HTTP_SUCCESS
)
242 return CompleteListing(list
.Pass(), entries
);
245 DriveApiErrorCode
FakeDriveServiceHelper::SearchByTitle(
246 const std::string
& folder_id
,
247 const std::string
& title
,
248 ScopedVector
<FileResource
>* entries
) {
249 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
250 scoped_ptr
<FileList
> list
;
251 fake_drive_service_
->SearchByTitle(
253 CreateResultReceiver(&error
, &list
));
254 base::RunLoop().RunUntilIdle();
255 if (error
!= google_apis::HTTP_SUCCESS
)
258 return CompleteListing(list
.Pass(), entries
);
261 DriveApiErrorCode
FakeDriveServiceHelper::GetFileResource(
262 const std::string
& file_id
,
263 scoped_ptr
<FileResource
>* entry
) {
264 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
265 fake_drive_service_
->GetFileResource(
267 CreateResultReceiver(&error
, entry
));
268 base::RunLoop().RunUntilIdle();
272 DriveApiErrorCode
FakeDriveServiceHelper::GetFileVisibility(
273 const std::string
& file_id
,
274 google_apis::drive::FileVisibility
* visibility
) {
275 return fake_drive_service_
->GetFileVisibility(
280 DriveApiErrorCode
FakeDriveServiceHelper::ReadFile(
281 const std::string
& file_id
,
282 std::string
* file_content
) {
283 scoped_ptr
<google_apis::FileResource
> file
;
284 DriveApiErrorCode error
= GetFileResource(file_id
, &file
);
285 if (error
!= google_apis::HTTP_SUCCESS
)
288 return google_apis::DRIVE_PARSE_ERROR
;
290 error
= google_apis::DRIVE_OTHER_ERROR
;
291 base::FilePath temp_file
;
292 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
, &temp_file
));
293 fake_drive_service_
->DownloadFile(
294 temp_file
, file
->file_id(),
295 base::Bind(&DownloadResultCallback
, &error
),
296 google_apis::GetContentCallback(),
297 google_apis::ProgressCallback());
298 base::RunLoop().RunUntilIdle();
299 if (error
!= google_apis::HTTP_SUCCESS
)
302 return base::ReadFileToString(temp_file
, file_content
)
303 ? google_apis::HTTP_SUCCESS
: google_apis::DRIVE_FILE_ERROR
;
306 DriveApiErrorCode
FakeDriveServiceHelper::GetAboutResource(
307 scoped_ptr
<AboutResource
>* about_resource
) {
308 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
309 fake_drive_service_
->GetAboutResource(
310 CreateResultReceiver(&error
, about_resource
));
311 base::RunLoop().RunUntilIdle();
315 DriveApiErrorCode
FakeDriveServiceHelper::CompleteListing(
316 scoped_ptr
<FileList
> list
,
317 ScopedVector
<FileResource
>* entries
) {
319 entries
->reserve(entries
->size() + list
->items().size());
320 std::vector
<FileResource
*> tmp
;
321 list
->mutable_items()->release(&tmp
);
322 for (std::vector
<FileResource
*>::const_iterator itr
=
323 tmp
.begin(); itr
!= tmp
.end(); ++itr
) {
324 entries
->push_back(*itr
);
327 GURL next_feed
= list
->next_link();
328 if (next_feed
.is_empty())
329 return google_apis::HTTP_SUCCESS
;
331 DriveApiErrorCode error
= google_apis::DRIVE_OTHER_ERROR
;
333 fake_drive_service_
->GetRemainingFileList(
335 CreateResultReceiver(&error
, &list
));
336 base::RunLoop().RunUntilIdle();
337 if (error
!= google_apis::HTTP_SUCCESS
)
342 void FakeDriveServiceHelper::Initialize() {
343 ASSERT_TRUE(base_dir_
.CreateUniqueTempDir());
344 temp_dir_
= base_dir_
.path().Append(FPL("tmp"));
345 ASSERT_TRUE(base::CreateDirectory(temp_dir_
));
348 base::FilePath
FakeDriveServiceHelper::WriteToTempFile(
349 const std::string
& content
) {
350 base::FilePath temp_file
;
351 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
, &temp_file
));
352 EXPECT_EQ(static_cast<int>(content
.size()),
353 base::WriteFile(temp_file
, content
.data(), content
.size()));
357 } // namespace drive_backend
358 } // namespace sync_file_system