| 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 "net/http/http_response_headers.h" |
| 8 | 9 |
| 9 namespace { | 10 namespace { |
| 10 | 11 |
| 11 bool AlmostEqual(const double x, const double y) { | 12 bool AlmostEqual(const double x, const double y) { |
| 12 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. | 13 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. |
| 13 } | 14 } |
| 14 | 15 |
| 15 } // namespace | 16 } // namespace |
| 16 | 17 |
| 17 namespace predictors { | 18 namespace predictors { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 config->max_consecutive_misses = 2; | 186 config->max_consecutive_misses = 2; |
| 186 config->max_redirect_consecutive_misses = 2; | 187 config->max_redirect_consecutive_misses = 2; |
| 187 config->min_resource_confidence_to_trigger_prefetch = 0.5; | 188 config->min_resource_confidence_to_trigger_prefetch = 0.5; |
| 188 } | 189 } |
| 189 config->is_url_learning_enabled = true; | 190 config->is_url_learning_enabled = true; |
| 190 config->is_manifests_enabled = true; | 191 config->is_manifests_enabled = true; |
| 191 config->is_origin_learning_enabled = true; | 192 config->is_origin_learning_enabled = true; |
| 192 config->mode = LoadingPredictorConfig::LEARNING; | 193 config->mode = LoadingPredictorConfig::LEARNING; |
| 193 } | 194 } |
| 194 | 195 |
| 196 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| 197 const char* headers) { |
| 198 return make_scoped_refptr(new net::HttpResponseHeaders( |
| 199 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); |
| 200 } |
| 201 |
| 202 MockURLRequestJob::MockURLRequestJob(net::URLRequest* request, |
| 203 const net::HttpResponseInfo& response_info, |
| 204 const std::string& mime_type) |
| 205 : net::URLRequestJob(request, nullptr), |
| 206 response_info_(response_info), |
| 207 mime_type_(mime_type) {} |
| 208 |
| 209 bool MockURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 210 *mime_type = mime_type_; |
| 211 return true; |
| 212 } |
| 213 |
| 214 void MockURLRequestJob::Start() { |
| 215 NotifyHeadersComplete(); |
| 216 } |
| 217 |
| 218 void MockURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 219 *info = response_info_; |
| 220 } |
| 221 |
| 222 MockURLRequestJobFactory::MockURLRequestJobFactory() {} |
| 223 MockURLRequestJobFactory::~MockURLRequestJobFactory() {} |
| 224 |
| 225 net::URLRequestJob* MockURLRequestJobFactory::MaybeCreateJobWithProtocolHandler( |
| 226 const std::string& scheme, |
| 227 net::URLRequest* request, |
| 228 net::NetworkDelegate* network_delegate) const { |
| 229 return new MockURLRequestJob(request, response_info_, mime_type_); |
| 230 } |
| 231 |
| 232 net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptRedirect( |
| 233 net::URLRequest* request, |
| 234 net::NetworkDelegate* network_delegate, |
| 235 const GURL& location) const { |
| 236 return nullptr; |
| 237 } |
| 238 |
| 239 net::URLRequestJob* MockURLRequestJobFactory::MaybeInterceptResponse( |
| 240 net::URLRequest* request, |
| 241 net::NetworkDelegate* network_delegate) const { |
| 242 return nullptr; |
| 243 } |
| 244 |
| 245 bool MockURLRequestJobFactory::IsHandledProtocol( |
| 246 const std::string& scheme) const { |
| 247 return true; |
| 248 } |
| 249 |
| 250 bool MockURLRequestJobFactory::IsSafeRedirectTarget( |
| 251 const GURL& location) const { |
| 252 return true; |
| 253 } |
| 254 |
| 195 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { | 255 std::ostream& operator<<(std::ostream& os, const PrefetchData& data) { |
| 196 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" | 256 os << "[" << data.primary_key() << "," << data.last_visit_time() << "]" |
| 197 << std::endl; | 257 << std::endl; |
| 198 for (const ResourceData& resource : data.resources()) | 258 for (const ResourceData& resource : data.resources()) |
| 199 os << "\t\t" << resource << std::endl; | 259 os << "\t\t" << resource << std::endl; |
| 200 return os; | 260 return os; |
| 201 } | 261 } |
| 202 | 262 |
| 203 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) { | 263 std::ostream& operator<<(std::ostream& os, const ResourceData& resource) { |
| 204 return os << "[" << resource.resource_url() << "," << resource.resource_type() | 264 return os << "[" << resource.resource_url() << "," << resource.resource_type() |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 | 437 |
| 378 return equal; | 438 return equal; |
| 379 } | 439 } |
| 380 | 440 |
| 381 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | 441 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { |
| 382 return lhs.url() == rhs.url() && | 442 return lhs.url() == rhs.url() && |
| 383 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); | 443 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); |
| 384 } | 444 } |
| 385 | 445 |
| 386 } // namespace precache | 446 } // namespace precache |
| OLD | NEW |