OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <set> | |
6 #include <utility> | |
7 #include <vector> | |
8 | |
9 #include "base/message_loop.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/predictors/predictor_database.h" | |
12 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "content/test/test_browser_thread.h" | |
15 #include "sql/statement.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace predictors { | |
19 | |
20 ResourcePrefetchPredictorTables::UrlTableRow CreateUrlTableRow( | |
dominich
2012/06/06 17:09:09
put this in an anonymous namespace
Shishir
2012/06/06 23:55:33
No longer required.
| |
21 const std::string& main_frame_url, | |
22 const std::string& resource_url, | |
23 ResourceType::Type resource_type, | |
24 int number_of_hits, | |
25 int number_of_misses, | |
26 int consecutive_misses, | |
27 double average_position) { | |
28 ResourcePrefetchPredictorTables::UrlTableRow row; | |
29 row.main_frame_url = GURL(main_frame_url); | |
30 row.resource_url = GURL(resource_url); | |
31 row.resource_type = resource_type; | |
32 row.number_of_hits = number_of_hits; | |
33 row.number_of_misses = number_of_misses; | |
34 row.consecutive_misses = consecutive_misses; | |
35 row.average_position = average_position; | |
36 row.UpdateScore(); | |
37 return row; | |
38 } | |
39 | |
40 class ResourcePrefetchPredictorTablesTest : public testing::Test { | |
41 public: | |
42 ResourcePrefetchPredictorTablesTest(); | |
43 virtual ~ResourcePrefetchPredictorTablesTest(); | |
44 | |
45 virtual void SetUp(); | |
dominich
2012/06/06 17:09:09
OVERRIDE
Shishir
2012/06/06 23:55:33
Done.
| |
46 virtual void TearDown(); | |
dominich
2012/06/06 17:09:09
OVERRIDE
Shishir
2012/06/06 23:55:33
Done.
| |
47 | |
48 protected: | |
49 void TestGetAllRows(); | |
50 void TestUpdateRowsForUrl(); | |
51 void TestDeleteRowsForUrls(); | |
52 void TestDeleteAllRows(); | |
53 | |
54 TestingProfile profile_; | |
55 scoped_ptr<PredictorDatabase> db_; | |
dominich
2012/06/06 17:09:09
can this just be an instance of a PredictorDatabas
Shishir
2012/06/06 23:55:33
I want to reset it for the ResourcePrefetchPredict
| |
56 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | |
57 MessageLoop loop_; | |
58 | |
59 private: | |
60 // Adds data to the URL table as well as test_url_rows_. | |
61 void InitializeSampleUrlData(); | |
62 | |
63 // Checks that the input vectors have the same elements, although they may | |
64 // be in different orders. | |
65 void TestUrlRowVectorsAreEqual( | |
66 const ResourcePrefetchPredictorTables::UrlTableRows& lhs, | |
67 const ResourcePrefetchPredictorTables::UrlTableRows& rhs) const; | |
68 bool UrlRowsAreEqual( | |
69 const ResourcePrefetchPredictorTables::UrlTableRow& lhs, | |
70 const ResourcePrefetchPredictorTables::UrlTableRow& rhs) const; | |
71 | |
72 content::TestBrowserThread db_thread_; | |
73 ResourcePrefetchPredictorTables::UrlTableRows test_url_rows_; | |
74 }; | |
75 | |
76 class ResourcePrefetchPredictorTablesReopenTest | |
77 : public ResourcePrefetchPredictorTablesTest { | |
78 public: | |
79 virtual void SetUp() { | |
80 // Write data to the table, and then reopen the db. | |
81 ResourcePrefetchPredictorTablesTest::SetUp(); | |
82 tables_ = NULL; | |
83 db_.reset(new PredictorDatabase(&profile_)); | |
84 loop_.RunAllPending(); | |
85 tables_ = db_->resource_prefetch_tables(); | |
86 } | |
87 }; | |
88 | |
89 ResourcePrefetchPredictorTablesTest::ResourcePrefetchPredictorTablesTest() | |
90 : loop_(MessageLoop::TYPE_DEFAULT), | |
91 db_thread_(content::BrowserThread::DB, &loop_) { | |
92 } | |
93 | |
94 ResourcePrefetchPredictorTablesTest::~ResourcePrefetchPredictorTablesTest() { | |
95 } | |
96 | |
97 void ResourcePrefetchPredictorTablesTest::SetUp() { | |
dominich
2012/06/06 17:09:09
you could do this in the constructor
Shishir
2012/06/06 23:55:33
Done
| |
98 db_.reset(new PredictorDatabase(&profile_)); | |
99 tables_ = db_->resource_prefetch_tables(); | |
100 loop_.RunAllPending(); | |
101 | |
102 tables_->DeleteAllRows(); | |
103 InitializeSampleUrlData(); | |
104 } | |
105 | |
106 void ResourcePrefetchPredictorTablesTest::TearDown() { | |
107 test_url_rows_.clear(); | |
dominich
2012/06/06 17:09:09
no need to call clear
Shishir
2012/06/06 23:55:33
Removed.
| |
108 tables_->DeleteAllRows(); | |
dominich
2012/06/06 17:09:09
you could do this in the destructor, if at all.
Shishir
2012/06/06 23:55:33
Removed.
| |
109 db_.reset(NULL); | |
dominich
2012/06/06 17:09:09
should be no need to call this reset(NULL)
Shishir
2012/06/06 23:55:33
Done.
| |
110 } | |
111 | |
112 void ResourcePrefetchPredictorTablesTest::TestGetAllRows() { | |
113 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; | |
114 tables_->GetAllRows(&actual_rows); | |
115 | |
116 TestUrlRowVectorsAreEqual(test_url_rows_, actual_rows); | |
117 } | |
118 | |
119 void ResourcePrefetchPredictorTablesTest::TestDeleteRowsForUrls() { | |
120 std::vector<GURL> urls_to_delete; | |
121 urls_to_delete.push_back(GURL("http://www.google.com")); | |
122 urls_to_delete.push_back(GURL("http://www.yahoo.com")); | |
123 | |
124 tables_->DeleteRowsForUrls(urls_to_delete); | |
125 | |
126 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; | |
127 tables_->GetAllRows(&actual_rows); | |
128 | |
129 ResourcePrefetchPredictorTables::UrlTableRows expected_rows; | |
130 expected_rows.push_back(test_url_rows_[5]); | |
131 expected_rows.push_back(test_url_rows_[6]); | |
132 | |
133 TestUrlRowVectorsAreEqual(expected_rows, actual_rows); | |
134 } | |
135 | |
136 void ResourcePrefetchPredictorTablesTest::TestUpdateRowsForUrl() { | |
137 ResourcePrefetchPredictorTables::UrlTableRows new_rows; | |
138 new_rows.push_back(CreateUrlTableRow( | |
139 "http://www.google.com", | |
140 "http://www.google.com/style.css", | |
141 ResourceType::STYLESHEET, | |
142 6, 2, 0, 1.0)); | |
143 new_rows.push_back(CreateUrlTableRow( | |
144 "http://www.google.com", | |
145 "http://www.google.com/image.png", | |
146 ResourceType::IMAGE, | |
147 6, 4, 1, 4.2)); | |
148 new_rows.push_back(CreateUrlTableRow( | |
149 "http://www.google.com", | |
150 "http://www.google.com/a.xml", | |
151 ResourceType::LAST_TYPE, | |
152 1, 0, 0, 6.1)); | |
153 new_rows.push_back(CreateUrlTableRow( | |
154 "http://www.google.com", | |
155 "http://www.resources.google.com/script.js", | |
156 ResourceType::SCRIPT, | |
157 12, 0, 0, 8.5)); | |
158 | |
159 tables_->UpdateRowsForUrl(GURL("http://www.google.com"), new_rows); | |
160 | |
161 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; | |
162 tables_->GetAllRows(&actual_rows); | |
163 | |
164 ResourcePrefetchPredictorTables::UrlTableRows expected_rows(new_rows); | |
165 expected_rows.push_back(test_url_rows_[5]); | |
166 expected_rows.push_back(test_url_rows_[6]); | |
167 expected_rows.push_back(test_url_rows_[7]); | |
168 | |
169 TestUrlRowVectorsAreEqual(expected_rows, actual_rows); | |
170 } | |
171 | |
172 void ResourcePrefetchPredictorTablesTest::TestDeleteAllRows() { | |
173 tables_->DeleteAllRows(); | |
174 | |
175 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; | |
176 tables_->GetAllRows(&actual_rows); | |
177 EXPECT_EQ(0, static_cast<int>(actual_rows.size())); | |
178 } | |
179 | |
180 void ResourcePrefetchPredictorTablesTest::TestUrlRowVectorsAreEqual( | |
181 const ResourcePrefetchPredictorTables::UrlTableRows& lhs, | |
182 const ResourcePrefetchPredictorTables::UrlTableRows& rhs) const { | |
183 EXPECT_EQ(lhs.size(), rhs.size()); | |
184 | |
185 std::set<std::pair<GURL, GURL> > resources_seen; | |
186 for (int i = 0; i < static_cast<int>(rhs.size()); ++i) { | |
187 std::pair<GURL, GURL> resource_id = std::make_pair(rhs[i].main_frame_url, | |
188 rhs[i].resource_url); | |
189 EXPECT_EQ(resources_seen.find(resource_id), resources_seen.end()); | |
190 | |
191 for (int j = 0; j < static_cast<int>(lhs.size()); ++j) { | |
192 if (UrlRowsAreEqual(rhs[i], lhs[j])) { | |
193 resources_seen.insert(resource_id); | |
194 break; | |
195 } | |
196 } | |
197 EXPECT_NE(resources_seen.find(resource_id), resources_seen.end()); | |
198 } | |
199 EXPECT_EQ(lhs.size(), resources_seen.size()); | |
200 } | |
201 | |
202 bool ResourcePrefetchPredictorTablesTest::UrlRowsAreEqual( | |
203 const ResourcePrefetchPredictorTables::UrlTableRow& lhs, | |
204 const ResourcePrefetchPredictorTables::UrlTableRow& rhs) const { | |
205 return lhs.main_frame_url == rhs.main_frame_url && | |
206 lhs.resource_url == rhs.resource_url && | |
207 lhs.resource_type == rhs.resource_type && | |
208 lhs.number_of_hits == rhs.number_of_hits && | |
209 lhs.number_of_misses == rhs.number_of_misses && | |
210 lhs.consecutive_misses == rhs.consecutive_misses && | |
211 lhs.average_position == rhs.average_position && | |
212 lhs.score == rhs.score; | |
213 } | |
214 | |
215 void ResourcePrefetchPredictorTablesTest::InitializeSampleUrlData() { | |
dominich
2012/06/06 17:09:09
clear test_url_rows_ here
Shishir
2012/06/06 23:55:33
Done.
| |
216 test_url_rows_.push_back(CreateUrlTableRow( | |
217 "http://www.google.com", | |
218 "http://www.google.com/style.css", | |
219 ResourceType::STYLESHEET, | |
220 5, 2, 1, 1.1)); | |
221 test_url_rows_.push_back(CreateUrlTableRow( | |
222 "http://www.google.com", | |
223 "http://www.google.com/script.js", | |
224 ResourceType::SCRIPT, | |
225 4, 0, 1, 2.1)); | |
226 test_url_rows_.push_back(CreateUrlTableRow( | |
227 "http://www.google.com", | |
228 "http://www.google.com/image.png", | |
229 ResourceType::IMAGE, | |
230 6, 3, 0, 2.2)); | |
231 test_url_rows_.push_back(CreateUrlTableRow( | |
232 "http://www.google.com", | |
233 "http://www.google.com/a.font", | |
234 ResourceType::LAST_TYPE, | |
235 2, 0, 0, 5.1)); | |
236 test_url_rows_.push_back(CreateUrlTableRow( | |
237 "http://www.google.com", | |
238 "http://www.resources.google.com/script.js", | |
239 ResourceType::SCRIPT, | |
240 11, 0, 0, 8.5)); | |
241 | |
242 test_url_rows_.push_back(CreateUrlTableRow( | |
243 "http://www.reddit.com", | |
244 "http://reddit-resource.com/script1.js", | |
245 ResourceType::SCRIPT, | |
246 4, 0, 1, 1.0)); | |
247 test_url_rows_.push_back(CreateUrlTableRow( | |
248 "http://www.reddit.com", | |
249 "http://reddit-resource.com/script2.js", | |
250 ResourceType::SCRIPT, | |
251 2, 0, 0, 2.1)); | |
252 | |
253 test_url_rows_.push_back(CreateUrlTableRow( | |
254 "http://www.yahoo.com", | |
255 "http://www.google.com/image.png", | |
256 ResourceType::IMAGE, | |
257 20, 1, 0, 10.0)); | |
258 | |
259 // Add all the rows to the table. | |
260 tables_->UpdateRowsForUrl( | |
261 GURL("http://www.google.com"), | |
262 ResourcePrefetchPredictorTables::UrlTableRows( | |
263 test_url_rows_.begin(), test_url_rows_.begin() + 5)); | |
264 tables_->UpdateRowsForUrl( | |
265 GURL("http://www.reddit.com"), | |
266 ResourcePrefetchPredictorTables::UrlTableRows( | |
267 test_url_rows_.begin() + 5, test_url_rows_.begin() + 7)); | |
268 tables_->UpdateRowsForUrl( | |
269 GURL("http://www.yahoo.com"), | |
270 ResourcePrefetchPredictorTables::UrlTableRows( | |
271 test_url_rows_.begin() + 7, test_url_rows_.begin() + 8)); | |
272 } | |
273 | |
274 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllRows) { | |
275 TestGetAllRows(); | |
276 } | |
277 | |
278 TEST_F(ResourcePrefetchPredictorTablesTest, UpdateRowsForUrl) { | |
279 TestUpdateRowsForUrl(); | |
280 } | |
281 | |
282 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteRowsForUrls) { | |
283 TestDeleteRowsForUrls(); | |
284 } | |
285 | |
286 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteAllRows) { | |
287 TestDeleteAllRows(); | |
288 } | |
289 | |
290 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllRows) { | |
291 TestGetAllRows(); | |
292 } | |
293 | |
294 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateRowsForUrl) { | |
295 TestUpdateRowsForUrl(); | |
296 } | |
297 | |
298 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteRowsForUrls) { | |
299 TestDeleteRowsForUrls(); | |
300 } | |
301 | |
302 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllRows) { | |
303 TestDeleteAllRows(); | |
304 } | |
305 | |
306 } // namespace predictors | |
OLD | NEW |