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

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

Issue 2355273002: Redirect handling in the resource_prefetch_predictor. (Closed)
Patch Set: Fix compilation complaints. 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 #include <utility>
9 10
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "chrome/browser/history/history_service_factory.h" 16 #include "chrome/browser/history/history_service_factory.h"
16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 17 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
17 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" 18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h"
18 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
19 #include "components/history/core/browser/history_service.h" 20 #include "components/history/core/browser/history_service.h"
20 #include "components/history/core/browser/history_types.h" 21 #include "components/history/core/browser/history_types.h"
21 #include "content/public/browser/resource_request_info.h" 22 #include "content/public/browser/resource_request_info.h"
22 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
23 #include "net/http/http_response_headers.h" 24 #include "net/http/http_response_headers.h"
24 #include "net/url_request/url_request_context.h" 25 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_job.h" 26 #include "net/url_request/url_request_job.h"
26 #include "net/url_request/url_request_test_util.h" 27 #include "net/url_request/url_request_test_util.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
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_redirect_endpoints(),
415 "https://facebook.com/google", 5, 1, 0);
416 InitializeRedirectStat(facebook.add_redirect_endpoints(),
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_redirect_endpoints(),
423 "https://nytimes.com", 2, 0, 0);
424
425 RedirectData google;
426 google.set_primary_key("http://google.com");
427 google.set_last_visit_time(8);
428 InitializeRedirectStat(google.add_redirect_endpoints(),
429 "https://google.com", 3, 0, 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_redirect_endpoints(), "www.bbc.com", 8, 4,
445 1);
446 InitializeRedirectStat(bbc.add_redirect_endpoints(), "m.bbc.com", 5, 8, 0);
447 InitializeRedirectStat(bbc.add_redirect_endpoints(), "bbc.co.uk", 1, 3, 0);
448
449 RedirectData microsoft;
450 microsoft.set_primary_key("microsoft.com");
451 microsoft.set_last_visit_time(10);
452 InitializeRedirectStat(microsoft.add_redirect_endpoints(),
453 "www.microsoft.com", 10, 0, 0);
454
455 test_host_redirect_data_.clear();
456 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc));
457 test_host_redirect_data_.insert(
458 std::make_pair(microsoft.primary_key(), microsoft));
459 }
376 } 460 }
377 461
462 // Tests that the predictor initializes correctly without any data.
378 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) { 463 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) {
379 // Tests that the predictor initializes correctly without any data.
380 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 464 EXPECT_TRUE(predictor_->url_table_cache_->empty());
381 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 465 EXPECT_TRUE(predictor_->host_table_cache_->empty());
466 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty());
467 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty());
382 } 468 }
383 469
470 // Tests that the history and the db tables data are loaded correctly.
384 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) { 471 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) {
385 // Tests that the history and the db tables data are loaded correctly.
386 AddUrlToHistory("http://www.google.com/", 4); 472 AddUrlToHistory("http://www.google.com/", 4);
387 AddUrlToHistory("http://www.yahoo.com/", 2); 473 AddUrlToHistory("http://www.yahoo.com/", 2);
388 474
389 EXPECT_CALL(*mock_tables_.get(), 475 EXPECT_CALL(*mock_tables_.get(),
390 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 476 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
391 Pointee(ContainerEq(PrefetchDataMap())))) 477 Pointee(ContainerEq(PrefetchDataMap())),
478 Pointee(ContainerEq(RedirectDataMap())),
479 Pointee(ContainerEq(RedirectDataMap()))))
392 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 480 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
393 SetArgPointee<1>(test_host_data_))); 481 SetArgPointee<1>(test_host_data_),
482 SetArgPointee<2>(test_url_redirect_data_),
483 SetArgPointee<3>(test_host_redirect_data_)));
394 484
395 ResetPredictor(); 485 ResetPredictor();
396 InitializePredictor(); 486 InitializePredictor();
397 487
398 // Test that the internal variables correctly initialized. 488 // Test that the internal variables correctly initialized.
399 EXPECT_EQ(predictor_->initialization_state_, 489 EXPECT_EQ(predictor_->initialization_state_,
400 ResourcePrefetchPredictor::INITIALIZED); 490 ResourcePrefetchPredictor::INITIALIZED);
401 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 491 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
402 492
403 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); 493 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_);
404 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); 494 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_);
495 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_);
496 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_);
405 } 497 }
406 498
499 // Single navigation but history count is low, so should not record.
407 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { 500 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) {
408 // Single navigation but history count is low, so should not record.
409 AddUrlToHistory("http://www.google.com", 1); 501 AddUrlToHistory("http://www.google.com", 1);
410 502
411 URLRequestSummary main_frame = CreateURLRequestSummary( 503 URLRequestSummary main_frame =
412 1, 1, "http://www.google.com", "http://www.google.com", 504 CreateURLRequestSummary(1, 1, "http://www.google.com");
413 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
414 predictor_->RecordURLRequest(main_frame); 505 predictor_->RecordURLRequest(main_frame);
415 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 506 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
416 507
508 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary(
509 1, 1, "http://www.google.com", "https://www.google.com");
510 predictor_->RecordURLRedirect(main_frame_redirect);
511 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
512 main_frame = CreateURLRequestSummary(1, 1, "https://www.google.com");
513
417 // Now add a few subresources. 514 // Now add a few subresources.
418 URLRequestSummary resource1 = CreateURLRequestSummary( 515 URLRequestSummary resource1 = CreateURLRequestSummary(
419 1, 1, "http://www.google.com", "http://google.com/style1.css", 516 1, 1, "https://www.google.com", "https://google.com/style1.css",
420 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 517 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
421 predictor_->RecordURLResponse(resource1); 518 predictor_->RecordURLResponse(resource1);
422 URLRequestSummary resource2 = CreateURLRequestSummary( 519 URLRequestSummary resource2 = CreateURLRequestSummary(
423 1, 1, "http://www.google.com", "http://google.com/script1.js", 520 1, 1, "https://www.google.com", "https://google.com/script1.js",
424 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 521 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
425 predictor_->RecordURLResponse(resource2); 522 predictor_->RecordURLResponse(resource2);
426 URLRequestSummary resource3 = CreateURLRequestSummary( 523 URLRequestSummary resource3 = CreateURLRequestSummary(
427 1, 1, "http://www.google.com", "http://google.com/script2.js", 524 1, 1, "https://www.google.com", "https://google.com/script2.js",
428 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 525 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
429 predictor_->RecordURLResponse(resource3); 526 predictor_->RecordURLResponse(resource3);
430 527
431 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 528 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
432 host_data.resources.push_back(CreateResourceData( 529 host_data.resources.push_back(CreateResourceData(
433 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 530 "https://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
434 0, 1.0, net::MEDIUM, false, false)); 531 0, 1.0, net::MEDIUM, false, false));
435 host_data.resources.push_back(CreateResourceData( 532 host_data.resources.push_back(CreateResourceData(
436 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 533 "https://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
437 2.0, net::MEDIUM, false, false)); 534 2.0, net::MEDIUM, false, false));
438 host_data.resources.push_back(CreateResourceData( 535 host_data.resources.push_back(CreateResourceData(
439 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 536 "https://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
440 3.0, net::MEDIUM, false, false)); 537 3.0, net::MEDIUM, false, false));
441 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 538 EXPECT_CALL(*mock_tables_.get(),
539 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
540 empty_host_redirect_data_));
442 541
443 predictor_->OnNavigationComplete(main_frame.navigation_id); 542 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
444 profile_->BlockUntilHistoryProcessesPendingRequests(); 543 profile_->BlockUntilHistoryProcessesPendingRequests();
445 } 544 }
446 545
546 // Single navigation that will be recorded. Will check for duplicate
547 // resources and also for number of resources saved.
447 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 548 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
448 // Single navigation that will be recorded. Will check for duplicate
449 // resources and also for number of resources saved.
450 AddUrlToHistory("http://www.google.com", 4); 549 AddUrlToHistory("http://www.google.com", 4);
451 550
452 URLRequestSummary main_frame = CreateURLRequestSummary( 551 URLRequestSummary main_frame =
453 1, 1, "http://www.google.com", "http://www.google.com", 552 CreateURLRequestSummary(1, 1, "http://www.google.com");
454 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
455 predictor_->RecordURLRequest(main_frame); 553 predictor_->RecordURLRequest(main_frame);
456 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 554 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
457 555
458 URLRequestSummary resource1 = CreateURLRequestSummary( 556 URLRequestSummary resource1 = CreateURLRequestSummary(
459 1, 1, "http://www.google.com", "http://google.com/style1.css", 557 1, 1, "http://www.google.com", "http://google.com/style1.css",
460 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 558 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
461 predictor_->RecordURLResponse(resource1); 559 predictor_->RecordURLResponse(resource1);
462 URLRequestSummary resource2 = CreateURLRequestSummary( 560 URLRequestSummary resource2 = CreateURLRequestSummary(
463 1, 1, "http://www.google.com", "http://google.com/script1.js", 561 1, 1, "http://www.google.com", "http://google.com/script1.js",
464 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 562 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
(...skipping 25 matching lines...) Expand all
490 0, 1.0, net::MEDIUM, false, false)); 588 0, 1.0, net::MEDIUM, false, false));
491 url_data.resources.push_back(CreateResourceData( 589 url_data.resources.push_back(CreateResourceData(
492 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 590 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
493 2.0, net::MEDIUM, false, false)); 591 2.0, net::MEDIUM, false, false));
494 url_data.resources.push_back(CreateResourceData( 592 url_data.resources.push_back(CreateResourceData(
495 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 593 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
496 3.0, net::MEDIUM, false, false)); 594 3.0, net::MEDIUM, false, false));
497 url_data.resources.push_back(CreateResourceData( 595 url_data.resources.push_back(CreateResourceData(
498 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 596 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
499 0, 7.0, net::MEDIUM, false, false)); 597 0, 7.0, net::MEDIUM, false, false));
500 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 598 EXPECT_CALL(*mock_tables_.get(),
599 UpdateData(url_data, empty_host_data_, empty_url_redirect_data_,
600 empty_host_redirect_data_));
501 601
502 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 602 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
503 host_data.resources = url_data.resources; 603 host_data.resources = url_data.resources;
504 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 604 EXPECT_CALL(*mock_tables_.get(),
605 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
606 empty_host_redirect_data_));
505 607
506 predictor_->OnNavigationComplete(main_frame.navigation_id); 608 predictor_->OnNavigationComplete(main_frame.navigation_id);
507 profile_->BlockUntilHistoryProcessesPendingRequests(); 609 profile_->BlockUntilHistoryProcessesPendingRequests();
508 } 610 }
509 611
612 // Tests that navigation is recorded correctly for URL already present in
613 // the database cache.
510 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { 614 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) {
511 // Tests that navigation is recorded correctly for URL already present in
512 // the database cache.
513 AddUrlToHistory("http://www.google.com", 4); 615 AddUrlToHistory("http://www.google.com", 4);
514 616
515 EXPECT_CALL(*mock_tables_.get(), 617 EXPECT_CALL(*mock_tables_.get(),
516 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 618 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
517 Pointee(ContainerEq(PrefetchDataMap())))) 619 Pointee(ContainerEq(PrefetchDataMap())),
620 Pointee(ContainerEq(RedirectDataMap())),
621 Pointee(ContainerEq(RedirectDataMap()))))
518 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 622 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
519 SetArgPointee<1>(test_host_data_))); 623 SetArgPointee<1>(test_host_data_)));
520 ResetPredictor(); 624 ResetPredictor();
521 InitializePredictor(); 625 InitializePredictor();
522 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); 626 EXPECT_EQ(3U, predictor_->url_table_cache_->size());
523 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); 627 EXPECT_EQ(2U, predictor_->host_table_cache_->size());
524 628
525 URLRequestSummary main_frame = CreateURLRequestSummary( 629 URLRequestSummary main_frame = CreateURLRequestSummary(
526 1, 1, "http://www.google.com", "http://www.google.com", 630 1, 1, "http://www.google.com", "http://www.google.com",
527 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 631 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)); 667 0, 1.0, net::MEDIUM, false, false));
564 url_data.resources.push_back(CreateResourceData( 668 url_data.resources.push_back(CreateResourceData(
565 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 669 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
566 2.0, net::MEDIUM, false, false)); 670 2.0, net::MEDIUM, false, false));
567 url_data.resources.push_back(CreateResourceData( 671 url_data.resources.push_back(CreateResourceData(
568 "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 1, 1, 672 "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 1, 1,
569 2.1, net::MEDIUM, false, false)); 673 2.1, net::MEDIUM, false, false));
570 url_data.resources.push_back(CreateResourceData( 674 url_data.resources.push_back(CreateResourceData(
571 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 675 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
572 3.0, net::MEDIUM, false, false)); 676 3.0, net::MEDIUM, false, false));
573 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 677 EXPECT_CALL(*mock_tables_.get(),
574 678 UpdateData(url_data, empty_host_data_, empty_url_redirect_data_,
575 EXPECT_CALL( 679 empty_host_redirect_data_));
576 *mock_tables_.get(), 680 EXPECT_CALL(*mock_tables_.get(),
577 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 681 DeleteSingleResourceDataPoint("www.facebook.com",
682 PREFETCH_KEY_TYPE_HOST));
578 683
579 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 684 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
580 host_data.resources.push_back(CreateResourceData( 685 host_data.resources.push_back(CreateResourceData(
581 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 686 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
582 0, 1.0, net::MEDIUM, false, false)); 687 0, 1.0, net::MEDIUM, false, false));
583 host_data.resources.push_back(CreateResourceData( 688 host_data.resources.push_back(CreateResourceData(
584 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 689 "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
585 2.0, net::MEDIUM, false, false)); 690 2.0, net::MEDIUM, false, false));
586 host_data.resources.push_back(CreateResourceData( 691 host_data.resources.push_back(CreateResourceData(
587 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 692 "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0,
588 3.0, net::MEDIUM, false, false)); 693 3.0, net::MEDIUM, false, false));
589 host_data.resources.push_back(CreateResourceData( 694 host_data.resources.push_back(CreateResourceData(
590 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 695 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
591 0, 7.0, net::MEDIUM, false, false)); 696 0, 7.0, net::MEDIUM, false, false));
592 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 697 EXPECT_CALL(*mock_tables_.get(),
698 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
699 empty_host_redirect_data_));
593 700
594 predictor_->OnNavigationComplete(main_frame.navigation_id); 701 predictor_->OnNavigationComplete(main_frame.navigation_id);
595 profile_->BlockUntilHistoryProcessesPendingRequests(); 702 profile_->BlockUntilHistoryProcessesPendingRequests();
596 } 703 }
597 704
705 // Tests that a URL is deleted before another is added if the cache is full.
598 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { 706 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) {
599 // Tests that a URL is deleted before another is added if the cache is full.
600 AddUrlToHistory("http://www.nike.com/", 4); 707 AddUrlToHistory("http://www.nike.com/", 4);
601 708
602 EXPECT_CALL(*mock_tables_.get(), 709 EXPECT_CALL(*mock_tables_.get(),
603 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 710 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
604 Pointee(ContainerEq(PrefetchDataMap())))) 711 Pointee(ContainerEq(PrefetchDataMap())),
712 Pointee(ContainerEq(RedirectDataMap())),
713 Pointee(ContainerEq(RedirectDataMap()))))
605 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 714 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
606 SetArgPointee<1>(test_host_data_))); 715 SetArgPointee<1>(test_host_data_)));
607 ResetPredictor(); 716 ResetPredictor();
608 InitializePredictor(); 717 InitializePredictor();
609 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); 718 EXPECT_EQ(3U, predictor_->url_table_cache_->size());
610 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); 719 EXPECT_EQ(2U, predictor_->host_table_cache_->size());
611 720
612 URLRequestSummary main_frame = CreateURLRequestSummary( 721 URLRequestSummary main_frame = CreateURLRequestSummary(
613 1, 1, "http://www.nike.com", "http://www.nike.com", 722 1, 1, "http://www.nike.com", "http://www.nike.com",
614 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 723 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
615 predictor_->RecordURLRequest(main_frame); 724 predictor_->RecordURLRequest(main_frame);
616 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 725 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
617 726
618 URLRequestSummary resource1 = CreateURLRequestSummary( 727 URLRequestSummary resource1 = CreateURLRequestSummary(
619 1, 1, "http://www.nike.com", "http://nike.com/style1.css", 728 1, 1, "http://www.nike.com", "http://nike.com/style1.css",
620 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 729 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
621 predictor_->RecordURLResponse(resource1); 730 predictor_->RecordURLResponse(resource1);
622 URLRequestSummary resource2 = CreateURLRequestSummary( 731 URLRequestSummary resource2 = CreateURLRequestSummary(
623 1, 1, "http://www.nike.com", "http://nike.com/image2.png", 732 1, 1, "http://www.nike.com", "http://nike.com/image2.png",
624 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 733 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
625 predictor_->RecordURLResponse(resource2); 734 predictor_->RecordURLResponse(resource2);
626 735
627 EXPECT_CALL( 736 EXPECT_CALL(*mock_tables_.get(),
628 *mock_tables_.get(), 737 DeleteSingleResourceDataPoint("http://www.google.com/",
629 DeleteSingleDataPoint("http://www.google.com/", PREFETCH_KEY_TYPE_URL)); 738 PREFETCH_KEY_TYPE_URL));
630 EXPECT_CALL( 739 EXPECT_CALL(*mock_tables_.get(),
631 *mock_tables_.get(), 740 DeleteSingleResourceDataPoint("www.facebook.com",
632 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 741 PREFETCH_KEY_TYPE_HOST));
633 742
634 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/"); 743 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/");
635 url_data.resources.push_back(CreateResourceData( 744 url_data.resources.push_back(CreateResourceData(
636 "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 745 "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0,
637 1.0, net::MEDIUM, false, false)); 746 1.0, net::MEDIUM, false, false));
638 url_data.resources.push_back(CreateResourceData( 747 url_data.resources.push_back(CreateResourceData(
639 "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, 748 "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0,
640 net::MEDIUM, false, false)); 749 net::MEDIUM, false, false));
641 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 750 EXPECT_CALL(*mock_tables_.get(),
751 UpdateData(url_data, empty_host_data_, empty_url_redirect_data_,
752 empty_host_redirect_data_));
642 753
643 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com"); 754 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com");
644 host_data.resources = url_data.resources; 755 host_data.resources = url_data.resources;
645 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 756 EXPECT_CALL(*mock_tables_.get(),
757 UpdateData(empty_url_data_, host_data, empty_url_redirect_data_,
758 empty_host_redirect_data_));
646 759
647 predictor_->OnNavigationComplete(main_frame.navigation_id); 760 predictor_->OnNavigationComplete(main_frame.navigation_id);
648 profile_->BlockUntilHistoryProcessesPendingRequests(); 761 profile_->BlockUntilHistoryProcessesPendingRequests();
649 } 762 }
650 763
764 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) {
765 AddUrlToHistory("https://facebook.com/google", 4);
766
767 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google");
768 predictor_->RecordURLRequest(fb1);
769 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
770
771 URLRequestSummary fb2 = CreateRedirectRequestSummary(
772 1, 1, "http://fb.com/google", "http://facebook.com/google");
773 predictor_->RecordURLRedirect(fb2);
774 URLRequestSummary fb3 = CreateRedirectRequestSummary(
775 1, 1, "http://facebook.com/google", "https://facebook.com/google");
776 predictor_->RecordURLRedirect(fb3);
777 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google");
778
779 // Since the navigation hasn't resources, corresponding entry
780 // in resource table will be deleted.
781 EXPECT_CALL(*mock_tables_.get(),
782 DeleteSingleResourceDataPoint("https://facebook.com/google",
783 PREFETCH_KEY_TYPE_URL));
784 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
785 "facebook.com", PREFETCH_KEY_TYPE_HOST));
786
787 RedirectData url_redirect_data;
788 url_redirect_data.set_primary_key("http://fb.com/google");
789 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
790 "https://facebook.com/google", 1, 0, 0);
791 EXPECT_CALL(*mock_tables_.get(),
792 UpdateData(empty_url_data_, empty_host_data_, url_redirect_data,
793 empty_host_redirect_data_));
794
795 RedirectData host_redirect_data;
796 host_redirect_data.set_primary_key("fb.com");
797 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(),
798 "facebook.com", 1, 0, 0);
799 EXPECT_CALL(*mock_tables_.get(),
800 UpdateData(empty_url_data_, empty_host_data_,
801 empty_url_redirect_data_, host_redirect_data));
802
803 predictor_->RecordMainFrameLoadComplete(fb_end);
804 profile_->BlockUntilHistoryProcessesPendingRequests();
805 }
806
807 // Tests that redirect is recorded correctly for URL already present in
808 // the database cache.
809 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) {
810 AddUrlToHistory("https://facebook.com/google", 4);
811
812 EXPECT_CALL(*mock_tables_.get(),
813 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
814 Pointee(ContainerEq(PrefetchDataMap())),
815 Pointee(ContainerEq(RedirectDataMap())),
816 Pointee(ContainerEq(RedirectDataMap()))))
817 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_),
818 SetArgPointee<3>(test_host_redirect_data_)));
819 ResetPredictor();
820 InitializePredictor();
821 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size());
822 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size());
823
824 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google");
825 predictor_->RecordURLRequest(fb1);
826 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
827
828 URLRequestSummary fb2 = CreateRedirectRequestSummary(
829 1, 1, "http://fb.com/google", "http://facebook.com/google");
830 predictor_->RecordURLRedirect(fb2);
831 URLRequestSummary fb3 = CreateRedirectRequestSummary(
832 1, 1, "http://facebook.com/google", "https://facebook.com/google");
833 predictor_->RecordURLRedirect(fb3);
834 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google");
835
836 // Oldest entries in tables will be superseded and deleted.
837 EXPECT_CALL(*mock_tables_.get(),
838 DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST));
839
840 // Since the navigation hasn't resources, corresponding entry
841 // in resource table will be deleted.
842 EXPECT_CALL(*mock_tables_.get(),
843 DeleteSingleResourceDataPoint("https://facebook.com/google",
844 PREFETCH_KEY_TYPE_URL));
845 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
846 "facebook.com", PREFETCH_KEY_TYPE_HOST));
847
848 RedirectData url_redirect_data;
849 url_redirect_data.set_primary_key("http://fb.com/google");
850 InitializeRedirectStat(url_redirect_data.add_redirect_endpoints(),
851 "https://facebook.com/google", 6, 1, 0);
852 // Existing redirect to https://facebook.com/login will be deleted because of
853 // too many consecutive misses.
854 EXPECT_CALL(*mock_tables_.get(),
855 UpdateData(empty_url_data_, empty_host_data_, url_redirect_data,
856 empty_host_redirect_data_));
857
858 RedirectData host_redirect_data;
859 host_redirect_data.set_primary_key("fb.com");
860 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(),
861 "facebook.com", 1, 0, 0);
862 EXPECT_CALL(*mock_tables_.get(),
863 UpdateData(empty_url_data_, empty_host_data_,
864 empty_url_redirect_data_, host_redirect_data));
865
866 predictor_->RecordMainFrameLoadComplete(fb_end);
867 profile_->BlockUntilHistoryProcessesPendingRequests();
868 }
869
651 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) { 870 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) {
652 // Add some dummy entries to cache. 871 // Add some dummy entries to cache.
653 predictor_->url_table_cache_->insert(std::make_pair( 872 predictor_->url_table_cache_->insert(std::make_pair(
654 "http://www.google.com/page1.html", 873 "http://www.google.com/page1.html",
655 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page1.html"))); 874 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page1.html")));
656 predictor_->url_table_cache_->insert(std::make_pair( 875 predictor_->url_table_cache_->insert(std::make_pair(
657 "http://www.google.com/page2.html", 876 "http://www.google.com/page2.html",
658 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page2.html"))); 877 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page2.html")));
659 predictor_->url_table_cache_->insert(std::make_pair( 878 predictor_->url_table_cache_->insert(std::make_pair(
660 "http://www.yahoo.com/", 879 "http://www.yahoo.com/",
661 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/"))); 880 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/")));
662 predictor_->url_table_cache_->insert(std::make_pair( 881 predictor_->url_table_cache_->insert(std::make_pair(
663 "http://www.apple.com/", 882 "http://www.apple.com/",
664 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.apple.com/"))); 883 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.apple.com/")));
665 predictor_->url_table_cache_->insert(std::make_pair( 884 predictor_->url_table_cache_->insert(std::make_pair(
666 "http://www.nike.com/", 885 "http://www.nike.com/",
667 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/"))); 886 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/")));
668 887
669 predictor_->host_table_cache_->insert(std::make_pair( 888 predictor_->host_table_cache_->insert(std::make_pair(
670 "www.google.com", 889 "www.google.com",
671 PrefetchData(PREFETCH_KEY_TYPE_HOST, "www.google.com"))); 890 PrefetchData(PREFETCH_KEY_TYPE_HOST, "www.google.com")));
672 predictor_->host_table_cache_->insert(std::make_pair( 891 predictor_->host_table_cache_->insert(std::make_pair(
673 "www.yahoo.com", 892 "www.yahoo.com",
674 PrefetchData(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"))); 893 PrefetchData(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com")));
675 predictor_->host_table_cache_->insert(std::make_pair( 894 predictor_->host_table_cache_->insert(std::make_pair(
676 "www.apple.com", 895 "www.apple.com",
677 PrefetchData(PREFETCH_KEY_TYPE_HOST, "www.apple.com"))); 896 PrefetchData(PREFETCH_KEY_TYPE_HOST, "www.apple.com")));
678 897
898 predictor_->url_redirect_table_cache_->insert(
899 std::make_pair("http://www.google.com/page1.html",
900 CreateRedirectData("http://www.google.com/page1.html")));
901 predictor_->url_redirect_table_cache_->insert(
902 std::make_pair("http://www.google.com/page2.html",
903 CreateRedirectData("http://www.google.com/page2.html")));
904 predictor_->url_redirect_table_cache_->insert(std::make_pair(
905 "http://www.apple.com/", CreateRedirectData("http://www.apple.com/")));
906 predictor_->url_redirect_table_cache_->insert(
907 std::make_pair("http://nyt.com/", CreateRedirectData("http://nyt.com/")));
908
909 predictor_->host_redirect_table_cache_->insert(
910 std::make_pair("www.google.com", CreateRedirectData("www.google.com")));
911 predictor_->host_redirect_table_cache_->insert(
912 std::make_pair("www.nike.com", CreateRedirectData("www.nike.com")));
913 predictor_->host_redirect_table_cache_->insert(std::make_pair(
914 "www.wikipedia.org", CreateRedirectData("www.wikipedia.org")));
915
679 history::URLRows rows; 916 history::URLRows rows;
680 rows.push_back(history::URLRow(GURL("http://www.google.com/page2.html"))); 917 rows.push_back(history::URLRow(GURL("http://www.google.com/page2.html")));
681 rows.push_back(history::URLRow(GURL("http://www.apple.com"))); 918 rows.push_back(history::URLRow(GURL("http://www.apple.com")));
682 rows.push_back(history::URLRow(GURL("http://www.nike.com"))); 919 rows.push_back(history::URLRow(GURL("http://www.nike.com")));
683 920
684 std::vector<std::string> urls_to_delete, hosts_to_delete; 921 std::vector<std::string> urls_to_delete, hosts_to_delete,
922 url_redirects_to_delete, host_redirects_to_delete;
685 urls_to_delete.push_back("http://www.google.com/page2.html"); 923 urls_to_delete.push_back("http://www.google.com/page2.html");
686 urls_to_delete.push_back("http://www.apple.com/"); 924 urls_to_delete.push_back("http://www.apple.com/");
687 urls_to_delete.push_back("http://www.nike.com/"); 925 urls_to_delete.push_back("http://www.nike.com/");
688 hosts_to_delete.push_back("www.google.com"); 926 hosts_to_delete.push_back("www.google.com");
689 hosts_to_delete.push_back("www.apple.com"); 927 hosts_to_delete.push_back("www.apple.com");
928 url_redirects_to_delete.push_back("http://www.google.com/page2.html");
929 url_redirects_to_delete.push_back("http://www.apple.com/");
930 host_redirects_to_delete.push_back("www.google.com");
931 host_redirects_to_delete.push_back("www.nike.com");
690 932
691 EXPECT_CALL( 933 EXPECT_CALL(*mock_tables_.get(),
692 *mock_tables_.get(), 934 DeleteResourceData(ContainerEq(urls_to_delete),
693 DeleteData(ContainerEq(urls_to_delete), ContainerEq(hosts_to_delete))); 935 ContainerEq(hosts_to_delete)));
936 EXPECT_CALL(*mock_tables_.get(),
937 DeleteRedirectData(ContainerEq(url_redirects_to_delete),
938 ContainerEq(host_redirects_to_delete)));
694 939
695 predictor_->DeleteUrls(rows); 940 predictor_->DeleteUrls(rows);
696 EXPECT_EQ(2U, predictor_->url_table_cache_->size()); 941 EXPECT_EQ(2U, predictor_->url_table_cache_->size());
697 EXPECT_EQ(1U, predictor_->host_table_cache_->size()); 942 EXPECT_EQ(1U, predictor_->host_table_cache_->size());
943 EXPECT_EQ(2U, predictor_->url_redirect_table_cache_->size());
944 EXPECT_EQ(1U, predictor_->host_redirect_table_cache_->size());
698 945
699 EXPECT_CALL(*mock_tables_.get(), DeleteAllData()); 946 EXPECT_CALL(*mock_tables_.get(), DeleteAllData());
700 947
701 predictor_->DeleteAllUrls(); 948 predictor_->DeleteAllUrls();
702 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 949 EXPECT_TRUE(predictor_->url_table_cache_->empty());
703 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 950 EXPECT_TRUE(predictor_->host_table_cache_->empty());
951 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty());
952 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty());
704 } 953 }
705 954
706 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) { 955 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) {
707 URLRequestSummary summary1 = CreateURLRequestSummary( 956 URLRequestSummary summary1 = CreateURLRequestSummary(
708 1, 1, "http://www.google.com", "http://www.google.com", 957 1, 1, "http://www.google.com", "http://www.google.com",
709 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 958 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
710 URLRequestSummary summary2 = CreateURLRequestSummary( 959 URLRequestSummary summary2 = CreateURLRequestSummary(
711 1, 2, "http://www.google.com", "http://www.google.com", 960 1, 2, "http://www.google.com", "http://www.google.com",
712 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 961 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
713 URLRequestSummary summary3 = CreateURLRequestSummary( 962 URLRequestSummary summary3 = CreateURLRequestSummary(
714 2, 1, "http://www.yahoo.com", "http://www.yahoo.com", 963 2, 1, "http://www.yahoo.com", "http://www.yahoo.com",
715 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 964 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
716 965
717 predictor_->OnMainFrameRequest(summary1); 966 predictor_->OnMainFrameRequest(summary1);
718 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 967 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
719 predictor_->OnMainFrameRequest(summary2); 968 predictor_->OnMainFrameRequest(summary2);
720 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 969 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
721 predictor_->OnMainFrameRequest(summary3); 970 predictor_->OnMainFrameRequest(summary3);
722 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 971 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
723 972
724 // Insert anther with same navigation id. It should replace. 973 // Insert another with same navigation id. It should replace.
725 URLRequestSummary summary4 = CreateURLRequestSummary( 974 URLRequestSummary summary4 = CreateURLRequestSummary(
726 1, 1, "http://www.nike.com", "http://www.nike.com", 975 1, 1, "http://www.nike.com", "http://www.nike.com",
727 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 976 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
728 URLRequestSummary summary5 = CreateURLRequestSummary( 977 URLRequestSummary summary5 = CreateURLRequestSummary(
729 1, 2, "http://www.google.com", "http://www.google.com", 978 1, 2, "http://www.google.com", "http://www.google.com",
730 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 979 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
731 980
732 predictor_->OnMainFrameRequest(summary4); 981 predictor_->OnMainFrameRequest(summary4);
733 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 982 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
734 983
(...skipping 11 matching lines...) Expand all
746 995
747 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) != 996 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) !=
748 predictor_->inflight_navigations_.end()); 997 predictor_->inflight_navigations_.end());
749 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) != 998 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) !=
750 predictor_->inflight_navigations_.end()); 999 predictor_->inflight_navigations_.end());
751 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) != 1000 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) !=
752 predictor_->inflight_navigations_.end()); 1001 predictor_->inflight_navigations_.end());
753 } 1002 }
754 1003
755 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) { 1004 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) {
756 URLRequestSummary summary1 = CreateURLRequestSummary( 1005 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 1006
766 predictor_->OnMainFrameRedirect(summary1); 1007 URLRequestSummary bbc1 = CreateURLRequestSummary(2, 2, "http://bbc.com");
1008 URLRequestSummary bbc2 = CreateRedirectRequestSummary(2, 2, "http://bbc.com",
1009 "https://www.bbc.com");
1010 NavigationID bbc_end = CreateNavigationID(2, 2, "https://www.bbc.com");
1011
1012 URLRequestSummary youtube1 =
1013 CreateURLRequestSummary(1, 2, "http://youtube.com");
1014 URLRequestSummary youtube2 = CreateRedirectRequestSummary(
1015 1, 2, "http://youtube.com", "https://youtube.com");
1016 NavigationID youtube_end = CreateNavigationID(1, 2, "https://youtube.com");
1017
1018 URLRequestSummary nyt1 = CreateURLRequestSummary(2, 1, "http://nyt.com");
1019 URLRequestSummary nyt2 = CreateRedirectRequestSummary(2, 1, "http://nyt.com",
1020 "http://nytimes.com");
1021 URLRequestSummary nyt3 = CreateRedirectRequestSummary(
1022 2, 1, "http://nytimes.com", "http://m.nytimes.com");
1023 NavigationID nyt_end = CreateNavigationID(2, 1, "http://m.nytimes.com");
1024
1025 URLRequestSummary fb1 = CreateURLRequestSummary(1, 3, "http://fb.com");
1026 URLRequestSummary fb2 = CreateRedirectRequestSummary(1, 3, "http://fb.com",
1027 "http://facebook.com");
1028 URLRequestSummary fb3 = CreateRedirectRequestSummary(
1029 1, 3, "http://facebook.com", "https://facebook.com");
1030 URLRequestSummary fb4 = CreateRedirectRequestSummary(
1031 1, 3, "https://facebook.com",
1032 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1033 NavigationID fb_end = CreateNavigationID(
1034 1, 3,
1035 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1036
1037 // Redirect with empty redirect_url will be deleted.
1038 predictor_->OnMainFrameRequest(yahoo);
1039 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1040 predictor_->OnMainFrameRedirect(yahoo);
767 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1041 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
768 1042
769 predictor_->OnMainFrameRequest(summary1); 1043 // Redirect without previous request works fine.
1044 // predictor_->OnMainFrameRequest(bbc1) missing.
1045 predictor_->OnMainFrameRedirect(bbc2);
770 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1046 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
771 predictor_->OnMainFrameRequest(summary2); 1047 EXPECT_EQ(bbc1.navigation_id.main_frame_url,
1048 predictor_->inflight_navigations_[bbc_end]->initial_url);
1049
1050 // http://youtube.com -> https://youtube.com.
1051 predictor_->OnMainFrameRequest(youtube1);
772 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1052 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1053 predictor_->OnMainFrameRedirect(youtube2);
1054 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1055 EXPECT_EQ(youtube1.navigation_id.main_frame_url,
1056 predictor_->inflight_navigations_[youtube_end]->initial_url);
773 1057
774 predictor_->OnMainFrameRedirect(summary3); 1058 // http://nyt.com -> http://nytimes.com -> http://m.nytimes.com.
775 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1059 predictor_->OnMainFrameRequest(nyt1);
776 predictor_->OnMainFrameRedirect(summary1); 1060 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
777 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1061 predictor_->OnMainFrameRedirect(nyt2);
778 predictor_->OnMainFrameRedirect(summary2); 1062 predictor_->OnMainFrameRedirect(nyt3);
779 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1063 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1064 EXPECT_EQ(nyt1.navigation_id.main_frame_url,
1065 predictor_->inflight_navigations_[nyt_end]->initial_url);
1066
1067 // http://fb.com -> http://facebook.com -> https://facebook.com ->
1068 // https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr.
1069 predictor_->OnMainFrameRequest(fb1);
1070 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1071 predictor_->OnMainFrameRedirect(fb2);
1072 predictor_->OnMainFrameRedirect(fb3);
1073 predictor_->OnMainFrameRedirect(fb4);
1074 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1075 EXPECT_EQ(fb1.navigation_id.main_frame_url,
1076 predictor_->inflight_navigations_[fb_end]->initial_url);
780 } 1077 }
781 1078
782 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) { 1079 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) {
783 // If there is no inflight navigation, nothing happens. 1080 // If there is no inflight navigation, nothing happens.
784 URLRequestSummary resource1 = CreateURLRequestSummary( 1081 URLRequestSummary resource1 = CreateURLRequestSummary(
785 1, 1, "http://www.google.com", "http://google.com/style1.css", 1082 1, 1, "http://www.google.com", "http://google.com/style1.css",
786 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 1083 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
787 predictor_->OnSubresourceResponse(resource1); 1084 predictor_->OnSubresourceResponse(resource1);
788 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1085 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
789 1086
790 // Add an inflight navigation. 1087 // Add an inflight navigation.
791 URLRequestSummary main_frame1 = CreateURLRequestSummary( 1088 URLRequestSummary main_frame1 = CreateURLRequestSummary(
792 1, 1, "http://www.google.com", "http://www.google.com", 1089 1, 1, "http://www.google.com", "http://www.google.com",
793 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1090 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
794 predictor_->OnMainFrameRequest(main_frame1); 1091 predictor_->OnMainFrameRequest(main_frame1);
795 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1092 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
796 1093
797 // Now add a few subresources. 1094 // Now add a few subresources.
798 URLRequestSummary resource2 = CreateURLRequestSummary( 1095 URLRequestSummary resource2 = CreateURLRequestSummary(
799 1, 1, "http://www.google.com", "http://google.com/script1.js", 1096 1, 1, "http://www.google.com", "http://google.com/script1.js",
800 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1097 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
801 URLRequestSummary resource3 = CreateURLRequestSummary( 1098 URLRequestSummary resource3 = CreateURLRequestSummary(
802 1, 1, "http://www.google.com", "http://google.com/script2.js", 1099 1, 1, "http://www.google.com", "http://google.com/script2.js",
803 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1100 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
804 predictor_->OnSubresourceResponse(resource1); 1101 predictor_->OnSubresourceResponse(resource1);
805 predictor_->OnSubresourceResponse(resource2); 1102 predictor_->OnSubresourceResponse(resource2);
806 predictor_->OnSubresourceResponse(resource3); 1103 predictor_->OnSubresourceResponse(resource3);
807 1104
808 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1105 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
809 EXPECT_EQ(3U, 1106 EXPECT_EQ(3U, predictor_->inflight_navigations_[main_frame1.navigation_id]
810 predictor_->inflight_navigations_[main_frame1.navigation_id]->size()); 1107 ->subresource_requests.size());
811 EXPECT_TRUE(URLRequestSummaryAreEqual( 1108 EXPECT_TRUE(URLRequestSummaryAreEqual(
812 resource1, 1109 resource1, predictor_->inflight_navigations_[main_frame1.navigation_id]
813 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(0))); 1110 ->subresource_requests[0]));
814 EXPECT_TRUE(URLRequestSummaryAreEqual( 1111 EXPECT_TRUE(URLRequestSummaryAreEqual(
815 resource2, 1112 resource2, predictor_->inflight_navigations_[main_frame1.navigation_id]
816 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(1))); 1113 ->subresource_requests[1]));
817 EXPECT_TRUE(URLRequestSummaryAreEqual( 1114 EXPECT_TRUE(URLRequestSummaryAreEqual(
818 resource3, 1115 resource3, predictor_->inflight_navigations_[main_frame1.navigation_id]
819 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(2))); 1116 ->subresource_requests[2]));
820 } 1117 }
821 1118
822 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) { 1119 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) {
823 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1120 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
824 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); 1121 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type"));
825 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1122 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
826 content::RESOURCE_TYPE_STYLESHEET, "")); 1123 content::RESOURCE_TYPE_STYLESHEET, ""));
827 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1124 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
828 content::RESOURCE_TYPE_WORKER, "text/css")); 1125 content::RESOURCE_TYPE_WORKER, "text/css"));
829 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1126 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 ResourcePrefetchPredictor::ShouldRecordResponse(file_request.get())); 1201 ResourcePrefetchPredictor::ShouldRecordResponse(file_request.get()));
905 } 1202 }
906 1203
907 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseSubresource) { 1204 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseSubresource) {
908 net::HttpResponseInfo response_info; 1205 net::HttpResponseInfo response_info;
909 response_info.headers = 1206 response_info.headers =
910 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n"); 1207 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
911 response_info.was_cached = true; 1208 response_info.was_cached = true;
912 url_request_job_factory_.set_response_info(response_info); 1209 url_request_job_factory_.set_response_info(response_info);
913 1210
914 // Protocol 1211 // Protocol.
915 std::unique_ptr<net::URLRequest> http_image_request = 1212 std::unique_ptr<net::URLRequest> http_image_request =
916 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, 1213 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
917 content::RESOURCE_TYPE_IMAGE, 1, 1, true); 1214 content::RESOURCE_TYPE_IMAGE, 1, 1, true);
918 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( 1215 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
919 http_image_request.get())); 1216 http_image_request.get()));
920 1217
921 std::unique_ptr<net::URLRequest> https_image_request = 1218 std::unique_ptr<net::URLRequest> https_image_request =
922 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM, 1219 CreateURLRequest(GURL("https://www.google.com/cat.png"), net::MEDIUM,
923 content::RESOURCE_TYPE_IMAGE, 1, 1, true); 1220 content::RESOURCE_TYPE_IMAGE, 1, 1, true);
924 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( 1221 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
925 https_image_request.get())); 1222 https_image_request.get()));
926 1223
927 std::unique_ptr<net::URLRequest> file_image_request = 1224 std::unique_ptr<net::URLRequest> file_image_request =
928 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, 1225 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM,
929 content::RESOURCE_TYPE_IMAGE, 1, 1, true); 1226 content::RESOURCE_TYPE_IMAGE, 1, 1, true);
930 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( 1227 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
931 file_image_request.get())); 1228 file_image_request.get()));
932 1229
933 // ResourceType 1230 // ResourceType.
934 std::unique_ptr<net::URLRequest> sub_frame_request = 1231 std::unique_ptr<net::URLRequest> sub_frame_request =
935 CreateURLRequest(GURL("http://www.google.com/frame.html"), net::MEDIUM, 1232 CreateURLRequest(GURL("http://www.google.com/frame.html"), net::MEDIUM,
936 content::RESOURCE_TYPE_SUB_FRAME, 1, 1, true); 1233 content::RESOURCE_TYPE_SUB_FRAME, 1, 1, true);
937 EXPECT_FALSE( 1234 EXPECT_FALSE(
938 ResourcePrefetchPredictor::ShouldRecordResponse(sub_frame_request.get())); 1235 ResourcePrefetchPredictor::ShouldRecordResponse(sub_frame_request.get()));
939 1236
940 std::unique_ptr<net::URLRequest> font_request = CreateURLRequest( 1237 std::unique_ptr<net::URLRequest> font_request = CreateURLRequest(
941 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM, 1238 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM,
942 content::RESOURCE_TYPE_FONT_RESOURCE, 1, 1, true); 1239 content::RESOURCE_TYPE_FONT_RESOURCE, 1, 1, true);
943 EXPECT_TRUE( 1240 EXPECT_TRUE(
(...skipping 22 matching lines...) Expand all
966 prefetch_font_request.get())); 1263 prefetch_font_request.get()));
967 1264
968 url_request_job_factory_.set_mime_type("font/woff-woff"); 1265 url_request_job_factory_.set_mime_type("font/woff-woff");
969 std::unique_ptr<net::URLRequest> prefetch_unknown_font_request = 1266 std::unique_ptr<net::URLRequest> prefetch_unknown_font_request =
970 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"), 1267 CreateURLRequest(GURL("http://www.google.com/comic-sans-ms.woff"),
971 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, 1, 1, 1268 net::MEDIUM, content::RESOURCE_TYPE_PREFETCH, 1, 1,
972 true); 1269 true);
973 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( 1270 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
974 prefetch_unknown_font_request.get())); 1271 prefetch_unknown_font_request.get()));
975 1272
976 // Not main frame 1273 // Not main frame.
977 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest( 1274 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest(
978 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM, 1275 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM,
979 content::RESOURCE_TYPE_FONT_RESOURCE, 1, 1, false); 1276 content::RESOURCE_TYPE_FONT_RESOURCE, 1, 1, false);
980 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( 1277 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
981 font_request_sub_frame.get())); 1278 font_request_sub_frame.get()));
982 } 1279 }
983 1280
984 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) { 1281 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
985 net::HttpResponseInfo response_info; 1282 net::HttpResponseInfo response_info;
986 response_info.headers = 1283 response_info.headers =
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 url_request_job_factory_.set_response_info(response_info); 1340 url_request_job_factory_.set_response_info(response_info);
1044 std::unique_ptr<net::URLRequest> request_etag = 1341 std::unique_ptr<net::URLRequest> request_etag =
1045 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, 1342 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1046 content::RESOURCE_TYPE_PREFETCH, 1, 1, true); 1343 content::RESOURCE_TYPE_PREFETCH, 1, 1, true);
1047 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); 1344 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1048 EXPECT_TRUE(summary.has_validators); 1345 EXPECT_TRUE(summary.has_validators);
1049 EXPECT_TRUE(summary.always_revalidate); 1346 EXPECT_TRUE(summary.always_revalidate);
1050 } 1347 }
1051 1348
1052 } // namespace predictors 1349 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698