Fix DownloadExtensionTest_OnDeterminingFilename_InterruptedResume

Review URL: https://codereview.chromium.org/12422012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192563 0039d316-1c4b-4281-b951-d872f2087c98
parent 36843acc
...@@ -36,9 +36,9 @@ ...@@ -36,9 +36,9 @@
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/page_transition_types.h" #include "content/public/common/page_transition_types.h"
#include "content/public/test/download_test_observer.h" #include "content/public/test/download_test_observer.h"
#include "content/public/test/test_file_error_injector.h"
#include "content/test/net/url_request_slow_download_job.h" #include "content/test/net/url_request_slow_download_job.h"
#include "net/base/data_url.h" #include "net/base/data_url.h"
#include "net/base/net_util.h" #include "net/base/net_util.h"
...@@ -86,6 +86,11 @@ class DownloadsEventsListener : public content::NotificationObserver { ...@@ -86,6 +86,11 @@ class DownloadsEventsListener : public content::NotificationObserver {
STLDeleteElements(&events_); STLDeleteElements(&events_);
} }
void ClearEvents() {
STLDeleteElements(&events_);
events_.clear();
}
class Event { class Event {
public: public:
Event(Profile* profile, Event(Profile* profile,
...@@ -335,6 +340,10 @@ class DownloadExtensionTest : public ExtensionApiTest { ...@@ -335,6 +340,10 @@ class DownloadExtensionTest : public ExtensionApiTest {
expected_error)); expected_error));
} }
void ClearEvents() {
events_listener_->ClearEvents();
}
std::string GetExtensionURL() { std::string GetExtensionURL() {
return extension_->url().spec(); return extension_->url().spec();
} }
...@@ -3096,108 +3105,154 @@ IN_PROC_BROWSER_TEST_F( ...@@ -3096,108 +3105,154 @@ IN_PROC_BROWSER_TEST_F(
result_id))); result_id)));
} }
// Test download interruption while extensions determining filename, re-run // TODO(benjhayden) Merge this with the other TestObservers.
// through fan-out and fan-in. class JustInProgressDownloadObserver
// TODO(rdsmith): FILE_OPERATION_INITIALIZE is not right for this test. : public content::DownloadTestObserverInProgress {
public:
JustInProgressDownloadObserver(
DownloadManager* download_manager, size_t wait_count)
: content::DownloadTestObserverInProgress(download_manager, wait_count) {
}
virtual ~JustInProgressDownloadObserver() {}
private:
virtual bool IsDownloadInFinalState(DownloadItem* item) OVERRIDE {
return item->GetState() == DownloadItem::IN_PROGRESS;
}
DISALLOW_COPY_AND_ASSIGN(JustInProgressDownloadObserver);
};
// Test download interruption while extensions determining filename. Should not
// re-dispatch onDeterminingFilename.
#if defined(OS_CHROMEOS)
#define MAYBE_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume \
DISABLED_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume
#else
#define MAYBE_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume \
DownloadExtensionTest_OnDeterminingFilename_InterruptedResume
#endif
IN_PROC_BROWSER_TEST_F( IN_PROC_BROWSER_TEST_F(
DownloadExtensionTest, DownloadExtensionTest,
DISABLED_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) { MAYBE_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableDownloadResumption);
LoadExtension("downloads_split"); LoadExtension("downloads_split");
CHECK(StartTestServer()); CHECK(StartTestServer());
std::string download_url = test_server()->GetURL("slow?0").spec();
GoOnTheRecord(); GoOnTheRecord();
AddFilenameDeterminer(); AddFilenameDeterminer();
// TODO Interrupt the download instead of responding to onDeterminingFilename.
scoped_refptr<content::TestFileErrorInjector> injector(
content::TestFileErrorInjector::Create(
GetCurrentManager()));
content::TestFileErrorInjector::FileErrorInfo error_info = {
download_url,
content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE,
0,
content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
injector->AddError(error_info);
injector->InjectErrors();
// Start a download. // Start a download.
scoped_ptr<base::Value> result(RunFunctionAndReturnResult( DownloadItem* item = NULL;
new DownloadsDownloadFunction(), base::StringPrintf( {
"[{\"url\": \"%s\"}]", download_url.c_str()))); DownloadManager* manager = GetCurrentManager();
ASSERT_TRUE(result.get()); scoped_ptr<content::DownloadTestObserver> observer(
int result_id = -1; new JustInProgressDownloadObserver(manager, 1));
ASSERT_TRUE(result->GetAsInteger(&result_id)); ASSERT_EQ(0, manager->InProgressCount());
DownloadItem* item = GetCurrentManager()->GetDownload(result_id); // Tabs created just for a download are automatically closed, invalidating
ASSERT_TRUE(item); // the download's WebContents. Downloads without WebContents cannot be
// resumed. http://crbug.com/225901
ui_test_utils::NavigateToURLWithDisposition(
current_browser(),
GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl),
CURRENT_TAB,
ui_test_utils::BROWSER_TEST_NONE);
observer->WaitForFinished();
EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::IN_PROGRESS));
DownloadManager::DownloadVector items;
manager->GetAllDownloads(&items);
for (DownloadManager::DownloadVector::iterator iter = items.begin();
iter != items.end(); ++iter) {
if ((*iter)->GetState() == DownloadItem::IN_PROGRESS) {
// There should be only one IN_PROGRESS item.
EXPECT_EQ(NULL, item);
item = *iter;
}
}
ASSERT_TRUE(item);
}
ScopedCancellingItem canceller(item); ScopedCancellingItem canceller(item);
ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
// Wait for the onCreated and onDeterminingFilename event. // Wait for the onCreated and onDeterminingFilename event.
ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
base::StringPrintf("[{\"danger\": \"safe\"," base::StringPrintf("[{\"danger\": \"safe\","
" \"incognito\": false," " \"incognito\": false,"
" \"id\": %d," " \"id\": %d,"
" \"mime\": \"text/plain\"," " \"mime\": \"application/octet-stream\","
" \"paused\": false," " \"paused\": false}]",
" \"url\": \"%s\"}]", item->GetId())));
result_id,
download_url.c_str())));
ASSERT_TRUE(WaitFor( ASSERT_TRUE(WaitFor(
events::kOnDownloadDeterminingFilename, events::kOnDownloadDeterminingFilename,
base::StringPrintf("[{\"id\": %d," base::StringPrintf("[{\"id\": %d,"
" \"incognito\": false," " \"incognito\": false,"
" \"filename\":\"slow.txt\"}]", " \"filename\":\"download-unknown-size\"}]",
result_id))); item->GetId())));
ASSERT_TRUE(item->GetTargetFilePath().empty()); ASSERT_TRUE(item->GetTargetFilePath().empty());
ASSERT_TRUE(item->IsInProgress()); ASSERT_TRUE(item->IsInProgress());
ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, ClearEvents();
base::StringPrintf("[{\"id\": %d," ui_test_utils::NavigateToURLWithDisposition(
" \"state\": {" current_browser(),
" \"previous\": \"in_progress\"," GURL(URLRequestSlowDownloadJob::kErrorDownloadUrl),
" \"current\": \"interrupted\"}}]", NEW_BACKGROUND_TAB,
result_id))); ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
ASSERT_TRUE(item->IsInterrupted());
item->ResumeInterruptedDownload();
// Wait for and respond to the onDeterminingFilename event. // Errors caught before filename determination are delayed until after
ASSERT_TRUE(WaitFor( // filename determination.
events::kOnDownloadDeterminingFilename,
base::StringPrintf("[{\"id\": %d,"
" \"incognito\": false,"
" \"filename\":\"slow.txt\"}]",
result_id)));
ASSERT_TRUE(item->GetTargetFilePath().empty());
ASSERT_TRUE(item->IsInProgress());
std::string error; std::string error;
ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
current_browser()->profile(), current_browser()->profile(),
false, false,
GetExtensionId(), GetExtensionId(),
result_id, item->GetId(),
base::FilePath(FILE_PATH_LITERAL("42.txt")), base::FilePath(FILE_PATH_LITERAL("42.txt")),
false, false,
&error)); &error)) << error;
EXPECT_EQ("", error); EXPECT_EQ("", error);
// The download should complete successfully.
ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
base::StringPrintf("[{\"id\": %d," base::StringPrintf("[{\"id\": %d,"
" \"filename\": {" " \"error\":{\"current\":20},"
" \"previous\": \"%s\"," " \"state\":{"
" \"current\": \"%s\"}," " \"previous\":\"in_progress\","
" \"state\": {" " \"current\":\"interrupted\"}}]",
" \"previous\": \"in_progress\"," item->GetId())));
" \"current\": \"complete\"}}]", // TODO(benjhayden) These next two lines fail on chromeos.
result_id, EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
GetFilename("42.txt.crdownload").c_str(), ASSERT_TRUE(item->IsInterrupted());
GetFilename("42.txt").c_str())));
ClearEvents();
item->ResumeInterruptedDownload();
// Errors caught before filename determination is complete are delayed until
// after filename determination so that, on resumption, filename determination
// does not need to be re-done. So, there will not be a second
// onDeterminingFilename event.
ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
base::StringPrintf("[{\"id\": %d," base::StringPrintf("[{\"id\": %d,"
" \"error\":{\"previous\":20},"
" \"state\":{"
" \"previous\":\"interrupted\","
" \"current\":\"in_progress\"}}]",
item->GetId())));
ClearEvents();
FinishPendingSlowDownloads();
// The download should complete successfully.
ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
base::StringPrintf("[{\"id\": %d,"
" \"filename\": {"
" \"previous\": \"%s\","
" \"current\": \"%s\"},"
" \"state\": {" " \"state\": {"
" \"previous\": \"in_progress\"," " \"previous\": \"in_progress\","
" \"current\": \"complete\"}}]", " \"current\": \"complete\"}}]",
result_id))); item->GetId(),
GetFilename("42.txt.crdownload").c_str(),
GetFilename("42.txt").c_str())));
} }
// TODO(benjhayden) Figure out why DisableExtension() does not fire // TODO(benjhayden) Figure out why DisableExtension() does not fire
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment