Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_unittest.cc

Issue 2937623007: predictors: Move more methods from ResourcePrefetchPredictor into LoadingDataCollector. (Closed)
Patch Set: Fix browser test Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_predictor_tab_helper.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16 matching lines...) Expand all
27 #include "net/url_request/url_request_context.h" 27 #include "net/url_request/url_request_context.h"
28 #include "net/url_request/url_request_job.h" 28 #include "net/url_request/url_request_job.h"
29 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
31 31
32 using testing::StrictMock; 32 using testing::StrictMock;
33 using testing::UnorderedElementsAre; 33 using testing::UnorderedElementsAre;
34 34
35 namespace predictors { 35 namespace predictors {
36 36
37 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
38 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
39 using PrefetchDataMap = std::map<std::string, PrefetchData>; 37 using PrefetchDataMap = std::map<std::string, PrefetchData>;
40 using RedirectDataMap = std::map<std::string, RedirectData>; 38 using RedirectDataMap = std::map<std::string, RedirectData>;
41 using OriginDataMap = std::map<std::string, OriginData>; 39 using OriginDataMap = std::map<std::string, OriginData>;
42 40
43 template <typename T> 41 template <typename T>
44 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { 42 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> {
45 public: 43 public:
46 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} 44 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {}
47 void GetAllData(std::map<std::string, T>* data_map, 45 void GetAllData(std::map<std::string, T>* data_map,
48 sql::Connection* db) const override { 46 sql::Connection* db) const override {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 protected: 104 protected:
107 ~MockResourcePrefetchPredictorTables() override = default; 105 ~MockResourcePrefetchPredictorTables() override = default;
108 }; 106 };
109 107
110 class MockResourcePrefetchPredictorObserver : public TestObserver { 108 class MockResourcePrefetchPredictorObserver : public TestObserver {
111 public: 109 public:
112 explicit MockResourcePrefetchPredictorObserver( 110 explicit MockResourcePrefetchPredictorObserver(
113 ResourcePrefetchPredictor* predictor) 111 ResourcePrefetchPredictor* predictor)
114 : TestObserver(predictor) {} 112 : TestObserver(predictor) {}
115 113
116 MOCK_METHOD2( 114 MOCK_METHOD2(OnNavigationLearned,
117 OnNavigationLearned, 115 void(size_t url_visit_count, const PageRequestSummary& summary));
118 void(size_t url_visit_count,
119 const ResourcePrefetchPredictor::PageRequestSummary& summary));
120 }; 116 };
121 117
122 class ResourcePrefetchPredictorTest : public testing::Test { 118 class ResourcePrefetchPredictorTest : public testing::Test {
123 public: 119 public:
124 ResourcePrefetchPredictorTest(); 120 ResourcePrefetchPredictorTest();
125 ~ResourcePrefetchPredictorTest() override; 121 ~ResourcePrefetchPredictorTest() override;
126 void SetUp() override; 122 void SetUp() override;
127 void TearDown() override; 123 void TearDown() override;
128 124
129 protected: 125 protected:
130 void AddUrlToHistory(const std::string& url, int visit_count) { 126 void AddUrlToHistory(const std::string& url, int visit_count) {
131 HistoryServiceFactory::GetForProfile(profile_.get(), 127 HistoryServiceFactory::GetForProfile(profile_.get(),
132 ServiceAccessType::EXPLICIT_ACCESS)-> 128 ServiceAccessType::EXPLICIT_ACCESS)->
133 AddPageWithDetails( 129 AddPageWithDetails(
134 GURL(url), 130 GURL(url),
135 base::string16(), 131 base::string16(),
136 visit_count, 132 visit_count,
137 0, 133 0,
138 base::Time::Now(), 134 base::Time::Now(),
139 false, 135 false,
140 history::SOURCE_BROWSED); 136 history::SOURCE_BROWSED);
141 profile_->BlockUntilHistoryProcessesPendingRequests(); 137 profile_->BlockUntilHistoryProcessesPendingRequests();
142 } 138 }
143 139
144 URLRequestSummary CreateRedirectRequestSummary(
145 SessionID::id_type session_id,
146 const std::string& main_frame_url,
147 const std::string& redirect_url) {
148 URLRequestSummary summary =
149 CreateURLRequestSummary(session_id, main_frame_url);
150 summary.redirect_url = GURL(redirect_url);
151 return summary;
152 }
153
154 void InitializePredictor() { 140 void InitializePredictor() {
155 loading_predictor_->StartInitialization(); 141 loading_predictor_->StartInitialization();
156 base::RunLoop loop; 142 base::RunLoop loop;
157 loop.RunUntilIdle(); // Runs the DB lookup. 143 loop.RunUntilIdle(); // Runs the DB lookup.
158 profile_->BlockUntilHistoryProcessesPendingRequests(); 144 profile_->BlockUntilHistoryProcessesPendingRequests();
159 } 145 }
160 146
161 void ResetPredictor(bool small_db = true) { 147 void ResetPredictor(bool small_db = true) {
162 LoadingPredictorConfig config; 148 LoadingPredictorConfig config;
163 PopulateTestConfig(&config, small_db); 149 PopulateTestConfig(&config, small_db);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 188
203 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); 189 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
204 profile_->BlockUntilHistoryProcessesPendingRequests(); 190 profile_->BlockUntilHistoryProcessesPendingRequests();
205 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( 191 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
206 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); 192 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS));
207 // Initialize the predictor with empty data. 193 // Initialize the predictor with empty data.
208 ResetPredictor(); 194 ResetPredictor();
209 EXPECT_EQ(predictor_->initialization_state_, 195 EXPECT_EQ(predictor_->initialization_state_,
210 ResourcePrefetchPredictor::NOT_INITIALIZED); 196 ResourcePrefetchPredictor::NOT_INITIALIZED);
211 InitializePredictor(); 197 InitializePredictor();
212 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
213 EXPECT_EQ(predictor_->initialization_state_, 198 EXPECT_EQ(predictor_->initialization_state_,
214 ResourcePrefetchPredictor::INITIALIZED); 199 ResourcePrefetchPredictor::INITIALIZED);
215 200
216 url_request_job_factory_.Reset(); 201 url_request_job_factory_.Reset();
217 url_request_context_.set_job_factory(&url_request_job_factory_); 202 url_request_context_.set_job_factory(&url_request_job_factory_);
218 203
219 histogram_tester_.reset(new base::HistogramTester()); 204 histogram_tester_.reset(new base::HistogramTester());
220 } 205 }
221 206
222 void ResourcePrefetchPredictorTest::TearDown() { 207 void ResourcePrefetchPredictorTest::TearDown() {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 mock_tables_->host_resource_table_.data_ = test_host_data_; 375 mock_tables_->host_resource_table_.data_ = test_host_data_;
391 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_; 376 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_;
392 mock_tables_->origin_table_.data_ = test_origin_data_; 377 mock_tables_->origin_table_.data_ = test_origin_data_;
393 378
394 ResetPredictor(); 379 ResetPredictor();
395 InitializePredictor(); 380 InitializePredictor();
396 381
397 // Test that the internal variables correctly initialized. 382 // Test that the internal variables correctly initialized.
398 EXPECT_EQ(predictor_->initialization_state_, 383 EXPECT_EQ(predictor_->initialization_state_,
399 ResourcePrefetchPredictor::INITIALIZED); 384 ResourcePrefetchPredictor::INITIALIZED);
400 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
401 385
402 // Integrity of the cache and the backend storage is checked on TearDown. 386 // Integrity of the cache and the backend storage is checked on TearDown.
403 } 387 }
404 388
405 // Single navigation but history count is low, so should not record url data. 389 // Single navigation but history count is low, so should not record url data.
406 TEST_F(ResourcePrefetchPredictorTest, NavigationLowHistoryCount) { 390 TEST_F(ResourcePrefetchPredictorTest, NavigationLowHistoryCount) {
407 const int kVisitCount = 1; 391 const int kVisitCount = 1;
408 AddUrlToHistory("https://www.google.com", kVisitCount); 392 AddUrlToHistory("https://www.google.com", kVisitCount);
409 393
410 URLRequestSummary main_frame = 394 URLRequestSummary main_frame =
411 CreateURLRequestSummary(1, "http://www.google.com"); 395 CreateURLRequestSummary(1, "http://www.google.com");
412 predictor_->RecordURLRequest(main_frame);
413 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
414 396
415 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary( 397 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary(
416 1, "http://www.google.com", "https://www.google.com"); 398 1, "http://www.google.com", "https://www.google.com");
417 predictor_->RecordURLRedirect(main_frame_redirect);
418 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
419 main_frame = CreateURLRequestSummary(1, "https://www.google.com"); 399 main_frame = CreateURLRequestSummary(1, "https://www.google.com");
420 400
421 // Now add a few subresources. 401 // Now add a few subresources.
422 URLRequestSummary resource1 = CreateURLRequestSummary( 402 URLRequestSummary resource1 = CreateURLRequestSummary(
423 1, "https://www.google.com", "https://google.com/style1.css", 403 1, "https://www.google.com", "https://google.com/style1.css",
424 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 404 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
425 predictor_->RecordURLResponse(resource1);
426 URLRequestSummary resource2 = CreateURLRequestSummary( 405 URLRequestSummary resource2 = CreateURLRequestSummary(
427 1, "https://www.google.com", "https://google.com/script1.js", 406 1, "https://www.google.com", "https://google.com/script1.js",
428 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 407 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
429 predictor_->RecordURLResponse(resource2);
430 URLRequestSummary resource3 = CreateURLRequestSummary( 408 URLRequestSummary resource3 = CreateURLRequestSummary(
431 1, "https://www.google.com", "https://google.com/script2.js", 409 1, "https://www.google.com", "https://google.com/script2.js",
432 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 410 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
433 predictor_->RecordURLResponse(resource3); 411
412 auto page_summary = CreatePageRequestSummary(
413 "https://www.google.com", "http://www.google.com",
414 {resource1, resource2, resource3});
434 415
435 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 416 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
436 EXPECT_CALL( 417 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
437 mock_observer,
438 OnNavigationLearned(kVisitCount,
439 CreatePageRequestSummary(
440 "https://www.google.com", "http://www.google.com",
441 {resource1, resource2, resource3})));
442 418
443 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 419 predictor_->RecordPageRequestSummary(
420 base::MakeUnique<PageRequestSummary>(page_summary));
444 profile_->BlockUntilHistoryProcessesPendingRequests(); 421 profile_->BlockUntilHistoryProcessesPendingRequests();
445 422
446 PrefetchData host_data = CreatePrefetchData("www.google.com"); 423 PrefetchData host_data = CreatePrefetchData("www.google.com");
447 InitializeResourceData(host_data.add_resources(), 424 InitializeResourceData(host_data.add_resources(),
448 "https://google.com/style1.css", 425 "https://google.com/style1.css",
449 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 426 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
450 net::MEDIUM, false, false); 427 net::MEDIUM, false, false);
451 InitializeResourceData( 428 InitializeResourceData(
452 host_data.add_resources(), "https://google.com/script1.js", 429 host_data.add_resources(), "https://google.com/script1.js",
453 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); 430 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false);
(...skipping 18 matching lines...) Expand all
472 } 449 }
473 450
474 // Single navigation that will be recorded. Will check for duplicate 451 // Single navigation that will be recorded. Will check for duplicate
475 // resources and also for number of resources saved. 452 // resources and also for number of resources saved.
476 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 453 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
477 const int kVisitCount = 4; 454 const int kVisitCount = 4;
478 AddUrlToHistory("http://www.google.com", kVisitCount); 455 AddUrlToHistory("http://www.google.com", kVisitCount);
479 456
480 URLRequestSummary main_frame = 457 URLRequestSummary main_frame =
481 CreateURLRequestSummary(1, "http://www.google.com"); 458 CreateURLRequestSummary(1, "http://www.google.com");
482 predictor_->RecordURLRequest(main_frame);
483 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
484 459
485 std::vector<URLRequestSummary> resources; 460 std::vector<URLRequestSummary> resources;
486 resources.push_back(CreateURLRequestSummary( 461 resources.push_back(CreateURLRequestSummary(
487 1, "http://www.google.com", "http://google.com/style1.css", 462 1, "http://www.google.com", "http://google.com/style1.css",
488 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); 463 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false));
489 predictor_->RecordURLResponse(resources.back());
490 resources.push_back(CreateURLRequestSummary( 464 resources.push_back(CreateURLRequestSummary(
491 1, "http://www.google.com", "http://google.com/script1.js", 465 1, "http://www.google.com", "http://google.com/script1.js",
492 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 466 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
493 predictor_->RecordURLResponse(resources.back());
494 resources.push_back(CreateURLRequestSummary( 467 resources.push_back(CreateURLRequestSummary(
495 1, "http://www.google.com", "http://google.com/script2.js", 468 1, "http://www.google.com", "http://google.com/script2.js",
496 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 469 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
497 predictor_->RecordURLResponse(resources.back());
498 resources.push_back(CreateURLRequestSummary( 470 resources.push_back(CreateURLRequestSummary(
499 1, "http://www.google.com", "http://google.com/script1.js", 471 1, "http://www.google.com", "http://google.com/script1.js",
500 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); 472 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true));
501 predictor_->RecordURLResponse(resources.back());
502 resources.push_back(CreateURLRequestSummary( 473 resources.push_back(CreateURLRequestSummary(
503 1, "http://www.google.com", "http://google.com/image1.png", 474 1, "http://www.google.com", "http://google.com/image1.png",
504 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 475 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
505 predictor_->RecordURLResponse(resources.back());
506 resources.push_back(CreateURLRequestSummary( 476 resources.push_back(CreateURLRequestSummary(
507 1, "http://www.google.com", "http://google.com/image2.png", 477 1, "http://www.google.com", "http://google.com/image2.png",
508 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 478 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
509 predictor_->RecordURLResponse(resources.back());
510 resources.push_back(CreateURLRequestSummary( 479 resources.push_back(CreateURLRequestSummary(
511 1, "http://www.google.com", "http://google.com/style2.css", 480 1, "http://www.google.com", "http://google.com/style2.css",
512 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 481 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
513 predictor_->RecordURLResponse(resources.back());
514 482
515 auto no_store = CreateURLRequestSummary( 483 auto no_store = CreateURLRequestSummary(
516 1, "http://www.google.com", 484 1, "http://www.google.com",
517 "http://static.google.com/style2-no-store.css", 485 "http://static.google.com/style2-no-store.css",
518 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 486 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
519 no_store.is_no_store = true; 487 no_store.is_no_store = true;
520 predictor_->RecordURLResponse(no_store);
521 488
522 auto redirected = CreateURLRequestSummary( 489 auto redirected = CreateURLRequestSummary(
523 1, "http://www.google.com", "http://reader.google.com/style.css", 490 1, "http://www.google.com", "http://reader.google.com/style.css",
524 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 491 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
525 redirected.redirect_url = GURL("http://dev.null.google.com/style.css"); 492 redirected.redirect_url = GURL("http://dev.null.google.com/style.css");
526 493
527 predictor_->RecordURLRedirect(redirected); 494 auto page_summary = CreatePageRequestSummary(
495 "http://www.google.com", "http://www.google.com", resources);
496 page_summary.UpdateOrAddToOrigins(no_store);
497 page_summary.UpdateOrAddToOrigins(redirected);
498
528 redirected.is_no_store = true; 499 redirected.is_no_store = true;
529 redirected.request_url = redirected.redirect_url; 500 redirected.request_url = redirected.redirect_url;
530 redirected.redirect_url = GURL(); 501 redirected.redirect_url = GURL();
531 502 page_summary.UpdateOrAddToOrigins(redirected);
532 predictor_->RecordURLResponse(redirected);
533 503
534 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 504 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
535 EXPECT_CALL(mock_observer, 505 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
536 OnNavigationLearned(
537 kVisitCount, CreatePageRequestSummary("http://www.google.com",
538 "http://www.google.com",
539 resources)));
540 506
541 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 507 predictor_->RecordPageRequestSummary(
508 base::MakeUnique<PageRequestSummary>(page_summary));
542 profile_->BlockUntilHistoryProcessesPendingRequests(); 509 profile_->BlockUntilHistoryProcessesPendingRequests();
543 510
544 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 511 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
545 InitializeResourceData(url_data.add_resources(), 512 InitializeResourceData(url_data.add_resources(),
546 "http://google.com/style1.css", 513 "http://google.com/style1.css",
547 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 514 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
548 net::MEDIUM, false, false); 515 net::MEDIUM, false, false);
549 InitializeResourceData(url_data.add_resources(), 516 InitializeResourceData(url_data.add_resources(),
550 "http://google.com/style2.css", 517 "http://google.com/style2.css",
551 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, 518 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 566
600 mock_tables_->url_resource_table_.data_ = test_url_data_; 567 mock_tables_->url_resource_table_.data_ = test_url_data_;
601 mock_tables_->host_resource_table_.data_ = test_host_data_; 568 mock_tables_->host_resource_table_.data_ = test_host_data_;
602 569
603 ResetPredictor(); 570 ResetPredictor();
604 InitializePredictor(); 571 InitializePredictor();
605 572
606 URLRequestSummary main_frame = CreateURLRequestSummary( 573 URLRequestSummary main_frame = CreateURLRequestSummary(
607 1, "http://www.google.com", "http://www.google.com", 574 1, "http://www.google.com", "http://www.google.com",
608 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 575 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
609 predictor_->RecordURLRequest(main_frame);
610 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
611 576
612 std::vector<URLRequestSummary> resources; 577 std::vector<URLRequestSummary> resources;
613 resources.push_back(CreateURLRequestSummary( 578 resources.push_back(CreateURLRequestSummary(
614 1, "http://www.google.com", "http://google.com/style1.css", 579 1, "http://www.google.com", "http://google.com/style1.css",
615 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); 580 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false));
616 predictor_->RecordURLResponse(resources.back());
617 resources.push_back(CreateURLRequestSummary( 581 resources.push_back(CreateURLRequestSummary(
618 1, "http://www.google.com", "http://google.com/script1.js", 582 1, "http://www.google.com", "http://google.com/script1.js",
619 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 583 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
620 predictor_->RecordURLResponse(resources.back());
621 resources.push_back(CreateURLRequestSummary( 584 resources.push_back(CreateURLRequestSummary(
622 1, "http://www.google.com", "http://google.com/script2.js", 585 1, "http://www.google.com", "http://google.com/script2.js",
623 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 586 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
624 predictor_->RecordURLResponse(resources.back());
625 resources.push_back(CreateURLRequestSummary( 587 resources.push_back(CreateURLRequestSummary(
626 1, "http://www.google.com", "http://google.com/script1.js", 588 1, "http://www.google.com", "http://google.com/script1.js",
627 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); 589 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true));
628 predictor_->RecordURLResponse(resources.back());
629 resources.push_back(CreateURLRequestSummary( 590 resources.push_back(CreateURLRequestSummary(
630 1, "http://www.google.com", "http://google.com/image1.png", 591 1, "http://www.google.com", "http://google.com/image1.png",
631 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 592 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
632 predictor_->RecordURLResponse(resources.back());
633 resources.push_back(CreateURLRequestSummary( 593 resources.push_back(CreateURLRequestSummary(
634 1, "http://www.google.com", "http://google.com/image2.png", 594 1, "http://www.google.com", "http://google.com/image2.png",
635 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 595 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
636 predictor_->RecordURLResponse(resources.back());
637 resources.push_back(CreateURLRequestSummary( 596 resources.push_back(CreateURLRequestSummary(
638 1, "http://www.google.com", "http://google.com/style2.css", 597 1, "http://www.google.com", "http://google.com/style2.css",
639 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 598 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
640 predictor_->RecordURLResponse(resources.back());
641 auto no_store = CreateURLRequestSummary( 599 auto no_store = CreateURLRequestSummary(
642 1, "http://www.google.com", 600 1, "http://www.google.com",
643 "http://static.google.com/style2-no-store.css", 601 "http://static.google.com/style2-no-store.css",
644 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 602 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
645 no_store.is_no_store = true; 603 no_store.is_no_store = true;
646 predictor_->RecordURLResponse(no_store); 604
605 auto page_summary = CreatePageRequestSummary(
606 "http://www.google.com", "http://www.google.com", resources);
607 page_summary.UpdateOrAddToOrigins(no_store);
647 608
648 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 609 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
649 EXPECT_CALL(mock_observer, 610 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
650 OnNavigationLearned(
651 kVisitCount, CreatePageRequestSummary("http://www.google.com",
652 "http://www.google.com",
653 resources)));
654 611
655 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 612 predictor_->RecordPageRequestSummary(
613 base::MakeUnique<PageRequestSummary>(page_summary));
656 profile_->BlockUntilHistoryProcessesPendingRequests(); 614 profile_->BlockUntilHistoryProcessesPendingRequests();
657 615
658 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 616 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
659 InitializeResourceData(url_data.add_resources(), 617 InitializeResourceData(url_data.add_resources(),
660 "http://google.com/style1.css", 618 "http://google.com/style1.css",
661 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, 619 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0,
662 net::MEDIUM, false, false); 620 net::MEDIUM, false, false);
663 InitializeResourceData(url_data.add_resources(), 621 InitializeResourceData(url_data.add_resources(),
664 "http://google.com/style2.css", 622 "http://google.com/style2.css",
665 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, 623 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
728 mock_tables_->url_resource_table_.data_ = test_url_data_; 686 mock_tables_->url_resource_table_.data_ = test_url_data_;
729 mock_tables_->host_resource_table_.data_ = test_host_data_; 687 mock_tables_->host_resource_table_.data_ = test_host_data_;
730 mock_tables_->origin_table_.data_ = test_origin_data_; 688 mock_tables_->origin_table_.data_ = test_origin_data_;
731 689
732 ResetPredictor(); 690 ResetPredictor();
733 InitializePredictor(); 691 InitializePredictor();
734 692
735 URLRequestSummary main_frame = CreateURLRequestSummary( 693 URLRequestSummary main_frame = CreateURLRequestSummary(
736 1, "http://www.nike.com", "http://www.nike.com", 694 1, "http://www.nike.com", "http://www.nike.com",
737 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 695 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
738 predictor_->RecordURLRequest(main_frame);
739 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
740 696
741 URLRequestSummary resource1 = CreateURLRequestSummary( 697 URLRequestSummary resource1 = CreateURLRequestSummary(
742 1, "http://www.nike.com", "http://nike.com/style1.css", 698 1, "http://www.nike.com", "http://nike.com/style1.css",
743 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 699 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
744 predictor_->RecordURLResponse(resource1);
745 URLRequestSummary resource2 = CreateURLRequestSummary( 700 URLRequestSummary resource2 = CreateURLRequestSummary(
746 1, "http://www.nike.com", "http://nike.com/image2.png", 701 1, "http://www.nike.com", "http://nike.com/image2.png",
747 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 702 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
748 predictor_->RecordURLResponse(resource2); 703
704 auto page_summary = CreatePageRequestSummary(
705 "http://www.nike.com", "http://www.nike.com", {resource1, resource2});
749 706
750 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 707 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
751 EXPECT_CALL(mock_observer, 708 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
752 OnNavigationLearned(
753 kVisitCount, CreatePageRequestSummary(
754 "http://www.nike.com", "http://www.nike.com",
755 {resource1, resource2})));
756 709
757 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 710 predictor_->RecordPageRequestSummary(
711 base::MakeUnique<PageRequestSummary>(page_summary));
758 profile_->BlockUntilHistoryProcessesPendingRequests(); 712 profile_->BlockUntilHistoryProcessesPendingRequests();
759 713
760 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); 714 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/");
761 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css", 715 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css",
762 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 716 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
763 net::MEDIUM, false, false); 717 net::MEDIUM, false, false);
764 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png", 718 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png",
765 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, 719 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0,
766 net::MEDIUM, false, false); 720 net::MEDIUM, false, false);
767 PrefetchDataMap expected_url_resource_data = test_url_data_; 721 PrefetchDataMap expected_url_resource_data = test_url_data_;
(...skipping 30 matching lines...) Expand all
798 OriginDataMap expected_origin_data = test_origin_data_; 752 OriginDataMap expected_origin_data = test_origin_data_;
799 expected_origin_data.erase("google.com"); 753 expected_origin_data.erase("google.com");
800 expected_origin_data["www.nike.com"] = origin_data; 754 expected_origin_data["www.nike.com"] = origin_data;
801 EXPECT_EQ(mock_tables_->origin_table_.data_, expected_origin_data); 755 EXPECT_EQ(mock_tables_->origin_table_.data_, expected_origin_data);
802 } 756 }
803 757
804 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { 758 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) {
805 const int kVisitCount = 4; 759 const int kVisitCount = 4;
806 AddUrlToHistory("https://facebook.com/google", kVisitCount); 760 AddUrlToHistory("https://facebook.com/google", kVisitCount);
807 761
808 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); 762 auto page_summary = CreatePageRequestSummary(
809 predictor_->RecordURLRequest(fb1); 763 "https://facebook.com/google", "http://fb.com/google",
810 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 764 std::vector<URLRequestSummary>());
811
812 URLRequestSummary fb2 = CreateRedirectRequestSummary(
813 1, "http://fb.com/google", "http://facebook.com/google");
814 predictor_->RecordURLRedirect(fb2);
815 URLRequestSummary fb3 = CreateRedirectRequestSummary(
816 1, "http://facebook.com/google", "https://facebook.com/google");
817 predictor_->RecordURLRedirect(fb3);
818 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
819 765
820 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 766 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
821 EXPECT_CALL( 767 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
822 mock_observer,
823 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
824 "https://facebook.com/google",
825 "http://fb.com/google",
826 std::vector<URLRequestSummary>())));
827 768
828 predictor_->RecordMainFrameLoadComplete(fb_end); 769 predictor_->RecordPageRequestSummary(
770 base::MakeUnique<PageRequestSummary>(page_summary));
829 profile_->BlockUntilHistoryProcessesPendingRequests(); 771 profile_->BlockUntilHistoryProcessesPendingRequests();
830 772
831 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); 773 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google");
832 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), 774 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
833 "https://facebook.com/google", 1, 0, 0); 775 "https://facebook.com/google", 1, 0, 0);
834 EXPECT_EQ( 776 EXPECT_EQ(
835 mock_tables_->url_redirect_table_.data_, 777 mock_tables_->url_redirect_table_.data_,
836 RedirectDataMap({{url_redirect_data.primary_key(), url_redirect_data}})); 778 RedirectDataMap({{url_redirect_data.primary_key(), url_redirect_data}}));
837 779
838 RedirectData host_redirect_data = CreateRedirectData("fb.com"); 780 RedirectData host_redirect_data = CreateRedirectData("fb.com");
839 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), 781 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(),
840 "facebook.com", 1, 0, 0); 782 "facebook.com", 1, 0, 0);
841 EXPECT_EQ(mock_tables_->host_redirect_table_.data_, 783 EXPECT_EQ(mock_tables_->host_redirect_table_.data_,
842 RedirectDataMap( 784 RedirectDataMap(
843 {{host_redirect_data.primary_key(), host_redirect_data}})); 785 {{host_redirect_data.primary_key(), host_redirect_data}}));
844 } 786 }
845 787
846 // Tests that redirect is recorded correctly for URL already present in 788 // Tests that redirect is recorded correctly for URL already present in
847 // the database cache. 789 // the database cache.
848 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { 790 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) {
849 const int kVisitCount = 7; 791 const int kVisitCount = 7;
850 AddUrlToHistory("https://facebook.com/google", kVisitCount); 792 AddUrlToHistory("https://facebook.com/google", kVisitCount);
851 793
852 mock_tables_->url_redirect_table_.data_ = test_url_redirect_data_; 794 mock_tables_->url_redirect_table_.data_ = test_url_redirect_data_;
853 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_; 795 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_;
854 796
855 ResetPredictor(); 797 ResetPredictor();
856 InitializePredictor(); 798 InitializePredictor();
857 799
858 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); 800 auto page_summary = CreatePageRequestSummary(
859 predictor_->RecordURLRequest(fb1); 801 "https://facebook.com/google", "http://fb.com/google",
860 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 802 std::vector<URLRequestSummary>());
861
862 URLRequestSummary fb2 = CreateRedirectRequestSummary(
863 1, "http://fb.com/google", "http://facebook.com/google");
864 predictor_->RecordURLRedirect(fb2);
865 URLRequestSummary fb3 = CreateRedirectRequestSummary(
866 1, "http://facebook.com/google", "https://facebook.com/google");
867 predictor_->RecordURLRedirect(fb3);
868 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
869 803
870 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 804 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
871 EXPECT_CALL( 805 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
872 mock_observer,
873 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
874 "https://facebook.com/google",
875 "http://fb.com/google",
876 std::vector<URLRequestSummary>())));
877 806
878 predictor_->RecordMainFrameLoadComplete(fb_end); 807 predictor_->RecordPageRequestSummary(
808 base::MakeUnique<PageRequestSummary>(page_summary));
879 profile_->BlockUntilHistoryProcessesPendingRequests(); 809 profile_->BlockUntilHistoryProcessesPendingRequests();
880 810
881 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); 811 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google");
882 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), 812 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
883 "https://facebook.com/google", 6, 1, 0); 813 "https://facebook.com/google", 6, 1, 0);
884 // Existing redirect to https://facebook.com/login will be deleted because of 814 // Existing redirect to https://facebook.com/login will be deleted because of
885 // too many consecutive misses. 815 // too many consecutive misses.
886 RedirectDataMap expected_url_redirect_data = test_url_redirect_data_; 816 RedirectDataMap expected_url_redirect_data = test_url_redirect_data_;
887 expected_url_redirect_data[url_redirect_data.primary_key()] = 817 expected_url_redirect_data[url_redirect_data.primary_key()] =
888 url_redirect_data; 818 url_redirect_data;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 EXPECT_EQ(mock_tables_->url_redirect_table_.data_, url_redirects); 910 EXPECT_EQ(mock_tables_->url_redirect_table_.data_, url_redirects);
981 EXPECT_EQ(mock_tables_->host_redirect_table_.data_, host_redirects); 911 EXPECT_EQ(mock_tables_->host_redirect_table_.data_, host_redirects);
982 912
983 predictor_->DeleteAllUrls(); 913 predictor_->DeleteAllUrls();
984 EXPECT_TRUE(mock_tables_->url_resource_table_.data_.empty()); 914 EXPECT_TRUE(mock_tables_->url_resource_table_.data_.empty());
985 EXPECT_TRUE(mock_tables_->host_resource_table_.data_.empty()); 915 EXPECT_TRUE(mock_tables_->host_resource_table_.data_.empty());
986 EXPECT_TRUE(mock_tables_->url_redirect_table_.data_.empty()); 916 EXPECT_TRUE(mock_tables_->url_redirect_table_.data_.empty());
987 EXPECT_TRUE(mock_tables_->host_redirect_table_.data_.empty()); 917 EXPECT_TRUE(mock_tables_->host_redirect_table_.data_.empty());
988 } 918 }
989 919
990 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) {
991 URLRequestSummary summary1 = CreateURLRequestSummary(
992 1, "http://www.google.com", "http://www.google.com",
993 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
994 URLRequestSummary summary2 = CreateURLRequestSummary(
995 2, "http://www.google.com", "http://www.google.com",
996 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
997 URLRequestSummary summary3 = CreateURLRequestSummary(
998 3, "http://www.yahoo.com", "http://www.yahoo.com",
999 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1000
1001 predictor_->OnMainFrameRequest(summary1);
1002 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1003 predictor_->OnMainFrameRequest(summary2);
1004 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1005 predictor_->OnMainFrameRequest(summary3);
1006 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1007
1008 // Insert another with same navigation id. It should replace.
1009 URLRequestSummary summary4 = CreateURLRequestSummary(
1010 1, "http://www.nike.com", "http://www.nike.com",
1011 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1012 URLRequestSummary summary5 = CreateURLRequestSummary(
1013 2, "http://www.google.com", "http://www.google.com",
1014 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1015
1016 predictor_->OnMainFrameRequest(summary4);
1017 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1018
1019 // Change this creation time so that it will go away on the next insert.
1020 summary5.navigation_id.creation_time = base::TimeTicks::Now() -
1021 base::TimeDelta::FromDays(1);
1022 predictor_->OnMainFrameRequest(summary5);
1023 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1024
1025 URLRequestSummary summary6 = CreateURLRequestSummary(
1026 4, "http://www.shoes.com", "http://www.shoes.com",
1027 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1028 predictor_->OnMainFrameRequest(summary6);
1029 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1030
1031 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) !=
1032 predictor_->inflight_navigations_.end());
1033 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) !=
1034 predictor_->inflight_navigations_.end());
1035 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) !=
1036 predictor_->inflight_navigations_.end());
1037 }
1038
1039 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) {
1040 URLRequestSummary yahoo = CreateURLRequestSummary(1, "http://yahoo.com");
1041
1042 URLRequestSummary bbc1 = CreateURLRequestSummary(2, "http://bbc.com");
1043 URLRequestSummary bbc2 =
1044 CreateRedirectRequestSummary(2, "http://bbc.com", "https://www.bbc.com");
1045 NavigationID bbc_end = CreateNavigationID(2, "https://www.bbc.com");
1046
1047 URLRequestSummary youtube1 = CreateURLRequestSummary(3, "http://youtube.com");
1048 URLRequestSummary youtube2 = CreateRedirectRequestSummary(
1049 3, "http://youtube.com", "https://youtube.com");
1050 NavigationID youtube_end = CreateNavigationID(3, "https://youtube.com");
1051
1052 URLRequestSummary nyt1 = CreateURLRequestSummary(4, "http://nyt.com");
1053 URLRequestSummary nyt2 =
1054 CreateRedirectRequestSummary(4, "http://nyt.com", "http://nytimes.com");
1055 URLRequestSummary nyt3 = CreateRedirectRequestSummary(4, "http://nytimes.com",
1056 "http://m.nytimes.com");
1057 NavigationID nyt_end = CreateNavigationID(4, "http://m.nytimes.com");
1058
1059 URLRequestSummary fb1 = CreateURLRequestSummary(5, "http://fb.com");
1060 URLRequestSummary fb2 =
1061 CreateRedirectRequestSummary(5, "http://fb.com", "http://facebook.com");
1062 URLRequestSummary fb3 = CreateRedirectRequestSummary(5, "http://facebook.com",
1063 "https://facebook.com");
1064 URLRequestSummary fb4 = CreateRedirectRequestSummary(
1065 5, "https://facebook.com",
1066 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1067 NavigationID fb_end = CreateNavigationID(
1068 5,
1069 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1070
1071 // Redirect with empty redirect_url will be deleted.
1072 predictor_->OnMainFrameRequest(yahoo);
1073 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1074 predictor_->OnMainFrameRedirect(yahoo);
1075 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
1076
1077 // Redirect without previous request works fine.
1078 // predictor_->OnMainFrameRequest(bbc1) missing.
1079 predictor_->OnMainFrameRedirect(bbc2);
1080 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1081 EXPECT_EQ(bbc1.navigation_id.main_frame_url,
1082 predictor_->inflight_navigations_[bbc_end]->initial_url);
1083
1084 // http://youtube.com -> https://youtube.com.
1085 predictor_->OnMainFrameRequest(youtube1);
1086 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1087 predictor_->OnMainFrameRedirect(youtube2);
1088 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1089 EXPECT_EQ(youtube1.navigation_id.main_frame_url,
1090 predictor_->inflight_navigations_[youtube_end]->initial_url);
1091
1092 // http://nyt.com -> http://nytimes.com -> http://m.nytimes.com.
1093 predictor_->OnMainFrameRequest(nyt1);
1094 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1095 predictor_->OnMainFrameRedirect(nyt2);
1096 predictor_->OnMainFrameRedirect(nyt3);
1097 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1098 EXPECT_EQ(nyt1.navigation_id.main_frame_url,
1099 predictor_->inflight_navigations_[nyt_end]->initial_url);
1100
1101 // http://fb.com -> http://facebook.com -> https://facebook.com ->
1102 // https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr.
1103 predictor_->OnMainFrameRequest(fb1);
1104 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1105 predictor_->OnMainFrameRedirect(fb2);
1106 predictor_->OnMainFrameRedirect(fb3);
1107 predictor_->OnMainFrameRedirect(fb4);
1108 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1109 EXPECT_EQ(fb1.navigation_id.main_frame_url,
1110 predictor_->inflight_navigations_[fb_end]->initial_url);
1111 }
1112
1113 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) {
1114 // If there is no inflight navigation, nothing happens.
1115 URLRequestSummary resource1 = CreateURLRequestSummary(
1116 1, "http://www.google.com", "http://google.com/style1.css",
1117 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
1118 predictor_->OnSubresourceResponse(resource1);
1119 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
1120
1121 // Add an inflight navigation.
1122 URLRequestSummary main_frame1 = CreateURLRequestSummary(
1123 1, "http://www.google.com", "http://www.google.com",
1124 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1125 predictor_->OnMainFrameRequest(main_frame1);
1126 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1127
1128 // Now add a few subresources.
1129 URLRequestSummary resource2 = CreateURLRequestSummary(
1130 1, "http://www.google.com", "http://google.com/script1.js",
1131 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1132 URLRequestSummary resource3 = CreateURLRequestSummary(
1133 1, "http://www.google.com", "http://google.com/script2.js",
1134 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1135 predictor_->OnSubresourceResponse(resource1);
1136 predictor_->OnSubresourceResponse(resource2);
1137 predictor_->OnSubresourceResponse(resource3);
1138
1139 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1140 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id]
1141 ->subresource_requests.size());
1142 EXPECT_EQ(resource1,
1143 predictor_->inflight_navigations_[main_frame1.navigation_id]
1144 ->subresource_requests[0]);
1145 EXPECT_EQ(resource2,
1146 predictor_->inflight_navigations_[main_frame1.navigation_id]
1147 ->subresource_requests[1]);
1148 EXPECT_EQ(resource3,
1149 predictor_->inflight_navigations_[main_frame1.navigation_id]
1150 ->subresource_requests[2]);
1151 }
1152
1153 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
1154 net::HttpResponseInfo response_info;
1155 response_info.headers =
1156 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1157 response_info.was_cached = true;
1158 url_request_job_factory_.set_response_info(response_info);
1159
1160 GURL url("http://www.google.com/cat.png");
1161 std::unique_ptr<net::URLRequest> request =
1162 CreateURLRequest(url_request_context_, url, net::MEDIUM,
1163 content::RESOURCE_TYPE_IMAGE, true);
1164 URLRequestSummary summary;
1165 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1166 EXPECT_EQ(url, summary.resource_url);
1167 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1168 EXPECT_TRUE(summary.was_cached);
1169 EXPECT_FALSE(summary.has_validators);
1170 EXPECT_FALSE(summary.always_revalidate);
1171
1172 // Navigation_id elements should be unset by default.
1173 EXPECT_EQ(-1, summary.navigation_id.tab_id);
1174 EXPECT_EQ(GURL(), summary.navigation_id.main_frame_url);
1175 }
1176
1177 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseContentType) {
1178 net::HttpResponseInfo response_info;
1179 response_info.headers = MakeResponseHeaders(
1180 "HTTP/1.1 200 OK\n\n"
1181 "Some: Headers\n"
1182 "Content-Type: image/whatever\n");
1183 url_request_job_factory_.set_response_info(response_info);
1184 url_request_job_factory_.set_mime_type("image/png");
1185
1186 std::unique_ptr<net::URLRequest> request = CreateURLRequest(
1187 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM,
1188 content::RESOURCE_TYPE_PREFETCH, true);
1189 URLRequestSummary summary;
1190 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1191 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1192 }
1193
1194 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseCachePolicy) {
1195 net::HttpResponseInfo response_info;
1196 response_info.headers = MakeResponseHeaders(
1197 "HTTP/1.1 200 OK\n"
1198 "Some: Headers\n");
1199 url_request_job_factory_.set_response_info(response_info);
1200
1201 std::unique_ptr<net::URLRequest> request_no_validators = CreateURLRequest(
1202 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM,
1203 content::RESOURCE_TYPE_PREFETCH, true);
1204
1205 URLRequestSummary summary;
1206 EXPECT_TRUE(
1207 URLRequestSummary::SummarizeResponse(*request_no_validators, &summary));
1208 EXPECT_FALSE(summary.has_validators);
1209
1210 response_info.headers = MakeResponseHeaders(
1211 "HTTP/1.1 200 OK\n"
1212 "ETag: \"Cr66\"\n"
1213 "Cache-Control: no-cache\n");
1214 url_request_job_factory_.set_response_info(response_info);
1215 std::unique_ptr<net::URLRequest> request_etag = CreateURLRequest(
1216 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM,
1217 content::RESOURCE_TYPE_PREFETCH, true);
1218 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1219 EXPECT_TRUE(summary.has_validators);
1220 EXPECT_TRUE(summary.always_revalidate);
1221 }
1222
1223 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) { 920 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) {
1224 // The data that will be used in populating. 921 // The data that will be used in populating.
1225 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); 922 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1);
1226 InitializeResourceData(google.add_resources(), "http://google.com/image1.png", 923 InitializeResourceData(google.add_resources(), "http://google.com/image1.png",
1227 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2, 924 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2,
1228 net::MEDIUM, false, false); // good 925 net::MEDIUM, false, false); // good
1229 InitializeResourceData(google.add_resources(), "http://google.com/style1.css", 926 InitializeResourceData(google.add_resources(), "http://google.com/style1.css",
1230 content::RESOURCE_TYPE_STYLESHEET, 2, 2, 1, 1.0, 927 content::RESOURCE_TYPE_STYLESHEET, 2, 2, 1, 1.0,
1231 net::MEDIUM, false, false); // still good 928 net::MEDIUM, false, false); // still good
1232 InitializeResourceData(google.add_resources(), "http://google.com/script3.js", 929 InitializeResourceData(google.add_resources(), "http://google.com/script3.js",
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
1447 predictor_->origin_data_->UpdateData(www_google.host(), www_google); 1144 predictor_->origin_data_->UpdateData(www_google.host(), www_google);
1448 1145
1449 prediction = base::MakeUnique<PreconnectPrediction>(); 1146 prediction = base::MakeUnique<PreconnectPrediction>();
1450 EXPECT_TRUE( 1147 EXPECT_TRUE(
1451 predictor_->PredictPreconnectOrigins(main_frame_url, prediction.get())); 1148 predictor_->PredictPreconnectOrigins(main_frame_url, prediction.get()));
1452 EXPECT_EQ(*prediction, CreatePreconnectPrediction("www.google.com", true, 1149 EXPECT_EQ(*prediction, CreatePreconnectPrediction("www.google.com", true,
1453 {GURL(gen_origin(4))}, 1150 {GURL(gen_origin(4))},
1454 std::vector<GURL>())); 1151 std::vector<GURL>()));
1455 } 1152 }
1456 1153
1457 TEST_F(ResourcePrefetchPredictorTest, TestRecordFirstContentfulPaint) {
1458 auto res1_time = base::TimeTicks::FromInternalValue(1);
1459 auto res2_time = base::TimeTicks::FromInternalValue(2);
1460 auto fcp_time = base::TimeTicks::FromInternalValue(3);
1461 auto res3_time = base::TimeTicks::FromInternalValue(4);
1462
1463 URLRequestSummary main_frame =
1464 CreateURLRequestSummary(1, "http://www.google.com");
1465 predictor_->RecordURLRequest(main_frame);
1466 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1467
1468 URLRequestSummary resource1 = CreateURLRequestSummary(
1469 1, "http://www.google.com", "http://google.com/style1.css",
1470 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
1471 resource1.response_time = res1_time;
1472 predictor_->RecordURLResponse(resource1);
1473 URLRequestSummary resource2 = CreateURLRequestSummary(
1474 1, "http://www.google.com", "http://google.com/script1.js",
1475 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1476 resource2.response_time = res2_time;
1477 predictor_->RecordURLResponse(resource2);
1478 URLRequestSummary resource3 = CreateURLRequestSummary(
1479 1, "http://www.google.com", "http://google.com/script2.js",
1480 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1481 resource3.response_time = res3_time;
1482 predictor_->RecordURLResponse(resource3);
1483
1484 predictor_->RecordFirstContentfulPaint(main_frame.navigation_id, fcp_time);
1485
1486 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
1487 profile_->BlockUntilHistoryProcessesPendingRequests();
1488
1489 PrefetchData host_data = CreatePrefetchData("www.google.com");
1490 InitializeResourceData(host_data.add_resources(),
1491 "http://google.com/style1.css",
1492 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
1493 net::MEDIUM, false, false);
1494 InitializeResourceData(
1495 host_data.add_resources(), "http://google.com/script1.js",
1496 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false);
1497 ResourceData* resource3_rd = host_data.add_resources();
1498 InitializeResourceData(resource3_rd, "http://google.com/script2.js",
1499 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0,
1500 net::MEDIUM, false, false);
1501 resource3_rd->set_before_first_contentful_paint(false);
1502 EXPECT_EQ(mock_tables_->host_resource_table_.data_,
1503 PrefetchDataMap({{host_data.primary_key(), host_data}}));
1504 }
1505
1506 } // namespace predictors 1154 } // namespace predictors
OLDNEW
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_predictor_tab_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698