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

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: Undo unneeded added mock class. Created 3 years, 6 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 17 matching lines...) Expand all
28 #include "net/url_request/url_request_context.h" 28 #include "net/url_request/url_request_context.h"
29 #include "net/url_request/url_request_job.h" 29 #include "net/url_request/url_request_job.h"
30 #include "testing/gmock/include/gmock/gmock.h" 30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
32 32
33 using testing::StrictMock; 33 using testing::StrictMock;
34 using testing::UnorderedElementsAre; 34 using testing::UnorderedElementsAre;
35 35
36 namespace predictors { 36 namespace predictors {
37 37
38 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
39 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
40 using PrefetchDataMap = std::map<std::string, PrefetchData>; 38 using PrefetchDataMap = std::map<std::string, PrefetchData>;
41 using RedirectDataMap = std::map<std::string, RedirectData>; 39 using RedirectDataMap = std::map<std::string, RedirectData>;
42 using OriginDataMap = std::map<std::string, OriginData>; 40 using OriginDataMap = std::map<std::string, OriginData>;
43 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>; 41 using ManifestDataMap = std::map<std::string, precache::PrecacheManifest>;
44 42
45 template <typename T>
46 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> {
47 public:
48 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {}
49 void GetAllData(std::map<std::string, T>* data_map,
50 sql::Connection* db) const override {
51 *data_map = data_;
52 }
53 void UpdateData(const std::string& key,
54 const T& data,
55 sql::Connection* db) override {
56 data_[key] = data;
57 }
58 void DeleteData(const std::vector<std::string>& keys,
59 sql::Connection* db) override {
60 for (const auto& key : keys)
61 data_.erase(key);
62 }
63 void DeleteAllData(sql::Connection* db) override { data_.clear(); }
64
65 std::map<std::string, T> data_;
66 };
67
68 class MockResourcePrefetchPredictorTables
69 : public ResourcePrefetchPredictorTables {
70 public:
71 MockResourcePrefetchPredictorTables() = default;
72
73 void ScheduleDBTask(const tracked_objects::Location& from_here,
74 DBTask task) override {
75 ExecuteDBTaskOnDBThread(std::move(task));
76 }
77
78 void ExecuteDBTaskOnDBThread(DBTask task) override {
79 std::move(task).Run(nullptr);
80 }
81
82 GlowplugKeyValueTable<PrefetchData>* url_resource_table() override {
83 return &url_resource_table_;
84 }
85
86 GlowplugKeyValueTable<RedirectData>* url_redirect_table() override {
87 return &url_redirect_table_;
88 }
89
90 GlowplugKeyValueTable<PrefetchData>* host_resource_table() override {
91 return &host_resource_table_;
92 }
93
94 GlowplugKeyValueTable<RedirectData>* host_redirect_table() override {
95 return &host_redirect_table_;
96 }
97
98 GlowplugKeyValueTable<precache::PrecacheManifest>* manifest_table() override {
99 return &manifest_table_;
100 }
101
102 GlowplugKeyValueTable<OriginData>* origin_table() override {
103 return &origin_table_;
104 }
105
106 FakeGlowplugKeyValueTable<PrefetchData> url_resource_table_;
107 FakeGlowplugKeyValueTable<RedirectData> url_redirect_table_;
108 FakeGlowplugKeyValueTable<PrefetchData> host_resource_table_;
109 FakeGlowplugKeyValueTable<RedirectData> host_redirect_table_;
110 FakeGlowplugKeyValueTable<precache::PrecacheManifest> manifest_table_;
111 FakeGlowplugKeyValueTable<OriginData> origin_table_;
112
113 protected:
114 ~MockResourcePrefetchPredictorTables() override = default;
115 };
116
117 class MockResourcePrefetchPredictorObserver : public TestObserver {
118 public:
119 explicit MockResourcePrefetchPredictorObserver(
120 ResourcePrefetchPredictor* predictor)
121 : TestObserver(predictor) {}
122
123 MOCK_METHOD2(
124 OnNavigationLearned,
125 void(size_t url_visit_count,
126 const ResourcePrefetchPredictor::PageRequestSummary& summary));
127 };
128
129 class ResourcePrefetchPredictorTest : public testing::Test { 43 class ResourcePrefetchPredictorTest : public testing::Test {
130 public: 44 public:
131 ResourcePrefetchPredictorTest(); 45 ResourcePrefetchPredictorTest();
132 ~ResourcePrefetchPredictorTest() override; 46 ~ResourcePrefetchPredictorTest() override;
133 void SetUp() override; 47 void SetUp() override;
134 void TearDown() override; 48 void TearDown() override;
135 49
136 protected: 50 protected:
137 void AddUrlToHistory(const std::string& url, int visit_count) { 51 void AddUrlToHistory(const std::string& url, int visit_count) {
138 HistoryServiceFactory::GetForProfile(profile_.get(), 52 HistoryServiceFactory::GetForProfile(profile_.get(),
139 ServiceAccessType::EXPLICIT_ACCESS)-> 53 ServiceAccessType::EXPLICIT_ACCESS)->
140 AddPageWithDetails( 54 AddPageWithDetails(
141 GURL(url), 55 GURL(url),
142 base::string16(), 56 base::string16(),
143 visit_count, 57 visit_count,
144 0, 58 0,
145 base::Time::Now(), 59 base::Time::Now(),
146 false, 60 false,
147 history::SOURCE_BROWSED); 61 history::SOURCE_BROWSED);
148 profile_->BlockUntilHistoryProcessesPendingRequests(); 62 profile_->BlockUntilHistoryProcessesPendingRequests();
149 } 63 }
150 64
151 URLRequestSummary CreateRedirectRequestSummary(
152 SessionID::id_type session_id,
153 const std::string& main_frame_url,
154 const std::string& redirect_url) {
155 URLRequestSummary summary =
156 CreateURLRequestSummary(session_id, main_frame_url);
157 summary.redirect_url = GURL(redirect_url);
158 return summary;
159 }
160
161 void InitializePredictor() { 65 void InitializePredictor() {
162 loading_predictor_->StartInitialization(); 66 loading_predictor_->StartInitialization();
163 base::RunLoop loop; 67 base::RunLoop loop;
164 loop.RunUntilIdle(); // Runs the DB lookup. 68 loop.RunUntilIdle(); // Runs the DB lookup.
165 profile_->BlockUntilHistoryProcessesPendingRequests(); 69 profile_->BlockUntilHistoryProcessesPendingRequests();
166 } 70 }
167 71
168 void ResetPredictor(bool small_db = true) { 72 void ResetPredictor(bool small_db = true) {
169 LoadingPredictorConfig config; 73 LoadingPredictorConfig config;
170 PopulateTestConfig(&config, small_db); 74 PopulateTestConfig(&config, small_db);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 114
211 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); 115 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
212 profile_->BlockUntilHistoryProcessesPendingRequests(); 116 profile_->BlockUntilHistoryProcessesPendingRequests();
213 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( 117 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
214 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); 118 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS));
215 // Initialize the predictor with empty data. 119 // Initialize the predictor with empty data.
216 ResetPredictor(); 120 ResetPredictor();
217 EXPECT_EQ(predictor_->initialization_state_, 121 EXPECT_EQ(predictor_->initialization_state_,
218 ResourcePrefetchPredictor::NOT_INITIALIZED); 122 ResourcePrefetchPredictor::NOT_INITIALIZED);
219 InitializePredictor(); 123 InitializePredictor();
220 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
221 EXPECT_EQ(predictor_->initialization_state_, 124 EXPECT_EQ(predictor_->initialization_state_,
222 ResourcePrefetchPredictor::INITIALIZED); 125 ResourcePrefetchPredictor::INITIALIZED);
223 126
224 url_request_job_factory_.Reset(); 127 url_request_job_factory_.Reset();
225 url_request_context_.set_job_factory(&url_request_job_factory_); 128 url_request_context_.set_job_factory(&url_request_job_factory_);
226 129
227 histogram_tester_.reset(new base::HistogramTester()); 130 histogram_tester_.reset(new base::HistogramTester());
228 } 131 }
229 132
230 void ResourcePrefetchPredictorTest::TearDown() { 133 void ResourcePrefetchPredictorTest::TearDown() {
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_; 323 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_;
421 mock_tables_->manifest_table_.data_ = test_manifest_data_; 324 mock_tables_->manifest_table_.data_ = test_manifest_data_;
422 mock_tables_->origin_table_.data_ = test_origin_data_; 325 mock_tables_->origin_table_.data_ = test_origin_data_;
423 326
424 ResetPredictor(); 327 ResetPredictor();
425 InitializePredictor(); 328 InitializePredictor();
426 329
427 // Test that the internal variables correctly initialized. 330 // Test that the internal variables correctly initialized.
428 EXPECT_EQ(predictor_->initialization_state_, 331 EXPECT_EQ(predictor_->initialization_state_,
429 ResourcePrefetchPredictor::INITIALIZED); 332 ResourcePrefetchPredictor::INITIALIZED);
430 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
431 333
432 // Integrity of the cache and the backend storage is checked on TearDown. 334 // Integrity of the cache and the backend storage is checked on TearDown.
433 } 335 }
434 336
435 // Single navigation but history count is low, so should not record url data. 337 // Single navigation but history count is low, so should not record url data.
436 TEST_F(ResourcePrefetchPredictorTest, NavigationLowHistoryCount) { 338 TEST_F(ResourcePrefetchPredictorTest, NavigationLowHistoryCount) {
437 const int kVisitCount = 1; 339 const int kVisitCount = 1;
438 AddUrlToHistory("https://www.google.com", kVisitCount); 340 AddUrlToHistory("https://www.google.com", kVisitCount);
439 341
440 URLRequestSummary main_frame = 342 URLRequestSummary main_frame =
441 CreateURLRequestSummary(1, "http://www.google.com"); 343 CreateURLRequestSummary(1, "http://www.google.com");
442 predictor_->RecordURLRequest(main_frame);
443 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
444 344
445 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary( 345 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary(
446 1, "http://www.google.com", "https://www.google.com"); 346 1, "http://www.google.com", "https://www.google.com");
447 predictor_->RecordURLRedirect(main_frame_redirect);
448 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
449 main_frame = CreateURLRequestSummary(1, "https://www.google.com"); 347 main_frame = CreateURLRequestSummary(1, "https://www.google.com");
450 348
451 // Now add a few subresources. 349 // Now add a few subresources.
452 URLRequestSummary resource1 = CreateURLRequestSummary( 350 URLRequestSummary resource1 = CreateURLRequestSummary(
453 1, "https://www.google.com", "https://google.com/style1.css", 351 1, "https://www.google.com", "https://google.com/style1.css",
454 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 352 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
455 predictor_->RecordURLResponse(resource1);
456 URLRequestSummary resource2 = CreateURLRequestSummary( 353 URLRequestSummary resource2 = CreateURLRequestSummary(
457 1, "https://www.google.com", "https://google.com/script1.js", 354 1, "https://www.google.com", "https://google.com/script1.js",
458 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 355 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
459 predictor_->RecordURLResponse(resource2);
460 URLRequestSummary resource3 = CreateURLRequestSummary( 356 URLRequestSummary resource3 = CreateURLRequestSummary(
461 1, "https://www.google.com", "https://google.com/script2.js", 357 1, "https://www.google.com", "https://google.com/script2.js",
462 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 358 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
463 predictor_->RecordURLResponse(resource3); 359
360 auto page_summary = CreatePageRequestSummary(
361 "https://www.google.com", "http://www.google.com",
362 {resource1, resource2, resource3});
464 363
465 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 364 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
466 EXPECT_CALL( 365 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
467 mock_observer,
468 OnNavigationLearned(kVisitCount,
469 CreatePageRequestSummary(
470 "https://www.google.com", "http://www.google.com",
471 {resource1, resource2, resource3})));
472 366
473 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 367 predictor_->RecordPageRequestSummary(
368 base::MakeUnique<PageRequestSummary>(page_summary));
474 profile_->BlockUntilHistoryProcessesPendingRequests(); 369 profile_->BlockUntilHistoryProcessesPendingRequests();
475 370
476 PrefetchData host_data = CreatePrefetchData("www.google.com"); 371 PrefetchData host_data = CreatePrefetchData("www.google.com");
477 InitializeResourceData(host_data.add_resources(), 372 InitializeResourceData(host_data.add_resources(),
478 "https://google.com/style1.css", 373 "https://google.com/style1.css",
479 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 374 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
480 net::MEDIUM, false, false); 375 net::MEDIUM, false, false);
481 InitializeResourceData( 376 InitializeResourceData(
482 host_data.add_resources(), "https://google.com/script1.js", 377 host_data.add_resources(), "https://google.com/script1.js",
483 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false); 378 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false);
(...skipping 18 matching lines...) Expand all
502 } 397 }
503 398
504 // Single navigation that will be recorded. Will check for duplicate 399 // Single navigation that will be recorded. Will check for duplicate
505 // resources and also for number of resources saved. 400 // resources and also for number of resources saved.
506 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 401 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
507 const int kVisitCount = 4; 402 const int kVisitCount = 4;
508 AddUrlToHistory("http://www.google.com", kVisitCount); 403 AddUrlToHistory("http://www.google.com", kVisitCount);
509 404
510 URLRequestSummary main_frame = 405 URLRequestSummary main_frame =
511 CreateURLRequestSummary(1, "http://www.google.com"); 406 CreateURLRequestSummary(1, "http://www.google.com");
512 predictor_->RecordURLRequest(main_frame);
513 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
514 407
515 std::vector<URLRequestSummary> resources; 408 std::vector<URLRequestSummary> resources;
516 resources.push_back(CreateURLRequestSummary( 409 resources.push_back(CreateURLRequestSummary(
517 1, "http://www.google.com", "http://google.com/style1.css", 410 1, "http://www.google.com", "http://google.com/style1.css",
518 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); 411 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false));
519 predictor_->RecordURLResponse(resources.back());
520 resources.push_back(CreateURLRequestSummary( 412 resources.push_back(CreateURLRequestSummary(
521 1, "http://www.google.com", "http://google.com/script1.js", 413 1, "http://www.google.com", "http://google.com/script1.js",
522 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 414 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
523 predictor_->RecordURLResponse(resources.back());
524 resources.push_back(CreateURLRequestSummary( 415 resources.push_back(CreateURLRequestSummary(
525 1, "http://www.google.com", "http://google.com/script2.js", 416 1, "http://www.google.com", "http://google.com/script2.js",
526 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 417 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
527 predictor_->RecordURLResponse(resources.back());
528 resources.push_back(CreateURLRequestSummary( 418 resources.push_back(CreateURLRequestSummary(
529 1, "http://www.google.com", "http://google.com/script1.js", 419 1, "http://www.google.com", "http://google.com/script1.js",
530 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); 420 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true));
531 predictor_->RecordURLResponse(resources.back());
532 resources.push_back(CreateURLRequestSummary( 421 resources.push_back(CreateURLRequestSummary(
533 1, "http://www.google.com", "http://google.com/image1.png", 422 1, "http://www.google.com", "http://google.com/image1.png",
534 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 423 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
535 predictor_->RecordURLResponse(resources.back());
536 resources.push_back(CreateURLRequestSummary( 424 resources.push_back(CreateURLRequestSummary(
537 1, "http://www.google.com", "http://google.com/image2.png", 425 1, "http://www.google.com", "http://google.com/image2.png",
538 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 426 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
539 predictor_->RecordURLResponse(resources.back());
540 resources.push_back(CreateURLRequestSummary( 427 resources.push_back(CreateURLRequestSummary(
541 1, "http://www.google.com", "http://google.com/style2.css", 428 1, "http://www.google.com", "http://google.com/style2.css",
542 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 429 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
543 predictor_->RecordURLResponse(resources.back());
544 430
545 auto no_store = CreateURLRequestSummary( 431 auto no_store = CreateURLRequestSummary(
546 1, "http://www.google.com", 432 1, "http://www.google.com",
547 "http://static.google.com/style2-no-store.css", 433 "http://static.google.com/style2-no-store.css",
548 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 434 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
549 no_store.is_no_store = true; 435 no_store.is_no_store = true;
550 predictor_->RecordURLResponse(no_store);
551 436
552 auto redirected = CreateURLRequestSummary( 437 auto redirected = CreateURLRequestSummary(
553 1, "http://www.google.com", "http://reader.google.com/style.css", 438 1, "http://www.google.com", "http://reader.google.com/style.css",
554 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 439 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
555 redirected.redirect_url = GURL("http://dev.null.google.com/style.css"); 440 redirected.redirect_url = GURL("http://dev.null.google.com/style.css");
556 441
557 predictor_->RecordURLRedirect(redirected); 442 auto page_summary = CreatePageRequestSummary(
443 "http://www.google.com", "http://www.google.com", resources);
444 page_summary.UpdateOrAddToOrigins(no_store);
445 page_summary.UpdateOrAddToOrigins(redirected);
446
558 redirected.is_no_store = true; 447 redirected.is_no_store = true;
559 redirected.request_url = redirected.redirect_url; 448 redirected.request_url = redirected.redirect_url;
560 redirected.redirect_url = GURL(); 449 redirected.redirect_url = GURL();
561 450 page_summary.UpdateOrAddToOrigins(redirected);
562 predictor_->RecordURLResponse(redirected);
563 451
564 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 452 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
565 EXPECT_CALL(mock_observer, 453 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
566 OnNavigationLearned(
567 kVisitCount, CreatePageRequestSummary("http://www.google.com",
568 "http://www.google.com",
569 resources)));
570 454
571 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 455 predictor_->RecordPageRequestSummary(
456 base::MakeUnique<PageRequestSummary>(page_summary));
572 profile_->BlockUntilHistoryProcessesPendingRequests(); 457 profile_->BlockUntilHistoryProcessesPendingRequests();
573 458
574 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 459 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
575 InitializeResourceData(url_data.add_resources(), 460 InitializeResourceData(url_data.add_resources(),
576 "http://google.com/style1.css", 461 "http://google.com/style1.css",
577 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 462 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
578 net::MEDIUM, false, false); 463 net::MEDIUM, false, false);
579 InitializeResourceData(url_data.add_resources(), 464 InitializeResourceData(url_data.add_resources(),
580 "http://google.com/style2.css", 465 "http://google.com/style2.css",
581 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, 466 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 514
630 mock_tables_->url_resource_table_.data_ = test_url_data_; 515 mock_tables_->url_resource_table_.data_ = test_url_data_;
631 mock_tables_->host_resource_table_.data_ = test_host_data_; 516 mock_tables_->host_resource_table_.data_ = test_host_data_;
632 517
633 ResetPredictor(); 518 ResetPredictor();
634 InitializePredictor(); 519 InitializePredictor();
635 520
636 URLRequestSummary main_frame = CreateURLRequestSummary( 521 URLRequestSummary main_frame = CreateURLRequestSummary(
637 1, "http://www.google.com", "http://www.google.com", 522 1, "http://www.google.com", "http://www.google.com",
638 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 523 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
639 predictor_->RecordURLRequest(main_frame);
640 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
641 524
642 std::vector<URLRequestSummary> resources; 525 std::vector<URLRequestSummary> resources;
643 resources.push_back(CreateURLRequestSummary( 526 resources.push_back(CreateURLRequestSummary(
644 1, "http://www.google.com", "http://google.com/style1.css", 527 1, "http://www.google.com", "http://google.com/style1.css",
645 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false)); 528 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false));
646 predictor_->RecordURLResponse(resources.back());
647 resources.push_back(CreateURLRequestSummary( 529 resources.push_back(CreateURLRequestSummary(
648 1, "http://www.google.com", "http://google.com/script1.js", 530 1, "http://www.google.com", "http://google.com/script1.js",
649 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 531 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
650 predictor_->RecordURLResponse(resources.back());
651 resources.push_back(CreateURLRequestSummary( 532 resources.push_back(CreateURLRequestSummary(
652 1, "http://www.google.com", "http://google.com/script2.js", 533 1, "http://www.google.com", "http://google.com/script2.js",
653 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false)); 534 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false));
654 predictor_->RecordURLResponse(resources.back());
655 resources.push_back(CreateURLRequestSummary( 535 resources.push_back(CreateURLRequestSummary(
656 1, "http://www.google.com", "http://google.com/script1.js", 536 1, "http://www.google.com", "http://google.com/script1.js",
657 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true)); 537 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", true));
658 predictor_->RecordURLResponse(resources.back());
659 resources.push_back(CreateURLRequestSummary( 538 resources.push_back(CreateURLRequestSummary(
660 1, "http://www.google.com", "http://google.com/image1.png", 539 1, "http://www.google.com", "http://google.com/image1.png",
661 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 540 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
662 predictor_->RecordURLResponse(resources.back());
663 resources.push_back(CreateURLRequestSummary( 541 resources.push_back(CreateURLRequestSummary(
664 1, "http://www.google.com", "http://google.com/image2.png", 542 1, "http://www.google.com", "http://google.com/image2.png",
665 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false)); 543 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false));
666 predictor_->RecordURLResponse(resources.back());
667 resources.push_back(CreateURLRequestSummary( 544 resources.push_back(CreateURLRequestSummary(
668 1, "http://www.google.com", "http://google.com/style2.css", 545 1, "http://www.google.com", "http://google.com/style2.css",
669 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true)); 546 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true));
670 predictor_->RecordURLResponse(resources.back());
671 auto no_store = CreateURLRequestSummary( 547 auto no_store = CreateURLRequestSummary(
672 1, "http://www.google.com", 548 1, "http://www.google.com",
673 "http://static.google.com/style2-no-store.css", 549 "http://static.google.com/style2-no-store.css",
674 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 550 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
675 no_store.is_no_store = true; 551 no_store.is_no_store = true;
676 predictor_->RecordURLResponse(no_store); 552
553 auto page_summary = CreatePageRequestSummary(
554 "http://www.google.com", "http://www.google.com", resources);
555 page_summary.UpdateOrAddToOrigins(no_store);
677 556
678 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 557 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
679 EXPECT_CALL(mock_observer, 558 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
680 OnNavigationLearned(
681 kVisitCount, CreatePageRequestSummary("http://www.google.com",
682 "http://www.google.com",
683 resources)));
684 559
685 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 560 predictor_->RecordPageRequestSummary(
561 base::MakeUnique<PageRequestSummary>(page_summary));
686 profile_->BlockUntilHistoryProcessesPendingRequests(); 562 profile_->BlockUntilHistoryProcessesPendingRequests();
687 563
688 PrefetchData url_data = CreatePrefetchData("http://www.google.com/"); 564 PrefetchData url_data = CreatePrefetchData("http://www.google.com/");
689 InitializeResourceData(url_data.add_resources(), 565 InitializeResourceData(url_data.add_resources(),
690 "http://google.com/style1.css", 566 "http://google.com/style1.css",
691 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, 567 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0,
692 net::MEDIUM, false, false); 568 net::MEDIUM, false, false);
693 InitializeResourceData(url_data.add_resources(), 569 InitializeResourceData(url_data.add_resources(),
694 "http://google.com/style2.css", 570 "http://google.com/style2.css",
695 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, 571 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 mock_tables_->url_resource_table_.data_ = test_url_data_; 634 mock_tables_->url_resource_table_.data_ = test_url_data_;
759 mock_tables_->host_resource_table_.data_ = test_host_data_; 635 mock_tables_->host_resource_table_.data_ = test_host_data_;
760 mock_tables_->origin_table_.data_ = test_origin_data_; 636 mock_tables_->origin_table_.data_ = test_origin_data_;
761 637
762 ResetPredictor(); 638 ResetPredictor();
763 InitializePredictor(); 639 InitializePredictor();
764 640
765 URLRequestSummary main_frame = CreateURLRequestSummary( 641 URLRequestSummary main_frame = CreateURLRequestSummary(
766 1, "http://www.nike.com", "http://www.nike.com", 642 1, "http://www.nike.com", "http://www.nike.com",
767 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 643 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
768 predictor_->RecordURLRequest(main_frame);
769 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
770 644
771 URLRequestSummary resource1 = CreateURLRequestSummary( 645 URLRequestSummary resource1 = CreateURLRequestSummary(
772 1, "http://www.nike.com", "http://nike.com/style1.css", 646 1, "http://www.nike.com", "http://nike.com/style1.css",
773 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 647 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
774 predictor_->RecordURLResponse(resource1);
775 URLRequestSummary resource2 = CreateURLRequestSummary( 648 URLRequestSummary resource2 = CreateURLRequestSummary(
776 1, "http://www.nike.com", "http://nike.com/image2.png", 649 1, "http://www.nike.com", "http://nike.com/image2.png",
777 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 650 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
778 predictor_->RecordURLResponse(resource2); 651
652 auto page_summary = CreatePageRequestSummary(
653 "http://www.nike.com", "http://www.nike.com", {resource1, resource2});
779 654
780 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 655 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
781 EXPECT_CALL(mock_observer, 656 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
782 OnNavigationLearned(
783 kVisitCount, CreatePageRequestSummary(
784 "http://www.nike.com", "http://www.nike.com",
785 {resource1, resource2})));
786 657
787 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id); 658 predictor_->RecordPageRequestSummary(
659 base::MakeUnique<PageRequestSummary>(page_summary));
788 profile_->BlockUntilHistoryProcessesPendingRequests(); 660 profile_->BlockUntilHistoryProcessesPendingRequests();
789 661
790 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/"); 662 PrefetchData url_data = CreatePrefetchData("http://www.nike.com/");
791 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css", 663 InitializeResourceData(url_data.add_resources(), "http://nike.com/style1.css",
792 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, 664 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
793 net::MEDIUM, false, false); 665 net::MEDIUM, false, false);
794 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png", 666 InitializeResourceData(url_data.add_resources(), "http://nike.com/image2.png",
795 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, 667 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0,
796 net::MEDIUM, false, false); 668 net::MEDIUM, false, false);
797 PrefetchDataMap expected_url_resource_data = test_url_data_; 669 PrefetchDataMap expected_url_resource_data = test_url_data_;
(...skipping 30 matching lines...) Expand all
828 OriginDataMap expected_origin_data = test_origin_data_; 700 OriginDataMap expected_origin_data = test_origin_data_;
829 expected_origin_data.erase("google.com"); 701 expected_origin_data.erase("google.com");
830 expected_origin_data["www.nike.com"] = origin_data; 702 expected_origin_data["www.nike.com"] = origin_data;
831 EXPECT_EQ(mock_tables_->origin_table_.data_, expected_origin_data); 703 EXPECT_EQ(mock_tables_->origin_table_.data_, expected_origin_data);
832 } 704 }
833 705
834 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) { 706 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) {
835 const int kVisitCount = 4; 707 const int kVisitCount = 4;
836 AddUrlToHistory("https://facebook.com/google", kVisitCount); 708 AddUrlToHistory("https://facebook.com/google", kVisitCount);
837 709
838 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); 710 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google");
alexilin 2017/06/23 16:55:05 nit: fb1, fb2, fb3, fb_end are not used. Feel free
trevordixon 2017/06/27 21:28:55 Done.
839 predictor_->RecordURLRequest(fb1);
840 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
841 711
842 URLRequestSummary fb2 = CreateRedirectRequestSummary( 712 URLRequestSummary fb2 = CreateRedirectRequestSummary(
843 1, "http://fb.com/google", "http://facebook.com/google"); 713 1, "http://fb.com/google", "http://facebook.com/google");
844 predictor_->RecordURLRedirect(fb2);
845 URLRequestSummary fb3 = CreateRedirectRequestSummary( 714 URLRequestSummary fb3 = CreateRedirectRequestSummary(
846 1, "http://facebook.com/google", "https://facebook.com/google"); 715 1, "http://facebook.com/google", "https://facebook.com/google");
847 predictor_->RecordURLRedirect(fb3);
848 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google"); 716 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
849 717
718 auto page_summary = CreatePageRequestSummary(
719 "https://facebook.com/google", "http://fb.com/google",
720 std::vector<URLRequestSummary>());
721
850 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 722 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
851 EXPECT_CALL( 723 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
852 mock_observer,
853 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
854 "https://facebook.com/google",
855 "http://fb.com/google",
856 std::vector<URLRequestSummary>())));
857 724
858 predictor_->RecordMainFrameLoadComplete(fb_end); 725 predictor_->RecordPageRequestSummary(
726 base::MakeUnique<PageRequestSummary>(page_summary));
859 profile_->BlockUntilHistoryProcessesPendingRequests(); 727 profile_->BlockUntilHistoryProcessesPendingRequests();
860 728
861 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); 729 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google");
862 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), 730 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
863 "https://facebook.com/google", 1, 0, 0); 731 "https://facebook.com/google", 1, 0, 0);
864 EXPECT_EQ( 732 EXPECT_EQ(
865 mock_tables_->url_redirect_table_.data_, 733 mock_tables_->url_redirect_table_.data_,
866 RedirectDataMap({{url_redirect_data.primary_key(), url_redirect_data}})); 734 RedirectDataMap({{url_redirect_data.primary_key(), url_redirect_data}}));
867 735
868 RedirectData host_redirect_data = CreateRedirectData("fb.com"); 736 RedirectData host_redirect_data = CreateRedirectData("fb.com");
869 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), 737 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(),
870 "facebook.com", 1, 0, 0); 738 "facebook.com", 1, 0, 0);
871 EXPECT_EQ(mock_tables_->host_redirect_table_.data_, 739 EXPECT_EQ(mock_tables_->host_redirect_table_.data_,
872 RedirectDataMap( 740 RedirectDataMap(
873 {{host_redirect_data.primary_key(), host_redirect_data}})); 741 {{host_redirect_data.primary_key(), host_redirect_data}}));
874 } 742 }
875 743
876 // Tests that redirect is recorded correctly for URL already present in 744 // Tests that redirect is recorded correctly for URL already present in
877 // the database cache. 745 // the database cache.
878 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { 746 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) {
879 const int kVisitCount = 7; 747 const int kVisitCount = 7;
880 AddUrlToHistory("https://facebook.com/google", kVisitCount); 748 AddUrlToHistory("https://facebook.com/google", kVisitCount);
881 749
882 mock_tables_->url_redirect_table_.data_ = test_url_redirect_data_; 750 mock_tables_->url_redirect_table_.data_ = test_url_redirect_data_;
883 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_; 751 mock_tables_->host_redirect_table_.data_ = test_host_redirect_data_;
884 752
885 ResetPredictor(); 753 ResetPredictor();
886 InitializePredictor(); 754 InitializePredictor();
887 755
888 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); 756 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google");
alexilin 2017/06/23 16:55:05 nit: ditto
trevordixon 2017/06/27 21:28:55 Done.
889 predictor_->RecordURLRequest(fb1);
890 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
891 757
892 URLRequestSummary fb2 = CreateRedirectRequestSummary( 758 URLRequestSummary fb2 = CreateRedirectRequestSummary(
893 1, "http://fb.com/google", "http://facebook.com/google"); 759 1, "http://fb.com/google", "http://facebook.com/google");
894 predictor_->RecordURLRedirect(fb2);
895 URLRequestSummary fb3 = CreateRedirectRequestSummary( 760 URLRequestSummary fb3 = CreateRedirectRequestSummary(
896 1, "http://facebook.com/google", "https://facebook.com/google"); 761 1, "http://facebook.com/google", "https://facebook.com/google");
897 predictor_->RecordURLRedirect(fb3);
898 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google"); 762 NavigationID fb_end = CreateNavigationID(1, "https://facebook.com/google");
899 763
764 auto page_summary = CreatePageRequestSummary(
765 "https://facebook.com/google", "http://fb.com/google",
766 std::vector<URLRequestSummary>());
767
900 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_); 768 StrictMock<MockResourcePrefetchPredictorObserver> mock_observer(predictor_);
901 EXPECT_CALL( 769 EXPECT_CALL(mock_observer, OnNavigationLearned(kVisitCount, page_summary));
902 mock_observer,
903 OnNavigationLearned(kVisitCount, CreatePageRequestSummary(
904 "https://facebook.com/google",
905 "http://fb.com/google",
906 std::vector<URLRequestSummary>())));
907 770
908 predictor_->RecordMainFrameLoadComplete(fb_end); 771 predictor_->RecordPageRequestSummary(
772 base::MakeUnique<PageRequestSummary>(page_summary));
909 profile_->BlockUntilHistoryProcessesPendingRequests(); 773 profile_->BlockUntilHistoryProcessesPendingRequests();
910 774
911 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google"); 775 RedirectData url_redirect_data = CreateRedirectData("http://fb.com/google");
912 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(), 776 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
913 "https://facebook.com/google", 6, 1, 0); 777 "https://facebook.com/google", 6, 1, 0);
914 // Existing redirect to https://facebook.com/login will be deleted because of 778 // Existing redirect to https://facebook.com/login will be deleted because of
915 // too many consecutive misses. 779 // too many consecutive misses.
916 RedirectDataMap expected_url_redirect_data = test_url_redirect_data_; 780 RedirectDataMap expected_url_redirect_data = test_url_redirect_data_;
917 expected_url_redirect_data[url_redirect_data.primary_key()] = 781 expected_url_redirect_data[url_redirect_data.primary_key()] =
918 url_redirect_data; 782 url_redirect_data;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 EXPECT_EQ(mock_tables_->manifest_table_.data_, manifests); 1008 EXPECT_EQ(mock_tables_->manifest_table_.data_, manifests);
1145 1009
1146 predictor_->DeleteAllUrls(); 1010 predictor_->DeleteAllUrls();
1147 EXPECT_TRUE(mock_tables_->url_resource_table_.data_.empty()); 1011 EXPECT_TRUE(mock_tables_->url_resource_table_.data_.empty());
1148 EXPECT_TRUE(mock_tables_->host_resource_table_.data_.empty()); 1012 EXPECT_TRUE(mock_tables_->host_resource_table_.data_.empty());
1149 EXPECT_TRUE(mock_tables_->url_redirect_table_.data_.empty()); 1013 EXPECT_TRUE(mock_tables_->url_redirect_table_.data_.empty());
1150 EXPECT_TRUE(mock_tables_->host_redirect_table_.data_.empty()); 1014 EXPECT_TRUE(mock_tables_->host_redirect_table_.data_.empty());
1151 EXPECT_TRUE(mock_tables_->manifest_table_.data_.empty()); 1015 EXPECT_TRUE(mock_tables_->manifest_table_.data_.empty());
1152 } 1016 }
1153 1017
1154 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) {
1155 URLRequestSummary summary1 = CreateURLRequestSummary(
1156 1, "http://www.google.com", "http://www.google.com",
1157 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1158 URLRequestSummary summary2 = CreateURLRequestSummary(
1159 2, "http://www.google.com", "http://www.google.com",
1160 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1161 URLRequestSummary summary3 = CreateURLRequestSummary(
1162 3, "http://www.yahoo.com", "http://www.yahoo.com",
1163 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1164
1165 predictor_->OnMainFrameRequest(summary1);
1166 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1167 predictor_->OnMainFrameRequest(summary2);
1168 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1169 predictor_->OnMainFrameRequest(summary3);
1170 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1171
1172 // Insert another with same navigation id. It should replace.
1173 URLRequestSummary summary4 = CreateURLRequestSummary(
1174 1, "http://www.nike.com", "http://www.nike.com",
1175 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1176 URLRequestSummary summary5 = CreateURLRequestSummary(
1177 2, "http://www.google.com", "http://www.google.com",
1178 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1179
1180 predictor_->OnMainFrameRequest(summary4);
1181 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1182
1183 // Change this creation time so that it will go away on the next insert.
1184 summary5.navigation_id.creation_time = base::TimeTicks::Now() -
1185 base::TimeDelta::FromDays(1);
1186 predictor_->OnMainFrameRequest(summary5);
1187 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1188
1189 URLRequestSummary summary6 = CreateURLRequestSummary(
1190 4, "http://www.shoes.com", "http://www.shoes.com",
1191 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1192 predictor_->OnMainFrameRequest(summary6);
1193 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1194
1195 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) !=
1196 predictor_->inflight_navigations_.end());
1197 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) !=
1198 predictor_->inflight_navigations_.end());
1199 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) !=
1200 predictor_->inflight_navigations_.end());
1201 }
1202
1203 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) {
1204 URLRequestSummary yahoo = CreateURLRequestSummary(1, "http://yahoo.com");
1205
1206 URLRequestSummary bbc1 = CreateURLRequestSummary(2, "http://bbc.com");
1207 URLRequestSummary bbc2 =
1208 CreateRedirectRequestSummary(2, "http://bbc.com", "https://www.bbc.com");
1209 NavigationID bbc_end = CreateNavigationID(2, "https://www.bbc.com");
1210
1211 URLRequestSummary youtube1 = CreateURLRequestSummary(3, "http://youtube.com");
1212 URLRequestSummary youtube2 = CreateRedirectRequestSummary(
1213 3, "http://youtube.com", "https://youtube.com");
1214 NavigationID youtube_end = CreateNavigationID(3, "https://youtube.com");
1215
1216 URLRequestSummary nyt1 = CreateURLRequestSummary(4, "http://nyt.com");
1217 URLRequestSummary nyt2 =
1218 CreateRedirectRequestSummary(4, "http://nyt.com", "http://nytimes.com");
1219 URLRequestSummary nyt3 = CreateRedirectRequestSummary(4, "http://nytimes.com",
1220 "http://m.nytimes.com");
1221 NavigationID nyt_end = CreateNavigationID(4, "http://m.nytimes.com");
1222
1223 URLRequestSummary fb1 = CreateURLRequestSummary(5, "http://fb.com");
1224 URLRequestSummary fb2 =
1225 CreateRedirectRequestSummary(5, "http://fb.com", "http://facebook.com");
1226 URLRequestSummary fb3 = CreateRedirectRequestSummary(5, "http://facebook.com",
1227 "https://facebook.com");
1228 URLRequestSummary fb4 = CreateRedirectRequestSummary(
1229 5, "https://facebook.com",
1230 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1231 NavigationID fb_end = CreateNavigationID(
1232 5,
1233 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1234
1235 // Redirect with empty redirect_url will be deleted.
1236 predictor_->OnMainFrameRequest(yahoo);
1237 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1238 predictor_->OnMainFrameRedirect(yahoo);
1239 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
1240
1241 // Redirect without previous request works fine.
1242 // predictor_->OnMainFrameRequest(bbc1) missing.
1243 predictor_->OnMainFrameRedirect(bbc2);
1244 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1245 EXPECT_EQ(bbc1.navigation_id.main_frame_url,
1246 predictor_->inflight_navigations_[bbc_end]->initial_url);
1247
1248 // http://youtube.com -> https://youtube.com.
1249 predictor_->OnMainFrameRequest(youtube1);
1250 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1251 predictor_->OnMainFrameRedirect(youtube2);
1252 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1253 EXPECT_EQ(youtube1.navigation_id.main_frame_url,
1254 predictor_->inflight_navigations_[youtube_end]->initial_url);
1255
1256 // http://nyt.com -> http://nytimes.com -> http://m.nytimes.com.
1257 predictor_->OnMainFrameRequest(nyt1);
1258 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1259 predictor_->OnMainFrameRedirect(nyt2);
1260 predictor_->OnMainFrameRedirect(nyt3);
1261 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1262 EXPECT_EQ(nyt1.navigation_id.main_frame_url,
1263 predictor_->inflight_navigations_[nyt_end]->initial_url);
1264
1265 // http://fb.com -> http://facebook.com -> https://facebook.com ->
1266 // https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr.
1267 predictor_->OnMainFrameRequest(fb1);
1268 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1269 predictor_->OnMainFrameRedirect(fb2);
1270 predictor_->OnMainFrameRedirect(fb3);
1271 predictor_->OnMainFrameRedirect(fb4);
1272 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1273 EXPECT_EQ(fb1.navigation_id.main_frame_url,
1274 predictor_->inflight_navigations_[fb_end]->initial_url);
1275 }
1276
1277 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) {
1278 // If there is no inflight navigation, nothing happens.
1279 URLRequestSummary resource1 = CreateURLRequestSummary(
1280 1, "http://www.google.com", "http://google.com/style1.css",
1281 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
1282 predictor_->OnSubresourceResponse(resource1);
1283 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
1284
1285 // Add an inflight navigation.
1286 URLRequestSummary main_frame1 = CreateURLRequestSummary(
1287 1, "http://www.google.com", "http://www.google.com",
1288 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1289 predictor_->OnMainFrameRequest(main_frame1);
1290 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1291
1292 // Now add a few subresources.
1293 URLRequestSummary resource2 = CreateURLRequestSummary(
1294 1, "http://www.google.com", "http://google.com/script1.js",
1295 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1296 URLRequestSummary resource3 = CreateURLRequestSummary(
1297 1, "http://www.google.com", "http://google.com/script2.js",
1298 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1299 predictor_->OnSubresourceResponse(resource1);
1300 predictor_->OnSubresourceResponse(resource2);
1301 predictor_->OnSubresourceResponse(resource3);
1302
1303 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1304 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id]
1305 ->subresource_requests.size());
1306 EXPECT_EQ(resource1,
1307 predictor_->inflight_navigations_[main_frame1.navigation_id]
1308 ->subresource_requests[0]);
1309 EXPECT_EQ(resource2,
1310 predictor_->inflight_navigations_[main_frame1.navigation_id]
1311 ->subresource_requests[1]);
1312 EXPECT_EQ(resource3,
1313 predictor_->inflight_navigations_[main_frame1.navigation_id]
1314 ->subresource_requests[2]);
1315 }
1316
1317 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) { 1018 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
alexilin 2017/06/23 16:55:05 nit: LoadingDataCollectorTest is the better place
trevordixon 2017/06/27 21:28:55 Done.
1318 net::HttpResponseInfo response_info; 1019 net::HttpResponseInfo response_info;
1319 response_info.headers = 1020 response_info.headers =
1320 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); 1021 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1321 response_info.was_cached = true; 1022 response_info.was_cached = true;
1322 url_request_job_factory_.set_response_info(response_info); 1023 url_request_job_factory_.set_response_info(response_info);
1323 1024
1324 GURL url("http://www.google.com/cat.png"); 1025 GURL url("http://www.google.com/cat.png");
1325 std::unique_ptr<net::URLRequest> request = 1026 std::unique_ptr<net::URLRequest> request =
1326 CreateURLRequest(url_request_context_, url, net::MEDIUM, 1027 CreateURLRequest(url_request_context_, url, net::MEDIUM,
1327 content::RESOURCE_TYPE_IMAGE, true); 1028 content::RESOURCE_TYPE_IMAGE, true);
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
1623 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1, 1324 content::RESOURCE_TYPE_FONT_RESOURCE, 10, 0, 1, 2.1,
1624 net::MEDIUM, false, false); 1325 net::MEDIUM, false, false);
1625 predictor_->url_resource_data_->UpdateData(www_google_url.primary_key(), 1326 predictor_->url_resource_data_->UpdateData(www_google_url.primary_key(),
1626 www_google_url); 1327 www_google_url);
1627 1328
1628 urls.clear(); 1329 urls.clear();
1629 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction)); 1330 EXPECT_TRUE(predictor_->GetPrefetchData(main_frame_url, &prediction));
1630 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url))); 1331 EXPECT_THAT(urls, UnorderedElementsAre(GURL(font_url)));
1631 } 1332 }
1632 1333
1633 TEST_F(ResourcePrefetchPredictorTest, TestRecordFirstContentfulPaint) {
alexilin 2017/06/23 16:55:05 nit: Could you add some simple test to check that
trevordixon 2017/06/27 21:28:55 Still working on this.
1634 auto res1_time = base::TimeTicks::FromInternalValue(1);
1635 auto res2_time = base::TimeTicks::FromInternalValue(2);
1636 auto fcp_time = base::TimeTicks::FromInternalValue(3);
1637 auto res3_time = base::TimeTicks::FromInternalValue(4);
1638
1639 URLRequestSummary main_frame =
1640 CreateURLRequestSummary(1, "http://www.google.com");
1641 predictor_->RecordURLRequest(main_frame);
1642 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1643
1644 URLRequestSummary resource1 = CreateURLRequestSummary(
1645 1, "http://www.google.com", "http://google.com/style1.css",
1646 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
1647 resource1.response_time = res1_time;
1648 predictor_->RecordURLResponse(resource1);
1649 URLRequestSummary resource2 = CreateURLRequestSummary(
1650 1, "http://www.google.com", "http://google.com/script1.js",
1651 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1652 resource2.response_time = res2_time;
1653 predictor_->RecordURLResponse(resource2);
1654 URLRequestSummary resource3 = CreateURLRequestSummary(
1655 1, "http://www.google.com", "http://google.com/script2.js",
1656 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
1657 resource3.response_time = res3_time;
1658 predictor_->RecordURLResponse(resource3);
1659
1660 predictor_->RecordFirstContentfulPaint(main_frame.navigation_id, fcp_time);
1661
1662 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
1663 profile_->BlockUntilHistoryProcessesPendingRequests();
1664
1665 PrefetchData host_data = CreatePrefetchData("www.google.com");
1666 InitializeResourceData(host_data.add_resources(),
1667 "http://google.com/style1.css",
1668 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0,
1669 net::MEDIUM, false, false);
1670 InitializeResourceData(
1671 host_data.add_resources(), "http://google.com/script1.js",
1672 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false);
1673 ResourceData* resource3_rd = host_data.add_resources();
1674 InitializeResourceData(resource3_rd, "http://google.com/script2.js",
1675 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0,
1676 net::MEDIUM, false, false);
1677 resource3_rd->set_before_first_contentful_paint(false);
1678 EXPECT_EQ(mock_tables_->host_resource_table_.data_,
1679 PrefetchDataMap({{host_data.primary_key(), host_data}}));
1680 }
1681
1682 } // namespace predictors 1334 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698