Chromium Code Reviews| Index: chrome/browser/predictors/loading_test_util.cc |
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor_test_util.cc b/chrome/browser/predictors/loading_test_util.cc |
| similarity index 83% |
| rename from chrome/browser/predictors/resource_prefetch_predictor_test_util.cc |
| rename to chrome/browser/predictors/loading_test_util.cc |
| index a8a3762f32c0a6e315faa55c3f6776b79c8abf1a..f07d4db10a114178f5d3e7d0f97cbc9d7e6d2e0b 100644 |
| --- a/chrome/browser/predictors/resource_prefetch_predictor_test_util.cc |
| +++ b/chrome/browser/predictors/loading_test_util.cc |
| @@ -2,12 +2,19 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| +#include "chrome/browser/predictors/loading_test_util.h" |
| #include <cmath> |
| +#include <memory> |
| + |
| +#include "content/public/browser/resource_request_info.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/url_request/url_request_test_util.h" |
| namespace { |
| +predictors::EmptyURLRequestDelegate empty_url_request_delegate; |
|
Benoit L
2017/06/07 12:01:05
nit: g_empty_url_request_delegate.
|
| + |
| bool AlmostEqual(const double x, const double y) { |
| return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. |
| } |
| @@ -192,6 +199,86 @@ void PopulateTestConfig(LoadingPredictorConfig* config, bool small_db) { |
| config->mode = LoadingPredictorConfig::LEARNING; |
| } |
| +scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| + const char* headers) { |
| + return make_scoped_refptr(new net::HttpResponseHeaders( |
| + net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); |
| +} |
| + |
| +MockURLRequestJob::MockURLRequestJob(net::URLRequest* request, |
| + const net::HttpResponseInfo& response_info, |
| + const std::string& mime_type) |
| + : net::URLRequestJob(request, nullptr), |
| + response_info_(response_info), |
| + mime_type_(mime_type) {} |
| + |
| +bool MockURLRequestJob::GetMimeType(std::string* mime_type) const { |
| + *mime_type = mime_type_; |
| + return true; |
| +} |
| + |
| +void MockURLRequestJob::Start() { |
| + NotifyHeadersComplete(); |
| +} |
| + |
| +void MockURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| + *info = response_info_; |
| +} |
| + |
| +MockURLRequestJobFactory::MockURLRequestJobFactory() {} |
| +MockURLRequestJobFactory::~MockURLRequestJobFactory() {} |
| + |
| +void MockURLRequestJobFactory::Reset() { |
| + response_info_ = net::HttpResponseInfo(); |
| + mime_type_ = std::string(); |
| +} |
| + |
| +net::URLRequestJob* MockURLRequestJobFactory::MaybeCreateJobWithProtocolHandler( |
| + const std::string& scheme, |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const { |
| + return new MockURLRequestJob(request, response_info_, mime_type_); |
| +} |
| + |
| +net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptRedirect( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate, |
| + const GURL& location) const { |
| + return nullptr; |
| +} |
| + |
| +net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptResponse( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const { |
| + return nullptr; |
| +} |
| + |
| +bool MockURLRequestJobFactory::IsHandledProtocol( |
| + const std::string& scheme) const { |
| + return true; |
| +} |
| + |
| +bool MockURLRequestJobFactory::IsSafeRedirectTarget( |
| + const GURL& location) const { |
| + return true; |
| +} |
| + |
| +std::unique_ptr<net::URLRequest> CreateURLRequest( |
| + const net::TestURLRequestContext& url_request_context, |
| + const GURL& url, |
| + net::RequestPriority priority, |
| + content::ResourceType resource_type, |
| + bool is_main_frame) { |
| + std::unique_ptr<net::URLRequest> request = url_request_context.CreateRequest( |
| + url, priority, &empty_url_request_delegate); |
| + request->set_first_party_for_cookies(url); |
| + content::ResourceRequestInfo::AllocateForTesting( |
| + request.get(), resource_type, nullptr, -1, -1, -1, is_main_frame, false, |
| + false, true, content::PREVIEWS_OFF); |
| + request->Start(); |
| + return request; |
| +} |
| + |
| std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { |
| os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" |
| << std::endl; |