| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 6 | 6 |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 16 #include "chrome/browser/history/history_service_factory.h" | 16 #include "chrome/browser/history/history_service_factory.h" |
| 17 #include "chrome/browser/predictors/loading_predictor.h" | 17 #include "chrome/browser/predictors/loading_predictor.h" |
| 18 #include "chrome/browser/predictors/loading_test_util.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 19 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | |
| 20 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 20 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 22 #include "components/history/core/browser/history_service.h" | 22 #include "components/history/core/browser/history_service.h" |
| 23 #include "components/history/core/browser/history_types.h" | 23 #include "components/history/core/browser/history_types.h" |
| 24 #include "components/sessions/core/session_id.h" | 24 #include "components/sessions/core/session_id.h" |
| 25 #include "content/public/browser/resource_request_info.h" | |
| 26 #include "content/public/test/test_browser_thread_bundle.h" | 25 #include "content/public/test/test_browser_thread_bundle.h" |
| 27 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 28 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 27 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 29 #include "net/url_request/url_request_context.h" | 28 #include "net/url_request/url_request_context.h" |
| 30 #include "net/url_request/url_request_job.h" | 29 #include "net/url_request/url_request_job.h" |
| 31 #include "net/url_request/url_request_test_util.h" | |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 32 |
| 35 using testing::StrictMock; | 33 using testing::StrictMock; |
| 36 using testing::UnorderedElementsAre; | 34 using testing::UnorderedElementsAre; |
| 37 | 35 |
| 38 namespace predictors { | 36 namespace predictors { |
| 39 | 37 |
| 40 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 38 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 41 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 39 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 42 using PrefetchDataMap = std::map<std::string, PrefetchData>; | 40 using PrefetchDataMap = std::map<std::string, PrefetchData>; |
| 43 using RedirectDataMap = std::map<std::string, RedirectData>; | 41 using RedirectDataMap = std::map<std::string, RedirectData>; |
| 44 using OriginDataMap = std::map<std::string, OriginData>; | 42 using OriginDataMap = std::map<std::string, OriginData>; |
| 45 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>; | 43 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>; |
| 46 | 44 |
| 47 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( | |
| 48 const char* headers) { | |
| 49 return make_scoped_refptr(new net::HttpResponseHeaders( | |
| 50 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); | |
| 51 } | |
| 52 | |
| 53 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { | |
| 54 void OnResponseStarted(net::URLRequest* request, int net_error) override {} | |
| 55 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} | |
| 56 }; | |
| 57 | |
| 58 class MockURLRequestJob : public net::URLRequestJob { | |
| 59 public: | |
| 60 MockURLRequestJob(net::URLRequest* request, | |
| 61 const net::HttpResponseInfo& response_info, | |
| 62 const std::string& mime_type) | |
| 63 : net::URLRequestJob(request, nullptr), | |
| 64 response_info_(response_info), | |
| 65 mime_type_(mime_type) {} | |
| 66 | |
| 67 bool GetMimeType(std::string* mime_type) const override { | |
| 68 *mime_type = mime_type_; | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 protected: | |
| 73 void Start() override { NotifyHeadersComplete(); } | |
| 74 void GetResponseInfo(net::HttpResponseInfo* info) override { | |
| 75 *info = response_info_; | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 net::HttpResponseInfo response_info_; | |
| 80 std::string mime_type_; | |
| 81 }; | |
| 82 | |
| 83 class MockURLRequestJobFactory : public net::URLRequestJobFactory { | |
| 84 public: | |
| 85 MockURLRequestJobFactory() {} | |
| 86 ~MockURLRequestJobFactory() override {} | |
| 87 | |
| 88 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | |
| 89 const std::string& scheme, | |
| 90 net::URLRequest* request, | |
| 91 net::NetworkDelegate* network_delegate) const override { | |
| 92 return new MockURLRequestJob(request, response_info_, mime_type_); | |
| 93 } | |
| 94 | |
| 95 net::URLRequestJob* MaybeInterceptRedirect( | |
| 96 net::URLRequest* request, | |
| 97 net::NetworkDelegate* network_delegate, | |
| 98 const GURL& location) const override { | |
| 99 return nullptr; | |
| 100 } | |
| 101 | |
| 102 net::URLRequestJob* MaybeInterceptResponse( | |
| 103 net::URLRequest* request, | |
| 104 net::NetworkDelegate* network_delegate) const override { | |
| 105 return nullptr; | |
| 106 } | |
| 107 | |
| 108 bool IsHandledProtocol(const std::string& scheme) const override { | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 bool IsSafeRedirectTarget(const GURL& location) const override { | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 void set_response_info(const net::HttpResponseInfo& response_info) { | |
| 117 response_info_ = response_info; | |
| 118 } | |
| 119 | |
| 120 void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; } | |
| 121 | |
| 122 private: | |
| 123 net::HttpResponseInfo response_info_; | |
| 124 std::string mime_type_; | |
| 125 }; | |
| 126 | |
| 127 template <typename T> | 45 template <typename T> |
| 128 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { | 46 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { |
| 129 public: | 47 public: |
| 130 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} | 48 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} |
| 131 void GetAllData(std::map<std::string, T>* data_map, | 49 void GetAllData(std::map<std::string, T>* data_map, |
| 132 sql::Connection* db) const override { | 50 sql::Connection* db) const override { |
| 133 *data_map = data_; | 51 *data_map = data_; |
| 134 } | 52 } |
| 135 void UpdateData(const std::string& key, | 53 void UpdateData(const std::string& key, |
| 136 const T& data, | 54 const T& data, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 URLRequestSummary CreateRedirectRequestSummary( | 151 URLRequestSummary CreateRedirectRequestSummary( |
| 234 SessionID::id_type session_id, | 152 SessionID::id_type session_id, |
| 235 const std::string& main_frame_url, | 153 const std::string& main_frame_url, |
| 236 const std::string& redirect_url) { | 154 const std::string& redirect_url) { |
| 237 URLRequestSummary summary = | 155 URLRequestSummary summary = |
| 238 CreateURLRequestSummary(session_id, main_frame_url); | 156 CreateURLRequestSummary(session_id, main_frame_url); |
| 239 summary.redirect_url = GURL(redirect_url); | 157 summary.redirect_url = GURL(redirect_url); |
| 240 return summary; | 158 return summary; |
| 241 } | 159 } |
| 242 | 160 |
| 243 std::unique_ptr<net::URLRequest> CreateURLRequest( | |
| 244 const GURL& url, | |
| 245 net::RequestPriority priority, | |
| 246 content::ResourceType resource_type, | |
| 247 bool is_main_frame) { | |
| 248 std::unique_ptr<net::URLRequest> request = | |
| 249 url_request_context_.CreateRequest(url, priority, | |
| 250 &url_request_delegate_, | |
| 251 TRAFFIC_ANNOTATION_FOR_TESTS); | |
| 252 request->set_first_party_for_cookies(url); | |
| 253 content::ResourceRequestInfo::AllocateForTesting( | |
| 254 request.get(), resource_type, nullptr, -1, -1, -1, is_main_frame, false, | |
| 255 false, true, content::PREVIEWS_OFF); | |
| 256 request->Start(); | |
| 257 return request; | |
| 258 } | |
| 259 | |
| 260 void InitializePredictor() { | 161 void InitializePredictor() { |
| 261 loading_predictor_->StartInitialization(); | 162 loading_predictor_->StartInitialization(); |
| 262 base::RunLoop loop; | 163 base::RunLoop loop; |
| 263 loop.RunUntilIdle(); // Runs the DB lookup. | 164 loop.RunUntilIdle(); // Runs the DB lookup. |
| 264 profile_->BlockUntilHistoryProcessesPendingRequests(); | 165 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 265 } | 166 } |
| 266 | 167 |
| 267 void ResetPredictor(bool small_db = true) { | 168 void ResetPredictor(bool small_db = true) { |
| 268 LoadingPredictorConfig config; | 169 LoadingPredictorConfig config; |
| 269 PopulateTestConfig(&config, small_db); | 170 PopulateTestConfig(&config, small_db); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 284 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables>> mock_tables_; | 185 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables>> mock_tables_; |
| 285 | 186 |
| 286 PrefetchDataMap test_url_data_; | 187 PrefetchDataMap test_url_data_; |
| 287 PrefetchDataMap test_host_data_; | 188 PrefetchDataMap test_host_data_; |
| 288 RedirectDataMap test_url_redirect_data_; | 189 RedirectDataMap test_url_redirect_data_; |
| 289 RedirectDataMap test_host_redirect_data_; | 190 RedirectDataMap test_host_redirect_data_; |
| 290 ManifestDataMap test_manifest_data_; | 191 ManifestDataMap test_manifest_data_; |
| 291 OriginDataMap test_origin_data_; | 192 OriginDataMap test_origin_data_; |
| 292 | 193 |
| 293 MockURLRequestJobFactory url_request_job_factory_; | 194 MockURLRequestJobFactory url_request_job_factory_; |
| 294 EmptyURLRequestDelegate url_request_delegate_; | |
| 295 | 195 |
| 296 std::unique_ptr<base::HistogramTester> histogram_tester_; | 196 std::unique_ptr<base::HistogramTester> histogram_tester_; |
| 297 }; | 197 }; |
| 298 | 198 |
| 299 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() | 199 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() |
| 300 : profile_(new TestingProfile()), | 200 : profile_(new TestingProfile()), |
| 301 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()) {} | 201 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()) {} |
| 302 | 202 |
| 303 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() { | 203 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() { |
| 304 profile_.reset(NULL); | 204 profile_.reset(NULL); |
| 305 base::RunLoop().RunUntilIdle(); | 205 base::RunLoop().RunUntilIdle(); |
| 306 } | 206 } |
| 307 | 207 |
| 308 void ResourcePrefetchPredictorTest::SetUp() { | 208 void ResourcePrefetchPredictorTest::SetUp() { |
| 309 InitializeSampleData(); | 209 InitializeSampleData(); |
| 310 | 210 |
| 311 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); | 211 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); |
| 312 profile_->BlockUntilHistoryProcessesPendingRequests(); | 212 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 313 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( | 213 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
| 314 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); | 214 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); |
| 315 // Initialize the predictor with empty data. | 215 // Initialize the predictor with empty data. |
| 316 ResetPredictor(); | 216 ResetPredictor(); |
| 317 EXPECT_EQ(predictor_->initialization_state_, | 217 EXPECT_EQ(predictor_->initialization_state_, |
| 318 ResourcePrefetchPredictor::NOT_INITIALIZED); | 218 ResourcePrefetchPredictor::NOT_INITIALIZED); |
| 319 InitializePredictor(); | 219 InitializePredictor(); |
| 320 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); | 220 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); |
| 321 EXPECT_EQ(predictor_->initialization_state_, | 221 EXPECT_EQ(predictor_->initialization_state_, |
| 322 ResourcePrefetchPredictor::INITIALIZED); | 222 ResourcePrefetchPredictor::INITIALIZED); |
| 323 | 223 |
| 224 url_request_job_factory_.Reset(); |
| 324 url_request_context_.set_job_factory(&url_request_job_factory_); | 225 url_request_context_.set_job_factory(&url_request_job_factory_); |
| 325 | 226 |
| 326 histogram_tester_.reset(new base::HistogramTester()); | 227 histogram_tester_.reset(new base::HistogramTester()); |
| 327 } | 228 } |
| 328 | 229 |
| 329 void ResourcePrefetchPredictorTest::TearDown() { | 230 void ResourcePrefetchPredictorTest::TearDown() { |
| 330 EXPECT_EQ(*predictor_->url_resource_data_->data_cache_, | 231 EXPECT_EQ(*predictor_->url_resource_data_->data_cache_, |
| 331 mock_tables_->url_resource_table_.data_); | 232 mock_tables_->url_resource_table_.data_); |
| 332 EXPECT_EQ(*predictor_->url_redirect_data_->data_cache_, | 233 EXPECT_EQ(*predictor_->url_redirect_data_->data_cache_, |
| 333 mock_tables_->url_redirect_table_.data_); | 234 mock_tables_->url_redirect_table_.data_); |
| (...skipping 1072 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1406 predictor_->inflight_navigations_[main_frame1.navigation_id] | 1307 predictor_->inflight_navigations_[main_frame1.navigation_id] |
| 1407 ->subresource_requests[0]); | 1308 ->subresource_requests[0]); |
| 1408 EXPECT_EQ(resource2, | 1309 EXPECT_EQ(resource2, |
| 1409 predictor_->inflight_navigations_[main_frame1.navigation_id] | 1310 predictor_->inflight_navigations_[main_frame1.navigation_id] |
| 1410 ->subresource_requests[1]); | 1311 ->subresource_requests[1]); |
| 1411 EXPECT_EQ(resource3, | 1312 EXPECT_EQ(resource3, |
| 1412 predictor_->inflight_navigations_[main_frame1.navigation_id] | 1313 predictor_->inflight_navigations_[main_frame1.navigation_id] |
| 1413 ->subresource_requests[2]); | 1314 ->subresource_requests[2]); |
| 1414 } | 1315 } |
| 1415 | 1316 |
| 1416 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) { | |
| 1417 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1418 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); | |
| 1419 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1420 content::RESOURCE_TYPE_STYLESHEET, "")); | |
| 1421 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1422 content::RESOURCE_TYPE_WORKER, "text/css")); | |
| 1423 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1424 content::RESOURCE_TYPE_WORKER, "")); | |
| 1425 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1426 content::RESOURCE_TYPE_PREFETCH, "text/css")); | |
| 1427 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1428 content::RESOURCE_TYPE_PREFETCH, "bogus/mime-type")); | |
| 1429 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1430 content::RESOURCE_TYPE_PREFETCH, "")); | |
| 1431 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1432 content::RESOURCE_TYPE_PREFETCH, "application/font-woff")); | |
| 1433 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1434 content::RESOURCE_TYPE_PREFETCH, "font/woff2")); | |
| 1435 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1436 content::RESOURCE_TYPE_XHR, "")); | |
| 1437 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1438 content::RESOURCE_TYPE_XHR, "bogus/mime-type")); | |
| 1439 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( | |
| 1440 content::RESOURCE_TYPE_XHR, "application/javascript")); | |
| 1441 } | |
| 1442 | |
| 1443 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordRequestMainFrame) { | |
| 1444 std::unique_ptr<net::URLRequest> http_request = | |
| 1445 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, | |
| 1446 content::RESOURCE_TYPE_IMAGE, true); | |
| 1447 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1448 http_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 1449 | |
| 1450 std::unique_ptr<net::URLRequest> https_request = | |
| 1451 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, | |
| 1452 content::RESOURCE_TYPE_IMAGE, true); | |
| 1453 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1454 https_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 1455 | |
| 1456 std::unique_ptr<net::URLRequest> file_request = | |
| 1457 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, | |
| 1458 content::RESOURCE_TYPE_IMAGE, true); | |
| 1459 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1460 file_request.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 1461 | |
| 1462 std::unique_ptr<net::URLRequest> https_request_with_port = | |
| 1463 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM, | |
| 1464 content::RESOURCE_TYPE_IMAGE, true); | |
| 1465 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1466 https_request_with_port.get(), content::RESOURCE_TYPE_MAIN_FRAME)); | |
| 1467 } | |
| 1468 | |
| 1469 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordRequestSubResource) { | |
| 1470 std::unique_ptr<net::URLRequest> http_request = | |
| 1471 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 1472 content::RESOURCE_TYPE_IMAGE, false); | |
| 1473 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1474 http_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 1475 | |
| 1476 std::unique_ptr<net::URLRequest> https_request = | |
| 1477 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, | |
| 1478 content::RESOURCE_TYPE_IMAGE, false); | |
| 1479 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1480 https_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 1481 | |
| 1482 std::unique_ptr<net::URLRequest> file_request = | |
| 1483 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, | |
| 1484 content::RESOURCE_TYPE_IMAGE, false); | |
| 1485 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1486 file_request.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 1487 | |
| 1488 std::unique_ptr<net::URLRequest> https_request_with_port = | |
| 1489 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM, | |
| 1490 content::RESOURCE_TYPE_IMAGE, false); | |
| 1491 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( | |
| 1492 https_request_with_port.get(), content::RESOURCE_TYPE_IMAGE)); | |
| 1493 } | |
| 1494 | |
| 1495 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseMainFrame) { | |
| 1496 net::HttpResponseInfo response_info; | |
| 1497 response_info.headers = MakeResponseHeaders(""); | |
| 1498 url_request_job_factory_.set_response_info(response_info); | |
| 1499 | |
| 1500 std::unique_ptr<net::URLRequest> http_request = | |
| 1501 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, | |
| 1502 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 1503 EXPECT_TRUE( | |
| 1504 ResourcePrefetchPredictor::ShouldRecordResponse(http_request.get())); | |
| 1505 | |
| 1506 std::unique_ptr<net::URLRequest> https_request = | |
| 1507 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, | |
| 1508 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 1509 EXPECT_TRUE( | |
| 1510 ResourcePrefetchPredictor::ShouldRecordResponse(https_request.get())); | |
| 1511 | |
| 1512 std::unique_ptr<net::URLRequest> file_request = | |
| 1513 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, | |
| 1514 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 1515 EXPECT_FALSE( | |
| 1516 ResourcePrefetchPredictor::ShouldRecordResponse(file_request.get())); | |
| 1517 | |
| 1518 std::unique_ptr<net::URLRequest> https_request_with_port = | |
| 1519 CreateURLRequest(GURL("https://www.google.com:666"), net::MEDIUM, | |
| 1520 content::RESOURCE_TYPE_MAIN_FRAME, true); | |
| 1521 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1522 https_request_with_port.get())); | |
| 1523 } | |
| 1524 | |
| 1525 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseSubresource) { | |
| 1526 net::HttpResponseInfo response_info; | |
| 1527 response_info.headers = | |
| 1528 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); | |
| 1529 response_info.was_cached = true; | |
| 1530 url_request_job_factory_.set_response_info(response_info); | |
| 1531 | |
| 1532 // Protocol. | |
| 1533 std::unique_ptr<net::URLRequest> http_image_request = | |
| 1534 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 1535 content::RESOURCE_TYPE_IMAGE, true); | |
| 1536 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1537 http_image_request.get())); | |
| 1538 | |
| 1539 std::unique_ptr<net::URLRequest> https_image_request = | |
| 1540 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, | |
| 1541 content::RESOURCE_TYPE_IMAGE, true); | |
| 1542 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1543 https_image_request.get())); | |
| 1544 | |
| 1545 std::unique_ptr<net::URLRequest> https_image_request_with_port = | |
| 1546 CreateURLRequest(GURL("https://www.google.com:666/cat.png"), net::MEDIUM, | |
| 1547 content::RESOURCE_TYPE_IMAGE, true); | |
| 1548 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1549 https_image_request_with_port.get())); | |
| 1550 | |
| 1551 std::unique_ptr<net::URLRequest> file_image_request = | |
| 1552 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, | |
| 1553 content::RESOURCE_TYPE_IMAGE, true); | |
| 1554 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1555 file_image_request.get())); | |
| 1556 | |
| 1557 // ResourceType. | |
| 1558 std::unique_ptr<net::URLRequest> sub_frame_request = | |
| 1559 CreateURLRequest(GURL("http://www.google.com/frame.html"), net::MEDIUM, | |
| 1560 content::RESOURCE_TYPE_SUB_FRAME, true); | |
| 1561 EXPECT_FALSE( | |
| 1562 ResourcePrefetchPredictor::ShouldRecordResponse(sub_frame_request.get())); | |
| 1563 | |
| 1564 std::unique_ptr<net::URLRequest> font_request = | |
| 1565 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
| 1566 net::MEDIUM, content::RESOURCE_TYPE_FONT_RESOURCE, true); | |
| 1567 EXPECT_TRUE( | |
| 1568 ResourcePrefetchPredictor::ShouldRecordResponse(font_request.get())); | |
| 1569 | |
| 1570 // From MIME Type. | |
| 1571 url_request_job_factory_.set_mime_type("image/png"); | |
| 1572 std::unique_ptr<net::URLRequest> prefetch_image_request = | |
| 1573 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 1574 content::RESOURCE_TYPE_PREFETCH, true); | |
| 1575 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1576 prefetch_image_request.get())); | |
| 1577 | |
| 1578 url_request_job_factory_.set_mime_type("image/my-wonderful-format"); | |
| 1579 std::unique_ptr<net::URLRequest> prefetch_unknown_image_request = | |
| 1580 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | |
| 1581 content::RESOURCE_TYPE_PREFETCH, true); | |
| 1582 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1583 prefetch_unknown_image_request.get())); | |
| 1584 | |
| 1585 url_request_job_factory_.set_mime_type("font/woff"); | |
| 1586 std::unique_ptr<net::URLRequest> prefetch_font_request = | |
| 1587 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
| 1588 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true); | |
| 1589 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1590 prefetch_font_request.get())); | |
| 1591 | |
| 1592 url_request_job_factory_.set_mime_type("font/woff-woff"); | |
| 1593 std::unique_ptr<net::URLRequest> prefetch_unknown_font_request = | |
| 1594 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), | |
| 1595 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true); | |
| 1596 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1597 prefetch_unknown_font_request.get())); | |
| 1598 | |
| 1599 // Not main frame. | |
| 1600 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest( | |
| 1601 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM, | |
| 1602 content::RESOURCE_TYPE_FONT_RESOURCE, false); | |
| 1603 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( | |
| 1604 font_request_sub_frame.get())); | |
| 1605 } | |
| 1606 | |
| 1607 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) { | 1317 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) { |
| 1608 net::HttpResponseInfo response_info; | 1318 net::HttpResponseInfo response_info; |
| 1609 response_info.headers = | 1319 response_info.headers = |
| 1610 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); | 1320 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); |
| 1611 response_info.was_cached = true; | 1321 response_info.was_cached = true; |
| 1612 url_request_job_factory_.set_response_info(response_info); | 1322 url_request_job_factory_.set_response_info(response_info); |
| 1613 | 1323 |
| 1614 GURL url("http://www.google.com/cat.png"); | 1324 GURL url("http://www.google.com/cat.png"); |
| 1615 std::unique_ptr<net::URLRequest> request = | 1325 std::unique_ptr<net::URLRequest> request = |
| 1616 CreateURLRequest(url, net::MEDIUM, content::RESOURCE_TYPE_IMAGE, true); | 1326 CreateURLRequest(url_request_context_, url, net::MEDIUM, |
| 1327 content::RESOURCE_TYPE_IMAGE, true); |
| 1617 URLRequestSummary summary; | 1328 URLRequestSummary summary; |
| 1618 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary)); | 1329 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary)); |
| 1619 EXPECT_EQ(url, summary.resource_url); | 1330 EXPECT_EQ(url, summary.resource_url); |
| 1620 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type); | 1331 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type); |
| 1621 EXPECT_TRUE(summary.was_cached); | 1332 EXPECT_TRUE(summary.was_cached); |
| 1622 EXPECT_FALSE(summary.has_validators); | 1333 EXPECT_FALSE(summary.has_validators); |
| 1623 EXPECT_FALSE(summary.always_revalidate); | 1334 EXPECT_FALSE(summary.always_revalidate); |
| 1624 | 1335 |
| 1625 // Navigation_id elements should be unset by default. | 1336 // Navigation_id elements should be unset by default. |
| 1626 EXPECT_EQ(-1, summary.navigation_id.tab_id); | 1337 EXPECT_EQ(-1, summary.navigation_id.tab_id); |
| 1627 EXPECT_EQ(GURL(), summary.navigation_id.main_frame_url); | 1338 EXPECT_EQ(GURL(), summary.navigation_id.main_frame_url); |
| 1628 } | 1339 } |
| 1629 | 1340 |
| 1630 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseContentType) { | 1341 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseContentType) { |
| 1631 net::HttpResponseInfo response_info; | 1342 net::HttpResponseInfo response_info; |
| 1632 response_info.headers = MakeResponseHeaders( | 1343 response_info.headers = MakeResponseHeaders( |
| 1633 "HTTP/1.1 200 OK\n\n" | 1344 "HTTP/1.1 200 OK\n\n" |
| 1634 "Some: Headers\n" | 1345 "Some: Headers\n" |
| 1635 "Content-Type: image/whatever\n"); | 1346 "Content-Type: image/whatever\n"); |
| 1636 url_request_job_factory_.set_response_info(response_info); | 1347 url_request_job_factory_.set_response_info(response_info); |
| 1637 url_request_job_factory_.set_mime_type("image/png"); | 1348 url_request_job_factory_.set_mime_type("image/png"); |
| 1638 | 1349 |
| 1639 std::unique_ptr<net::URLRequest> request = | 1350 std::unique_ptr<net::URLRequest> request = CreateURLRequest( |
| 1640 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | 1351 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM, |
| 1641 content::RESOURCE_TYPE_PREFETCH, true); | 1352 content::RESOURCE_TYPE_PREFETCH, true); |
| 1642 URLRequestSummary summary; | 1353 URLRequestSummary summary; |
| 1643 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary)); | 1354 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary)); |
| 1644 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type); | 1355 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type); |
| 1645 } | 1356 } |
| 1646 | 1357 |
| 1647 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseCachePolicy) { | 1358 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseCachePolicy) { |
| 1648 net::HttpResponseInfo response_info; | 1359 net::HttpResponseInfo response_info; |
| 1649 response_info.headers = MakeResponseHeaders( | 1360 response_info.headers = MakeResponseHeaders( |
| 1650 "HTTP/1.1 200 OK\n" | 1361 "HTTP/1.1 200 OK\n" |
| 1651 "Some: Headers\n"); | 1362 "Some: Headers\n"); |
| 1652 url_request_job_factory_.set_response_info(response_info); | 1363 url_request_job_factory_.set_response_info(response_info); |
| 1653 | 1364 |
| 1654 std::unique_ptr<net::URLRequest> request_no_validators = | 1365 std::unique_ptr<net::URLRequest> request_no_validators = CreateURLRequest( |
| 1655 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | 1366 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM, |
| 1656 content::RESOURCE_TYPE_PREFETCH, true); | 1367 content::RESOURCE_TYPE_PREFETCH, true); |
| 1657 | 1368 |
| 1658 URLRequestSummary summary; | 1369 URLRequestSummary summary; |
| 1659 EXPECT_TRUE( | 1370 EXPECT_TRUE( |
| 1660 URLRequestSummary::SummarizeResponse(*request_no_validators, &summary)); | 1371 URLRequestSummary::SummarizeResponse(*request_no_validators, &summary)); |
| 1661 EXPECT_FALSE(summary.has_validators); | 1372 EXPECT_FALSE(summary.has_validators); |
| 1662 | 1373 |
| 1663 response_info.headers = MakeResponseHeaders( | 1374 response_info.headers = MakeResponseHeaders( |
| 1664 "HTTP/1.1 200 OK\n" | 1375 "HTTP/1.1 200 OK\n" |
| 1665 "ETag: \"Cr66\"\n" | 1376 "ETag: \"Cr66\"\n" |
| 1666 "Cache-Control: no-cache\n"); | 1377 "Cache-Control: no-cache\n"); |
| 1667 url_request_job_factory_.set_response_info(response_info); | 1378 url_request_job_factory_.set_response_info(response_info); |
| 1668 std::unique_ptr<net::URLRequest> request_etag = | 1379 std::unique_ptr<net::URLRequest> request_etag = CreateURLRequest( |
| 1669 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, | 1380 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM, |
| 1670 content::RESOURCE_TYPE_PREFETCH, true); | 1381 content::RESOURCE_TYPE_PREFETCH, true); |
| 1671 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); | 1382 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); |
| 1672 EXPECT_TRUE(summary.has_validators); | 1383 EXPECT_TRUE(summary.has_validators); |
| 1673 EXPECT_TRUE(summary.always_revalidate); | 1384 EXPECT_TRUE(summary.always_revalidate); |
| 1674 } | 1385 } |
| 1675 | 1386 |
| 1676 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) { | 1387 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) { |
| 1677 // The data that will be used in populating. | 1388 // The data that will be used in populating. |
| 1678 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); | 1389 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); |
| 1679 InitializeResourceData(google.add_resources(), "http://google.com/image1.png", | 1390 InitializeResourceData(google.add_resources(), "http://google.com/image1.png", |
| 1680 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2, | 1391 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2, |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1962 ResourceData* resource3_rd = host_data.add_resources(); | 1673 ResourceData* resource3_rd = host_data.add_resources(); |
| 1963 InitializeResourceData(resource3_rd, "http://google.com/script2.js", | 1674 InitializeResourceData(resource3_rd, "http://google.com/script2.js", |
| 1964 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, | 1675 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, |
| 1965 net::MEDIUM, false, false); | 1676 net::MEDIUM, false, false); |
| 1966 resource3_rd->set_before_first_contentful_paint(false); | 1677 resource3_rd->set_before_first_contentful_paint(false); |
| 1967 EXPECT_EQ(mock_tables_->host_resource_table_.data_, | 1678 EXPECT_EQ(mock_tables_->host_resource_table_.data_, |
| 1968 PrefetchDataMap({{host_data.primary_key(), host_data}})); | 1679 PrefetchDataMap({{host_data.primary_key(), host_data}})); |
| 1969 } | 1680 } |
| 1970 | 1681 |
| 1971 } // namespace predictors | 1682 } // namespace predictors |
| OLD | NEW |