Chromium Code Reviews| 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/loading_test_util.h" | 5 #include "chrome/browser/predictors/loading_test_util.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | |
| 9 | 10 |
| 10 #include "content/public/browser/resource_request_info.h" | 11 #include "content/public/browser/resource_request_info.h" |
| 11 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { | 17 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { |
| 17 void OnResponseStarted(net::URLRequest* request, int net_error) override {} | 18 void OnResponseStarted(net::URLRequest* request, int net_error) override {} |
| 18 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} | 19 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} |
| 19 }; | 20 }; |
| 20 | 21 |
| 21 EmptyURLRequestDelegate g_empty_url_request_delegate; | 22 EmptyURLRequestDelegate g_empty_url_request_delegate; |
| 22 | 23 |
| 23 bool AlmostEqual(const double x, const double y) { | 24 bool AlmostEqual(const double x, const double y) { |
| 24 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. | 25 return std::fabs(x - y) <= 1e-6; // Arbitrary but close enough. |
| 25 } | 26 } |
| 26 | 27 |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 namespace predictors { | 30 namespace predictors { |
| 30 | 31 |
| 31 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 32 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 33 using Prediction = ResourcePrefetchPredictor::Prediction; | 32 using Prediction = ResourcePrefetchPredictor::Prediction; |
| 34 | 33 |
| 35 MockResourcePrefetchPredictor::MockResourcePrefetchPredictor( | 34 MockResourcePrefetchPredictor::MockResourcePrefetchPredictor( |
| 36 const LoadingPredictorConfig& config, | 35 const LoadingPredictorConfig& config, |
| 37 Profile* profile) | 36 Profile* profile) |
| 38 : ResourcePrefetchPredictor(config, profile) {} | 37 : ResourcePrefetchPredictor(config, profile) {} |
| 39 | 38 |
| 40 MockResourcePrefetchPredictor::~MockResourcePrefetchPredictor() = default; | 39 MockResourcePrefetchPredictor::~MockResourcePrefetchPredictor() = default; |
| 41 | 40 |
| 41 MockResourcePrefetchPredictorTables::MockResourcePrefetchPredictorTables() = | |
| 42 default; | |
| 43 | |
| 44 void MockResourcePrefetchPredictorTables::ScheduleDBTask( | |
| 45 const tracked_objects::Location& from_here, | |
| 46 DBTask task) { | |
| 47 ExecuteDBTaskOnDBThread(std::move(task)); | |
| 48 } | |
| 49 | |
| 50 void MockResourcePrefetchPredictorTables::ExecuteDBTaskOnDBThread(DBTask task) { | |
| 51 std::move(task).Run(nullptr); | |
| 52 } | |
| 53 | |
| 54 GlowplugKeyValueTable<PrefetchData>* | |
| 55 MockResourcePrefetchPredictorTables::url_resource_table() { | |
| 56 return &url_resource_table_; | |
| 57 } | |
| 58 | |
| 59 GlowplugKeyValueTable<RedirectData>* | |
| 60 MockResourcePrefetchPredictorTables::url_redirect_table() { | |
| 61 return &url_redirect_table_; | |
| 62 } | |
| 63 | |
| 64 GlowplugKeyValueTable<PrefetchData>* | |
| 65 MockResourcePrefetchPredictorTables::host_resource_table() { | |
| 66 return &host_resource_table_; | |
| 67 } | |
| 68 | |
| 69 GlowplugKeyValueTable<RedirectData>* | |
| 70 MockResourcePrefetchPredictorTables::host_redirect_table() { | |
| 71 return &host_redirect_table_; | |
| 72 } | |
| 73 | |
| 74 GlowplugKeyValueTable<precache::PrecacheManifest>* | |
| 75 MockResourcePrefetchPredictorTables::manifest_table() { | |
| 76 return &manifest_table_; | |
| 77 } | |
| 78 | |
| 79 GlowplugKeyValueTable<OriginData>* | |
| 80 MockResourcePrefetchPredictorTables::origin_table() { | |
| 81 return &origin_table_; | |
| 82 } | |
| 83 | |
| 84 MockResourcePrefetchPredictorTables::~MockResourcePrefetchPredictorTables() = | |
| 85 default; | |
| 86 | |
| 87 MockResourcePrefetchPredictorObserver::MockResourcePrefetchPredictorObserver( | |
| 88 ResourcePrefetchPredictor* predictor) | |
| 89 : TestObserver(predictor) {} | |
| 90 | |
| 91 MockResourcePrefetchPredictorObserver:: | |
| 92 ~MockResourcePrefetchPredictorObserver() {} | |
| 93 | |
| 42 void InitializeResourceData(ResourceData* resource, | 94 void InitializeResourceData(ResourceData* resource, |
| 43 const std::string& resource_url, | 95 const std::string& resource_url, |
| 44 content::ResourceType resource_type, | 96 content::ResourceType resource_type, |
| 45 int number_of_hits, | 97 int number_of_hits, |
| 46 int number_of_misses, | 98 int number_of_misses, |
| 47 int consecutive_misses, | 99 int consecutive_misses, |
| 48 double average_position, | 100 double average_position, |
| 49 net::RequestPriority priority, | 101 net::RequestPriority priority, |
| 50 bool has_validators, | 102 bool has_validators, |
| 51 bool always_revalidate) { | 103 bool always_revalidate) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 } | 209 } |
| 158 | 210 |
| 159 PageRequestSummary CreatePageRequestSummary( | 211 PageRequestSummary CreatePageRequestSummary( |
| 160 const std::string& main_frame_url, | 212 const std::string& main_frame_url, |
| 161 const std::string& initial_url, | 213 const std::string& initial_url, |
| 162 const std::vector<URLRequestSummary>& subresource_requests) { | 214 const std::vector<URLRequestSummary>& subresource_requests) { |
| 163 GURL main_frame_gurl(main_frame_url); | 215 GURL main_frame_gurl(main_frame_url); |
| 164 PageRequestSummary summary(main_frame_gurl); | 216 PageRequestSummary summary(main_frame_gurl); |
| 165 summary.initial_url = GURL(initial_url); | 217 summary.initial_url = GURL(initial_url); |
| 166 summary.subresource_requests = subresource_requests; | 218 summary.subresource_requests = subresource_requests; |
| 219 for (auto& request_summary : subresource_requests) | |
| 220 summary.UpdateOrAddToOrigins(request_summary); | |
| 167 return summary; | 221 return summary; |
| 168 } | 222 } |
| 169 | 223 |
| 170 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, | 224 URLRequestSummary CreateURLRequestSummary(SessionID::id_type tab_id, |
| 171 const std::string& main_frame_url, | 225 const std::string& main_frame_url, |
| 172 const std::string& resource_url, | 226 const std::string& resource_url, |
| 173 content::ResourceType resource_type, | 227 content::ResourceType resource_type, |
| 174 net::RequestPriority priority, | 228 net::RequestPriority priority, |
| 175 const std::string& mime_type, | 229 const std::string& mime_type, |
| 176 bool was_cached, | 230 bool was_cached, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 189 summary.was_cached = was_cached; | 243 summary.was_cached = was_cached; |
| 190 if (!redirect_url.empty()) | 244 if (!redirect_url.empty()) |
| 191 summary.redirect_url = GURL(redirect_url); | 245 summary.redirect_url = GURL(redirect_url); |
| 192 summary.has_validators = has_validators; | 246 summary.has_validators = has_validators; |
| 193 summary.always_revalidate = always_revalidate; | 247 summary.always_revalidate = always_revalidate; |
| 194 summary.is_no_store = false; | 248 summary.is_no_store = false; |
| 195 summary.network_accessed = true; | 249 summary.network_accessed = true; |
| 196 return summary; | 250 return summary; |
| 197 } | 251 } |
| 198 | 252 |
| 253 URLRequestSummary CreateRedirectRequestSummary( | |
| 254 SessionID::id_type session_id, | |
| 255 const std::string& main_frame_url, | |
| 256 const std::string& redirect_url) { | |
| 257 URLRequestSummary summary = | |
| 258 CreateURLRequestSummary(session_id, main_frame_url); | |
| 259 summary.redirect_url = GURL(redirect_url); | |
| 260 return summary; | |
| 261 } | |
| 262 | |
| 199 ResourcePrefetchPredictor::Prediction CreatePrediction( | 263 ResourcePrefetchPredictor::Prediction CreatePrediction( |
| 200 const std::string& main_frame_key, | 264 const std::string& main_frame_key, |
| 201 std::vector<GURL> subresource_urls) { | 265 std::vector<GURL> subresource_urls) { |
| 202 Prediction prediction; | 266 Prediction prediction; |
| 203 prediction.main_frame_key = main_frame_key; | 267 prediction.main_frame_key = main_frame_key; |
| 204 prediction.subresource_urls = subresource_urls; | 268 prediction.subresource_urls = subresource_urls; |
| 205 prediction.is_host = true; | 269 prediction.is_host = true; |
| 206 prediction.is_redirected = false; | 270 prediction.is_redirected = false; |
| 207 return prediction; | 271 return prediction; |
| 208 } | 272 } |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { | 479 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs) { |
| 416 return lhs.url() == rhs.url() && | 480 return lhs.url() == rhs.url() && |
| 417 lhs.number_of_hits() == rhs.number_of_hits() && | 481 lhs.number_of_hits() == rhs.number_of_hits() && |
| 418 lhs.number_of_misses() == rhs.number_of_misses() && | 482 lhs.number_of_misses() == rhs.number_of_misses() && |
| 419 lhs.consecutive_misses() == rhs.consecutive_misses(); | 483 lhs.consecutive_misses() == rhs.consecutive_misses(); |
| 420 } | 484 } |
| 421 | 485 |
| 422 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { | 486 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs) { |
| 423 return lhs.main_frame_url == rhs.main_frame_url && | 487 return lhs.main_frame_url == rhs.main_frame_url && |
| 424 lhs.initial_url == rhs.initial_url && | 488 lhs.initial_url == rhs.initial_url && |
| 425 lhs.subresource_requests == rhs.subresource_requests; | 489 lhs.subresource_requests == rhs.subresource_requests; |
|
alexilin
2017/06/23 16:55:05
It's seems like we need to compare lhs.origins and
trevordixon
2017/06/27 21:28:55
Done.
| |
| 426 } | 490 } |
| 427 | 491 |
| 428 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { | 492 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs) { |
| 429 return lhs.navigation_id == rhs.navigation_id && | 493 return lhs.navigation_id == rhs.navigation_id && |
| 430 lhs.resource_url == rhs.resource_url && | 494 lhs.resource_url == rhs.resource_url && |
| 431 lhs.resource_type == rhs.resource_type && | 495 lhs.resource_type == rhs.resource_type && |
| 432 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && | 496 lhs.priority == rhs.priority && lhs.mime_type == rhs.mime_type && |
| 433 lhs.before_first_contentful_paint == | 497 lhs.before_first_contentful_paint == |
| 434 rhs.before_first_contentful_paint && | 498 rhs.before_first_contentful_paint && |
| 435 lhs.was_cached == rhs.was_cached && | 499 lhs.was_cached == rhs.was_cached && |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 | 552 |
| 489 return equal; | 553 return equal; |
| 490 } | 554 } |
| 491 | 555 |
| 492 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { | 556 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs) { |
| 493 return lhs.url() == rhs.url() && | 557 return lhs.url() == rhs.url() && |
| 494 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); | 558 AlmostEqual(lhs.weight_ratio(), rhs.weight_ratio()); |
| 495 } | 559 } |
| 496 | 560 |
| 497 } // namespace precache | 561 } // namespace precache |
| OLD | NEW |