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" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 85 |
86 namespace { | 86 namespace { |
87 | 87 |
88 // IDs and paths of CRX files used in tests. | 88 // IDs and paths of CRX files used in tests. |
89 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; | 89 const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; |
90 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); | 90 const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx")); |
91 | 91 |
92 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; | 92 const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; |
93 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); | 93 const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx")); |
94 | 94 |
| 95 // Get History Information. |
| 96 class DownloadsHistoryDataCollector { |
| 97 public: |
| 98 explicit DownloadsHistoryDataCollector(Profile* profile) |
| 99 : profile_(profile), result_valid_(false) {} |
| 100 |
| 101 bool WaitForDownloadInfo(std::vector<history::DownloadRow>* results) { |
| 102 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
| 103 profile_, Profile::EXPLICIT_ACCESS); |
| 104 DCHECK(hs); |
| 105 hs->QueryDownloads( |
| 106 &callback_consumer_, |
| 107 base::Bind(&DownloadsHistoryDataCollector::OnQueryDownloadsComplete, |
| 108 base::Unretained(this))); |
| 109 |
| 110 content::RunMessageLoop(); |
| 111 if (result_valid_) { |
| 112 *results = results_; |
| 113 } |
| 114 return result_valid_; |
| 115 } |
| 116 |
| 117 private: |
| 118 void OnQueryDownloadsComplete(std::vector<history::DownloadRow>* entries) { |
| 119 result_valid_ = true; |
| 120 results_ = *entries; |
| 121 MessageLoopForUI::current()->Quit(); |
| 122 } |
| 123 |
| 124 Profile* profile_; |
| 125 std::vector<history::DownloadRow> results_; |
| 126 bool result_valid_; |
| 127 CancelableRequestConsumer callback_consumer_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector); |
| 130 }; |
| 131 |
95 // Mock that simulates a permissions dialog where the user denies | 132 // Mock that simulates a permissions dialog where the user denies |
96 // permission to install. TODO(skerner): This could be shared with | 133 // permission to install. TODO(skerner): This could be shared with |
97 // extensions tests. Find a common place for this class. | 134 // extensions tests. Find a common place for this class. |
98 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { | 135 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { |
99 public: | 136 public: |
100 MockAbortExtensionInstallPrompt() : | 137 MockAbortExtensionInstallPrompt() : |
101 ExtensionInstallPrompt(NULL) { | 138 ExtensionInstallPrompt(NULL) { |
102 } | 139 } |
103 | 140 |
104 // Simulate a user abort on an extension installation. | 141 // Simulate a user abort on an extension installation. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 193 |
157 bool WasAutoOpened(DownloadItem* item) { | 194 bool WasAutoOpened(DownloadItem* item) { |
158 return item->GetAutoOpened(); | 195 return item->GetAutoOpened(); |
159 } | 196 } |
160 | 197 |
161 // Called when a download starts. Marks the download as hidden. | 198 // Called when a download starts. Marks the download as hidden. |
162 void SetHiddenDownloadCallback(DownloadItem* item, net::Error error) { | 199 void SetHiddenDownloadCallback(DownloadItem* item, net::Error error) { |
163 download_util::SetShouldShowInShelf(item, false); | 200 download_util::SetShouldShowInShelf(item, false); |
164 } | 201 } |
165 | 202 |
| 203 // Callback for HistoryObserver; used in DownloadHistoryCheck |
| 204 bool HasDataAndName(const history::DownloadRow& row) { |
| 205 return row.received_bytes > 0 && !row.target_path.empty(); |
| 206 } |
| 207 |
166 } // namespace | 208 } // namespace |
167 | 209 |
168 // While an object of this class exists, it will mock out download | 210 // While an object of this class exists, it will mock out download |
169 // opening for all downloads created on the specified download manager. | 211 // opening for all downloads created on the specified download manager. |
170 class MockDownloadOpeningObserver : public DownloadManager::Observer { | 212 class MockDownloadOpeningObserver : public DownloadManager::Observer { |
171 public: | 213 public: |
172 explicit MockDownloadOpeningObserver(DownloadManager* manager) | 214 explicit MockDownloadOpeningObserver(DownloadManager* manager) |
173 : download_manager_(manager) { | 215 : download_manager_(manager) { |
174 download_manager_->AddObserver(this); | 216 download_manager_->AddObserver(this); |
175 } | 217 } |
176 | 218 |
177 ~MockDownloadOpeningObserver() { | 219 ~MockDownloadOpeningObserver() { |
178 download_manager_->RemoveObserver(this); | 220 download_manager_->RemoveObserver(this); |
179 } | 221 } |
180 | 222 |
181 virtual void OnDownloadCreated( | 223 virtual void OnDownloadCreated( |
182 DownloadManager* manager, DownloadItem* item) OVERRIDE { | 224 DownloadManager* manager, DownloadItem* item) OVERRIDE { |
183 item->MockDownloadOpenForTesting(); | 225 item->MockDownloadOpenForTesting(); |
184 } | 226 } |
185 | 227 |
186 private: | 228 private: |
187 DownloadManager* download_manager_; | 229 DownloadManager* download_manager_; |
188 | 230 |
189 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver); | 231 DISALLOW_COPY_AND_ASSIGN(MockDownloadOpeningObserver); |
190 }; | 232 }; |
191 | 233 |
192 class HistoryObserver : public DownloadHistory::Observer { | 234 class HistoryObserver : public DownloadHistory::Observer { |
193 public: | 235 public: |
| 236 typedef base::Callback<bool(const history::DownloadRow&)> FilterCallback; |
| 237 |
194 explicit HistoryObserver(Profile* profile) | 238 explicit HistoryObserver(Profile* profile) |
195 : profile_(profile), | 239 : profile_(profile), |
196 waiting_(false), | 240 waiting_(false), |
197 seen_stored_(false) { | 241 seen_stored_(false) { |
198 DownloadServiceFactory::GetForProfile(profile_)-> | 242 DownloadServiceFactory::GetForProfile(profile_)-> |
199 GetDownloadHistory()->AddObserver(this); | 243 GetDownloadHistory()->AddObserver(this); |
200 } | 244 } |
201 | 245 |
202 virtual ~HistoryObserver() { | 246 virtual ~HistoryObserver() { |
203 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); | 247 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); |
204 if (service && service->GetDownloadHistory()) | 248 if (service && service->GetDownloadHistory()) |
205 service->GetDownloadHistory()->RemoveObserver(this); | 249 service->GetDownloadHistory()->RemoveObserver(this); |
206 } | 250 } |
207 | 251 |
| 252 void SetFilterCallback(const FilterCallback& callback) { |
| 253 callback_ = callback; |
| 254 } |
| 255 |
208 virtual void OnDownloadStored( | 256 virtual void OnDownloadStored( |
209 content::DownloadItem* item, | 257 content::DownloadItem* item, |
210 const history::DownloadRow& info) OVERRIDE { | 258 const history::DownloadRow& info) OVERRIDE { |
| 259 if (!callback_.is_null() && (!callback_.Run(info))) |
| 260 return; |
| 261 |
211 seen_stored_ = true; | 262 seen_stored_ = true; |
212 if (waiting_) | 263 if (waiting_) |
213 MessageLoopForUI::current()->Quit(); | 264 MessageLoopForUI::current()->Quit(); |
214 } | 265 } |
215 | 266 |
216 virtual void OnDownloadHistoryDestroyed() OVERRIDE { | 267 virtual void OnDownloadHistoryDestroyed() OVERRIDE { |
217 DownloadServiceFactory::GetForProfile(profile_)-> | 268 DownloadServiceFactory::GetForProfile(profile_)-> |
218 GetDownloadHistory()->RemoveObserver(this); | 269 GetDownloadHistory()->RemoveObserver(this); |
219 } | 270 } |
220 | 271 |
221 void WaitForStored() { | 272 void WaitForStored() { |
222 if (seen_stored_) | 273 if (seen_stored_) |
223 return; | 274 return; |
224 waiting_ = true; | 275 waiting_ = true; |
225 content::RunMessageLoop(); | 276 content::RunMessageLoop(); |
226 waiting_ = false; | 277 waiting_ = false; |
227 } | 278 } |
228 | 279 |
229 private: | 280 private: |
230 Profile* profile_; | 281 Profile* profile_; |
231 bool waiting_; | 282 bool waiting_; |
232 bool seen_stored_; | 283 bool seen_stored_; |
| 284 FilterCallback callback_; |
233 | 285 |
234 DISALLOW_COPY_AND_ASSIGN(HistoryObserver); | 286 DISALLOW_COPY_AND_ASSIGN(HistoryObserver); |
235 }; | 287 }; |
236 | 288 |
237 class DownloadTest : public InProcessBrowserTest { | 289 class DownloadTest : public InProcessBrowserTest { |
238 public: | 290 public: |
239 // Choice of navigation or direct fetch. Used by |DownloadFileCheckErrors()|. | 291 // Choice of navigation or direct fetch. Used by |DownloadFileCheckErrors()|. |
240 enum DownloadMethod { | 292 enum DownloadMethod { |
241 DOWNLOAD_NAVIGATE, | 293 DOWNLOAD_NAVIGATE, |
242 DOWNLOAD_DIRECT | 294 DOWNLOAD_DIRECT |
(...skipping 1152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1395 #endif | 1447 #endif |
1396 | 1448 |
1397 EXPECT_EQ(1, browser()->tab_count()); | 1449 EXPECT_EQ(1, browser()->tab_count()); |
1398 // Download shelf should close. Download panel stays open on ChromeOS. | 1450 // Download shelf should close. Download panel stays open on ChromeOS. |
1399 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 1451 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
1400 | 1452 |
1401 CheckDownload(browser(), file, file); | 1453 CheckDownload(browser(), file, file); |
1402 } | 1454 } |
1403 | 1455 |
1404 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { | 1456 IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { |
1405 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); | 1457 GURL download_url(URLRequestSlowDownloadJob::kKnownSizeUrl); |
1406 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); | 1458 std::string url_path(download_url.path()); |
| 1459 FilePath file( |
| 1460 FILE_PATH_LITERAL(url_path.substr(url_path.find_last_of('/') + 1))); |
| 1461 |
| 1462 // We use the server so that we can get a redirect and test url_chain |
| 1463 // persistence. |
| 1464 ASSERT_TRUE(test_server()->Start()); |
| 1465 GURL redirect_url = test_server()->GetURL( |
| 1466 "server-redirect?" + download_url.spec()); |
| 1467 |
| 1468 // Download the url and wait until the object has been stored. |
| 1469 base::Time start(base::Time::Now()); |
1407 HistoryObserver observer(browser()->profile()); | 1470 HistoryObserver observer(browser()->profile()); |
1408 DownloadAndWait(browser(), download_url); | 1471 observer.SetFilterCallback(base::Bind(&HasDataAndName)); |
| 1472 ui_test_utils::NavigateToURL(browser(), redirect_url); |
1409 observer.WaitForStored(); | 1473 observer.WaitForStored(); |
| 1474 |
| 1475 // Get the details on what was stored into the history. |
| 1476 std::vector<history::DownloadRow> downloads_in_database; |
| 1477 ASSERT_TRUE(DownloadsHistoryDataCollector( |
| 1478 browser()->profile()).WaitForDownloadInfo(&downloads_in_database)); |
| 1479 ASSERT_EQ(1u, downloads_in_database.size()); |
| 1480 |
| 1481 // Confirm history storage is what you expect for a partially completed |
| 1482 // slow download job. |
| 1483 history::DownloadRow& row(downloads_in_database[0]); |
| 1484 EXPECT_EQ(DestinationFile(browser(), file), row.target_path); |
| 1485 EXPECT_EQ(download_util::GetCrDownloadPath(DestinationFile(browser(), file)), |
| 1486 row.current_path); |
| 1487 ASSERT_EQ(2u, row.url_chain.size()); |
| 1488 EXPECT_EQ(redirect_url.spec(), row.url_chain[0].spec()); |
| 1489 EXPECT_EQ(download_url.spec(), row.url_chain[1].spec()); |
| 1490 EXPECT_LE(start, row.start_time); |
| 1491 EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize, row.received_bytes); |
| 1492 EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize |
| 1493 + URLRequestSlowDownloadJob::kSecondDownloadSize, row.total_bytes); |
| 1494 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, row.state); |
| 1495 EXPECT_FALSE(row.opened); |
| 1496 |
| 1497 // Finish the download. We're ok relying on the history to be flushed |
| 1498 // at this point as our queries will be behind the history updates |
| 1499 // invoked by completion. |
| 1500 scoped_ptr<content::DownloadTestObserver> download_observer( |
| 1501 CreateWaiter(browser(), 1)); |
| 1502 ui_test_utils::NavigateToURL(browser(), |
| 1503 GURL(URLRequestSlowDownloadJob::kErrorDownloadUrl)); |
| 1504 download_observer->WaitForFinished(); |
| 1505 EXPECT_EQ(1u, download_observer->NumDownloadsSeenInState( |
| 1506 DownloadItem::INTERRUPTED)); |
| 1507 base::Time end(base::Time::Now()); |
| 1508 |
| 1509 // Get what was stored in the history. |
| 1510 ASSERT_TRUE(DownloadsHistoryDataCollector( |
| 1511 browser()->profile()).WaitForDownloadInfo(&downloads_in_database)); |
| 1512 ASSERT_EQ(1u, downloads_in_database.size()); |
| 1513 |
| 1514 // Confirm history storage is what you expect for a completed |
| 1515 // slow download job. |
| 1516 history::DownloadRow& row1(downloads_in_database[0]); |
| 1517 EXPECT_EQ(DestinationFile(browser(), file), row1.target_path); |
| 1518 EXPECT_EQ(download_util::GetCrDownloadPath(DestinationFile(browser(), file)), |
| 1519 row.current_path); |
| 1520 ASSERT_EQ(2u, row1.url_chain.size()); |
| 1521 EXPECT_EQ(redirect_url.spec(), row1.url_chain[0].spec()); |
| 1522 EXPECT_EQ(download_url.spec(), row1.url_chain[1].spec()); |
| 1523 EXPECT_LE(start, row1.start_time); |
| 1524 EXPECT_GE(end, row1.end_time); |
| 1525 EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize, |
| 1526 row1.received_bytes); |
| 1527 EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize |
| 1528 + URLRequestSlowDownloadJob::kSecondDownloadSize, row1.total_bytes); |
| 1529 EXPECT_EQ(content::DownloadItem::INTERRUPTED, row1.state); |
| 1530 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, |
| 1531 row.interrupt_reason); |
| 1532 EXPECT_FALSE(row1.opened); |
1410 } | 1533 } |
1411 | 1534 |
1412 // Test for crbug.com/14505. This tests that chrome:// urls are still functional | 1535 // Test for crbug.com/14505. This tests that chrome:// urls are still functional |
1413 // after download of a file while viewing another chrome://. | 1536 // after download of a file while viewing another chrome://. |
1414 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { | 1537 IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) { |
1415 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); | 1538 FilePath file(FILE_PATH_LITERAL("download-test1.lib")); |
1416 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); | 1539 GURL download_url(URLRequestMockHTTPJob::GetMockUrl(file)); |
1417 GURL flags_url(chrome::kChromeUIFlagsURL); | 1540 GURL flags_url(chrome::kChromeUIFlagsURL); |
1418 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); | 1541 GURL extensions_url(chrome::kChromeUIExtensionsFrameURL); |
1419 | 1542 |
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2226 WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 2349 WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
2227 scoped_ptr<DownloadUrlParameters> params( | 2350 scoped_ptr<DownloadUrlParameters> params( |
2228 DownloadUrlParameters::FromWebContents(web_contents, url)); | 2351 DownloadUrlParameters::FromWebContents(web_contents, url)); |
2229 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); | 2352 params->set_callback(base::Bind(&SetHiddenDownloadCallback)); |
2230 download_manager->DownloadUrl(params.Pass()); | 2353 download_manager->DownloadUrl(params.Pass()); |
2231 observer->WaitForFinished(); | 2354 observer->WaitForFinished(); |
2232 | 2355 |
2233 // Verify that download shelf is not shown. | 2356 // Verify that download shelf is not shown. |
2234 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); | 2357 EXPECT_FALSE(browser()->window()->IsDownloadShelfVisible()); |
2235 } | 2358 } |
OLD | NEW |