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

Side by Side Diff: chrome/browser/extensions/api/downloads/downloads_api_unittest.cc

Issue 10735089: DownloadManager::Observer::OnDownloadCreated (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 5 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
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 <algorithm> 5 #include <algorithm>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 291
292 bool WaitFor(const std::string& event_name, const std::string& json_args) { 292 bool WaitFor(const std::string& event_name, const std::string& json_args) {
293 return events_listener_->WaitFor( 293 return events_listener_->WaitFor(
294 current_browser()->profile(), event_name, json_args); 294 current_browser()->profile(), event_name, json_args);
295 } 295 }
296 296
297 bool WaitForInterruption(DownloadItem* item, int expected_error, 297 bool WaitForInterruption(DownloadItem* item, int expected_error,
298 const std::string& on_created_event) { 298 const std::string& on_created_event) {
299 if (!WaitFor(events::kOnDownloadCreated, on_created_event)) 299 if (!WaitFor(events::kOnDownloadCreated, on_created_event))
300 return false; 300 return false;
301 // The item may or may not be interrupted before the onCreated event fires. 301 // Now, onCreated is always fired before interruption.
302 if (item->IsInterrupted()) { 302 return WaitFor(events::kOnDownloadChanged,
303 scoped_ptr<base::Value> args(base::JSONReader::Read(on_created_event)); 303 base::StringPrintf("[{\"id\": %d,"
304 base::ListValue* args_list = NULL; 304 " \"error\": {\"current\": %d},"
305 base::DictionaryValue* args_dict = NULL; 305 " \"state\": {"
306 if (!args->GetAsList(&args_list) || 306 " \"previous\": \"in_progress\","
307 !args_list->GetDictionary(0, &args_dict)) 307 " \"current\": \"interrupted\"}}]",
308 return false; 308 item->GetId(),
309 args_dict->SetString("state", "interrupted"); 309 expected_error));
310 args_dict->SetInteger("error", expected_error);
311 std::string created_error;
312 base::JSONWriter::Write(args_list, &created_error);
313 // This is not waiting for a different event, it's refining the
314 // expectations on the onCreated event that was just caught. Specifically,
315 // if a DownloadItem is already interrupted by the time the onCreated
316 // event fires, then the onCreated event should already describe the
317 // error.
318 return WaitFor(events::kOnDownloadCreated, created_error);
319 } else {
320 return WaitFor(events::kOnDownloadChanged,
321 base::StringPrintf("[{\"id\": %d,"
322 " \"error\": {\"current\": %d},"
323 " \"state\": {"
324 " \"previous\": \"in_progress\","
325 " \"current\": \"interrupted\"}}]",
326 item->GetId(),
327 expected_error));
328 }
329 } 310 }
330 311
331 std::string GetExtensionURL() { 312 std::string GetExtensionURL() {
332 return extension_->url().spec(); 313 return extension_->url().spec();
333 } 314 }
334 315
335 std::string GetFilename(const char* path) { 316 std::string GetFilename(const char* path) {
336 std::string result = 317 std::string result =
337 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); 318 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe();
338 #if defined(OS_WIN) 319 #if defined(OS_WIN)
(...skipping 1158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1497 int result_id = -1; 1478 int result_id = -1;
1498 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1479 ASSERT_TRUE(result->GetAsInteger(&result_id));
1499 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 1480 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
1500 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 1481 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
1501 ASSERT_TRUE(item); 1482 ASSERT_TRUE(item);
1502 ScopedCancellingItem canceller(item); 1483 ScopedCancellingItem canceller(item);
1503 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1484 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
1504 1485
1505 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 1486 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
1506 base::StringPrintf("[{\"danger\": \"safe\"," 1487 base::StringPrintf("[{\"danger\": \"safe\","
1507 " \"filename\": \"%s\","
1508 " \"incognito\": false," 1488 " \"incognito\": false,"
1509 " \"mime\": \"text/plain\"," 1489 " \"mime\": \"text/plain\","
1510 " \"paused\": false," 1490 " \"paused\": false,"
1511 " \"url\": \"%s\"}]", 1491 " \"url\": \"%s\"}]",
1512 GetFilename("slow.txt.crdownload").c_str(),
1513 download_url.c_str()))); 1492 download_url.c_str())));
1514 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 1493 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
1515 base::StringPrintf("[{\"id\": %d," 1494 base::StringPrintf("[{\"id\": %d,"
1516 " \"filename\": {" 1495 " \"filename\": {"
1517 " \"previous\": \"%s\"," 1496 " \"previous\": \"%s\","
1518 " \"current\": \"%s\"}," 1497 " \"current\": \"%s\"},"
1519 " \"state\": {" 1498 " \"state\": {"
1520 " \"previous\": \"in_progress\"," 1499 " \"previous\": \"in_progress\","
1521 " \"current\": \"complete\"}}]", 1500 " \"current\": \"complete\"}}]",
1522 result_id, 1501 result_id,
(...skipping 17 matching lines...) Expand all
1540 int result_id = -1; 1519 int result_id = -1;
1541 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1520 ASSERT_TRUE(result->GetAsInteger(&result_id));
1542 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 1521 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
1543 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 1522 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
1544 ASSERT_TRUE(item); 1523 ASSERT_TRUE(item);
1545 ScopedCancellingItem canceller(item); 1524 ScopedCancellingItem canceller(item);
1546 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1525 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
1547 1526
1548 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 1527 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
1549 base::StringPrintf("[{\"danger\": \"safe\"," 1528 base::StringPrintf("[{\"danger\": \"safe\","
1550 " \"filename\": \"%s\","
1551 " \"incognito\": true," 1529 " \"incognito\": true,"
1552 " \"mime\": \"text/plain\"," 1530 " \"mime\": \"text/plain\","
1553 " \"paused\": false," 1531 " \"paused\": false,"
1554 " \"url\": \"%s\"}]", 1532 " \"url\": \"%s\"}]",
1555 GetFilename("slow.txt.crdownload").c_str(),
1556 download_url.c_str()))); 1533 download_url.c_str())));
1557 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 1534 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
1558 base::StringPrintf("[{\"id\":%d," 1535 base::StringPrintf("[{\"id\":%d,"
1559 " \"filename\": {" 1536 " \"filename\": {"
1560 " \"previous\": \"%s\"," 1537 " \"previous\": \"%s\","
1561 " \"current\": \"%s\"}," 1538 " \"current\": \"%s\"},"
1562 " \"state\": {" 1539 " \"state\": {"
1563 " \"current\": \"complete\"," 1540 " \"current\": \"complete\","
1564 " \"previous\": \"in_progress\"}}]", 1541 " \"previous\": \"in_progress\"}}]",
1565 result_id, 1542 result_id,
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1696 int result_id = -1; 1673 int result_id = -1;
1697 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1674 ASSERT_TRUE(result->GetAsInteger(&result_id));
1698 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 1675 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
1699 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 1676 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
1700 ASSERT_TRUE(item); 1677 ASSERT_TRUE(item);
1701 ScopedCancellingItem canceller(item); 1678 ScopedCancellingItem canceller(item);
1702 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1679 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
1703 1680
1704 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 1681 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
1705 base::StringPrintf("[{\"danger\": \"safe\"," 1682 base::StringPrintf("[{\"danger\": \"safe\","
1706 " \"filename\": \"%s\","
1707 " \"incognito\": false," 1683 " \"incognito\": false,"
1708 " \"mime\": \"text/plain\"," 1684 " \"mime\": \"text/plain\","
1709 " \"paused\": false," 1685 " \"paused\": false,"
1710 " \"url\": \"%s\"}]", 1686 " \"url\": \"%s\"}]",
1711 GetFilename("slow.txt.crdownload").c_str(),
1712 download_url.c_str()))); 1687 download_url.c_str())));
1713 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 1688 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
1714 base::StringPrintf("[{\"id\": %d," 1689 base::StringPrintf("[{\"id\": %d,"
1715 " \"filename\": {" 1690 " \"filename\": {"
1716 " \"previous\": \"%s\"," 1691 " \"previous\": \"%s\","
1717 " \"current\": \"%s\"}," 1692 " \"current\": \"%s\"},"
1718 " \"state\": {" 1693 " \"state\": {"
1719 " \"previous\": \"in_progress\"," 1694 " \"previous\": \"in_progress\","
1720 " \"current\": \"complete\"}}]", 1695 " \"current\": \"complete\"}}]",
1721 result_id, 1696 result_id,
(...skipping 17 matching lines...) Expand all
1739 int result_id = -1; 1714 int result_id = -1;
1740 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1715 ASSERT_TRUE(result->GetAsInteger(&result_id));
1741 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 1716 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
1742 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 1717 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
1743 ASSERT_TRUE(item); 1718 ASSERT_TRUE(item);
1744 ScopedCancellingItem canceller(item); 1719 ScopedCancellingItem canceller(item);
1745 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1720 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
1746 1721
1747 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 1722 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
1748 base::StringPrintf("[{\"danger\": \"safe\"," 1723 base::StringPrintf("[{\"danger\": \"safe\","
1749 " \"filename\": \"%s\","
1750 " \"incognito\": false," 1724 " \"incognito\": false,"
1751 " \"mime\": \"text/plain\"," 1725 " \"mime\": \"text/plain\","
1752 " \"paused\": false," 1726 " \"paused\": false,"
1753 " \"url\": \"%s\"}]", 1727 " \"url\": \"%s\"}]",
1754 GetFilename("data.txt.crdownload").c_str(),
1755 download_url.c_str()))); 1728 download_url.c_str())));
1756 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 1729 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
1757 base::StringPrintf("[{\"id\": %d," 1730 base::StringPrintf("[{\"id\": %d,"
1758 " \"filename\": {" 1731 " \"filename\": {"
1759 " \"previous\": \"%s\"," 1732 " \"previous\": \"%s\","
1760 " \"current\": \"%s\"}," 1733 " \"current\": \"%s\"},"
1761 " \"state\": {" 1734 " \"state\": {"
1762 " \"previous\": \"in_progress\"," 1735 " \"previous\": \"in_progress\","
1763 " \"current\": \"complete\"}}]", 1736 " \"current\": \"complete\"}}]",
1764 result_id, 1737 result_id,
(...skipping 20 matching lines...) Expand all
1785 int result_id = -1; 1758 int result_id = -1;
1786 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1759 ASSERT_TRUE(result->GetAsInteger(&result_id));
1787 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 1760 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
1788 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 1761 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
1789 ASSERT_TRUE(item); 1762 ASSERT_TRUE(item);
1790 ScopedCancellingItem canceller(item); 1763 ScopedCancellingItem canceller(item);
1791 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1764 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
1792 1765
1793 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 1766 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
1794 base::StringPrintf("[{\"danger\": \"safe\"," 1767 base::StringPrintf("[{\"danger\": \"safe\","
1795 " \"filename\": \"%s\","
1796 " \"incognito\": false," 1768 " \"incognito\": false,"
1797 " \"mime\": \"text/html\"," 1769 " \"mime\": \"text/html\","
1798 " \"paused\": false," 1770 " \"paused\": false,"
1799 " \"url\": \"%s\"}]", 1771 " \"url\": \"%s\"}]",
1800 GetFilename("file.txt.crdownload").c_str(),
1801 download_url.c_str()))); 1772 download_url.c_str())));
1802 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 1773 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
1803 base::StringPrintf("[{\"id\": %d," 1774 base::StringPrintf("[{\"id\": %d,"
1804 " \"filename\": {" 1775 " \"filename\": {"
1805 " \"previous\": \"%s\"," 1776 " \"previous\": \"%s\","
1806 " \"current\": \"%s\"}," 1777 " \"current\": \"%s\"},"
1807 " \"state\": {" 1778 " \"state\": {"
1808 " \"previous\": \"in_progress\"," 1779 " \"previous\": \"in_progress\","
1809 " \"current\": \"complete\"}}]", 1780 " \"current\": \"complete\"}}]",
1810 result_id, 1781 result_id,
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
1992 int result_id = -1; 1963 int result_id = -1;
1993 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1964 ASSERT_TRUE(result->GetAsInteger(&result_id));
1994 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 1965 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
1995 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 1966 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
1996 ASSERT_TRUE(item); 1967 ASSERT_TRUE(item);
1997 ScopedCancellingItem canceller(item); 1968 ScopedCancellingItem canceller(item);
1998 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1969 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
1999 1970
2000 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 1971 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2001 base::StringPrintf("[{\"danger\": \"safe\"," 1972 base::StringPrintf("[{\"danger\": \"safe\","
2002 " \"incognito\": false," 1973 " \"incognito\": false,"
2003 " \"mime\": \"application/octet-stream\"," 1974 " \"mime\": \"application/octet-stream\","
2004 " \"paused\": false," 1975 " \"paused\": false,"
2005 " \"bytesReceived\": 164," 1976 " \"url\": \"%s\"}]", download_url.c_str())));
2006 " \"url\": \"%s\"}]", download_url.c_str())));
2007 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 1977 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2008 base::StringPrintf("[{\"id\": %d," 1978 base::StringPrintf("[{\"id\": %d,"
2009 " \"state\": {" 1979 " \"state\": {"
2010 " \"previous\": \"in_progress\"," 1980 " \"previous\": \"in_progress\","
2011 " \"current\": \"complete\"}}]", result_id))); 1981 " \"current\": \"complete\"},"
1982 " \"filename\": {"
1983 " \"previous\": \"%s\","
1984 " \"current\": \"%s\"}}]",
1985 result_id,
1986 GetFilename("post-succeed.txt.crdownload").c_str(),
1987 GetFilename("post-succeed.txt").c_str())));
2012 } 1988 }
2013 1989
2014 // Test that downloadPostSuccess would fail if the resource requires the POST 1990 // Test that downloadPostSuccess would fail if the resource requires the POST
2015 // method, and chrome fails to propagate the |method| parameter back to the 1991 // method, and chrome fails to propagate the |method| parameter back to the
2016 // server. This tests both that testserver.py does not succeed when it should 1992 // server. This tests both that testserver.py does not succeed when it should
2017 // fail, and this tests how the downloads extension api exposes the failure to 1993 // fail, and this tests how the downloads extension api exposes the failure to
2018 // extensions. 1994 // extensions.
2019 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1995 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2020 DownloadExtensionTest_Download_Post_Get) { 1996 DownloadExtensionTest_Download_Post_Get) {
2021 LoadExtension("downloads_split"); 1997 LoadExtension("downloads_split");
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
2157 ASSERT_TRUE(result->GetAsInteger(&result_id)); 2133 ASSERT_TRUE(result->GetAsInteger(&result_id));
2158 2134
2159 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id); 2135 DownloadItem* item = GetCurrentManager()->GetActiveDownloadItem(result_id);
2160 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id); 2136 if (!item) item = GetCurrentManager()->GetDownloadItem(result_id);
2161 ASSERT_TRUE(item); 2137 ASSERT_TRUE(item);
2162 ScopedCancellingItem canceller(item); 2138 ScopedCancellingItem canceller(item);
2163 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 2139 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2164 2140
2165 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 2141 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2166 base::StringPrintf("[{\"danger\": \"safe\"," 2142 base::StringPrintf("[{\"danger\": \"safe\","
2167 " \"filename\": \"%s\","
2168 " \"incognito\": false," 2143 " \"incognito\": false,"
2169 " \"mime\": \"text/plain\"," 2144 " \"mime\": \"text/plain\","
2170 " \"paused\": false," 2145 " \"paused\": false,"
2171 " \"url\": \"%s\"}]", 2146 " \"url\": \"%s\"}]",
2172 GetFilename("on_record.txt.crdownload").c_str(),
2173 download_url.c_str()))); 2147 download_url.c_str())));
2174 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 2148 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2175 base::StringPrintf("[{\"id\": %d," 2149 base::StringPrintf("[{\"id\": %d,"
2176 " \"filename\": {" 2150 " \"filename\": {"
2177 " \"previous\": \"%s\"," 2151 " \"previous\": \"%s\","
2178 " \"current\": \"%s\"}," 2152 " \"current\": \"%s\"},"
2179 " \"state\": {" 2153 " \"state\": {"
2180 " \"previous\": \"in_progress\"," 2154 " \"previous\": \"in_progress\","
2181 " \"current\": \"complete\"}}]", 2155 " \"current\": \"complete\"}}]",
2182 result_id, 2156 result_id,
2183 GetFilename("on_record.txt.crdownload").c_str(), 2157 GetFilename("on_record.txt.crdownload").c_str(),
2184 GetFilename("on_record.txt").c_str()))); 2158 GetFilename("on_record.txt").c_str())));
2185 std::string disk_data; 2159 std::string disk_data;
2186 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2160 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2187 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2161 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2188 } 2162 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/downloads/downloads_api.cc ('k') | content/browser/download/download_item_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698