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

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: Address alexilin feedback. 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
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 18 matching lines...) Expand all
29 #include "net/url_request/url_request_context.h" 29 #include "net/url_request/url_request_context.h"
30 #include "net/url_request/url_request_job.h" 30 #include "net/url_request/url_request_job.h"
31 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 using testing::StrictMock; 34 using testing::StrictMock;
35 using testing::UnorderedElementsAre; 35 using testing::UnorderedElementsAre;
36 36
37 namespace predictors { 37 namespace predictors {
38 38
39 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
40 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
41 using PrefetchDataMap = std::map<std::string, PrefetchData>; 39 using PrefetchDataMap = std::map<std::string, PrefetchData>;
42 using RedirectDataMap = std::map<std::string, RedirectData>; 40 using RedirectDataMap = std::map<std::string, RedirectData>;
43 using OriginDataMap = std::map<std::string, OriginData>; 41 using OriginDataMap = std::map<std::string, OriginData>;
44 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>; 42 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>;
45 43
46 template <typename T> 44 template <typename T>
47 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { 45 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> {
48 public: 46 public:
49 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} 47 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {}
50 void GetAllData(std::map<std::string, T>* data_map, 48 void GetAllData(std::map<std::string, T>* data_map,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 protected: 114 protected:
117 ~MockResourcePrefetchPredictorTables() override = default; 115 ~MockResourcePrefetchPredictorTables() override = default;
118 }; 116 };
119 117
120 class MockResourcePrefetchPredictorObserver : public TestObserver { 118 class MockResourcePrefetchPredictorObserver : public TestObserver {
121 public: 119 public:
122 explicit MockResourcePrefetchPredictorObserver( 120 explicit MockResourcePrefetchPredictorObserver(
123 ResourcePrefetchPredictor* predictor) 121 ResourcePrefetchPredictor* predictor)
124 : TestObserver(predictor) {} 122 : TestObserver(predictor) {}
125 123
126 MOCK_METHOD2( 124 MOCK_METHOD2(OnNavigationLearned,
127 OnNavigationLearned, 125 void(size_t url_visit_count, const PageRequestSummary& summary));
128 void(size_t url_visit_count,
129 const ResourcePrefetchPredictor::PageRequestSummary& summary));
130 }; 126 };
131 127
132 class ResourcePrefetchPredictorTest : public testing::Test { 128 class ResourcePrefetchPredictorTest : public testing::Test {
133 public: 129 public:
134 ResourcePrefetchPredictorTest(); 130 ResourcePrefetchPredictorTest();
135 ~ResourcePrefetchPredictorTest() override; 131 ~ResourcePrefetchPredictorTest() override;
136 void SetUp() override; 132 void SetUp() override;
137 void TearDown() override; 133 void TearDown() override;
138 134
139 protected: 135 protected:
140 void AddUrlToHistory(const std::string& url, int visit_count) { 136 void AddUrlToHistory(const std::string& url, int visit_count) {
141 HistoryServiceFactory::GetForProfile(profile_.get(), 137 HistoryServiceFactory::GetForProfile(profile_.get(),
142 ServiceAccessType::EXPLICIT_ACCESS)-> 138 ServiceAccessType::EXPLICIT_ACCESS)->
143 AddPageWithDetails( 139 AddPageWithDetails(
144 GURL(url), 140 GURL(url),
145 base::string16(), 141 base::string16(),
146 visit_count, 142 visit_count,
147 0, 143 0,
148 base::Time::Now(), 144 base::Time::Now(),
149 false, 145 false,
150 history::SOURCE_BROWSED); 146 history::SOURCE_BROWSED);
151 profile_->BlockUntilHistoryProcessesPendingRequests(); 147 profile_->BlockUntilHistoryProcessesPendingRequests();
152 } 148 }
153 149
154 URLRequestSummary CreateRedirectRequestSummary(
155 SessionID::id_type session_id,
156 const std::string& main_frame_url,
157 const std::string& redirect_url) {
158 URLRequestSummary summary =
159 CreateURLRequestSummary(session_id, main_frame_url);
160 summary.redirect_url = GURL(redirect_url);
161 return summary;
162 }
163
164 void InitializePredictor() { 150 void InitializePredictor() {
165 loading_predictor_->StartInitialization(); 151 loading_predictor_->StartInitialization();
166 db_task_runner_->RunUntilIdle(); 152 db_task_runner_->RunUntilIdle();
167 profile_->BlockUntilHistoryProcessesPendingRequests(); 153 profile_->BlockUntilHistoryProcessesPendingRequests();
168 } 154 }
169 155
170 void ResetPredictor(bool small_db = true) { 156 void ResetPredictor(bool small_db = true) {
171 LoadingPredictorConfig config; 157 LoadingPredictorConfig config;
172 PopulateTestConfig(&config, small_db); 158 PopulateTestConfig(&config, small_db);
173 loading_predictor_ = 159 loading_predictor_ =
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 198
213 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); 199 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
214 profile_->BlockUntilHistoryProcessesPendingRequests(); 200 profile_->BlockUntilHistoryProcessesPendingRequests();
215 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( 201 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
216 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); 202 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS));
217 // Initialize the predictor with empty data. 203 // Initialize the predictor with empty data.
218 ResetPredictor(); 204 ResetPredictor();
219 EXPECT_EQ(predictor_->initialization_state_, 205 EXPECT_EQ(predictor_->initialization_state_,
220 ResourcePrefetchPredictor::NOT_INITIALIZED); 206 ResourcePrefetchPredictor::NOT_INITIALIZED);
221 InitializePredictor(); 207 InitializePredictor();
222 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
223 EXPECT_EQ(predictor_->initialization_state_, 208 EXPECT_EQ(predictor_->initialization_state_,
224 ResourcePrefetchPredictor::INITIALIZED); 209 ResourcePrefetchPredictor::INITIALIZED);
225 210
226 url_request_job_factory_.Reset(); 211 url_request_job_factory_.Reset();
227 url_request_context_.set_job_factory(&url_request_job_factory_); 212 url_request_context_.set_job_factory(&url_request_job_factory_);
228 213
229 histogram_tester_ = base::MakeUnique<base::HistogramTester>(); 214 histogram_tester_ = base::MakeUnique<base::HistogramTester>();
230 } 215 }
231 216
232 void ResourcePrefetchPredictorTest::TearDown() { 217 void ResourcePrefetchPredictorTest::TearDown() {
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_; 407 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_;
423 mock_tables_->manifest_table_.data_ = test_manifest_data_; 408 mock_tables_->manifest_table_.data_ = test_manifest_data_;
424 mock_tables_->origin_table_.data_ = test_origin_data_; 409 mock_tables_->origin_table_.data_ = test_origin_data_;
425 410
426 ResetPredictor(); 411 ResetPredictor();
427 InitializePredictor(); 412 InitializePredictor();
428 413
429 // Test that the internal variables correctly initialized. 414 // Test that the internal variables correctly initialized.
430 EXPECT_EQ(predictor_->initialization_state_, 415 EXPECT_EQ(predictor_->initialization_state_,
431 ResourcePrefetchPredictor::INITIALIZED); 416 ResourcePrefetchPredictor::INITIALIZED);
432 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
433 417
434 // Integrity of the cache and the backend storage is checked on TearDown. 418 // Integrity of the cache and the backend storage is checked on TearDown.
435 } 419 }
436 420
437 // Single navigation but history count is low, so should not record url data. 421 // Single navigation but history count is low, so should not record url data.
438 TEST_F(ResourcePrefetchPredictorTest, NavigationLowHistoryCount) { 422 TEST_F(ResourcePrefetchPredictorTest, NavigationLowHistoryCount) {
439 const int kVisitCount = 1; 423 const int kVisitCount = 1;
440 AddUrlToHistory("https://www.google.com", kVisitCount); 424 AddUrlToHistory("https://www.google.com", kVisitCount);
441 425
442 URLRequestSummary main_frame = 426 URLRequestSummary main_frame =
443 CreateURLRequestSummary(1, "http://www.google.com"); 427 CreateURLRequestSummary(1, "http://www.google.com");
444 predictor_->RecordURLRequest(main_frame);
445 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
446 428
447 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary( 429 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary(
448 1, "http://www.google.com", "https://www.google.com"); 430 1, "http://www.google.com", "https://www.google.com");
449 predictor_->RecordURLRedirect(main_frame_redirect);
450 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
451 main_frame = CreateURLRequestSummary(1, "https://www.google.com"); 431 main_frame = CreateURLRequestSummary(1, "https://www.google.com");
452 432
453 // Now add a few subresources. 433 // Now add a few subresources.
454 URLRequestSummary resource1 = CreateURLRequestSummary( 434 URLRequestSummary resource1 = CreateURLRequestSummary(
455 1, "https://www.google.com", "https://google.com/style1.css", 435 1, "https://www.google.com", "https://google.com/style1.css",
456 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 436 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
457 predictor_->RecordURLResponse(resource1);
458 URLRequestSummary resource2 = CreateURLRequestSummary( 437 URLRequestSummary resource2 = CreateURLRequestSummary(
459 1, "https://www.google.com", "https://google.com/script1.js", 438 1, "https://www.google.com", "https://google.com/script1.js",
460 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 439 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
461 predictor_->RecordURLResponse(resource2);
462 URLRequestSummary resource3 = CreateURLRequestSummary( 440 URLRequestSummary resource3 = CreateURLRequestSummary(
463 1, "https://www.google.com", "https://google.com/script2.js", 441 1, "https://www.google.com", "https://google.com/script2.js",
464 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 442 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
465 predictor_->RecordURLResponse(resource3); 443
444 auto page_summary = CreatePageRequestSummary(
445 "https://www.google.com", "http://www.google.com",
446 {resource1, resource2, resource3});
466 447
467 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 448 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
468 EXPECT_CALL( 449 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
469 mock_observer,
470 OnNavigationLearned(kVisitCount,
471 CreatePageRequestSummary(
472 "https://www.google.com", "http://www.google.com",
473 {resource1, resource2, resource3})));
474 450
475 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 451 predictor_->RecordPageRequestSummary(
452 base::MakeUnique<PageRequestSummary>(page_summary));
476 profile_->BlockUntilHistoryProcessesPendingRequests(); 453 profile_->BlockUntilHistoryProcessesPendingRequests();
477 454
478 PrefetchData host_data = CreatePrefetchData("www.google.com"); 455 PrefetchData host_data = CreatePrefetchData("www.google.com");
479 InitializeResourceData(host_data.add_resources(), 456 InitializeResourceData(host_data.add_resources(),
480 "https://google.com/style1.css", 457 "https://google.com/style1.css",
481 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 458 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
482 net::MEDIUM, false, false); 459 net::MEDIUM, false, false);
483 InitializeResourceData( 460 InitializeResourceData(
484 host_data.add_resources(), "https://google.com/script1.js", 461 host_data.add_resources(), "https://google.com/script1.js",
485 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); 462 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false);
(...skipping 18 matching lines...) Expand all
504 } 481 }
505 482
506 // Single navigation that will be recorded. Will check for duplicate 483 // Single navigation that will be recorded. Will check for duplicate
507 // resources and also for number of resources saved. 484 // resources and also for number of resources saved.
508 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 485 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
509 const int kVisitCount = 4; 486 const int kVisitCount = 4;
510 AddUrlToHistory("http://www.google.com", kVisitCount); 487 AddUrlToHistory("http://www.google.com", kVisitCount);
511 488
512 URLRequestSummary main_frame = 489 URLRequestSummary main_frame =
513 CreateURLRequestSummary(1, "http://www.google.com"); 490 CreateURLRequestSummary(1, "http://www.google.com");
514 predictor_->RecordURLRequest(main_frame);
515 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
516 491
517 std::vector<URLRequestSummary> resources; 492 std::vector<URLRequestSummary> resources;
518 resources.push_back(CreateURLRequestSummary( 493 resources.push_back(CreateURLRequestSummary(
519 1, "http://www.google.com", "http://google.com/style1.css", 494 1, "http://www.google.com", "http://google.com/style1.css",
520 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); 495 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false));
521 predictor_->RecordURLResponse(resources.back());
522 resources.push_back(CreateURLRequestSummary( 496 resources.push_back(CreateURLRequestSummary(
523 1, "http://www.google.com", "http://google.com/script1.js", 497 1, "http://www.google.com", "http://google.com/script1.js",
524 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 498 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
525 predictor_->RecordURLResponse(resources.back());
526 resources.push_back(CreateURLRequestSummary( 499 resources.push_back(CreateURLRequestSummary(
527 1, "http://www.google.com", "http://google.com/script2.js", 500 1, "http://www.google.com", "http://google.com/script2.js",
528 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 501 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
529 predictor_->RecordURLResponse(resources.back());
530 resources.push_back(CreateURLRequestSummary( 502 resources.push_back(CreateURLRequestSummary(
531 1, "http://www.google.com", "http://google.com/script1.js", 503 1, "http://www.google.com", "http://google.com/script1.js",
532 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); 504 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true));
533 predictor_->RecordURLResponse(resources.back());
534 resources.push_back(CreateURLRequestSummary( 505 resources.push_back(CreateURLRequestSummary(
535 1, "http://www.google.com", "http://google.com/image1.png", 506 1, "http://www.google.com", "http://google.com/image1.png",
536 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 507 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
537 predictor_->RecordURLResponse(resources.back());
538 resources.push_back(CreateURLRequestSummary( 508 resources.push_back(CreateURLRequestSummary(
539 1, "http://www.google.com", "http://google.com/image2.png", 509 1, "http://www.google.com", "http://google.com/image2.png",
540 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 510 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
541 predictor_->RecordURLResponse(resources.back());
542 resources.push_back(CreateURLRequestSummary( 511 resources.push_back(CreateURLRequestSummary(
543 1, "http://www.google.com", "http://google.com/style2.css", 512 1, "http://www.google.com", "http://google.com/style2.css",
544 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 513 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
545 predictor_->RecordURLResponse(resources.back());
546 514
547 auto no_store = CreateURLRequestSummary( 515 auto no_store = CreateURLRequestSummary(
548 1, "http://www.google.com", 516 1, "http://www.google.com",
549 "http://static.google.com/style2-no-store.css", 517 "http://static.google.com/style2-no-store.css",
550 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 518 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
551 no_store.is_no_store = true; 519 no_store.is_no_store = true;
552 predictor_->RecordURLResponse(no_store);
553 520
554 auto redirected = CreateURLRequestSummary( 521 auto redirected = CreateURLRequestSummary(
555 1, "http://www.google.com", "http://reader.google.com/style.css", 522 1, "http://www.google.com", "http://reader.google.com/style.css",
556 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 523 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
557 redirected.redirect_url = GURL("http://dev.null.google.com/style.css"); 524 redirected.redirect_url = GURL("http://dev.null.google.com/style.css");
558 525
559 predictor_->RecordURLRedirect(redirected); 526 auto page_summary = CreatePageRequestSummary(
527 "http://www.google.com", "http://www.google.com", resources);
528 page_summary.UpdateOrAddToOrigins(no_store);
529 page_summary.UpdateOrAddToOrigins(redirected);
530
560 redirected.is_no_store = true; 531 redirected.is_no_store = true;
561 redirected.request_url = redirected.redirect_url; 532 redirected.request_url = redirected.redirect_url;
562 redirected.redirect_url = GURL(); 533 redirected.redirect_url = GURL();
563 534 page_summary.UpdateOrAddToOrigins(redirected);
564 predictor_->RecordURLResponse(redirected);
565 535
566 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 536 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
567 EXPECT_CALL(mock_observer, 537 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
568 OnNavigationLearned(
569 kVisitCount, CreatePageRequestSummary("http://www.google.com",
570 "http://www.google.com",
571 resources)));
572 538
573 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 539 predictor_->RecordPageRequestSummary(
540 base::MakeUnique<PageRequestSummary>(page_summary));
574 profile_->BlockUntilHistoryProcessesPendingRequests(); 541 profile_->BlockUntilHistoryProcessesPendingRequests();
575 542
576 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 543 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
577 InitializeResourceData(url_data.add_resources(), 544 InitializeResourceData(url_data.add_resources(),
578 "http://google.com/style1.css", 545 "http://google.com/style1.css",
579 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 546 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
580 net::MEDIUM, false, false); 547 net::MEDIUM, false, false);
581 InitializeResourceData(url_data.add_resources(), 548 InitializeResourceData(url_data.add_resources(),
582 "http://google.com/style2.css", 549 "http://google.com/style2.css",
583 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, 550 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 598
632 mock_tables_->url_resource_table_.data_ = test_url_data_; 599 mock_tables_->url_resource_table_.data_ = test_url_data_;
633 mock_tables_->host_resource_table_.data_ = test_host_data_; 600 mock_tables_->host_resource_table_.data_ = test_host_data_;
634 601
635 ResetPredictor(); 602 ResetPredictor();
636 InitializePredictor(); 603 InitializePredictor();
637 604
638 URLRequestSummary main_frame = CreateURLRequestSummary( 605 URLRequestSummary main_frame = CreateURLRequestSummary(
639 1, "http://www.google.com", "http://www.google.com", 606 1, "http://www.google.com", "http://www.google.com",
640 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 607 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
641 predictor_->RecordURLRequest(main_frame);
642 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
643 608
644 std::vector<URLRequestSummary> resources; 609 std::vector<URLRequestSummary> resources;
645 resources.push_back(CreateURLRequestSummary( 610 resources.push_back(CreateURLRequestSummary(
646 1, "http://www.google.com", "http://google.com/style1.css", 611 1, "http://www.google.com", "http://google.com/style1.css",
647 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); 612 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false));
648 predictor_->RecordURLResponse(resources.back());
649 resources.push_back(CreateURLRequestSummary( 613 resources.push_back(CreateURLRequestSummary(
650 1, "http://www.google.com", "http://google.com/script1.js", 614 1, "http://www.google.com", "http://google.com/script1.js",
651 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 615 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
652 predictor_->RecordURLResponse(resources.back());
653 resources.push_back(CreateURLRequestSummary( 616 resources.push_back(CreateURLRequestSummary(
654 1, "http://www.google.com", "http://google.com/script2.js", 617 1, "http://www.google.com", "http://google.com/script2.js",
655 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 618 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
656 predictor_->RecordURLResponse(resources.back());
657 resources.push_back(CreateURLRequestSummary( 619 resources.push_back(CreateURLRequestSummary(
658 1, "http://www.google.com", "http://google.com/script1.js", 620 1, "http://www.google.com", "http://google.com/script1.js",
659 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); 621 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true));
660 predictor_->RecordURLResponse(resources.back());
661 resources.push_back(CreateURLRequestSummary( 622 resources.push_back(CreateURLRequestSummary(
662 1, "http://www.google.com", "http://google.com/image1.png", 623 1, "http://www.google.com", "http://google.com/image1.png",
663 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 624 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
664 predictor_->RecordURLResponse(resources.back());
665 resources.push_back(CreateURLRequestSummary( 625 resources.push_back(CreateURLRequestSummary(
666 1, "http://www.google.com", "http://google.com/image2.png", 626 1, "http://www.google.com", "http://google.com/image2.png",
667 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 627 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
668 predictor_->RecordURLResponse(resources.back());
669 resources.push_back(CreateURLRequestSummary( 628 resources.push_back(CreateURLRequestSummary(
670 1, "http://www.google.com", "http://google.com/style2.css", 629 1, "http://www.google.com", "http://google.com/style2.css",
671 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 630 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
672 predictor_->RecordURLResponse(resources.back());
673 auto no_store = CreateURLRequestSummary( 631 auto no_store = CreateURLRequestSummary(
674 1, "http://www.google.com", 632 1, "http://www.google.com",
675 "http://static.google.com/style2-no-store.css", 633 "http://static.google.com/style2-no-store.css",
676 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 634 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
677 no_store.is_no_store = true; 635 no_store.is_no_store = true;
678 predictor_->RecordURLResponse(no_store); 636
637 auto page_summary = CreatePageRequestSummary(
638 "http://www.google.com", "http://www.google.com", resources);
639 page_summary.UpdateOrAddToOrigins(no_store);
679 640
680 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 641 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
681 EXPECT_CALL(mock_observer, 642 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
682 OnNavigationLearned(
683 kVisitCount, CreatePageRequestSummary("http://www.google.com",
684 "http://www.google.com",
685 resources)));
686 643
687 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 644 predictor_->RecordPageRequestSummary(
645 base::MakeUnique<PageRequestSummary>(page_summary));
688 profile_->BlockUntilHistoryProcessesPendingRequests(); 646 profile_->BlockUntilHistoryProcessesPendingRequests();
689 647
690 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 648 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
691 InitializeResourceData(url_data.add_resources(), 649 InitializeResourceData(url_data.add_resources(),
692 "http://google.com/style1.css", 650 "http://google.com/style1.css",
693 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, 651 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0,
694 net::MEDIUM, false, false); 652 net::MEDIUM, false, false);
695 InitializeResourceData(url_data.add_resources(), 653 InitializeResourceData(url_data.add_resources(),
696 "http://google.com/style2.css", 654 "http://google.com/style2.css",
697 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, 655 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 mock_tables_->url_resource_table_.data_ = test_url_data_; 718 mock_tables_->url_resource_table_.data_ = test_url_data_;
761 mock_tables_->host_resource_table_.data_ = test_host_data_; 719 mock_tables_->host_resource_table_.data_ = test_host_data_;
762 mock_tables_->origin_table_.data_ = test_origin_data_; 720 mock_tables_->origin_table_.data_ = test_origin_data_;
763 721
764 ResetPredictor(); 722 ResetPredictor();
765 InitializePredictor(); 723 InitializePredictor();
766 724
767 URLRequestSummary main_frame = CreateURLRequestSummary( 725 URLRequestSummary main_frame = CreateURLRequestSummary(
768 1, "http://www.nike.com", "http://www.nike.com", 726 1, "http://www.nike.com", "http://www.nike.com",
769 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 727 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
770 predictor_->RecordURLRequest(main_frame);
771 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
772 728
773 URLRequestSummary resource1 = CreateURLRequestSummary( 729 URLRequestSummary resource1 = CreateURLRequestSummary(
774 1, "http://www.nike.com", "http://nike.com/style1.css", 730 1, "http://www.nike.com", "http://nike.com/style1.css",
775 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 731 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
776 predictor_->RecordURLResponse(resource1);
777 URLRequestSummary resource2 = CreateURLRequestSummary( 732 URLRequestSummary resource2 = CreateURLRequestSummary(
778 1, "http://www.nike.com", "http://nike.com/image2.png", 733 1, "http://www.nike.com", "http://nike.com/image2.png",
779 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 734 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
780 predictor_->RecordURLResponse(resource2); 735
736 auto page_summary = CreatePageRequestSummary(
737 "http://www.nike.com", "http://www.nike.com", {resource1, resource2});
781 738
782 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 739 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
783 EXPECT_CALL(mock_observer, 740 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
784 OnNavigationLearned(
785 kVisitCount, CreatePageRequestSummary(
786 "http://www.nike.com", "http://www.nike.com",
787 {resource1, resource2})));
788 741
789 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 742 predictor_->RecordPageRequestSummary(
743 base::MakeUnique<PageRequestSummary>(page_summary));
790 profile_->BlockUntilHistoryProcessesPendingRequests(); 744 profile_->BlockUntilHistoryProcessesPendingRequests();
791 745
792 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); 746 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/");
793 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css", 747 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css",
794 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 748 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
795 net::MEDIUM, false, false); 749 net::MEDIUM, false, false);
796 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png", 750 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png",
797 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, 751 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0,
798 net::MEDIUM, false, false); 752 net::MEDIUM, false, false);
799 PrefetchDataMap expected_url_resource_data = test_url_data_; 753 PrefetchDataMap expected_url_resource_data = test_url_data_;
(...skipping 30 matching lines...) Expand all
830 OriginDataMap expected_origin_data = test_origin_data_; 784 OriginDataMap expected_origin_data = test_origin_data_;
831 expected_origin_data.erase("google.com"); 785 expected_origin_data.erase("google.com");
832 expected_origin_data["www.nike.com"] = origin_data; 786 expected_origin_data["www.nike.com"] = origin_data;
833 EXPECT_EQ(mock_tables_->origin_table_.data_, expected_origin_data); 787 EXPECT_EQ(mock_tables_->origin_table_.data_, expected_origin_data);
834 } 788 }
835 789
836 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { 790 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) {
837 const int kVisitCount = 4; 791 const int kVisitCount = 4;
838 AddUrlToHistory("https://facebook.com/google", kVisitCount); 792 AddUrlToHistory("https://facebook.com/google", kVisitCount);
839 793
840 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); 794 auto page_summary = CreatePageRequestSummary(
841 predictor_->RecordURLRequest(fb1); 795 "https://facebook.com/google", "http://fb.com/google",
842 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 796 std::vector<URLRequestSummary>());
843
844 URLRequestSummary fb2 = CreateRedirectRequestSummary(
845 1, "http://fb.com/google", "http://facebook.com/google");
846 predictor_->RecordURLRedirect(fb2);
847 URLRequestSummary fb3 = CreateRedirectRequestSummary(
848 1, "http://facebook.com/google", "https://facebook.com/google");
849 predictor_->RecordURLRedirect(fb3);
850 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
851 797
852 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 798 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
853 EXPECT_CALL( 799 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
854 mock_observer,
855 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
856 "https://facebook.com/google",
857 "http://fb.com/google",
858 std::vector<URLRequestSummary>())));
859 800
860 predictor_->RecordMainFrameLoadComplete(fb_end); 801 predictor_->RecordPageRequestSummary(
802 base::MakeUnique<PageRequestSummary>(page_summary));
861 profile_->BlockUntilHistoryProcessesPendingRequests(); 803 profile_->BlockUntilHistoryProcessesPendingRequests();
862 804
863 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); 805 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google");
864 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), 806 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
865 "https://facebook.com/google", 1, 0, 0); 807 "https://facebook.com/google", 1, 0, 0);
866 EXPECT_EQ( 808 EXPECT_EQ(
867 mock_tables_->url_redirect_table_.data_, 809 mock_tables_->url_redirect_table_.data_,
868 RedirectDataMap({{url_redirect_data.primary_key(), url_redirect_data}})); 810 RedirectDataMap({{url_redirect_data.primary_key(), url_redirect_data}}));
869 811
870 RedirectData host_redirect_data = CreateRedirectData("fb.com"); 812 RedirectData host_redirect_data = CreateRedirectData("fb.com");
871 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), 813 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(),
872 "facebook.com", 1, 0, 0); 814 "facebook.com", 1, 0, 0);
873 EXPECT_EQ(mock_tables_->host_redirect_table_.data_, 815 EXPECT_EQ(mock_tables_->host_redirect_table_.data_,
874 RedirectDataMap( 816 RedirectDataMap(
875 {{host_redirect_data.primary_key(), host_redirect_data}})); 817 {{host_redirect_data.primary_key(), host_redirect_data}}));
876 } 818 }
877 819
878 // Tests that redirect is recorded correctly for URL already present in 820 // Tests that redirect is recorded correctly for URL already present in
879 // the database cache. 821 // the database cache.
880 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { 822 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) {
881 const int kVisitCount = 7; 823 const int kVisitCount = 7;
882 AddUrlToHistory("https://facebook.com/google", kVisitCount); 824 AddUrlToHistory("https://facebook.com/google", kVisitCount);
883 825
884 mock_tables_->url_redirect_table_.data_ = test_url_redirect_data_; 826 mock_tables_->url_redirect_table_.data_ = test_url_redirect_data_;
885 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_; 827 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_;
886 828
887 ResetPredictor(); 829 ResetPredictor();
888 InitializePredictor(); 830 InitializePredictor();
889 831
890 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); 832 auto page_summary = CreatePageRequestSummary(
891 predictor_->RecordURLRequest(fb1); 833 "https://facebook.com/google", "http://fb.com/google",
892 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 834 std::vector<URLRequestSummary>());
893
894 URLRequestSummary fb2 = CreateRedirectRequestSummary(
895 1, "http://fb.com/google", "http://facebook.com/google");
896 predictor_->RecordURLRedirect(fb2);
897 URLRequestSummary fb3 = CreateRedirectRequestSummary(
898 1, "http://facebook.com/google", "https://facebook.com/google");
899 predictor_->RecordURLRedirect(fb3);
900 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
901 835
902 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 836 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
903 EXPECT_CALL( 837 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
904 mock_observer,
905 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
906 "https://facebook.com/google",
907 "http://fb.com/google",
908 std::vector<URLRequestSummary>())));
909 838
910 predictor_->RecordMainFrameLoadComplete(fb_end); 839 predictor_->RecordPageRequestSummary(
840 base::MakeUnique<PageRequestSummary>(page_summary));
911 profile_->BlockUntilHistoryProcessesPendingRequests(); 841 profile_->BlockUntilHistoryProcessesPendingRequests();
912 842
913 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); 843 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google");
914 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), 844 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
915 "https://facebook.com/google", 6, 1, 0); 845 "https://facebook.com/google", 6, 1, 0);
916 // Existing redirect to https://facebook.com/login will be deleted because of 846 // Existing redirect to https://facebook.com/login will be deleted because of
917 // too many consecutive misses. 847 // too many consecutive misses.
918 RedirectDataMap expected_url_redirect_data = test_url_redirect_data_; 848 RedirectDataMap expected_url_redirect_data = test_url_redirect_data_;
919 expected_url_redirect_data[url_redirect_data.primary_key()] = 849 expected_url_redirect_data[url_redirect_data.primary_key()] =
920 url_redirect_data; 850 url_redirect_data;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1146 EXPECT_EQ(mock_tables_->manifest_table_.data_, manifests); 1076 EXPECT_EQ(mock_tables_->manifest_table_.data_, manifests);
1147 1077
1148 predictor_->DeleteAllUrls(); 1078 predictor_->DeleteAllUrls();
1149 EXPECT_TRUE(mock_tables_->url_resource_table_.data_.empty()); 1079 EXPECT_TRUE(mock_tables_->url_resource_table_.data_.empty());
1150 EXPECT_TRUE(mock_tables_->host_resource_table_.data_.empty()); 1080 EXPECT_TRUE(mock_tables_->host_resource_table_.data_.empty());
1151 EXPECT_TRUE(mock_tables_->url_redirect_table_.data_.empty()); 1081 EXPECT_TRUE(mock_tables_->url_redirect_table_.data_.empty());
1152 EXPECT_TRUE(mock_tables_->host_redirect_table_.data_.empty()); 1082 EXPECT_TRUE(mock_tables_->host_redirect_table_.data_.empty());
1153 EXPECT_TRUE(mock_tables_->manifest_table_.data_.empty()); 1083 EXPECT_TRUE(mock_tables_->manifest_table_.data_.empty());
1154 } 1084 }
1155 1085
1156 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) { 1086 // TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
alexilin 2017/06/30 14:58:49 Don't forget to remove these comments.
trevordixon 2017/07/11 11:08:08 Done.
1157 URLRequestSummary summary1 = CreateURLRequestSummary( 1087 // net::HttpResponseInfo response_info;
1158 1, "http://www.google.com", "http://www.google.com", 1088 // response_info.headers =
1159 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1089 // MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1160 URLRequestSummary summary2 = CreateURLRequestSummary( 1090 // response_info.was_cached = true;
1161 2, "http://www.google.com", "http://www.google.com", 1091 // url_request_job_factory_.set_response_info(response_info);
1162 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1163 URLRequestSummary summary3 = CreateURLRequestSummary(
1164 3, "http://www.yahoo.com", "http://www.yahoo.com",
1165 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1166 1092
1167 predictor_->OnMainFrameRequest(summary1); 1093 // GURL url("http://www.google.com/cat.png");
1168 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1094 // std::unique_ptr<net::URLRequest> request =
1169 predictor_->OnMainFrameRequest(summary2); 1095 // CreateURLRequest(url_request_context_, url, net::MEDIUM,
1170 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1096 // content::RESOURCE_TYPE_IMAGE, true);
1171 predictor_->OnMainFrameRequest(summary3); 1097 // URLRequestSummary summary;
1172 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 1098 // EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1099 // EXPECT_EQ(url, summary.resource_url);
1100 // EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1101 // EXPECT_TRUE(summary.was_cached);
1102 // EXPECT_FALSE(summary.has_validators);
1103 // EXPECT_FALSE(summary.always_revalidate);
1173 1104
1174 // Insert another with same navigation id. It should replace. 1105 // // Navigation_id elements should be unset by default.
1175 URLRequestSummary summary4 = CreateURLRequestSummary( 1106 // EXPECT_EQ(-1, summary.navigation_id.tab_id);
1176 1, "http://www.nike.com", "http://www.nike.com", 1107 // EXPECT_EQ(GURL(), summary.navigation_id.main_frame_url);
1177 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1108 // }
1178 URLRequestSummary summary5 = CreateURLRequestSummary(
1179 2, "http://www.google.com", "http://www.google.com",
1180 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1181 1109
1182 predictor_->OnMainFrameRequest(summary4); 1110 // TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseContentType) {
1183 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 1111 // net::HttpResponseInfo response_info;
1112 // response_info.headers = MakeResponseHeaders(
1113 // "HTTP/1.1 200 OK\n\n"
1114 // "Some: Headers\n"
1115 // "Content-Type: image/whatever\n");
1116 // url_request_job_factory_.set_response_info(response_info);
1117 // url_request_job_factory_.set_mime_type("image/png");
1184 1118
1185 // Change this creation time so that it will go away on the next insert. 1119 // std::unique_ptr<net::URLRequest> request = CreateURLRequest(
1186 summary5.navigation_id.creation_time = base::TimeTicks::Now() - 1120 // url_request_context_, GURL("http://www.google.com/cat.png"),
1187 base::TimeDelta::FromDays(1); 1121 // net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true);
1188 predictor_->OnMainFrameRequest(summary5); 1122 // URLRequestSummary summary;
1189 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 1123 // EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1124 // EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1125 // }
1190 1126
1191 URLRequestSummary summary6 = CreateURLRequestSummary( 1127 // TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseCachePolicy) {
1192 4, "http://www.shoes.com", "http://www.shoes.com", 1128 // net::HttpResponseInfo response_info;
1193 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1129 // response_info.headers = MakeResponseHeaders(
1194 predictor_->OnMainFrameRequest(summary6); 1130 // "HTTP/1.1 200 OK\n"
1195 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 1131 // "Some: Headers\n");
1132 // url_request_job_factory_.set_response_info(response_info);
1196 1133
1197 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) != 1134 // std::unique_ptr<net::URLRequest> request_no_validators = CreateURLRequest(
1198 predictor_->inflight_navigations_.end()); 1135 // url_request_context_, GURL("http://www.google.com/cat.png"),
1199 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) != 1136 // net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true);
1200 predictor_->inflight_navigations_.end());
1201 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) !=
1202 predictor_->inflight_navigations_.end());
1203 }
1204 1137
1205 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) { 1138 // URLRequestSummary summary;
1206 URLRequestSummary yahoo = CreateURLRequestSummary(1, "http://yahoo.com"); 1139 // EXPECT_TRUE(
1140 // URLRequestSummary::SummarizeResponse(*request_no_validators,
1141 // &summary));
1142 // EXPECT_FALSE(summary.has_validators);
1207 1143
1208 URLRequestSummary bbc1 = CreateURLRequestSummary(2, "http://bbc.com"); 1144 // response_info.headers = MakeResponseHeaders(
1209 URLRequestSummary bbc2 = 1145 // "HTTP/1.1 200 OK\n"
1210 CreateRedirectRequestSummary(2, "http://bbc.com", "https://www.bbc.com"); 1146 // "ETag: \"Cr66\"\n"
1211 NavigationID bbc_end = CreateNavigationID(2, "https://www.bbc.com"); 1147 // "Cache-Control: no-cache\n");
1212 1148 // url_request_job_factory_.set_response_info(response_info);
1213 URLRequestSummary youtube1 = CreateURLRequestSummary(3, "http://youtube.com"); 1149 // std::unique_ptr<net::URLRequest> request_etag = CreateURLRequest(
1214 URLRequestSummary youtube2 = CreateRedirectRequestSummary( 1150 // url_request_context_, GURL("http://www.google.com/cat.png"),
1215 3, "http://youtube.com", "https://youtube.com"); 1151 // net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, true);
1216 NavigationID youtube_end = CreateNavigationID(3, "https://youtube.com"); 1152 // EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1217 1153 // EXPECT_TRUE(summary.has_validators);
1218 URLRequestSummary nyt1 = CreateURLRequestSummary(4, "http://nyt.com"); 1154 // EXPECT_TRUE(summary.always_revalidate);
1219 URLRequestSummary nyt2 = 1155 // }
1220 CreateRedirectRequestSummary(4, "http://nyt.com", "http://nytimes.com");
1221 URLRequestSummary nyt3 = CreateRedirectRequestSummary(4, "http://nytimes.com",
1222 "http://m.nytimes.com");
1223 NavigationID nyt_end = CreateNavigationID(4, "http://m.nytimes.com");
1224
1225 URLRequestSummary fb1 = CreateURLRequestSummary(5, "http://fb.com");
1226 URLRequestSummary fb2 =
1227 CreateRedirectRequestSummary(5, "http://fb.com", "http://facebook.com");
1228 URLRequestSummary fb3 = CreateRedirectRequestSummary(5, "http://facebook.com",
1229 "https://facebook.com");
1230 URLRequestSummary fb4 = CreateRedirectRequestSummary(
1231 5, "https://facebook.com",
1232 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1233 NavigationID fb_end = CreateNavigationID(
1234 5,
1235 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1236
1237 // Redirect with empty redirect_url will be deleted.
1238 predictor_->OnMainFrameRequest(yahoo);
1239 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1240 predictor_->OnMainFrameRedirect(yahoo);
1241 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
1242
1243 // Redirect without previous request works fine.
1244 // predictor_->OnMainFrameRequest(bbc1) missing.
1245 predictor_->OnMainFrameRedirect(bbc2);
1246 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1247 EXPECT_EQ(bbc1.navigation_id.main_frame_url,
1248 predictor_->inflight_navigations_[bbc_end]->initial_url);
1249
1250 // http://youtube.com -> https://youtube.com.
1251 predictor_->OnMainFrameRequest(youtube1);
1252 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1253 predictor_->OnMainFrameRedirect(youtube2);
1254 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1255 EXPECT_EQ(youtube1.navigation_id.main_frame_url,
1256 predictor_->inflight_navigations_[youtube_end]->initial_url);
1257
1258 // http://nyt.com -> http://nytimes.com -> http://m.nytimes.com.
1259 predictor_->OnMainFrameRequest(nyt1);
1260 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1261 predictor_->OnMainFrameRedirect(nyt2);
1262 predictor_->OnMainFrameRedirect(nyt3);
1263 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1264 EXPECT_EQ(nyt1.navigation_id.main_frame_url,
1265 predictor_->inflight_navigations_[nyt_end]->initial_url);
1266
1267 // http://fb.com -> http://facebook.com -> https://facebook.com ->
1268 // https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr.
1269 predictor_->OnMainFrameRequest(fb1);
1270 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1271 predictor_->OnMainFrameRedirect(fb2);
1272 predictor_->OnMainFrameRedirect(fb3);
1273 predictor_->OnMainFrameRedirect(fb4);
1274 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1275 EXPECT_EQ(fb1.navigation_id.main_frame_url,
1276 predictor_->inflight_navigations_[fb_end]->initial_url);
1277 }
1278
1279 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) {
1280 // If there is no inflight navigation, nothing happens.
1281 URLRequestSummary resource1 = CreateURLRequestSummary(
1282 1, "http://www.google.com", "http://google.com/style1.css",
1283 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
1284 predictor_->OnSubresourceResponse(resource1);
1285 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
1286
1287 // Add an inflight navigation.
1288 URLRequestSummary main_frame1 = CreateURLRequestSummary(
1289 1, "http://www.google.com", "http://www.google.com",
1290 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1291 predictor_->OnMainFrameRequest(main_frame1);
1292 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1293
1294 // Now add a few subresources.
1295 URLRequestSummary resource2 = CreateURLRequestSummary(
1296 1, "http://www.google.com", "http://google.com/script1.js",
1297 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1298 URLRequestSummary resource3 = CreateURLRequestSummary(
1299 1, "http://www.google.com", "http://google.com/script2.js",
1300 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1301 predictor_->OnSubresourceResponse(resource1);
1302 predictor_->OnSubresourceResponse(resource2);
1303 predictor_->OnSubresourceResponse(resource3);
1304
1305 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1306 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id]
1307 ->subresource_requests.size());
1308 EXPECT_EQ(resource1,
1309 predictor_->inflight_navigations_[main_frame1.navigation_id]
1310 ->subresource_requests[0]);
1311 EXPECT_EQ(resource2,
1312 predictor_->inflight_navigations_[main_frame1.navigation_id]
1313 ->subresource_requests[1]);
1314 EXPECT_EQ(resource3,
1315 predictor_->inflight_navigations_[main_frame1.navigation_id]
1316 ->subresource_requests[2]);
1317 }
1318
1319 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
1320 net::HttpResponseInfo response_info;
1321 response_info.headers =
1322 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1323 response_info.was_cached = true;
1324 url_request_job_factory_.set_response_info(response_info);
1325
1326 GURL url("http://www.google.com/cat.png");
1327 std::unique_ptr<net::URLRequest> request =
1328 CreateURLRequest(url_request_context_, url, net::MEDIUM,
1329 content::RESOURCE_TYPE_IMAGE, true);
1330 URLRequestSummary summary;
1331 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1332 EXPECT_EQ(url, summary.resource_url);
1333 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1334 EXPECT_TRUE(summary.was_cached);
1335 EXPECT_FALSE(summary.has_validators);
1336 EXPECT_FALSE(summary.always_revalidate);
1337
1338 // Navigation_id elements should be unset by default.
1339 EXPECT_EQ(-1, summary.navigation_id.tab_id);
1340 EXPECT_EQ(GURL(), summary.navigation_id.main_frame_url);
1341 }
1342
1343 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseContentType) {
1344 net::HttpResponseInfo response_info;
1345 response_info.headers = MakeResponseHeaders(
1346 "HTTP/1.1 200 OK\n\n"
1347 "Some: Headers\n"
1348 "Content-Type: image/whatever\n");
1349 url_request_job_factory_.set_response_info(response_info);
1350 url_request_job_factory_.set_mime_type("image/png");
1351
1352 std::unique_ptr<net::URLRequest> request = CreateURLRequest(
1353 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM,
1354 content::RESOURCE_TYPE_PREFETCH, true);
1355 URLRequestSummary summary;
1356 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1357 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1358 }
1359
1360 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseCachePolicy) {
1361 net::HttpResponseInfo response_info;
1362 response_info.headers = MakeResponseHeaders(
1363 "HTTP/1.1 200 OK\n"
1364 "Some: Headers\n");
1365 url_request_job_factory_.set_response_info(response_info);
1366
1367 std::unique_ptr<net::URLRequest> request_no_validators = CreateURLRequest(
1368 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM,
1369 content::RESOURCE_TYPE_PREFETCH, true);
1370
1371 URLRequestSummary summary;
1372 EXPECT_TRUE(
1373 URLRequestSummary::SummarizeResponse(*request_no_validators, &summary));
1374 EXPECT_FALSE(summary.has_validators);
1375
1376 response_info.headers = MakeResponseHeaders(
1377 "HTTP/1.1 200 OK\n"
1378 "ETag: \"Cr66\"\n"
1379 "Cache-Control: no-cache\n");
1380 url_request_job_factory_.set_response_info(response_info);
1381 std::unique_ptr<net::URLRequest> request_etag = CreateURLRequest(
1382 url_request_context_, GURL("http://www.google.com/cat.png"), net::MEDIUM,
1383 content::RESOURCE_TYPE_PREFETCH, true);
1384 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1385 EXPECT_TRUE(summary.has_validators);
1386 EXPECT_TRUE(summary.always_revalidate);
1387 }
1388 1156
1389 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) { 1157 TEST_F(ResourcePrefetchPredictorTest, PopulatePrefetcherRequest) {
1390 // The data that will be used in populating. 1158 // The data that will be used in populating.
1391 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1); 1159 PrefetchData google = CreatePrefetchData("http://www.google.com/", 1);
1392 InitializeResourceData(google.add_resources(), "http://google.com/image1.png", 1160 InitializeResourceData(google.add_resources(), "http://google.com/image1.png",
1393 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2, 1161 content::RESOURCE_TYPE_IMAGE, 10, 0, 0, 2.2,
1394 net::MEDIUM, false, false); // good 1162 net::MEDIUM, false, false); // good
1395 InitializeResourceData(google.add_resources(), "http://google.com/style1.css", 1163 InitializeResourceData(google.add_resources(), "http://google.com/style1.css",
1396 content::RESOURCE_TYPE_STYLESHEET, 2, 2, 1, 1.0, 1164 content::RESOURCE_TYPE_STYLESHEET, 2, 2, 1, 1.0,
1397 net::MEDIUM, false, false); // still good 1165 net::MEDIUM, false, false); // still good
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
1680 predictor_->origin_data_->UpdateData(www_google.host(), www_google); 1448 predictor_->origin_data_->UpdateData(www_google.host(), www_google);
1681 1449
1682 prediction = base::MakeUnique<PreconnectPrediction>(); 1450 prediction = base::MakeUnique<PreconnectPrediction>();
1683 EXPECT_TRUE( 1451 EXPECT_TRUE(
1684 predictor_->PredictPreconnectOrigins(main_frame_url, prediction.get())); 1452 predictor_->PredictPreconnectOrigins(main_frame_url, prediction.get()));
1685 EXPECT_EQ(*prediction, CreatePreconnectPrediction("www.google.com", true, 1453 EXPECT_EQ(*prediction, CreatePreconnectPrediction("www.google.com", true,
1686 {GURL(gen_origin(4))}, 1454 {GURL(gen_origin(4))},
1687 std::vector<GURL>())); 1455 std::vector<GURL>()));
1688 } 1456 }
1689 1457
1690 TEST_F(ResourcePrefetchPredictorTest, TestRecordFirstContentfulPaint) {
trevordixon 2017/06/27 21:29:46 Still need to test that fcp is recorded in db.
1691 auto res1_time = base::TimeTicks::FromInternalValue(1);
1692 auto res2_time = base::TimeTicks::FromInternalValue(2);
1693 auto fcp_time = base::TimeTicks::FromInternalValue(3);
1694 auto res3_time = base::TimeTicks::FromInternalValue(4);
1695
1696 URLRequestSummary main_frame =
1697 CreateURLRequestSummary(1, "http://www.google.com");
1698 predictor_->RecordURLRequest(main_frame);
1699 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1700
1701 URLRequestSummary resource1 = CreateURLRequestSummary(
1702 1, "http://www.google.com", "http://google.com/style1.css",
1703 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
1704 resource1.response_time = res1_time;
1705 predictor_->RecordURLResponse(resource1);
1706 URLRequestSummary resource2 = CreateURLRequestSummary(
1707 1, "http://www.google.com", "http://google.com/script1.js",
1708 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1709 resource2.response_time = res2_time;
1710 predictor_->RecordURLResponse(resource2);
1711 URLRequestSummary resource3 = CreateURLRequestSummary(
1712 1, "http://www.google.com", "http://google.com/script2.js",
1713 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1714 resource3.response_time = res3_time;
1715 predictor_->RecordURLResponse(resource3);
1716
1717 predictor_->RecordFirstContentfulPaint(main_frame.navigation_id, fcp_time);
1718
1719 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
1720 profile_->BlockUntilHistoryProcessesPendingRequests();
1721
1722 PrefetchData host_data = CreatePrefetchData("www.google.com");
1723 InitializeResourceData(host_data.add_resources(),
1724 "http://google.com/style1.css",
1725 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
1726 net::MEDIUM, false, false);
1727 InitializeResourceData(
1728 host_data.add_resources(), "http://google.com/script1.js",
1729 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false);
1730 ResourceData* resource3_rd = host_data.add_resources();
1731 InitializeResourceData(resource3_rd, "http://google.com/script2.js",
1732 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0,
1733 net::MEDIUM, false, false);
1734 resource3_rd->set_before_first_contentful_paint(false);
1735 EXPECT_EQ(mock_tables_->host_resource_table_.data_,
1736 PrefetchDataMap({{host_data.primary_key(), host_data}}));
1737 }
1738
1739 } // namespace predictors 1458 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698