OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <fstream> | 5 #include <fstream> |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 URLRow row(GURL(url), 0); | 132 URLRow row(GURL(url), 0); |
133 row.set_title(UTF8ToUTF16(title)); | 133 row.set_title(UTF8ToUTF16(title)); |
134 row.set_visit_count(visit_count); | 134 row.set_visit_count(visit_count); |
135 row.set_typed_count(typed_count); | 135 row.set_typed_count(typed_count); |
136 row.set_last_visit(base::Time::NowFromSystemTime() - | 136 row.set_last_visit(base::Time::NowFromSystemTime() - |
137 base::TimeDelta::FromDays(last_visit_ago)); | 137 base::TimeDelta::FromDays(last_visit_ago)); |
138 return row; | 138 return row; |
139 } | 139 } |
140 | 140 |
141 String16Vector InMemoryURLIndexTest::Make1Term(const char* term) const { | 141 String16Vector InMemoryURLIndexTest::Make1Term(const char* term) const { |
142 String16Vector terms; | 142 String16Vector original_terms; |
143 terms.push_back(UTF8ToUTF16(term)); | 143 original_terms.push_back(UTF8ToUTF16(term)); |
144 return terms; | 144 return original_terms; |
145 } | 145 } |
146 | 146 |
147 String16Vector InMemoryURLIndexTest::Make2Terms(const char* term_1, | 147 String16Vector InMemoryURLIndexTest::Make2Terms(const char* term_1, |
148 const char* term_2) const { | 148 const char* term_2) const { |
149 String16Vector terms; | 149 String16Vector original_terms; |
150 terms.push_back(UTF8ToUTF16(term_1)); | 150 original_terms.push_back(UTF8ToUTF16(term_1)); |
151 terms.push_back(UTF8ToUTF16(term_2)); | 151 original_terms.push_back(UTF8ToUTF16(term_2)); |
152 return terms; | 152 return original_terms; |
153 } | 153 } |
154 | 154 |
155 void InMemoryURLIndexTest::CheckTerm( | 155 void InMemoryURLIndexTest::CheckTerm( |
156 const InMemoryURLIndex::SearchTermCacheMap& cache, | 156 const InMemoryURLIndex::SearchTermCacheMap& cache, |
157 string16 term) const { | 157 string16 term) const { |
158 InMemoryURLIndex::SearchTermCacheMap::const_iterator cache_iter( | 158 InMemoryURLIndex::SearchTermCacheMap::const_iterator cache_iter( |
159 cache.find(term)); | 159 cache.find(term)); |
160 ASSERT_NE(cache.end(), cache_iter) | 160 ASSERT_NE(cache.end(), cache_iter) |
161 << "Cache does not contain '" << term << "' but should."; | 161 << "Cache does not contain '" << term << "' but should."; |
162 InMemoryURLIndex::SearchTermCacheItem cache_item = cache_iter->second; | 162 InMemoryURLIndex::SearchTermCacheItem cache_item = cache_iter->second; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 const size_t kMaxLen = arraysize(url_format) + 5; | 208 const size_t kMaxLen = arraysize(url_format) + 5; |
209 char url_string[kMaxLen + 1]; | 209 char url_string[kMaxLen + 1]; |
210 for (int i = 0; i < 600; ++i) { | 210 for (int i = 0; i < 600; ++i) { |
211 base::snprintf(url_string, kMaxLen, url_format, i); | 211 base::snprintf(url_string, kMaxLen, url_format, i); |
212 URLRow row(MakeURLRow(url_string, "Google Search", 20, 0, 20)); | 212 URLRow row(MakeURLRow(url_string, "Google Search", 20, 0, 20)); |
213 AddURL(row); | 213 AddURL(row); |
214 } | 214 } |
215 } | 215 } |
216 | 216 |
217 TEST_F(InMemoryURLIndexTest, Construction) { | 217 TEST_F(InMemoryURLIndexTest, Construction) { |
218 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 218 url_index_.reset(new InMemoryURLIndex(FilePath())); |
219 EXPECT_TRUE(url_index_.get()); | 219 EXPECT_TRUE(url_index_.get()); |
220 } | 220 } |
221 | 221 |
222 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { | 222 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { |
223 // Verify that the database contains the expected number of items, which | 223 // Verify that the database contains the expected number of items, which |
224 // is the pre-filtered count, i.e. all of the items. | 224 // is the pre-filtered count, i.e. all of the items. |
225 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); | 225 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); |
226 EXPECT_TRUE(statement); | 226 EXPECT_TRUE(statement); |
227 uint64 row_count = 0; | 227 uint64 row_count = 0; |
228 while (statement.Step()) ++row_count; | 228 while (statement.Step()) ++row_count; |
229 EXPECT_EQ(1U, row_count); | 229 EXPECT_EQ(1U, row_count); |
230 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 230 url_index_.reset(new InMemoryURLIndex(FilePath())); |
231 url_index_->Init("en,ja,hi,zh"); | 231 url_index_->Init(this, "en,ja,hi,zh"); |
232 url_index_->ReloadFromHistory(this); | |
233 URLIndexPrivateData& private_data(*(url_index_->private_data_)); | 232 URLIndexPrivateData& private_data(*(url_index_->private_data_)); |
234 | 233 |
235 // history_info_map_ should have the same number of items as were filtered. | 234 // history_info_map_ should have the same number of items as were filtered. |
236 EXPECT_EQ(1U, private_data.history_info_map_.size()); | 235 EXPECT_EQ(1U, private_data.history_info_map_.size()); |
237 EXPECT_EQ(35U, private_data.char_word_map_.size()); | 236 EXPECT_EQ(35U, private_data.char_word_map_.size()); |
238 EXPECT_EQ(17U, private_data.word_map_.size()); | 237 EXPECT_EQ(17U, private_data.word_map_.size()); |
239 } | 238 } |
240 | 239 |
241 TEST_F(InMemoryURLIndexTest, Retrieval) { | 240 TEST_F(InMemoryURLIndexTest, Retrieval) { |
242 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 241 url_index_.reset(new InMemoryURLIndex(FilePath())); |
243 url_index_->Init("en,ja,hi,zh"); | 242 url_index_->Init(this, "en,ja,hi,zh"); |
244 url_index_->ReloadFromHistory(this); | |
245 // The term will be lowercased by the search. | 243 // The term will be lowercased by the search. |
246 | 244 |
247 // See if a very specific term gives a single result. | 245 // See if a very specific term gives a single result. |
248 ScoredHistoryMatches matches = | 246 ScoredHistoryMatches matches = |
249 url_index_->HistoryItemsForTerms(Make1Term("DrudgeReport")); | 247 url_index_->HistoryItemsForTerms(Make1Term("DrudgeReport")); |
250 ASSERT_EQ(1U, matches.size()); | 248 ASSERT_EQ(1U, matches.size()); |
251 | 249 |
252 // Verify that we got back the result we expected. | 250 // Verify that we got back the result we expected. |
253 EXPECT_EQ(5, matches[0].url_info.id()); | 251 EXPECT_EQ(5, matches[0].url_info.id()); |
254 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec()); | 252 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec()); |
(...skipping 11 matching lines...) Expand all Loading... |
266 ASSERT_EQ(1U, matches.size()); | 264 ASSERT_EQ(1U, matches.size()); |
267 // The results should have a very high score. | 265 // The results should have a very high score. |
268 EXPECT_GT(matches[0].raw_score, 900); | 266 EXPECT_GT(matches[0].raw_score, 900); |
269 EXPECT_EQ(32, matches[0].url_info.id()); | 267 EXPECT_EQ(32, matches[0].url_info.id()); |
270 EXPECT_EQ("https://nearlyperfectresult.com/", | 268 EXPECT_EQ("https://nearlyperfectresult.com/", |
271 matches[0].url_info.url().spec()); // Note: URL gets lowercased. | 269 matches[0].url_info.url().spec()); // Note: URL gets lowercased. |
272 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"), | 270 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"), |
273 matches[0].url_info.title()); | 271 matches[0].url_info.title()); |
274 | 272 |
275 // Search which should result in very poor result. | 273 // Search which should result in very poor result. |
276 String16Vector terms; | 274 String16Vector original_terms; |
277 terms.push_back(ASCIIToUTF16("z")); | 275 original_terms.push_back(ASCIIToUTF16("z")); |
278 terms.push_back(ASCIIToUTF16("y")); | 276 original_terms.push_back(ASCIIToUTF16("y")); |
279 terms.push_back(ASCIIToUTF16("x")); | 277 original_terms.push_back(ASCIIToUTF16("x")); |
280 matches = url_index_->HistoryItemsForTerms(terms); | 278 matches = url_index_->HistoryItemsForTerms(original_terms); |
281 ASSERT_EQ(1U, matches.size()); | 279 ASSERT_EQ(1U, matches.size()); |
282 // The results should have a poor score. | 280 // The results should have a poor score. |
283 EXPECT_LT(matches[0].raw_score, 500); | 281 EXPECT_LT(matches[0].raw_score, 500); |
284 EXPECT_EQ(33, matches[0].url_info.id()); | 282 EXPECT_EQ(33, matches[0].url_info.id()); |
285 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/", | 283 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/", |
286 matches[0].url_info.url().spec()); // Note: URL gets lowercased. | 284 matches[0].url_info.url().spec()); // Note: URL gets lowercased. |
287 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"), | 285 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"), |
288 matches[0].url_info.title()); | 286 matches[0].url_info.title()); |
289 | 287 |
290 // Search which will match at the end of an URL with encoded characters. | 288 // Search which will match at the end of an URL with encoded characters. |
291 matches = url_index_->HistoryItemsForTerms(Make1Term("ice")); | 289 matches = url_index_->HistoryItemsForTerms(Make1Term("ice")); |
292 ASSERT_EQ(1U, matches.size()); | 290 ASSERT_EQ(1U, matches.size()); |
293 } | 291 } |
294 | 292 |
295 TEST_F(ExpandedInMemoryURLIndexTest, ShortCircuit) { | 293 TEST_F(ExpandedInMemoryURLIndexTest, ShortCircuit) { |
296 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 294 url_index_.reset(new InMemoryURLIndex(FilePath())); |
297 url_index_->Init("en,ja,hi,zh"); | 295 url_index_->Init(this, "en,ja,hi,zh"); |
298 url_index_->ReloadFromHistory(this); | |
299 | 296 |
300 // A search for 'w' should short-circuit and not return any matches. | 297 // A search for 'w' should short-circuit and not return any matches. |
301 ScoredHistoryMatches matches = | 298 ScoredHistoryMatches matches = |
302 url_index_->HistoryItemsForTerms(Make1Term("w")); | 299 url_index_->HistoryItemsForTerms(Make1Term("w")); |
303 EXPECT_TRUE(matches.empty()); | 300 EXPECT_TRUE(matches.empty()); |
304 | 301 |
305 // A search for 'working' should not short-circuit. | 302 // A search for 'working' should not short-circuit. |
306 matches = url_index_->HistoryItemsForTerms(Make1Term("working")); | 303 matches = url_index_->HistoryItemsForTerms(Make1Term("working")); |
307 EXPECT_EQ(1U, matches.size()); | 304 EXPECT_EQ(1U, matches.size()); |
308 } | 305 } |
309 | 306 |
310 TEST_F(InMemoryURLIndexTest, TitleSearch) { | 307 TEST_F(InMemoryURLIndexTest, TitleSearch) { |
311 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 308 url_index_.reset(new InMemoryURLIndex(FilePath())); |
312 url_index_->Init("en,ja,hi,zh"); | 309 url_index_->Init(this, "en,ja,hi,zh"); |
313 url_index_->ReloadFromHistory(this); | |
314 // Signal if someone has changed the test DB. | 310 // Signal if someone has changed the test DB. |
315 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size()); | 311 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size()); |
316 String16Vector terms; | 312 String16Vector original_terms; |
317 | 313 |
318 // Ensure title is being searched. | 314 // Ensure title is being searched. |
319 terms.push_back(ASCIIToUTF16("MORTGAGE")); | 315 original_terms.push_back(ASCIIToUTF16("MORTGAGE")); |
320 terms.push_back(ASCIIToUTF16("RATE")); | 316 original_terms.push_back(ASCIIToUTF16("RATE")); |
321 terms.push_back(ASCIIToUTF16("DROPS")); | 317 original_terms.push_back(ASCIIToUTF16("DROPS")); |
322 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms); | 318 ScoredHistoryMatches matches = |
| 319 url_index_->HistoryItemsForTerms(original_terms); |
323 ASSERT_EQ(1U, matches.size()); | 320 ASSERT_EQ(1U, matches.size()); |
324 | 321 |
325 // Verify that we got back the result we expected. | 322 // Verify that we got back the result we expected. |
326 EXPECT_EQ(1, matches[0].url_info.id()); | 323 EXPECT_EQ(1, matches[0].url_info.id()); |
327 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708", | 324 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708", |
328 matches[0].url_info.url().spec()); | 325 matches[0].url_info.url().spec()); |
329 EXPECT_EQ(ASCIIToUTF16( | 326 EXPECT_EQ(ASCIIToUTF16( |
330 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"), | 327 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"), |
331 matches[0].url_info.title()); | 328 matches[0].url_info.title()); |
332 } | 329 } |
333 | 330 |
334 TEST_F(InMemoryURLIndexTest, TitleChange) { | 331 TEST_F(InMemoryURLIndexTest, TitleChange) { |
335 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 332 url_index_.reset(new InMemoryURLIndex(FilePath())); |
336 url_index_->Init("en,ja,hi,zh"); | 333 url_index_->Init(this, "en,ja,hi,zh"); |
337 url_index_->ReloadFromHistory(this); | |
338 | 334 |
339 // Verify current title terms retrieves desired item. | 335 // Verify current title terms retrieves desired item. |
340 String16Vector original_terms; | 336 String16Vector original_terms; |
341 original_terms.push_back(ASCIIToUTF16("lebronomics")); | 337 original_terms.push_back(ASCIIToUTF16("lebronomics")); |
342 original_terms.push_back(ASCIIToUTF16("could")); | 338 original_terms.push_back(ASCIIToUTF16("could")); |
343 original_terms.push_back(ASCIIToUTF16("high")); | 339 original_terms.push_back(ASCIIToUTF16("high")); |
344 original_terms.push_back(ASCIIToUTF16("taxes")); | 340 original_terms.push_back(ASCIIToUTF16("taxes")); |
345 original_terms.push_back(ASCIIToUTF16("influence")); | 341 original_terms.push_back(ASCIIToUTF16("influence")); |
346 ScoredHistoryMatches matches = | 342 ScoredHistoryMatches matches = |
347 url_index_->HistoryItemsForTerms(original_terms); | 343 url_index_->HistoryItemsForTerms(original_terms); |
(...skipping 15 matching lines...) Expand all Loading... |
363 new_terms.push_back(ASCIIToUTF16("eat")); | 359 new_terms.push_back(ASCIIToUTF16("eat")); |
364 new_terms.push_back(ASCIIToUTF16("oats")); | 360 new_terms.push_back(ASCIIToUTF16("oats")); |
365 new_terms.push_back(ASCIIToUTF16("little")); | 361 new_terms.push_back(ASCIIToUTF16("little")); |
366 new_terms.push_back(ASCIIToUTF16("lambs")); | 362 new_terms.push_back(ASCIIToUTF16("lambs")); |
367 new_terms.push_back(ASCIIToUTF16("ivy")); | 363 new_terms.push_back(ASCIIToUTF16("ivy")); |
368 matches = url_index_->HistoryItemsForTerms(new_terms); | 364 matches = url_index_->HistoryItemsForTerms(new_terms); |
369 ASSERT_EQ(0U, matches.size()); | 365 ASSERT_EQ(0U, matches.size()); |
370 | 366 |
371 // Update the row. | 367 // Update the row. |
372 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy")); | 368 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy")); |
373 url_index_->UpdateURL(old_row); | 369 url_index_->UpdateURL(expected_id, old_row); |
374 | 370 |
375 // Verify we get the row using the new terms but not the original terms. | 371 // Verify we get the row using the new terms but not the original terms. |
376 matches = url_index_->HistoryItemsForTerms(new_terms); | 372 matches = url_index_->HistoryItemsForTerms(new_terms); |
377 ASSERT_EQ(1U, matches.size()); | 373 ASSERT_EQ(1U, matches.size()); |
378 EXPECT_EQ(expected_id, matches[0].url_info.id()); | 374 EXPECT_EQ(expected_id, matches[0].url_info.id()); |
379 matches = url_index_->HistoryItemsForTerms(original_terms); | 375 matches = url_index_->HistoryItemsForTerms(original_terms); |
380 ASSERT_EQ(0U, matches.size()); | 376 ASSERT_EQ(0U, matches.size()); |
381 } | 377 } |
382 | 378 |
383 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) { | 379 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) { |
384 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 380 url_index_.reset(new InMemoryURLIndex(FilePath())); |
385 url_index_->Init("en,ja,hi,zh"); | 381 url_index_->Init(this, "en,ja,hi,zh"); |
386 url_index_->ReloadFromHistory(this); | |
387 | 382 |
388 // The presence of duplicate characters should succeed. Exercise by cycling | 383 // The presence of duplicate characters should succeed. Exercise by cycling |
389 // through a string with several duplicate characters. | 384 // through a string with several duplicate characters. |
390 ScoredHistoryMatches matches = | 385 ScoredHistoryMatches matches = |
391 url_index_->HistoryItemsForTerms(Make1Term("ABRA")); | 386 url_index_->HistoryItemsForTerms(Make1Term("ABRA")); |
392 ASSERT_EQ(1U, matches.size()); | 387 ASSERT_EQ(1U, matches.size()); |
393 EXPECT_EQ(28, matches[0].url_info.id()); | 388 EXPECT_EQ(28, matches[0].url_info.id()); |
394 EXPECT_EQ("http://www.ddj.com/windows/184416623", | 389 EXPECT_EQ("http://www.ddj.com/windows/184416623", |
395 matches[0].url_info.url().spec()); | 390 matches[0].url_info.url().spec()); |
396 | 391 |
(...skipping 14 matching lines...) Expand all Loading... |
411 EXPECT_EQ(28, matches[0].url_info.id()); | 406 EXPECT_EQ(28, matches[0].url_info.id()); |
412 } | 407 } |
413 | 408 |
414 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { | 409 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { |
415 // Verify that match results for previously typed characters are retained | 410 // Verify that match results for previously typed characters are retained |
416 // (in the term_char_word_set_cache_) and reused, if possible, in future | 411 // (in the term_char_word_set_cache_) and reused, if possible, in future |
417 // autocompletes. | 412 // autocompletes. |
418 typedef InMemoryURLIndex::SearchTermCacheMap::iterator CacheIter; | 413 typedef InMemoryURLIndex::SearchTermCacheMap::iterator CacheIter; |
419 typedef InMemoryURLIndex::SearchTermCacheItem CacheItem; | 414 typedef InMemoryURLIndex::SearchTermCacheItem CacheItem; |
420 | 415 |
421 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 416 url_index_.reset(new InMemoryURLIndex(FilePath())); |
422 url_index_->Init("en,ja,hi,zh"); | 417 url_index_->Init(this, "en,ja,hi,zh"); |
423 url_index_->ReloadFromHistory(this); | |
424 | 418 |
425 InMemoryURLIndex::SearchTermCacheMap& cache(url_index_->search_term_cache_); | 419 InMemoryURLIndex::SearchTermCacheMap& cache(url_index_->search_term_cache_); |
426 | 420 |
427 // The cache should be empty at this point. | 421 // The cache should be empty at this point. |
428 EXPECT_EQ(0U, cache.size()); | 422 EXPECT_EQ(0U, cache.size()); |
429 | 423 |
430 // Now simulate typing search terms into the omnibox and check the state of | 424 // Now simulate typing search terms into the omnibox and check the state of |
431 // the cache as each item is 'typed'. | 425 // the cache as each item is 'typed'. |
432 | 426 |
433 // Simulate typing "r" giving "r" in the simulated omnibox. The results for | 427 // Simulate typing "r" giving "r" in the simulated omnibox. The results for |
434 // 'r' will be not cached because it is only 1 character long. | 428 // 'r' will be not cached because it is only 1 character long. |
435 String16Vector terms; | 429 String16Vector original_terms; |
436 string16 term_r = ASCIIToUTF16("r"); | 430 string16 term_r = ASCIIToUTF16("r"); |
437 terms.push_back(term_r); | 431 original_terms.push_back(term_r); |
438 url_index_->HistoryItemsForTerms(terms); | 432 url_index_->HistoryItemsForTerms(original_terms); |
439 EXPECT_EQ(0U, cache.size()); | 433 EXPECT_EQ(0U, cache.size()); |
440 | 434 |
441 // Simulate typing "re" giving "r re" in the simulated omnibox. | 435 // Simulate typing "re" giving "r re" in the simulated omnibox. |
442 string16 term_re = ASCIIToUTF16("re"); | 436 string16 term_re = ASCIIToUTF16("re"); |
443 terms.push_back(term_re); | 437 original_terms.push_back(term_re); |
444 // 're' should be cached at this point but not 'r' as it is a single | 438 // 're' should be cached at this point but not 'r' as it is a single |
445 // character. | 439 // character. |
446 ASSERT_EQ(2U, terms.size()); | 440 ASSERT_EQ(2U, original_terms.size()); |
447 url_index_->HistoryItemsForTerms(terms); | 441 url_index_->HistoryItemsForTerms(original_terms); |
448 ASSERT_EQ(1U, cache.size()); | 442 ASSERT_EQ(1U, cache.size()); |
449 CheckTerm(cache, term_re); | 443 CheckTerm(cache, term_re); |
450 | 444 |
451 // Simulate typing "reco" giving "r re reco" in the simulated omnibox. | 445 // Simulate typing "reco" giving "r re reco" in the simulated omnibox. |
452 string16 term_reco = ASCIIToUTF16("reco"); | 446 string16 term_reco = ASCIIToUTF16("reco"); |
453 terms.push_back(term_reco); | 447 original_terms.push_back(term_reco); |
454 // 're' and 'reco' should be cached at this point but not 'r' as it is a | 448 // 're' and 'reco' should be cached at this point but not 'r' as it is a |
455 // single character. | 449 // single character. |
456 url_index_->HistoryItemsForTerms(terms); | 450 url_index_->HistoryItemsForTerms(original_terms); |
457 ASSERT_EQ(2U, cache.size()); | 451 ASSERT_EQ(2U, cache.size()); |
458 CheckTerm(cache, term_re); | 452 CheckTerm(cache, term_re); |
459 CheckTerm(cache, term_reco); | 453 CheckTerm(cache, term_reco); |
460 | 454 |
461 terms.clear(); // Simulate pressing <ESC>. | 455 original_terms.clear(); // Simulate pressing <ESC>. |
462 | 456 |
463 // Simulate typing "mort". | 457 // Simulate typing "mort". |
464 string16 term_mort = ASCIIToUTF16("mort"); | 458 string16 term_mort = ASCIIToUTF16("mort"); |
465 terms.push_back(term_mort); | 459 original_terms.push_back(term_mort); |
466 // Since we now have only one search term, the cached results for 're' and | 460 // Since we now have only one search term, the cached results for 're' and |
467 // 'reco' should be purged, giving us only 1 item in the cache (for 'mort'). | 461 // 'reco' should be purged, giving us only 1 item in the cache (for 'mort'). |
468 url_index_->HistoryItemsForTerms(terms); | 462 url_index_->HistoryItemsForTerms(original_terms); |
469 ASSERT_EQ(1U, cache.size()); | 463 ASSERT_EQ(1U, cache.size()); |
470 CheckTerm(cache, term_mort); | 464 CheckTerm(cache, term_mort); |
471 | 465 |
472 // Simulate typing "reco" giving "mort reco" in the simulated omnibox. | 466 // Simulate typing "reco" giving "mort reco" in the simulated omnibox. |
473 terms.push_back(term_reco); | 467 original_terms.push_back(term_reco); |
474 url_index_->HistoryItemsForTerms(terms); | 468 url_index_->HistoryItemsForTerms(original_terms); |
475 ASSERT_EQ(2U, cache.size()); | 469 ASSERT_EQ(2U, cache.size()); |
476 CheckTerm(cache, term_mort); | 470 CheckTerm(cache, term_mort); |
477 CheckTerm(cache, term_reco); | 471 CheckTerm(cache, term_reco); |
478 | 472 |
479 // Simulate a <DELETE> by removing the 'reco' and adding back the 'rec'. | 473 // Simulate a <DELETE> by removing the 'reco' and adding back the 'rec'. |
480 terms.resize(terms.size() - 1); | 474 original_terms.resize(original_terms.size() - 1); |
481 string16 term_rec = ASCIIToUTF16("rec"); | 475 string16 term_rec = ASCIIToUTF16("rec"); |
482 terms.push_back(term_rec); | 476 original_terms.push_back(term_rec); |
483 url_index_->HistoryItemsForTerms(terms); | 477 url_index_->HistoryItemsForTerms(original_terms); |
484 ASSERT_EQ(2U, cache.size()); | 478 ASSERT_EQ(2U, cache.size()); |
485 CheckTerm(cache, term_mort); | 479 CheckTerm(cache, term_mort); |
486 CheckTerm(cache, term_rec); | 480 CheckTerm(cache, term_rec); |
487 } | 481 } |
488 | 482 |
489 TEST_F(InMemoryURLIndexTest, Scoring) { | 483 TEST_F(InMemoryURLIndexTest, Scoring) { |
490 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 3, 30, 1)); | 484 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 3, 30, 1)); |
491 // Test scores based on position. | 485 // Test scores based on position. |
492 ScoredHistoryMatch scored_a( | 486 ScoredHistoryMatch scored_a( |
493 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("abc"))); | 487 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("abc"))); |
(...skipping 21 matching lines...) Expand all Loading... |
515 InMemoryURLIndex::ScoredMatchForURL(row_c, Make1Term("abc"))); | 509 InMemoryURLIndex::ScoredMatchForURL(row_c, Make1Term("abc"))); |
516 EXPECT_GT(scored_g.raw_score, scored_a.raw_score); | 510 EXPECT_GT(scored_g.raw_score, scored_a.raw_score); |
517 // Test scores based on typed_count. | 511 // Test scores based on typed_count. |
518 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); | 512 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); |
519 ScoredHistoryMatch scored_h( | 513 ScoredHistoryMatch scored_h( |
520 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc"))); | 514 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc"))); |
521 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); | 515 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); |
522 } | 516 } |
523 | 517 |
524 TEST_F(InMemoryURLIndexTest, AddNewRows) { | 518 TEST_F(InMemoryURLIndexTest, AddNewRows) { |
525 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 519 url_index_.reset(new InMemoryURLIndex(FilePath())); |
526 url_index_->Init("en,ja,hi,zh"); | 520 url_index_->Init(this, "en,ja,hi,zh"); |
527 url_index_->ReloadFromHistory(this); | 521 String16Vector original_terms; |
528 String16Vector terms; | |
529 | 522 |
530 // Verify that the row we're going to add does not already exist. | 523 // Verify that the row we're going to add does not already exist. |
531 URLID new_row_id = 87654321; | 524 URLID new_row_id = 87654321; |
532 // Newly created URLRows get a last_visit time of 'right now' so it should | 525 // Newly created URLRows get a last_visit time of 'right now' so it should |
533 // qualify as a quick result candidate. | 526 // qualify as a quick result candidate. |
534 terms.push_back(ASCIIToUTF16("brokeandalone")); | 527 original_terms.push_back(ASCIIToUTF16("brokeandalone")); |
535 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty()); | 528 EXPECT_TRUE(url_index_->HistoryItemsForTerms(original_terms).empty()); |
536 | 529 |
537 // Add a new row. | 530 // Add a new row. |
538 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); | 531 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); |
539 new_row.set_last_visit(base::Time::Now()); | 532 new_row.set_last_visit(base::Time::Now()); |
540 url_index_->UpdateURL(new_row); | 533 url_index_->UpdateURL(new_row_id, new_row); |
541 | 534 |
542 // Verify that we can retrieve it. | 535 // Verify that we can retrieve it. |
543 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size()); | 536 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(original_terms).size()); |
544 | 537 |
545 // Add it again just to be sure that is harmless. | 538 // Add it again just to be sure that is harmless. |
546 url_index_->UpdateURL(new_row); | 539 url_index_->UpdateURL(new_row_id, new_row); |
547 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size()); | 540 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(original_terms).size()); |
548 } | 541 } |
549 | 542 |
550 TEST_F(InMemoryURLIndexTest, DeleteRows) { | 543 TEST_F(InMemoryURLIndexTest, DeleteRows) { |
551 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 544 url_index_.reset(new InMemoryURLIndex(FilePath())); |
552 url_index_->Init("en,ja,hi,zh"); | 545 url_index_->Init(this, "en,ja,hi,zh"); |
553 url_index_->ReloadFromHistory(this); | 546 String16Vector original_terms; |
554 String16Vector terms; | |
555 | 547 |
556 // Make sure we actually get an existing result. | 548 // Make sure we actually get an existing result. |
557 terms.push_back(ASCIIToUTF16("DrudgeReport")); | 549 original_terms.push_back(ASCIIToUTF16("DrudgeReport")); |
558 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms); | 550 ScoredHistoryMatches matches = |
| 551 url_index_->HistoryItemsForTerms(original_terms); |
559 ASSERT_EQ(1U, matches.size()); | 552 ASSERT_EQ(1U, matches.size()); |
560 | 553 |
561 // Determine the row id for that result, delete that id, then search again. | 554 // Determine the row id for that result, delete that id, then search again. |
562 url_index_->DeleteURL(matches[0].url_info); | 555 url_index_->DeleteURL(matches[0].url_info.id()); |
563 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty()); | 556 EXPECT_TRUE(url_index_->HistoryItemsForTerms(original_terms).empty()); |
564 } | 557 } |
565 | 558 |
566 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { | 559 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { |
567 struct TestData { | 560 struct TestData { |
568 const std::string url_spec; | 561 const std::string url_spec; |
569 const bool expected_is_whitelisted; | 562 const bool expected_is_whitelisted; |
570 } data[] = { | 563 } data[] = { |
571 // URLs with whitelisted schemes. | 564 // URLs with whitelisted schemes. |
572 { "about:histograms", true }, | 565 { "about:histograms", true }, |
573 { "chrome://settings", true }, | 566 { "chrome://settings", true }, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false }, | 622 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false }, |
630 { "tftp://example.com/mystartupfile", false }, | 623 { "tftp://example.com/mystartupfile", false }, |
631 { "tip://123.123.123.123/?urn:xopen:xid", false }, | 624 { "tip://123.123.123.123/?urn:xopen:xid", false }, |
632 { "tv:nbc.com", false }, | 625 { "tv:nbc.com", false }, |
633 { "urn:foo:A123,456", false }, | 626 { "urn:foo:A123,456", false }, |
634 { "vemmi://zeus.mctel.fr/demo", false }, | 627 { "vemmi://zeus.mctel.fr/demo", false }, |
635 { "wais://www.mydomain.net:8765/mydatabase", false }, | 628 { "wais://www.mydomain.net:8765/mydatabase", false }, |
636 { "xmpp:node@example.com", false }, | 629 { "xmpp:node@example.com", false }, |
637 { "xmpp://guest@example.com", false }, | 630 { "xmpp://guest@example.com", false }, |
638 }; | 631 }; |
639 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 632 url_index_.reset(new InMemoryURLIndex(FilePath())); |
640 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 633 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
641 GURL url(data[i].url_spec); | 634 GURL url(data[i].url_spec); |
642 EXPECT_EQ(data[i].expected_is_whitelisted, | 635 EXPECT_EQ(data[i].expected_is_whitelisted, |
643 url_index_->URLSchemeIsWhitelisted(url)); | 636 url_index_->URLSchemeIsWhitelisted(url)); |
644 } | 637 } |
645 } | 638 } |
646 | 639 |
647 TEST_F(InMemoryURLIndexTest, CacheFilePath) { | 640 TEST_F(InMemoryURLIndexTest, CacheFilePath) { |
648 url_index_.reset(new InMemoryURLIndex( | 641 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL( |
649 NULL, FilePath(FILE_PATH_LITERAL("/flammmy/frammy/")))); | 642 "/flammmy/frammy/")))); |
650 FilePath full_file_path; | 643 FilePath full_file_path; |
651 url_index_->GetCacheFilePath(&full_file_path); | 644 url_index_->GetCacheFilePath(&full_file_path); |
652 std::vector<FilePath::StringType> expected_parts; | 645 std::vector<FilePath::StringType> expected_parts; |
653 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")). | 646 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")). |
654 GetComponents(&expected_parts); | 647 GetComponents(&expected_parts); |
655 std::vector<FilePath::StringType> actual_parts; | 648 std::vector<FilePath::StringType> actual_parts; |
656 full_file_path.GetComponents(&actual_parts); | 649 full_file_path.GetComponents(&actual_parts); |
657 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 650 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
658 size_t count = expected_parts.size(); | 651 size_t count = expected_parts.size(); |
659 for (size_t i = 0; i < count; ++i) | 652 for (size_t i = 0; i < count; ++i) |
660 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 653 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
661 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 654 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
662 url_index_->history_dir_.clear(); | 655 url_index_->history_dir_.clear(); |
663 } | 656 } |
664 | 657 |
665 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { | 658 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { |
666 // Save the cache to a protobuf, restore it, and compare the results. | 659 // Save the cache to a protobuf, restore it, and compare the results. |
667 url_index_.reset(new InMemoryURLIndex(NULL, FilePath())); | 660 url_index_.reset(new InMemoryURLIndex(FilePath())); |
668 InMemoryURLIndex& url_index(*(url_index_.get())); | 661 InMemoryURLIndex& url_index(*(url_index_.get())); |
669 url_index.Init("en,ja,hi,zh"); | 662 url_index.Init(this, "en,ja,hi,zh"); |
670 url_index_->ReloadFromHistory(this); | |
671 in_memory_url_index::InMemoryURLIndexCacheItem index_cache; | 663 in_memory_url_index::InMemoryURLIndexCacheItem index_cache; |
672 url_index.SavePrivateData(&index_cache); | 664 url_index.SavePrivateData(&index_cache); |
673 | 665 |
674 // Capture our private data so we can later compare for equality. | 666 // Capture our private data so we can later compare for equality. |
675 URLIndexPrivateData& private_data(*(url_index_->private_data_)); | 667 URLIndexPrivateData& private_data(*(url_index_->private_data_)); |
676 String16Vector word_list(private_data.word_list_); | 668 String16Vector word_list(private_data.word_list_); |
677 WordMap word_map(private_data.word_map_); | 669 WordMap word_map(private_data.word_map_); |
678 CharWordIDMap char_word_map(private_data.char_word_map_); | 670 CharWordIDMap char_word_map(private_data.char_word_map_); |
679 WordIDHistoryMap word_id_history_map(private_data.word_id_history_map_); | 671 WordIDHistoryMap word_id_history_map(private_data.word_id_history_map_); |
680 HistoryIDWordMap history_id_word_map(private_data.history_id_word_map_); | 672 HistoryIDWordMap history_id_word_map(private_data.history_id_word_map_); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 const URLRow& expected_row(expected->second); | 725 const URLRow& expected_row(expected->second); |
734 const URLRow& actual_row(actual->second); | 726 const URLRow& actual_row(actual->second); |
735 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); | 727 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); |
736 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); | 728 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); |
737 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); | 729 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); |
738 EXPECT_EQ(expected_row.url(), actual_row.url()); | 730 EXPECT_EQ(expected_row.url(), actual_row.url()); |
739 } | 731 } |
740 } | 732 } |
741 | 733 |
742 } // namespace history | 734 } // namespace history |
OLD | NEW |