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

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(int32 /* id */, FilePath& path) {
155 select_file_dialog_seen_ = true; 160 select_file_dialog_seen_ = true;
161 suggested_path_ = path;
156 SignalIfFinished(); 162 SignalIfFinished();
157 } 163 }
158 164
159 private: 165 private:
160 // Called when we know that a download item is in a final state. 166 // 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 167 // 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 168 // final state; multiple notifications may occur once the item is in
163 // that state. So we keep our own track of transitions into final. 169 // that state. So we keep our own track of transitions into final.
164 void DownloadInFinalState(DownloadItem* download) { 170 void DownloadInFinalState(DownloadItem* download) {
165 if (finished_downloads_.find(download) != finished_downloads_.end()) { 171 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. 217 // The state on which to consider the DownloadItem finished.
212 DownloadItem::DownloadState download_finished_state_; 218 DownloadItem::DownloadState download_finished_state_;
213 219
214 // True if we should transition the DownloadsObserver to finished if 220 // True if we should transition the DownloadsObserver to finished if
215 // the select file dialog comes up. 221 // the select file dialog comes up.
216 bool finish_on_select_file_; 222 bool finish_on_select_file_;
217 223
218 // True if we've seen the select file dialog. 224 // True if we've seen the select file dialog.
219 bool select_file_dialog_seen_; 225 bool select_file_dialog_seen_;
220 226
227 // The suggested file path in the select file dialog.
228 FilePath suggested_path_;
229
221 DISALLOW_COPY_AND_ASSIGN(DownloadsObserver); 230 DISALLOW_COPY_AND_ASSIGN(DownloadsObserver);
222 }; 231 };
223 232
224 // WaitForFlush() returns after: 233 // WaitForFlush() returns after:
225 // * There are no IN_PROGRESS download items remaining on the 234 // * There are no IN_PROGRESS download items remaining on the
226 // DownloadManager. 235 // DownloadManager.
227 // * There have been two round trip messages through the file and 236 // * There have been two round trip messages through the file and
228 // IO threads. 237 // IO threads.
229 // This almost certainly means that a Download cancel has propagated through 238 // This almost certainly means that a Download cancel has propagated through
230 // the system. 239 // the system.
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 if (!downloads_directory_.CreateUniqueTempDir()) 469 if (!downloads_directory_.CreateUniqueTempDir())
461 return false; 470 return false;
462 471
463 browser->profile()->GetPrefs()->SetFilePath( 472 browser->profile()->GetPrefs()->SetFilePath(
464 prefs::kDownloadDefaultDirectory, 473 prefs::kDownloadDefaultDirectory,
465 downloads_directory_.path()); 474 downloads_directory_.path());
466 475
467 return true; 476 return true;
468 } 477 }
469 478
479 // Delete the default folder for downloaded files.
480 bool DeleteDownloadsDirectory() {
481 return downloads_directory_.Delete();
482 }
483
470 DownloadPrefs* GetDownloadPrefs(Browser* browser) { 484 DownloadPrefs* GetDownloadPrefs(Browser* browser) {
471 return browser->profile()->GetDownloadManager()->download_prefs(); 485 return browser->profile()->GetDownloadManager()->download_prefs();
472 } 486 }
473 487
474 FilePath GetDownloadDirectory(Browser* browser) { 488 FilePath GetDownloadDirectory(Browser* browser) {
475 DownloadManager* download_mananger = 489 DownloadManager* download_mananger =
476 browser->profile()->GetDownloadManager(); 490 browser->profile()->GetDownloadManager();
477 return download_mananger->download_prefs()->download_path(); 491 return download_mananger->download_prefs()->download_path();
478 } 492 }
479 493
(...skipping 16 matching lines...) Expand all
496 browser->profile()->GetDownloadManager(); 510 browser->profile()->GetDownloadManager();
497 return new DownloadsObserver( 511 return new DownloadsObserver(
498 download_manager, num_downloads, 512 download_manager, num_downloads,
499 DownloadItem::IN_PROGRESS, // Has started 513 DownloadItem::IN_PROGRESS, // Has started
500 true); // Bail on select file 514 true); // Bail on select file
501 } 515 }
502 516
503 // Download |url|, then wait for the download to finish. 517 // Download |url|, then wait for the download to finish.
504 // |disposition| indicates where the navigation occurs (current tab, new 518 // |disposition| indicates where the navigation occurs (current tab, new
505 // foreground tab, etc). 519 // foreground tab, etc).
506 // |expectation| indicates whether or not a Select File dialog should be 520 // |expectation| indicates whether or not a select file dialog should be
507 // open when the download is finished, or if we don't care. 521 // open when the download is finished, or if we don't care.
508 // If the dialog appears, the routine exits. The only effect |expectation| 522 // If the dialog appears, the routine exits. The only effect |expectation|
509 // has is whether or not the test succeeds. 523 // 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 524 // |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. 525 // values in the ui_test_utils::BrowserTestWaitFlags enum.
512 void DownloadAndWaitWithDisposition(Browser* browser, 526 void DownloadAndWaitWithDisposition(Browser* browser,
513 const GURL& url, 527 const GURL& url,
514 WindowOpenDisposition disposition, 528 WindowOpenDisposition disposition,
515 SelectExpectation expectation, 529 SelectExpectation expectation,
516 int browser_test_flags) { 530 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 785 // Files for these tests are found in DIR_TEST_DATA (currently
772 // "chrome\test\data\", see chrome_paths.cc). 786 // "chrome\test\data\", see chrome_paths.cc).
773 // Mock responses have extension .mock-http-headers appended to the file name. 787 // Mock responses have extension .mock-http-headers appended to the file name.
774 788
775 // Download a file due to the associated MIME type. 789 // Download a file due to the associated MIME type.
776 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { 790 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
777 ASSERT_TRUE(InitialSetup(false)); 791 ASSERT_TRUE(InitialSetup(false));
778 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 792 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
779 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 793 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
780 794
781 // Download the file and wait. We do not expect the Select File dialog. 795 // Download the file and wait. We do not expect the select file dialog.
782 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 796 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
783 797
784 // Check state. 798 // Check state.
785 EXPECT_EQ(1, browser()->tab_count()); 799 EXPECT_EQ(1, browser()->tab_count());
786 CheckDownload(browser(), file, file); 800 CheckDownload(browser(), file, file);
787 EXPECT_TRUE(IsDownloadUIVisible(browser())); 801 EXPECT_TRUE(IsDownloadUIVisible(browser()));
788 } 802 }
789 803
804 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadedDirectory) {
805 ASSERT_TRUE(InitialSetup(false));
806 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
807 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
808
809 // Download the file and wait. We do not expect the select file dialog.
810 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
811
812 FilePath file_path(DestinationFile(browser(), file));
813 EXPECT_TRUE(file_util::PathExists(file_path));
814 CheckDownload(browser(), file, file);
815 EXPECT_EQ(1, browser()->tab_count());
816 EXPECT_TRUE(IsDownloadUIVisible(browser()));
817
818 // Delete the default folder for downloaded files.
Paweł Hajdan Jr. 2011/05/31 17:15:53 Do we only need to do the setup above to get the f
haraken1 2011/06/02 09:13:22 Done.
819 ASSERT_TRUE(DeleteDownloadsDirectory());
820 ASSERT_FALSE(file_util::PathExists(file_path));
821
822 // Download the file and wait. We expect the select file dialog.
823 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
824
825 FilePath default_downloads_path;
826 PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_downloads_path);
Paweł Hajdan Jr. 2011/05/31 17:15:53 Please check the return value.
haraken1 2011/06/02 09:13:22 Done.
827 file_path = default_downloads_path.Append(file);
828 EXPECT_FALSE(file_util::PathExists(file_path));
Randy Smith (Not in Mondays) 2011/05/31 23:03:10 Would it be also appropriate to confirm that we di
haraken1 2011/06/02 09:13:22 Done.
829 EXPECT_EQ(1, browser()->tab_count());
830
831 scoped_ptr<DownloadsObserver> observer(CreateWaiter(browser(), 1));
832 ui_test_utils::NavigateToURLWithDisposition(
Paweł Hajdan Jr. 2011/05/31 17:15:53 Why do we have a second navigation to |url|? By "f
haraken1 2011/06/02 09:13:22 I cannot yet say the exact reason but this Navigat
Randy Smith (Not in Mondays) 2011/06/02 19:13:57 Haraken: Let's figure out why the test isn't behav
haraken1 2011/06/03 06:50:26 Ahh, I got it. Thank you. After all, for simplic
833 browser(), url, CURRENT_TAB,
834 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
835
836 // Waits for the download to complete.
837 observer->WaitForFinished();
838
839 EXPECT_TRUE(observer->IsSeenSelectFileDialogWithPath(file_path));
840 }
841
790 #if defined(OS_WIN) 842 #if defined(OS_WIN)
791 // Download a file and confirm that the zone identifier (on windows) 843 // Download a file and confirm that the zone identifier (on windows)
792 // is set to internet. 844 // is set to internet.
793 IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) { 845 IN_PROC_BROWSER_TEST_F(DownloadTest, CheckInternetZone) {
794 ASSERT_TRUE(InitialSetup(false)); 846 ASSERT_TRUE(InitialSetup(false));
795 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 847 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
796 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 848 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
797 849
798 // Download the file and wait. We do not expect the Select File dialog. 850 // Download the file and wait. We do not expect the select file dialog.
799 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 851 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
800 852
801 // Check state. Special file state must be checked before CheckDownload, 853 // Check state. Special file state must be checked before CheckDownload,
802 // as CheckDownload will delete the output file. 854 // as CheckDownload will delete the output file.
803 EXPECT_EQ(1, browser()->tab_count()); 855 EXPECT_EQ(1, browser()->tab_count());
804 FilePath downloaded_file(DestinationFile(browser(), file)); 856 FilePath downloaded_file(DestinationFile(browser(), file));
805 if (file_util::VolumeSupportsADS(downloaded_file)) 857 if (file_util::VolumeSupportsADS(downloaded_file))
806 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file)); 858 EXPECT_TRUE(file_util::HasInternetZoneIdentifier(downloaded_file));
807 CheckDownload(browser(), file, file); 859 CheckDownload(browser(), file, file);
808 EXPECT_TRUE(IsDownloadUIVisible(browser())); 860 EXPECT_TRUE(IsDownloadUIVisible(browser()));
809 } 861 }
810 #endif 862 #endif
811 863
812 // Put up a Select File dialog when the file is downloaded, due to its MIME 864 // Put up a select file dialog when the file is downloaded, due to its MIME
813 // type. 865 // type.
814 // 866 //
815 // This test runs correctly, but leaves behind turds in the test user's 867 // 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 868 // download directory because of http://crbug.com/62099. No big loss; it
817 // was primarily confirming DownloadsObserver wait on select file dialog 869 // was primarily confirming DownloadsObserver wait on select file dialog
818 // functionality anyway. 870 // functionality anyway.
819 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) { 871 IN_PROC_BROWSER_TEST_F(DownloadTest, DISABLED_DownloadMimeTypeSelect) {
820 ASSERT_TRUE(InitialSetup(true)); 872 ASSERT_TRUE(InitialSetup(true));
821 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 873 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
822 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 874 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
823 875
824 // Download the file and wait. We expect the Select File dialog to appear 876 // Download the file and wait. We expect the select file dialog to appear
825 // due to the MIME type. 877 // due to the MIME type.
826 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG); 878 DownloadAndWait(browser(), url, EXPECT_SELECT_DIALOG);
827 879
828 // Check state. 880 // Check state.
829 EXPECT_EQ(1, browser()->tab_count()); 881 EXPECT_EQ(1, browser()->tab_count());
830 // Since we exited while the Select File dialog was visible, there should not 882 // 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. 883 // be anything in the download shelf and so it should not be visible.
832 EXPECT_FALSE(IsDownloadUIVisible(browser())); 884 EXPECT_FALSE(IsDownloadUIVisible(browser()));
833 } 885 }
834 886
835 // Access a file with a viewable mime-type, verify that a download 887 // Access a file with a viewable mime-type, verify that a download
836 // did not initiate. 888 // did not initiate.
837 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) { 889 IN_PROC_BROWSER_TEST_F(DownloadTest, NoDownload) {
838 ASSERT_TRUE(InitialSetup(false)); 890 ASSERT_TRUE(InitialSetup(false));
839 FilePath file(FILE_PATH_LITERAL("download-test2.html")); 891 FilePath file(FILE_PATH_LITERAL("download-test2.html"));
840 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 892 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
1299 1351
1300 // Confirm a download makes it into the history properly. 1352 // Confirm a download makes it into the history properly.
1301 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { 1353 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
1302 ASSERT_TRUE(InitialSetup(false)); 1354 ASSERT_TRUE(InitialSetup(false));
1303 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); 1355 FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
1304 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1356 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1305 FilePath origin_file(OriginFile(file)); 1357 FilePath origin_file(OriginFile(file));
1306 int64 origin_size; 1358 int64 origin_size;
1307 file_util::GetFileSize(origin_file, &origin_size); 1359 file_util::GetFileSize(origin_file, &origin_size);
1308 1360
1309 // Download the file and wait. We do not expect the Select File dialog. 1361 // Download the file and wait. We do not expect the select file dialog.
1310 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG); 1362 DownloadAndWait(browser(), url, EXPECT_NO_SELECT_DIALOG);
1311 1363
1312 // Get details of what downloads have just happened. 1364 // Get details of what downloads have just happened.
1313 std::vector<DownloadItem*> downloads; 1365 std::vector<DownloadItem*> downloads;
1314 GetDownloads(browser(), &downloads); 1366 GetDownloads(browser(), &downloads);
1315 ASSERT_EQ(1u, downloads.size()); 1367 ASSERT_EQ(1u, downloads.size());
1316 int64 db_handle = downloads[0]->db_handle(); 1368 int64 db_handle = downloads[0]->db_handle();
1317 1369
1318 // Check state. 1370 // Check state.
1319 EXPECT_EQ(1, browser()->tab_count()); 1371 EXPECT_EQ(1, browser()->tab_count());
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1417 string16(), &downloads); 1469 string16(), &downloads);
1418 ASSERT_EQ(1u, downloads.size()); 1470 ASSERT_EQ(1u, downloads.size());
1419 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state()); 1471 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->state());
1420 EXPECT_TRUE(downloads[0]->opened()); 1472 EXPECT_TRUE(downloads[0]->opened());
1421 1473
1422 // As long as we're here, confirmed everything else is good. 1474 // As long as we're here, confirmed everything else is good.
1423 EXPECT_EQ(1, browser()->tab_count()); 1475 EXPECT_EQ(1, browser()->tab_count());
1424 CheckDownload(browser(), file, file); 1476 CheckDownload(browser(), file, file);
1425 EXPECT_FALSE(IsDownloadUIVisible(browser())); // Auto-opened. 1477 EXPECT_FALSE(IsDownloadUIVisible(browser())); // Auto-opened.
1426 } 1478 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/download/download_manager.h » ('j') | chrome/browser/download/download_manager.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698