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 <sstream> |
6 #include <utility> | 7 #include <utility> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/predictors/predictor_database.h" | 13 #include "chrome/browser/predictors/predictor_database.h" |
13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 14 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 15 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
14 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
15 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
16 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
17 #include "sql/statement.h" | 19 #include "sql/statement.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace predictors { | 22 namespace predictors { |
21 | 23 |
| 24 using chrome_browser_predictors::ResourceData; |
| 25 |
22 class ResourcePrefetchPredictorTablesTest : public testing::Test { | 26 class ResourcePrefetchPredictorTablesTest : public testing::Test { |
23 public: | 27 public: |
24 ResourcePrefetchPredictorTablesTest(); | 28 ResourcePrefetchPredictorTablesTest(); |
25 ~ResourcePrefetchPredictorTablesTest() override; | 29 ~ResourcePrefetchPredictorTablesTest() override; |
26 void SetUp() override; | 30 void SetUp() override; |
27 void TearDown() override; | 31 void TearDown() override; |
28 | 32 |
29 protected: | 33 protected: |
30 void TestGetAllData(); | 34 void TestGetAllData(); |
31 void TestUpdateData(); | 35 void TestUpdateData(); |
32 void TestDeleteData(); | 36 void TestDeleteData(); |
33 void TestDeleteSingleDataPoint(); | 37 void TestDeleteSingleDataPoint(); |
34 void TestDeleteAllData(); | 38 void TestDeleteAllData(); |
35 | 39 |
36 base::MessageLoop loop_; | 40 base::MessageLoop loop_; |
37 content::TestBrowserThread db_thread_; | 41 content::TestBrowserThread db_thread_; |
38 TestingProfile profile_; | 42 TestingProfile profile_; |
39 std::unique_ptr<PredictorDatabase> db_; | 43 std::unique_ptr<PredictorDatabase> db_; |
40 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 44 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
41 | 45 |
42 private: | 46 private: |
43 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; | |
44 typedef std::vector<ResourceRow> ResourceRows; | |
45 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; | 47 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; |
46 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 48 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
47 | 49 |
48 // Initializes the tables, |test_url_data_| and |test_host_data_|. | 50 // Initializes the tables, |test_url_data_| and |test_host_data_|. |
49 void InitializeSampleData(); | 51 void InitializeSampleData(); |
50 | 52 |
51 // Checks that the input PrefetchData are the same, although the resources | 53 // Checks that the input PrefetchData are the same, although the resources |
52 // can be in different order. | 54 // can be in different order. |
53 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, | 55 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, |
54 const PrefetchDataMap& rhs) const; | 56 const PrefetchDataMap& rhs) const; |
55 void TestResourceRowsAreEqual(const ResourceRows& lhs, | 57 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, |
56 const ResourceRows& rhs) const; | 58 const std::vector<ResourceData>& rhs) const; |
57 | 59 |
58 void AddKey(PrefetchDataMap* m, const std::string& key) const; | 60 void AddKey(PrefetchDataMap* m, const std::string& key) const; |
59 | 61 |
60 static void LogResource(const ResourceRow& row) { | |
61 LOG(ERROR) << "\t\t" << row.resource_url << "\t" << row.resource_type | |
62 << "\t" << row.number_of_hits << "\t" << row.number_of_misses | |
63 << "\t" << row.consecutive_misses << "\t" << row.average_position | |
64 << "\t" << row.priority << "\t" << row.has_validators << "\t" | |
65 << row.always_revalidate << "\t" << row.score; | |
66 } | |
67 | |
68 // Useful for debugging tests. | 62 // Useful for debugging tests. |
69 void PrintPrefetchData(const PrefetchData& data) const { | 63 void PrintPrefetchData(const PrefetchData& data) const { |
70 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key | 64 LOG(ERROR) << "[" << data.key_type << "," << data.primary_key |
71 << "," << data.last_visit.ToInternalValue() << "]"; | 65 << "," << data.last_visit.ToInternalValue() << "]"; |
72 for (const ResourceRow& resource : data.resources) | 66 for (const ResourceData& resource : data.resources) { |
73 LogResource(resource); | 67 std::ostringstream stream; |
| 68 PrintTo(resource, &stream); |
| 69 LOG(ERROR) << stream.str(); |
| 70 } |
74 } | 71 } |
75 | 72 |
76 PrefetchDataMap test_url_data_; | 73 PrefetchDataMap test_url_data_; |
77 PrefetchDataMap test_host_data_; | 74 PrefetchDataMap test_host_data_; |
78 }; | 75 }; |
79 | 76 |
80 class ResourcePrefetchPredictorTablesReopenTest | 77 class ResourcePrefetchPredictorTablesReopenTest |
81 : public ResourcePrefetchPredictorTablesTest { | 78 : public ResourcePrefetchPredictorTablesTest { |
82 public: | 79 public: |
83 void SetUp() override { | 80 void SetUp() override { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 PrefetchDataMap expected_host_data; | 161 PrefetchDataMap expected_host_data; |
165 AddKey(&expected_host_data, "www.yahoo.com"); | 162 AddKey(&expected_host_data, "www.yahoo.com"); |
166 | 163 |
167 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 164 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
168 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 165 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
169 } | 166 } |
170 | 167 |
171 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { | 168 void ResourcePrefetchPredictorTablesTest::TestUpdateData() { |
172 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 169 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
173 google.last_visit = base::Time::FromInternalValue(10); | 170 google.last_visit = base::Time::FromInternalValue(10); |
174 google.resources.push_back(ResourceRow(std::string(), | 171 google.resources.push_back(MakeResourceData( |
175 "http://www.google.com/style.css", | 172 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 6, |
176 content::RESOURCE_TYPE_STYLESHEET, 6, | 173 2, 0, 1.0, net::MEDIUM, true, false)); |
177 2, 0, 1.0, net::MEDIUM, true, false)); | 174 google.resources.push_back(MakeResourceData( |
178 google.resources.push_back(ResourceRow( | 175 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 4, 1, |
179 std::string(), "http://www.google.com/image.png", | 176 4.2, net::MEDIUM, false, false)); |
180 content::RESOURCE_TYPE_IMAGE, 6, 4, 1, 4.2, net::MEDIUM, false, false)); | 177 google.resources.push_back(MakeResourceData( |
181 google.resources.push_back(ResourceRow(std::string(), | 178 "http://www.google.com/a.xml", content::RESOURCE_TYPE_LAST_TYPE, 1, 0, 0, |
182 "http://www.google.com/a.xml", | 179 6.1, net::MEDIUM, false, false)); |
183 content::RESOURCE_TYPE_LAST_TYPE, 1, 0, | 180 google.resources.push_back(MakeResourceData( |
184 0, 6.1, net::MEDIUM, false, false)); | 181 "http://www.resources.google.com/script.js", |
185 google.resources.push_back(ResourceRow( | |
186 std::string(), "http://www.resources.google.com/script.js", | |
187 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); | 182 content::RESOURCE_TYPE_SCRIPT, 12, 0, 0, 8.5, net::MEDIUM, true, true)); |
188 | 183 |
189 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 184 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
190 yahoo.last_visit = base::Time::FromInternalValue(7); | 185 yahoo.last_visit = base::Time::FromInternalValue(7); |
191 yahoo.resources.push_back(ResourceRow( | 186 yahoo.resources.push_back(MakeResourceData( |
192 std::string(), "http://www.yahoo.com/image.png", | 187 "http://www.yahoo.com/image.png", content::RESOURCE_TYPE_IMAGE, 120, 1, 1, |
193 content::RESOURCE_TYPE_IMAGE, 120, 1, 1, 10.0, net::MEDIUM, true, false)); | 188 10.0, net::MEDIUM, true, false)); |
194 | 189 |
195 tables_->UpdateData(google, yahoo); | 190 tables_->UpdateData(google, yahoo); |
196 | 191 |
197 PrefetchDataMap actual_url_data, actual_host_data; | 192 PrefetchDataMap actual_url_data, actual_host_data; |
198 tables_->GetAllData(&actual_url_data, &actual_host_data); | 193 tables_->GetAllData(&actual_url_data, &actual_host_data); |
199 | 194 |
200 PrefetchDataMap expected_url_data, expected_host_data; | 195 PrefetchDataMap expected_url_data, expected_host_data; |
201 AddKey(&expected_url_data, "http://www.reddit.com"); | 196 AddKey(&expected_url_data, "http://www.reddit.com"); |
202 AddKey(&expected_url_data, "http://www.yahoo.com"); | 197 AddKey(&expected_url_data, "http://www.yahoo.com"); |
203 expected_url_data.insert(std::make_pair("http://www.google.com", google)); | 198 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); | 211 tables_->GetAllData(&actual_url_data, &actual_host_data); |
217 EXPECT_TRUE(actual_url_data.empty()); | 212 EXPECT_TRUE(actual_url_data.empty()); |
218 EXPECT_TRUE(actual_host_data.empty()); | 213 EXPECT_TRUE(actual_host_data.empty()); |
219 } | 214 } |
220 | 215 |
221 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( | 216 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( |
222 const PrefetchDataMap& lhs, | 217 const PrefetchDataMap& lhs, |
223 const PrefetchDataMap& rhs) const { | 218 const PrefetchDataMap& rhs) const { |
224 EXPECT_EQ(lhs.size(), rhs.size()); | 219 EXPECT_EQ(lhs.size(), rhs.size()); |
225 | 220 |
226 for (PrefetchDataMap::const_iterator rhs_it = rhs.begin(); | 221 for (const std::pair<const std::string&, PrefetchData>& p : rhs) { |
227 rhs_it != rhs.end(); ++rhs_it) { | 222 PrefetchDataMap::const_iterator lhs_it = lhs.find(p.first); |
228 PrefetchDataMap::const_iterator lhs_it = lhs.find(rhs_it->first); | 223 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
229 ASSERT_TRUE(lhs_it != lhs.end()) << rhs_it->first; | 224 TestResourcesAreEqual(lhs_it->second.resources, p.second.resources); |
230 | |
231 TestResourceRowsAreEqual(lhs_it->second.resources, | |
232 rhs_it->second.resources); | |
233 } | 225 } |
234 } | 226 } |
235 | 227 |
236 void ResourcePrefetchPredictorTablesTest::TestResourceRowsAreEqual( | 228 void ResourcePrefetchPredictorTablesTest::TestResourcesAreEqual( |
237 const ResourceRows& lhs, | 229 const std::vector<ResourceData>& lhs, |
238 const ResourceRows& rhs) const { | 230 const std::vector<ResourceData>& rhs) const { |
239 EXPECT_EQ(lhs.size(), rhs.size()); | 231 EXPECT_EQ(lhs.size(), rhs.size()); |
240 | 232 |
241 std::set<GURL> resources_seen; | 233 std::set<GURL> resources_seen; |
242 for (ResourceRows::const_iterator rhs_it = rhs.begin(); | 234 for (const ResourceData& rhs_resource : rhs) { |
243 rhs_it != rhs.end(); ++rhs_it) { | 235 const GURL& resource_url = GURL(rhs_resource.resource_url()); |
244 const GURL& resource = rhs_it->resource_url; | 236 EXPECT_FALSE(base::ContainsKey(resources_seen, resource_url)); |
245 EXPECT_FALSE(base::ContainsKey(resources_seen, resource)); | |
246 | 237 |
247 for (ResourceRows::const_iterator lhs_it = lhs.begin(); | 238 for (const ResourceData& lhs_resource : lhs) { |
248 lhs_it != lhs.end(); ++lhs_it) { | 239 if (rhs_resource == lhs_resource) { |
249 if (*rhs_it == *lhs_it) { | 240 resources_seen.insert(resource_url); |
250 resources_seen.insert(resource); | |
251 break; | 241 break; |
252 } | 242 } |
253 } | 243 } |
254 EXPECT_TRUE(base::ContainsKey(resources_seen, resource)); | 244 EXPECT_TRUE(base::ContainsKey(resources_seen, resource_url)); |
255 } | 245 } |
256 EXPECT_EQ(lhs.size(), resources_seen.size()); | 246 EXPECT_EQ(lhs.size(), resources_seen.size()); |
257 } | 247 } |
258 | 248 |
259 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, | 249 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, |
260 const std::string& key) const { | 250 const std::string& key) const { |
261 PrefetchDataMap::const_iterator it = test_url_data_.find(key); | 251 PrefetchDataMap::const_iterator it = test_url_data_.find(key); |
262 if (it != test_url_data_.end()) { | 252 if (it != test_url_data_.end()) { |
263 m->insert(std::make_pair(it->first, it->second)); | 253 m->insert(std::make_pair(it->first, it->second)); |
264 return; | 254 return; |
265 } | 255 } |
266 it = test_host_data_.find(key); | 256 it = test_host_data_.find(key); |
267 ASSERT_TRUE(it != test_host_data_.end()); | 257 ASSERT_TRUE(it != test_host_data_.end()); |
268 m->insert(std::make_pair(it->first, it->second)); | 258 m->insert(std::make_pair(it->first, it->second)); |
269 } | 259 } |
270 | 260 |
271 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 261 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
272 { // Url data. | 262 { // Url data. |
273 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); | 263 PrefetchData google(PREFETCH_KEY_TYPE_URL, "http://www.google.com"); |
274 google.last_visit = base::Time::FromInternalValue(1); | 264 google.last_visit = base::Time::FromInternalValue(1); |
| 265 google.resources.push_back(MakeResourceData( |
| 266 "http://www.google.com/style.css", content::RESOURCE_TYPE_STYLESHEET, 5, |
| 267 2, 1, 1.1, net::MEDIUM, false, false)); |
| 268 google.resources.push_back(MakeResourceData( |
| 269 "http://www.google.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, 0, |
| 270 1, 2.1, net::MEDIUM, false, false)); |
| 271 google.resources.push_back(MakeResourceData( |
| 272 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, |
| 273 0, 2.2, net::MEDIUM, false, false)); |
| 274 google.resources.push_back(MakeResourceData( |
| 275 "http://www.google.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, 0, |
| 276 0, 5.1, net::MEDIUM, false, false)); |
275 google.resources.push_back( | 277 google.resources.push_back( |
276 ResourceRow(std::string(), "http://www.google.com/style.css", | 278 MakeResourceData("http://www.resources.google.com/script.js", |
277 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, | 279 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
278 net::MEDIUM, false, false)); | 280 net::MEDIUM, false, false)); |
279 google.resources.push_back(ResourceRow(std::string(), | |
280 "http://www.google.com/script.js", | |
281 content::RESOURCE_TYPE_SCRIPT, 4, 0, | |
282 1, 2.1, net::MEDIUM, false, false)); | |
283 google.resources.push_back(ResourceRow( | |
284 std::string(), "http://www.google.com/image.png", | |
285 content::RESOURCE_TYPE_IMAGE, 6, 3, 0, 2.2, net::MEDIUM, false, false)); | |
286 google.resources.push_back( | |
287 ResourceRow(std::string(), "http://www.google.com/a.font", | |
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, | |
293 false, false)); | |
294 | 281 |
295 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); | 282 PrefetchData reddit(PREFETCH_KEY_TYPE_URL, "http://www.reddit.com"); |
296 reddit.last_visit = base::Time::FromInternalValue(2); | 283 reddit.last_visit = base::Time::FromInternalValue(2); |
297 reddit.resources.push_back( | 284 reddit.resources.push_back(MakeResourceData( |
298 ResourceRow(std::string(), "http://reddit-resource.com/script1.js", | 285 "http://reddit-resource.com/script1.js", content::RESOURCE_TYPE_SCRIPT, |
299 content::RESOURCE_TYPE_SCRIPT, 4, 0, 1, 1.0, net::MEDIUM, | 286 4, 0, 1, 1.0, net::MEDIUM, false, false)); |
300 false, false)); | 287 reddit.resources.push_back(MakeResourceData( |
301 reddit.resources.push_back( | 288 "http://reddit-resource.com/script2.js", content::RESOURCE_TYPE_SCRIPT, |
302 ResourceRow(std::string(), "http://reddit-resource.com/script2.js", | 289 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 | 290 |
306 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); | 291 PrefetchData yahoo(PREFETCH_KEY_TYPE_URL, "http://www.yahoo.com"); |
307 yahoo.last_visit = base::Time::FromInternalValue(3); | 292 yahoo.last_visit = base::Time::FromInternalValue(3); |
308 yahoo.resources.push_back(ResourceRow(std::string(), | 293 yahoo.resources.push_back(MakeResourceData( |
309 "http://www.google.com/image.png", | 294 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, |
310 content::RESOURCE_TYPE_IMAGE, 20, 1, | 295 0, 10.0, net::MEDIUM, false, false)); |
311 0, 10.0, net::MEDIUM, false, false)); | |
312 | 296 |
313 test_url_data_.clear(); | 297 test_url_data_.clear(); |
314 test_url_data_.insert(std::make_pair("http://www.google.com", google)); | 298 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)); | 299 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)); | 300 test_url_data_.insert(std::make_pair("http://www.yahoo.com", yahoo)); |
317 | 301 |
318 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); | 302 PrefetchData empty_host_data(PREFETCH_KEY_TYPE_HOST, std::string()); |
319 tables_->UpdateData(google, empty_host_data); | 303 tables_->UpdateData(google, empty_host_data); |
320 tables_->UpdateData(reddit, empty_host_data); | 304 tables_->UpdateData(reddit, empty_host_data); |
321 tables_->UpdateData(yahoo, empty_host_data); | 305 tables_->UpdateData(yahoo, empty_host_data); |
322 } | 306 } |
323 | 307 |
324 { // Host data. | 308 { // Host data. |
325 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); | 309 PrefetchData facebook(PREFETCH_KEY_TYPE_HOST, "www.facebook.com"); |
326 facebook.last_visit = base::Time::FromInternalValue(4); | 310 facebook.last_visit = base::Time::FromInternalValue(4); |
| 311 facebook.resources.push_back(MakeResourceData( |
| 312 "http://www.facebook.com/style.css", content::RESOURCE_TYPE_STYLESHEET, |
| 313 5, 2, 1, 1.1, net::MEDIUM, false, false)); |
| 314 facebook.resources.push_back(MakeResourceData( |
| 315 "http://www.facebook.com/script.js", content::RESOURCE_TYPE_SCRIPT, 4, |
| 316 0, 1, 2.1, net::MEDIUM, false, false)); |
| 317 facebook.resources.push_back(MakeResourceData( |
| 318 "http://www.facebook.com/image.png", content::RESOURCE_TYPE_IMAGE, 6, 3, |
| 319 0, 2.2, net::MEDIUM, false, false)); |
| 320 facebook.resources.push_back(MakeResourceData( |
| 321 "http://www.facebook.com/a.font", content::RESOURCE_TYPE_LAST_TYPE, 2, |
| 322 0, 0, 5.1, net::MEDIUM, false, false)); |
327 facebook.resources.push_back( | 323 facebook.resources.push_back( |
328 ResourceRow(std::string(), "http://www.facebook.com/style.css", | 324 MakeResourceData("http://www.resources.facebook.com/script.js", |
329 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, | 325 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 8.5, |
330 net::MEDIUM, false, false)); | 326 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)); | |
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 | 327 |
347 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); | 328 PrefetchData yahoo(PREFETCH_KEY_TYPE_HOST, "www.yahoo.com"); |
348 yahoo.last_visit = base::Time::FromInternalValue(5); | 329 yahoo.last_visit = base::Time::FromInternalValue(5); |
349 yahoo.resources.push_back(ResourceRow(std::string(), | 330 yahoo.resources.push_back(MakeResourceData( |
350 "http://www.google.com/image.png", | 331 "http://www.google.com/image.png", content::RESOURCE_TYPE_IMAGE, 20, 1, |
351 content::RESOURCE_TYPE_IMAGE, 20, 1, | 332 0, 10.0, net::MEDIUM, false, false)); |
352 0, 10.0, net::MEDIUM, false, false)); | |
353 | 333 |
354 test_host_data_.clear(); | 334 test_host_data_.clear(); |
355 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); | 335 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); |
356 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); | 336 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); |
357 | 337 |
358 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); | 338 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); |
359 tables_->UpdateData(empty_url_data, facebook); | 339 tables_->UpdateData(empty_url_data, facebook); |
360 tables_->UpdateData(empty_url_data, yahoo); | 340 tables_->UpdateData(empty_url_data, yahoo); |
361 } | 341 } |
362 } | 342 } |
363 | 343 |
364 // Test cases. | 344 // Test cases. |
365 | 345 |
366 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { | 346 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { |
367 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; | 347 ResourceData js_resource = MakeResourceData( |
368 ResourceRow js_resource( | 348 "http://www.resources.google.com/script.js", |
369 std::string(), "http://www.resources.google.com/script.js", | |
370 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); | 349 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); |
371 ResourceRow image_resource( | 350 ResourceData image_resource = MakeResourceData( |
372 std::string(), "http://www.resources.google.com/image.jpg", | 351 "http://www.resources.google.com/image.jpg", content::RESOURCE_TYPE_IMAGE, |
373 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM, false, false); | 352 11, 0, 0, 1., net::MEDIUM, false, false); |
374 ResourceRow css_resource(std::string(), | 353 ResourceData css_resource = |
375 "http://www.resources.google.com/stylesheet.css", | 354 MakeResourceData("http://www.resources.google.com/stylesheet.css", |
376 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., | 355 content::RESOURCE_TYPE_STYLESHEET, 11, 0, 0, 1., |
377 net::MEDIUM, false, false); | 356 net::MEDIUM, false, false); |
378 ResourceRow font_resource(std::string(), | 357 ResourceData font_resource = |
379 "http://www.resources.google.com/font.woff", | 358 MakeResourceData("http://www.resources.google.com/font.woff", |
380 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., | 359 content::RESOURCE_TYPE_FONT_RESOURCE, 11, 0, 0, 1., |
381 net::MEDIUM, false, false); | 360 net::MEDIUM, false, false); |
382 EXPECT_TRUE(js_resource.score == css_resource.score); | 361 EXPECT_TRUE(js_resource.score() == css_resource.score()); |
383 EXPECT_TRUE(js_resource.score == font_resource.score); | 362 EXPECT_TRUE(js_resource.score() == font_resource.score()); |
384 EXPECT_NEAR(199., js_resource.score, 1e-4); | 363 EXPECT_NEAR(199., js_resource.score(), 1e-4); |
385 EXPECT_NEAR(99., image_resource.score, 1e-4); | 364 EXPECT_NEAR(99., image_resource.score(), 1e-4); |
386 } | 365 } |
387 | 366 |
388 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { | 367 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllData) { |
389 TestGetAllData(); | 368 TestGetAllData(); |
390 } | 369 } |
391 | 370 |
392 TEST_F(ResourcePrefetchPredictorTablesTest, UpdateData) { | 371 TEST_F(ResourcePrefetchPredictorTablesTest, UpdateData) { |
393 TestUpdateData(); | 372 TestUpdateData(); |
394 } | 373 } |
395 | 374 |
(...skipping 23 matching lines...) Expand all Loading... |
419 | 398 |
420 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 399 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
421 TestDeleteSingleDataPoint(); | 400 TestDeleteSingleDataPoint(); |
422 } | 401 } |
423 | 402 |
424 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 403 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
425 TestDeleteAllData(); | 404 TestDeleteAllData(); |
426 } | 405 } |
427 | 406 |
428 } // namespace predictors | 407 } // namespace predictors |
OLD | NEW |