This source file includes following definitions.
- HandleRequest
- SetUpOnMainThread
- CleanUpOnMainThread
- InitializeDownloadSettings
- SendDoneEvent
- LoadTestExtension
- GetDownloadManager
- DeleteDownloadAndWaitForFlush
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/api/streams_private.h"
#include "chrome/common/extensions/mime_types_handler.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/test_switches.h"
#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/render_process_host.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/download_test_observer.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
using content::BrowserContext;
using content::BrowserThread;
using content::DownloadItem;
using content::DownloadManager;
using content::DownloadUrlParameters;
using content::ResourceController;
using content::WebContents;
using extensions::Event;
using extensions::ExtensionSystem;
using net::test_server::BasicHttpResponse;
using net::test_server::HttpRequest;
using net::test_server::HttpResponse;
using net::test_server::EmbeddedTestServer;
using testing::_;
namespace streams_private = extensions::api::streams_private;
namespace {
scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse());
if (request.relative_url == "/doc_path.doc") {
response->set_code(net::HTTP_OK);
response->set_content_type("application/msword");
return response.PassAs<HttpResponse>();
}
if (request.relative_url == "/spreadsheet_path.xls") {
response->set_code(net::HTTP_OK);
response->set_content_type("application/msexcel");
response->AddCustomHeader("Test-Header", "part1");
response->AddCustomHeader("Test-Header", "part2");
return response.PassAs<HttpResponse>();
}
if (request.relative_url == "/text_path_attch.txt") {
response->set_code(net::HTTP_OK);
response->set_content("txt content");
response->set_content_type("text/plain");
response->AddCustomHeader("Content-Disposition",
"attachment; filename=test_path.txt");
return response.PassAs<HttpResponse>();
}
if (request.relative_url == "/text_path.txt") {
response->set_code(net::HTTP_OK);
response->set_content("txt content");
response->set_content_type("text/plain");
return response.PassAs<HttpResponse>();
}
if (request.relative_url == "/index.html") {
response->set_code(net::HTTP_OK);
response->set_content("html content");
response->set_content_type("text/html");
return response.PassAs<HttpResponse>();
}
if (request.relative_url == "/favicon.ico") {
response->set_code(net::HTTP_NOT_FOUND);
return response.PassAs<HttpResponse>();
}
EXPECT_TRUE(false) << "NOTREACHED!";
response->set_code(net::HTTP_NOT_FOUND);
return response.PassAs<HttpResponse>();
}
class StreamsPrivateApiTest : public ExtensionApiTest {
public:
StreamsPrivateApiTest() {}
virtual ~StreamsPrivateApiTest() {}
virtual void SetUpOnMainThread() OVERRIDE {
test_server_.reset(new EmbeddedTestServer);
ASSERT_TRUE(test_server_->InitializeAndWaitUntilReady());
test_server_->RegisterRequestHandler(base::Bind(&HandleRequest));
ExtensionApiTest::SetUpOnMainThread();
}
virtual void CleanUpOnMainThread() OVERRIDE {
EXPECT_TRUE(test_server_->ShutdownAndWaitUntilComplete());
test_server_.reset();
ExtensionApiTest::CleanUpOnMainThread();
}
void InitializeDownloadSettings() {
ASSERT_TRUE(browser());
ASSERT_TRUE(downloads_dir_.CreateUniqueTempDir());
browser()->profile()->GetPrefs()->SetFilePath(
prefs::kDownloadDefaultDirectory, downloads_dir_.path());
browser()->profile()->GetPrefs()->SetBoolean(
prefs::kPromptForDownload, false);
DownloadManager* manager = GetDownloadManager();
DownloadPrefs::FromDownloadManager(manager)->ResetAutoOpen();
manager->RemoveAllDownloads();
}
void SendDoneEvent() {
streams_private::StreamInfo info;
info.mime_type = "test/done";
info.original_url = "http://foo";
info.stream_url = "blob://bar";
info.tab_id = 10;
info.expected_content_size = 20;
scoped_ptr<Event> event(
new Event(streams_private::OnExecuteMimeTypeHandler::kEventName,
streams_private::OnExecuteMimeTypeHandler::Create(info)));
ExtensionSystem::Get(browser()->profile())->event_router()->
DispatchEventToExtension(test_extension_id_, event.Pass());
}
const extensions::Extension* LoadTestExtension() {
test_extension_id_ = "oickdpebdnfbgkcaoklfcdhjniefkcji";
const extensions::Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("streams_private/handle_mime_type"));
if (!extension)
return NULL;
MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
if (!handler) {
message_ = "No mime type handlers defined.";
return NULL;
}
DCHECK_EQ(test_extension_id_, extension->id());
return extension;
}
DownloadManager* GetDownloadManager() const {
DownloadManager* download_manager =
BrowserContext::GetDownloadManager(browser()->profile());
EXPECT_TRUE(download_manager);
return download_manager;
}
void DeleteDownloadAndWaitForFlush(DownloadItem* download,
DownloadManager* manager) {
scoped_refptr<content::DownloadTestFlushObserver> flush_observer(
new content::DownloadTestFlushObserver(manager));
download->Remove();
flush_observer->WaitForFlush();
}
protected:
std::string test_extension_id_;
scoped_ptr<EmbeddedTestServer> test_server_;
base::ScopedTempDir downloads_dir_;
};
IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, Navigate) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
ASSERT_TRUE(LoadTestExtension()) << message_;
ResultCatcher catcher;
ui_test_utils::NavigateToURL(browser(),
test_server_->GetURL("/doc_path.doc"));
base::MessageLoop::current()->RunUntilIdle();
DownloadManager* download_manager = GetDownloadManager();
std::vector<DownloadItem*> downloads;
download_manager->GetAllDownloads(&downloads);
ASSERT_EQ(0u, downloads.size());
EXPECT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, NavigateCrossSite) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
ASSERT_TRUE(LoadTestExtension()) << message_;
ResultCatcher catcher;
std::string initial_host = "www.example.com";
host_resolver()->AddRule(initial_host, "127.0.0.1");
GURL::Replacements replacements;
replacements.SetHostStr(initial_host);
GURL initial_url =
test_server_->GetURL("/index.html").ReplaceComponents(replacements);
ui_test_utils::NavigateToURL(browser(), initial_url);
ui_test_utils::NavigateToURL(browser(),
test_server_->GetURL("/doc_path.doc"));
base::MessageLoop::current()->RunUntilIdle();
DownloadManager* download_manager = GetDownloadManager();
std::vector<DownloadItem*> downloads;
download_manager->GetAllDownloads(&downloads);
ASSERT_EQ(0u, downloads.size());
EXPECT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, NavigateToAnAttachment) {
InitializeDownloadSettings();
ASSERT_TRUE(LoadTestExtension()) << message_;
ResultCatcher catcher;
DownloadManager* download_manager = GetDownloadManager();
scoped_ptr<content::DownloadTestObserver> download_observer(
new content::DownloadTestObserverInProgress(download_manager, 1));
ui_test_utils::NavigateToURL(browser(),
test_server_->GetURL("/text_path_attch.txt"));
download_observer->WaitForFinished();
DownloadManager::DownloadVector downloads;
download_manager->GetAllDownloads(&downloads);
ASSERT_EQ(1u, downloads.size());
DeleteDownloadAndWaitForFlush(downloads[0], download_manager);
SendDoneEvent();
EXPECT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, DirectDownload) {
InitializeDownloadSettings();
ASSERT_TRUE(LoadTestExtension()) << message_;
ResultCatcher catcher;
DownloadManager* download_manager = GetDownloadManager();
scoped_ptr<content::DownloadTestObserver> download_observer(
new content::DownloadTestObserverInProgress(download_manager, 1));
GURL url = test_server_->GetURL("/text_path.txt");
base::FilePath target_path =
downloads_dir_.path().Append(FILE_PATH_LITERAL("download_target.txt"));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(web_contents);
scoped_ptr<DownloadUrlParameters> params(
DownloadUrlParameters::FromWebContents(web_contents, url));
params->set_file_path(target_path);
download_manager->DownloadUrl(params.Pass());
download_observer->WaitForFinished();
std::vector<DownloadItem*> downloads;
download_manager->GetAllDownloads(&downloads);
ASSERT_EQ(1u, downloads.size());
DeleteDownloadAndWaitForFlush(downloads[0], download_manager);
SendDoneEvent();
EXPECT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(StreamsPrivateApiTest, Headers) {
#if defined(OS_WIN) && defined(USE_ASH)
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
return;
#endif
ASSERT_TRUE(LoadTestExtension()) << message_;
ResultCatcher catcher;
ui_test_utils::NavigateToURL(browser(),
test_server_->GetURL("/spreadsheet_path.xls"));
base::MessageLoop::current()->RunUntilIdle();
DownloadManager* download_manager = GetDownloadManager();
std::vector<DownloadItem*> downloads;
download_manager->GetAllDownloads(&downloads);
ASSERT_EQ(0u, downloads.size());
EXPECT_TRUE(catcher.GetNextResult());
}
}