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

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

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Correct typo Created 9 years, 6 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/test/test_file_util.h" 10 #include "base/test/test_file_util.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 std::set<DownloadItem*>::iterator it = downloads_observed_.begin(); 84 std::set<DownloadItem*>::iterator it = downloads_observed_.begin();
85 for (; it != downloads_observed_.end(); ++it) { 85 for (; it != downloads_observed_.end(); ++it) {
86 (*it)->RemoveObserver(this); 86 (*it)->RemoveObserver(this);
87 } 87 }
88 download_manager_->RemoveObserver(this); 88 download_manager_->RemoveObserver(this);
89 } 89 }
90 90
91 // State accessors. 91 // State accessors.
92 bool select_file_dialog_seen() { return select_file_dialog_seen_; } 92 bool select_file_dialog_seen() { return select_file_dialog_seen_; }
93 93
94 // Checks if the select file dialog suggesting |path| was displayed.
95 bool IsSeenSelectFileDialogWithPath(FilePath& path) {
96 return select_file_dialog_seen_ && suggested_path_ == path;
97 }
98
94 // Wait for whatever state was specified in the constructor. 99 // Wait for whatever state was specified in the constructor.
95 void WaitForFinished() { 100 void WaitForFinished() {
96 if (!IsFinished()) { 101 if (!IsFinished()) {
97 waiting_ = true; 102 waiting_ = true;
98 ui_test_utils::RunMessageLoop(); 103 ui_test_utils::RunMessageLoop();
99 waiting_ = false; 104 waiting_ = false;
100 } 105 }
101 } 106 }
102 107
103 // Return true if everything's happened that we're configured for. 108 // Return true if everything's happened that we're configured for.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 // If it is finished and we are observing it, stop. 149 // If it is finished and we are observing it, stop.
145 if (finished_it != finished_downloads_.end() && 150 if (finished_it != finished_downloads_.end() &&
146 observed_it != downloads_observed_.end()) { 151 observed_it != downloads_observed_.end()) {
147 (*it)->RemoveObserver(this); 152 (*it)->RemoveObserver(this);
148 downloads_observed_.erase(observed_it); 153 downloads_observed_.erase(observed_it);
149 continue; 154 continue;
150 } 155 }
151 } 156 }
152 } 157 }
153 158
154 virtual void SelectFileDialogDisplayed(int32 /* id */) { 159 virtual void SelectFileDialogDisplayed(
160 int32 /* id */, FilePath& suggested_path) {
155 select_file_dialog_seen_ = true; 161 select_file_dialog_seen_ = true;
162 suggested_path_ = suggested_path;
156 SignalIfFinished(); 163 SignalIfFinished();
157 } 164 }
158 165
159 private: 166 private:
160 // Called when we know that a download item is in a final state. 167 // Called when we know that a download item is in a final state.
161 // Note that this is not the same as it first transitioning in to the 168 // Note that this is not the same as it first transitioning in to the
162 // final state; multiple notifications may occur once the item is in 169 // final state; multiple notifications may occur once the item is in
163 // that state. So we keep our own track of transitions into final. 170 // that state. So we keep our own track of transitions into final.
164 void DownloadInFinalState(DownloadItem* download) { 171 void DownloadInFinalState(DownloadItem* download) {
165 if (finished_downloads_.find(download) != finished_downloads_.end()) { 172 if (finished_downloads_.find(download) != finished_downloads_.end()) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // The state on which to consider the DownloadItem finished. 218 // The state on which to consider the DownloadItem finished.
212 DownloadItem::DownloadState download_finished_state_; 219 DownloadItem::DownloadState download_finished_state_;
213 220
214 // True if we should transition the DownloadsObserver to finished if 221 // True if we should transition the DownloadsObserver to finished if
215 // the select file dialog comes up. 222 // the select file dialog comes up.
216 bool finish_on_select_file_; 223 bool finish_on_select_file_;
217 224
218 // True if we've seen the select file dialog. 225 // True if we've seen the select file dialog.
219 bool select_file_dialog_seen_; 226 bool select_file_dialog_seen_;
220 227
228 // The suggested file path in the select file dialog.
229 FilePath suggested_path_;
230
221 DISALLOW_COPY_AND_ASSIGN(DownloadsObserver); 231 DISALLOW_COPY_AND_ASSIGN(DownloadsObserver);
222 }; 232 };
223 233
224 // WaitForFlush() returns after: 234 // WaitForFlush() returns after:
225 // * There are no IN_PROGRESS download items remaining on the 235 // * There are no IN_PROGRESS download items remaining on the
226 // DownloadManager. 236 // DownloadManager.
227 // * There have been two round trip messages through the file and 237 // * There have been two round trip messages through the file and
228 // IO threads. 238 // IO threads.
229 // This almost certainly means that a Download cancel has propagated through 239 // This almost certainly means that a Download cancel has propagated through
230 // the system. 240 // the system.
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 if (!downloads_directory_.CreateUniqueTempDir()) 470 if (!downloads_directory_.CreateUniqueTempDir())
461 return false; 471 return false;
462 472
463 browser->profile()->GetPrefs()->SetFilePath( 473 browser->profile()->GetPrefs()->SetFilePath(
464 prefs::kDownloadDefaultDirectory, 474 prefs::kDownloadDefaultDirectory,
465 downloads_directory_.path()); 475 downloads_directory_.path());
466 476
467 return true; 477 return true;
468 } 478 }
469 479
480 // Delete the default folder for downloaded files.
481 bool DeleteDownloadsDirectory() {
482 return downloads_directory_.Delete();
483 }
484
470 DownloadPrefs* GetDownloadPrefs(Browser* browser) { 485 DownloadPrefs* GetDownloadPrefs(Browser* browser) {
471 return browser->profile()->GetDownloadManager()->download_prefs(); 486 return browser->profile()->GetDownloadManager()->download_prefs();
472 } 487 }
473 488
474 FilePath GetDownloadDirectory(Browser* browser) { 489 FilePath GetDownloadDirectory(Browser* browser) {
475 DownloadManager* download_mananger = 490 DownloadManager* download_mananger =
476 browser->profile()->GetDownloadManager(); 491 browser->profile()->GetDownloadManager();
477 return download_mananger->download_prefs()->download_path(); 492 return download_mananger->download_prefs()->download_path();
478 } 493 }
479 494
(...skipping 16 matching lines...) Expand all
496 browser->profile()->GetDownloadManager(); 511 browser->profile()->GetDownloadManager();
497 return new DownloadsObserver( 512 return new DownloadsObserver(
498 download_manager, num_downloads, 513 download_manager, num_downloads,
499 DownloadItem::IN_PROGRESS, // Has started 514 DownloadItem::IN_PROGRESS, // Has started
500 true); // Bail on select file 515 true); // Bail on select file
501 } 516 }
502 517
503 // Download |url|, then wait for the download to finish. 518 // Download |url|, then wait for the download to finish.
504 // |disposition| indicates where the navigation occurs (current tab, new 519 // |disposition| indicates where the navigation occurs (current tab, new
505 // foreground tab, etc). 520 // foreground tab, etc).
506 // |expectation| indicates whether or not a Select File dialog should be 521 // |expectation| indicates whether or not a select file dialog should be
507 // open when the download is finished, or if we don't care. 522 // open when the download is finished, or if we don't care.
508 // If the dialog appears, the routine exits. The only effect |expectation| 523 // If the dialog appears, the routine exits. The only effect |expectation|
509 // has is whether or not the test succeeds. 524 // has is whether or not the test succeeds.
510 // |browser_test_flags| indicate what to wait for, and is an OR of 0 or more 525 // |browser_test_flags| indicate what to wait for, and is an OR of 0 or more
511 // values in the ui_test_utils::BrowserTestWaitFlags enum. 526 // values in the ui_test_utils::BrowserTestWaitFlags enum.
512 void DownloadAndWaitWithDisposition(Browser* browser, 527 void DownloadAndWaitWithDisposition(Browser* browser,
513 const GURL& url, 528 const GURL& url,
514 WindowOpenDisposition disposition, 529 WindowOpenDisposition disposition,
515 SelectExpectation expectation, 530 SelectExpectation expectation,
516 int browser_test_flags) { 531 int browser_test_flags) {
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 // Files for these tests are found in DIR_TEST_DATA (currently 786 // Files for these tests are found in DIR_TEST_DATA (currently
772 // "chrome\test\data\", see chrome_paths.cc). 787 // "chrome\test\data\", see chrome_paths.cc).
773 // Mock responses have extension .mock-http-headers appended to the file name. 788 // Mock responses have extension .mock-http-headers appended to the file name.
774 789
775 // Download a file due to the associated MIME type. 790 // Download a file due to the associated MIME type.
776 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { 791 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
777 ASSERT_TRUE(InitialSetup(false)); 792 ASSERT_TRUE(InitialSetup(false));
778 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 793 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
779 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 794 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
780 795
781 // Download the file and wait. We do not expect the Select File dialog. 796 // Download the file and wait. We do not expect the select file dialog.
782 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 797 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
783 798
784 // Check state. 799 // Check state.
785 EXPECT_EQ(1, browser()->tab_count()); 800 EXPECT_EQ(1, browser()->tab_count());
786 CheckDownload(browser(), file, file); 801 CheckDownload(browser(), file, file);
787 EXPECT_TRUE(IsDownloadUIVisible(browser())); 802 EXPECT_TRUE(IsDownloadUIVisible(browser()));
788 } 803 }
789 804
805 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadedFolder) {
806 ASSERT_TRUE(InitialSetup(false));
807 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
808 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
809 FilePath file_path(DestinationFile(browser(), file));
810
811 // Delete the default folder for downloaded files.
812 ASSERT_TRUE(DeleteDownloadsDirectory());
813 ASSERT_FALSE(file_util::PathExists(GetDownloadDirectory(browser())));
814
815 // Download the file and wait. We expect the select file dialog.
816 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
817
818 FilePath default_downloads_path;
819 ASSERT_TRUE(PathService::Get(
820 chrome::DIR_DEFAULT_DOWNLOADS, &default_downloads_path));
821 file_path = default_downloads_path.Append(file);
822 EXPECT_FALSE(file_util::PathExists(file_path));
823 EXPECT_FALSE(file_util::PathExists(GetDownloadDirectory(browser())));
824 EXPECT_EQ(1, browser()->tab_count());
825
826 scoped_ptr<DownloadsObserver> observer(CreateWaiter(browser(), 1));
827 ui_test_utils::NavigateToURLWithDisposition(
828 browser(), url, CURRENT_TAB,
829 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
830
831 // Waits for the download to complete.
832 observer->WaitForFinished();
833
834 EXPECT_TRUE(observer->IsSeenSelectFileDialogWithPath(file_path));
835 }
836
790 #if defined(OS_WIN) 837 #if defined(OS_WIN)
791 // Download a file and confirm that the zone identifier (on windows) 838 // Download a file and confirm that the zone identifier (on windows)
792 // is set to internet. 839 // is set to internet.
793 IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) { 840 IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
794 ASSERT_TRUE(InitialSetup(false)); 841 ASSERT_TRUE(InitialSetup(false));
795 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 842 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
796 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 843 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
797 844
798 // Download the file and wait. We do not expect the Select File dialog. 845 // Download the file and wait. We do not expect the select file dialog.
799 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 846 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
800 847
801 // Check state. Special file state must be checked before CheckDownload, 848 // Check state. Special file state must be checked before CheckDownload,
802 // as CheckDownload will delete the output file. 849 // as CheckDownload will delete the output file.
803 EXPECT_EQ(1, browser()->tab_count()); 850 EXPECT_EQ(1, browser()->tab_count());
804 FilePath downloaded_file(DestinationFile(browser(), file)); 851 FilePath downloaded_file(DestinationFile(browser(), file));
805 if (file_util::VolumeSupportsADS(downloaded_file)) 852 if (file_util::VolumeSupportsADS(downloaded_file))
806 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file)); 853 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file));
807 CheckDownload(browser(), file, file); 854 CheckDownload(browser(), file, file);
808 EXPECT_TRUE(IsDownloadUIVisible(browser())); 855 EXPECT_TRUE(IsDownloadUIVisible(browser()));
809 } 856 }
810 #endif 857 #endif
811 858
812 // Put up a Select File dialog when the file is downloaded, due to its MIME 859 // Put up a select file dialog when the file is downloaded, due to its MIME
813 // type. 860 // type.
814 // 861 //
815 // This test runs correctly, but leaves behind turds in the test user's 862 // This test runs correctly, but leaves behind turds in the test user's
816 // download directory because of http://crbug.com/62099. No big loss; it 863 // download directory because of http://crbug.com/62099. No big loss; it
817 // was primarily confirming DownloadsObserver wait on select file dialog 864 // was primarily confirming DownloadsObserver wait on select file dialog
818 // functionality anyway. 865 // functionality anyway.
819 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) { 866 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
820 ASSERT_TRUE(InitialSetup(true)); 867 ASSERT_TRUE(InitialSetup(true));
821 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 868 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
822 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 869 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
823 870
824 // Download the file and wait. We expect the Select File dialog to appear 871 // Download the file and wait. We expect the select file dialog to appear
825 // due to the MIME type. 872 // due to the MIME type.
826 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG); 873 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
827 874
828 // Check state. 875 // Check state.
829 EXPECT_EQ(1, browser()->tab_count()); 876 EXPECT_EQ(1, browser()->tab_count());
830 // Since we exited while the Select File dialog was visible, there should not 877 // Since we exited while the select file dialog was visible, there should not
831 // be anything in the download shelf and so it should not be visible. 878 // be anything in the download shelf and so it should not be visible.
832 EXPECT_FALSE(IsDownloadUIVisible(browser())); 879 EXPECT_FALSE(IsDownloadUIVisible(browser()));
833 } 880 }
834 881
835 // Access a file with a viewable mime-type, verify that a download 882 // Access a file with a viewable mime-type, verify that a download
836 // did not initiate. 883 // did not initiate.
837 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) { 884 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) {
838 ASSERT_TRUE(InitialSetup(false)); 885 ASSERT_TRUE(InitialSetup(false));
839 FilePath file(FILE_PATH_LITERAL("download-test2.html")); 886 FilePath file(FILE_PATH_LITERAL("download-test2.html"));
840 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 887 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
1299 1346
1300 // Confirm a download makes it into the history properly. 1347 // Confirm a download makes it into the history properly.
1301 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1348 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1302 ASSERT_TRUE(InitialSetup(false)); 1349 ASSERT_TRUE(InitialSetup(false));
1303 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1350 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1304 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1351 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1305 FilePath origin_file(OriginFile(file)); 1352 FilePath origin_file(OriginFile(file));
1306 int64 origin_size; 1353 int64 origin_size;
1307 file_util::GetFileSize(origin_file, &origin_size); 1354 file_util::GetFileSize(origin_file, &origin_size);
1308 1355
1309 // Download the file and wait. We do not expect the Select File dialog. 1356 // Download the file and wait. We do not expect the select file dialog.
1310 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1357 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1311 1358
1312 // Get details of what downloads have just happened. 1359 // Get details of what downloads have just happened.
1313 std::vector<DownloadItem*> downloads; 1360 std::vector<DownloadItem*> downloads;
1314 GetDownloads(browser(), &downloads); 1361 GetDownloads(browser(), &downloads);
1315 ASSERT_EQ(1u, downloads.size()); 1362 ASSERT_EQ(1u, downloads.size());
1316 int64 db_handle = downloads[0]->db_handle(); 1363 int64 db_handle = downloads[0]->db_handle();
1317 1364
1318 // Check state. 1365 // Check state.
1319 EXPECT_EQ(1, browser()->tab_count()); 1366 EXPECT_EQ(1, browser()->tab_count());
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1417 string16(), &downloads); 1464 string16(), &downloads);
1418 ASSERT_EQ(1u, downloads.size()); 1465 ASSERT_EQ(1u, downloads.size());
1419 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state()); 1466 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state());
1420 EXPECT_TRUE(downloads[0]->opened()); 1467 EXPECT_TRUE(downloads[0]->opened());
1421 1468
1422 // As long as we're here, confirmed everything else is good. 1469 // As long as we're here, confirmed everything else is good.
1423 EXPECT_EQ(1, browser()->tab_count()); 1470 EXPECT_EQ(1, browser()->tab_count());
1424 CheckDownload(browser(), file, file); 1471 CheckDownload(browser(), file, file);
1425 EXPECT_FALSE(IsDownloadUIVisible(browser())); // Auto-opened. 1472 EXPECT_FALSE(IsDownloadUIVisible(browser())); // Auto-opened.
1426 } 1473 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698