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

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: Fixed various problems surfaced by trybots. 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 49 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
50 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); 50 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx"));
51 51
52 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; 52 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
53 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); 53 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx"));
54 54
55 // Action a test should take if a dangerous download is encountered. 55 // Action a test should take if a dangerous download is encountered.
56 enum DangerousDownloadAction { 56 enum DangerousDownloadAction {
57 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download 57 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download
58 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download 58 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download
59 ON_DANGEROUS_DOWNLOAD_IGNORE, // Don't do anything; calling code will handle.
59 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen 60 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen
60 }; 61 };
61 62
62 // Fake user click on "Accept". 63 // Fake user click on "Accept".
63 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, 64 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager,
64 int32 download_id) { 65 int32 download_id) {
65 DownloadItem* download = download_manager->GetDownloadItem(download_id); 66 DownloadItem* download = download_manager->GetDownloadItem(download_id);
66 download->DangerousDownloadValidated(); 67 download->DangerousDownloadValidated();
67 } 68 }
68 69
69 // Fake user click on "Deny". 70 // Fake user click on "Deny".
70 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, 71 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager,
71 int32 download_id) { 72 int32 download_id) {
72 DownloadItem* download = download_manager->GetDownloadItem(download_id); 73 DownloadItem* download = download_manager->GetDownloadItem(download_id);
73 ASSERT_TRUE(download->IsPartialDownload()); 74 ASSERT_TRUE(download->IsPartialDownload());
74 download->Cancel(true); 75 download->Cancel();
75 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 76 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
76 } 77 }
77 78
78 // Construction of this class defines a system state, based on some number 79 // Construction of this class defines a system state, based on some number
79 // of downloads being seen in a particular state + other events that 80 // of downloads being seen in a particular state + other events that
80 // may occur in the download system. That state will be recorded if it 81 // may occur in the download system. That state will be recorded if it
81 // occurs at any point after construction. When that state occurs, the class 82 // occurs at any point after construction. When that state occurs, the class
82 // is considered finished. Callers may either probe for the finished state, or 83 // is considered finished. Callers may either probe for the finished state, or
83 // wait on it. 84 // wait on it.
84 // 85 //
85 // TODO(rdsmith): Detect manager going down, remove pointer to 86 // TODO(rdsmith): Detect manager going down, remove pointer to
86 // DownloadManager, transition to finished. (For right now we 87 // DownloadManager, transition to finished. (For right now we
87 // just use a scoped_refptr<> to keep it around, but that may cause 88 // just use a scoped_refptr<> to keep it around, but that may cause
88 // timeouts on waiting if a DownloadManager::Shutdown() occurs which 89 // timeouts on waiting if a DownloadManager::Shutdown() occurs which
89 // cancels our in-progress downloads.) 90 // cancels our in-progress downloads.)
90 class DownloadsObserver : public DownloadManager::Observer, 91 class DownloadsObserver : public DownloadManager::Observer,
91 public DownloadItem::Observer { 92 public DownloadItem::Observer {
92 public: 93 public:
94 typedef std::set<DownloadItem::DownloadState> StateSet;
95
93 // Create an object that will be considered finished when |wait_count| 96 // Create an object that will be considered finished when |wait_count|
94 // download items have entered state |download_finished_state|. 97 // download items have entered any states in |download_finished_states|.
95 // If |finish_on_select_file| is true, the object will also be 98 // If |finish_on_select_file| is true, the object will also be
96 // considered finished if the DownloadManager raises a 99 // considered finished if the DownloadManager raises a
97 // SelectFileDialogDisplayed() notification. 100 // SelectFileDialogDisplayed() notification.
98 101
99 // TODO(rdsmith): Consider rewriting the interface to take a list of events 102 // TODO(rdsmith): Consider rewriting the interface to take a list of events
100 // to treat as completion events. 103 // to treat as completion events.
101 DownloadsObserver(DownloadManager* download_manager, 104 DownloadsObserver(DownloadManager* download_manager,
102 size_t wait_count, 105 size_t wait_count,
103 DownloadItem::DownloadState download_finished_state, 106 StateSet download_finished_states,
104 bool finish_on_select_file, 107 bool finish_on_select_file,
105 DangerousDownloadAction dangerous_download_action) 108 DangerousDownloadAction dangerous_download_action)
106 : download_manager_(download_manager), 109 : download_manager_(download_manager),
107 wait_count_(wait_count), 110 wait_count_(wait_count),
108 finished_downloads_at_construction_(0), 111 finished_downloads_at_construction_(0),
109 waiting_(false), 112 waiting_(false),
110 download_finished_state_(download_finished_state), 113 download_finished_states_(download_finished_states),
111 finish_on_select_file_(finish_on_select_file), 114 finish_on_select_file_(finish_on_select_file),
112 select_file_dialog_seen_(false), 115 select_file_dialog_seen_(false),
113 dangerous_download_action_(dangerous_download_action) { 116 dangerous_download_action_(dangerous_download_action) {
114 download_manager_->AddObserver(this); // Will call initial ModelChanged(). 117 download_manager_->AddObserver(this); // Will call initial ModelChanged().
115 finished_downloads_at_construction_ = finished_downloads_.size(); 118 finished_downloads_at_construction_ = finished_downloads_.size();
116 EXPECT_NE(DownloadItem::REMOVING, download_finished_state) 119 EXPECT_TRUE(download_finished_states.find(DownloadItem::REMOVING) ==
120 download_finished_states.end())
117 << "Waiting for REMOVING is not supported. Try COMPLETE."; 121 << "Waiting for REMOVING is not supported. Try COMPLETE.";
118 } 122 }
119 123
120 ~DownloadsObserver() { 124 ~DownloadsObserver() {
121 std::set<DownloadItem*>::iterator it = downloads_observed_.begin(); 125 std::set<DownloadItem*>::iterator it = downloads_observed_.begin();
122 for (; it != downloads_observed_.end(); ++it) 126 for (; it != downloads_observed_.end(); ++it)
123 (*it)->RemoveObserver(this); 127 (*it)->RemoveObserver(this);
124 128
125 download_manager_->RemoveObserver(this); 129 download_manager_->RemoveObserver(this);
126 } 130 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 NewRunnableFunction( 189 NewRunnableFunction(
186 &DenyDangerousDownload, 190 &DenyDangerousDownload,
187 download_manager_, 191 download_manager_,
188 download->id())); 192 download->id()));
189 break; 193 break;
190 194
191 case ON_DANGEROUS_DOWNLOAD_FAIL: 195 case ON_DANGEROUS_DOWNLOAD_FAIL:
192 ADD_FAILURE() << "Unexpected dangerous download item."; 196 ADD_FAILURE() << "Unexpected dangerous download item.";
193 break; 197 break;
194 198
199 case ON_DANGEROUS_DOWNLOAD_IGNORE:
200 break;
201
195 default: 202 default:
196 NOTREACHED(); 203 NOTREACHED();
197 } 204 }
198 } 205 }
199 206
200 if (download->state() == download_finished_state_) { 207 if (download_finished_states_.find(download->state()) !=
208 download_finished_states_.end()) {
201 DownloadInFinalState(download); 209 DownloadInFinalState(download);
202 } 210 }
203 } 211 }
204 212
205 virtual void OnDownloadOpened(DownloadItem* download) {} 213 virtual void OnDownloadOpened(DownloadItem* download) {}
206 214
207 // DownloadManager::Observer 215 // DownloadManager::Observer
208 virtual void ModelChanged() { 216 virtual void ModelChanged() {
209 // Regenerate DownloadItem observers. If there are any download items 217 // Regenerate DownloadItem observers. If there are any download items
210 // in our final state, note them in |finished_downloads_| 218 // in our final state, note them in |finished_downloads_|
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 // construction and return from wait. But some downloads may be in our 301 // construction and return from wait. But some downloads may be in our
294 // final state (and thus be entered into |finished_downloads_|) when we 302 // final state (and thus be entered into |finished_downloads_|) when we
295 // construct this class. We don't want to count those in our transition 303 // construct this class. We don't want to count those in our transition
296 // to finished. 304 // to finished.
297 int finished_downloads_at_construction_; 305 int finished_downloads_at_construction_;
298 306
299 // Whether an internal message loop has been started and must be quit upon 307 // Whether an internal message loop has been started and must be quit upon
300 // all downloads completing. 308 // all downloads completing.
301 bool waiting_; 309 bool waiting_;
302 310
303 // The state on which to consider the DownloadItem finished. 311 // The states on which to consider the DownloadItem finished.
304 DownloadItem::DownloadState download_finished_state_; 312 StateSet download_finished_states_;
305 313
306 // True if we should transition the DownloadsObserver to finished if 314 // True if we should transition the DownloadsObserver to finished if
307 // the select file dialog comes up. 315 // the select file dialog comes up.
308 bool finish_on_select_file_; 316 bool finish_on_select_file_;
309 317
310 // True if we've seen the select file dialog. 318 // True if we've seen the select file dialog.
311 bool select_file_dialog_seen_; 319 bool select_file_dialog_seen_;
312 320
313 // Action to take if a dangerous download is encountered. 321 // Action to take if a dangerous download is encountered.
314 DangerousDownloadAction dangerous_download_action_; 322 DangerousDownloadAction dangerous_download_action_;
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 } 490 }
483 491
484 ResourceDispatcherHost* resource_dispatcher_host_; 492 ResourceDispatcherHost* resource_dispatcher_host_;
485 DownloadFileManager* download_file_manager_; 493 DownloadFileManager* download_file_manager_;
486 int rdh_pending_requests_; 494 int rdh_pending_requests_;
487 int dfm_pending_downloads_; 495 int dfm_pending_downloads_;
488 496
489 DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector); 497 DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector);
490 }; 498 };
491 499
500 static const DownloadItem::DownloadState kTerminalStates[] = {
501 DownloadItem::CANCELLED,
502 DownloadItem::INTERRUPTED,
503 DownloadItem::COMPLETE,
504 };
505
506 static const DownloadItem::DownloadState kInProgressStates[] = {
507 DownloadItem::IN_PROGRESS,
508 };
509
510 // Get History Information.
511 class DownloadsHistoryDataCollector {
512 public:
513 explicit DownloadsHistoryDataCollector(int64 download_db_handle,
514 DownloadManager* manager)
515 : result_valid_(false),
516 download_db_handle_(download_db_handle) {
517 HistoryService* hs =
518 manager->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
519 DCHECK(hs);
520 hs->QueryDownloads(
521 &callback_consumer_,
522 NewCallback(this,
523 &DownloadsHistoryDataCollector::OnQueryDownloadsComplete));
524
525 // Cannot complete immediately because the history backend runs on a
526 // separate thread, so we can assume that the RunMessageLoop below will
527 // be exited by the Quit in OnQueryDownloadsComplete.
528 ui_test_utils::RunMessageLoop();
529 }
530
531 bool GetDownloadsHistoryEntry(DownloadHistoryInfo* result) {
532 DCHECK(result);
533 *result = result_;
534 return result_valid_;
535 }
536
537 private:
538 void OnQueryDownloadsComplete(
539 std::vector<DownloadHistoryInfo>* entries) {
540 result_valid_ = false;
541 for (std::vector<DownloadHistoryInfo>::const_iterator it = entries->begin();
542 it != entries->end(); ++it) {
543 if (it->db_handle == download_db_handle_) {
544 result_ = *it;
545 result_valid_ = true;
546 }
547 }
548 MessageLoopForUI::current()->Quit();
549 }
550
551 DownloadHistoryInfo result_;
552 bool result_valid_;
553 int64 download_db_handle_;
554 CancelableRequestConsumer callback_consumer_;
555
556 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
557 };
558
559 // Mock that simulates a permissions dialog where the user denies
560 // permission to install. TODO(skerner): This could be shared with
561 // extensions tests. Find a common place for this class.
562 class MockAbortExtensionInstallUI : public ExtensionInstallUI {
563 public:
564 MockAbortExtensionInstallUI() : ExtensionInstallUI(NULL) {}
565
566 // Simulate a user abort on an extension installation.
567 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
568 delegate->InstallUIAbort(true);
569 MessageLoopForUI::current()->Quit();
570 }
571
572 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
573 virtual void OnInstallFailure(const std::string& error) {}
574 };
575
576 // Mock that simulates a permissions dialog where the user allows
577 // installation.
578 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
579 public:
580 explicit MockAutoConfirmExtensionInstallUI(Profile* profile) :
581 ExtensionInstallUI(profile) {}
582
583 // Proceed without confirmation prompt.
584 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
585 delegate->InstallUIProceed();
586 }
587
588 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
589 virtual void OnInstallFailure(const std::string& error) {}
590 };
591
592 // While an object of this class exists, it will mock out download
593 // opening for all downloads created on the specified download manager.
594 class MockDownloadOpeningObserver : public DownloadManager::Observer {
595 public:
596 explicit MockDownloadOpeningObserver(DownloadManager* manager)
597 : download_manager_(manager) {
598 download_manager_->AddObserver(this);
599 }
600
601 ~MockDownloadOpeningObserver() {
602 download_manager_->RemoveObserver(this);
603 }
604
605 // DownloadManager::Observer
606 virtual void ModelChanged() {
607 std::vector<DownloadItem*> downloads;
608 download_manager_->SearchDownloads(string16(), &downloads);
609
610 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
611 it != downloads.end(); ++it) {
612 (*it)->TestMockDownloadOpen();
613 }
614 }
615
616 private:
617 DownloadManager* download_manager_;
618
619 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
620 };
621
622 } // namespace
623
624 // Not in anonymous namespace so that friend class from DownloadManager
625 // can target it.
492 class DownloadTest : public InProcessBrowserTest { 626 class DownloadTest : public InProcessBrowserTest {
493 public: 627 public:
494 enum SelectExpectation { 628 enum SelectExpectation {
495 EXPECT_NO_SELECT_DIALOG = -1, 629 EXPECT_NO_SELECT_DIALOG = -1,
496 EXPECT_NOTHING, 630 EXPECT_NOTHING,
497 EXPECT_SELECT_DIALOG 631 EXPECT_SELECT_DIALOG
498 }; 632 };
499 633
500 DownloadTest() { 634 DownloadTest() {
501 EnableDOMAutomation(); 635 EnableDOMAutomation();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 if (!downloads_directory_.CreateUniqueTempDir()) 692 if (!downloads_directory_.CreateUniqueTempDir())
559 return false; 693 return false;
560 694
561 browser->profile()->GetPrefs()->SetFilePath( 695 browser->profile()->GetPrefs()->SetFilePath(
562 prefs::kDownloadDefaultDirectory, 696 prefs::kDownloadDefaultDirectory,
563 downloads_directory_.path()); 697 downloads_directory_.path());
564 698
565 return true; 699 return true;
566 } 700 }
567 701
702 // For tests that want to test system reaction to files
703 // going away underneath them.
704 void DeleteDownloadsDirectory() {
705 EXPECT_TRUE(downloads_directory_.Delete());
706 }
707
568 DownloadPrefs* GetDownloadPrefs(Browser* browser) { 708 DownloadPrefs* GetDownloadPrefs(Browser* browser) {
569 return browser->profile()->GetDownloadManager()->download_prefs(); 709 return browser->profile()->GetDownloadManager()->download_prefs();
570 } 710 }
571 711
572 FilePath GetDownloadDirectory(Browser* browser) { 712 FilePath GetDownloadDirectory(Browser* browser) {
573 DownloadManager* download_mananger = 713 DownloadManager* download_mananger =
574 browser->profile()->GetDownloadManager(); 714 browser->profile()->GetDownloadManager();
575 return download_mananger->download_prefs()->download_path(); 715 return download_mananger->download_prefs()->download_path();
576 } 716 }
577 717
578 // Create a DownloadsObserver that will wait for the 718 // Create a DownloadsObserver that will wait for the
579 // specified number of downloads to finish. 719 // specified number of downloads to finish.
580 DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) { 720 DownloadsObserver* CreateWaiter(Browser* browser, int num_downloads) {
581 DownloadManager* download_manager = 721 DownloadManager* download_manager =
582 browser->profile()->GetDownloadManager(); 722 browser->profile()->GetDownloadManager();
583 return new DownloadsObserver( 723 return new DownloadsObserver(
584 download_manager, num_downloads, 724 download_manager, num_downloads,
585 DownloadItem::COMPLETE, // Really done 725 DownloadsObserver::StateSet(
586 false, // Bail on select file 726 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
727 true, // Bail on select file
587 ON_DANGEROUS_DOWNLOAD_FAIL); 728 ON_DANGEROUS_DOWNLOAD_FAIL);
588 } 729 }
589 730
590 // Create a DownloadsObserver that will wait for the 731 // Create a DownloadsObserver that will wait for the
732 // specified number of downloads to finish, and is
733 // ok with dangerous downloads. Note that use of this
734 // waiter is conditional on accepting the dangerous download.
735 DownloadsObserver* CreateDangerousWaiter(
736 Browser* browser, int num_downloads) {
737 DownloadManager* download_manager =
738 browser->profile()->GetDownloadManager();
739 return new DownloadsObserver(
740 download_manager, num_downloads,
741 DownloadsObserver::StateSet(
742 kTerminalStates, kTerminalStates + arraysize(kTerminalStates)),
743 true, // Bail on select file
744 ON_DANGEROUS_DOWNLOAD_IGNORE);
745 }
746
747 // Create a DownloadsObserver that will wait for the
591 // specified number of downloads to start. 748 // specified number of downloads to start.
592 DownloadsObserver* CreateInProgressWaiter(Browser* browser, 749 DownloadsObserver* CreateInProgressWaiter(Browser* browser,
593 int num_downloads) { 750 int num_downloads) {
594 DownloadManager* download_manager = 751 DownloadManager* download_manager =
595 browser->profile()->GetDownloadManager(); 752 browser->profile()->GetDownloadManager();
596 return new DownloadsObserver( 753 return new DownloadsObserver(
597 download_manager, num_downloads, 754 download_manager, num_downloads,
598 DownloadItem::IN_PROGRESS, // Has started 755 DownloadsObserver::StateSet(
756 kInProgressStates,
757 kInProgressStates + arraysize(kInProgressStates)),
599 true, // Bail on select file 758 true, // Bail on select file
600 ON_DANGEROUS_DOWNLOAD_FAIL); 759 ON_DANGEROUS_DOWNLOAD_IGNORE);
601 } 760 }
602 761
603 // Create a DownloadsObserver that will wait for the 762 // Create a DownloadsObserver that will wait for the
604 // specified number of downloads to finish, or for 763 // specified number of downloads to finish, or for
605 // a dangerous download warning to be shown. 764 // a dangerous download warning to be shown.
606 DownloadsObserver* DangerousInstallWaiter( 765 DownloadsObserver* DangerousInstallWaiter(
607 Browser* browser, 766 Browser* browser,
608 int num_downloads, 767 int num_downloads,
609 DownloadItem::DownloadState final_state, 768 DownloadItem::DownloadState final_state,
610 DangerousDownloadAction dangerous_download_action) { 769 DangerousDownloadAction dangerous_download_action) {
770 DownloadsObserver::StateSet states;
771 states.insert(final_state);
611 DownloadManager* download_manager = 772 DownloadManager* download_manager =
612 browser->profile()->GetDownloadManager(); 773 browser->profile()->GetDownloadManager();
613 return new DownloadsObserver( 774 return new DownloadsObserver(
614 download_manager, num_downloads, 775 download_manager, num_downloads,
615 final_state, 776 states,
616 true, // Bail on select file 777 true, // Bail on select file
617 dangerous_download_action); 778 dangerous_download_action);
618 } 779 }
619 780
620 // Download |url|, then wait for the download to finish. 781 // Download |url|, then wait for the download to finish.
621 // |disposition| indicates where the navigation occurs (current tab, new 782 // |disposition| indicates where the navigation occurs (current tab, new
622 // foreground tab, etc). 783 // foreground tab, etc).
623 // |expectation| indicates whether or not a Select File dialog should be 784 // |expectation| indicates whether or not a Select File dialog should be
624 // open when the download is finished, or if we don't care. 785 // open when the download is finished, or if we don't care.
625 // If the dialog appears, the routine exits. The only effect |expectation| 786 // If the dialog appears, the routine exits. The only effect |expectation|
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 if (!downloaded_path_exists) 921 if (!downloaded_path_exists)
761 return false; 922 return false;
762 923
763 // Delete the file we just downloaded. 924 // Delete the file we just downloaded.
764 EXPECT_TRUE(file_util::DieFileDie(download_path, true)); 925 EXPECT_TRUE(file_util::DieFileDie(download_path, true));
765 EXPECT_FALSE(file_util::PathExists(download_path)); 926 EXPECT_FALSE(file_util::PathExists(download_path));
766 927
767 return true; 928 return true;
768 } 929 }
769 930
770 void GetDownloads(Browser* browser, std::vector<DownloadItem*>* downloads) { 931 void GetPersistentDownloads(Browser* browser,
932 std::vector<DownloadItem*>* downloads) {
933 DCHECK(downloads);
934 downloads->clear();
935 DownloadManager* manager = browser->profile()->GetDownloadManager();
936 manager->SearchDownloads(string16(), downloads);
937 }
938
939 void GetInProgressDownloads(Browser* browser,
940 std::vector<DownloadItem*>* downloads) {
941 downloads->clear();
771 DCHECK(downloads); 942 DCHECK(downloads);
772 DownloadManager* manager = browser->profile()->GetDownloadManager(); 943 DownloadManager* manager = browser->profile()->GetDownloadManager();
773 manager->SearchDownloads(string16(), downloads); 944 manager->GetInProgressDownloads(downloads);
774 } 945 }
775 946
776 // Check that the download UI (shelf on non-chromeos or panel on chromeos) 947 // Check that the download UI (shelf on non-chromeos or panel on chromeos)
777 // is visible or not as expected. Additionally, check that the filename 948 // is visible or not as expected. Additionally, check that the filename
778 // is present in the UI (currently only on chromeos). 949 // is present in the UI (currently only on chromeos).
779 void CheckDownloadUI(Browser* browser, bool expected_non_cros, 950 void CheckDownloadUI(Browser* browser, bool expected_non_cros,
780 bool expected_cros, const FilePath& filename) { 951 bool expected_cros, const FilePath& filename) {
781 #if defined(OS_CHROMEOS) 952 #if defined(OS_CHROMEOS)
782 Browser* popup = ActiveDownloadsUI::GetPopup(); 953 Browser* popup = ActiveDownloadsUI::GetPopup();
783 EXPECT_EQ(expected_cros, popup != NULL); 954 EXPECT_EQ(expected_cros, popup != NULL);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 } 986 }
816 987
817 private: 988 private:
818 // Location of the test data. 989 // Location of the test data.
819 FilePath test_dir_; 990 FilePath test_dir_;
820 991
821 // Location of the downloads directory for these tests 992 // Location of the downloads directory for these tests
822 ScopedTempDir downloads_directory_; 993 ScopedTempDir downloads_directory_;
823 }; 994 };
824 995
825 // Get History Information.
826 class DownloadsHistoryDataCollector {
827 public:
828 explicit DownloadsHistoryDataCollector(int64 download_db_handle,
829 DownloadManager* manager)
830 : result_valid_(false),
831 download_db_handle_(download_db_handle) {
832 HistoryService* hs =
833 manager->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
834 DCHECK(hs);
835 hs->QueryDownloads(
836 &callback_consumer_,
837 NewCallback(this,
838 &DownloadsHistoryDataCollector::OnQueryDownloadsComplete));
839
840 // Cannot complete immediately because the history backend runs on a
841 // separate thread, so we can assume that the RunMessageLoop below will
842 // be exited by the Quit in OnQueryDownloadsComplete.
843 ui_test_utils::RunMessageLoop();
844 }
845
846 bool GetDownloadsHistoryEntry(DownloadHistoryInfo* result) {
847 DCHECK(result);
848 *result = result_;
849 return result_valid_;
850 }
851
852 private:
853 void OnQueryDownloadsComplete(
854 std::vector<DownloadHistoryInfo>* entries) {
855 result_valid_ = false;
856 for (std::vector<DownloadHistoryInfo>::const_iterator it = entries->begin();
857 it != entries->end(); ++it) {
858 if (it->db_handle == download_db_handle_) {
859 result_ = *it;
860 result_valid_ = true;
861 }
862 }
863 MessageLoopForUI::current()->Quit();
864 }
865
866 DownloadHistoryInfo result_;
867 bool result_valid_;
868 int64 download_db_handle_;
869 CancelableRequestConsumer callback_consumer_;
870
871 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
872 };
873
874 // Mock that simulates a permissions dialog where the user denies
875 // permission to install. TODO(skerner): This could be shared with
876 // extensions tests. Find a common place for this class.
877 class MockAbortExtensionInstallUI : public ExtensionInstallUI {
878 public:
879 MockAbortExtensionInstallUI() : ExtensionInstallUI(NULL) {}
880
881 // Simulate a user abort on an extension installation.
882 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
883 delegate->InstallUIAbort(true);
884 MessageLoopForUI::current()->Quit();
885 }
886
887 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
888 virtual void OnInstallFailure(const std::string& error) {}
889 };
890
891 // Mock that simulates a permissions dialog where the user allows
892 // installation.
893 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
894 public:
895 explicit MockAutoConfirmExtensionInstallUI(Profile* profile) :
896 ExtensionInstallUI(profile) {}
897
898 // Proceed without confirmation prompt.
899 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
900 delegate->InstallUIProceed();
901 }
902
903 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
904 virtual void OnInstallFailure(const std::string& error) {}
905 };
906
907 } // namespace
908
909 // While an object of this class exists, it will mock out download
910 // opening for all downloads created on the specified download manager.
911 class MockDownloadOpeningObserver : public DownloadManager::Observer {
912 public:
913 explicit MockDownloadOpeningObserver(DownloadManager* manager)
914 : download_manager_(manager) {
915 download_manager_->AddObserver(this);
916 }
917
918 ~MockDownloadOpeningObserver() {
919 download_manager_->RemoveObserver(this);
920 }
921
922 // DownloadManager::Observer
923 virtual void ModelChanged() {
924 std::vector<DownloadItem*> downloads;
925 download_manager_->SearchDownloads(string16(), &downloads);
926
927 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
928 it != downloads.end(); ++it) {
929 (*it)->TestMockDownloadOpen();
930 }
931 }
932
933 private:
934 DownloadManager* download_manager_;
935
936 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
937 };
938
939 // NOTES: 996 // NOTES:
940 // 997 //
941 // Files for these tests are found in DIR_TEST_DATA (currently 998 // Files for these tests are found in DIR_TEST_DATA (currently
942 // "chrome\test\data\", see chrome_paths.cc). 999 // "chrome\test\data\", see chrome_paths.cc).
943 // Mock responses have extension .mock-http-headers appended to the file name. 1000 // Mock responses have extension .mock-http-headers appended to the file name.
944 1001
945 // Download a file due to the associated MIME type. 1002 // Download a file due to the associated MIME type.
946 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { 1003 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
947 ASSERT_TRUE(InitialSetup(false)); 1004 ASSERT_TRUE(InitialSetup(false));
948 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1005 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
(...skipping 23 matching lines...) Expand all
972 // as CheckDownload will delete the output file. 1029 // as CheckDownload will delete the output file.
973 EXPECT_EQ(1, browser()->tab_count()); 1030 EXPECT_EQ(1, browser()->tab_count());
974 FilePath downloaded_file(DestinationFile(browser(), file)); 1031 FilePath downloaded_file(DestinationFile(browser(), file));
975 if (file_util::VolumeSupportsADS(downloaded_file)) 1032 if (file_util::VolumeSupportsADS(downloaded_file))
976 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file)); 1033 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file));
977 CheckDownload(browser(), file, file); 1034 CheckDownload(browser(), file, file);
978 CheckDownloadUI(browser(), true, true, file); 1035 CheckDownloadUI(browser(), true, true, file);
979 } 1036 }
980 #endif 1037 #endif
981 1038
982 // Put up a Select File dialog when the file is downloaded, due to its MIME 1039 // Put up a Select File dialog when the file is downloaded, due to
983 // type. 1040 // prompt_for_download==true argument to InitialSetup().
984 // 1041 // Confirm that we can cancel the download in that state.
985 // This test runs correctly, but leaves behind turds in the test user's 1042 IN_PROC_BROWSER_TEST_F(DownloadTest, CancelFromFileSelection) {
986 // download directory because of http://crbug.com/62099. No big loss; it
987 // was primarily confirming DownloadsObserver wait on select file dialog
988 // functionality anyway.
989 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
990 ASSERT_TRUE(InitialSetup(true)); 1043 ASSERT_TRUE(InitialSetup(true));
991 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1044 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
992 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1045 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
993 1046
994 // Download the file and wait. We expect the Select File dialog to appear 1047 // Download the file and wait. We expect the Select File dialog to appear.
995 // due to the MIME type.
996 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG); 1048 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1049 // To allow Select File dialog to be fully raised.
1050 MessageLoopForUI::current()->RunAllPending();
1051
1052 std::vector<DownloadItem*> active_downloads, history_downloads;
1053 GetInProgressDownloads(browser(), &active_downloads);
1054 ASSERT_EQ(1u, active_downloads.size());
1055 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1056 GetPersistentDownloads(browser(), &history_downloads);
1057 EXPECT_EQ(0u, history_downloads.size());
1058
1059 // This should remove the download as it hasn't yet been entered into
1060 // the history.
1061 active_downloads[0]->Cancel();
1062
1063 GetInProgressDownloads(browser(), &active_downloads);
1064 EXPECT_EQ(0u, active_downloads.size());
1065 GetPersistentDownloads(browser(), &history_downloads);
1066 EXPECT_EQ(0u, history_downloads.size());
997 1067
998 // Check state. 1068 // Check state.
999 EXPECT_EQ(1, browser()->tab_count()); 1069 EXPECT_EQ(1, browser()->tab_count());
1000 // Since we exited while the Select File dialog was visible, there should not 1070 // Since we exited while the Select File dialog was visible, there should not
1071 // be anything in the download shelf and so it should not be visible.
1072 CheckDownloadUI(browser(), false, false, FilePath());
1073 }
1074
1075 // Put up a Select File dialog when the file is downloaded, due to
1076 // prompt_for_download==true argument to InitialSetup().
1077 // Confirm that we can remove the download in that state.
1078 IN_PROC_BROWSER_TEST_F(DownloadTest, RemoveFromFileSelection) {
1079 ASSERT_TRUE(InitialSetup(true));
1080 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1081 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1082
1083 // Download the file and wait. We expect the Select File dialog to appear.
1084 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1085
1086 std::vector<DownloadItem*> active_downloads, history_downloads;
1087 GetInProgressDownloads(browser(), &active_downloads);
1088 ASSERT_EQ(1u, active_downloads.size());
1089 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1090 GetPersistentDownloads(browser(), &history_downloads);
1091 EXPECT_EQ(0u, history_downloads.size());
1092
1093 // Confirm the file can be successfully removed from the select file
1094 // dialog blocked state.
1095 active_downloads[0]->Remove();
1096
1097 GetInProgressDownloads(browser(), &active_downloads);
1098 EXPECT_EQ(0u, active_downloads.size());
1099 GetPersistentDownloads(browser(), &history_downloads);
1100 EXPECT_EQ(0u, history_downloads.size());
1101
1102 EXPECT_EQ(1, browser()->tab_count());
1103 // Since we exited while the Select File dialog was visible, there should not
1104 // be anything in the download shelf and so it should not be visible.
1105 CheckDownloadUI(browser(), false, false, FilePath());
1106 }
1107
1108 // Put up a Select File dialog when the file is downloaded, due to
1109 // prompt_for_download==true argument to InitialSetup().
1110 // Confirm that an error coming in from the network works properly
1111 // when in that state.
1112 IN_PROC_BROWSER_TEST_F(DownloadTest, InterruptFromFileSelection) {
1113 ASSERT_TRUE(InitialSetup(true));
1114 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
1115
1116 // Download the file and wait. We expect the Select File dialog to appear.
1117 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
1118
1119 std::vector<DownloadItem*> active_downloads, history_downloads;
1120 GetInProgressDownloads(browser(), &active_downloads);
1121 ASSERT_EQ(1u, active_downloads.size());
1122 EXPECT_EQ(DownloadItem::IN_PROGRESS, active_downloads[0]->state());
1123 GetPersistentDownloads(browser(), &history_downloads);
1124 EXPECT_EQ(0u, history_downloads.size());
1125
1126 // Complete the download with error.
1127 GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
1128 ui_test_utils::NavigateToURL(browser(), error_url);
1129 MessageLoopForUI::current()->RunAllPending();
1130
1131 // Confirm that a download error before entry into history
1132 // deletes the download.
1133 GetInProgressDownloads(browser(), &active_downloads);
1134 EXPECT_EQ(0u, active_downloads.size());
1135 GetPersistentDownloads(browser(), &history_downloads);
1136 EXPECT_EQ(0u, history_downloads.size());
1137
1138 // Check state.
1139 EXPECT_EQ(1, browser()->tab_count());
1140 // Since we exited while the Select File dialog was visible, there should not
1001 // be anything in the download shelf and so it should not be visible. 1141 // be anything in the download shelf and so it should not be visible.
1002 CheckDownloadUI(browser(), false, false, FilePath()); 1142 CheckDownloadUI(browser(), false, false, FilePath());
1003 } 1143 }
1004 1144
1005 // Access a file with a viewable mime-type, verify that a download 1145 // Access a file with a viewable mime-type, verify that a download
1006 // did not initiate. 1146 // did not initiate.
1007 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) { 1147 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) {
1008 ASSERT_TRUE(InitialSetup(false)); 1148 ASSERT_TRUE(InitialSetup(false));
1009 FilePath file(FILE_PATH_LITERAL("download-test2.html")); 1149 FilePath file(FILE_PATH_LITERAL("download-test2.html"));
1010 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1150 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 1563
1424 // Create a download, wait until it's started, and confirm 1564 // Create a download, wait until it's started, and confirm
1425 // we're in the expected state. 1565 // we're in the expected state.
1426 scoped_ptr<DownloadsObserver> observer( 1566 scoped_ptr<DownloadsObserver> observer(
1427 CreateInProgressWaiter(browser(), 1)); 1567 CreateInProgressWaiter(browser(), 1));
1428 ui_test_utils::NavigateToURL( 1568 ui_test_utils::NavigateToURL(
1429 browser(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); 1569 browser(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl));
1430 observer->WaitForFinished(); 1570 observer->WaitForFinished();
1431 1571
1432 std::vector<DownloadItem*> downloads; 1572 std::vector<DownloadItem*> downloads;
1433 browser()->profile()->GetDownloadManager()->SearchDownloads( 1573 GetPersistentDownloads(browser(), &downloads);
1434 string16(), &downloads);
1435 ASSERT_EQ(1u, downloads.size()); 1574 ASSERT_EQ(1u, downloads.size());
1436 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state()); 1575 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state());
1437 CheckDownloadUI(browser(), true, true, FilePath()); 1576 CheckDownloadUI(browser(), true, true, FilePath());
1438 1577
1439 // Cancel the download and wait for download system quiesce. 1578 // Cancel the download and wait for download system quiesce.
1440 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); 1579 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD);
1441 scoped_refptr<DownloadsFlushObserver> flush_observer( 1580 scoped_refptr<DownloadsFlushObserver> flush_observer(
1442 new DownloadsFlushObserver(browser()->profile()->GetDownloadManager())); 1581 new DownloadsFlushObserver(browser()->profile()->GetDownloadManager()));
1443 flush_observer->WaitForFlush(); 1582 flush_observer->WaitForFlush();
1444 1583
1445 // Get the important info from other threads and check it. 1584 // Get the important info from other threads and check it.
1446 scoped_refptr<CancelTestDataCollector> info(new CancelTestDataCollector()); 1585 scoped_refptr<CancelTestDataCollector> info(new CancelTestDataCollector());
1447 info->WaitForDataCollected(); 1586 info->WaitForDataCollected();
1448 EXPECT_EQ(0, info->rdh_pending_requests()); 1587 EXPECT_EQ(0, info->rdh_pending_requests());
1449 EXPECT_EQ(0, info->dfm_pending_downloads()); 1588 EXPECT_EQ(0, info->dfm_pending_downloads());
1450 1589
1451 // Using "DownloadItem::Remove" follows the discard dangerous download path, 1590 // Using "DownloadItem::Remove" follows the discard dangerous download path,
1452 // which completely removes the browser from the shelf and closes the shelf 1591 // which completely removes the browser from the shelf and closes the shelf
1453 // if it was there. Download panel stays open on ChromeOS. 1592 // if it was there. Download panel stays open on ChromeOS.
1454 CheckDownloadUI(browser(), false, true, FilePath()); 1593 CheckDownloadUI(browser(), false, true, FilePath());
1455 } 1594 }
1456 1595
1596 // Do a dangerous download and confirm that the download does
1597 // not complete until user accept, and that all states are
1598 // correct along the way.
1599 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerous) {
1600 ASSERT_TRUE(InitialSetup(false));
1601 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1602 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1603
1604 EXPECT_EQ(1, browser()->tab_count());
1605
1606 scoped_ptr<DownloadsObserver> observer(
1607 CreateInProgressWaiter(browser(), 1));
1608 ui_test_utils::NavigateToURL(browser(), url);
1609 observer->WaitForFinished();
1610
1611 // We should have one download, in history, and it should
1612 // still be dangerous.
1613 std::vector<DownloadItem*> downloads;
1614 GetPersistentDownloads(browser(), &downloads);
1615 ASSERT_EQ(1u, downloads.size());
1616 DownloadItem* download = downloads[0];
1617 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1618 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1619 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1620 CheckDownloadUI(browser(), true, true, file);
1621
1622 // See if accepting completes the download and changes the safety
1623 // state.
1624 scoped_ptr<DownloadsObserver> completion_observer(
1625 CreateDangerousWaiter(browser(), 1));
1626 AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
1627 download->id());
1628 completion_observer->WaitForFinished();
1629
1630 GetPersistentDownloads(browser(), &downloads);
1631 ASSERT_EQ(1u, downloads.size());
1632 ASSERT_EQ(downloads[0], download);
1633 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
1634 EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
1635 CheckDownloadUI(browser(), true, true, file);
1636 }
1637
1638 // Confirm that a dangerous download that gets a file error before
1639 // completion ends in the right state (currently cancelled because file
1640 // errors are non-resumable). Note that this is really testing
1641 // to make sure errors from the final rename are propagated properly.
1642 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousFileError) {
1643 ASSERT_TRUE(InitialSetup(false));
1644 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1645 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1646
1647 EXPECT_EQ(1, browser()->tab_count());
1648
1649 scoped_ptr<DownloadsObserver> observer(
1650 CreateInProgressWaiter(browser(), 1));
1651 ui_test_utils::NavigateToURL(browser(), url);
1652 observer->WaitForFinished();
1653
1654 // We should have one download, in history, and it should
1655 // still be dangerous.
1656 std::vector<DownloadItem*> downloads;
1657 GetPersistentDownloads(browser(), &downloads);
1658 ASSERT_EQ(1u, downloads.size());
1659 DownloadItem* download = downloads[0];
1660 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1661 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1662 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1663 CheckDownloadUI(browser(), true, true, file);
1664
1665 // Accept it after nuking the directory into which it's being downloaded;
1666 // that should complete the download with an error.
1667 DeleteDownloadsDirectory();
1668 scoped_ptr<DownloadsObserver> completion_observer(
1669 CreateDangerousWaiter(browser(), 1));
1670 AcceptDangerousDownload(browser()->profile()->GetDownloadManager(),
1671 download->id());
1672 completion_observer->WaitForFinished();
1673
1674 GetPersistentDownloads(browser(), &downloads);
1675 ASSERT_EQ(1u, downloads.size());
1676 ASSERT_EQ(downloads[0], download);
1677 // Persistent errors currently -> CANCELLED.
1678 EXPECT_EQ(DownloadItem::CANCELLED, download->state());
1679 EXPECT_EQ(DownloadItem::DANGEROUS_BUT_VALIDATED, download->safety_state());
1680 CheckDownloadUI(browser(), true, true, file);
1681 }
1682
1683 // Confirm that declining a dangerous download erases it from living memory.
1684 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadDangerousDecline) {
1685 ASSERT_TRUE(InitialSetup(false));
1686 FilePath file(FILE_PATH_LITERAL("download-dangerous.jar"));
1687 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1688
1689 EXPECT_EQ(1, browser()->tab_count());
1690
1691 scoped_ptr<DownloadsObserver> observer(
1692 CreateInProgressWaiter(browser(), 1));
1693 ui_test_utils::NavigateToURL(browser(), url);
1694 observer->WaitForFinished();
1695
1696 // We should have one download, in history, and it should
1697 // still be dangerous.
1698 std::vector<DownloadItem*> downloads;
1699 GetPersistentDownloads(browser(), &downloads);
1700 ASSERT_EQ(1u, downloads.size());
1701 DownloadItem* download = downloads[0];
1702 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
1703 EXPECT_EQ(DownloadItem::DANGEROUS, download->safety_state());
1704 EXPECT_EQ(DownloadItem::DANGEROUS_FILE, download->GetDangerType());
1705 CheckDownloadUI(browser(), true, true, file);
1706
1707 DenyDangerousDownload(browser()->profile()->GetDownloadManager(),
1708 download->id());
1709
1710 GetPersistentDownloads(browser(), &downloads);
1711 ASSERT_EQ(0u, downloads.size());
1712 CheckDownloadUI(browser(), false, true, file);
1713 }
1714
1715 // Fail a download with a network error partway through, and make sure the
1716 // state is INTERRUPTED and the error is propagated.
1717 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadInterrupted) {
1718 ASSERT_TRUE(InitialSetup(false));
1719 GURL url(URLRequestSlowDownloadJob::kKnownSizeUrl);
1720
1721 scoped_ptr<DownloadsObserver> observer(
1722 CreateInProgressWaiter(browser(), 1));
1723 ui_test_utils::NavigateToURL(browser(), url);
1724 observer->WaitForFinished();
1725
1726 std::vector<DownloadItem*> downloads;
1727 GetPersistentDownloads(browser(), &downloads);
1728 ASSERT_EQ(1u, downloads.size());
1729 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->state());
1730 FilePath filename;
1731 net::FileURLToFilePath(url, &filename);
1732 CheckDownloadUI(browser(), true, true, filename);
1733
1734 // Fail the download
1735 GURL error_url(URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
1736 ui_test_utils::NavigateToURL(browser(), error_url);
1737 MessageLoopForUI::current()->RunAllPending();
1738
1739 // Should still be visible, with INTERRUPTED state.
1740 GetPersistentDownloads(browser(), &downloads);
1741 ASSERT_EQ(1u, downloads.size());
1742 DownloadItem* download = downloads[0];
1743 ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
1744 // TODO(rdsmith): Confirm error provided by URLRequest is shown
1745 // in DownloadItem.
1746 CheckDownloadUI(browser(), true, true, filename);
1747
1748 // Confirm cancel does nothing.
1749 download->Cancel();
1750 MessageLoopForUI::current()->RunAllPending();
1751
1752 GetPersistentDownloads(browser(), &downloads);
1753 ASSERT_EQ(1u, downloads.size());
1754 ASSERT_EQ(download, downloads[0]);
1755 ASSERT_EQ(DownloadItem::INTERRUPTED, download->state());
1756 CheckDownloadUI(browser(), true, true, filename);
1757
1758 // Confirm remove gets rid of it.
1759 download->Remove();
1760 download = NULL;
1761 MessageLoopForUI::current()->RunAllPending();
1762
1763 GetPersistentDownloads(browser(), &downloads);
1764 ASSERT_EQ(0u, downloads.size());
1765 CheckDownloadUI(browser(), false, true, FilePath());
1766 }
1767
1457 // Confirm a download makes it into the history properly. 1768 // Confirm a download makes it into the history properly.
1458 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1769 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1459 ASSERT_TRUE(InitialSetup(false)); 1770 ASSERT_TRUE(InitialSetup(false));
1460 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1771 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1461 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1772 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1462 FilePath origin_file(OriginFile(file)); 1773 FilePath origin_file(OriginFile(file));
1463 int64 origin_size; 1774 int64 origin_size;
1464 file_util::GetFileSize(origin_file, &origin_size); 1775 file_util::GetFileSize(origin_file, &origin_size);
1465 1776
1466 // Download the file and wait. We do not expect the Select File dialog. 1777 // Download the file and wait. We do not expect the Select File dialog.
1467 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1778 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1468 1779
1469 // Get details of what downloads have just happened. 1780 // Get details of what downloads have just happened.
1470 std::vector<DownloadItem*> downloads; 1781 std::vector<DownloadItem*> downloads;
1471 GetDownloads(browser(), &downloads); 1782 GetPersistentDownloads(browser(), &downloads);
1472 ASSERT_EQ(1u, downloads.size()); 1783 ASSERT_EQ(1u, downloads.size());
1473 int64 db_handle = downloads[0]->db_handle(); 1784 int64 db_handle = downloads[0]->db_handle();
1474 1785
1475 // Check state. 1786 // Check state.
1476 EXPECT_EQ(1, browser()->tab_count()); 1787 EXPECT_EQ(1, browser()->tab_count());
1477 CheckDownload(browser(), file, file); 1788 CheckDownload(browser(), file, file);
1478 CheckDownloadUI(browser(), true, true, file); 1789 CheckDownloadUI(browser(), true, true, file);
1479 1790
1480 // Check history results. 1791 // Check history results.
1481 DownloadsHistoryDataCollector history_collector( 1792 DownloadsHistoryDataCollector history_collector(
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1557 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file)); 1868 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file));
1558 1869
1559 // Mock out external opening on all downloads until end of test. 1870 // Mock out external opening on all downloads until end of test.
1560 MockDownloadOpeningObserver observer( 1871 MockDownloadOpeningObserver observer(
1561 browser()->profile()->GetDownloadManager()); 1872 browser()->profile()->GetDownloadManager());
1562 1873
1563 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1874 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1564 1875
1565 // Find the download and confirm it was opened. 1876 // Find the download and confirm it was opened.
1566 std::vector<DownloadItem*> downloads; 1877 std::vector<DownloadItem*> downloads;
1567 browser()->profile()->GetDownloadManager()->SearchDownloads( 1878 GetPersistentDownloads(browser(), &downloads);
1568 string16(), &downloads);
1569 ASSERT_EQ(1u, downloads.size()); 1879 ASSERT_EQ(1u, downloads.size());
1570 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state()); 1880 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state());
1571 EXPECT_TRUE(downloads[0]->opened()); 1881 EXPECT_TRUE(downloads[0]->opened());
1572 1882
1573 // As long as we're here, confirmed everything else is good. 1883 // As long as we're here, confirmed everything else is good.
1574 EXPECT_EQ(1, browser()->tab_count()); 1884 EXPECT_EQ(1, browser()->tab_count());
1575 CheckDownload(browser(), file, file); 1885 CheckDownload(browser(), file, file);
1576 // Download shelf should close. Download panel stays open on ChromeOS. 1886 // Download shelf should close. Download panel stays open on ChromeOS.
1577 CheckDownloadUI(browser(), false, true, file); 1887 CheckDownloadUI(browser(), false, true, file);
1578 } 1888 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1710 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen()); 2020 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen());
1711 2021
1712 // Download shelf should close. Download panel stays open on ChromeOS. 2022 // Download shelf should close. Download panel stays open on ChromeOS.
1713 CheckDownloadUI(browser(), false, true, FilePath()); 2023 CheckDownloadUI(browser(), false, true, FilePath());
1714 2024
1715 // Check that the extension was installed. 2025 // Check that the extension was installed.
1716 ExtensionService* extension_service = 2026 ExtensionService* extension_service =
1717 browser()->profile()->GetExtensionService(); 2027 browser()->profile()->GetExtensionService();
1718 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false)); 2028 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false));
1719 } 2029 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698