| 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/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "net/base/request_priority.h" |
| 16 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace predictors { | 20 namespace predictors { |
| 20 | 21 |
| 21 class ResourcePrefetchPredictorTablesTest : public testing::Test { | 22 class ResourcePrefetchPredictorTablesTest : public testing::Test { |
| 22 public: | 23 public: |
| 23 ResourcePrefetchPredictorTablesTest(); | 24 ResourcePrefetchPredictorTablesTest(); |
| 24 ~ResourcePrefetchPredictorTablesTest() override; | 25 ~ResourcePrefetchPredictorTablesTest() override; |
| 25 void SetUp() override; | 26 void SetUp() override; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 void AddKey(PrefetchDataMap* m, const std::string& key) const; | 58 void AddKey(PrefetchDataMap* m, const std::string& key) const; |
| 58 | 59 |
| 59 // Useful for debugging test. | 60 // Useful for debugging test. |
| 60 void PrintPrefetchData(const PrefetchData& data) const { | 61 void PrintPrefetchData(const PrefetchData& data) const { |
| 61 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key | 62 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key |
| 62 << "," << data.last_visit.ToInternalValue() << "]"; | 63 << "," << data.last_visit.ToInternalValue() << "]"; |
| 63 for (ResourceRows::const_iterator it = data.resources.begin(); | 64 for (ResourceRows::const_iterator it = data.resources.begin(); |
| 64 it != data.resources.end(); ++it) { | 65 it != data.resources.end(); ++it) { |
| 65 LOG(ERROR) << "\t\t" << it->resource_url << "\t" << it->resource_type | 66 LOG(ERROR) << "\t\t" << it->resource_url << "\t" << it->resource_type |
| 66 << "\t" << it->number_of_hits << "\t" << it->number_of_misses | 67 << "\t" << it->number_of_hits << "\t" << it->number_of_misses |
| 67 << "\t" << it->consecutive_misses | 68 << "\t" << it->consecutive_misses << "\t" |
| 68 << "\t" << it->average_position | 69 << it->average_position << "\t" << it->priority << "\t" |
| 69 << "\t" << it->score; | 70 << it->score; |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 PrefetchDataMap test_url_data_; | 74 PrefetchDataMap test_url_data_; |
| 74 PrefetchDataMap test_host_data_; | 75 PrefetchDataMap test_host_data_; |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 class ResourcePrefetchPredictorTablesReopenTest | 78 class ResourcePrefetchPredictorTablesReopenTest |
| 78 : public ResourcePrefetchPredictorTablesTest { | 79 : public ResourcePrefetchPredictorTablesTest { |
| 79 public: | 80 public: |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 PrefetchDataMap expected_host_data; | 162 PrefetchDataMap expected_host_data; |
| 162 AddKey(&expected_host_data, "www.yahoo.com"); | 163 AddKey(&expected_host_data, "www.yahoo.com"); |
| 163 | 164 |
| 164 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 165 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 165 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 166 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { | 169 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { |
| 169 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 170 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
| 170 google.last_visit = base::Time::FromInternalValue(10); | 171 google.last_visit = base::Time::FromInternalValue(10); |
| 171 google.resources.push_back(ResourceRow(std::string(), | 172 google.resources.push_back(ResourceRow( |
| 172 "http://www.google.com/style.css", | 173 std::string(), "http://www.google.com/style.css", |
| 173 content::RESOURCE_TYPE_STYLESHEET, | 174 content::RESOURCE_TYPE_STYLESHEET, 6, 2, 0, 1.0, net::MEDIUM)); |
| 174 6, | 175 google.resources.push_back( |
| 175 2, | 176 ResourceRow(std::string(), "http://www.google.com/image.png", |
| 176 0, | 177 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM)); |
| 177 1.0)); | 178 google.resources.push_back( |
| 178 google.resources.push_back(ResourceRow(std::string(), | 179 ResourceRow(std::string(), "http://www.google.com/a.xml", |
| 179 "http://www.google.com/image.png", | 180 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, 6.1, net::MEDIUM)); |
| 180 content::RESOURCE_TYPE_IMAGE, | 181 google.resources.push_back( |
| 181 6, | 182 ResourceRow(std::string(), "http://www.resources.google.com/script.js", |
| 182 4, | 183 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM)); |
| 183 1, | |
| 184 4.2)); | |
| 185 google.resources.push_back(ResourceRow(std::string(), | |
| 186 "http://www.google.com/a.xml", | |
| 187 content::RESOURCE_TYPE_LAST_TYPE, | |
| 188 1, | |
| 189 0, | |
| 190 0, | |
| 191 6.1)); | |
| 192 google.resources | |
| 193 .push_back(ResourceRow(std::string(), | |
| 194 "http://www.resources.google.com/script.js", | |
| 195 content::RESOURCE_TYPE_SCRIPT, | |
| 196 12, | |
| 197 0, | |
| 198 0, | |
| 199 8.5)); | |
| 200 | 184 |
| 201 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 185 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
| 202 yahoo.last_visit = base::Time::FromInternalValue(7); | 186 yahoo.last_visit = base::Time::FromInternalValue(7); |
| 203 yahoo.resources.push_back(ResourceRow(std::string(), | 187 yahoo.resources.push_back( |
| 204 "http://www.yahoo.com/image.png", | 188 ResourceRow(std::string(), "http://www.yahoo.com/image.png", |
| 205 content::RESOURCE_TYPE_IMAGE, | 189 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM)); |
| 206 120, | |
| 207 1, | |
| 208 1, | |
| 209 10.0)); | |
| 210 | 190 |
| 211 tables_->UpdateData(google, yahoo); | 191 tables_->UpdateData(google, yahoo); |
| 212 | 192 |
| 213 PrefetchDataMap actual_url_data, actual_host_data; | 193 PrefetchDataMap actual_url_data, actual_host_data; |
| 214 tables_->GetAllData(&actual_url_data, &actual_host_data); | 194 tables_->GetAllData(&actual_url_data, &actual_host_data); |
| 215 | 195 |
| 216 PrefetchDataMap expected_url_data, expected_host_data; | 196 PrefetchDataMap expected_url_data, expected_host_data; |
| 217 AddKey(&expected_url_data, "http://www.reddit.com"); | 197 AddKey(&expected_url_data, "http://www.reddit.com"); |
| 218 AddKey(&expected_url_data, "http://www.yahoo.com"); | 198 AddKey(&expected_url_data, "http://www.yahoo.com"); |
| 219 expected_url_data.insert(std::make_pair("http://www.google.com", google)); | 199 expected_url_data.insert(std::make_pair("http://www.google.com", google)); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } | 261 } |
| 282 it = test_host_data_.find(key); | 262 it = test_host_data_.find(key); |
| 283 ASSERT_TRUE(it != test_host_data_.end()); | 263 ASSERT_TRUE(it != test_host_data_.end()); |
| 284 m->insert(std::make_pair(it->first, it->second)); | 264 m->insert(std::make_pair(it->first, it->second)); |
| 285 } | 265 } |
| 286 | 266 |
| 287 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 267 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
| 288 { // Url data. | 268 { // Url data. |
| 289 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 269 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
| 290 google.last_visit = base::Time::FromInternalValue(1); | 270 google.last_visit = base::Time::FromInternalValue(1); |
| 291 google.resources.push_back(ResourceRow(std::string(), | 271 google.resources.push_back(ResourceRow( |
| 292 "http://www.google.com/style.css", | 272 std::string(), "http://www.google.com/style.css", |
| 293 content::RESOURCE_TYPE_STYLESHEET, | 273 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, net::MEDIUM)); |
| 294 5, | 274 google.resources.push_back( |
| 295 2, | 275 ResourceRow(std::string(), "http://www.google.com/script.js", |
| 296 1, | 276 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM)); |
| 297 1.1)); | 277 google.resources.push_back( |
| 298 google.resources.push_back(ResourceRow(std::string(), | 278 ResourceRow(std::string(), "http://www.google.com/image.png", |
| 299 "http://www.google.com/script.js", | 279 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM)); |
| 300 content::RESOURCE_TYPE_SCRIPT, | 280 google.resources.push_back(ResourceRow( |
| 301 4, | 281 std::string(), "http://www.google.com/a.font", |
| 302 0, | 282 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM)); |
| 303 1, | 283 google.resources.push_back( |
| 304 2.1)); | 284 ResourceRow(std::string(), "http://www.resources.google.com/script.js", |
| 305 google.resources.push_back(ResourceRow(std::string(), | 285 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM)); |
| 306 "http://www.google.com/image.png", | |
| 307 content::RESOURCE_TYPE_IMAGE, | |
| 308 6, | |
| 309 3, | |
| 310 0, | |
| 311 2.2)); | |
| 312 google.resources.push_back(ResourceRow(std::string(), | |
| 313 "http://www.google.com/a.font", | |
| 314 content::RESOURCE_TYPE_LAST_TYPE, | |
| 315 2, | |
| 316 0, | |
| 317 0, | |
| 318 5.1)); | |
| 319 google.resources | |
| 320 .push_back(ResourceRow(std::string(), | |
| 321 "http://www.resources.google.com/script.js", | |
| 322 content::RESOURCE_TYPE_SCRIPT, | |
| 323 11, | |
| 324 0, | |
| 325 0, | |
| 326 8.5)); | |
| 327 | 286 |
| 328 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); | 287 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); |
| 329 reddit.last_visit = base::Time::FromInternalValue(2); | 288 reddit.last_visit = base::Time::FromInternalValue(2); |
| 330 reddit.resources | 289 reddit.resources.push_back( |
| 331 .push_back(ResourceRow(std::string(), | 290 ResourceRow(std::string(), "http://reddit-resource.com/script1.js", |
| 332 "http://reddit-resource.com/script1.js", | 291 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM)); |
| 333 content::RESOURCE_TYPE_SCRIPT, | 292 reddit.resources.push_back( |
| 334 4, | 293 ResourceRow(std::string(), "http://reddit-resource.com/script2.js", |
| 335 0, | 294 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM)); |
| 336 1, | |
| 337 1.0)); | |
| 338 reddit.resources | |
| 339 .push_back(ResourceRow(std::string(), | |
| 340 "http://reddit-resource.com/script2.js", | |
| 341 content::RESOURCE_TYPE_SCRIPT, | |
| 342 2, | |
| 343 0, | |
| 344 0, | |
| 345 2.1)); | |
| 346 | 295 |
| 347 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); | 296 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); |
| 348 yahoo.last_visit = base::Time::FromInternalValue(3); | 297 yahoo.last_visit = base::Time::FromInternalValue(3); |
| 349 yahoo.resources.push_back(ResourceRow(std::string(), | 298 yahoo.resources.push_back( |
| 350 "http://www.google.com/image.png", | 299 ResourceRow(std::string(), "http://www.google.com/image.png", |
| 351 content::RESOURCE_TYPE_IMAGE, | 300 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, net::MEDIUM)); |
| 352 20, | |
| 353 1, | |
| 354 0, | |
| 355 10.0)); | |
| 356 | 301 |
| 357 test_url_data_.clear(); | 302 test_url_data_.clear(); |
| 358 test_url_data_.insert(std::make_pair("http://www.google.com", google)); | 303 test_url_data_.insert(std::make_pair("http://www.google.com", google)); |
| 359 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); | 304 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); |
| 360 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); | 305 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); |
| 361 | 306 |
| 362 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); | 307 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); |
| 363 tables_->UpdateData(google, empty_host_data); | 308 tables_->UpdateData(google, empty_host_data); |
| 364 tables_->UpdateData(reddit, empty_host_data); | 309 tables_->UpdateData(reddit, empty_host_data); |
| 365 tables_->UpdateData(yahoo, empty_host_data); | 310 tables_->UpdateData(yahoo, empty_host_data); |
| 366 } | 311 } |
| 367 | 312 |
| 368 { // Host data. | 313 { // Host data. |
| 369 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); | 314 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); |
| 370 facebook.last_visit = base::Time::FromInternalValue(4); | 315 facebook.last_visit = base::Time::FromInternalValue(4); |
| 371 facebook.resources | 316 facebook.resources.push_back(ResourceRow( |
| 372 .push_back(ResourceRow(std::string(), | 317 std::string(), "http://www.facebook.com/style.css", |
| 373 "http://www.facebook.com/style.css", | 318 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, net::MEDIUM)); |
| 374 content::RESOURCE_TYPE_STYLESHEET, | 319 facebook.resources.push_back( |
| 375 5, | 320 ResourceRow(std::string(), "http://www.facebook.com/script.js", |
| 376 2, | 321 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM)); |
| 377 1, | 322 facebook.resources.push_back( |
| 378 1.1)); | 323 ResourceRow(std::string(), "http://www.facebook.com/image.png", |
| 379 facebook.resources | 324 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM)); |
| 380 .push_back(ResourceRow(std::string(), | 325 facebook.resources.push_back(ResourceRow( |
| 381 "http://www.facebook.com/script.js", | 326 std::string(), "http://www.facebook.com/a.font", |
| 382 content::RESOURCE_TYPE_SCRIPT, | 327 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM)); |
| 383 4, | 328 facebook.resources.push_back(ResourceRow( |
| 384 0, | 329 std::string(), "http://www.resources.facebook.com/script.js", |
| 385 1, | 330 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM)); |
| 386 2.1)); | |
| 387 facebook.resources | |
| 388 .push_back(ResourceRow(std::string(), | |
| 389 "http://www.facebook.com/image.png", | |
| 390 content::RESOURCE_TYPE_IMAGE, | |
| 391 6, | |
| 392 3, | |
| 393 0, | |
| 394 2.2)); | |
| 395 facebook.resources.push_back(ResourceRow(std::string(), | |
| 396 "http://www.facebook.com/a.font", | |
| 397 content::RESOURCE_TYPE_LAST_TYPE, | |
| 398 2, | |
| 399 0, | |
| 400 0, | |
| 401 5.1)); | |
| 402 facebook.resources | |
| 403 .push_back(ResourceRow(std::string(), | |
| 404 "http://www.resources.facebook.com/script.js", | |
| 405 content::RESOURCE_TYPE_SCRIPT, | |
| 406 11, | |
| 407 0, | |
| 408 0, | |
| 409 8.5)); | |
| 410 | 331 |
| 411 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 332 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
| 412 yahoo.last_visit = base::Time::FromInternalValue(5); | 333 yahoo.last_visit = base::Time::FromInternalValue(5); |
| 413 yahoo.resources.push_back(ResourceRow(std::string(), | 334 yahoo.resources.push_back( |
| 414 "http://www.google.com/image.png", | 335 ResourceRow(std::string(), "http://www.google.com/image.png", |
| 415 content::RESOURCE_TYPE_IMAGE, | 336 content::RESOURCE_TYPE_IMAGE, 20, 1, 0, 10.0, net::MEDIUM)); |
| 416 20, | |
| 417 1, | |
| 418 0, | |
| 419 10.0)); | |
| 420 | 337 |
| 421 test_host_data_.clear(); | 338 test_host_data_.clear(); |
| 422 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); | 339 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); |
| 423 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); | 340 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); |
| 424 | 341 |
| 425 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); | 342 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); |
| 426 tables_->UpdateData(empty_url_data, facebook); | 343 tables_->UpdateData(empty_url_data, facebook); |
| 427 tables_->UpdateData(empty_url_data, yahoo); | 344 tables_->UpdateData(empty_url_data, yahoo); |
| 428 } | 345 } |
| 429 } | 346 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 | 381 |
| 465 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 382 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
| 466 TestDeleteSingleDataPoint(); | 383 TestDeleteSingleDataPoint(); |
| 467 } | 384 } |
| 468 | 385 |
| 469 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 386 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
| 470 TestDeleteAllData(); | 387 TestDeleteAllData(); |
| 471 } | 388 } |
| 472 | 389 |
| 473 } // namespace predictors | 390 } // namespace predictors |
| OLD | NEW |