Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(929)

Side by Side Diff: chrome/browser/history/in_memory_url_index_unittest.cc

Issue 9030031: Move InMemoryURLIndex Caching Operations to FILE Thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Missed a private data swap. Try to fix update phase failure. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <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/message_loop.h"
9 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/scoped_temp_dir.h"
10 #include "base/string16.h" 12 #include "base/string16.h"
11 #include "base/string_util.h" 13 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/autocomplete/autocomplete.h" 15 #include "chrome/browser/autocomplete/autocomplete.h"
14 #include "chrome/browser/history/in_memory_database.h" 16 #include "chrome/browser/history/in_memory_database.h"
15 #include "chrome/browser/history/in_memory_url_index.h" 17 #include "chrome/browser/history/in_memory_url_index.h"
16 #include "chrome/browser/history/in_memory_url_index_types.h" 18 #include "chrome/browser/history/in_memory_url_index_types.h"
19 #include "chrome/browser/history/url_index_private_data.h"
17 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/test/test_browser_thread.h"
18 #include "sql/transaction.h" 23 #include "sql/transaction.h"
19 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
20 25
26 using content::BrowserThread;
27
21 // The test version of the history url database table ('url') is contained in 28 // The test version of the history url database table ('url') is contained in
22 // a database file created from a text file('url_history_provider_test.db.txt'). 29 // a database file created from a text file('url_history_provider_test.db.txt').
23 // The only difference between this table and a live 'urls' table from a 30 // The only difference between this table and a live 'urls' table from a
24 // profile is that the last_visit_time column in the test table contains a 31 // profile is that the last_visit_time column in the test table contains a
25 // number specifying the number of days relative to 'today' to which the 32 // number specifying the number of days relative to 'today' to which the
26 // absolute time should be set during the test setup stage. 33 // absolute time should be set during the test setup stage.
27 // 34 //
28 // The format of the test database text file is of a SQLite .dump file. 35 // The format of the test database text file is of a SQLite .dump file.
29 // Note that only lines whose first character is an upper-case letter are 36 // Note that only lines whose first character is an upper-case letter are
30 // processed when creating the test database. 37 // processed when creating the test database.
31 38
32 namespace history { 39 namespace history {
33 40
41 // -----------------------------------------------------------------------------
42
43 // Observer class so the unit tests can wait while the cache is being saved.
44 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver {
45 public:
46 explicit CacheFileSaverObserver(MessageLoop* loop)
Peter Kasting 2012/01/14 00:12:49 Nit: I know it's just a test file, but I'd prefer
mrossetti 2012/03/03 05:05:56 Will do in the next CL.
47 : loop_(loop),
48 succeeded_(false) {
49 DCHECK(loop);
50 }
51
52 virtual void OnCacheSaveFinished() OVERRIDE {
53 succeeded_ = true;
54 loop_->Quit();
55 }
56 virtual void OnCacheSaveFailed() OVERRIDE { loop_->Quit(); }
57
58 private:
59 MessageLoop* loop_;
60 bool succeeded_;
61 DISALLOW_COPY_AND_ASSIGN(CacheFileSaverObserver);
62 };
63
64 // Observer class so the unit tests can wait while the cache is being restored.
65 class CacheFileReaderObserver : public InMemoryURLIndex::RestoreCacheObserver {
66 public:
67 explicit CacheFileReaderObserver(MessageLoop* loop)
68 : loop_(loop),
69 succeeded_(false) {
70 DCHECK(loop);
71 }
72
73 virtual void OnCacheRestoreFinished(bool succeeded) OVERRIDE {
74 succeeded_ = succeeded;
75 loop_->Quit();
76 }
77
78 private:
79 MessageLoop* loop_;
80 bool succeeded_;
81 DISALLOW_COPY_AND_ASSIGN(CacheFileReaderObserver);
82 };
83
84 // -----------------------------------------------------------------------------
85
34 class InMemoryURLIndexTest : public testing::Test, 86 class InMemoryURLIndexTest : public testing::Test,
35 public InMemoryDatabase { 87 public InMemoryDatabase {
36 public: 88 public:
37 InMemoryURLIndexTest() { InitFromScratch(); } 89 InMemoryURLIndexTest() { InitFromScratch(); }
38 90
39 protected: 91 protected:
40 // Test setup. 92 // Test setup.
41 virtual void SetUp(); 93 virtual void SetUp();
42 94
43 // Allows the database containing the test data to be customized by 95 // Allows the database containing the test data to be customized by
(...skipping 11 matching lines...) Expand all
55 107
56 // Convenience functions for easily creating vectors of search terms. 108 // Convenience functions for easily creating vectors of search terms.
57 String16Vector Make1Term(const char* term) const; 109 String16Vector Make1Term(const char* term) const;
58 String16Vector Make2Terms(const char* term_1, const char* term_2) const; 110 String16Vector Make2Terms(const char* term_1, const char* term_2) const;
59 111
60 // Validates that the given |term| is contained in |cache| and that it is 112 // Validates that the given |term| is contained in |cache| and that it is
61 // marked as in-use. 113 // marked as in-use.
62 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, 114 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache,
63 string16 term) const; 115 string16 term) const;
64 116
65 scoped_ptr<InMemoryURLIndex> url_index_; 117 scoped_ptr<TestingProfile> profile_;
118 scoped_refptr<InMemoryURLIndex> url_index_;
66 }; 119 };
67 120
68 void InMemoryURLIndexTest::SetUp() { 121 void InMemoryURLIndexTest::SetUp() {
122 profile_.reset(new TestingProfile());
123
69 // Create and populate a working copy of the URL history database. 124 // Create and populate a working copy of the URL history database.
70 FilePath history_proto_path; 125 FilePath history_proto_path;
71 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path); 126 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path);
72 history_proto_path = history_proto_path.Append( 127 history_proto_path = history_proto_path.Append(
73 FILE_PATH_LITERAL("History")); 128 FILE_PATH_LITERAL("History"));
74 history_proto_path = history_proto_path.Append(TestDBName()); 129 history_proto_path = history_proto_path.Append(TestDBName());
75 EXPECT_TRUE(file_util::PathExists(history_proto_path)); 130 EXPECT_TRUE(file_util::PathExists(history_proto_path));
76 131
77 std::ifstream proto_file(history_proto_path.value().c_str()); 132 std::ifstream proto_file(history_proto_path.value().c_str());
78 static const size_t kCommandBufferMaxSize = 2048; 133 static const size_t kCommandBufferMaxSize = 2048;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 243
189 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { 244 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest {
190 protected: 245 protected:
191 FilePath::StringType TestDBName() const; 246 FilePath::StringType TestDBName() const;
192 }; 247 };
193 248
194 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { 249 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const {
195 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); 250 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt");
196 } 251 }
197 252
198 TEST_F(InMemoryURLIndexTest, Construction) {
199 url_index_.reset(new InMemoryURLIndex(FilePath()));
200 EXPECT_TRUE(url_index_.get());
201 }
202
203 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { 253 TEST_F(LimitedInMemoryURLIndexTest, Initialization) {
204 // Verify that the database contains the expected number of items, which 254 // Verify that the database contains the expected number of items, which
205 // is the pre-filtered count, i.e. all of the items. 255 // is the pre-filtered count, i.e. all of the items.
206 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); 256 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;"));
207 EXPECT_TRUE(statement); 257 EXPECT_TRUE(statement);
208 uint64 row_count = 0; 258 uint64 row_count = 0;
209 while (statement.Step()) ++row_count; 259 while (statement.Step()) ++row_count;
210 EXPECT_EQ(1U, row_count); 260 EXPECT_EQ(1U, row_count);
211 url_index_.reset(new InMemoryURLIndex(FilePath())); 261 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
212 url_index_->Init(this, "en,ja,hi,zh"); 262 url_index_->Init("en,ja,hi,zh");
263 url_index_->RebuildFromHistory(this);
213 URLIndexPrivateData& private_data(*(url_index_->private_data_)); 264 URLIndexPrivateData& private_data(*(url_index_->private_data_));
214 265
215 // history_info_map_ should have the same number of items as were filtered. 266 // history_info_map_ should have the same number of items as were filtered.
216 EXPECT_EQ(1U, private_data.history_info_map_.size()); 267 EXPECT_EQ(1U, private_data.history_info_map_.size());
217 EXPECT_EQ(35U, private_data.char_word_map_.size()); 268 EXPECT_EQ(35U, private_data.char_word_map_.size());
218 EXPECT_EQ(17U, private_data.word_map_.size()); 269 EXPECT_EQ(17U, private_data.word_map_.size());
219 } 270 }
220 271
272 //------------------------------------------------------------------------------
273
274 TEST_F(InMemoryURLIndexTest, Construction) {
275 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
276 EXPECT_TRUE(url_index_.get());
277 }
278
221 TEST_F(InMemoryURLIndexTest, Retrieval) { 279 TEST_F(InMemoryURLIndexTest, Retrieval) {
222 url_index_.reset(new InMemoryURLIndex(FilePath())); 280 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
223 url_index_->Init(this, "en,ja,hi,zh"); 281 url_index_->Init("en,ja,hi,zh");
282 url_index_->RebuildFromHistory(this);
224 // The term will be lowercased by the search. 283 // The term will be lowercased by the search.
225 284
226 // See if a very specific term gives a single result. 285 // See if a very specific term gives a single result.
227 ScoredHistoryMatches matches = 286 ScoredHistoryMatches matches =
228 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport")); 287 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"));
229 ASSERT_EQ(1U, matches.size()); 288 ASSERT_EQ(1U, matches.size());
230 289
231 // Verify that we got back the result we expected. 290 // Verify that we got back the result we expected.
232 EXPECT_EQ(5, matches[0].url_info.id()); 291 EXPECT_EQ(5, matches[0].url_info.id());
233 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec()); 292 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec());
(...skipping 27 matching lines...) Expand all
261 matches[0].url_info.url().spec()); // Note: URL gets lowercased. 320 matches[0].url_info.url().spec()); // Note: URL gets lowercased.
262 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"), 321 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"),
263 matches[0].url_info.title()); 322 matches[0].url_info.title());
264 323
265 // Search which will match at the end of an URL with encoded characters. 324 // Search which will match at the end of an URL with encoded characters.
266 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ice")); 325 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ice"));
267 ASSERT_EQ(1U, matches.size()); 326 ASSERT_EQ(1U, matches.size());
268 } 327 }
269 328
270 TEST_F(InMemoryURLIndexTest, ProperStringMatching) { 329 TEST_F(InMemoryURLIndexTest, ProperStringMatching) {
271 url_index_.reset(new InMemoryURLIndex(FilePath())); 330 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
272 url_index_->Init(this, "en,ja,hi,zh"); 331 url_index_->Init("en,ja,hi,zh");
332 url_index_->RebuildFromHistory(this);
273 333
274 // Search for the following with the expected results: 334 // Search for the following with the expected results:
275 // "atdmt view" - found 335 // "atdmt view" - found
276 // "atdmt.view" - not found 336 // "atdmt.view" - not found
277 // "view.atdmt" - found 337 // "view.atdmt" - found
278 ScoredHistoryMatches matches = 338 ScoredHistoryMatches matches =
279 url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt view")); 339 url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt view"));
280 ASSERT_EQ(1U, matches.size()); 340 ASSERT_EQ(1U, matches.size());
281 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view")); 341 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view"));
282 ASSERT_EQ(0U, matches.size()); 342 ASSERT_EQ(0U, matches.size());
283 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt")); 343 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt"));
284 ASSERT_EQ(1U, matches.size()); 344 ASSERT_EQ(1U, matches.size());
285 } 345 }
286 346
287 TEST_F(InMemoryURLIndexTest, HugeResultSet) { 347 TEST_F(InMemoryURLIndexTest, HugeResultSet) {
288 url_index_.reset(new InMemoryURLIndex(FilePath())); 348 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
289 url_index_->Init(this, "en,ja,hi,zh"); 349 url_index_->Init("en,ja,hi,zh");
350 url_index_->RebuildFromHistory(this);
290 351
291 // Create a huge set of qualifying history items. 352 // Create a huge set of qualifying history items.
292 for (URLID row_id = 5000; row_id < 6000; ++row_id) { 353 for (URLID row_id = 5000; row_id < 6000; ++row_id) {
293 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), row_id); 354 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), row_id);
294 new_row.set_last_visit(base::Time::Now()); 355 new_row.set_last_visit(base::Time::Now());
295 url_index_->UpdateURL(row_id, new_row); 356 url_index_->UpdateURL(new_row);
296 } 357 }
297 358
298 ScoredHistoryMatches matches = 359 ScoredHistoryMatches matches =
299 url_index_->HistoryItemsForTerms(ASCIIToUTF16("b")); 360 url_index_->HistoryItemsForTerms(ASCIIToUTF16("b"));
300 ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size()); 361 ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size());
301 // There are 7 matches already in the database. 362 // There are 7 matches already in the database.
302 URLIndexPrivateData& private_data(*(url_index_->private_data_.get())); 363 URLIndexPrivateData& private_data(*(url_index_->private_data_.get()));
303 ASSERT_EQ(1007U, private_data.pre_filter_item_count_); 364 ASSERT_EQ(1007U, private_data.pre_filter_item_count_);
304 ASSERT_EQ(500U, private_data.post_filter_item_count_); 365 ASSERT_EQ(500U, private_data.post_filter_item_count_);
305 ASSERT_EQ(AutocompleteProvider::kMaxMatches, 366 ASSERT_EQ(AutocompleteProvider::kMaxMatches,
306 private_data.post_scoring_item_count_); 367 private_data.post_scoring_item_count_);
307 } 368 }
308 369
309 TEST_F(InMemoryURLIndexTest, TitleSearch) { 370 TEST_F(InMemoryURLIndexTest, TitleSearch) {
310 url_index_.reset(new InMemoryURLIndex(FilePath())); 371 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
311 url_index_->Init(this, "en,ja,hi,zh"); 372 url_index_->Init("en,ja,hi,zh");
373 url_index_->RebuildFromHistory(this);
312 // Signal if someone has changed the test DB. 374 // Signal if someone has changed the test DB.
313 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size()); 375 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size());
314 376
315 // Ensure title is being searched. 377 // Ensure title is being searched.
316 ScoredHistoryMatches matches = 378 ScoredHistoryMatches matches =
317 url_index_->HistoryItemsForTerms(ASCIIToUTF16("MORTGAGE RATE DROPS")); 379 url_index_->HistoryItemsForTerms(ASCIIToUTF16("MORTGAGE RATE DROPS"));
318 ASSERT_EQ(1U, matches.size()); 380 ASSERT_EQ(1U, matches.size());
319 381
320 // Verify that we got back the result we expected. 382 // Verify that we got back the result we expected.
321 EXPECT_EQ(1, matches[0].url_info.id()); 383 EXPECT_EQ(1, matches[0].url_info.id());
322 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708", 384 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708",
323 matches[0].url_info.url().spec()); 385 matches[0].url_info.url().spec());
324 EXPECT_EQ(ASCIIToUTF16( 386 EXPECT_EQ(ASCIIToUTF16(
325 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"), 387 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"),
326 matches[0].url_info.title()); 388 matches[0].url_info.title());
327 } 389 }
328 390
329 TEST_F(InMemoryURLIndexTest, TitleChange) { 391 TEST_F(InMemoryURLIndexTest, TitleChange) {
330 url_index_.reset(new InMemoryURLIndex(FilePath())); 392 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
331 url_index_->Init(this, "en,ja,hi,zh"); 393 url_index_->Init("en,ja,hi,zh");
394 url_index_->RebuildFromHistory(this);
332 395
333 // Verify current title terms retrieves desired item. 396 // Verify current title terms retrieves desired item.
334 string16 original_terms = 397 string16 original_terms =
335 ASCIIToUTF16("lebronomics could high taxes influence"); 398 ASCIIToUTF16("lebronomics could high taxes influence");
336 ScoredHistoryMatches matches = 399 ScoredHistoryMatches matches =
337 url_index_->HistoryItemsForTerms(original_terms); 400 url_index_->HistoryItemsForTerms(original_terms);
338 ASSERT_EQ(1U, matches.size()); 401 ASSERT_EQ(1U, matches.size());
339 402
340 // Verify that we got back the result we expected. 403 // Verify that we got back the result we expected.
341 const URLID expected_id = 3; 404 const URLID expected_id = 3;
342 EXPECT_EQ(expected_id, matches[0].url_info.id()); 405 EXPECT_EQ(expected_id, matches[0].url_info.id());
343 EXPECT_EQ("http://www.businessandmedia.org/articles/2010/20100708120415.aspx", 406 EXPECT_EQ("http://www.businessandmedia.org/articles/2010/20100708120415.aspx",
344 matches[0].url_info.url().spec()); 407 matches[0].url_info.url().spec());
345 EXPECT_EQ(ASCIIToUTF16( 408 EXPECT_EQ(ASCIIToUTF16(
346 "LeBronomics: Could High Taxes Influence James' Team Decision?"), 409 "LeBronomics: Could High Taxes Influence James' Team Decision?"),
347 matches[0].url_info.title()); 410 matches[0].url_info.title());
348 URLRow old_row(matches[0].url_info); 411 URLRow old_row(matches[0].url_info);
349 412
350 // Verify new title terms retrieves nothing. 413 // Verify new title terms retrieves nothing.
351 string16 new_terms = ASCIIToUTF16("does eat oats little lambs ivy"); 414 string16 new_terms = ASCIIToUTF16("does eat oats little lambs ivy");
352 matches = url_index_->HistoryItemsForTerms(new_terms); 415 matches = url_index_->HistoryItemsForTerms(new_terms);
353 ASSERT_EQ(0U, matches.size()); 416 ASSERT_EQ(0U, matches.size());
354 417
355 // Update the row. 418 // Update the row.
356 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy")); 419 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy"));
357 url_index_->UpdateURL(expected_id, old_row); 420 url_index_->UpdateURL(old_row);
358 421
359 // Verify we get the row using the new terms but not the original terms. 422 // Verify we get the row using the new terms but not the original terms.
360 matches = url_index_->HistoryItemsForTerms(new_terms); 423 matches = url_index_->HistoryItemsForTerms(new_terms);
361 ASSERT_EQ(1U, matches.size()); 424 ASSERT_EQ(1U, matches.size());
362 EXPECT_EQ(expected_id, matches[0].url_info.id()); 425 EXPECT_EQ(expected_id, matches[0].url_info.id());
363 matches = url_index_->HistoryItemsForTerms(original_terms); 426 matches = url_index_->HistoryItemsForTerms(original_terms);
364 ASSERT_EQ(0U, matches.size()); 427 ASSERT_EQ(0U, matches.size());
365 } 428 }
366 429
367 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) { 430 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) {
368 url_index_.reset(new InMemoryURLIndex(FilePath())); 431 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
369 url_index_->Init(this, "en,ja,hi,zh"); 432 url_index_->Init("en,ja,hi,zh");
433 url_index_->RebuildFromHistory(this);
370 434
371 // The presence of duplicate characters should succeed. Exercise by cycling 435 // The presence of duplicate characters should succeed. Exercise by cycling
372 // through a string with several duplicate characters. 436 // through a string with several duplicate characters.
373 ScoredHistoryMatches matches = 437 ScoredHistoryMatches matches =
374 url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRA")); 438 url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRA"));
375 ASSERT_EQ(1U, matches.size()); 439 ASSERT_EQ(1U, matches.size());
376 EXPECT_EQ(28, matches[0].url_info.id()); 440 EXPECT_EQ(28, matches[0].url_info.id());
377 EXPECT_EQ("http://www.ddj.com/windows/184416623", 441 EXPECT_EQ("http://www.ddj.com/windows/184416623",
378 matches[0].url_info.url().spec()); 442 matches[0].url_info.url().spec());
379 443
(...skipping 14 matching lines...) Expand all
394 EXPECT_EQ(28, matches[0].url_info.id()); 458 EXPECT_EQ(28, matches[0].url_info.id());
395 } 459 }
396 460
397 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { 461 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) {
398 // Verify that match results for previously typed characters are retained 462 // Verify that match results for previously typed characters are retained
399 // (in the term_char_word_set_cache_) and reused, if possible, in future 463 // (in the term_char_word_set_cache_) and reused, if possible, in future
400 // autocompletes. 464 // autocompletes.
401 typedef URLIndexPrivateData::SearchTermCacheMap::iterator CacheIter; 465 typedef URLIndexPrivateData::SearchTermCacheMap::iterator CacheIter;
402 typedef URLIndexPrivateData::SearchTermCacheItem CacheItem; 466 typedef URLIndexPrivateData::SearchTermCacheItem CacheItem;
403 467
404 url_index_.reset(new InMemoryURLIndex(FilePath())); 468 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
405 url_index_->Init(this, "en,ja,hi,zh"); 469 url_index_->Init("en,ja,hi,zh");
470 url_index_->RebuildFromHistory(this);
406 471
407 URLIndexPrivateData::SearchTermCacheMap& cache( 472 URLIndexPrivateData::SearchTermCacheMap& cache(
408 url_index_->private_data_->search_term_cache_); 473 url_index_->private_data_->search_term_cache_);
409 474
410 // The cache should be empty at this point. 475 // The cache should be empty at this point.
411 EXPECT_EQ(0U, cache.size()); 476 EXPECT_EQ(0U, cache.size());
412 477
413 // Now simulate typing search terms into the omnibox and check the state of 478 // Now simulate typing search terms into the omnibox and check the state of
414 // the cache as each item is 'typed'. 479 // the cache as each item is 'typed'.
415 480
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 URLIndexPrivateData::ScoredMatchForURL(row_c, Make1Term("abc"))); 547 URLIndexPrivateData::ScoredMatchForURL(row_c, Make1Term("abc")));
483 EXPECT_GT(scored_g.raw_score, scored_a.raw_score); 548 EXPECT_GT(scored_g.raw_score, scored_a.raw_score);
484 // Test scores based on typed_count. 549 // Test scores based on typed_count.
485 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); 550 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10));
486 ScoredHistoryMatch scored_h( 551 ScoredHistoryMatch scored_h(
487 URLIndexPrivateData::ScoredMatchForURL(row_d, Make1Term("abc"))); 552 URLIndexPrivateData::ScoredMatchForURL(row_d, Make1Term("abc")));
488 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); 553 EXPECT_GT(scored_h.raw_score, scored_a.raw_score);
489 } 554 }
490 555
491 TEST_F(InMemoryURLIndexTest, AddNewRows) { 556 TEST_F(InMemoryURLIndexTest, AddNewRows) {
492 url_index_.reset(new InMemoryURLIndex(FilePath())); 557 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
493 url_index_->Init(this, "en,ja,hi,zh"); 558 url_index_->Init("en,ja,hi,zh");
494 559
495 // Verify that the row we're going to add does not already exist. 560 // Verify that the row we're going to add does not already exist.
496 URLID new_row_id = 87654321; 561 URLID new_row_id = 87654321;
497 // Newly created URLRows get a last_visit time of 'right now' so it should 562 // Newly created URLRows get a last_visit time of 'right now' so it should
498 // qualify as a quick result candidate. 563 // qualify as a quick result candidate.
499 EXPECT_TRUE(url_index_->HistoryItemsForTerms( 564 EXPECT_TRUE(url_index_->HistoryItemsForTerms(
500 ASCIIToUTF16("brokeandalone")).empty()); 565 ASCIIToUTF16("brokeandalone")).empty());
501 566
502 // Add a new row. 567 // Add a new row.
503 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); 568 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id);
569 new_row.set_title(ASCIIToUTF16("Timothy Little and His Amazing Dog Boo"));
504 new_row.set_last_visit(base::Time::Now()); 570 new_row.set_last_visit(base::Time::Now());
505 url_index_->UpdateURL(new_row_id, new_row); 571 url_index_->UpdateURL(new_row);
506 572
507 // Verify that we can retrieve it. 573 // Verify that we can retrieve it.
508 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms( 574 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(
509 ASCIIToUTF16("brokeandalone")).size()); 575 ASCIIToUTF16("brokeandalone")).size());
510 576
577 // Verify that we can retrieve it by title.
578 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(
579 ASCIIToUTF16("Timothy Little")).size());
580
511 // Add it again just to be sure that is harmless. 581 // Add it again just to be sure that is harmless.
512 url_index_->UpdateURL(new_row_id, new_row); 582 url_index_->UpdateURL(new_row);
513 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms( 583 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(
514 ASCIIToUTF16("brokeandalone")).size()); 584 ASCIIToUTF16("brokeandalone")).size());
515 } 585 }
516 586
517 TEST_F(InMemoryURLIndexTest, DeleteRows) { 587 TEST_F(InMemoryURLIndexTest, DeleteRows) {
518 url_index_.reset(new InMemoryURLIndex(FilePath())); 588 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
519 url_index_->Init(this, "en,ja,hi,zh"); 589 url_index_->Init("en,ja,hi,zh");
590 url_index_->RebuildFromHistory(this);
520 591
521 ScoredHistoryMatches matches = 592 ScoredHistoryMatches matches =
522 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport")); 593 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"));
523 ASSERT_EQ(1U, matches.size()); 594 ASSERT_EQ(1U, matches.size());
524 595
525 // Determine the row id for that result, delete that id, then search again. 596 // Determine the row id for that result, delete that id, then search again.
526 url_index_->DeleteURL(matches[0].url_info.id()); 597 url_index_->DeleteURL(matches[0].url_info.url());
527 EXPECT_TRUE(url_index_->HistoryItemsForTerms( 598 EXPECT_TRUE(url_index_->HistoryItemsForTerms(
528 ASCIIToUTF16("DrudgeReport")).empty()); 599 ASCIIToUTF16("DrudgeReport")).empty());
529 } 600 }
530 601
531 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { 602 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) {
532 struct TestData { 603 struct TestData {
533 const std::string url_spec; 604 const std::string url_spec;
534 const bool expected_is_whitelisted; 605 const bool expected_is_whitelisted;
535 } data[] = { 606 } data[] = {
536 // URLs with whitelisted schemes. 607 // URLs with whitelisted schemes.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false }, 665 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false },
595 { "tftp://example.com/mystartupfile", false }, 666 { "tftp://example.com/mystartupfile", false },
596 { "tip://123.123.123.123/?urn:xopen:xid", false }, 667 { "tip://123.123.123.123/?urn:xopen:xid", false },
597 { "tv:nbc.com", false }, 668 { "tv:nbc.com", false },
598 { "urn:foo:A123,456", false }, 669 { "urn:foo:A123,456", false },
599 { "vemmi://zeus.mctel.fr/demo", false }, 670 { "vemmi://zeus.mctel.fr/demo", false },
600 { "wais://www.mydomain.net:8765/mydatabase", false }, 671 { "wais://www.mydomain.net:8765/mydatabase", false },
601 { "xmpp:node@example.com", false }, 672 { "xmpp:node@example.com", false },
602 { "xmpp://guest@example.com", false }, 673 { "xmpp://guest@example.com", false },
603 }; 674 };
604 url_index_.reset(new InMemoryURLIndex(FilePath())); 675 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
605 URLIndexPrivateData& private_data(*(url_index_->private_data_.get())); 676 URLIndexPrivateData& private_data(*(url_index_->private_data_.get()));
606 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 677 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
607 GURL url(data[i].url_spec); 678 GURL url(data[i].url_spec);
608 EXPECT_EQ(data[i].expected_is_whitelisted, 679 EXPECT_EQ(data[i].expected_is_whitelisted,
609 private_data.URLSchemeIsWhitelisted(url)); 680 private_data.URLSchemeIsWhitelisted(url));
610 } 681 }
611 } 682 }
612 683
613 TEST_F(InMemoryURLIndexTest, CacheFilePath) { 684 TEST_F(InMemoryURLIndexTest, CacheFilePath) {
614 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL( 685 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath(FILE_PATH_LITERAL(
615 "/flammmy/frammy/")))); 686 "/flammmy/frammy/")));
616 FilePath full_file_path; 687 FilePath full_file_path;
617 url_index_->GetCacheFilePath(&full_file_path); 688 url_index_->GetCacheFilePath(&full_file_path);
618 std::vector<FilePath::StringType> expected_parts; 689 std::vector<FilePath::StringType> expected_parts;
619 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")). 690 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")).
620 GetComponents(&expected_parts); 691 GetComponents(&expected_parts);
621 std::vector<FilePath::StringType> actual_parts; 692 std::vector<FilePath::StringType> actual_parts;
622 full_file_path.GetComponents(&actual_parts); 693 full_file_path.GetComponents(&actual_parts);
623 ASSERT_EQ(expected_parts.size(), actual_parts.size()); 694 ASSERT_EQ(expected_parts.size(), actual_parts.size());
624 size_t count = expected_parts.size(); 695 size_t count = expected_parts.size();
625 for (size_t i = 0; i < count; ++i) 696 for (size_t i = 0; i < count; ++i)
626 EXPECT_EQ(expected_parts[i], actual_parts[i]); 697 EXPECT_EQ(expected_parts[i], actual_parts[i]);
627 // Must clear the history_dir_ to satisfy the dtor's DCHECK. 698 // Must clear the history_dir_ to satisfy the dtor's DCHECK.
628 url_index_->history_dir_.clear(); 699 url_index_->history_dir_.clear();
629 } 700 }
630 701
631 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { 702 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) {
703 MessageLoop message_loop;
704 content::TestBrowserThread fake_ui_thread(BrowserThread::UI, &message_loop);
705 content::TestBrowserThread fake_file_thread(BrowserThread::FILE,
706 &message_loop);
707
632 // Save the cache to a protobuf, restore it, and compare the results. 708 // Save the cache to a protobuf, restore it, and compare the results.
633 url_index_.reset(new InMemoryURLIndex(FilePath())); 709 url_index_ = new InMemoryURLIndex(profile_.get(), FilePath());
634 InMemoryURLIndex& url_index(*(url_index_.get())); 710 InMemoryURLIndex& url_index(*(url_index_.get()));
635 url_index.Init(this, "en,ja,hi,zh"); 711 url_index.Init("en,ja,hi,zh");
636 in_memory_url_index::InMemoryURLIndexCacheItem index_cache; 712 url_index.RebuildFromHistory(this);
637 URLIndexPrivateData& private_data(*(url_index_->private_data_.get()));
638 private_data.SavePrivateData(&index_cache);
639 713
640 // Capture our private data so we can later compare for equality. 714 URLIndexPrivateData& private_data(*(url_index_->private_data_));
641 String16Vector word_list(private_data.word_list_);
642 WordMap word_map(private_data.word_map_);
643 CharWordIDMap char_word_map(private_data.char_word_map_);
644 WordIDHistoryMap word_id_history_map(private_data.word_id_history_map_);
645 HistoryIDWordMap history_id_word_map(private_data.history_id_word_map_);
646 HistoryInfoMap history_info_map(private_data.history_info_map_);
647 715
648 // Prove that there is really something there. 716 // Ensure that there is really something there to be saved.
649 EXPECT_FALSE(private_data.word_list_.empty()); 717 EXPECT_FALSE(private_data.word_list_.empty());
650 // available_words_ will already be empty since we have freshly built the 718 // available_words_ will already be empty since we have freshly built the
651 // data set for this test. 719 // data set for this test.
652 EXPECT_TRUE(private_data.available_words_.empty()); 720 EXPECT_TRUE(private_data.available_words_.empty());
653 EXPECT_FALSE(private_data.word_map_.empty()); 721 EXPECT_FALSE(private_data.word_map_.empty());
654 EXPECT_FALSE(private_data.char_word_map_.empty()); 722 EXPECT_FALSE(private_data.char_word_map_.empty());
655 EXPECT_FALSE(private_data.word_id_history_map_.empty()); 723 EXPECT_FALSE(private_data.word_id_history_map_.empty());
656 EXPECT_FALSE(private_data.history_id_word_map_.empty()); 724 EXPECT_FALSE(private_data.history_id_word_map_.empty());
657 EXPECT_FALSE(private_data.history_info_map_.empty()); 725 EXPECT_FALSE(private_data.history_info_map_.empty());
658 726
659 // Clear and then prove it's clear. 727 // Capture the current private data for later comparison to restored data.
660 private_data.Clear(); 728 URLIndexPrivateData old_data(*(url_index_->private_data_));
729
730 // Save then restore our private data.
731 ScopedTempDir temp_directory;
732 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
733 FilePath temp_file;
734 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory.path(),
735 &temp_file));
736 CacheFileSaverObserver save_observer(&message_loop);
737 url_index_->set_save_cache_observer(&save_observer);
738 url_index.DoSaveToCacheFile(temp_file);
739 message_loop.Run();
740
741 // Clear and then prove it's clear before restoring.
742 url_index.ClearPrivateData();
661 EXPECT_TRUE(private_data.word_list_.empty()); 743 EXPECT_TRUE(private_data.word_list_.empty());
662 EXPECT_TRUE(private_data.available_words_.empty()); 744 EXPECT_TRUE(private_data.available_words_.empty());
663 EXPECT_TRUE(private_data.word_map_.empty()); 745 EXPECT_TRUE(private_data.word_map_.empty());
664 EXPECT_TRUE(private_data.char_word_map_.empty()); 746 EXPECT_TRUE(private_data.char_word_map_.empty());
665 EXPECT_TRUE(private_data.word_id_history_map_.empty()); 747 EXPECT_TRUE(private_data.word_id_history_map_.empty());
666 EXPECT_TRUE(private_data.history_id_word_map_.empty()); 748 EXPECT_TRUE(private_data.history_id_word_map_.empty());
667 EXPECT_TRUE(private_data.history_info_map_.empty()); 749 EXPECT_TRUE(private_data.history_info_map_.empty());
668 750
669 // Restore the cache. 751 CacheFileReaderObserver read_observer(&message_loop);
670 EXPECT_TRUE(private_data.RestorePrivateData(index_cache)); 752 url_index_->set_restore_cache_observer(&read_observer);
753 url_index.DoRestoreFromCacheFile(temp_file);
754 message_loop.Run();
671 755
672 // Compare the restored and captured for equality. 756 URLIndexPrivateData& new_data(*(url_index_->private_data_));
673 EXPECT_EQ(word_list.size(), private_data.word_list_.size()); 757
674 EXPECT_EQ(word_map.size(), private_data.word_map_.size()); 758 // Compare the captured and restored for equality.
675 EXPECT_EQ(char_word_map.size(), private_data.char_word_map_.size()); 759 EXPECT_EQ(old_data.word_list_.size(), new_data.word_list_.size());
676 EXPECT_EQ(word_id_history_map.size(), 760 EXPECT_EQ(old_data.word_map_.size(), new_data.word_map_.size());
677 private_data.word_id_history_map_.size()); 761 EXPECT_EQ(old_data.char_word_map_.size(), new_data.char_word_map_.size());
678 EXPECT_EQ(history_id_word_map.size(), 762 EXPECT_EQ(old_data.word_id_history_map_.size(),
679 private_data.history_id_word_map_.size()); 763 new_data.word_id_history_map_.size());
680 EXPECT_EQ(history_info_map.size(), private_data.history_info_map_.size()); 764 EXPECT_EQ(old_data.history_id_word_map_.size(),
765 new_data.history_id_word_map_.size());
766 EXPECT_EQ(old_data.history_info_map_.size(),
767 new_data.history_info_map_.size());
681 // WordList must be index-by-index equal. 768 // WordList must be index-by-index equal.
682 size_t count = word_list.size(); 769 size_t count = old_data.word_list_.size();
683 for (size_t i = 0; i < count; ++i) 770 for (size_t i = 0; i < count; ++i)
684 EXPECT_EQ(word_list[i], private_data.word_list_[i]); 771 EXPECT_EQ(old_data.word_list_[i], new_data.word_list_[i]);
685 772
686 ExpectMapOfContainersIdentical(char_word_map, 773 ExpectMapOfContainersIdentical(old_data.char_word_map_,
687 private_data.char_word_map_); 774 new_data.char_word_map_);
688 ExpectMapOfContainersIdentical(word_id_history_map, 775 ExpectMapOfContainersIdentical(old_data.word_id_history_map_,
689 private_data.word_id_history_map_); 776 new_data.word_id_history_map_);
690 ExpectMapOfContainersIdentical(history_id_word_map, 777 ExpectMapOfContainersIdentical(old_data.history_id_word_map_,
691 private_data.history_id_word_map_); 778 new_data.history_id_word_map_);
692 779
693 for (HistoryInfoMap::const_iterator expected = history_info_map.begin(); 780 for (HistoryInfoMap::const_iterator expected =
694 expected != history_info_map.end(); ++expected) { 781 old_data.history_info_map_.begin();
782 expected != old_data.history_info_map_.end(); ++expected) {
695 HistoryInfoMap::const_iterator actual = 783 HistoryInfoMap::const_iterator actual =
696 private_data.history_info_map_.find(expected->first); 784 new_data.history_info_map_.find(expected->first);
697 ASSERT_FALSE(private_data.history_info_map_.end() == actual); 785 ASSERT_FALSE(new_data.history_info_map_.end() == actual);
698 const URLRow& expected_row(expected->second); 786 const URLRow& expected_row(expected->second);
699 const URLRow& actual_row(actual->second); 787 const URLRow& actual_row(actual->second);
700 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); 788 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count());
701 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); 789 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count());
702 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); 790 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit());
703 EXPECT_EQ(expected_row.url(), actual_row.url()); 791 EXPECT_EQ(expected_row.url(), actual_row.url());
704 } 792 }
705 } 793 }
706 794
707 } // namespace history 795 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698