OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/google_apis/drive_uploader.h" | 5 #include "chrome/browser/google_apis/drive_uploader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 const std::string& title, | 79 const std::string& title, |
80 const InitiateUploadCallback& callback) OVERRIDE { | 80 const InitiateUploadCallback& callback) OVERRIDE { |
81 EXPECT_EQ(kTestDocumentTitle, title); | 81 EXPECT_EQ(kTestDocumentTitle, title); |
82 EXPECT_EQ(kTestMimeType, content_type); | 82 EXPECT_EQ(kTestMimeType, content_type); |
83 const int64 expected_size = expected_upload_content_.size(); | 83 const int64 expected_size = expected_upload_content_.size(); |
84 EXPECT_EQ(expected_size, content_length); | 84 EXPECT_EQ(expected_size, content_length); |
85 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); | 85 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); |
86 | 86 |
87 // Calls back the upload URL for subsequent ResumeUpload operations. | 87 // Calls back the upload URL for subsequent ResumeUpload operations. |
88 // InitiateUpload is an asynchronous function, so don't callback directly. | 88 // InitiateUpload is an asynchronous function, so don't callback directly. |
89 MessageLoop::current()->PostTask(FROM_HERE, | 89 base::MessageLoop::current()->PostTask( |
90 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); | 90 FROM_HERE, base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); |
91 } | 91 } |
92 | 92 |
93 virtual void InitiateUploadExistingFile( | 93 virtual void InitiateUploadExistingFile( |
94 const base::FilePath& drive_file_path, | 94 const base::FilePath& drive_file_path, |
95 const std::string& content_type, | 95 const std::string& content_type, |
96 int64 content_length, | 96 int64 content_length, |
97 const std::string& resource_id, | 97 const std::string& resource_id, |
98 const std::string& etag, | 98 const std::string& etag, |
99 const InitiateUploadCallback& callback) OVERRIDE { | 99 const InitiateUploadCallback& callback) OVERRIDE { |
100 EXPECT_EQ(kTestMimeType, content_type); | 100 EXPECT_EQ(kTestMimeType, content_type); |
101 const int64 expected_size = expected_upload_content_.size(); | 101 const int64 expected_size = expected_upload_content_.size(); |
102 EXPECT_EQ(expected_size, content_length); | 102 EXPECT_EQ(expected_size, content_length); |
103 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); | 103 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); |
104 | 104 |
105 if (!etag.empty() && etag != kTestETag) { | 105 if (!etag.empty() && etag != kTestETag) { |
106 MessageLoop::current()->PostTask(FROM_HERE, | 106 base::MessageLoop::current()->PostTask( |
107 base::Bind(callback, HTTP_PRECONDITION, GURL())); | 107 FROM_HERE, base::Bind(callback, HTTP_PRECONDITION, GURL())); |
108 return; | 108 return; |
109 } | 109 } |
110 | 110 |
111 // Calls back the upload URL for subsequent ResumeUpload operations. | 111 // Calls back the upload URL for subsequent ResumeUpload operations. |
112 // InitiateUpload is an asynchronous function, so don't callback directly. | 112 // InitiateUpload is an asynchronous function, so don't callback directly. |
113 MessageLoop::current()->PostTask(FROM_HERE, | 113 base::MessageLoop::current()->PostTask( |
114 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); | 114 FROM_HERE, base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); |
115 } | 115 } |
116 | 116 |
117 // Handles a request for uploading a chunk of bytes. | 117 // Handles a request for uploading a chunk of bytes. |
118 virtual void ResumeUpload( | 118 virtual void ResumeUpload( |
119 UploadMode upload_mode, | 119 UploadMode upload_mode, |
120 const base::FilePath& drive_file_path, | 120 const base::FilePath& drive_file_path, |
121 const GURL& upload_url, | 121 const GURL& upload_url, |
122 int64 start_position, | 122 int64 start_position, |
123 int64 end_position, | 123 int64 end_position, |
124 int64 content_length, | 124 int64 content_length, |
(...skipping 26 matching lines...) Expand all Loading... |
151 EXPECT_EQ(kTestMimeType, content_type); | 151 EXPECT_EQ(kTestMimeType, content_type); |
152 | 152 |
153 // Update the internal status of the current upload session. | 153 // Update the internal status of the current upload session. |
154 resume_upload_call_count_++; | 154 resume_upload_call_count_++; |
155 received_bytes_ = end_position; | 155 received_bytes_ = end_position; |
156 | 156 |
157 // Callback progress | 157 // Callback progress |
158 if (!progress_callback.is_null()) { | 158 if (!progress_callback.is_null()) { |
159 // For the testing purpose, it always notifies the progress at the end of | 159 // For the testing purpose, it always notifies the progress at the end of |
160 // each chunk uploading. | 160 // each chunk uploading. |
161 MessageLoop::current()->PostTask(FROM_HERE, | 161 base::MessageLoop::current()->PostTask( |
162 base::Bind(progress_callback, expected_chunk_size, | 162 FROM_HERE, |
163 expected_chunk_size)); | 163 base::Bind( |
| 164 progress_callback, expected_chunk_size, expected_chunk_size)); |
164 } | 165 } |
165 | 166 |
166 // Callback with response. | 167 // Callback with response. |
167 UploadRangeResponse response; | 168 UploadRangeResponse response; |
168 scoped_ptr<ResourceEntry> entry; | 169 scoped_ptr<ResourceEntry> entry; |
169 if (received_bytes_ == content_length) { | 170 if (received_bytes_ == content_length) { |
170 response = UploadRangeResponse( | 171 response = UploadRangeResponse( |
171 upload_mode == UPLOAD_NEW_FILE ? HTTP_CREATED : HTTP_SUCCESS, | 172 upload_mode == UPLOAD_NEW_FILE ? HTTP_CREATED : HTTP_SUCCESS, |
172 -1, -1); | 173 -1, -1); |
173 | 174 |
174 base::DictionaryValue dict; | 175 base::DictionaryValue dict; |
175 dict.Set("id.$t", new base::StringValue(kTestDummyId)); | 176 dict.Set("id.$t", new base::StringValue(kTestDummyId)); |
176 entry = ResourceEntry::CreateFrom(dict); | 177 entry = ResourceEntry::CreateFrom(dict); |
177 } else { | 178 } else { |
178 response = UploadRangeResponse(HTTP_RESUME_INCOMPLETE, 0, end_position); | 179 response = UploadRangeResponse(HTTP_RESUME_INCOMPLETE, 0, end_position); |
179 } | 180 } |
180 // ResumeUpload is an asynchronous function, so don't callback directly. | 181 // ResumeUpload is an asynchronous function, so don't callback directly. |
181 MessageLoop::current()->PostTask(FROM_HERE, | 182 base::MessageLoop::current()->PostTask( |
182 base::Bind(callback, response, base::Passed(&entry))); | 183 FROM_HERE, base::Bind(callback, response, base::Passed(&entry))); |
183 } | 184 } |
184 | 185 |
185 std::string expected_upload_content_; | 186 std::string expected_upload_content_; |
186 int64 received_bytes_; | 187 int64 received_bytes_; |
187 int64 resume_upload_call_count_; | 188 int64 resume_upload_call_count_; |
188 }; | 189 }; |
189 | 190 |
190 // Mock DriveService that returns a failure at InitiateUpload(). | 191 // Mock DriveService that returns a failure at InitiateUpload(). |
191 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { | 192 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { |
192 // Returns error. | 193 // Returns error. |
193 virtual void InitiateUploadNewFile( | 194 virtual void InitiateUploadNewFile( |
194 const base::FilePath& drive_file_path, | 195 const base::FilePath& drive_file_path, |
195 const std::string& content_type, | 196 const std::string& content_type, |
196 int64 content_length, | 197 int64 content_length, |
197 const std::string& parent_resource_id, | 198 const std::string& parent_resource_id, |
198 const std::string& title, | 199 const std::string& title, |
199 const InitiateUploadCallback& callback) OVERRIDE { | 200 const InitiateUploadCallback& callback) OVERRIDE { |
200 MessageLoop::current()->PostTask(FROM_HERE, | 201 base::MessageLoop::current()->PostTask( |
201 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 202 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
202 } | 203 } |
203 | 204 |
204 virtual void InitiateUploadExistingFile( | 205 virtual void InitiateUploadExistingFile( |
205 const base::FilePath& drive_file_path, | 206 const base::FilePath& drive_file_path, |
206 const std::string& content_type, | 207 const std::string& content_type, |
207 int64 content_length, | 208 int64 content_length, |
208 const std::string& resource_id, | 209 const std::string& resource_id, |
209 const std::string& etag, | 210 const std::string& etag, |
210 const InitiateUploadCallback& callback) OVERRIDE { | 211 const InitiateUploadCallback& callback) OVERRIDE { |
211 MessageLoop::current()->PostTask(FROM_HERE, | 212 base::MessageLoop::current()->PostTask( |
212 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 213 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
213 } | 214 } |
214 | 215 |
215 // Should not be used. | 216 // Should not be used. |
216 virtual void ResumeUpload( | 217 virtual void ResumeUpload( |
217 UploadMode upload_mode, | 218 UploadMode upload_mode, |
218 const base::FilePath& drive_file_path, | 219 const base::FilePath& drive_file_path, |
219 const GURL& upload_url, | 220 const GURL& upload_url, |
220 int64 start_position, | 221 int64 start_position, |
221 int64 end_position, | 222 int64 end_position, |
222 int64 content_length, | 223 int64 content_length, |
223 const std::string& content_type, | 224 const std::string& content_type, |
224 const scoped_refptr<net::IOBuffer>& buf, | 225 const scoped_refptr<net::IOBuffer>& buf, |
225 const UploadRangeCallback& callback, | 226 const UploadRangeCallback& callback, |
226 const ProgressCallback& progress_callback) OVERRIDE { | 227 const ProgressCallback& progress_callback) OVERRIDE { |
227 NOTREACHED(); | 228 NOTREACHED(); |
228 } | 229 } |
229 }; | 230 }; |
230 | 231 |
231 // Mock DriveService that returns a failure at ResumeUpload(). | 232 // Mock DriveService that returns a failure at ResumeUpload(). |
232 class MockDriveServiceNoConnectionAtResume : public DummyDriveService { | 233 class MockDriveServiceNoConnectionAtResume : public DummyDriveService { |
233 // Succeeds and returns an upload location URL. | 234 // Succeeds and returns an upload location URL. |
234 virtual void InitiateUploadNewFile( | 235 virtual void InitiateUploadNewFile( |
235 const base::FilePath& drive_file_path, | 236 const base::FilePath& drive_file_path, |
236 const std::string& content_type, | 237 const std::string& content_type, |
237 int64 content_length, | 238 int64 content_length, |
238 const std::string& parent_resource_id, | 239 const std::string& parent_resource_id, |
239 const std::string& title, | 240 const std::string& title, |
240 const InitiateUploadCallback& callback) OVERRIDE { | 241 const InitiateUploadCallback& callback) OVERRIDE { |
241 MessageLoop::current()->PostTask(FROM_HERE, | 242 base::MessageLoop::current()->PostTask( |
242 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); | 243 FROM_HERE, base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); |
243 } | 244 } |
244 | 245 |
245 virtual void InitiateUploadExistingFile( | 246 virtual void InitiateUploadExistingFile( |
246 const base::FilePath& drive_file_path, | 247 const base::FilePath& drive_file_path, |
247 const std::string& content_type, | 248 const std::string& content_type, |
248 int64 content_length, | 249 int64 content_length, |
249 const std::string& resource_id, | 250 const std::string& resource_id, |
250 const std::string& etag, | 251 const std::string& etag, |
251 const InitiateUploadCallback& callback) OVERRIDE { | 252 const InitiateUploadCallback& callback) OVERRIDE { |
252 MessageLoop::current()->PostTask(FROM_HERE, | 253 base::MessageLoop::current()->PostTask( |
253 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); | 254 FROM_HERE, base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadURL))); |
254 } | 255 } |
255 | 256 |
256 // Returns error. | 257 // Returns error. |
257 virtual void ResumeUpload( | 258 virtual void ResumeUpload( |
258 UploadMode upload_mode, | 259 UploadMode upload_mode, |
259 const base::FilePath& drive_file_path, | 260 const base::FilePath& drive_file_path, |
260 const GURL& upload_url, | 261 const GURL& upload_url, |
261 int64 start_position, | 262 int64 start_position, |
262 int64 end_position, | 263 int64 end_position, |
263 int64 content_length, | 264 int64 content_length, |
264 const std::string& content_type, | 265 const std::string& content_type, |
265 const scoped_refptr<net::IOBuffer>& buf, | 266 const scoped_refptr<net::IOBuffer>& buf, |
266 const UploadRangeCallback& callback, | 267 const UploadRangeCallback& callback, |
267 const ProgressCallback& progress_callback) OVERRIDE { | 268 const ProgressCallback& progress_callback) OVERRIDE { |
268 MessageLoop::current()->PostTask(FROM_HERE, | 269 base::MessageLoop::current()->PostTask( |
| 270 FROM_HERE, |
269 base::Bind(callback, | 271 base::Bind(callback, |
270 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), | 272 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), |
271 base::Passed(scoped_ptr<ResourceEntry>()))); | 273 base::Passed(scoped_ptr<ResourceEntry>()))); |
272 } | 274 } |
273 }; | 275 }; |
274 | 276 |
275 class DriveUploaderTest : public testing::Test { | 277 class DriveUploaderTest : public testing::Test { |
276 public: | 278 public: |
277 DriveUploaderTest() | 279 DriveUploaderTest() |
278 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 280 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
279 } | 281 } |
280 | 282 |
281 virtual void SetUp() OVERRIDE { | 283 virtual void SetUp() OVERRIDE { |
282 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 284 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
283 } | 285 } |
284 | 286 |
285 virtual void TearDown() OVERRIDE { | 287 virtual void TearDown() OVERRIDE { |
286 ASSERT_TRUE(temp_dir_.Delete()); | 288 ASSERT_TRUE(temp_dir_.Delete()); |
287 } | 289 } |
288 | 290 |
289 protected: | 291 protected: |
290 MessageLoopForUI message_loop_; | 292 base::MessageLoopForUI message_loop_; |
291 content::TestBrowserThread ui_thread_; | 293 content::TestBrowserThread ui_thread_; |
292 base::ScopedTempDir temp_dir_; | 294 base::ScopedTempDir temp_dir_; |
293 }; | 295 }; |
294 | 296 |
295 } // namespace | 297 } // namespace |
296 | 298 |
297 TEST_F(DriveUploaderTest, UploadExisting0KB) { | 299 TEST_F(DriveUploaderTest, UploadExisting0KB) { |
298 base::FilePath local_path; | 300 base::FilePath local_path; |
299 std::string data; | 301 std::string data; |
300 ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 0, | 302 ASSERT_TRUE(CreateFileOfSpecifiedSize(temp_dir_.path(), 0, |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 test_util::CreateCopyResultCallback( | 575 test_util::CreateCopyResultCallback( |
574 &error, &drive_path, &file_path, &resource_entry), | 576 &error, &drive_path, &file_path, &resource_entry), |
575 google_apis::ProgressCallback()); | 577 google_apis::ProgressCallback()); |
576 test_util::RunBlockingPoolTask(); | 578 test_util::RunBlockingPoolTask(); |
577 | 579 |
578 // Should return failure without doing any attempt to connect to the server. | 580 // Should return failure without doing any attempt to connect to the server. |
579 EXPECT_EQ(DRIVE_UPLOAD_ERROR_NOT_FOUND, error); | 581 EXPECT_EQ(DRIVE_UPLOAD_ERROR_NOT_FOUND, error); |
580 } | 582 } |
581 | 583 |
582 } // namespace google_apis | 584 } // namespace google_apis |
OLD | NEW |