Chromium Code Reviews| Index: content/browser/download/download_browsertest.cc |
| diff --git a/content/browser/download/download_browsertest.cc b/content/browser/download/download_browsertest.cc |
| index 46030b1e08c42065384c72aa98fd76f078939263..62e7fe8baac27cb691e4f9bb3ac242f42f3c2c06 100644 |
| --- a/content/browser/download/download_browsertest.cc |
| +++ b/content/browser/download/download_browsertest.cc |
| @@ -177,7 +177,6 @@ DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
| DownloadManager* download_manager, |
| bool calculate_hash, |
| const net::BoundNetLog& bound_net_log) { |
| - |
| return new DownloadFileWithDelay( |
| info, stream.Pass(), new DownloadRequestHandle(info->request_handle), |
| download_manager, calculate_hash, |
| @@ -224,6 +223,76 @@ void DownloadFileWithDelayFactory::WaitForSomeCallback() { |
| } |
| } |
| +class CountingDownloadFile : public DownloadFileImpl { |
|
benjhayden
2012/10/09 18:29:05
Want to put this in content/public/test?
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
Not until there's some need for it beyond this fil
|
| + public: |
| + CountingDownloadFile( |
| + const DownloadCreateInfo* info, |
| + scoped_ptr<content::ByteStreamReader> stream, |
| + DownloadRequestHandleInterface* request_handle, |
| + scoped_refptr<content::DownloadManager> download_manager, |
| + bool calculate_hash, |
| + scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| + const net::BoundNetLog& bound_net_log) |
| + : DownloadFileImpl(info, stream.Pass(), request_handle, download_manager, |
| + calculate_hash, power_save_blocker.Pass(), |
| + bound_net_log) {} |
| + |
| + virtual ~CountingDownloadFile() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + active_files_--; |
| + } |
| + |
| + virtual content::DownloadInterruptReason Initialize() OVERRIDE { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + active_files_++; |
| + return DownloadFileImpl::Initialize(); |
| + } |
| + |
| + static void GetNumberActiveFiles(int* result) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + *result = active_files_; |
| + } |
| + |
| + // Can be called on any thread, and will block (running message loop) |
| + // until data is returned. |
| + static int GetNumberActiveFilesFromFileThread() { |
| + int result; |
|
benjhayden
2012/10/09 18:29:05
Can you just '= -1'? Un-initialized variables make
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
Whoops. Sure. Done.
|
| + BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&CountingDownloadFile::GetNumberActiveFiles, &result), |
| + MessageLoop::current()->QuitClosure()); |
| + MessageLoop::current()->Run(); |
| + return result; |
| + } |
| + |
| + private: |
| + static int active_files_; |
| +}; |
| + |
| +int CountingDownloadFile::active_files_ = 0; |
| + |
| +class CountingDownloadFileFactory : public DownloadFileFactory { |
|
benjhayden
2012/10/09 18:29:05
Templatize DownloadFileFactory?
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
Templatize? I'm not sure what you mean? What wou
benjhayden
2012/10/09 20:01:47
Ah, sorry, I was wondering if the pattern of a DFF
|
| + public: |
| + CountingDownloadFileFactory() {} |
| + virtual ~CountingDownloadFileFactory() {} |
| + |
| + // DownloadFileFactory interface. |
| + virtual content::DownloadFile* CreateFile( |
| + DownloadCreateInfo* info, |
| + scoped_ptr<content::ByteStreamReader> stream, |
| + DownloadManager* download_manager, |
| + bool calculate_hash, |
| + const net::BoundNetLog& bound_net_log) OVERRIDE { |
| + return new CountingDownloadFile( |
| + info, stream.Pass(), new DownloadRequestHandle(info->request_handle), |
| + download_manager, calculate_hash, |
| + scoped_ptr<content::PowerSaveBlocker>( |
| + new content::PowerSaveBlocker( |
| + content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| + "Download in progress")).Pass(), |
| + bound_net_log); |
| + } |
| +}; |
| + |
| } // namespace |
| class DownloadContentTest : public ContentBrowserTest { |
| @@ -263,13 +332,21 @@ class DownloadContentTest : public ContentBrowserTest { |
| return new DownloadTestObserverInProgress(download_manager, num_downloads); |
| } |
| + // Note: Cannot be used with other alternative DownloadFileFactorys |
|
benjhayden
2012/10/09 18:29:05
How likely is it that we'd want to use Counting* w
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
It's my usual metric: I don't want to add complexi
|
| + void SetupEnsureNoPendingDownloads() { |
| + GetDownloadFileManager()->SetFileFactoryForTesting( |
| + scoped_ptr<content::DownloadFileFactory>( |
| + new CountingDownloadFileFactory()).Pass()); |
| + } |
| + |
| bool EnsureNoPendingDownloads() { |
| bool result = true; |
| BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, |
| base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
| MessageLoop::current()->Run(); |
| - return result && DownloadManager::EnsureNoPendingDownloadsForTesting(); |
| + return result && |
| + (CountingDownloadFile::GetNumberActiveFilesFromFileThread() == 0); |
| } |
| void DownloadAndWait(Shell* shell, const GURL& url) { |
| @@ -342,6 +419,8 @@ IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { |
| // DownloadItem, and wait for the state that indicates the item has been |
| // entered in the history and made visible in the UI. |
| + SetupEnsureNoPendingDownloads(); |
| + |
| // Create a download, wait until it's started, and confirm |
| // we're in the expected state. |
| scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
| @@ -366,6 +445,8 @@ IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { |
| // Check that downloading multiple (in this case, 2) files does not result in |
| // corrupted files. |
| IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { |
| + SetupEnsureNoPendingDownloads(); |
| + |
| // Create a download, wait until it's started, and confirm |
| // we're in the expected state. |
| scoped_ptr<DownloadTestObserver> observer1( |