| 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 "content/browser/download/download_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "content/browser/browser_thread_impl.h" | 12 #include "content/browser/browser_thread_impl.h" |
| 13 #include "content/browser/download/download_buffer.h" | 13 #include "content/browser/download/byte_stream.h" |
| 14 #include "content/browser/download/download_create_info.h" | 14 #include "content/browser/download/download_create_info.h" |
| 15 #include "content/browser/download/download_interrupt_reasons_impl.h" | 15 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 16 #include "content/browser/download/download_request_handle.h" | 16 #include "content/browser/download/download_request_handle.h" |
| 17 #include "content/browser/download/mock_download_file.h" | 17 #include "content/browser/download/mock_download_file.h" |
| 18 #include "content/public/browser/download_id.h" | 18 #include "content/public/browser/download_id.h" |
| 19 #include "content/test/mock_download_manager.h" | 19 #include "content/test/mock_download_manager.h" |
| 20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 class MockDownloadFileFactory : | 38 class MockDownloadFileFactory : |
| 39 public DownloadFileManager::DownloadFileFactory { | 39 public DownloadFileManager::DownloadFileFactory { |
| 40 | 40 |
| 41 public: | 41 public: |
| 42 MockDownloadFileFactory() {} | 42 MockDownloadFileFactory() {} |
| 43 virtual ~MockDownloadFileFactory() {} | 43 virtual ~MockDownloadFileFactory() {} |
| 44 | 44 |
| 45 virtual content::DownloadFile* CreateFile( | 45 virtual content::DownloadFile* CreateFile( |
| 46 DownloadCreateInfo* info, | 46 DownloadCreateInfo* info, |
| 47 scoped_ptr<content::ByteStreamReader> stream, |
| 47 const DownloadRequestHandle& request_handle, | 48 const DownloadRequestHandle& request_handle, |
| 48 content::DownloadManager* download_manager, | 49 content::DownloadManager* download_manager, |
| 49 bool calculate_hash, | 50 bool calculate_hash, |
| 50 const net::BoundNetLog& bound_net_log) OVERRIDE; | 51 const net::BoundNetLog& bound_net_log) OVERRIDE; |
| 51 | 52 |
| 52 MockDownloadFile* GetExistingFile(const DownloadId& id); | 53 MockDownloadFile* GetExistingFile(const DownloadId& id); |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 std::map<DownloadId, MockDownloadFile*> files_; | 56 std::map<DownloadId, MockDownloadFile*> files_; |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 content::DownloadFile* MockDownloadFileFactory::CreateFile( | 59 content::DownloadFile* MockDownloadFileFactory::CreateFile( |
| 59 DownloadCreateInfo* info, | 60 DownloadCreateInfo* info, |
| 61 scoped_ptr<content::ByteStreamReader> stream, |
| 60 const DownloadRequestHandle& request_handle, | 62 const DownloadRequestHandle& request_handle, |
| 61 content::DownloadManager* download_manager, | 63 content::DownloadManager* download_manager, |
| 62 bool calculate_hash, | 64 bool calculate_hash, |
| 63 const net::BoundNetLog& bound_net_log) { | 65 const net::BoundNetLog& bound_net_log) { |
| 64 DCHECK(files_.end() == files_.find(info->download_id)); | 66 DCHECK(files_.end() == files_.find(info->download_id)); |
| 65 MockDownloadFile* created_file = new MockDownloadFile(); | 67 MockDownloadFile* created_file = new MockDownloadFile(); |
| 66 files_[info->download_id] = created_file; | 68 files_[info->download_id] = created_file; |
| 67 | 69 |
| 68 ON_CALL(*created_file, GetDownloadManager()) | 70 ON_CALL(*created_file, GetDownloadManager()) |
| 69 .WillByDefault(Return(download_manager)); | 71 .WillByDefault(Return(download_manager)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 static const int32 kDummyDownloadId; | 122 static const int32 kDummyDownloadId; |
| 121 static const int32 kDummyDownloadId2; | 123 static const int32 kDummyDownloadId2; |
| 122 static const int kDummyChildId; | 124 static const int kDummyChildId; |
| 123 static const int kDummyRequestId; | 125 static const int kDummyRequestId; |
| 124 | 126 |
| 125 // We need a UI |BrowserThread| in order to destruct |download_manager_|, | 127 // We need a UI |BrowserThread| in order to destruct |download_manager_|, |
| 126 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, | 128 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, |
| 127 // calling Release() on |download_manager_| won't ever result in its | 129 // calling Release() on |download_manager_| won't ever result in its |
| 128 // destructor being called and we get a leak. | 130 // destructor being called and we get a leak. |
| 129 DownloadFileManagerTest() | 131 DownloadFileManagerTest() |
| 130 : download_buffer_(new content::DownloadBuffer()), | 132 : ui_thread_(BrowserThread::UI, &loop_), |
| 131 ui_thread_(BrowserThread::UI, &loop_), | |
| 132 file_thread_(BrowserThread::FILE, &loop_) { | 133 file_thread_(BrowserThread::FILE, &loop_) { |
| 133 } | 134 } |
| 134 | 135 |
| 135 ~DownloadFileManagerTest() { | 136 ~DownloadFileManagerTest() { |
| 136 } | 137 } |
| 137 | 138 |
| 138 virtual void SetUp() { | 139 virtual void SetUp() { |
| 139 download_manager_ = new MockDownloadManager(); | 140 download_manager_ = new MockDownloadManager(); |
| 140 request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); | 141 request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); |
| 141 download_file_factory_ = new MockDownloadFileFactory; | 142 download_file_factory_ = new MockDownloadFileFactory; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 159 // Clears all gmock expectations for the download file |id| and the manager. | 160 // Clears all gmock expectations for the download file |id| and the manager. |
| 160 void ClearExpectations(DownloadId id) { | 161 void ClearExpectations(DownloadId id) { |
| 161 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 162 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
| 162 Mock::VerifyAndClearExpectations(file); | 163 Mock::VerifyAndClearExpectations(file); |
| 163 Mock::VerifyAndClearExpectations(download_manager_); | 164 Mock::VerifyAndClearExpectations(download_manager_); |
| 164 } | 165 } |
| 165 | 166 |
| 166 // Start a download. | 167 // Start a download. |
| 167 // |info| is the information needed to create a new download file. | 168 // |info| is the information needed to create a new download file. |
| 168 // |id| is the download ID of the new download file. | 169 // |id| is the download ID of the new download file. |
| 169 void StartDownload(DownloadCreateInfo* info, const DownloadId& id) { | 170 void StartDownload(scoped_ptr<DownloadCreateInfo> info, |
| 171 const DownloadId& id) { |
| 170 // Expected call sequence: | 172 // Expected call sequence: |
| 171 // StartDownload | 173 // StartDownload |
| 172 // DownloadManager::CreateDownloadItem | 174 // DownloadManager::CreateDownloadItem |
| 173 // DownloadManagerDelegate::GenerateFileHash | 175 // DownloadManagerDelegate::GenerateFileHash |
| 174 // Process one message in the message loop | 176 // Process one message in the message loop |
| 175 // CreateDownloadFile | 177 // CreateDownloadFile |
| 176 // new MockDownloadFile, add to downloads_[id] | 178 // new MockDownloadFile, add to downloads_[id] |
| 177 // DownloadRequestHandle::ResumeRequest | 179 // DownloadRequestHandle::ResumeRequest |
| 178 // StartUpdateTimer | 180 // StartUpdateTimer |
| 179 // DownloadCreateInfo is destroyed | 181 // DownloadCreateInfo is destroyed |
| 180 // Process one message in the message loop | 182 // Process one message in the message loop |
| 181 // DownloadManager::StartDownload | 183 // DownloadManager::StartDownload |
| 182 info->download_id = id; | 184 info->download_id = id; |
| 183 | 185 |
| 184 // Set expectations and return values. | 186 // Set expectations and return values. |
| 185 EXPECT_CALL(*download_manager_, CreateDownloadItem(info, _)) | 187 EXPECT_CALL(*download_manager_, CreateDownloadItem(info.get(), _)) |
| 186 .Times(1) | 188 .Times(1) |
| 187 .WillOnce(Return(net::BoundNetLog())); | 189 .WillOnce(Return(net::BoundNetLog())); |
| 188 EXPECT_CALL(*download_manager_, GenerateFileHash()) | 190 EXPECT_CALL(*download_manager_, GenerateFileHash()) |
| 189 .Times(AtLeast(1)) | 191 .Times(AtLeast(1)) |
| 190 .WillRepeatedly(Return(false)); | 192 .WillRepeatedly(Return(false)); |
| 191 EXPECT_CALL(*download_manager_, StartDownload(id.local())); | 193 EXPECT_CALL(*download_manager_, StartDownload(id.local())); |
| 192 | 194 |
| 193 download_file_manager_->StartDownload(info, *request_handle_); | 195 download_file_manager_->StartDownload( |
| 196 info.Pass(), scoped_ptr<content::ByteStreamReader>(), *request_handle_); |
| 194 ProcessAllPendingMessages(); | 197 ProcessAllPendingMessages(); |
| 195 | 198 |
| 196 ClearExpectations(id); | 199 ClearExpectations(id); |
| 197 } | 200 } |
| 198 | 201 |
| 199 // Creates a new |net::IOBuffer| of size |length| with |data|, and attaches | |
| 200 // it to the download buffer. | |
| 201 bool UpdateBuffer(const char* data, size_t length) { | |
| 202 // We are passing ownership of this buffer to the download file manager. | |
| 203 // NOTE: we are padding io_buffer with one extra character so that the | |
| 204 // mock testing framework can treat it as a NULL-delimited string. | |
| 205 net::IOBuffer* io_buffer = new net::IOBuffer(length + 1); | |
| 206 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. | |
| 207 io_buffer->AddRef(); | |
| 208 memcpy(io_buffer->data(), data, length); | |
| 209 io_buffer->data()[length] = 0; | |
| 210 | |
| 211 download_buffer_->AddData(io_buffer, length); | |
| 212 | |
| 213 return true; | |
| 214 } | |
| 215 | |
| 216 // Updates the download file. | |
| 217 // |id| is the download ID of the download file. | |
| 218 // |data| is the buffer containing the data. | |
| 219 // |length| is the length of the data buffer. | |
| 220 // |error_to_insert| is the error to simulate. For no error, use net::OK. | |
| 221 void UpdateDownload(const DownloadId& id, | |
| 222 const char* data, | |
| 223 size_t length, | |
| 224 net::Error error_to_insert) { | |
| 225 // Expected call sequence: | |
| 226 // Create DownloadBuffer | |
| 227 // UpdateDownload | |
| 228 // GetDownloadFile | |
| 229 // DownloadFile::AppendDataToFile | |
| 230 // | |
| 231 // On error: | |
| 232 // DownloadFile::GetDownloadManager | |
| 233 // DownloadFile::BytesSoFar | |
| 234 // CancelDownload | |
| 235 // Process one message in the message loop | |
| 236 // DownloadManager::OnDownloadInterrupted | |
| 237 EXPECT_TRUE(UpdateBuffer(data, length)); | |
| 238 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | |
| 239 ASSERT_TRUE(file != NULL); | |
| 240 byte_count_[id] += length; | |
| 241 | |
| 242 // Make sure our comparison string is NULL-terminated. | |
| 243 char* expected_data = new char[length + 1]; | |
| 244 memcpy(expected_data, data, length); | |
| 245 expected_data[length] = 0; | |
| 246 | |
| 247 EXPECT_CALL(*file, AppendDataToFile(StrEq(expected_data), length)) | |
| 248 .Times(1) | |
| 249 .WillOnce(Return(error_to_insert)); | |
| 250 | |
| 251 if (error_to_insert != net::OK) { | |
| 252 EXPECT_CALL(*file, GetDownloadManager()) | |
| 253 .Times(AtLeast(1)); | |
| 254 EXPECT_CALL(*file, BytesSoFar()) | |
| 255 .Times(AtLeast(1)) | |
| 256 .WillRepeatedly(Return(byte_count_[id])); | |
| 257 EXPECT_CALL(*file, GetHashState()) | |
| 258 .Times(AtLeast(1)); | |
| 259 EXPECT_CALL(*file, Cancel()); | |
| 260 } | |
| 261 | |
| 262 download_file_manager_->UpdateDownload(id, download_buffer_.get()); | |
| 263 | |
| 264 if (error_to_insert != net::OK) { | |
| 265 EXPECT_CALL(*download_manager_, | |
| 266 OnDownloadInterrupted( | |
| 267 id.local(), | |
| 268 byte_count_[id], | |
| 269 "", | |
| 270 content::ConvertNetErrorToInterruptReason( | |
| 271 error_to_insert, content::DOWNLOAD_INTERRUPT_FROM_DISK))); | |
| 272 | |
| 273 ProcessAllPendingMessages(); | |
| 274 ++error_count_[id]; | |
| 275 } | |
| 276 | |
| 277 ClearExpectations(id); | |
| 278 delete [] expected_data; | |
| 279 } | |
| 280 | |
| 281 // Renames the download file. | 202 // Renames the download file. |
| 282 // |id| is the download ID of the download file. | 203 // |id| is the download ID of the download file. |
| 283 // |new_path| is the new file path. | 204 // |new_path| is the new file path. |
| 284 // |unique_path| is the actual path that the download file will be | 205 // |unique_path| is the actual path that the download file will be |
| 285 // renamed to. If there is an existing file at | 206 // renamed to. If there is an existing file at |
| 286 // |new_path| and |replace| is false, then |new_path| | 207 // |new_path| and |replace| is false, then |new_path| |
| 287 // will be uniquified. | 208 // will be uniquified. |
| 288 // |rename_error| is the error to inject. For no error, use net::OK. | 209 // |rename_error| is the error to inject. For no error, use net::OK. |
| 289 // |state| whether we are renaming an in-progress download or a | 210 // |state| whether we are renaming an in-progress download or a |
| 290 // completed download. | 211 // completed download. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 content::DOWNLOAD_INTERRUPT_FROM_DISK))); | 273 content::DOWNLOAD_INTERRUPT_FROM_DISK))); |
| 353 ProcessAllPendingMessages(); | 274 ProcessAllPendingMessages(); |
| 354 ++error_count_[id]; | 275 ++error_count_[id]; |
| 355 } else if (state == COMPLETE) { | 276 } else if (state == COMPLETE) { |
| 356 EXPECT_CALL(*download_manager_, OnDownloadRenamedToFinalName( | 277 EXPECT_CALL(*download_manager_, OnDownloadRenamedToFinalName( |
| 357 id.local(), unique_path, _)); | 278 id.local(), unique_path, _)); |
| 358 ProcessAllPendingMessages(); | 279 ProcessAllPendingMessages(); |
| 359 } | 280 } |
| 360 } | 281 } |
| 361 | 282 |
| 362 // Finish the download operation. | |
| 363 // |id| is the download ID of the download file. | |
| 364 // |reason| is the interrupt reason to inject. For no interrupt, use | |
| 365 // DOWNLOAD_INTERRUPT_REASON_NONE. | |
| 366 // |security_string| is the extra security information, if interrupted. | |
| 367 void OnResponseCompleted(const DownloadId& id, | |
| 368 content::DownloadInterruptReason reason, | |
| 369 const std::string& security_string) { | |
| 370 // OnResponseCompleted | |
| 371 // GetDownloadFile | |
| 372 // DownloadFile::Finish | |
| 373 // DownloadFile::GetDownloadManager | |
| 374 // DownloadFile::BytesSoFar | |
| 375 // Process one message in the message loop | |
| 376 // | |
| 377 // OK: | |
| 378 // DownloadFile::GetHash | |
| 379 // DownloadManager::OnResponseCompleted | |
| 380 // | |
| 381 // On Error: | |
| 382 // DownloadManager::OnDownloadInterrupted | |
| 383 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | |
| 384 ASSERT_TRUE(file != NULL); | |
| 385 | |
| 386 EXPECT_CALL(*file, Finish()); | |
| 387 EXPECT_CALL(*file, GetDownloadManager()); | |
| 388 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 389 EXPECT_CALL(*file, GetHash(_)) | |
| 390 .WillOnce(Return(false)); | |
| 391 } else { | |
| 392 EXPECT_CALL(*file, GetHashState()); | |
| 393 } | |
| 394 EXPECT_CALL(*file, BytesSoFar()) | |
| 395 .Times(AtLeast(1)) | |
| 396 .WillRepeatedly(Return(byte_count_[id])); | |
| 397 | |
| 398 download_file_manager_->OnResponseCompleted(id, reason, security_string); | |
| 399 | |
| 400 if (reason == content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 401 EXPECT_CALL(*download_manager_, | |
| 402 OnResponseCompleted(id.local(), byte_count_[id], "")) | |
| 403 .Times(1); | |
| 404 } else { | |
| 405 EXPECT_EQ(0, error_count_[id]); | |
| 406 | |
| 407 EXPECT_CALL(*download_manager_, | |
| 408 OnDownloadInterrupted(id.local(), | |
| 409 byte_count_[id], | |
| 410 "", | |
| 411 reason)) | |
| 412 .Times(1); | |
| 413 } | |
| 414 | |
| 415 ProcessAllPendingMessages(); | |
| 416 | |
| 417 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) | |
| 418 ++error_count_[id]; | |
| 419 | |
| 420 ClearExpectations(id); | |
| 421 } | |
| 422 | |
| 423 void CleanUp(DownloadId id) { | 283 void CleanUp(DownloadId id) { |
| 424 // Expected calls: | 284 // Expected calls: |
| 425 // DownloadFileManager::CancelDownload | 285 // DownloadFileManager::CancelDownload |
| 426 // DownloadFile::Cancel | 286 // DownloadFile::Cancel |
| 427 // DownloadFileManager::EraseDownload | 287 // DownloadFileManager::EraseDownload |
| 428 // if no more downloads: | 288 // if no more downloads: |
| 429 // DownloadFileManager::StopUpdateTimer | 289 // DownloadFileManager::StopUpdateTimer |
| 430 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 290 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
| 431 ASSERT_TRUE(file != NULL); | 291 ASSERT_TRUE(file != NULL); |
| 432 | 292 |
| 433 EXPECT_CALL(*file, Cancel()); | 293 EXPECT_CALL(*file, Cancel()); |
| 434 | 294 |
| 435 download_file_manager_->CancelDownload(id); | 295 download_file_manager_->CancelDownload(id); |
| 436 | 296 |
| 437 EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); | 297 EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); |
| 438 } | 298 } |
| 439 | 299 |
| 440 protected: | 300 protected: |
| 441 scoped_refptr<MockDownloadManager> download_manager_; | 301 scoped_refptr<MockDownloadManager> download_manager_; |
| 442 scoped_ptr<MockDownloadRequestHandle> request_handle_; | 302 scoped_ptr<MockDownloadRequestHandle> request_handle_; |
| 443 MockDownloadFileFactory* download_file_factory_; | 303 MockDownloadFileFactory* download_file_factory_; |
| 444 scoped_refptr<DownloadFileManager> download_file_manager_; | 304 scoped_refptr<DownloadFileManager> download_file_manager_; |
| 445 scoped_refptr<content::DownloadBuffer> download_buffer_; | |
| 446 | 305 |
| 447 // Per-download statistics. | 306 // Per-download statistics. |
| 448 std::map<DownloadId, int64> byte_count_; | 307 std::map<DownloadId, int64> byte_count_; |
| 449 std::map<DownloadId, int> error_count_; | 308 std::map<DownloadId, int> error_count_; |
| 450 | 309 |
| 451 private: | 310 private: |
| 452 MessageLoop loop_; | 311 MessageLoop loop_; |
| 453 | 312 |
| 454 // UI thread. | 313 // UI thread. |
| 455 BrowserThreadImpl ui_thread_; | 314 BrowserThreadImpl ui_thread_; |
| 456 | 315 |
| 457 // File thread to satisfy debug checks in DownloadFile. | 316 // File thread to satisfy debug checks in DownloadFile. |
| 458 BrowserThreadImpl file_thread_; | 317 BrowserThreadImpl file_thread_; |
| 459 }; | 318 }; |
| 460 | 319 |
| 461 const char* DownloadFileManagerTest::kTestData1 = | 320 const char* DownloadFileManagerTest::kTestData1 = |
| 462 "Let's write some data to the file!\n"; | 321 "Let's write some data to the file!\n"; |
| 463 const char* DownloadFileManagerTest::kTestData2 = "Writing more data.\n"; | 322 const char* DownloadFileManagerTest::kTestData2 = "Writing more data.\n"; |
| 464 const char* DownloadFileManagerTest::kTestData3 = "Final line."; | 323 const char* DownloadFileManagerTest::kTestData3 = "Final line."; |
| 465 const char* DownloadFileManagerTest::kTestData4 = "Writing, writing, writing\n"; | 324 const char* DownloadFileManagerTest::kTestData4 = "Writing, writing, writing\n"; |
| 466 const char* DownloadFileManagerTest::kTestData5 = "All we do is writing,\n"; | 325 const char* DownloadFileManagerTest::kTestData5 = "All we do is writing,\n"; |
| 467 const char* DownloadFileManagerTest::kTestData6 = "Rawhide!"; | 326 const char* DownloadFileManagerTest::kTestData6 = "Rawhide!"; |
| 468 | 327 |
| 469 const int32 DownloadFileManagerTest::kDummyDownloadId = 23; | 328 const int32 DownloadFileManagerTest::kDummyDownloadId = 23; |
| 470 const int32 DownloadFileManagerTest::kDummyDownloadId2 = 77; | 329 const int32 DownloadFileManagerTest::kDummyDownloadId2 = 77; |
| 471 const int DownloadFileManagerTest::kDummyChildId = 3; | 330 const int DownloadFileManagerTest::kDummyChildId = 3; |
| 472 const int DownloadFileManagerTest::kDummyRequestId = 67; | 331 const int DownloadFileManagerTest::kDummyRequestId = 67; |
| 473 | 332 |
| 474 TEST_F(DownloadFileManagerTest, CancelAtStart) { | 333 TEST_F(DownloadFileManagerTest, CancelAtStart) { |
| 475 DownloadCreateInfo* info = new DownloadCreateInfo; | 334 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 476 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 335 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 477 | 336 |
| 478 StartDownload(info, dummy_id); | 337 StartDownload(info.Pass(), dummy_id); |
| 479 | 338 |
| 480 CleanUp(dummy_id); | 339 CleanUp(dummy_id); |
| 481 } | 340 } |
| 482 | 341 |
| 483 TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { | 342 TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { |
| 484 // Same as StartDownload, at first. | 343 // Same as StartDownload, at first. |
| 485 DownloadCreateInfo* info = new DownloadCreateInfo; | 344 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 486 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 345 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 487 | 346 |
| 488 StartDownload(info, dummy_id); | 347 StartDownload(info.Pass(), dummy_id); |
| 489 | |
| 490 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 491 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 492 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 493 | |
| 494 CleanUp(dummy_id); | |
| 495 } | |
| 496 | |
| 497 TEST_F(DownloadFileManagerTest, DownloadWithError) { | |
| 498 // Same as StartDownload, at first. | |
| 499 DownloadCreateInfo* info = new DownloadCreateInfo; | |
| 500 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 501 | |
| 502 StartDownload(info, dummy_id); | |
| 503 | |
| 504 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 505 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), | |
| 506 net::ERR_FILE_NO_SPACE); | |
| 507 } | |
| 508 | |
| 509 TEST_F(DownloadFileManagerTest, CompleteDownload) { | |
| 510 // Same as StartDownload, at first. | |
| 511 DownloadCreateInfo* info = new DownloadCreateInfo; | |
| 512 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 513 | |
| 514 StartDownload(info, dummy_id); | |
| 515 | |
| 516 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 517 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 518 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 519 | |
| 520 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 521 | |
| 522 CleanUp(dummy_id); | |
| 523 } | |
| 524 | |
| 525 TEST_F(DownloadFileManagerTest, CompleteDownloadWithError) { | |
| 526 // Same as StartDownload, at first. | |
| 527 DownloadCreateInfo* info = new DownloadCreateInfo; | |
| 528 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 529 | |
| 530 StartDownload(info, dummy_id); | |
| 531 | |
| 532 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 533 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 534 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 535 | |
| 536 OnResponseCompleted(dummy_id, | |
| 537 content::DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED, | |
| 538 ""); | |
| 539 | 348 |
| 540 CleanUp(dummy_id); | 349 CleanUp(dummy_id); |
| 541 } | 350 } |
| 542 | 351 |
| 543 TEST_F(DownloadFileManagerTest, RenameInProgress) { | 352 TEST_F(DownloadFileManagerTest, RenameInProgress) { |
| 544 // Same as StartDownload, at first. | 353 // Same as StartDownload, at first. |
| 545 DownloadCreateInfo* info = new DownloadCreateInfo; | 354 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 546 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 355 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 547 ScopedTempDir download_dir; | 356 ScopedTempDir download_dir; |
| 548 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 357 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 549 | 358 |
| 550 StartDownload(info, dummy_id); | 359 StartDownload(info.Pass(), dummy_id); |
| 551 | 360 |
| 552 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 553 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 554 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 361 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 555 RenameFile(dummy_id, foo, foo, net::OK, IN_PROGRESS, OVERWRITE); | 362 RenameFile(dummy_id, foo, foo, net::OK, IN_PROGRESS, OVERWRITE); |
| 556 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 557 | |
| 558 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 559 | 363 |
| 560 CleanUp(dummy_id); | 364 CleanUp(dummy_id); |
| 561 } | 365 } |
| 562 | 366 |
| 563 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { | 367 TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { |
| 564 // Same as StartDownload, at first. | 368 // Same as StartDownload, at first. |
| 565 DownloadCreateInfo* info = new DownloadCreateInfo; | 369 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 566 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 370 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 567 ScopedTempDir download_dir; | 371 ScopedTempDir download_dir; |
| 568 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 372 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 569 | 373 |
| 570 StartDownload(info, dummy_id); | 374 StartDownload(info.Pass(), dummy_id); |
| 571 | 375 |
| 572 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 573 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 574 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 376 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 575 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, | 377 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, |
| 576 IN_PROGRESS, OVERWRITE); | 378 IN_PROGRESS, OVERWRITE); |
| 577 | 379 |
| 578 CleanUp(dummy_id); | 380 CleanUp(dummy_id); |
| 579 } | 381 } |
| 580 | 382 |
| 581 TEST_F(DownloadFileManagerTest, RenameCompleting) { | 383 TEST_F(DownloadFileManagerTest, RenameWithUniquification) { |
| 582 // Same as StartDownload, at first. | 384 // Same as StartDownload, at first. |
| 583 DownloadCreateInfo* info = new DownloadCreateInfo; | 385 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 584 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 386 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 585 ScopedTempDir download_dir; | 387 ScopedTempDir download_dir; |
| 586 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 388 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 587 | 389 |
| 588 StartDownload(info, dummy_id); | 390 StartDownload(info.Pass(), dummy_id); |
| 589 | |
| 590 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 591 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 592 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 593 | |
| 594 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 595 | |
| 596 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | |
| 597 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | |
| 598 | |
| 599 CleanUp(dummy_id); | |
| 600 } | |
| 601 | |
| 602 TEST_F(DownloadFileManagerTest, RenameCompletingWithUniquification) { | |
| 603 // Same as StartDownload, at first. | |
| 604 DownloadCreateInfo* info = new DownloadCreateInfo; | |
| 605 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 606 ScopedTempDir download_dir; | |
| 607 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | |
| 608 | |
| 609 StartDownload(info, dummy_id); | |
| 610 | |
| 611 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 612 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 613 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 614 | |
| 615 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 616 | 391 |
| 617 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 392 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 618 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); | 393 FilePath unique_foo(foo.InsertBeforeExtension(FILE_PATH_LITERAL(" (1)"))); |
| 619 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, | 394 // Create a file at |foo|. Since we are specifying DONT_OVERWRITE, |
| 620 // RenameCompletingDownloadFile() should pick "foo (1).txt" instead of | 395 // RenameCompletingDownloadFile() should pick "foo (1).txt" instead of |
| 621 // overwriting this file. | 396 // overwriting this file. |
| 622 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); | 397 ASSERT_EQ(0, file_util::WriteFile(foo, "", 0)); |
| 623 RenameFile(dummy_id, foo, unique_foo, net::OK, COMPLETE, DONT_OVERWRITE); | 398 RenameFile(dummy_id, foo, unique_foo, net::OK, COMPLETE, DONT_OVERWRITE); |
| 624 | 399 |
| 625 CleanUp(dummy_id); | 400 CleanUp(dummy_id); |
| 626 } | 401 } |
| 627 | 402 |
| 628 TEST_F(DownloadFileManagerTest, RenameCompletingWithError) { | |
| 629 // Same as StartDownload, at first. | |
| 630 DownloadCreateInfo* info = new DownloadCreateInfo; | |
| 631 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | |
| 632 ScopedTempDir download_dir; | |
| 633 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | |
| 634 | |
| 635 StartDownload(info, dummy_id); | |
| 636 | |
| 637 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 638 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 639 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 640 | |
| 641 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 642 | |
| 643 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | |
| 644 RenameFile(dummy_id, foo, foo, net::ERR_FILE_PATH_TOO_LONG, | |
| 645 COMPLETE, OVERWRITE); | |
| 646 | |
| 647 CleanUp(dummy_id); | |
| 648 } | |
| 649 | |
| 650 TEST_F(DownloadFileManagerTest, RenameTwice) { | 403 TEST_F(DownloadFileManagerTest, RenameTwice) { |
| 651 // Same as StartDownload, at first. | 404 // Same as StartDownload, at first. |
| 652 DownloadCreateInfo* info = new DownloadCreateInfo; | 405 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 653 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 406 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 654 ScopedTempDir download_dir; | 407 ScopedTempDir download_dir; |
| 655 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 408 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 656 | 409 |
| 657 StartDownload(info, dummy_id); | 410 StartDownload(info.Pass(), dummy_id); |
| 658 | 411 |
| 659 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | |
| 660 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 661 FilePath crfoo(download_dir.path().Append( | 412 FilePath crfoo(download_dir.path().Append( |
| 662 FILE_PATH_LITERAL("foo.txt.crdownload"))); | 413 FILE_PATH_LITERAL("foo.txt.crdownload"))); |
| 663 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); | 414 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); |
| 664 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 665 | |
| 666 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 667 | 415 |
| 668 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 416 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 669 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 417 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
| 670 | 418 |
| 671 CleanUp(dummy_id); | 419 CleanUp(dummy_id); |
| 672 } | 420 } |
| 673 | 421 |
| 674 TEST_F(DownloadFileManagerTest, TwoDownloads) { | 422 TEST_F(DownloadFileManagerTest, TwoDownloads) { |
| 675 // Same as StartDownload, at first. | 423 // Same as StartDownload, at first. |
| 676 DownloadCreateInfo* info = new DownloadCreateInfo; | 424 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 677 DownloadCreateInfo* info2 = new DownloadCreateInfo; | 425 scoped_ptr<DownloadCreateInfo> info2(new DownloadCreateInfo); |
| 678 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); | 426 DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); |
| 679 DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); | 427 DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); |
| 680 ScopedTempDir download_dir; | 428 ScopedTempDir download_dir; |
| 681 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); | 429 ASSERT_TRUE(download_dir.CreateUniqueTempDir()); |
| 682 | 430 |
| 683 StartDownload(info, dummy_id); | 431 StartDownload(info.Pass(), dummy_id); |
| 684 | 432 |
| 685 UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); | 433 StartDownload(info2.Pass(), dummy_id2); |
| 686 | 434 |
| 687 StartDownload(info2, dummy_id2); | |
| 688 | |
| 689 UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); | |
| 690 | |
| 691 UpdateDownload(dummy_id2, kTestData4, strlen(kTestData4), net::OK); | |
| 692 FilePath crbar(download_dir.path().Append( | 435 FilePath crbar(download_dir.path().Append( |
| 693 FILE_PATH_LITERAL("bar.txt.crdownload"))); | 436 FILE_PATH_LITERAL("bar.txt.crdownload"))); |
| 694 RenameFile(dummy_id2, crbar, crbar, net::OK, IN_PROGRESS, OVERWRITE); | 437 RenameFile(dummy_id2, crbar, crbar, net::OK, IN_PROGRESS, OVERWRITE); |
| 695 | 438 |
| 696 FilePath crfoo(download_dir.path().Append( | 439 FilePath crfoo(download_dir.path().Append( |
| 697 FILE_PATH_LITERAL("foo.txt.crdownload"))); | 440 FILE_PATH_LITERAL("foo.txt.crdownload"))); |
| 698 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); | 441 RenameFile(dummy_id, crfoo, crfoo, net::OK, IN_PROGRESS, OVERWRITE); |
| 699 | 442 |
| 700 UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); | |
| 701 | |
| 702 UpdateDownload(dummy_id2, kTestData5, strlen(kTestData5), net::OK); | |
| 703 UpdateDownload(dummy_id2, kTestData6, strlen(kTestData6), net::OK); | |
| 704 | |
| 705 OnResponseCompleted(dummy_id2, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 706 | |
| 707 OnResponseCompleted(dummy_id, content::DOWNLOAD_INTERRUPT_REASON_NONE, ""); | |
| 708 | 443 |
| 709 FilePath bar(download_dir.path().Append(FILE_PATH_LITERAL("bar.txt"))); | 444 FilePath bar(download_dir.path().Append(FILE_PATH_LITERAL("bar.txt"))); |
| 710 RenameFile(dummy_id2, bar, bar, net::OK, COMPLETE, OVERWRITE); | 445 RenameFile(dummy_id2, bar, bar, net::OK, COMPLETE, OVERWRITE); |
| 711 | 446 |
| 712 CleanUp(dummy_id2); | 447 CleanUp(dummy_id2); |
| 713 | 448 |
| 714 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); | 449 FilePath foo(download_dir.path().Append(FILE_PATH_LITERAL("foo.txt"))); |
| 715 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); | 450 RenameFile(dummy_id, foo, foo, net::OK, COMPLETE, OVERWRITE); |
| 716 | 451 |
| 717 CleanUp(dummy_id); | 452 CleanUp(dummy_id); |
| 718 } | 453 } |
| 719 | 454 |
| 720 | 455 |
| 721 // TODO(ahendrickson) -- A test for updating progress. | 456 // TODO(ahendrickson) -- A test for updating progress. |
| 722 // Expected call sequence: | 457 // Expected call sequence: |
| 723 // UpdateInProgressDownloads | 458 // UpdateInProgressDownloads |
| 724 // DownloadFile::GetDownloadFile | 459 // DownloadFile::GetDownloadFile |
| 725 // Process one message in the message loop | 460 // Process one message in the message loop |
| 726 // DownloadManager::UpdateDownload | 461 // DownloadManager::UpdateDownload |
| 727 | 462 |
| 728 // TODO(ahendrickson) -- A test for download manager shutdown. | 463 // TODO(ahendrickson) -- A test for download manager shutdown. |
| 729 // Expected call sequence: | 464 // Expected call sequence: |
| 730 // OnDownloadManagerShutdown | 465 // OnDownloadManagerShutdown |
| 731 // DownloadFile::GetDownloadManager | 466 // DownloadFile::GetDownloadManager |
| 732 // DownloadFile::CancelDownloadRequest | 467 // DownloadFile::CancelDownloadRequest |
| 733 // DownloadFile::~DownloadFile | 468 // DownloadFile::~DownloadFile |
| OLD | NEW |