Chromium Code Reviews| 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 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 using testing::ContainerEq; | 30 using testing::ContainerEq; |
| 31 using testing::Pointee; | 31 using testing::Pointee; |
| 32 using testing::SetArgPointee; | 32 using testing::SetArgPointee; |
| 33 using testing::StrictMock; | 33 using testing::StrictMock; |
| 34 using testing::UnorderedElementsAre; | 34 using testing::UnorderedElementsAre; |
| 35 | 35 |
| 36 namespace predictors { | 36 namespace predictors { |
| 37 | 37 |
| 38 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; | 38 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 39 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 39 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 40 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 40 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; |
| 41 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; | |
| 41 | 42 |
| 42 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( | 43 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| 43 const char* headers) { | 44 const char* headers) { |
| 44 return make_scoped_refptr(new net::HttpResponseHeaders( | 45 return make_scoped_refptr(new net::HttpResponseHeaders( |
| 45 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); | 46 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); |
| 46 } | 47 } |
| 47 | 48 |
| 48 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { | 49 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { |
| 49 void OnResponseStarted(net::URLRequest* request, int net_error) override {} | 50 void OnResponseStarted(net::URLRequest* request, int net_error) override {} |
| 50 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} | 51 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 void(const std::vector<std::string>& urls, | 147 void(const std::vector<std::string>& urls, |
| 147 const std::vector<std::string>& hosts)); | 148 const std::vector<std::string>& hosts)); |
| 148 MOCK_METHOD2(DeleteSingleRedirectDataPoint, | 149 MOCK_METHOD2(DeleteSingleRedirectDataPoint, |
| 149 void(const std::string& key, PrefetchKeyType key_type)); | 150 void(const std::string& key, PrefetchKeyType key_type)); |
| 150 MOCK_METHOD0(DeleteAllData, void()); | 151 MOCK_METHOD0(DeleteAllData, void()); |
| 151 | 152 |
| 152 protected: | 153 protected: |
| 153 ~MockResourcePrefetchPredictorTables() { } | 154 ~MockResourcePrefetchPredictorTables() { } |
| 154 }; | 155 }; |
| 155 | 156 |
| 157 class MockResourcePrefetchPredictorObserver | |
| 158 : public ResourcePrefetchPredictor::TestObserver { | |
| 159 public: | |
| 160 MockResourcePrefetchPredictorObserver(ResourcePrefetchPredictor* predictor) | |
| 161 : TestObserver(predictor) {} | |
| 162 | |
| 163 MOCK_METHOD2( | |
| 164 OnNavigationLearned, | |
| 165 void(size_t url_visit_count, | |
| 166 const ResourcePrefetchPredictor::PageRequestSummary& summary)); | |
| 167 }; | |
| 168 | |
| 156 class ResourcePrefetchPredictorTest : public testing::Test { | 169 class ResourcePrefetchPredictorTest : public testing::Test { |
| 157 public: | 170 public: |
| 158 ResourcePrefetchPredictorTest(); | 171 ResourcePrefetchPredictorTest(); |
| 159 ~ResourcePrefetchPredictorTest() override; | 172 ~ResourcePrefetchPredictorTest() override; |
| 160 void SetUp() override; | 173 void SetUp() override; |
| 161 void TearDown() override; | 174 void TearDown() override; |
| 162 | 175 |
| 163 protected: | 176 protected: |
| 164 void AddUrlToHistory(const std::string& url, int visit_count) { | 177 void AddUrlToHistory(const std::string& url, int visit_count) { |
| 165 HistoryServiceFactory::GetForProfile(profile_.get(), | 178 HistoryServiceFactory::GetForProfile(profile_.get(), |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 177 | 190 |
| 178 NavigationID CreateNavigationID(int process_id, | 191 NavigationID CreateNavigationID(int process_id, |
| 179 int render_frame_id, | 192 int render_frame_id, |
| 180 const std::string& main_frame_url) { | 193 const std::string& main_frame_url) { |
| 181 NavigationID navigation_id(process_id, render_frame_id, | 194 NavigationID navigation_id(process_id, render_frame_id, |
| 182 GURL(main_frame_url)); | 195 GURL(main_frame_url)); |
| 183 navigation_id.creation_time = base::TimeTicks::Now(); | 196 navigation_id.creation_time = base::TimeTicks::Now(); |
| 184 return navigation_id; | 197 return navigation_id; |
| 185 } | 198 } |
| 186 | 199 |
| 200 PageRequestSummary CreatePageRequestSummary( | |
| 201 const std::string& main_frame_url, | |
| 202 const std::string& initial_url, | |
| 203 const std::vector<URLRequestSummary>& subresource_requests) { | |
| 204 GURL main_frame_gurl(main_frame_url); | |
| 205 PageRequestSummary summary(main_frame_gurl); | |
| 206 summary.initial_url = GURL(initial_url); | |
| 207 summary.subresource_requests = subresource_requests; | |
| 208 return summary; | |
| 209 } | |
| 210 | |
| 187 URLRequestSummary CreateURLRequestSummary( | 211 URLRequestSummary CreateURLRequestSummary( |
| 188 int process_id, | 212 int process_id, |
| 189 int render_frame_id, | 213 int render_frame_id, |
| 190 const std::string& main_frame_url, | 214 const std::string& main_frame_url, |
| 191 const std::string& resource_url = std::string(), | 215 const std::string& resource_url = std::string(), |
| 192 content::ResourceType resource_type = content::RESOURCE_TYPE_MAIN_FRAME, | 216 content::ResourceType resource_type = content::RESOURCE_TYPE_MAIN_FRAME, |
| 193 net::RequestPriority priority = net::MEDIUM, | 217 net::RequestPriority priority = net::MEDIUM, |
| 194 const std::string& mime_type = std::string(), | 218 const std::string& mime_type = std::string(), |
| 195 bool was_cached = false) { | 219 bool was_cached = false) { |
| 196 URLRequestSummary summary; | 220 URLRequestSummary summary; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 config.min_url_visit_count = 2; | 281 config.min_url_visit_count = 2; |
| 258 config.max_resources_per_entry = 4; | 282 config.max_resources_per_entry = 4; |
| 259 config.max_consecutive_misses = 2; | 283 config.max_consecutive_misses = 2; |
| 260 config.min_resource_confidence_to_trigger_prefetch = 0.5; | 284 config.min_resource_confidence_to_trigger_prefetch = 0.5; |
| 261 | 285 |
| 262 // TODO(shishir): Enable the prefetching mode in the tests. | 286 // TODO(shishir): Enable the prefetching mode in the tests. |
| 263 config.mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; | 287 config.mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; |
| 264 config.mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; | 288 config.mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; |
| 265 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); | 289 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); |
| 266 predictor_->set_mock_tables(mock_tables_); | 290 predictor_->set_mock_tables(mock_tables_); |
| 291 mock_observer_.reset(new StrictMock<MockResourcePrefetchPredictorObserver>( | |
|
pasko
2016/10/24 17:59:38
If there is a ResetPredictor() somewhere in the te
pasko
2016/10/24 17:59:38
It would be nice to avoid this having a 'global' m
alexilin
2016/10/25 11:38:09
Yeah, it's a kind of dangerous situation.
Let's tr
pasko
2016/10/25 14:05:40
this latter sgtm
alexilin
2016/10/25 15:10:58
Done.
| |
| 292 predictor_.get())); | |
| 267 } | 293 } |
| 268 | 294 |
| 269 void InitializeSampleData(); | 295 void InitializeSampleData(); |
| 270 | 296 |
| 271 content::TestBrowserThreadBundle thread_bundle_; | 297 content::TestBrowserThreadBundle thread_bundle_; |
| 272 std::unique_ptr<TestingProfile> profile_; | 298 std::unique_ptr<TestingProfile> profile_; |
| 273 net::TestURLRequestContext url_request_context_; | 299 net::TestURLRequestContext url_request_context_; |
| 274 | 300 |
| 275 std::unique_ptr<ResourcePrefetchPredictor> predictor_; | 301 std::unique_ptr<ResourcePrefetchPredictor> predictor_; |
| 276 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_; | 302 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_; |
| 303 std::unique_ptr<StrictMock<MockResourcePrefetchPredictorObserver>> | |
| 304 mock_observer_; | |
| 277 | 305 |
| 278 PrefetchDataMap test_url_data_; | 306 PrefetchDataMap test_url_data_; |
| 279 PrefetchDataMap test_host_data_; | 307 PrefetchDataMap test_host_data_; |
| 280 RedirectDataMap test_url_redirect_data_; | 308 RedirectDataMap test_url_redirect_data_; |
| 281 RedirectDataMap test_host_redirect_data_; | 309 RedirectDataMap test_host_redirect_data_; |
| 282 PrefetchData empty_resource_data_; | 310 PrefetchData empty_resource_data_; |
| 283 RedirectData empty_redirect_data_; | 311 RedirectData empty_redirect_data_; |
| 284 | 312 |
| 285 MockURLRequestJobFactory url_request_job_factory_; | 313 MockURLRequestJobFactory url_request_job_factory_; |
| 286 EmptyURLRequestDelegate url_request_delegate_; | 314 EmptyURLRequestDelegate url_request_delegate_; |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 472 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); | 500 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); |
| 473 | 501 |
| 474 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); | 502 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); |
| 475 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); | 503 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); |
| 476 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_); | 504 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_); |
| 477 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_); | 505 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_); |
| 478 } | 506 } |
| 479 | 507 |
| 480 // Single navigation but history count is low, so should not record. | 508 // Single navigation but history count is low, so should not record. |
| 481 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { | 509 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { |
| 482 AddUrlToHistory("http://www.google.com", 1); | 510 const int kVisitCount = 1; |
| 511 AddUrlToHistory("https://www.google.com", kVisitCount); | |
| 483 | 512 |
| 484 URLRequestSummary main_frame = | 513 URLRequestSummary main_frame = |
| 485 CreateURLRequestSummary(1, 1, "http://www.google.com"); | 514 CreateURLRequestSummary(1, 1, "http://www.google.com"); |
| 486 predictor_->RecordURLRequest(main_frame); | 515 predictor_->RecordURLRequest(main_frame); |
| 487 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 516 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 488 | 517 |
| 489 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary( | 518 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary( |
| 490 1, 1, "http://www.google.com", "https://www.google.com"); | 519 1, 1, "http://www.google.com", "https://www.google.com"); |
| 491 predictor_->RecordURLRedirect(main_frame_redirect); | 520 predictor_->RecordURLRedirect(main_frame_redirect); |
| 492 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 521 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 493 main_frame = CreateURLRequestSummary(1, 1, "https://www.google.com"); | 522 main_frame = CreateURLRequestSummary(1, 1, "https://www.google.com"); |
| 494 | 523 |
| 495 // Now add a few subresources. | 524 // Now add a few subresources. |
| 496 URLRequestSummary resource1 = CreateURLRequestSummary( | 525 URLRequestSummary resource1 = CreateURLRequestSummary( |
| 497 1, 1, "https://www.google.com", "https://google.com/style1.css", | 526 1, 1, "https://www.google.com", "https://google.com/style1.css", |
| 498 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); | 527 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); |
| 499 predictor_->RecordURLResponse(resource1); | 528 predictor_->RecordURLResponse(resource1); |
| 500 URLRequestSummary resource2 = CreateURLRequestSummary( | 529 URLRequestSummary resource2 = CreateURLRequestSummary( |
| 501 1, 1, "https://www.google.com", "https://google.com/script1.js", | 530 1, 1, "https://www.google.com", "https://google.com/script1.js", |
| 502 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); | 531 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); |
| 503 predictor_->RecordURLResponse(resource2); | 532 predictor_->RecordURLResponse(resource2); |
| 504 URLRequestSummary resource3 = CreateURLRequestSummary( | 533 URLRequestSummary resource3 = CreateURLRequestSummary( |
| 505 1, 1, "https://www.google.com", "https://google.com/script2.js", | 534 1, 1, "https://www.google.com", "https://google.com/script2.js", |
| 506 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); | 535 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); |
| 507 predictor_->RecordURLResponse(resource3); | 536 predictor_->RecordURLResponse(resource3); |
| 508 | 537 |
| 538 EXPECT_CALL( | |
| 539 *mock_observer_.get(), | |
| 540 OnNavigationLearned(kVisitCount, | |
| 541 CreatePageRequestSummary( | |
| 542 "https://www.google.com", "http://www.google.com", | |
| 543 {resource1, resource2, resource3}))); | |
| 544 | |
| 509 PrefetchData host_data = CreatePrefetchData("www.google.com"); | 545 PrefetchData host_data = CreatePrefetchData("www.google.com"); |
| 510 InitializeResourceData(host_data.add_resources(), | 546 InitializeResourceData(host_data.add_resources(), |
| 511 "https://google.com/style1.css", | 547 "https://google.com/style1.css", |
| 512 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, | 548 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, |
| 513 net::MEDIUM, false, false); | 549 net::MEDIUM, false, false); |
| 514 InitializeResourceData( | 550 InitializeResourceData( |
| 515 host_data.add_resources(), "https://google.com/script1.js", | 551 host_data.add_resources(), "https://google.com/script1.js", |
| 516 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); | 552 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); |
| 517 InitializeResourceData( | 553 InitializeResourceData( |
| 518 host_data.add_resources(), "https://google.com/script2.js", | 554 host_data.add_resources(), "https://google.com/script2.js", |
| 519 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 555 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
| 520 EXPECT_CALL(*mock_tables_.get(), | 556 EXPECT_CALL(*mock_tables_.get(), |
| 521 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 557 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 522 empty_redirect_data_)); | 558 empty_redirect_data_)); |
| 523 | 559 |
| 524 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); | 560 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 525 profile_->BlockUntilHistoryProcessesPendingRequests(); | 561 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 526 } | 562 } |
| 527 | 563 |
| 528 // Single navigation that will be recorded. Will check for duplicate | 564 // Single navigation that will be recorded. Will check for duplicate |
| 529 // resources and also for number of resources saved. | 565 // resources and also for number of resources saved. |
| 530 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { | 566 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { |
| 531 AddUrlToHistory("http://www.google.com", 4); | 567 const int kVisitCount = 4; |
| 568 AddUrlToHistory("http://www.google.com", kVisitCount); | |
| 532 | 569 |
| 533 URLRequestSummary main_frame = | 570 URLRequestSummary main_frame = |
| 534 CreateURLRequestSummary(1, 1, "http://www.google.com"); | 571 CreateURLRequestSummary(1, 1, "http://www.google.com"); |
| 535 predictor_->RecordURLRequest(main_frame); | 572 predictor_->RecordURLRequest(main_frame); |
| 536 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 573 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 537 | 574 |
| 538 URLRequestSummary resource1 = CreateURLRequestSummary( | 575 std::vector<URLRequestSummary> resources; |
| 576 resources.push_back(CreateURLRequestSummary( | |
| 539 1, 1, "http://www.google.com", "http://google.com/style1.css", | 577 1, 1, "http://www.google.com", "http://google.com/style1.css", |
| 540 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); | 578 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); |
| 541 predictor_->RecordURLResponse(resource1); | 579 predictor_->RecordURLResponse(resources.back()); |
| 542 URLRequestSummary resource2 = CreateURLRequestSummary( | 580 resources.push_back(CreateURLRequestSummary( |
| 543 1, 1, "http://www.google.com", "http://google.com/script1.js", | 581 1, 1, "http://www.google.com", "http://google.com/script1.js", |
| 544 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); | 582 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); |
| 545 predictor_->RecordURLResponse(resource2); | 583 predictor_->RecordURLResponse(resources.back()); |
| 546 URLRequestSummary resource3 = CreateURLRequestSummary( | 584 resources.push_back(CreateURLRequestSummary( |
| 547 1, 1, "http://www.google.com", "http://google.com/script2.js", | 585 1, 1, "http://www.google.com", "http://google.com/script2.js", |
| 548 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); | 586 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); |
| 549 predictor_->RecordURLResponse(resource3); | 587 predictor_->RecordURLResponse(resources.back()); |
| 550 URLRequestSummary resource4 = CreateURLRequestSummary( | 588 resources.push_back(CreateURLRequestSummary( |
| 551 1, 1, "http://www.google.com", "http://google.com/script1.js", | 589 1, 1, "http://www.google.com", "http://google.com/script1.js", |
| 552 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true); | 590 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); |
| 553 predictor_->RecordURLResponse(resource4); | 591 predictor_->RecordURLResponse(resources.back()); |
| 554 URLRequestSummary resource5 = CreateURLRequestSummary( | 592 resources.push_back(CreateURLRequestSummary( |
| 555 1, 1, "http://www.google.com", "http://google.com/image1.png", | 593 1, 1, "http://www.google.com", "http://google.com/image1.png", |
| 556 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); | 594 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); |
| 557 predictor_->RecordURLResponse(resource5); | 595 predictor_->RecordURLResponse(resources.back()); |
| 558 URLRequestSummary resource6 = CreateURLRequestSummary( | 596 resources.push_back(CreateURLRequestSummary( |
| 559 1, 1, "http://www.google.com", "http://google.com/image2.png", | 597 1, 1, "http://www.google.com", "http://google.com/image2.png", |
| 560 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); | 598 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); |
| 561 predictor_->RecordURLResponse(resource6); | 599 predictor_->RecordURLResponse(resources.back()); |
| 562 URLRequestSummary resource7 = CreateURLRequestSummary( | 600 resources.push_back(CreateURLRequestSummary( |
| 563 1, 1, "http://www.google.com", "http://google.com/style2.css", | 601 1, 1, "http://www.google.com", "http://google.com/style2.css", |
| 564 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); | 602 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); |
| 565 predictor_->RecordURLResponse(resource7); | 603 predictor_->RecordURLResponse(resources.back()); |
| 604 | |
| 605 EXPECT_CALL(*mock_observer_.get(), | |
| 606 OnNavigationLearned( | |
| 607 kVisitCount, CreatePageRequestSummary("http://www.google.com", | |
| 608 "http://www.google.com", | |
| 609 resources))); | |
| 566 | 610 |
| 567 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); | 611 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); |
| 568 InitializeResourceData(url_data.add_resources(), | 612 InitializeResourceData(url_data.add_resources(), |
| 569 "http://google.com/style1.css", | 613 "http://google.com/style1.css", |
| 570 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, | 614 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, |
| 571 net::MEDIUM, false, false); | 615 net::MEDIUM, false, false); |
| 572 InitializeResourceData( | 616 InitializeResourceData( |
| 573 url_data.add_resources(), "http://google.com/script1.js", | 617 url_data.add_resources(), "http://google.com/script1.js", |
| 574 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); | 618 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); |
| 575 InitializeResourceData( | 619 InitializeResourceData( |
| 576 url_data.add_resources(), "http://google.com/script2.js", | 620 url_data.add_resources(), "http://google.com/script2.js", |
| 577 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 621 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
| 578 InitializeResourceData(url_data.add_resources(), | 622 InitializeResourceData(url_data.add_resources(), |
| 579 "http://google.com/style2.css", | 623 "http://google.com/style2.css", |
| 580 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, | 624 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, |
| 581 net::MEDIUM, false, false); | 625 net::MEDIUM, false, false); |
| 582 EXPECT_CALL(*mock_tables_.get(), | 626 EXPECT_CALL(*mock_tables_.get(), |
| 583 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, | 627 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, |
| 584 empty_redirect_data_)); | 628 empty_redirect_data_)); |
| 585 | 629 |
| 586 PrefetchData host_data = CreatePrefetchData("www.google.com"); | 630 PrefetchData host_data = CreatePrefetchData("www.google.com"); |
| 587 host_data.mutable_resources()->CopyFrom(url_data.resources()); | 631 host_data.mutable_resources()->CopyFrom(url_data.resources()); |
| 588 EXPECT_CALL(*mock_tables_.get(), | 632 EXPECT_CALL(*mock_tables_.get(), |
| 589 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 633 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 590 empty_redirect_data_)); | 634 empty_redirect_data_)); |
| 591 | 635 |
| 592 predictor_->OnNavigationComplete(main_frame.navigation_id); | 636 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 593 profile_->BlockUntilHistoryProcessesPendingRequests(); | 637 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 594 } | 638 } |
| 595 | 639 |
| 596 // Tests that navigation is recorded correctly for URL already present in | 640 // Tests that navigation is recorded correctly for URL already present in |
| 597 // the database cache. | 641 // the database cache. |
| 598 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { | 642 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { |
| 599 AddUrlToHistory("http://www.google.com", 4); | 643 const int kVisitCount = 4; |
| 644 AddUrlToHistory("http://www.google.com", kVisitCount); | |
| 600 | 645 |
| 601 EXPECT_CALL(*mock_tables_.get(), | 646 EXPECT_CALL(*mock_tables_.get(), |
| 602 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 647 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
| 603 Pointee(ContainerEq(PrefetchDataMap())), | 648 Pointee(ContainerEq(PrefetchDataMap())), |
| 604 Pointee(ContainerEq(RedirectDataMap())), | 649 Pointee(ContainerEq(RedirectDataMap())), |
| 605 Pointee(ContainerEq(RedirectDataMap())))) | 650 Pointee(ContainerEq(RedirectDataMap())))) |
| 606 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), | 651 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), |
| 607 SetArgPointee<1>(test_host_data_))); | 652 SetArgPointee<1>(test_host_data_))); |
| 608 ResetPredictor(); | 653 ResetPredictor(); |
| 609 InitializePredictor(); | 654 InitializePredictor(); |
| 610 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); | 655 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); |
| 611 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); | 656 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); |
| 612 | 657 |
| 613 URLRequestSummary main_frame = CreateURLRequestSummary( | 658 URLRequestSummary main_frame = CreateURLRequestSummary( |
| 614 1, 1, "http://www.google.com", "http://www.google.com", | 659 1, 1, "http://www.google.com", "http://www.google.com", |
| 615 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); | 660 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); |
| 616 predictor_->RecordURLRequest(main_frame); | 661 predictor_->RecordURLRequest(main_frame); |
| 617 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 662 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 618 | 663 |
| 619 URLRequestSummary resource1 = CreateURLRequestSummary( | 664 std::vector<URLRequestSummary> resources; |
| 665 resources.push_back(CreateURLRequestSummary( | |
| 620 1, 1, "http://www.google.com", "http://google.com/style1.css", | 666 1, 1, "http://www.google.com", "http://google.com/style1.css", |
| 621 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); | 667 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); |
| 622 predictor_->RecordURLResponse(resource1); | 668 predictor_->RecordURLResponse(resources.back()); |
| 623 URLRequestSummary resource2 = CreateURLRequestSummary( | 669 resources.push_back(CreateURLRequestSummary( |
| 624 1, 1, "http://www.google.com", "http://google.com/script1.js", | 670 1, 1, "http://www.google.com", "http://google.com/script1.js", |
| 625 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); | 671 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); |
| 626 predictor_->RecordURLResponse(resource2); | 672 predictor_->RecordURLResponse(resources.back()); |
| 627 URLRequestSummary resource3 = CreateURLRequestSummary( | 673 resources.push_back(CreateURLRequestSummary( |
| 628 1, 1, "http://www.google.com", "http://google.com/script2.js", | 674 1, 1, "http://www.google.com", "http://google.com/script2.js", |
| 629 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); | 675 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); |
| 630 predictor_->RecordURLResponse(resource3); | 676 predictor_->RecordURLResponse(resources.back()); |
| 631 URLRequestSummary resource4 = CreateURLRequestSummary( | 677 resources.push_back(CreateURLRequestSummary( |
| 632 1, 1, "http://www.google.com", "http://google.com/script1.js", | 678 1, 1, "http://www.google.com", "http://google.com/script1.js", |
| 633 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true); | 679 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); |
| 634 predictor_->RecordURLResponse(resource4); | 680 predictor_->RecordURLResponse(resources.back()); |
| 635 URLRequestSummary resource5 = CreateURLRequestSummary( | 681 resources.push_back(CreateURLRequestSummary( |
| 636 1, 1, "http://www.google.com", "http://google.com/image1.png", | 682 1, 1, "http://www.google.com", "http://google.com/image1.png", |
| 637 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); | 683 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); |
| 638 predictor_->RecordURLResponse(resource5); | 684 predictor_->RecordURLResponse(resources.back()); |
| 639 URLRequestSummary resource6 = CreateURLRequestSummary( | 685 resources.push_back(CreateURLRequestSummary( |
| 640 1, 1, "http://www.google.com", "http://google.com/image2.png", | 686 1, 1, "http://www.google.com", "http://google.com/image2.png", |
| 641 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); | 687 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); |
| 642 predictor_->RecordURLResponse(resource6); | 688 predictor_->RecordURLResponse(resources.back()); |
| 643 URLRequestSummary resource7 = CreateURLRequestSummary( | 689 resources.push_back(CreateURLRequestSummary( |
| 644 1, 1, "http://www.google.com", "http://google.com/style2.css", | 690 1, 1, "http://www.google.com", "http://google.com/style2.css", |
| 645 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); | 691 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); |
| 646 predictor_->RecordURLResponse(resource7); | 692 predictor_->RecordURLResponse(resources.back()); |
| 693 | |
| 694 EXPECT_CALL(*mock_observer_.get(), | |
| 695 OnNavigationLearned( | |
| 696 kVisitCount, CreatePageRequestSummary("http://www.google.com", | |
| 697 "http://www.google.com", | |
| 698 resources))); | |
| 647 | 699 |
| 648 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); | 700 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); |
| 649 InitializeResourceData(url_data.add_resources(), | 701 InitializeResourceData(url_data.add_resources(), |
| 650 "http://google.com/style1.css", | 702 "http://google.com/style1.css", |
| 651 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, | 703 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, |
| 652 net::MEDIUM, false, false); | 704 net::MEDIUM, false, false); |
| 653 InitializeResourceData( | 705 InitializeResourceData( |
| 654 url_data.add_resources(), "http://google.com/script1.js", | 706 url_data.add_resources(), "http://google.com/script1.js", |
| 655 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); | 707 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); |
| 656 InitializeResourceData( | 708 InitializeResourceData( |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 678 host_data.add_resources(), "http://google.com/script2.js", | 730 host_data.add_resources(), "http://google.com/script2.js", |
| 679 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); | 731 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false); |
| 680 InitializeResourceData(host_data.add_resources(), | 732 InitializeResourceData(host_data.add_resources(), |
| 681 "http://google.com/style2.css", | 733 "http://google.com/style2.css", |
| 682 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, | 734 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, |
| 683 net::MEDIUM, false, false); | 735 net::MEDIUM, false, false); |
| 684 EXPECT_CALL(*mock_tables_.get(), | 736 EXPECT_CALL(*mock_tables_.get(), |
| 685 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 737 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 686 empty_redirect_data_)); | 738 empty_redirect_data_)); |
| 687 | 739 |
| 688 predictor_->OnNavigationComplete(main_frame.navigation_id); | 740 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 689 profile_->BlockUntilHistoryProcessesPendingRequests(); | 741 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 690 } | 742 } |
| 691 | 743 |
| 692 // Tests that a URL is deleted before another is added if the cache is full. | 744 // Tests that a URL is deleted before another is added if the cache is full. |
| 693 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { | 745 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { |
| 694 AddUrlToHistory("http://www.nike.com/", 4); | 746 const int kVisitCount = 4; |
| 747 AddUrlToHistory("http://www.nike.com/", kVisitCount); | |
| 695 | 748 |
| 696 EXPECT_CALL(*mock_tables_.get(), | 749 EXPECT_CALL(*mock_tables_.get(), |
| 697 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 750 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
| 698 Pointee(ContainerEq(PrefetchDataMap())), | 751 Pointee(ContainerEq(PrefetchDataMap())), |
| 699 Pointee(ContainerEq(RedirectDataMap())), | 752 Pointee(ContainerEq(RedirectDataMap())), |
| 700 Pointee(ContainerEq(RedirectDataMap())))) | 753 Pointee(ContainerEq(RedirectDataMap())))) |
| 701 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), | 754 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), |
| 702 SetArgPointee<1>(test_host_data_))); | 755 SetArgPointee<1>(test_host_data_))); |
| 703 ResetPredictor(); | 756 ResetPredictor(); |
| 704 InitializePredictor(); | 757 InitializePredictor(); |
| 705 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); | 758 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); |
| 706 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); | 759 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); |
| 707 | 760 |
| 708 URLRequestSummary main_frame = CreateURLRequestSummary( | 761 URLRequestSummary main_frame = CreateURLRequestSummary( |
| 709 1, 1, "http://www.nike.com", "http://www.nike.com", | 762 1, 1, "http://www.nike.com", "http://www.nike.com", |
| 710 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); | 763 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); |
| 711 predictor_->RecordURLRequest(main_frame); | 764 predictor_->RecordURLRequest(main_frame); |
| 712 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 765 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 713 | 766 |
| 714 URLRequestSummary resource1 = CreateURLRequestSummary( | 767 URLRequestSummary resource1 = CreateURLRequestSummary( |
| 715 1, 1, "http://www.nike.com", "http://nike.com/style1.css", | 768 1, 1, "http://www.nike.com", "http://nike.com/style1.css", |
| 716 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); | 769 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); |
| 717 predictor_->RecordURLResponse(resource1); | 770 predictor_->RecordURLResponse(resource1); |
| 718 URLRequestSummary resource2 = CreateURLRequestSummary( | 771 URLRequestSummary resource2 = CreateURLRequestSummary( |
| 719 1, 1, "http://www.nike.com", "http://nike.com/image2.png", | 772 1, 1, "http://www.nike.com", "http://nike.com/image2.png", |
| 720 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); | 773 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); |
| 721 predictor_->RecordURLResponse(resource2); | 774 predictor_->RecordURLResponse(resource2); |
| 722 | 775 |
| 776 EXPECT_CALL(*mock_observer_.get(), | |
| 777 OnNavigationLearned( | |
| 778 kVisitCount, CreatePageRequestSummary( | |
| 779 "http://www.nike.com", "http://www.nike.com", | |
| 780 {resource1, resource2}))); | |
| 781 | |
| 723 EXPECT_CALL(*mock_tables_.get(), | 782 EXPECT_CALL(*mock_tables_.get(), |
| 724 DeleteSingleResourceDataPoint("http://www.google.com/", | 783 DeleteSingleResourceDataPoint("http://www.google.com/", |
| 725 PREFETCH_KEY_TYPE_URL)); | 784 PREFETCH_KEY_TYPE_URL)); |
| 726 EXPECT_CALL(*mock_tables_.get(), | 785 EXPECT_CALL(*mock_tables_.get(), |
| 727 DeleteSingleResourceDataPoint("www.facebook.com", | 786 DeleteSingleResourceDataPoint("www.facebook.com", |
| 728 PREFETCH_KEY_TYPE_HOST)); | 787 PREFETCH_KEY_TYPE_HOST)); |
| 729 | 788 |
| 730 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); | 789 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); |
| 731 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css", | 790 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css", |
| 732 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, | 791 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, |
| 733 net::MEDIUM, false, false); | 792 net::MEDIUM, false, false); |
| 734 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png", | 793 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png", |
| 735 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, | 794 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, |
| 736 net::MEDIUM, false, false); | 795 net::MEDIUM, false, false); |
| 737 EXPECT_CALL(*mock_tables_.get(), | 796 EXPECT_CALL(*mock_tables_.get(), |
| 738 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, | 797 UpdateData(url_data, empty_resource_data_, empty_redirect_data_, |
| 739 empty_redirect_data_)); | 798 empty_redirect_data_)); |
| 740 | 799 |
| 741 PrefetchData host_data = CreatePrefetchData("www.nike.com"); | 800 PrefetchData host_data = CreatePrefetchData("www.nike.com"); |
| 742 host_data.mutable_resources()->CopyFrom(url_data.resources()); | 801 host_data.mutable_resources()->CopyFrom(url_data.resources()); |
| 743 EXPECT_CALL(*mock_tables_.get(), | 802 EXPECT_CALL(*mock_tables_.get(), |
| 744 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, | 803 UpdateData(empty_resource_data_, host_data, empty_redirect_data_, |
| 745 empty_redirect_data_)); | 804 empty_redirect_data_)); |
| 746 | 805 |
| 747 predictor_->OnNavigationComplete(main_frame.navigation_id); | 806 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); |
| 748 profile_->BlockUntilHistoryProcessesPendingRequests(); | 807 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 749 } | 808 } |
| 750 | 809 |
| 751 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { | 810 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { |
| 752 AddUrlToHistory("https://facebook.com/google", 4); | 811 const int kVisitCount = 4; |
| 812 AddUrlToHistory("https://facebook.com/google", kVisitCount); | |
| 753 | 813 |
| 754 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google"); | 814 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google"); |
| 755 predictor_->RecordURLRequest(fb1); | 815 predictor_->RecordURLRequest(fb1); |
| 756 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 816 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 757 | 817 |
| 758 URLRequestSummary fb2 = CreateRedirectRequestSummary( | 818 URLRequestSummary fb2 = CreateRedirectRequestSummary( |
| 759 1, 1, "http://fb.com/google", "http://facebook.com/google"); | 819 1, 1, "http://fb.com/google", "http://facebook.com/google"); |
| 760 predictor_->RecordURLRedirect(fb2); | 820 predictor_->RecordURLRedirect(fb2); |
| 761 URLRequestSummary fb3 = CreateRedirectRequestSummary( | 821 URLRequestSummary fb3 = CreateRedirectRequestSummary( |
| 762 1, 1, "http://facebook.com/google", "https://facebook.com/google"); | 822 1, 1, "http://facebook.com/google", "https://facebook.com/google"); |
| 763 predictor_->RecordURLRedirect(fb3); | 823 predictor_->RecordURLRedirect(fb3); |
| 764 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google"); | 824 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google"); |
| 765 | 825 |
| 826 EXPECT_CALL( | |
| 827 *mock_observer_.get(), | |
| 828 OnNavigationLearned(kVisitCount, CreatePageRequestSummary( | |
| 829 "https://facebook.com/google", | |
| 830 "http://fb.com/google", | |
| 831 std::vector<URLRequestSummary>()))); | |
| 832 | |
| 766 // Since the navigation hasn't resources, corresponding entry | 833 // Since the navigation hasn't resources, corresponding entry |
| 767 // in resource table will be deleted. | 834 // in resource table will be deleted. |
| 768 EXPECT_CALL(*mock_tables_.get(), | 835 EXPECT_CALL(*mock_tables_.get(), |
| 769 DeleteSingleResourceDataPoint("https://facebook.com/google", | 836 DeleteSingleResourceDataPoint("https://facebook.com/google", |
| 770 PREFETCH_KEY_TYPE_URL)); | 837 PREFETCH_KEY_TYPE_URL)); |
| 771 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint( | 838 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint( |
| 772 "facebook.com", PREFETCH_KEY_TYPE_HOST)); | 839 "facebook.com", PREFETCH_KEY_TYPE_HOST)); |
| 773 | 840 |
| 774 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); | 841 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); |
| 775 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), | 842 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), |
| 776 "https://facebook.com/google", 1, 0, 0); | 843 "https://facebook.com/google", 1, 0, 0); |
| 777 EXPECT_CALL(*mock_tables_.get(), | 844 EXPECT_CALL(*mock_tables_.get(), |
| 778 UpdateData(empty_resource_data_, empty_resource_data_, | 845 UpdateData(empty_resource_data_, empty_resource_data_, |
| 779 url_redirect_data, empty_redirect_data_)); | 846 url_redirect_data, empty_redirect_data_)); |
| 780 | 847 |
| 781 RedirectData host_redirect_data = CreateRedirectData("fb.com"); | 848 RedirectData host_redirect_data = CreateRedirectData("fb.com"); |
| 782 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), | 849 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), |
| 783 "facebook.com", 1, 0, 0); | 850 "facebook.com", 1, 0, 0); |
| 784 EXPECT_CALL(*mock_tables_.get(), | 851 EXPECT_CALL(*mock_tables_.get(), |
| 785 UpdateData(empty_resource_data_, empty_resource_data_, | 852 UpdateData(empty_resource_data_, empty_resource_data_, |
| 786 empty_redirect_data_, host_redirect_data)); | 853 empty_redirect_data_, host_redirect_data)); |
| 787 | 854 |
| 788 predictor_->RecordMainFrameLoadComplete(fb_end); | 855 predictor_->RecordMainFrameLoadComplete(fb_end); |
| 789 profile_->BlockUntilHistoryProcessesPendingRequests(); | 856 profile_->BlockUntilHistoryProcessesPendingRequests(); |
| 790 } | 857 } |
| 791 | 858 |
| 792 // Tests that redirect is recorded correctly for URL already present in | 859 // Tests that redirect is recorded correctly for URL already present in |
| 793 // the database cache. | 860 // the database cache. |
| 794 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { | 861 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { |
| 795 AddUrlToHistory("https://facebook.com/google", 4); | 862 const int kVisitCount = 7; |
| 863 AddUrlToHistory("https://facebook.com/google", kVisitCount); | |
| 796 | 864 |
| 797 EXPECT_CALL(*mock_tables_.get(), | 865 EXPECT_CALL(*mock_tables_.get(), |
| 798 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 866 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
| 799 Pointee(ContainerEq(PrefetchDataMap())), | 867 Pointee(ContainerEq(PrefetchDataMap())), |
| 800 Pointee(ContainerEq(RedirectDataMap())), | 868 Pointee(ContainerEq(RedirectDataMap())), |
| 801 Pointee(ContainerEq(RedirectDataMap())))) | 869 Pointee(ContainerEq(RedirectDataMap())))) |
| 802 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_), | 870 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_), |
| 803 SetArgPointee<3>(test_host_redirect_data_))); | 871 SetArgPointee<3>(test_host_redirect_data_))); |
| 804 ResetPredictor(); | 872 ResetPredictor(); |
| 805 InitializePredictor(); | 873 InitializePredictor(); |
| 806 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size()); | 874 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size()); |
| 807 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size()); | 875 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size()); |
| 808 | 876 |
| 809 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google"); | 877 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google"); |
| 810 predictor_->RecordURLRequest(fb1); | 878 predictor_->RecordURLRequest(fb1); |
| 811 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 879 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
| 812 | 880 |
| 813 URLRequestSummary fb2 = CreateRedirectRequestSummary( | 881 URLRequestSummary fb2 = CreateRedirectRequestSummary( |
| 814 1, 1, "http://fb.com/google", "http://facebook.com/google"); | 882 1, 1, "http://fb.com/google", "http://facebook.com/google"); |
| 815 predictor_->RecordURLRedirect(fb2); | 883 predictor_->RecordURLRedirect(fb2); |
| 816 URLRequestSummary fb3 = CreateRedirectRequestSummary( | 884 URLRequestSummary fb3 = CreateRedirectRequestSummary( |
| 817 1, 1, "http://facebook.com/google", "https://facebook.com/google"); | 885 1, 1, "http://facebook.com/google", "https://facebook.com/google"); |
| 818 predictor_->RecordURLRedirect(fb3); | 886 predictor_->RecordURLRedirect(fb3); |
| 819 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google"); | 887 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google"); |
| 820 | 888 |
| 889 EXPECT_CALL( | |
| 890 *mock_observer_.get(), | |
| 891 OnNavigationLearned(kVisitCount, CreatePageRequestSummary( | |
| 892 "https://facebook.com/google", | |
| 893 "http://fb.com/google", | |
| 894 std::vector<URLRequestSummary>()))); | |
| 895 | |
| 821 // Oldest entries in tables will be superseded and deleted. | 896 // Oldest entries in tables will be superseded and deleted. |
| 822 EXPECT_CALL(*mock_tables_.get(), | 897 EXPECT_CALL(*mock_tables_.get(), |
| 823 DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST)); | 898 DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST)); |
| 824 | 899 |
| 825 // Since the navigation hasn't resources, corresponding entry | 900 // Since the navigation hasn't resources, corresponding entry |
| 826 // in resource table will be deleted. | 901 // in resource table will be deleted. |
| 827 EXPECT_CALL(*mock_tables_.get(), | 902 EXPECT_CALL(*mock_tables_.get(), |
| 828 DeleteSingleResourceDataPoint("https://facebook.com/google", | 903 DeleteSingleResourceDataPoint("https://facebook.com/google", |
| 829 PREFETCH_KEY_TYPE_URL)); | 904 PREFETCH_KEY_TYPE_URL)); |
| 830 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint( | 905 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint( |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1491 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, | 1566 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, |
| 1492 net::MEDIUM, false, false); | 1567 net::MEDIUM, false, false); |
| 1493 predictor_->url_table_cache_->insert( | 1568 predictor_->url_table_cache_->insert( |
| 1494 std::make_pair(www_google_url.primary_key(), www_google_url)); | 1569 std::make_pair(www_google_url.primary_key(), www_google_url)); |
| 1495 | 1570 |
| 1496 urls.clear(); | 1571 urls.clear(); |
| 1497 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); | 1572 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &urls)); |
| 1498 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); | 1573 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); |
| 1499 } | 1574 } |
| 1500 | 1575 |
| 1576 TEST_F(ResourcePrefetchPredictorTest, SetObserverForTesting) { | |
| 1577 std::unique_ptr<ResourcePrefetchPredictor> predictor = | |
| 1578 base::MakeUnique<ResourcePrefetchPredictor>(predictor_->config_, | |
| 1579 predictor_->profile_); | |
| 1580 | |
| 1581 // Test that observer is created correctly. | |
| 1582 MockResourcePrefetchPredictorObserver observer1(predictor.get()); | |
| 1583 EXPECT_EQ(observer1.predictor_, predictor.get()); | |
| 1584 EXPECT_EQ(predictor->observer_, &observer1); | |
| 1585 | |
| 1586 // Test that observer is superseded correctly. | |
| 1587 MockResourcePrefetchPredictorObserver observer2(predictor.get()); | |
| 1588 EXPECT_EQ(observer2.predictor_, predictor.get()); | |
| 1589 EXPECT_EQ(predictor->observer_, &observer2); | |
| 1590 EXPECT_EQ(observer1.predictor_, nullptr); | |
| 1591 | |
| 1592 // Test that observer imforms predictor about its death. | |
|
pasko
2016/10/24 17:59:38
nit: s/imforms/informs/
alexilin
2016/10/25 11:38:09
Done.
| |
| 1593 { | |
| 1594 MockResourcePrefetchPredictorObserver observer3(predictor.get()); | |
| 1595 EXPECT_EQ(observer3.predictor_, predictor.get()); | |
| 1596 EXPECT_EQ(predictor->observer_, &observer3); | |
| 1597 } | |
| 1598 EXPECT_EQ(predictor->observer_, nullptr); | |
| 1599 | |
| 1600 // Test that predictor informs observer about its death. | |
| 1601 MockResourcePrefetchPredictorObserver observer4(predictor.get()); | |
| 1602 EXPECT_EQ(observer4.predictor_, predictor.get()); | |
| 1603 EXPECT_EQ(predictor->observer_, &observer4); | |
| 1604 predictor.reset(); | |
| 1605 EXPECT_EQ(observer4.predictor_, nullptr); | |
| 1606 } | |
| 1607 | |
| 1501 } // namespace predictors | 1608 } // namespace predictors |
| OLD | NEW |