| 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" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 const GURL& referrer_url, | 79 const GURL& referrer_url, |
| 80 bool calculate_hash, | 80 bool calculate_hash, |
| 81 scoped_ptr<ByteStreamReader> stream, | 81 scoped_ptr<ByteStreamReader> stream, |
| 82 const net::BoundNetLog& bound_net_log, | 82 const net::BoundNetLog& bound_net_log, |
| 83 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 83 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 84 base::WeakPtr<DownloadDestinationObserver> observer, | 84 base::WeakPtr<DownloadDestinationObserver> observer, |
| 85 base::WeakPtr<DownloadFileWithDelayFactory> owner); | 85 base::WeakPtr<DownloadFileWithDelayFactory> owner); |
| 86 | 86 |
| 87 virtual ~DownloadFileWithDelay(); | 87 virtual ~DownloadFileWithDelay(); |
| 88 | 88 |
| 89 // Wraps DownloadFileImpl::Rename and intercepts the return callback, | 89 // Wraps DownloadFileImpl::Rename* and intercepts the return callback, |
| 90 // storing it in the factory that produced this object for later | 90 // storing it in the factory that produced this object for later |
| 91 // retrieval. | 91 // retrieval. |
| 92 virtual void Rename(const FilePath& full_path, | 92 virtual void RenameAndUniquify( |
| 93 bool overwrite_existing_file, | 93 const FilePath& full_path, |
| 94 const RenameCompletionCallback& callback) OVERRIDE; | 94 const RenameCompletionCallback& callback) OVERRIDE; |
| 95 | 95 virtual void RenameAndAnnotate( |
| 96 // Wraps DownloadFileImpl::Detach and intercepts the return callback, | 96 const FilePath& full_path, |
| 97 // storing it in the factory that produced this object for later | 97 const RenameCompletionCallback& callback) OVERRIDE; |
| 98 // retrieval. | |
| 99 virtual void Detach(const DetachCompletionCallback& callback) OVERRIDE; | |
| 100 | 98 |
| 101 private: | 99 private: |
| 102 static void RenameCallbackWrapper( | 100 static void RenameCallbackWrapper( |
| 103 DownloadFileWithDelayFactory* factory, | 101 DownloadFileWithDelayFactory* factory, |
| 104 const RenameCompletionCallback& original_callback, | 102 const RenameCompletionCallback& original_callback, |
| 105 DownloadInterruptReason reason, | 103 DownloadInterruptReason reason, |
| 106 const FilePath& path); | 104 const FilePath& path); |
| 107 | 105 |
| 108 static void DetachCallbackWrapper( | |
| 109 DownloadFileWithDelayFactory* factory, | |
| 110 const DetachCompletionCallback& original_callback, | |
| 111 DownloadInterruptReason interrupt_reason); | |
| 112 | |
| 113 // This variable may only be read on the FILE thread, and may only be | 106 // This variable may only be read on the FILE thread, and may only be |
| 114 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) | 107 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) |
| 115 // on the UI thread. This is because after construction, | 108 // on the UI thread. This is because after construction, |
| 116 // DownloadFileWithDelay lives on the file thread, but | 109 // DownloadFileWithDelay lives on the file thread, but |
| 117 // DownloadFileWithDelayFactory is purely a UI thread object. | 110 // DownloadFileWithDelayFactory is purely a UI thread object. |
| 118 base::WeakPtr<DownloadFileWithDelayFactory> owner_; | 111 base::WeakPtr<DownloadFileWithDelayFactory> owner_; |
| 119 | 112 |
| 120 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); | 113 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); |
| 121 }; | 114 }; |
| 122 | 115 |
| 123 // All routines on this class must be called on the UI thread. | 116 // All routines on this class must be called on the UI thread. |
| 124 class DownloadFileWithDelayFactory : public DownloadFileFactory { | 117 class DownloadFileWithDelayFactory : public DownloadFileFactory { |
| 125 public: | 118 public: |
| 126 DownloadFileWithDelayFactory(); | 119 DownloadFileWithDelayFactory(); |
| 127 virtual ~DownloadFileWithDelayFactory(); | 120 virtual ~DownloadFileWithDelayFactory(); |
| 128 | 121 |
| 129 // DownloadFileFactory interface. | 122 // DownloadFileFactory interface. |
| 130 virtual DownloadFile* CreateFile( | 123 virtual DownloadFile* CreateFile( |
| 131 scoped_ptr<DownloadSaveInfo> save_info, | 124 scoped_ptr<DownloadSaveInfo> save_info, |
| 132 const FilePath& default_download_directory, | 125 const FilePath& default_download_directory, |
| 133 const GURL& url, | 126 const GURL& url, |
| 134 const GURL& referrer_url, | 127 const GURL& referrer_url, |
| 135 bool calculate_hash, | 128 bool calculate_hash, |
| 136 scoped_ptr<ByteStreamReader> stream, | 129 scoped_ptr<ByteStreamReader> stream, |
| 137 const net::BoundNetLog& bound_net_log, | 130 const net::BoundNetLog& bound_net_log, |
| 138 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; | 131 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; |
| 139 | 132 |
| 140 void AddRenameCallback(base::Closure callback); | 133 void AddRenameCallback(base::Closure callback); |
| 141 void AddDetachCallback(base::Closure callback); | |
| 142 void GetAllRenameCallbacks(std::vector<base::Closure>* results); | 134 void GetAllRenameCallbacks(std::vector<base::Closure>* results); |
| 143 void GetAllDetachCallbacks(std::vector<base::Closure>* results); | |
| 144 | 135 |
| 145 // Do not return until either GetAllRenameCallbacks() or | 136 // Do not return until GetAllRenameCallbacks() will return a non-empty list. |
| 146 // GetAllDetachCallbacks() will return a non-empty list. | |
| 147 void WaitForSomeCallback(); | 137 void WaitForSomeCallback(); |
| 148 | 138 |
| 149 private: | 139 private: |
| 150 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; | 140 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; |
| 151 std::vector<base::Closure> rename_callbacks_; | 141 std::vector<base::Closure> rename_callbacks_; |
| 152 std::vector<base::Closure> detach_callbacks_; | |
| 153 bool waiting_; | 142 bool waiting_; |
| 154 | 143 |
| 155 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); | 144 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); |
| 156 }; | 145 }; |
| 157 | 146 |
| 158 DownloadFileWithDelay::DownloadFileWithDelay( | 147 DownloadFileWithDelay::DownloadFileWithDelay( |
| 159 scoped_ptr<DownloadSaveInfo> save_info, | 148 scoped_ptr<DownloadSaveInfo> save_info, |
| 160 const FilePath& default_download_directory, | 149 const FilePath& default_download_directory, |
| 161 const GURL& url, | 150 const GURL& url, |
| 162 const GURL& referrer_url, | 151 const GURL& referrer_url, |
| 163 bool calculate_hash, | 152 bool calculate_hash, |
| 164 scoped_ptr<ByteStreamReader> stream, | 153 scoped_ptr<ByteStreamReader> stream, |
| 165 const net::BoundNetLog& bound_net_log, | 154 const net::BoundNetLog& bound_net_log, |
| 166 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 155 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 167 base::WeakPtr<DownloadDestinationObserver> observer, | 156 base::WeakPtr<DownloadDestinationObserver> observer, |
| 168 base::WeakPtr<DownloadFileWithDelayFactory> owner) | 157 base::WeakPtr<DownloadFileWithDelayFactory> owner) |
| 169 : DownloadFileImpl( | 158 : DownloadFileImpl( |
| 170 save_info.Pass(), default_download_directory, url, referrer_url, | 159 save_info.Pass(), default_download_directory, url, referrer_url, |
| 171 calculate_hash, stream.Pass(), bound_net_log, | 160 calculate_hash, stream.Pass(), bound_net_log, |
| 172 power_save_blocker.Pass(), observer), | 161 power_save_blocker.Pass(), observer), |
| 173 owner_(owner) {} | 162 owner_(owner) {} |
| 174 | 163 |
| 175 DownloadFileWithDelay::~DownloadFileWithDelay() {} | 164 DownloadFileWithDelay::~DownloadFileWithDelay() {} |
| 176 | 165 |
| 177 void DownloadFileWithDelay::Rename(const FilePath& full_path, | 166 void DownloadFileWithDelay::RenameAndUniquify( |
| 178 bool overwrite_existing_file, | 167 const FilePath& full_path, const RenameCompletionCallback& callback) { |
| 179 const RenameCompletionCallback& callback) { | |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 181 DownloadFileImpl::Rename( | 169 DownloadFileImpl::RenameAndUniquify( |
| 182 full_path, overwrite_existing_file, | 170 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, |
| 183 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, | 171 owner_, callback)); |
| 184 owner_, callback)); | |
| 185 } | 172 } |
| 186 | 173 |
| 187 void DownloadFileWithDelay::Detach(const DetachCompletionCallback& callback) { | 174 void DownloadFileWithDelay::RenameAndAnnotate( |
| 175 const FilePath& full_path, const RenameCompletionCallback& callback) { |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 189 DownloadFileImpl::Detach( | 177 DownloadFileImpl::RenameAndAnnotate( |
| 190 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, | 178 full_path, base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, |
| 191 owner_, callback)); | 179 owner_, callback)); |
| 192 } | 180 } |
| 193 | 181 |
| 194 // static | 182 // static |
| 195 void DownloadFileWithDelay::RenameCallbackWrapper( | 183 void DownloadFileWithDelay::RenameCallbackWrapper( |
| 196 DownloadFileWithDelayFactory* factory, | 184 DownloadFileWithDelayFactory* factory, |
| 197 const RenameCompletionCallback& original_callback, | 185 const RenameCompletionCallback& original_callback, |
| 198 DownloadInterruptReason reason, | 186 DownloadInterruptReason reason, |
| 199 const FilePath& path) { | 187 const FilePath& path) { |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 201 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); | 189 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); |
| 202 } | 190 } |
| 203 | 191 |
| 204 // static | |
| 205 void DownloadFileWithDelay::DetachCallbackWrapper( | |
| 206 DownloadFileWithDelayFactory* factory, | |
| 207 const DetachCompletionCallback& original_callback, | |
| 208 DownloadInterruptReason interrupt_reason) { | |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 210 factory->AddDetachCallback(base::Bind(original_callback, interrupt_reason)); | |
| 211 } | |
| 212 | |
| 213 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() | 192 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() |
| 214 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 193 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 215 waiting_(false) {} | 194 waiting_(false) {} |
| 216 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} | 195 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} |
| 217 | 196 |
| 218 DownloadFile* DownloadFileWithDelayFactory::CreateFile( | 197 DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
| 219 scoped_ptr<DownloadSaveInfo> save_info, | 198 scoped_ptr<DownloadSaveInfo> save_info, |
| 220 const FilePath& default_download_directory, | 199 const FilePath& default_download_directory, |
| 221 const GURL& url, | 200 const GURL& url, |
| 222 const GURL& referrer_url, | 201 const GURL& referrer_url, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 234 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); | 213 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); |
| 235 } | 214 } |
| 236 | 215 |
| 237 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { | 216 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 239 rename_callbacks_.push_back(callback); | 218 rename_callbacks_.push_back(callback); |
| 240 if (waiting_) | 219 if (waiting_) |
| 241 MessageLoopForUI::current()->Quit(); | 220 MessageLoopForUI::current()->Quit(); |
| 242 } | 221 } |
| 243 | 222 |
| 244 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { | |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 246 detach_callbacks_.push_back(callback); | |
| 247 if (waiting_) | |
| 248 MessageLoopForUI::current()->Quit(); | |
| 249 } | |
| 250 | |
| 251 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( | 223 void DownloadFileWithDelayFactory::GetAllRenameCallbacks( |
| 252 std::vector<base::Closure>* results) { | 224 std::vector<base::Closure>* results) { |
| 253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 254 results->swap(rename_callbacks_); | 226 results->swap(rename_callbacks_); |
| 255 } | 227 } |
| 256 | 228 |
| 257 void DownloadFileWithDelayFactory::GetAllDetachCallbacks( | |
| 258 std::vector<base::Closure>* results) { | |
| 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 260 results->swap(detach_callbacks_); | |
| 261 } | |
| 262 | |
| 263 void DownloadFileWithDelayFactory::WaitForSomeCallback() { | 229 void DownloadFileWithDelayFactory::WaitForSomeCallback() { |
| 264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 265 | 231 |
| 266 if (rename_callbacks_.empty() && detach_callbacks_.empty()) { | 232 if (rename_callbacks_.empty()) { |
| 267 waiting_ = true; | 233 waiting_ = true; |
| 268 RunMessageLoop(); | 234 RunMessageLoop(); |
| 269 waiting_ = false; | 235 waiting_ = false; |
| 270 } | 236 } |
| 271 } | 237 } |
| 272 | 238 |
| 273 bool WasPersisted(DownloadItem* item) { | 239 bool WasPersisted(DownloadItem* item) { |
| 274 return item->IsPersisted(); | 240 return item->IsPersisted(); |
| 275 } | 241 } |
| 276 | 242 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 new PowerSaveBlocker( | 310 new PowerSaveBlocker( |
| 345 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 311 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 346 "Download in progress")); | 312 "Download in progress")); |
| 347 return new CountingDownloadFile( | 313 return new CountingDownloadFile( |
| 348 save_info.Pass(), default_downloads_directory, url, referrer_url, | 314 save_info.Pass(), default_downloads_directory, url, referrer_url, |
| 349 calculate_hash, stream.Pass(), bound_net_log, | 315 calculate_hash, stream.Pass(), bound_net_log, |
| 350 psb.Pass(), observer); | 316 psb.Pass(), observer); |
| 351 } | 317 } |
| 352 }; | 318 }; |
| 353 | 319 |
| 320 class TestShellDownloadManagerDelegate : public ShellDownloadManagerDelegate { |
| 321 public: |
| 322 TestShellDownloadManagerDelegate() |
| 323 : delay_download_open_(false) {} |
| 324 |
| 325 virtual bool ShouldOpenDownload(DownloadItem* item) OVERRIDE { |
| 326 if (delay_download_open_) { |
| 327 delayed_ids_.push_back(item->GetId()); |
| 328 return false; |
| 329 } |
| 330 return true; |
| 331 } |
| 332 |
| 333 void SetDelayedOpen(bool delay) { |
| 334 delay_download_open_ = delay; |
| 335 } |
| 336 |
| 337 void GetDelayedIds(std::vector<int32>* ids) { |
| 338 ids->swap(delayed_ids_); |
| 339 } |
| 340 private: |
| 341 virtual ~TestShellDownloadManagerDelegate() {} |
| 342 |
| 343 bool delay_download_open_; |
| 344 std::vector<int32> delayed_ids_; |
| 345 }; |
| 346 |
| 354 } // namespace | 347 } // namespace |
| 355 | 348 |
| 356 class DownloadContentTest : public ContentBrowserTest { | 349 class DownloadContentTest : public ContentBrowserTest { |
| 357 protected: | 350 protected: |
| 358 virtual void SetUpOnMainThread() OVERRIDE { | 351 virtual void SetUpOnMainThread() OVERRIDE { |
| 359 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 352 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
| 360 | 353 |
| 361 ShellDownloadManagerDelegate* delegate = | 354 TestShellDownloadManagerDelegate* delegate = |
| 362 static_cast<ShellDownloadManagerDelegate*>( | 355 new TestShellDownloadManagerDelegate(); |
| 363 shell()->web_contents()->GetBrowserContext() | |
| 364 ->GetDownloadManagerDelegate()); | |
| 365 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path()); | 356 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path()); |
| 357 DownloadManager* manager = DownloadManagerForShell(shell()); |
| 358 manager->SetDelegate(delegate); |
| 359 delegate->SetDownloadManager(manager); |
| 366 | 360 |
| 367 BrowserThread::PostTask( | 361 BrowserThread::PostTask( |
| 368 BrowserThread::IO, FROM_HERE, | 362 BrowserThread::IO, FROM_HERE, |
| 369 base::Bind(&URLRequestSlowDownloadJob::AddUrlHandler)); | 363 base::Bind(&URLRequestSlowDownloadJob::AddUrlHandler)); |
| 370 FilePath mock_base(GetTestFilePath("download", "")); | 364 FilePath mock_base(GetTestFilePath("download", "")); |
| 371 BrowserThread::PostTask( | 365 BrowserThread::PostTask( |
| 372 BrowserThread::IO, FROM_HERE, | 366 BrowserThread::IO, FROM_HERE, |
| 373 base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, mock_base)); | 367 base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, mock_base)); |
| 374 } | 368 } |
| 375 | 369 |
| 370 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate( |
| 371 DownloadManager* manager) { |
| 372 return static_cast<TestShellDownloadManagerDelegate*>( |
| 373 manager->GetDelegate()); |
| 374 } |
| 375 |
| 376 // Create a DownloadTestObserverTerminal that will wait for the | 376 // Create a DownloadTestObserverTerminal that will wait for the |
| 377 // specified number of downloads to finish. | 377 // specified number of downloads to finish. |
| 378 DownloadTestObserver* CreateWaiter( | 378 DownloadTestObserver* CreateWaiter( |
| 379 Shell* shell, int num_downloads) { | 379 Shell* shell, int num_downloads) { |
| 380 DownloadManager* download_manager = DownloadManagerForShell(shell); | 380 DownloadManager* download_manager = DownloadManagerForShell(shell); |
| 381 return new DownloadTestObserverTerminal(download_manager, num_downloads, | 381 return new DownloadTestObserverTerminal(download_manager, num_downloads, |
| 382 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); | 382 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
| 383 } | 383 } |
| 384 | 384 |
| 385 // Create a DownloadTestObserverInProgress that will wait for the | 385 // Create a DownloadTestObserverInProgress that will wait for the |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 download_manager->SetDownloadFileFactoryForTesting( | 563 download_manager->SetDownloadFileFactoryForTesting( |
| 564 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); | 564 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
| 565 | 565 |
| 566 // Create a download | 566 // Create a download |
| 567 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 567 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 568 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 568 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 569 | 569 |
| 570 // Wait until the first (intermediate file) rename and execute the callback. | 570 // Wait until the first (intermediate file) rename and execute the callback. |
| 571 file_factory->WaitForSomeCallback(); | 571 file_factory->WaitForSomeCallback(); |
| 572 std::vector<base::Closure> callbacks; | 572 std::vector<base::Closure> callbacks; |
| 573 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 574 ASSERT_TRUE(callbacks.empty()); | |
| 575 file_factory->GetAllRenameCallbacks(&callbacks); | 573 file_factory->GetAllRenameCallbacks(&callbacks); |
| 576 ASSERT_EQ(1u, callbacks.size()); | 574 ASSERT_EQ(1u, callbacks.size()); |
| 577 callbacks[0].Run(); | 575 callbacks[0].Run(); |
| 578 callbacks.clear(); | 576 callbacks.clear(); |
| 579 | 577 |
| 580 // Wait until the second (final) rename callback is posted. | 578 // Wait until the second (final) rename callback is posted. |
| 581 file_factory->WaitForSomeCallback(); | 579 file_factory->WaitForSomeCallback(); |
| 582 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 583 ASSERT_TRUE(callbacks.empty()); | |
| 584 file_factory->GetAllRenameCallbacks(&callbacks); | 580 file_factory->GetAllRenameCallbacks(&callbacks); |
| 585 ASSERT_EQ(1u, callbacks.size()); | 581 ASSERT_EQ(1u, callbacks.size()); |
| 586 | 582 |
| 587 // Cancel it. | 583 // Cancel it. |
| 588 std::vector<DownloadItem*> items; | 584 std::vector<DownloadItem*> items; |
| 589 download_manager->GetAllDownloads(&items); | 585 download_manager->GetAllDownloads(&items); |
| 590 ASSERT_EQ(1u, items.size()); | 586 ASSERT_EQ(1u, items.size()); |
| 591 items[0]->Cancel(true); | 587 items[0]->Cancel(true); |
| 592 RunAllPendingInMessageLoop(); | 588 RunAllPendingInMessageLoop(); |
| 593 | 589 |
| 594 // Check state. | 590 // Check state. |
| 595 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 591 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
| 596 | 592 |
| 597 // Run final rename callback. | 593 // Run final rename callback. |
| 598 callbacks[0].Run(); | 594 callbacks[0].Run(); |
| 599 callbacks.clear(); | 595 callbacks.clear(); |
| 600 | 596 |
| 601 // Check state. | 597 // Check state. |
| 602 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 598 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
| 603 } | 599 } |
| 604 | 600 |
| 605 // Try to cancel just after we release the download file, by delaying | 601 // Try to cancel just after we release the download file, by delaying |
| 606 // release. | 602 // in ShouldOpenDownload. |
| 607 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { | 603 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { |
| 604 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
| 605 |
| 606 // Mark delegate for delayed open. |
| 607 GetDownloadManagerDelegate(download_manager)->SetDelayedOpen(true); |
| 608 |
| 608 // Setup new factory. | 609 // Setup new factory. |
| 609 DownloadFileWithDelayFactory* file_factory = | 610 DownloadFileWithDelayFactory* file_factory = |
| 610 new DownloadFileWithDelayFactory(); | 611 new DownloadFileWithDelayFactory(); |
| 611 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); | |
| 612 download_manager->SetDownloadFileFactoryForTesting( | 612 download_manager->SetDownloadFileFactoryForTesting( |
| 613 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); | 613 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
| 614 | 614 |
| 615 // Create a download | 615 // Create a download |
| 616 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 616 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 617 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 617 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 618 | 618 |
| 619 // Wait until the first (intermediate file) rename and execute the callback. | 619 // Wait until the first (intermediate file) rename and execute the callback. |
| 620 file_factory->WaitForSomeCallback(); | 620 file_factory->WaitForSomeCallback(); |
| 621 std::vector<base::Closure> callbacks; | 621 std::vector<base::Closure> callbacks; |
| 622 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 623 ASSERT_TRUE(callbacks.empty()); | |
| 624 file_factory->GetAllRenameCallbacks(&callbacks); | 622 file_factory->GetAllRenameCallbacks(&callbacks); |
| 625 ASSERT_EQ(1u, callbacks.size()); | 623 ASSERT_EQ(1u, callbacks.size()); |
| 626 callbacks[0].Run(); | 624 callbacks[0].Run(); |
| 627 callbacks.clear(); | 625 callbacks.clear(); |
| 628 | 626 |
| 629 // Wait until the second (final) rename callback is posted. | 627 // Wait until the second (final) rename callback is posted. |
| 630 file_factory->WaitForSomeCallback(); | 628 file_factory->WaitForSomeCallback(); |
| 631 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 632 ASSERT_TRUE(callbacks.empty()); | |
| 633 file_factory->GetAllRenameCallbacks(&callbacks); | 629 file_factory->GetAllRenameCallbacks(&callbacks); |
| 634 ASSERT_EQ(1u, callbacks.size()); | 630 ASSERT_EQ(1u, callbacks.size()); |
| 635 | 631 |
| 636 // Call it. | 632 // Call it. |
| 637 callbacks[0].Run(); | 633 callbacks[0].Run(); |
| 638 callbacks.clear(); | 634 callbacks.clear(); |
| 639 | 635 |
| 640 // Confirm download still IN_PROGRESS. | 636 // Confirm download still IN_PROGRESS (internal state COMPLETING). |
| 641 std::vector<DownloadItem*> items; | 637 std::vector<DownloadItem*> items; |
| 642 download_manager->GetAllDownloads(&items); | 638 download_manager->GetAllDownloads(&items); |
| 643 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 639 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 644 | 640 |
| 645 // Cancel the download; confirm cancel fails. | 641 // Cancel the download; confirm cancel fails. |
| 646 ASSERT_EQ(1u, items.size()); | 642 ASSERT_EQ(1u, items.size()); |
| 647 items[0]->Cancel(true); | 643 items[0]->Cancel(true); |
| 648 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 644 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 649 | 645 |
| 650 // Confirm detach callback and run it. | 646 // Need to complete open test. |
| 651 file_factory->WaitForSomeCallback(); | 647 std::vector<int32> delayed_ids; |
| 652 file_factory->GetAllRenameCallbacks(&callbacks); | 648 GetDownloadManagerDelegate(download_manager)->GetDelayedIds(&delayed_ids); |
| 653 ASSERT_TRUE(callbacks.empty()); | 649 ASSERT_EQ(1u, delayed_ids.size()); |
| 654 file_factory->GetAllDetachCallbacks(&callbacks); | 650 ASSERT_EQ(items[0]->GetId(), delayed_ids[0]); |
| 655 ASSERT_EQ(1u, callbacks.size()); | 651 items[0]->DelayedDownloadOpened(false); |
| 656 callbacks[0].Run(); | |
| 657 callbacks.clear(); | |
| 658 | 652 |
| 659 // *Now* the download should be complete. | 653 // *Now* the download should be complete. |
| 660 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 654 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
| 661 } | 655 } |
| 662 | 656 |
| 663 // Try to shutdown with a download in progress to make sure shutdown path | 657 // Try to shutdown with a download in progress to make sure shutdown path |
| 664 // works properly. | 658 // works properly. |
| 665 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { | 659 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { |
| 666 // Create a download that won't complete. | 660 // Create a download that won't complete. |
| 667 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); | 661 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 EXPECT_CALL(item_observer, OnDownloadDestroyed(items[0])) | 693 EXPECT_CALL(item_observer, OnDownloadDestroyed(items[0])) |
| 700 .WillOnce(Return()); | 694 .WillOnce(Return()); |
| 701 } | 695 } |
| 702 DownloadManagerForShell(shell())->Shutdown(); | 696 DownloadManagerForShell(shell())->Shutdown(); |
| 703 items.clear(); | 697 items.clear(); |
| 704 } | 698 } |
| 705 | 699 |
| 706 // Try to shutdown just after we release the download file, by delaying | 700 // Try to shutdown just after we release the download file, by delaying |
| 707 // release. | 701 // release. |
| 708 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { | 702 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { |
| 703 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
| 704 |
| 705 // Mark delegate for delayed open. |
| 706 GetDownloadManagerDelegate(download_manager)->SetDelayedOpen(true); |
| 707 |
| 709 // Setup new factory. | 708 // Setup new factory. |
| 710 DownloadFileWithDelayFactory* file_factory = | 709 DownloadFileWithDelayFactory* file_factory = |
| 711 new DownloadFileWithDelayFactory(); | 710 new DownloadFileWithDelayFactory(); |
| 712 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( | 711 download_manager->SetDownloadFileFactoryForTesting( |
| 713 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); | 712 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
| 714 | 713 |
| 715 // Create a download | 714 // Create a download |
| 716 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 715 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 717 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 716 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
| 718 | 717 |
| 719 // Wait until the first (intermediate file) rename and execute the callback. | 718 // Wait until the first (intermediate file) rename and execute the callback. |
| 720 file_factory->WaitForSomeCallback(); | 719 file_factory->WaitForSomeCallback(); |
| 721 std::vector<base::Closure> callbacks; | 720 std::vector<base::Closure> callbacks; |
| 722 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 723 ASSERT_TRUE(callbacks.empty()); | |
| 724 file_factory->GetAllRenameCallbacks(&callbacks); | 721 file_factory->GetAllRenameCallbacks(&callbacks); |
| 725 ASSERT_EQ(1u, callbacks.size()); | 722 ASSERT_EQ(1u, callbacks.size()); |
| 726 callbacks[0].Run(); | 723 callbacks[0].Run(); |
| 727 callbacks.clear(); | 724 callbacks.clear(); |
| 728 | 725 |
| 729 // Wait until the second (final) rename callback is posted. | 726 // Wait until the second (final) rename callback is posted. |
| 730 file_factory->WaitForSomeCallback(); | 727 file_factory->WaitForSomeCallback(); |
| 731 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 732 ASSERT_TRUE(callbacks.empty()); | |
| 733 file_factory->GetAllRenameCallbacks(&callbacks); | 728 file_factory->GetAllRenameCallbacks(&callbacks); |
| 734 ASSERT_EQ(1u, callbacks.size()); | 729 ASSERT_EQ(1u, callbacks.size()); |
| 735 | 730 |
| 736 // Call it. | 731 // Call it. |
| 737 callbacks[0].Run(); | 732 callbacks[0].Run(); |
| 738 callbacks.clear(); | 733 callbacks.clear(); |
| 739 | 734 |
| 740 // Confirm download isn't complete yet. | 735 // Confirm download isn't complete yet. |
| 741 std::vector<DownloadItem*> items; | 736 std::vector<DownloadItem*> items; |
| 742 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 737 DownloadManagerForShell(shell())->GetAllDownloads(&items); |
| 743 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 738 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 744 | 739 |
| 745 // Cancel the download; confirm cancel fails anyway. | 740 // Cancel the download; confirm cancel fails anyway. |
| 746 ASSERT_EQ(1u, items.size()); | 741 ASSERT_EQ(1u, items.size()); |
| 747 items[0]->Cancel(true); | 742 items[0]->Cancel(true); |
| 748 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 743 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 749 RunAllPendingInMessageLoop(); | 744 RunAllPendingInMessageLoop(); |
| 750 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 745 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
| 751 | 746 |
| 752 // Get the detach callback that should have been produced by the above. | 747 MockDownloadItemObserver observer; |
| 753 file_factory->WaitForSomeCallback(); | 748 items[0]->AddObserver(&observer); |
| 754 file_factory->GetAllRenameCallbacks(&callbacks); | 749 EXPECT_CALL(observer, OnDownloadDestroyed(items[0])); |
| 755 ASSERT_TRUE(callbacks.empty()); | |
| 756 file_factory->GetAllDetachCallbacks(&callbacks); | |
| 757 ASSERT_EQ(1u, callbacks.size()); | |
| 758 | 750 |
| 759 // Shutdown the download manager. Mostly this is confirming a lack of | 751 // Shutdown the download manager. Mostly this is confirming a lack of |
| 760 // crashes. | 752 // crashes. |
| 761 DownloadManagerForShell(shell())->Shutdown(); | 753 DownloadManagerForShell(shell())->Shutdown(); |
| 762 | |
| 763 // Run the detach callback; shouldn't cause any problems. | |
| 764 callbacks[0].Run(); | |
| 765 callbacks.clear(); | |
| 766 } | 754 } |
| 767 | 755 |
| 768 } // namespace content | 756 } // namespace content |
| OLD | NEW |