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

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

Issue 2260573002: predictors: Track whether resources have validators, and require validation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Typo. Created 4 years, 4 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 *os << "[" << data.key_type << "," << data.primary_key 52 *os << "[" << data.key_type << "," << data.primary_key
53 << "," << data.last_visit.ToInternalValue() << "]\n"; 53 << "," << data.last_visit.ToInternalValue() << "]\n";
54 for (ResourceRows::const_iterator it = data.resources.begin(); 54 for (ResourceRows::const_iterator it = data.resources.begin();
55 it != data.resources.end(); ++it) { 55 it != data.resources.end(); ++it) {
56 *os << "\t\t"; 56 *os << "\t\t";
57 PrintTo(*it, os); 57 PrintTo(*it, os);
58 *os << "\n"; 58 *os << "\n";
59 } 59 }
60 } 60 }
61 61
62 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders(
63 const char* headers) {
64 return make_scoped_refptr(new net::HttpResponseHeaders(
65 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers))));
66 }
67
62 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { 68 class EmptyURLRequestDelegate : public net::URLRequest::Delegate {
63 void OnResponseStarted(net::URLRequest* request) override {} 69 void OnResponseStarted(net::URLRequest* request) override {}
64 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} 70 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {}
65 }; 71 };
66 72
67 class MockURLRequestJob : public net::URLRequestJob { 73 class MockURLRequestJob : public net::URLRequestJob {
68 public: 74 public:
69 MockURLRequestJob(net::URLRequest* request, 75 MockURLRequestJob(net::URLRequest* request,
70 const net::HttpResponseInfo& response_info, 76 const net::HttpResponseInfo& response_info,
71 const std::string& mime_type) 77 const std::string& mime_type)
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 319
314 void ResourcePrefetchPredictorTest::TearDown() { 320 void ResourcePrefetchPredictorTest::TearDown() {
315 predictor_.reset(NULL); 321 predictor_.reset(NULL);
316 profile_->DestroyHistoryService(); 322 profile_->DestroyHistoryService();
317 } 323 }
318 324
319 void ResourcePrefetchPredictorTest::InitializeSampleData() { 325 void ResourcePrefetchPredictorTest::InitializeSampleData() {
320 { // Url data. 326 { // Url data.
321 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); 327 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com/");
322 google.last_visit = base::Time::FromInternalValue(1); 328 google.last_visit = base::Time::FromInternalValue(1);
329 google.resources.push_back(
330 ResourceRow(std::string(), "http://google.com/style1.css",
331 content::RESOURCE_TYPE_STYLESHEET, 3, 2, 1, 1.0,
332 net::MEDIUM, false, false));
333 google.resources.push_back(ResourceRow(std::string(),
334 "http://google.com/script3.js",
335 content::RESOURCE_TYPE_SCRIPT, 4, 0,
336 1, 2.1, net::MEDIUM, false, false));
337 google.resources.push_back(ResourceRow(std::string(),
338 "http://google.com/script4.js",
339 content::RESOURCE_TYPE_SCRIPT, 11, 0,
340 0, 2.1, net::MEDIUM, false, false));
323 google.resources.push_back(ResourceRow( 341 google.resources.push_back(ResourceRow(
324 std::string(), "http://google.com/style1.css", 342 std::string(), "http://google.com/image1.png",
325 content::RESOURCE_TYPE_STYLESHEET, 3, 2, 1, 1.0, net::MEDIUM)); 343 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false));
326 google.resources.push_back( 344 google.resources.push_back(
327 ResourceRow(std::string(), "http://google.com/script3.js", 345 ResourceRow(std::string(), "http://google.com/a.font",
328 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM)); 346 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM,
329 google.resources.push_back( 347 false, false));
330 ResourceRow(std::string(), "http://google.com/script4.js",
331 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 2.1, net::MEDIUM));
332 google.resources.push_back(
333 ResourceRow(std::string(), "http://google.com/image1.png",
334 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM));
335 google.resources.push_back(ResourceRow(
336 std::string(), "http://google.com/a.font",
337 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM));
338 348
339 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com/"); 349 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com/");
340 reddit.last_visit = base::Time::FromInternalValue(2); 350 reddit.last_visit = base::Time::FromInternalValue(2);
341 reddit.resources.push_back( 351 reddit.resources.push_back(
342 ResourceRow(std::string(), "http://reddit-resource.com/script1.js", 352 ResourceRow(std::string(), "http://reddit-resource.com/script1.js",
343 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM)); 353 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM,
354 false, false));
344 reddit.resources.push_back( 355 reddit.resources.push_back(
345 ResourceRow(std::string(), "http://reddit-resource.com/script2.js", 356 ResourceRow(std::string(), "http://reddit-resource.com/script2.js",
346 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM)); 357 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM,
358 false, false));
347 359
348 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/"); 360 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/");
349 yahoo.last_visit = base::Time::FromInternalValue(3); 361 yahoo.last_visit = base::Time::FromInternalValue(3);
350 yahoo.resources.push_back( 362 yahoo.resources.push_back(ResourceRow(std::string(),
351 ResourceRow(std::string(), "http://google.com/image.png", 363 "http://google.com/image.png",
352 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, net::MEDIUM)); 364 content::RESOURCE_TYPE_IMAGE, 20, 1,
365 0, 10.0, net::MEDIUM, false, false));
353 366
354 test_url_data_.clear(); 367 test_url_data_.clear();
355 test_url_data_.insert(std::make_pair("http://www.google.com/", google)); 368 test_url_data_.insert(std::make_pair("http://www.google.com/", google));
356 test_url_data_.insert(std::make_pair("http://www.reddit.com/", reddit)); 369 test_url_data_.insert(std::make_pair("http://www.reddit.com/", reddit));
357 test_url_data_.insert(std::make_pair("http://www.yahoo.com/", yahoo)); 370 test_url_data_.insert(std::make_pair("http://www.yahoo.com/", yahoo));
358 } 371 }
359 372
360 { // Host data. 373 { // Host data.
361 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); 374 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com");
362 facebook.last_visit = base::Time::FromInternalValue(4); 375 facebook.last_visit = base::Time::FromInternalValue(4);
363 facebook.resources.push_back(ResourceRow( 376 facebook.resources.push_back(
364 std::string(), "http://www.facebook.com/style.css", 377 ResourceRow(std::string(), "http://www.facebook.com/style.css",
365 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, net::MEDIUM)); 378 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1,
379 net::MEDIUM, false, false));
366 facebook.resources.push_back( 380 facebook.resources.push_back(
367 ResourceRow(std::string(), "http://www.facebook.com/script.js", 381 ResourceRow(std::string(), "http://www.facebook.com/script.js",
368 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM)); 382 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM,
383 false, false));
384 facebook.resources.push_back(ResourceRow(
385 std::string(), "http://www.facebook.com/image.png",
386 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false));
369 facebook.resources.push_back( 387 facebook.resources.push_back(
370 ResourceRow(std::string(), "http://www.facebook.com/image.png", 388 ResourceRow(std::string(), "http://www.facebook.com/a.font",
371 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM)); 389 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM,
372 facebook.resources.push_back(ResourceRow( 390 false, false));
373 std::string(), "http://www.facebook.com/a.font",
374 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM));
375 facebook.resources.push_back(ResourceRow( 391 facebook.resources.push_back(ResourceRow(
376 std::string(), "http://www.resources.facebook.com/script.js", 392 std::string(), "http://www.resources.facebook.com/script.js",
377 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM)); 393 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, false,
394 false));
378 395
379 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); 396 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com");
380 yahoo.last_visit = base::Time::FromInternalValue(5); 397 yahoo.last_visit = base::Time::FromInternalValue(5);
381 yahoo.resources.push_back( 398 yahoo.resources.push_back(ResourceRow(std::string(),
382 ResourceRow(std::string(), "http://google.com/image.png", 399 "http://google.com/image.png",
383 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, net::MEDIUM)); 400 content::RESOURCE_TYPE_IMAGE, 20, 1,
401 0, 10.0, net::MEDIUM, false, false));
384 402
385 test_host_data_.clear(); 403 test_host_data_.clear();
386 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); 404 test_host_data_.insert(std::make_pair("www.facebook.com", facebook));
387 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); 405 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo));
388 } 406 }
389 } 407 }
390 408
391 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) { 409 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) {
392 // Tests that the predictor initializes correctly without any data. 410 // Tests that the predictor initializes correctly without any data.
393 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 411 EXPECT_TRUE(predictor_->url_table_cache_->empty());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 URLRequestSummary resource2 = CreateURLRequestSummary( 453 URLRequestSummary resource2 = CreateURLRequestSummary(
436 1, 1, "http://www.google.com", "http://google.com/script1.js", 454 1, 1, "http://www.google.com", "http://google.com/script1.js",
437 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 455 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
438 predictor_->RecordURLResponse(resource2); 456 predictor_->RecordURLResponse(resource2);
439 URLRequestSummary resource3 = CreateURLRequestSummary( 457 URLRequestSummary resource3 = CreateURLRequestSummary(
440 1, 1, "http://www.google.com", "http://google.com/script2.js", 458 1, 1, "http://www.google.com", "http://google.com/script2.js",
441 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false); 459 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM, "text/javascript", false);
442 predictor_->RecordURLResponse(resource3); 460 predictor_->RecordURLResponse(resource3);
443 461
444 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 462 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
463 host_data.resources.push_back(
464 ResourceRow(std::string(), "http://google.com/style1.css",
465 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM,
466 false, false));
445 host_data.resources.push_back(ResourceRow( 467 host_data.resources.push_back(ResourceRow(
446 std::string(), "http://google.com/style1.css", 468 std::string(), "http://google.com/script1.js",
447 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM)); 469 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false));
448 host_data.resources.push_back( 470 host_data.resources.push_back(ResourceRow(
449 ResourceRow(std::string(), "http://google.com/script1.js", 471 std::string(), "http://google.com/script2.js",
450 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM)); 472 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false));
451 host_data.resources.push_back(
452 ResourceRow(std::string(), "http://google.com/script2.js",
453 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM));
454 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 473 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data));
455 474
456 predictor_->OnNavigationComplete(main_frame.navigation_id); 475 predictor_->OnNavigationComplete(main_frame.navigation_id);
457 profile_->BlockUntilHistoryProcessesPendingRequests(); 476 profile_->BlockUntilHistoryProcessesPendingRequests();
458 } 477 }
459 478
460 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { 479 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) {
461 // Single navigation that will be recorded. Will check for duplicate 480 // Single navigation that will be recorded. Will check for duplicate
462 // resources and also for number of resources saved. 481 // resources and also for number of resources saved.
463 AddUrlToHistory("http://www.google.com", 4); 482 AddUrlToHistory("http://www.google.com", 4);
(...skipping 27 matching lines...) Expand all
491 URLRequestSummary resource6 = CreateURLRequestSummary( 510 URLRequestSummary resource6 = CreateURLRequestSummary(
492 1, 1, "http://www.google.com", "http://google.com/image2.png", 511 1, 1, "http://www.google.com", "http://google.com/image2.png",
493 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 512 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
494 predictor_->RecordURLResponse(resource6); 513 predictor_->RecordURLResponse(resource6);
495 URLRequestSummary resource7 = CreateURLRequestSummary( 514 URLRequestSummary resource7 = CreateURLRequestSummary(
496 1, 1, "http://www.google.com", "http://google.com/style2.css", 515 1, 1, "http://www.google.com", "http://google.com/style2.css",
497 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 516 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
498 predictor_->RecordURLResponse(resource7); 517 predictor_->RecordURLResponse(resource7);
499 518
500 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); 519 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.google.com/");
520 url_data.resources.push_back(
521 ResourceRow(std::string(), "http://google.com/style1.css",
522 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM,
523 false, false));
501 url_data.resources.push_back(ResourceRow( 524 url_data.resources.push_back(ResourceRow(
502 std::string(), "http://google.com/style1.css", 525 std::string(), "http://google.com/script1.js",
503 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM)); 526 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false));
527 url_data.resources.push_back(ResourceRow(
528 std::string(), "http://google.com/script2.js",
529 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false));
504 url_data.resources.push_back( 530 url_data.resources.push_back(
505 ResourceRow(std::string(), "http://google.com/script1.js", 531 ResourceRow(std::string(), "http://google.com/style2.css",
506 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM)); 532 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, net::MEDIUM,
507 url_data.resources.push_back( 533 false, false));
508 ResourceRow(std::string(), "http://google.com/script2.js",
509 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM));
510 url_data.resources.push_back(ResourceRow(
511 std::string(), "http://google.com/style2.css",
512 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, net::MEDIUM));
513 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 534 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_));
514 535
515 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 536 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
516 host_data.resources = url_data.resources; 537 host_data.resources = url_data.resources;
517 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 538 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data));
518 539
519 predictor_->OnNavigationComplete(main_frame.navigation_id); 540 predictor_->OnNavigationComplete(main_frame.navigation_id);
520 profile_->BlockUntilHistoryProcessesPendingRequests(); 541 profile_->BlockUntilHistoryProcessesPendingRequests();
521 } 542 }
522 543
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 URLRequestSummary resource6 = CreateURLRequestSummary( 585 URLRequestSummary resource6 = CreateURLRequestSummary(
565 1, 1, "http://www.google.com", "http://google.com/image2.png", 586 1, 1, "http://www.google.com", "http://google.com/image2.png",
566 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false); 587 content::RESOURCE_TYPE_IMAGE, net::MEDIUM, "image/png", false);
567 predictor_->RecordURLResponse(resource6); 588 predictor_->RecordURLResponse(resource6);
568 URLRequestSummary resource7 = CreateURLRequestSummary( 589 URLRequestSummary resource7 = CreateURLRequestSummary(
569 1, 1, "http://www.google.com", "http://google.com/style2.css", 590 1, 1, "http://www.google.com", "http://google.com/style2.css",
570 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true); 591 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", true);
571 predictor_->RecordURLResponse(resource7); 592 predictor_->RecordURLResponse(resource7);
572 593
573 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); 594 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.google.com/");
595 url_data.resources.push_back(
596 ResourceRow(std::string(), "http://google.com/style1.css",
597 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, net::MEDIUM,
598 false, false));
574 url_data.resources.push_back(ResourceRow( 599 url_data.resources.push_back(ResourceRow(
575 std::string(), "http://google.com/style1.css", 600 std::string(), "http://google.com/script1.js",
576 content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, net::MEDIUM)); 601 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false));
577 url_data.resources.push_back( 602 url_data.resources.push_back(ResourceRow(
578 ResourceRow(std::string(), "http://google.com/script1.js", 603 std::string(), "http://google.com/script4.js",
579 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM)); 604 content::RESOURCE_TYPE_SCRIPT, 11, 1, 1, 2.1, net::MEDIUM, false, false));
580 url_data.resources.push_back( 605 url_data.resources.push_back(ResourceRow(
581 ResourceRow(std::string(), "http://google.com/script4.js", 606 std::string(), "http://google.com/script2.js",
582 content::RESOURCE_TYPE_SCRIPT, 11, 1, 1, 2.1, net::MEDIUM)); 607 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false));
583 url_data.resources.push_back(
584 ResourceRow(std::string(), "http://google.com/script2.js",
585 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM));
586 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 608 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_));
587 609
588 EXPECT_CALL( 610 EXPECT_CALL(
589 *mock_tables_.get(), 611 *mock_tables_.get(),
590 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 612 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST));
591 613
592 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 614 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
615 host_data.resources.push_back(
616 ResourceRow(std::string(), "http://google.com/style1.css",
617 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM,
618 false, false));
593 host_data.resources.push_back(ResourceRow( 619 host_data.resources.push_back(ResourceRow(
594 std::string(), "http://google.com/style1.css", 620 std::string(), "http://google.com/script1.js",
595 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM)); 621 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false));
622 host_data.resources.push_back(ResourceRow(
623 std::string(), "http://google.com/script2.js",
624 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false));
596 host_data.resources.push_back( 625 host_data.resources.push_back(
597 ResourceRow(std::string(), "http://google.com/script1.js", 626 ResourceRow(std::string(), "http://google.com/style2.css",
598 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM)); 627 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, net::MEDIUM,
599 host_data.resources.push_back( 628 false, false));
600 ResourceRow(std::string(), "http://google.com/script2.js",
601 content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM));
602 host_data.resources.push_back(ResourceRow(
603 std::string(), "http://google.com/style2.css",
604 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, net::MEDIUM));
605 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 629 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data));
606 630
607 predictor_->OnNavigationComplete(main_frame.navigation_id); 631 predictor_->OnNavigationComplete(main_frame.navigation_id);
608 profile_->BlockUntilHistoryProcessesPendingRequests(); 632 profile_->BlockUntilHistoryProcessesPendingRequests();
609 } 633 }
610 634
611 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { 635 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) {
612 // Tests that a URL is deleted before another is added if the cache is full. 636 // Tests that a URL is deleted before another is added if the cache is full.
613 AddUrlToHistory("http://www.nike.com/", 4); 637 AddUrlToHistory("http://www.nike.com/", 4);
614 638
(...skipping 23 matching lines...) Expand all
638 predictor_->RecordURLResponse(resource2); 662 predictor_->RecordURLResponse(resource2);
639 663
640 EXPECT_CALL( 664 EXPECT_CALL(
641 *mock_tables_.get(), 665 *mock_tables_.get(),
642 DeleteSingleDataPoint("http://www.google.com/", PREFETCH_KEY_TYPE_URL)); 666 DeleteSingleDataPoint("http://www.google.com/", PREFETCH_KEY_TYPE_URL));
643 EXPECT_CALL( 667 EXPECT_CALL(
644 *mock_tables_.get(), 668 *mock_tables_.get(),
645 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); 669 DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST));
646 670
647 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/"); 671 PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/");
672 url_data.resources.push_back(
673 ResourceRow(std::string(), "http://nike.com/style1.css",
674 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM,
675 false, false));
648 url_data.resources.push_back(ResourceRow( 676 url_data.resources.push_back(ResourceRow(
649 std::string(), "http://nike.com/style1.css", 677 std::string(), "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE,
650 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM)); 678 1, 0, 0, 2.0, net::MEDIUM, false, false));
651 url_data.resources.push_back(
652 ResourceRow(std::string(), "http://nike.com/image2.png",
653 content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, net::MEDIUM));
654 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); 679 EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_));
655 680
656 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com"); 681 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com");
657 host_data.resources = url_data.resources; 682 host_data.resources = url_data.resources;
658 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 683 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data));
659 684
660 predictor_->OnNavigationComplete(main_frame.navigation_id); 685 predictor_->OnNavigationComplete(main_frame.navigation_id);
661 profile_->BlockUntilHistoryProcessesPendingRequests(); 686 profile_->BlockUntilHistoryProcessesPendingRequests();
662 } 687 }
663 688
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 main_frame.navigation_id.creation_time = base::TimeTicks(); 872 main_frame.navigation_id.creation_time = base::TimeTicks();
848 EXPECT_TRUE(main_frame.navigation_id.creation_time.is_null()); 873 EXPECT_TRUE(main_frame.navigation_id.creation_time.is_null());
849 874
850 // Now add a subresource. 875 // Now add a subresource.
851 URLRequestSummary resource1 = CreateURLRequestSummary( 876 URLRequestSummary resource1 = CreateURLRequestSummary(
852 1, 1, "http://www.google.com", "http://google.com/style1.css", 877 1, 1, "http://www.google.com", "http://google.com/style1.css",
853 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false); 878 content::RESOURCE_TYPE_STYLESHEET, net::MEDIUM, "text/css", false);
854 predictor_->RecordURLResponse(resource1); 879 predictor_->RecordURLResponse(resource1);
855 880
856 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); 881 PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com");
857 host_data.resources.push_back(ResourceRow( 882 host_data.resources.push_back(
858 std::string(), "http://google.com/style1.css", 883 ResourceRow(std::string(), "http://google.com/style1.css",
859 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM)); 884 content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM,
885 false, false));
860 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); 886 EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data));
861 887
862 // The page load time will be collected by RPP_HISTOGRAM_MEDIUM_TIMES, which 888 // The page load time will be collected by RPP_HISTOGRAM_MEDIUM_TIMES, which
863 // has a upper bound of 3 minutes. 889 // has a upper bound of 3 minutes.
864 base::TimeDelta plt = 890 base::TimeDelta plt =
865 predictor_->OnNavigationComplete(main_frame.navigation_id); 891 predictor_->OnNavigationComplete(main_frame.navigation_id);
866 EXPECT_LT(plt, base::TimeDelta::FromSeconds(180)); 892 EXPECT_LT(plt, base::TimeDelta::FromSeconds(180));
867 893
868 profile_->BlockUntilHistoryProcessesPendingRequests(); 894 profile_->BlockUntilHistoryProcessesPendingRequests();
869 } 895 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 950
925 std::unique_ptr<net::URLRequest> file_request = 951 std::unique_ptr<net::URLRequest> file_request =
926 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM, 952 CreateURLRequest(GURL("file://www.google.com/cat.png"), net::MEDIUM,
927 content::RESOURCE_TYPE_IMAGE, 1, 1, false); 953 content::RESOURCE_TYPE_IMAGE, 1, 1, false);
928 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest( 954 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordRequest(
929 file_request.get(), content::RESOURCE_TYPE_IMAGE)); 955 file_request.get(), content::RESOURCE_TYPE_IMAGE));
930 } 956 }
931 957
932 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseMainFrame) { 958 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseMainFrame) {
933 net::HttpResponseInfo response_info; 959 net::HttpResponseInfo response_info;
934 response_info.headers = 960 response_info.headers = MakeResponseHeaders("");
935 scoped_refptr<net::HttpResponseHeaders>(new net::HttpResponseHeaders(""));
936 url_request_job_factory_.set_response_info(response_info); 961 url_request_job_factory_.set_response_info(response_info);
937 962
938 std::unique_ptr<net::URLRequest> http_request = 963 std::unique_ptr<net::URLRequest> http_request =
939 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM, 964 CreateURLRequest(GURL("http://www.google.com"), net::MEDIUM,
940 content::RESOURCE_TYPE_MAIN_FRAME, 1, 1, true); 965 content::RESOURCE_TYPE_MAIN_FRAME, 1, 1, true);
941 EXPECT_TRUE( 966 EXPECT_TRUE(
942 ResourcePrefetchPredictor::ShouldRecordResponse(http_request.get())); 967 ResourcePrefetchPredictor::ShouldRecordResponse(http_request.get()));
943 968
944 std::unique_ptr<net::URLRequest> https_request = 969 std::unique_ptr<net::URLRequest> https_request =
945 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM, 970 CreateURLRequest(GURL("https://www.google.com"), net::MEDIUM,
946 content::RESOURCE_TYPE_MAIN_FRAME, 1, 1, true); 971 content::RESOURCE_TYPE_MAIN_FRAME, 1, 1, true);
947 EXPECT_TRUE( 972 EXPECT_TRUE(
948 ResourcePrefetchPredictor::ShouldRecordResponse(https_request.get())); 973 ResourcePrefetchPredictor::ShouldRecordResponse(https_request.get()));
949 974
950 std::unique_ptr<net::URLRequest> file_request = 975 std::unique_ptr<net::URLRequest> file_request =
951 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM, 976 CreateURLRequest(GURL("file://www.google.com"), net::MEDIUM,
952 content::RESOURCE_TYPE_MAIN_FRAME, 1, 1, true); 977 content::RESOURCE_TYPE_MAIN_FRAME, 1, 1, true);
953 EXPECT_FALSE( 978 EXPECT_FALSE(
954 ResourcePrefetchPredictor::ShouldRecordResponse(file_request.get())); 979 ResourcePrefetchPredictor::ShouldRecordResponse(file_request.get()));
955 } 980 }
956 981
957 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseSubresource) { 982 TEST_F(ResourcePrefetchPredictorTest, ShouldRecordResponseSubresource) {
958 net::HttpResponseInfo response_info; 983 net::HttpResponseInfo response_info;
959 response_info.headers = scoped_refptr<net::HttpResponseHeaders>( 984 response_info.headers = MakeResponseHeaders
pasko 2016/08/19 16:17:42 does it compile without opening paren?
960 new net::HttpResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n")); 985 "HTTP/1.1 200 OK\n\nSome: Headers\n");
961 response_info.was_cached = true; 986 response_info.was_cached = true;
962 url_request_job_factory_.set_response_info(response_info); 987 url_request_job_factory_.set_response_info(response_info);
963 988
964 // Protocol 989 // Protocol
965 std::unique_ptr<net::URLRequest> http_image_request = 990 std::unique_ptr<net::URLRequest> http_image_request =
966 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM, 991 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
967 content::RESOURCE_TYPE_IMAGE, 1, 1, true); 992 content::RESOURCE_TYPE_IMAGE, 1, 1, true);
968 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse( 993 EXPECT_TRUE(ResourcePrefetchPredictor::ShouldRecordResponse(
969 http_image_request.get())); 994 http_image_request.get()));
970 995
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1024 prefetch_unknown_font_request.get())); 1049 prefetch_unknown_font_request.get()));
1025 1050
1026 // Not main frame 1051 // Not main frame
1027 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest( 1052 std::unique_ptr<net::URLRequest> font_request_sub_frame = CreateURLRequest(
1028 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM, 1053 GURL("http://www.google.com/comic-sans-ms.woff"), net::MEDIUM,
1029 content::RESOURCE_TYPE_FONT_RESOURCE, 1, 1, false); 1054 content::RESOURCE_TYPE_FONT_RESOURCE, 1, 1, false);
1030 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse( 1055 EXPECT_FALSE(ResourcePrefetchPredictor::ShouldRecordResponse(
1031 font_request_sub_frame.get())); 1056 font_request_sub_frame.get()));
1032 } 1057 }
1033 1058
1059 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponse) {
1060 net::HttpResponseInfo response_info;
1061 response_info.headers =
1062 MakeResponseHeaders("HTTP/1.1 200 OK\n\nSome: Headers\n");
1063 response_info.was_cached = true;
1064 url_request_job_factory_.set_response_info(response_info);
1065
1066 GURL url("http://www.google.com/cat.png");
1067 std::unique_ptr<net::URLRequest> request = CreateURLRequest(
1068 url, net::MEDIUM, content::RESOURCE_TYPE_IMAGE, 1, 1, true);
1069 URLRequestSummary summary;
1070 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1071 EXPECT_EQ(1, summary.navigation_id.render_process_id);
1072 EXPECT_EQ(1, summary.navigation_id.render_frame_id);
1073 EXPECT_EQ(url, summary.navigation_id.main_frame_url);
1074 EXPECT_EQ(url, summary.resource_url);
1075 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1076 EXPECT_TRUE(summary.was_cached);
1077 EXPECT_FALSE(summary.has_validators);
1078 EXPECT_FALSE(summary.always_revalidate);
1079 }
1080
1081 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseContentType) {
1082 net::HttpResponseInfo response_info;
1083 response_info.headers = MakeResponseHeaders(
1084 "HTTP/1.1 200 OK\n\n"
1085 "Some: Headers\n"
1086 "Content-Type: image/whatever\n");
1087 url_request_job_factory_.set_response_info(response_info);
1088 url_request_job_factory_.set_mime_type("image/png");
1089
1090 std::unique_ptr<net::URLRequest> request =
1091 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1092 content::RESOURCE_TYPE_PREFETCH, 1, 1, true);
1093 URLRequestSummary summary;
1094 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request, &summary));
1095 EXPECT_EQ(content::RESOURCE_TYPE_IMAGE, summary.resource_type);
1096 }
1097
1098 TEST_F(ResourcePrefetchPredictorTest, SummarizeResponseCachePolicy) {
1099 net::HttpResponseInfo response_info;
1100 response_info.headers = MakeResponseHeaders(
1101 "HTTP/1.1 200 OK\n"
1102 "Some: Headers\n");
1103 url_request_job_factory_.set_response_info(response_info);
1104
1105 std::unique_ptr<net::URLRequest> request_no_validators =
1106 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1107 content::RESOURCE_TYPE_PREFETCH, 1, 1, true);
1108
1109 URLRequestSummary summary;
1110 EXPECT_TRUE(
1111 URLRequestSummary::SummarizeResponse(*request_no_validators, &summary));
1112 EXPECT_FALSE(summary.has_validators);
1113
1114 response_info.headers = MakeResponseHeaders(
1115 "HTTP/1.1 200 OK\n"
1116 "ETag: \"Cr66\"\n"
1117 "Cache-Control: no-cache\n");
1118 url_request_job_factory_.set_response_info(response_info);
1119 std::unique_ptr<net::URLRequest> request_etag =
1120 CreateURLRequest(GURL("http://www.google.com/cat.png"), net::MEDIUM,
1121 content::RESOURCE_TYPE_PREFETCH, 1, 1, true);
1122 EXPECT_TRUE(URLRequestSummary::SummarizeResponse(*request_etag, &summary));
1123 EXPECT_TRUE(summary.has_validators);
1124 EXPECT_TRUE(summary.always_revalidate);
1125 }
1126
1034 } // namespace predictors 1127 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698