Commit fa0a35a8 authored by John Abd-El-Malek's avatar John Abd-El-Malek Committed by Commit Bot

Convert prerender tests to always use URLLoaderInterceptor.

The test class now works with frame requests when the network service is disabled.

Bug: 776589
Change-Id: Iec439f88db2e2daddec350251dc19309a1c2bd5c
Reviewed-on: https://chromium-review.googlesource.com/1038651
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarMatthew Cary <mattcary@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555405}
parent e70c4a04
......@@ -530,17 +530,6 @@ page_load_metrics::PageLoadExtraInfo GenericPageLoadExtraInfo(
dest_url, false /* started_in_foreground */);
}
// Helper function, to allow passing a UI closure to
// CreateHangingFirstRequestInterceptor() instead of a IO callback.
base::Callback<void(net::URLRequest*)> GetIOCallbackFromUIClosure(
base::Closure ui_closure) {
auto lambda = [](base::Closure closure, net::URLRequest*) {
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
closure);
};
return base::Bind(lambda, ui_closure);
}
} // namespace
class PrerenderBrowserTest : public test_utils::PrerenderInProcessBrowserTest {
......@@ -916,9 +905,6 @@ class PrerenderBrowserTest : public test_utils::PrerenderInProcessBrowserTest {
void CreateHangingFirstRequestInterceptor(const GURL& url,
const base::FilePath& file,
base::Closure closure) {
// TODO(jam): use the URLLoaderInterceptor for the non-network service path
// once http://crbug.com/740130 is fixed.
if (base::FeatureList::IsEnabled(network::features::kNetworkService)) {
DCHECK(!interceptor_);
interceptor_ = std::make_unique<content::URLLoaderInterceptor>(
base::BindLambdaForTesting(
......@@ -929,18 +915,13 @@ class PrerenderBrowserTest : public test_utils::PrerenderInProcessBrowserTest {
first = false;
// Need to leak the client pipe, or else the renderer will
// get a disconnect error and load the error page.
auto* leak_client = new network::mojom::URLLoaderClientPtr;
*leak_client = std::move(params->client);
(void)params->client.PassInterface().PassHandle().release();
closure.Run();
return true;
}
}
return false;
}));
} else {
test_utils::CreateHangingFirstRequestInterceptor(
url, file, GetIOCallbackFromUIClosure(closure));
}
}
private:
......
......@@ -88,28 +88,6 @@ class NoStatePrefetchBrowserTest
host_resolver()->AddRule("*", "127.0.0.1");
}
// Set up a request counter for |path|, which is also the location of the data
// served by the request.
void CountRequestFor(const std::string& path_str, RequestCounter* counter) {
url::StringPieceReplacements<base::FilePath::StringType> replacement;
base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str);
replacement.SetPathStr(file_path.value());
const GURL url = src_server()->base_url().ReplaceComponents(replacement);
CountRequestForUrl(url, path_str, counter);
}
// As above, but specify the data path and URL separately.
void CountRequestForUrl(const GURL& url,
const std::string& path_str,
RequestCounter* counter) {
base::FilePath url_file = ui_test_utils::GetTestFilePath(
base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&prerender::test_utils::CreateCountingInterceptorOnIO,
url, url_file, counter->AsWeakPtr()));
}
void OverridePrerenderManagerTimeTicks() {
// The default zero time causes the prerender manager to do strange things.
clock_.Advance(base::TimeDelta::FromSeconds(1));
......@@ -279,16 +257,6 @@ IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchLoadFlag) {
GURL prefetch_page = src_server()->GetURL(kPrefetchPage);
GURL prefetch_script = src_server()->GetURL(kPrefetchScript);
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
// Until http://crbug.com/747130 is fixed, navigation requests won't go
// through URLLoader.
prerender::test_utils::InterceptRequest(
prefetch_page,
base::Bind([](net::URLRequest* request) {
EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH);
}));
}
content::URLLoaderInterceptor interceptor(base::Bind(
[](const GURL& prefetch_page, const GURL& prefetch_script,
content::URLLoaderInterceptor::RequestParams* params) {
......@@ -447,12 +415,6 @@ IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Prefetch301LoadFlags) {
EXPECT_TRUE(request->load_flags() & net::LOAD_PREFETCH);
});
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
// Until http://crbug.com/747130 is fixed, navigation requests won't go
// through URLLoader.
prerender::test_utils::InterceptRequest(page_url, verify_prefetch_only);
}
content::URLLoaderInterceptor interceptor(base::Bind(
[](const GURL& page_url,
content::URLLoaderInterceptor::RequestParams* params) {
......
......@@ -157,45 +157,6 @@ class CountingInterceptorWithCallback : public net::URLRequestInterceptor {
DISALLOW_COPY_AND_ASSIGN(CountingInterceptorWithCallback);
};
// URLRequestJob (and associated handler) which hangs.
class HangingURLRequestJob : public net::URLRequestJob {
public:
HangingURLRequestJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate)
: net::URLRequestJob(request, network_delegate) {
}
void Start() override {}
private:
~HangingURLRequestJob() override {}
};
class HangingFirstRequestInterceptor : public net::URLRequestInterceptor {
public:
HangingFirstRequestInterceptor(
const base::FilePath& file,
base::Callback<void(net::URLRequest*)> callback)
: file_(file), callback_(callback), first_run_(true) {}
~HangingFirstRequestInterceptor() override {}
net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
if (first_run_) {
first_run_ = false;
if (!callback_.is_null())
callback_.Run(request);
return new HangingURLRequestJob(request, network_delegate);
}
return new net::URLRequestMockHTTPJob(request, network_delegate, file_);
}
private:
base::FilePath file_;
base::Callback<void(net::URLRequest*)> callback_;
mutable bool first_run_;
};
// An ExternalProtocolHandler that blocks everything and asserts it never is
// called.
......@@ -238,17 +199,6 @@ class NeverRunsExternalProtocolHandlerDelegate
void FinishedProcessingCheck() override { NOTREACHED(); }
};
void CreateHangingFirstRequestInterceptorOnIO(
const GURL& url,
const base::FilePath& file,
base::Callback<void(net::URLRequest*)> callback_io) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
std::unique_ptr<net::URLRequestInterceptor> interceptor(
new HangingFirstRequestInterceptor(file, callback_io));
net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
url, std::move(interceptor));
}
} // namespace
RequestCounter::RequestCounter() : count_(0), expected_count_(-1) {}
......@@ -848,35 +798,12 @@ void CreateCountingInterceptorOnIO(
url, std::make_unique<CountingInterceptor>(file, counter));
}
void InterceptRequest(const GURL& url,
base::Callback<void(net::URLRequest*)> callback_io) {
CountingInterceptorWithCallback::Initialize(url, nullptr, callback_io);
}
void InterceptRequestAndCount(
const GURL& url,
RequestCounter* counter,
base::Callback<void(net::URLRequest*)> callback_io) {
CountingInterceptorWithCallback::Initialize(url, counter, callback_io);
}
void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(file));
}
void CreateHangingFirstRequestInterceptor(
const GURL& url,
const base::FilePath& file,
base::Callback<void(net::URLRequest*)> callback_io) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&CreateHangingFirstRequestInterceptorOnIO, url, file,
callback_io));
}
} // namespace test_utils
} // namespace prerender
......@@ -439,31 +439,9 @@ void CreateCountingInterceptorOnIO(
const base::FilePath& file,
const base::WeakPtr<RequestCounter>& counter);
// When the |url| hits the net::URLRequestFilter (on the IO thread), executes
// the |callback_io| providing the request to it. Does not modify the behavior
// or the request job.
void InterceptRequest(const GURL& url,
base::Callback<void(net::URLRequest*)> callback_io);
// When the |url| hits the net::URLRequestFilter (on the IO thread), executes
// the |callback_io| providing the request to it, also pings the |counter| on UI
// thread. Does not modify the behavior or the request job.
void InterceptRequestAndCount(
const GURL& url,
RequestCounter* counter,
base::Callback<void(net::URLRequest*)> callback_io);
// Makes |url| respond to requests with the contents of |file|.
void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file);
// Makes |url| never respond on the first load, and then with the contents of
// |file| afterwards. When the first load has been scheduled, runs |callback_io|
// on the IO thread.
void CreateHangingFirstRequestInterceptor(
const GURL& url,
const base::FilePath& file,
base::Callback<void(net::URLRequest*)> callback_io);
} // namespace test_utils
} // namespace prerender
......
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