Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / fake_drive_service_helper.cc
bloba0a6ecf32df5edc675490c1573d89145a7b52832
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/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 "content/public/test/test_browser_thread.h"
15 #include "google_apis/drive/drive_api_parser.h"
16 #include "google_apis/drive/gdata_wapi_parser.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webkit/browser/fileapi/file_system_url.h"
20 #define FPL(path) FILE_PATH_LITERAL(path)
22 using google_apis::AboutResource;
23 using google_apis::GDataErrorCode;
24 using google_apis::ResourceEntry;
25 using google_apis::ResourceList;
27 namespace sync_file_system {
28 namespace drive_backend {
30 namespace {
32 void UploadResultCallback(GDataErrorCode* error_out,
33 scoped_ptr<ResourceEntry>* entry_out,
34 GDataErrorCode error,
35 const GURL& upload_location,
36 scoped_ptr<ResourceEntry> entry) {
37 ASSERT_TRUE(error_out);
38 ASSERT_TRUE(entry_out);
39 *error_out = error;
40 *entry_out = entry.Pass();
43 void DownloadResultCallback(GDataErrorCode* error_out,
44 GDataErrorCode error,
45 const base::FilePath& local_file) {
46 ASSERT_TRUE(error_out);
47 *error_out = error;
50 } // namespace
52 FakeDriveServiceHelper::FakeDriveServiceHelper(
53 drive::FakeDriveService* fake_drive_service,
54 drive::DriveUploaderInterface* drive_uploader,
55 const std::string& sync_root_folder_title)
56 : fake_drive_service_(fake_drive_service),
57 drive_uploader_(drive_uploader),
58 sync_root_folder_title_(sync_root_folder_title) {
59 Initialize();
62 FakeDriveServiceHelper::~FakeDriveServiceHelper() {
65 GDataErrorCode FakeDriveServiceHelper::AddOrphanedFolder(
66 const std::string& title,
67 std::string* folder_id) {
68 EXPECT_TRUE(folder_id);
70 std::string root_folder_id = fake_drive_service_->GetRootResourceId();
71 GDataErrorCode error = AddFolder(root_folder_id, title, folder_id);
72 if (error != google_apis::HTTP_CREATED)
73 return error;
75 error = google_apis::GDATA_OTHER_ERROR;
76 fake_drive_service_->RemoveResourceFromDirectory(
77 root_folder_id, *folder_id,
78 CreateResultReceiver(&error));
79 base::RunLoop().RunUntilIdle();
81 if (error != google_apis::HTTP_NO_CONTENT)
82 return error;
83 return google_apis::HTTP_CREATED;
86 GDataErrorCode FakeDriveServiceHelper::AddFolder(
87 const std::string& parent_folder_id,
88 const std::string& title,
89 std::string* folder_id) {
90 EXPECT_TRUE(folder_id);
92 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
93 scoped_ptr<ResourceEntry> folder;
94 fake_drive_service_->AddNewDirectory(
95 parent_folder_id, title,
96 CreateResultReceiver(&error, &folder));
97 base::RunLoop().RunUntilIdle();
99 if (error == google_apis::HTTP_CREATED)
100 *folder_id = folder->resource_id();
101 return error;
104 GDataErrorCode FakeDriveServiceHelper::AddFile(
105 const std::string& parent_folder_id,
106 const std::string& title,
107 const std::string& content,
108 std::string* file_id) {
109 EXPECT_TRUE(file_id);
110 base::FilePath temp_file = WriteToTempFile(content);
112 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
113 scoped_ptr<ResourceEntry> file;
114 drive_uploader_->UploadNewFile(
115 parent_folder_id, temp_file, title,
116 "application/octet-stream",
117 base::Bind(&UploadResultCallback, &error, &file),
118 google_apis::ProgressCallback());
119 base::RunLoop().RunUntilIdle();
121 if (error == google_apis::HTTP_SUCCESS)
122 *file_id = file->resource_id();
123 return error;
126 GDataErrorCode FakeDriveServiceHelper::UpdateFile(
127 const std::string& file_id,
128 const std::string& content) {
129 base::FilePath temp_file = WriteToTempFile(content);
130 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
131 scoped_ptr<ResourceEntry> file;
132 drive_uploader_->UploadExistingFile(
133 file_id, temp_file,
134 "application/octet-stream",
135 std::string(), // etag
136 base::Bind(&UploadResultCallback, &error, &file),
137 google_apis::ProgressCallback());
138 base::RunLoop().RunUntilIdle();
139 return error;
142 GDataErrorCode FakeDriveServiceHelper::DeleteResource(
143 const std::string& file_id) {
144 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
145 fake_drive_service_->DeleteResource(
146 file_id,
147 std::string(), // etag
148 CreateResultReceiver(&error));
149 base::RunLoop().RunUntilIdle();
150 return error;
153 GDataErrorCode FakeDriveServiceHelper::TrashResource(
154 const std::string& file_id) {
155 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
156 fake_drive_service_->TrashResource(
157 file_id,
158 CreateResultReceiver(&error));
159 base::RunLoop().RunUntilIdle();
160 return error;
163 GDataErrorCode FakeDriveServiceHelper::GetSyncRootFolderID(
164 std::string* sync_root_folder_id) {
165 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
166 scoped_ptr<ResourceList> resource_list;
167 fake_drive_service_->SearchByTitle(
168 sync_root_folder_title_, std::string(),
169 CreateResultReceiver(&error, &resource_list));
170 base::RunLoop().RunUntilIdle();
171 if (error != google_apis::HTTP_SUCCESS)
172 return error;
174 const ScopedVector<ResourceEntry>& entries = resource_list->entries();
175 for (ScopedVector<ResourceEntry>::const_iterator itr = entries.begin();
176 itr != entries.end(); ++itr) {
177 const ResourceEntry& entry = **itr;
178 if (!entry.GetLinkByType(google_apis::Link::LINK_PARENT)) {
179 *sync_root_folder_id = entry.resource_id();
180 return google_apis::HTTP_SUCCESS;
183 return google_apis::HTTP_NOT_FOUND;
186 GDataErrorCode FakeDriveServiceHelper::ListFilesInFolder(
187 const std::string& folder_id,
188 ScopedVector<ResourceEntry>* entries) {
189 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
190 scoped_ptr<ResourceList> list;
191 fake_drive_service_->GetResourceListInDirectory(
192 folder_id,
193 CreateResultReceiver(&error, &list));
194 base::RunLoop().RunUntilIdle();
195 if (error != google_apis::HTTP_SUCCESS)
196 return error;
198 return CompleteListing(list.Pass(), entries);
201 GDataErrorCode FakeDriveServiceHelper::SearchByTitle(
202 const std::string& folder_id,
203 const std::string& title,
204 ScopedVector<ResourceEntry>* entries) {
205 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
206 scoped_ptr<ResourceList> list;
207 fake_drive_service_->SearchByTitle(
208 title, folder_id,
209 CreateResultReceiver(&error, &list));
210 base::RunLoop().RunUntilIdle();
211 if (error != google_apis::HTTP_SUCCESS)
212 return error;
214 return CompleteListing(list.Pass(), entries);
217 GDataErrorCode FakeDriveServiceHelper::GetResourceEntry(
218 const std::string& file_id,
219 scoped_ptr<ResourceEntry>* entry) {
220 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
221 fake_drive_service_->GetResourceEntry(
222 file_id,
223 CreateResultReceiver(&error, entry));
224 base::RunLoop().RunUntilIdle();
225 return error;
228 GDataErrorCode FakeDriveServiceHelper::ReadFile(
229 const std::string& file_id,
230 std::string* file_content) {
231 scoped_ptr<google_apis::ResourceEntry> file;
232 GDataErrorCode error = GetResourceEntry(file_id, &file);
233 if (error != google_apis::HTTP_SUCCESS)
234 return error;
235 if (!file)
236 return google_apis::GDATA_PARSE_ERROR;
238 error = google_apis::GDATA_OTHER_ERROR;
239 base::FilePath temp_file;
240 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_, &temp_file));
241 fake_drive_service_->DownloadFile(
242 temp_file, file->resource_id(),
243 base::Bind(&DownloadResultCallback, &error),
244 google_apis::GetContentCallback(),
245 google_apis::ProgressCallback());
246 base::RunLoop().RunUntilIdle();
247 if (error != google_apis::HTTP_SUCCESS)
248 return error;
250 return base::ReadFileToString(temp_file, file_content)
251 ? google_apis::HTTP_SUCCESS : google_apis::GDATA_FILE_ERROR;
254 GDataErrorCode FakeDriveServiceHelper::GetAboutResource(
255 scoped_ptr<AboutResource>* about_resource) {
256 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
257 fake_drive_service_->GetAboutResource(
258 CreateResultReceiver(&error, about_resource));
259 base::RunLoop().RunUntilIdle();
260 return error;
263 GDataErrorCode FakeDriveServiceHelper::CompleteListing(
264 scoped_ptr<ResourceList> list,
265 ScopedVector<ResourceEntry>* entries) {
266 while (true) {
267 entries->reserve(entries->size() + list->entries().size());
268 for (ScopedVector<ResourceEntry>::iterator itr =
269 list->mutable_entries()->begin();
270 itr != list->mutable_entries()->end(); ++itr) {
271 entries->push_back(*itr);
272 *itr = NULL;
275 GURL next_feed;
276 if (!list->GetNextFeedURL(&next_feed))
277 return google_apis::HTTP_SUCCESS;
279 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
280 list.reset();
281 fake_drive_service_->GetRemainingFileList(
282 next_feed,
283 CreateResultReceiver(&error, &list));
284 base::RunLoop().RunUntilIdle();
285 if (error != google_apis::HTTP_SUCCESS)
286 return error;
290 void FakeDriveServiceHelper::Initialize() {
291 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
292 temp_dir_ = base_dir_.path().Append(FPL("tmp"));
293 ASSERT_TRUE(base::CreateDirectory(temp_dir_));
296 base::FilePath FakeDriveServiceHelper::WriteToTempFile(
297 const std::string& content) {
298 base::FilePath temp_file;
299 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_, &temp_file));
300 EXPECT_EQ(static_cast<int>(content.size()),
301 file_util::WriteFile(temp_file, content.data(), content.size()));
302 return temp_file;
305 } // namespace drive_backend
306 } // namespace sync_file_system