Index: chrome/browser/predictors/resource_prefetch_predictor_unittest.cc |
diff --git a/chrome/browser/predictors/resource_prefetch_predictor_unittest.cc b/chrome/browser/predictors/resource_prefetch_predictor_unittest.cc |
index 54390ef2b26752c35bf19a61e5434227d4d5e2dc..6f0c4412a371d4ece8efda25b5de29998d1b86c2 100644 |
--- a/chrome/browser/predictors/resource_prefetch_predictor_unittest.cc |
+++ b/chrome/browser/predictors/resource_prefetch_predictor_unittest.cc |
@@ -14,6 +14,7 @@ |
#include "base/time/time.h" |
#include "chrome/browser/history/history_service_factory.h" |
#include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
+#include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
#include "chrome/test/base/testing_profile.h" |
#include "components/history/core/browser/history_service.h" |
#include "components/history/core/browser/history_types.h" |
@@ -32,33 +33,14 @@ using testing::Pointee; |
using testing::SetArgPointee; |
using testing::StrictMock; |
+using chrome_browser_predictors::ResourceData; |
+ |
namespace predictors { |
typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; |
-typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; |
-typedef std::vector<ResourceRow> ResourceRows; |
typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; |
typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
-// For printing failures nicely. |
-void PrintTo(const ResourceRow& row, ::std::ostream* os) { |
- *os << "[" << row.primary_key << "," << row.resource_url |
- << "," << row.resource_type << "," << row.number_of_hits |
- << "," << row.number_of_misses << "," << row.consecutive_misses |
- << "," << row.average_position << "," << row.score << "]"; |
-} |
- |
-void PrintTo(const PrefetchData& data, ::std::ostream* os) { |
- *os << "[" << data.key_type << "," << data.primary_key |
- << "," << data.last_visit.ToInternalValue() << "]\n"; |
- for (ResourceRows::const_iterator it = data.resources.begin(); |
- it != data.resources.end(); ++it) { |
- *os << "\t\t"; |
- PrintTo(*it, os); |
- *os << "\n"; |
- } |
-} |
- |
scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
const char* headers) { |
return make_scoped_refptr(new net::HttpResponseHeaders( |
@@ -326,43 +308,36 @@ void ResourcePrefetchPredictorTest::InitializeSampleData() { |
{ // Url data. |
PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); |
google.last_visit = base::Time::FromInternalValue(1); |
- google.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 3, 2, 1, 1.0, |
- net::MEDIUM, false, false)); |
- google.resources.push_back(ResourceRow(std::string(), |
- "http://google.com/script3.js", |
- content::RESOURCE_TYPE_SCRIPT, 4, 0, |
- 1, 2.1, net::MEDIUM, false, false)); |
- google.resources.push_back(ResourceRow(std::string(), |
- "http://google.com/script4.js", |
- content::RESOURCE_TYPE_SCRIPT, 11, 0, |
- 0, 2.1, net::MEDIUM, false, false)); |
- google.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/image1.png", |
- content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false)); |
- google.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/a.font", |
- content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM, |
- false, false)); |
+ google.resources.push_back(MakeResourceData( |
+ "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 3, 2, |
+ 1, 1.0, net::MEDIUM, false, false)); |
+ google.resources.push_back(MakeResourceData( |
+ "http://google.com/script3.js", content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, |
+ 2.1, net::MEDIUM, false, false)); |
+ google.resources.push_back(MakeResourceData( |
+ "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, |
+ 2.1, net::MEDIUM, false, false)); |
+ google.resources.push_back(MakeResourceData( |
+ "http://google.com/image1.png", content::RESOURCE_TYPE_IMAGE, 6, 3, 0, |
+ 2.2, net::MEDIUM, false, false)); |
+ google.resources.push_back(MakeResourceData( |
+ "http://google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, |
+ 5.1, net::MEDIUM, false, false)); |
PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com/"); |
reddit.last_visit = base::Time::FromInternalValue(2); |
- reddit.resources.push_back( |
- ResourceRow(std::string(), "http://reddit-resource.com/script1.js", |
- content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM, |
- false, false)); |
- reddit.resources.push_back( |
- ResourceRow(std::string(), "http://reddit-resource.com/script2.js", |
- content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM, |
- false, false)); |
+ reddit.resources.push_back(MakeResourceData( |
+ "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, |
+ 4, 0, 1, 1.0, net::MEDIUM, false, false)); |
+ reddit.resources.push_back(MakeResourceData( |
+ "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, |
+ 2, 0, 0, 2.1, net::MEDIUM, false, false)); |
PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com/"); |
yahoo.last_visit = base::Time::FromInternalValue(3); |
- yahoo.resources.push_back(ResourceRow(std::string(), |
- "http://google.com/image.png", |
- content::RESOURCE_TYPE_IMAGE, 20, 1, |
- 0, 10.0, net::MEDIUM, false, false)); |
+ yahoo.resources.push_back(MakeResourceData( |
+ "http://google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, 0, |
+ 10.0, net::MEDIUM, false, false)); |
test_url_data_.clear(); |
test_url_data_.insert(std::make_pair("http://www.google.com/", google)); |
@@ -373,32 +348,28 @@ void ResourcePrefetchPredictorTest::InitializeSampleData() { |
{ // Host data. |
PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); |
facebook.last_visit = base::Time::FromInternalValue(4); |
+ facebook.resources.push_back(MakeResourceData( |
+ "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, |
+ 5, 2, 1, 1.1, net::MEDIUM, false, false)); |
+ facebook.resources.push_back(MakeResourceData( |
+ "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, |
+ 0, 1, 2.1, net::MEDIUM, false, false)); |
+ facebook.resources.push_back(MakeResourceData( |
+ "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, |
+ 0, 2.2, net::MEDIUM, false, false)); |
+ facebook.resources.push_back(MakeResourceData( |
+ "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, |
+ 0, 0, 5.1, net::MEDIUM, false, false)); |
facebook.resources.push_back( |
- ResourceRow(std::string(), "http://www.facebook.com/style.css", |
- content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, |
- net::MEDIUM, false, false)); |
- facebook.resources.push_back( |
- ResourceRow(std::string(), "http://www.facebook.com/script.js", |
- content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM, |
- false, false)); |
- facebook.resources.push_back(ResourceRow( |
- std::string(), "http://www.facebook.com/image.png", |
- content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false)); |
- facebook.resources.push_back( |
- ResourceRow(std::string(), "http://www.facebook.com/a.font", |
- content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM, |
- false, false)); |
- facebook.resources.push_back(ResourceRow( |
- std::string(), "http://www.resources.facebook.com/script.js", |
- content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, false, |
- false)); |
+ MakeResourceData("http://www.resources.facebook.com/script.js", |
+ content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
+ net::MEDIUM, false, false)); |
PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
yahoo.last_visit = base::Time::FromInternalValue(5); |
- yahoo.resources.push_back(ResourceRow(std::string(), |
- "http://google.com/image.png", |
- content::RESOURCE_TYPE_IMAGE, 20, 1, |
- 0, 10.0, net::MEDIUM, false, false)); |
+ yahoo.resources.push_back(MakeResourceData( |
+ "http://google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, 0, |
+ 10.0, net::MEDIUM, false, false)); |
test_host_data_.clear(); |
test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); |
@@ -460,16 +431,15 @@ TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { |
predictor_->RecordURLResponse(resource3); |
PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); |
- host_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM, |
- false, false)); |
- host_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script1.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false)); |
- host_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script2.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, |
+ 0, 1.0, net::MEDIUM, false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 2.0, net::MEDIUM, false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 3.0, net::MEDIUM, false, false)); |
EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); |
predictor_->OnNavigationComplete(main_frame.navigation_id); |
@@ -517,20 +487,18 @@ TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDB) { |
predictor_->RecordURLResponse(resource7); |
PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); |
- url_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM, |
- false, false)); |
- url_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script1.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false)); |
- url_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script2.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false)); |
- url_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style2.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, net::MEDIUM, |
- false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, |
+ 0, 1.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 2.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 3.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, |
+ 0, 7.0, net::MEDIUM, false, false)); |
EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); |
PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); |
@@ -592,19 +560,18 @@ TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { |
predictor_->RecordURLResponse(resource7); |
PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.google.com/"); |
- url_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 4, 2, 0, 1.0, net::MEDIUM, |
- false, false)); |
- url_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script1.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false)); |
- url_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script4.js", |
- content::RESOURCE_TYPE_SCRIPT, 11, 1, 1, 2.1, net::MEDIUM, false, false)); |
- url_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script2.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 4, 2, |
+ 0, 1.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 2.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script4.js", content::RESOURCE_TYPE_SCRIPT, 11, 1, 1, |
+ 2.1, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 3.0, net::MEDIUM, false, false)); |
EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); |
EXPECT_CALL( |
@@ -612,20 +579,18 @@ TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { |
DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); |
PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); |
- host_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM, |
- false, false)); |
- host_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script1.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 2.0, net::MEDIUM, false, false)); |
- host_data.resources.push_back(ResourceRow( |
- std::string(), "http://google.com/script2.js", |
- content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, 3.0, net::MEDIUM, false, false)); |
- host_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style2.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 7.0, net::MEDIUM, |
- false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, |
+ 0, 1.0, net::MEDIUM, false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script1.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 2.0, net::MEDIUM, false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/script2.js", content::RESOURCE_TYPE_SCRIPT, 1, 0, 0, |
+ 3.0, net::MEDIUM, false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style2.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, |
+ 0, 7.0, net::MEDIUM, false, false)); |
EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); |
predictor_->OnNavigationComplete(main_frame.navigation_id); |
@@ -669,13 +634,12 @@ TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { |
DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST)); |
PrefetchData url_data(PREFETCH_KEY_TYPE_URL, "http://www.nike.com/"); |
- url_data.resources.push_back( |
- ResourceRow(std::string(), "http://nike.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM, |
- false, false)); |
- url_data.resources.push_back(ResourceRow( |
- std::string(), "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE, |
- 1, 0, 0, 2.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://nike.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, |
+ 1.0, net::MEDIUM, false, false)); |
+ url_data.resources.push_back(MakeResourceData( |
+ "http://nike.com/image2.png", content::RESOURCE_TYPE_IMAGE, 1, 0, 0, 2.0, |
+ net::MEDIUM, false, false)); |
EXPECT_CALL(*mock_tables_.get(), UpdateData(url_data, empty_host_data_)); |
PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.nike.com"); |
@@ -879,10 +843,9 @@ TEST_F(ResourcePrefetchPredictorTest, GetCorrectPLT) { |
predictor_->RecordURLResponse(resource1); |
PrefetchData host_data(PREFETCH_KEY_TYPE_HOST, "www.google.com"); |
- host_data.resources.push_back( |
- ResourceRow(std::string(), "http://google.com/style1.css", |
- content::RESOURCE_TYPE_STYLESHEET, 1, 0, 0, 1.0, net::MEDIUM, |
- false, false)); |
+ host_data.resources.push_back(MakeResourceData( |
+ "http://google.com/style1.css", content::RESOURCE_TYPE_STYLESHEET, 1, 0, |
+ 0, 1.0, net::MEDIUM, false, false)); |
EXPECT_CALL(*mock_tables_.get(), UpdateData(empty_url_data_, host_data)); |
// The page load time will be collected by RPP_HISTOGRAM_MEDIUM_TIMES, which |