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