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

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

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

Powered by Google App Engine
This is Rietveld 408576698