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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 PrefetchDataMap expected_host_data; | 164 PrefetchDataMap expected_host_data; |
165 AddKey(&expected_host_data, "www.yahoo.com"); | 165 AddKey(&expected_host_data, "www.yahoo.com"); |
166 | 166 |
167 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 167 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
168 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 168 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
169 } | 169 } |
170 | 170 |
171 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { | 171 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { |
172 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 172 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
173 google.last_visit = base::Time::FromInternalValue(10); | 173 google.last_visit = base::Time::FromInternalValue(10); |
174 google.resources.push_back(ResourceRow(std::string(), | 174 google.resources.push_back(ResourceRow("http://www.google.com/style.css", |
175 "http://www.google.com/style.css", | |
176 content::RESOURCE_TYPE_STYLESHEET, 6, | 175 content::RESOURCE_TYPE_STYLESHEET, 6, |
177 2, 0, 1.0, net::MEDIUM, true, false)); | 176 2, 0, 1.0, net::MEDIUM, true, false)); |
178 google.resources.push_back(ResourceRow( | 177 google.resources.push_back(ResourceRow("http://www.google.com/image.png", |
179 std::string(), "http://www.google.com/image.png", | 178 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, |
180 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM, false, false)); | 179 4.2, net::MEDIUM, false, false)); |
181 google.resources.push_back(ResourceRow(std::string(), | 180 google.resources.push_back(ResourceRow("http://www.google.com/a.xml", |
182 "http://www.google.com/a.xml", | |
183 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, | 181 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, |
184 0, 6.1, net::MEDIUM, false, false)); | 182 0, 6.1, net::MEDIUM, false, false)); |
185 google.resources.push_back(ResourceRow( | 183 google.resources.push_back(ResourceRow( |
186 std::string(), "http://www.resources.google.com/script.js", | 184 "http://www.resources.google.com/script.js", |
187 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); | 185 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); |
188 | 186 |
189 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 187 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
190 yahoo.last_visit = base::Time::FromInternalValue(7); | 188 yahoo.last_visit = base::Time::FromInternalValue(7); |
191 yahoo.resources.push_back(ResourceRow( | 189 yahoo.resources.push_back(ResourceRow("http://www.yahoo.com/image.png", |
192 std::string(), "http://www.yahoo.com/image.png", | 190 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, |
193 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM, true, false)); | 191 10.0, net::MEDIUM, true, false)); |
194 | 192 |
195 tables_->UpdateData(google, yahoo); | 193 tables_->UpdateData(google, yahoo); |
196 | 194 |
197 PrefetchDataMap actual_url_data, actual_host_data; | 195 PrefetchDataMap actual_url_data, actual_host_data; |
198 tables_->GetAllData(&actual_url_data, &actual_host_data); | 196 tables_->GetAllData(&actual_url_data, &actual_host_data); |
199 | 197 |
200 PrefetchDataMap expected_url_data, expected_host_data; | 198 PrefetchDataMap expected_url_data, expected_host_data; |
201 AddKey(&expected_url_data, "http://www.reddit.com"); | 199 AddKey(&expected_url_data, "http://www.reddit.com"); |
202 AddKey(&expected_url_data, "http://www.yahoo.com"); | 200 AddKey(&expected_url_data, "http://www.yahoo.com"); |
203 expected_url_data.insert(std::make_pair("http://www.google.com", google)); | 201 expected_url_data.insert(std::make_pair("http://www.google.com", google)); |
(...skipping 12 matching lines...) Expand all Loading... |
216 tables_->GetAllData(&actual_url_data, &actual_host_data); | 214 tables_->GetAllData(&actual_url_data, &actual_host_data); |
217 EXPECT_TRUE(actual_url_data.empty()); | 215 EXPECT_TRUE(actual_url_data.empty()); |
218 EXPECT_TRUE(actual_host_data.empty()); | 216 EXPECT_TRUE(actual_host_data.empty()); |
219 } | 217 } |
220 | 218 |
221 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( | 219 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( |
222 const PrefetchDataMap& lhs, | 220 const PrefetchDataMap& lhs, |
223 const PrefetchDataMap& rhs) const { | 221 const PrefetchDataMap& rhs) const { |
224 EXPECT_EQ(lhs.size(), rhs.size()); | 222 EXPECT_EQ(lhs.size(), rhs.size()); |
225 | 223 |
226 for (PrefetchDataMap::const_iterator rhs_it = rhs.begin(); | 224 for (const std::pair<std::string, PrefetchData>& p : rhs) { |
227 rhs_it != rhs.end(); ++rhs_it) { | 225 PrefetchDataMap::const_iterator lhs_it = lhs.find(p.first); |
228 PrefetchDataMap::const_iterator lhs_it = lhs.find(rhs_it->first); | 226 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
229 ASSERT_TRUE(lhs_it != lhs.end()) << rhs_it->first; | |
230 | 227 |
231 TestResourceRowsAreEqual(lhs_it->second.resources, | 228 TestResourceRowsAreEqual(lhs_it->second.resources, p.second.resources); |
232 rhs_it->second.resources); | |
233 } | 229 } |
234 } | 230 } |
235 | 231 |
236 void ResourcePrefetchPredictorTablesTest::TestResourceRowsAreEqual( | 232 void ResourcePrefetchPredictorTablesTest::TestResourceRowsAreEqual( |
237 const ResourceRows& lhs, | 233 const ResourceRows& lhs, |
238 const ResourceRows& rhs) const { | 234 const ResourceRows& rhs) const { |
239 EXPECT_EQ(lhs.size(), rhs.size()); | 235 EXPECT_EQ(lhs.size(), rhs.size()); |
240 | 236 |
241 std::set<GURL> resources_seen; | 237 std::set<GURL> resources_seen; |
242 for (ResourceRows::const_iterator rhs_it = rhs.begin(); | 238 for (ResourceRows::const_iterator rhs_it = rhs.begin(); |
(...skipping 22 matching lines...) Expand all Loading... |
265 } | 261 } |
266 it = test_host_data_.find(key); | 262 it = test_host_data_.find(key); |
267 ASSERT_TRUE(it != test_host_data_.end()); | 263 ASSERT_TRUE(it != test_host_data_.end()); |
268 m->insert(std::make_pair(it->first, it->second)); | 264 m->insert(std::make_pair(it->first, it->second)); |
269 } | 265 } |
270 | 266 |
271 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 267 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
272 { // Url data. | 268 { // Url data. |
273 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 269 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
274 google.last_visit = base::Time::FromInternalValue(1); | 270 google.last_visit = base::Time::FromInternalValue(1); |
275 google.resources.push_back( | 271 google.resources.push_back(ResourceRow( |
276 ResourceRow(std::string(), "http://www.google.com/style.css", | 272 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, |
277 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, | 273 2, 1, 1.1, net::MEDIUM, false, false)); |
278 net::MEDIUM, false, false)); | 274 google.resources.push_back(ResourceRow("http://www.google.com/script.js", |
279 google.resources.push_back(ResourceRow(std::string(), | |
280 "http://www.google.com/script.js", | |
281 content::RESOURCE_TYPE_SCRIPT, 4, 0, | 275 content::RESOURCE_TYPE_SCRIPT, 4, 0, |
282 1, 2.1, net::MEDIUM, false, false)); | 276 1, 2.1, net::MEDIUM, false, false)); |
| 277 google.resources.push_back(ResourceRow("http://www.google.com/image.png", |
| 278 content::RESOURCE_TYPE_IMAGE, 6, 3, |
| 279 0, 2.2, net::MEDIUM, false, false)); |
283 google.resources.push_back(ResourceRow( | 280 google.resources.push_back(ResourceRow( |
284 std::string(), "http://www.google.com/image.png", | 281 "http://www.google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, |
285 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false)); | 282 0, 5.1, net::MEDIUM, false, false)); |
286 google.resources.push_back( | 283 google.resources.push_back( |
287 ResourceRow(std::string(), "http://www.google.com/a.font", | 284 ResourceRow("http://www.resources.google.com/script.js", |
288 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM, | |
289 false, false)); | |
290 google.resources.push_back( | |
291 ResourceRow(std::string(), "http://www.resources.google.com/script.js", | |
292 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, | 285 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, |
293 false, false)); | 286 false, false)); |
294 | 287 |
295 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); | 288 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); |
296 reddit.last_visit = base::Time::FromInternalValue(2); | 289 reddit.last_visit = base::Time::FromInternalValue(2); |
297 reddit.resources.push_back( | 290 reddit.resources.push_back(ResourceRow( |
298 ResourceRow(std::string(), "http://reddit-resource.com/script1.js", | 291 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, |
299 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM, | 292 4, 0, 1, 1.0, net::MEDIUM, false, false)); |
300 false, false)); | 293 reddit.resources.push_back(ResourceRow( |
301 reddit.resources.push_back( | 294 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, |
302 ResourceRow(std::string(), "http://reddit-resource.com/script2.js", | 295 2, 0, 0, 2.1, net::MEDIUM, false, false)); |
303 content::RESOURCE_TYPE_SCRIPT, 2, 0, 0, 2.1, net::MEDIUM, | |
304 false, false)); | |
305 | 296 |
306 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); | 297 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); |
307 yahoo.last_visit = base::Time::FromInternalValue(3); | 298 yahoo.last_visit = base::Time::FromInternalValue(3); |
308 yahoo.resources.push_back(ResourceRow(std::string(), | 299 yahoo.resources.push_back(ResourceRow("http://www.google.com/image.png", |
309 "http://www.google.com/image.png", | |
310 content::RESOURCE_TYPE_IMAGE, 20, 1, | 300 content::RESOURCE_TYPE_IMAGE, 20, 1, |
311 0, 10.0, net::MEDIUM, false, false)); | 301 0, 10.0, net::MEDIUM, false, false)); |
312 | 302 |
313 test_url_data_.clear(); | 303 test_url_data_.clear(); |
314 test_url_data_.insert(std::make_pair("http://www.google.com", google)); | 304 test_url_data_.insert(std::make_pair("http://www.google.com", google)); |
315 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); | 305 test_url_data_.insert(std::make_pair("http://www.reddit.com", reddit)); |
316 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); | 306 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); |
317 | 307 |
318 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); | 308 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); |
319 tables_->UpdateData(google, empty_host_data); | 309 tables_->UpdateData(google, empty_host_data); |
320 tables_->UpdateData(reddit, empty_host_data); | 310 tables_->UpdateData(reddit, empty_host_data); |
321 tables_->UpdateData(yahoo, empty_host_data); | 311 tables_->UpdateData(yahoo, empty_host_data); |
322 } | 312 } |
323 | 313 |
324 { // Host data. | 314 { // Host data. |
325 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); | 315 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); |
326 facebook.last_visit = base::Time::FromInternalValue(4); | 316 facebook.last_visit = base::Time::FromInternalValue(4); |
| 317 facebook.resources.push_back(ResourceRow( |
| 318 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, |
| 319 5, 2, 1, 1.1, net::MEDIUM, false, false)); |
| 320 facebook.resources.push_back(ResourceRow( |
| 321 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, |
| 322 0, 1, 2.1, net::MEDIUM, false, false)); |
| 323 facebook.resources.push_back(ResourceRow( |
| 324 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, |
| 325 0, 2.2, net::MEDIUM, false, false)); |
| 326 facebook.resources.push_back(ResourceRow( |
| 327 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, |
| 328 0, 0, 5.1, net::MEDIUM, false, false)); |
327 facebook.resources.push_back( | 329 facebook.resources.push_back( |
328 ResourceRow(std::string(), "http://www.facebook.com/style.css", | 330 ResourceRow("http://www.resources.facebook.com/script.js", |
329 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, | 331 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, |
330 net::MEDIUM, false, false)); | |
331 facebook.resources.push_back( | |
332 ResourceRow(std::string(), "http://www.facebook.com/script.js", | |
333 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 2.1, net::MEDIUM, | |
334 false, false)); | 332 false, false)); |
335 facebook.resources.push_back(ResourceRow( | |
336 std::string(), "http://www.facebook.com/image.png", | |
337 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false)); | |
338 facebook.resources.push_back( | |
339 ResourceRow(std::string(), "http://www.facebook.com/a.font", | |
340 content::RESOURCE_TYPE_LAST_TYPE, 2, 0, 0, 5.1, net::MEDIUM, | |
341 false, false)); | |
342 facebook.resources.push_back(ResourceRow( | |
343 std::string(), "http://www.resources.facebook.com/script.js", | |
344 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, net::MEDIUM, false, | |
345 false)); | |
346 | 333 |
347 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 334 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
348 yahoo.last_visit = base::Time::FromInternalValue(5); | 335 yahoo.last_visit = base::Time::FromInternalValue(5); |
349 yahoo.resources.push_back(ResourceRow(std::string(), | 336 yahoo.resources.push_back(ResourceRow("http://www.google.com/image.png", |
350 "http://www.google.com/image.png", | |
351 content::RESOURCE_TYPE_IMAGE, 20, 1, | 337 content::RESOURCE_TYPE_IMAGE, 20, 1, |
352 0, 10.0, net::MEDIUM, false, false)); | 338 0, 10.0, net::MEDIUM, false, false)); |
353 | 339 |
354 test_host_data_.clear(); | 340 test_host_data_.clear(); |
355 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); | 341 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); |
356 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); | 342 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); |
357 | 343 |
358 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); | 344 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); |
359 tables_->UpdateData(empty_url_data, facebook); | 345 tables_->UpdateData(empty_url_data, facebook); |
360 tables_->UpdateData(empty_url_data, yahoo); | 346 tables_->UpdateData(empty_url_data, yahoo); |
361 } | 347 } |
362 } | 348 } |
363 | 349 |
364 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { | 350 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { |
365 db_.reset(new PredictorDatabase(&profile_)); | 351 db_.reset(new PredictorDatabase(&profile_)); |
366 base::RunLoop().RunUntilIdle(); | 352 base::RunLoop().RunUntilIdle(); |
367 tables_ = db_->resource_prefetch_tables(); | 353 tables_ = db_->resource_prefetch_tables(); |
368 } | 354 } |
369 | 355 |
370 // Test cases. | 356 // Test cases. |
371 | 357 |
372 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { | 358 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { |
373 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; | 359 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; |
374 ResourceRow js_resource( | 360 ResourceRow js_resource("http://www.resources.google.com/script.js", |
375 std::string(), "http://www.resources.google.com/script.js", | 361 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., |
376 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); | 362 net::MEDIUM, false, false); |
377 ResourceRow image_resource( | 363 ResourceRow image_resource("http://www.resources.google.com/image.jpg", |
378 std::string(), "http://www.resources.google.com/image.jpg", | 364 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., |
379 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM, false, false); | 365 net::MEDIUM, false, false); |
380 ResourceRow css_resource(std::string(), | 366 ResourceRow css_resource("http://www.resources.google.com/stylesheet.css", |
381 "http://www.resources.google.com/stylesheet.css", | |
382 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., | 367 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., |
383 net::MEDIUM, false, false); | 368 net::MEDIUM, false, false); |
384 ResourceRow font_resource(std::string(), | 369 ResourceRow font_resource("http://www.resources.google.com/font.woff", |
385 "http://www.resources.google.com/font.woff", | |
386 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., | 370 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., |
387 net::MEDIUM, false, false); | 371 net::MEDIUM, false, false); |
388 EXPECT_TRUE(js_resource.score == css_resource.score); | 372 EXPECT_TRUE(js_resource.score == css_resource.score); |
389 EXPECT_TRUE(js_resource.score == font_resource.score); | 373 EXPECT_TRUE(js_resource.score == font_resource.score); |
390 EXPECT_NEAR(199., js_resource.score, 1e-4); | 374 EXPECT_NEAR(199., js_resource.score, 1e-4); |
391 EXPECT_NEAR(99., image_resource.score, 1e-4); | 375 EXPECT_NEAR(99., image_resource.score, 1e-4); |
392 } | 376 } |
393 | 377 |
394 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { | 378 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { |
395 TestGetAllData(); | 379 TestGetAllData(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 | 434 |
451 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 435 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
452 TestDeleteSingleDataPoint(); | 436 TestDeleteSingleDataPoint(); |
453 } | 437 } |
454 | 438 |
455 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 439 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
456 TestDeleteAllData(); | 440 TestDeleteAllData(); |
457 } | 441 } |
458 | 442 |
459 } // namespace predictors | 443 } // namespace predictors |
OLD | NEW |