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

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

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

Powered by Google App Engine
This is Rietveld 408576698