| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 const std::string& parent_resource_id, | 67 const std::string& parent_resource_id, |
| 68 const std::string& title, | 68 const std::string& title, |
| 69 const InitiateUploadCallback& callback) OVERRIDE { | 69 const InitiateUploadCallback& callback) OVERRIDE { |
| 70 EXPECT_EQ(kTestDocumentTitle, title); | 70 EXPECT_EQ(kTestDocumentTitle, title); |
| 71 EXPECT_EQ(kTestMimeType, content_type); | 71 EXPECT_EQ(kTestMimeType, content_type); |
| 72 EXPECT_EQ(expected_content_length_, content_length); | 72 EXPECT_EQ(expected_content_length_, content_length); |
| 73 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); | 73 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); |
| 74 | 74 |
| 75 // Calls back the upload URL for subsequent ResumeUpload operations. | 75 // Calls back the upload URL for subsequent ResumeUpload operations. |
| 76 // InitiateUpload is an asynchronous function, so don't callback directly. | 76 // InitiateUpload is an asynchronous function, so don't callback directly. |
| 77 MessageLoop::current()->PostTask(FROM_HERE, | 77 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 78 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); | 78 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); |
| 79 } | 79 } |
| 80 | 80 |
| 81 virtual void InitiateUploadExistingFile( | 81 virtual void InitiateUploadExistingFile( |
| 82 const base::FilePath& drive_file_path, | 82 const base::FilePath& drive_file_path, |
| 83 const std::string& content_type, | 83 const std::string& content_type, |
| 84 int64 content_length, | 84 int64 content_length, |
| 85 const std::string& resource_id, | 85 const std::string& resource_id, |
| 86 const std::string& etag, | 86 const std::string& etag, |
| 87 const InitiateUploadCallback& callback) OVERRIDE { | 87 const InitiateUploadCallback& callback) OVERRIDE { |
| 88 EXPECT_EQ(kTestMimeType, content_type); | 88 EXPECT_EQ(kTestMimeType, content_type); |
| 89 EXPECT_EQ(expected_content_length_, content_length); | 89 EXPECT_EQ(expected_content_length_, content_length); |
| 90 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); | 90 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); |
| 91 | 91 |
| 92 if (!etag.empty() && etag != kTestETag) { | 92 if (!etag.empty() && etag != kTestETag) { |
| 93 MessageLoop::current()->PostTask(FROM_HERE, | 93 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 94 base::Bind(callback, HTTP_PRECONDITION, GURL())); | 94 base::Bind(callback, HTTP_PRECONDITION, GURL())); |
| 95 return; | 95 return; |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Calls back the upload URL for subsequent ResumeUpload operations. | 98 // Calls back the upload URL for subsequent ResumeUpload operations. |
| 99 // InitiateUpload is an asynchronous function, so don't callback directly. | 99 // InitiateUpload is an asynchronous function, so don't callback directly. |
| 100 MessageLoop::current()->PostTask(FROM_HERE, | 100 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 101 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); | 101 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // Handles a request for uploading a chunk of bytes. | 104 // Handles a request for uploading a chunk of bytes. |
| 105 virtual void ResumeUpload( | 105 virtual void ResumeUpload( |
| 106 const base::FilePath& drive_file_path, | 106 const base::FilePath& drive_file_path, |
| 107 const GURL& upload_location, | 107 const GURL& upload_location, |
| 108 int64 start_position, | 108 int64 start_position, |
| 109 int64 end_position, | 109 int64 end_position, |
| 110 int64 content_length, | 110 int64 content_length, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 131 | 131 |
| 132 // Update the internal status of the current upload session. | 132 // Update the internal status of the current upload session. |
| 133 resume_upload_call_count_++; | 133 resume_upload_call_count_++; |
| 134 received_bytes_ = end_position; | 134 received_bytes_ = end_position; |
| 135 | 135 |
| 136 // Callback progress | 136 // Callback progress |
| 137 if (!progress_callback.is_null()) { | 137 if (!progress_callback.is_null()) { |
| 138 // For the testing purpose, it always notifies the progress at the end of | 138 // For the testing purpose, it always notifies the progress at the end of |
| 139 // each chunk uploading. | 139 // each chunk uploading. |
| 140 int64 chunk_size = end_position - start_position; | 140 int64 chunk_size = end_position - start_position; |
| 141 MessageLoop::current()->PostTask(FROM_HERE, | 141 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 142 base::Bind(progress_callback, chunk_size, chunk_size)); | 142 base::Bind(progress_callback, chunk_size, chunk_size)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 SendUploadRangeResponse(upload_location, callback); | 145 SendUploadRangeResponse(upload_location, callback); |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Handles a request to fetch the current upload status. | 148 // Handles a request to fetch the current upload status. |
| 149 virtual void GetUploadStatus(const base::FilePath& drive_file_path, | 149 virtual void GetUploadStatus(const base::FilePath& drive_file_path, |
| 150 const GURL& upload_location, | 150 const GURL& upload_location, |
| 151 int64 content_length, | 151 int64 content_length, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 171 response = UploadRangeResponse(response_code, -1, -1); | 171 response = UploadRangeResponse(response_code, -1, -1); |
| 172 | 172 |
| 173 base::DictionaryValue dict; | 173 base::DictionaryValue dict; |
| 174 dict.Set("id.$t", new base::StringValue(kTestDummyId)); | 174 dict.Set("id.$t", new base::StringValue(kTestDummyId)); |
| 175 entry = ResourceEntry::CreateFrom(dict); | 175 entry = ResourceEntry::CreateFrom(dict); |
| 176 } else { | 176 } else { |
| 177 response = UploadRangeResponse( | 177 response = UploadRangeResponse( |
| 178 HTTP_RESUME_INCOMPLETE, 0, received_bytes_); | 178 HTTP_RESUME_INCOMPLETE, 0, received_bytes_); |
| 179 } | 179 } |
| 180 // ResumeUpload is an asynchronous function, so don't callback directly. | 180 // ResumeUpload is an asynchronous function, so don't callback directly. |
| 181 MessageLoop::current()->PostTask(FROM_HERE, | 181 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 182 base::Bind(callback, response, base::Passed(&entry))); | 182 base::Bind(callback, response, base::Passed(&entry))); |
| 183 } | 183 } |
| 184 | 184 |
| 185 const base::FilePath expected_upload_file_; | 185 const base::FilePath expected_upload_file_; |
| 186 const int64 expected_content_length_; | 186 const int64 expected_content_length_; |
| 187 int64 received_bytes_; | 187 int64 received_bytes_; |
| 188 int64 resume_upload_call_count_; | 188 int64 resume_upload_call_count_; |
| 189 }; | 189 }; |
| 190 | 190 |
| 191 // Mock DriveService that returns a failure at InitiateUpload(). | 191 // Mock DriveService that returns a failure at InitiateUpload(). |
| 192 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { | 192 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { |
| 193 // Returns error. | 193 // Returns error. |
| 194 virtual void InitiateUploadNewFile( | 194 virtual void InitiateUploadNewFile( |
| 195 const base::FilePath& drive_file_path, | 195 const base::FilePath& drive_file_path, |
| 196 const std::string& content_type, | 196 const std::string& content_type, |
| 197 int64 content_length, | 197 int64 content_length, |
| 198 const std::string& parent_resource_id, | 198 const std::string& parent_resource_id, |
| 199 const std::string& title, | 199 const std::string& title, |
| 200 const InitiateUploadCallback& callback) OVERRIDE { | 200 const InitiateUploadCallback& callback) OVERRIDE { |
| 201 MessageLoop::current()->PostTask(FROM_HERE, | 201 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 202 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 202 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
| 203 } | 203 } |
| 204 | 204 |
| 205 virtual void InitiateUploadExistingFile( | 205 virtual void InitiateUploadExistingFile( |
| 206 const base::FilePath& drive_file_path, | 206 const base::FilePath& drive_file_path, |
| 207 const std::string& content_type, | 207 const std::string& content_type, |
| 208 int64 content_length, | 208 int64 content_length, |
| 209 const std::string& resource_id, | 209 const std::string& resource_id, |
| 210 const std::string& etag, | 210 const std::string& etag, |
| 211 const InitiateUploadCallback& callback) OVERRIDE { | 211 const InitiateUploadCallback& callback) OVERRIDE { |
| 212 MessageLoop::current()->PostTask(FROM_HERE, | 212 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 213 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 213 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
| 214 } | 214 } |
| 215 | 215 |
| 216 // Should not be used. | 216 // Should not be used. |
| 217 virtual void ResumeUpload( | 217 virtual void ResumeUpload( |
| 218 const base::FilePath& drive_file_path, | 218 const base::FilePath& drive_file_path, |
| 219 const GURL& upload_url, | 219 const GURL& upload_url, |
| 220 int64 start_position, | 220 int64 start_position, |
| 221 int64 end_position, | 221 int64 end_position, |
| 222 int64 content_length, | 222 int64 content_length, |
| 223 const std::string& content_type, | 223 const std::string& content_type, |
| 224 const base::FilePath& local_file_path, | 224 const base::FilePath& local_file_path, |
| 225 const UploadRangeCallback& callback, | 225 const UploadRangeCallback& callback, |
| 226 const ProgressCallback& progress_callback) OVERRIDE { | 226 const ProgressCallback& progress_callback) OVERRIDE { |
| 227 NOTREACHED(); | 227 NOTREACHED(); |
| 228 } | 228 } |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 // Mock DriveService that returns a failure at ResumeUpload(). | 231 // Mock DriveService that returns a failure at ResumeUpload(). |
| 232 class MockDriveServiceNoConnectionAtResume : public DummyDriveService { | 232 class MockDriveServiceNoConnectionAtResume : public DummyDriveService { |
| 233 // Succeeds and returns an upload location URL. | 233 // Succeeds and returns an upload location URL. |
| 234 virtual void InitiateUploadNewFile( | 234 virtual void InitiateUploadNewFile( |
| 235 const base::FilePath& drive_file_path, | 235 const base::FilePath& drive_file_path, |
| 236 const std::string& content_type, | 236 const std::string& content_type, |
| 237 int64 content_length, | 237 int64 content_length, |
| 238 const std::string& parent_resource_id, | 238 const std::string& parent_resource_id, |
| 239 const std::string& title, | 239 const std::string& title, |
| 240 const InitiateUploadCallback& callback) OVERRIDE { | 240 const InitiateUploadCallback& callback) OVERRIDE { |
| 241 MessageLoop::current()->PostTask(FROM_HERE, | 241 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 242 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); | 242 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadNewFileURL))); |
| 243 } | 243 } |
| 244 | 244 |
| 245 virtual void InitiateUploadExistingFile( | 245 virtual void InitiateUploadExistingFile( |
| 246 const base::FilePath& drive_file_path, | 246 const base::FilePath& drive_file_path, |
| 247 const std::string& content_type, | 247 const std::string& content_type, |
| 248 int64 content_length, | 248 int64 content_length, |
| 249 const std::string& resource_id, | 249 const std::string& resource_id, |
| 250 const std::string& etag, | 250 const std::string& etag, |
| 251 const InitiateUploadCallback& callback) OVERRIDE { | 251 const InitiateUploadCallback& callback) OVERRIDE { |
| 252 MessageLoop::current()->PostTask(FROM_HERE, | 252 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 253 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); | 253 base::Bind(callback, HTTP_SUCCESS, GURL(kTestUploadExistingFileURL))); |
| 254 } | 254 } |
| 255 | 255 |
| 256 // Returns error. | 256 // Returns error. |
| 257 virtual void ResumeUpload( | 257 virtual void ResumeUpload( |
| 258 const base::FilePath& drive_file_path, | 258 const base::FilePath& drive_file_path, |
| 259 const GURL& upload_url, | 259 const GURL& upload_url, |
| 260 int64 start_position, | 260 int64 start_position, |
| 261 int64 end_position, | 261 int64 end_position, |
| 262 int64 content_length, | 262 int64 content_length, |
| 263 const std::string& content_type, | 263 const std::string& content_type, |
| 264 const base::FilePath& local_file_path, | 264 const base::FilePath& local_file_path, |
| 265 const UploadRangeCallback& callback, | 265 const UploadRangeCallback& callback, |
| 266 const ProgressCallback& progress_callback) OVERRIDE { | 266 const ProgressCallback& progress_callback) OVERRIDE { |
| 267 MessageLoop::current()->PostTask(FROM_HERE, | 267 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 268 base::Bind(callback, | 268 base::Bind(callback, |
| 269 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), | 269 UploadRangeResponse(GDATA_NO_CONNECTION, -1, -1), |
| 270 base::Passed(scoped_ptr<ResourceEntry>()))); | 270 base::Passed(scoped_ptr<ResourceEntry>()))); |
| 271 } | 271 } |
| 272 }; | 272 }; |
| 273 | 273 |
| 274 class DriveUploaderTest : public testing::Test { | 274 class DriveUploaderTest : public testing::Test { |
| 275 public: | 275 public: |
| 276 DriveUploaderTest() | 276 DriveUploaderTest() |
| 277 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 277 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 278 } | 278 } |
| 279 | 279 |
| 280 virtual void SetUp() OVERRIDE { | 280 virtual void SetUp() OVERRIDE { |
| 281 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 281 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 282 } | 282 } |
| 283 | 283 |
| 284 virtual void TearDown() OVERRIDE { | 284 virtual void TearDown() OVERRIDE { |
| 285 ASSERT_TRUE(temp_dir_.Delete()); | 285 ASSERT_TRUE(temp_dir_.Delete()); |
| 286 } | 286 } |
| 287 | 287 |
| 288 protected: | 288 protected: |
| 289 MessageLoopForUI message_loop_; | 289 base::MessageLoopForUI message_loop_; |
| 290 content::TestBrowserThread ui_thread_; | 290 content::TestBrowserThread ui_thread_; |
| 291 base::ScopedTempDir temp_dir_; | 291 base::ScopedTempDir temp_dir_; |
| 292 }; | 292 }; |
| 293 | 293 |
| 294 } // namespace | 294 } // namespace |
| 295 | 295 |
| 296 TEST_F(DriveUploaderTest, UploadExisting0KB) { | 296 TEST_F(DriveUploaderTest, UploadExisting0KB) { |
| 297 base::FilePath local_path; | 297 base::FilePath local_path; |
| 298 std::string data; | 298 std::string data; |
| 299 ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize( | 299 ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize( |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 EXPECT_EQ(HTTP_SUCCESS, error); | 530 EXPECT_EQ(HTTP_SUCCESS, error); |
| 531 EXPECT_TRUE(upload_location.is_empty()); | 531 EXPECT_TRUE(upload_location.is_empty()); |
| 532 ASSERT_TRUE(resource_entry); | 532 ASSERT_TRUE(resource_entry); |
| 533 EXPECT_EQ(kTestDummyId, resource_entry->id()); | 533 EXPECT_EQ(kTestDummyId, resource_entry->id()); |
| 534 ASSERT_EQ(1U, upload_progress_values.size()); | 534 ASSERT_EQ(1U, upload_progress_values.size()); |
| 535 EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024), | 535 EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024), |
| 536 upload_progress_values[0]); | 536 upload_progress_values[0]); |
| 537 } | 537 } |
| 538 | 538 |
| 539 } // namespace google_apis | 539 } // namespace google_apis |
| OLD | NEW |