Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: chrome/browser/download/download_browsertest.cc

Issue 7983037: Revert 102126 - Make cancel remove cancelled download from active queues at time of cancel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/file_path.h" 6 #include "base/file_path.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h" 10 #include "base/scoped_temp_dir.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 53 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
54 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); 54 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx"));
55 55
56 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; 56 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
57 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); 57 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx"));
58 58
59 // Action a test should take if a dangerous download is encountered. 59 // Action a test should take if a dangerous download is encountered.
60 enum DangerousDownloadAction { 60 enum DangerousDownloadAction {
61 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download 61 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download
62 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download 62 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download
63 ON_DANGEROUS_DOWNLOAD_IGNORE, // Don't do anything; calling code will handle.
64 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen 63 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen
65 }; 64 };
66 65
67 // Fake user click on "Accept". 66 // Fake user click on "Accept".
68 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, 67 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager,
69 int32 download_id) { 68 int32 download_id) {
70 DownloadItem* download = download_manager->GetDownloadItem(download_id); 69 DownloadItem* download = download_manager->GetDownloadItem(download_id);
71 download->DangerousDownloadValidated(); 70 download->DangerousDownloadValidated();
72 } 71 }
73 72
74 // Fake user click on "Deny". 73 // Fake user click on "Deny".
75 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, 74 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager,
76 int32 download_id) { 75 int32 download_id) {
77 DownloadItem* download = download_manager->GetDownloadItem(download_id); 76 DownloadItem* download = download_manager->GetDownloadItem(download_id);
78 ASSERT_TRUE(download->IsPartialDownload()); 77 ASSERT_TRUE(download->IsPartialDownload());
79 download->Cancel(); 78 download->Cancel(true);
80 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 79 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
81 } 80 }
82 81
83 // Construction of this class defines a system state, based on some number 82 // Construction of this class defines a system state, based on some number
84 // of downloads being seen in a particular state + other events that 83 // of downloads being seen in a particular state + other events that
85 // may occur in the download system. That state will be recorded if it 84 // may occur in the download system. That state will be recorded if it
86 // occurs at any point after construction. When that state occurs, the class 85 // occurs at any point after construction. When that state occurs, the class
87 // is considered finished. Callers may either probe for the finished state, or 86 // is considered finished. Callers may either probe for the finished state, or
88 // wait on it. 87 // wait on it.
89 // 88 //
90 // TODO(rdsmith): Detect manager going down, remove pointer to 89 // TODO(rdsmith): Detect manager going down, remove pointer to
91 // DownloadManager, transition to finished. (For right now we 90 // DownloadManager, transition to finished. (For right now we
92 // just use a scoped_refptr<> to keep it around, but that may cause 91 // just use a scoped_refptr<> to keep it around, but that may cause
93 // timeouts on waiting if a DownloadManager::Shutdown() occurs which 92 // timeouts on waiting if a DownloadManager::Shutdown() occurs which
94 // cancels our in-progress downloads.) 93 // cancels our in-progress downloads.)
95 class DownloadsObserver : public DownloadManager::Observer, 94 class DownloadsObserver : public DownloadManager::Observer,
96 public DownloadItem::Observer { 95 public DownloadItem::Observer {
97 public: 96 public:
98 typedef std::set<DownloadItem::DownloadState> StateSet;
99
100 // Create an object that will be considered finished when |wait_count| 97 // Create an object that will be considered finished when |wait_count|
101 // download items have entered any states in |download_finished_states|. 98 // download items have entered state |download_finished_state|.
102 // If |finish_on_select_file| is true, the object will also be 99 // If |finish_on_select_file| is true, the object will also be
103 // considered finished if the DownloadManager raises a 100 // considered finished if the DownloadManager raises a
104 // SelectFileDialogDisplayed() notification. 101 // SelectFileDialogDisplayed() notification.
105 102
106 // TODO(rdsmith): Consider rewriting the interface to take a list of events 103 // TODO(rdsmith): Consider rewriting the interface to take a list of events
107 // to treat as completion events. 104 // to treat as completion events.
108 DownloadsObserver(DownloadManager* download_manager, 105 DownloadsObserver(DownloadManager* download_manager,
109 size_t wait_count, 106 size_t wait_count,
110 StateSet download_finished_states, 107 DownloadItem::DownloadState download_finished_state,
111 bool finish_on_select_file, 108 bool finish_on_select_file,
112 DangerousDownloadAction dangerous_download_action) 109 DangerousDownloadAction dangerous_download_action)
113 : download_manager_(download_manager), 110 : download_manager_(download_manager),
114 wait_count_(wait_count), 111 wait_count_(wait_count),
115 finished_downloads_at_construction_(0), 112 finished_downloads_at_construction_(0),
116 waiting_(false), 113 waiting_(false),
117 download_finished_states_(download_finished_states), 114 download_finished_state_(download_finished_state),
118 finish_on_select_file_(finish_on_select_file), 115 finish_on_select_file_(finish_on_select_file),
119 select_file_dialog_seen_(false), 116 select_file_dialog_seen_(false),
120 dangerous_download_action_(dangerous_download_action) { 117 dangerous_download_action_(dangerous_download_action) {
121 download_manager_->AddObserver(this); // Will call initial ModelChanged(). 118 download_manager_->AddObserver(this); // Will call initial ModelChanged().
122 finished_downloads_at_construction_ = finished_downloads_.size(); 119 finished_downloads_at_construction_ = finished_downloads_.size();
123 EXPECT_TRUE(download_finished_states.find(DownloadItem::REMOVING) == 120 EXPECT_NE(DownloadItem::REMOVING, download_finished_state)
124 download_finished_states.end())
125 << "Waiting for REMOVING is not supported. Try COMPLETE."; 121 << "Waiting for REMOVING is not supported. Try COMPLETE.";
126 } 122 }
127 123
128 ~DownloadsObserver() { 124 ~DownloadsObserver() {
129 std::set<DownloadItem*>::iterator it = downloads_observed_.begin(); 125 std::set<DownloadItem*>::iterator it = downloads_observed_.begin();
130 for (; it != downloads_observed_.end(); ++it) 126 for (; it != downloads_observed_.end(); ++it)
131 (*it)->RemoveObserver(this); 127 (*it)->RemoveObserver(this);
132 128
133 download_manager_->RemoveObserver(this); 129 download_manager_->RemoveObserver(this);
134 } 130 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 NewRunnableFunction( 189 NewRunnableFunction(
194 &DenyDangerousDownload, 190 &DenyDangerousDownload,
195 download_manager_, 191 download_manager_,
196 download->id())); 192 download->id()));
197 break; 193 break;
198 194
199 case ON_DANGEROUS_DOWNLOAD_FAIL: 195 case ON_DANGEROUS_DOWNLOAD_FAIL:
200 ADD_FAILURE() << "Unexpected dangerous download item."; 196 ADD_FAILURE() << "Unexpected dangerous download item.";
201 break; 197 break;
202 198
203 case ON_DANGEROUS_DOWNLOAD_IGNORE:
204 break;
205
206 default: 199 default:
207 NOTREACHED(); 200 NOTREACHED();
208 } 201 }
209 } 202 }
210 203
211 if (download_finished_states_.find(download->state()) != 204 if (download->state() == download_finished_state_) {
212 download_finished_states_.end()) {
213 DownloadInFinalState(download); 205 DownloadInFinalState(download);
214 } 206 }
215 } 207 }
216 208
217 virtual void OnDownloadOpened(DownloadItem* download) {} 209 virtual void OnDownloadOpened(DownloadItem* download) {}
218 210
219 // DownloadManager::Observer 211 // DownloadManager::Observer
220 virtual void ModelChanged() { 212 virtual void ModelChanged() {
221 // Regenerate DownloadItem observers. If there are any download items 213 // Regenerate DownloadItem observers. If there are any download items
222 // in our final state, note them in |finished_downloads_| 214 // in our final state, note them in |finished_downloads_|
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 // construction and return from wait. But some downloads may be in our 297 // construction and return from wait. But some downloads may be in our
306 // final state (and thus be entered into |finished_downloads_|) when we 298 // final state (and thus be entered into |finished_downloads_|) when we
307 // construct this class. We don't want to count those in our transition 299 // construct this class. We don't want to count those in our transition
308 // to finished. 300 // to finished.
309 int finished_downloads_at_construction_; 301 int finished_downloads_at_construction_;
310 302
311 // Whether an internal message loop has been started and must be quit upon 303 // Whether an internal message loop has been started and must be quit upon
312 // all downloads completing. 304 // all downloads completing.
313 bool waiting_; 305 bool waiting_;
314 306
315 // The states on which to consider the DownloadItem finished. 307 // The state on which to consider the DownloadItem finished.
316 StateSet download_finished_states_; 308 DownloadItem::DownloadState download_finished_state_;
317 309
318 // True if we should transition the DownloadsObserver to finished if 310 // True if we should transition the DownloadsObserver to finished if
319 // the select file dialog comes up. 311 // the select file dialog comes up.
320 bool finish_on_select_file_; 312 bool finish_on_select_file_;
321 313
322 // True if we've seen the select file dialog. 314 // True if we've seen the select file dialog.
323 bool select_file_dialog_seen_; 315 bool select_file_dialog_seen_;
324 316
325 // Action to take if a dangerous download is encountered. 317 // Action to take if a dangerous download is encountered.
326 DangerousDownloadAction dangerous_download_action_; 318 DangerousDownloadAction dangerous_download_action_;
327 319
328 // Holds the download ids which were dangerous. 320 // Holds the download ids which were dangerous.
329 std::set<int32> dangerous_downloads_seen_; 321 std::set<int32> dangerous_downloads_seen_;
330 322
331 DISALLOW_COPY_AND_ASSIGN(DownloadsObserver); 323 DISALLOW_COPY_AND_ASSIGN(DownloadsObserver);
332 }; 324 };
333 325
334 // Ping through an arbitrary set of threads. Must be run from the UI 326 // WaitForFlush() returns after:
335 // thread. 327 // * There are no IN_PROGRESS download items remaining on the
336 class ThreadPinger : public base::RefCountedThreadSafe<ThreadPinger> { 328 // DownloadManager.
329 // * There have been two round trip messages through the file and
330 // IO threads.
331 // This almost certainly means that a Download cancel has propagated through
332 // the system.
333 class DownloadsFlushObserver
334 : public DownloadManager::Observer,
335 public DownloadItem::Observer,
336 public base::RefCountedThreadSafe<DownloadsFlushObserver> {
337 public: 337 public:
338 ThreadPinger(const BrowserThread::ID ids[], size_t num_ids) : 338 explicit DownloadsFlushObserver(DownloadManager* download_manager)
339 next_thread_index_(0u), 339 : download_manager_(download_manager),
340 ids_(ids, ids + num_ids) {} 340 waiting_for_zero_inprogress_(true) {}
341 341
342 void Ping() { 342 void WaitForFlush() {
343 if (ids_.size() == 0) 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
344 return; 344 download_manager_->AddObserver(this);
345 NextThread();
346 ui_test_utils::RunMessageLoop(); 345 ui_test_utils::RunMessageLoop();
347 } 346 }
348 347
349 private: 348 // DownloadsManager observer methods.
350 void NextThread() { 349 virtual void ModelChanged() {
351 if (next_thread_index_ == ids_.size()) { 350 // Model has changed, so there may be more DownloadItems to observe.
352 BrowserThread::PostTask( 351 CheckDownloadsInProgress(true);
353 BrowserThread::UI, FROM_HERE, new MessageLoop::QuitTask()); 352 }
354 } else { 353
355 BrowserThread::ID next_id(ids_[next_thread_index_++]); 354 // DownloadItem observer methods.
356 BrowserThread::PostTask( 355 virtual void OnDownloadUpdated(DownloadItem* download) {
357 next_id, FROM_HERE, 356 // No change in DownloadItem set on manager.
358 NewRunnableMethod(this, &ThreadPinger::NextThread)); 357 CheckDownloadsInProgress(false);
358 }
359 virtual void OnDownloadOpened(DownloadItem* download) {}
360
361 protected:
362 friend class base::RefCountedThreadSafe<DownloadsFlushObserver>;
363
364 virtual ~DownloadsFlushObserver() {
365 download_manager_->RemoveObserver(this);
366 for (std::set<DownloadItem*>::iterator it = downloads_observed_.begin();
367 it != downloads_observed_.end(); ++it) {
368 (*it)->RemoveObserver(this);
359 } 369 }
360 } 370 }
361 371
362 size_t next_thread_index_; 372 private:
363 std::vector<BrowserThread::ID> ids_; 373 // If we're waiting for that flush point, check the number
374 // of downloads in the IN_PROGRESS state and take appropriate
375 // action. If requested, also observes all downloads while iterating.
376 void CheckDownloadsInProgress(bool observe_downloads) {
377 if (waiting_for_zero_inprogress_) {
378 int count = 0;
379
380 std::vector<DownloadItem*> downloads;
381 download_manager_->SearchDownloads(string16(), &downloads);
382 std::vector<DownloadItem*>::iterator it = downloads.begin();
383 for (; it != downloads.end(); ++it) {
384 if ((*it)->state() == DownloadItem::IN_PROGRESS)
385 count++;
386 if (observe_downloads) {
387 if (downloads_observed_.find(*it) == downloads_observed_.end()) {
388 (*it)->AddObserver(this);
389 }
390 // Download items are forever, and we don't want to make
391 // assumptions about future state transitions, so once we
392 // start observing them, we don't stop until destruction.
393 }
394 }
395
396 if (count == 0) {
397 waiting_for_zero_inprogress_ = false;
398 // Stop observing DownloadItems. We maintain the observation
399 // of DownloadManager so that we don't have to independently track
400 // whether we are observing it for conditional destruction.
401 for (std::set<DownloadItem*>::iterator it = downloads_observed_.begin();
402 it != downloads_observed_.end(); ++it) {
403 (*it)->RemoveObserver(this);
404 }
405 downloads_observed_.clear();
406
407 // Trigger next step. We need to go past the IO thread twice, as
408 // there's a self-task posting in the IO thread cancel path.
409 BrowserThread::PostTask(
410 BrowserThread::FILE, FROM_HERE,
411 NewRunnableMethod(this,
412 &DownloadsFlushObserver::PingFileThread, 2));
413 }
414 }
415 }
416
417 void PingFileThread(int cycle) {
418 BrowserThread::PostTask(
419 BrowserThread::IO, FROM_HERE,
420 NewRunnableMethod(this, &DownloadsFlushObserver::PingIOThread,
421 cycle));
422 }
423
424 void PingIOThread(int cycle) {
425 if (--cycle) {
426 BrowserThread::PostTask(
427 BrowserThread::UI, FROM_HERE,
428 NewRunnableMethod(this, &DownloadsFlushObserver::PingFileThread,
429 cycle));
430 } else {
431 BrowserThread::PostTask(
432 BrowserThread::UI, FROM_HERE, new MessageLoop::QuitTask());
433 }
434 }
435
436 DownloadManager* download_manager_;
437 std::set<DownloadItem*> downloads_observed_;
438 bool waiting_for_zero_inprogress_;
439
440 DISALLOW_COPY_AND_ASSIGN(DownloadsFlushObserver);
364 }; 441 };
365 442
366 // Collect the information from FILE and IO threads needed for the Cancel 443 // Collect the information from FILE and IO threads needed for the Cancel
367 // Test, specifically the number of outstanding requests on the 444 // Test, specifically the number of outstanding requests on the
368 // ResourceDispatcherHost and the number of pending downloads on the 445 // ResourceDispatcherHost and the number of pending downloads on the
369 // DownloadFileManager. 446 // DownloadFileManager.
370 class CancelTestDataCollector 447 class CancelTestDataCollector
371 : public base::RefCountedThreadSafe<CancelTestDataCollector> { 448 : public base::RefCountedThreadSafe<CancelTestDataCollector> {
372 public: 449 public:
373 CancelTestDataCollector() 450 CancelTestDataCollector()
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 } 501 }
425 502
426 virtual void ChooseDownloadPath(TabContents* tab_contents, 503 virtual void ChooseDownloadPath(TabContents* tab_contents,
427 const FilePath& suggested_path, 504 const FilePath& suggested_path,
428 void* data) OVERRIDE { 505 void* data) OVERRIDE {
429 if (download_manager_) 506 if (download_manager_)
430 download_manager_->FileSelected(suggested_path, data); 507 download_manager_->FileSelected(suggested_path, data);
431 } 508 }
432 }; 509 };
433 510
434 // Don't respond to ChooseDownloadPath until a cancel is requested
435 // out of band. Can handle only one outstanding request at a time
436 // for a download path.
437 class BlockCancelFileDelegate : public ChromeDownloadManagerDelegate {
438 public:
439 explicit BlockCancelFileDelegate(Profile* profile)
440 : ChromeDownloadManagerDelegate(profile),
441 choose_download_path_called_(false),
442 choose_download_path_data_(NULL) {
443 SetDownloadManager(profile->GetDownloadManager());
444 }
445
446 virtual void ChooseDownloadPath(TabContents* tab_contents,
447 const FilePath& suggested_path,
448 void* data) OVERRIDE {
449 CHECK(!choose_download_path_called_);
450 choose_download_path_called_ = true;
451 choose_download_path_data_ = data;
452 }
453
454 void CancelOutstandingDownloadPathRequest() {
455 if (choose_download_path_called_) {
456 if (download_manager_)
457 download_manager_->FileSelectionCanceled(choose_download_path_data_);
458 choose_download_path_called_ = false;
459 choose_download_path_data_ = NULL;
460 }
461 }
462 private:
463 bool choose_download_path_called_;
464 void *choose_download_path_data_;
465 };
466
467 // Get History Information. 511 // Get History Information.
468 class DownloadsHistoryDataCollector { 512 class DownloadsHistoryDataCollector {
469 public: 513 public:
470 DownloadsHistoryDataCollector(int64 download_db_handle, 514 DownloadsHistoryDataCollector(int64 download_db_handle,
471 DownloadManager* manager) 515 DownloadManager* manager)
472 : result_valid_(false), 516 : result_valid_(false),
473 download_db_handle_(download_db_handle) { 517 download_db_handle_(download_db_handle) {
474 HistoryService* hs = 518 HistoryService* hs =
475 Profile::FromBrowserContext(manager->browser_context())-> 519 Profile::FromBrowserContext(manager->browser_context())->
476 GetHistoryService(Profile::EXPLICIT_ACCESS); 520 GetHistoryService(Profile::EXPLICIT_ACCESS);
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 (*it)->TestMockDownloadOpen(); 618 (*it)->TestMockDownloadOpen();
575 } 619 }
576 } 620 }
577 621
578 private: 622 private:
579 DownloadManager* download_manager_; 623 DownloadManager* download_manager_;
580 624
581 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver); 625 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
582 }; 626 };
583 627
584 static const DownloadItem::DownloadState kTerminalStates[] = {
585 DownloadItem::CANCELLED,
586 DownloadItem::INTERRUPTED,
587 DownloadItem::COMPLETE,
588 };
589
590 static const DownloadItem::DownloadState kInProgressStates[] = {
591 DownloadItem::IN_PROGRESS,
592 };
593
594 static const BrowserThread::ID kIOFileX2ThreadList[] = {
595 BrowserThread::IO, BrowserThread::FILE,
596 BrowserThread::IO, BrowserThread::FILE };
597
598 static const BrowserThread::ID kExternalTerminationThreadList[] = {
599 BrowserThread::IO, BrowserThread::IO, BrowserThread::FILE };
600
601 // Not in anonymous namespace so that friend class from DownloadManager
602 // can target it.
603 class DownloadTest : public InProcessBrowserTest { 628 class DownloadTest : public InProcessBrowserTest {
604 public: 629 public:
605 enum SelectExpectation { 630 enum SelectExpectation {
606 EXPECT_NO_SELECT_DIALOG = -1, 631 EXPECT_NO_SELECT_DIALOG = -1,
607 EXPECT_NOTHING, 632 EXPECT_NOTHING,
608 EXPECT_SELECT_DIALOG 633 EXPECT_SELECT_DIALOG
609 }; 634 };
610 635
611 DownloadTest() { 636 DownloadTest() {
612 EnableDOMAutomation(); 637 EnableDOMAutomation();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 if (!downloads_directory_.CreateUniqueTempDir()) 700 if (!downloads_directory_.CreateUniqueTempDir())
676 return false; 701 return false;
677 702
678 browser->profile()->GetPrefs()->SetFilePath( 703 browser->profile()->GetPrefs()->SetFilePath(
679 prefs::kDownloadDefaultDirectory, 704 prefs::kDownloadDefaultDirectory,
680 downloads_directory_.path()); 705 downloads_directory_.path());
681 706
682 return true; 707 return true;
683 } 708 }
684 709
685 // For tests that want to test system reaction to files
686 // going away underneath them.
687 void DeleteDownloadsDirectory() {
688 EXPECT_TRUE(downloads_directory_.Delete());
689 }
690
691 DownloadPrefs* GetDownloadPrefs(Browser* browser) { 710 DownloadPrefs* GetDownloadPrefs(Browser* browser) {
692 return DownloadPrefs::FromDownloadManager( 711 return DownloadPrefs::FromDownloadManager(
693 browser->profile()->GetDownloadManager()); 712 browser->profile()->GetDownloadManager());
694 } 713 }
695 714
696 FilePath GetDownloadDirectory(Browser* browser) { 715 FilePath GetDownloadDirectory(Browser* browser) {
697 return GetDownloadPrefs(browser)->download_path(); 716 return GetDownloadPrefs(browser)->download_path();
698 } 717 }
699 718
700 // Create a DownloadsObserver that will wait for the 719 // Create a DownloadsObserver that will wait for the
701 // specified number of downloads to finish. 720 // specified number of downloads to finish.
702 DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) { 721 DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) {
703 DownloadManager* download_manager = 722 DownloadManager* download_manager =
704 browser->profile()->GetDownloadManager(); 723 browser->profile()->GetDownloadManager();
705 return new DownloadsObserver( 724 return new DownloadsObserver(
706 download_manager, num_downloads, 725 download_manager, num_downloads,
707 DownloadsObserver::StateSet( 726 DownloadItem::COMPLETE, // Really done
708 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
709 true, // Bail on select file 727 true, // Bail on select file
710 ON_DANGEROUS_DOWNLOAD_FAIL); 728 ON_DANGEROUS_DOWNLOAD_FAIL);
711 } 729 }
712 730
713 // Create a DownloadsObserver that will wait for the 731 // Create a DownloadsObserver that will wait for the
714 // specified number of downloads to finish, and is
715 // ok with dangerous downloads. Note that use of this
716 // waiter is conditional on accepting the dangerous download.
717 DownloadsObserver* CreateDangerousWaiter(
718 Browser* browser, int num_downloads) {
719 DownloadManager* download_manager =
720 browser->profile()->GetDownloadManager();
721 return new DownloadsObserver(
722 download_manager, num_downloads,
723 DownloadsObserver::StateSet(
724 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
725 true, // Bail on select file
726 ON_DANGEROUS_DOWNLOAD_IGNORE);
727 }
728
729 // Create a DownloadsObserver that will wait for the
730 // specified number of downloads to start. 732 // specified number of downloads to start.
731 DownloadsObserver* CreateInProgressWaiter(Browser* browser, 733 DownloadsObserver* CreateInProgressWaiter(Browser* browser,
732 int num_downloads) { 734 int num_downloads) {
733 DownloadManager* download_manager = 735 DownloadManager* download_manager =
734 browser->profile()->GetDownloadManager(); 736 browser->profile()->GetDownloadManager();
735 return new DownloadsObserver( 737 return new DownloadsObserver(
736 download_manager, num_downloads, 738 download_manager, num_downloads,
737 DownloadsObserver::StateSet( 739 DownloadItem::IN_PROGRESS, // Has started
738 kInProgressStates,
739 kInProgressStates + arraysize(kInProgressStates)),
740 true, // Bail on select file 740 true, // Bail on select file
741 ON_DANGEROUS_DOWNLOAD_IGNORE); 741 ON_DANGEROUS_DOWNLOAD_FAIL);
742 } 742 }
743 743
744 // Create a DownloadsObserver that will wait for the 744 // Create a DownloadsObserver that will wait for the
745 // specified number of downloads to finish, or for 745 // specified number of downloads to finish, or for
746 // a dangerous download warning to be shown. 746 // a dangerous download warning to be shown.
747 DownloadsObserver* DangerousInstallWaiter( 747 DownloadsObserver* DangerousInstallWaiter(
748 Browser* browser, 748 Browser* browser,
749 int num_downloads, 749 int num_downloads,
750 DownloadItem::DownloadState final_state, 750 DownloadItem::DownloadState final_state,
751 DangerousDownloadAction dangerous_download_action) { 751 DangerousDownloadAction dangerous_download_action) {
752 DownloadsObserver::StateSet states;
753 states.insert(final_state);
754 DownloadManager* download_manager = 752 DownloadManager* download_manager =
755 browser->profile()->GetDownloadManager(); 753 browser->profile()->GetDownloadManager();
756 return new DownloadsObserver( 754 return new DownloadsObserver(
757 download_manager, num_downloads, 755 download_manager, num_downloads,
758 states, 756 final_state,
759 true, // Bail on select file 757 true, // Bail on select file
760 dangerous_download_action); 758 dangerous_download_action);
761 } 759 }
762 760
763 // Download |url|, then wait for the download to finish. 761 // Download |url|, then wait for the download to finish.
764 // |disposition| indicates where the navigation occurs (current tab, new 762 // |disposition| indicates where the navigation occurs (current tab, new
765 // foreground tab, etc). 763 // foreground tab, etc).
766 // |expectation| indicates whether or not a Select File dialog should be 764 // |expectation| indicates whether or not a Select File dialog should be
767 // open when the download is finished, or if we don't care. 765 // open when the download is finished, or if we don't care.
768 // If the dialog appears, the routine exits. The only effect |expectation| 766 // If the dialog appears, the routine exits. The only effect |expectation|
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 if (!downloaded_path_exists) 901 if (!downloaded_path_exists)
904 return false; 902 return false;
905 903
906 // Delete the file we just downloaded. 904 // Delete the file we just downloaded.
907 EXPECT_TRUE(file_util::DieFileDie(download_path, true)); 905 EXPECT_TRUE(file_util::DieFileDie(download_path, true));
908 EXPECT_FALSE(file_util::PathExists(download_path)); 906 EXPECT_FALSE(file_util::PathExists(download_path));
909 907
910 return true; 908 return true;
911 } 909 }
912 910
913 void GetPersistentDownloads(Browser* browser, 911 void GetDownloads(Browser* browser, std::vector<DownloadItem*>* downloads) {
914 std::vector<DownloadItem*>* downloads) {
915 DCHECK(downloads); 912 DCHECK(downloads);
916 downloads->clear();
917 DownloadManager* manager = browser->profile()->GetDownloadManager(); 913 DownloadManager* manager = browser->profile()->GetDownloadManager();
918 manager->SearchDownloads(string16(), downloads); 914 manager->SearchDownloads(string16(), downloads);
919 } 915 }
920 916
921 void GetInProgressDownloads(Browser* browser,
922 std::vector<DownloadItem*>* downloads) {
923 downloads->clear();
924 DCHECK(downloads);
925 DownloadManager* manager = browser->profile()->GetDownloadManager();
926 manager->GetInProgressDownloads(downloads);
927 }
928
929 size_t NumberInProgressDownloads(Browser* browser) {
930 std::vector<DownloadItem*> downloads;
931 browser->profile()->GetDownloadManager()->GetInProgressDownloads(
932 &downloads);
933 return downloads.size();
934 }
935
936 void WaitForIOFileX2() {
937 scoped_refptr<ThreadPinger> pinger(
938 new ThreadPinger(kIOFileX2ThreadList, arraysize(kIOFileX2ThreadList)));
939 pinger->Ping();
940 }
941
942 // Check that the download UI (shelf on non-chromeos or panel on chromeos) 917 // Check that the download UI (shelf on non-chromeos or panel on chromeos)
943 // is visible or not as expected. Additionally, check that the filename 918 // is visible or not as expected. Additionally, check that the filename
944 // is present in the UI (currently only on chromeos). 919 // is present in the UI (currently only on chromeos).
945 void CheckDownloadUI(Browser* browser, bool expected_non_cros, 920 void CheckDownloadUI(Browser* browser, bool expected_non_cros,
946 bool expected_cros, const FilePath& filename) { 921 bool expected_cros, const FilePath& filename) {
947 #if defined(OS_CHROMEOS) 922 #if defined(OS_CHROMEOS)
948 #if defined(TOUCH_UI) 923 #if defined(TOUCH_UI)
949 TabContents* download_contents = ActiveDownloadsUI::GetPopup(NULL); 924 TabContents* download_contents = ActiveDownloadsUI::GetPopup(NULL);
950 EXPECT_EQ(expected_cros, download_contents != NULL); 925 EXPECT_EQ(expected_cros, download_contents != NULL);
951 if (!download_contents || filename.empty()) 926 if (!download_contents || filename.empty())
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 974
1000 DownloadManager* manager = browser->profile()->GetDownloadManager(); 975 DownloadManager* manager = browser->profile()->GetDownloadManager();
1001 976
1002 new_delegate->SetDownloadManager(manager); 977 new_delegate->SetDownloadManager(manager);
1003 manager->set_delegate(new_delegate); 978 manager->set_delegate(new_delegate);
1004 979
1005 // Gives ownership to Profile. 980 // Gives ownership to Profile.
1006 browser->profile()->SetDownloadManagerDelegate(new_delegate); 981 browser->profile()->SetDownloadManagerDelegate(new_delegate);
1007 } 982 }
1008 983
1009 // Arrange for select file calls on the given browser from the
1010 // download manager to block until explicitly released.
1011 // Note that the returned pointer is non-owning; the lifetime
1012 // of the object will be that of the profile.
1013 BlockCancelFileDelegate* BlockSelectFile(Browser* browser) {
1014 BlockCancelFileDelegate* new_delegate =
1015 new BlockCancelFileDelegate(browser->profile());
1016
1017 DownloadManager* manager = browser->profile()->GetDownloadManager();
1018
1019 new_delegate->SetDownloadManager(manager);
1020 manager->set_delegate(new_delegate);
1021
1022 // Gives ownership to Profile.
1023 browser->profile()->SetDownloadManagerDelegate(new_delegate);
1024
1025 return new_delegate;
1026 }
1027
1028 // Wait for an external termination (e.g. signalling
1029 // URLRequestSlowDownloadJob to return an error) to propagate through
1030 // this sytem. This involves hopping over to the IO thread (twice,
1031 // because of possible self-posts from URLRequestJob) then
1032 // chasing the (presumed) notification through the download
1033 // system (FILE->UI).
1034 void WaitForExternalTermination() {
1035 scoped_refptr<ThreadPinger> pinger(
1036 new ThreadPinger(
1037 kExternalTerminationThreadList,
1038 arraysize(kExternalTerminationThreadList)));
1039 pinger->Ping();
1040 }
1041
1042 private: 984 private:
1043 // Location of the test data. 985 // Location of the test data.
1044 FilePath test_dir_; 986 FilePath test_dir_;
1045 987
1046 // Location of the downloads directory for these tests 988 // Location of the downloads directory for these tests
1047 ScopedTempDir downloads_directory_; 989 ScopedTempDir downloads_directory_;
1048 }; 990 };
1049 991
1050 // NOTES: 992 // NOTES:
1051 // 993 //
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1040 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1099 1041
1100 NullSelectFile(browser()); 1042 NullSelectFile(browser());
1101 1043
1102 // Download the file and wait. We expect the Select File dialog to appear 1044 // Download the file and wait. We expect the Select File dialog to appear
1103 // due to the MIME type, but we still wait until the download completes. 1045 // due to the MIME type, but we still wait until the download completes.
1104 scoped_ptr<DownloadsObserver> observer( 1046 scoped_ptr<DownloadsObserver> observer(
1105 new DownloadsObserver( 1047 new DownloadsObserver(
1106 browser()->profile()->GetDownloadManager(), 1048 browser()->profile()->GetDownloadManager(),
1107 1, 1049 1,
1108 DownloadsObserver::StateSet( 1050 DownloadItem::COMPLETE, // Really done
1109 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
1110 false, // Continue on select file. 1051 false, // Continue on select file.
1111 ON_DANGEROUS_DOWNLOAD_FAIL)); 1052 ON_DANGEROUS_DOWNLOAD_FAIL));
1112 ui_test_utils::NavigateToURLWithDisposition( 1053 ui_test_utils::NavigateToURLWithDisposition(
1113 browser(), url, CURRENT_TAB, 1054 browser(), url, CURRENT_TAB,
1114 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 1055 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1115 observer->WaitForFinished(); 1056 observer->WaitForFinished();
1116 EXPECT_TRUE(observer->select_file_dialog_seen()); 1057 EXPECT_TRUE(observer->select_file_dialog_seen());
1117 1058
1059 // Check state.
1118 EXPECT_EQ(1, browser()->tab_count()); 1060 EXPECT_EQ(1, browser()->tab_count());
1119 CheckDownload(browser(), file, file); 1061 CheckDownload(browser(), file, file);
1120 CheckDownloadUI(browser(), true, true, file); 1062 CheckDownloadUI(browser(), true, true, file);
1121 } 1063 }
1122 1064
1123 // Put up a Select File dialog when the file is downloaded, due to
1124 // prompt_for_download==true argument to InitialSetup().
1125 // Confirm that we can cancel the download in that state.
1126 IN_PROC_BROWSER_TEST_F(DownloadTest, CancelAtFileSelection) {
1127 ASSERT_TRUE(InitialSetup(true));
1128 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1129 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1130
1131 BlockCancelFileDelegate* delegate = BlockSelectFile(browser());
1132
1133 // Download the file and wait. We expect the Select File dialog to appear.
1134 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1135
1136 std::vector<DownloadItem*> active_downloads, history_downloads;
1137 GetInProgressDownloads(browser(), &active_downloads);
1138 ASSERT_EQ(1u, active_downloads.size());
1139 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1140 GetPersistentDownloads(browser(), &history_downloads);
1141 EXPECT_EQ(0u, history_downloads.size());
1142
1143 // This should remove the download as it hasn't yet been entered into
1144 // the history.
1145 active_downloads[0]->Cancel();
1146 MessageLoopForUI::current()->RunAllPending();
1147
1148 GetInProgressDownloads(browser(), &active_downloads);
1149 EXPECT_EQ(0u, active_downloads.size());
1150 GetPersistentDownloads(browser(), &history_downloads);
1151 EXPECT_EQ(0u, history_downloads.size());
1152
1153 // Check state.
1154 EXPECT_EQ(1, browser()->tab_count());
1155 // Since we exited while the Select File dialog was visible, there should not
1156 // be anything in the download shelf and so it should not be visible.
1157 CheckDownloadUI(browser(), false, false, FilePath());
1158
1159 // Return from file selection to release allocated data.
1160 delegate->CancelOutstandingDownloadPathRequest();
1161 }
1162
1163 // Put up a Select File dialog when the file is downloaded, due to
1164 // prompt_for_download==true argument to InitialSetup().
1165 // Confirm that we can cancel the download as if the user had hit cancel.
1166 IN_PROC_BROWSER_TEST_F(DownloadTest, CancelFromFileSelection) {
1167 ASSERT_TRUE(InitialSetup(true));
1168 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1169 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1170
1171 BlockCancelFileDelegate* delegate = BlockSelectFile(browser());
1172
1173 // Download the file and wait. We expect the Select File dialog to appear.
1174 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1175
1176 std::vector<DownloadItem*> active_downloads, history_downloads;
1177 GetInProgressDownloads(browser(), &active_downloads);
1178 ASSERT_EQ(1u, active_downloads.size());
1179 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1180 GetPersistentDownloads(browser(), &history_downloads);
1181 EXPECT_EQ(0u, history_downloads.size());
1182
1183 // (Effectively) Click the Cancel button. This should remove the download
1184 // as it hasn't yet been entered into the history.
1185 delegate->CancelOutstandingDownloadPathRequest();
1186 MessageLoopForUI::current()->RunAllPending();
1187
1188 GetInProgressDownloads(browser(), &active_downloads);
1189 EXPECT_EQ(0u, active_downloads.size());
1190 GetPersistentDownloads(browser(), &history_downloads);
1191 EXPECT_EQ(0u, history_downloads.size());
1192
1193 // Check state.
1194 EXPECT_EQ(1, browser()->tab_count());
1195 // Since we exited while the Select File dialog was visible, there should not
1196 // be anything in the download shelf and so it should not be visible.
1197 CheckDownloadUI(browser(), false, false, FilePath());
1198 }
1199
1200 // Put up a Select File dialog when the file is downloaded, due to
1201 // prompt_for_download==true argument to InitialSetup().
1202 // Confirm that we can remove the download in that state.
1203 IN_PROC_BROWSER_TEST_F(DownloadTest, RemoveFromFileSelection) {
1204 ASSERT_TRUE(InitialSetup(true));
1205 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1206 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1207
1208 BlockCancelFileDelegate* delegate = BlockSelectFile(browser());
1209
1210 // Download the file and wait. We expect the Select File dialog to appear.
1211 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1212
1213 std::vector<DownloadItem*> active_downloads, history_downloads;
1214 GetInProgressDownloads(browser(), &active_downloads);
1215 ASSERT_EQ(1u, active_downloads.size());
1216 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1217 GetPersistentDownloads(browser(), &history_downloads);
1218 EXPECT_EQ(0u, history_downloads.size());
1219
1220 // Confirm the file can be successfully removed from the select file
1221 // dialog blocked state.
1222 active_downloads[0]->Remove();
1223 MessageLoopForUI::current()->RunAllPending();
1224
1225 GetInProgressDownloads(browser(), &active_downloads);
1226 EXPECT_EQ(0u, active_downloads.size());
1227 GetPersistentDownloads(browser(), &history_downloads);
1228 EXPECT_EQ(0u, history_downloads.size());
1229
1230 EXPECT_EQ(1, browser()->tab_count());
1231 // Since we exited while the Select File dialog was visible, there should not
1232 // be anything in the download shelf and so it should not be visible.
1233 CheckDownloadUI(browser(), false, false, FilePath());
1234
1235 // Return from file selection to release allocated data.
1236 delegate->CancelOutstandingDownloadPathRequest();
1237 }
1238
1239 // Put up a Select File dialog when the file is downloaded, due to
1240 // prompt_for_download==true argument to InitialSetup().
1241 // Confirm that an error coming in from the network works properly
1242 // when in that state.
1243 IN_PROC_BROWSER_TEST_F(DownloadTest, InterruptFromFileSelection) {
1244 ASSERT_TRUE(InitialSetup(true));
1245 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
1246
1247 BlockCancelFileDelegate* delegate = BlockSelectFile(browser());
1248
1249 // Download the file and wait. We expect the Select File dialog to appear.
1250 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1251
1252 std::vector<DownloadItem*> active_downloads, history_downloads;
1253 GetInProgressDownloads(browser(), &active_downloads);
1254 ASSERT_EQ(1u, active_downloads.size());
1255 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1256 GetPersistentDownloads(browser(), &history_downloads);
1257 EXPECT_EQ(0u, history_downloads.size());
1258
1259 // Complete the download with error.
1260 GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
1261 ui_test_utils::NavigateToURL(browser(), error_url);
1262 MessageLoopForUI::current()->RunAllPending();
1263 WaitForExternalTermination();
1264
1265 // Confirm that a download error before entry into history
1266 // deletes the download.
1267 GetInProgressDownloads(browser(), &active_downloads);
1268 EXPECT_EQ(0u, active_downloads.size());
1269 GetPersistentDownloads(browser(), &history_downloads);
1270 EXPECT_EQ(0u, history_downloads.size());
1271
1272 // Since we exited while the Select File dialog was visible, there should not
1273 // be anything in the download shelf and so it should not be visible.
1274 CheckDownloadUI(browser(), false, false, FilePath());
1275
1276 // Return from file selection to release allocated data.
1277 delegate->CancelOutstandingDownloadPathRequest();
1278 }
1279
1280 // Access a file with a viewable mime-type, verify that a download 1065 // Access a file with a viewable mime-type, verify that a download
1281 // did not initiate. 1066 // did not initiate.
1282 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) { 1067 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) {
1283 ASSERT_TRUE(InitialSetup(false)); 1068 ASSERT_TRUE(InitialSetup(false));
1284 FilePath file(FILE_PATH_LITERAL("download-test2.html")); 1069 FilePath file(FILE_PATH_LITERAL("download-test2.html"));
1285 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1070 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1286 FilePath file_path(DestinationFile(browser(), file)); 1071 FilePath file_path(DestinationFile(browser(), file));
1287 1072
1288 // Open a web page and wait. 1073 // Open a web page and wait.
1289 ui_test_utils::NavigateToURL(browser(), url); 1074 ui_test_utils::NavigateToURL(browser(), url);
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
1669 ExpectWindowCountAfterDownload(1); 1454 ExpectWindowCountAfterDownload(1);
1670 #endif 1455 #endif
1671 1456
1672 EXPECT_EQ(1, browser()->tab_count()); 1457 EXPECT_EQ(1, browser()->tab_count());
1673 // Download shelf should close. Download panel stays open on ChromeOS. 1458 // Download shelf should close. Download panel stays open on ChromeOS.
1674 CheckDownloadUI(browser(), false, true, file); 1459 CheckDownloadUI(browser(), false, true, file);
1675 1460
1676 CheckDownload(browser(), file, file); 1461 CheckDownload(browser(), file, file);
1677 } 1462 }
1678 1463
1679 // Create a download, wait until it's visible on the DownloadManager,
1680 // then cancel it.
1681 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadCancelled) { 1464 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadCancelled) {
1682 ASSERT_TRUE(InitialSetup(false)); 1465 ASSERT_TRUE(InitialSetup(false));
1683 EXPECT_EQ(1, browser()->tab_count()); 1466 EXPECT_EQ(1, browser()->tab_count());
1684 1467
1685 // The code below is slightly fragile. Currently, 1468 // TODO(rdsmith): Fragile code warning! The code below relies on the
1686 // the DownloadsObserver will only finish when the new download has 1469 // DownloadsObserver only finishing when the new download has reached
1687 // reached the state of being entered into the history and being 1470 // the state of being entered into the history and being user-visible
1688 // user-visible. However, by the pure semantics of 1471 // (that's what's required for the Remove to be valid and for the
1472 // download shelf to be visible). By the pure semantics of
1689 // DownloadsObserver, that's not guaranteed; DownloadItems are created 1473 // DownloadsObserver, that's not guaranteed; DownloadItems are created
1690 // in the IN_PROGRESS state and made known to the DownloadManager 1474 // in the IN_PROGRESS state and made known to the DownloadManager
1691 // immediately, so any ModelChanged event on the DownloadManager after 1475 // immediately, so any ModelChanged event on the DownloadManager after
1692 // navigation would allow the observer to return. At some point we'll 1476 // navigation would allow the observer to return. However, the only
1693 // probably change the DownloadManager to fire a ModelChanged event 1477 // ModelChanged() event the code will currently fire is in
1694 // earlier in download processing. This test should continue to work, 1478 // OnCreateDownloadEntryComplete, at which point the download item will
1695 // as a cancel is valid at any point during download process. However, 1479 // be in the state we need.
1696 // at that point it'll be testing two different things, depending on 1480 // The right way to fix this is to create finer grained states on the
1697 // what state the download item has reached when it is cancelled: 1481 // DownloadItem, and wait for the state that indicates the item has been
1698 // a) cancelling from a pre-history entry when the only record of a 1482 // entered in the history and made visible in the UI.
1699 // download item is on the active_downloads_ queue, and b) cancelling
1700 // from a post-history entry when the download is present on the
1701 // history_downloads_ list.
1702 //
1703 // Note that the above is why we don't do any UI testing here--we don't
1704 // know whether or not the download item has been made visible to the user.
1705
1706 // TODO(rdsmith): After we fire ModelChanged events at finer granularity,
1707 // split this test into subtests that tests canceling from each separate
1708 // download item state. Also include UI testing as appropriate in those
1709 // split tests.
1710 1483
1711 // Create a download, wait until it's started, and confirm 1484 // Create a download, wait until it's started, and confirm
1712 // we're in the expected state. 1485 // we're in the expected state.
1713 scoped_ptr<DownloadsObserver> observer( 1486 scoped_ptr<DownloadsObserver> observer(
1714 CreateInProgressWaiter(browser(), 1)); 1487 CreateInProgressWaiter(browser(), 1));
1715 ui_test_utils::NavigateToURL( 1488 ui_test_utils::NavigateToURL(
1716 browser(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); 1489 browser(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl));
1717 observer->WaitForFinished(); 1490 observer->WaitForFinished();
1718 1491
1719 std::vector<DownloadItem*> downloads; 1492 std::vector<DownloadItem*> downloads;
1720 GetPersistentDownloads(browser(), &downloads); 1493 browser()->profile()->GetDownloadManager()->SearchDownloads(
1494 string16(), &downloads);
1721 ASSERT_EQ(1u, downloads.size()); 1495 ASSERT_EQ(1u, downloads.size());
1722 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state()); 1496 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state());
1497 CheckDownloadUI(browser(), true, true, FilePath());
1723 1498
1724 // Cancel the download and wait for download system quiesce. 1499 // Cancel the download and wait for download system quiesce.
1725 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 1500 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
1726 ASSERT_EQ(0u, NumberInProgressDownloads(browser())); 1501 scoped_refptr<DownloadsFlushObserver> flush_observer(
1727 WaitForIOFileX2(); 1502 new DownloadsFlushObserver(browser()->profile()->GetDownloadManager()));
1503 flush_observer->WaitForFlush();
1728 1504
1729 // Get the important info from other threads and check it. 1505 // Get the important info from other threads and check it.
1730 scoped_refptr<CancelTestDataCollector> info(new CancelTestDataCollector()); 1506 scoped_refptr<CancelTestDataCollector> info(new CancelTestDataCollector());
1731 info->WaitForDataCollected(); 1507 info->WaitForDataCollected();
1732 EXPECT_EQ(0, info->rdh_pending_requests()); 1508 EXPECT_EQ(0, info->rdh_pending_requests());
1733 EXPECT_EQ(0, info->dfm_pending_downloads()); 1509 EXPECT_EQ(0, info->dfm_pending_downloads());
1734 }
1735 1510
1736 // Do a dangerous download and confirm that the download does 1511 // Using "DownloadItem::Remove" follows the discard dangerous download path,
1737 // not complete until user accept, and that all states are 1512 // which completely removes the browser from the shelf and closes the shelf
1738 // correct along the way. 1513 // if it was there. Download panel stays open on ChromeOS.
1739 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerous) {
1740 ASSERT_TRUE(InitialSetup(false));
1741 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1742 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1743
1744 EXPECT_EQ(1, browser()->tab_count());
1745
1746 scoped_ptr<DownloadsObserver> observer(
1747 CreateInProgressWaiter(browser(), 1));
1748 ui_test_utils::NavigateToURL(browser(), url);
1749 observer->WaitForFinished();
1750
1751 // We should have one download, in history, and it should
1752 // still be dangerous.
1753 std::vector<DownloadItem*> downloads;
1754 GetPersistentDownloads(browser(), &downloads);
1755 ASSERT_EQ(1u, downloads.size());
1756 DownloadItem* download = downloads[0];
1757 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1758 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1759 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1760 // In ChromeOS, popup will be up, but file name will be unrecognizable.
1761 CheckDownloadUI(browser(), true, true, FilePath());
1762
1763 // See if accepting completes the download and changes the safety
1764 // state.
1765 scoped_ptr<DownloadsObserver> completion_observer(
1766 CreateDangerousWaiter(browser(), 1));
1767 AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
1768 download->id());
1769 completion_observer->WaitForFinished();
1770
1771 GetPersistentDownloads(browser(), &downloads);
1772 ASSERT_EQ(1u, downloads.size());
1773 ASSERT_EQ(downloads[0], download);
1774 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
1775 EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
1776 CheckDownloadUI(browser(), true, true, file);
1777 }
1778
1779 // Confirm that a dangerous download that gets a file error before
1780 // completion ends in the right state (currently cancelled because file
1781 // errors are non-resumable). Note that this is really testing
1782 // to make sure errors from the final rename are propagated properly.
1783 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousFileError) {
1784 ASSERT_TRUE(InitialSetup(false));
1785 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1786 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1787
1788 EXPECT_EQ(1, browser()->tab_count());
1789
1790 scoped_ptr<DownloadsObserver> observer(
1791 CreateInProgressWaiter(browser(), 1));
1792 ui_test_utils::NavigateToURL(browser(), url);
1793 observer->WaitForFinished();
1794
1795 // We should have one download, in history, and it should
1796 // still be dangerous.
1797 std::vector<DownloadItem*> downloads;
1798 GetPersistentDownloads(browser(), &downloads);
1799 ASSERT_EQ(1u, downloads.size());
1800 DownloadItem* download = downloads[0];
1801 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1802 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1803 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1804 // In ChromeOS, popup will be up, but file name will be unrecognizable.
1805 CheckDownloadUI(browser(), true, true, FilePath());
1806
1807 // Accept it after nuking the directory into which it's being downloaded;
1808 // that should complete the download with an error.
1809 DeleteDownloadsDirectory();
1810 scoped_ptr<DownloadsObserver> completion_observer(
1811 CreateDangerousWaiter(browser(), 1));
1812 AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
1813 download->id());
1814 completion_observer->WaitForFinished();
1815
1816 GetPersistentDownloads(browser(), &downloads);
1817 ASSERT_EQ(1u, downloads.size());
1818 ASSERT_EQ(downloads[0], download);
1819 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state());
1820 EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
1821 // In ChromeOS, popup will still be up, but the file will have been
1822 // deleted.
1823 CheckDownloadUI(browser(), true, true, FilePath());
1824 }
1825
1826 // Confirm that declining a dangerous download erases it from living memory.
1827 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousDecline) {
1828 ASSERT_TRUE(InitialSetup(false));
1829 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1830 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1831
1832 EXPECT_EQ(1, browser()->tab_count());
1833
1834 scoped_ptr<DownloadsObserver> observer(
1835 CreateInProgressWaiter(browser(), 1));
1836 ui_test_utils::NavigateToURL(browser(), url);
1837 observer->WaitForFinished();
1838
1839 // We should have one download, in history, and it should
1840 // still be dangerous.
1841 std::vector<DownloadItem*> downloads;
1842 GetPersistentDownloads(browser(), &downloads);
1843 ASSERT_EQ(1u, downloads.size());
1844 DownloadItem* download = downloads[0];
1845 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1846 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1847 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1848 CheckDownloadUI(browser(), true, true, FilePath());
1849
1850 DenyDangerousDownload(browser()->profile()->GetDownloadManager(),
1851 download->id());
1852
1853 GetPersistentDownloads(browser(), &downloads);
1854 ASSERT_EQ(0u, downloads.size());
1855 CheckDownloadUI(browser(), false, true, FilePath());
1856 }
1857
1858 // Fail a download with a network error partway through, and make sure the
1859 // state is INTERRUPTED and the error is propagated.
1860 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadInterrupted) {
1861 ASSERT_TRUE(InitialSetup(false));
1862 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
1863
1864 scoped_ptr<DownloadsObserver> observer(
1865 CreateInProgressWaiter(browser(), 1));
1866 ui_test_utils::NavigateToURL(browser(), url);
1867 observer->WaitForFinished();
1868
1869 DownloadItem* download1;
1870 std::vector<DownloadItem*> downloads;
1871 GetPersistentDownloads(browser(), &downloads);
1872 ASSERT_EQ(1u, downloads.size());
1873 download1 = downloads[0];
1874 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->state());
1875 FilePath filename;
1876 net::FileURLToFilePath(url, &filename);
1877 CheckDownloadUI(browser(), true, true,
1878 download_util::GetCrDownloadPath(filename.BaseName()));
1879
1880 // Fail the download
1881 GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
1882 ui_test_utils::NavigateToURL(browser(), error_url);
1883 MessageLoopForUI::current()->RunAllPending();
1884 WaitForExternalTermination();
1885
1886 // Should still be visible, with INTERRUPTED state.
1887 GetPersistentDownloads(browser(), &downloads);
1888 ASSERT_EQ(1u, downloads.size());
1889 DownloadItem* download = downloads[0];
1890 ASSERT_EQ(download, download1);
1891 ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
1892 // TODO(rdsmith): Confirm error provided by URLRequest is shown
1893 // in DownloadItem.
1894 CheckDownloadUI(browser(), true, true, FilePath());
1895
1896 // Confirm cancel does nothing.
1897 download->Cancel();
1898 MessageLoopForUI::current()->RunAllPending();
1899
1900 GetPersistentDownloads(browser(), &downloads);
1901 ASSERT_EQ(1u, downloads.size());
1902 ASSERT_EQ(download, downloads[0]);
1903 ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
1904 CheckDownloadUI(browser(), true, true, FilePath());
1905
1906 // Confirm remove gets rid of it.
1907 download->Remove();
1908 download = NULL;
1909 MessageLoopForUI::current()->RunAllPending();
1910
1911 GetPersistentDownloads(browser(), &downloads);
1912 ASSERT_EQ(0u, downloads.size());
1913 CheckDownloadUI(browser(), false, true, FilePath()); 1514 CheckDownloadUI(browser(), false, true, FilePath());
1914 } 1515 }
1915 1516
1916 // Confirm a download makes it into the history properly. 1517 // Confirm a download makes it into the history properly.
1917 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1518 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1918 ASSERT_TRUE(InitialSetup(false)); 1519 ASSERT_TRUE(InitialSetup(false));
1919 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1520 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1920 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1521 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1921 FilePath origin_file(OriginFile(file)); 1522 FilePath origin_file(OriginFile(file));
1922 int64 origin_size; 1523 int64 origin_size;
1923 file_util::GetFileSize(origin_file, &origin_size); 1524 file_util::GetFileSize(origin_file, &origin_size);
1924 1525
1925 // Download the file and wait. We do not expect the Select File dialog. 1526 // Download the file and wait. We do not expect the Select File dialog.
1926 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1527 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1927 1528
1928 // Get details of what downloads have just happened. 1529 // Get details of what downloads have just happened.
1929 std::vector<DownloadItem*> downloads; 1530 std::vector<DownloadItem*> downloads;
1930 GetPersistentDownloads(browser(), &downloads); 1531 GetDownloads(browser(), &downloads);
1931 ASSERT_EQ(1u, downloads.size()); 1532 ASSERT_EQ(1u, downloads.size());
1932 int64 db_handle = downloads[0]->db_handle(); 1533 int64 db_handle = downloads[0]->db_handle();
1933 1534
1934 // Check state. 1535 // Check state.
1935 EXPECT_EQ(1, browser()->tab_count()); 1536 EXPECT_EQ(1, browser()->tab_count());
1936 CheckDownload(browser(), file, file); 1537 CheckDownload(browser(), file, file);
1937 CheckDownloadUI(browser(), true, true, file); 1538 CheckDownloadUI(browser(), true, true, file);
1938 1539
1939 // Check history results. 1540 // Check history results.
1940 DownloadsHistoryDataCollector history_collector( 1541 DownloadsHistoryDataCollector history_collector(
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
2032 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file)); 1633 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file));
2033 1634
2034 // Mock out external opening on all downloads until end of test. 1635 // Mock out external opening on all downloads until end of test.
2035 MockDownloadOpeningObserver observer( 1636 MockDownloadOpeningObserver observer(
2036 browser()->profile()->GetDownloadManager()); 1637 browser()->profile()->GetDownloadManager());
2037 1638
2038 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1639 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
2039 1640
2040 // Find the download and confirm it was opened. 1641 // Find the download and confirm it was opened.
2041 std::vector<DownloadItem*> downloads; 1642 std::vector<DownloadItem*> downloads;
2042 GetPersistentDownloads(browser(), &downloads); 1643 browser()->profile()->GetDownloadManager()->SearchDownloads(
1644 string16(), &downloads);
2043 ASSERT_EQ(1u, downloads.size()); 1645 ASSERT_EQ(1u, downloads.size());
2044 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state()); 1646 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state());
2045 EXPECT_TRUE(downloads[0]->opened()); 1647 EXPECT_TRUE(downloads[0]->opened());
2046 1648
2047 // As long as we're here, confirmed everything else is good. 1649 // As long as we're here, confirmed everything else is good.
2048 EXPECT_EQ(1, browser()->tab_count()); 1650 EXPECT_EQ(1, browser()->tab_count());
2049 CheckDownload(browser(), file, file); 1651 CheckDownload(browser(), file, file);
2050 // Download shelf should close. Download panel stays open on ChromeOS. 1652 // Download shelf should close. Download panel stays open on ChromeOS.
2051 CheckDownloadUI(browser(), false, true, FilePath()); 1653 CheckDownloadUI(browser(), false, true, FilePath());
2052 } 1654 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen()); 1786 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen());
2185 1787
2186 // Download shelf should close. Download panel stays open on ChromeOS. 1788 // Download shelf should close. Download panel stays open on ChromeOS.
2187 CheckDownloadUI(browser(), false, true, FilePath()); 1789 CheckDownloadUI(browser(), false, true, FilePath());
2188 1790
2189 // Check that the extension was installed. 1791 // Check that the extension was installed.
2190 ExtensionService* extension_service = 1792 ExtensionService* extension_service =
2191 browser()->profile()->GetExtensionService(); 1793 browser()->profile()->GetExtensionService();
2192 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false)); 1794 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false));
2193 } 1795 }
OLDNEW
« no previous file with comments | « chrome/browser/download/chrome_download_manager_delegate.cc ('k') | chrome/browser/download/download_history.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698