OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/predictors/autocomplete_action_predictor.h" | 5 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
14 #include "chrome/browser/history/history.h" | 14 #include "chrome/browser/history/history.h" |
15 #include "chrome/browser/history/in_memory_database.h" | 15 #include "chrome/browser/history/in_memory_database.h" |
16 #include "chrome/browser/history/url_database.h" | 16 #include "chrome/browser/history/url_database.h" |
17 #include "chrome/browser/prerender/prerender_field_trial.h" | 17 #include "chrome/browser/prerender/prerender_field_trial.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/guid.h" | 19 #include "chrome/common/guid.h" |
20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
21 #include "content/test/test_browser_thread.h" | 21 #include "content/test/test_browser_thread.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 using predictors::AutocompleteActionPredictor; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 struct TestUrlInfo { | 29 struct TestUrlInfo { |
29 GURL url; | 30 GURL url; |
30 string16 title; | 31 string16 title; |
31 int days_from_now; | 32 int days_from_now; |
32 string16 user_text; | 33 string16 user_text; |
33 int number_of_hits; | 34 int number_of_hits; |
34 int number_of_misses; | 35 int number_of_misses; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 ASCIIToUTF16("just a test"), 2, 0, | 68 ASCIIToUTF16("just a test"), 2, 0, |
68 AutocompleteActionPredictor::ACTION_NONE }, | 69 AutocompleteActionPredictor::ACTION_NONE }, |
69 { GURL("http://www.testsite.com/i.html"), | 70 { GURL("http://www.testsite.com/i.html"), |
70 ASCIIToUTF16("Test - site - just a test"), 28, | 71 ASCIIToUTF16("Test - site - just a test"), 28, |
71 ASCIIToUTF16("just a test"), 2, 0, | 72 ASCIIToUTF16("just a test"), 2, 0, |
72 AutocompleteActionPredictor::ACTION_NONE } | 73 AutocompleteActionPredictor::ACTION_NONE } |
73 }; | 74 }; |
74 | 75 |
75 } // end namespace | 76 } // end namespace |
76 | 77 |
| 78 namespace predictors { |
| 79 |
77 class AutocompleteActionPredictorTest : public testing::Test { | 80 class AutocompleteActionPredictorTest : public testing::Test { |
78 public: | 81 public: |
79 AutocompleteActionPredictorTest() | 82 AutocompleteActionPredictorTest() |
80 : loop_(MessageLoop::TYPE_DEFAULT), | 83 : loop_(MessageLoop::TYPE_DEFAULT), |
81 ui_thread_(BrowserThread::UI, &loop_), | 84 ui_thread_(BrowserThread::UI, &loop_), |
82 db_thread_(BrowserThread::DB, &loop_), | 85 db_thread_(BrowserThread::DB, &loop_), |
83 file_thread_(BrowserThread::FILE, &loop_), | 86 file_thread_(BrowserThread::FILE, &loop_), |
84 predictor_(new AutocompleteActionPredictor(&profile_)) { | 87 predictor_(new AutocompleteActionPredictor(&profile_)) { |
85 } | 88 } |
86 | 89 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 base::Time::Now() - base::TimeDelta::FromDays( | 127 base::Time::Now() - base::TimeDelta::FromDays( |
125 test_row.days_from_now); | 128 test_row.days_from_now); |
126 | 129 |
127 history::URLRow row(test_row.url); | 130 history::URLRow row(test_row.url); |
128 row.set_title(test_row.title); | 131 row.set_title(test_row.title); |
129 row.set_last_visit(visit_time); | 132 row.set_last_visit(visit_time); |
130 | 133 |
131 return url_db->AddURL(row); | 134 return url_db->AddURL(row); |
132 } | 135 } |
133 | 136 |
134 AutocompleteActionPredictorDatabase::Row CreateRowFromTestUrlInfo( | 137 AutocompleteActionPredictorTable::Row CreateRowFromTestUrlInfo( |
135 const TestUrlInfo& test_row) const { | 138 const TestUrlInfo& test_row) const { |
136 AutocompleteActionPredictorDatabase::Row row; | 139 AutocompleteActionPredictorTable::Row row; |
137 row.id = guid::GenerateGUID(); | 140 row.id = guid::GenerateGUID(); |
138 row.user_text = test_row.user_text; | 141 row.user_text = test_row.user_text; |
139 row.url = test_row.url; | 142 row.url = test_row.url; |
140 row.number_of_hits = test_row.number_of_hits; | 143 row.number_of_hits = test_row.number_of_hits; |
141 row.number_of_misses = test_row.number_of_misses; | 144 row.number_of_misses = test_row.number_of_misses; |
142 return row; | 145 return row; |
143 } | 146 } |
144 | 147 |
145 void AddAllRows() { | 148 void AddAllRows() { |
146 for (size_t i = 0; i < arraysize(test_url_db); ++i) | 149 for (size_t i = 0; i < arraysize(test_url_db); ++i) |
147 AddRow(test_url_db[i]); | 150 AddRow(test_url_db[i]); |
148 } | 151 } |
149 | 152 |
150 std::string AddRow(const TestUrlInfo& test_row) { | 153 std::string AddRow(const TestUrlInfo& test_row) { |
151 AutocompleteActionPredictor::DBCacheKey key = { test_row.user_text, | 154 AutocompleteActionPredictor::DBCacheKey key = { test_row.user_text, |
152 test_row.url }; | 155 test_row.url }; |
153 AutocompleteActionPredictorDatabase::Row row = | 156 AutocompleteActionPredictorTable::Row row = |
154 CreateRowFromTestUrlInfo(test_row); | 157 CreateRowFromTestUrlInfo(test_row); |
155 predictor_->AddRow(key, row); | 158 predictor_->AddAndUpdateRows( |
| 159 AutocompleteActionPredictorTable::Rows(1, row), |
| 160 AutocompleteActionPredictorTable::Rows()); |
156 | 161 |
157 return row.id; | 162 return row.id; |
158 } | 163 } |
159 | 164 |
160 void UpdateRow(AutocompleteActionPredictor::DBCacheKey key, | 165 void UpdateRow(const AutocompleteActionPredictorTable::Row& row) { |
161 const AutocompleteActionPredictorDatabase::Row& row) { | 166 AutocompleteActionPredictor::DBCacheKey key = { row.user_text, row.url }; |
162 AutocompleteActionPredictor::DBCacheMap::iterator it = | 167 ASSERT_TRUE(db_cache()->find(key) != db_cache()->end()); |
163 db_cache()->find(key); | 168 predictor_->AddAndUpdateRows( |
164 ASSERT_TRUE(it != db_cache()->end()); | 169 AutocompleteActionPredictorTable::Rows(), |
165 | 170 AutocompleteActionPredictorTable::Rows(1, row)); |
166 predictor_->UpdateRow(it, row); | |
167 } | 171 } |
168 | 172 |
169 void DeleteAllRows() { | 173 void DeleteAllRows() { |
170 predictor_->DeleteAllRows(); | 174 predictor_->DeleteAllRows(); |
171 } | 175 } |
172 | 176 |
173 void DeleteRowsWithURLs(const history::URLRows& rows) { | 177 void DeleteRowsWithURLs(const history::URLRows& rows) { |
174 predictor_->DeleteRowsWithURLs(rows); | 178 predictor_->DeleteRowsWithURLs(rows); |
175 } | 179 } |
176 | 180 |
177 void DeleteOldIdsFromCaches( | 181 void DeleteOldIdsFromCaches( |
178 std::vector<AutocompleteActionPredictorDatabase::Row::Id>* id_list) { | 182 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list) { |
179 HistoryService* history_service = | 183 HistoryService* history_service = |
180 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 184 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
181 ASSERT_TRUE(history_service); | 185 ASSERT_TRUE(history_service); |
182 | 186 |
183 history::URLDatabase* url_db = history_service->InMemoryDatabase(); | 187 history::URLDatabase* url_db = history_service->InMemoryDatabase(); |
184 ASSERT_TRUE(url_db); | 188 ASSERT_TRUE(url_db); |
185 | 189 |
186 predictor_->DeleteOldIdsFromCaches(url_db, id_list); | 190 predictor_->DeleteOldIdsFromCaches(url_db, id_list); |
187 } | 191 } |
188 | 192 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); | 235 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); |
232 | 236 |
233 // Get the data back out of the cache. | 237 // Get the data back out of the cache. |
234 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url }; | 238 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url }; |
235 DBCacheMap::const_iterator it = db_cache()->find(key); | 239 DBCacheMap::const_iterator it = db_cache()->find(key); |
236 EXPECT_TRUE(it != db_cache()->end()); | 240 EXPECT_TRUE(it != db_cache()->end()); |
237 | 241 |
238 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key); | 242 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key); |
239 EXPECT_TRUE(id_it != db_id_cache()->end()); | 243 EXPECT_TRUE(id_it != db_id_cache()->end()); |
240 | 244 |
241 AutocompleteActionPredictorDatabase::Row update_row; | 245 AutocompleteActionPredictorTable::Row update_row; |
242 update_row.id = id_it->second; | 246 update_row.id = id_it->second; |
243 update_row.user_text = key.user_text; | 247 update_row.user_text = key.user_text; |
244 update_row.url = key.url; | 248 update_row.url = key.url; |
245 update_row.number_of_hits = it->second.number_of_hits + 1; | 249 update_row.number_of_hits = it->second.number_of_hits + 1; |
246 update_row.number_of_misses = it->second.number_of_misses + 2; | 250 update_row.number_of_misses = it->second.number_of_misses + 2; |
247 | 251 |
248 UpdateRow(key, update_row); | 252 UpdateRow(update_row); |
249 | 253 |
250 // Get the updated version. | 254 // Get the updated version. |
251 DBCacheMap::const_iterator update_it = db_cache()->find(key); | 255 DBCacheMap::const_iterator update_it = db_cache()->find(key); |
252 EXPECT_TRUE(update_it != db_cache()->end()); | 256 EXPECT_TRUE(update_it != db_cache()->end()); |
253 | 257 |
254 EXPECT_EQ(update_row.number_of_hits, update_it->second.number_of_hits); | 258 EXPECT_EQ(update_row.number_of_hits, update_it->second.number_of_hits); |
255 EXPECT_EQ(update_row.number_of_misses, update_it->second.number_of_misses); | 259 EXPECT_EQ(update_row.number_of_misses, update_it->second.number_of_misses); |
256 | 260 |
257 DBIdCacheMap::const_iterator update_id_it = db_id_cache()->find(key); | 261 DBIdCacheMap::const_iterator update_id_it = db_id_cache()->find(key); |
258 EXPECT_TRUE(update_id_it != db_id_cache()->end()); | 262 EXPECT_TRUE(update_id_it != db_id_cache()->end()); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 for (size_t i = 0; i < arraysize(test_url_db); ++i) { | 294 for (size_t i = 0; i < arraysize(test_url_db); ++i) { |
291 DBCacheKey key = { test_url_db[i].user_text, test_url_db[i].url }; | 295 DBCacheKey key = { test_url_db[i].user_text, test_url_db[i].url }; |
292 | 296 |
293 bool deleted = (i < 2); | 297 bool deleted = (i < 2); |
294 EXPECT_EQ(deleted, db_cache()->find(key) == db_cache()->end()); | 298 EXPECT_EQ(deleted, db_cache()->find(key) == db_cache()->end()); |
295 EXPECT_EQ(deleted, db_id_cache()->find(key) == db_id_cache()->end()); | 299 EXPECT_EQ(deleted, db_id_cache()->find(key) == db_id_cache()->end()); |
296 } | 300 } |
297 } | 301 } |
298 | 302 |
299 TEST_F(AutocompleteActionPredictorTest, DeleteOldIdsFromCaches) { | 303 TEST_F(AutocompleteActionPredictorTest, DeleteOldIdsFromCaches) { |
300 std::vector<AutocompleteActionPredictorDatabase::Row::Id> expected; | 304 std::vector<AutocompleteActionPredictorTable::Row::Id> expected; |
301 std::vector<AutocompleteActionPredictorDatabase::Row::Id> all_ids; | 305 std::vector<AutocompleteActionPredictorTable::Row::Id> all_ids; |
302 | 306 |
303 for (size_t i = 0; i < arraysize(test_url_db); ++i) { | 307 for (size_t i = 0; i < arraysize(test_url_db); ++i) { |
304 std::string row_id = AddRow(test_url_db[i]); | 308 std::string row_id = AddRow(test_url_db[i]); |
305 all_ids.push_back(row_id); | 309 all_ids.push_back(row_id); |
306 | 310 |
307 bool exclude_url = StartsWithASCII(test_url_db[i].url.path(), "/d", true) || | 311 bool exclude_url = StartsWithASCII(test_url_db[i].url.path(), "/d", true) || |
308 (test_url_db[i].days_from_now > maximum_days_to_keep_entry()); | 312 (test_url_db[i].days_from_now > maximum_days_to_keep_entry()); |
309 | 313 |
310 if (exclude_url) | 314 if (exclude_url) |
311 expected.push_back(row_id); | 315 expected.push_back(row_id); |
312 else | 316 else |
313 ASSERT_TRUE(AddRowToHistory(test_url_db[i])); | 317 ASSERT_TRUE(AddRowToHistory(test_url_db[i])); |
314 } | 318 } |
315 | 319 |
316 std::vector<AutocompleteActionPredictorDatabase::Row::Id> id_list; | 320 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list; |
317 DeleteOldIdsFromCaches(&id_list); | 321 DeleteOldIdsFromCaches(&id_list); |
318 EXPECT_EQ(expected.size(), id_list.size()); | 322 EXPECT_EQ(expected.size(), id_list.size()); |
319 EXPECT_EQ(all_ids.size() - expected.size(), db_cache()->size()); | 323 EXPECT_EQ(all_ids.size() - expected.size(), db_cache()->size()); |
320 EXPECT_EQ(all_ids.size() - expected.size(), db_id_cache()->size()); | 324 EXPECT_EQ(all_ids.size() - expected.size(), db_id_cache()->size()); |
321 | 325 |
322 for (std::vector<AutocompleteActionPredictorDatabase::Row::Id>::iterator it = | 326 for (std::vector<AutocompleteActionPredictorTable::Row::Id>::iterator it = |
323 all_ids.begin(); | 327 all_ids.begin(); |
324 it != all_ids.end(); ++it) { | 328 it != all_ids.end(); ++it) { |
325 bool in_expected = | 329 bool in_expected = |
326 (std::find(expected.begin(), expected.end(), *it) != expected.end()); | 330 (std::find(expected.begin(), expected.end(), *it) != expected.end()); |
327 bool in_list = | 331 bool in_list = |
328 (std::find(id_list.begin(), id_list.end(), *it) != id_list.end()); | 332 (std::find(id_list.begin(), id_list.end(), *it) != id_list.end()); |
329 EXPECT_EQ(in_expected, in_list); | 333 EXPECT_EQ(in_expected, in_list); |
330 } | 334 } |
331 } | 335 } |
332 | 336 |
(...skipping 22 matching lines...) Expand all Loading... |
355 AutocompleteActionPredictor::Action expected_action = | 359 AutocompleteActionPredictor::Action expected_action = |
356 (test_url_db[i].expected_action == | 360 (test_url_db[i].expected_action == |
357 AutocompleteActionPredictor::ACTION_PRERENDER) ? | 361 AutocompleteActionPredictor::ACTION_PRERENDER) ? |
358 AutocompleteActionPredictor::ACTION_PRECONNECT : | 362 AutocompleteActionPredictor::ACTION_PRECONNECT : |
359 test_url_db[i].expected_action; | 363 test_url_db[i].expected_action; |
360 EXPECT_EQ(expected_action, | 364 EXPECT_EQ(expected_action, |
361 predictor()->RecommendAction(test_url_db[i].user_text, match)) | 365 predictor()->RecommendAction(test_url_db[i].user_text, match)) |
362 << "Unexpected action for " << match.destination_url; | 366 << "Unexpected action for " << match.destination_url; |
363 } | 367 } |
364 } | 368 } |
| 369 |
| 370 } // namespace predictors |
OLD | NEW |