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

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

Issue 9316109: Move Ownership of IMUI to HistoryService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 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/string_util.h"
10 #include "base/string16.h" 12 #include "base/string16.h"
11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/autocomplete/autocomplete.h" 14 #include "chrome/browser/autocomplete/autocomplete.h"
14 #include "chrome/browser/history/in_memory_database.h" 15 #include "chrome/browser/history/history_backend.h"
16 #include "chrome/browser/history/history_database.h"
17 #include "chrome/browser/history/history.h"
Peter Kasting 2012/02/04 02:20:31 Nit: This goes above other two
mrossetti 2012/02/04 04:30:53 Done.
18 #include "chrome/browser/history/in_memory_url_index_types.h"
15 #include "chrome/browser/history/in_memory_url_index.h" 19 #include "chrome/browser/history/in_memory_url_index.h"
16 #include "chrome/browser/history/in_memory_url_index_types.h" 20 #include "chrome/browser/history/url_index_private_data.h"
17 #include "chrome/common/chrome_paths.h" 21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/test/test_browser_thread.h"
18 #include "sql/transaction.h" 24 #include "sql/transaction.h"
19 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
20 26
21 // The test version of the history url database table ('url') is contained in 27 // 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'). 28 // 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 29 // 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 30 // 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 31 // number specifying the number of days relative to 'today' to which the
26 // absolute time should be set during the test setup stage. 32 // absolute time should be set during the test setup stage.
27 // 33 //
28 // The format of the test database text file is of a SQLite .dump file. 34 // 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 35 // Note that only lines whose first character is an upper-case letter are
30 // processed when creating the test database. 36 // processed when creating the test database.
31 37
32 namespace history { 38 namespace history {
33 39
34 class InMemoryURLIndexTest : public testing::Test, 40 class InMemoryURLIndexTest : public testing::Test {
35 public InMemoryDatabase {
36 public: 41 public:
37 InMemoryURLIndexTest() { InitFromScratch(); } 42 InMemoryURLIndexTest();
38 43
39 protected: 44 protected:
40 // Test setup. 45 // Test setup.
41 virtual void SetUp(); 46 virtual void SetUp();
42 47
43 // Allows the database containing the test data to be customized by 48 // Allows the database containing the test data to be customized by
44 // subclasses. 49 // subclasses.
45 virtual FilePath::StringType TestDBName() const; 50 virtual FilePath::StringType TestDBName() const;
46 51
47 // Convenience function to create a URLRow with basic data for |url|, |title|, 52 // Convenience function to create a URLRow with basic data for |url|, |title|,
48 // |visit_count|, and |typed_count|. |last_visit_ago| gives the number of 53 // |visit_count|, and |typed_count|. |last_visit_ago| gives the number of
49 // days from now to set the URL's last_visit. 54 // days from now to set the URL's last_visit.
50 URLRow MakeURLRow(const char* url, 55 URLRow MakeURLRow(const char* url,
51 const char* title, 56 const char* title,
52 int visit_count, 57 int visit_count,
53 int last_visit_ago, 58 int last_visit_ago,
54 int typed_count); 59 int typed_count);
55 60
56 // Convenience functions for easily creating vectors of search terms. 61 // Convenience functions for easily creating vectors of search terms.
57 String16Vector Make1Term(const char* term) const; 62 String16Vector Make1Term(const char* term) const;
58 String16Vector Make2Terms(const char* term_1, const char* term_2) const; 63 String16Vector Make2Terms(const char* term_1, const char* term_2) const;
59 64
60 // Validates that the given |term| is contained in |cache| and that it is 65 // Validates that the given |term| is contained in |cache| and that it is
61 // marked as in-use. 66 // marked as in-use.
62 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, 67 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache,
63 string16 term) const; 68 string16 term) const;
64 69
70 // Pass-through functions to simplify our friendship with InMemoryURLIndex
71 // and HistoryService.
72 sql::Connection& GetDB();
73 URLIndexPrivateData* GetPrivateData() const;
74 bool GetCacheFilePath(FilePath* file_path) const;
75 void ClearHistoryDir() const;
76
77 MessageLoopForUI message_loop_;
78 content::TestBrowserThread ui_thread_;
79 content::TestBrowserThread file_thread_;
80 TestingProfile profile_;
81
65 scoped_ptr<InMemoryURLIndex> url_index_; 82 scoped_ptr<InMemoryURLIndex> url_index_;
83 HistoryDatabase* history_database_;
66 }; 84 };
67 85
86 InMemoryURLIndexTest::InMemoryURLIndexTest()
87 : ui_thread_(content::BrowserThread::UI, &message_loop_),
88 file_thread_(content::BrowserThread::FILE, &message_loop_) {
89 }
90
91 URLIndexPrivateData* InMemoryURLIndexTest::GetPrivateData() const {
92 DCHECK(url_index_->private_data_.get());
93 return url_index_->private_data_.get();
94 }
95
96 bool InMemoryURLIndexTest::GetCacheFilePath(FilePath* file_path) const {
97 DCHECK(file_path);
98 return url_index_->GetCacheFilePath(file_path);
99 }
100
101 void InMemoryURLIndexTest::ClearHistoryDir() const {
102 url_index_->history_dir_.clear();
103 }
104
105 sql::Connection& InMemoryURLIndexTest::GetDB() {
106 return history_database_->GetDB();
107 }
108
68 void InMemoryURLIndexTest::SetUp() { 109 void InMemoryURLIndexTest::SetUp() {
110 // We cannot access the database until the backend has been loaded.
111 profile_.CreateHistoryService(true, false);
112 profile_.CreateBookmarkModel(true);
113 profile_.BlockUntilBookmarkModelLoaded();
114 profile_.BlockUntilHistoryProcessesPendingRequests();
115 HistoryService* history_service =
116 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS);
117 ASSERT_TRUE(history_service);
118 HistoryBackend* backend = history_service->history_backend_.get();
119 history_database_ = backend->db();
120
69 // Create and populate a working copy of the URL history database. 121 // Create and populate a working copy of the URL history database.
70 FilePath history_proto_path; 122 FilePath history_proto_path;
71 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path); 123 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path);
72 history_proto_path = history_proto_path.Append( 124 history_proto_path = history_proto_path.Append(
73 FILE_PATH_LITERAL("History")); 125 FILE_PATH_LITERAL("History"));
74 history_proto_path = history_proto_path.Append(TestDBName()); 126 history_proto_path = history_proto_path.Append(TestDBName());
75 EXPECT_TRUE(file_util::PathExists(history_proto_path)); 127 EXPECT_TRUE(file_util::PathExists(history_proto_path));
76 128
77 std::ifstream proto_file(history_proto_path.value().c_str()); 129 std::ifstream proto_file(history_proto_path.value().c_str());
78 static const size_t kCommandBufferMaxSize = 2048; 130 static const size_t kCommandBufferMaxSize = 2048;
79 char sql_cmd_line[kCommandBufferMaxSize]; 131 char sql_cmd_line[kCommandBufferMaxSize];
80 132
81 sql::Connection& db(GetDB()); 133 sql::Connection& db(GetDB());
134 ASSERT_TRUE(db.is_open());
82 { 135 {
83 sql::Transaction transaction(&db); 136 sql::Transaction transaction(&db);
84 transaction.Begin(); 137 transaction.Begin();
85 while (!proto_file.eof()) { 138 while (!proto_file.eof()) {
86 proto_file.getline(sql_cmd_line, kCommandBufferMaxSize); 139 proto_file.getline(sql_cmd_line, kCommandBufferMaxSize);
87 if (!proto_file.eof()) { 140 if (!proto_file.eof()) {
88 // We only process lines which begin with a upper-case letter. 141 // We only process lines which begin with a upper-case letter.
89 // TODO(mrossetti): Can iswupper() be used here? 142 // TODO(mrossetti): Can iswupper() be used here?
90 if (sql_cmd_line[0] >= 'A' && sql_cmd_line[0] <= 'Z') { 143 if (sql_cmd_line[0] >= 'A' && sql_cmd_line[0] <= 'Z') {
91 std::string sql_cmd(sql_cmd_line); 144 std::string sql_cmd(sql_cmd_line);
(...skipping 11 matching lines...) Expand all
103 sql::Statement statement(db.GetUniqueStatement( 156 sql::Statement statement(db.GetUniqueStatement(
104 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls;")); 157 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls;"));
105 ASSERT_TRUE(statement.is_valid()); 158 ASSERT_TRUE(statement.is_valid());
106 base::Time time_right_now = base::Time::NowFromSystemTime(); 159 base::Time time_right_now = base::Time::NowFromSystemTime();
107 base::TimeDelta day_delta = base::TimeDelta::FromDays(1); 160 base::TimeDelta day_delta = base::TimeDelta::FromDays(1);
108 { 161 {
109 sql::Transaction transaction(&db); 162 sql::Transaction transaction(&db);
110 transaction.Begin(); 163 transaction.Begin();
111 while (statement.Step()) { 164 while (statement.Step()) {
112 URLRow row; 165 URLRow row;
113 FillURLRow(statement, &row); 166 history_database_->FillURLRow(statement, &row);
114 base::Time last_visit = time_right_now; 167 base::Time last_visit = time_right_now;
115 for (int64 i = row.last_visit().ToInternalValue(); i > 0; --i) 168 for (int64 i = row.last_visit().ToInternalValue(); i > 0; --i)
116 last_visit -= day_delta; 169 last_visit -= day_delta;
117 row.set_last_visit(last_visit); 170 row.set_last_visit(last_visit);
118 UpdateURLRow(row.id(), row); 171 history_database_->UpdateURLRow(row.id(), row);
119 } 172 }
120 transaction.Commit(); 173 transaction.Commit();
121 } 174 }
175
176 url_index_.reset(
177 new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh"));
178 url_index_->Init();
179 url_index_->RebuildFromHistory(history_database_);
122 } 180 }
123 181
124 FilePath::StringType InMemoryURLIndexTest::TestDBName() const { 182 FilePath::StringType InMemoryURLIndexTest::TestDBName() const {
125 return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); 183 return FILE_PATH_LITERAL("url_history_provider_test.db.txt");
126 } 184 }
127 185
128 URLRow InMemoryURLIndexTest::MakeURLRow(const char* url, 186 URLRow InMemoryURLIndexTest::MakeURLRow(const char* url,
129 const char* title, 187 const char* title,
130 int visit_count, 188 int visit_count,
131 int last_visit_ago, 189 int last_visit_ago,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 246
189 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { 247 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest {
190 protected: 248 protected:
191 FilePath::StringType TestDBName() const; 249 FilePath::StringType TestDBName() const;
192 }; 250 };
193 251
194 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { 252 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const {
195 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); 253 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt");
196 } 254 }
197 255
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) { 256 TEST_F(LimitedInMemoryURLIndexTest, Initialization) {
204 // Verify that the database contains the expected number of items, which 257 // Verify that the database contains the expected number of items, which
205 // is the pre-filtered count, i.e. all of the items. 258 // is the pre-filtered count, i.e. all of the items.
206 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); 259 sql::Connection& db(GetDB());
260 sql::Statement statement(db.GetUniqueStatement("SELECT * FROM urls;"));
207 ASSERT_TRUE(statement.is_valid()); 261 ASSERT_TRUE(statement.is_valid());
208 uint64 row_count = 0; 262 uint64 row_count = 0;
209 while (statement.Step()) ++row_count; 263 while (statement.Step()) ++row_count;
210 EXPECT_EQ(1U, row_count); 264 EXPECT_EQ(1U, row_count);
211 url_index_.reset(new InMemoryURLIndex(FilePath())); 265 url_index_.reset(
212 url_index_->Init(this, "en,ja,hi,zh"); 266 new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh"));
267 url_index_->Init();
268 url_index_->RebuildFromHistory(history_database_);
213 URLIndexPrivateData& private_data(*(url_index_->private_data_)); 269 URLIndexPrivateData& private_data(*(url_index_->private_data_));
214 270
215 // history_info_map_ should have the same number of items as were filtered. 271 // history_info_map_ should have the same number of items as were filtered.
216 EXPECT_EQ(1U, private_data.history_info_map_.size()); 272 EXPECT_EQ(1U, private_data.history_info_map_.size());
217 EXPECT_EQ(35U, private_data.char_word_map_.size()); 273 EXPECT_EQ(35U, private_data.char_word_map_.size());
218 EXPECT_EQ(17U, private_data.word_map_.size()); 274 EXPECT_EQ(17U, private_data.word_map_.size());
219 } 275 }
220 276
221 TEST_F(InMemoryURLIndexTest, Retrieval) { 277 TEST_F(InMemoryURLIndexTest, Retrieval) {
222 url_index_.reset(new InMemoryURLIndex(FilePath()));
223 url_index_->Init(this, "en,ja,hi,zh");
224 // The term will be lowercased by the search.
225
226 // See if a very specific term gives a single result. 278 // See if a very specific term gives a single result.
227 ScoredHistoryMatches matches = 279 ScoredHistoryMatches matches =
228 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport")); 280 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"));
229 ASSERT_EQ(1U, matches.size()); 281 ASSERT_EQ(1U, matches.size());
230 282
231 // Verify that we got back the result we expected. 283 // Verify that we got back the result we expected.
232 EXPECT_EQ(5, matches[0].url_info.id()); 284 EXPECT_EQ(5, matches[0].url_info.id());
233 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec()); 285 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec());
234 EXPECT_EQ(ASCIIToUTF16("DRUDGE REPORT 2010"), matches[0].url_info.title()); 286 EXPECT_EQ(ASCIIToUTF16("DRUDGE REPORT 2010"), matches[0].url_info.title());
235 EXPECT_TRUE(matches[0].can_inline); 287 EXPECT_TRUE(matches[0].can_inline);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("fubar")); 337 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("fubar"));
286 ASSERT_EQ(1U, matches.size()); 338 ASSERT_EQ(1U, matches.size());
287 EXPECT_EQ(34, matches[0].url_info.id()); 339 EXPECT_EQ(34, matches[0].url_info.id());
288 EXPECT_EQ("http://fubarfubarandfubar.com/", matches[0].url_info.url().spec()); 340 EXPECT_EQ("http://fubarfubarandfubar.com/", matches[0].url_info.url().spec());
289 EXPECT_EQ(ASCIIToUTF16("Situation Normal -- FUBARED"), 341 EXPECT_EQ(ASCIIToUTF16("Situation Normal -- FUBARED"),
290 matches[0].url_info.title()); 342 matches[0].url_info.title());
291 EXPECT_TRUE(matches[0].can_inline); 343 EXPECT_TRUE(matches[0].can_inline);
292 } 344 }
293 345
294 TEST_F(InMemoryURLIndexTest, URLPrefixMatching) { 346 TEST_F(InMemoryURLIndexTest, URLPrefixMatching) {
295 url_index_.reset(new InMemoryURLIndex(FilePath()));
296 url_index_->Init(this, "en,ja,hi,zh");
297
298 // "drudgere" - found, can inline 347 // "drudgere" - found, can inline
299 ScoredHistoryMatches matches = 348 ScoredHistoryMatches matches =
300 url_index_->HistoryItemsForTerms(ASCIIToUTF16("drudgere")); 349 url_index_->HistoryItemsForTerms(ASCIIToUTF16("drudgere"));
301 ASSERT_EQ(1U, matches.size()); 350 ASSERT_EQ(1U, matches.size());
302 EXPECT_TRUE(matches[0].can_inline); 351 EXPECT_TRUE(matches[0].can_inline);
303 352
304 // "http://drudgere" - found, can inline 353 // "http://drudgere" - found, can inline
305 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("http://drudgere")); 354 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("http://drudgere"));
306 ASSERT_EQ(1U, matches.size()); 355 ASSERT_EQ(1U, matches.size());
307 EXPECT_TRUE(matches[0].can_inline); 356 EXPECT_TRUE(matches[0].can_inline);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 EXPECT_TRUE(matches[0].can_inline); 397 EXPECT_TRUE(matches[0].can_inline);
349 398
350 // "tp://www.cnn.com" - found, cannot inline 399 // "tp://www.cnn.com" - found, cannot inline
351 matches = 400 matches =
352 url_index_->HistoryItemsForTerms(ASCIIToUTF16("tp://www.cnn.com")); 401 url_index_->HistoryItemsForTerms(ASCIIToUTF16("tp://www.cnn.com"));
353 ASSERT_EQ(1U, matches.size()); 402 ASSERT_EQ(1U, matches.size());
354 EXPECT_TRUE(!matches[0].can_inline); 403 EXPECT_TRUE(!matches[0].can_inline);
355 } 404 }
356 405
357 TEST_F(InMemoryURLIndexTest, ProperStringMatching) { 406 TEST_F(InMemoryURLIndexTest, ProperStringMatching) {
358 url_index_.reset(new InMemoryURLIndex(FilePath()));
359 url_index_->Init(this, "en,ja,hi,zh");
360
361 // Search for the following with the expected results: 407 // Search for the following with the expected results:
362 // "atdmt view" - found 408 // "atdmt view" - found
363 // "atdmt.view" - not found 409 // "atdmt.view" - not found
364 // "view.atdmt" - found 410 // "view.atdmt" - found
365 ScoredHistoryMatches matches = 411 ScoredHistoryMatches matches =
366 url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt view")); 412 url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt view"));
367 ASSERT_EQ(1U, matches.size()); 413 ASSERT_EQ(1U, matches.size());
368 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view")); 414 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("atdmt.view"));
369 ASSERT_EQ(0U, matches.size()); 415 ASSERT_EQ(0U, matches.size());
370 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt")); 416 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("view.atdmt"));
371 ASSERT_EQ(1U, matches.size()); 417 ASSERT_EQ(1U, matches.size());
372 } 418 }
373 419
374 TEST_F(InMemoryURLIndexTest, HugeResultSet) { 420 TEST_F(InMemoryURLIndexTest, HugeResultSet) {
375 url_index_.reset(new InMemoryURLIndex(FilePath()));
376 url_index_->Init(this, "en,ja,hi,zh");
377
378 // Create a huge set of qualifying history items. 421 // Create a huge set of qualifying history items.
379 for (URLID row_id = 5000; row_id < 6000; ++row_id) { 422 for (URLID row_id = 5000; row_id < 6000; ++row_id) {
380 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), row_id); 423 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), row_id);
381 new_row.set_last_visit(base::Time::Now()); 424 new_row.set_last_visit(base::Time::Now());
382 url_index_->UpdateURL(row_id, new_row); 425 EXPECT_TRUE(url_index_->UpdateURL(new_row));
383 } 426 }
384 427
385 ScoredHistoryMatches matches = 428 ScoredHistoryMatches matches =
386 url_index_->HistoryItemsForTerms(ASCIIToUTF16("b")); 429 url_index_->HistoryItemsForTerms(ASCIIToUTF16("b"));
387 ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size()); 430 ASSERT_EQ(AutocompleteProvider::kMaxMatches, matches.size());
388 // There are 7 matches already in the database. 431 // There are 7 matches already in the database.
389 URLIndexPrivateData& private_data(*(url_index_->private_data_.get())); 432 URLIndexPrivateData& private_data(*GetPrivateData());
390 ASSERT_EQ(1008U, private_data.pre_filter_item_count_); 433 ASSERT_EQ(1008U, private_data.pre_filter_item_count_);
391 ASSERT_EQ(500U, private_data.post_filter_item_count_); 434 ASSERT_EQ(500U, private_data.post_filter_item_count_);
392 ASSERT_EQ(AutocompleteProvider::kMaxMatches, 435 ASSERT_EQ(AutocompleteProvider::kMaxMatches,
393 private_data.post_scoring_item_count_); 436 private_data.post_scoring_item_count_);
394 } 437 }
395 438
396 TEST_F(InMemoryURLIndexTest, TitleSearch) { 439 TEST_F(InMemoryURLIndexTest, TitleSearch) {
397 url_index_.reset(new InMemoryURLIndex(FilePath()));
398 url_index_->Init(this, "en,ja,hi,zh");
399 // Signal if someone has changed the test DB. 440 // Signal if someone has changed the test DB.
400 EXPECT_EQ(28U, url_index_->private_data_->history_info_map_.size()); 441 EXPECT_EQ(28U, GetPrivateData()->history_info_map_.size());
401 442
402 // Ensure title is being searched. 443 // Ensure title is being searched.
403 ScoredHistoryMatches matches = 444 ScoredHistoryMatches matches =
404 url_index_->HistoryItemsForTerms(ASCIIToUTF16("MORTGAGE RATE DROPS")); 445 url_index_->HistoryItemsForTerms(ASCIIToUTF16("MORTGAGE RATE DROPS"));
405 ASSERT_EQ(1U, matches.size()); 446 ASSERT_EQ(1U, matches.size());
406 447
407 // Verify that we got back the result we expected. 448 // Verify that we got back the result we expected.
408 EXPECT_EQ(1, matches[0].url_info.id()); 449 EXPECT_EQ(1, matches[0].url_info.id());
409 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708", 450 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708",
410 matches[0].url_info.url().spec()); 451 matches[0].url_info.url().spec());
411 EXPECT_EQ(ASCIIToUTF16( 452 EXPECT_EQ(ASCIIToUTF16(
412 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"), 453 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"),
413 matches[0].url_info.title()); 454 matches[0].url_info.title());
414 } 455 }
415 456
416 TEST_F(InMemoryURLIndexTest, TitleChange) { 457 TEST_F(InMemoryURLIndexTest, TitleChange) {
417 url_index_.reset(new InMemoryURLIndex(FilePath()));
418 url_index_->Init(this, "en,ja,hi,zh");
419
420 // Verify current title terms retrieves desired item. 458 // Verify current title terms retrieves desired item.
421 string16 original_terms = 459 string16 original_terms =
422 ASCIIToUTF16("lebronomics could high taxes influence"); 460 ASCIIToUTF16("lebronomics could high taxes influence");
423 ScoredHistoryMatches matches = 461 ScoredHistoryMatches matches =
424 url_index_->HistoryItemsForTerms(original_terms); 462 url_index_->HistoryItemsForTerms(original_terms);
425 ASSERT_EQ(1U, matches.size()); 463 ASSERT_EQ(1U, matches.size());
426 464
427 // Verify that we got back the result we expected. 465 // Verify that we got back the result we expected.
428 const URLID expected_id = 3; 466 const URLID expected_id = 3;
429 EXPECT_EQ(expected_id, matches[0].url_info.id()); 467 EXPECT_EQ(expected_id, matches[0].url_info.id());
430 EXPECT_EQ("http://www.businessandmedia.org/articles/2010/20100708120415.aspx", 468 EXPECT_EQ("http://www.businessandmedia.org/articles/2010/20100708120415.aspx",
431 matches[0].url_info.url().spec()); 469 matches[0].url_info.url().spec());
432 EXPECT_EQ(ASCIIToUTF16( 470 EXPECT_EQ(ASCIIToUTF16(
433 "LeBronomics: Could High Taxes Influence James' Team Decision?"), 471 "LeBronomics: Could High Taxes Influence James' Team Decision?"),
434 matches[0].url_info.title()); 472 matches[0].url_info.title());
435 URLRow old_row(matches[0].url_info); 473 URLRow old_row(matches[0].url_info);
436 474
437 // Verify new title terms retrieves nothing. 475 // Verify new title terms retrieves nothing.
438 string16 new_terms = ASCIIToUTF16("does eat oats little lambs ivy"); 476 string16 new_terms = ASCIIToUTF16("does eat oats little lambs ivy");
439 matches = url_index_->HistoryItemsForTerms(new_terms); 477 matches = url_index_->HistoryItemsForTerms(new_terms);
440 ASSERT_EQ(0U, matches.size()); 478 ASSERT_EQ(0U, matches.size());
441 479
442 // Update the row. 480 // Update the row.
443 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy")); 481 old_row.set_title(ASCIIToUTF16("Does eat oats and little lambs eat ivy"));
444 url_index_->UpdateURL(expected_id, old_row); 482 EXPECT_TRUE(url_index_->UpdateURL(old_row));
445 483
446 // Verify we get the row using the new terms but not the original terms. 484 // Verify we get the row using the new terms but not the original terms.
447 matches = url_index_->HistoryItemsForTerms(new_terms); 485 matches = url_index_->HistoryItemsForTerms(new_terms);
448 ASSERT_EQ(1U, matches.size()); 486 ASSERT_EQ(1U, matches.size());
449 EXPECT_EQ(expected_id, matches[0].url_info.id()); 487 EXPECT_EQ(expected_id, matches[0].url_info.id());
450 matches = url_index_->HistoryItemsForTerms(original_terms); 488 matches = url_index_->HistoryItemsForTerms(original_terms);
451 ASSERT_EQ(0U, matches.size()); 489 ASSERT_EQ(0U, matches.size());
452 } 490 }
453 491
454 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) { 492 TEST_F(InMemoryURLIndexTest, NonUniqueTermCharacterSets) {
455 url_index_.reset(new InMemoryURLIndex(FilePath()));
456 url_index_->Init(this, "en,ja,hi,zh");
457
458 // The presence of duplicate characters should succeed. Exercise by cycling 493 // The presence of duplicate characters should succeed. Exercise by cycling
459 // through a string with several duplicate characters. 494 // through a string with several duplicate characters.
460 ScoredHistoryMatches matches = 495 ScoredHistoryMatches matches =
461 url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRA")); 496 url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRA"));
462 ASSERT_EQ(1U, matches.size()); 497 ASSERT_EQ(1U, matches.size());
463 EXPECT_EQ(28, matches[0].url_info.id()); 498 EXPECT_EQ(28, matches[0].url_info.id());
464 EXPECT_EQ("http://www.ddj.com/windows/184416623", 499 EXPECT_EQ("http://www.ddj.com/windows/184416623",
465 matches[0].url_info.url().spec()); 500 matches[0].url_info.url().spec());
466 501
467 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRACAD")); 502 matches = url_index_->HistoryItemsForTerms(ASCIIToUTF16("ABRACAD"));
(...skipping 13 matching lines...) Expand all
481 EXPECT_EQ(28, matches[0].url_info.id()); 516 EXPECT_EQ(28, matches[0].url_info.id());
482 } 517 }
483 518
484 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { 519 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) {
485 // Verify that match results for previously typed characters are retained 520 // Verify that match results for previously typed characters are retained
486 // (in the term_char_word_set_cache_) and reused, if possible, in future 521 // (in the term_char_word_set_cache_) and reused, if possible, in future
487 // autocompletes. 522 // autocompletes.
488 typedef URLIndexPrivateData::SearchTermCacheMap::iterator CacheIter; 523 typedef URLIndexPrivateData::SearchTermCacheMap::iterator CacheIter;
489 typedef URLIndexPrivateData::SearchTermCacheItem CacheItem; 524 typedef URLIndexPrivateData::SearchTermCacheItem CacheItem;
490 525
491 url_index_.reset(new InMemoryURLIndex(FilePath()));
492 url_index_->Init(this, "en,ja,hi,zh");
493
494 URLIndexPrivateData::SearchTermCacheMap& cache( 526 URLIndexPrivateData::SearchTermCacheMap& cache(
495 url_index_->private_data_->search_term_cache_); 527 GetPrivateData()->search_term_cache_);
496 528
497 // The cache should be empty at this point. 529 // The cache should be empty at this point.
498 EXPECT_EQ(0U, cache.size()); 530 EXPECT_EQ(0U, cache.size());
499 531
500 // Now simulate typing search terms into the omnibox and check the state of 532 // Now simulate typing search terms into the omnibox and check the state of
501 // the cache as each item is 'typed'. 533 // the cache as each item is 'typed'.
502 534
503 // Simulate typing "r" giving "r" in the simulated omnibox. The results for 535 // Simulate typing "r" giving "r" in the simulated omnibox. The results for
504 // 'r' will be not cached because it is only 1 character long. 536 // 'r' will be not cached because it is only 1 character long.
505 url_index_->HistoryItemsForTerms(ASCIIToUTF16("r")); 537 url_index_->HistoryItemsForTerms(ASCIIToUTF16("r"));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); 607 EXPECT_GT(scored_h.raw_score, scored_a.raw_score);
576 // Test scores based on a terms appearing multiple times. 608 // Test scores based on a terms appearing multiple times.
577 URLRow row_i(MakeURLRow("http://csi.csi.csi/csi_csi", 609 URLRow row_i(MakeURLRow("http://csi.csi.csi/csi_csi",
578 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 10)); 610 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 10));
579 ScoredHistoryMatch scored_i(URLIndexPrivateData::ScoredMatchForURL( 611 ScoredHistoryMatch scored_i(URLIndexPrivateData::ScoredMatchForURL(
580 row_i, ASCIIToUTF16("csi"), Make1Term("csi"))); 612 row_i, ASCIIToUTF16("csi"), Make1Term("csi")));
581 EXPECT_LT(scored_i.raw_score, 1400); 613 EXPECT_LT(scored_i.raw_score, 1400);
582 } 614 }
583 615
584 TEST_F(InMemoryURLIndexTest, AddNewRows) { 616 TEST_F(InMemoryURLIndexTest, AddNewRows) {
585 url_index_.reset(new InMemoryURLIndex(FilePath()));
586 url_index_->Init(this, "en,ja,hi,zh");
587
588 // Verify that the row we're going to add does not already exist. 617 // Verify that the row we're going to add does not already exist.
589 URLID new_row_id = 87654321; 618 URLID new_row_id = 87654321;
590 // Newly created URLRows get a last_visit time of 'right now' so it should 619 // Newly created URLRows get a last_visit time of 'right now' so it should
591 // qualify as a quick result candidate. 620 // qualify as a quick result candidate.
592 EXPECT_TRUE(url_index_->HistoryItemsForTerms( 621 EXPECT_TRUE(url_index_->HistoryItemsForTerms(
593 ASCIIToUTF16("brokeandalone")).empty()); 622 ASCIIToUTF16("brokeandalone")).empty());
594 623
595 // Add a new row. 624 // Add a new row.
596 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); 625 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id++);
597 new_row.set_last_visit(base::Time::Now()); 626 new_row.set_last_visit(base::Time::Now());
598 url_index_->UpdateURL(new_row_id, new_row); 627 EXPECT_TRUE(url_index_->UpdateURL(new_row));
599 628
600 // Verify that we can retrieve it. 629 // Verify that we can retrieve it.
601 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms( 630 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(
602 ASCIIToUTF16("brokeandalone")).size()); 631 ASCIIToUTF16("brokeandalone")).size());
603 632
604 // Add it again just to be sure that is harmless. 633 // Add it again just to be sure that is harmless and that it does not update
605 url_index_->UpdateURL(new_row_id, new_row); 634 // the index.
635 EXPECT_FALSE(url_index_->UpdateURL(new_row));
606 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms( 636 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(
607 ASCIIToUTF16("brokeandalone")).size()); 637 ASCIIToUTF16("brokeandalone")).size());
638
639 // Make up an URL that does not qualify and try to add it.
640 URLRow unqualified_row(GURL("http://www.brokeandaloneinmanitoba.com/"),
641 new_row_id++);
642 EXPECT_FALSE(url_index_->UpdateURL(new_row));
608 } 643 }
609 644
610 TEST_F(InMemoryURLIndexTest, DeleteRows) { 645 TEST_F(InMemoryURLIndexTest, DeleteRows) {
611 url_index_.reset(new InMemoryURLIndex(FilePath()));
612 url_index_->Init(this, "en,ja,hi,zh");
613
614 ScoredHistoryMatches matches = 646 ScoredHistoryMatches matches =
615 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport")); 647 url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"));
616 ASSERT_EQ(1U, matches.size()); 648 ASSERT_EQ(1U, matches.size());
617 649
618 // Determine the row id for that result, delete that id, then search again. 650 // Determine the row id for that result, delete that id, then search again.
619 url_index_->DeleteURL(matches[0].url_info.id()); 651 EXPECT_TRUE(url_index_->DeleteURL(matches[0].url_info.url()));
620 EXPECT_TRUE(url_index_->HistoryItemsForTerms( 652 EXPECT_TRUE(url_index_->HistoryItemsForTerms(
621 ASCIIToUTF16("DrudgeReport")).empty()); 653 ASCIIToUTF16("DrudgeReport")).empty());
654
655 // Make up an URL that does not exist in the database and delete it.
656 GURL url("http://www.hokeypokey.com/putyourrightfootin.html");
657 EXPECT_FALSE(url_index_->DeleteURL(url));
622 } 658 }
623 659
624 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { 660 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) {
625 struct TestData { 661 struct TestData {
626 const std::string url_spec; 662 const std::string url_spec;
627 const bool expected_is_whitelisted; 663 const bool expected_is_whitelisted;
628 } data[] = { 664 } data[] = {
629 // URLs with whitelisted schemes. 665 // URLs with whitelisted schemes.
630 { "about:histograms", true }, 666 { "about:histograms", true },
631 { "chrome://settings", true }, 667 { "chrome://settings", true },
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false }, 723 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false },
688 { "tftp://example.com/mystartupfile", false }, 724 { "tftp://example.com/mystartupfile", false },
689 { "tip://123.123.123.123/?urn:xopen:xid", false }, 725 { "tip://123.123.123.123/?urn:xopen:xid", false },
690 { "tv:nbc.com", false }, 726 { "tv:nbc.com", false },
691 { "urn:foo:A123,456", false }, 727 { "urn:foo:A123,456", false },
692 { "vemmi://zeus.mctel.fr/demo", false }, 728 { "vemmi://zeus.mctel.fr/demo", false },
693 { "wais://www.mydomain.net:8765/mydatabase", false }, 729 { "wais://www.mydomain.net:8765/mydatabase", false },
694 { "xmpp:node@example.com", false }, 730 { "xmpp:node@example.com", false },
695 { "xmpp://guest@example.com", false }, 731 { "xmpp://guest@example.com", false },
696 }; 732 };
697 url_index_.reset(new InMemoryURLIndex(FilePath())); 733
698 URLIndexPrivateData& private_data(*(url_index_->private_data_.get())); 734 URLIndexPrivateData& private_data(*GetPrivateData());
699 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 735 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
700 GURL url(data[i].url_spec); 736 GURL url(data[i].url_spec);
701 EXPECT_EQ(data[i].expected_is_whitelisted, 737 EXPECT_EQ(data[i].expected_is_whitelisted,
702 private_data.URLSchemeIsWhitelisted(url)); 738 private_data.URLSchemeIsWhitelisted(url));
703 } 739 }
704 } 740 }
705 741
706 TEST_F(InMemoryURLIndexTest, CacheFilePath) {
707 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL(
708 "/flammmy/frammy/"))));
709 FilePath full_file_path;
710 url_index_->GetCacheFilePath(&full_file_path);
711 std::vector<FilePath::StringType> expected_parts;
712 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")).
713 GetComponents(&expected_parts);
714 std::vector<FilePath::StringType> actual_parts;
715 full_file_path.GetComponents(&actual_parts);
716 ASSERT_EQ(expected_parts.size(), actual_parts.size());
717 size_t count = expected_parts.size();
718 for (size_t i = 0; i < count; ++i)
719 EXPECT_EQ(expected_parts[i], actual_parts[i]);
720 // Must clear the history_dir_ to satisfy the dtor's DCHECK.
721 url_index_->history_dir_.clear();
722 }
723
724 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { 742 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) {
725 // Save the cache to a protobuf, restore it, and compare the results. 743 // Save the cache to a protobuf, restore it, and compare the results.
726 url_index_.reset(new InMemoryURLIndex(FilePath()));
727 InMemoryURLIndex& url_index(*(url_index_.get()));
728 url_index.Init(this, "en,ja,hi,zh");
729 in_memory_url_index::InMemoryURLIndexCacheItem index_cache; 744 in_memory_url_index::InMemoryURLIndexCacheItem index_cache;
730 URLIndexPrivateData& private_data(*(url_index_->private_data_.get())); 745 URLIndexPrivateData& private_data(*GetPrivateData());
731 private_data.SavePrivateData(&index_cache); 746 private_data.SavePrivateData(&index_cache);
732 747
733 // Capture our private data so we can later compare for equality. 748 // Capture our private data so we can later compare for equality.
734 String16Vector word_list(private_data.word_list_); 749 String16Vector word_list(private_data.word_list_);
735 WordMap word_map(private_data.word_map_); 750 WordMap word_map(private_data.word_map_);
736 CharWordIDMap char_word_map(private_data.char_word_map_); 751 CharWordIDMap char_word_map(private_data.char_word_map_);
737 WordIDHistoryMap word_id_history_map(private_data.word_id_history_map_); 752 WordIDHistoryMap word_id_history_map(private_data.word_id_history_map_);
738 HistoryIDWordMap history_id_word_map(private_data.history_id_word_map_); 753 HistoryIDWordMap history_id_word_map(private_data.history_id_word_map_);
739 HistoryInfoMap history_info_map(private_data.history_info_map_); 754 HistoryInfoMap history_info_map(private_data.history_info_map_);
740 755
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 ASSERT_FALSE(private_data.history_info_map_.end() == actual); 805 ASSERT_FALSE(private_data.history_info_map_.end() == actual);
791 const URLRow& expected_row(expected->second); 806 const URLRow& expected_row(expected->second);
792 const URLRow& actual_row(actual->second); 807 const URLRow& actual_row(actual->second);
793 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); 808 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count());
794 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); 809 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count());
795 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); 810 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit());
796 EXPECT_EQ(expected_row.url(), actual_row.url()); 811 EXPECT_EQ(expected_row.url(), actual_row.url());
797 } 812 }
798 } 813 }
799 814
815 class InMemoryURLIndexCacheTest : public testing::Test {
816 public:
817 InMemoryURLIndexCacheTest() {}
818
819 protected:
820 virtual void SetUp() OVERRIDE;
821
822 ScopedTempDir temp_dir_;
823 scoped_ptr<InMemoryURLIndex> url_index_;
824 };
825
826 void InMemoryURLIndexCacheTest::SetUp() {
827 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
828 FilePath path(temp_dir_.path());
829 url_index_.reset(
830 new InMemoryURLIndex(NULL, path, "en,ja,hi,zh"));
831 }
832
833 TEST_F(InMemoryURLIndexCacheTest, CacheFilePath) {
834 FilePath expectedPath =
835 temp_dir_.path().Append(FILE_PATH_LITERAL("History Provider Cache"));
836 std::vector<std::string> expected_parts;
837 expectedPath.GetComponents(&expected_parts);
838 FilePath full_file_path;
839 ASSERT_TRUE(url_index_->GetCacheFilePath(&full_file_path));
840 std::vector<std::string> actual_parts;
841 full_file_path.GetComponents(&actual_parts);
842 ASSERT_EQ(expected_parts.size(), actual_parts.size());
843 size_t count = expected_parts.size();
844 for (size_t i = 0; i < count; ++i)
845 EXPECT_EQ(expected_parts[i], actual_parts[i]);
846 // Must clear the history_dir_ to satisfy the dtor's DCHECK.
847 url_index_->history_dir_.clear();
848 }
849
800 } // namespace history 850 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698