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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() | 170 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() |
171 : waiting_(false) {} | 171 : waiting_(false) {} |
172 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} | 172 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} |
173 | 173 |
174 DownloadFile* DownloadFileWithDelayFactory::CreateFile( | 174 DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
175 DownloadCreateInfo* info, | 175 DownloadCreateInfo* info, |
176 scoped_ptr<content::ByteStreamReader> stream, | 176 scoped_ptr<content::ByteStreamReader> stream, |
177 DownloadManager* download_manager, | 177 DownloadManager* download_manager, |
178 bool calculate_hash, | 178 bool calculate_hash, |
179 const net::BoundNetLog& bound_net_log) { | 179 const net::BoundNetLog& bound_net_log) { |
180 | |
181 return new DownloadFileWithDelay( | 180 return new DownloadFileWithDelay( |
182 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), | 181 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), |
183 download_manager, calculate_hash, | 182 download_manager, calculate_hash, |
184 scoped_ptr<content::PowerSaveBlocker>( | 183 scoped_ptr<content::PowerSaveBlocker>( |
185 new content::PowerSaveBlocker( | 184 new content::PowerSaveBlocker( |
186 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 185 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
187 "Download in progress")).Pass(), | 186 "Download in progress")).Pass(), |
188 bound_net_log, this); | 187 bound_net_log, this); |
189 } | 188 } |
190 | 189 |
(...skipping 26 matching lines...) Expand all Loading... |
217 void DownloadFileWithDelayFactory::WaitForSomeCallback() { | 216 void DownloadFileWithDelayFactory::WaitForSomeCallback() { |
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
219 | 218 |
220 if (rename_callbacks_.empty() && detach_callbacks_.empty()) { | 219 if (rename_callbacks_.empty() && detach_callbacks_.empty()) { |
221 waiting_ = true; | 220 waiting_ = true; |
222 RunMessageLoop(); | 221 RunMessageLoop(); |
223 waiting_ = false; | 222 waiting_ = false; |
224 } | 223 } |
225 } | 224 } |
226 | 225 |
| 226 class CountingDownloadFile : public DownloadFileImpl { |
| 227 public: |
| 228 CountingDownloadFile( |
| 229 const DownloadCreateInfo* info, |
| 230 scoped_ptr<content::ByteStreamReader> stream, |
| 231 DownloadRequestHandleInterface* request_handle, |
| 232 scoped_refptr<content::DownloadManager> download_manager, |
| 233 bool calculate_hash, |
| 234 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| 235 const net::BoundNetLog& bound_net_log) |
| 236 : DownloadFileImpl(info, stream.Pass(), request_handle, download_manager, |
| 237 calculate_hash, power_save_blocker.Pass(), |
| 238 bound_net_log) {} |
| 239 |
| 240 virtual ~CountingDownloadFile() { |
| 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 242 active_files_--; |
| 243 } |
| 244 |
| 245 virtual content::DownloadInterruptReason Initialize() OVERRIDE { |
| 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 247 active_files_++; |
| 248 return DownloadFileImpl::Initialize(); |
| 249 } |
| 250 |
| 251 static void GetNumberActiveFiles(int* result) { |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 253 *result = active_files_; |
| 254 } |
| 255 |
| 256 // Can be called on any thread, and will block (running message loop) |
| 257 // until data is returned. |
| 258 static int GetNumberActiveFilesFromFileThread() { |
| 259 int result = -1; |
| 260 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, |
| 261 base::Bind(&CountingDownloadFile::GetNumberActiveFiles, &result), |
| 262 MessageLoop::current()->QuitClosure()); |
| 263 MessageLoop::current()->Run(); |
| 264 DCHECK_NE(-1, result); |
| 265 return result; |
| 266 } |
| 267 |
| 268 private: |
| 269 static int active_files_; |
| 270 }; |
| 271 |
| 272 int CountingDownloadFile::active_files_ = 0; |
| 273 |
| 274 class CountingDownloadFileFactory : public DownloadFileFactory { |
| 275 public: |
| 276 CountingDownloadFileFactory() {} |
| 277 virtual ~CountingDownloadFileFactory() {} |
| 278 |
| 279 // DownloadFileFactory interface. |
| 280 virtual content::DownloadFile* CreateFile( |
| 281 DownloadCreateInfo* info, |
| 282 scoped_ptr<content::ByteStreamReader> stream, |
| 283 DownloadManager* download_manager, |
| 284 bool calculate_hash, |
| 285 const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 286 return new CountingDownloadFile( |
| 287 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), |
| 288 download_manager, calculate_hash, |
| 289 scoped_ptr<content::PowerSaveBlocker>( |
| 290 new content::PowerSaveBlocker( |
| 291 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 292 "Download in progress")).Pass(), |
| 293 bound_net_log); |
| 294 } |
| 295 }; |
| 296 |
227 } // namespace | 297 } // namespace |
228 | 298 |
229 class DownloadContentTest : public ContentBrowserTest { | 299 class DownloadContentTest : public ContentBrowserTest { |
230 protected: | 300 protected: |
231 virtual void SetUpOnMainThread() OVERRIDE { | 301 virtual void SetUpOnMainThread() OVERRIDE { |
232 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 302 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
233 | 303 |
234 ShellDownloadManagerDelegate* delegate = | 304 ShellDownloadManagerDelegate* delegate = |
235 static_cast<ShellDownloadManagerDelegate*>( | 305 static_cast<ShellDownloadManagerDelegate*>( |
236 shell()->web_contents()->GetBrowserContext() | 306 shell()->web_contents()->GetBrowserContext() |
(...skipping 19 matching lines...) Expand all Loading... |
256 } | 326 } |
257 | 327 |
258 // Create a DownloadTestObserverInProgress that will wait for the | 328 // Create a DownloadTestObserverInProgress that will wait for the |
259 // specified number of downloads to start. | 329 // specified number of downloads to start. |
260 DownloadTestObserver* CreateInProgressWaiter( | 330 DownloadTestObserver* CreateInProgressWaiter( |
261 Shell* shell, int num_downloads) { | 331 Shell* shell, int num_downloads) { |
262 DownloadManager* download_manager = DownloadManagerForShell(shell); | 332 DownloadManager* download_manager = DownloadManagerForShell(shell); |
263 return new DownloadTestObserverInProgress(download_manager, num_downloads); | 333 return new DownloadTestObserverInProgress(download_manager, num_downloads); |
264 } | 334 } |
265 | 335 |
| 336 // Note: Cannot be used with other alternative DownloadFileFactorys |
| 337 void SetupEnsureNoPendingDownloads() { |
| 338 GetDownloadFileManager()->SetFileFactoryForTesting( |
| 339 scoped_ptr<content::DownloadFileFactory>( |
| 340 new CountingDownloadFileFactory()).Pass()); |
| 341 } |
| 342 |
266 bool EnsureNoPendingDownloads() { | 343 bool EnsureNoPendingDownloads() { |
267 bool result = true; | 344 bool result = true; |
268 BrowserThread::PostTask( | 345 BrowserThread::PostTask( |
269 BrowserThread::IO, FROM_HERE, | 346 BrowserThread::IO, FROM_HERE, |
270 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 347 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
271 MessageLoop::current()->Run(); | 348 MessageLoop::current()->Run(); |
272 return result && DownloadManager::EnsureNoPendingDownloadsForTesting(); | 349 return result && |
| 350 (CountingDownloadFile::GetNumberActiveFilesFromFileThread() == 0); |
273 } | 351 } |
274 | 352 |
275 void DownloadAndWait(Shell* shell, const GURL& url) { | 353 void DownloadAndWait(Shell* shell, const GURL& url) { |
276 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); | 354 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); |
277 NavigateToURL(shell, url); | 355 NavigateToURL(shell, url); |
278 observer->WaitForFinished(); | 356 observer->WaitForFinished(); |
279 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); | 357 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
280 } | 358 } |
281 | 359 |
282 // Checks that |path| is has |file_size| bytes, and matches the |value| | 360 // Checks that |path| is has |file_size| bytes, and matches the |value| |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 // DownloadItems are created in the IN_PROGRESS state and made known | 413 // DownloadItems are created in the IN_PROGRESS state and made known |
336 // to the DownloadManager immediately, so any ModelChanged event on | 414 // to the DownloadManager immediately, so any ModelChanged event on |
337 // the DownloadManager after navigation would allow the observer to | 415 // the DownloadManager after navigation would allow the observer to |
338 // return. However, the only ModelChanged() event the code will | 416 // return. However, the only ModelChanged() event the code will |
339 // currently fire is in OnCreateDownloadEntryComplete, at which | 417 // currently fire is in OnCreateDownloadEntryComplete, at which |
340 // point the download item will be in the state we need. | 418 // point the download item will be in the state we need. |
341 // The right way to fix this is to create finer grained states on the | 419 // The right way to fix this is to create finer grained states on the |
342 // DownloadItem, and wait for the state that indicates the item has been | 420 // DownloadItem, and wait for the state that indicates the item has been |
343 // entered in the history and made visible in the UI. | 421 // entered in the history and made visible in the UI. |
344 | 422 |
| 423 SetupEnsureNoPendingDownloads(); |
| 424 |
345 // Create a download, wait until it's started, and confirm | 425 // Create a download, wait until it's started, and confirm |
346 // we're in the expected state. | 426 // we're in the expected state. |
347 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); | 427 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
348 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 428 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
349 observer->WaitForFinished(); | 429 observer->WaitForFinished(); |
350 | 430 |
351 std::vector<DownloadItem*> downloads; | 431 std::vector<DownloadItem*> downloads; |
352 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 432 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
353 ASSERT_EQ(1u, downloads.size()); | 433 ASSERT_EQ(1u, downloads.size()); |
354 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); | 434 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); |
355 | 435 |
356 // Cancel the download and wait for download system quiesce. | 436 // Cancel the download and wait for download system quiesce. |
357 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 437 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
358 scoped_refptr<DownloadTestFlushObserver> flush_observer( | 438 scoped_refptr<DownloadTestFlushObserver> flush_observer( |
359 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); | 439 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); |
360 flush_observer->WaitForFlush(); | 440 flush_observer->WaitForFlush(); |
361 | 441 |
362 // Get the important info from other threads and check it. | 442 // Get the important info from other threads and check it. |
363 EXPECT_TRUE(EnsureNoPendingDownloads()); | 443 EXPECT_TRUE(EnsureNoPendingDownloads()); |
364 } | 444 } |
365 | 445 |
366 // Check that downloading multiple (in this case, 2) files does not result in | 446 // Check that downloading multiple (in this case, 2) files does not result in |
367 // corrupted files. | 447 // corrupted files. |
368 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { | 448 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { |
| 449 SetupEnsureNoPendingDownloads(); |
| 450 |
369 // Create a download, wait until it's started, and confirm | 451 // Create a download, wait until it's started, and confirm |
370 // we're in the expected state. | 452 // we're in the expected state. |
371 scoped_ptr<DownloadTestObserver> observer1( | 453 scoped_ptr<DownloadTestObserver> observer1( |
372 CreateInProgressWaiter(shell(), 1)); | 454 CreateInProgressWaiter(shell(), 1)); |
373 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 455 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
374 observer1->WaitForFinished(); | 456 observer1->WaitForFinished(); |
375 | 457 |
376 std::vector<DownloadItem*> downloads; | 458 std::vector<DownloadItem*> downloads; |
377 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 459 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
378 ASSERT_EQ(1u, downloads.size()); | 460 ASSERT_EQ(1u, downloads.size()); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 file_factory->GetAllRenameCallbacks(&callbacks); | 601 file_factory->GetAllRenameCallbacks(&callbacks); |
520 ASSERT_TRUE(callbacks.empty()); | 602 ASSERT_TRUE(callbacks.empty()); |
521 file_factory->GetAllDetachCallbacks(&callbacks); | 603 file_factory->GetAllDetachCallbacks(&callbacks); |
522 ASSERT_EQ(1u, callbacks.size()); | 604 ASSERT_EQ(1u, callbacks.size()); |
523 callbacks[0].Run(); | 605 callbacks[0].Run(); |
524 callbacks.clear(); | 606 callbacks.clear(); |
525 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 607 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
526 } | 608 } |
527 | 609 |
528 } // namespace content | 610 } // namespace content |
OLD | NEW |