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

Unified Diff: chrome/browser/download/save_page_browsertest.cc

Issue 10665049: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/download/save_page_browsertest.cc
diff --git a/chrome/browser/download/save_page_browsertest.cc b/chrome/browser/download/save_page_browsertest.cc
index 92ed9b39dfeeac30cb8f7995808452cd24cff96e..29136073b9e173972a0e2964243b3df0a1943de8 100644
--- a/chrome/browser/download/save_page_browsertest.cc
+++ b/chrome/browser/download/save_page_browsertest.cc
@@ -13,9 +13,11 @@
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_history.h"
+#include "chrome/browser/download/download_persistent_store_info.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_service.h"
#include "chrome/browser/download/download_service_factory.h"
+#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/prefs/pref_member.h"
#include "chrome/browser/prefs/pref_service.h"
@@ -32,7 +34,6 @@
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/download_manager.h"
-#include "content/public/browser/download_persistent_store_info.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/web_contents.h"
@@ -49,7 +50,6 @@ using content::BrowserContext;
using content::BrowserThread;
using content::DownloadItem;
using content::DownloadManager;
-using content::DownloadPersistentStoreInfo;
using content::WebContents;
namespace {
@@ -124,13 +124,15 @@ class SavePageBrowserTest : public InProcessBrowserTest {
}
void QueryDownloadHistory() {
+ content::RunAllPendingInMessageLoop(content::BrowserThread::DB);
+ content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
+
// Query the history system.
- ChromeDownloadManagerDelegate* delegate =
- static_cast<ChromeDownloadManagerDelegate*>(
- GetDownloadManager()->GetDelegate());
- delegate->download_history()->Load(
- base::Bind(&SavePageBrowserTest::OnQueryDownloadEntriesComplete,
- base::Unretained(this)));
+ HistoryService* hs = HistoryServiceFactory::GetForProfile(
+ browser()->profile(), Profile::EXPLICIT_ACCESS);
+ hs->QueryDownloads(&history_consumer_, base::Bind(
+ &SavePageBrowserTest::OnQueryDownloadEntriesComplete,
+ base::Unretained(this)));
// Run message loop until a quit message is sent from
// OnQueryDownloadEntriesComplete().
@@ -145,56 +147,33 @@ class SavePageBrowserTest : public InProcessBrowserTest {
MessageLoopForUI::current()->Quit();
}
- struct DownloadPersistentStoreInfoMatch
- : public std::unary_function<DownloadPersistentStoreInfo, bool> {
-
- DownloadPersistentStoreInfoMatch(const GURL& url,
- const FilePath& path,
- int64 num_files)
- : url_(url),
- path_(path),
- num_files_(num_files) {
- }
-
- bool operator() (const DownloadPersistentStoreInfo& info) const {
- return info.url == url_ &&
- info.path == path_ &&
- // For non-MHTML save packages, received_bytes is actually the
- // number of files.
- ((num_files_ < 0) ||
- (info.received_bytes == num_files_)) &&
- info.total_bytes == 0 &&
- info.state == DownloadItem::COMPLETE;
- }
-
- GURL url_;
- FilePath path_;
- int64 num_files_;
- };
-
void CheckDownloadHistory(const GURL& url,
const FilePath& path,
- int64 num_files) {
+ int64 num_files,
+ bool expect_found) {
QueryDownloadHistory();
-
- std::vector<DownloadPersistentStoreInfo>::iterator found =
- std::find_if(history_entries_.begin(), history_entries_.end(),
- DownloadPersistentStoreInfoMatch(url, path, num_files));
-
- if (found == history_entries_.end()) {
- LOG(ERROR) << "Missing url=" << url.spec()
- << " path=" << path.value()
- << " received=" << num_files;
- for (size_t index = 0; index < history_entries_.size(); ++index) {
- LOG(ERROR) << "History@" << index << ": url="
- << history_entries_[index].url.spec()
- << " path=" << history_entries_[index].path.value()
- << " received=" << history_entries_[index].received_bytes
- << " total=" << history_entries_[index].total_bytes
- << " state=" << history_entries_[index].state;
+ bool found = false;
+ for (std::vector<DownloadPersistentStoreInfo>::iterator it =
+ history_entries_.begin();
+ it != history_entries_.end(); ++it) {
+ if (it->url != url) {
+ LOG(WARNING) << it->url.spec() << " != " << url.spec();
+ } else if (it->path != path) {
+ LOG(WARNING) << it->path.value() << " != " << path.value();
+ } else if ((num_files >= 0) &&
+ (it->received_bytes != num_files)) {
+ LOG(WARNING) << it->received_bytes << " != " << num_files;
+ } else if ((num_files >= 0) &&
+ (it->total_bytes != num_files)) {
+ LOG(WARNING) << it->total_bytes << " != " << num_files;
+ } else if (it->state != DownloadItem::COMPLETE) {
+ LOG(WARNING) << it->state << " != " << DownloadItem::COMPLETE;
+ } else {
+ found = true;
+ break;
}
- EXPECT_TRUE(false);
}
+ EXPECT_EQ(found, expect_found);
}
std::vector<DownloadPersistentStoreInfo> history_entries_;
@@ -206,6 +185,7 @@ class SavePageBrowserTest : public InProcessBrowserTest {
ScopedTempDir save_dir_;
private:
+ CancelableRequestConsumer history_consumer_;
DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest);
};
@@ -223,7 +203,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
EXPECT_EQ(url, WaitForSavePackageToFinish());
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
- CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
+ CheckDownloadHistory(url, full_file_name, 1, true); // a.htm is 1 file.
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
@@ -248,7 +228,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) {
EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish());
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
- CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file.
+ CheckDownloadHistory(actual_page_url, full_file_name, 1, true);
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
@@ -268,7 +248,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
EXPECT_EQ(url, WaitForSavePackageToFinish());
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
- CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
+ CheckDownloadHistory(url, full_file_name, 3, true); // b.htm is 3 files.
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_TRUE(file_util::PathExists(dir));
@@ -301,7 +281,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
EXPECT_EQ(url, WaitForSavePackageToFinish());
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
- CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files.
+ CheckDownloadHistory(url, full_file_name, 3, true); // b.htm is 3 files.
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_TRUE(file_util::PathExists(dir));
@@ -327,15 +307,13 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) {
EXPECT_EQ(url, WaitForSavePackageToFinish());
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
- CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file.
+ CheckDownloadHistory(url, full_file_name, 1, true); // a.htm is 1 file.
EXPECT_EQ(GetDownloadManager()->RemoveAllDownloads(), 1);
// Should not be in history.
QueryDownloadHistory();
- EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(),
- DownloadPersistentStoreInfoMatch(url, full_file_name, 1)),
- history_entries_.end());
+ CheckDownloadHistory(url, full_file_name, 1, false);
EXPECT_TRUE(file_util::PathExists(full_file_name));
EXPECT_FALSE(file_util::PathExists(dir));
@@ -408,7 +386,7 @@ IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) {
content::NotificationService::AllSources());
chrome::SavePage(browser());
observer.Wait();
- CheckDownloadHistory(url, full_file_name, -1);
+ CheckDownloadHistory(url, full_file_name, -1, true);
EXPECT_TRUE(file_util::PathExists(full_file_name));
int64 actual_file_size = -1;

Powered by Google App Engine
This is Rietveld 408576698