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" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // We're in a content_browsertest; we know that the DownloadManager | 66 // We're in a content_browsertest; we know that the DownloadManager |
69 // is a DownloadManagerImpl. | 67 // is a DownloadManagerImpl. |
70 return static_cast<DownloadManagerImpl*>( | 68 return static_cast<DownloadManagerImpl*>( |
71 BrowserContext::GetDownloadManager( | 69 BrowserContext::GetDownloadManager( |
72 shell->web_contents()->GetBrowserContext())); | 70 shell->web_contents()->GetBrowserContext())); |
73 } | 71 } |
74 | 72 |
75 class DownloadFileWithDelay : public DownloadFileImpl { | 73 class DownloadFileWithDelay : public DownloadFileImpl { |
76 public: | 74 public: |
77 DownloadFileWithDelay( | 75 DownloadFileWithDelay( |
78 const DownloadCreateInfo* info, | 76 const DownloadSaveInfo& save_info, |
79 scoped_ptr<content::ByteStreamReader> stream, | 77 const FilePath& default_download_directory, |
80 DownloadRequestHandleInterface* request_handle, | 78 const GURL& url, |
81 scoped_refptr<content::DownloadManager> download_manager, | 79 const GURL& referrer_url, |
82 bool calculate_hash, | 80 int64 received_bytes, |
83 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 81 bool calculate_hash, |
84 const net::BoundNetLog& bound_net_log, | 82 scoped_ptr<ByteStreamReader> stream, |
85 // |owner| is required to outlive the DownloadFileWithDelay. | 83 const net::BoundNetLog& bound_net_log, |
86 DownloadFileWithDelayFactory* owner); | 84 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 85 base::WeakPtr<DownloadDestinationObserver> observer, |
| 86 base::WeakPtr<DownloadFileWithDelayFactory> owner); |
87 | 87 |
88 virtual ~DownloadFileWithDelay(); | 88 virtual ~DownloadFileWithDelay(); |
89 | 89 |
90 // Wraps DownloadFileImpl::Rename and intercepts the return callback, | 90 // Wraps DownloadFileImpl::Rename and intercepts the return callback, |
91 // storing it in the factory that produced this object for later | 91 // storing it in the factory that produced this object for later |
92 // retrieval. | 92 // retrieval. |
93 virtual void Rename(const FilePath& full_path, | 93 virtual void Rename(const FilePath& full_path, |
94 bool overwrite_existing_file, | 94 bool overwrite_existing_file, |
95 const RenameCompletionCallback& callback) OVERRIDE; | 95 const RenameCompletionCallback& callback) OVERRIDE; |
96 | 96 |
97 // Wraps DownloadFileImpl::Detach and intercepts the return callback, | 97 // Wraps DownloadFileImpl::Detach and intercepts the return callback, |
98 // storing it in the factory that produced this object for later | 98 // storing it in the factory that produced this object for later |
99 // retrieval. | 99 // retrieval. |
100 virtual void Detach(base::Closure callback) OVERRIDE; | 100 virtual void Detach(base::Closure callback) OVERRIDE; |
101 | 101 |
102 private: | 102 private: |
103 static void RenameCallbackWrapper( | 103 static void RenameCallbackWrapper( |
104 DownloadFileWithDelayFactory* factory, | 104 DownloadFileWithDelayFactory* factory, |
105 const RenameCompletionCallback& original_callback, | 105 const RenameCompletionCallback& original_callback, |
106 content::DownloadInterruptReason reason, | 106 DownloadInterruptReason reason, |
107 const FilePath& path); | 107 const FilePath& path); |
108 | 108 |
109 static void DetachCallbackWrapper( | 109 static void DetachCallbackWrapper( |
110 DownloadFileWithDelayFactory* factory, | 110 DownloadFileWithDelayFactory* factory, |
111 const base::Closure& original_callback); | 111 const base::Closure& original_callback); |
112 | 112 |
113 // May only be used on the UI thread. | 113 // This variable may only be read on the FILE thread, and may only be |
114 DownloadFileWithDelayFactory* owner_; | 114 // indirected through (e.g. methods on DownloadFileWithDelayFactory called) |
| 115 // on the UI thread. This is because after construction, |
| 116 // DownloadFileWithDelay lives on the file thread, but |
| 117 // DownloadFileWithDelayFactory is purely a UI thread object. |
| 118 base::WeakPtr<DownloadFileWithDelayFactory> owner_; |
115 | 119 |
116 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); | 120 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelay); |
117 }; | 121 }; |
118 | 122 |
| 123 // All routines on this class must be called on the UI thread. |
119 class DownloadFileWithDelayFactory : public DownloadFileFactory { | 124 class DownloadFileWithDelayFactory : public DownloadFileFactory { |
120 public: | 125 public: |
121 DownloadFileWithDelayFactory(); | 126 DownloadFileWithDelayFactory(); |
122 virtual ~DownloadFileWithDelayFactory(); | 127 virtual ~DownloadFileWithDelayFactory(); |
123 | 128 |
124 // DownloadFileFactory interface. | 129 // DownloadFileFactory interface. |
125 virtual content::DownloadFile* CreateFile( | 130 virtual DownloadFile* CreateFile( |
126 DownloadCreateInfo* info, | 131 const DownloadSaveInfo& save_info, |
127 scoped_ptr<content::ByteStreamReader> stream, | 132 const FilePath& default_download_directory, |
128 DownloadManager* download_manager, | 133 GURL url, |
| 134 GURL referrer_url, |
| 135 int64 received_bytes, |
129 bool calculate_hash, | 136 bool calculate_hash, |
130 const net::BoundNetLog& bound_net_log) OVERRIDE; | 137 scoped_ptr<ByteStreamReader> stream, |
| 138 const net::BoundNetLog& bound_net_log, |
| 139 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; |
131 | 140 |
132 // Must all be called on the UI thread. | |
133 void AddRenameCallback(base::Closure callback); | 141 void AddRenameCallback(base::Closure callback); |
134 void AddDetachCallback(base::Closure callback); | 142 void AddDetachCallback(base::Closure callback); |
135 void GetAllRenameCallbacks(std::vector<base::Closure>* results); | 143 void GetAllRenameCallbacks(std::vector<base::Closure>* results); |
136 void GetAllDetachCallbacks(std::vector<base::Closure>* results); | 144 void GetAllDetachCallbacks(std::vector<base::Closure>* results); |
137 | 145 |
138 // Do not return until either GetAllRenameCallbacks() or | 146 // Do not return until either GetAllRenameCallbacks() or |
139 // GetAllDetachCallbacks() will return a non-empty list. | 147 // GetAllDetachCallbacks() will return a non-empty list. |
140 void WaitForSomeCallback(); | 148 void WaitForSomeCallback(); |
141 | 149 |
142 private: | 150 private: |
| 151 base::WeakPtrFactory<DownloadFileWithDelayFactory> weak_ptr_factory_; |
143 std::vector<base::Closure> rename_callbacks_; | 152 std::vector<base::Closure> rename_callbacks_; |
144 std::vector<base::Closure> detach_callbacks_; | 153 std::vector<base::Closure> detach_callbacks_; |
145 bool waiting_; | 154 bool waiting_; |
146 | 155 |
147 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); | 156 DISALLOW_COPY_AND_ASSIGN(DownloadFileWithDelayFactory); |
148 }; | 157 }; |
149 | 158 |
150 DownloadFileWithDelay::DownloadFileWithDelay( | 159 DownloadFileWithDelay::DownloadFileWithDelay( |
151 const DownloadCreateInfo* info, | 160 const DownloadSaveInfo& save_info, |
152 scoped_ptr<content::ByteStreamReader> stream, | 161 const FilePath& default_download_directory, |
153 DownloadRequestHandleInterface* request_handle, | 162 const GURL& url, |
154 scoped_refptr<content::DownloadManager> download_manager, | 163 const GURL& referrer_url, |
155 bool calculate_hash, | 164 int64 received_bytes, |
156 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | 165 bool calculate_hash, |
157 const net::BoundNetLog& bound_net_log, | 166 scoped_ptr<ByteStreamReader> stream, |
158 DownloadFileWithDelayFactory* owner) | 167 const net::BoundNetLog& bound_net_log, |
159 : DownloadFileImpl(info, stream.Pass(), request_handle, download_manager, | 168 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
160 calculate_hash, power_save_blocker.Pass(), | 169 base::WeakPtr<DownloadDestinationObserver> observer, |
161 bound_net_log), | 170 base::WeakPtr<DownloadFileWithDelayFactory> owner) |
| 171 : DownloadFileImpl( |
| 172 save_info, default_download_directory, url, referrer_url, |
| 173 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 174 power_save_blocker.Pass(), observer), |
162 owner_(owner) {} | 175 owner_(owner) {} |
163 | 176 |
164 DownloadFileWithDelay::~DownloadFileWithDelay() {} | 177 DownloadFileWithDelay::~DownloadFileWithDelay() {} |
165 | 178 |
166 void DownloadFileWithDelay::Rename(const FilePath& full_path, | 179 void DownloadFileWithDelay::Rename(const FilePath& full_path, |
167 bool overwrite_existing_file, | 180 bool overwrite_existing_file, |
168 const RenameCompletionCallback& callback) { | 181 const RenameCompletionCallback& callback) { |
169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
170 DownloadFileImpl::Rename( | 183 DownloadFileImpl::Rename( |
171 full_path, overwrite_existing_file, | 184 full_path, overwrite_existing_file, |
172 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, | 185 base::Bind(DownloadFileWithDelay::RenameCallbackWrapper, |
173 base::Unretained(owner_), callback)); | 186 owner_, callback)); |
174 } | 187 } |
175 | 188 |
176 void DownloadFileWithDelay::Detach(base::Closure callback) { | 189 void DownloadFileWithDelay::Detach(base::Closure callback) { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
178 DownloadFileImpl::Detach( | 191 DownloadFileImpl::Detach( |
179 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, | 192 base::Bind(DownloadFileWithDelay::DetachCallbackWrapper, |
180 base::Unretained(owner_), callback)); | 193 owner_, callback)); |
181 } | 194 } |
182 | 195 |
183 // static | 196 // static |
184 void DownloadFileWithDelay::RenameCallbackWrapper( | 197 void DownloadFileWithDelay::RenameCallbackWrapper( |
185 DownloadFileWithDelayFactory* factory, | 198 DownloadFileWithDelayFactory* factory, |
186 const RenameCompletionCallback& original_callback, | 199 const RenameCompletionCallback& original_callback, |
187 content::DownloadInterruptReason reason, | 200 DownloadInterruptReason reason, |
188 const FilePath& path) { | 201 const FilePath& path) { |
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
190 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); | 203 factory->AddRenameCallback(base::Bind(original_callback, reason, path)); |
191 } | 204 } |
192 | 205 |
193 // static | 206 // static |
194 void DownloadFileWithDelay::DetachCallbackWrapper( | 207 void DownloadFileWithDelay::DetachCallbackWrapper( |
195 DownloadFileWithDelayFactory* factory, | 208 DownloadFileWithDelayFactory* factory, |
196 const base::Closure& original_callback) { | 209 const base::Closure& original_callback) { |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
198 factory->AddDetachCallback(original_callback); | 211 factory->AddDetachCallback(original_callback); |
199 } | 212 } |
200 | 213 |
201 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() | 214 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() |
202 : waiting_(false) {} | 215 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 216 waiting_(false) {} |
203 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} | 217 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} |
204 | 218 |
205 DownloadFile* DownloadFileWithDelayFactory::CreateFile( | 219 DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
206 DownloadCreateInfo* info, | 220 const DownloadSaveInfo& save_info, |
207 scoped_ptr<content::ByteStreamReader> stream, | 221 const FilePath& default_download_directory, |
208 DownloadManager* download_manager, | 222 GURL url, |
| 223 GURL referrer_url, |
| 224 int64 received_bytes, |
209 bool calculate_hash, | 225 bool calculate_hash, |
210 const net::BoundNetLog& bound_net_log) { | 226 scoped_ptr<ByteStreamReader> stream, |
211 | 227 const net::BoundNetLog& bound_net_log, |
| 228 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 229 scoped_ptr<PowerSaveBlocker> psb( |
| 230 new PowerSaveBlocker( |
| 231 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 232 "Download in progress")); |
212 return new DownloadFileWithDelay( | 233 return new DownloadFileWithDelay( |
213 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), | 234 save_info, default_download_directory, url, referrer_url, |
214 download_manager, calculate_hash, | 235 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
215 scoped_ptr<content::PowerSaveBlocker>( | 236 psb.Pass(), observer, weak_ptr_factory_.GetWeakPtr()); |
216 new content::PowerSaveBlocker( | |
217 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
218 "Download in progress")).Pass(), | |
219 bound_net_log, this); | |
220 } | 237 } |
221 | 238 |
222 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { | 239 void DownloadFileWithDelayFactory::AddRenameCallback(base::Closure callback) { |
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
224 rename_callbacks_.push_back(callback); | 241 rename_callbacks_.push_back(callback); |
225 if (waiting_) | 242 if (waiting_) |
226 MessageLoopForUI::current()->Quit(); | 243 MessageLoopForUI::current()->Quit(); |
227 } | 244 } |
228 | 245 |
229 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { | 246 void DownloadFileWithDelayFactory::AddDetachCallback(base::Closure callback) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 return false; | 353 return false; |
337 | 354 |
338 // Check the contents. | 355 // Check the contents. |
339 EXPECT_EQ(value, file_contents); | 356 EXPECT_EQ(value, file_contents); |
340 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) | 357 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
341 return false; | 358 return false; |
342 | 359 |
343 return true; | 360 return true; |
344 } | 361 } |
345 | 362 |
346 DownloadFileManager* GetDownloadFileManager() { | |
347 ResourceDispatcherHostImpl* rdh(ResourceDispatcherHostImpl::Get()); | |
348 return rdh->download_file_manager(); | |
349 } | |
350 | |
351 private: | 363 private: |
352 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { | 364 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
353 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) | 365 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) |
354 *result = false; | 366 *result = false; |
355 BrowserThread::PostTask( | 367 BrowserThread::PostTask( |
356 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 368 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
357 } | 369 } |
358 | 370 |
359 // Location of the downloads directory for these tests | 371 // Location of the downloads directory for these tests |
360 ScopedTempDir downloads_directory_; | 372 ScopedTempDir downloads_directory_; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 ASSERT_TRUE(file_util::ContentsEqual( | 465 ASSERT_TRUE(file_util::ContentsEqual( |
454 file2, GetTestFilePath("download", "download-test.lib"))); | 466 file2, GetTestFilePath("download", "download-test.lib"))); |
455 } | 467 } |
456 | 468 |
457 // Try to cancel just before we release the download file, by delaying final | 469 // Try to cancel just before we release the download file, by delaying final |
458 // rename callback. | 470 // rename callback. |
459 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { | 471 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtFinalRename) { |
460 // Setup new factory. | 472 // Setup new factory. |
461 DownloadFileWithDelayFactory* file_factory = | 473 DownloadFileWithDelayFactory* file_factory = |
462 new DownloadFileWithDelayFactory(); | 474 new DownloadFileWithDelayFactory(); |
463 GetDownloadFileManager()->SetFileFactoryForTesting( | 475 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
464 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 476 download_manager->SetDownloadFileFactoryForTesting( |
| 477 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
465 | 478 |
466 // Create a download | 479 // Create a download |
467 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 480 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
468 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 481 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
469 | 482 |
470 // Wait until the first (intermediate file) rename and execute the callback. | 483 // Wait until the first (intermediate file) rename and execute the callback. |
471 file_factory->WaitForSomeCallback(); | 484 file_factory->WaitForSomeCallback(); |
472 std::vector<base::Closure> callbacks; | 485 std::vector<base::Closure> callbacks; |
473 file_factory->GetAllDetachCallbacks(&callbacks); | 486 file_factory->GetAllDetachCallbacks(&callbacks); |
474 ASSERT_TRUE(callbacks.empty()); | 487 ASSERT_TRUE(callbacks.empty()); |
475 file_factory->GetAllRenameCallbacks(&callbacks); | 488 file_factory->GetAllRenameCallbacks(&callbacks); |
476 ASSERT_EQ(1u, callbacks.size()); | 489 ASSERT_EQ(1u, callbacks.size()); |
477 callbacks[0].Run(); | 490 callbacks[0].Run(); |
478 callbacks.clear(); | 491 callbacks.clear(); |
479 | 492 |
480 // Wait until the second (final) rename callback is posted. | 493 // Wait until the second (final) rename callback is posted. |
481 file_factory->WaitForSomeCallback(); | 494 file_factory->WaitForSomeCallback(); |
482 file_factory->GetAllDetachCallbacks(&callbacks); | 495 file_factory->GetAllDetachCallbacks(&callbacks); |
483 ASSERT_TRUE(callbacks.empty()); | 496 ASSERT_TRUE(callbacks.empty()); |
484 file_factory->GetAllRenameCallbacks(&callbacks); | 497 file_factory->GetAllRenameCallbacks(&callbacks); |
485 ASSERT_EQ(1u, callbacks.size()); | 498 ASSERT_EQ(1u, callbacks.size()); |
486 | 499 |
487 // Cancel it. | 500 // Cancel it. |
488 std::vector<DownloadItem*> items; | 501 std::vector<DownloadItem*> items; |
489 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 502 download_manager->GetAllDownloads(&items); |
490 ASSERT_EQ(1u, items.size()); | 503 ASSERT_EQ(1u, items.size()); |
491 items[0]->Cancel(true); | 504 items[0]->Cancel(true); |
492 RunAllPendingInMessageLoop(); | 505 RunAllPendingInMessageLoop(); |
493 | 506 |
494 // Check state. | 507 // Check state. |
495 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 508 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
496 | 509 |
497 // Run final rename callback. | 510 // Run final rename callback. |
498 callbacks[0].Run(); | 511 callbacks[0].Run(); |
499 callbacks.clear(); | 512 callbacks.clear(); |
500 | 513 |
501 // Check state. | 514 // Check state. |
502 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); | 515 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); |
503 } | 516 } |
504 | 517 |
505 // Try to cancel just after we release the download file, by delaying | 518 // Try to cancel just after we release the download file, by delaying |
506 // release. | 519 // release. |
507 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { | 520 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelAtRelease) { |
508 // Setup new factory. | 521 // Setup new factory. |
509 DownloadFileWithDelayFactory* file_factory = | 522 DownloadFileWithDelayFactory* file_factory = |
510 new DownloadFileWithDelayFactory(); | 523 new DownloadFileWithDelayFactory(); |
511 GetDownloadFileManager()->SetFileFactoryForTesting( | 524 DownloadManagerImpl* download_manager(DownloadManagerForShell(shell())); |
512 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 525 download_manager->SetDownloadFileFactoryForTesting( |
| 526 scoped_ptr<DownloadFileFactory>(file_factory).Pass()); |
513 | 527 |
514 // Create a download | 528 // Create a download |
515 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 529 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
516 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 530 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
517 | 531 |
518 // Wait until the first (intermediate file) rename and execute the callback. | 532 // Wait until the first (intermediate file) rename and execute the callback. |
519 file_factory->WaitForSomeCallback(); | 533 file_factory->WaitForSomeCallback(); |
520 std::vector<base::Closure> callbacks; | 534 std::vector<base::Closure> callbacks; |
521 file_factory->GetAllDetachCallbacks(&callbacks); | 535 file_factory->GetAllDetachCallbacks(&callbacks); |
522 ASSERT_TRUE(callbacks.empty()); | 536 ASSERT_TRUE(callbacks.empty()); |
523 file_factory->GetAllRenameCallbacks(&callbacks); | 537 file_factory->GetAllRenameCallbacks(&callbacks); |
524 ASSERT_EQ(1u, callbacks.size()); | 538 ASSERT_EQ(1u, callbacks.size()); |
525 callbacks[0].Run(); | 539 callbacks[0].Run(); |
526 callbacks.clear(); | 540 callbacks.clear(); |
527 | 541 |
528 // Wait until the second (final) rename callback is posted. | 542 // Wait until the second (final) rename callback is posted. |
529 file_factory->WaitForSomeCallback(); | 543 file_factory->WaitForSomeCallback(); |
530 file_factory->GetAllDetachCallbacks(&callbacks); | 544 file_factory->GetAllDetachCallbacks(&callbacks); |
531 ASSERT_TRUE(callbacks.empty()); | 545 ASSERT_TRUE(callbacks.empty()); |
532 file_factory->GetAllRenameCallbacks(&callbacks); | 546 file_factory->GetAllRenameCallbacks(&callbacks); |
533 ASSERT_EQ(1u, callbacks.size()); | 547 ASSERT_EQ(1u, callbacks.size()); |
534 | 548 |
535 // Call it. | 549 // Call it. |
536 callbacks[0].Run(); | 550 callbacks[0].Run(); |
537 callbacks.clear(); | 551 callbacks.clear(); |
538 | 552 |
539 // Confirm download isn't complete yet. | 553 // Confirm download still IN_PROGRESS. |
540 std::vector<DownloadItem*> items; | 554 std::vector<DownloadItem*> items; |
541 DownloadManagerForShell(shell())->GetAllDownloads(&items); | 555 download_manager->GetAllDownloads(&items); |
542 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 556 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
543 | 557 |
544 // Cancel the download; confirm cancel fails anyway. | 558 // Cancel the download; confirm cancel fails. |
545 ASSERT_EQ(1u, items.size()); | 559 ASSERT_EQ(1u, items.size()); |
546 items[0]->Cancel(true); | 560 items[0]->Cancel(true); |
547 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | 561 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); |
548 RunAllPendingInMessageLoop(); | |
549 EXPECT_EQ(DownloadItem::IN_PROGRESS, items[0]->GetState()); | |
550 | 562 |
551 // Confirm detach callback and run it. | 563 // Confirm detach callback and run it. |
552 file_factory->WaitForSomeCallback(); | 564 file_factory->WaitForSomeCallback(); |
553 file_factory->GetAllRenameCallbacks(&callbacks); | 565 file_factory->GetAllRenameCallbacks(&callbacks); |
554 ASSERT_TRUE(callbacks.empty()); | 566 ASSERT_TRUE(callbacks.empty()); |
555 file_factory->GetAllDetachCallbacks(&callbacks); | 567 file_factory->GetAllDetachCallbacks(&callbacks); |
556 ASSERT_EQ(1u, callbacks.size()); | 568 ASSERT_EQ(1u, callbacks.size()); |
557 callbacks[0].Run(); | 569 callbacks[0].Run(); |
558 callbacks.clear(); | 570 callbacks.clear(); |
| 571 |
| 572 // *Now* the download should be complete. |
559 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 573 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
560 } | 574 } |
561 | 575 |
562 // Try to shutdown with a download in progress to make sure shutdown path | 576 // Try to shutdown with a download in progress to make sure shutdown path |
563 // works properly. | 577 // works properly. |
564 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { | 578 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownInProgress) { |
565 // Create a download that won't complete. | 579 // Create a download that won't complete. |
566 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); | 580 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
567 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 581 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
568 observer->WaitForFinished(); | 582 observer->WaitForFinished(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 DownloadManagerForShell(shell())->Shutdown(); | 616 DownloadManagerForShell(shell())->Shutdown(); |
603 items.clear(); | 617 items.clear(); |
604 } | 618 } |
605 | 619 |
606 // Try to shutdown just after we release the download file, by delaying | 620 // Try to shutdown just after we release the download file, by delaying |
607 // release. | 621 // release. |
608 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { | 622 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ShutdownAtRelease) { |
609 // Setup new factory. | 623 // Setup new factory. |
610 DownloadFileWithDelayFactory* file_factory = | 624 DownloadFileWithDelayFactory* file_factory = |
611 new DownloadFileWithDelayFactory(); | 625 new DownloadFileWithDelayFactory(); |
612 GetDownloadFileManager()->SetFileFactoryForTesting( | 626 DownloadManagerForShell(shell())->SetDownloadFileFactoryForTesting( |
613 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); | 627 scoped_ptr<content::DownloadFileFactory>(file_factory).Pass()); |
614 | 628 |
615 // Create a download | 629 // Create a download |
616 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 630 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
617 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); | 631 NavigateToURL(shell(), URLRequestMockHTTPJob::GetMockUrl(file)); |
618 | 632 |
619 // Wait until the first (intermediate file) rename and execute the callback. | 633 // Wait until the first (intermediate file) rename and execute the callback. |
620 file_factory->WaitForSomeCallback(); | 634 file_factory->WaitForSomeCallback(); |
621 std::vector<base::Closure> callbacks; | 635 std::vector<base::Closure> callbacks; |
622 file_factory->GetAllDetachCallbacks(&callbacks); | 636 file_factory->GetAllDetachCallbacks(&callbacks); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 // Shutdown the download manager. Mostly this is confirming a lack of | 673 // Shutdown the download manager. Mostly this is confirming a lack of |
660 // crashes. | 674 // crashes. |
661 DownloadManagerForShell(shell())->Shutdown(); | 675 DownloadManagerForShell(shell())->Shutdown(); |
662 | 676 |
663 // Run the detach callback; shouldn't cause any problems. | 677 // Run the detach callback; shouldn't cause any problems. |
664 callbacks[0].Run(); | 678 callbacks[0].Run(); |
665 callbacks.clear(); | 679 callbacks.clear(); |
666 } | 680 } |
667 | 681 |
668 } // namespace content | 682 } // namespace content |
OLD | NEW |