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

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

Issue 10665049: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 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/download_browsertest.cc
diff --git a/chrome/browser/download/download_browsertest.cc b/chrome/browser/download/download_browsertest.cc
index f4fb4a58b539e86fdc079726ed725efa26679e93..cda254027a93bf5bbf4ab6da7c9feb59456d3348 100644
--- a/chrome/browser/download/download_browsertest.cc
+++ b/chrome/browser/download/download_browsertest.cc
@@ -28,6 +28,7 @@
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/extensions/extension_install_prompt.h"
#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/history/download_persistent_store_info.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/net/url_request_mock_util.h"
@@ -49,7 +50,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/download_save_info.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/browser/notification_source.h"
@@ -72,7 +72,6 @@ using content::BrowserContext;
using content::BrowserThread;
using content::DownloadItem;
using content::DownloadManager;
-using content::DownloadPersistentStoreInfo;
using content::DownloadUrlParameters;
using content::WebContents;
using extensions::Extension;
@@ -86,58 +85,6 @@ const FilePath kGoodCrxPath(FILE_PATH_LITERAL("extensions/good.crx"));
const char kLargeThemeCrxId[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
const FilePath kLargeThemePath(FILE_PATH_LITERAL("extensions/theme2.crx"));
-// Get History Information.
-class DownloadsHistoryDataCollector {
- public:
- DownloadsHistoryDataCollector(int64 download_db_handle,
- DownloadManager* manager)
- : result_valid_(false),
- download_db_handle_(download_db_handle) {
- HistoryService* hs = HistoryServiceFactory::GetForProfile(
- Profile::FromBrowserContext(manager->GetBrowserContext()),
- Profile::EXPLICIT_ACCESS);
- DCHECK(hs);
- hs->QueryDownloads(
- &callback_consumer_,
- base::Bind(&DownloadsHistoryDataCollector::OnQueryDownloadsComplete,
- base::Unretained(this)));
-
- // TODO(rdsmith): Move message loop out of constructor.
- // Cannot complete immediately because the history backend runs on a
- // separate thread, so we can assume that the RunMessageLoop below will
- // be exited by the Quit in OnQueryDownloadsComplete.
- content::RunMessageLoop();
- }
-
- bool GetDownloadsHistoryEntry(DownloadPersistentStoreInfo* result) {
- DCHECK(result);
- *result = result_;
- return result_valid_;
- }
-
- private:
- void OnQueryDownloadsComplete(
- std::vector<DownloadPersistentStoreInfo>* entries) {
- result_valid_ = false;
- for (std::vector<DownloadPersistentStoreInfo>::const_iterator it =
- entries->begin();
- it != entries->end(); ++it) {
- if (it->db_handle == download_db_handle_) {
- result_ = *it;
- result_valid_ = true;
- }
- }
- MessageLoopForUI::current()->Quit();
- }
-
- DownloadPersistentStoreInfo result_;
- bool result_valid_;
- int64 download_db_handle_;
- CancelableRequestConsumer callback_consumer_;
-
- DISALLOW_COPY_AND_ASSIGN(DownloadsHistoryDataCollector);
-};
-
// Mock that simulates a permissions dialog where the user denies
// permission to install. TODO(skerner): This could be shared with
// extensions tests. Find a common place for this class.
@@ -197,6 +144,22 @@ class TestRenderViewContextMenu : public RenderViewContextMenu {
DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu);
};
+typedef std::vector<DownloadPersistentStoreInfo> InfoVector;
+
+void HistoryQueryCallbackOnUI(
+ InfoVector* out,
+ scoped_ptr<InfoVector> in) {
+ in->swap(*out);
+ MessageLoop::current()->Quit();
+}
+
+void HistoryQueryCallbackOnDB(
+ InfoVector* out,
+ scoped_ptr<InfoVector> in) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&HistoryQueryCallbackOnUI, out, base::Passed(in.Pass())));
+}
+
} // namespace
// While an object of this class exists, it will mock out download
@@ -865,6 +828,50 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadMimeType) {
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
}
+IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistory) {
+ FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
+ GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
+ HistoryService* hs = HistoryServiceFactory::GetForProfile(
+ browser()->profile(), Profile::EXPLICIT_ACCESS);
+ InfoVector infos;
+ hs->QueryDownloads(base::Bind(&HistoryQueryCallbackOnDB, &infos));
+ MessageLoop::current()->Run();
+ EXPECT_EQ(0, static_cast<int>(infos.size()));
+
+ // Download the file and wait. We do not expect the Select File dialog.
+ DownloadAndWait(browser(), url);
+ CheckDownload(browser(), file, file);
+
+ DownloadManager* manager = DownloadManagerForBrowser(browser());
+ DownloadManager::DownloadVector downloads;
+ manager->SearchDownloads(string16(), &downloads);
+ EXPECT_EQ(1, static_cast<int>(downloads.size()));
+
+ hs->QueryDownloads(base::Bind(&HistoryQueryCallbackOnDB, &infos));
+ MessageLoop::current()->Run();
+ EXPECT_EQ(1, static_cast<int>(infos.size()));
+ EXPECT_EQ(downloads[0]->GetFullPath().value(), infos[0].path.value());
+ EXPECT_EQ(url.spec(), infos[0].url.spec());
+ EXPECT_EQ("", infos[0].referrer_url.spec());
+ EXPECT_EQ(downloads[0]->GetStartTime().ToTimeT(),
+ infos[0].start_time.ToTimeT());
+ EXPECT_EQ(downloads[0]->GetEndTime().ToTimeT(), infos[0].end_time.ToTimeT());
+ EXPECT_EQ(downloads[0]->GetReceivedBytes(), infos[0].received_bytes);
+ EXPECT_EQ(downloads[0]->GetTotalBytes(), infos[0].total_bytes);
+ EXPECT_EQ(DownloadItem::COMPLETE, infos[0].state);
+ EXPECT_EQ(1, infos[0].db_handle);
+ EXPECT_EQ(false, infos[0].opened);
+
+ downloads[0]->MockDownloadOpenForTesting();
+ downloads[0]->OpenDownload();
+ EXPECT_TRUE(downloads[0]->GetOpened());
+ hs->QueryDownloads(base::Bind(&HistoryQueryCallbackOnDB, &infos));
+ MessageLoop::current()->Run();
+ EXPECT_EQ(1, static_cast<int>(infos.size()));
+ EXPECT_EQ(1, infos[0].db_handle);
+ EXPECT_EQ(true, infos[0].opened);
+}
+
#if defined(OS_WIN)
// Download a file and confirm that the zone identifier (on windows)
// is set to internet.
@@ -1407,42 +1414,6 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, NewWindow) {
CheckDownload(browser(), file, file);
}
-// Confirm a download makes it into the history properly.
-IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) {
- FilePath file(FILE_PATH_LITERAL("download-test1.lib"));
- GURL url(URLRequestMockHTTPJob::GetMockUrl(file));
- FilePath origin_file(OriginFile(file));
- int64 origin_size;
- file_util::GetFileSize(origin_file, &origin_size);
-
- // Download the file and wait. We do not expect the Select File dialog.
- DownloadAndWait(browser(), url);
-
- // Get details of what downloads have just happened.
- std::vector<DownloadItem*> downloads;
- GetDownloads(browser(), &downloads);
- ASSERT_EQ(1u, downloads.size());
- int64 db_handle = downloads[0]->GetDbHandle();
-
- // Check state.
- EXPECT_EQ(1, browser()->tab_count());
- CheckDownload(browser(), file, file);
- EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
-
- // Check history results.
- DownloadsHistoryDataCollector history_collector(
- db_handle,
- DownloadManagerForBrowser(browser()));
- DownloadPersistentStoreInfo info;
- EXPECT_TRUE(history_collector.GetDownloadsHistoryEntry(&info)) << db_handle;
- EXPECT_EQ(file, info.path.BaseName());
- EXPECT_EQ(url, info.url);
- // Ignore start_time.
- EXPECT_EQ(origin_size, info.received_bytes);
- EXPECT_EQ(origin_size, info.total_bytes);
- EXPECT_EQ(DownloadItem::COMPLETE, info.state);
-}
-
// Test for crbug.com/14505. This tests that chrome:// urls are still functional
// after download of a file while viewing another chrome://.
IN_PROC_BROWSER_TEST_F(DownloadTest, ChromeURLAfterDownload) {
@@ -1694,56 +1665,46 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, CrxLargeTheme) {
ASSERT_TRUE(extension_service->GetExtensionById(kLargeThemeCrxId, false));
}
-// Sort download items by db_handle.
+// Sort download items by id.
static bool DownloadItemSorter(DownloadItem* d1, DownloadItem* d2) {
- return d1->GetDbHandle() < d2->GetDbHandle();
+ return d1->GetId() < d2->GetId();
}
// Confirm that searching through the history works properly
IN_PROC_BROWSER_TEST_F(DownloadTest, SearchDownloads) {
- // Downloads to populate history with.
- base::Time current(base::Time::Now());
- DownloadPersistentStoreInfo population_entries[] = {
- DownloadPersistentStoreInfo(
- FilePath(FILE_PATH_LITERAL("/path/to/file")),
- GURL("http://www.google.com/fantasy_download"),
- GURL(""),
- current - base::TimeDelta::FromMinutes(5),
- current,
- 128,
- 128,
- DownloadItem::COMPLETE,
- 1,
- false),
- DownloadPersistentStoreInfo(
- FilePath(FILE_PATH_LITERAL("/path/to/another_file")),
- GURL("http://www.google.com/reality_download"),
- GURL(""),
- current - base::TimeDelta::FromMinutes(10),
- current,
- 256,
- 256,
- DownloadItem::COMPLETE,
- 2,
- false),
- DownloadPersistentStoreInfo(
- FilePath(FILE_PATH_LITERAL("/different_path/to/another_file")),
- GURL("http://www.izzle.com/not_really_a_download"),
- GURL(""),
- current - base::TimeDelta::FromMinutes(15),
- current,
- 512,
- 512,
- DownloadItem::COMPLETE,
- 3,
- true)
- };
- std::vector<DownloadPersistentStoreInfo> entries(
- population_entries, population_entries + arraysize(population_entries));
-
// Populate the manager.
DownloadManager* manager = DownloadManagerForBrowser(browser());
- manager->OnPersistentStoreQueryComplete(&entries);
+ base::Time current(base::Time::Now());
+ manager->CreateDownloadItem(
+ FilePath(FILE_PATH_LITERAL("/path/to/file")),
+ GURL("http://www.google.com/fantasy_download"),
+ GURL(""),
+ current - base::TimeDelta::FromMinutes(5),
+ current,
+ 128,
+ 128,
+ DownloadItem::COMPLETE,
+ false);
+ manager->CreateDownloadItem(
+ FilePath(FILE_PATH_LITERAL("/path/to/another_file")),
+ GURL("http://www.google.com/reality_download"),
+ GURL(""),
+ current - base::TimeDelta::FromMinutes(10),
+ current,
+ 256,
+ 256,
+ DownloadItem::COMPLETE,
+ false);
+ manager->CreateDownloadItem(
+ FilePath(FILE_PATH_LITERAL("/different_path/to/another_file")),
+ GURL("http://www.izzle.com/not_really_a_download"),
+ GURL(""),
+ current - base::TimeDelta::FromMinutes(15),
+ current,
+ 512,
+ 512,
+ DownloadItem::COMPLETE,
+ true);
// Do some searches and check the results.
std::vector<DownloadItem*> search_results;
@@ -1767,7 +1728,6 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, SearchDownloads) {
EXPECT_EQ(128, d1->GetReceivedBytes());
EXPECT_EQ(128, d1->GetTotalBytes());
EXPECT_EQ(DownloadItem::COMPLETE, d1->GetState());
- EXPECT_EQ(1, d1->GetDbHandle());
EXPECT_FALSE(d1->GetOpened());
EXPECT_EQ(FilePath(FILE_PATH_LITERAL("/path/to/another_file")),
@@ -1780,7 +1740,6 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, SearchDownloads) {
EXPECT_EQ(256, d2->GetReceivedBytes());
EXPECT_EQ(256, d2->GetTotalBytes());
EXPECT_EQ(DownloadItem::COMPLETE, d2->GetState());
- EXPECT_EQ(2, d2->GetDbHandle());
EXPECT_FALSE(d2->GetOpened());
EXPECT_EQ(FilePath(FILE_PATH_LITERAL("/different_path/to/another_file")),
@@ -1793,7 +1752,6 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, SearchDownloads) {
EXPECT_EQ(512, d3->GetReceivedBytes());
EXPECT_EQ(512, d3->GetTotalBytes());
EXPECT_EQ(DownloadItem::COMPLETE, d3->GetState());
- EXPECT_EQ(3, d3->GetDbHandle());
EXPECT_TRUE(d3->GetOpened());
}
search_results.clear();
@@ -1803,24 +1761,18 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, SearchDownloads) {
ASSERT_EQ(2u, search_results.size());
std::sort(search_results.begin(), search_results.end(),
DownloadItemSorter);
- EXPECT_EQ(1, search_results[0]->GetDbHandle());
- EXPECT_EQ(2, search_results[1]->GetDbHandle());
search_results.clear();
manager->SearchDownloads(UTF8ToUTF16("real"), &search_results);
ASSERT_EQ(2u, search_results.size());
std::sort(search_results.begin(), search_results.end(),
DownloadItemSorter);
- EXPECT_EQ(2, search_results[0]->GetDbHandle());
- EXPECT_EQ(3, search_results[1]->GetDbHandle());
search_results.clear();
manager->SearchDownloads(UTF8ToUTF16("another_file"), &search_results);
ASSERT_EQ(2u, search_results.size());
std::sort(search_results.begin(), search_results.end(),
DownloadItemSorter);
- EXPECT_EQ(2, search_results[0]->GetDbHandle());
- EXPECT_EQ(3, search_results[1]->GetDbHandle());
search_results.clear();
}
« no previous file with comments | « chrome/browser/download/chrome_download_manager_delegate.cc ('k') | chrome/browser/download/download_history.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698