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" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/autocomplete.h" | |
13 #include "chrome/browser/history/in_memory_database.h" | 14 #include "chrome/browser/history/in_memory_database.h" |
14 #include "chrome/browser/history/in_memory_url_index.h" | 15 #include "chrome/browser/history/in_memory_url_index.h" |
15 #include "chrome/browser/history/in_memory_url_index_types.h" | 16 #include "chrome/browser/history/in_memory_url_index_types.h" |
16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
17 #include "sql/transaction.h" | 18 #include "sql/transaction.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 // The test version of the history url database table ('url') is contained in | 21 // The test version of the history url database table ('url') is contained in |
21 // a database file created from a text file('url_history_provider_test.db.txt'). | 22 // a database file created from a text file('url_history_provider_test.db.txt'). |
22 // The only difference between this table and a live 'urls' table from a | 23 // The only difference between this table and a live 'urls' table from a |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 | 188 |
188 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { | 189 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { |
189 protected: | 190 protected: |
190 FilePath::StringType TestDBName() const; | 191 FilePath::StringType TestDBName() const; |
191 }; | 192 }; |
192 | 193 |
193 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { | 194 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { |
194 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); | 195 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); |
195 } | 196 } |
196 | 197 |
197 class ExpandedInMemoryURLIndexTest : public InMemoryURLIndexTest { | |
198 protected: | |
199 virtual void SetUp(); | |
200 }; | |
201 | |
202 void ExpandedInMemoryURLIndexTest::SetUp() { | |
203 InMemoryURLIndexTest::SetUp(); | |
204 // Add 600 more history items. | |
205 // NOTE: Keep the string length constant at least the length of the format | |
206 // string plus 5 to account for a 3 digit number and terminator. | |
207 char url_format[] = "http://www.google.com/%d"; | |
208 const size_t kMaxLen = arraysize(url_format) + 5; | |
209 char url_string[kMaxLen + 1]; | |
210 for (int i = 0; i < 600; ++i) { | |
211 base::snprintf(url_string, kMaxLen, url_format, i); | |
212 URLRow row(MakeURLRow(url_string, "Google Search", 20, 0, 20)); | |
213 AddURL(row); | |
214 } | |
215 } | |
216 | |
217 TEST_F(InMemoryURLIndexTest, Construction) { | 198 TEST_F(InMemoryURLIndexTest, Construction) { |
218 url_index_.reset(new InMemoryURLIndex(FilePath())); | 199 url_index_.reset(new InMemoryURLIndex(FilePath())); |
219 EXPECT_TRUE(url_index_.get()); | 200 EXPECT_TRUE(url_index_.get()); |
220 } | 201 } |
221 | 202 |
222 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { | 203 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { |
223 // Verify that the database contains the expected number of items, which | 204 // Verify that the database contains the expected number of items, which |
224 // is the pre-filtered count, i.e. all of the items. | 205 // is the pre-filtered count, i.e. all of the items. |
225 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); | 206 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); |
226 EXPECT_TRUE(statement); | 207 EXPECT_TRUE(statement); |
(...skipping 10 matching lines...) Expand all Loading... | |
237 EXPECT_EQ(17U, private_data.word_map_.size()); | 218 EXPECT_EQ(17U, private_data.word_map_.size()); |
238 } | 219 } |
239 | 220 |
240 TEST_F(InMemoryURLIndexTest, Retrieval) { | 221 TEST_F(InMemoryURLIndexTest, Retrieval) { |
241 url_index_.reset(new InMemoryURLIndex(FilePath())); | 222 url_index_.reset(new InMemoryURLIndex(FilePath())); |
242 url_index_->Init(this, "en,ja,hi,zh"); | 223 url_index_->Init(this, "en,ja,hi,zh"); |
243 // The term will be lowercased by the search. | 224 // The term will be lowercased by the search. |
244 | 225 |
245 // See if a very specific term gives a single result. | 226 // See if a very specific term gives a single result. |
246 ScoredHistoryMatches matches = | 227 ScoredHistoryMatches matches = |
247 url_index_->HistoryItemsForTerms(Make1Term("DrudgeReport")); | 228 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport")); |
248 ASSERT_EQ(1U, matches.size()); | 229 ASSERT_EQ(1U, matches.size()); |
249 | 230 |
250 // Verify that we got back the result we expected. | 231 // Verify that we got back the result we expected. |
251 EXPECT_EQ(5, matches[0].url_info.id()); | 232 EXPECT_EQ(5, matches[0].url_info.id()); |
252 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec()); | 233 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec()); |
253 EXPECT_EQ(ASCIIToUTF16("DRUDGE REPORT 2010"), matches[0].url_info.title()); | 234 EXPECT_EQ(ASCIIToUTF16("DRUDGE REPORT 2010"), matches[0].url_info.title()); |
254 | 235 |
255 // Search which should result in multiple results. | 236 // Search which should result in multiple results. |
256 matches = url_index_->HistoryItemsForTerms(Make1Term("drudge")); | 237 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("drudge")); |
257 ASSERT_EQ(2U, matches.size()); | 238 ASSERT_EQ(2U, matches.size()); |
258 // The results should be in descending score order. | 239 // The results should be in descending score order. |
259 EXPECT_GE(matches[0].raw_score, matches[1].raw_score); | 240 EXPECT_GE(matches[0].raw_score, matches[1].raw_score); |
260 | 241 |
261 // Search which should result in nearly perfect result. | 242 // Search which should result in nearly perfect result. |
262 matches = url_index_->HistoryItemsForTerms(Make2Terms("https", | 243 matches = url_index_->HistoryItemsForTerms( |
263 "NearlyPerfectResult")); | 244 ASCIIToUTF16("https NearlyPerfectResult")); |
264 ASSERT_EQ(1U, matches.size()); | 245 ASSERT_EQ(1U, matches.size()); |
265 // The results should have a very high score. | 246 // The results should have a very high score. |
266 EXPECT_GT(matches[0].raw_score, 900); | 247 EXPECT_GT(matches[0].raw_score, 900); |
267 EXPECT_EQ(32, matches[0].url_info.id()); | 248 EXPECT_EQ(32, matches[0].url_info.id()); |
268 EXPECT_EQ("https://nearlyperfectresult.com/", | 249 EXPECT_EQ("https://nearlyperfectresult.com/", |
269 matches[0].url_info.url().spec()); // Note: URL gets lowercased. | 250 matches[0].url_info.url().spec()); // Note: URL gets lowercased. |
270 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"), | 251 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"), |
271 matches[0].url_info.title()); | 252 matches[0].url_info.title()); |
272 | 253 |
273 // Search which should result in very poor result. | 254 // Search which should result in very poor result. |
274 String16Vector original_terms; | 255 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("z y x")); |
275 original_terms.push_back(ASCIIToUTF16("z")); | |
276 original_terms.push_back(ASCIIToUTF16("y")); | |
277 original_terms.push_back(ASCIIToUTF16("x")); | |
278 matches = url_index_->HistoryItemsForTerms(original_terms); | |
279 ASSERT_EQ(1U, matches.size()); | 256 ASSERT_EQ(1U, matches.size()); |
280 // The results should have a poor score. | 257 // The results should have a poor score. |
281 EXPECT_LT(matches[0].raw_score, 500); | 258 EXPECT_LT(matches[0].raw_score, 500); |
282 EXPECT_EQ(33, matches[0].url_info.id()); | 259 EXPECT_EQ(33, matches[0].url_info.id()); |
283 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/", | 260 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/", |
284 matches[0].url_info.url().spec()); // Note: URL gets lowercased. | 261 matches[0].url_info.url().spec()); // Note: URL gets lowercased. |
285 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"), | 262 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"), |
286 matches[0].url_info.title()); | 263 matches[0].url_info.title()); |
287 | 264 |
288 // Search which will match at the end of an URL with encoded characters. | 265 // Search which will match at the end of an URL with encoded characters. |
289 matches = url_index_->HistoryItemsForTerms(Make1Term("ice")); | 266 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ice")); |
290 ASSERT_EQ(1U, matches.size()); | 267 ASSERT_EQ(1U, matches.size()); |
291 } | 268 } |
292 | 269 |
293 TEST_F(ExpandedInMemoryURLIndexTest, ShortCircuit) { | 270 TEST_F(InMemoryURLIndexTest, ProperStringMatching) { |
271 url_index_.reset(new InMemoryURLIndex(FilePath())); | |
272 url_index_->Init(this, "en,ja,hi,zh"); | |
GeorgeY
2011/11/30 22:21:41
Please fix indentation
mrossetti
2011/11/30 22:45:24
Done.
| |
273 | |
274 // Search for the following with the expected results: | |
275 // "atdmt view" - found | |
276 // "atdmt.view" - not found | |
277 // "view.atdmt" - found | |
278 ScoredHistoryMatches matches = | |
GeorgeY
2011/11/30 22:21:41
Please fix indentation
mrossetti
2011/11/30 22:45:24
Done.
| |
279 url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt view")); | |
280 ASSERT_EQ(1U, matches.size()); | |
281 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view")); | |
282 ASSERT_EQ(0U, matches.size()); | |
283 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt")); | |
284 ASSERT_EQ(1U, matches.size()); | |
285 } | |
286 | |
287 TEST_F(InMemoryURLIndexTest, HugeResultSet) { | |
294 url_index_.reset(new InMemoryURLIndex(FilePath())); | 288 url_index_.reset(new InMemoryURLIndex(FilePath())); |
295 url_index_->Init(this, "en,ja,hi,zh"); | 289 url_index_->Init(this, "en,ja,hi,zh"); |
296 | 290 |
297 // A search for 'w' should short-circuit and not return any matches. | 291 // Create a huge set of qualifying history items. |
292 for (URLID row_id = 5000; row_id < 6000; ++row_id) { | |
293 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), row_id); | |
294 new_row.set_last_visit(base::Time::Now()); | |
295 url_index_->UpdateURL(row_id, new_row); | |
296 } | |
297 | |
298 ScoredHistoryMatches matches = | 298 ScoredHistoryMatches matches = |
299 url_index_->HistoryItemsForTerms(Make1Term("w")); | 299 url_index_->HistoryItemsForTerms(ASCIIToUTF16("b")); |
300 EXPECT_TRUE(matches.empty()); | 300 ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size()); |
301 | 301 // There are 7 matches already in the database. |
302 // A search for 'working' should not short-circuit. | 302 ASSERT_EQ(1007U, url_index_->pre_filter_item_count); |
303 matches = url_index_->HistoryItemsForTerms(Make1Term("working")); | 303 ASSERT_EQ(500U, url_index_->post_filter_item_count); |
304 EXPECT_EQ(1U, matches.size()); | 304 ASSERT_EQ(AutocompleteProvider::kMaxMatches, |
305 url_index_->post_scoring_item_count); | |
305 } | 306 } |
306 | 307 |
307 TEST_F(InMemoryURLIndexTest, TitleSearch) { | 308 TEST_F(InMemoryURLIndexTest, TitleSearch) { |
308 url_index_.reset(new InMemoryURLIndex(FilePath())); | 309 url_index_.reset(new InMemoryURLIndex(FilePath())); |
309 url_index_->Init(this, "en,ja,hi,zh"); | 310 url_index_->Init(this, "en,ja,hi,zh"); |
310 // Signal if someone has changed the test DB. | 311 // Signal if someone has changed the test DB. |
311 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size()); | 312 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size()); |
312 String16Vector original_terms; | |
313 | 313 |
314 // Ensure title is being searched. | 314 // Ensure title is being searched. |
315 original_terms.push_back(ASCIIToUTF16("MORTGAGE")); | |
316 original_terms.push_back(ASCIIToUTF16("RATE")); | |
317 original_terms.push_back(ASCIIToUTF16("DROPS")); | |
318 ScoredHistoryMatches matches = | 315 ScoredHistoryMatches matches = |
319 url_index_->HistoryItemsForTerms(original_terms); | 316 url_index_->HistoryItemsForTerms(ASCIIToUTF16("MORTGAGE RATE DROPS")); |
320 ASSERT_EQ(1U, matches.size()); | 317 ASSERT_EQ(1U, matches.size()); |
321 | 318 |
322 // Verify that we got back the result we expected. | 319 // Verify that we got back the result we expected. |
323 EXPECT_EQ(1, matches[0].url_info.id()); | 320 EXPECT_EQ(1, matches[0].url_info.id()); |
324 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708", | 321 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708", |
325 matches[0].url_info.url().spec()); | 322 matches[0].url_info.url().spec()); |
326 EXPECT_EQ(ASCIIToUTF16( | 323 EXPECT_EQ(ASCIIToUTF16( |
327 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"), | 324 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"), |
328 matches[0].url_info.title()); | 325 matches[0].url_info.title()); |
329 } | 326 } |
330 | 327 |
331 TEST_F(InMemoryURLIndexTest, TitleChange) { | 328 TEST_F(InMemoryURLIndexTest, TitleChange) { |
332 url_index_.reset(new InMemoryURLIndex(FilePath())); | 329 url_index_.reset(new InMemoryURLIndex(FilePath())); |
333 url_index_->Init(this, "en,ja,hi,zh"); | 330 url_index_->Init(this, "en,ja,hi,zh"); |
334 | 331 |
335 // Verify current title terms retrieves desired item. | 332 // Verify current title terms retrieves desired item. |
336 String16Vector original_terms; | 333 string16 original_terms = |
337 original_terms.push_back(ASCIIToUTF16("lebronomics")); | 334 ASCIIToUTF16("lebronomics could high taxes influence"); |
338 original_terms.push_back(ASCIIToUTF16("could")); | |
339 original_terms.push_back(ASCIIToUTF16("high")); | |
340 original_terms.push_back(ASCIIToUTF16("taxes")); | |
341 original_terms.push_back(ASCIIToUTF16("influence")); | |
342 ScoredHistoryMatches matches = | 335 ScoredHistoryMatches matches = |
343 url_index_->HistoryItemsForTerms(original_terms); | 336 url_index_->HistoryItemsForTerms(original_terms); |
344 ASSERT_EQ(1U, matches.size()); | 337 ASSERT_EQ(1U, matches.size()); |
345 | 338 |
346 // Verify that we got back the result we expected. | 339 // Verify that we got back the result we expected. |
347 const URLID expected_id = 3; | 340 const URLID expected_id = 3; |
348 EXPECT_EQ(expected_id, matches[0].url_info.id()); | 341 EXPECT_EQ(expected_id, matches[0].url_info.id()); |
349 EXPECT_EQ("http://www.businessandmedia.org/articles/2010/20100708120415.aspx", | 342 EXPECT_EQ("http://www.businessandmedia.org/articles/2010/20100708120415.aspx", |
350 matches[0].url_info.url().spec()); | 343 matches[0].url_info.url().spec()); |
351 EXPECT_EQ(ASCIIToUTF16( | 344 EXPECT_EQ(ASCIIToUTF16( |
352 "LeBronomics: Could High Taxes Influence James' Team Decision?"), | 345 "LeBronomics: Could High Taxes Influence James' Team Decision?"), |
353 matches[0].url_info.title()); | 346 matches[0].url_info.title()); |
354 URLRow old_row(matches[0].url_info); | 347 URLRow old_row(matches[0].url_info); |
355 | 348 |
356 // Verify new title terms retrieves nothing. | 349 // Verify new title terms retrieves nothing. |
357 String16Vector new_terms; | 350 string16 new_terms = ASCIIToUTF16("does eat oats little lambs ivy"); |
358 new_terms.push_back(ASCIIToUTF16("does")); | |
359 new_terms.push_back(ASCIIToUTF16("eat")); | |
360 new_terms.push_back(ASCIIToUTF16("oats")); | |
361 new_terms.push_back(ASCIIToUTF16("little")); | |
362 new_terms.push_back(ASCIIToUTF16("lambs")); | |
363 new_terms.push_back(ASCIIToUTF16("ivy")); | |
364 matches = url_index_->HistoryItemsForTerms(new_terms); | 351 matches = url_index_->HistoryItemsForTerms(new_terms); |
365 ASSERT_EQ(0U, matches.size()); | 352 ASSERT_EQ(0U, matches.size()); |
366 | 353 |
367 // Update the row. | 354 // Update the row. |
368 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy")); | 355 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy")); |
369 url_index_->UpdateURL(expected_id, old_row); | 356 url_index_->UpdateURL(expected_id, old_row); |
370 | 357 |
371 // Verify we get the row using the new terms but not the original terms. | 358 // Verify we get the row using the new terms but not the original terms. |
372 matches = url_index_->HistoryItemsForTerms(new_terms); | 359 matches = url_index_->HistoryItemsForTerms(new_terms); |
373 ASSERT_EQ(1U, matches.size()); | 360 ASSERT_EQ(1U, matches.size()); |
374 EXPECT_EQ(expected_id, matches[0].url_info.id()); | 361 EXPECT_EQ(expected_id, matches[0].url_info.id()); |
375 matches = url_index_->HistoryItemsForTerms(original_terms); | 362 matches = url_index_->HistoryItemsForTerms(original_terms); |
376 ASSERT_EQ(0U, matches.size()); | 363 ASSERT_EQ(0U, matches.size()); |
377 } | 364 } |
378 | 365 |
379 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) { | 366 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) { |
380 url_index_.reset(new InMemoryURLIndex(FilePath())); | 367 url_index_.reset(new InMemoryURLIndex(FilePath())); |
381 url_index_->Init(this, "en,ja,hi,zh"); | 368 url_index_->Init(this, "en,ja,hi,zh"); |
382 | 369 |
383 // The presence of duplicate characters should succeed. Exercise by cycling | 370 // The presence of duplicate characters should succeed. Exercise by cycling |
384 // through a string with several duplicate characters. | 371 // through a string with several duplicate characters. |
385 ScoredHistoryMatches matches = | 372 ScoredHistoryMatches matches = |
386 url_index_->HistoryItemsForTerms(Make1Term("ABRA")); | 373 url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRA")); |
387 ASSERT_EQ(1U, matches.size()); | 374 ASSERT_EQ(1U, matches.size()); |
388 EXPECT_EQ(28, matches[0].url_info.id()); | 375 EXPECT_EQ(28, matches[0].url_info.id()); |
389 EXPECT_EQ("http://www.ddj.com/windows/184416623", | 376 EXPECT_EQ("http://www.ddj.com/windows/184416623", |
390 matches[0].url_info.url().spec()); | 377 matches[0].url_info.url().spec()); |
391 | 378 |
392 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACAD")); | 379 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRACAD")); |
393 ASSERT_EQ(1U, matches.size()); | 380 ASSERT_EQ(1U, matches.size()); |
394 EXPECT_EQ(28, matches[0].url_info.id()); | 381 EXPECT_EQ(28, matches[0].url_info.id()); |
395 | 382 |
396 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACADABRA")); | 383 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRACADABRA")); |
397 ASSERT_EQ(1U, matches.size()); | 384 ASSERT_EQ(1U, matches.size()); |
398 EXPECT_EQ(28, matches[0].url_info.id()); | 385 EXPECT_EQ(28, matches[0].url_info.id()); |
399 | 386 |
400 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACADABR")); | 387 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRACADABR")); |
401 ASSERT_EQ(1U, matches.size()); | 388 ASSERT_EQ(1U, matches.size()); |
402 EXPECT_EQ(28, matches[0].url_info.id()); | 389 EXPECT_EQ(28, matches[0].url_info.id()); |
403 | 390 |
404 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACA")); | 391 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRACA")); |
405 ASSERT_EQ(1U, matches.size()); | 392 ASSERT_EQ(1U, matches.size()); |
406 EXPECT_EQ(28, matches[0].url_info.id()); | 393 EXPECT_EQ(28, matches[0].url_info.id()); |
407 } | 394 } |
408 | 395 |
409 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { | 396 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { |
410 // Verify that match results for previously typed characters are retained | 397 // Verify that match results for previously typed characters are retained |
411 // (in the term_char_word_set_cache_) and reused, if possible, in future | 398 // (in the term_char_word_set_cache_) and reused, if possible, in future |
412 // autocompletes. | 399 // autocompletes. |
413 typedef InMemoryURLIndex::SearchTermCacheMap::iterator CacheIter; | 400 typedef InMemoryURLIndex::SearchTermCacheMap::iterator CacheIter; |
414 typedef InMemoryURLIndex::SearchTermCacheItem CacheItem; | 401 typedef InMemoryURLIndex::SearchTermCacheItem CacheItem; |
415 | 402 |
416 url_index_.reset(new InMemoryURLIndex(FilePath())); | 403 url_index_.reset(new InMemoryURLIndex(FilePath())); |
417 url_index_->Init(this, "en,ja,hi,zh"); | 404 url_index_->Init(this, "en,ja,hi,zh"); |
418 | 405 |
419 InMemoryURLIndex::SearchTermCacheMap& cache(url_index_->search_term_cache_); | 406 InMemoryURLIndex::SearchTermCacheMap& cache(url_index_->search_term_cache_); |
420 | 407 |
421 // The cache should be empty at this point. | 408 // The cache should be empty at this point. |
422 EXPECT_EQ(0U, cache.size()); | 409 EXPECT_EQ(0U, cache.size()); |
423 | 410 |
424 // Now simulate typing search terms into the omnibox and check the state of | 411 // Now simulate typing search terms into the omnibox and check the state of |
425 // the cache as each item is 'typed'. | 412 // the cache as each item is 'typed'. |
426 | 413 |
427 // Simulate typing "r" giving "r" in the simulated omnibox. The results for | 414 // 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. | 415 // 'r' will be not cached because it is only 1 character long. |
429 String16Vector original_terms; | 416 url_index_->HistoryItemsForTerms(ASCIIToUTF16("r")); |
430 string16 term_r = ASCIIToUTF16("r"); | |
431 original_terms.push_back(term_r); | |
432 url_index_->HistoryItemsForTerms(original_terms); | |
433 EXPECT_EQ(0U, cache.size()); | 417 EXPECT_EQ(0U, cache.size()); |
434 | 418 |
435 // Simulate typing "re" giving "r re" in the simulated omnibox. | 419 // Simulate typing "re" giving "r re" in the simulated omnibox. |
436 string16 term_re = ASCIIToUTF16("re"); | |
437 original_terms.push_back(term_re); | |
438 // 're' should be cached at this point but not 'r' as it is a single | 420 // 're' should be cached at this point but not 'r' as it is a single |
439 // character. | 421 // character. |
440 ASSERT_EQ(2U, original_terms.size()); | 422 url_index_->HistoryItemsForTerms(ASCIIToUTF16("r re")); |
441 url_index_->HistoryItemsForTerms(original_terms); | |
442 ASSERT_EQ(1U, cache.size()); | 423 ASSERT_EQ(1U, cache.size()); |
443 CheckTerm(cache, term_re); | 424 CheckTerm(cache, ASCIIToUTF16("re")); |
444 | 425 |
445 // Simulate typing "reco" giving "r re reco" in the simulated omnibox. | 426 // Simulate typing "reco" giving "r re reco" in the simulated omnibox. |
446 string16 term_reco = ASCIIToUTF16("reco"); | |
447 original_terms.push_back(term_reco); | |
448 // 're' and 'reco' should be cached at this point but not 'r' as it is a | 427 // 're' and 'reco' should be cached at this point but not 'r' as it is a |
449 // single character. | 428 // single character. |
450 url_index_->HistoryItemsForTerms(original_terms); | 429 url_index_->HistoryItemsForTerms(ASCIIToUTF16("r re reco")); |
451 ASSERT_EQ(2U, cache.size()); | 430 ASSERT_EQ(2U, cache.size()); |
452 CheckTerm(cache, term_re); | 431 CheckTerm(cache, ASCIIToUTF16("re")); |
453 CheckTerm(cache, term_reco); | 432 CheckTerm(cache, ASCIIToUTF16("reco")); |
454 | |
455 original_terms.clear(); // Simulate pressing <ESC>. | |
456 | 433 |
457 // Simulate typing "mort". | 434 // Simulate typing "mort". |
458 string16 term_mort = ASCIIToUTF16("mort"); | |
459 original_terms.push_back(term_mort); | |
460 // Since we now have only one search term, the cached results for 're' and | 435 // 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'). | 436 // 'reco' should be purged, giving us only 1 item in the cache (for 'mort'). |
462 url_index_->HistoryItemsForTerms(original_terms); | 437 url_index_->HistoryItemsForTerms(ASCIIToUTF16("mort")); |
463 ASSERT_EQ(1U, cache.size()); | 438 ASSERT_EQ(1U, cache.size()); |
464 CheckTerm(cache, term_mort); | 439 CheckTerm(cache, ASCIIToUTF16("mort")); |
465 | 440 |
466 // Simulate typing "reco" giving "mort reco" in the simulated omnibox. | 441 // Simulate typing "reco" giving "mort reco" in the simulated omnibox. |
467 original_terms.push_back(term_reco); | 442 url_index_->HistoryItemsForTerms(ASCIIToUTF16("mort reco")); |
468 url_index_->HistoryItemsForTerms(original_terms); | |
469 ASSERT_EQ(2U, cache.size()); | 443 ASSERT_EQ(2U, cache.size()); |
470 CheckTerm(cache, term_mort); | 444 CheckTerm(cache, ASCIIToUTF16("mort")); |
471 CheckTerm(cache, term_reco); | 445 CheckTerm(cache, ASCIIToUTF16("reco")); |
472 | 446 |
473 // Simulate a <DELETE> by removing the 'reco' and adding back the 'rec'. | 447 // Simulate a <DELETE> by removing the 'reco' and adding back the 'rec'. |
474 original_terms.resize(original_terms.size() - 1); | 448 url_index_->HistoryItemsForTerms(ASCIIToUTF16("mort rec")); |
475 string16 term_rec = ASCIIToUTF16("rec"); | |
476 original_terms.push_back(term_rec); | |
477 url_index_->HistoryItemsForTerms(original_terms); | |
478 ASSERT_EQ(2U, cache.size()); | 449 ASSERT_EQ(2U, cache.size()); |
479 CheckTerm(cache, term_mort); | 450 CheckTerm(cache, ASCIIToUTF16("mort")); |
480 CheckTerm(cache, term_rec); | 451 CheckTerm(cache, ASCIIToUTF16("rec")); |
481 } | 452 } |
482 | 453 |
483 TEST_F(InMemoryURLIndexTest, Scoring) { | 454 TEST_F(InMemoryURLIndexTest, Scoring) { |
484 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 3, 30, 1)); | 455 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 3, 30, 1)); |
485 // Test scores based on position. | 456 // Test scores based on position. |
486 ScoredHistoryMatch scored_a( | 457 ScoredHistoryMatch scored_a( |
487 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("abc"))); | 458 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("abc"))); |
488 ScoredHistoryMatch scored_b( | 459 ScoredHistoryMatch scored_b( |
489 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("bcd"))); | 460 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("bcd"))); |
490 EXPECT_GT(scored_a.raw_score, scored_b.raw_score); | 461 EXPECT_GT(scored_a.raw_score, scored_b.raw_score); |
(...skipping 20 matching lines...) Expand all Loading... | |
511 // Test scores based on typed_count. | 482 // Test scores based on typed_count. |
512 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); | 483 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); |
513 ScoredHistoryMatch scored_h( | 484 ScoredHistoryMatch scored_h( |
514 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc"))); | 485 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc"))); |
515 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); | 486 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); |
516 } | 487 } |
517 | 488 |
518 TEST_F(InMemoryURLIndexTest, AddNewRows) { | 489 TEST_F(InMemoryURLIndexTest, AddNewRows) { |
519 url_index_.reset(new InMemoryURLIndex(FilePath())); | 490 url_index_.reset(new InMemoryURLIndex(FilePath())); |
520 url_index_->Init(this, "en,ja,hi,zh"); | 491 url_index_->Init(this, "en,ja,hi,zh"); |
521 String16Vector original_terms; | |
522 | 492 |
523 // Verify that the row we're going to add does not already exist. | 493 // Verify that the row we're going to add does not already exist. |
524 URLID new_row_id = 87654321; | 494 URLID new_row_id = 87654321; |
525 // Newly created URLRows get a last_visit time of 'right now' so it should | 495 // Newly created URLRows get a last_visit time of 'right now' so it should |
526 // qualify as a quick result candidate. | 496 // qualify as a quick result candidate. |
527 original_terms.push_back(ASCIIToUTF16("brokeandalone")); | 497 EXPECT_TRUE(url_index_->HistoryItemsForTerms( |
528 EXPECT_TRUE(url_index_->HistoryItemsForTerms(original_terms).empty()); | 498 ASCIIToUTF16("brokeandalone")).empty()); |
529 | 499 |
530 // Add a new row. | 500 // Add a new row. |
531 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); | 501 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); |
532 new_row.set_last_visit(base::Time::Now()); | 502 new_row.set_last_visit(base::Time::Now()); |
533 url_index_->UpdateURL(new_row_id, new_row); | 503 url_index_->UpdateURL(new_row_id, new_row); |
534 | 504 |
535 // Verify that we can retrieve it. | 505 // Verify that we can retrieve it. |
536 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(original_terms).size()); | 506 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms( |
507 ASCIIToUTF16("brokeandalone")).size()); | |
537 | 508 |
538 // Add it again just to be sure that is harmless. | 509 // Add it again just to be sure that is harmless. |
539 url_index_->UpdateURL(new_row_id, new_row); | 510 url_index_->UpdateURL(new_row_id, new_row); |
540 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(original_terms).size()); | 511 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms( |
512 ASCIIToUTF16("brokeandalone")).size()); | |
541 } | 513 } |
542 | 514 |
543 TEST_F(InMemoryURLIndexTest, DeleteRows) { | 515 TEST_F(InMemoryURLIndexTest, DeleteRows) { |
544 url_index_.reset(new InMemoryURLIndex(FilePath())); | 516 url_index_.reset(new InMemoryURLIndex(FilePath())); |
545 url_index_->Init(this, "en,ja,hi,zh"); | 517 url_index_->Init(this, "en,ja,hi,zh"); |
546 String16Vector original_terms; | |
547 | 518 |
548 // Make sure we actually get an existing result. | |
549 original_terms.push_back(ASCIIToUTF16("DrudgeReport")); | |
550 ScoredHistoryMatches matches = | 519 ScoredHistoryMatches matches = |
551 url_index_->HistoryItemsForTerms(original_terms); | 520 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport")); |
552 ASSERT_EQ(1U, matches.size()); | 521 ASSERT_EQ(1U, matches.size()); |
553 | 522 |
554 // Determine the row id for that result, delete that id, then search again. | 523 // Determine the row id for that result, delete that id, then search again. |
555 url_index_->DeleteURL(matches[0].url_info.id()); | 524 url_index_->DeleteURL(matches[0].url_info.id()); |
556 EXPECT_TRUE(url_index_->HistoryItemsForTerms(original_terms).empty()); | 525 EXPECT_TRUE(url_index_->HistoryItemsForTerms( |
526 ASCIIToUTF16("DrudgeReport")).empty()); | |
557 } | 527 } |
558 | 528 |
559 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { | 529 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { |
560 struct TestData { | 530 struct TestData { |
561 const std::string url_spec; | 531 const std::string url_spec; |
562 const bool expected_is_whitelisted; | 532 const bool expected_is_whitelisted; |
563 } data[] = { | 533 } data[] = { |
564 // URLs with whitelisted schemes. | 534 // URLs with whitelisted schemes. |
565 { "about:histograms", true }, | 535 { "about:histograms", true }, |
566 { "chrome://settings", true }, | 536 { "chrome://settings", true }, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
725 const URLRow& expected_row(expected->second); | 695 const URLRow& expected_row(expected->second); |
726 const URLRow& actual_row(actual->second); | 696 const URLRow& actual_row(actual->second); |
727 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); | 697 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); |
728 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); | 698 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); |
729 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); | 699 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); |
730 EXPECT_EQ(expected_row.url(), actual_row.url()); | 700 EXPECT_EQ(expected_row.url(), actual_row.url()); |
731 } | 701 } |
732 } | 702 } |
733 | 703 |
734 } // namespace history | 704 } // namespace history |
OLD | NEW |