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

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

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

Powered by Google App Engine
This is Rietveld 408576698