Chromium Code Reviews| 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 372db2fd8eb815d1b213c03f3f943341771f8188..ed96bf855b3d19d431fe488faaffb4aac5d44b04 100644 |
| --- a/chrome/browser/download/save_page_browsertest.cc |
| +++ b/chrome/browser/download/save_page_browsertest.cc |
| @@ -17,6 +17,8 @@ |
| #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/download_persistent_store_info.h" |
| +#include "chrome/browser/history/history_service_factory.h" |
| #include "chrome/browser/net/url_request_mock_util.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| #include "chrome/browser/profiles/profile.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" |
| @@ -50,13 +51,56 @@ using content::BrowserContext; |
| using content::BrowserThread; |
| using content::DownloadItem; |
| using content::DownloadManager; |
| -using content::DownloadPersistentStoreInfo; |
| using content::URLRequestMockHTTPJob; |
| using content::WebContents; |
| +class DownloadPersistedObserver : public DownloadHistory::Observer { |
| + public: |
| + typedef base::Callback<bool(const DownloadPersistentStoreInfo&)> |
| + PersistedFilter; |
| + |
| + DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) |
| + : profile_(profile), |
| + filter_(filter) { |
| + DownloadServiceFactory::GetForProfile(profile)-> |
| + GetDownloadManagerDelegate()->AddHistoryObserver(this); |
| + } |
| + |
| + virtual ~DownloadPersistedObserver() { |
| + DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); |
| + if (service && service->GetDownloadManagerDelegate()) |
| + service->GetDownloadManagerDelegate()->RemoveHistoryObserver(this); |
| + } |
| + |
| + bool WaitForPersisted() { |
| + if (persisted_) |
| + return true; |
| + waiting_ = true; |
| + content::RunMessageLoop(); |
| + waiting_ = false; |
| + return persisted_; |
| + } |
| + |
| + virtual void OnDownloadStored(DownloadItem* item, |
| + const DownloadPersistentStoreInfo& info) { |
| + persisted_ = filter_.Run(info); |
| + if (persisted_ && waiting_) |
| + MessageLoopForUI::current()->Quit(); |
| + } |
| + |
| + private: |
| + Profile* profile_; |
| + DownloadItem* item_; |
| + PersistedFilter filter_; |
| + bool waiting_; |
| + bool persisted_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
| +}; |
| + |
| const FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); |
| -const char kAppendedExtension[] = |
| +static const char kAppendedExtension[] = |
| #if defined(OS_WIN) |
| ".htm"; |
| #else |
| @@ -99,7 +143,6 @@ class DownloadItemCreatedObserver : public DownloadManager::Observer { |
| } |
| private: |
| - |
| // DownloadManager::Observer |
| virtual void OnDownloadCreated( |
| DownloadManager* manager, DownloadItem* item) OVERRIDE { |
| @@ -124,59 +167,22 @@ class DownloadItemCreatedObserver : public DownloadManager::Observer { |
| DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); |
| }; |
| -class DownloadPersistedObserver : public DownloadItem::Observer { |
| - public: |
| - explicit DownloadPersistedObserver(DownloadItem* item) |
| - : waiting_(false), item_(item) { |
| - item->AddObserver(this); |
| - } |
| - |
| - ~DownloadPersistedObserver() { |
| - if (item_) |
| - item_->RemoveObserver(this); |
| - } |
| - |
| - // Wait for download item to get the persisted bit set. |
| - // Note that this class provides no protection against the download |
| - // being destroyed between creation and return of WaitForPersisted(); |
| - // the caller must guarantee that in some other fashion. |
| - void WaitForPersisted() { |
| - // In combination with OnDownloadDestroyed() below, verify the |
| - // above interface contract. |
| - DCHECK(item_); |
| - |
| - if (item_->IsPersisted()) |
| - return; |
| - |
| - waiting_ = true; |
| - content::RunMessageLoop(); |
| - waiting_ = false; |
| - |
| - return; |
| - } |
| - |
| - private: |
| - // DownloadItem::Observer |
| - virtual void OnDownloadUpdated(DownloadItem* item) OVERRIDE { |
| - DCHECK_EQ(item, item_); |
| - |
| - if (waiting_ && item->IsPersisted()) |
| - MessageLoopForUI::current()->Quit(); |
| - } |
| - |
| - virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE { |
| - if (item != item_) |
| - return; |
| - |
| - item_->RemoveObserver(this); |
| - item_ = NULL; |
| - } |
| - |
| - bool waiting_; |
| - DownloadItem* item_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
| -}; |
| +bool DownloadStoredProperly( |
| + const GURL& expected_url, |
| + const FilePath& expected_path, |
| + int64 num_files, |
| + DownloadItem::DownloadState expected_state, |
| + const DownloadPersistentStoreInfo& info) { |
| + if (info.path != expected_path) |
| + return false; |
| + if (info.url != expected_url) |
| + return false; |
| + if ((num_files >= 0) && (info.received_bytes != num_files)) |
| + return false; |
| + if (info.state != expected_state) |
| + return false; |
| + return true; |
| +} |
| class SavePageBrowserTest : public InProcessBrowserTest { |
| public: |
| @@ -221,7 +227,9 @@ class SavePageBrowserTest : public InProcessBrowserTest { |
| return current_tab; |
| } |
| - bool WaitForSavePackageToFinish(GURL* url_at_finish) const { |
| + // Returns true if and when there was a single download created, and its url |
| + // is |expected_url|. |
| + bool WaitForSavePackageToFinish(const GURL& expected_url) const { |
| content::WindowedNotificationObserver observer( |
| content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| content::NotificationService::AllSources()); |
| @@ -238,24 +246,23 @@ class SavePageBrowserTest : public InProcessBrowserTest { |
| return false; |
| DownloadItem* download_item(items[0]); |
| - // Note on synchronization: |
| - // |
| - // For each Save Page As operation, we create a corresponding shell |
| - // DownloadItem to display progress to the user. That DownloadItem |
| - // goes through its own state transitions, including being persisted |
| - // out to the history database, and the download shelf is not shown |
| - // until after the persistence occurs. Save Package completion (and |
| - // marking the DownloadItem as completed) occurs asynchronously from |
| - // persistence. Thus if we want to examine either UI state or DB |
| - // state, we need to wait until both the save package operation is |
| - // complete and the relevant download item has been persisted. |
| - DownloadPersistedObserver(download_item).WaitForPersisted(); |
| - |
| - *url_at_finish = content::Details<DownloadItem>(observer.details()).ptr()-> |
| - GetOriginalUrl(); |
| - return true; |
| + return ((expected_url == download_item->GetOriginalUrl()) && |
| + (expected_url == content::Details<DownloadItem>( |
| + observer.details()).ptr()->GetOriginalUrl())); |
| } |
| + // Note on synchronization: |
| + // |
| + // For each Save Page As operation, we create a corresponding shell |
| + // DownloadItem to display progress to the user. That DownloadItem goes |
| + // through its own state transitions, including being persisted out to the |
| + // history database, and the download shelf is not shown until after the |
| + // persistence occurs. Save Package completion (and marking the DownloadItem |
| + // as completed) occurs asynchronously from persistence. Thus if we want to |
| + // examine either UI state or DB state, we need to wait until both the save |
| + // package operation is complete and the relevant download item has been |
| + // persisted. |
| + |
| DownloadManager* GetDownloadManager() const { |
| DownloadManager* download_manager = |
| BrowserContext::GetDownloadManager(browser()->profile()); |
| @@ -263,92 +270,6 @@ class SavePageBrowserTest : public InProcessBrowserTest { |
| return download_manager; |
| } |
| - void QueryDownloadHistory() { |
| - // Query the history system. |
| - ChromeDownloadManagerDelegate* delegate = |
| - static_cast<ChromeDownloadManagerDelegate*>( |
| - GetDownloadManager()->GetDelegate()); |
| - delegate->download_history()->Load( |
| - base::Bind(&SavePageBrowserTest::OnQueryDownloadEntriesComplete, |
| - base::Unretained(this))); |
| - |
| - // Run message loop until a quit message is sent from |
| - // OnQueryDownloadEntriesComplete(). |
| - content::RunMessageLoop(); |
| - } |
| - |
| - void OnQueryDownloadEntriesComplete( |
| - std::vector<DownloadPersistentStoreInfo>* entries) { |
| - history_entries_ = *entries; |
| - |
| - // Indicate thet we have received the history and can continue. |
| - MessageLoopForUI::current()->Quit(); |
| - } |
| - |
| - struct DownloadPersistentStoreInfoMatch |
| - : public std::unary_function<DownloadPersistentStoreInfo, bool> { |
| - |
| - DownloadPersistentStoreInfoMatch(const GURL& url, |
| - const FilePath& path, |
| - int64 num_files, |
| - DownloadItem::DownloadState state) |
| - : url_(url), |
| - path_(path), |
| - num_files_(num_files), |
| - state_(state) {} |
| - |
| - 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 == state_; |
| - } |
| - |
| - GURL url_; |
| - FilePath path_; |
| - int64 num_files_; |
| - DownloadItem::DownloadState state_; |
| - }; |
| - |
| - void CheckDownloadHistory(const GURL& url, |
| - const FilePath& path, |
| - int64 num_files, |
| - DownloadItem::DownloadState state) { |
| - // Make sure the relevant download item made it into the history. |
| - std::vector<DownloadItem*> downloads; |
| - GetDownloadManager()->GetAllDownloads(&downloads); |
| - ASSERT_EQ(1u, downloads.size()); |
| - |
| - QueryDownloadHistory(); |
| - |
| - std::vector<DownloadPersistentStoreInfo>::iterator found = |
| - std::find_if(history_entries_.begin(), history_entries_.end(), |
| - DownloadPersistentStoreInfoMatch(url, path, num_files, |
| - state)); |
| - |
| - if (found == history_entries_.end()) { |
| - LOG(ERROR) << "Missing url=" << url.spec() |
| - << " path=" << path.value() |
| - << " received=" << num_files |
| - << " state=" << state; |
| - 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; |
| - } |
| - EXPECT_TRUE(false); |
| - } |
| - } |
| - |
| - std::vector<DownloadPersistentStoreInfo> history_entries_; |
| - |
| // Path to directory containing test data. |
| FilePath test_dir_; |
| @@ -369,16 +290,16 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
| FilePath full_file_name, dir; |
| GetDestinationPaths("a", &full_file_name, &dir); |
| + DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| + &DownloadStoredProperly, |
| + url, full_file_name, 1, DownloadItem::COMPLETE)); |
|
Randy Smith (Not in Mondays)
2012/11/02 23:31:25
The fact that this is needed, rather than folding
benjhayden
2012/11/06 20:01:14
Done.
|
| ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(url, output_url); |
| + ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
| + persisted.WaitForPersisted(); |
| EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| - // a.htm is 1 file. |
| - CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); |
| EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| EXPECT_FALSE(file_util::PathExists(dir)); |
| @@ -396,6 +317,9 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { |
| FilePath full_file_name, dir; |
| GetDestinationPaths("a", &full_file_name, &dir); |
| + DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| + &DownloadStoredProperly, |
| + url, full_file_name, -1, DownloadItem::CANCELLED)); |
| DownloadItemCreatedObserver creation_observer(manager); |
| ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| @@ -406,14 +330,12 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyCancel) { |
| // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. |
| // Currently it's ignored. |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(url, output_url); |
| + ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
| // -1 to disable number of files check; we don't update after cancel, and |
| // we don't know when the single file completed in relationship to |
| // the cancel. |
| - CheckDownloadHistory(url, full_file_name, -1, DownloadItem::CANCELLED); |
| + persisted.WaitForPersisted(); |
| EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| @@ -460,17 +382,16 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
| FilePath full_file_name, dir; |
| GetDestinationPaths("a", &full_file_name, &dir); |
| + DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| + &DownloadStoredProperly, |
| + actual_page_url, full_file_name, 1, DownloadItem::COMPLETE)); |
| ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(actual_page_url, output_url); |
| + ASSERT_TRUE(WaitForSavePackageToFinish(actual_page_url)); |
| EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| - // a.htm is 1 file. |
| - CheckDownloadHistory(actual_page_url, full_file_name, 1, |
| - DownloadItem::COMPLETE); |
| + persisted.WaitForPersisted(); |
| EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| EXPECT_FALSE(file_util::PathExists(dir)); |
| @@ -484,16 +405,16 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
| FilePath full_file_name, dir; |
| GetDestinationPaths("b", &full_file_name, &dir); |
| + DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| + &DownloadStoredProperly, |
| + url, full_file_name, 3, DownloadItem::COMPLETE)); |
| ASSERT_TRUE(GetCurrentTab()->SavePage( |
| full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(url, output_url); |
| + ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
| EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| - // b.htm is 3 files. |
| - CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); |
| + persisted.WaitForPersisted(); |
| EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| EXPECT_TRUE(file_util::PathExists(dir)); |
| @@ -520,16 +441,16 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { |
| std::string("Test page for saving page feature") + kAppendedExtension); |
| FilePath dir = save_dir_.path().AppendASCII( |
| "Test page for saving page feature_files"); |
| + DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| + &DownloadStoredProperly, |
| + url, full_file_name, 3, DownloadItem::COMPLETE)); |
| ASSERT_TRUE(GetCurrentTab()->SavePage( |
| full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(url, output_url); |
| + ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
| EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| - // b.htm is 3 files. |
| - CheckDownloadHistory(url, full_file_name, 3, DownloadItem::COMPLETE); |
| + persisted.WaitForPersisted(); |
| EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| EXPECT_TRUE(file_util::PathExists(dir)); |
| @@ -552,22 +473,13 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, RemoveFromList) { |
| ASSERT_TRUE(GetCurrentTab()->SavePage(full_file_name, dir, |
| content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(url, output_url); |
| + ASSERT_TRUE(WaitForSavePackageToFinish(url)); |
| EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| - // a.htm is 1 file. |
| - CheckDownloadHistory(url, full_file_name, 1, DownloadItem::COMPLETE); |
| 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, DownloadItem::COMPLETE)), |
| - history_entries_.end()); |
| + // TODO(benjhayden) Should not be in history. |
|
Randy Smith (Not in Mondays)
2012/11/02 23:31:25
I'm confused--does this mean that it *is* in the h
benjhayden
2012/11/06 20:01:14
It was a reminder to test that the item was remove
|
| EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| EXPECT_FALSE(file_util::PathExists(dir)); |
| @@ -635,11 +547,15 @@ IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { |
| #else |
| SavePackageFilePicker::SetShouldPromptUser(false); |
| #endif |
| + content::WindowedNotificationObserver observer( |
| + content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| + content::NotificationService::AllSources()); |
| + DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
| + &DownloadStoredProperly, |
| + url, full_file_name, -1, DownloadItem::COMPLETE)); |
|
Randy Smith (Not in Mondays)
2012/11/02 23:31:25
Why the change?
benjhayden
2012/11/06 20:01:14
Done.
|
| chrome::SavePage(browser()); |
| - GURL output_url; |
| - ASSERT_TRUE(WaitForSavePackageToFinish(&output_url)); |
| - EXPECT_EQ(url, output_url); |
| - CheckDownloadHistory(url, full_file_name, -1, DownloadItem::COMPLETE); |
| + observer.Wait(); |
| + persisted.WaitForPersisted(); |
| EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| int64 actual_file_size = -1; |