| 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 <memory> | 5 #include <memory> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 content::TestBrowserThreadBundle thread_bundle_; | 39 content::TestBrowserThreadBundle thread_bundle_; |
| 40 TestingProfile profile_; | 40 TestingProfile profile_; |
| 41 std::unique_ptr<PredictorDatabase> db_; | 41 std::unique_ptr<PredictorDatabase> db_; |
| 42 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 42 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 43 | 43 |
| 44 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; | 44 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; |
| 45 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; | 45 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 using PrefetchData = ResourcePrefetchPredictorTables::PrefetchData; | |
| 49 | |
| 50 // Initializes the tables, |test_url_data_| and |test_host_data_|. | 48 // Initializes the tables, |test_url_data_| and |test_host_data_|. |
| 51 void InitializeSampleData(); | 49 void InitializeSampleData(); |
| 52 | 50 |
| 53 // Checks that the input PrefetchData are the same, although the resources | 51 // Checks that the input PrefetchData are the same, although the resources |
| 54 // can be in different order. | 52 // can be in different order. |
| 55 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, | 53 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, |
| 56 const PrefetchDataMap& rhs) const; | 54 const PrefetchDataMap& rhs) const; |
| 57 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, | 55 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, |
| 58 const std::vector<ResourceData>& rhs) const; | 56 const std::vector<ResourceData>& rhs) const; |
| 59 | 57 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 218 |
| 221 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 219 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 222 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 220 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 223 TestRedirectDataAreEqual(expected_url_redirect_data, | 221 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 224 actual_url_redirect_data); | 222 actual_url_redirect_data); |
| 225 TestRedirectDataAreEqual(expected_host_redirect_data, | 223 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 226 actual_host_redirect_data); | 224 actual_host_redirect_data); |
| 227 } | 225 } |
| 228 | 226 |
| 229 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { | 227 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { |
| 230 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 228 PrefetchData google = CreatePrefetchData("http://www.google.com", 10); |
| 231 google.last_visit = base::Time::FromInternalValue(10); | 229 InitializeResourceData(google.add_resources(), |
| 232 google.resources.push_back(CreateResourceData( | 230 "http://www.google.com/style.css", |
| 233 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 6, | 231 content::RESOURCE_TYPE_STYLESHEET, 6, 2, 0, 1.0, |
| 234 2, 0, 1.0, net::MEDIUM, true, false)); | 232 net::MEDIUM, true, false); |
| 235 google.resources.push_back(CreateResourceData( | 233 InitializeResourceData( |
| 236 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 4, 1, | 234 google.add_resources(), "http://www.google.com/image.png", |
| 237 4.2, net::MEDIUM, false, false)); | 235 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM, false, false); |
| 238 google.resources.push_back(CreateResourceData( | 236 InitializeResourceData(google.add_resources(), "http://www.google.com/a.xml", |
| 239 "http://www.google.com/a.xml", content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, | 237 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, 6.1, |
| 240 6.1, net::MEDIUM, false, false)); | 238 net::MEDIUM, false, false); |
| 241 google.resources.push_back(CreateResourceData( | 239 InitializeResourceData( |
| 242 "http://www.resources.google.com/script.js", | 240 google.add_resources(), "http://www.resources.google.com/script.js", |
| 243 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); | 241 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true); |
| 244 | 242 |
| 245 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 243 PrefetchData yahoo = CreatePrefetchData("www.yahoo.com", 7); |
| 246 yahoo.last_visit = base::Time::FromInternalValue(7); | 244 InitializeResourceData( |
| 247 yahoo.resources.push_back(CreateResourceData( | 245 yahoo.add_resources(), "http://www.yahoo.com/image.png", |
| 248 "http://www.yahoo.com/image.png", content::RESOURCE_TYPE_IMAGE, 120, 1, 1, | 246 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM, true, false); |
| 249 10.0, net::MEDIUM, true, false)); | |
| 250 | 247 |
| 251 RedirectData facebook; | 248 RedirectData facebook = CreateRedirectData("http://fb.com/google", 20); |
| 252 facebook.set_primary_key("http://fb.com/google"); | |
| 253 facebook.set_last_visit_time(20); | |
| 254 InitializeRedirectStat(facebook.add_redirect_endpoints(), | 249 InitializeRedirectStat(facebook.add_redirect_endpoints(), |
| 255 "https://facebook.fr/google", 4, 2, 1); | 250 "https://facebook.fr/google", 4, 2, 1); |
| 256 | 251 |
| 257 RedirectData microsoft; | 252 RedirectData microsoft = CreateRedirectData("microsoft.com", 21); |
| 258 microsoft.set_primary_key("microsoft.com"); | |
| 259 microsoft.set_last_visit_time(21); | |
| 260 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "m.microsoft.com", | 253 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "m.microsoft.com", |
| 261 5, 7, 1); | 254 5, 7, 1); |
| 262 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "microsoft.org", 7, | 255 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "microsoft.org", 7, |
| 263 2, 0); | 256 2, 0); |
| 264 | 257 |
| 265 tables_->UpdateData(google, yahoo, facebook, microsoft); | 258 tables_->UpdateData(google, yahoo, facebook, microsoft); |
| 266 | 259 |
| 267 PrefetchDataMap actual_url_data, actual_host_data; | 260 PrefetchDataMap actual_url_data, actual_host_data; |
| 268 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; | 261 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 269 tables_->GetAllData(&actual_url_data, &actual_host_data, | 262 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 EXPECT_TRUE(actual_host_data.empty()); | 299 EXPECT_TRUE(actual_host_data.empty()); |
| 307 EXPECT_TRUE(actual_url_redirect_data.empty()); | 300 EXPECT_TRUE(actual_url_redirect_data.empty()); |
| 308 EXPECT_TRUE(actual_host_redirect_data.empty()); | 301 EXPECT_TRUE(actual_host_redirect_data.empty()); |
| 309 } | 302 } |
| 310 | 303 |
| 311 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( | 304 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( |
| 312 const PrefetchDataMap& lhs, | 305 const PrefetchDataMap& lhs, |
| 313 const PrefetchDataMap& rhs) const { | 306 const PrefetchDataMap& rhs) const { |
| 314 EXPECT_EQ(lhs.size(), rhs.size()); | 307 EXPECT_EQ(lhs.size(), rhs.size()); |
| 315 | 308 |
| 316 for (const std::pair<std::string, PrefetchData>& p : rhs) { | 309 for (const auto& p : rhs) { |
| 317 const auto lhs_it = lhs.find(p.first); | 310 const auto lhs_it = lhs.find(p.first); |
| 318 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; | 311 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
| 319 EXPECT_TRUE(lhs_it->second.key_type == p.second.key_type); | 312 EXPECT_TRUE(lhs_it->second.primary_key() == p.second.primary_key()); |
| 320 EXPECT_TRUE(lhs_it->second.last_visit == p.second.last_visit); | 313 EXPECT_TRUE(lhs_it->second.last_visit_time() == p.second.last_visit_time()); |
| 321 | 314 |
| 322 TestResourcesAreEqual(lhs_it->second.resources, p.second.resources); | 315 std::vector<ResourceData> lhs_resources(lhs_it->second.resources().begin(), |
| 316 lhs_it->second.resources().end()); |
| 317 std::vector<ResourceData> rhs_resources(p.second.resources().begin(), |
| 318 p.second.resources().end()); |
| 319 |
| 320 TestResourcesAreEqual(lhs_resources, rhs_resources); |
| 323 } | 321 } |
| 324 } | 322 } |
| 325 | 323 |
| 326 void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual( | 324 void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual( |
| 327 const std::vector<ResourceData>& lhs, | 325 const std::vector<ResourceData>& lhs, |
| 328 const std::vector<ResourceData>& rhs) const { | 326 const std::vector<ResourceData>& rhs) const { |
| 329 EXPECT_EQ(lhs.size(), rhs.size()); | 327 EXPECT_EQ(lhs.size(), rhs.size()); |
| 330 | 328 |
| 331 std::set<std::string> resources_seen; | 329 std::map<std::string, ResourceData> lhs_index; |
| 332 for (const auto& rhs_resource : rhs) { | 330 // Repeated resources are not allowed. |
| 333 const std::string& resource = rhs_resource.resource_url(); | 331 for (const auto& r : lhs) |
| 334 EXPECT_FALSE(base::ContainsKey(resources_seen, resource)); | 332 EXPECT_TRUE(lhs_index.insert(std::make_pair(r.resource_url(), r)).second); |
| 335 | 333 |
| 336 for (const auto& lhs_resource : lhs) { | 334 for (const auto& r : rhs) { |
| 337 if (lhs_resource == rhs_resource) { | 335 auto lhs_it = lhs_index.find(r.resource_url()); |
| 338 resources_seen.insert(resource); | 336 if (lhs_it != lhs_index.end()) { |
| 339 break; | 337 EXPECT_EQ(r, lhs_it->second); |
| 340 } | 338 lhs_index.erase(lhs_it); |
| 339 } else { |
| 340 ADD_FAILURE() << r.resource_url(); |
| 341 } | 341 } |
| 342 EXPECT_TRUE(base::ContainsKey(resources_seen, resource)); | |
| 343 } | 342 } |
| 344 EXPECT_EQ(lhs.size(), resources_seen.size()); | 343 |
| 344 EXPECT_TRUE(lhs_index.empty()); |
| 345 } | 345 } |
| 346 | 346 |
| 347 void ResourcePrefetchPredictorTablesTest::TestRedirectDataAreEqual( | 347 void ResourcePrefetchPredictorTablesTest::TestRedirectDataAreEqual( |
| 348 const RedirectDataMap& lhs, | 348 const RedirectDataMap& lhs, |
| 349 const RedirectDataMap& rhs) const { | 349 const RedirectDataMap& rhs) const { |
| 350 EXPECT_EQ(lhs.size(), rhs.size()); | 350 EXPECT_EQ(lhs.size(), rhs.size()); |
| 351 | 351 |
| 352 for (const auto& p : rhs) { | 352 for (const auto& p : rhs) { |
| 353 const auto lhs_it = lhs.find(p.first); | 353 const auto lhs_it = lhs.find(p.first); |
| 354 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; | 354 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 if (it != test_url_redirect_data_.end()) { | 407 if (it != test_url_redirect_data_.end()) { |
| 408 m->insert(*it); | 408 m->insert(*it); |
| 409 return; | 409 return; |
| 410 } | 410 } |
| 411 it = test_host_redirect_data_.find(key); | 411 it = test_host_redirect_data_.find(key); |
| 412 ASSERT_TRUE(it != test_host_redirect_data_.end()); | 412 ASSERT_TRUE(it != test_host_redirect_data_.end()); |
| 413 m->insert(*it); | 413 m->insert(*it); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 416 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
| 417 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); | 417 PrefetchData empty_resource_data; |
| 418 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); | 418 RedirectData empty_redirect_data; |
| 419 RedirectData empty_url_redirect_data; | |
| 420 RedirectData empty_host_redirect_data; | |
| 421 | 419 |
| 422 { // Url data. | 420 { // Url data. |
| 423 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 421 PrefetchData google = CreatePrefetchData("http://www.google.com", 1); |
| 424 google.last_visit = base::Time::FromInternalValue(1); | 422 InitializeResourceData(google.add_resources(), |
| 425 google.resources.push_back(CreateResourceData( | 423 "http://www.google.com/style.css", |
| 426 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, | 424 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, |
| 427 2, 1, 1.1, net::MEDIUM, false, false)); | 425 net::MEDIUM, false, false); |
| 428 google.resources.push_back(CreateResourceData( | 426 InitializeResourceData( |
| 429 "http://www.google.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 0, | 427 google.add_resources(), "http://www.google.com/script.js", |
| 430 1, 2.1, net::MEDIUM, false, false)); | 428 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM, false, false); |
| 431 google.resources.push_back(CreateResourceData( | 429 InitializeResourceData( |
| 432 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, | 430 google.add_resources(), "http://www.google.com/image.png", |
| 433 0, 2.2, net::MEDIUM, false, false)); | 431 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false); |
| 434 google.resources.push_back(CreateResourceData( | 432 InitializeResourceData(google.add_resources(), |
| 435 "http://www.google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, | 433 "http://www.google.com/a.font", |
| 436 0, 5.1, net::MEDIUM, false, false)); | 434 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, |
| 437 google.resources.push_back( | 435 net::MEDIUM, false, false); |
| 438 CreateResourceData("http://www.resources.google.com/script.js", | 436 InitializeResourceData(google.add_resources(), |
| 437 "http://www.resources.google.com/script.js", |
| 439 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, | 438 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
| 440 net::MEDIUM, false, false)); | 439 net::MEDIUM, false, false); |
| 441 | 440 |
| 442 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); | 441 PrefetchData reddit = CreatePrefetchData("http://www.reddit.com", 2); |
| 443 reddit.last_visit = base::Time::FromInternalValue(2); | 442 InitializeResourceData( |
| 444 reddit.resources.push_back(CreateResourceData( | 443 reddit.add_resources(), "http://reddit-resource.com/script1.js", |
| 445 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, | 444 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM, false, false); |
| 446 4, 0, 1, 1.0, net::MEDIUM, false, false)); | 445 InitializeResourceData( |
| 447 reddit.resources.push_back(CreateResourceData( | 446 reddit.add_resources(), "http://reddit-resource.com/script2.js", |
| 448 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, | 447 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM, false, false); |
| 449 2, 0, 0, 2.1, net::MEDIUM, false, false)); | |
| 450 | 448 |
| 451 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); | 449 PrefetchData yahoo = CreatePrefetchData("http://www.yahoo.com", 3); |
| 452 yahoo.last_visit = base::Time::FromInternalValue(3); | 450 InitializeResourceData(yahoo.add_resources(), |
| 453 yahoo.resources.push_back(CreateResourceData( | 451 "http://www.google.com/image.png", |
| 454 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, | 452 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, |
| 455 0, 10.0, net::MEDIUM, false, false)); | 453 net::MEDIUM, false, false); |
| 456 | 454 |
| 457 test_url_data_.clear(); | 455 test_url_data_.clear(); |
| 458 test_url_data_.insert(std::make_pair("http://www.google.com", google)); | 456 test_url_data_.insert(std::make_pair(google.primary_key(), google)); |
| 459 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); | 457 test_url_data_.insert(std::make_pair(reddit.primary_key(), reddit)); |
| 460 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); | 458 test_url_data_.insert(std::make_pair(yahoo.primary_key(), yahoo)); |
| 461 | 459 |
| 462 tables_->UpdateData(google, empty_host_data, empty_url_redirect_data, | 460 tables_->UpdateData(google, empty_resource_data, empty_redirect_data, |
| 463 empty_host_redirect_data); | 461 empty_redirect_data); |
| 464 tables_->UpdateData(reddit, empty_host_data, empty_url_redirect_data, | 462 tables_->UpdateData(reddit, empty_resource_data, empty_redirect_data, |
| 465 empty_host_redirect_data); | 463 empty_redirect_data); |
| 466 tables_->UpdateData(yahoo, empty_host_data, empty_url_redirect_data, | 464 tables_->UpdateData(yahoo, empty_resource_data, empty_redirect_data, |
| 467 empty_host_redirect_data); | 465 empty_redirect_data); |
| 468 } | 466 } |
| 469 | 467 |
| 470 { // Host data. | 468 { // Host data. |
| 471 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); | 469 PrefetchData facebook = CreatePrefetchData("www.facebook.com", 4); |
| 472 facebook.last_visit = base::Time::FromInternalValue(4); | 470 InitializeResourceData(facebook.add_resources(), |
| 473 facebook.resources.push_back(CreateResourceData( | 471 "http://www.facebook.com/style.css", |
| 474 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, | 472 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, |
| 475 5, 2, 1, 1.1, net::MEDIUM, false, false)); | 473 net::MEDIUM, false, false); |
| 476 facebook.resources.push_back(CreateResourceData( | 474 InitializeResourceData( |
| 477 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, | 475 facebook.add_resources(), "http://www.facebook.com/script.js", |
| 478 0, 1, 2.1, net::MEDIUM, false, false)); | 476 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM, false, false); |
| 479 facebook.resources.push_back(CreateResourceData( | 477 InitializeResourceData( |
| 480 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, | 478 facebook.add_resources(), "http://www.facebook.com/image.png", |
| 481 0, 2.2, net::MEDIUM, false, false)); | 479 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false); |
| 482 facebook.resources.push_back(CreateResourceData( | 480 InitializeResourceData(facebook.add_resources(), |
| 483 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, | 481 "http://www.facebook.com/a.font", |
| 484 0, 0, 5.1, net::MEDIUM, false, false)); | 482 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, |
| 485 facebook.resources.push_back( | 483 net::MEDIUM, false, false); |
| 486 CreateResourceData("http://www.resources.facebook.com/script.js", | 484 InitializeResourceData(facebook.add_resources(), |
| 485 "http://www.resources.facebook.com/script.js", |
| 487 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, | 486 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
| 488 net::MEDIUM, false, false)); | 487 net::MEDIUM, false, false); |
| 489 | 488 |
| 490 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 489 PrefetchData yahoo = CreatePrefetchData("www.yahoo.com", 5); |
| 491 yahoo.last_visit = base::Time::FromInternalValue(5); | 490 InitializeResourceData(yahoo.add_resources(), |
| 492 yahoo.resources.push_back(CreateResourceData( | 491 "http://www.google.com/image.png", |
| 493 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, | 492 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, |
| 494 0, 10.0, net::MEDIUM, false, false)); | 493 net::MEDIUM, false, false); |
| 495 | 494 |
| 496 test_host_data_.clear(); | 495 test_host_data_.clear(); |
| 497 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); | 496 test_host_data_.insert(std::make_pair(facebook.primary_key(), facebook)); |
| 498 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); | 497 test_host_data_.insert(std::make_pair(yahoo.primary_key(), yahoo)); |
| 499 | 498 |
| 500 tables_->UpdateData(empty_url_data, facebook, empty_url_redirect_data, | 499 tables_->UpdateData(empty_resource_data, facebook, empty_redirect_data, |
| 501 empty_host_redirect_data); | 500 empty_redirect_data); |
| 502 tables_->UpdateData(empty_url_data, yahoo, empty_url_redirect_data, | 501 tables_->UpdateData(empty_resource_data, yahoo, empty_redirect_data, |
| 503 empty_host_redirect_data); | 502 empty_redirect_data); |
| 504 } | 503 } |
| 505 | 504 |
| 506 { // Url redirect data. | 505 { // Url redirect data. |
| 507 RedirectData facebook; | 506 RedirectData facebook = CreateRedirectData("http://fb.com/google", 6); |
| 508 facebook.set_primary_key("http://fb.com/google"); | |
| 509 facebook.set_last_visit_time(6); | |
| 510 InitializeRedirectStat(facebook.add_redirect_endpoints(), | 507 InitializeRedirectStat(facebook.add_redirect_endpoints(), |
| 511 "https://facebook.com/google", 5, 1, 0); | 508 "https://facebook.com/google", 5, 1, 0); |
| 512 InitializeRedirectStat(facebook.add_redirect_endpoints(), | 509 InitializeRedirectStat(facebook.add_redirect_endpoints(), |
| 513 "https://facebook.com/login", 3, 5, 1); | 510 "https://facebook.com/login", 3, 5, 1); |
| 514 | 511 |
| 515 RedirectData nytimes; | 512 RedirectData nytimes = CreateRedirectData("http://nyt.com", 7); |
| 516 nytimes.set_primary_key("http://nyt.com"); | |
| 517 nytimes.set_last_visit_time(7); | |
| 518 InitializeRedirectStat(nytimes.add_redirect_endpoints(), | 513 InitializeRedirectStat(nytimes.add_redirect_endpoints(), |
| 519 "https://nytimes.com", 2, 0, 0); | 514 "https://nytimes.com", 2, 0, 0); |
| 520 | 515 |
| 521 RedirectData google; | 516 RedirectData google = CreateRedirectData("http://google.com", 8); |
| 522 google.set_primary_key("http://google.com"); | |
| 523 google.set_last_visit_time(8); | |
| 524 InitializeRedirectStat(google.add_redirect_endpoints(), | 517 InitializeRedirectStat(google.add_redirect_endpoints(), |
| 525 "https://google.com", 3, 0, 0); | 518 "https://google.com", 3, 0, 0); |
| 526 | 519 |
| 527 test_url_redirect_data_.clear(); | 520 test_url_redirect_data_.clear(); |
| 528 test_url_redirect_data_.insert( | 521 test_url_redirect_data_.insert( |
| 529 std::make_pair(facebook.primary_key(), facebook)); | 522 std::make_pair(facebook.primary_key(), facebook)); |
| 530 test_url_redirect_data_.insert( | 523 test_url_redirect_data_.insert( |
| 531 std::make_pair(nytimes.primary_key(), nytimes)); | 524 std::make_pair(nytimes.primary_key(), nytimes)); |
| 532 test_url_redirect_data_.insert( | 525 test_url_redirect_data_.insert( |
| 533 std::make_pair(google.primary_key(), google)); | 526 std::make_pair(google.primary_key(), google)); |
| 534 | 527 |
| 535 tables_->UpdateData(empty_url_data, empty_host_data, facebook, | 528 tables_->UpdateData(empty_resource_data, empty_resource_data, facebook, |
| 536 empty_host_redirect_data); | 529 empty_redirect_data); |
| 537 tables_->UpdateData(empty_url_data, empty_host_data, nytimes, | 530 tables_->UpdateData(empty_resource_data, empty_resource_data, nytimes, |
| 538 empty_host_redirect_data); | 531 empty_redirect_data); |
| 539 tables_->UpdateData(empty_url_data, empty_host_data, google, | 532 tables_->UpdateData(empty_resource_data, empty_resource_data, google, |
| 540 empty_host_redirect_data); | 533 empty_redirect_data); |
| 541 } | 534 } |
| 542 | 535 |
| 543 { // Host redirect data. | 536 { // Host redirect data. |
| 544 RedirectData bbc; | 537 RedirectData bbc = CreateRedirectData("bbc.com", 9); |
| 545 bbc.set_primary_key("bbc.com"); | |
| 546 bbc.set_last_visit_time(9); | |
| 547 InitializeRedirectStat(bbc.add_redirect_endpoints(), "www.bbc.com", 8, 4, | 538 InitializeRedirectStat(bbc.add_redirect_endpoints(), "www.bbc.com", 8, 4, |
| 548 1); | 539 1); |
| 549 InitializeRedirectStat(bbc.add_redirect_endpoints(), "m.bbc.com", 5, 8, 0); | 540 InitializeRedirectStat(bbc.add_redirect_endpoints(), "m.bbc.com", 5, 8, 0); |
| 550 InitializeRedirectStat(bbc.add_redirect_endpoints(), "bbc.co.uk", 1, 3, 0); | 541 InitializeRedirectStat(bbc.add_redirect_endpoints(), "bbc.co.uk", 1, 3, 0); |
| 551 | 542 |
| 552 RedirectData microsoft; | 543 RedirectData microsoft = CreateRedirectData("microsoft.com", 10); |
| 553 microsoft.set_primary_key("microsoft.com"); | |
| 554 microsoft.set_last_visit_time(10); | |
| 555 InitializeRedirectStat(microsoft.add_redirect_endpoints(), | 544 InitializeRedirectStat(microsoft.add_redirect_endpoints(), |
| 556 "www.microsoft.com", 10, 0, 0); | 545 "www.microsoft.com", 10, 0, 0); |
| 557 | 546 |
| 558 test_host_redirect_data_.clear(); | 547 test_host_redirect_data_.clear(); |
| 559 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); | 548 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); |
| 560 test_host_redirect_data_.insert( | 549 test_host_redirect_data_.insert( |
| 561 std::make_pair(microsoft.primary_key(), microsoft)); | 550 std::make_pair(microsoft.primary_key(), microsoft)); |
| 562 tables_->UpdateData(empty_url_data, empty_host_data, | 551 tables_->UpdateData(empty_resource_data, empty_resource_data, |
| 563 empty_url_redirect_data, bbc); | 552 empty_redirect_data, bbc); |
| 564 tables_->UpdateData(empty_url_data, empty_host_data, | 553 tables_->UpdateData(empty_resource_data, empty_resource_data, |
| 565 empty_url_redirect_data, microsoft); | 554 empty_redirect_data, microsoft); |
| 566 } | 555 } |
| 567 } | 556 } |
| 568 | 557 |
| 569 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { | 558 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { |
| 570 db_.reset(new PredictorDatabase(&profile_)); | 559 db_.reset(new PredictorDatabase(&profile_)); |
| 571 base::RunLoop().RunUntilIdle(); | 560 base::RunLoop().RunUntilIdle(); |
| 572 tables_ = db_->resource_prefetch_tables(); | 561 tables_ = db_->resource_prefetch_tables(); |
| 573 } | 562 } |
| 574 | 563 |
| 575 // Test cases. | 564 // Test cases. |
| 576 | 565 |
| 577 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeResourceScore) { | 566 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeResourceScore) { |
| 578 auto compute_score = [](net::RequestPriority priority, | 567 auto compute_score = [](net::RequestPriority priority, |
| 579 content::ResourceType resource_type, | 568 content::ResourceType resource_type, |
| 580 double average_position) { | 569 double average_position) { |
| 581 return ResourcePrefetchPredictorTables::ComputeResourceScore( | 570 ResourceData resource; |
| 582 CreateResourceData("", resource_type, 0, 0, 0, average_position, | 571 InitializeResourceData(&resource, "", resource_type, 0, 0, 0, |
| 583 priority, false, false)); | 572 average_position, priority, false, false); |
| 573 return ResourcePrefetchPredictorTables::ComputeResourceScore(resource); |
| 584 }; | 574 }; |
| 585 | 575 |
| 586 // Priority is more important than the rest. | 576 // Priority is more important than the rest. |
| 587 EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 1.) > | 577 EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_SCRIPT, 1.) > |
| 588 compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.)); | 578 compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.)); |
| 589 | 579 |
| 590 EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) > | 580 EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) > |
| 591 compute_score(net::MEDIUM, content::RESOURCE_TYPE_SCRIPT, 1.)); | 581 compute_score(net::MEDIUM, content::RESOURCE_TYPE_SCRIPT, 1.)); |
| 592 EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) > | 582 EXPECT_TRUE(compute_score(net::HIGHEST, content::RESOURCE_TYPE_IMAGE, 42.) > |
| 593 compute_score(net::LOW, content::RESOURCE_TYPE_SCRIPT, 1.)); | 583 compute_score(net::LOW, content::RESOURCE_TYPE_SCRIPT, 1.)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 | 669 |
| 680 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 670 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
| 681 TestDeleteSingleDataPoint(); | 671 TestDeleteSingleDataPoint(); |
| 682 } | 672 } |
| 683 | 673 |
| 684 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 674 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
| 685 TestDeleteAllData(); | 675 TestDeleteAllData(); |
| 686 } | 676 } |
| 687 | 677 |
| 688 } // namespace predictors | 678 } // namespace predictors |
| OLD | NEW |