| OLD | NEW |
| 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 <set> | 5 #include <set> |
| 6 #include <utility> | 6 #include <utility> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/predictors/predictor_database.h" | 12 #include "chrome/browser/predictors/predictor_database.h" |
| 13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 14 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 16 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
| 17 #include "sql/statement.h" | 18 #include "sql/statement.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace predictors { | 21 namespace predictors { |
| 21 | 22 |
| 22 class ResourcePrefetchPredictorTablesTest : public testing::Test { | 23 class ResourcePrefetchPredictorTablesTest : public testing::Test { |
| 23 public: | 24 public: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 36 | 37 |
| 37 base::MessageLoop loop_; | 38 base::MessageLoop loop_; |
| 38 content::TestBrowserThread db_thread_; | 39 content::TestBrowserThread db_thread_; |
| 39 TestingProfile profile_; | 40 TestingProfile profile_; |
| 40 std::unique_ptr<PredictorDatabase> db_; | 41 std::unique_ptr<PredictorDatabase> db_; |
| 41 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 42 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 42 | 43 |
| 43 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; | 44 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; |
| 44 | 45 |
| 45 private: | 46 private: |
| 46 using ResourceRow = ResourcePrefetchPredictorTables::ResourceRow; | |
| 47 using ResourceRows = std::vector<ResourceRow>; | |
| 48 using PrefetchData = ResourcePrefetchPredictorTables::PrefetchData; | 47 using PrefetchData = ResourcePrefetchPredictorTables::PrefetchData; |
| 49 | 48 |
| 50 // Initializes the tables, |test_url_data_| and |test_host_data_|. | 49 // Initializes the tables, |test_url_data_| and |test_host_data_|. |
| 51 void InitializeSampleData(); | 50 void InitializeSampleData(); |
| 52 | 51 |
| 53 // Checks that the input PrefetchData are the same, although the resources | 52 // Checks that the input PrefetchData are the same, although the resources |
| 54 // can be in different order. | 53 // can be in different order. |
| 55 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, | 54 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, |
| 56 const PrefetchDataMap& rhs) const; | 55 const PrefetchDataMap& rhs) const; |
| 57 void TestResourceRowsAreEqual(const ResourceRows& lhs, | 56 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, |
| 58 const ResourceRows& rhs) const; | 57 const std::vector<ResourceData>& rhs) const; |
| 59 | 58 |
| 60 void AddKey(PrefetchDataMap* m, const std::string& key) const; | 59 void AddKey(PrefetchDataMap* m, const std::string& key) const; |
| 61 | 60 |
| 62 static void LogResource(const ResourceRow& row) { | |
| 63 LOG(ERROR) << "\t\t" << row.resource_url << "\t" << row.resource_type | |
| 64 << "\t" << row.number_of_hits << "\t" << row.number_of_misses | |
| 65 << "\t" << row.consecutive_misses << "\t" << row.average_position | |
| 66 << "\t" << row.priority << "\t" << row.has_validators << "\t" | |
| 67 << row.always_revalidate << "\t" << row.score; | |
| 68 } | |
| 69 | |
| 70 // Useful for debugging tests. | |
| 71 void PrintPrefetchData(const PrefetchData& data) const { | |
| 72 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key | |
| 73 << "," << data.last_visit.ToInternalValue() << "]"; | |
| 74 for (const ResourceRow& resource : data.resources) | |
| 75 LogResource(resource); | |
| 76 } | |
| 77 | |
| 78 PrefetchDataMap test_url_data_; | 61 PrefetchDataMap test_url_data_; |
| 79 PrefetchDataMap test_host_data_; | 62 PrefetchDataMap test_host_data_; |
| 80 }; | 63 }; |
| 81 | 64 |
| 82 class ResourcePrefetchPredictorTablesReopenTest | 65 class ResourcePrefetchPredictorTablesReopenTest |
| 83 : public ResourcePrefetchPredictorTablesTest { | 66 : public ResourcePrefetchPredictorTablesTest { |
| 84 public: | 67 public: |
| 85 void SetUp() override { | 68 void SetUp() override { |
| 86 // Write data to the table, and then reopen the db. | 69 // Write data to the table, and then reopen the db. |
| 87 ResourcePrefetchPredictorTablesTest::SetUp(); | 70 ResourcePrefetchPredictorTablesTest::SetUp(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 PrefetchDataMap expected_host_data; | 147 PrefetchDataMap expected_host_data; |
| 165 AddKey(&expected_host_data, "www.yahoo.com"); | 148 AddKey(&expected_host_data, "www.yahoo.com"); |
| 166 | 149 |
| 167 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 150 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 168 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 151 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 169 } | 152 } |
| 170 | 153 |
| 171 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { | 154 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { |
| 172 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 155 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
| 173 google.last_visit = base::Time::FromInternalValue(10); | 156 google.last_visit = base::Time::FromInternalValue(10); |
| 174 google.resources.push_back(ResourceRow("http://www.google.com/style.css", | 157 google.resources.push_back(CreateResourceData( |
| 175 content::RESOURCE_TYPE_STYLESHEET, 6, | 158 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 6, |
| 176 2, 0, 1.0, net::MEDIUM, true, false)); | 159 2, 0, 1.0, net::MEDIUM, true, false)); |
| 177 google.resources.push_back(ResourceRow("http://www.google.com/image.png", | 160 google.resources.push_back(CreateResourceData( |
| 178 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, | 161 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 4, 1, |
| 179 4.2, net::MEDIUM, false, false)); | 162 4.2, net::MEDIUM, false, false)); |
| 180 google.resources.push_back(ResourceRow("http://www.google.com/a.xml", | 163 google.resources.push_back(CreateResourceData( |
| 181 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, | 164 "http://www.google.com/a.xml", content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, |
| 182 0, 6.1, net::MEDIUM, false, false)); | 165 6.1, net::MEDIUM, false, false)); |
| 183 google.resources.push_back(ResourceRow( | 166 google.resources.push_back(CreateResourceData( |
| 184 "http://www.resources.google.com/script.js", | 167 "http://www.resources.google.com/script.js", |
| 185 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); | 168 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); |
| 186 | 169 |
| 187 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 170 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
| 188 yahoo.last_visit = base::Time::FromInternalValue(7); | 171 yahoo.last_visit = base::Time::FromInternalValue(7); |
| 189 yahoo.resources.push_back(ResourceRow("http://www.yahoo.com/image.png", | 172 yahoo.resources.push_back(CreateResourceData( |
| 190 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, | 173 "http://www.yahoo.com/image.png", content::RESOURCE_TYPE_IMAGE, 120, 1, 1, |
| 191 10.0, net::MEDIUM, true, false)); | 174 10.0, net::MEDIUM, true, false)); |
| 192 | 175 |
| 193 tables_->UpdateData(google, yahoo); | 176 tables_->UpdateData(google, yahoo); |
| 194 | 177 |
| 195 PrefetchDataMap actual_url_data, actual_host_data; | 178 PrefetchDataMap actual_url_data, actual_host_data; |
| 196 tables_->GetAllData(&actual_url_data, &actual_host_data); | 179 tables_->GetAllData(&actual_url_data, &actual_host_data); |
| 197 | 180 |
| 198 PrefetchDataMap expected_url_data, expected_host_data; | 181 PrefetchDataMap expected_url_data, expected_host_data; |
| 199 AddKey(&expected_url_data, "http://www.reddit.com"); | 182 AddKey(&expected_url_data, "http://www.reddit.com"); |
| 200 AddKey(&expected_url_data, "http://www.yahoo.com"); | 183 AddKey(&expected_url_data, "http://www.yahoo.com"); |
| 201 expected_url_data.insert(std::make_pair("http://www.google.com", google)); | 184 expected_url_data.insert(std::make_pair("http://www.google.com", google)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 218 | 201 |
| 219 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( | 202 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( |
| 220 const PrefetchDataMap& lhs, | 203 const PrefetchDataMap& lhs, |
| 221 const PrefetchDataMap& rhs) const { | 204 const PrefetchDataMap& rhs) const { |
| 222 EXPECT_EQ(lhs.size(), rhs.size()); | 205 EXPECT_EQ(lhs.size(), rhs.size()); |
| 223 | 206 |
| 224 for (const std::pair<std::string, PrefetchData>& p : rhs) { | 207 for (const std::pair<std::string, PrefetchData>& p : rhs) { |
| 225 PrefetchDataMap::const_iterator lhs_it = lhs.find(p.first); | 208 PrefetchDataMap::const_iterator lhs_it = lhs.find(p.first); |
| 226 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; | 209 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
| 227 | 210 |
| 228 TestResourceRowsAreEqual(lhs_it->second.resources, p.second.resources); | 211 TestResourcesAreEqual(lhs_it->second.resources, p.second.resources); |
| 229 } | 212 } |
| 230 } | 213 } |
| 231 | 214 |
| 232 void ResourcePrefetchPredictorTablesTest::TestResourceRowsAreEqual( | 215 void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual( |
| 233 const ResourceRows& lhs, | 216 const std::vector<ResourceData>& lhs, |
| 234 const ResourceRows& rhs) const { | 217 const std::vector<ResourceData>& rhs) const { |
| 235 EXPECT_EQ(lhs.size(), rhs.size()); | 218 EXPECT_EQ(lhs.size(), rhs.size()); |
| 236 | 219 |
| 237 std::set<GURL> resources_seen; | 220 std::set<std::string> resources_seen; |
| 238 for (ResourceRows::const_iterator rhs_it = rhs.begin(); | 221 for (const auto& rhs_resource : rhs) { |
| 239 rhs_it != rhs.end(); ++rhs_it) { | 222 const std::string& resource = rhs_resource.resource_url(); |
| 240 const GURL& resource = rhs_it->resource_url; | |
| 241 EXPECT_FALSE(base::ContainsKey(resources_seen, resource)); | 223 EXPECT_FALSE(base::ContainsKey(resources_seen, resource)); |
| 242 | 224 |
| 243 for (ResourceRows::const_iterator lhs_it = lhs.begin(); | 225 for (const auto& lhs_resource : lhs) { |
| 244 lhs_it != lhs.end(); ++lhs_it) { | 226 if (lhs_resource == rhs_resource) { |
| 245 if (*rhs_it == *lhs_it) { | |
| 246 resources_seen.insert(resource); | 227 resources_seen.insert(resource); |
| 247 break; | 228 break; |
| 248 } | 229 } |
| 249 } | 230 } |
| 250 EXPECT_TRUE(base::ContainsKey(resources_seen, resource)); | 231 EXPECT_TRUE(base::ContainsKey(resources_seen, resource)); |
| 251 } | 232 } |
| 252 EXPECT_EQ(lhs.size(), resources_seen.size()); | 233 EXPECT_EQ(lhs.size(), resources_seen.size()); |
| 253 } | 234 } |
| 254 | 235 |
| 255 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, | 236 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, |
| 256 const std::string& key) const { | 237 const std::string& key) const { |
| 257 PrefetchDataMap::const_iterator it = test_url_data_.find(key); | 238 PrefetchDataMap::const_iterator it = test_url_data_.find(key); |
| 258 if (it != test_url_data_.end()) { | 239 if (it != test_url_data_.end()) { |
| 259 m->insert(std::make_pair(it->first, it->second)); | 240 m->insert(std::make_pair(it->first, it->second)); |
| 260 return; | 241 return; |
| 261 } | 242 } |
| 262 it = test_host_data_.find(key); | 243 it = test_host_data_.find(key); |
| 263 ASSERT_TRUE(it != test_host_data_.end()); | 244 ASSERT_TRUE(it != test_host_data_.end()); |
| 264 m->insert(std::make_pair(it->first, it->second)); | 245 m->insert(std::make_pair(it->first, it->second)); |
| 265 } | 246 } |
| 266 | 247 |
| 267 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 248 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
| 268 { // Url data. | 249 { // Url data. |
| 269 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 250 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
| 270 google.last_visit = base::Time::FromInternalValue(1); | 251 google.last_visit = base::Time::FromInternalValue(1); |
| 271 google.resources.push_back(ResourceRow( | 252 google.resources.push_back(CreateResourceData( |
| 272 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, | 253 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, |
| 273 2, 1, 1.1, net::MEDIUM, false, false)); | 254 2, 1, 1.1, net::MEDIUM, false, false)); |
| 274 google.resources.push_back(ResourceRow("http://www.google.com/script.js", | 255 google.resources.push_back(CreateResourceData( |
| 275 content::RESOURCE_TYPE_SCRIPT, 4, 0, | 256 "http://www.google.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 0, |
| 276 1, 2.1, net::MEDIUM, false, false)); | 257 1, 2.1, net::MEDIUM, false, false)); |
| 277 google.resources.push_back(ResourceRow("http://www.google.com/image.png", | 258 google.resources.push_back(CreateResourceData( |
| 278 content::RESOURCE_TYPE_IMAGE, 6, 3, | 259 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, |
| 279 0, 2.2, net::MEDIUM, false, false)); | 260 0, 2.2, net::MEDIUM, false, false)); |
| 280 google.resources.push_back(ResourceRow( | 261 google.resources.push_back(CreateResourceData( |
| 281 "http://www.google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, | 262 "http://www.google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, |
| 282 0, 5.1, net::MEDIUM, false, false)); | 263 0, 5.1, net::MEDIUM, false, false)); |
| 283 google.resources.push_back( | 264 google.resources.push_back( |
| 284 ResourceRow("http://www.resources.google.com/script.js", | 265 CreateResourceData("http://www.resources.google.com/script.js", |
| 285 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, | 266 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
| 286 false, false)); | 267 net::MEDIUM, false, false)); |
| 287 | 268 |
| 288 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); | 269 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); |
| 289 reddit.last_visit = base::Time::FromInternalValue(2); | 270 reddit.last_visit = base::Time::FromInternalValue(2); |
| 290 reddit.resources.push_back(ResourceRow( | 271 reddit.resources.push_back(CreateResourceData( |
| 291 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, | 272 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, |
| 292 4, 0, 1, 1.0, net::MEDIUM, false, false)); | 273 4, 0, 1, 1.0, net::MEDIUM, false, false)); |
| 293 reddit.resources.push_back(ResourceRow( | 274 reddit.resources.push_back(CreateResourceData( |
| 294 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, | 275 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, |
| 295 2, 0, 0, 2.1, net::MEDIUM, false, false)); | 276 2, 0, 0, 2.1, net::MEDIUM, false, false)); |
| 296 | 277 |
| 297 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); | 278 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); |
| 298 yahoo.last_visit = base::Time::FromInternalValue(3); | 279 yahoo.last_visit = base::Time::FromInternalValue(3); |
| 299 yahoo.resources.push_back(ResourceRow("http://www.google.com/image.png", | 280 yahoo.resources.push_back(CreateResourceData( |
| 300 content::RESOURCE_TYPE_IMAGE, 20, 1, | 281 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, |
| 301 0, 10.0, net::MEDIUM, false, false)); | 282 0, 10.0, net::MEDIUM, false, false)); |
| 302 | 283 |
| 303 test_url_data_.clear(); | 284 test_url_data_.clear(); |
| 304 test_url_data_.insert(std::make_pair("http://www.google.com", google)); | 285 test_url_data_.insert(std::make_pair("http://www.google.com", google)); |
| 305 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); | 286 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); |
| 306 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); | 287 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); |
| 307 | 288 |
| 308 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); | 289 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); |
| 309 tables_->UpdateData(google, empty_host_data); | 290 tables_->UpdateData(google, empty_host_data); |
| 310 tables_->UpdateData(reddit, empty_host_data); | 291 tables_->UpdateData(reddit, empty_host_data); |
| 311 tables_->UpdateData(yahoo, empty_host_data); | 292 tables_->UpdateData(yahoo, empty_host_data); |
| 312 } | 293 } |
| 313 | 294 |
| 314 { // Host data. | 295 { // Host data. |
| 315 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); | 296 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); |
| 316 facebook.last_visit = base::Time::FromInternalValue(4); | 297 facebook.last_visit = base::Time::FromInternalValue(4); |
| 317 facebook.resources.push_back(ResourceRow( | 298 facebook.resources.push_back(CreateResourceData( |
| 318 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, | 299 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, |
| 319 5, 2, 1, 1.1, net::MEDIUM, false, false)); | 300 5, 2, 1, 1.1, net::MEDIUM, false, false)); |
| 320 facebook.resources.push_back(ResourceRow( | 301 facebook.resources.push_back(CreateResourceData( |
| 321 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, | 302 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, |
| 322 0, 1, 2.1, net::MEDIUM, false, false)); | 303 0, 1, 2.1, net::MEDIUM, false, false)); |
| 323 facebook.resources.push_back(ResourceRow( | 304 facebook.resources.push_back(CreateResourceData( |
| 324 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, | 305 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, |
| 325 0, 2.2, net::MEDIUM, false, false)); | 306 0, 2.2, net::MEDIUM, false, false)); |
| 326 facebook.resources.push_back(ResourceRow( | 307 facebook.resources.push_back(CreateResourceData( |
| 327 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, | 308 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, |
| 328 0, 0, 5.1, net::MEDIUM, false, false)); | 309 0, 0, 5.1, net::MEDIUM, false, false)); |
| 329 facebook.resources.push_back( | 310 facebook.resources.push_back( |
| 330 ResourceRow("http://www.resources.facebook.com/script.js", | 311 CreateResourceData("http://www.resources.facebook.com/script.js", |
| 331 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, | 312 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
| 332 false, false)); | 313 net::MEDIUM, false, false)); |
| 333 | 314 |
| 334 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 315 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
| 335 yahoo.last_visit = base::Time::FromInternalValue(5); | 316 yahoo.last_visit = base::Time::FromInternalValue(5); |
| 336 yahoo.resources.push_back(ResourceRow("http://www.google.com/image.png", | 317 yahoo.resources.push_back(CreateResourceData( |
| 337 content::RESOURCE_TYPE_IMAGE, 20, 1, | 318 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, |
| 338 0, 10.0, net::MEDIUM, false, false)); | 319 0, 10.0, net::MEDIUM, false, false)); |
| 339 | 320 |
| 340 test_host_data_.clear(); | 321 test_host_data_.clear(); |
| 341 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); | 322 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); |
| 342 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); | 323 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); |
| 343 | 324 |
| 344 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); | 325 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); |
| 345 tables_->UpdateData(empty_url_data, facebook); | 326 tables_->UpdateData(empty_url_data, facebook); |
| 346 tables_->UpdateData(empty_url_data, yahoo); | 327 tables_->UpdateData(empty_url_data, yahoo); |
| 347 } | 328 } |
| 348 } | 329 } |
| 349 | 330 |
| 350 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { | 331 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { |
| 351 db_.reset(new PredictorDatabase(&profile_)); | 332 db_.reset(new PredictorDatabase(&profile_)); |
| 352 base::RunLoop().RunUntilIdle(); | 333 base::RunLoop().RunUntilIdle(); |
| 353 tables_ = db_->resource_prefetch_tables(); | 334 tables_ = db_->resource_prefetch_tables(); |
| 354 } | 335 } |
| 355 | 336 |
| 356 // Test cases. | 337 // Test cases. |
| 357 | 338 |
| 358 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { | 339 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { |
| 359 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; | 340 ResourceData js_resource = CreateResourceData( |
| 360 ResourceRow js_resource("http://www.resources.google.com/script.js", | 341 "http://www.resources.google.com/script.js", |
| 361 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., | 342 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); |
| 362 net::MEDIUM, false, false); | 343 ResourceData image_resource = CreateResourceData( |
| 363 ResourceRow image_resource("http://www.resources.google.com/image.jpg", | 344 "http://www.resources.google.com/image.jpg", content::RESOURCE_TYPE_IMAGE, |
| 364 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., | 345 11, 0, 0, 1., net::MEDIUM, false, false); |
| 365 net::MEDIUM, false, false); | 346 ResourceData css_resource = |
| 366 ResourceRow css_resource("http://www.resources.google.com/stylesheet.css", | 347 CreateResourceData("http://www.resources.google.com/stylesheet.css", |
| 367 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., | 348 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., |
| 368 net::MEDIUM, false, false); | 349 net::MEDIUM, false, false); |
| 369 ResourceRow font_resource("http://www.resources.google.com/font.woff", | 350 ResourceData font_resource = |
| 370 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., | 351 CreateResourceData("http://www.resources.google.com/font.woff", |
| 371 net::MEDIUM, false, false); | 352 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., |
| 372 EXPECT_TRUE(js_resource.score == css_resource.score); | 353 net::MEDIUM, false, false); |
| 373 EXPECT_TRUE(js_resource.score == font_resource.score); | 354 float js_resource_score = |
| 374 EXPECT_NEAR(199., js_resource.score, 1e-4); | 355 ResourcePrefetchPredictorTables::ComputeScore(js_resource); |
| 375 EXPECT_NEAR(99., image_resource.score, 1e-4); | 356 float css_resource_score = |
| 357 ResourcePrefetchPredictorTables::ComputeScore(css_resource); |
| 358 float font_resource_score = |
| 359 ResourcePrefetchPredictorTables::ComputeScore(font_resource); |
| 360 float image_resource_score = |
| 361 ResourcePrefetchPredictorTables::ComputeScore(image_resource); |
| 362 |
| 363 EXPECT_TRUE(js_resource_score == css_resource_score); |
| 364 EXPECT_TRUE(js_resource_score == font_resource_score); |
| 365 EXPECT_NEAR(199., js_resource_score, 1e-4); |
| 366 EXPECT_NEAR(99., image_resource_score, 1e-4); |
| 376 } | 367 } |
| 377 | 368 |
| 378 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { | 369 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { |
| 379 TestGetAllData(); | 370 TestGetAllData(); |
| 380 } | 371 } |
| 381 | 372 |
| 382 TEST_F(ResourcePrefetchPredictorTablesTest, UpdateData) { | 373 TEST_F(ResourcePrefetchPredictorTablesTest, UpdateData) { |
| 383 TestUpdateData(); | 374 TestUpdateData(); |
| 384 } | 375 } |
| 385 | 376 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 | 425 |
| 435 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 426 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
| 436 TestDeleteSingleDataPoint(); | 427 TestDeleteSingleDataPoint(); |
| 437 } | 428 } |
| 438 | 429 |
| 439 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 430 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
| 440 TestDeleteAllData(); | 431 TestDeleteAllData(); |
| 441 } | 432 } |
| 442 | 433 |
| 443 } // namespace predictors | 434 } // namespace predictors |
| OLD | NEW |