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

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

Issue 7859001: Added ability to set ChromeDownloadManagerDelegate for testing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Various nits from self-review. Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/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.h" 10 #include "base/stl_util.h"
11 #include "base/test/test_file_util.h" 11 #include "base/test/test_file_util.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/download/chrome_download_manager_delegate.h"
14 #include "chrome/browser/download/download_crx_util.h" 15 #include "chrome/browser/download/download_crx_util.h"
15 #include "chrome/browser/download/download_history.h" 16 #include "chrome/browser/download/download_history.h"
16 #include "chrome/browser/download/download_prefs.h" 17 #include "chrome/browser/download/download_prefs.h"
17 #include "chrome/browser/download/download_shelf.h" 18 #include "chrome/browser/download/download_shelf.h"
18 #include "chrome/browser/download/download_util.h" 19 #include "chrome/browser/download/download_util.h"
19 #include "chrome/browser/extensions/extension_install_ui.h" 20 #include "chrome/browser/extensions/extension_install_ui.h"
20 #include "chrome/browser/extensions/extension_service.h" 21 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/history/history.h" 22 #include "chrome/browser/history/history.h"
22 #include "chrome/browser/prefs/pref_service.h" 23 #include "chrome/browser/prefs/pref_service.h"
23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 } 484 }
484 485
485 ResourceDispatcherHost* resource_dispatcher_host_; 486 ResourceDispatcherHost* resource_dispatcher_host_;
486 DownloadFileManager* download_file_manager_; 487 DownloadFileManager* download_file_manager_;
487 int rdh_pending_requests_; 488 int rdh_pending_requests_;
488 int dfm_pending_downloads_; 489 int dfm_pending_downloads_;
489 490
490 DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector); 491 DISALLOW_COPY_AND_ASSIGN(CancelTestDataCollector);
491 }; 492 };
492 493
494 class PickSuggestedFileDelegate : public ChromeDownloadManagerDelegate {
495 public:
496 explicit PickSuggestedFileDelegate(Profile* profile)
497 : ChromeDownloadManagerDelegate(profile) {
498 SetDownloadManager(profile->GetDownloadManager());
499 }
500
501 virtual void ChooseDownloadPath(TabContents* tab_contents,
502 const FilePath& suggested_path,
503 void* data) OVERRIDE {
ahendrickson 2011/09/08 21:24:40 Clang may not like the OVERRIDE here.
Randy Smith (Not in Mondays) 2011/09/08 21:27:12 I'll run the try bot; thanks for the suggestion.
504 if (download_manager_)
505 download_manager_->FileSelected(suggested_path, data);
506 }
507 };
508
509 // Get History Information.
510 class DownloadsHistoryDataCollector {
511 public:
512 DownloadsHistoryDataCollector(int64 download_db_handle,
513 DownloadManager* manager)
514 : result_valid_(false),
515 download_db_handle_(download_db_handle) {
516 HistoryService* hs =
517 Profile::FromBrowserContext(manager->browser_context())->
518 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();
ahendrickson 2011/09/08 21:24:40 I've never liked running a message loop inside a c
Randy Smith (Not in Mondays) 2011/09/08 21:27:12 This wasn't something I changed in this CL (moved
benjhayden 2011/09/12 16:32:25 Maybe just a TODO then?
Randy Smith (Not in Mondays) 2011/09/12 17:52:58 Sure (I don't find myself caring much, but it prob
529 }
530
531 bool GetDownloadsHistoryEntry(DownloadPersistentStoreInfo* result) {
532 DCHECK(result);
533 *result = result_;
534 return result_valid_;
535 }
536
537 private:
538 void OnQueryDownloadsComplete(
539 std::vector<DownloadPersistentStoreInfo>* entries) {
540 result_valid_ = false;
541 for (std::vector<DownloadPersistentStoreInfo>::const_iterator it =
542 entries->begin();
543 it != entries->end(); ++it) {
544 if (it->db_handle == download_db_handle_) {
545 result_ = *it;
546 result_valid_ = true;
547 }
548 }
549 MessageLoopForUI::current()->Quit();
550 }
551
552 DownloadPersistentStoreInfo result_;
553 bool result_valid_;
554 int64 download_db_handle_;
555 CancelableRequestConsumer callback_consumer_;
556
557 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
558 };
559
560 // Mock that simulates a permissions dialog where the user denies
561 // permission to install. TODO(skerner): This could be shared with
562 // extensions tests. Find a common place for this class.
563 class MockAbortExtensionInstallUI : public ExtensionInstallUI {
564 public:
565 MockAbortExtensionInstallUI() : ExtensionInstallUI(NULL) {}
566
567 // Simulate a user abort on an extension installation.
568 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
569 delegate->InstallUIAbort(true);
570 MessageLoopForUI::current()->Quit();
571 }
572
573 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
574 virtual void OnInstallFailure(const std::string& error) {}
575 };
576
577 // Mock that simulates a permissions dialog where the user allows
578 // installation.
579 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
580 public:
581 explicit MockAutoConfirmExtensionInstallUI(Profile* profile)
582 : ExtensionInstallUI(profile) {}
583
584 // Proceed without confirmation prompt.
585 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
586 delegate->InstallUIProceed();
587 }
588
589 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
590 virtual void OnInstallFailure(const std::string& error) {}
591 };
592
593 } // namespace
594
595 // While an object of this class exists, it will mock out download
596 // opening for all downloads created on the specified download manager.
597 class MockDownloadOpeningObserver : public DownloadManager::Observer {
598 public:
599 explicit MockDownloadOpeningObserver(DownloadManager* manager)
600 : download_manager_(manager) {
601 download_manager_->AddObserver(this);
602 }
603
604 ~MockDownloadOpeningObserver() {
605 download_manager_->RemoveObserver(this);
606 }
607
608 // DownloadManager::Observer
609 virtual void ModelChanged() {
610 std::vector<DownloadItem*> downloads;
611 download_manager_->SearchDownloads(string16(), &downloads);
612
613 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
614 it != downloads.end(); ++it) {
615 (*it)->TestMockDownloadOpen();
616 }
617 }
618
619 private:
620 DownloadManager* download_manager_;
621
622 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
623 };
624
493 class DownloadTest : public InProcessBrowserTest { 625 class DownloadTest : public InProcessBrowserTest {
494 public: 626 public:
495 enum SelectExpectation { 627 enum SelectExpectation {
496 EXPECT_NO_SELECT_DIALOG = -1, 628 EXPECT_NO_SELECT_DIALOG = -1,
497 EXPECT_NOTHING, 629 EXPECT_NOTHING,
498 EXPECT_SELECT_DIALOG 630 EXPECT_SELECT_DIALOG
499 }; 631 };
500 632
501 DownloadTest() { 633 DownloadTest() {
502 EnableDOMAutomation(); 634 EnableDOMAutomation();
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
807 } 939 }
808 static void ExpectWindowCountAfterDownload(size_t expected) { 940 static void ExpectWindowCountAfterDownload(size_t expected) {
809 #if defined(OS_CHROMEOS) 941 #if defined(OS_CHROMEOS)
810 // On ChromeOS, a download panel is created to display 942 // On ChromeOS, a download panel is created to display
811 // download information, and this counts as a window. 943 // download information, and this counts as a window.
812 expected++; 944 expected++;
813 #endif 945 #endif
814 EXPECT_EQ(expected, BrowserList::size()); 946 EXPECT_EQ(expected, BrowserList::size());
815 } 947 }
816 948
949 // Arrange for select file calls on the given browser from the
950 // download manager to always choose the suggested file.
951 void NullSelectFile(Browser* browser) {
952 PickSuggestedFileDelegate* new_delegate =
953 new PickSuggestedFileDelegate(browser->profile());
954
955 DownloadManager* manager = browser->profile()->GetDownloadManager();
956
957 new_delegate->SetDownloadManager(manager);
958 manager->set_delegate(new_delegate);
959
960 // Gives ownership to Profile.
961 browser->profile()->SetDownloadManagerDelegate(new_delegate);
962 }
963
817 private: 964 private:
818 // Location of the test data. 965 // Location of the test data.
819 FilePath test_dir_; 966 FilePath test_dir_;
820 967
821 // Location of the downloads directory for these tests 968 // Location of the downloads directory for these tests
822 ScopedTempDir downloads_directory_; 969 ScopedTempDir downloads_directory_;
823 }; 970 };
824 971
825 // Get History Information.
826 class DownloadsHistoryDataCollector {
827 public:
828 DownloadsHistoryDataCollector(int64 download_db_handle,
829 DownloadManager* manager)
830 : result_valid_(false),
831 download_db_handle_(download_db_handle) {
832 HistoryService* hs =
833 Profile::FromBrowserContext(manager->browser_context())->
834 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(DownloadPersistentStoreInfo* result) {
848 DCHECK(result);
849 *result = result_;
850 return result_valid_;
851 }
852
853 private:
854 void OnQueryDownloadsComplete(
855 std::vector<DownloadPersistentStoreInfo>* entries) {
856 result_valid_ = false;
857 for (std::vector<DownloadPersistentStoreInfo>::const_iterator it =
858 entries->begin();
859 it != entries->end(); ++it) {
860 if (it->db_handle == download_db_handle_) {
861 result_ = *it;
862 result_valid_ = true;
863 }
864 }
865 MessageLoopForUI::current()->Quit();
866 }
867
868 DownloadPersistentStoreInfo result_;
869 bool result_valid_;
870 int64 download_db_handle_;
871 CancelableRequestConsumer callback_consumer_;
872
873 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
874 };
875
876 // Mock that simulates a permissions dialog where the user denies
877 // permission to install. TODO(skerner): This could be shared with
878 // extensions tests. Find a common place for this class.
879 class MockAbortExtensionInstallUI : public ExtensionInstallUI {
880 public:
881 MockAbortExtensionInstallUI() : ExtensionInstallUI(NULL) {}
882
883 // Simulate a user abort on an extension installation.
884 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
885 delegate->InstallUIAbort(true);
886 MessageLoopForUI::current()->Quit();
887 }
888
889 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
890 virtual void OnInstallFailure(const std::string& error) {}
891 };
892
893 // Mock that simulates a permissions dialog where the user allows
894 // installation.
895 class MockAutoConfirmExtensionInstallUI : public ExtensionInstallUI {
896 public:
897 explicit MockAutoConfirmExtensionInstallUI(Profile* profile)
898 : ExtensionInstallUI(profile) {}
899
900 // Proceed without confirmation prompt.
901 virtual void ConfirmInstall(Delegate* delegate, const Extension* extension) {
902 delegate->InstallUIProceed();
903 }
904
905 virtual void OnInstallSuccess(const Extension* extension, SkBitmap* icon) {}
906 virtual void OnInstallFailure(const std::string& error) {}
907 };
908
909 } // namespace
910
911 // While an object of this class exists, it will mock out download
912 // opening for all downloads created on the specified download manager.
913 class MockDownloadOpeningObserver : public DownloadManager::Observer {
914 public:
915 explicit MockDownloadOpeningObserver(DownloadManager* manager)
916 : download_manager_(manager) {
917 download_manager_->AddObserver(this);
918 }
919
920 ~MockDownloadOpeningObserver() {
921 download_manager_->RemoveObserver(this);
922 }
923
924 // DownloadManager::Observer
925 virtual void ModelChanged() {
926 std::vector<DownloadItem*> downloads;
927 download_manager_->SearchDownloads(string16(), &downloads);
928
929 for (std::vector<DownloadItem*>::iterator it = downloads.begin();
930 it != downloads.end(); ++it) {
931 (*it)->TestMockDownloadOpen();
932 }
933 }
934
935 private:
936 DownloadManager* download_manager_;
937
938 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver);
939 };
940
941 // NOTES: 972 // NOTES:
942 // 973 //
943 // Files for these tests are found in DIR_TEST_DATA (currently 974 // Files for these tests are found in DIR_TEST_DATA (currently
944 // "chrome\test\data\", see chrome_paths.cc). 975 // "chrome\test\data\", see chrome_paths.cc).
945 // Mock responses have extension .mock-http-headers appended to the file name. 976 // Mock responses have extension .mock-http-headers appended to the file name.
946 977
947 // Download a file due to the associated MIME type. 978 // Download a file due to the associated MIME type.
948 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { 979 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
949 ASSERT_TRUE(InitialSetup(false)); 980 ASSERT_TRUE(InitialSetup(false));
950 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 981 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
(...skipping 23 matching lines...) Expand all
974 // as CheckDownload will delete the output file. 1005 // as CheckDownload will delete the output file.
975 EXPECT_EQ(1, browser()->tab_count()); 1006 EXPECT_EQ(1, browser()->tab_count());
976 FilePath downloaded_file(DestinationFile(browser(), file)); 1007 FilePath downloaded_file(DestinationFile(browser(), file));
977 if (file_util::VolumeSupportsADS(downloaded_file)) 1008 if (file_util::VolumeSupportsADS(downloaded_file))
978 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file)); 1009 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file));
979 CheckDownload(browser(), file, file); 1010 CheckDownload(browser(), file, file);
980 CheckDownloadUI(browser(), true, true, file); 1011 CheckDownloadUI(browser(), true, true, file);
981 } 1012 }
982 #endif 1013 #endif
983 1014
984 // Put up a Select File dialog when the file is downloaded, due to its MIME 1015 // Put up a Select File dialog when the file is downloaded, due to
985 // type. 1016 // downloads preferences settings.
986 // 1017 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeTypeSelect) {
987 // This test runs correctly, but leaves behind turds in the test user's
988 // download directory because of http://crbug.com/62099. No big loss; it
989 // was primarily confirming DownloadsObserver wait on select file dialog
990 // functionality anyway.
991 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
992 ASSERT_TRUE(InitialSetup(true)); 1018 ASSERT_TRUE(InitialSetup(true));
993 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1019 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
994 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1020 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
995 1021
1022 NullSelectFile(browser());
1023
996 // Download the file and wait. We expect the Select File dialog to appear 1024 // Download the file and wait. We expect the Select File dialog to appear
997 // due to the MIME type. 1025 // due to the MIME type, but we still wait until the download completes.
998 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG); 1026 scoped_ptr<DownloadsObserver> observer(
1027 new DownloadsObserver(
1028 browser()->profile()->GetDownloadManager(),
1029 1,
1030 DownloadItem::COMPLETE, // Really done
1031 false, // Continue on select file.
1032 ON_DANGEROUS_DOWNLOAD_FAIL));
1033 ui_test_utils::NavigateToURLWithDisposition(
1034 browser(), url, CURRENT_TAB,
1035 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1036 observer->WaitForFinished();
1037 EXPECT_TRUE(observer->select_file_dialog_seen());
999 1038
1000 // Check state. 1039 // Check state.
1001 EXPECT_EQ(1, browser()->tab_count()); 1040 EXPECT_EQ(1, browser()->tab_count());
1002 // Since we exited while the Select File dialog was visible, there should not 1041 CheckDownload(browser(), file, file);
1003 // be anything in the download shelf and so it should not be visible. 1042 CheckDownloadUI(browser(), true, true, file);
1004 CheckDownloadUI(browser(), false, false, FilePath());
1005 } 1043 }
1006 1044
1007 // Access a file with a viewable mime-type, verify that a download 1045 // Access a file with a viewable mime-type, verify that a download
1008 // did not initiate. 1046 // did not initiate.
1009 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) { 1047 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) {
1010 ASSERT_TRUE(InitialSetup(false)); 1048 ASSERT_TRUE(InitialSetup(false));
1011 FilePath file(FILE_PATH_LITERAL("download-test2.html")); 1049 FilePath file(FILE_PATH_LITERAL("download-test2.html"));
1012 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1050 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1013 FilePath file_path(DestinationFile(browser(), file)); 1051 FilePath file_path(DestinationFile(browser(), file));
1014 1052
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after
1730 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen()); 1768 EXPECT_EQ(1u, observer->NumDangerousDownloadsSeen());
1731 1769
1732 // Download shelf should close. Download panel stays open on ChromeOS. 1770 // Download shelf should close. Download panel stays open on ChromeOS.
1733 CheckDownloadUI(browser(), false, true, FilePath()); 1771 CheckDownloadUI(browser(), false, true, FilePath());
1734 1772
1735 // Check that the extension was installed. 1773 // Check that the extension was installed.
1736 ExtensionService* extension_service = 1774 ExtensionService* extension_service =
1737 browser()->profile()->GetExtensionService(); 1775 browser()->profile()->GetExtensionService();
1738 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false)); 1776 ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false));
1739 } 1777 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698