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 2355273002: Redirect handling in the resource_prefetch_predictor. (Closed)
Patch Set: Minor changes. Created 4 years, 2 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 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 20 matching lines...) Expand all
31 using testing::ContainerEq; 31 using testing::ContainerEq;
32 using testing::Pointee; 32 using testing::Pointee;
33 using testing::SetArgPointee; 33 using testing::SetArgPointee;
34 using testing::StrictMock; 34 using testing::StrictMock;
35 35
36 namespace predictors { 36 namespace predictors {
37 37
38 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; 38 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary;
39 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; 39 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData;
40 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; 40 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
41 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
41 42
42 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( 43 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders(
43 const char* headers) { 44 const char* headers) {
44 return make_scoped_refptr(new net::HttpResponseHeaders( 45 return make_scoped_refptr(new net::HttpResponseHeaders(
45 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); 46 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
46 } 47 }
47 48
48 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { 49 class EmptyURLRequestDelegate : public net::URLRequest::Delegate {
49 void OnResponseStarted(net::URLRequest* request) override {} 50 void OnResponseStarted(net::URLRequest* request) override {}
50 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} 51 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {}
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 private: 121 private:
121 net::HttpResponseInfo response_info_; 122 net::HttpResponseInfo response_info_;
122 std::string mime_type_; 123 std::string mime_type_;
123 }; 124 };
124 125
125 class MockResourcePrefetchPredictorTables 126 class MockResourcePrefetchPredictorTables
126 : public ResourcePrefetchPredictorTables { 127 : public ResourcePrefetchPredictorTables {
127 public: 128 public:
128 MockResourcePrefetchPredictorTables() { } 129 MockResourcePrefetchPredictorTables() { }
129 130
130 MOCK_METHOD2(GetAllData, void(PrefetchDataMap* url_data_map, 131 MOCK_METHOD4(GetAllData,
131 PrefetchDataMap* host_data_map)); 132 void(PrefetchDataMap* url_data_map,
132 MOCK_METHOD2(UpdateData, void(const PrefetchData& url_data, 133 PrefetchDataMap* host_data_map,
133 const PrefetchData& host_data)); 134 RedirectDataMap* url_redirect_data_map,
134 MOCK_METHOD2(DeleteData, void(const std::vector<std::string>& urls, 135 RedirectDataMap* host_redirect_data_map));
135 const std::vector<std::string>& hosts)); 136 MOCK_METHOD4(UpdateData,
136 MOCK_METHOD2(DeleteSingleDataPoint, void(const std::string& key, 137 void(const PrefetchData& url_data,
137 PrefetchKeyType key_type)); 138 const PrefetchData& host_data,
139 const RedirectData& url_redirect_data,
140 const RedirectData& host_redirect_data));
141 MOCK_METHOD2(DeleteResourceData,
142 void(const std::vector<std::string>& urls,
143 const std::vector<std::string>& hosts));
144 MOCK_METHOD2(DeleteSingleResourceDataPoint,
145 void(const std::string& key, PrefetchKeyType key_type));
146 MOCK_METHOD2(DeleteRedirectData,
147 void(const std::vector<std::string>& urls,
148 const std::vector<std::string>& hosts));
149 MOCK_METHOD2(DeleteSingleRedirectDataPoint,
150 void(const std::string& key, PrefetchKeyType key_type));
138 MOCK_METHOD0(DeleteAllData, void()); 151 MOCK_METHOD0(DeleteAllData, void());
139 152
140 protected: 153 protected:
141 ~MockResourcePrefetchPredictorTables() { } 154 ~MockResourcePrefetchPredictorTables() { }
142 }; 155 };
143 156
144 class ResourcePrefetchPredictorTest : public testing::Test { 157 class ResourcePrefetchPredictorTest : public testing::Test {
145 public: 158 public:
146 ResourcePrefetchPredictorTest(); 159 ResourcePrefetchPredictorTest();
147 ~ResourcePrefetchPredictorTest() override; 160 ~ResourcePrefetchPredictorTest() override;
(...skipping 17 matching lines...) Expand all
165 178
166 NavigationID CreateNavigationID(int process_id, 179 NavigationID CreateNavigationID(int process_id,
167 int render_frame_id, 180 int render_frame_id,
168 const std::string& main_frame_url) { 181 const std::string& main_frame_url) {
169 NavigationID navigation_id(process_id, render_frame_id, 182 NavigationID navigation_id(process_id, render_frame_id,
170 GURL(main_frame_url)); 183 GURL(main_frame_url));
171 navigation_id.creation_time = base::TimeTicks::Now(); 184 navigation_id.creation_time = base::TimeTicks::Now();
172 return navigation_id; 185 return navigation_id;
173 } 186 }
174 187
175 ResourcePrefetchPredictor::URLRequestSummary CreateURLRequestSummary( 188 URLRequestSummary CreateURLRequestSummary(
176 int process_id, 189 int process_id,
177 int render_frame_id, 190 int render_frame_id,
178 const std::string& main_frame_url, 191 const std::string& main_frame_url,
179 const std::string& resource_url, 192 const std::string& resource_url = std::string(),
180 content::ResourceType resource_type, 193 content::ResourceType resource_type = content::RESOURCE_TYPE_MAIN_FRAME,
181 net::RequestPriority priority, 194 net::RequestPriority priority = net::MEDIUM,
182 const std::string& mime_type, 195 const std::string& mime_type = std::string(),
183 bool was_cached) { 196 bool was_cached = false) {
184 ResourcePrefetchPredictor::URLRequestSummary summary; 197 URLRequestSummary summary;
185 summary.navigation_id = CreateNavigationID(process_id, render_frame_id, 198 summary.navigation_id = CreateNavigationID(process_id, render_frame_id,
186 main_frame_url); 199 main_frame_url);
187 summary.resource_url = GURL(resource_url); 200 summary.resource_url =
201 resource_url.empty() ? GURL(main_frame_url) : GURL(resource_url);
188 summary.resource_type = resource_type; 202 summary.resource_type = resource_type;
189 summary.priority = priority; 203 summary.priority = priority;
190 summary.mime_type = mime_type; 204 summary.mime_type = mime_type;
191 summary.was_cached = was_cached; 205 summary.was_cached = was_cached;
192 return summary; 206 return summary;
193 } 207 }
194 208
209 URLRequestSummary CreateRedirectRequestSummary(
210 int process_id,
211 int render_frame_id,
212 const std::string& main_frame_url,
213 const std::string& redirect_url) {
214 URLRequestSummary summary =
215 CreateURLRequestSummary(process_id, render_frame_id, main_frame_url);
216 summary.redirect_url = GURL(redirect_url);
217 return summary;
218 }
219
195 std::unique_ptr<net::URLRequest> CreateURLRequest( 220 std::unique_ptr<net::URLRequest> CreateURLRequest(
196 const GURL& url, 221 const GURL& url,
197 net::RequestPriority priority, 222 net::RequestPriority priority,
198 content::ResourceType resource_type, 223 content::ResourceType resource_type,
199 int render_process_id, 224 int render_process_id,
200 int render_frame_id, 225 int render_frame_id,
201 bool is_main_frame) { 226 bool is_main_frame) {
202 std::unique_ptr<net::URLRequest> request = 227 std::unique_ptr<net::URLRequest> request =
203 url_request_context_.CreateRequest(url, priority, 228 url_request_context_.CreateRequest(url, priority,
204 &url_request_delegate_); 229 &url_request_delegate_);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 content::TestBrowserThread ui_thread_; 272 content::TestBrowserThread ui_thread_;
248 content::TestBrowserThread db_thread_; 273 content::TestBrowserThread db_thread_;
249 std::unique_ptr<TestingProfile> profile_; 274 std::unique_ptr<TestingProfile> profile_;
250 net::TestURLRequestContext url_request_context_; 275 net::TestURLRequestContext url_request_context_;
251 276
252 std::unique_ptr<ResourcePrefetchPredictor> predictor_; 277 std::unique_ptr<ResourcePrefetchPredictor> predictor_;
253 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_; 278 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_;
254 279
255 PrefetchDataMap test_url_data_; 280 PrefetchDataMap test_url_data_;
256 PrefetchDataMap test_host_data_; 281 PrefetchDataMap test_host_data_;
282 RedirectDataMap test_url_redirect_data_;
283 RedirectDataMap test_host_redirect_data_;
257 PrefetchData empty_url_data_; 284 PrefetchData empty_url_data_;
258 PrefetchData empty_host_data_; 285 PrefetchData empty_host_data_;
286 RedirectData empty_url_redirect_data_;
287 RedirectData empty_host_redirect_data_;
259 288
260 MockURLRequestJobFactory url_request_job_factory_; 289 MockURLRequestJobFactory url_request_job_factory_;
261 EmptyURLRequestDelegate url_request_delegate_; 290 EmptyURLRequestDelegate url_request_delegate_;
262 }; 291 };
263 292
264 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() 293 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest()
265 : loop_(base::MessageLoop::TYPE_DEFAULT), 294 : loop_(base::MessageLoop::TYPE_DEFAULT),
266 ui_thread_(content::BrowserThread::UI, &loop_), 295 ui_thread_(content::BrowserThread::UI, &loop_),
267 db_thread_(content::BrowserThread::DB, &loop_), 296 db_thread_(content::BrowserThread::DB, &loop_),
268 profile_(new TestingProfile()), 297 profile_(new TestingProfile()),
269 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()), 298 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()),
270 empty_url_data_(PREFETCH_KEY_TYPE_URL, std::string()), 299 empty_url_data_(PREFETCH_KEY_TYPE_URL, std::string()),
271 empty_host_data_(PREFETCH_KEY_TYPE_HOST, std::string()) {} 300 empty_host_data_(PREFETCH_KEY_TYPE_HOST, std::string()),
301 empty_url_redirect_data_(),
302 empty_host_redirect_data_() {}
272 303
273 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() { 304 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() {
274 profile_.reset(NULL); 305 profile_.reset(NULL);
275 base::RunLoop().RunUntilIdle(); 306 base::RunLoop().RunUntilIdle();
276 } 307 }
277 308
278 void ResourcePrefetchPredictorTest::SetUp() { 309 void ResourcePrefetchPredictorTest::SetUp() {
279 InitializeSampleData(); 310 InitializeSampleData();
280 311
281 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); 312 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
282 profile_->BlockUntilHistoryProcessesPendingRequests(); 313 profile_->BlockUntilHistoryProcessesPendingRequests();
283 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( 314 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
284 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); 315 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS));
285 // Initialize the predictor with empty data. 316 // Initialize the predictor with empty data.
286 ResetPredictor(); 317 ResetPredictor();
287 EXPECT_EQ(predictor_->initialization_state_, 318 EXPECT_EQ(predictor_->initialization_state_,
288 ResourcePrefetchPredictor::NOT_INITIALIZED); 319 ResourcePrefetchPredictor::NOT_INITIALIZED);
289 EXPECT_CALL(*mock_tables_.get(), 320 EXPECT_CALL(*mock_tables_.get(),
290 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 321 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
291 Pointee(ContainerEq(PrefetchDataMap())))); 322 Pointee(ContainerEq(PrefetchDataMap())),
323 Pointee(ContainerEq(RedirectDataMap())),
324 Pointee(ContainerEq(RedirectDataMap()))));
292 InitializePredictor(); 325 InitializePredictor();
293 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 326 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
294 EXPECT_EQ(predictor_->initialization_state_, 327 EXPECT_EQ(predictor_->initialization_state_,
295 ResourcePrefetchPredictor::INITIALIZED); 328 ResourcePrefetchPredictor::INITIALIZED);
296 329
297 url_request_context_.set_job_factory(&url_request_job_factory_); 330 url_request_context_.set_job_factory(&url_request_job_factory_);
298 } 331 }
299 332
300 void ResourcePrefetchPredictorTest::TearDown() { 333 void ResourcePrefetchPredictorTest::TearDown() {
301 predictor_.reset(NULL); 334 predictor_.reset(NULL);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); 399 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com");
367 yahoo.last_visit = base::Time::FromInternalValue(5); 400 yahoo.last_visit = base::Time::FromInternalValue(5);
368 yahoo.resources.push_back(CreateResourceData( 401 yahoo.resources.push_back(CreateResourceData(
369 "http://google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 402 "http://google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, 0,
370 10.0, net::MEDIUM, false, false)); 403 10.0, net::MEDIUM, false, false));
371 404
372 test_host_data_.clear(); 405 test_host_data_.clear();
373 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); 406 test_host_data_.insert(std::make_pair("www.facebook.com", facebook));
374 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); 407 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo));
375 } 408 }
409
410 { // Url redirect data.
411 RedirectData facebook;
412 facebook.set_primary_key("http://fb.com/google");
413 facebook.set_last_visit_time(6);
414 InitializeRedirectStat(facebook.add_redirects(),
415 "https://facebook.com/google", 5, 1, 0);
416 InitializeRedirectStat(facebook.add_redirects(),
417 "https://facebook.com/login", 3, 5, 1);
418
419 RedirectData nytimes;
420 nytimes.set_primary_key("http://nyt.com");
421 nytimes.set_last_visit_time(7);
422 InitializeRedirectStat(nytimes.add_redirects(), "https://nytimes.com", 2, 0,
423 0);
424
425 RedirectData google;
426 google.set_primary_key("http://google.com");
427 google.set_last_visit_time(8);
428 InitializeRedirectStat(google.add_redirects(), "https://google.com", 3, 0,
429 0);
430
431 test_url_redirect_data_.clear();
432 test_url_redirect_data_.insert(
433 std::make_pair(facebook.primary_key(), facebook));
434 test_url_redirect_data_.insert(
435 std::make_pair(nytimes.primary_key(), nytimes));
436 test_url_redirect_data_.insert(
437 std::make_pair(google.primary_key(), google));
438 }
439
440 { // Host redirect data.
441 RedirectData bbc;
442 bbc.set_primary_key("bbc.com");
443 bbc.set_last_visit_time(9);
444 InitializeRedirectStat(bbc.add_redirects(), "www.bbc.com", 8, 4, 1);
445 InitializeRedirectStat(bbc.add_redirects(), "m.bbc.com", 5, 8, 0);
446 InitializeRedirectStat(bbc.add_redirects(), "bbc.co.uk", 1, 3, 0);
447
448 RedirectData microsoft;
449 microsoft.set_primary_key("microsoft.com");
450 microsoft.set_last_visit_time(10);
451 InitializeRedirectStat(microsoft.add_redirects(), "www.microsoft.com", 10,
452 0, 0);
453
454 test_host_redirect_data_.clear();
455 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc));
456 test_host_redirect_data_.insert(
457 std::make_pair(microsoft.primary_key(), microsoft));
458 }
376 } 459 }
377 460
378 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) { 461 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) {
379 // Tests that the predictor initializes correctly without any data. 462 // Tests that the predictor initializes correctly without any data.
380 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 463 EXPECT_TRUE(predictor_->url_table_cache_->empty());
381 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 464 EXPECT_TRUE(predictor_->host_table_cache_->empty());
465 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty());
466 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty());
382 } 467 }
383 468
384 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) { 469 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) {
385 // Tests that the history and the db tables data are loaded correctly. 470 // Tests that the history and the db tables data are loaded correctly.
386 AddUrlToHistory("http://www.google.com/", 4); 471 AddUrlToHistory("http://www.google.com/", 4);
387 AddUrlToHistory("http://www.yahoo.com/", 2); 472 AddUrlToHistory("http://www.yahoo.com/", 2);
388 473
389 EXPECT_CALL(*mock_tables_.get(), 474 EXPECT_CALL(*mock_tables_.get(),
390 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 475 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
391 Pointee(ContainerEq(PrefetchDataMap())))) 476 Pointee(ContainerEq(PrefetchDataMap())),
477 Pointee(ContainerEq(RedirectDataMap())),
478 Pointee(ContainerEq(RedirectDataMap()))))
392 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 479 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
393 SetArgPointee<1>(test_host_data_))); 480 SetArgPointee<1>(test_host_data_),
481 SetArgPointee<2>(test_url_redirect_data_),
482 SetArgPointee<3>(test_host_redirect_data_)));
394 483
395 ResetPredictor(); 484 ResetPredictor();
396 InitializePredictor(); 485 InitializePredictor();
397 486
398 // Test that the internal variables correctly initialized. 487 // Test that the internal variables correctly initialized.
399 EXPECT_EQ(predictor_->initialization_state_, 488 EXPECT_EQ(predictor_->initialization_state_,
400 ResourcePrefetchPredictor::INITIALIZED); 489 ResourcePrefetchPredictor::INITIALIZED);
401 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 490 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
402 491
403 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); 492 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_);
404 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); 493 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_);
494 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_);
495 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_);
405 } 496 }
406 497
407 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { 498 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) {
408 // Single navigation but history count is low, so should not record. 499 // Single navigation but history count is low, so should not record.
409 AddUrlToHistory("http://www.google.com", 1); 500 AddUrlToHistory("http://www.google.com", 1);
410 501
411 URLRequestSummary main_frame = CreateURLRequestSummary( 502 URLRequestSummary main_frame =
412 1, 1, "http://www.google.com", "http://www.google.com", 503 CreateURLRequestSummary(1, 1, "http://www.google.com");
413 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
414 predictor_->RecordURLRequest(main_frame); 504 predictor_->RecordURLRequest(main_frame);
415 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 505 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
416 506
507 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary(
508 1, 1, "http://www.google.com", "https://www.google.com");
509 predictor_->RecordURLRedirect(main_frame_redirect);
510 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
511 main_frame = CreateURLRequestSummary(1, 1, "https://www.google.com");
512
417 // Now add a few subresources. 513 // Now add a few subresources.
418 URLRequestSummary resource1 = CreateURLRequestSummary( 514 URLRequestSummary resource1 = CreateURLRequestSummary(
419 1, 1, "http://www.google.com", "http://google.com/style1.css", 515 1, 1, "https://www.google.com", "https://google.com/style1.css",
420 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 516 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
421 predictor_->RecordURLResponse(resource1); 517 predictor_->RecordURLResponse(resource1);
422 URLRequestSummary resource2 = CreateURLRequestSummary( 518 URLRequestSummary resource2 = CreateURLRequestSummary(
423 1, 1, "http://www.google.com", "http://google.com/script1.js", 519 1, 1, "https://www.google.com", "https://google.com/script1.js",
424 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 520 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
425 predictor_->RecordURLResponse(resource2); 521 predictor_->RecordURLResponse(resource2);
426 URLRequestSummary resource3 = CreateURLRequestSummary( 522 URLRequestSummary resource3 = CreateURLRequestSummary(
427 1, 1, "http://www.google.com", "http://google.com/script2.js", 523 1, 1, "https://www.google.com", "https://google.com/script2.js",
428 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 524 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
429 predictor_->RecordURLResponse(resource3); 525 predictor_->RecordURLResponse(resource3);
430 526
431 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 527 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
432 host_data.resources.push_back(CreateResourceData( 528 host_data.resources.push_back(CreateResourceData(
433 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 529 "https://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
434 0, 1.0, net::MEDIUM, false, false)); 530 0, 1.0, net::MEDIUM, false, false));
435 host_data.resources.push_back(CreateResourceData( 531 host_data.resources.push_back(CreateResourceData(
436 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 532 "https://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
437 2.0, net::MEDIUM, false, false)); 533 2.0, net::MEDIUM, false, false));
438 host_data.resources.push_back(CreateResourceData( 534 host_data.resources.push_back(CreateResourceData(
439 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 535 "https://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
440 3.0, net::MEDIUM, false, false)); 536 3.0, net::MEDIUM, false, false));
441 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 537 EXPECT_CALL(*mock_tables_.get(),
538 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
539 empty_host_redirect_data_));
442 540
443 predictor_->OnNavigationComplete(main_frame.navigation_id); 541 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
444 profile_->BlockUntilHistoryProcessesPendingRequests(); 542 profile_->BlockUntilHistoryProcessesPendingRequests();
445 } 543 }
446 544
447 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 545 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
448 // Single navigation that will be recorded. Will check for duplicate 546 // Single navigation that will be recorded. Will check for duplicate
449 // resources and also for number of resources saved. 547 // resources and also for number of resources saved.
450 AddUrlToHistory("http://www.google.com", 4); 548 AddUrlToHistory("http://www.google.com", 4);
451 549
452 URLRequestSummary main_frame = CreateURLRequestSummary( 550 URLRequestSummary main_frame =
453 1, 1, "http://www.google.com", "http://www.google.com", 551 CreateURLRequestSummary(1, 1, "http://www.google.com");
454 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
455 predictor_->RecordURLRequest(main_frame); 552 predictor_->RecordURLRequest(main_frame);
456 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 553 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
457 554
458 URLRequestSummary resource1 = CreateURLRequestSummary( 555 URLRequestSummary resource1 = CreateURLRequestSummary(
459 1, 1, "http://www.google.com", "http://google.com/style1.css", 556 1, 1, "http://www.google.com", "http://google.com/style1.css",
460 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 557 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
461 predictor_->RecordURLResponse(resource1); 558 predictor_->RecordURLResponse(resource1);
462 URLRequestSummary resource2 = CreateURLRequestSummary( 559 URLRequestSummary resource2 = CreateURLRequestSummary(
463 1, 1, "http://www.google.com", "http://google.com/script1.js", 560 1, 1, "http://www.google.com", "http://google.com/script1.js",
464 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 561 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
(...skipping 25 matching lines...) Expand all
490 0, 1.0, net::MEDIUM, false, false)); 587 0, 1.0, net::MEDIUM, false, false));
491 url_data.resources.push_back(CreateResourceData( 588 url_data.resources.push_back(CreateResourceData(
492 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 589 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
493 2.0, net::MEDIUM, false, false)); 590 2.0, net::MEDIUM, false, false));
494 url_data.resources.push_back(CreateResourceData( 591 url_data.resources.push_back(CreateResourceData(
495 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 592 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
496 3.0, net::MEDIUM, false, false)); 593 3.0, net::MEDIUM, false, false));
497 url_data.resources.push_back(CreateResourceData( 594 url_data.resources.push_back(CreateResourceData(
498 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 595 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
499 0, 7.0, net::MEDIUM, false, false)); 596 0, 7.0, net::MEDIUM, false, false));
500 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 597 EXPECT_CALL(*mock_tables_.get(),
598 UpdateData(url_data, empty_host_data_, empty_url_redirect_data_,
599 empty_host_redirect_data_));
501 600
502 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 601 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
503 host_data.resources = url_data.resources; 602 host_data.resources = url_data.resources;
504 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 603 EXPECT_CALL(*mock_tables_.get(),
604 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
605 empty_host_redirect_data_));
505 606
506 predictor_->OnNavigationComplete(main_frame.navigation_id); 607 predictor_->OnNavigationComplete(main_frame.navigation_id);
507 profile_->BlockUntilHistoryProcessesPendingRequests(); 608 profile_->BlockUntilHistoryProcessesPendingRequests();
508 } 609 }
509 610
510 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { 611 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) {
511 // Tests that navigation is recorded correctly for URL already present in 612 // Tests that navigation is recorded correctly for URL already present in
512 // the database cache. 613 // the database cache.
513 AddUrlToHistory("http://www.google.com", 4); 614 AddUrlToHistory("http://www.google.com", 4);
514 615
515 EXPECT_CALL(*mock_tables_.get(), 616 EXPECT_CALL(*mock_tables_.get(),
516 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 617 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
517 Pointee(ContainerEq(PrefetchDataMap())))) 618 Pointee(ContainerEq(PrefetchDataMap())),
619 Pointee(ContainerEq(RedirectDataMap())),
620 Pointee(ContainerEq(RedirectDataMap()))))
518 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 621 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
519 SetArgPointee<1>(test_host_data_))); 622 SetArgPointee<1>(test_host_data_)));
520 ResetPredictor(); 623 ResetPredictor();
521 InitializePredictor(); 624 InitializePredictor();
522 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); 625 EXPECT_EQ(3U, predictor_->url_table_cache_->size());
523 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); 626 EXPECT_EQ(2U, predictor_->host_table_cache_->size());
524 627
525 URLRequestSummary main_frame = CreateURLRequestSummary( 628 URLRequestSummary main_frame = CreateURLRequestSummary(
526 1, 1, "http://www.google.com", "http://www.google.com", 629 1, 1, "http://www.google.com", "http://www.google.com",
527 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 630 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 0, 1.0, net::MEDIUM, false, false)); 666 0, 1.0, net::MEDIUM, false, false));
564 url_data.resources.push_back(CreateResourceData( 667 url_data.resources.push_back(CreateResourceData(
565 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 668 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
566 2.0, net::MEDIUM, false, false)); 669 2.0, net::MEDIUM, false, false));
567 url_data.resources.push_back(CreateResourceData( 670 url_data.resources.push_back(CreateResourceData(
568 "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 1, 1, 671 "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 1, 1,
569 2.1, net::MEDIUM, false, false)); 672 2.1, net::MEDIUM, false, false));
570 url_data.resources.push_back(CreateResourceData( 673 url_data.resources.push_back(CreateResourceData(
571 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 674 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
572 3.0, net::MEDIUM, false, false)); 675 3.0, net::MEDIUM, false, false));
573 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 676 EXPECT_CALL(*mock_tables_.get(),
574 677 UpdateData(url_data, empty_host_data_, empty_url_redirect_data_,
575 EXPECT_CALL( 678 empty_host_redirect_data_));
576 *mock_tables_.get(), 679 EXPECT_CALL(*mock_tables_.get(),
577 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 680 DeleteSingleResourceDataPoint("www.facebook.com",
681 PREFETCH_KEY_TYPE_HOST));
578 682
579 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 683 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
580 host_data.resources.push_back(CreateResourceData( 684 host_data.resources.push_back(CreateResourceData(
581 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 685 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
582 0, 1.0, net::MEDIUM, false, false)); 686 0, 1.0, net::MEDIUM, false, false));
583 host_data.resources.push_back(CreateResourceData( 687 host_data.resources.push_back(CreateResourceData(
584 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 688 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
585 2.0, net::MEDIUM, false, false)); 689 2.0, net::MEDIUM, false, false));
586 host_data.resources.push_back(CreateResourceData( 690 host_data.resources.push_back(CreateResourceData(
587 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 691 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
588 3.0, net::MEDIUM, false, false)); 692 3.0, net::MEDIUM, false, false));
589 host_data.resources.push_back(CreateResourceData( 693 host_data.resources.push_back(CreateResourceData(
590 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 694 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
591 0, 7.0, net::MEDIUM, false, false)); 695 0, 7.0, net::MEDIUM, false, false));
592 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 696 EXPECT_CALL(*mock_tables_.get(),
697 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
698 empty_host_redirect_data_));
593 699
594 predictor_->OnNavigationComplete(main_frame.navigation_id); 700 predictor_->OnNavigationComplete(main_frame.navigation_id);
595 profile_->BlockUntilHistoryProcessesPendingRequests(); 701 profile_->BlockUntilHistoryProcessesPendingRequests();
596 } 702 }
597 703
598 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { 704 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) {
599 // Tests that a URL is deleted before another is added if the cache is full. 705 // Tests that a URL is deleted before another is added if the cache is full.
600 AddUrlToHistory("http://www.nike.com/", 4); 706 AddUrlToHistory("http://www.nike.com/", 4);
601 707
602 EXPECT_CALL(*mock_tables_.get(), 708 EXPECT_CALL(*mock_tables_.get(),
603 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 709 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
604 Pointee(ContainerEq(PrefetchDataMap())))) 710 Pointee(ContainerEq(PrefetchDataMap())),
711 Pointee(ContainerEq(RedirectDataMap())),
712 Pointee(ContainerEq(RedirectDataMap()))))
605 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 713 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
606 SetArgPointee<1>(test_host_data_))); 714 SetArgPointee<1>(test_host_data_)));
607 ResetPredictor(); 715 ResetPredictor();
608 InitializePredictor(); 716 InitializePredictor();
609 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); 717 EXPECT_EQ(3U, predictor_->url_table_cache_->size());
610 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); 718 EXPECT_EQ(2U, predictor_->host_table_cache_->size());
611 719
612 URLRequestSummary main_frame = CreateURLRequestSummary( 720 URLRequestSummary main_frame = CreateURLRequestSummary(
613 1, 1, "http://www.nike.com", "http://www.nike.com", 721 1, 1, "http://www.nike.com", "http://www.nike.com",
614 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 722 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
615 predictor_->RecordURLRequest(main_frame); 723 predictor_->RecordURLRequest(main_frame);
616 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 724 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
617 725
618 URLRequestSummary resource1 = CreateURLRequestSummary( 726 URLRequestSummary resource1 = CreateURLRequestSummary(
619 1, 1, "http://www.nike.com", "http://nike.com/style1.css", 727 1, 1, "http://www.nike.com", "http://nike.com/style1.css",
620 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 728 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
621 predictor_->RecordURLResponse(resource1); 729 predictor_->RecordURLResponse(resource1);
622 URLRequestSummary resource2 = CreateURLRequestSummary( 730 URLRequestSummary resource2 = CreateURLRequestSummary(
623 1, 1, "http://www.nike.com", "http://nike.com/image2.png", 731 1, 1, "http://www.nike.com", "http://nike.com/image2.png",
624 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 732 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
625 predictor_->RecordURLResponse(resource2); 733 predictor_->RecordURLResponse(resource2);
626 734
627 EXPECT_CALL( 735 EXPECT_CALL(*mock_tables_.get(),
628 *mock_tables_.get(), 736 DeleteSingleResourceDataPoint("http://www.google.com/",
629 DeleteSingleDataPoint("http://www.google.com/", PREFETCH_KEY_TYPE_URL)); 737 PREFETCH_KEY_TYPE_URL));
630 EXPECT_CALL( 738 EXPECT_CALL(*mock_tables_.get(),
631 *mock_tables_.get(), 739 DeleteSingleResourceDataPoint("www.facebook.com",
632 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 740 PREFETCH_KEY_TYPE_HOST));
633 741
634 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/"); 742 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/");
635 url_data.resources.push_back(CreateResourceData( 743 url_data.resources.push_back(CreateResourceData(
636 "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 744 "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0,
637 1.0, net::MEDIUM, false, false)); 745 1.0, net::MEDIUM, false, false));
638 url_data.resources.push_back(CreateResourceData( 746 url_data.resources.push_back(CreateResourceData(
639 "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, 747 "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0,
640 net::MEDIUM, false, false)); 748 net::MEDIUM, false, false));
641 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 749 EXPECT_CALL(*mock_tables_.get(),
750 UpdateData(url_data, empty_host_data_, empty_url_redirect_data_,
751 empty_host_redirect_data_));
642 752
643 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com"); 753 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com");
644 host_data.resources = url_data.resources; 754 host_data.resources = url_data.resources;
645 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 755 EXPECT_CALL(*mock_tables_.get(),
756 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
757 empty_host_redirect_data_));
646 758
647 predictor_->OnNavigationComplete(main_frame.navigation_id); 759 predictor_->OnNavigationComplete(main_frame.navigation_id);
648 profile_->BlockUntilHistoryProcessesPendingRequests(); 760 profile_->BlockUntilHistoryProcessesPendingRequests();
649 } 761 }
650 762
763 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) {
764 AddUrlToHistory("https://facebook.com/google", 4);
765
766 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google");
767 predictor_->RecordURLRequest(fb1);
768 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
769
770 URLRequestSummary fb2 = CreateRedirectRequestSummary(
771 1, 1, "http://fb.com/google", "http://facebook.com/google");
772 predictor_->RecordURLRedirect(fb2);
773 URLRequestSummary fb3 = CreateRedirectRequestSummary(
774 1, 1, "http://facebook.com/google", "https://facebook.com/google");
775 predictor_->RecordURLRedirect(fb3);
776 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google");
777
778 // Since the navigation hasn't resources, corresponding entry
779 // in resource table will be deleted
pasko 2016/09/26 14:18:23 s/deleted/deleted./ Same below: add full stops in
alexilin 2016/09/26 15:38:29 Done.
780 EXPECT_CALL(*mock_tables_.get(),
781 DeleteSingleResourceDataPoint("https://facebook.com/google",
782 PREFETCH_KEY_TYPE_URL));
783 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
784 "facebook.com", PREFETCH_KEY_TYPE_HOST));
785
786 RedirectData url_redirect_data;
787 url_redirect_data.set_primary_key("http://fb.com/google");
788 InitializeRedirectStat(url_redirect_data.add_redirects(),
789 "https://facebook.com/google", 1, 0, 0);
790 EXPECT_CALL(*mock_tables_.get(),
791 UpdateData(empty_url_data_, empty_host_data_, url_redirect_data,
792 empty_host_redirect_data_));
793
794 RedirectData host_redirect_data;
795 host_redirect_data.set_primary_key("fb.com");
796 InitializeRedirectStat(host_redirect_data.add_redirects(), "facebook.com", 1,
797 0, 0);
798 EXPECT_CALL(*mock_tables_.get(),
799 UpdateData(empty_url_data_, empty_host_data_,
800 empty_url_redirect_data_, host_redirect_data));
801
802 predictor_->RecordMainFrameLoadComplete(fb_end);
803 profile_->BlockUntilHistoryProcessesPendingRequests();
804 }
805
806 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) {
807 // Tests that redirect is recorded correctly for URL already present in
pasko 2016/09/26 14:18:23 If the comment applies to the test as a whole, it
alexilin 2016/09/26 15:38:29 Also fixed existing code. Done.
808 // the database cache.
809 AddUrlToHistory("https://facebook.com/google", 4);
810
811 EXPECT_CALL(*mock_tables_.get(),
812 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
813 Pointee(ContainerEq(PrefetchDataMap())),
814 Pointee(ContainerEq(RedirectDataMap())),
815 Pointee(ContainerEq(RedirectDataMap()))))
816 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_),
817 SetArgPointee<3>(test_host_redirect_data_)));
818 ResetPredictor();
819 InitializePredictor();
820 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size());
821 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size());
822
823 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google");
824 predictor_->RecordURLRequest(fb1);
825 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
826
827 URLRequestSummary fb2 = CreateRedirectRequestSummary(
828 1, 1, "http://fb.com/google", "http://facebook.com/google");
829 predictor_->RecordURLRedirect(fb2);
830 URLRequestSummary fb3 = CreateRedirectRequestSummary(
831 1, 1, "http://facebook.com/google", "https://facebook.com/google");
832 predictor_->RecordURLRedirect(fb3);
833 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google");
834
835 // Oldest entries in tables will be superseded and deleted
836 EXPECT_CALL(*mock_tables_.get(),
837 DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST));
838
839 // Since the navigation hasn't resources, corresponding entry
840 // in resource table will be deleted
841 EXPECT_CALL(*mock_tables_.get(),
842 DeleteSingleResourceDataPoint("https://facebook.com/google",
843 PREFETCH_KEY_TYPE_URL));
844 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
845 "facebook.com", PREFETCH_KEY_TYPE_HOST));
846
847 RedirectData url_redirect_data;
848 url_redirect_data.set_primary_key("http://fb.com/google");
849 InitializeRedirectStat(url_redirect_data.add_redirects(),
850 "https://facebook.com/google", 6, 1, 0);
851 // Exising redirect to https://facebook.com/login will be deleted because of
pasko 2016/09/26 14:18:23 s/Exising/Existing/
alexilin 2016/09/26 15:38:29 Done.
852 // too many consecutive misses.
853 EXPECT_CALL(*mock_tables_.get(),
854 UpdateData(empty_url_data_, empty_host_data_, url_redirect_data,
855 empty_host_redirect_data_));
856
857 RedirectData host_redirect_data;
858 host_redirect_data.set_primary_key("fb.com");
859 InitializeRedirectStat(host_redirect_data.add_redirects(), "facebook.com", 1,
860 0, 0);
861 EXPECT_CALL(*mock_tables_.get(),
862 UpdateData(empty_url_data_, empty_host_data_,
863 empty_url_redirect_data_, host_redirect_data));
864
865 predictor_->RecordMainFrameLoadComplete(fb_end);
866 profile_->BlockUntilHistoryProcessesPendingRequests();
867 }
868
651 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) { 869 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) {
652 // Add some dummy entries to cache. 870 // Add some dummy entries to cache.
653 predictor_->url_table_cache_->insert(std::make_pair( 871 predictor_->url_table_cache_->insert(std::make_pair(
654 "http://www.google.com/page1.html", 872 "http://www.google.com/page1.html",
655 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page1.html"))); 873 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page1.html")));
656 predictor_->url_table_cache_->insert(std::make_pair( 874 predictor_->url_table_cache_->insert(std::make_pair(
657 "http://www.google.com/page2.html", 875 "http://www.google.com/page2.html",
658 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page2.html"))); 876 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page2.html")));
659 predictor_->url_table_cache_->insert(std::make_pair( 877 predictor_->url_table_cache_->insert(std::make_pair(
660 "http://www.yahoo.com/", 878 "http://www.yahoo.com/",
(...skipping 20 matching lines...) Expand all
681 rows.push_back(history::URLRow(GURL("http://www.apple.com"))); 899 rows.push_back(history::URLRow(GURL("http://www.apple.com")));
682 rows.push_back(history::URLRow(GURL("http://www.nike.com"))); 900 rows.push_back(history::URLRow(GURL("http://www.nike.com")));
683 901
684 std::vector<std::string> urls_to_delete, hosts_to_delete; 902 std::vector<std::string> urls_to_delete, hosts_to_delete;
685 urls_to_delete.push_back("http://www.google.com/page2.html"); 903 urls_to_delete.push_back("http://www.google.com/page2.html");
686 urls_to_delete.push_back("http://www.apple.com/"); 904 urls_to_delete.push_back("http://www.apple.com/");
687 urls_to_delete.push_back("http://www.nike.com/"); 905 urls_to_delete.push_back("http://www.nike.com/");
688 hosts_to_delete.push_back("www.google.com"); 906 hosts_to_delete.push_back("www.google.com");
689 hosts_to_delete.push_back("www.apple.com"); 907 hosts_to_delete.push_back("www.apple.com");
690 908
691 EXPECT_CALL( 909 EXPECT_CALL(*mock_tables_.get(),
692 *mock_tables_.get(), 910 DeleteResourceData(ContainerEq(urls_to_delete),
693 DeleteData(ContainerEq(urls_to_delete), ContainerEq(hosts_to_delete))); 911 ContainerEq(hosts_to_delete)));
694 912
695 predictor_->DeleteUrls(rows); 913 predictor_->DeleteUrls(rows);
696 EXPECT_EQ(2U, predictor_->url_table_cache_->size()); 914 EXPECT_EQ(2U, predictor_->url_table_cache_->size());
697 EXPECT_EQ(1U, predictor_->host_table_cache_->size()); 915 EXPECT_EQ(1U, predictor_->host_table_cache_->size());
698 916
699 EXPECT_CALL(*mock_tables_.get(), DeleteAllData()); 917 EXPECT_CALL(*mock_tables_.get(), DeleteAllData());
700 918
701 predictor_->DeleteAllUrls(); 919 predictor_->DeleteAllUrls();
702 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 920 EXPECT_TRUE(predictor_->url_table_cache_->empty());
703 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 921 EXPECT_TRUE(predictor_->host_table_cache_->empty());
(...skipping 10 matching lines...) Expand all
714 2, 1, "http://www.yahoo.com", "http://www.yahoo.com", 932 2, 1, "http://www.yahoo.com", "http://www.yahoo.com",
715 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 933 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
716 934
717 predictor_->OnMainFrameRequest(summary1); 935 predictor_->OnMainFrameRequest(summary1);
718 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 936 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
719 predictor_->OnMainFrameRequest(summary2); 937 predictor_->OnMainFrameRequest(summary2);
720 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 938 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
721 predictor_->OnMainFrameRequest(summary3); 939 predictor_->OnMainFrameRequest(summary3);
722 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 940 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
723 941
724 // Insert anther with same navigation id. It should replace. 942 // Insert another with same navigation id. It should replace.
725 URLRequestSummary summary4 = CreateURLRequestSummary( 943 URLRequestSummary summary4 = CreateURLRequestSummary(
726 1, 1, "http://www.nike.com", "http://www.nike.com", 944 1, 1, "http://www.nike.com", "http://www.nike.com",
727 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 945 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
728 URLRequestSummary summary5 = CreateURLRequestSummary( 946 URLRequestSummary summary5 = CreateURLRequestSummary(
729 1, 2, "http://www.google.com", "http://www.google.com", 947 1, 2, "http://www.google.com", "http://www.google.com",
730 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 948 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
731 949
732 predictor_->OnMainFrameRequest(summary4); 950 predictor_->OnMainFrameRequest(summary4);
733 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 951 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
734 952
(...skipping 11 matching lines...) Expand all
746 964
747 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) != 965 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) !=
748 predictor_->inflight_navigations_.end()); 966 predictor_->inflight_navigations_.end());
749 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) != 967 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) !=
750 predictor_->inflight_navigations_.end()); 968 predictor_->inflight_navigations_.end());
751 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) != 969 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) !=
752 predictor_->inflight_navigations_.end()); 970 predictor_->inflight_navigations_.end());
753 } 971 }
754 972
755 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) { 973 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) {
756 URLRequestSummary summary1 = CreateURLRequestSummary( 974 URLRequestSummary yahoo = CreateURLRequestSummary(1, 1, "http://yahoo.com");
757 1, 1, "http://www.google.com", "http://www.google.com",
758 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
759 URLRequestSummary summary2 = CreateURLRequestSummary(
760 1, 2, "http://www.google.com", "http://www.google.com",
761 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
762 URLRequestSummary summary3 = CreateURLRequestSummary(
763 2, 1, "http://www.yahoo.com", "http://www.yahoo.com",
764 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
765 975
766 predictor_->OnMainFrameRedirect(summary1); 976 URLRequestSummary bbc1 = CreateURLRequestSummary(2, 2, "http://bbc.com");
977 URLRequestSummary bbc2 = CreateRedirectRequestSummary(2, 2, "http://bbc.com",
978 "https://www.bbc.com");
979 NavigationID bbc_end = CreateNavigationID(2, 2, "https://www.bbc.com");
980
981 URLRequestSummary youtube1 =
982 CreateURLRequestSummary(1, 2, "http://youtube.com");
983 URLRequestSummary youtube2 = CreateRedirectRequestSummary(
984 1, 2, "http://youtube.com", "https://youtube.com");
985 NavigationID youtube_end = CreateNavigationID(1, 2, "https://youtube.com");
986
987 URLRequestSummary nyt1 = CreateURLRequestSummary(2, 1, "http://nyt.com");
988 URLRequestSummary nyt2 = CreateRedirectRequestSummary(2, 1, "http://nyt.com",
989 "http://nytimes.com");
990 URLRequestSummary nyt3 = CreateRedirectRequestSummary(
991 2, 1, "http://nytimes.com", "http://m.nytimes.com");
992 NavigationID nyt_end = CreateNavigationID(2, 1, "http://m.nytimes.com");
993
994 URLRequestSummary fb1 = CreateURLRequestSummary(1, 3, "http://fb.com");
995 URLRequestSummary fb2 = CreateRedirectRequestSummary(1, 3, "http://fb.com",
996 "http://facebook.com");
997 URLRequestSummary fb3 = CreateRedirectRequestSummary(
998 1, 3, "http://facebook.com", "https://facebook.com");
999 URLRequestSummary fb4 = CreateRedirectRequestSummary(
1000 1, 3, "https://facebook.com",
1001 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1002 NavigationID fb_end = CreateNavigationID(
1003 1, 3,
1004 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1005
1006 // Redirect with empty redirect_url will be deleted
1007 predictor_->OnMainFrameRequest(yahoo);
1008 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1009 predictor_->OnMainFrameRedirect(yahoo);
767 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1010 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
768 1011
769 predictor_->OnMainFrameRequest(summary1); 1012 // Redirect without previous request works fine
1013 // predictor_->OnMainFrameRequest(bbc1) missing
1014 predictor_->OnMainFrameRedirect(bbc2);
770 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1015 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
771 predictor_->OnMainFrameRequest(summary2); 1016 EXPECT_EQ(bbc1.navigation_id.main_frame_url,
1017 predictor_->inflight_navigations_[bbc_end]->initial_url);
1018
1019 // http://youtube.com -> https://youtube.com
1020 predictor_->OnMainFrameRequest(youtube1);
772 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1021 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1022 predictor_->OnMainFrameRedirect(youtube2);
1023 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1024 EXPECT_EQ(youtube1.navigation_id.main_frame_url,
1025 predictor_->inflight_navigations_[youtube_end]->initial_url);
773 1026
774 predictor_->OnMainFrameRedirect(summary3); 1027 // http://nyt.com -> http://nytimes.com -> http://m.nytimes.com
775 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1028 predictor_->OnMainFrameRequest(nyt1);
776 predictor_->OnMainFrameRedirect(summary1); 1029 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
777 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1030 predictor_->OnMainFrameRedirect(nyt2);
778 predictor_->OnMainFrameRedirect(summary2); 1031 predictor_->OnMainFrameRedirect(nyt3);
779 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1032 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1033 EXPECT_EQ(nyt1.navigation_id.main_frame_url,
1034 predictor_->inflight_navigations_[nyt_end]->initial_url);
1035
1036 // http://fb.com -> http://facebook.com -> https://facebook.com ->
1037 // https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr
1038 predictor_->OnMainFrameRequest(fb1);
1039 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1040 predictor_->OnMainFrameRedirect(fb2);
1041 predictor_->OnMainFrameRedirect(fb3);
1042 predictor_->OnMainFrameRedirect(fb4);
1043 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1044 EXPECT_EQ(fb1.navigation_id.main_frame_url,
1045 predictor_->inflight_navigations_[fb_end]->initial_url);
780 } 1046 }
781 1047
782 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) { 1048 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) {
783 // If there is no inflight navigation, nothing happens. 1049 // If there is no inflight navigation, nothing happens.
784 URLRequestSummary resource1 = CreateURLRequestSummary( 1050 URLRequestSummary resource1 = CreateURLRequestSummary(
785 1, 1, "http://www.google.com", "http://google.com/style1.css", 1051 1, 1, "http://www.google.com", "http://google.com/style1.css",
786 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 1052 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
787 predictor_->OnSubresourceResponse(resource1); 1053 predictor_->OnSubresourceResponse(resource1);
788 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1054 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
789 1055
790 // Add an inflight navigation. 1056 // Add an inflight navigation.
791 URLRequestSummary main_frame1 = CreateURLRequestSummary( 1057 URLRequestSummary main_frame1 = CreateURLRequestSummary(
792 1, 1, "http://www.google.com", "http://www.google.com", 1058 1, 1, "http://www.google.com", "http://www.google.com",
793 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1059 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
794 predictor_->OnMainFrameRequest(main_frame1); 1060 predictor_->OnMainFrameRequest(main_frame1);
795 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1061 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
796 1062
797 // Now add a few subresources. 1063 // Now add a few subresources.
798 URLRequestSummary resource2 = CreateURLRequestSummary( 1064 URLRequestSummary resource2 = CreateURLRequestSummary(
799 1, 1, "http://www.google.com", "http://google.com/script1.js", 1065 1, 1, "http://www.google.com", "http://google.com/script1.js",
800 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1066 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
801 URLRequestSummary resource3 = CreateURLRequestSummary( 1067 URLRequestSummary resource3 = CreateURLRequestSummary(
802 1, 1, "http://www.google.com", "http://google.com/script2.js", 1068 1, 1, "http://www.google.com", "http://google.com/script2.js",
803 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1069 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
804 predictor_->OnSubresourceResponse(resource1); 1070 predictor_->OnSubresourceResponse(resource1);
805 predictor_->OnSubresourceResponse(resource2); 1071 predictor_->OnSubresourceResponse(resource2);
806 predictor_->OnSubresourceResponse(resource3); 1072 predictor_->OnSubresourceResponse(resource3);
807 1073
808 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1074 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
809 EXPECT_EQ(3U, 1075 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id]
810 predictor_->inflight_navigations_[main_frame1.navigation_id]->size()); 1076 ->subresource_requests.size());
811 EXPECT_TRUE(URLRequestSummaryAreEqual( 1077 EXPECT_TRUE(URLRequestSummaryAreEqual(
812 resource1, 1078 resource1, predictor_->inflight_navigations_[main_frame1.navigation_id]
813 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(0))); 1079 ->subresource_requests[0]));
814 EXPECT_TRUE(URLRequestSummaryAreEqual( 1080 EXPECT_TRUE(URLRequestSummaryAreEqual(
815 resource2, 1081 resource2, predictor_->inflight_navigations_[main_frame1.navigation_id]
816 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(1))); 1082 ->subresource_requests[1]));
817 EXPECT_TRUE(URLRequestSummaryAreEqual( 1083 EXPECT_TRUE(URLRequestSummaryAreEqual(
818 resource3, 1084 resource3, predictor_->inflight_navigations_[main_frame1.navigation_id]
819 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(2))); 1085 ->subresource_requests[2]));
820 } 1086 }
821 1087
822 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) { 1088 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) {
823 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1089 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
824 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); 1090 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type"));
825 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1091 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
826 content::RESOURCE_TYPE_STYLESHEET, "")); 1092 content::RESOURCE_TYPE_STYLESHEET, ""));
827 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1093 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
828 content::RESOURCE_TYPE_WORKER, "text/css")); 1094 content::RESOURCE_TYPE_WORKER, "text/css"));
829 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1095 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 url_request_job_factory_.set_response_info(response_info); 1309 url_request_job_factory_.set_response_info(response_info);
1044 std::unique_ptr<net::URLRequest> request_etag = 1310 std::unique_ptr<net::URLRequest> request_etag =
1045 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, 1311 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1046 content::RESOURCE_TYPE_PREFETCH, 1, 1, true); 1312 content::RESOURCE_TYPE_PREFETCH, 1, 1, true);
1047 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); 1313 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1048 EXPECT_TRUE(summary.has_validators); 1314 EXPECT_TRUE(summary.has_validators);
1049 EXPECT_TRUE(summary.always_revalidate); 1315 EXPECT_TRUE(summary.always_revalidate);
1050 } 1316 }
1051 1317
1052 } // namespace predictors 1318 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698