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

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

Issue 7294013: Modified cancel and interrupt processing to avoid race with history. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to TOT. Again :-}. Created 9 years, 5 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/file_path.h" 5 #include "base/file_path.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/download/download_util.h" 21 #include "chrome/browser/download/download_util.h"
22 #include "chrome/browser/extensions/extension_install_ui.h" 22 #include "chrome/browser/extensions/extension_install_ui.h"
23 #include "chrome/browser/extensions/extension_service.h" 23 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/history/download_history_info.h" 24 #include "chrome/browser/history/download_history_info.h"
25 #include "chrome/browser/history/history.h" 25 #include "chrome/browser/history/history.h"
26 #include "chrome/browser/prefs/pref_service.h" 26 #include "chrome/browser/prefs/pref_service.h"
27 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/ui/browser.h" 28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_list.h" 29 #include "chrome/browser/ui/browser_list.h"
30 #include "chrome/browser/ui/browser_window.h" 30 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/browser/ui/shell_dialogs.h"
31 #include "chrome/browser/ui/webui/active_downloads_ui.h" 32 #include "chrome/browser/ui/webui/active_downloads_ui.h"
32 #include "chrome/common/chrome_notification_types.h" 33 #include "chrome/common/chrome_notification_types.h"
33 #include "chrome/common/chrome_paths.h" 34 #include "chrome/common/chrome_paths.h"
34 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
35 #include "chrome/common/url_constants.h" 36 #include "chrome/common/url_constants.h"
36 #include "chrome/test/in_process_browser_test.h" 37 #include "chrome/test/in_process_browser_test.h"
37 #include "chrome/test/ui_test_utils.h" 38 #include "chrome/test/ui_test_utils.h"
38 #include "content/browser/cancelable_request.h" 39 #include "content/browser/cancelable_request.h"
39 #include "content/browser/net/url_request_mock_http_job.h" 40 #include "content/browser/net/url_request_mock_http_job.h"
40 #include "content/browser/renderer_host/resource_dispatcher_host.h" 41 #include "content/browser/renderer_host/resource_dispatcher_host.h"
41 #include "content/browser/tab_contents/tab_contents.h" 42 #include "content/browser/tab_contents/tab_contents.h"
42 #include "content/browser/net/url_request_slow_download_job.h" 43 #include "content/browser/net/url_request_slow_download_job.h"
43 #include "content/common/page_transition_types.h" 44 #include "content/common/page_transition_types.h"
44 #include "net/base/net_util.h" 45 #include "net/base/net_util.h"
45 #include "testing/gtest/include/gtest/gtest.h" 46 #include "testing/gtest/include/gtest/gtest.h"
46 47
47 namespace { 48 namespace {
48 49
49 // IDs and paths of CRX files used in tests. 50 // IDs and paths of CRX files used in tests.
50 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 51 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
51 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); 52 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx"));
52 53
53 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; 54 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
54 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); 55 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx"));
55 56
56 // Action a test should take if a dangerous download is encountered. 57 // Action a test should take if a dangerous download is encountered.
57 enum DangerousDownloadAction { 58 enum DangerousDownloadAction {
58 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download 59 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download
59 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download 60 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download
61 ON_DANGEROUS_DOWNLOAD_IGNORE, // Don't do anything; calling code will handle.
60 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen 62 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen
61 }; 63 };
62 64
63 // Fake user click on "Accept". 65 // Fake user click on "Accept".
64 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, 66 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager,
65 int32 download_id) { 67 int32 download_id) {
66 DownloadItem* download = download_manager->GetDownloadItem(download_id); 68 DownloadItem* download = download_manager->GetDownloadItem(download_id);
67 download->DangerousDownloadValidated(); 69 download->DangerousDownloadValidated();
68 } 70 }
69 71
70 // Fake user click on "Deny". 72 // Fake user click on "Deny".
71 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, 73 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager,
72 int32 download_id) { 74 int32 download_id) {
73 DownloadItem* download = download_manager->GetDownloadItem(download_id); 75 DownloadItem* download = download_manager->GetDownloadItem(download_id);
74 ASSERT_TRUE(download->IsPartialDownload()); 76 ASSERT_TRUE(download->IsPartialDownload());
75 download->Cancel(true); 77 download->Cancel();
76 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 78 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
77 } 79 }
78 80
79 // Construction of this class defines a system state, based on some number 81 // Construction of this class defines a system state, based on some number
80 // of downloads being seen in a particular state + other events that 82 // of downloads being seen in a particular state + other events that
81 // may occur in the download system. That state will be recorded if it 83 // may occur in the download system. That state will be recorded if it
82 // occurs at any point after construction. When that state occurs, the class 84 // occurs at any point after construction. When that state occurs, the class
83 // is considered finished. Callers may either probe for the finished state, or 85 // is considered finished. Callers may either probe for the finished state, or
84 // wait on it. 86 // wait on it.
85 // 87 //
86 // TODO(rdsmith): Detect manager going down, remove pointer to 88 // TODO(rdsmith): Detect manager going down, remove pointer to
87 // DownloadManager, transition to finished. (For right now we 89 // DownloadManager, transition to finished. (For right now we
88 // just use a scoped_refptr<> to keep it around, but that may cause 90 // just use a scoped_refptr<> to keep it around, but that may cause
89 // timeouts on waiting if a DownloadManager::Shutdown() occurs which 91 // timeouts on waiting if a DownloadManager::Shutdown() occurs which
90 // cancels our in-progress downloads.) 92 // cancels our in-progress downloads.)
91 class DownloadsObserver : public DownloadManager::Observer, 93 class DownloadsObserver : public DownloadManager::Observer,
92 public DownloadItem::Observer { 94 public DownloadItem::Observer {
93 public: 95 public:
96 typedef std::set<DownloadItem::DownloadState> StateSet;
97
94 // Create an object that will be considered finished when |wait_count| 98 // Create an object that will be considered finished when |wait_count|
95 // download items have entered state |download_finished_state|. 99 // download items have entered any states in |download_finished_states|.
96 // If |finish_on_select_file| is true, the object will also be 100 // If |finish_on_select_file| is true, the object will also be
97 // considered finished if the DownloadManager raises a 101 // considered finished if the DownloadManager raises a
98 // SelectFileDialogDisplayed() notification. 102 // SelectFileDialogDisplayed() notification.
99 103
100 // TODO(rdsmith): Consider rewriting the interface to take a list of events 104 // TODO(rdsmith): Consider rewriting the interface to take a list of events
101 // to treat as completion events. 105 // to treat as completion events.
102 DownloadsObserver(DownloadManager* download_manager, 106 DownloadsObserver(DownloadManager* download_manager,
103 size_t wait_count, 107 size_t wait_count,
104 DownloadItem::DownloadState download_finished_state, 108 StateSet download_finished_states,
105 bool finish_on_select_file, 109 bool finish_on_select_file,
106 DangerousDownloadAction dangerous_download_action) 110 DangerousDownloadAction dangerous_download_action)
107 : download_manager_(download_manager), 111 : download_manager_(download_manager),
108 wait_count_(wait_count), 112 wait_count_(wait_count),
109 finished_downloads_at_construction_(0), 113 finished_downloads_at_construction_(0),
110 waiting_(false), 114 waiting_(false),
111 download_finished_state_(download_finished_state), 115 download_finished_states_(download_finished_states),
112 finish_on_select_file_(finish_on_select_file), 116 finish_on_select_file_(finish_on_select_file),
113 select_file_dialog_seen_(false), 117 select_file_dialog_seen_(false),
114 dangerous_download_action_(dangerous_download_action) { 118 dangerous_download_action_(dangerous_download_action) {
115 download_manager_->AddObserver(this); // Will call initial ModelChanged(). 119 download_manager_->AddObserver(this); // Will call initial ModelChanged().
116 finished_downloads_at_construction_ = finished_downloads_.size(); 120 finished_downloads_at_construction_ = finished_downloads_.size();
117 EXPECT_NE(DownloadItem::REMOVING, download_finished_state) 121 EXPECT_TRUE(download_finished_states.find(DownloadItem::REMOVING) ==
122 download_finished_states.end())
118 << "Waiting for REMOVING is not supported. Try COMPLETE."; 123 << "Waiting for REMOVING is not supported. Try COMPLETE.";
119 } 124 }
120 125
121 ~DownloadsObserver() { 126 ~DownloadsObserver() {
122 std::set<DownloadItem*>::iterator it = downloads_observed_.begin(); 127 std::set<DownloadItem*>::iterator it = downloads_observed_.begin();
123 for (; it != downloads_observed_.end(); ++it) 128 for (; it != downloads_observed_.end(); ++it)
124 (*it)->RemoveObserver(this); 129 (*it)->RemoveObserver(this);
125 130
126 download_manager_->RemoveObserver(this); 131 download_manager_->RemoveObserver(this);
127 } 132 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 NewRunnableFunction( 191 NewRunnableFunction(
187 &DenyDangerousDownload, 192 &DenyDangerousDownload,
188 download_manager_, 193 download_manager_,
189 download->id())); 194 download->id()));
190 break; 195 break;
191 196
192 case ON_DANGEROUS_DOWNLOAD_FAIL: 197 case ON_DANGEROUS_DOWNLOAD_FAIL:
193 ADD_FAILURE() << "Unexpected dangerous download item."; 198 ADD_FAILURE() << "Unexpected dangerous download item.";
194 break; 199 break;
195 200
201 case ON_DANGEROUS_DOWNLOAD_IGNORE:
202 break;
203
196 default: 204 default:
197 NOTREACHED(); 205 NOTREACHED();
198 } 206 }
199 } 207 }
200 208
201 if (download->state() == download_finished_state_) { 209 if (download_finished_states_.find(download->state()) !=
210 download_finished_states_.end()) {
202 DownloadInFinalState(download); 211 DownloadInFinalState(download);
203 } 212 }
204 } 213 }
205 214
206 virtual void OnDownloadOpened(DownloadItem* download) {} 215 virtual void OnDownloadOpened(DownloadItem* download) {}
207 216
208 // DownloadManager::Observer 217 // DownloadManager::Observer
209 virtual void ModelChanged() { 218 virtual void ModelChanged() {
210 // Regenerate DownloadItem observers. If there are any download items 219 // Regenerate DownloadItem observers. If there are any download items
211 // in our final state, note them in |finished_downloads_| 220 // in our final state, note them in |finished_downloads_|
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 // construction and return from wait. But some downloads may be in our 303 // construction and return from wait. But some downloads may be in our
295 // final state (and thus be entered into |finished_downloads_|) when we 304 // final state (and thus be entered into |finished_downloads_|) when we
296 // construct this class. We don't want to count those in our transition 305 // construct this class. We don't want to count those in our transition
297 // to finished. 306 // to finished.
298 int finished_downloads_at_construction_; 307 int finished_downloads_at_construction_;
299 308
300 // Whether an internal message loop has been started and must be quit upon 309 // Whether an internal message loop has been started and must be quit upon
301 // all downloads completing. 310 // all downloads completing.
302 bool waiting_; 311 bool waiting_;
303 312
304 // The state on which to consider the DownloadItem finished. 313 // The states on which to consider the DownloadItem finished.
305 DownloadItem::DownloadState download_finished_state_; 314 StateSet download_finished_states_;
306 315
307 // True if we should transition the DownloadsObserver to finished if 316 // True if we should transition the DownloadsObserver to finished if
308 // the select file dialog comes up. 317 // the select file dialog comes up.
309 bool finish_on_select_file_; 318 bool finish_on_select_file_;
310 319
311 // True if we've seen the select file dialog. 320 // True if we've seen the select file dialog.
312 bool select_file_dialog_seen_; 321 bool select_file_dialog_seen_;
313 322
314 // Action to take if a dangerous download is encountered. 323 // Action to take if a dangerous download is encountered.
315 DangerousDownloadAction dangerous_download_action_; 324 DangerousDownloadAction dangerous_download_action_;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 } 492 }
484 493
485 ResourceDispatcherHost* resource_dispatcher_host_; 494 ResourceDispatcherHost* resource_dispatcher_host_;
486 DownloadFileManager* download_file_manager_; 495 DownloadFileManager* download_file_manager_;
487 int rdh_pending_requests_; 496 int rdh_pending_requests_;
488 int dfm_pending_downloads_; 497 int dfm_pending_downloads_;
489 498
490 DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector); 499 DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector);
491 }; 500 };
492 501
502 static const DownloadItem::DownloadState kTerminalStates[] = {
503 DownloadItem::CANCELLED,
504 DownloadItem::INTERRUPTED,
505 DownloadItem::COMPLETE,
506 };
507
508 static const DownloadItem::DownloadState kInProgressStates[] = {
509 DownloadItem::IN_PROGRESS,
510 };
511
512 // Get History Information.
513 class DownloadsHistoryDataCollector {
514 public:
515 explicit DownloadsHistoryDataCollector(int64 download_db_handle,
516 DownloadManager* manager)
517 : result_valid_(false),
518 download_db_handle_(download_db_handle) {
519 DCHECK(manager);
520 HistoryService* hs =
521 manager->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
522 DCHECK(hs);
523 hs->QueryDownloads(
524 &callback_consumer_,
525 NewCallback(this,
526 &DownloadsHistoryDataCollector::OnQueryDownloadsComplete));
527
528 // Cannot complete immediately because the history backend runs on a
529 // separate thread, so we can assume that the RunMessageLoop below will
530 // be exited by the Quit in OnQueryDownloadsComplete.
531 ui_test_utils::RunMessageLoop();
532 }
533
534 bool GetDownloadsHistoryEntry(DownloadHistoryInfo* result) {
535 DCHECK(result);
536 *result = result_;
537 return result_valid_;
538 }
539
540 private:
541 void OnQueryDownloadsComplete(
542 std::vector<DownloadHistoryInfo>* entries) {
543 result_valid_ = false;
544 for (std::vector<DownloadHistoryInfo>::const_iterator it = entries->begin();
545 it != entries->end(); ++it) {
546 if (it->db_handle == download_db_handle_) {
547 result_ = *it;
548 result_valid_ = true;
549 break;
550 }
551 }
552 MessageLoopForUI::current()->Quit();
553 }
554
555 DownloadHistoryInfo result_;
556 bool result_valid_;
557 int64 download_db_handle_;
558 CancelableRequestConsumer callback_consumer_;
559
560 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
561 };
562
563 // Mock that simulates a permissions dialog where the user denies
564 // permission to install. TODO(skerner): This could be shared with
565 // extensions tests. Find a common place for this class.
566 class MockAbortExtensionInstallUI : public ExtensionInstallUI {
567 public:
568 MockAbortExtensionInstallUI() : ExtensionInstallUI(NULL) {}
569
570 // Simulate a user abort on an extension installation.
571 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
572 delegate->InstallUIAbort(true);
573 MessageLoopForUI::current()->Quit();
574 }
575
576 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
577 virtual void OnInstallFailure(const std::string& error) {}
578 };
579
580 // Mock that simulates a permissions dialog where the user allows
581 // installation.
582 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
583 public:
584 explicit MockAutoConfirmExtensionInstallUI(Profile* profile) :
585 ExtensionInstallUI(profile) {}
586
587 // Proceed without confirmation prompt.
588 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
589 delegate->InstallUIProceed();
590 }
591
592 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
593 virtual void OnInstallFailure(const std::string& error) {}
594 };
595
596 // While any objects of this class exists, all DownloadItems added to the
597 // specified download manager will have opening mocked out.
598 class MockDownloadOpeningObserver : public DownloadManager::Observer {
599 public:
600 explicit MockDownloadOpeningObserver(DownloadManager* manager)
601 : download_manager_(manager) {
602 download_manager_->AddObserver(this);
603 }
604
605 ~MockDownloadOpeningObserver() {
606 download_manager_->RemoveObserver(this);
607 }
608
609 // DownloadManager::Observer
610 virtual void ModelChanged() {
611 std::vector<DownloadItem*> downloads;
612 download_manager_->SearchDownloads(string16(), &downloads);
613
614 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
615 it != downloads.end(); ++it) {
616 (*it)->TestMockDownloadOpen();
617 }
618 }
619
620 private:
621 DownloadManager* download_manager_;
622
623 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
624 };
625
626 // No-ops select files from the download manager.
627 // Returns a cancel callback on destruction.
628 class MockSelectFileDialog : public SelectFileDialog {
629 public:
630 explicit MockSelectFileDialog(SelectFileDialog::Listener* listener)
631 : SelectFileDialog(listener),
632 listener_(listener),
633 params_(NULL),
634 active_(false) {}
635 ~MockSelectFileDialog() {
636 if (listener_)
637 listener_->FileSelectionCanceled(params_);
638 }
639
640 static MockSelectFileDialog* Create(Listener* listener) {
641 return new MockSelectFileDialog(listener);
642 }
643
644 virtual bool IsRunning(gfx::NativeWindow owning_window) const {
645 return active_;
646 }
647
648 virtual void ListenerDestroyed() {
649 listener_ = NULL;
650 }
651
652 virtual void SelectFileImpl(SelectFileDialog::Type type,
653 const string16& title,
654 const FilePath& default_path,
655 const FileTypeInfo* file_types,
656 int file_type_index,
657 const FilePath::StringType& default_extension,
658 gfx::NativeWindow owning_window,
659 void *params) {
660 active_ = true;
661 params_ = params;
662 }
663 private:
664 SelectFileDialog::Listener* listener_;
665 void* params_;
666 bool active_;
667 };
668
669 } // namespace
670
671 // Not in anonymous namespace so that friend class from DownloadManager
672 // can target it.
493 class DownloadTest : public InProcessBrowserTest { 673 class DownloadTest : public InProcessBrowserTest {
494 public: 674 public:
495 enum SelectExpectation { 675 enum SelectExpectation {
496 EXPECT_NO_SELECT_DIALOG = -1, 676 EXPECT_NO_SELECT_DIALOG = -1,
497 EXPECT_NOTHING, 677 EXPECT_NOTHING,
498 EXPECT_SELECT_DIALOG 678 EXPECT_SELECT_DIALOG
499 }; 679 };
500 680
501 DownloadTest() { 681 DownloadTest() {
502 EnableDOMAutomation(); 682 EnableDOMAutomation();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 if (!downloads_directory_.CreateUniqueTempDir()) 739 if (!downloads_directory_.CreateUniqueTempDir())
560 return false; 740 return false;
561 741
562 browser->profile()->GetPrefs()->SetFilePath( 742 browser->profile()->GetPrefs()->SetFilePath(
563 prefs::kDownloadDefaultDirectory, 743 prefs::kDownloadDefaultDirectory,
564 downloads_directory_.path()); 744 downloads_directory_.path());
565 745
566 return true; 746 return true;
567 } 747 }
568 748
749 // For tests that want to test system reaction to files
750 // going away underneath them.
751 void DeleteDownloadsDirectory() {
752 EXPECT_TRUE(downloads_directory_.Delete());
753 }
754
569 DownloadPrefs* GetDownloadPrefs(Browser* browser) { 755 DownloadPrefs* GetDownloadPrefs(Browser* browser) {
570 return browser->profile()->GetDownloadManager()->download_prefs(); 756 return browser->profile()->GetDownloadManager()->download_prefs();
571 } 757 }
572 758
573 FilePath GetDownloadDirectory(Browser* browser) { 759 FilePath GetDownloadDirectory(Browser* browser) {
574 DownloadManager* download_mananger = 760 DownloadManager* download_mananger =
575 browser->profile()->GetDownloadManager(); 761 browser->profile()->GetDownloadManager();
576 return download_mananger->download_prefs()->download_path(); 762 return download_mananger->download_prefs()->download_path();
577 } 763 }
578 764
579 // Create a DownloadsObserver that will wait for the 765 // Create a DownloadsObserver that will wait for the
580 // specified number of downloads to finish. 766 // specified number of downloads to finish.
581 DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) { 767 DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) {
582 DownloadManager* download_manager = 768 DownloadManager* download_manager =
583 browser->profile()->GetDownloadManager(); 769 browser->profile()->GetDownloadManager();
584 return new DownloadsObserver( 770 return new DownloadsObserver(
585 download_manager, num_downloads, 771 download_manager, num_downloads,
586 DownloadItem::COMPLETE, // Really done 772 DownloadsObserver::StateSet(
587 false, // Bail on select file 773 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
774 true, // Bail on select file
588 ON_DANGEROUS_DOWNLOAD_FAIL); 775 ON_DANGEROUS_DOWNLOAD_FAIL);
589 } 776 }
590 777
591 // Create a DownloadsObserver that will wait for the 778 // Create a DownloadsObserver that will wait for the
779 // specified number of downloads to finish, and is
780 // ok with dangerous downloads. Note that use of this
781 // waiter is conditional on accepting the dangerous download.
782 DownloadsObserver* CreateDangerousWaiter(
783 Browser* browser, int num_downloads) {
784 DownloadManager* download_manager =
785 browser->profile()->GetDownloadManager();
786 return new DownloadsObserver(
787 download_manager, num_downloads,
788 DownloadsObserver::StateSet(
789 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
790 true, // Bail on select file
791 ON_DANGEROUS_DOWNLOAD_IGNORE);
792 }
793
794 // Create a DownloadsObserver that will wait for the
592 // specified number of downloads to start. 795 // specified number of downloads to start.
593 DownloadsObserver* CreateInProgressWaiter(Browser* browser, 796 DownloadsObserver* CreateInProgressWaiter(Browser* browser,
594 int num_downloads) { 797 int num_downloads) {
595 DownloadManager* download_manager = 798 DownloadManager* download_manager =
596 browser->profile()->GetDownloadManager(); 799 browser->profile()->GetDownloadManager();
597 return new DownloadsObserver( 800 return new DownloadsObserver(
598 download_manager, num_downloads, 801 download_manager, num_downloads,
599 DownloadItem::IN_PROGRESS, // Has started 802 DownloadsObserver::StateSet(
803 kInProgressStates,
804 kInProgressStates + arraysize(kInProgressStates)),
600 true, // Bail on select file 805 true, // Bail on select file
601 ON_DANGEROUS_DOWNLOAD_FAIL); 806 ON_DANGEROUS_DOWNLOAD_IGNORE);
602 } 807 }
603 808
604 // Create a DownloadsObserver that will wait for the 809 // Create a DownloadsObserver that will wait for the
605 // specified number of downloads to finish, or for 810 // specified number of downloads to finish, or for
606 // a dangerous download warning to be shown. 811 // a dangerous download warning to be shown.
607 DownloadsObserver* DangerousInstallWaiter( 812 DownloadsObserver* DangerousInstallWaiter(
608 Browser* browser, 813 Browser* browser,
609 int num_downloads, 814 int num_downloads,
610 DownloadItem::DownloadState final_state, 815 DownloadItem::DownloadState final_state,
611 DangerousDownloadAction dangerous_download_action) { 816 DangerousDownloadAction dangerous_download_action) {
817 DownloadsObserver::StateSet states;
818 states.insert(final_state);
612 DownloadManager* download_manager = 819 DownloadManager* download_manager =
613 browser->profile()->GetDownloadManager(); 820 browser->profile()->GetDownloadManager();
614 return new DownloadsObserver( 821 return new DownloadsObserver(
615 download_manager, num_downloads, 822 download_manager, num_downloads,
616 final_state, 823 states,
617 true, // Bail on select file 824 true, // Bail on select file
618 dangerous_download_action); 825 dangerous_download_action);
619 } 826 }
620 827
621 // Download |url|, then wait for the download to finish. 828 // Download |url|, then wait for the download to finish.
622 // |disposition| indicates where the navigation occurs (current tab, new 829 // |disposition| indicates where the navigation occurs (current tab, new
623 // foreground tab, etc). 830 // foreground tab, etc).
624 // |expectation| indicates whether or not a Select File dialog should be 831 // |expectation| indicates whether or not a Select File dialog should be
625 // open when the download is finished, or if we don't care. 832 // open when the download is finished, or if we don't care.
626 // If the dialog appears, the routine exits. The only effect |expectation| 833 // If the dialog appears, the routine exits. The only effect |expectation|
(...skipping 28 matching lines...) Expand all
655 const GURL& url, 862 const GURL& url,
656 SelectExpectation expectation) { 863 SelectExpectation expectation) {
657 DownloadAndWaitWithDisposition( 864 DownloadAndWaitWithDisposition(
658 browser, 865 browser,
659 url, 866 url,
660 CURRENT_TAB, 867 CURRENT_TAB,
661 expectation, 868 expectation,
662 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 869 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
663 } 870 }
664 871
872 // Mock or unmock the select file dialog for the download manager
873 // associated with the specified browser.
874 void NoopSelectFileDialog(Browser* browser, bool mock) {
875 if (mock) {
876 scoped_refptr<SelectFileDialog> dialog(
877 MockSelectFileDialog::Create(
878 browser->profile()->GetDownloadManager()));
879 browser->profile()->GetDownloadManager()->SetSelectFileDialog(dialog);
880 } else {
881 browser->profile()->GetDownloadManager()->SetSelectFileDialog(NULL);
882 }
883 }
884
665 // Should only be called when the download is known to have finished 885 // Should only be called when the download is known to have finished
666 // (in error or not). 886 // (in error or not).
667 // Returning false indicates a failure of the function, and should be asserted 887 // Returning false indicates a failure of the function, and should be asserted
668 // in the caller. 888 // in the caller.
669 bool CheckDownload(Browser* browser, 889 bool CheckDownload(Browser* browser,
670 const FilePath& downloaded_filename, 890 const FilePath& downloaded_filename,
671 const FilePath& origin_filename) { 891 const FilePath& origin_filename) {
672 // Find the path to which the data will be downloaded. 892 // Find the path to which the data will be downloaded.
673 FilePath downloaded_file(DestinationFile(browser, downloaded_filename)); 893 FilePath downloaded_file(DestinationFile(browser, downloaded_filename));
674 894
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 if (!downloaded_path_exists) 981 if (!downloaded_path_exists)
762 return false; 982 return false;
763 983
764 // Delete the file we just downloaded. 984 // Delete the file we just downloaded.
765 EXPECT_TRUE(file_util::DieFileDie(download_path, true)); 985 EXPECT_TRUE(file_util::DieFileDie(download_path, true));
766 EXPECT_FALSE(file_util::PathExists(download_path)); 986 EXPECT_FALSE(file_util::PathExists(download_path));
767 987
768 return true; 988 return true;
769 } 989 }
770 990
771 void GetDownloads(Browser* browser, std::vector<DownloadItem*>* downloads) { 991 void GetPersistentDownloads(Browser* browser,
992 std::vector<DownloadItem*>* downloads) {
993 DCHECK(downloads);
994 downloads->clear();
995 DownloadManager* manager = browser->profile()->GetDownloadManager();
996 manager->SearchDownloads(string16(), downloads);
997 }
998
999 void GetInProgressDownloads(Browser* browser,
1000 std::vector<DownloadItem*>* downloads) {
1001 downloads->clear();
772 DCHECK(downloads); 1002 DCHECK(downloads);
773 DownloadManager* manager = browser->profile()->GetDownloadManager(); 1003 DownloadManager* manager = browser->profile()->GetDownloadManager();
774 manager->SearchDownloads(string16(), downloads); 1004 manager->GetInProgressDownloads(downloads);
775 } 1005 }
776 1006
777 // Check that the download UI (shelf on non-chromeos or panel on chromeos) 1007 // Check that the download UI (shelf on non-chromeos or panel on chromeos)
778 // is visible or not as expected. Additionally, check that the filename 1008 // is visible or not as expected. Additionally, check that the filename
779 // is present in the UI (currently only on chromeos). 1009 // is present in the UI (currently only on chromeos).
780 void CheckDownloadUI(Browser* browser, bool expected_non_cros, 1010 void CheckDownloadUI(Browser* browser, bool expected_non_cros,
781 bool expected_cros, const FilePath& filename) { 1011 bool expected_cros, const FilePath& filename) {
782 #if defined(OS_CHROMEOS) 1012 #if defined(OS_CHROMEOS)
783 Browser* popup = ActiveDownloadsUI::GetPopup(); 1013 Browser* popup = ActiveDownloadsUI::GetPopup();
784 EXPECT_EQ(expected_cros, popup != NULL); 1014 EXPECT_EQ(expected_cros, popup != NULL);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 } 1046 }
817 1047
818 private: 1048 private:
819 // Location of the test data. 1049 // Location of the test data.
820 FilePath test_dir_; 1050 FilePath test_dir_;
821 1051
822 // Location of the downloads directory for these tests 1052 // Location of the downloads directory for these tests
823 ScopedTempDir downloads_directory_; 1053 ScopedTempDir downloads_directory_;
824 }; 1054 };
825 1055
826 // Get History Information.
827 class DownloadsHistoryDataCollector {
828 public:
829 explicit DownloadsHistoryDataCollector(int64 download_db_handle,
830 DownloadManager* manager)
831 : result_valid_(false),
832 download_db_handle_(download_db_handle) {
833 HistoryService* hs =
834 manager->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
835 DCHECK(hs);
836 hs->QueryDownloads(
837 &callback_consumer_,
838 NewCallback(this,
839 &DownloadsHistoryDataCollector::OnQueryDownloadsComplete));
840
841 // Cannot complete immediately because the history backend runs on a
842 // separate thread, so we can assume that the RunMessageLoop below will
843 // be exited by the Quit in OnQueryDownloadsComplete.
844 ui_test_utils::RunMessageLoop();
845 }
846
847 bool GetDownloadsHistoryEntry(DownloadHistoryInfo* result) {
848 DCHECK(result);
849 *result = result_;
850 return result_valid_;
851 }
852
853 private:
854 void OnQueryDownloadsComplete(
855 std::vector<DownloadHistoryInfo>* entries) {
856 result_valid_ = false;
857 for (std::vector<DownloadHistoryInfo>::const_iterator it = entries->begin();
858 it != entries->end(); ++it) {
859 if (it->db_handle == download_db_handle_) {
860 result_ = *it;
861 result_valid_ = true;
862 }
863 }
864 MessageLoopForUI::current()->Quit();
865 }
866
867 DownloadHistoryInfo result_;
868 bool result_valid_;
869 int64 download_db_handle_;
870 CancelableRequestConsumer callback_consumer_;
871
872 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
873 };
874
875 // Mock that simulates a permissions dialog where the user denies
876 // permission to install. TODO(skerner): This could be shared with
877 // extensions tests. Find a common place for this class.
878 class MockAbortExtensionInstallUI : public ExtensionInstallUI {
879 public:
880 MockAbortExtensionInstallUI() : ExtensionInstallUI(NULL) {}
881
882 // Simulate a user abort on an extension installation.
883 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
884 delegate->InstallUIAbort(true);
885 MessageLoopForUI::current()->Quit();
886 }
887
888 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
889 virtual void OnInstallFailure(const std::string& error) {}
890 };
891
892 // Mock that simulates a permissions dialog where the user allows
893 // installation.
894 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
895 public:
896 explicit MockAutoConfirmExtensionInstallUI(Profile* profile) :
897 ExtensionInstallUI(profile) {}
898
899 // Proceed without confirmation prompt.
900 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
901 delegate->InstallUIProceed();
902 }
903
904 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
905 virtual void OnInstallFailure(const std::string& error) {}
906 };
907
908 } // namespace
909
910 // While an object of this class exists, it will mock out download
911 // opening for all downloads created on the specified download manager.
912 class MockDownloadOpeningObserver : public DownloadManager::Observer {
913 public:
914 explicit MockDownloadOpeningObserver(DownloadManager* manager)
915 : download_manager_(manager) {
916 download_manager_->AddObserver(this);
917 }
918
919 ~MockDownloadOpeningObserver() {
920 download_manager_->RemoveObserver(this);
921 }
922
923 // DownloadManager::Observer
924 virtual void ModelChanged() {
925 std::vector<DownloadItem*> downloads;
926 download_manager_->SearchDownloads(string16(), &downloads);
927
928 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
929 it != downloads.end(); ++it) {
930 (*it)->TestMockDownloadOpen();
931 }
932 }
933
934 private:
935 DownloadManager* download_manager_;
936
937 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
938 };
939
940 // NOTES: 1056 // NOTES:
941 // 1057 //
942 // Files for these tests are found in DIR_TEST_DATA (currently 1058 // Files for these tests are found in DIR_TEST_DATA (currently
943 // "chrome\test\data\", see chrome_paths.cc). 1059 // "chrome\test\data\", see chrome_paths.cc).
944 // Mock responses have extension .mock-http-headers appended to the file name. 1060 // Mock responses have extension .mock-http-headers appended to the file name.
945 1061
946 // Download a file due to the associated MIME type. 1062 // Download a file due to the associated MIME type.
947 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { 1063 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
948 ASSERT_TRUE(InitialSetup(false)); 1064 ASSERT_TRUE(InitialSetup(false));
949 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1065 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
(...skipping 23 matching lines...) Expand all
973 // as CheckDownload will delete the output file. 1089 // as CheckDownload will delete the output file.
974 EXPECT_EQ(1, browser()->tab_count()); 1090 EXPECT_EQ(1, browser()->tab_count());
975 FilePath downloaded_file(DestinationFile(browser(), file)); 1091 FilePath downloaded_file(DestinationFile(browser(), file));
976 if (file_util::VolumeSupportsADS(downloaded_file)) 1092 if (file_util::VolumeSupportsADS(downloaded_file))
977 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file)); 1093 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file));
978 CheckDownload(browser(), file, file); 1094 CheckDownload(browser(), file, file);
979 CheckDownloadUI(browser(), true, true, file); 1095 CheckDownloadUI(browser(), true, true, file);
980 } 1096 }
981 #endif 1097 #endif
982 1098
983 // Put up a Select File dialog when the file is downloaded, due to its MIME 1099 // Put up a Select File dialog when the file is downloaded, due to
984 // type. 1100 // prompt_for_download==true argument to InitialSetup().
985 // 1101 // Confirm that we can cancel the download in that state.
986 // This test runs correctly, but leaves behind turds in the test user's 1102 IN_PROC_BROWSER_TEST_F(DownloadTest, CancelFromFileSelection) {
987 // download directory because of http://crbug.com/62099. No big loss; it
988 // was primarily confirming DownloadsObserver wait on select file dialog
989 // functionality anyway.
990 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
991 ASSERT_TRUE(InitialSetup(true)); 1103 ASSERT_TRUE(InitialSetup(true));
992 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1104 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
993 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1105 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
994 1106
995 // Download the file and wait. We expect the Select File dialog to appear 1107 // Setup select file interposition.
996 // due to the MIME type. 1108 NoopSelectFileDialog(browser(), true);
1109
1110 // Download the file and wait. We expect the Select File dialog to appear.
997 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG); 1111 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
998 1112
1113 std::vector<DownloadItem*> active_downloads, history_downloads;
1114 GetInProgressDownloads(browser(), &active_downloads);
1115 ASSERT_EQ(1u, active_downloads.size());
1116 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1117 GetPersistentDownloads(browser(), &history_downloads);
1118 EXPECT_EQ(0u, history_downloads.size());
1119
1120 // This should remove the download as it hasn't yet been entered into
1121 // the history.
1122 active_downloads[0]->Cancel();
1123
1124 GetInProgressDownloads(browser(), &active_downloads);
1125 EXPECT_EQ(0u, active_downloads.size());
1126 GetPersistentDownloads(browser(), &history_downloads);
1127 EXPECT_EQ(0u, history_downloads.size());
1128
999 // Check state. 1129 // Check state.
1000 EXPECT_EQ(1, browser()->tab_count()); 1130 EXPECT_EQ(1, browser()->tab_count());
1001 // Since we exited while the Select File dialog was visible, there should not 1131 // Since we exited while the Select File dialog was visible, there should not
1132 // be anything in the download shelf and so it should not be visible.
1133 CheckDownloadUI(browser(), false, false, FilePath());
1134 }
1135
1136 // Put up a Select File dialog when the file is downloaded, due to
1137 // prompt_for_download==true argument to InitialSetup().
1138 // Confirm that we can remove the download in that state.
1139 IN_PROC_BROWSER_TEST_F(DownloadTest, RemoveFromFileSelection) {
1140 ASSERT_TRUE(InitialSetup(true));
1141 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1142 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1143
1144 // Setup select file interposition.
1145 NoopSelectFileDialog(browser(), true);
1146
1147 // Download the file and wait. We expect the Select File dialog to appear.
1148 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1149
1150 std::vector<DownloadItem*> active_downloads, history_downloads;
1151 GetInProgressDownloads(browser(), &active_downloads);
1152 ASSERT_EQ(1u, active_downloads.size());
1153 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1154 GetPersistentDownloads(browser(), &history_downloads);
1155 EXPECT_EQ(0u, history_downloads.size());
1156
1157 // Confirm the file can be successfully removed from the select file
1158 // dialog blocked state.
1159 active_downloads[0]->Remove();
1160
1161 GetInProgressDownloads(browser(), &active_downloads);
1162 EXPECT_EQ(0u, active_downloads.size());
1163 GetPersistentDownloads(browser(), &history_downloads);
1164 EXPECT_EQ(0u, history_downloads.size());
1165
1166 EXPECT_EQ(1, browser()->tab_count());
1167 // Since we exited while the Select File dialog was visible, there should not
1168 // be anything in the download shelf and so it should not be visible.
1169 CheckDownloadUI(browser(), false, false, FilePath());
1170 }
1171
1172 // Put up a Select File dialog when the file is downloaded, due to
1173 // prompt_for_download==true argument to InitialSetup().
1174 // Confirm that an error coming in from the network works properly
1175 // when in that state.
1176 IN_PROC_BROWSER_TEST_F(DownloadTest, InterruptFromFileSelection) {
1177 ASSERT_TRUE(InitialSetup(true));
1178 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
1179
1180 // Download the file and wait. We expect the Select File dialog to appear.
1181 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1182
1183 std::vector<DownloadItem*> active_downloads, history_downloads;
1184 GetInProgressDownloads(browser(), &active_downloads);
1185 ASSERT_EQ(1u, active_downloads.size());
1186 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1187 GetPersistentDownloads(browser(), &history_downloads);
1188 EXPECT_EQ(0u, history_downloads.size());
1189
1190 // Complete the download with error.
1191 GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
1192 ui_test_utils::NavigateToURL(browser(), error_url);
1193 MessageLoopForUI::current()->RunAllPending();
1194
1195 // Confirm that a download error before entry into history
1196 // deletes the download.
1197 GetInProgressDownloads(browser(), &active_downloads);
1198 EXPECT_EQ(0u, active_downloads.size());
1199 GetPersistentDownloads(browser(), &history_downloads);
1200 EXPECT_EQ(0u, history_downloads.size());
1201
1202 // Check state.
1203 EXPECT_EQ(1, browser()->tab_count());
1204 // Since we exited while the Select File dialog was visible, there should not
1002 // be anything in the download shelf and so it should not be visible. 1205 // be anything in the download shelf and so it should not be visible.
1003 CheckDownloadUI(browser(), false, false, FilePath()); 1206 CheckDownloadUI(browser(), false, false, FilePath());
1004 } 1207 }
1005 1208
1006 // Access a file with a viewable mime-type, verify that a download 1209 // Access a file with a viewable mime-type, verify that a download
1007 // did not initiate. 1210 // did not initiate.
1008 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) { 1211 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) {
1009 ASSERT_TRUE(InitialSetup(false)); 1212 ASSERT_TRUE(InitialSetup(false));
1010 FilePath file(FILE_PATH_LITERAL("download-test2.html")); 1213 FilePath file(FILE_PATH_LITERAL("download-test2.html"));
1011 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1214 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
1424 1627
1425 // Create a download, wait until it's started, and confirm 1628 // Create a download, wait until it's started, and confirm
1426 // we're in the expected state. 1629 // we're in the expected state.
1427 scoped_ptr<DownloadsObserver> observer( 1630 scoped_ptr<DownloadsObserver> observer(
1428 CreateInProgressWaiter(browser(), 1)); 1631 CreateInProgressWaiter(browser(), 1));
1429 ui_test_utils::NavigateToURL( 1632 ui_test_utils::NavigateToURL(
1430 browser(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); 1633 browser(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl));
1431 observer->WaitForFinished(); 1634 observer->WaitForFinished();
1432 1635
1433 std::vector<DownloadItem*> downloads; 1636 std::vector<DownloadItem*> downloads;
1434 browser()->profile()->GetDownloadManager()->SearchDownloads( 1637 GetPersistentDownloads(browser(), &downloads);
1435 string16(), &downloads);
1436 ASSERT_EQ(1u, downloads.size()); 1638 ASSERT_EQ(1u, downloads.size());
1437 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state()); 1639 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state());
1438 CheckDownloadUI(browser(), true, true, FilePath()); 1640 CheckDownloadUI(browser(), true, true, FilePath());
1439 1641
1440 // Cancel the download and wait for download system quiesce. 1642 // Cancel the download and wait for download system quiesce.
1441 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 1643 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
1442 scoped_refptr<DownloadsFlushObserver> flush_observer( 1644 scoped_refptr<DownloadsFlushObserver> flush_observer(
1443 new DownloadsFlushObserver(browser()->profile()->GetDownloadManager())); 1645 new DownloadsFlushObserver(browser()->profile()->GetDownloadManager()));
1444 flush_observer->WaitForFlush(); 1646 flush_observer->WaitForFlush();
1445 1647
1446 // Get the important info from other threads and check it. 1648 // Get the important info from other threads and check it.
1447 scoped_refptr<CancelTestDataCollector> info(new CancelTestDataCollector()); 1649 scoped_refptr<CancelTestDataCollector> info(new CancelTestDataCollector());
1448 info->WaitForDataCollected(); 1650 info->WaitForDataCollected();
1449 EXPECT_EQ(0, info->rdh_pending_requests()); 1651 EXPECT_EQ(0, info->rdh_pending_requests());
1450 EXPECT_EQ(0, info->dfm_pending_downloads()); 1652 EXPECT_EQ(0, info->dfm_pending_downloads());
1451 1653
1452 // Using "DownloadItem::Remove" follows the discard dangerous download path, 1654 // Using "DownloadItem::Remove" follows the discard dangerous download path,
1453 // which completely removes the browser from the shelf and closes the shelf 1655 // which completely removes the browser from the shelf and closes the shelf
1454 // if it was there. Download panel stays open on ChromeOS. 1656 // if it was there. Download panel stays open on ChromeOS.
1455 CheckDownloadUI(browser(), false, true, FilePath()); 1657 CheckDownloadUI(browser(), false, true, FilePath());
1456 } 1658 }
1457 1659
1660 // Do a dangerous download and confirm that the download does
1661 // not complete until user accept, and that all states are
1662 // correct along the way.
1663 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerous) {
1664 ASSERT_TRUE(InitialSetup(false));
1665 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1666 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1667
1668 EXPECT_EQ(1, browser()->tab_count());
1669
1670 scoped_ptr<DownloadsObserver> observer(
1671 CreateInProgressWaiter(browser(), 1));
1672 ui_test_utils::NavigateToURL(browser(), url);
1673 observer->WaitForFinished();
1674
1675 // We should have one download, in history, and it should
1676 // still be dangerous.
1677 std::vector<DownloadItem*> downloads;
1678 GetPersistentDownloads(browser(), &downloads);
1679 ASSERT_EQ(1u, downloads.size());
1680 DownloadItem* download = downloads[0];
1681 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1682 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1683 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1684 // In ChromeOS, popup will be up, but file name will be unrecognizable.
1685 CheckDownloadUI(browser(), true, true, FilePath());
1686
1687 // See if accepting completes the download and changes the safety
1688 // state.
1689 scoped_ptr<DownloadsObserver> completion_observer(
1690 CreateDangerousWaiter(browser(), 1));
1691 AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
1692 download->id());
1693 completion_observer->WaitForFinished();
1694
1695 GetPersistentDownloads(browser(), &downloads);
1696 ASSERT_EQ(1u, downloads.size());
1697 ASSERT_EQ(downloads[0], download);
1698 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
1699 EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
1700 CheckDownloadUI(browser(), true, true, file);
1701 }
1702
1703 // Confirm that a dangerous download that gets a file error before
1704 // completion ends in the right state (currently cancelled because file
1705 // errors are non-resumable). Note that this is really testing
1706 // to make sure errors from the final rename are propagated properly.
1707 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousFileError) {
1708 ASSERT_TRUE(InitialSetup(false));
1709 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1710 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1711
1712 EXPECT_EQ(1, browser()->tab_count());
1713
1714 scoped_ptr<DownloadsObserver> observer(
1715 CreateInProgressWaiter(browser(), 1));
1716 ui_test_utils::NavigateToURL(browser(), url);
1717 observer->WaitForFinished();
1718
1719 // We should have one download, in history, and it should
1720 // still be dangerous.
1721 std::vector<DownloadItem*> downloads;
1722 GetPersistentDownloads(browser(), &downloads);
1723 ASSERT_EQ(1u, downloads.size());
1724 DownloadItem* download = downloads[0];
1725 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1726 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1727 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1728 // In ChromeOS, popup will be up, but file name will be unrecognizable.
1729 CheckDownloadUI(browser(), true, true, FilePath());
1730
1731 // Accept it after nuking the directory into which it's being downloaded;
1732 // that should complete the download with an error.
1733 DeleteDownloadsDirectory();
1734 scoped_ptr<DownloadsObserver> completion_observer(
1735 CreateDangerousWaiter(browser(), 1));
1736 AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
1737 download->id());
1738 completion_observer->WaitForFinished();
1739
1740 GetPersistentDownloads(browser(), &downloads);
1741 ASSERT_EQ(1u, downloads.size());
1742 ASSERT_EQ(downloads[0], download);
1743 // Persistent errors currently -> CANCELLED.
1744 EXPECT_EQ(DownloadItem::CANCELLED, download->state());
1745 EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
1746 // In ChromeOS, popup will still be up, but the file will have been
1747 // deleted.
1748 CheckDownloadUI(browser(), true, true, FilePath());
1749 }
1750
1751 // Confirm that declining a dangerous download erases it from living memory.
1752 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousDecline) {
1753 ASSERT_TRUE(InitialSetup(false));
1754 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1755 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1756
1757 EXPECT_EQ(1, browser()->tab_count());
1758
1759 scoped_ptr<DownloadsObserver> observer(
1760 CreateInProgressWaiter(browser(), 1));
1761 ui_test_utils::NavigateToURL(browser(), url);
1762 observer->WaitForFinished();
1763
1764 // We should have one download, in history, and it should
1765 // still be dangerous.
1766 std::vector<DownloadItem*> downloads;
1767 GetPersistentDownloads(browser(), &downloads);
1768 ASSERT_EQ(1u, downloads.size());
1769 DownloadItem* download = downloads[0];
1770 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1771 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1772 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1773 CheckDownloadUI(browser(), true, true, FilePath());
1774
1775 DenyDangerousDownload(browser()->profile()->GetDownloadManager(),
1776 download->id());
1777
1778 GetPersistentDownloads(browser(), &downloads);
1779 ASSERT_EQ(0u, downloads.size());
1780 CheckDownloadUI(browser(), false, true, FilePath());
1781 }
1782
1783 // Fail a download with a network error partway through, and make sure the
1784 // state is INTERRUPTED and the error is propagated.
1785 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadInterrupted) {
1786 ASSERT_TRUE(InitialSetup(false));
1787 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
1788
1789 scoped_ptr<DownloadsObserver> observer(
1790 CreateInProgressWaiter(browser(), 1));
1791 ui_test_utils::NavigateToURL(browser(), url);
1792 observer->WaitForFinished();
1793
1794 std::vector<DownloadItem*> downloads;
1795 GetPersistentDownloads(browser(), &downloads);
1796 ASSERT_EQ(1u, downloads.size());
1797 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state());
1798 FilePath filename;
1799 net::FileURLToFilePath(url, &filename);
1800 CheckDownloadUI(browser(), true, true,
1801 download_util::GetCrDownloadPath(filename.BaseName()));
1802
1803 // Fail the download
1804 GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
1805 ui_test_utils::NavigateToURL(browser(), error_url);
1806 MessageLoopForUI::current()->RunAllPending();
1807
1808 // Should still be visible, with INTERRUPTED state.
1809 GetPersistentDownloads(browser(), &downloads);
1810 ASSERT_EQ(1u, downloads.size());
1811 DownloadItem* download = downloads[0];
1812 ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
1813 // TODO(rdsmith): Confirm error provided by URLRequest is shown
1814 // in DownloadItem.
1815 CheckDownloadUI(browser(), true, true, FilePath());
1816
1817 // Confirm cancel does nothing.
1818 download->Cancel();
1819 MessageLoopForUI::current()->RunAllPending();
1820
1821 GetPersistentDownloads(browser(), &downloads);
1822 ASSERT_EQ(1u, downloads.size());
1823 ASSERT_EQ(download, downloads[0]);
1824 ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
1825 CheckDownloadUI(browser(), true, true, FilePath());
1826
1827 // Confirm remove gets rid of it.
1828 download->Remove();
1829 download = NULL;
1830 MessageLoopForUI::current()->RunAllPending();
1831
1832 GetPersistentDownloads(browser(), &downloads);
1833 ASSERT_EQ(0u, downloads.size());
1834 CheckDownloadUI(browser(), false, true, FilePath());
1835 }
1836
1458 // Confirm a download makes it into the history properly. 1837 // Confirm a download makes it into the history properly.
1459 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1838 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1460 ASSERT_TRUE(InitialSetup(false)); 1839 ASSERT_TRUE(InitialSetup(false));
1461 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1840 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1462 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1841 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1463 FilePath origin_file(OriginFile(file)); 1842 FilePath origin_file(OriginFile(file));
1464 int64 origin_size; 1843 int64 origin_size;
1465 file_util::GetFileSize(origin_file, &origin_size); 1844 file_util::GetFileSize(origin_file, &origin_size);
1466 1845
1467 // Download the file and wait. We do not expect the Select File dialog. 1846 // Download the file and wait. We do not expect the Select File dialog.
1468 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1847 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1469 1848
1470 // Get details of what downloads have just happened. 1849 // Get details of what downloads have just happened.
1471 std::vector<DownloadItem*> downloads; 1850 std::vector<DownloadItem*> downloads;
1472 GetDownloads(browser(), &downloads); 1851 GetPersistentDownloads(browser(), &downloads);
1473 ASSERT_EQ(1u, downloads.size()); 1852 ASSERT_EQ(1u, downloads.size());
1474 int64 db_handle = downloads[0]->db_handle(); 1853 int64 db_handle = downloads[0]->db_handle();
1475 1854
1476 // Check state. 1855 // Check state.
1477 EXPECT_EQ(1, browser()->tab_count()); 1856 EXPECT_EQ(1, browser()->tab_count());
1478 CheckDownload(browser(), file, file); 1857 CheckDownload(browser(), file, file);
1479 CheckDownloadUI(browser(), true, true, file); 1858 CheckDownloadUI(browser(), true, true, file);
1480 1859
1481 // Check history results. 1860 // Check history results.
1482 DownloadsHistoryDataCollector history_collector( 1861 DownloadsHistoryDataCollector history_collector(
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1558 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file)); 1937 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file));
1559 1938
1560 // Mock out external opening on all downloads until end of test. 1939 // Mock out external opening on all downloads until end of test.
1561 MockDownloadOpeningObserver observer( 1940 MockDownloadOpeningObserver observer(
1562 browser()->profile()->GetDownloadManager()); 1941 browser()->profile()->GetDownloadManager());
1563 1942
1564 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1943 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1565 1944
1566 // Find the download and confirm it was opened. 1945 // Find the download and confirm it was opened.
1567 std::vector<DownloadItem*> downloads; 1946 std::vector<DownloadItem*> downloads;
1568 browser()->profile()->GetDownloadManager()->SearchDownloads( 1947 GetPersistentDownloads(browser(), &downloads);
1569 string16(), &downloads);
1570 ASSERT_EQ(1u, downloads.size()); 1948 ASSERT_EQ(1u, downloads.size());
1571 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state()); 1949 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state());
1572 EXPECT_TRUE(downloads[0]->opened()); 1950 EXPECT_TRUE(downloads[0]->opened());
1573 1951
1574 // As long as we're here, confirmed everything else is good. 1952 // As long as we're here, confirmed everything else is good.
1575 EXPECT_EQ(1, browser()->tab_count()); 1953 EXPECT_EQ(1, browser()->tab_count());
1576 CheckDownload(browser(), file, file); 1954 CheckDownload(browser(), file, file);
1577 // Download shelf should close. Download panel stays open on ChromeOS. 1955 // Download shelf should close. Download panel stays open on ChromeOS.
1578 CheckDownloadUI(browser(), false, true, FilePath()); 1956 CheckDownloadUI(browser(), false, true, FilePath());
1579 } 1957 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1711 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen()); 2089 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen());
1712 2090
1713 // Download shelf should close. Download panel stays open on ChromeOS. 2091 // Download shelf should close. Download panel stays open on ChromeOS.
1714 CheckDownloadUI(browser(), false, true, FilePath()); 2092 CheckDownloadUI(browser(), false, true, FilePath());
1715 2093
1716 // Check that the extension was installed. 2094 // Check that the extension was installed.
1717 ExtensionService* extension_service = 2095 ExtensionService* extension_service =
1718 browser()->profile()->GetExtensionService(); 2096 browser()->profile()->GetExtensionService();
1719 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false)); 2097 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false));
1720 } 2098 }
OLDNEW
« no previous file with comments | « chrome/browser/automation/testing_automation_provider.cc ('k') | chrome/browser/download/download_file_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698