| 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 7e19b38216a445aacbfd1eaa1597c1dbe6d2ec2d..ce660d24bce864c893b710cddd79d695edee8ace 100644
|
| --- a/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc
|
| +++ b/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc
|
| @@ -3,6 +3,7 @@
|
| // found in the LICENSE file.
|
|
|
| #include <set>
|
| +#include <sstream>
|
| #include <utility>
|
| #include <vector>
|
|
|
| @@ -11,6 +12,7 @@
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "chrome/browser/predictors/predictor_database.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 "content/public/test/test_browser_thread.h"
|
| #include "net/base/request_priority.h"
|
| @@ -19,6 +21,8 @@
|
|
|
| namespace predictors {
|
|
|
| +using chrome_browser_predictors::ResourceData;
|
| +
|
| class ResourcePrefetchPredictorTablesTest : public testing::Test {
|
| public:
|
| ResourcePrefetchPredictorTablesTest();
|
| @@ -40,8 +44,6 @@ class ResourcePrefetchPredictorTablesTest : public testing::Test {
|
| scoped_refptr<ResourcePrefetchPredictorTables> tables_;
|
|
|
| private:
|
| - typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
|
| - typedef std::vector<ResourceRow> ResourceRows;
|
| typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData;
|
| typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
|
|
|
| @@ -52,25 +54,20 @@ class ResourcePrefetchPredictorTablesTest : public testing::Test {
|
| // can be in different order.
|
| void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs,
|
| const PrefetchDataMap& rhs) const;
|
| - void TestResourceRowsAreEqual(const ResourceRows& lhs,
|
| - const ResourceRows& rhs) const;
|
| + void TestResourcesAreEqual(const std::vector<ResourceData>& lhs,
|
| + const std::vector<ResourceData>& rhs) const;
|
|
|
| void AddKey(PrefetchDataMap* m, const std::string& key) const;
|
|
|
| - static void LogResource(const ResourceRow& row) {
|
| - LOG(ERROR) << "\t\t" << row.resource_url << "\t" << row.resource_type
|
| - << "\t" << row.number_of_hits << "\t" << row.number_of_misses
|
| - << "\t" << row.consecutive_misses << "\t" << row.average_position
|
| - << "\t" << row.priority << "\t" << row.has_validators << "\t"
|
| - << row.always_revalidate << "\t" << row.score;
|
| - }
|
| -
|
| // Useful for debugging tests.
|
| void PrintPrefetchData(const PrefetchData& data) const {
|
| LOG(ERROR) << "[" << data.key_type << "," << data.primary_key
|
| << "," << data.last_visit.ToInternalValue() << "]";
|
| - for (const ResourceRow& resource : data.resources)
|
| - LogResource(resource);
|
| + for (const ResourceData& resource : data.resources) {
|
| + std::ostringstream stream;
|
| + PrintTo(resource, &stream);
|
| + LOG(ERROR) << stream.str();
|
| + }
|
| }
|
|
|
| PrefetchDataMap test_url_data_;
|
| @@ -171,26 +168,24 @@ void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() {
|
| void ResourcePrefetchPredictorTablesTest::TestUpdateData() {
|
| PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com");
|
| google.last_visit = base::Time::FromInternalValue(10);
|
| - google.resources.push_back(ResourceRow(std::string(),
|
| - "http://www.google.com/style.css",
|
| - content::RESOURCE_TYPE_STYLESHEET, 6,
|
| - 2, 0, 1.0, net::MEDIUM, true, false));
|
| - google.resources.push_back(ResourceRow(
|
| - std::string(), "http://www.google.com/image.png",
|
| - content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM, false, false));
|
| - google.resources.push_back(ResourceRow(std::string(),
|
| - "http://www.google.com/a.xml",
|
| - content::RESOURCE_TYPE_LAST_TYPE, 1, 0,
|
| - 0, 6.1, net::MEDIUM, false, false));
|
| - google.resources.push_back(ResourceRow(
|
| - std::string(), "http://www.resources.google.com/script.js",
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 6,
|
| + 2, 0, 1.0, net::MEDIUM, true, false));
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 4, 1,
|
| + 4.2, net::MEDIUM, false, false));
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/a.xml", content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0,
|
| + 6.1, net::MEDIUM, false, false));
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.resources.google.com/script.js",
|
| content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true));
|
|
|
| PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com");
|
| yahoo.last_visit = base::Time::FromInternalValue(7);
|
| - yahoo.resources.push_back(ResourceRow(
|
| - std::string(), "http://www.yahoo.com/image.png",
|
| - content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM, true, false));
|
| + yahoo.resources.push_back(MakeResourceData(
|
| + "http://www.yahoo.com/image.png", content::RESOURCE_TYPE_IMAGE, 120, 1, 1,
|
| + 10.0, net::MEDIUM, true, false));
|
|
|
| tables_->UpdateData(google, yahoo);
|
|
|
| @@ -223,35 +218,30 @@ void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual(
|
| const PrefetchDataMap& rhs) const {
|
| EXPECT_EQ(lhs.size(), rhs.size());
|
|
|
| - for (PrefetchDataMap::const_iterator rhs_it = rhs.begin();
|
| - rhs_it != rhs.end(); ++rhs_it) {
|
| - PrefetchDataMap::const_iterator lhs_it = lhs.find(rhs_it->first);
|
| - ASSERT_TRUE(lhs_it != lhs.end()) << rhs_it->first;
|
| -
|
| - TestResourceRowsAreEqual(lhs_it->second.resources,
|
| - rhs_it->second.resources);
|
| + for (const std::pair<const std::string&, PrefetchData>& p : rhs) {
|
| + PrefetchDataMap::const_iterator lhs_it = lhs.find(p.first);
|
| + ASSERT_TRUE(lhs_it != lhs.end()) << p.first;
|
| + TestResourcesAreEqual(lhs_it->second.resources, p.second.resources);
|
| }
|
| }
|
|
|
| -void ResourcePrefetchPredictorTablesTest::TestResourceRowsAreEqual(
|
| - const ResourceRows& lhs,
|
| - const ResourceRows& rhs) const {
|
| +void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual(
|
| + const std::vector<ResourceData>& lhs,
|
| + const std::vector<ResourceData>& rhs) const {
|
| EXPECT_EQ(lhs.size(), rhs.size());
|
|
|
| std::set<GURL> resources_seen;
|
| - for (ResourceRows::const_iterator rhs_it = rhs.begin();
|
| - rhs_it != rhs.end(); ++rhs_it) {
|
| - const GURL& resource = rhs_it->resource_url;
|
| - EXPECT_FALSE(base::ContainsKey(resources_seen, resource));
|
| -
|
| - for (ResourceRows::const_iterator lhs_it = lhs.begin();
|
| - lhs_it != lhs.end(); ++lhs_it) {
|
| - if (*rhs_it == *lhs_it) {
|
| - resources_seen.insert(resource);
|
| + for (const ResourceData& rhs_resource : rhs) {
|
| + const GURL& resource_url = GURL(rhs_resource.resource_url());
|
| + EXPECT_FALSE(base::ContainsKey(resources_seen, resource_url));
|
| +
|
| + for (const ResourceData& lhs_resource : lhs) {
|
| + if (rhs_resource == lhs_resource) {
|
| + resources_seen.insert(resource_url);
|
| break;
|
| }
|
| }
|
| - EXPECT_TRUE(base::ContainsKey(resources_seen, resource));
|
| + EXPECT_TRUE(base::ContainsKey(resources_seen, resource_url));
|
| }
|
| EXPECT_EQ(lhs.size(), resources_seen.size());
|
| }
|
| @@ -272,43 +262,37 @@ void ResourcePrefetchPredictorTablesTest::InitializeSampleData() {
|
| { // Url data.
|
| PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com");
|
| google.last_visit = base::Time::FromInternalValue(1);
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5,
|
| + 2, 1, 1.1, net::MEDIUM, false, false));
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 0,
|
| + 1, 2.1, net::MEDIUM, false, false));
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3,
|
| + 0, 2.2, net::MEDIUM, false, false));
|
| + google.resources.push_back(MakeResourceData(
|
| + "http://www.google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0,
|
| + 0, 5.1, net::MEDIUM, false, false));
|
| google.resources.push_back(
|
| - ResourceRow(std::string(), "http://www.google.com/style.css",
|
| - content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1,
|
| - net::MEDIUM, false, false));
|
| - google.resources.push_back(ResourceRow(std::string(),
|
| - "http://www.google.com/script.js",
|
| - content::RESOURCE_TYPE_SCRIPT, 4, 0,
|
| - 1, 2.1, net::MEDIUM, false, false));
|
| - google.resources.push_back(ResourceRow(
|
| - std::string(), "http://www.google.com/image.png",
|
| - content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false));
|
| - google.resources.push_back(
|
| - ResourceRow(std::string(), "http://www.google.com/a.font",
|
| - content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM,
|
| - false, false));
|
| - google.resources.push_back(
|
| - ResourceRow(std::string(), "http://www.resources.google.com/script.js",
|
| - content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM,
|
| - false, false));
|
| + MakeResourceData("http://www.resources.google.com/script.js",
|
| + content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5,
|
| + 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://www.google.com/image.png",
|
| - content::RESOURCE_TYPE_IMAGE, 20, 1,
|
| - 0, 10.0, net::MEDIUM, false, false));
|
| + yahoo.resources.push_back(MakeResourceData(
|
| + "http://www.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));
|
| @@ -324,32 +308,28 @@ void ResourcePrefetchPredictorTablesTest::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://www.google.com/image.png",
|
| - content::RESOURCE_TYPE_IMAGE, 20, 1,
|
| - 0, 10.0, net::MEDIUM, false, false));
|
| + yahoo.resources.push_back(MakeResourceData(
|
| + "http://www.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));
|
| @@ -364,25 +344,24 @@ void ResourcePrefetchPredictorTablesTest::InitializeSampleData() {
|
| // Test cases.
|
|
|
| TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) {
|
| - typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
|
| - ResourceRow js_resource(
|
| - std::string(), "http://www.resources.google.com/script.js",
|
| + ResourceData js_resource = MakeResourceData(
|
| + "http://www.resources.google.com/script.js",
|
| content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false);
|
| - ResourceRow image_resource(
|
| - std::string(), "http://www.resources.google.com/image.jpg",
|
| - content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM, false, false);
|
| - ResourceRow css_resource(std::string(),
|
| - "http://www.resources.google.com/stylesheet.css",
|
| - content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1.,
|
| - net::MEDIUM, false, false);
|
| - ResourceRow font_resource(std::string(),
|
| - "http://www.resources.google.com/font.woff",
|
| - content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1.,
|
| - net::MEDIUM, false, false);
|
| - EXPECT_TRUE(js_resource.score == css_resource.score);
|
| - EXPECT_TRUE(js_resource.score == font_resource.score);
|
| - EXPECT_NEAR(199., js_resource.score, 1e-4);
|
| - EXPECT_NEAR(99., image_resource.score, 1e-4);
|
| + ResourceData image_resource = MakeResourceData(
|
| + "http://www.resources.google.com/image.jpg", content::RESOURCE_TYPE_IMAGE,
|
| + 11, 0, 0, 1., net::MEDIUM, false, false);
|
| + ResourceData css_resource =
|
| + MakeResourceData("http://www.resources.google.com/stylesheet.css",
|
| + content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1.,
|
| + net::MEDIUM, false, false);
|
| + ResourceData font_resource =
|
| + MakeResourceData("http://www.resources.google.com/font.woff",
|
| + content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1.,
|
| + net::MEDIUM, false, false);
|
| + EXPECT_TRUE(js_resource.score() == css_resource.score());
|
| + EXPECT_TRUE(js_resource.score() == font_resource.score());
|
| + EXPECT_NEAR(199., js_resource.score(), 1e-4);
|
| + EXPECT_NEAR(99., image_resource.score(), 1e-4);
|
| }
|
|
|
| TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) {
|
|
|