OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 5 #include "chrome/browser/predictors/loading_test_util.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
| 8 #include <memory> |
| 9 |
| 10 #include "content/public/browser/resource_request_info.h" |
| 11 #include "net/http/http_response_headers.h" |
| 12 #include "net/url_request/url_request_test_util.h" |
8 | 13 |
9 namespace { | 14 namespace { |
10 | 15 |
| 16 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { |
| 17 void OnResponseStarted(net::URLRequest* request, int net_error) override {} |
| 18 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} |
| 19 }; |
| 20 |
| 21 EmptyURLRequestDelegate g_empty_url_request_delegate; |
| 22 |
11 bool AlmostEqual(const double x, const double y) { | 23 bool AlmostEqual(const double x, const double y) { |
12 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. | 24 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. |
13 } | 25 } |
14 | 26 |
15 } // namespace | 27 } // namespace |
16 | 28 |
17 namespace predictors { | 29 namespace predictors { |
18 | 30 |
19 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 31 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
20 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 32 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 config->max_consecutive_misses = 2; | 216 config->max_consecutive_misses = 2; |
205 config->max_redirect_consecutive_misses = 2; | 217 config->max_redirect_consecutive_misses = 2; |
206 config->min_resource_confidence_to_trigger_prefetch = 0.5; | 218 config->min_resource_confidence_to_trigger_prefetch = 0.5; |
207 } | 219 } |
208 config->is_url_learning_enabled = true; | 220 config->is_url_learning_enabled = true; |
209 config->is_manifests_enabled = true; | 221 config->is_manifests_enabled = true; |
210 config->is_origin_learning_enabled = true; | 222 config->is_origin_learning_enabled = true; |
211 config->mode = LoadingPredictorConfig::LEARNING; | 223 config->mode = LoadingPredictorConfig::LEARNING; |
212 } | 224 } |
213 | 225 |
| 226 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| 227 const char* headers) { |
| 228 return make_scoped_refptr(new net::HttpResponseHeaders( |
| 229 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); |
| 230 } |
| 231 |
| 232 MockURLRequestJob::MockURLRequestJob(net::URLRequest* request, |
| 233 const net::HttpResponseInfo& response_info, |
| 234 const std::string& mime_type) |
| 235 : net::URLRequestJob(request, nullptr), |
| 236 response_info_(response_info), |
| 237 mime_type_(mime_type) {} |
| 238 |
| 239 bool MockURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 240 *mime_type = mime_type_; |
| 241 return true; |
| 242 } |
| 243 |
| 244 void MockURLRequestJob::Start() { |
| 245 NotifyHeadersComplete(); |
| 246 } |
| 247 |
| 248 void MockURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 249 *info = response_info_; |
| 250 } |
| 251 |
| 252 MockURLRequestJobFactory::MockURLRequestJobFactory() {} |
| 253 MockURLRequestJobFactory::~MockURLRequestJobFactory() {} |
| 254 |
| 255 void MockURLRequestJobFactory::Reset() { |
| 256 response_info_ = net::HttpResponseInfo(); |
| 257 mime_type_ = std::string(); |
| 258 } |
| 259 |
| 260 net::URLRequestJob* MockURLRequestJobFactory::MaybeCreateJobWithProtocolHandler( |
| 261 const std::string& scheme, |
| 262 net::URLRequest* request, |
| 263 net::NetworkDelegate* network_delegate) const { |
| 264 return new MockURLRequestJob(request, response_info_, mime_type_); |
| 265 } |
| 266 |
| 267 net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptRedirect( |
| 268 net::URLRequest* request, |
| 269 net::NetworkDelegate* network_delegate, |
| 270 const GURL& location) const { |
| 271 return nullptr; |
| 272 } |
| 273 |
| 274 net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptResponse( |
| 275 net::URLRequest* request, |
| 276 net::NetworkDelegate* network_delegate) const { |
| 277 return nullptr; |
| 278 } |
| 279 |
| 280 bool MockURLRequestJobFactory::IsHandledProtocol( |
| 281 const std::string& scheme) const { |
| 282 return true; |
| 283 } |
| 284 |
| 285 bool MockURLRequestJobFactory::IsSafeRedirectTarget( |
| 286 const GURL& location) const { |
| 287 return true; |
| 288 } |
| 289 |
| 290 std::unique_ptr<net::URLRequest> CreateURLRequest( |
| 291 const net::TestURLRequestContext& url_request_context, |
| 292 const GURL& url, |
| 293 net::RequestPriority priority, |
| 294 content::ResourceType resource_type, |
| 295 bool is_main_frame) { |
| 296 std::unique_ptr<net::URLRequest> request = url_request_context.CreateRequest( |
| 297 url, priority, &g_empty_url_request_delegate); |
| 298 request->set_first_party_for_cookies(url); |
| 299 content::ResourceRequestInfo::AllocateForTesting( |
| 300 request.get(), resource_type, nullptr, -1, -1, -1, is_main_frame, false, |
| 301 false, true, content::PREVIEWS_OFF); |
| 302 request->Start(); |
| 303 return request; |
| 304 } |
| 305 |
214 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { | 306 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { |
215 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" | 307 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" |
216 << std::endl; | 308 << std::endl; |
217 for (const ResourceData& resource : data.resources()) | 309 for (const ResourceData& resource : data.resources()) |
218 os << "\t\t" << resource << std::endl; | 310 os << "\t\t" << resource << std::endl; |
219 return os; | 311 return os; |
220 } | 312 } |
221 | 313 |
222 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) { | 314 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) { |
223 return os << "[" << resource.resource_url() << "," << resource.resource_type() | 315 return os << "[" << resource.resource_url() << "," << resource.resource_type() |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 | 488 |
397 return equal; | 489 return equal; |
398 } | 490 } |
399 | 491 |
400 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | 492 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { |
401 return lhs.url() == rhs.url() && | 493 return lhs.url() == rhs.url() && |
402 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); | 494 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); |
403 } | 495 } |
404 | 496 |
405 } // namespace precache | 497 } // namespace precache |
OLD | NEW |