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/drive/drive_uploader.h" | 5 #include "chrome/browser/drive/drive_uploader.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 private: | 75 private: |
76 // DriveServiceInterface overrides. | 76 // DriveServiceInterface overrides. |
77 // Handles a request for obtaining an upload location URL. | 77 // Handles a request for obtaining an upload location URL. |
78 virtual CancelCallback InitiateUploadNewFile( | 78 virtual CancelCallback InitiateUploadNewFile( |
79 const std::string& content_type, | 79 const std::string& content_type, |
80 int64 content_length, | 80 int64 content_length, |
81 const std::string& parent_resource_id, | 81 const std::string& parent_resource_id, |
82 const std::string& title, | 82 const std::string& title, |
83 const InitiateUploadNewFileOptions& options, | 83 const InitiateUploadNewFileOptions& options, |
84 const InitiateUploadCallback& callback) OVERRIDE { | 84 const InitiateUploadCallback& callback) override { |
85 EXPECT_EQ(kTestDocumentTitle, title); | 85 EXPECT_EQ(kTestDocumentTitle, title); |
86 EXPECT_EQ(kTestMimeType, content_type); | 86 EXPECT_EQ(kTestMimeType, content_type); |
87 EXPECT_EQ(expected_content_length_, content_length); | 87 EXPECT_EQ(expected_content_length_, content_length); |
88 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); | 88 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); |
89 | 89 |
90 // Calls back the upload URL for subsequent ResumeUpload requests. | 90 // Calls back the upload URL for subsequent ResumeUpload requests. |
91 // InitiateUpload is an asynchronous function, so don't callback directly. | 91 // InitiateUpload is an asynchronous function, so don't callback directly. |
92 base::MessageLoop::current()->PostTask(FROM_HERE, | 92 base::MessageLoop::current()->PostTask(FROM_HERE, |
93 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); | 93 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); |
94 return CancelCallback(); | 94 return CancelCallback(); |
95 } | 95 } |
96 | 96 |
97 virtual CancelCallback InitiateUploadExistingFile( | 97 virtual CancelCallback InitiateUploadExistingFile( |
98 const std::string& content_type, | 98 const std::string& content_type, |
99 int64 content_length, | 99 int64 content_length, |
100 const std::string& resource_id, | 100 const std::string& resource_id, |
101 const InitiateUploadExistingFileOptions& options, | 101 const InitiateUploadExistingFileOptions& options, |
102 const InitiateUploadCallback& callback) OVERRIDE { | 102 const InitiateUploadCallback& callback) override { |
103 EXPECT_EQ(kTestMimeType, content_type); | 103 EXPECT_EQ(kTestMimeType, content_type); |
104 EXPECT_EQ(expected_content_length_, content_length); | 104 EXPECT_EQ(expected_content_length_, content_length); |
105 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); | 105 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); |
106 | 106 |
107 if (!options.etag.empty() && options.etag != kTestETag) { | 107 if (!options.etag.empty() && options.etag != kTestETag) { |
108 base::MessageLoop::current()->PostTask(FROM_HERE, | 108 base::MessageLoop::current()->PostTask(FROM_HERE, |
109 base::Bind(callback, HTTP_PRECONDITION, GURL())); | 109 base::Bind(callback, HTTP_PRECONDITION, GURL())); |
110 return CancelCallback(); | 110 return CancelCallback(); |
111 } | 111 } |
112 | 112 |
113 // Calls back the upload URL for subsequent ResumeUpload requests. | 113 // Calls back the upload URL for subsequent ResumeUpload requests. |
114 // InitiateUpload is an asynchronous function, so don't callback directly. | 114 // InitiateUpload is an asynchronous function, so don't callback directly. |
115 base::MessageLoop::current()->PostTask(FROM_HERE, | 115 base::MessageLoop::current()->PostTask(FROM_HERE, |
116 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); | 116 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); |
117 return CancelCallback(); | 117 return CancelCallback(); |
118 } | 118 } |
119 | 119 |
120 // Handles a request for uploading a chunk of bytes. | 120 // Handles a request for uploading a chunk of bytes. |
121 virtual CancelCallback ResumeUpload( | 121 virtual CancelCallback ResumeUpload( |
122 const GURL& upload_location, | 122 const GURL& upload_location, |
123 int64 start_position, | 123 int64 start_position, |
124 int64 end_position, | 124 int64 end_position, |
125 int64 content_length, | 125 int64 content_length, |
126 const std::string& content_type, | 126 const std::string& content_type, |
127 const base::FilePath& local_file_path, | 127 const base::FilePath& local_file_path, |
128 const UploadRangeCallback& callback, | 128 const UploadRangeCallback& callback, |
129 const ProgressCallback& progress_callback) OVERRIDE { | 129 const ProgressCallback& progress_callback) override { |
130 // The upload range should start from the current first unreceived byte. | 130 // The upload range should start from the current first unreceived byte. |
131 EXPECT_EQ(received_bytes_, start_position); | 131 EXPECT_EQ(received_bytes_, start_position); |
132 EXPECT_EQ(expected_upload_file_, local_file_path); | 132 EXPECT_EQ(expected_upload_file_, local_file_path); |
133 | 133 |
134 // The upload data must be split into 512KB chunks. | 134 // The upload data must be split into 512KB chunks. |
135 const int64 expected_chunk_end = | 135 const int64 expected_chunk_end = |
136 std::min(received_bytes_ + kUploadChunkSize, expected_content_length_); | 136 std::min(received_bytes_ + kUploadChunkSize, expected_content_length_); |
137 EXPECT_EQ(expected_chunk_end, end_position); | 137 EXPECT_EQ(expected_chunk_end, end_position); |
138 | 138 |
139 // The upload URL returned by InitiateUpload() must be used. | 139 // The upload URL returned by InitiateUpload() must be used. |
(...skipping 18 matching lines...) Expand all Loading... |
158 } | 158 } |
159 | 159 |
160 SendUploadRangeResponse(upload_location, callback); | 160 SendUploadRangeResponse(upload_location, callback); |
161 return CancelCallback(); | 161 return CancelCallback(); |
162 } | 162 } |
163 | 163 |
164 // Handles a request to fetch the current upload status. | 164 // Handles a request to fetch the current upload status. |
165 virtual CancelCallback GetUploadStatus( | 165 virtual CancelCallback GetUploadStatus( |
166 const GURL& upload_location, | 166 const GURL& upload_location, |
167 int64 content_length, | 167 int64 content_length, |
168 const UploadRangeCallback& callback) OVERRIDE { | 168 const UploadRangeCallback& callback) override { |
169 EXPECT_EQ(expected_content_length_, content_length); | 169 EXPECT_EQ(expected_content_length_, content_length); |
170 // The upload URL returned by InitiateUpload() must be used. | 170 // The upload URL returned by InitiateUpload() must be used. |
171 EXPECT_TRUE(GURL(kTestUploadNewFileURL) == upload_location || | 171 EXPECT_TRUE(GURL(kTestUploadNewFileURL) == upload_location || |
172 GURL(kTestUploadExistingFileURL) == upload_location); | 172 GURL(kTestUploadExistingFileURL) == upload_location); |
173 | 173 |
174 SendUploadRangeResponse(upload_location, callback); | 174 SendUploadRangeResponse(upload_location, callback); |
175 return CancelCallback(); | 175 return CancelCallback(); |
176 } | 176 } |
177 | 177 |
178 // Runs |callback| with the current upload status. | 178 // Runs |callback| with the current upload status. |
(...skipping 27 matching lines...) Expand all Loading... |
206 | 206 |
207 // Mock DriveService that returns a failure at InitiateUpload(). | 207 // Mock DriveService that returns a failure at InitiateUpload(). |
208 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { | 208 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { |
209 // Returns error. | 209 // Returns error. |
210 virtual CancelCallback InitiateUploadNewFile( | 210 virtual CancelCallback InitiateUploadNewFile( |
211 const std::string& content_type, | 211 const std::string& content_type, |
212 int64 content_length, | 212 int64 content_length, |
213 const std::string& parent_resource_id, | 213 const std::string& parent_resource_id, |
214 const std::string& title, | 214 const std::string& title, |
215 const InitiateUploadNewFileOptions& options, | 215 const InitiateUploadNewFileOptions& options, |
216 const InitiateUploadCallback& callback) OVERRIDE { | 216 const InitiateUploadCallback& callback) override { |
217 base::MessageLoop::current()->PostTask(FROM_HERE, | 217 base::MessageLoop::current()->PostTask(FROM_HERE, |
218 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 218 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
219 return CancelCallback(); | 219 return CancelCallback(); |
220 } | 220 } |
221 | 221 |
222 virtual CancelCallback InitiateUploadExistingFile( | 222 virtual CancelCallback InitiateUploadExistingFile( |
223 const std::string& content_type, | 223 const std::string& content_type, |
224 int64 content_length, | 224 int64 content_length, |
225 const std::string& resource_id, | 225 const std::string& resource_id, |
226 const InitiateUploadExistingFileOptions& options, | 226 const InitiateUploadExistingFileOptions& options, |
227 const InitiateUploadCallback& callback) OVERRIDE { | 227 const InitiateUploadCallback& callback) override { |
228 base::MessageLoop::current()->PostTask(FROM_HERE, | 228 base::MessageLoop::current()->PostTask(FROM_HERE, |
229 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 229 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
230 return CancelCallback(); | 230 return CancelCallback(); |
231 } | 231 } |
232 | 232 |
233 // Should not be used. | 233 // Should not be used. |
234 virtual CancelCallback ResumeUpload( | 234 virtual CancelCallback ResumeUpload( |
235 const GURL& upload_url, | 235 const GURL& upload_url, |
236 int64 start_position, | 236 int64 start_position, |
237 int64 end_position, | 237 int64 end_position, |
238 int64 content_length, | 238 int64 content_length, |
239 const std::string& content_type, | 239 const std::string& content_type, |
240 const base::FilePath& local_file_path, | 240 const base::FilePath& local_file_path, |
241 const UploadRangeCallback& callback, | 241 const UploadRangeCallback& callback, |
242 const ProgressCallback& progress_callback) OVERRIDE { | 242 const ProgressCallback& progress_callback) override { |
243 NOTREACHED(); | 243 NOTREACHED(); |
244 return CancelCallback(); | 244 return CancelCallback(); |
245 } | 245 } |
246 }; | 246 }; |
247 | 247 |
248 // Mock DriveService that returns a failure at ResumeUpload(). | 248 // Mock DriveService that returns a failure at ResumeUpload(). |
249 class MockDriveServiceNoConnectionAtResume : public DummyDriveService { | 249 class MockDriveServiceNoConnectionAtResume : public DummyDriveService { |
250 // Succeeds and returns an upload location URL. | 250 // Succeeds and returns an upload location URL. |
251 virtual CancelCallback InitiateUploadNewFile( | 251 virtual CancelCallback InitiateUploadNewFile( |
252 const std::string& content_type, | 252 const std::string& content_type, |
253 int64 content_length, | 253 int64 content_length, |
254 const std::string& parent_resource_id, | 254 const std::string& parent_resource_id, |
255 const std::string& title, | 255 const std::string& title, |
256 const InitiateUploadNewFileOptions& options, | 256 const InitiateUploadNewFileOptions& options, |
257 const InitiateUploadCallback& callback) OVERRIDE { | 257 const InitiateUploadCallback& callback) override { |
258 base::MessageLoop::current()->PostTask(FROM_HERE, | 258 base::MessageLoop::current()->PostTask(FROM_HERE, |
259 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); | 259 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); |
260 return CancelCallback(); | 260 return CancelCallback(); |
261 } | 261 } |
262 | 262 |
263 virtual CancelCallback InitiateUploadExistingFile( | 263 virtual CancelCallback InitiateUploadExistingFile( |
264 const std::string& content_type, | 264 const std::string& content_type, |
265 int64 content_length, | 265 int64 content_length, |
266 const std::string& resource_id, | 266 const std::string& resource_id, |
267 const InitiateUploadExistingFileOptions& options, | 267 const InitiateUploadExistingFileOptions& options, |
268 const InitiateUploadCallback& callback) OVERRIDE { | 268 const InitiateUploadCallback& callback) override { |
269 base::MessageLoop::current()->PostTask(FROM_HERE, | 269 base::MessageLoop::current()->PostTask(FROM_HERE, |
270 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); | 270 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); |
271 return CancelCallback(); | 271 return CancelCallback(); |
272 } | 272 } |
273 | 273 |
274 // Returns error. | 274 // Returns error. |
275 virtual CancelCallback ResumeUpload( | 275 virtual CancelCallback ResumeUpload( |
276 const GURL& upload_url, | 276 const GURL& upload_url, |
277 int64 start_position, | 277 int64 start_position, |
278 int64 end_position, | 278 int64 end_position, |
279 int64 content_length, | 279 int64 content_length, |
280 const std::string& content_type, | 280 const std::string& content_type, |
281 const base::FilePath& local_file_path, | 281 const base::FilePath& local_file_path, |
282 const UploadRangeCallback& callback, | 282 const UploadRangeCallback& callback, |
283 const ProgressCallback& progress_callback) OVERRIDE { | 283 const ProgressCallback& progress_callback) override { |
284 base::MessageLoop::current()->PostTask(FROM_HERE, | 284 base::MessageLoop::current()->PostTask(FROM_HERE, |
285 base::Bind(callback, | 285 base::Bind(callback, |
286 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), | 286 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), |
287 base::Passed(scoped_ptr<FileResource>()))); | 287 base::Passed(scoped_ptr<FileResource>()))); |
288 return CancelCallback(); | 288 return CancelCallback(); |
289 } | 289 } |
290 }; | 290 }; |
291 | 291 |
292 // Mock DriveService that returns a failure at GetUploadStatus(). | 292 // Mock DriveService that returns a failure at GetUploadStatus(). |
293 class MockDriveServiceNoConnectionAtGetUploadStatus : public DummyDriveService { | 293 class MockDriveServiceNoConnectionAtGetUploadStatus : public DummyDriveService { |
294 // Returns error. | 294 // Returns error. |
295 virtual CancelCallback GetUploadStatus( | 295 virtual CancelCallback GetUploadStatus( |
296 const GURL& upload_url, | 296 const GURL& upload_url, |
297 int64 content_length, | 297 int64 content_length, |
298 const UploadRangeCallback& callback) OVERRIDE { | 298 const UploadRangeCallback& callback) override { |
299 base::MessageLoop::current()->PostTask(FROM_HERE, | 299 base::MessageLoop::current()->PostTask(FROM_HERE, |
300 base::Bind(callback, | 300 base::Bind(callback, |
301 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), | 301 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), |
302 base::Passed(scoped_ptr<FileResource>()))); | 302 base::Passed(scoped_ptr<FileResource>()))); |
303 return CancelCallback(); | 303 return CancelCallback(); |
304 } | 304 } |
305 }; | 305 }; |
306 | 306 |
307 class DriveUploaderTest : public testing::Test { | 307 class DriveUploaderTest : public testing::Test { |
308 public: | 308 public: |
309 virtual void SetUp() OVERRIDE { | 309 virtual void SetUp() override { |
310 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 310 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
311 } | 311 } |
312 | 312 |
313 protected: | 313 protected: |
314 content::TestBrowserThreadBundle thread_bundle_; | 314 content::TestBrowserThreadBundle thread_bundle_; |
315 base::ScopedTempDir temp_dir_; | 315 base::ScopedTempDir temp_dir_; |
316 }; | 316 }; |
317 | 317 |
318 } // namespace | 318 } // namespace |
319 | 319 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 EXPECT_EQ(HTTP_SUCCESS, error); | 579 EXPECT_EQ(HTTP_SUCCESS, error); |
580 EXPECT_TRUE(upload_location.is_empty()); | 580 EXPECT_TRUE(upload_location.is_empty()); |
581 ASSERT_TRUE(entry); | 581 ASSERT_TRUE(entry); |
582 EXPECT_EQ(kTestDummyMd5, entry->md5_checksum()); | 582 EXPECT_EQ(kTestDummyMd5, entry->md5_checksum()); |
583 ASSERT_EQ(1U, upload_progress_values.size()); | 583 ASSERT_EQ(1U, upload_progress_values.size()); |
584 EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024), | 584 EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024), |
585 upload_progress_values[0]); | 585 upload_progress_values[0]); |
586 } | 586 } |
587 | 587 |
588 } // namespace drive | 588 } // namespace drive |
OLD | NEW |