| 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 // This file contains download browser tests that are known to be runnable | 5 // This file contains download browser tests that are known to be runnable |
| 6 // in a pure content context. Over time tests should be migrated here. | 6 // in a pure content context. Over time tests should be migrated here. |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 const base::FilePath& default_download_directory, | 115 const base::FilePath& default_download_directory, |
| 116 const GURL& url, | 116 const GURL& url, |
| 117 const GURL& referrer_url, | 117 const GURL& referrer_url, |
| 118 bool calculate_hash, | 118 bool calculate_hash, |
| 119 scoped_ptr<ByteStreamReader> stream, | 119 scoped_ptr<ByteStreamReader> stream, |
| 120 const net::BoundNetLog& bound_net_log, | 120 const net::BoundNetLog& bound_net_log, |
| 121 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 121 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 122 base::WeakPtr<DownloadDestinationObserver> observer, | 122 base::WeakPtr<DownloadDestinationObserver> observer, |
| 123 base::WeakPtr<DownloadFileWithDelayFactory> owner); | 123 base::WeakPtr<DownloadFileWithDelayFactory> owner); |
| 124 | 124 |
| 125 virtual ~DownloadFileWithDelay(); | 125 ~DownloadFileWithDelay() override; |
| 126 | 126 |
| 127 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, | 127 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, |
| 128 // storing it in the factory that produced this object for later | 128 // storing it in the factory that produced this object for later |
| 129 // retrieval. | 129 // retrieval. |
| 130 virtual void RenameAndUniquify( | 130 void RenameAndUniquify(const base::FilePath& full_path, |
| 131 const base::FilePath& full_path, | 131 const RenameCompletionCallback& callback) override; |
| 132 const RenameCompletionCallback& callback) override; | 132 void RenameAndAnnotate(const base::FilePath& full_path, |
| 133 virtual void RenameAndAnnotate( | 133 const RenameCompletionCallback& callback) override; |
| 134 const base::FilePath& full_path, | |
| 135 const RenameCompletionCallback& callback) override; | |
| 136 | 134 |
| 137 private: | 135 private: |
| 138 static void RenameCallbackWrapper( | 136 static void RenameCallbackWrapper( |
| 139 const base::WeakPtr<DownloadFileWithDelayFactory>& factory, | 137 const base::WeakPtr<DownloadFileWithDelayFactory>& factory, |
| 140 const RenameCompletionCallback& original_callback, | 138 const RenameCompletionCallback& original_callback, |
| 141 DownloadInterruptReason reason, | 139 DownloadInterruptReason reason, |
| 142 const base::FilePath& path); | 140 const base::FilePath& path); |
| 143 | 141 |
| 144 // This variable may only be read on the FILE thread, and may only be | 142 // This variable may only be read on the FILE thread, and may only be |
| 145 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) | 143 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) |
| 146 // on the UI thread. This is because after construction, | 144 // on the UI thread. This is because after construction, |
| 147 // DownloadFileWithDelay lives on the file thread, but | 145 // DownloadFileWithDelay lives on the file thread, but |
| 148 // DownloadFileWithDelayFactory is purely a UI thread object. | 146 // DownloadFileWithDelayFactory is purely a UI thread object. |
| 149 base::WeakPtr<DownloadFileWithDelayFactory> owner_; | 147 base::WeakPtr<DownloadFileWithDelayFactory> owner_; |
| 150 | 148 |
| 151 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); | 149 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); |
| 152 }; | 150 }; |
| 153 | 151 |
| 154 // All routines on this class must be called on the UI thread. | 152 // All routines on this class must be called on the UI thread. |
| 155 class DownloadFileWithDelayFactory : public DownloadFileFactory { | 153 class DownloadFileWithDelayFactory : public DownloadFileFactory { |
| 156 public: | 154 public: |
| 157 DownloadFileWithDelayFactory(); | 155 DownloadFileWithDelayFactory(); |
| 158 virtual ~DownloadFileWithDelayFactory(); | 156 ~DownloadFileWithDelayFactory() override; |
| 159 | 157 |
| 160 // DownloadFileFactory interface. | 158 // DownloadFileFactory interface. |
| 161 virtual DownloadFile* CreateFile( | 159 DownloadFile* CreateFile( |
| 162 scoped_ptr<DownloadSaveInfo> save_info, | 160 scoped_ptr<DownloadSaveInfo> save_info, |
| 163 const base::FilePath& default_download_directory, | 161 const base::FilePath& default_download_directory, |
| 164 const GURL& url, | 162 const GURL& url, |
| 165 const GURL& referrer_url, | 163 const GURL& referrer_url, |
| 166 bool calculate_hash, | 164 bool calculate_hash, |
| 167 scoped_ptr<ByteStreamReader> stream, | 165 scoped_ptr<ByteStreamReader> stream, |
| 168 const net::BoundNetLog& bound_net_log, | 166 const net::BoundNetLog& bound_net_log, |
| 169 base::WeakPtr<DownloadDestinationObserver> observer) override; | 167 base::WeakPtr<DownloadDestinationObserver> observer) override; |
| 170 | 168 |
| 171 void AddRenameCallback(base::Closure callback); | 169 void AddRenameCallback(base::Closure callback); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 const GURL& referrer_url, | 283 const GURL& referrer_url, |
| 286 bool calculate_hash, | 284 bool calculate_hash, |
| 287 scoped_ptr<ByteStreamReader> stream, | 285 scoped_ptr<ByteStreamReader> stream, |
| 288 const net::BoundNetLog& bound_net_log, | 286 const net::BoundNetLog& bound_net_log, |
| 289 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 287 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 290 base::WeakPtr<DownloadDestinationObserver> observer) | 288 base::WeakPtr<DownloadDestinationObserver> observer) |
| 291 : DownloadFileImpl(save_info.Pass(), default_downloads_directory, | 289 : DownloadFileImpl(save_info.Pass(), default_downloads_directory, |
| 292 url, referrer_url, calculate_hash, | 290 url, referrer_url, calculate_hash, |
| 293 stream.Pass(), bound_net_log, observer) {} | 291 stream.Pass(), bound_net_log, observer) {} |
| 294 | 292 |
| 295 virtual ~CountingDownloadFile() { | 293 ~CountingDownloadFile() override { |
| 296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 297 active_files_--; | 295 active_files_--; |
| 298 } | 296 } |
| 299 | 297 |
| 300 virtual void Initialize(const InitializeCallback& callback) override { | 298 void Initialize(const InitializeCallback& callback) override { |
| 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 302 active_files_++; | 300 active_files_++; |
| 303 return DownloadFileImpl::Initialize(callback); | 301 return DownloadFileImpl::Initialize(callback); |
| 304 } | 302 } |
| 305 | 303 |
| 306 static void GetNumberActiveFiles(int* result) { | 304 static void GetNumberActiveFiles(int* result) { |
| 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 308 *result = active_files_; | 306 *result = active_files_; |
| 309 } | 307 } |
| 310 | 308 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 324 | 322 |
| 325 private: | 323 private: |
| 326 static int active_files_; | 324 static int active_files_; |
| 327 }; | 325 }; |
| 328 | 326 |
| 329 int CountingDownloadFile::active_files_ = 0; | 327 int CountingDownloadFile::active_files_ = 0; |
| 330 | 328 |
| 331 class CountingDownloadFileFactory : public DownloadFileFactory { | 329 class CountingDownloadFileFactory : public DownloadFileFactory { |
| 332 public: | 330 public: |
| 333 CountingDownloadFileFactory() {} | 331 CountingDownloadFileFactory() {} |
| 334 virtual ~CountingDownloadFileFactory() {} | 332 ~CountingDownloadFileFactory() override {} |
| 335 | 333 |
| 336 // DownloadFileFactory interface. | 334 // DownloadFileFactory interface. |
| 337 virtual DownloadFile* CreateFile( | 335 DownloadFile* CreateFile( |
| 338 scoped_ptr<DownloadSaveInfo> save_info, | 336 scoped_ptr<DownloadSaveInfo> save_info, |
| 339 const base::FilePath& default_downloads_directory, | 337 const base::FilePath& default_downloads_directory, |
| 340 const GURL& url, | 338 const GURL& url, |
| 341 const GURL& referrer_url, | 339 const GURL& referrer_url, |
| 342 bool calculate_hash, | 340 bool calculate_hash, |
| 343 scoped_ptr<ByteStreamReader> stream, | 341 scoped_ptr<ByteStreamReader> stream, |
| 344 const net::BoundNetLog& bound_net_log, | 342 const net::BoundNetLog& bound_net_log, |
| 345 base::WeakPtr<DownloadDestinationObserver> observer) override { | 343 base::WeakPtr<DownloadDestinationObserver> observer) override { |
| 346 scoped_ptr<PowerSaveBlocker> psb( | 344 scoped_ptr<PowerSaveBlocker> psb( |
| 347 PowerSaveBlocker::Create( | 345 PowerSaveBlocker::Create( |
| 348 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 346 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 349 "Download in progress")); | 347 "Download in progress")); |
| 350 return new CountingDownloadFile( | 348 return new CountingDownloadFile( |
| 351 save_info.Pass(), default_downloads_directory, url, referrer_url, | 349 save_info.Pass(), default_downloads_directory, url, referrer_url, |
| 352 calculate_hash, stream.Pass(), bound_net_log, | 350 calculate_hash, stream.Pass(), bound_net_log, |
| 353 psb.Pass(), observer); | 351 psb.Pass(), observer); |
| 354 } | 352 } |
| 355 }; | 353 }; |
| 356 | 354 |
| 357 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { | 355 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { |
| 358 public: | 356 public: |
| 359 TestShellDownloadManagerDelegate() | 357 TestShellDownloadManagerDelegate() |
| 360 : delay_download_open_(false) {} | 358 : delay_download_open_(false) {} |
| 361 virtual ~TestShellDownloadManagerDelegate() {} | 359 ~TestShellDownloadManagerDelegate() override {} |
| 362 | 360 |
| 363 virtual bool ShouldOpenDownload( | 361 bool ShouldOpenDownload( |
| 364 DownloadItem* item, | 362 DownloadItem* item, |
| 365 const DownloadOpenDelayedCallback& callback) override { | 363 const DownloadOpenDelayedCallback& callback) override { |
| 366 if (delay_download_open_) { | 364 if (delay_download_open_) { |
| 367 delayed_callbacks_.push_back(callback); | 365 delayed_callbacks_.push_back(callback); |
| 368 return false; | 366 return false; |
| 369 } | 367 } |
| 370 return true; | 368 return true; |
| 371 } | 369 } |
| 372 | 370 |
| 373 void SetDelayedOpen(bool delay) { | 371 void SetDelayedOpen(bool delay) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 393 | 391 |
| 394 typedef std::vector<RecordStruct> RecordVector; | 392 typedef std::vector<RecordStruct> RecordVector; |
| 395 | 393 |
| 396 RecordingDownloadObserver(DownloadItem* download) | 394 RecordingDownloadObserver(DownloadItem* download) |
| 397 : download_(download) { | 395 : download_(download) { |
| 398 last_state_.state = download->GetState(); | 396 last_state_.state = download->GetState(); |
| 399 last_state_.bytes_received = download->GetReceivedBytes(); | 397 last_state_.bytes_received = download->GetReceivedBytes(); |
| 400 download_->AddObserver(this); | 398 download_->AddObserver(this); |
| 401 } | 399 } |
| 402 | 400 |
| 403 virtual ~RecordingDownloadObserver() { | 401 ~RecordingDownloadObserver() override { RemoveObserver(); } |
| 404 RemoveObserver(); | |
| 405 } | |
| 406 | 402 |
| 407 void CompareToExpectedRecord(const RecordStruct expected[], size_t size) { | 403 void CompareToExpectedRecord(const RecordStruct expected[], size_t size) { |
| 408 EXPECT_EQ(size, record_.size()); | 404 EXPECT_EQ(size, record_.size()); |
| 409 int min = size > record_.size() ? record_.size() : size; | 405 int min = size > record_.size() ? record_.size() : size; |
| 410 for (int i = 0; i < min; ++i) { | 406 for (int i = 0; i < min; ++i) { |
| 411 EXPECT_EQ(expected[i].state, record_[i].state) << "Iteration " << i; | 407 EXPECT_EQ(expected[i].state, record_[i].state) << "Iteration " << i; |
| 412 EXPECT_EQ(expected[i].bytes_received, record_[i].bytes_received) | 408 EXPECT_EQ(expected[i].bytes_received, record_[i].bytes_received) |
| 413 << "Iteration " << i; | 409 << "Iteration " << i; |
| 414 } | 410 } |
| 415 } | 411 } |
| 416 | 412 |
| 417 private: | 413 private: |
| 418 virtual void OnDownloadUpdated(DownloadItem* download) override { | 414 void OnDownloadUpdated(DownloadItem* download) override { |
| 419 DCHECK_EQ(download_, download); | 415 DCHECK_EQ(download_, download); |
| 420 DownloadItem::DownloadState state = download->GetState(); | 416 DownloadItem::DownloadState state = download->GetState(); |
| 421 int bytes = download->GetReceivedBytes(); | 417 int bytes = download->GetReceivedBytes(); |
| 422 if (last_state_.state != state || last_state_.bytes_received > bytes) { | 418 if (last_state_.state != state || last_state_.bytes_received > bytes) { |
| 423 last_state_.state = state; | 419 last_state_.state = state; |
| 424 last_state_.bytes_received = bytes; | 420 last_state_.bytes_received = bytes; |
| 425 record_.push_back(last_state_); | 421 record_.push_back(last_state_); |
| 426 } | 422 } |
| 427 } | 423 } |
| 428 | 424 |
| 429 virtual void OnDownloadDestroyed(DownloadItem* download) override { | 425 void OnDownloadDestroyed(DownloadItem* download) override { |
| 430 DCHECK_EQ(download_, download); | 426 DCHECK_EQ(download_, download); |
| 431 RemoveObserver(); | 427 RemoveObserver(); |
| 432 } | 428 } |
| 433 | 429 |
| 434 void RemoveObserver() { | 430 void RemoveObserver() { |
| 435 if (download_) { | 431 if (download_) { |
| 436 download_->RemoveObserver(this); | 432 download_->RemoveObserver(this); |
| 437 download_ = NULL; | 433 download_ = NULL; |
| 438 } | 434 } |
| 439 } | 435 } |
| 440 | 436 |
| 441 DownloadItem* download_; | 437 DownloadItem* download_; |
| 442 RecordStruct last_state_; | 438 RecordStruct last_state_; |
| 443 RecordVector record_; | 439 RecordVector record_; |
| 444 }; | 440 }; |
| 445 | 441 |
| 446 // Get the next created download. | 442 // Get the next created download. |
| 447 class DownloadCreateObserver : DownloadManager::Observer { | 443 class DownloadCreateObserver : DownloadManager::Observer { |
| 448 public: | 444 public: |
| 449 DownloadCreateObserver(DownloadManager* manager) | 445 DownloadCreateObserver(DownloadManager* manager) |
| 450 : manager_(manager), | 446 : manager_(manager), |
| 451 item_(NULL), | 447 item_(NULL), |
| 452 waiting_(false) { | 448 waiting_(false) { |
| 453 manager_->AddObserver(this); | 449 manager_->AddObserver(this); |
| 454 } | 450 } |
| 455 | 451 |
| 456 virtual ~DownloadCreateObserver() { | 452 ~DownloadCreateObserver() override { |
| 457 if (manager_) | 453 if (manager_) |
| 458 manager_->RemoveObserver(this); | 454 manager_->RemoveObserver(this); |
| 459 manager_ = NULL; | 455 manager_ = NULL; |
| 460 } | 456 } |
| 461 | 457 |
| 462 virtual void ManagerGoingDown(DownloadManager* manager) override { | 458 void ManagerGoingDown(DownloadManager* manager) override { |
| 463 DCHECK_EQ(manager_, manager); | 459 DCHECK_EQ(manager_, manager); |
| 464 manager_->RemoveObserver(this); | 460 manager_->RemoveObserver(this); |
| 465 manager_ = NULL; | 461 manager_ = NULL; |
| 466 } | 462 } |
| 467 | 463 |
| 468 virtual void OnDownloadCreated(DownloadManager* manager, | 464 void OnDownloadCreated(DownloadManager* manager, |
| 469 DownloadItem* download) override { | 465 DownloadItem* download) override { |
| 470 if (!item_) | 466 if (!item_) |
| 471 item_ = download; | 467 item_ = download; |
| 472 | 468 |
| 473 if (waiting_) | 469 if (waiting_) |
| 474 base::MessageLoopForUI::current()->Quit(); | 470 base::MessageLoopForUI::current()->Quit(); |
| 475 } | 471 } |
| 476 | 472 |
| 477 DownloadItem* WaitForFinished() { | 473 DownloadItem* WaitForFinished() { |
| 478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 479 if (!item_) { | 475 if (!item_) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 // help up by buffering in the underlying downloads ByteStream data | 563 // help up by buffering in the underlying downloads ByteStream data |
| 568 // transfer. This is important because on resumption tests we wait | 564 // transfer. This is important because on resumption tests we wait |
| 569 // until we've gotten the data we expect before allowing the test server | 565 // until we've gotten the data we expect before allowing the test server |
| 570 // to send its reset, to get around hard close semantics on the Windows | 566 // to send its reset, to get around hard close semantics on the Windows |
| 571 // socket layer implementation. | 567 // socket layer implementation. |
| 572 int GetSafeBufferChunk() const { | 568 int GetSafeBufferChunk() const { |
| 573 return (DownloadResourceHandler::kDownloadByteStreamSize / | 569 return (DownloadResourceHandler::kDownloadByteStreamSize / |
| 574 ByteStreamWriter::kFractionBufferBeforeSending) + 1; | 570 ByteStreamWriter::kFractionBufferBeforeSending) + 1; |
| 575 } | 571 } |
| 576 | 572 |
| 577 virtual void SetUpOnMainThread() override { | 573 void SetUpOnMainThread() override { |
| 578 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 574 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
| 579 | 575 |
| 580 test_delegate_.reset(new TestShellDownloadManagerDelegate()); | 576 test_delegate_.reset(new TestShellDownloadManagerDelegate()); |
| 581 test_delegate_->SetDownloadBehaviorForTesting(downloads_directory_.path()); | 577 test_delegate_->SetDownloadBehaviorForTesting(downloads_directory_.path()); |
| 582 DownloadManager* manager = DownloadManagerForShell(shell()); | 578 DownloadManager* manager = DownloadManagerForShell(shell()); |
| 583 manager->GetDelegate()->Shutdown(); | 579 manager->GetDelegate()->Shutdown(); |
| 584 manager->SetDelegate(test_delegate_.get()); | 580 manager->SetDelegate(test_delegate_.get()); |
| 585 test_delegate_->SetDownloadManager(manager); | 581 test_delegate_->SetDownloadManager(manager); |
| 586 | 582 |
| 587 BrowserThread::PostTask( | 583 BrowserThread::PostTask( |
| (...skipping 1226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1814 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); | 1810 ASSERT_TRUE(test_server.InitializeAndWaitUntilReady()); |
| 1815 | 1811 |
| 1816 GURL url = test_server.GetURL("/empty.bin"); | 1812 GURL url = test_server.GetURL("/empty.bin"); |
| 1817 test_server.ServeFilesFromDirectory(GetTestFilePath("download", "")); | 1813 test_server.ServeFilesFromDirectory(GetTestFilePath("download", "")); |
| 1818 | 1814 |
| 1819 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); | 1815 NavigateToURLAndWaitForDownload(shell(), url, DownloadItem::COMPLETE); |
| 1820 // That's it. This should work without crashing. | 1816 // That's it. This should work without crashing. |
| 1821 } | 1817 } |
| 1822 | 1818 |
| 1823 } // namespace content | 1819 } // namespace content |
| OLD | NEW |