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/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.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_prefs.h" | 24 #include "chrome/browser/download/download_prefs.h" |
25 #include "chrome/browser/download/download_request_limiter.h" | 25 #include "chrome/browser/download/download_request_limiter.h" |
26 #include "chrome/browser/download/download_service.h" | |
27 #include "chrome/browser/download/download_service_factory.h" | |
26 #include "chrome/browser/download/download_shelf.h" | 28 #include "chrome/browser/download/download_shelf.h" |
27 #include "chrome/browser/download/download_test_file_chooser_observer.h" | 29 #include "chrome/browser/download/download_test_file_chooser_observer.h" |
28 #include "chrome/browser/download/download_util.h" | 30 #include "chrome/browser/download/download_util.h" |
29 #include "chrome/browser/extensions/extension_install_prompt.h" | 31 #include "chrome/browser/extensions/extension_install_prompt.h" |
30 #include "chrome/browser/extensions/extension_service.h" | 32 #include "chrome/browser/extensions/extension_service.h" |
33 #include "chrome/browser/history/download_persistent_store_info.h" | |
31 #include "chrome/browser/history/history.h" | 34 #include "chrome/browser/history/history.h" |
32 #include "chrome/browser/history/history_service_factory.h" | 35 #include "chrome/browser/history/history_service_factory.h" |
33 #include "chrome/browser/net/url_request_mock_util.h" | 36 #include "chrome/browser/net/url_request_mock_util.h" |
34 #include "chrome/browser/prefs/pref_service.h" | 37 #include "chrome/browser/prefs/pref_service.h" |
35 #include "chrome/browser/profiles/profile.h" | 38 #include "chrome/browser/profiles/profile.h" |
36 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 39 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
37 #include "chrome/browser/ui/browser.h" | 40 #include "chrome/browser/ui/browser.h" |
38 #include "chrome/browser/ui/browser_commands.h" | 41 #include "chrome/browser/ui/browser_commands.h" |
39 #include "chrome/browser/ui/browser_list.h" | 42 #include "chrome/browser/ui/browser_list.h" |
40 #include "chrome/browser/ui/browser_tabstrip.h" | 43 #include "chrome/browser/ui/browser_tabstrip.h" |
41 #include "chrome/browser/ui/browser_window.h" | 44 #include "chrome/browser/ui/browser_window.h" |
42 #include "chrome/browser/ui/chrome_pages.h" | 45 #include "chrome/browser/ui/chrome_pages.h" |
43 #include "chrome/browser/ui/host_desktop.h" | 46 #include "chrome/browser/ui/host_desktop.h" |
44 #include "chrome/common/chrome_notification_types.h" | 47 #include "chrome/common/chrome_notification_types.h" |
45 #include "chrome/common/chrome_paths.h" | 48 #include "chrome/common/chrome_paths.h" |
46 #include "chrome/common/extensions/feature_switch.h" | 49 #include "chrome/common/extensions/feature_switch.h" |
47 #include "chrome/common/pref_names.h" | 50 #include "chrome/common/pref_names.h" |
48 #include "chrome/common/url_constants.h" | 51 #include "chrome/common/url_constants.h" |
49 #include "chrome/test/base/in_process_browser_test.h" | 52 #include "chrome/test/base/in_process_browser_test.h" |
50 #include "chrome/test/base/ui_test_utils.h" | 53 #include "chrome/test/base/ui_test_utils.h" |
51 #include "content/public/browser/download_item.h" | 54 #include "content/public/browser/download_item.h" |
52 #include "content/public/browser/download_manager.h" | 55 #include "content/public/browser/download_manager.h" |
53 #include "content/public/browser/download_persistent_store_info.h" | |
54 #include "content/public/browser/download_save_info.h" | 56 #include "content/public/browser/download_save_info.h" |
55 #include "content/public/browser/download_url_parameters.h" | 57 #include "content/public/browser/download_url_parameters.h" |
56 #include "content/public/browser/notification_source.h" | 58 #include "content/public/browser/notification_source.h" |
57 #include "content/public/browser/render_view_host.h" | 59 #include "content/public/browser/render_view_host.h" |
58 #include "content/public/browser/resource_context.h" | 60 #include "content/public/browser/resource_context.h" |
59 #include "content/public/browser/web_contents.h" | 61 #include "content/public/browser/web_contents.h" |
60 #include "content/public/common/context_menu_params.h" | 62 #include "content/public/common/context_menu_params.h" |
61 #include "content/public/common/page_transition_types.h" | 63 #include "content/public/common/page_transition_types.h" |
62 #include "content/public/test/browser_test_utils.h" | 64 #include "content/public/test/browser_test_utils.h" |
63 #include "content/public/test/download_test_observer.h" | 65 #include "content/public/test/download_test_observer.h" |
64 #include "content/public/test/test_file_error_injector.h" | 66 #include "content/public/test/test_file_error_injector.h" |
65 #include "content/public/test/test_navigation_observer.h" | 67 #include "content/public/test/test_navigation_observer.h" |
66 #include "content/test/net/url_request_mock_http_job.h" | 68 #include "content/test/net/url_request_mock_http_job.h" |
67 #include "content/test/net/url_request_slow_download_job.h" | 69 #include "content/test/net/url_request_slow_download_job.h" |
68 #include "net/base/net_util.h" | 70 #include "net/base/net_util.h" |
69 #include "net/test/test_server.h" | 71 #include "net/test/test_server.h" |
70 #include "testing/gtest/include/gtest/gtest.h" | 72 #include "testing/gtest/include/gtest/gtest.h" |
71 | 73 |
72 using content::BrowserContext; | 74 using content::BrowserContext; |
73 using content::BrowserThread; | 75 using content::BrowserThread; |
74 using content::DownloadItem; | 76 using content::DownloadItem; |
75 using content::DownloadManager; | 77 using content::DownloadManager; |
76 using content::DownloadPersistentStoreInfo; | |
77 using content::DownloadUrlParameters; | 78 using content::DownloadUrlParameters; |
78 using content::URLRequestMockHTTPJob; | 79 using content::URLRequestMockHTTPJob; |
79 using content::URLRequestSlowDownloadJob; | 80 using content::URLRequestSlowDownloadJob; |
80 using content::WebContents; | 81 using content::WebContents; |
81 using extensions::Extension; | 82 using extensions::Extension; |
82 using extensions::FeatureSwitch; | 83 using extensions::FeatureSwitch; |
83 | 84 |
84 namespace { | 85 namespace { |
85 | 86 |
86 // IDs and paths of CRX files used in tests. | 87 // IDs and paths of CRX files used in tests. |
87 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; | 88 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; |
88 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); | 89 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); |
89 | 90 |
90 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; | 91 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; |
91 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); | 92 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); |
92 | 93 |
93 // Get History Information. | |
94 class DownloadsHistoryDataCollector { | |
95 public: | |
96 DownloadsHistoryDataCollector(int64 download_db_handle, | |
97 DownloadManager* manager) | |
98 : result_valid_(false), | |
99 download_db_handle_(download_db_handle) { | |
100 HistoryService* hs = HistoryServiceFactory::GetForProfile( | |
101 Profile::FromBrowserContext(manager->GetBrowserContext()), | |
102 Profile::EXPLICIT_ACCESS); | |
103 DCHECK(hs); | |
104 hs->QueryDownloads( | |
105 &callback_consumer_, | |
106 base::Bind(&DownloadsHistoryDataCollector::OnQueryDownloadsComplete, | |
107 base::Unretained(this))); | |
108 | |
109 // TODO(rdsmith): Move message loop out of constructor. | |
110 // Cannot complete immediately because the history backend runs on a | |
111 // separate thread, so we can assume that the RunMessageLoop below will | |
112 // be exited by the Quit in OnQueryDownloadsComplete. | |
113 content::RunMessageLoop(); | |
114 } | |
115 | |
116 bool GetDownloadsHistoryEntry(DownloadPersistentStoreInfo* result) { | |
117 DCHECK(result); | |
118 *result = result_; | |
119 return result_valid_; | |
120 } | |
121 | |
122 private: | |
123 void OnQueryDownloadsComplete( | |
124 std::vector<DownloadPersistentStoreInfo>* entries) { | |
125 result_valid_ = false; | |
126 for (std::vector<DownloadPersistentStoreInfo>::const_iterator it = | |
127 entries->begin(); | |
128 it != entries->end(); ++it) { | |
129 if (it->db_handle == download_db_handle_) { | |
130 result_ = *it; | |
131 result_valid_ = true; | |
132 } | |
133 } | |
134 MessageLoopForUI::current()->Quit(); | |
135 } | |
136 | |
137 DownloadPersistentStoreInfo result_; | |
138 bool result_valid_; | |
139 int64 download_db_handle_; | |
140 CancelableRequestConsumer callback_consumer_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector); | |
143 }; | |
144 | |
145 // Mock that simulates a permissions dialog where the user denies | 94 // Mock that simulates a permissions dialog where the user denies |
146 // permission to install. TODO(skerner): This could be shared with | 95 // permission to install. TODO(skerner): This could be shared with |
147 // extensions tests. Find a common place for this class. | 96 // extensions tests. Find a common place for this class. |
148 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { | 97 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { |
149 public: | 98 public: |
150 MockAbortExtensionInstallPrompt() : | 99 MockAbortExtensionInstallPrompt() : |
151 ExtensionInstallPrompt(NULL) { | 100 ExtensionInstallPrompt(NULL) { |
152 } | 101 } |
153 | 102 |
154 // Simulate a user abort on an extension installation. | 103 // Simulate a user abort on an extension installation. |
(...skipping 1244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1399 ExpectWindowCountAfterDownload(1); | 1348 ExpectWindowCountAfterDownload(1); |
1400 #endif | 1349 #endif |
1401 | 1350 |
1402 EXPECT_EQ(1, browser()->tab_count()); | 1351 EXPECT_EQ(1, browser()->tab_count()); |
1403 // Download shelf should close. Download panel stays open on ChromeOS. | 1352 // Download shelf should close. Download panel stays open on ChromeOS. |
1404 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 1353 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
1405 | 1354 |
1406 CheckDownload(browser(), file, file); | 1355 CheckDownload(browser(), file, file); |
1407 } | 1356 } |
1408 | 1357 |
1409 // Confirm a download makes it into the history properly. | 1358 class HistoryObserver : public DownloadHistory::Observer { |
1359 public: | |
1360 explicit HistoryObserver(Profile* profile) | |
1361 : profile_(profile), | |
1362 waiting_(false), | |
1363 seen_stored_(false) { | |
1364 DownloadServiceFactory::GetForProfile(profile_)-> | |
1365 GetDownloadHistory()->AddObserver(this); | |
1366 } | |
1367 | |
1368 virtual ~HistoryObserver() { | |
1369 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); | |
1370 if (service && service->GetDownloadManagerDelegate()) | |
Randy Smith (Not in Mondays)
2012/11/09 21:36:39
Why is it relevant whether or not the service has
benjhayden
2012/11/12 18:44:16
Done.
| |
1371 service->GetDownloadHistory()->RemoveObserver(this); | |
1372 } | |
1373 | |
1374 virtual void OnDownloadStored( | |
1375 content::DownloadItem* item, | |
1376 const DownloadPersistentStoreInfo& info) OVERRIDE { | |
1377 seen_stored_ = true; | |
1378 if (waiting_) | |
1379 MessageLoopForUI::current()->Quit(); | |
1380 } | |
1381 | |
1382 void WaitForStored() { | |
1383 if (seen_stored_) | |
1384 return; | |
1385 waiting_ = true; | |
1386 content::RunMessageLoop(); | |
1387 waiting_ = false; | |
1388 } | |
1389 | |
1390 private: | |
1391 Profile* profile_; | |
1392 bool waiting_; | |
1393 bool seen_stored_; | |
1394 DISALLOW_COPY_AND_ASSIGN(HistoryObserver); | |
1395 }; | |
1396 | |
1410 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { | 1397 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { |
1411 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); | 1398 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); |
1412 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); | 1399 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); |
1413 FilePath origin_file(OriginFile(file)); | 1400 HistoryObserver observer(browser()->profile()); |
1414 int64 origin_size; | 1401 DownloadAndWait(browser(), download_url); |
1415 file_util::GetFileSize(origin_file, &origin_size); | 1402 observer.WaitForStored(); |
1416 | |
1417 // Download the file and wait. We do not expect the Select File dialog. | |
1418 DownloadAndWait(browser(), url); | |
1419 | |
1420 // Get details of what downloads have just happened. | |
1421 std::vector<DownloadItem*> downloads; | |
1422 GetDownloads(browser(), &downloads); | |
1423 ASSERT_EQ(1u, downloads.size()); | |
1424 int64 db_handle = downloads[0]->GetDbHandle(); | |
1425 | |
1426 // Check state. | |
1427 EXPECT_EQ(1, browser()->tab_count()); | |
1428 CheckDownload(browser(), file, file); | |
1429 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
1430 | |
1431 // Check history results. | |
1432 DownloadsHistoryDataCollector history_collector( | |
1433 db_handle, | |
1434 DownloadManagerForBrowser(browser())); | |
1435 DownloadPersistentStoreInfo info; | |
1436 EXPECT_TRUE(history_collector.GetDownloadsHistoryEntry(&info)) << db_handle; | |
1437 EXPECT_EQ(file, info.path.BaseName()); | |
1438 EXPECT_EQ(url, info.url); | |
1439 // Ignore start_time. | |
1440 EXPECT_EQ(origin_size, info.received_bytes); | |
1441 EXPECT_EQ(origin_size, info.total_bytes); | |
1442 EXPECT_EQ(DownloadItem::COMPLETE, info.state); | |
1443 } | 1403 } |
1444 | 1404 |
1445 // Test for crbug.com/14505. This tests that chrome:// urls are still functional | 1405 // Test for crbug.com/14505. This tests that chrome:// urls are still functional |
1446 // after download of a file while viewing another chrome://. | 1406 // after download of a file while viewing another chrome://. |
1447 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { | 1407 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { |
1448 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); | 1408 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); |
1449 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); | 1409 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); |
1450 GURL flags_url(chrome::kChromeUIFlagsURL); | 1410 GURL flags_url(chrome::kChromeUIFlagsURL); |
1451 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); | 1411 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); |
1452 | 1412 |
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2259 WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 2219 WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
2260 scoped_ptr<DownloadUrlParameters> params( | 2220 scoped_ptr<DownloadUrlParameters> params( |
2261 DownloadUrlParameters::FromWebContents(web_contents, url)); | 2221 DownloadUrlParameters::FromWebContents(web_contents, url)); |
2262 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); | 2222 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); |
2263 download_manager->DownloadUrl(params.Pass()); | 2223 download_manager->DownloadUrl(params.Pass()); |
2264 observer->WaitForFinished(); | 2224 observer->WaitForFinished(); |
2265 | 2225 |
2266 // Verify that download shelf is not shown. | 2226 // Verify that download shelf is not shown. |
2267 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 2227 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
2268 } | 2228 } |
OLD | NEW |