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

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

Issue 2321343002: Redirect handling in resource prefetch predictor (Closed)
Patch Set: Update some comments Created 4 years, 3 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 18 matching lines...) Expand all
29 29
30 using testing::ContainerEq; 30 using testing::ContainerEq;
31 using testing::Pointee; 31 using testing::Pointee;
32 using testing::SetArgPointee; 32 using testing::SetArgPointee;
33 using testing::StrictMock; 33 using testing::StrictMock;
34 34
35 namespace predictors { 35 namespace predictors {
36 36
37 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; 37 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary;
38 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; 38 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
39 typedef std::vector<ResourceRow> ResourceRows; 39 typedef ResourcePrefetchPredictorTables::RedirectRow RedirectRow;
40 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; 40 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData;
41 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; 41 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
42 typedef ResourcePrefetchPredictorTables::RedirectData RedirectData;
43 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
42 44
43 // For printing failures nicely. 45 // For printing failures nicely.
44 void PrintTo(const ResourceRow& row, ::std::ostream* os) { 46 void PrintTo(const ResourceRow& row, ::std::ostream* os) {
45 *os << "[" 47 *os << "["
46 << "," << row.resource_url << "," << row.resource_type << "," 48 << "," << row.resource_url << "," << row.resource_type << ","
47 << row.number_of_hits << "," << row.number_of_misses << "," 49 << row.number_of_hits << "," << row.number_of_misses << ","
48 << row.consecutive_misses << "," << row.average_position << "," 50 << row.consecutive_misses << "," << row.average_position << ","
49 << row.score << "]"; 51 << row.score << "]";
50 } 52 }
51 53
52 void PrintTo(const PrefetchData& data, ::std::ostream* os) { 54 void PrintTo(const PrefetchData& data, ::std::ostream* os) {
53 *os << "[" << data.key_type << "," << data.primary_key 55 *os << "[" << data.key_type << "," << data.primary_key
54 << "," << data.last_visit.ToInternalValue() << "]\n"; 56 << "," << data.last_visit.ToInternalValue() << "]\n";
55 for (ResourceRows::const_iterator it = data.resources.begin(); 57 for (const ResourceRow& resource : data.resources) {
56 it != data.resources.end(); ++it) {
57 *os << "\t\t"; 58 *os << "\t\t";
58 PrintTo(*it, os); 59 PrintTo(resource, os);
59 *os << "\n"; 60 *os << "\n";
60 } 61 }
61 } 62 }
62 63
63 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( 64 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders(
64 const char* headers) { 65 const char* headers) {
65 return make_scoped_refptr(new net::HttpResponseHeaders( 66 return make_scoped_refptr(new net::HttpResponseHeaders(
66 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); 67 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
67 } 68 }
68 69
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 private: 142 private:
142 net::HttpResponseInfo response_info_; 143 net::HttpResponseInfo response_info_;
143 std::string mime_type_; 144 std::string mime_type_;
144 }; 145 };
145 146
146 class MockResourcePrefetchPredictorTables 147 class MockResourcePrefetchPredictorTables
147 : public ResourcePrefetchPredictorTables { 148 : public ResourcePrefetchPredictorTables {
148 public: 149 public:
149 MockResourcePrefetchPredictorTables() { } 150 MockResourcePrefetchPredictorTables() { }
150 151
151 MOCK_METHOD2(GetAllData, void(PrefetchDataMap* url_data_map, 152 MOCK_METHOD4(GetAllData, void(PrefetchDataMap* url_data_map,
152 PrefetchDataMap* host_data_map)); 153 PrefetchDataMap* host_data_map,
153 MOCK_METHOD2(UpdateData, void(const PrefetchData& url_data, 154 RedirectDataMap* url_redirect_data_map,
154 const PrefetchData& host_data)); 155 RedirectDataMap* host_redirect_data_map));
155 MOCK_METHOD2(DeleteData, void(const std::vector<std::string>& urls, 156 MOCK_METHOD4(UpdateData, void(const PrefetchData& url_data,
156 const std::vector<std::string>& hosts)); 157 const PrefetchData& host_data,
157 MOCK_METHOD2(DeleteSingleDataPoint, void(const std::string& key, 158 const RedirectData& url_redirect_data,
158 PrefetchKeyType key_type)); 159 const RedirectData& host_redirect_data));
160 MOCK_METHOD2(DeleteResourceData, void(const std::vector<std::string>& urls,
161 const std::vector<std::string>& hosts));
162 MOCK_METHOD2(DeleteSingleResourceDataPoint, void(const std::string& key,
163 PrefetchKeyType key_type));
164 MOCK_METHOD2(DeleteRedirectData, void(const std::vector<std::string>& urls,
165 const std::vector<std::string>& hosts));
166 MOCK_METHOD2(DeleteSingleRedirectDataPoint, void(const std::string& key,
167 PrefetchKeyType key_type));
159 MOCK_METHOD0(DeleteAllData, void()); 168 MOCK_METHOD0(DeleteAllData, void());
160 169
161 protected: 170 protected:
162 ~MockResourcePrefetchPredictorTables() { } 171 ~MockResourcePrefetchPredictorTables() { }
163 }; 172 };
164 173
165 class ResourcePrefetchPredictorTest : public testing::Test { 174 class ResourcePrefetchPredictorTest : public testing::Test {
166 public: 175 public:
167 ResourcePrefetchPredictorTest(); 176 ResourcePrefetchPredictorTest();
168 ~ResourcePrefetchPredictorTest() override; 177 ~ResourcePrefetchPredictorTest() override;
(...skipping 17 matching lines...) Expand all
186 195
187 NavigationID CreateNavigationID(int process_id, 196 NavigationID CreateNavigationID(int process_id,
188 int render_frame_id, 197 int render_frame_id,
189 const std::string& main_frame_url) { 198 const std::string& main_frame_url) {
190 NavigationID navigation_id(process_id, render_frame_id, 199 NavigationID navigation_id(process_id, render_frame_id,
191 GURL(main_frame_url)); 200 GURL(main_frame_url));
192 navigation_id.creation_time = base::TimeTicks::Now(); 201 navigation_id.creation_time = base::TimeTicks::Now();
193 return navigation_id; 202 return navigation_id;
194 } 203 }
195 204
196 ResourcePrefetchPredictor::URLRequestSummary CreateURLRequestSummary( 205 URLRequestSummary CreateURLRequestSummary(
197 int process_id, 206 int process_id,
198 int render_frame_id, 207 int render_frame_id,
199 const std::string& main_frame_url, 208 const std::string& main_frame_url,
200 const std::string& resource_url, 209 const std::string& resource_url = std::string(),
201 content::ResourceType resource_type, 210 content::ResourceType resource_type = content::RESOURCE_TYPE_MAIN_FRAME,
202 net::RequestPriority priority, 211 net::RequestPriority priority = net::MEDIUM,
203 const std::string& mime_type, 212 const std::string& mime_type = std::string(),
204 bool was_cached) { 213 bool was_cached = false) {
205 ResourcePrefetchPredictor::URLRequestSummary summary; 214 URLRequestSummary summary;
206 summary.navigation_id = CreateNavigationID(process_id, render_frame_id, 215 summary.navigation_id = CreateNavigationID(process_id, render_frame_id,
207 main_frame_url); 216 main_frame_url);
208 summary.resource_url = GURL(resource_url); 217 summary.resource_url = resource_url.empty() ?
218 GURL(main_frame_url) : GURL(resource_url);
209 summary.resource_type = resource_type; 219 summary.resource_type = resource_type;
210 summary.priority = priority; 220 summary.priority = priority;
211 summary.mime_type = mime_type; 221 summary.mime_type = mime_type;
212 summary.was_cached = was_cached; 222 summary.was_cached = was_cached;
213 return summary; 223 return summary;
214 } 224 }
215 225
226 URLRequestSummary CreateRedirectRequestSummary(
227 int process_id,
228 int render_frame_id,
229 const std::string& main_frame_url,
230 const std::string& redirect_url) {
231 URLRequestSummary summary = CreateURLRequestSummary(
232 process_id, render_frame_id, main_frame_url);
233 summary.redirect_url = GURL(redirect_url);
234 return summary;
235 }
236
216 std::unique_ptr<net::URLRequest> CreateURLRequest( 237 std::unique_ptr<net::URLRequest> CreateURLRequest(
217 const GURL& url, 238 const GURL& url,
218 net::RequestPriority priority, 239 net::RequestPriority priority,
219 content::ResourceType resource_type, 240 content::ResourceType resource_type,
220 int render_process_id, 241 int render_process_id,
221 int render_frame_id, 242 int render_frame_id,
222 bool is_main_frame) { 243 bool is_main_frame) {
223 std::unique_ptr<net::URLRequest> request = 244 std::unique_ptr<net::URLRequest> request =
224 url_request_context_.CreateRequest(url, priority, 245 url_request_context_.CreateRequest(url, priority,
225 &url_request_delegate_); 246 &url_request_delegate_);
(...skipping 20 matching lines...) Expand all
246 lhs.mime_type == rhs.mime_type && 267 lhs.mime_type == rhs.mime_type &&
247 lhs.was_cached == rhs.was_cached; 268 lhs.was_cached == rhs.was_cached;
248 } 269 }
249 270
250 void ResetPredictor() { 271 void ResetPredictor() {
251 ResourcePrefetchPredictorConfig config; 272 ResourcePrefetchPredictorConfig config;
252 config.max_urls_to_track = 3; 273 config.max_urls_to_track = 3;
253 config.max_hosts_to_track = 2; 274 config.max_hosts_to_track = 2;
254 config.min_url_visit_count = 2; 275 config.min_url_visit_count = 2;
255 config.max_resources_per_entry = 4; 276 config.max_resources_per_entry = 4;
277 config.max_redirects_per_entry = 3;
256 config.max_consecutive_misses = 2; 278 config.max_consecutive_misses = 2;
257 279
258 // TODO(shishir): Enable the prefetching mode in the tests. 280 // TODO(shishir): Enable the prefetching mode in the tests.
259 config.mode |= ResourcePrefetchPredictorConfig::URL_LEARNING; 281 config.mode |= ResourcePrefetchPredictorConfig::URL_LEARNING;
260 config.mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING; 282 config.mode |= ResourcePrefetchPredictorConfig::HOST_LEARNING;
261 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get())); 283 predictor_.reset(new ResourcePrefetchPredictor(config, profile_.get()));
262 predictor_->set_mock_tables(mock_tables_); 284 predictor_->set_mock_tables(mock_tables_);
263 } 285 }
264 286
265 void InitializeSampleData(); 287 void InitializeSampleData();
266 288
267 base::MessageLoop loop_; 289 base::MessageLoop loop_;
268 content::TestBrowserThread ui_thread_; 290 content::TestBrowserThread ui_thread_;
269 content::TestBrowserThread db_thread_; 291 content::TestBrowserThread db_thread_;
270 std::unique_ptr<TestingProfile> profile_; 292 std::unique_ptr<TestingProfile> profile_;
271 net::TestURLRequestContext url_request_context_; 293 net::TestURLRequestContext url_request_context_;
272 294
273 std::unique_ptr<ResourcePrefetchPredictor> predictor_; 295 std::unique_ptr<ResourcePrefetchPredictor> predictor_;
274 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_; 296 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables> > mock_tables_;
275 297
276 PrefetchDataMap test_url_data_; 298 PrefetchDataMap test_url_data_;
277 PrefetchDataMap test_host_data_; 299 PrefetchDataMap test_host_data_;
300 RedirectDataMap test_url_redirect_data_;
301 RedirectDataMap test_host_redirect_data_;
278 PrefetchData empty_url_data_; 302 PrefetchData empty_url_data_;
279 PrefetchData empty_host_data_; 303 PrefetchData empty_host_data_;
304 RedirectData empty_url_redirect_data_;
305 RedirectData empty_host_redirect_data_;
280 306
281 MockURLRequestJobFactory url_request_job_factory_; 307 MockURLRequestJobFactory url_request_job_factory_;
282 EmptyURLRequestDelegate url_request_delegate_; 308 EmptyURLRequestDelegate url_request_delegate_;
283 }; 309 };
284 310
285 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() 311 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest()
286 : loop_(base::MessageLoop::TYPE_DEFAULT), 312 : loop_(base::MessageLoop::TYPE_DEFAULT),
287 ui_thread_(content::BrowserThread::UI, &loop_), 313 ui_thread_(content::BrowserThread::UI, &loop_),
288 db_thread_(content::BrowserThread::DB, &loop_), 314 db_thread_(content::BrowserThread::DB, &loop_),
289 profile_(new TestingProfile()), 315 profile_(new TestingProfile()),
290 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()), 316 mock_tables_(new StrictMock<MockResourcePrefetchPredictorTables>()),
291 empty_url_data_(PREFETCH_KEY_TYPE_URL, std::string()), 317 empty_url_data_(PREFETCH_KEY_TYPE_URL, std::string()),
292 empty_host_data_(PREFETCH_KEY_TYPE_HOST, std::string()) {} 318 empty_host_data_(PREFETCH_KEY_TYPE_HOST, std::string()),
319 empty_url_redirect_data_(PREFETCH_KEY_TYPE_URL, std::string()),
320 empty_host_redirect_data_(PREFETCH_KEY_TYPE_HOST, std::string()) {}
293 321
294 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() { 322 ResourcePrefetchPredictorTest::~ResourcePrefetchPredictorTest() {
295 profile_.reset(NULL); 323 profile_.reset(NULL);
296 base::RunLoop().RunUntilIdle(); 324 base::RunLoop().RunUntilIdle();
297 } 325 }
298 326
299 void ResourcePrefetchPredictorTest::SetUp() { 327 void ResourcePrefetchPredictorTest::SetUp() {
300 InitializeSampleData(); 328 InitializeSampleData();
301 329
302 ASSERT_TRUE(profile_->CreateHistoryService(true, false)); 330 ASSERT_TRUE(profile_->CreateHistoryService(true, false));
303 profile_->BlockUntilHistoryProcessesPendingRequests(); 331 profile_->BlockUntilHistoryProcessesPendingRequests();
304 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( 332 EXPECT_TRUE(HistoryServiceFactory::GetForProfile(
305 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); 333 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS));
306 // Initialize the predictor with empty data. 334 // Initialize the predictor with empty data.
307 ResetPredictor(); 335 ResetPredictor();
308 EXPECT_EQ(predictor_->initialization_state_, 336 EXPECT_EQ(predictor_->initialization_state_,
309 ResourcePrefetchPredictor::NOT_INITIALIZED); 337 ResourcePrefetchPredictor::NOT_INITIALIZED);
310 EXPECT_CALL(*mock_tables_.get(), 338 EXPECT_CALL(*mock_tables_.get(),
311 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 339 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
312 Pointee(ContainerEq(PrefetchDataMap())))); 340 Pointee(ContainerEq(PrefetchDataMap())),
341 Pointee(ContainerEq(RedirectDataMap())),
342 Pointee(ContainerEq(RedirectDataMap()))));
313 InitializePredictor(); 343 InitializePredictor();
314 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 344 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
315 EXPECT_EQ(predictor_->initialization_state_, 345 EXPECT_EQ(predictor_->initialization_state_,
316 ResourcePrefetchPredictor::INITIALIZED); 346 ResourcePrefetchPredictor::INITIALIZED);
317 347
318 url_request_context_.set_job_factory(&url_request_job_factory_); 348 url_request_context_.set_job_factory(&url_request_job_factory_);
319 } 349 }
320 350
321 void ResourcePrefetchPredictorTest::TearDown() { 351 void ResourcePrefetchPredictorTest::TearDown() {
322 predictor_.reset(NULL); 352 predictor_.reset(NULL);
323 profile_->DestroyHistoryService(); 353 profile_->DestroyHistoryService();
324 } 354 }
325 355
326 void ResourcePrefetchPredictorTest::InitializeSampleData() { 356 void ResourcePrefetchPredictorTest::InitializeSampleData() {
327 { // Url data. 357 { // Url data.
328 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); 358 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com/");
329 google.last_visit = base::Time::FromInternalValue(1); 359 google.last_visit = base::Time::FromInternalValue(1);
330 google.resources.push_back(ResourceRow( 360 google.resources.push_back(ResourceRow(
331 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 3, 2, 361 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 3, 2,
332 1, 1.0, net::MEDIUM, false, false)); 362 1, 1.0, net::MEDIUM, false, false));
333 google.resources.push_back(ResourceRow("http://google.com/script3.js",
334 content::RESOURCE_TYPE_SCRIPT, 4, 0,
335 1, 2.1, net::MEDIUM, false, false));
336 google.resources.push_back(ResourceRow("http://google.com/script4.js",
337 content::RESOURCE_TYPE_SCRIPT, 11, 0,
338 0, 2.1, net::MEDIUM, false, false));
339 google.resources.push_back(ResourceRow("http://google.com/image1.png",
340 content::RESOURCE_TYPE_IMAGE, 6, 3,
341 0, 2.2, net::MEDIUM, false, false));
342 google.resources.push_back(ResourceRow( 363 google.resources.push_back(ResourceRow(
343 "http://google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 364 "http://google.com/script3.js", content::RESOURCE_TYPE_SCRIPT, 4, 0,
344 5.1, net::MEDIUM, false, false)); 365 1, 2.1, net::MEDIUM, false, false));
366 google.resources.push_back(ResourceRow(
367 "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 0,
368 0, 2.1, net::MEDIUM, false, false));
369 google.resources.push_back(ResourceRow(
370 "http://google.com/image1.png", content::RESOURCE_TYPE_IMAGE, 6, 3,
371 0, 2.2, net::MEDIUM, false, false));
372 google.resources.push_back(ResourceRow(
373 "http://google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0,
374 0, 5.1, net::MEDIUM, false, false));
345 375
346 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com/"); 376 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com/");
347 reddit.last_visit = base::Time::FromInternalValue(2); 377 reddit.last_visit = base::Time::FromInternalValue(2);
348 reddit.resources.push_back(ResourceRow( 378 reddit.resources.push_back(ResourceRow(
349 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 379 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT,
350 4, 0, 1, 1.0, net::MEDIUM, false, false)); 380 4, 0, 1, 1.0, net::MEDIUM, false, false));
351 reddit.resources.push_back(ResourceRow( 381 reddit.resources.push_back(ResourceRow(
352 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 382 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT,
353 2, 0, 0, 2.1, net::MEDIUM, false, false)); 383 2, 0, 0, 2.1, net::MEDIUM, false, false));
354 384
355 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/"); 385 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/");
356 yahoo.last_visit = base::Time::FromInternalValue(3); 386 yahoo.last_visit = base::Time::FromInternalValue(3);
357 yahoo.resources.push_back(ResourceRow("http://google.com/image.png", 387 yahoo.resources.push_back(ResourceRow(
358 content::RESOURCE_TYPE_IMAGE, 20, 1, 388 "http://google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1,
359 0, 10.0, net::MEDIUM, false, false)); 389 0, 10.0, net::MEDIUM, false, false));
360 390
361 test_url_data_.clear(); 391 test_url_data_.clear();
362 test_url_data_.insert(std::make_pair("http://www.google.com/", google)); 392 test_url_data_.insert(std::make_pair("http://www.google.com/", google));
363 test_url_data_.insert(std::make_pair("http://www.reddit.com/", reddit)); 393 test_url_data_.insert(std::make_pair("http://www.reddit.com/", reddit));
364 test_url_data_.insert(std::make_pair("http://www.yahoo.com/", yahoo)); 394 test_url_data_.insert(std::make_pair("http://www.yahoo.com/", yahoo));
365 } 395 }
366 396
367 { // Host data. 397 { // Host data.
368 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); 398 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com");
369 facebook.last_visit = base::Time::FromInternalValue(4); 399 facebook.last_visit = base::Time::FromInternalValue(4);
370 facebook.resources.push_back(ResourceRow( 400 facebook.resources.push_back(ResourceRow(
371 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 401 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET,
372 5, 2, 1, 1.1, net::MEDIUM, false, false)); 402 5, 2, 1, 1.1, net::MEDIUM, false, false));
373 facebook.resources.push_back(ResourceRow( 403 facebook.resources.push_back(ResourceRow(
374 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 404 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4,
375 0, 1, 2.1, net::MEDIUM, false, false)); 405 0, 1, 2.1, net::MEDIUM, false, false));
376 facebook.resources.push_back(ResourceRow( 406 facebook.resources.push_back(ResourceRow(
377 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, 407 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3,
378 0, 2.2, net::MEDIUM, false, false)); 408 0, 2.2, net::MEDIUM, false, false));
379 facebook.resources.push_back(ResourceRow( 409 facebook.resources.push_back(ResourceRow(
380 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 410 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2,
381 0, 0, 5.1, net::MEDIUM, false, false)); 411 0, 0, 5.1, net::MEDIUM, false, false));
382 facebook.resources.push_back( 412 facebook.resources.push_back(ResourceRow(
383 ResourceRow("http://www.resources.facebook.com/script.js", 413 "http://www.resources.facebook.com/script.js",
384 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, 414 content::RESOURCE_TYPE_SCRIPT, 11, 0,
385 false, false)); 415 0, 8.5, net::MEDIUM, false, false));
386 416
387 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); 417 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com");
388 yahoo.last_visit = base::Time::FromInternalValue(5); 418 yahoo.last_visit = base::Time::FromInternalValue(5);
389 yahoo.resources.push_back(ResourceRow("http://google.com/image.png", 419 yahoo.resources.push_back(ResourceRow("http://google.com/image.png",
390 content::RESOURCE_TYPE_IMAGE, 20, 1, 420 content::RESOURCE_TYPE_IMAGE, 20, 1,
391 0, 10.0, net::MEDIUM, false, false)); 421 0, 10.0, net::MEDIUM, false, false));
392 422
393 test_host_data_.clear(); 423 test_host_data_.clear();
394 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); 424 test_host_data_.insert(std::make_pair("www.facebook.com", facebook));
395 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); 425 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo));
396 } 426 }
427
428 { // Url redirect data.
429 RedirectData facebook(PREFETCH_KEY_TYPE_URL, "http://fb.com/google");
430 facebook.last_visit = base::Time::FromInternalValue(6);
431 facebook.redirects.push_back(RedirectRow(
432 "https://facebook.com/google", 5, 1, 0));
433 facebook.redirects.push_back(RedirectRow(
434 "https://facebook.com/login", 3, 5, 1));
435
436 RedirectData nytimes(PREFETCH_KEY_TYPE_URL, "http://nyt.com");
437 nytimes.last_visit = base::Time::FromInternalValue(7);
438 nytimes.redirects.push_back(RedirectRow(
439 "https://nytimes.com", 2, 0, 0));
440
441 RedirectData google(PREFETCH_KEY_TYPE_URL, "http://google.com");
442 google.last_visit = base::Time::FromInternalValue(8);
443 google.redirects.push_back(RedirectRow(
444 "https://google.com", 3, 0, 0));
445
446 test_url_redirect_data_.clear();
447 test_url_redirect_data_.insert(std::make_pair(
448 facebook.primary_key, facebook));
449 test_url_redirect_data_.insert(std::make_pair(
450 nytimes.primary_key, nytimes));
451 test_url_redirect_data_.insert(std::make_pair(
452 google.primary_key, google));
453 }
454
455 { // Host redirect data.
456 RedirectData bbc(PREFETCH_KEY_TYPE_HOST, "bbc.com");
457 bbc.last_visit = base::Time::FromInternalValue(9);
458 bbc.redirects.push_back(RedirectRow(
459 "www.bbc.com", 8, 4, 1));
460 bbc.redirects.push_back(RedirectRow(
461 "m.bbc.com", 5, 8, 0));
462 bbc.redirects.push_back(RedirectRow(
463 "bbc.co.uk", 1, 3, 0));
464
465 RedirectData microsoft(PREFETCH_KEY_TYPE_HOST, "microsoft.com");
466 microsoft.last_visit = base::Time::FromInternalValue(10);
467 microsoft.redirects.push_back(RedirectRow(
468 "www.microsoft.com", 10, 0, 0));
469
470 test_host_redirect_data_.clear();
471 test_host_redirect_data_.insert(std::make_pair(
472 bbc.primary_key, bbc));
473 test_host_redirect_data_.insert(std::make_pair(
474 microsoft.primary_key, microsoft));
475 }
397 } 476 }
398 477
399 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) { 478 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) {
400 // Tests that the predictor initializes correctly without any data. 479 // Tests that the predictor initializes correctly without any data.
401 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 480 EXPECT_TRUE(predictor_->url_table_cache_->empty());
402 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 481 EXPECT_TRUE(predictor_->host_table_cache_->empty());
482 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty());
483 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty());
403 } 484 }
404 485
405 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) { 486 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) {
406 // Tests that the history and the db tables data are loaded correctly. 487 // Tests that the history and the db tables data are loaded correctly.
407 AddUrlToHistory("http://www.google.com/", 4); 488 AddUrlToHistory("http://www.google.com/", 4);
408 AddUrlToHistory("http://www.yahoo.com/", 2); 489 AddUrlToHistory("http://www.yahoo.com/", 2);
409 490
410 EXPECT_CALL(*mock_tables_.get(), 491 EXPECT_CALL(*mock_tables_.get(),
411 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 492 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
412 Pointee(ContainerEq(PrefetchDataMap())))) 493 Pointee(ContainerEq(PrefetchDataMap())),
494 Pointee(ContainerEq(RedirectDataMap())),
495 Pointee(ContainerEq(RedirectDataMap()))))
413 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 496 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
414 SetArgPointee<1>(test_host_data_))); 497 SetArgPointee<1>(test_host_data_),
498 SetArgPointee<2>(test_url_redirect_data_),
499 SetArgPointee<3>(test_host_redirect_data_)));
415 500
416 ResetPredictor(); 501 ResetPredictor();
417 InitializePredictor(); 502 InitializePredictor();
418 503
419 // Test that the internal variables correctly initialized. 504 // Test that the internal variables correctly initialized.
420 EXPECT_EQ(predictor_->initialization_state_, 505 EXPECT_EQ(predictor_->initialization_state_,
421 ResourcePrefetchPredictor::INITIALIZED); 506 ResourcePrefetchPredictor::INITIALIZED);
422 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 507 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
423 508
424 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); 509 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_);
425 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); 510 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_);
511 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_);
512 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_);
426 } 513 }
427 514
428 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { 515 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) {
429 // Single navigation but history count is low, so should not record. 516 // Single navigation but history count is low, so should not record.
430 AddUrlToHistory("http://www.google.com", 1); 517 AddUrlToHistory("http://www.google.com", 1);
431 518
432 URLRequestSummary main_frame = CreateURLRequestSummary( 519 URLRequestSummary main_frame = CreateURLRequestSummary(
433 1, 1, "http://www.google.com", "http://www.google.com", 520 1, 1, "http://www.google.com");
434 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
435 predictor_->RecordURLRequest(main_frame); 521 predictor_->RecordURLRequest(main_frame);
436 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 522 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
437 523
524 URLRequestSummary main_frame_redirect = CreateRedirectRequestSummary(
525 1, 1, "http://www.google.com", "https://www.google.com");
526 predictor_->RecordURLRedirect(main_frame_redirect);
527 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
528 main_frame = CreateURLRequestSummary(1, 1, "https://www.google.com");
529
438 // Now add a few subresources. 530 // Now add a few subresources.
439 URLRequestSummary resource1 = CreateURLRequestSummary( 531 URLRequestSummary resource1 = CreateURLRequestSummary(
440 1, 1, "http://www.google.com", "http://google.com/style1.css", 532 1, 1, "https://www.google.com", "https://google.com/style1.css",
441 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 533 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
442 predictor_->RecordURLResponse(resource1); 534 predictor_->RecordURLResponse(resource1);
443 URLRequestSummary resource2 = CreateURLRequestSummary( 535 URLRequestSummary resource2 = CreateURLRequestSummary(
444 1, 1, "http://www.google.com", "http://google.com/script1.js", 536 1, 1, "https://www.google.com", "https://google.com/script1.js",
445 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 537 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
446 predictor_->RecordURLResponse(resource2); 538 predictor_->RecordURLResponse(resource2);
447 URLRequestSummary resource3 = CreateURLRequestSummary( 539 URLRequestSummary resource3 = CreateURLRequestSummary(
448 1, 1, "http://www.google.com", "http://google.com/script2.js", 540 1, 1, "https://www.google.com", "https://google.com/script2.js",
449 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 541 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
450 predictor_->RecordURLResponse(resource3); 542 predictor_->RecordURLResponse(resource3);
451 543
452 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 544 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
453 host_data.resources.push_back(ResourceRow( 545 host_data.resources.push_back(ResourceRow(
454 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 546 "https://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
455 0, 1.0, net::MEDIUM, false, false)); 547 0, 1.0, net::MEDIUM, false, false));
456 host_data.resources.push_back(ResourceRow("http://google.com/script1.js", 548 host_data.resources.push_back(ResourceRow(
457 content::RESOURCE_TYPE_SCRIPT, 1, 0, 549 "https://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0,
458 0, 2.0, net::MEDIUM, false, false)); 550 0, 2.0, net::MEDIUM, false, false));
459 host_data.resources.push_back(ResourceRow("http://google.com/script2.js", 551 host_data.resources.push_back(ResourceRow(
460 content::RESOURCE_TYPE_SCRIPT, 1, 0, 552 "https://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0,
461 0, 3.0, net::MEDIUM, false, false)); 553 0, 3.0, net::MEDIUM, false, false));
462 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 554 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data,
555 empty_url_redirect_data_,
556 empty_host_redirect_data_));
463 557
464 predictor_->OnNavigationComplete(main_frame.navigation_id); 558 predictor_->RecordMainFrameLoadComplete(main_frame.navigation_id);
465 profile_->BlockUntilHistoryProcessesPendingRequests(); 559 profile_->BlockUntilHistoryProcessesPendingRequests();
466 } 560 }
467 561
468 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 562 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
469 // Single navigation that will be recorded. Will check for duplicate 563 // Single navigation that will be recorded. Will check for duplicate
470 // resources and also for number of resources saved. 564 // resources and also for number of resources saved.
471 AddUrlToHistory("http://www.google.com", 4); 565 AddUrlToHistory("http://www.google.com", 4);
472 566
473 URLRequestSummary main_frame = CreateURLRequestSummary( 567 URLRequestSummary main_frame = CreateURLRequestSummary(
474 1, 1, "http://www.google.com", "http://www.google.com", 568 1, 1, "http://www.google.com");
475 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
476 predictor_->RecordURLRequest(main_frame); 569 predictor_->RecordURLRequest(main_frame);
477 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 570 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
478 571
479 URLRequestSummary resource1 = CreateURLRequestSummary( 572 URLRequestSummary resource1 = CreateURLRequestSummary(
480 1, 1, "http://www.google.com", "http://google.com/style1.css", 573 1, 1, "http://www.google.com", "http://google.com/style1.css",
481 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 574 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
482 predictor_->RecordURLResponse(resource1); 575 predictor_->RecordURLResponse(resource1);
483 URLRequestSummary resource2 = CreateURLRequestSummary( 576 URLRequestSummary resource2 = CreateURLRequestSummary(
484 1, 1, "http://www.google.com", "http://google.com/script1.js", 577 1, 1, "http://www.google.com", "http://google.com/script1.js",
485 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 578 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
(...skipping 25 matching lines...) Expand all
511 0, 1.0, net::MEDIUM, false, false)); 604 0, 1.0, net::MEDIUM, false, false));
512 url_data.resources.push_back(ResourceRow("http://google.com/script1.js", 605 url_data.resources.push_back(ResourceRow("http://google.com/script1.js",
513 content::RESOURCE_TYPE_SCRIPT, 1, 0, 606 content::RESOURCE_TYPE_SCRIPT, 1, 0,
514 0, 2.0, net::MEDIUM, false, false)); 607 0, 2.0, net::MEDIUM, false, false));
515 url_data.resources.push_back(ResourceRow("http://google.com/script2.js", 608 url_data.resources.push_back(ResourceRow("http://google.com/script2.js",
516 content::RESOURCE_TYPE_SCRIPT, 1, 0, 609 content::RESOURCE_TYPE_SCRIPT, 1, 0,
517 0, 3.0, net::MEDIUM, false, false)); 610 0, 3.0, net::MEDIUM, false, false));
518 url_data.resources.push_back(ResourceRow( 611 url_data.resources.push_back(ResourceRow(
519 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 612 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
520 0, 7.0, net::MEDIUM, false, false)); 613 0, 7.0, net::MEDIUM, false, false));
521 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 614 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_,
615 empty_url_redirect_data_,
616 empty_host_redirect_data_));
522 617
523 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 618 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
524 host_data.resources = url_data.resources; 619 host_data.resources = url_data.resources;
525 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 620 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data,
621 empty_url_redirect_data_,
622 empty_host_redirect_data_));
526 623
527 predictor_->OnNavigationComplete(main_frame.navigation_id); 624 predictor_->OnNavigationComplete(main_frame.navigation_id);
528 profile_->BlockUntilHistoryProcessesPendingRequests(); 625 profile_->BlockUntilHistoryProcessesPendingRequests();
529 } 626 }
530 627
531 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { 628 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) {
532 // Tests that navigation is recorded correctly for URL already present in 629 // Tests that navigation is recorded correctly for URL already present in
533 // the database cache. 630 // the database cache.
534 AddUrlToHistory("http://www.google.com", 4); 631 AddUrlToHistory("http://www.google.com", 4);
535 632
536 EXPECT_CALL(*mock_tables_.get(), 633 EXPECT_CALL(*mock_tables_.get(),
537 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 634 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
538 Pointee(ContainerEq(PrefetchDataMap())))) 635 Pointee(ContainerEq(PrefetchDataMap())),
636 Pointee(ContainerEq(RedirectDataMap())),
637 Pointee(ContainerEq(RedirectDataMap()))))
539 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 638 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
540 SetArgPointee<1>(test_host_data_))); 639 SetArgPointee<1>(test_host_data_)));
541 ResetPredictor(); 640 ResetPredictor();
542 InitializePredictor(); 641 InitializePredictor();
543 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); 642 EXPECT_EQ(3U, predictor_->url_table_cache_->size());
544 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); 643 EXPECT_EQ(2U, predictor_->host_table_cache_->size());
545 644
546 URLRequestSummary main_frame = CreateURLRequestSummary( 645 URLRequestSummary main_frame = CreateURLRequestSummary(
547 1, 1, "http://www.google.com", "http://www.google.com", 646 1, 1, "http://www.google.com", "http://www.google.com",
548 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 647 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 0, 1.0, net::MEDIUM, false, false)); 683 0, 1.0, net::MEDIUM, false, false));
585 url_data.resources.push_back(ResourceRow("http://google.com/script1.js", 684 url_data.resources.push_back(ResourceRow("http://google.com/script1.js",
586 content::RESOURCE_TYPE_SCRIPT, 1, 0, 685 content::RESOURCE_TYPE_SCRIPT, 1, 0,
587 0, 2.0, net::MEDIUM, false, false)); 686 0, 2.0, net::MEDIUM, false, false));
588 url_data.resources.push_back(ResourceRow("http://google.com/script4.js", 687 url_data.resources.push_back(ResourceRow("http://google.com/script4.js",
589 content::RESOURCE_TYPE_SCRIPT, 11, 1, 688 content::RESOURCE_TYPE_SCRIPT, 11, 1,
590 1, 2.1, net::MEDIUM, false, false)); 689 1, 2.1, net::MEDIUM, false, false));
591 url_data.resources.push_back(ResourceRow("http://google.com/script2.js", 690 url_data.resources.push_back(ResourceRow("http://google.com/script2.js",
592 content::RESOURCE_TYPE_SCRIPT, 1, 0, 691 content::RESOURCE_TYPE_SCRIPT, 1, 0,
593 0, 3.0, net::MEDIUM, false, false)); 692 0, 3.0, net::MEDIUM, false, false));
594 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 693 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_,
694 empty_url_redirect_data_,
695 empty_host_redirect_data_));
595 696
596 EXPECT_CALL( 697 EXPECT_CALL(
597 *mock_tables_.get(), 698 *mock_tables_.get(),
598 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 699 DeleteSingleResourceDataPoint("www.facebook.com",
700 PREFETCH_KEY_TYPE_HOST));
599 701
600 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 702 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
601 host_data.resources.push_back(ResourceRow( 703 host_data.resources.push_back(ResourceRow(
602 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 704 "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
603 0, 1.0, net::MEDIUM, false, false)); 705 0, 1.0, net::MEDIUM, false, false));
604 host_data.resources.push_back(ResourceRow("http://google.com/script1.js", 706 host_data.resources.push_back(ResourceRow("http://google.com/script1.js",
605 content::RESOURCE_TYPE_SCRIPT, 1, 0, 707 content::RESOURCE_TYPE_SCRIPT, 1, 0,
606 0, 2.0, net::MEDIUM, false, false)); 708 0, 2.0, net::MEDIUM, false, false));
607 host_data.resources.push_back(ResourceRow("http://google.com/script2.js", 709 host_data.resources.push_back(ResourceRow("http://google.com/script2.js",
608 content::RESOURCE_TYPE_SCRIPT, 1, 0, 710 content::RESOURCE_TYPE_SCRIPT, 1, 0,
609 0, 3.0, net::MEDIUM, false, false)); 711 0, 3.0, net::MEDIUM, false, false));
610 host_data.resources.push_back(ResourceRow( 712 host_data.resources.push_back(ResourceRow(
611 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 713 "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0,
612 0, 7.0, net::MEDIUM, false, false)); 714 0, 7.0, net::MEDIUM, false, false));
613 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 715 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data,
716 empty_url_redirect_data_,
717 empty_host_redirect_data_));
614 718
615 predictor_->OnNavigationComplete(main_frame.navigation_id); 719 predictor_->OnNavigationComplete(main_frame.navigation_id);
616 profile_->BlockUntilHistoryProcessesPendingRequests(); 720 profile_->BlockUntilHistoryProcessesPendingRequests();
617 } 721 }
618 722
619 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { 723 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) {
620 // Tests that a URL is deleted before another is added if the cache is full. 724 // Tests that a URL is deleted before another is added if the cache is full.
621 AddUrlToHistory("http://www.nike.com/", 4); 725 AddUrlToHistory("http://www.nike.com/", 4);
622 726
623 EXPECT_CALL(*mock_tables_.get(), 727 EXPECT_CALL(*mock_tables_.get(),
624 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 728 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
625 Pointee(ContainerEq(PrefetchDataMap())))) 729 Pointee(ContainerEq(PrefetchDataMap())),
730 Pointee(ContainerEq(RedirectDataMap())),
731 Pointee(ContainerEq(RedirectDataMap()))))
626 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), 732 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_),
627 SetArgPointee<1>(test_host_data_))); 733 SetArgPointee<1>(test_host_data_)));
628 ResetPredictor(); 734 ResetPredictor();
629 InitializePredictor(); 735 InitializePredictor();
630 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); 736 EXPECT_EQ(3U, predictor_->url_table_cache_->size());
631 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); 737 EXPECT_EQ(2U, predictor_->host_table_cache_->size());
632 738
633 URLRequestSummary main_frame = CreateURLRequestSummary( 739 URLRequestSummary main_frame = CreateURLRequestSummary(
634 1, 1, "http://www.nike.com", "http://www.nike.com", 740 1, 1, "http://www.nike.com", "http://www.nike.com",
635 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 741 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
636 predictor_->RecordURLRequest(main_frame); 742 predictor_->RecordURLRequest(main_frame);
637 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 743 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
638 744
639 URLRequestSummary resource1 = CreateURLRequestSummary( 745 URLRequestSummary resource1 = CreateURLRequestSummary(
640 1, 1, "http://www.nike.com", "http://nike.com/style1.css", 746 1, 1, "http://www.nike.com", "http://nike.com/style1.css",
641 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 747 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
642 predictor_->RecordURLResponse(resource1); 748 predictor_->RecordURLResponse(resource1);
643 URLRequestSummary resource2 = CreateURLRequestSummary( 749 URLRequestSummary resource2 = CreateURLRequestSummary(
644 1, 1, "http://www.nike.com", "http://nike.com/image2.png", 750 1, 1, "http://www.nike.com", "http://nike.com/image2.png",
645 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 751 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
646 predictor_->RecordURLResponse(resource2); 752 predictor_->RecordURLResponse(resource2);
647 753
648 EXPECT_CALL( 754 EXPECT_CALL(
649 *mock_tables_.get(), 755 *mock_tables_.get(),
650 DeleteSingleDataPoint("http://www.google.com/", PREFETCH_KEY_TYPE_URL)); 756 DeleteSingleResourceDataPoint("http://www.google.com/",
757 PREFETCH_KEY_TYPE_URL));
651 EXPECT_CALL( 758 EXPECT_CALL(
652 *mock_tables_.get(), 759 *mock_tables_.get(),
653 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 760 DeleteSingleResourceDataPoint("www.facebook.com",
761 PREFETCH_KEY_TYPE_HOST));
654 762
655 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/"); 763 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/");
656 url_data.resources.push_back(ResourceRow( 764 url_data.resources.push_back(ResourceRow(
657 "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 765 "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0,
658 1.0, net::MEDIUM, false, false)); 766 1.0, net::MEDIUM, false, false));
659 url_data.resources.push_back(ResourceRow("http://nike.com/image2.png", 767 url_data.resources.push_back(ResourceRow("http://nike.com/image2.png",
660 content::RESOURCE_TYPE_IMAGE, 1, 0, 768 content::RESOURCE_TYPE_IMAGE, 1, 0,
661 0, 2.0, net::MEDIUM, false, false)); 769 0, 2.0, net::MEDIUM, false, false));
662 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 770 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_,
771 empty_url_redirect_data_,
772 empty_host_redirect_data_));
663 773
664 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com"); 774 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com");
665 host_data.resources = url_data.resources; 775 host_data.resources = url_data.resources;
666 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 776 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data,
777 empty_url_redirect_data_,
778 empty_host_redirect_data_));
667 779
668 predictor_->OnNavigationComplete(main_frame.navigation_id); 780 predictor_->OnNavigationComplete(main_frame.navigation_id);
669 profile_->BlockUntilHistoryProcessesPendingRequests(); 781 profile_->BlockUntilHistoryProcessesPendingRequests();
670 } 782 }
671 783
784 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlNotInDB) {
785 AddUrlToHistory("https://facebook.com/google", 4);
786
787 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google");
788 predictor_->RecordURLRequest(fb1);
789 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
790
791 URLRequestSummary fb2 = CreateRedirectRequestSummary(1, 1,
792 "http://fb.com/google", "http://facebook.com/google");
793 predictor_->RecordURLRedirect(fb2);
794 URLRequestSummary fb3 = CreateRedirectRequestSummary(1, 1,
795 "http://facebook.com/google", "https://facebook.com/google");
796 predictor_->RecordURLRedirect(fb3);
797 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google");
798
799 // Since the navigation hasn't resources, corresponding entry
800 // in resource table will be deleted
801 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
802 "https://facebook.com/google", PREFETCH_KEY_TYPE_URL));
803 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
804 "facebook.com", PREFETCH_KEY_TYPE_HOST));
805
806 RedirectData url_redirect_data(PREFETCH_KEY_TYPE_URL, "http://fb.com/google");
807 url_redirect_data.redirects.push_back(RedirectRow(
808 "https://facebook.com/google", 1, 0, 0));
809 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, empty_host_data_,
810 url_redirect_data,
811 empty_host_redirect_data_));
812
813 RedirectData host_redirect_data(PREFETCH_KEY_TYPE_HOST, "fb.com");
814 host_redirect_data.redirects.push_back(RedirectRow(
815 "facebook.com", 1, 0, 0));
816 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, empty_host_data_,
817 empty_url_redirect_data_,
818 host_redirect_data));
819
820 predictor_->RecordMainFrameLoadComplete(fb_end);
821 profile_->BlockUntilHistoryProcessesPendingRequests();
822 }
823
824 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) {
825 // Tests that redirect is recorded correctly for URL already present in
826 // the database cache.
827 AddUrlToHistory("https://facebook.com/google", 4);
828
829 EXPECT_CALL(*mock_tables_.get(),
830 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
831 Pointee(ContainerEq(PrefetchDataMap())),
832 Pointee(ContainerEq(RedirectDataMap())),
833 Pointee(ContainerEq(RedirectDataMap()))))
834 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_),
835 SetArgPointee<3>(test_host_redirect_data_)));
836 ResetPredictor();
837 InitializePredictor();
838 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size());
839 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size());
840
841 URLRequestSummary fb1 = CreateURLRequestSummary(1, 1, "http://fb.com/google");
842 predictor_->RecordURLRequest(fb1);
843 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
844
845 URLRequestSummary fb2 = CreateRedirectRequestSummary(1, 1,
846 "http://fb.com/google", "http://facebook.com/google");
847 predictor_->RecordURLRedirect(fb2);
848 URLRequestSummary fb3 = CreateRedirectRequestSummary(1, 1,
849 "http://facebook.com/google", "https://facebook.com/google");
850 predictor_->RecordURLRedirect(fb3);
851 NavigationID fb_end = CreateNavigationID(1, 1, "https://facebook.com/google");
852
853 // Oldest entries in tables will be superseded and deleted
854 EXPECT_CALL(*mock_tables_.get(), DeleteSingleRedirectDataPoint(
855 "bbc.com", PREFETCH_KEY_TYPE_HOST));
856
857 // Since the navigation hasn't resources, corresponding entry
858 // in resource table will be deleted
859 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
860 "https://facebook.com/google", PREFETCH_KEY_TYPE_URL));
861 EXPECT_CALL(*mock_tables_.get(), DeleteSingleResourceDataPoint(
862 "facebook.com", PREFETCH_KEY_TYPE_HOST));
863
864 RedirectData url_redirect_data(PREFETCH_KEY_TYPE_URL, "http://fb.com/google");
865 url_redirect_data.redirects.push_back(RedirectRow(
866 "https://facebook.com/google", 6, 1, 0));
867 // Exising redirect to https://facebook.com/login will be deleted because of
868 // too many consecutive misses.
869 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, empty_host_data_,
870 url_redirect_data,
871 empty_host_redirect_data_));
872
873 RedirectData host_redirect_data(PREFETCH_KEY_TYPE_HOST, "fb.com");
874 host_redirect_data.redirects.push_back(RedirectRow(
875 "facebook.com", 1, 0, 0));
876 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, empty_host_data_,
877 empty_url_redirect_data_,
878 host_redirect_data));
879
880 predictor_->RecordMainFrameLoadComplete(fb_end);
881 profile_->BlockUntilHistoryProcessesPendingRequests();
882 }
883
672 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) { 884 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) {
673 // Add some dummy entries to cache. 885 // Add some dummy entries to cache.
674 predictor_->url_table_cache_->insert(std::make_pair( 886 predictor_->url_table_cache_->insert(std::make_pair(
675 "http://www.google.com/page1.html", 887 "http://www.google.com/page1.html",
676 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page1.html"))); 888 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page1.html")));
677 predictor_->url_table_cache_->insert(std::make_pair( 889 predictor_->url_table_cache_->insert(std::make_pair(
678 "http://www.google.com/page2.html", 890 "http://www.google.com/page2.html",
679 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page2.html"))); 891 PrefetchData(PREFETCH_KEY_TYPE_URL, "http://www.google.com/page2.html")));
680 predictor_->url_table_cache_->insert(std::make_pair( 892 predictor_->url_table_cache_->insert(std::make_pair(
681 "http://www.yahoo.com/", 893 "http://www.yahoo.com/",
(...skipping 22 matching lines...) Expand all
704 916
705 std::vector<std::string> urls_to_delete, hosts_to_delete; 917 std::vector<std::string> urls_to_delete, hosts_to_delete;
706 urls_to_delete.push_back("http://www.google.com/page2.html"); 918 urls_to_delete.push_back("http://www.google.com/page2.html");
707 urls_to_delete.push_back("http://www.apple.com/"); 919 urls_to_delete.push_back("http://www.apple.com/");
708 urls_to_delete.push_back("http://www.nike.com/"); 920 urls_to_delete.push_back("http://www.nike.com/");
709 hosts_to_delete.push_back("www.google.com"); 921 hosts_to_delete.push_back("www.google.com");
710 hosts_to_delete.push_back("www.apple.com"); 922 hosts_to_delete.push_back("www.apple.com");
711 923
712 EXPECT_CALL( 924 EXPECT_CALL(
713 *mock_tables_.get(), 925 *mock_tables_.get(),
714 DeleteData(ContainerEq(urls_to_delete), ContainerEq(hosts_to_delete))); 926 DeleteResourceData(ContainerEq(urls_to_delete),
927 ContainerEq(hosts_to_delete)));
715 928
716 predictor_->DeleteUrls(rows); 929 predictor_->DeleteUrls(rows);
717 EXPECT_EQ(2U, predictor_->url_table_cache_->size()); 930 EXPECT_EQ(2U, predictor_->url_table_cache_->size());
718 EXPECT_EQ(1U, predictor_->host_table_cache_->size()); 931 EXPECT_EQ(1U, predictor_->host_table_cache_->size());
719 932
720 EXPECT_CALL(*mock_tables_.get(), DeleteAllData()); 933 EXPECT_CALL(*mock_tables_.get(), DeleteAllData());
721 934
722 predictor_->DeleteAllUrls(); 935 predictor_->DeleteAllUrls();
723 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 936 EXPECT_TRUE(predictor_->url_table_cache_->empty());
724 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 937 EXPECT_TRUE(predictor_->host_table_cache_->empty());
(...skipping 10 matching lines...) Expand all
735 2, 1, "http://www.yahoo.com", "http://www.yahoo.com", 948 2, 1, "http://www.yahoo.com", "http://www.yahoo.com",
736 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 949 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
737 950
738 predictor_->OnMainFrameRequest(summary1); 951 predictor_->OnMainFrameRequest(summary1);
739 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 952 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
740 predictor_->OnMainFrameRequest(summary2); 953 predictor_->OnMainFrameRequest(summary2);
741 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 954 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
742 predictor_->OnMainFrameRequest(summary3); 955 predictor_->OnMainFrameRequest(summary3);
743 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 956 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
744 957
745 // Insert anther with same navigation id. It should replace. 958 // Insert another with same navigation id. It should replace.
746 URLRequestSummary summary4 = CreateURLRequestSummary( 959 URLRequestSummary summary4 = CreateURLRequestSummary(
747 1, 1, "http://www.nike.com", "http://www.nike.com", 960 1, 1, "http://www.nike.com", "http://www.nike.com",
748 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 961 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
749 URLRequestSummary summary5 = CreateURLRequestSummary( 962 URLRequestSummary summary5 = CreateURLRequestSummary(
750 1, 2, "http://www.google.com", "http://www.google.com", 963 1, 2, "http://www.google.com", "http://www.google.com",
751 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 964 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
752 965
753 predictor_->OnMainFrameRequest(summary4); 966 predictor_->OnMainFrameRequest(summary4);
754 EXPECT_EQ(3U, predictor_->inflight_navigations_.size()); 967 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
755 968
(...skipping 11 matching lines...) Expand all
767 980
768 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) != 981 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary3.navigation_id) !=
769 predictor_->inflight_navigations_.end()); 982 predictor_->inflight_navigations_.end());
770 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) != 983 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary4.navigation_id) !=
771 predictor_->inflight_navigations_.end()); 984 predictor_->inflight_navigations_.end());
772 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) != 985 EXPECT_TRUE(predictor_->inflight_navigations_.find(summary6.navigation_id) !=
773 predictor_->inflight_navigations_.end()); 986 predictor_->inflight_navigations_.end());
774 } 987 }
775 988
776 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) { 989 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRedirect) {
777 URLRequestSummary summary1 = CreateURLRequestSummary( 990 URLRequestSummary yahoo = CreateURLRequestSummary(1, 1, "http://yahoo.com");
778 1, 1, "http://www.google.com", "http://www.google.com",
779 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
780 URLRequestSummary summary2 = CreateURLRequestSummary(
781 1, 2, "http://www.google.com", "http://www.google.com",
782 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
783 URLRequestSummary summary3 = CreateURLRequestSummary(
784 2, 1, "http://www.yahoo.com", "http://www.yahoo.com",
785 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
786 991
787 predictor_->OnMainFrameRedirect(summary1); 992 URLRequestSummary bbc1 = CreateURLRequestSummary(2, 2, "http://bbc.com");
993 URLRequestSummary bbc2 = CreateRedirectRequestSummary(
994 2, 2, "http://bbc.com", "https://www.bbc.com");
995 NavigationID bbc_end = CreateNavigationID(2, 2, "https://www.bbc.com");
996
997 URLRequestSummary youtube1 = CreateURLRequestSummary(
998 1, 2, "http://youtube.com");
999 URLRequestSummary youtube2 = CreateRedirectRequestSummary(
1000 1, 2, "http://youtube.com", "https://youtube.com");
1001 NavigationID youtube_end = CreateNavigationID(1, 2, "https://youtube.com");
1002
1003 URLRequestSummary nyt1 = CreateURLRequestSummary(2, 1, "http://nyt.com");
1004 URLRequestSummary nyt2 = CreateRedirectRequestSummary(
1005 2, 1, "http://nyt.com", "http://nytimes.com");
1006 URLRequestSummary nyt3 = CreateRedirectRequestSummary(
1007 2, 1, "http://nytimes.com", "http://m.nytimes.com");
1008 NavigationID nyt_end = CreateNavigationID(2, 1, "http://m.nytimes.com");
1009
1010 URLRequestSummary fb1 = CreateURLRequestSummary(1, 3, "http://fb.com");
1011 URLRequestSummary fb2 = CreateRedirectRequestSummary(
1012 1, 3, "http://fb.com", "http://facebook.com");
1013 URLRequestSummary fb3 = CreateRedirectRequestSummary(
1014 1, 3, "http://facebook.com","https://facebook.com");
1015 URLRequestSummary fb4 = CreateRedirectRequestSummary(
1016 1, 3, "https://facebook.com",
1017 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1018 NavigationID fb_end = CreateNavigationID(1, 3,
1019 "https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr");
1020
1021 // Redirect with empty redirect_url will be deleted
1022 predictor_->OnMainFrameRequest(yahoo);
1023 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
1024 predictor_->OnMainFrameRedirect(yahoo);
788 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1025 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
789 1026
790 predictor_->OnMainFrameRequest(summary1); 1027 // Redirect without previous request works fine
1028 // predictor_->OnMainFrameRequest(bbc1) missing
1029 predictor_->OnMainFrameRedirect(bbc2);
791 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1030 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
792 predictor_->OnMainFrameRequest(summary2); 1031 EXPECT_EQ(bbc1.navigation_id.main_frame_url,
1032 predictor_->inflight_navigations_[bbc_end]->initial_url);
1033
1034 // http://youtube.com -> https://youtube.com
1035 predictor_->OnMainFrameRequest(youtube1);
793 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1036 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1037 predictor_->OnMainFrameRedirect(youtube2);
1038 EXPECT_EQ(2U, predictor_->inflight_navigations_.size());
1039 EXPECT_EQ(youtube1.navigation_id.main_frame_url,
1040 predictor_->inflight_navigations_[youtube_end]->initial_url);
794 1041
795 predictor_->OnMainFrameRedirect(summary3); 1042 // http://nyt.com -> http://nytimes.com -> http://m.nytimes.com
796 EXPECT_EQ(2U, predictor_->inflight_navigations_.size()); 1043 predictor_->OnMainFrameRequest(nyt1);
797 predictor_->OnMainFrameRedirect(summary1); 1044 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
798 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1045 predictor_->OnMainFrameRedirect(nyt2);
799 predictor_->OnMainFrameRedirect(summary2); 1046 predictor_->OnMainFrameRedirect(nyt3);
800 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1047 EXPECT_EQ(3U, predictor_->inflight_navigations_.size());
1048 EXPECT_EQ(nyt1.navigation_id.main_frame_url,
1049 predictor_->inflight_navigations_[nyt_end]->initial_url);
1050
1051 // http://fb.com -> http://facebook.com -> https://facebook.com ->
1052 // https://m.facebook.com/?refsrc=https%3A%2F%2Fwww.facebook.com%2F&_rdr
1053 predictor_->OnMainFrameRequest(fb1);
1054 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1055 predictor_->OnMainFrameRedirect(fb2);
1056 predictor_->OnMainFrameRedirect(fb3);
1057 predictor_->OnMainFrameRedirect(fb4);
1058 EXPECT_EQ(4U, predictor_->inflight_navigations_.size());
1059 EXPECT_EQ(fb1.navigation_id.main_frame_url,
1060 predictor_->inflight_navigations_[fb_end]->initial_url);
801 } 1061 }
802 1062
803 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) { 1063 TEST_F(ResourcePrefetchPredictorTest, OnSubresourceResponse) {
804 // If there is no inflight navigation, nothing happens. 1064 // If there is no inflight navigation, nothing happens.
805 URLRequestSummary resource1 = CreateURLRequestSummary( 1065 URLRequestSummary resource1 = CreateURLRequestSummary(
806 1, 1, "http://www.google.com", "http://google.com/style1.css", 1066 1, 1, "http://www.google.com", "http://google.com/style1.css",
807 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 1067 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
808 predictor_->OnSubresourceResponse(resource1); 1068 predictor_->OnSubresourceResponse(resource1);
809 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); 1069 EXPECT_TRUE(predictor_->inflight_navigations_.empty());
810 1070
(...skipping 10 matching lines...) Expand all
821 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1081 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
822 URLRequestSummary resource3 = CreateURLRequestSummary( 1082 URLRequestSummary resource3 = CreateURLRequestSummary(
823 1, 1, "http://www.google.com", "http://google.com/script2.js", 1083 1, 1, "http://www.google.com", "http://google.com/script2.js",
824 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 1084 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
825 predictor_->OnSubresourceResponse(resource1); 1085 predictor_->OnSubresourceResponse(resource1);
826 predictor_->OnSubresourceResponse(resource2); 1086 predictor_->OnSubresourceResponse(resource2);
827 predictor_->OnSubresourceResponse(resource3); 1087 predictor_->OnSubresourceResponse(resource3);
828 1088
829 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); 1089 EXPECT_EQ(1U, predictor_->inflight_navigations_.size());
830 EXPECT_EQ(3U, 1090 EXPECT_EQ(3U,
831 predictor_->inflight_navigations_[main_frame1.navigation_id]->size()); 1091 predictor_->inflight_navigations_[main_frame1.navigation_id]->
1092 requests->size());
832 EXPECT_TRUE(URLRequestSummaryAreEqual( 1093 EXPECT_TRUE(URLRequestSummaryAreEqual(
833 resource1, 1094 resource1,
834 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(0))); 1095 predictor_->inflight_navigations_[main_frame1.navigation_id]->
1096 requests->at(0)));
835 EXPECT_TRUE(URLRequestSummaryAreEqual( 1097 EXPECT_TRUE(URLRequestSummaryAreEqual(
836 resource2, 1098 resource2,
837 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(1))); 1099 predictor_->inflight_navigations_[main_frame1.navigation_id]->
1100 requests->at(1)));
838 EXPECT_TRUE(URLRequestSummaryAreEqual( 1101 EXPECT_TRUE(URLRequestSummaryAreEqual(
839 resource3, 1102 resource3,
840 predictor_->inflight_navigations_[main_frame1.navigation_id]->at(2))); 1103 predictor_->inflight_navigations_[main_frame1.navigation_id]->
1104 requests->at(2)));
841 } 1105 }
842 1106
843 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) { 1107 TEST_F(ResourcePrefetchPredictorTest, HandledResourceTypes) {
844 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1108 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
845 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type")); 1109 content::RESOURCE_TYPE_STYLESHEET, "bogus/mime-type"));
846 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType( 1110 EXPECT_TRUE(ResourcePrefetchPredictor::IsHandledResourceType(
847 content::RESOURCE_TYPE_STYLESHEET, "")); 1111 content::RESOURCE_TYPE_STYLESHEET, ""));
848 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1112 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
849 content::RESOURCE_TYPE_WORKER, "text/css")); 1113 content::RESOURCE_TYPE_WORKER, "text/css"));
850 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType( 1114 EXPECT_FALSE(ResourcePrefetchPredictor::IsHandledResourceType(
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
1064 url_request_job_factory_.set_response_info(response_info); 1328 url_request_job_factory_.set_response_info(response_info);
1065 std::unique_ptr<net::URLRequest> request_etag = 1329 std::unique_ptr<net::URLRequest> request_etag =
1066 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, 1330 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1067 content::RESOURCE_TYPE_PREFETCH, 1, 1, true); 1331 content::RESOURCE_TYPE_PREFETCH, 1, 1, true);
1068 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary)); 1332 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1069 EXPECT_TRUE(summary.has_validators); 1333 EXPECT_TRUE(summary.has_validators);
1070 EXPECT_TRUE(summary.always_revalidate); 1334 EXPECT_TRUE(summary.always_revalidate);
1071 } 1335 }
1072 1336
1073 } // namespace predictors 1337 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698