| 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" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 void TestDeleteSingleDataPoint(); | 35 void TestDeleteSingleDataPoint(); |
| 36 void TestDeleteAllData(); | 36 void TestDeleteAllData(); |
| 37 | 37 |
| 38 base::MessageLoop loop_; | 38 base::MessageLoop loop_; |
| 39 content::TestBrowserThread db_thread_; | 39 content::TestBrowserThread db_thread_; |
| 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 | 46 |
| 46 private: | 47 private: |
| 47 using PrefetchData = ResourcePrefetchPredictorTables::PrefetchData; | 48 using PrefetchData = ResourcePrefetchPredictorTables::PrefetchData; |
| 48 | 49 |
| 49 // Initializes the tables, |test_url_data_| and |test_host_data_|. | 50 // Initializes the tables, |test_url_data_| and |test_host_data_|. |
| 50 void InitializeSampleData(); | 51 void InitializeSampleData(); |
| 51 | 52 |
| 52 // Checks that the input PrefetchData are the same, although the resources | 53 // Checks that the input PrefetchData are the same, although the resources |
| 53 // can be in different order. | 54 // can be in different order. |
| 54 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, | 55 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, |
| 55 const PrefetchDataMap& rhs) const; | 56 const PrefetchDataMap& rhs) const; |
| 56 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, | 57 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, |
| 57 const std::vector<ResourceData>& rhs) const; | 58 const std::vector<ResourceData>& rhs) const; |
| 58 | 59 |
| 60 // Checks that the input RedirectData are the same, although the redirects |
| 61 // can be in different order. |
| 62 void TestRedirectDataAreEqual(const RedirectDataMap& lhs, |
| 63 const RedirectDataMap& rhs) const; |
| 64 void TestRedirectsAreEqual(const std::vector<RedirectStat>& lhs, |
| 65 const std::vector<RedirectStat>& rhs) const; |
| 66 |
| 59 void AddKey(PrefetchDataMap* m, const std::string& key) const; | 67 void AddKey(PrefetchDataMap* m, const std::string& key) const; |
| 68 void AddKey(RedirectDataMap* m, const std::string& key) const; |
| 60 | 69 |
| 61 PrefetchDataMap test_url_data_; | 70 PrefetchDataMap test_url_data_; |
| 62 PrefetchDataMap test_host_data_; | 71 PrefetchDataMap test_host_data_; |
| 72 RedirectDataMap test_url_redirect_data_; |
| 73 RedirectDataMap test_host_redirect_data_; |
| 63 }; | 74 }; |
| 64 | 75 |
| 65 class ResourcePrefetchPredictorTablesReopenTest | 76 class ResourcePrefetchPredictorTablesReopenTest |
| 66 : public ResourcePrefetchPredictorTablesTest { | 77 : public ResourcePrefetchPredictorTablesTest { |
| 67 public: | 78 public: |
| 68 void SetUp() override { | 79 void SetUp() override { |
| 69 // Write data to the table, and then reopen the db. | 80 // Write data to the table, and then reopen the db. |
| 70 ResourcePrefetchPredictorTablesTest::SetUp(); | 81 ResourcePrefetchPredictorTablesTest::SetUp(); |
| 71 ResourcePrefetchPredictorTablesTest::TearDown(); | 82 ResourcePrefetchPredictorTablesTest::TearDown(); |
| 72 | 83 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 91 } | 102 } |
| 92 | 103 |
| 93 void ResourcePrefetchPredictorTablesTest::TearDown() { | 104 void ResourcePrefetchPredictorTablesTest::TearDown() { |
| 94 tables_ = NULL; | 105 tables_ = NULL; |
| 95 db_.reset(); | 106 db_.reset(); |
| 96 base::RunLoop().RunUntilIdle(); | 107 base::RunLoop().RunUntilIdle(); |
| 97 } | 108 } |
| 98 | 109 |
| 99 void ResourcePrefetchPredictorTablesTest::TestGetAllData() { | 110 void ResourcePrefetchPredictorTablesTest::TestGetAllData() { |
| 100 PrefetchDataMap actual_url_data, actual_host_data; | 111 PrefetchDataMap actual_url_data, actual_host_data; |
| 101 tables_->GetAllData(&actual_url_data, &actual_host_data); | 112 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 113 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 114 &actual_url_redirect_data, &actual_host_redirect_data); |
| 102 | 115 |
| 103 TestPrefetchDataAreEqual(test_url_data_, actual_url_data); | 116 TestPrefetchDataAreEqual(test_url_data_, actual_url_data); |
| 104 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); | 117 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); |
| 118 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); |
| 119 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 105 } | 120 } |
| 106 | 121 |
| 107 void ResourcePrefetchPredictorTablesTest::TestDeleteData() { | 122 void ResourcePrefetchPredictorTablesTest::TestDeleteData() { |
| 108 std::vector<std::string> urls_to_delete, hosts_to_delete; | 123 std::vector<std::string> urls_to_delete = {"http://www.google.com", |
| 109 urls_to_delete.push_back("http://www.google.com"); | 124 "http://www.yahoo.com"}; |
| 110 urls_to_delete.push_back("http://www.yahoo.com"); | 125 std::vector<std::string> hosts_to_delete = {"www.yahoo.com"}; |
| 111 hosts_to_delete.push_back("www.yahoo.com"); | |
| 112 | 126 |
| 113 tables_->DeleteData(urls_to_delete, hosts_to_delete); | 127 tables_->DeleteResourceData(urls_to_delete, hosts_to_delete); |
| 128 |
| 129 urls_to_delete = {"http://fb.com/google", "http://google.com"}; |
| 130 hosts_to_delete = {"microsoft.com"}; |
| 131 |
| 132 tables_->DeleteRedirectData(urls_to_delete, hosts_to_delete); |
| 114 | 133 |
| 115 PrefetchDataMap actual_url_data, actual_host_data; | 134 PrefetchDataMap actual_url_data, actual_host_data; |
| 116 tables_->GetAllData(&actual_url_data, &actual_host_data); | 135 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 136 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 137 &actual_url_redirect_data, &actual_host_redirect_data); |
| 117 | 138 |
| 118 PrefetchDataMap expected_url_data, expected_host_data; | 139 PrefetchDataMap expected_url_data, expected_host_data; |
| 140 RedirectDataMap expected_url_redirect_data, expected_host_redirect_data; |
| 119 AddKey(&expected_url_data, "http://www.reddit.com"); | 141 AddKey(&expected_url_data, "http://www.reddit.com"); |
| 120 AddKey(&expected_host_data, "www.facebook.com"); | 142 AddKey(&expected_host_data, "www.facebook.com"); |
| 143 AddKey(&expected_url_redirect_data, "http://nyt.com"); |
| 144 AddKey(&expected_host_redirect_data, "bbc.com"); |
| 121 | 145 |
| 122 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 146 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 123 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 147 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 148 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 149 actual_url_redirect_data); |
| 150 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 151 actual_host_redirect_data); |
| 124 } | 152 } |
| 125 | 153 |
| 126 void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() { | 154 void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() { |
| 127 // Delete a URL. | 155 // Delete a URL. |
| 128 tables_->DeleteSingleDataPoint("http://www.reddit.com", | 156 tables_->DeleteSingleResourceDataPoint("http://www.reddit.com", |
| 129 PREFETCH_KEY_TYPE_URL); | 157 PREFETCH_KEY_TYPE_URL); |
| 130 | 158 |
| 131 PrefetchDataMap actual_url_data, actual_host_data; | 159 PrefetchDataMap actual_url_data, actual_host_data; |
| 132 tables_->GetAllData(&actual_url_data, &actual_host_data); | 160 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 161 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 162 &actual_url_redirect_data, &actual_host_redirect_data); |
| 133 | 163 |
| 134 PrefetchDataMap expected_url_data; | 164 PrefetchDataMap expected_url_data; |
| 135 AddKey(&expected_url_data, "http://www.google.com"); | 165 AddKey(&expected_url_data, "http://www.google.com"); |
| 136 AddKey(&expected_url_data, "http://www.yahoo.com"); | 166 AddKey(&expected_url_data, "http://www.yahoo.com"); |
| 137 | 167 |
| 138 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 168 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 139 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); | 169 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); |
| 170 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); |
| 171 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 140 | 172 |
| 141 // Delete a host. | 173 // Delete a host. |
| 142 tables_->DeleteSingleDataPoint("www.facebook.com", PREFETCH_KEY_TYPE_HOST); | 174 tables_->DeleteSingleResourceDataPoint("www.facebook.com", |
| 175 PREFETCH_KEY_TYPE_HOST); |
| 143 actual_url_data.clear(); | 176 actual_url_data.clear(); |
| 144 actual_host_data.clear(); | 177 actual_host_data.clear(); |
| 145 tables_->GetAllData(&actual_url_data, &actual_host_data); | 178 actual_url_redirect_data.clear(); |
| 179 actual_host_redirect_data.clear(); |
| 180 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 181 &actual_url_redirect_data, &actual_host_redirect_data); |
| 146 | 182 |
| 147 PrefetchDataMap expected_host_data; | 183 PrefetchDataMap expected_host_data; |
| 148 AddKey(&expected_host_data, "www.yahoo.com"); | 184 AddKey(&expected_host_data, "www.yahoo.com"); |
| 149 | 185 |
| 150 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 186 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 151 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 187 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 188 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); |
| 189 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 190 |
| 191 // Delete a URL redirect. |
| 192 tables_->DeleteSingleRedirectDataPoint("http://nyt.com", |
| 193 PREFETCH_KEY_TYPE_URL); |
| 194 actual_url_data.clear(); |
| 195 actual_host_data.clear(); |
| 196 actual_url_redirect_data.clear(); |
| 197 actual_host_redirect_data.clear(); |
| 198 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 199 &actual_url_redirect_data, &actual_host_redirect_data); |
| 200 |
| 201 RedirectDataMap expected_url_redirect_data; |
| 202 AddKey(&expected_url_redirect_data, "http://fb.com/google"); |
| 203 AddKey(&expected_url_redirect_data, "http://google.com"); |
| 204 |
| 205 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 206 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 207 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 208 actual_url_redirect_data); |
| 209 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 210 |
| 211 // Delete a host redirect. |
| 212 tables_->DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST); |
| 213 actual_url_data.clear(); |
| 214 actual_host_data.clear(); |
| 215 actual_url_redirect_data.clear(); |
| 216 actual_host_redirect_data.clear(); |
| 217 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 218 &actual_url_redirect_data, &actual_host_redirect_data); |
| 219 |
| 220 RedirectDataMap expected_host_redirect_data; |
| 221 AddKey(&expected_host_redirect_data, "microsoft.com"); |
| 222 |
| 223 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 224 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 225 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 226 actual_url_redirect_data); |
| 227 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 228 actual_host_redirect_data); |
| 152 } | 229 } |
| 153 | 230 |
| 154 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { | 231 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { |
| 155 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 232 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
| 156 google.last_visit = base::Time::FromInternalValue(10); | 233 google.last_visit = base::Time::FromInternalValue(10); |
| 157 google.resources.push_back(CreateResourceData( | 234 google.resources.push_back(CreateResourceData( |
| 158 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 6, | 235 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 6, |
| 159 2, 0, 1.0, net::MEDIUM, true, false)); | 236 2, 0, 1.0, net::MEDIUM, true, false)); |
| 160 google.resources.push_back(CreateResourceData( | 237 google.resources.push_back(CreateResourceData( |
| 161 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 4, 1, | 238 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 4, 1, |
| 162 4.2, net::MEDIUM, false, false)); | 239 4.2, net::MEDIUM, false, false)); |
| 163 google.resources.push_back(CreateResourceData( | 240 google.resources.push_back(CreateResourceData( |
| 164 "http://www.google.com/a.xml", content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, | 241 "http://www.google.com/a.xml", content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, |
| 165 6.1, net::MEDIUM, false, false)); | 242 6.1, net::MEDIUM, false, false)); |
| 166 google.resources.push_back(CreateResourceData( | 243 google.resources.push_back(CreateResourceData( |
| 167 "http://www.resources.google.com/script.js", | 244 "http://www.resources.google.com/script.js", |
| 168 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); | 245 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); |
| 169 | 246 |
| 170 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 247 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
| 171 yahoo.last_visit = base::Time::FromInternalValue(7); | 248 yahoo.last_visit = base::Time::FromInternalValue(7); |
| 172 yahoo.resources.push_back(CreateResourceData( | 249 yahoo.resources.push_back(CreateResourceData( |
| 173 "http://www.yahoo.com/image.png", content::RESOURCE_TYPE_IMAGE, 120, 1, 1, | 250 "http://www.yahoo.com/image.png", content::RESOURCE_TYPE_IMAGE, 120, 1, 1, |
| 174 10.0, net::MEDIUM, true, false)); | 251 10.0, net::MEDIUM, true, false)); |
| 175 | 252 |
| 176 tables_->UpdateData(google, yahoo); | 253 RedirectData facebook; |
| 254 facebook.set_primary_key("http://fb.com/google"); |
| 255 facebook.set_last_visit_time(20); |
| 256 InitializeRedirectStat(facebook.add_redirects(), "https://facebook.fr/google", |
| 257 4, 2, 1); |
| 258 |
| 259 RedirectData microsoft; |
| 260 microsoft.set_primary_key("microsoft.com"); |
| 261 microsoft.set_last_visit_time(21); |
| 262 InitializeRedirectStat(microsoft.add_redirects(), "m.microsoft.com", 5, 7, 1); |
| 263 InitializeRedirectStat(microsoft.add_redirects(), "microsoft.org", 7, 2, 0); |
| 264 |
| 265 tables_->UpdateData(google, yahoo, facebook, microsoft); |
| 177 | 266 |
| 178 PrefetchDataMap actual_url_data, actual_host_data; | 267 PrefetchDataMap actual_url_data, actual_host_data; |
| 179 tables_->GetAllData(&actual_url_data, &actual_host_data); | 268 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 269 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 270 &actual_url_redirect_data, &actual_host_redirect_data); |
| 180 | 271 |
| 181 PrefetchDataMap expected_url_data, expected_host_data; | 272 PrefetchDataMap expected_url_data, expected_host_data; |
| 273 RedirectDataMap expected_url_redirect_data, expected_host_redirect_data; |
| 182 AddKey(&expected_url_data, "http://www.reddit.com"); | 274 AddKey(&expected_url_data, "http://www.reddit.com"); |
| 183 AddKey(&expected_url_data, "http://www.yahoo.com"); | 275 AddKey(&expected_url_data, "http://www.yahoo.com"); |
| 184 expected_url_data.insert(std::make_pair("http://www.google.com", google)); | 276 expected_url_data.insert(std::make_pair("http://www.google.com", google)); |
| 185 | 277 |
| 186 AddKey(&expected_host_data, "www.facebook.com"); | 278 AddKey(&expected_host_data, "www.facebook.com"); |
| 187 expected_host_data.insert(std::make_pair("www.yahoo.com", yahoo)); | 279 expected_host_data.insert(std::make_pair("www.yahoo.com", yahoo)); |
| 188 | 280 |
| 281 AddKey(&expected_url_redirect_data, "http://nyt.com"); |
| 282 AddKey(&expected_url_redirect_data, "http://google.com"); |
| 283 expected_url_redirect_data.insert( |
| 284 std::make_pair("http://fb.com/google", facebook)); |
| 285 |
| 286 AddKey(&expected_host_redirect_data, "bbc.com"); |
| 287 expected_host_redirect_data.insert( |
| 288 std::make_pair("microsoft.com", microsoft)); |
| 289 |
| 189 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 290 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 190 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 291 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 292 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 293 actual_url_redirect_data); |
| 294 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 295 actual_host_redirect_data); |
| 191 } | 296 } |
| 192 | 297 |
| 193 void ResourcePrefetchPredictorTablesTest::TestDeleteAllData() { | 298 void ResourcePrefetchPredictorTablesTest::TestDeleteAllData() { |
| 194 tables_->DeleteAllData(); | 299 tables_->DeleteAllData(); |
| 195 | 300 |
| 196 PrefetchDataMap actual_url_data, actual_host_data; | 301 PrefetchDataMap actual_url_data, actual_host_data; |
| 197 tables_->GetAllData(&actual_url_data, &actual_host_data); | 302 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 303 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 304 &actual_url_redirect_data, &actual_host_redirect_data); |
| 198 EXPECT_TRUE(actual_url_data.empty()); | 305 EXPECT_TRUE(actual_url_data.empty()); |
| 199 EXPECT_TRUE(actual_host_data.empty()); | 306 EXPECT_TRUE(actual_host_data.empty()); |
| 307 EXPECT_TRUE(actual_url_redirect_data.empty()); |
| 308 EXPECT_TRUE(actual_host_redirect_data.empty()); |
| 200 } | 309 } |
| 201 | 310 |
| 202 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( | 311 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( |
| 203 const PrefetchDataMap& lhs, | 312 const PrefetchDataMap& lhs, |
| 204 const PrefetchDataMap& rhs) const { | 313 const PrefetchDataMap& rhs) const { |
| 205 EXPECT_EQ(lhs.size(), rhs.size()); | 314 EXPECT_EQ(lhs.size(), rhs.size()); |
| 206 | 315 |
| 207 for (const std::pair<std::string, PrefetchData>& p : rhs) { | 316 for (const std::pair<std::string, PrefetchData>& p : rhs) { |
| 208 PrefetchDataMap::const_iterator lhs_it = lhs.find(p.first); | 317 const auto lhs_it = lhs.find(p.first); |
| 209 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; | 318 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
| 319 EXPECT_TRUE(lhs_it->second.key_type == p.second.key_type); |
| 320 EXPECT_TRUE(lhs_it->second.last_visit == p.second.last_visit); |
| 210 | 321 |
| 211 TestResourcesAreEqual(lhs_it->second.resources, p.second.resources); | 322 TestResourcesAreEqual(lhs_it->second.resources, p.second.resources); |
| 212 } | 323 } |
| 213 } | 324 } |
| 214 | 325 |
| 215 void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual( | 326 void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual( |
| 216 const std::vector<ResourceData>& lhs, | 327 const std::vector<ResourceData>& lhs, |
| 217 const std::vector<ResourceData>& rhs) const { | 328 const std::vector<ResourceData>& rhs) const { |
| 218 EXPECT_EQ(lhs.size(), rhs.size()); | 329 EXPECT_EQ(lhs.size(), rhs.size()); |
| 219 | 330 |
| 220 std::set<std::string> resources_seen; | 331 std::set<std::string> resources_seen; |
| 221 for (const auto& rhs_resource : rhs) { | 332 for (const auto& rhs_resource : rhs) { |
| 222 const std::string& resource = rhs_resource.resource_url(); | 333 const std::string& resource = rhs_resource.resource_url(); |
| 223 EXPECT_FALSE(base::ContainsKey(resources_seen, resource)); | 334 EXPECT_FALSE(base::ContainsKey(resources_seen, resource)); |
| 224 | 335 |
| 225 for (const auto& lhs_resource : lhs) { | 336 for (const auto& lhs_resource : lhs) { |
| 226 if (lhs_resource == rhs_resource) { | 337 if (lhs_resource == rhs_resource) { |
| 227 resources_seen.insert(resource); | 338 resources_seen.insert(resource); |
| 228 break; | 339 break; |
| 229 } | 340 } |
| 230 } | 341 } |
| 231 EXPECT_TRUE(base::ContainsKey(resources_seen, resource)); | 342 EXPECT_TRUE(base::ContainsKey(resources_seen, resource)); |
| 232 } | 343 } |
| 233 EXPECT_EQ(lhs.size(), resources_seen.size()); | 344 EXPECT_EQ(lhs.size(), resources_seen.size()); |
| 234 } | 345 } |
| 235 | 346 |
| 347 void ResourcePrefetchPredictorTablesTest::TestRedirectDataAreEqual( |
| 348 const RedirectDataMap& lhs, |
| 349 const RedirectDataMap& rhs) const { |
| 350 EXPECT_EQ(lhs.size(), rhs.size()); |
| 351 |
| 352 for (const auto& p : rhs) { |
| 353 const auto lhs_it = lhs.find(p.first); |
| 354 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
| 355 EXPECT_EQ(lhs_it->second.primary_key(), p.second.primary_key()); |
| 356 EXPECT_EQ(lhs_it->second.last_visit_time(), p.second.last_visit_time()); |
| 357 |
| 358 std::vector<RedirectStat> lhs_redirects(lhs_it->second.redirects().begin(), |
| 359 lhs_it->second.redirects().end()); |
| 360 std::vector<RedirectStat> rhs_redirects(p.second.redirects().begin(), |
| 361 p.second.redirects().end()); |
| 362 |
| 363 TestRedirectsAreEqual(lhs_redirects, rhs_redirects); |
| 364 } |
| 365 } |
| 366 |
| 367 void ResourcePrefetchPredictorTablesTest::TestRedirectsAreEqual( |
| 368 const std::vector<RedirectStat>& lhs, |
| 369 const std::vector<RedirectStat>& rhs) const { |
| 370 EXPECT_EQ(lhs.size(), rhs.size()); |
| 371 |
| 372 std::map<std::string, RedirectStat> lhs_index; |
| 373 // Repeated redirects are not allowed |
| 374 for (const auto& r : lhs) |
| 375 EXPECT_TRUE(lhs_index.insert(std::make_pair(r.url(), r)).second); |
| 376 |
| 377 for (const auto& r : rhs) { |
| 378 auto lhs_it = lhs_index.find(r.url()); |
| 379 if (lhs_it != lhs_index.end()) { |
| 380 EXPECT_EQ(r, lhs_it->second); |
| 381 lhs_index.erase(lhs_it); |
| 382 } else { |
| 383 ADD_FAILURE() << r.url(); |
| 384 } |
| 385 } |
| 386 |
| 387 EXPECT_TRUE(lhs_index.empty()); |
| 388 } |
| 389 |
| 236 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, | 390 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, |
| 237 const std::string& key) const { | 391 const std::string& key) const { |
| 238 PrefetchDataMap::const_iterator it = test_url_data_.find(key); | 392 PrefetchDataMap::const_iterator it = test_url_data_.find(key); |
| 239 if (it != test_url_data_.end()) { | 393 if (it != test_url_data_.end()) { |
| 240 m->insert(std::make_pair(it->first, it->second)); | 394 m->insert(*it); |
| 241 return; | 395 return; |
| 242 } | 396 } |
| 243 it = test_host_data_.find(key); | 397 it = test_host_data_.find(key); |
| 244 ASSERT_TRUE(it != test_host_data_.end()); | 398 ASSERT_TRUE(it != test_host_data_.end()); |
| 245 m->insert(std::make_pair(it->first, it->second)); | 399 m->insert(*it); |
| 400 } |
| 401 |
| 402 void ResourcePrefetchPredictorTablesTest::AddKey(RedirectDataMap* m, |
| 403 const std::string& key) const { |
| 404 auto it = test_url_redirect_data_.find(key); |
| 405 if (it != test_url_redirect_data_.end()) { |
| 406 m->insert(*it); |
| 407 return; |
| 408 } |
| 409 it = test_host_redirect_data_.find(key); |
| 410 ASSERT_TRUE(it != test_host_redirect_data_.end()); |
| 411 m->insert(*it); |
| 246 } | 412 } |
| 247 | 413 |
| 248 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 414 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
| 415 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); |
| 416 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); |
| 417 RedirectData empty_url_redirect_data; |
| 418 RedirectData empty_host_redirect_data; |
| 419 |
| 249 { // Url data. | 420 { // Url data. |
| 250 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 421 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
| 251 google.last_visit = base::Time::FromInternalValue(1); | 422 google.last_visit = base::Time::FromInternalValue(1); |
| 252 google.resources.push_back(CreateResourceData( | 423 google.resources.push_back(CreateResourceData( |
| 253 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, | 424 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, |
| 254 2, 1, 1.1, net::MEDIUM, false, false)); | 425 2, 1, 1.1, net::MEDIUM, false, false)); |
| 255 google.resources.push_back(CreateResourceData( | 426 google.resources.push_back(CreateResourceData( |
| 256 "http://www.google.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 0, | 427 "http://www.google.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 0, |
| 257 1, 2.1, net::MEDIUM, false, false)); | 428 1, 2.1, net::MEDIUM, false, false)); |
| 258 google.resources.push_back(CreateResourceData( | 429 google.resources.push_back(CreateResourceData( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 279 yahoo.last_visit = base::Time::FromInternalValue(3); | 450 yahoo.last_visit = base::Time::FromInternalValue(3); |
| 280 yahoo.resources.push_back(CreateResourceData( | 451 yahoo.resources.push_back(CreateResourceData( |
| 281 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, | 452 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, |
| 282 0, 10.0, net::MEDIUM, false, false)); | 453 0, 10.0, net::MEDIUM, false, false)); |
| 283 | 454 |
| 284 test_url_data_.clear(); | 455 test_url_data_.clear(); |
| 285 test_url_data_.insert(std::make_pair("http://www.google.com", google)); | 456 test_url_data_.insert(std::make_pair("http://www.google.com", google)); |
| 286 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); | 457 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); |
| 287 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); | 458 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); |
| 288 | 459 |
| 289 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); | 460 tables_->UpdateData(google, empty_host_data, empty_url_redirect_data, |
| 290 tables_->UpdateData(google, empty_host_data); | 461 empty_host_redirect_data); |
| 291 tables_->UpdateData(reddit, empty_host_data); | 462 tables_->UpdateData(reddit, empty_host_data, empty_url_redirect_data, |
| 292 tables_->UpdateData(yahoo, empty_host_data); | 463 empty_host_redirect_data); |
| 464 tables_->UpdateData(yahoo, empty_host_data, empty_url_redirect_data, |
| 465 empty_host_redirect_data); |
| 293 } | 466 } |
| 294 | 467 |
| 295 { // Host data. | 468 { // Host data. |
| 296 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); | 469 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); |
| 297 facebook.last_visit = base::Time::FromInternalValue(4); | 470 facebook.last_visit = base::Time::FromInternalValue(4); |
| 298 facebook.resources.push_back(CreateResourceData( | 471 facebook.resources.push_back(CreateResourceData( |
| 299 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, | 472 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, |
| 300 5, 2, 1, 1.1, net::MEDIUM, false, false)); | 473 5, 2, 1, 1.1, net::MEDIUM, false, false)); |
| 301 facebook.resources.push_back(CreateResourceData( | 474 facebook.resources.push_back(CreateResourceData( |
| 302 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, | 475 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 315 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 488 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
| 316 yahoo.last_visit = base::Time::FromInternalValue(5); | 489 yahoo.last_visit = base::Time::FromInternalValue(5); |
| 317 yahoo.resources.push_back(CreateResourceData( | 490 yahoo.resources.push_back(CreateResourceData( |
| 318 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, | 491 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, |
| 319 0, 10.0, net::MEDIUM, false, false)); | 492 0, 10.0, net::MEDIUM, false, false)); |
| 320 | 493 |
| 321 test_host_data_.clear(); | 494 test_host_data_.clear(); |
| 322 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); | 495 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); |
| 323 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); | 496 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); |
| 324 | 497 |
| 325 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); | 498 tables_->UpdateData(empty_url_data, facebook, empty_url_redirect_data, |
| 326 tables_->UpdateData(empty_url_data, facebook); | 499 empty_host_redirect_data); |
| 327 tables_->UpdateData(empty_url_data, yahoo); | 500 tables_->UpdateData(empty_url_data, yahoo, empty_url_redirect_data, |
| 501 empty_host_redirect_data); |
| 502 } |
| 503 |
| 504 { // Url redirect data. |
| 505 RedirectData facebook; |
| 506 facebook.set_primary_key("http://fb.com/google"); |
| 507 facebook.set_last_visit_time(6); |
| 508 InitializeRedirectStat(facebook.add_redirects(), |
| 509 "https://facebook.com/google", 5, 1, 0); |
| 510 InitializeRedirectStat(facebook.add_redirects(), |
| 511 "https://facebook.com/login", 3, 5, 1); |
| 512 |
| 513 RedirectData nytimes; |
| 514 nytimes.set_primary_key("http://nyt.com"); |
| 515 nytimes.set_last_visit_time(7); |
| 516 InitializeRedirectStat(nytimes.add_redirects(), "https://nytimes.com", 2, 0, |
| 517 0); |
| 518 |
| 519 RedirectData google; |
| 520 google.set_primary_key("http://google.com"); |
| 521 google.set_last_visit_time(8); |
| 522 InitializeRedirectStat(google.add_redirects(), "https://google.com", 3, 0, |
| 523 0); |
| 524 |
| 525 test_url_redirect_data_.clear(); |
| 526 test_url_redirect_data_.insert( |
| 527 std::make_pair(facebook.primary_key(), facebook)); |
| 528 test_url_redirect_data_.insert( |
| 529 std::make_pair(nytimes.primary_key(), nytimes)); |
| 530 test_url_redirect_data_.insert( |
| 531 std::make_pair(google.primary_key(), google)); |
| 532 |
| 533 tables_->UpdateData(empty_url_data, empty_host_data, facebook, |
| 534 empty_host_redirect_data); |
| 535 tables_->UpdateData(empty_url_data, empty_host_data, nytimes, |
| 536 empty_host_redirect_data); |
| 537 tables_->UpdateData(empty_url_data, empty_host_data, google, |
| 538 empty_host_redirect_data); |
| 539 } |
| 540 |
| 541 { // Host redirect data. |
| 542 RedirectData bbc; |
| 543 bbc.set_primary_key("bbc.com"); |
| 544 bbc.set_last_visit_time(9); |
| 545 InitializeRedirectStat(bbc.add_redirects(), "www.bbc.com", 8, 4, 1); |
| 546 InitializeRedirectStat(bbc.add_redirects(), "m.bbc.com", 5, 8, 0); |
| 547 InitializeRedirectStat(bbc.add_redirects(), "bbc.co.uk", 1, 3, 0); |
| 548 |
| 549 RedirectData microsoft; |
| 550 microsoft.set_primary_key("microsoft.com"); |
| 551 microsoft.set_last_visit_time(10); |
| 552 InitializeRedirectStat(microsoft.add_redirects(), "www.microsoft.com", 10, |
| 553 0, 0); |
| 554 |
| 555 test_host_redirect_data_.clear(); |
| 556 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); |
| 557 test_host_redirect_data_.insert( |
| 558 std::make_pair(microsoft.primary_key(), microsoft)); |
| 559 tables_->UpdateData(empty_url_data, empty_host_data, |
| 560 empty_url_redirect_data, bbc); |
| 561 tables_->UpdateData(empty_url_data, empty_host_data, |
| 562 empty_url_redirect_data, microsoft); |
| 328 } | 563 } |
| 329 } | 564 } |
| 330 | 565 |
| 331 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { | 566 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { |
| 332 db_.reset(new PredictorDatabase(&profile_)); | 567 db_.reset(new PredictorDatabase(&profile_)); |
| 333 base::RunLoop().RunUntilIdle(); | 568 base::RunLoop().RunUntilIdle(); |
| 334 tables_ = db_->resource_prefetch_tables(); | 569 tables_ = db_->resource_prefetch_tables(); |
| 335 } | 570 } |
| 336 | 571 |
| 337 // Test cases. | 572 // Test cases. |
| 338 | 573 |
| 339 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { | 574 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeResourceScore) { |
| 340 ResourceData js_resource = CreateResourceData( | 575 ResourceData js_resource = CreateResourceData( |
| 341 "http://www.resources.google.com/script.js", | 576 "http://www.resources.google.com/script.js", |
| 342 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); | 577 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); |
| 343 ResourceData image_resource = CreateResourceData( | 578 ResourceData image_resource = CreateResourceData( |
| 344 "http://www.resources.google.com/image.jpg", content::RESOURCE_TYPE_IMAGE, | 579 "http://www.resources.google.com/image.jpg", content::RESOURCE_TYPE_IMAGE, |
| 345 11, 0, 0, 1., net::MEDIUM, false, false); | 580 11, 0, 0, 1., net::MEDIUM, false, false); |
| 346 ResourceData css_resource = | 581 ResourceData css_resource = |
| 347 CreateResourceData("http://www.resources.google.com/stylesheet.css", | 582 CreateResourceData("http://www.resources.google.com/stylesheet.css", |
| 348 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., | 583 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., |
| 349 net::MEDIUM, false, false); | 584 net::MEDIUM, false, false); |
| 350 ResourceData font_resource = | 585 ResourceData font_resource = |
| 351 CreateResourceData("http://www.resources.google.com/font.woff", | 586 CreateResourceData("http://www.resources.google.com/font.woff", |
| 352 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., | 587 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., |
| 353 net::MEDIUM, false, false); | 588 net::MEDIUM, false, false); |
| 354 float js_resource_score = | 589 float js_resource_score = |
| 355 ResourcePrefetchPredictorTables::ComputeScore(js_resource); | 590 ResourcePrefetchPredictorTables::ComputeResourceScore(js_resource); |
| 356 float css_resource_score = | 591 float css_resource_score = |
| 357 ResourcePrefetchPredictorTables::ComputeScore(css_resource); | 592 ResourcePrefetchPredictorTables::ComputeResourceScore(css_resource); |
| 358 float font_resource_score = | 593 float font_resource_score = |
| 359 ResourcePrefetchPredictorTables::ComputeScore(font_resource); | 594 ResourcePrefetchPredictorTables::ComputeResourceScore(font_resource); |
| 360 float image_resource_score = | 595 float image_resource_score = |
| 361 ResourcePrefetchPredictorTables::ComputeScore(image_resource); | 596 ResourcePrefetchPredictorTables::ComputeResourceScore(image_resource); |
| 362 | 597 |
| 363 EXPECT_TRUE(js_resource_score == css_resource_score); | 598 EXPECT_TRUE(js_resource_score == css_resource_score); |
| 364 EXPECT_TRUE(js_resource_score == font_resource_score); | 599 EXPECT_TRUE(js_resource_score == font_resource_score); |
| 365 EXPECT_NEAR(199., js_resource_score, 1e-4); | 600 EXPECT_NEAR(199., js_resource_score, 1e-4); |
| 366 EXPECT_NEAR(99., image_resource_score, 1e-4); | 601 EXPECT_NEAR(99., image_resource_score, 1e-4); |
| 367 } | 602 } |
| 368 | 603 |
| 369 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { | 604 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { |
| 370 TestGetAllData(); | 605 TestGetAllData(); |
| 371 } | 606 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 399 ResourcePrefetchPredictorTables::SetDatabaseVersion(db, version + 1)); | 634 ResourcePrefetchPredictorTables::SetDatabaseVersion(db, version + 1)); |
| 400 EXPECT_EQ(version + 1, | 635 EXPECT_EQ(version + 1, |
| 401 ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); | 636 ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); |
| 402 | 637 |
| 403 ReopenDatabase(); | 638 ReopenDatabase(); |
| 404 | 639 |
| 405 db = tables_->DB(); | 640 db = tables_->DB(); |
| 406 ASSERT_EQ(version, ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); | 641 ASSERT_EQ(version, ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); |
| 407 | 642 |
| 408 PrefetchDataMap url_data, host_data; | 643 PrefetchDataMap url_data, host_data; |
| 409 tables_->GetAllData(&url_data, &host_data); | 644 RedirectDataMap url_redirect_data, host_redirect_data; |
| 645 tables_->GetAllData(&url_data, &host_data, &url_redirect_data, |
| 646 &host_redirect_data); |
| 410 EXPECT_TRUE(url_data.empty()); | 647 EXPECT_TRUE(url_data.empty()); |
| 411 EXPECT_TRUE(host_data.empty()); | 648 EXPECT_TRUE(host_data.empty()); |
| 412 } | 649 } |
| 413 | 650 |
| 414 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) { | 651 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) { |
| 415 TestGetAllData(); | 652 TestGetAllData(); |
| 416 } | 653 } |
| 417 | 654 |
| 418 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateData) { | 655 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateData) { |
| 419 TestUpdateData(); | 656 TestUpdateData(); |
| 420 } | 657 } |
| 421 | 658 |
| 422 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteData) { | 659 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteData) { |
| 423 TestDeleteData(); | 660 TestDeleteData(); |
| 424 } | 661 } |
| 425 | 662 |
| 426 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 663 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
| 427 TestDeleteSingleDataPoint(); | 664 TestDeleteSingleDataPoint(); |
| 428 } | 665 } |
| 429 | 666 |
| 430 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 667 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
| 431 TestDeleteAllData(); | 668 TestDeleteAllData(); |
| 432 } | 669 } |
| 433 | 670 |
| 434 } // namespace predictors | 671 } // namespace predictors |
| OLD | NEW |