| 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/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| 11 #include "content/browser/download/download_file_factory.h" | 11 #include "content/browser/download/download_file_factory.h" |
| 12 #include "content/browser/download/download_file_impl.h" | 12 #include "content/browser/download/download_file_impl.h" |
| 13 #include "content/browser/download/download_file_manager.h" | |
| 14 #include "content/browser/download/download_item_impl.h" | 13 #include "content/browser/download/download_item_impl.h" |
| 15 #include "content/browser/download/download_manager_impl.h" | 14 #include "content/browser/download/download_manager_impl.h" |
| 16 #include "content/browser/power_save_blocker.h" | 15 #include "content/browser/power_save_blocker.h" |
| 17 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | |
| 18 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 19 #include "content/public/test/download_test_observer.h" | 17 #include "content/public/test/download_test_observer.h" |
| 20 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
| 21 #include "content/shell/shell.h" | 19 #include "content/shell/shell.h" |
| 22 #include "content/shell/shell_browser_context.h" | 20 #include "content/shell/shell_browser_context.h" |
| 23 #include "content/shell/shell_download_manager_delegate.h" | 21 #include "content/shell/shell_download_manager_delegate.h" |
| 24 #include "content/test/content_browser_test.h" | 22 #include "content/test/content_browser_test.h" |
| 25 #include "content/test/content_browser_test_utils.h" | 23 #include "content/test/content_browser_test_utils.h" |
| 26 #include "content/test/net/url_request_mock_http_job.h" | 24 #include "content/test/net/url_request_mock_http_job.h" |
| 27 #include "content/test/net/url_request_slow_download_job.h" | 25 #include "content/test/net/url_request_slow_download_job.h" |
| 28 #include "googleurl/src/gurl.h" | 26 #include "googleurl/src/gurl.h" |
| 29 | 27 |
| 30 namespace content { | 28 namespace content { |
| 31 | 29 |
| 32 namespace { | 30 namespace { |
| 33 | 31 |
| 34 class DownloadFileWithDelayFactory; | 32 class DownloadFileWithDelayFactory; |
| 35 | 33 |
| 36 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) { | 34 static DownloadManagerImpl* DownloadManagerForShell(Shell* shell) { |
| 37 // We're in a content_browsertest; we know that the DownloadManager | 35 // We're in a content_browsertest; we know that the DownloadManager |
| 38 // is a DownloadManagerImpl. | 36 // is a DownloadManagerImpl. |
| 39 return static_cast<DownloadManagerImpl*>( | 37 return static_cast<DownloadManagerImpl*>( |
| 40 BrowserContext::GetDownloadManager( | 38 BrowserContext::GetDownloadManager( |
| 41 shell->web_contents()->GetBrowserContext())); | 39 shell->web_contents()->GetBrowserContext())); |
| 42 } | 40 } |
| 43 | 41 |
| 44 class DownloadFileWithDelay : public DownloadFileImpl { | 42 class DownloadFileWithDelay : public DownloadFileImpl { |
| 45 public: | 43 public: |
| 46 DownloadFileWithDelay( | 44 DownloadFileWithDelay( |
| 47 const DownloadCreateInfo* info, | 45 const content::DownloadSaveInfo& save_info, |
| 48 scoped_ptr<content::ByteStreamReader> stream, | 46 const FilePath& default_download_directory, |
| 49 DownloadRequestHandleInterface* request_handle, | 47 const GURL& url, |
| 50 scoped_refptr<content::DownloadManager> download_manager, | 48 const GURL& referrer_url, |
| 51 bool calculate_hash, | 49 int64 received_bytes, |
| 52 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 50 bool calculate_hash, |
| 53 const net::BoundNetLog& bound_net_log, | 51 scoped_ptr<content::ByteStreamReader> stream, |
| 54 // |owner| is required to outlive the DownloadFileWithDelay. | 52 const net::BoundNetLog& bound_net_log, |
| 55 DownloadFileWithDelayFactory* owner); | 53 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| 54 base::WeakPtr<content::DownloadDestinationObserver> observer, |
| 55 base::WeakPtr<DownloadFileWithDelayFactory> owner); |
| 56 | 56 |
| 57 virtual ~DownloadFileWithDelay(); | 57 virtual ~DownloadFileWithDelay(); |
| 58 | 58 |
| 59 // Wraps DownloadFileImpl::Rename and intercepts the return callback, | 59 // Wraps DownloadFileImpl::Rename and intercepts the return callback, |
| 60 // storing it in the factory that produced this object for later | 60 // storing it in the factory that produced this object for later |
| 61 // retrieval. | 61 // retrieval. |
| 62 virtual void Rename(const FilePath& full_path, | 62 virtual void Rename(const FilePath& full_path, |
| 63 bool overwrite_existing_file, | 63 bool overwrite_existing_file, |
| 64 const RenameCompletionCallback& callback) OVERRIDE; | 64 const RenameCompletionCallback& callback) OVERRIDE; |
| 65 | 65 |
| 66 // Wraps DownloadFileImpl::Detach and intercepts the return callback, | 66 // Wraps DownloadFileImpl::Detach and intercepts the return callback, |
| 67 // storing it in the factory that produced this object for later | 67 // storing it in the factory that produced this object for later |
| 68 // retrieval. | 68 // retrieval. |
| 69 virtual void Detach(base::Closure callback) OVERRIDE; | 69 virtual void Detach(base::Closure callback) OVERRIDE; |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 static void RenameCallbackWrapper( | 72 static void RenameCallbackWrapper( |
| 73 DownloadFileWithDelayFactory* factory, | 73 DownloadFileWithDelayFactory* factory, |
| 74 const RenameCompletionCallback& original_callback, | 74 const RenameCompletionCallback& original_callback, |
| 75 content::DownloadInterruptReason reason, | 75 content::DownloadInterruptReason reason, |
| 76 const FilePath& path); | 76 const FilePath& path); |
| 77 | 77 |
| 78 static void DetachCallbackWrapper( | 78 static void DetachCallbackWrapper( |
| 79 DownloadFileWithDelayFactory* factory, | 79 DownloadFileWithDelayFactory* factory, |
| 80 const base::Closure& original_callback); | 80 const base::Closure& original_callback); |
| 81 | 81 |
| 82 // May only be used on the UI thread. | 82 // This variable may only be read on the FILE thread, and may only be |
| 83 DownloadFileWithDelayFactory* owner_; | 83 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) |
| 84 // on the UI thread. This is because after construction, |
| 85 // DownloadFileWithDelay lives on the file thread, but |
| 86 // DownloadFileWithDelayFactory is purely a UI thread object. |
| 87 base::WeakPtr<DownloadFileWithDelayFactory> owner_; |
| 84 | 88 |
| 85 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); | 89 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); |
| 86 }; | 90 }; |
| 87 | 91 |
| 92 // All routines on this class must be called on the UI thread. |
| 88 class DownloadFileWithDelayFactory : public DownloadFileFactory { | 93 class DownloadFileWithDelayFactory : public DownloadFileFactory { |
| 89 public: | 94 public: |
| 90 DownloadFileWithDelayFactory(); | 95 DownloadFileWithDelayFactory(); |
| 91 virtual ~DownloadFileWithDelayFactory(); | 96 virtual ~DownloadFileWithDelayFactory(); |
| 92 | 97 |
| 93 // DownloadFileFactory interface. | 98 // DownloadFileFactory interface. |
| 94 virtual content::DownloadFile* CreateFile( | 99 virtual DownloadFile* CreateFile( |
| 95 DownloadCreateInfo* info, | 100 const content::DownloadSaveInfo& save_info, |
| 101 const FilePath& default_download_directory, |
| 102 GURL url, |
| 103 GURL referrer_url, |
| 104 int64 received_bytes, |
| 105 bool calculate_hash, |
| 96 scoped_ptr<content::ByteStreamReader> stream, | 106 scoped_ptr<content::ByteStreamReader> stream, |
| 97 DownloadManager* download_manager, | 107 const net::BoundNetLog& bound_net_log, |
| 98 bool calculate_hash, | 108 base::WeakPtr<content::DownloadDestinationObserver> observer) OVERRIDE; |
| 99 const net::BoundNetLog& bound_net_log) OVERRIDE; | |
| 100 | 109 |
| 101 // Must all be called on the UI thread. | |
| 102 void AddRenameCallback(base::Closure callback); | 110 void AddRenameCallback(base::Closure callback); |
| 103 void AddDetachCallback(base::Closure callback); | 111 void AddDetachCallback(base::Closure callback); |
| 104 void GetAllRenameCallbacks(std::vector<base::Closure>* results); | 112 void GetAllRenameCallbacks(std::vector<base::Closure>* results); |
| 105 void GetAllDetachCallbacks(std::vector<base::Closure>* results); | 113 void GetAllDetachCallbacks(std::vector<base::Closure>* results); |
| 106 | 114 |
| 107 // Do not return until either GetAllRenameCallbacks() or | 115 // Do not return until either GetAllRenameCallbacks() or |
| 108 // GetAllDetachCallbacks() will return a non-empty list. | 116 // GetAllDetachCallbacks() will return a non-empty list. |
| 109 void WaitForSomeCallback(); | 117 void WaitForSomeCallback(); |
| 110 | 118 |
| 111 private: | 119 private: |
| 120 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; |
| 112 std::vector<base::Closure> rename_callbacks_; | 121 std::vector<base::Closure> rename_callbacks_; |
| 113 std::vector<base::Closure> detach_callbacks_; | 122 std::vector<base::Closure> detach_callbacks_; |
| 114 bool waiting_; | 123 bool waiting_; |
| 115 | 124 |
| 116 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); | 125 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); |
| 117 }; | 126 }; |
| 118 | 127 |
| 119 DownloadFileWithDelay::DownloadFileWithDelay( | 128 DownloadFileWithDelay::DownloadFileWithDelay( |
| 120 const DownloadCreateInfo* info, | 129 const content::DownloadSaveInfo& save_info, |
| 121 scoped_ptr<content::ByteStreamReader> stream, | 130 const FilePath& default_download_directory, |
| 122 DownloadRequestHandleInterface* request_handle, | 131 const GURL& url, |
| 123 scoped_refptr<content::DownloadManager> download_manager, | 132 const GURL& referrer_url, |
| 124 bool calculate_hash, | 133 int64 received_bytes, |
| 125 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 134 bool calculate_hash, |
| 126 const net::BoundNetLog& bound_net_log, | 135 scoped_ptr<content::ByteStreamReader> stream, |
| 127 DownloadFileWithDelayFactory* owner) | 136 const net::BoundNetLog& bound_net_log, |
| 128 : DownloadFileImpl(info, stream.Pass(), request_handle, download_manager, | 137 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| 129 calculate_hash, power_save_blocker.Pass(), | 138 base::WeakPtr<content::DownloadDestinationObserver> observer, |
| 130 bound_net_log), | 139 base::WeakPtr<DownloadFileWithDelayFactory> owner) |
| 140 : DownloadFileImpl( |
| 141 save_info, default_download_directory, url, referrer_url, |
| 142 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 143 power_save_blocker.Pass(), observer), |
| 131 owner_(owner) {} | 144 owner_(owner) {} |
| 132 | 145 |
| 133 DownloadFileWithDelay::~DownloadFileWithDelay() {} | 146 DownloadFileWithDelay::~DownloadFileWithDelay() {} |
| 134 | 147 |
| 135 void DownloadFileWithDelay::Rename(const FilePath& full_path, | 148 void DownloadFileWithDelay::Rename(const FilePath& full_path, |
| 136 bool overwrite_existing_file, | 149 bool overwrite_existing_file, |
| 137 const RenameCompletionCallback& callback) { | 150 const RenameCompletionCallback& callback) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 139 DownloadFileImpl::Rename( | 152 DownloadFileImpl::Rename( |
| 140 full_path, overwrite_existing_file, | 153 full_path, overwrite_existing_file, |
| 141 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, | 154 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, |
| 142 base::Unretained(owner_), callback)); | 155 owner_, callback)); |
| 143 } | 156 } |
| 144 | 157 |
| 145 void DownloadFileWithDelay::Detach(base::Closure callback) { | 158 void DownloadFileWithDelay::Detach(base::Closure callback) { |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 147 DownloadFileImpl::Detach( | 160 DownloadFileImpl::Detach( |
| 148 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, | 161 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, |
| 149 base::Unretained(owner_), callback)); | 162 owner_, callback)); |
| 150 } | 163 } |
| 151 | 164 |
| 152 // static | 165 // static |
| 153 void DownloadFileWithDelay::RenameCallbackWrapper( | 166 void DownloadFileWithDelay::RenameCallbackWrapper( |
| 154 DownloadFileWithDelayFactory* factory, | 167 DownloadFileWithDelayFactory* factory, |
| 155 const RenameCompletionCallback& original_callback, | 168 const RenameCompletionCallback& original_callback, |
| 156 content::DownloadInterruptReason reason, | 169 content::DownloadInterruptReason reason, |
| 157 const FilePath& path) { | 170 const FilePath& path) { |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 159 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); | 172 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); |
| 160 } | 173 } |
| 161 | 174 |
| 162 // static | 175 // static |
| 163 void DownloadFileWithDelay::DetachCallbackWrapper( | 176 void DownloadFileWithDelay::DetachCallbackWrapper( |
| 164 DownloadFileWithDelayFactory* factory, | 177 DownloadFileWithDelayFactory* factory, |
| 165 const base::Closure& original_callback) { | 178 const base::Closure& original_callback) { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 167 factory->AddDetachCallback(original_callback); | 180 factory->AddDetachCallback(original_callback); |
| 168 } | 181 } |
| 169 | 182 |
| 170 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() | 183 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() |
| 171 : waiting_(false) {} | 184 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 185 waiting_(false) {} |
| 172 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} | 186 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} |
| 173 | 187 |
| 174 DownloadFile* DownloadFileWithDelayFactory::CreateFile( | 188 DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
| 175 DownloadCreateInfo* info, | 189 const content::DownloadSaveInfo& save_info, |
| 190 const FilePath& default_download_directory, |
| 191 GURL url, |
| 192 GURL referrer_url, |
| 193 int64 received_bytes, |
| 194 bool calculate_hash, |
| 176 scoped_ptr<content::ByteStreamReader> stream, | 195 scoped_ptr<content::ByteStreamReader> stream, |
| 177 DownloadManager* download_manager, | 196 const net::BoundNetLog& bound_net_log, |
| 178 bool calculate_hash, | 197 base::WeakPtr<content::DownloadDestinationObserver> observer) { |
| 179 const net::BoundNetLog& bound_net_log) { | 198 scoped_ptr<content::PowerSaveBlocker> psb( |
| 180 | 199 new content::PowerSaveBlocker( |
| 200 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 201 "Download in progress")); |
| 181 return new DownloadFileWithDelay( | 202 return new DownloadFileWithDelay( |
| 182 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), | 203 save_info, default_download_directory, url, referrer_url, |
| 183 download_manager, calculate_hash, | 204 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 184 scoped_ptr<content::PowerSaveBlocker>( | 205 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); |
| 185 new content::PowerSaveBlocker( | |
| 186 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
| 187 "Download in progress")).Pass(), | |
| 188 bound_net_log, this); | |
| 189 } | 206 } |
| 190 | 207 |
| 191 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { | 208 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 193 rename_callbacks_.push_back(callback); | 210 rename_callbacks_.push_back(callback); |
| 194 if (waiting_) | 211 if (waiting_) |
| 195 MessageLoopForUI::current()->Quit(); | 212 MessageLoopForUI::current()->Quit(); |
| 196 } | 213 } |
| 197 | 214 |
| 198 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { | 215 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 return false; | 318 return false; |
| 302 | 319 |
| 303 // Check the contents. | 320 // Check the contents. |
| 304 EXPECT_EQ(value, file_contents); | 321 EXPECT_EQ(value, file_contents); |
| 305 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 322 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
| 306 return false; | 323 return false; |
| 307 | 324 |
| 308 return true; | 325 return true; |
| 309 } | 326 } |
| 310 | 327 |
| 311 DownloadFileManager* GetDownloadFileManager() { | |
| 312 ResourceDispatcherHostImpl* rdh(ResourceDispatcherHostImpl::Get()); | |
| 313 return rdh->download_file_manager(); | |
| 314 } | |
| 315 | |
| 316 private: | 328 private: |
| 317 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { | 329 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 318 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) | 330 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) |
| 319 *result = false; | 331 *result = false; |
| 320 BrowserThread::PostTask( | 332 BrowserThread::PostTask( |
| 321 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 333 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 322 } | 334 } |
| 323 | 335 |
| 324 // Location of the downloads directory for these tests | 336 // Location of the downloads directory for these tests |
| 325 ScopedTempDir downloads_directory_; | 337 ScopedTempDir downloads_directory_; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 ASSERT_TRUE(file_util::ContentsEqual( | 430 ASSERT_TRUE(file_util::ContentsEqual( |
| 419 file2, GetTestFilePath("download", "download-test.lib"))); | 431 file2, GetTestFilePath("download", "download-test.lib"))); |
| 420 } | 432 } |
| 421 | 433 |
| 422 // Try to cancel just before we release the download file, by delaying final | 434 // Try to cancel just before we release the download file, by delaying final |
| 423 // rename callback. | 435 // rename callback. |
| 424 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { | 436 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { |
| 425 // Setup new factory. | 437 // Setup new factory. |
| 426 DownloadFileWithDelayFactory* file_factory = | 438 DownloadFileWithDelayFactory* file_factory = |
| 427 new DownloadFileWithDelayFactory(); | 439 new DownloadFileWithDelayFactory(); |
| 428 GetDownloadFileManager()->SetFileFactoryForTesting( | 440 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
| 441 download_manager->SetDownloadFileFactoryForTesting( |
| 429 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 442 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); |
| 430 | 443 |
| 431 // Create a download | 444 // Create a download |
| 432 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 445 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 433 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 446 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 434 | 447 |
| 435 // Wait until the first (intermediate file) rename and execute the callback. | 448 // Wait until the first (intermediate file) rename and execute the callback. |
| 436 file_factory->WaitForSomeCallback(); | 449 file_factory->WaitForSomeCallback(); |
| 437 std::vector<base::Closure> callbacks; | 450 std::vector<base::Closure> callbacks; |
| 438 file_factory->GetAllDetachCallbacks(&callbacks); | 451 file_factory->GetAllDetachCallbacks(&callbacks); |
| 439 ASSERT_TRUE(callbacks.empty()); | 452 ASSERT_TRUE(callbacks.empty()); |
| 440 file_factory->GetAllRenameCallbacks(&callbacks); | 453 file_factory->GetAllRenameCallbacks(&callbacks); |
| 441 ASSERT_EQ(1u, callbacks.size()); | 454 ASSERT_EQ(1u, callbacks.size()); |
| 442 callbacks[0].Run(); | 455 callbacks[0].Run(); |
| 443 callbacks.clear(); | 456 callbacks.clear(); |
| 444 | 457 |
| 445 // Wait until the second (final) rename callback is posted. | 458 // Wait until the second (final) rename callback is posted. |
| 446 file_factory->WaitForSomeCallback(); | 459 file_factory->WaitForSomeCallback(); |
| 447 file_factory->GetAllDetachCallbacks(&callbacks); | 460 file_factory->GetAllDetachCallbacks(&callbacks); |
| 448 ASSERT_TRUE(callbacks.empty()); | 461 ASSERT_TRUE(callbacks.empty()); |
| 449 file_factory->GetAllRenameCallbacks(&callbacks); | 462 file_factory->GetAllRenameCallbacks(&callbacks); |
| 450 ASSERT_EQ(1u, callbacks.size()); | 463 ASSERT_EQ(1u, callbacks.size()); |
| 451 | 464 |
| 452 // Cancel it. | 465 // Cancel it. |
| 453 std::vector<DownloadItem*> items; | 466 std::vector<DownloadItem*> items; |
| 454 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 467 download_manager->GetAllDownloads(&items); |
| 455 ASSERT_EQ(1u, items.size()); | 468 ASSERT_EQ(1u, items.size()); |
| 456 items[0]->Cancel(true); | 469 items[0]->Cancel(true); |
| 457 RunAllPendingInMessageLoop(); | 470 RunAllPendingInMessageLoop(); |
| 458 | 471 |
| 459 // Check state. | 472 // Check state. |
| 460 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 473 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
| 461 | 474 |
| 462 // Run final rename callback. | 475 // Run final rename callback. |
| 463 callbacks[0].Run(); | 476 callbacks[0].Run(); |
| 464 callbacks.clear(); | 477 callbacks.clear(); |
| 465 | 478 |
| 466 // Check state. | 479 // Check state. |
| 467 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 480 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
| 468 } | 481 } |
| 469 | 482 |
| 470 // Try to cancel just after we release the download file, by delaying | 483 // Try to cancel just after we release the download file, by delaying |
| 471 // release. | 484 // release. |
| 472 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { | 485 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { |
| 473 // Setup new factory. | 486 // Setup new factory. |
| 474 // Setup new factory. | |
| 475 DownloadFileWithDelayFactory* file_factory = | 487 DownloadFileWithDelayFactory* file_factory = |
| 476 new DownloadFileWithDelayFactory(); | 488 new DownloadFileWithDelayFactory(); |
| 477 GetDownloadFileManager()->SetFileFactoryForTesting( | 489 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
| 490 download_manager->SetDownloadFileFactoryForTesting( |
| 478 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 491 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); |
| 479 | 492 |
| 480 // Create a download | 493 // Create a download |
| 481 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 494 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 482 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 495 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 483 | 496 |
| 484 // Wait until the first (intermediate file) rename and execute the callback. | 497 // Wait until the first (intermediate file) rename and execute the callback. |
| 485 file_factory->WaitForSomeCallback(); | 498 file_factory->WaitForSomeCallback(); |
| 486 std::vector<base::Closure> callbacks; | 499 std::vector<base::Closure> callbacks; |
| 487 file_factory->GetAllDetachCallbacks(&callbacks); | 500 file_factory->GetAllDetachCallbacks(&callbacks); |
| 488 ASSERT_TRUE(callbacks.empty()); | 501 ASSERT_TRUE(callbacks.empty()); |
| 489 file_factory->GetAllRenameCallbacks(&callbacks); | 502 file_factory->GetAllRenameCallbacks(&callbacks); |
| 490 ASSERT_EQ(1u, callbacks.size()); | 503 ASSERT_EQ(1u, callbacks.size()); |
| 491 callbacks[0].Run(); | 504 callbacks[0].Run(); |
| 492 callbacks.clear(); | 505 callbacks.clear(); |
| 493 | 506 |
| 494 // Wait until the second (final) rename callback is posted. | 507 // Wait until the second (final) rename callback is posted. |
| 495 file_factory->WaitForSomeCallback(); | 508 file_factory->WaitForSomeCallback(); |
| 496 file_factory->GetAllDetachCallbacks(&callbacks); | 509 file_factory->GetAllDetachCallbacks(&callbacks); |
| 497 ASSERT_TRUE(callbacks.empty()); | 510 ASSERT_TRUE(callbacks.empty()); |
| 498 file_factory->GetAllRenameCallbacks(&callbacks); | 511 file_factory->GetAllRenameCallbacks(&callbacks); |
| 499 ASSERT_EQ(1u, callbacks.size()); | 512 ASSERT_EQ(1u, callbacks.size()); |
| 500 | 513 |
| 501 // Call it. | 514 // Call it. |
| 502 callbacks[0].Run(); | 515 callbacks[0].Run(); |
| 503 callbacks.clear(); | 516 callbacks.clear(); |
| 504 | 517 |
| 505 // Confirm download isn't complete yet. | 518 // Confirm download still IN_PROGRESS. |
| 506 std::vector<DownloadItem*> items; | 519 std::vector<DownloadItem*> items; |
| 507 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 520 download_manager->GetAllDownloads(&items); |
| 508 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 521 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 509 | 522 |
| 510 // Cancel the download; confirm cancel fails anyway. | 523 // Cancel the download; confirm cancel fails. |
| 511 ASSERT_EQ(1u, items.size()); | 524 ASSERT_EQ(1u, items.size()); |
| 512 items[0]->Cancel(true); | 525 items[0]->Cancel(true); |
| 513 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 526 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 514 RunAllPendingInMessageLoop(); | |
| 515 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | |
| 516 | 527 |
| 517 // Confirm detach callback and run it. | 528 // Confirm detach callback and run it. |
| 518 file_factory->WaitForSomeCallback(); | 529 file_factory->WaitForSomeCallback(); |
| 519 file_factory->GetAllRenameCallbacks(&callbacks); | 530 file_factory->GetAllRenameCallbacks(&callbacks); |
| 520 ASSERT_TRUE(callbacks.empty()); | 531 ASSERT_TRUE(callbacks.empty()); |
| 521 file_factory->GetAllDetachCallbacks(&callbacks); | 532 file_factory->GetAllDetachCallbacks(&callbacks); |
| 522 ASSERT_EQ(1u, callbacks.size()); | 533 ASSERT_EQ(1u, callbacks.size()); |
| 523 callbacks[0].Run(); | 534 callbacks[0].Run(); |
| 524 callbacks.clear(); | 535 callbacks.clear(); |
| 536 |
| 537 // *Now* the download should be complete. |
| 525 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 538 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
| 526 } | 539 } |
| 527 | 540 |
| 528 } // namespace content | 541 } // namespace content |
| OLD | NEW |