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

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

Issue 12047039: download: Remove DownloadManager::MockDownloadOpenForTesting (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and comment fix Created 7 years, 10 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
« no previous file with comments | « no previous file | chrome/browser/download/download_test_file_activity_observer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_service.h" 36 #include "chrome/browser/history/history_service.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
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
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 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 BrowserThread::PostTask( 803 BrowserThread::PostTask(
804 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); 804 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
805 } 805 }
806 806
807 // Location of the test data. 807 // Location of the test data.
808 FilePath test_dir_; 808 FilePath test_dir_;
809 809
810 // Location of the downloads directory for these tests 810 // Location of the downloads directory for these tests
811 base::ScopedTempDir downloads_directory_; 811 base::ScopedTempDir downloads_directory_;
812 812
813 scoped_ptr<DownloadTestFileChooserObserver> file_chooser_observer_; 813 scoped_ptr<DownloadTestFileActivityObserver> file_activity_observer_;
814 }; 814 };
815 815
816 // NOTES: 816 // NOTES:
817 // 817 //
818 // Files for these tests are found in DIR_TEST_DATA (currently 818 // Files for these tests are found in DIR_TEST_DATA (currently
819 // "chrome\test\data\", see chrome_paths.cc). 819 // "chrome\test\data\", see chrome_paths.cc).
820 // Mock responses have extension .mock-http-headers appended to the file name. 820 // Mock responses have extension .mock-http-headers appended to the file name.
821 821
822 // Download a file due to the associated MIME type. 822 // Download a file due to the associated MIME type.
823 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) { 823 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
1459 } 1459 }
1460 1460
1461 // Test to make sure auto-open works. 1461 // Test to make sure auto-open works.
1462 IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) { 1462 IN_PROC_BROWSER_TEST_F(DownloadTest, AutoOpen) {
1463 FilePath file(FILE_PATH_LITERAL("download-autoopen.txt")); 1463 FilePath file(FILE_PATH_LITERAL("download-autoopen.txt"));
1464 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); 1464 GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
1465 1465
1466 ASSERT_TRUE( 1466 ASSERT_TRUE(
1467 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file)); 1467 GetDownloadPrefs(browser())->EnableAutoOpenBasedOnExtension(file));
1468 1468
1469 DownloadManagerForBrowser(browser())->MockDownloadOpenForTesting();
1470
1471 DownloadAndWait(browser(), url); 1469 DownloadAndWait(browser(), url);
1472 1470
1473 // Find the download and confirm it was opened. 1471 // Find the download and confirm it was opened.
1474 std::vector<DownloadItem*> downloads; 1472 std::vector<DownloadItem*> downloads;
1475 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads); 1473 DownloadManagerForBrowser(browser())->GetAllDownloads(&downloads);
1476 ASSERT_EQ(1u, downloads.size()); 1474 ASSERT_EQ(1u, downloads.size());
1477 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState()); 1475 EXPECT_EQ(DownloadItem::COMPLETE, downloads[0]->GetState());
1478 1476
1479 // Unfortunately, this will block forever, causing a timeout, if 1477 // Unfortunately, this will block forever, causing a timeout, if
1480 // the download is never opened. 1478 // the download is never opened.
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after
2208 browser()->tab_strip_model()->GetActiveWebContents(); 2206 browser()->tab_strip_model()->GetActiveWebContents();
2209 scoped_ptr<DownloadUrlParameters> params( 2207 scoped_ptr<DownloadUrlParameters> params(
2210 DownloadUrlParameters::FromWebContents(web_contents, url)); 2208 DownloadUrlParameters::FromWebContents(web_contents, url));
2211 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); 2209 params->set_callback(base::Bind(&SetHiddenDownloadCallback));
2212 download_manager->DownloadUrl(params.Pass()); 2210 download_manager->DownloadUrl(params.Pass());
2213 observer->WaitForFinished(); 2211 observer->WaitForFinished();
2214 2212
2215 // Verify that download shelf is not shown. 2213 // Verify that download shelf is not shown.
2216 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); 2214 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible());
2217 } 2215 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/download/download_test_file_activity_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698