Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_unittest.cc

Issue 2762673002: predictors: Pass manifests from Bork to store in ResourcePrefetchPredictor. (Closed)
Patch Set: Add delegate tests for PrecacheFetcher. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/predictors/resource_prefetch_predictor.h" 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
6 6
7 #include <iostream> 7 #include <iostream>
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 void(PrefetchDataMap* url_data_map, 135 void(PrefetchDataMap* url_data_map,
136 PrefetchDataMap* host_data_map, 136 PrefetchDataMap* host_data_map,
137 RedirectDataMap* url_redirect_data_map, 137 RedirectDataMap* url_redirect_data_map,
138 RedirectDataMap* host_redirect_data_map, 138 RedirectDataMap* host_redirect_data_map,
139 ManifestDataMap* manifest_data_map)); 139 ManifestDataMap* manifest_data_map));
140 MOCK_METHOD4(UpdateData, 140 MOCK_METHOD4(UpdateData,
141 void(const PrefetchData& url_data, 141 void(const PrefetchData& url_data,
142 const PrefetchData& host_data, 142 const PrefetchData& host_data,
143 const RedirectData& url_redirect_data, 143 const RedirectData& url_redirect_data,
144 const RedirectData& host_redirect_data)); 144 const RedirectData& host_redirect_data));
145 MOCK_METHOD2(UpdateManifestData,
146 void(const std::string& host,
147 const precache::PrecacheManifest& manifest_data));
145 MOCK_METHOD2(DeleteResourceData, 148 MOCK_METHOD2(DeleteResourceData,
146 void(const std::vector<std::string>& urls, 149 void(const std::vector<std::string>& urls,
147 const std::vector<std::string>& hosts)); 150 const std::vector<std::string>& hosts));
148 MOCK_METHOD2(DeleteSingleResourceDataPoint, 151 MOCK_METHOD2(DeleteSingleResourceDataPoint,
149 void(const std::string& key, PrefetchKeyType key_type)); 152 void(const std::string& key, PrefetchKeyType key_type));
150 MOCK_METHOD2(DeleteRedirectData, 153 MOCK_METHOD2(DeleteRedirectData,
151 void(const std::vector<std::string>& urls, 154 void(const std::vector<std::string>& urls,
152 const std::vector<std::string>& hosts)); 155 const std::vector<std::string>& hosts));
153 MOCK_METHOD2(DeleteSingleRedirectDataPoint, 156 MOCK_METHOD2(DeleteSingleRedirectDataPoint,
154 void(const std::string& key, PrefetchKeyType key_type)); 157 void(const std::string& key, PrefetchKeyType key_type));
158 MOCK_METHOD1(DeleteManifestData, void(const std::vector<std::string>& hosts));
155 MOCK_METHOD0(DeleteAllData, void()); 159 MOCK_METHOD0(DeleteAllData, void());
156 160
157 protected: 161 protected:
158 ~MockResourcePrefetchPredictorTables() { } 162 ~MockResourcePrefetchPredictorTables() { }
159 }; 163 };
160 164
161 class MockResourcePrefetchPredictorObserver : public TestObserver { 165 class MockResourcePrefetchPredictorObserver : public TestObserver {
162 public: 166 public:
163 explicit MockResourcePrefetchPredictorObserver( 167 explicit MockResourcePrefetchPredictorObserver(
164 ResourcePrefetchPredictor* predictor) 168 ResourcePrefetchPredictor* predictor)
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 421
418 RedirectData microsoft = CreateRedirectData("microsoft.com", 10); 422 RedirectData microsoft = CreateRedirectData("microsoft.com", 10);
419 InitializeRedirectStat(microsoft.add_redirect_endpoints(), 423 InitializeRedirectStat(microsoft.add_redirect_endpoints(),
420 "www.microsoft.com", 10, 0, 0); 424 "www.microsoft.com", 10, 0, 0);
421 425
422 test_host_redirect_data_.clear(); 426 test_host_redirect_data_.clear();
423 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); 427 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc));
424 test_host_redirect_data_.insert( 428 test_host_redirect_data_.insert(
425 std::make_pair(microsoft.primary_key(), microsoft)); 429 std::make_pair(microsoft.primary_key(), microsoft));
426 } 430 }
431
432 { // Manifest data.
433 precache::PrecacheManifest google = CreateManifestData(11);
434 InitializePrecacheResource(google.add_resource(),
435 "http://google.com/script.js", 0.5);
436 InitializePrecacheResource(google.add_resource(),
437 "http://static.google.com/style.css", 0.333);
438
439 precache::PrecacheManifest facebook = CreateManifestData(12);
440 InitializePrecacheResource(facebook.add_resource(),
441 "http://fb.com/static.css", 0.99);
442
443 test_manifest_data_.insert(std::make_pair("google.com", google));
444 test_manifest_data_.insert(std::make_pair("facebook.com", facebook));
445 }
427 } 446 }
428 447
429 void ResourcePrefetchPredictorTest::TestRedirectStatusHistogram( 448 void ResourcePrefetchPredictorTest::TestRedirectStatusHistogram(
430 const std::string& predictor_initial_key, 449 const std::string& predictor_initial_key,
431 const std::string& predictor_key, 450 const std::string& predictor_key,
432 const std::string& navigation_initial_url, 451 const std::string& navigation_initial_url,
433 const std::string& navigation_url, 452 const std::string& navigation_url,
434 ResourcePrefetchPredictor::RedirectStatus expected_status) { 453 ResourcePrefetchPredictor::RedirectStatus expected_status) {
435 // Database initialization. 454 // Database initialization.
436 const std::string& script_url = "https://cdn.google.com/script.js"; 455 const std::string& script_url = "https://cdn.google.com/script.js";
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 static_assert(static_cast<int>(net::MAXIMUM_PRIORITY) == 506 static_assert(static_cast<int>(net::MAXIMUM_PRIORITY) ==
488 static_cast<int>(ResourceData_Priority_REQUEST_PRIORITY_HIGHEST), 507 static_cast<int>(ResourceData_Priority_REQUEST_PRIORITY_HIGHEST),
489 "Database/Net priority mismatch: Maximum"); 508 "Database/Net priority mismatch: Maximum");
490 509
491 // Tests that the predictor initializes correctly without any data. 510 // Tests that the predictor initializes correctly without any data.
492 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) { 511 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeEmpty) {
493 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 512 EXPECT_TRUE(predictor_->url_table_cache_->empty());
494 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 513 EXPECT_TRUE(predictor_->host_table_cache_->empty());
495 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty()); 514 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty());
496 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty()); 515 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty());
516 EXPECT_TRUE(predictor_->manifest_table_cache_->empty());
497 } 517 }
498 518
499 // Tests that the history and the db tables data are loaded correctly. 519 // Tests that the history and the db tables data are loaded correctly.
500 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) { 520 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) {
501 AddUrlToHistory("http://www.google.com/", 4); 521 AddUrlToHistory("http://www.google.com/", 4);
502 AddUrlToHistory("http://www.yahoo.com/", 2); 522 AddUrlToHistory("http://www.yahoo.com/", 2);
503 523
504 EXPECT_CALL(*mock_tables_.get(), 524 EXPECT_CALL(*mock_tables_.get(),
505 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), 525 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
506 Pointee(ContainerEq(PrefetchDataMap())), 526 Pointee(ContainerEq(PrefetchDataMap())),
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(), 976 InitializeRedirectStat(host_redirect_data.add_redirect_endpoints(),
957 "facebook.com", 1, 0, 0); 977 "facebook.com", 1, 0, 0);
958 EXPECT_CALL(*mock_tables_.get(), 978 EXPECT_CALL(*mock_tables_.get(),
959 UpdateData(empty_resource_data_, empty_resource_data_, 979 UpdateData(empty_resource_data_, empty_resource_data_,
960 empty_redirect_data_, host_redirect_data)); 980 empty_redirect_data_, host_redirect_data));
961 981
962 predictor_->RecordMainFrameLoadComplete(fb_end); 982 predictor_->RecordMainFrameLoadComplete(fb_end);
963 profile_->BlockUntilHistoryProcessesPendingRequests(); 983 profile_->BlockUntilHistoryProcessesPendingRequests();
964 } 984 }
965 985
986 TEST_F(ResourcePrefetchPredictorTest, ManifestHostNotInDB) {
987 precache::PrecacheManifest manifest = CreateManifestData(1);
988 InitializePrecacheResource(manifest.add_resource(),
989 "http://cdn.google.com/script.js", 0.9);
990 InitializePrecacheResource(manifest.add_resource(),
991 "http://cdn.google.com/style.css", 0.75);
992
993 EXPECT_CALL(*mock_tables_.get(), UpdateManifestData("google.com", manifest));
994
995 predictor_->OnManifestFetched("google.com", manifest);
996 }
997
998 TEST_F(ResourcePrefetchPredictorTest, ManifestHostInDB) {
999 EXPECT_CALL(*mock_tables_.get(),
1000 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
1001 Pointee(ContainerEq(PrefetchDataMap())),
1002 Pointee(ContainerEq(RedirectDataMap())),
1003 Pointee(ContainerEq(RedirectDataMap())),
1004 Pointee(ContainerEq(ManifestDataMap()))))
1005 .WillOnce(SetArgPointee<4>(test_manifest_data_));
1006 ResetPredictor();
1007 InitializePredictor();
1008 EXPECT_EQ(2U, predictor_->manifest_table_cache_->size());
1009
1010 precache::PrecacheManifest manifest = CreateManifestData(1);
1011 InitializePrecacheResource(manifest.add_resource(),
1012 "http://google.com/image.jpg", 0.1);
1013
1014 EXPECT_CALL(*mock_tables_.get(), UpdateManifestData("google.com", manifest));
1015
1016 predictor_->OnManifestFetched("google.com", manifest);
1017 }
1018
1019 TEST_F(ResourcePrefetchPredictorTest, ManifestHostNotInDBAndDBFull) {
1020 EXPECT_CALL(*mock_tables_.get(),
1021 GetAllData(Pointee(ContainerEq(PrefetchDataMap())),
1022 Pointee(ContainerEq(PrefetchDataMap())),
1023 Pointee(ContainerEq(RedirectDataMap())),
1024 Pointee(ContainerEq(RedirectDataMap())),
1025 Pointee(ContainerEq(ManifestDataMap()))))
1026 .WillOnce(SetArgPointee<4>(test_manifest_data_));
1027 ResetPredictor();
1028 InitializePredictor();
1029 EXPECT_EQ(2U, predictor_->manifest_table_cache_->size());
1030
1031 precache::PrecacheManifest manifest = CreateManifestData(1);
1032 InitializePrecacheResource(manifest.add_resource(),
1033 "http://en.wikipedia.org/logo.png", 1.0);
1034
1035 EXPECT_CALL(*mock_tables_.get(),
1036 DeleteManifestData(std::vector<std::string>({"google.com"})));
1037
1038 EXPECT_CALL(*mock_tables_.get(),
1039 UpdateManifestData("en.wikipedia.org", manifest));
1040
1041 predictor_->OnManifestFetched("en.wikipedia.org", manifest);
1042 }
1043
966 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) { 1044 TEST_F(ResourcePrefetchPredictorTest, DeleteUrls) {
967 // Add some dummy entries to cache. 1045 // Add some dummy entries to cache.
968 predictor_->url_table_cache_->insert( 1046 predictor_->url_table_cache_->insert(
969 std::make_pair("http://www.google.com/page1.html", 1047 std::make_pair("http://www.google.com/page1.html",
970 CreatePrefetchData("http://www.google.com/page1.html"))); 1048 CreatePrefetchData("http://www.google.com/page1.html")));
971 predictor_->url_table_cache_->insert( 1049 predictor_->url_table_cache_->insert(
972 std::make_pair("http://www.google.com/page2.html", 1050 std::make_pair("http://www.google.com/page2.html",
973 CreatePrefetchData("http://www.google.com/page2.html"))); 1051 CreatePrefetchData("http://www.google.com/page2.html")));
974 predictor_->url_table_cache_->insert(std::make_pair( 1052 predictor_->url_table_cache_->insert(std::make_pair(
975 "http://www.yahoo.com/", CreatePrefetchData("http://www.yahoo.com/"))); 1053 "http://www.yahoo.com/", CreatePrefetchData("http://www.yahoo.com/")));
(...skipping 20 matching lines...) Expand all
996 predictor_->url_redirect_table_cache_->insert( 1074 predictor_->url_redirect_table_cache_->insert(
997 std::make_pair("http://nyt.com/", CreateRedirectData("http://nyt.com/"))); 1075 std::make_pair("http://nyt.com/", CreateRedirectData("http://nyt.com/")));
998 1076
999 predictor_->host_redirect_table_cache_->insert( 1077 predictor_->host_redirect_table_cache_->insert(
1000 std::make_pair("www.google.com", CreateRedirectData("www.google.com"))); 1078 std::make_pair("www.google.com", CreateRedirectData("www.google.com")));
1001 predictor_->host_redirect_table_cache_->insert( 1079 predictor_->host_redirect_table_cache_->insert(
1002 std::make_pair("www.nike.com", CreateRedirectData("www.nike.com"))); 1080 std::make_pair("www.nike.com", CreateRedirectData("www.nike.com")));
1003 predictor_->host_redirect_table_cache_->insert(std::make_pair( 1081 predictor_->host_redirect_table_cache_->insert(std::make_pair(
1004 "www.wikipedia.org", CreateRedirectData("www.wikipedia.org"))); 1082 "www.wikipedia.org", CreateRedirectData("www.wikipedia.org")));
1005 1083
1084 predictor_->manifest_table_cache_->insert(
1085 std::make_pair("google.com", CreateManifestData()));
1086 predictor_->manifest_table_cache_->insert(
1087 std::make_pair("apple.com", CreateManifestData()));
1088 predictor_->manifest_table_cache_->insert(
1089 std::make_pair("en.wikipedia.org", CreateManifestData()));
1090
1006 history::URLRows rows; 1091 history::URLRows rows;
1007 rows.push_back(history::URLRow(GURL("http://www.google.com/page2.html"))); 1092 rows.push_back(history::URLRow(GURL("http://www.google.com/page2.html")));
1008 rows.push_back(history::URLRow(GURL("http://www.apple.com"))); 1093 rows.push_back(history::URLRow(GURL("http://www.apple.com")));
1009 rows.push_back(history::URLRow(GURL("http://www.nike.com"))); 1094 rows.push_back(history::URLRow(GURL("http://www.nike.com")));
1010 1095
1011 std::vector<std::string> urls_to_delete, hosts_to_delete, 1096 std::vector<std::string> urls_to_delete, hosts_to_delete,
1012 url_redirects_to_delete, host_redirects_to_delete; 1097 url_redirects_to_delete, host_redirects_to_delete, manifests_to_delete;
1013 urls_to_delete.push_back("http://www.google.com/page2.html"); 1098 urls_to_delete.push_back("http://www.google.com/page2.html");
1014 urls_to_delete.push_back("http://www.apple.com/"); 1099 urls_to_delete.push_back("http://www.apple.com/");
1015 urls_to_delete.push_back("http://www.nike.com/"); 1100 urls_to_delete.push_back("http://www.nike.com/");
1016 hosts_to_delete.push_back("www.google.com"); 1101 hosts_to_delete.push_back("www.google.com");
1017 hosts_to_delete.push_back("www.apple.com"); 1102 hosts_to_delete.push_back("www.apple.com");
1018 url_redirects_to_delete.push_back("http://www.google.com/page2.html"); 1103 url_redirects_to_delete.push_back("http://www.google.com/page2.html");
1019 url_redirects_to_delete.push_back("http://www.apple.com/"); 1104 url_redirects_to_delete.push_back("http://www.apple.com/");
1020 host_redirects_to_delete.push_back("www.google.com"); 1105 host_redirects_to_delete.push_back("www.google.com");
1021 host_redirects_to_delete.push_back("www.nike.com"); 1106 host_redirects_to_delete.push_back("www.nike.com");
1107 manifests_to_delete.push_back("google.com");
1108 manifests_to_delete.push_back("apple.com");
1022 1109
1023 EXPECT_CALL(*mock_tables_.get(), 1110 EXPECT_CALL(*mock_tables_.get(),
1024 DeleteResourceData(ContainerEq(urls_to_delete), 1111 DeleteResourceData(ContainerEq(urls_to_delete),
1025 ContainerEq(hosts_to_delete))); 1112 ContainerEq(hosts_to_delete)));
1026 EXPECT_CALL(*mock_tables_.get(), 1113 EXPECT_CALL(*mock_tables_.get(),
1027 DeleteRedirectData(ContainerEq(url_redirects_to_delete), 1114 DeleteRedirectData(ContainerEq(url_redirects_to_delete),
1028 ContainerEq(host_redirects_to_delete))); 1115 ContainerEq(host_redirects_to_delete)));
1116 EXPECT_CALL(*mock_tables_.get(),
1117 DeleteManifestData(ContainerEq(manifests_to_delete)));
1029 1118
1030 predictor_->DeleteUrls(rows); 1119 predictor_->DeleteUrls(rows);
1031 EXPECT_EQ(2U, predictor_->url_table_cache_->size()); 1120 EXPECT_EQ(2U, predictor_->url_table_cache_->size());
1032 EXPECT_EQ(1U, predictor_->host_table_cache_->size()); 1121 EXPECT_EQ(1U, predictor_->host_table_cache_->size());
1033 EXPECT_EQ(2U, predictor_->url_redirect_table_cache_->size()); 1122 EXPECT_EQ(2U, predictor_->url_redirect_table_cache_->size());
1034 EXPECT_EQ(1U, predictor_->host_redirect_table_cache_->size()); 1123 EXPECT_EQ(1U, predictor_->host_redirect_table_cache_->size());
1124 EXPECT_EQ(1U, predictor_->manifest_table_cache_->size());
1035 1125
1036 EXPECT_CALL(*mock_tables_.get(), DeleteAllData()); 1126 EXPECT_CALL(*mock_tables_.get(), DeleteAllData());
1037 1127
1038 predictor_->DeleteAllUrls(); 1128 predictor_->DeleteAllUrls();
1039 EXPECT_TRUE(predictor_->url_table_cache_->empty()); 1129 EXPECT_TRUE(predictor_->url_table_cache_->empty());
1040 EXPECT_TRUE(predictor_->host_table_cache_->empty()); 1130 EXPECT_TRUE(predictor_->host_table_cache_->empty());
1041 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty()); 1131 EXPECT_TRUE(predictor_->url_redirect_table_cache_->empty());
1042 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty()); 1132 EXPECT_TRUE(predictor_->host_redirect_table_cache_->empty());
1133 EXPECT_TRUE(predictor_->manifest_table_cache_->empty());
1043 } 1134 }
1044 1135
1045 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) { 1136 TEST_F(ResourcePrefetchPredictorTest, OnMainFrameRequest) {
1046 URLRequestSummary summary1 = CreateURLRequestSummary( 1137 URLRequestSummary summary1 = CreateURLRequestSummary(
1047 1, "http://www.google.com", "http://www.google.com", 1138 1, "http://www.google.com", "http://www.google.com",
1048 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1139 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1049 URLRequestSummary summary2 = CreateURLRequestSummary( 1140 URLRequestSummary summary2 = CreateURLRequestSummary(
1050 2, "http://www.google.com", "http://www.google.com", 1141 2, "http://www.google.com", "http://www.google.com",
1051 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); 1142 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false);
1052 URLRequestSummary summary3 = CreateURLRequestSummary( 1143 URLRequestSummary summary3 = CreateURLRequestSummary(
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
1743 predictor_->host_table_cache_->insert( 1834 predictor_->host_table_cache_->insert(
1744 std::make_pair(google.primary_key(), google)); 1835 std::make_pair(google.primary_key(), google));
1745 1836
1746 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); 1837 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL);
1747 predictor_->StopPrefetching(GURL(main_frame_url)); 1838 predictor_->StopPrefetching(GURL(main_frame_url));
1748 histogram_tester_->ExpectTotalCount( 1839 histogram_tester_->ExpectTotalCount(
1749 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); 1840 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1);
1750 } 1841 }
1751 1842
1752 } // namespace predictors 1843 } // namespace predictors
OLDNEW
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_predictor_test_util.cc ('k') | components/precache/content/precache_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698