| Index: chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc
|
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc b/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc
|
| index 7f26eb4b963eb4bcaf13e715640ded2081888818..123d080aa20041959ac7620a234c9990638cf23d 100644
|
| --- a/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc
|
| +++ b/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc
|
| @@ -44,11 +44,12 @@ class ResourcePrefetchPredictorTablesTest : public testing::Test {
|
| using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap;
|
| using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap;
|
| using ManifestDataMap = ResourcePrefetchPredictorTables::ManifestDataMap;
|
| + using OriginDataMap = ResourcePrefetchPredictorTables::OriginDataMap;
|
|
|
| private:
|
| // Initializes the tables, |test_url_data_|, |test_host_data_|,
|
| - // |test_url_redirect_data_|, |test_host_redirect_data_| and
|
| - // |test_manifest_data_|.
|
| + // |test_url_redirect_data_|, |test_host_redirect_data_|,
|
| + // |test_manifest_data_|, and |test_origin_data|.
|
| void InitializeSampleData();
|
|
|
| // Checks that the input PrefetchData are the same, although the resources
|
| @@ -73,15 +74,22 @@ class ResourcePrefetchPredictorTablesTest : public testing::Test {
|
| const std::vector<precache::PrecacheResource>& lhs,
|
| const std::vector<precache::PrecacheResource>& rhs) const;
|
|
|
| + void TestOriginDataAreEqual(const OriginDataMap& lhs,
|
| + const OriginDataMap& rhs) const;
|
| + void TestOriginStatsAreEqual(const std::vector<OriginStat>& lhs,
|
| + const std::vector<OriginStat>& rhs) const;
|
| +
|
| void AddKey(PrefetchDataMap* m, const std::string& key) const;
|
| void AddKey(RedirectDataMap* m, const std::string& key) const;
|
| void AddKey(ManifestDataMap* m, const std::string& key) const;
|
| + void AddKey(OriginDataMap* m, const std::string& key) const;
|
|
|
| PrefetchDataMap test_url_data_;
|
| PrefetchDataMap test_host_data_;
|
| RedirectDataMap test_url_redirect_data_;
|
| RedirectDataMap test_host_redirect_data_;
|
| ManifestDataMap test_manifest_data_;
|
| + OriginDataMap test_origin_data_;
|
| };
|
|
|
| class ResourcePrefetchPredictorTablesReopenTest
|
| @@ -120,15 +128,17 @@ void ResourcePrefetchPredictorTablesTest::TestGetAllData() {
|
| PrefetchDataMap actual_url_data, actual_host_data;
|
| RedirectDataMap actual_url_redirect_data, actual_host_redirect_data;
|
| ManifestDataMap actual_manifest_data;
|
| + OriginDataMap actual_origin_data;
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| TestPrefetchDataAreEqual(test_url_data_, actual_url_data);
|
| TestPrefetchDataAreEqual(test_host_data_, actual_host_data);
|
| TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data);
|
| TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data);
|
| TestManifestDataAreEqual(test_manifest_data_, actual_manifest_data);
|
| + TestOriginDataAreEqual(test_origin_data_, actual_origin_data);
|
| }
|
|
|
| void ResourcePrefetchPredictorTablesTest::TestDeleteData() {
|
| @@ -144,21 +154,27 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteData() {
|
| hosts_to_delete = {"en.wikipedia.org"};
|
| tables_->DeleteManifestData(hosts_to_delete);
|
|
|
| + tables_->DeleteOriginData({"twitter.com"});
|
| +
|
| PrefetchDataMap actual_url_data, actual_host_data;
|
| RedirectDataMap actual_url_redirect_data, actual_host_redirect_data;
|
| ManifestDataMap actual_manifest_data;
|
| + OriginDataMap actual_origin_data;
|
| +
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| PrefetchDataMap expected_url_data, expected_host_data;
|
| RedirectDataMap expected_url_redirect_data, expected_host_redirect_data;
|
| ManifestDataMap expected_manifest_data;
|
| + OriginDataMap expected_origin_data;
|
| AddKey(&expected_url_data, "http://www.reddit.com");
|
| AddKey(&expected_host_data, "www.facebook.com");
|
| AddKey(&expected_url_redirect_data, "http://nyt.com");
|
| AddKey(&expected_host_redirect_data, "bbc.com");
|
| AddKey(&expected_manifest_data, "youtube.com");
|
| + AddKey(&expected_origin_data, "abc.xyz");
|
|
|
| TestPrefetchDataAreEqual(expected_url_data, actual_url_data);
|
| TestPrefetchDataAreEqual(expected_host_data, actual_host_data);
|
| @@ -167,6 +183,7 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteData() {
|
| TestRedirectDataAreEqual(expected_host_redirect_data,
|
| actual_host_redirect_data);
|
| TestManifestDataAreEqual(expected_manifest_data, actual_manifest_data);
|
| + TestOriginDataAreEqual(expected_origin_data, actual_origin_data);
|
| }
|
|
|
| void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() {
|
| @@ -177,9 +194,10 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() {
|
| PrefetchDataMap actual_url_data, actual_host_data;
|
| RedirectDataMap actual_url_redirect_data, actual_host_redirect_data;
|
| ManifestDataMap actual_manifest_data;
|
| + OriginDataMap actual_origin_data;
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| PrefetchDataMap expected_url_data;
|
| AddKey(&expected_url_data, "http://www.google.com");
|
| @@ -198,9 +216,10 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() {
|
| actual_url_redirect_data.clear();
|
| actual_host_redirect_data.clear();
|
| actual_manifest_data.clear();
|
| + actual_origin_data.clear();
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| PrefetchDataMap expected_host_data;
|
| AddKey(&expected_host_data, "www.yahoo.com");
|
| @@ -218,9 +237,10 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() {
|
| actual_url_redirect_data.clear();
|
| actual_host_redirect_data.clear();
|
| actual_manifest_data.clear();
|
| + actual_origin_data.clear();
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| RedirectDataMap expected_url_redirect_data;
|
| AddKey(&expected_url_redirect_data, "http://fb.com/google");
|
| @@ -239,9 +259,10 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() {
|
| actual_url_redirect_data.clear();
|
| actual_host_redirect_data.clear();
|
| actual_manifest_data.clear();
|
| + actual_origin_data.clear();
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| RedirectDataMap expected_host_redirect_data;
|
| AddKey(&expected_host_redirect_data, "microsoft.com");
|
| @@ -293,16 +314,23 @@ void ResourcePrefetchPredictorTablesTest::TestUpdateData() {
|
|
|
| tables_->UpdateManifestData("theverge.com", theverge);
|
|
|
| + OriginData twitter = CreateOriginData("twitter.com");
|
| + InitializeOriginStat(twitter.add_origins(), "https://dogs.twitter.com", 10, 1,
|
| + 0, 12., false, true);
|
| + tables_->UpdateOriginData(twitter);
|
| +
|
| PrefetchDataMap actual_url_data, actual_host_data;
|
| RedirectDataMap actual_url_redirect_data, actual_host_redirect_data;
|
| ManifestDataMap actual_manifest_data;
|
| + OriginDataMap actual_origin_data;
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
|
|
| PrefetchDataMap expected_url_data, expected_host_data;
|
| RedirectDataMap expected_url_redirect_data, expected_host_redirect_data;
|
| ManifestDataMap expected_manifest_data;
|
| + OriginDataMap expected_origin_data;
|
| AddKey(&expected_url_data, "http://www.reddit.com");
|
| AddKey(&expected_url_data, "http://www.yahoo.com");
|
| expected_url_data.insert(std::make_pair("http://www.google.com", google));
|
| @@ -323,6 +351,9 @@ void ResourcePrefetchPredictorTablesTest::TestUpdateData() {
|
| AddKey(&expected_manifest_data, "en.wikipedia.org");
|
| expected_manifest_data.insert(std::make_pair("theverge.com", theverge));
|
|
|
| + AddKey(&expected_origin_data, "abc.xyz");
|
| + expected_origin_data.insert({"twitter.com", twitter});
|
| +
|
| TestPrefetchDataAreEqual(expected_url_data, actual_url_data);
|
| TestPrefetchDataAreEqual(expected_host_data, actual_host_data);
|
| TestRedirectDataAreEqual(expected_url_redirect_data,
|
| @@ -338,9 +369,10 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteAllData() {
|
| PrefetchDataMap actual_url_data, actual_host_data;
|
| RedirectDataMap actual_url_redirect_data, actual_host_redirect_data;
|
| ManifestDataMap actual_manifest_data;
|
| + OriginDataMap actual_origin_data;
|
| tables_->GetAllData(&actual_url_data, &actual_host_data,
|
| &actual_url_redirect_data, &actual_host_redirect_data,
|
| - &actual_manifest_data);
|
| + &actual_manifest_data, &actual_origin_data);
|
| EXPECT_TRUE(actual_url_data.empty());
|
| EXPECT_TRUE(actual_host_data.empty());
|
| EXPECT_TRUE(actual_url_redirect_data.empty());
|
| @@ -478,6 +510,46 @@ void ResourcePrefetchPredictorTablesTest::TestManifestResourcesAreEqual(
|
| EXPECT_TRUE(lhs_index.empty());
|
| }
|
|
|
| +void ResourcePrefetchPredictorTablesTest::TestOriginDataAreEqual(
|
| + const OriginDataMap& lhs,
|
| + const OriginDataMap& rhs) const {
|
| + EXPECT_EQ(lhs.size(), rhs.size());
|
| +
|
| + for (const auto& o : rhs) {
|
| + const auto lhs_it = lhs.find(o.first);
|
| + ASSERT_TRUE(lhs_it != lhs.end()) << o.first;
|
| + std::vector<OriginStat> lhs_origins(lhs_it->second.origins().begin(),
|
| + lhs_it->second.origins().end());
|
| + std::vector<OriginStat> rhs_origins(o.second.origins().begin(),
|
| + o.second.origins().end());
|
| +
|
| + TestOriginStatsAreEqual(lhs_origins, rhs_origins);
|
| + }
|
| +}
|
| +
|
| +void ResourcePrefetchPredictorTablesTest::TestOriginStatsAreEqual(
|
| + const std::vector<OriginStat>& lhs,
|
| + const std::vector<OriginStat>& rhs) const {
|
| + EXPECT_EQ(lhs.size(), rhs.size());
|
| +
|
| + std::map<std::string, OriginStat> lhs_index;
|
| + // Repeated origins are not allowed.
|
| + for (const auto& o : lhs)
|
| + EXPECT_TRUE(lhs_index.insert({o.origin(), o}).second);
|
| +
|
| + for (const auto& o : rhs) {
|
| + auto lhs_it = lhs_index.find(o.origin());
|
| + if (lhs_it != lhs_index.end()) {
|
| + EXPECT_EQ(o, lhs_it->second);
|
| + lhs_index.erase(lhs_it);
|
| + } else {
|
| + ADD_FAILURE() << o.origin();
|
| + }
|
| + }
|
| +
|
| + EXPECT_TRUE(lhs_index.empty());
|
| +}
|
| +
|
| void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m,
|
| const std::string& key) const {
|
| PrefetchDataMap::const_iterator it = test_url_data_.find(key);
|
| @@ -509,6 +581,13 @@ void ResourcePrefetchPredictorTablesTest::AddKey(ManifestDataMap* m,
|
| m->insert(*it);
|
| }
|
|
|
| +void ResourcePrefetchPredictorTablesTest::AddKey(OriginDataMap* m,
|
| + const std::string& key) const {
|
| + auto it = test_origin_data_.find(key);
|
| + ASSERT_TRUE(it != test_origin_data_.end());
|
| + m->insert(*it);
|
| +}
|
| +
|
| void ResourcePrefetchPredictorTablesTest::InitializeSampleData() {
|
| PrefetchData empty_resource_data;
|
| RedirectData empty_redirect_data;
|
| @@ -669,6 +748,28 @@ void ResourcePrefetchPredictorTablesTest::InitializeSampleData() {
|
| tables_->UpdateManifestData("en.wikipedia.org", wikipedia);
|
| tables_->UpdateManifestData("youtube.com", youtube);
|
| }
|
| +
|
| + { // Origin data.
|
| + OriginData twitter;
|
| + twitter.set_host("twitter.com");
|
| + InitializeOriginStat(twitter.add_origins(), "https://cdn.twitter.com", 10,
|
| + 1, 0, 12., false, true);
|
| + InitializeOriginStat(twitter.add_origins(), "https://cats.twitter.com", 10,
|
| + 1, 0, 20., false, true);
|
| +
|
| + OriginData alphabet;
|
| + alphabet.set_host("abc.xyz");
|
| + InitializeOriginStat(alphabet.add_origins(), "https://abc.def.xyz", 10, 1,
|
| + 0, 12., false, true);
|
| + InitializeOriginStat(alphabet.add_origins(), "https://alpha.beta.com", 10,
|
| + 1, 0, 20., false, true);
|
| +
|
| + test_origin_data_.clear();
|
| + test_origin_data_.insert({"twitter.com", twitter});
|
| + test_origin_data_.insert({"abc.xyz", alphabet});
|
| + tables_->UpdateOriginData(twitter);
|
| + tables_->UpdateOriginData(alphabet);
|
| + }
|
| }
|
|
|
| void ResourcePrefetchPredictorTablesTest::ReopenDatabase() {
|
| @@ -690,17 +791,17 @@ TEST_F(ResourcePrefetchPredictorTablesTest, ComputeResourceScore) {
|
| };
|
|
|
| // Priority is more important than the rest.
|
| - EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 1.) >
|
| - compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.));
|
| -
|
| - EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) >
|
| - compute_score(net::MEDIUM, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| - EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) >
|
| - compute_score(net::LOW, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| - EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) >
|
| - compute_score(net::LOWEST, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| - EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) >
|
| - compute_score(net::IDLE, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| + EXPECT_GT(compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 1.),
|
| + compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.));
|
| +
|
| + EXPECT_GT(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.),
|
| + compute_score(net::MEDIUM, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| + EXPECT_GT(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.),
|
| + compute_score(net::LOW, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| + EXPECT_GT(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.),
|
| + compute_score(net::LOWEST, content::RESOURCE_TYPE_SCRIPT, 1.));
|
| + EXPECT_GT(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.),
|
| + compute_score(net::IDLE, content::RESOURCE_TYPE_SCRIPT, 1.));
|
|
|
| // Scripts and stylesheets are equivalent.
|
| EXPECT_NEAR(
|
| @@ -709,19 +810,59 @@ TEST_F(ResourcePrefetchPredictorTablesTest, ComputeResourceScore) {
|
| 1e-4);
|
|
|
| // Scripts are more important than fonts and images, and the rest.
|
| - EXPECT_TRUE(
|
| - compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 42.) >
|
| + EXPECT_GT(
|
| + compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 42.),
|
| compute_score(net::HIGHEST, content::RESOURCE_TYPE_FONT_RESOURCE, 42.));
|
| - EXPECT_TRUE(
|
| - compute_score(net::HIGHEST, content::RESOURCE_TYPE_FONT_RESOURCE, 42.) >
|
| + EXPECT_GT(
|
| + compute_score(net::HIGHEST, content::RESOURCE_TYPE_FONT_RESOURCE, 42.),
|
| compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.));
|
| - EXPECT_TRUE(
|
| - compute_score(net::HIGHEST, content::RESOURCE_TYPE_FONT_RESOURCE, 42.) >
|
| + EXPECT_GT(
|
| + compute_score(net::HIGHEST, content::RESOURCE_TYPE_FONT_RESOURCE, 42.),
|
| compute_score(net::HIGHEST, content::RESOURCE_TYPE_FAVICON, 42.));
|
|
|
| // All else being equal, position matters.
|
| - EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 12.) >
|
| - compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 42.));
|
| + EXPECT_GT(compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 12.),
|
| + compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 42.));
|
| +}
|
| +
|
| +TEST_F(ResourcePrefetchPredictorTablesTest, ComputeOriginScore) {
|
| + auto compute_score = [](int hits, int misses, double average_position,
|
| + bool always_access_network, bool accessed_network) {
|
| + OriginStat origin;
|
| + InitializeOriginStat(&origin, "", hits, misses, 0, average_position,
|
| + always_access_network, accessed_network);
|
| + return ResourcePrefetchPredictorTables::ComputeOriginScore(origin);
|
| + };
|
| +
|
| + // High-confidence is more important than the rest.
|
| + EXPECT_GT(compute_score(20, 2, 12., false, false),
|
| + compute_score(2, 0, 12., false, false));
|
| + EXPECT_GT(compute_score(20, 2, 12., false, false),
|
| + compute_score(2, 0, 2., true, true));
|
| +
|
| + // Don't care about the confidence as long as it's high.
|
| + EXPECT_NEAR(compute_score(20, 2, 12., false, false),
|
| + compute_score(50, 6, 12., false, false), 1e-4);
|
| +
|
| + // Mandatory network access.
|
| + EXPECT_GT(compute_score(1, 1, 12., true, false),
|
| + compute_score(1, 1, 12., false, false));
|
| + EXPECT_GT(compute_score(1, 1, 12., true, false),
|
| + compute_score(1, 1, 12., false, true));
|
| + EXPECT_GT(compute_score(1, 1, 12., true, false),
|
| + compute_score(1, 1, 2., false, true));
|
| +
|
| + // Accessed network.
|
| + EXPECT_GT(compute_score(1, 1, 12., false, true),
|
| + compute_score(1, 1, 12., false, false));
|
| + EXPECT_GT(compute_score(1, 1, 12., false, true),
|
| + compute_score(1, 1, 2., false, false));
|
| +
|
| + // All else being equal, position matters.
|
| + EXPECT_GT(compute_score(1, 1, 2., false, false),
|
| + compute_score(1, 1, 12., false, false));
|
| + EXPECT_GT(compute_score(1, 1, 2., true, true),
|
| + compute_score(1, 1, 12., true, true));
|
| }
|
|
|
| TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) {
|
| @@ -766,13 +907,15 @@ TEST_F(ResourcePrefetchPredictorTablesTest, DatabaseIsResetWhenIncompatible) {
|
| PrefetchDataMap url_data, host_data;
|
| RedirectDataMap url_redirect_data, host_redirect_data;
|
| ManifestDataMap manifest_data;
|
| + OriginDataMap origin_data;
|
| tables_->GetAllData(&url_data, &host_data, &url_redirect_data,
|
| - &host_redirect_data, &manifest_data);
|
| + &host_redirect_data, &manifest_data, &origin_data);
|
| EXPECT_TRUE(url_data.empty());
|
| EXPECT_TRUE(host_data.empty());
|
| EXPECT_TRUE(url_redirect_data.empty());
|
| EXPECT_TRUE(host_redirect_data.empty());
|
| EXPECT_TRUE(manifest_data.empty());
|
| + EXPECT_TRUE(origin_data.empty());
|
| }
|
|
|
| TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) {
|
|
|