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