| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <sstream> | 5 #include <sstream> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/test/test_file_util.h" | 16 #include "base/test/test_file_util.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/app/chrome_command_ids.h" | 18 #include "chrome/app/chrome_command_ids.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/common/cancelable_request.h" | 20 #include "chrome/browser/common/cancelable_request.h" |
| 21 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 21 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 22 #include "chrome/browser/download/download_crx_util.h" | 22 #include "chrome/browser/download/download_crx_util.h" |
| 23 #include "chrome/browser/download/download_history.h" | 23 #include "chrome/browser/download/download_history.h" |
| 24 #include "chrome/browser/download/download_item_model.h" | 24 #include "chrome/browser/download/download_item_model.h" |
| 25 #include "chrome/browser/download/download_prefs.h" | 25 #include "chrome/browser/download/download_prefs.h" |
| 26 #include "chrome/browser/download/download_request_limiter.h" | 26 #include "chrome/browser/download/download_request_limiter.h" |
| 27 #include "chrome/browser/download/download_service.h" | 27 #include "chrome/browser/download/download_service.h" |
| 28 #include "chrome/browser/download/download_service_factory.h" | 28 #include "chrome/browser/download/download_service_factory.h" |
| 29 #include "chrome/browser/download/download_shelf.h" | 29 #include "chrome/browser/download/download_shelf.h" |
| 30 #include "chrome/browser/download/download_test_file_chooser_observer.h" | 30 #include "chrome/browser/download/download_test_file_activity_observer.h" |
| 31 #include "chrome/browser/download/download_util.h" | 31 #include "chrome/browser/download/download_util.h" |
| 32 #include "chrome/browser/extensions/extension_install_prompt.h" | 32 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 33 #include "chrome/browser/extensions/extension_service.h" | 33 #include "chrome/browser/extensions/extension_service.h" |
| 34 #include "chrome/browser/extensions/extension_system.h" | 34 #include "chrome/browser/extensions/extension_system.h" |
| 35 #include "chrome/browser/history/download_row.h" | 35 #include "chrome/browser/history/download_row.h" |
| 36 #include "chrome/browser/history/history.h" | 36 #include "chrome/browser/history/history.h" |
| 37 #include "chrome/browser/history/history_service_factory.h" | 37 #include "chrome/browser/history/history_service_factory.h" |
| 38 #include "chrome/browser/net/url_request_mock_util.h" | 38 #include "chrome/browser/net/url_request_mock_util.h" |
| 39 #include "chrome/browser/prefs/pref_service.h" | 39 #include "chrome/browser/prefs/pref_service.h" |
| 40 #include "chrome/browser/profiles/profile.h" | 40 #include "chrome/browser/profiles/profile.h" |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 DownloadTest() {} | 238 DownloadTest() {} |
| 239 | 239 |
| 240 virtual void SetUpOnMainThread() OVERRIDE { | 240 virtual void SetUpOnMainThread() OVERRIDE { |
| 241 BrowserThread::PostTask( | 241 BrowserThread::PostTask( |
| 242 BrowserThread::IO, FROM_HERE, | 242 BrowserThread::IO, FROM_HERE, |
| 243 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 243 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 244 ASSERT_TRUE(InitialSetup()); | 244 ASSERT_TRUE(InitialSetup()); |
| 245 } | 245 } |
| 246 | 246 |
| 247 virtual void CleanUpOnMainThread() OVERRIDE { | 247 virtual void CleanUpOnMainThread() OVERRIDE { |
| 248 // Needs to be torn down on the main thread. file_chooser_observer_ holds a | 248 // Needs to be torn down on the main thread. file_activity_observer_ holds a |
| 249 // reference to the ChromeDownloadManagerDelegate which should be destroyed | 249 // reference to the ChromeDownloadManagerDelegate which should be destroyed |
| 250 // on the UI thread. | 250 // on the UI thread. |
| 251 file_chooser_observer_.reset(); | 251 file_activity_observer_.reset(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 // Returning false indicates a failure of the setup, and should be asserted | 254 // Returning false indicates a failure of the setup, and should be asserted |
| 255 // in the caller. | 255 // in the caller. |
| 256 virtual bool InitialSetup() { | 256 virtual bool InitialSetup() { |
| 257 bool have_test_dir = PathService::Get(chrome::DIR_TEST_DATA, &test_dir_); | 257 bool have_test_dir = PathService::Get(chrome::DIR_TEST_DATA, &test_dir_); |
| 258 EXPECT_TRUE(have_test_dir); | 258 EXPECT_TRUE(have_test_dir); |
| 259 if (!have_test_dir) | 259 if (!have_test_dir) |
| 260 return false; | 260 return false; |
| 261 | 261 |
| 262 // Sanity check default values for window / tab count and shelf visibility. | 262 // Sanity check default values for window / tab count and shelf visibility. |
| 263 int window_count = BrowserList::size(); | 263 int window_count = BrowserList::size(); |
| 264 EXPECT_EQ(1, window_count); | 264 EXPECT_EQ(1, window_count); |
| 265 EXPECT_EQ(1, browser()->tab_strip_model()->count()); | 265 EXPECT_EQ(1, browser()->tab_strip_model()->count()); |
| 266 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 266 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
| 267 | 267 |
| 268 // Set up the temporary download folder. | 268 // Set up the temporary download folder. |
| 269 bool created_downloads_dir = CreateAndSetDownloadsDirectory(browser()); | 269 bool created_downloads_dir = CreateAndSetDownloadsDirectory(browser()); |
| 270 EXPECT_TRUE(created_downloads_dir); | 270 EXPECT_TRUE(created_downloads_dir); |
| 271 if (!created_downloads_dir) | 271 if (!created_downloads_dir) |
| 272 return false; | 272 return false; |
| 273 browser()->profile()->GetPrefs()->SetBoolean( | 273 browser()->profile()->GetPrefs()->SetBoolean( |
| 274 prefs::kPromptForDownload, false); | 274 prefs::kPromptForDownload, false); |
| 275 | 275 |
| 276 DownloadManager* manager = DownloadManagerForBrowser(browser()); | 276 DownloadManager* manager = DownloadManagerForBrowser(browser()); |
| 277 DownloadPrefs::FromDownloadManager(manager)->ResetAutoOpen(); | 277 DownloadPrefs::FromDownloadManager(manager)->ResetAutoOpen(); |
| 278 manager->RemoveAllDownloads(); | 278 manager->RemoveAllDownloads(); |
| 279 | 279 |
| 280 file_chooser_observer_.reset( | 280 file_activity_observer_.reset( |
| 281 new DownloadTestFileChooserObserver(browser()->profile())); | 281 new DownloadTestFileActivityObserver(browser()->profile())); |
| 282 | 282 |
| 283 return true; | 283 return true; |
| 284 } | 284 } |
| 285 | 285 |
| 286 protected: | 286 protected: |
| 287 | 287 |
| 288 enum SizeTestType { | 288 enum SizeTestType { |
| 289 SIZE_TEST_TYPE_KNOWN, | 289 SIZE_TEST_TYPE_KNOWN, |
| 290 SIZE_TEST_TYPE_UNKNOWN, | 290 SIZE_TEST_TYPE_UNKNOWN, |
| 291 }; | 291 }; |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 DCHECK(downloads); | 536 DCHECK(downloads); |
| 537 DownloadManager* manager = DownloadManagerForBrowser(browser); | 537 DownloadManager* manager = DownloadManagerForBrowser(browser); |
| 538 manager->GetAllDownloads(downloads); | 538 manager->GetAllDownloads(downloads); |
| 539 } | 539 } |
| 540 | 540 |
| 541 static void ExpectWindowCountAfterDownload(size_t expected) { | 541 static void ExpectWindowCountAfterDownload(size_t expected) { |
| 542 EXPECT_EQ(expected, BrowserList::size()); | 542 EXPECT_EQ(expected, BrowserList::size()); |
| 543 } | 543 } |
| 544 | 544 |
| 545 void EnableFileChooser(bool enable) { | 545 void EnableFileChooser(bool enable) { |
| 546 file_chooser_observer_->EnableFileChooser(enable); | 546 file_activity_observer_->EnableFileChooser(enable); |
| 547 } | 547 } |
| 548 | 548 |
| 549 bool DidShowFileChooser() { | 549 bool DidShowFileChooser() { |
| 550 return file_chooser_observer_->TestAndResetDidShowFileChooser(); | 550 return file_activity_observer_->TestAndResetDidShowFileChooser(); |
| 551 } | 551 } |
| 552 | 552 |
| 553 // Checks that |path| is has |file_size| bytes, and matches the |value| | 553 // Checks that |path| is has |file_size| bytes, and matches the |value| |
| 554 // string. | 554 // string. |
| 555 bool VerifyFile(const FilePath& path, | 555 bool VerifyFile(const FilePath& path, |
| 556 const std::string& value, | 556 const std::string& value, |
| 557 const int64 file_size) { | 557 const int64 file_size) { |
| 558 std::string file_contents; | 558 std::string file_contents; |
| 559 | 559 |
| 560 bool read = file_util::ReadFileToString(path, &file_contents); | 560 bool read = file_util::ReadFileToString(path, &file_contents); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 BrowserThread::PostTask( | 802 BrowserThread::PostTask( |
| 803 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 803 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 804 } | 804 } |
| 805 | 805 |
| 806 // Location of the test data. | 806 // Location of the test data. |
| 807 FilePath test_dir_; | 807 FilePath test_dir_; |
| 808 | 808 |
| 809 // Location of the downloads directory for these tests | 809 // Location of the downloads directory for these tests |
| 810 base::ScopedTempDir downloads_directory_; | 810 base::ScopedTempDir downloads_directory_; |
| 811 | 811 |
| 812 scoped_ptr<DownloadTestFileChooserObserver> file_chooser_observer_; | 812 scoped_ptr<DownloadTestFileActivityObserver> file_activity_observer_; |
| 813 }; | 813 }; |
| 814 | 814 |
| 815 // NOTES: | 815 // NOTES: |
| 816 // | 816 // |
| 817 // Files for these tests are found in DIR_TEST_DATA (currently | 817 // Files for these tests are found in DIR_TEST_DATA (currently |
| 818 // "chrome\test\data\", see chrome_paths.cc). | 818 // "chrome\test\data\", see chrome_paths.cc). |
| 819 // Mock responses have extension .mock-http-headers appended to the file name. | 819 // Mock responses have extension .mock-http-headers appended to the file name. |
| 820 | 820 |
| 821 // Download a file due to the associated MIME type. | 821 // Download a file due to the associated MIME type. |
| 822 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { | 822 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { |
| (...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1456 } | 1456 } |
| 1457 | 1457 |
| 1458 // Test to make sure auto-open works. | 1458 // Test to make sure auto-open works. |
| 1459 IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) { | 1459 IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) { |
| 1460 FilePath file(FILE_PATH_LITERAL("download-autoopen.txt")); | 1460 FilePath file(FILE_PATH_LITERAL("download-autoopen.txt")); |
| 1461 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); | 1461 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); |
| 1462 | 1462 |
| 1463 ASSERT_TRUE( | 1463 ASSERT_TRUE( |
| 1464 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file)); | 1464 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file)); |
| 1465 | 1465 |
| 1466 DownloadManagerForBrowser(browser())->MockDownloadOpenForTesting(); | |
| 1467 | |
| 1468 DownloadAndWait(browser(), url); | 1466 DownloadAndWait(browser(), url); |
| 1469 | 1467 |
| 1470 // Find the download and confirm it was opened. | 1468 // Find the download and confirm it was opened. |
| 1471 std::vector<DownloadItem*> downloads; | 1469 std::vector<DownloadItem*> downloads; |
| 1472 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads); | 1470 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads); |
| 1473 ASSERT_EQ(1u, downloads.size()); | 1471 ASSERT_EQ(1u, downloads.size()); |
| 1474 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState()); | 1472 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState()); |
| 1475 | 1473 |
| 1476 // Unfortunately, this will block forever, causing a timeout, if | 1474 // Unfortunately, this will block forever, causing a timeout, if |
| 1477 // the download is never opened. | 1475 // the download is never opened. |
| (...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2200 WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 2198 WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
| 2201 scoped_ptr<DownloadUrlParameters> params( | 2199 scoped_ptr<DownloadUrlParameters> params( |
| 2202 DownloadUrlParameters::FromWebContents(web_contents, url)); | 2200 DownloadUrlParameters::FromWebContents(web_contents, url)); |
| 2203 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); | 2201 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); |
| 2204 download_manager->DownloadUrl(params.Pass()); | 2202 download_manager->DownloadUrl(params.Pass()); |
| 2205 observer->WaitForFinished(); | 2203 observer->WaitForFinished(); |
| 2206 | 2204 |
| 2207 // Verify that download shelf is not shown. | 2205 // Verify that download shelf is not shown. |
| 2208 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 2206 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
| 2209 } | 2207 } |
| OLD | NEW |