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

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

Issue 6652008: Implemented substring matching within page titles. Fixed bug where URL was be... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 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) 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 <stdio.h> 5 #include <stdio.h>
6 6
7 #include <fstream> 7 #include <fstream>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 30 matching lines...) Expand all
41 InMemoryURLIndexTest() { InitFromScratch(); } 41 InMemoryURLIndexTest() { InitFromScratch(); }
42 42
43 protected: 43 protected:
44 // Test setup. 44 // Test setup.
45 virtual void SetUp() { 45 virtual void SetUp() {
46 // Create and populate a working copy of the URL history database. 46 // Create and populate a working copy of the URL history database.
47 FilePath history_proto_path; 47 FilePath history_proto_path;
48 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path); 48 PathService::Get(chrome::DIR_TEST_DATA, &history_proto_path);
49 history_proto_path = history_proto_path.Append( 49 history_proto_path = history_proto_path.Append(
50 FILE_PATH_LITERAL("History")); 50 FILE_PATH_LITERAL("History"));
51 history_proto_path = history_proto_path.Append( 51 history_proto_path = history_proto_path.Append(TestDBName());
52 FILE_PATH_LITERAL("url_history_provider_test.db.txt"));
53 EXPECT_TRUE(file_util::PathExists(history_proto_path)); 52 EXPECT_TRUE(file_util::PathExists(history_proto_path));
54 53
55 std::ifstream proto_file(history_proto_path.value().c_str()); 54 std::ifstream proto_file(history_proto_path.value().c_str());
56 static const size_t kCommandBufferMaxSize = 2048; 55 static const size_t kCommandBufferMaxSize = 2048;
57 char sql_cmd_line[kCommandBufferMaxSize]; 56 char sql_cmd_line[kCommandBufferMaxSize];
58 57
59 sql::Connection& db(GetDB()); 58 sql::Connection& db(GetDB());
60 { 59 {
61 sql::Transaction transaction(&db); 60 sql::Transaction transaction(&db);
62 transaction.Begin(); 61 transaction.Begin();
(...skipping 12 matching lines...) Expand all
75 transaction.Commit(); 74 transaction.Commit();
76 } 75 }
77 proto_file.close(); 76 proto_file.close();
78 77
79 // Update the last_visit_time table column 78 // Update the last_visit_time table column
80 // such that it represents a time relative to 'now'. 79 // such that it represents a time relative to 'now'.
81 sql::Statement statement(db.GetUniqueStatement( 80 sql::Statement statement(db.GetUniqueStatement(
82 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls;")); 81 "SELECT" HISTORY_URL_ROW_FIELDS "FROM urls;"));
83 EXPECT_TRUE(statement); 82 EXPECT_TRUE(statement);
84 base::Time time_right_now = base::Time::NowFromSystemTime(); 83 base::Time time_right_now = base::Time::NowFromSystemTime();
85 base::TimeDelta day_delta = base::TimeDelta::FromDays(1); 84 base::TimeDelta day_delta = base::TimeDelta::FromDays(1);
86 { 85 {
87 sql::Transaction transaction(&db); 86 sql::Transaction transaction(&db);
88 transaction.Begin(); 87 transaction.Begin();
89 while (statement.Step()) { 88 while (statement.Step()) {
90 URLRow row; 89 URLRow row;
91 FillURLRow(statement, &row); 90 FillURLRow(statement, &row);
92 base::Time last_visit = time_right_now; 91 base::Time last_visit = time_right_now;
93 for (int64 i = row.last_visit().ToInternalValue(); i > 0; --i) 92 for (int64 i = row.last_visit().ToInternalValue(); i > 0; --i)
94 last_visit -= day_delta; 93 last_visit -= day_delta;
95 row.set_last_visit(last_visit); 94 row.set_last_visit(last_visit);
96 UpdateURLRow(row.id(), row); 95 UpdateURLRow(row.id(), row);
97 } 96 }
98 transaction.Commit(); 97 transaction.Commit();
99 } 98 }
100 } 99 }
101 100
101 virtual FilePath::StringType TestDBName() const {
102 return FILE_PATH_LITERAL("url_history_provider_test.db.txt");
103 }
104
105 URLRow MakeURLRow(const char* url,
106 const char* title,
107 int visit_count,
108 int last_visit_ago,
109 int typed_count) {
110 URLRow row(GURL(url), 0);
111 row.set_title(UTF8ToUTF16(title));
112 row.set_visit_count(visit_count);
113 row.set_typed_count(typed_count);
114 row.set_last_visit(base::Time::NowFromSystemTime() -
115 base::TimeDelta::FromDays(last_visit_ago));
116 return row;
117 }
118
119 InMemoryURLIndex::String16Vector Make1Term(const char* term) {
120 InMemoryURLIndex::String16Vector terms;
121 terms.push_back(UTF8ToUTF16(term));
122 return terms;
123 }
124
125 InMemoryURLIndex::String16Vector Make2Terms(const char* term_1,
126 const char* term_2) {
127 InMemoryURLIndex::String16Vector terms;
128 terms.push_back(UTF8ToUTF16(term_1));
129 terms.push_back(UTF8ToUTF16(term_2));
130 return terms;
131 }
132
102 scoped_ptr<InMemoryURLIndex> url_index_; 133 scoped_ptr<InMemoryURLIndex> url_index_;
103 }; 134 };
104 135
136 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest {
137 protected:
138 FilePath::StringType TestDBName() const {
139 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt");
140 }
141 };
142
105 TEST_F(InMemoryURLIndexTest, Construction) { 143 TEST_F(InMemoryURLIndexTest, Construction) {
106 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy")))); 144 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
107 EXPECT_TRUE(url_index_.get()); 145 EXPECT_TRUE(url_index_.get());
108 } 146 }
109 147
110 // TODO(mrossetti): Write python script to calculate the validation criteria. 148 TEST_F(LimitedInMemoryURLIndexTest, Initialization) {
111 TEST_F(InMemoryURLIndexTest, Initialization) {
112 // Verify that the database contains the expected number of items, which 149 // Verify that the database contains the expected number of items, which
113 // is the pre-filtered count, i.e. all of the items. 150 // is the pre-filtered count, i.e. all of the items.
114 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); 151 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;"));
115 EXPECT_TRUE(statement); 152 EXPECT_TRUE(statement);
116 uint64 row_count = 0; 153 uint64 row_count = 0;
117 while (statement.Step()) ++row_count; 154 while (statement.Step()) ++row_count;
118 EXPECT_EQ(row_count, 33U); 155 EXPECT_EQ(1U, row_count);
119 url_index_.reset(new InMemoryURLIndex); 156 url_index_.reset(new InMemoryURLIndex);
120 url_index_->Init(this, "en,ja,hi,zh"); 157 url_index_->Init(this, "en,ja,hi,zh");
121 EXPECT_EQ(url_index_->history_item_count_, 28); 158 EXPECT_EQ(1, url_index_->history_item_count_);
122 159
123 // history_info_map_ should have the same number of items as were filtered. 160 // history_info_map_ should have the same number of items as were filtered.
124 EXPECT_EQ(url_index_->history_info_map_.size(), 28U); 161 EXPECT_EQ(1U, url_index_->history_info_map_.size());
125 EXPECT_EQ(url_index_->char_word_map_.size(), 37U); 162 EXPECT_EQ(36U, url_index_->char_word_map_.size());
126 EXPECT_EQ(url_index_->word_map_.size(), 91U); 163 EXPECT_EQ(17U, url_index_->word_map_.size());
127 } 164 }
128 165
129 TEST_F(InMemoryURLIndexTest, Retrieval) { 166 TEST_F(InMemoryURLIndexTest, Retrieval) {
130 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy")))); 167 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
131 url_index_->Init(this, "en,ja,hi,zh"); 168 url_index_->Init(this, "en,ja,hi,zh");
132 InMemoryURLIndex::String16Vector terms; 169 InMemoryURLIndex::String16Vector terms;
133 // The term will be lowercased by the search. 170 // The term will be lowercased by the search.
134 171
135 // See if a very specific term gives a single result. 172 // See if a very specific term gives a single result.
136 terms.push_back(ASCIIToUTF16("DrudgeReport")); 173 terms.push_back(ASCIIToUTF16("DrudgeReport"));
137 EXPECT_EQ(url_index_->HistoryItemsForTerms(terms).size(), 1U); 174 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms);
175 EXPECT_EQ(1U, matches.size());
176
177 // Verify that we got back the result we expected.
178 EXPECT_EQ(5, matches[0].url_info.id());
179 EXPECT_EQ("http://drudgereport.com/", matches[0].url_info.url().spec());
180 EXPECT_EQ(ASCIIToUTF16("DRUDGE REPORT 2010"), matches[0].url_info.title());
138 181
139 // Search which should result in multiple results. 182 // Search which should result in multiple results.
140 terms.clear(); 183 terms.clear();
141 terms.push_back(ASCIIToUTF16("drudge")); 184 terms.push_back(ASCIIToUTF16("drudge"));
142 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms); 185 matches = url_index_->HistoryItemsForTerms(terms);
143 ASSERT_EQ(url_index_->HistoryItemsForTerms(terms).size(), 2U); 186 ASSERT_EQ(2U, url_index_->HistoryItemsForTerms(terms).size());
144 // The results should be in descending score order. 187 // The results should be in descending score order.
145 EXPECT_GT(matches[0].raw_score, matches[1].raw_score); 188 EXPECT_GT(matches[0].raw_score, matches[1].raw_score);
146 189
147 // Search which should result in nearly perfect result. 190 // Search which should result in nearly perfect result.
148 terms.clear(); 191 terms.clear();
149 terms.push_back(ASCIIToUTF16("http")); 192 terms.push_back(ASCIIToUTF16("https"));
150 terms.push_back(ASCIIToUTF16("NearlyPerfectResult")); 193 terms.push_back(ASCIIToUTF16("NearlyPerfectResult"));
151 matches = url_index_->HistoryItemsForTerms(terms); 194 matches = url_index_->HistoryItemsForTerms(terms);
152 ASSERT_EQ(matches.size(), 1U); 195 ASSERT_EQ(1U, matches.size());
153 // The results should have a very high score. 196 // The results should have a very high score.
154 EXPECT_GT(matches[0].raw_score, 900); 197 EXPECT_GT(matches[0].raw_score, 900);
198 EXPECT_EQ(32, matches[0].url_info.id());
199 EXPECT_EQ("https://nearlyperfectresult.com/",
200 matches[0].url_info.url().spec()); // Note: URL gets lowercased.
201 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"),
202 matches[0].url_info.title());
155 203
156 // Search which should result in very poor result. 204 // Search which should result in very poor result.
157 terms.clear(); 205 terms.clear();
158 terms.push_back(ASCIIToUTF16("z")); 206 terms.push_back(ASCIIToUTF16("z"));
159 terms.push_back(ASCIIToUTF16("y")); 207 terms.push_back(ASCIIToUTF16("y"));
160 terms.push_back(ASCIIToUTF16("x")); 208 terms.push_back(ASCIIToUTF16("x"));
161 matches = url_index_->HistoryItemsForTerms(terms); 209 matches = url_index_->HistoryItemsForTerms(terms);
162 ASSERT_EQ(matches.size(), 1U); 210 ASSERT_EQ(1U, matches.size());
163 // The results should have a poor score. 211 // The results should have a poor score.
164 EXPECT_LT(matches[0].raw_score, 200); 212 EXPECT_LT(matches[0].raw_score, 200);
213 EXPECT_EQ(33, matches[0].url_info.id());
214 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/",
215 matches[0].url_info.url().spec()); // Note: URL gets lowercased.
216 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"),
217 matches[0].url_info.title());
218 }
219
220 TEST_F(InMemoryURLIndexTest, TitleSearch) {
221 url_index_.reset(new InMemoryURLIndex());
222 url_index_->Init(this, "en,ja,hi,zh");
223 // Signal if someone has changed the test DB.
224 EXPECT_EQ(28U, url_index_->history_info_map_.size());
225 InMemoryURLIndex::String16Vector terms;
226
227 // Ensure title is being searched.
228 terms.push_back(ASCIIToUTF16("MORTGAGE"));
229 terms.push_back(ASCIIToUTF16("RATE"));
230 terms.push_back(ASCIIToUTF16("DROPS"));
231 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms);
232 EXPECT_EQ(1U, matches.size());
233
234 // Verify that we got back the result we expected.
235 EXPECT_EQ(1, matches[0].url_info.id());
236 EXPECT_EQ("http://www.reuters.com/article/idUSN0839880620100708",
237 matches[0].url_info.url().spec());
238 EXPECT_EQ(ASCIIToUTF16(
239 "UPDATE 1-US 30-yr mortgage rate drops to new record low | Reuters"),
240 matches[0].url_info.title());
165 } 241 }
166 242
167 TEST_F(InMemoryURLIndexTest, Char16Utilities) { 243 TEST_F(InMemoryURLIndexTest, Char16Utilities) {
168 string16 term = ASCIIToUTF16("drudgereport"); 244 string16 term = ASCIIToUTF16("drudgereport");
169 string16 expected = ASCIIToUTF16("drugepot"); 245 string16 expected = ASCIIToUTF16("drugepot");
170 EXPECT_EQ(expected.size(), 246 EXPECT_EQ(expected.size(),
171 InMemoryURLIndex::Char16SetFromString16(term).size()); 247 InMemoryURLIndex::Char16SetFromString16(term).size());
172 InMemoryURLIndex::Char16Vector c_vector = 248 InMemoryURLIndex::Char16Vector c_vector =
173 InMemoryURLIndex::Char16VectorFromString16(term); 249 InMemoryURLIndex::Char16VectorFromString16(term);
174 ASSERT_EQ(expected.size(), c_vector.size()); 250 ASSERT_EQ(expected.size(), c_vector.size());
175 251
176 InMemoryURLIndex::Char16Vector::const_iterator c_iter = c_vector.begin(); 252 InMemoryURLIndex::Char16Vector::const_iterator c_iter = c_vector.begin();
177 for (string16::const_iterator s_iter = expected.begin(); 253 for (string16::const_iterator s_iter = expected.begin();
178 s_iter != expected.end(); ++s_iter, ++c_iter) 254 s_iter != expected.end(); ++s_iter, ++c_iter)
179 EXPECT_EQ(*s_iter, *c_iter); 255 EXPECT_EQ(*s_iter, *c_iter);
180 } 256 }
181 257
258 TEST_F(InMemoryURLIndexTest, StaticFunctions) {
259 // Test WordVectorFromString16
260 string16 string_a(ASCIIToUTF16("http://www.google.com/ frammy the brammy"));
261 InMemoryURLIndex::String16Vector string_vec =
262 InMemoryURLIndex::WordVectorFromString16(string_a, false);
263 EXPECT_EQ(7U, string_vec.size());
264 // See if we got the words we expected.
265 EXPECT_EQ(UTF8ToUTF16("http"), string_vec[0]);
266 EXPECT_EQ(UTF8ToUTF16("www"), string_vec[1]);
267 EXPECT_EQ(UTF8ToUTF16("google"), string_vec[2]);
268 EXPECT_EQ(UTF8ToUTF16("com"), string_vec[3]);
269 EXPECT_EQ(UTF8ToUTF16("frammy"), string_vec[4]);
270 EXPECT_EQ(UTF8ToUTF16("the"), string_vec[5]);
271 EXPECT_EQ(UTF8ToUTF16("brammy"), string_vec[6]);
272
273 string_vec = InMemoryURLIndex::WordVectorFromString16(string_a, true);
274 EXPECT_EQ(5U, string_vec.size());
275 EXPECT_EQ(UTF8ToUTF16("http://"), string_vec[0]);
276 EXPECT_EQ(UTF8ToUTF16("www.google.com/"), string_vec[1]);
277 EXPECT_EQ(UTF8ToUTF16("frammy"), string_vec[2]);
278 EXPECT_EQ(UTF8ToUTF16("the"), string_vec[3]);
279 EXPECT_EQ(UTF8ToUTF16("brammy"), string_vec[4]);
280
281 // Test WordSetFromString16
282 string16 string_b(ASCIIToUTF16(
283 "http://web.google.com/search Google Web Search"));
284 InMemoryURLIndex::String16Set string_set =
285 InMemoryURLIndex::WordSetFromString16(string_b);
286 EXPECT_EQ(5U, string_set.size());
287 // See if we got the words we expected.
288 EXPECT_TRUE(string_set.find(UTF8ToUTF16("com")) != string_set.end());
289 EXPECT_TRUE(string_set.find(UTF8ToUTF16("google")) != string_set.end());
290 EXPECT_TRUE(string_set.find(UTF8ToUTF16("http")) != string_set.end());
291 EXPECT_TRUE(string_set.find(UTF8ToUTF16("search")) != string_set.end());
292 EXPECT_TRUE(string_set.find(UTF8ToUTF16("web")) != string_set.end());
293
294 // Test SortAndDeoverlap
295 TermMatches matches_a;
296 matches_a.push_back(TermMatch(1, 13, 10));
297 matches_a.push_back(TermMatch(2, 23, 10));
298 matches_a.push_back(TermMatch(3, 3, 10));
299 matches_a.push_back(TermMatch(4, 40, 5));
300 TermMatches matches_b = InMemoryURLIndex::SortAndDeoverlap(matches_a);
301 // Nothing should have been eliminated.
302 EXPECT_EQ(matches_a.size(), matches_b.size());
303 // The order should now be 3, 1, 2, 4.
304 EXPECT_EQ(3, matches_b[0].term_num);
305 EXPECT_EQ(1, matches_b[1].term_num);
306 EXPECT_EQ(2, matches_b[2].term_num);
307 EXPECT_EQ(4, matches_b[3].term_num);
308 matches_a.push_back(TermMatch(5, 18, 10));
309 matches_a.push_back(TermMatch(6, 38, 5));
310 matches_b = InMemoryURLIndex::SortAndDeoverlap(matches_a);
311 // Two matches should have been eliminated.
312 EXPECT_EQ(matches_a.size() - 2, matches_b.size());
313 // The order should now be 3, 1, 2, 6.
314 EXPECT_EQ(3, matches_b[0].term_num);
315 EXPECT_EQ(1, matches_b[1].term_num);
316 EXPECT_EQ(2, matches_b[2].term_num);
317 EXPECT_EQ(6, matches_b[3].term_num);
318
319 // Test MatchTermInString
320 TermMatches matches_c = InMemoryURLIndex::MatchTermInString(
321 UTF8ToUTF16("x"), UTF8ToUTF16("axbxcxdxex fxgx/hxixjx.kx"), 123);
322 ASSERT_EQ(11U, matches_c.size());
323 const size_t expected_offsets[] = { 1, 3, 5, 7, 9, 12, 14, 17, 19, 21, 24 };
324 for (int i = 0; i < 11; ++i)
325 EXPECT_EQ(expected_offsets[i], matches_c[i].offset);
326 }
327
182 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { 328 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) {
183 // Verify that match results for previously typed characters are retained 329 // Verify that match results for previously typed characters are retained
184 // (in the term_char_word_set_cache_) and reused, if possible, in future 330 // (in the term_char_word_set_cache_) and reused, if possible, in future
185 // autocompletes. 331 // autocompletes.
186 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy")))); 332 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
187 url_index_->Init(this, "en,ja,hi,zh"); 333 url_index_->Init(this, "en,ja,hi,zh");
188 334
189 // Verify that we can find something that already exists. 335 // Verify that we can find something that already exists.
190 InMemoryURLIndex::String16Vector terms; 336 InMemoryURLIndex::String16Vector terms;
191 string16 term = ASCIIToUTF16("drudgerepo"); 337 string16 term = ASCIIToUTF16("drudgerepo");
192 terms.push_back(term); 338 terms.push_back(term);
193 EXPECT_EQ(url_index_->HistoryItemsForTerms(terms).size(), 1U); 339 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size());
194 340
195 { 341 {
196 // Exercise the term matching cache with the same term. 342 // Exercise the term matching cache with the same term.
197 InMemoryURLIndex::Char16Vector uni_chars = 343 InMemoryURLIndex::Char16Vector uni_chars =
198 InMemoryURLIndex::Char16VectorFromString16(term); 344 InMemoryURLIndex::Char16VectorFromString16(term);
199 EXPECT_EQ(uni_chars.size(), 7U); // Equivalent to 'degopru' 345 EXPECT_EQ(uni_chars.size(), 7U); // Equivalent to 'degopru'
200 EXPECT_EQ(url_index_->CachedResultsIndexForTerm(uni_chars), 6U); 346 EXPECT_EQ(6U, url_index_->CachedResultsIndexForTerm(uni_chars));
201 } 347 }
202 348
203 { 349 {
204 // Back off a character. 350 // Back off a character.
205 InMemoryURLIndex::Char16Vector uni_chars = 351 InMemoryURLIndex::Char16Vector uni_chars =
206 InMemoryURLIndex::Char16VectorFromString16(ASCIIToUTF16("drudgerep")); 352 InMemoryURLIndex::Char16VectorFromString16(ASCIIToUTF16("drudgerep"));
207 EXPECT_EQ(uni_chars.size(), 6U); // Equivalent to 'degpru' 353 EXPECT_EQ(6U, uni_chars.size()); // Equivalent to 'degpru'
208 EXPECT_EQ(url_index_->CachedResultsIndexForTerm(uni_chars), 5U); 354 EXPECT_EQ(5U, url_index_->CachedResultsIndexForTerm(uni_chars));
209 } 355 }
210 356
211 { 357 {
212 // Add a couple of characters. 358 // Add a couple of characters.
213 InMemoryURLIndex::Char16Vector uni_chars = 359 InMemoryURLIndex::Char16Vector uni_chars =
214 InMemoryURLIndex::Char16VectorFromString16( 360 InMemoryURLIndex::Char16VectorFromString16(
215 ASCIIToUTF16("drudgereporta")); 361 ASCIIToUTF16("drudgereporta"));
216 EXPECT_EQ(uni_chars.size(), 9U); // Equivalent to 'adegoprtu' 362 EXPECT_EQ(9U, uni_chars.size()); // Equivalent to 'adegoprtu'
217 EXPECT_EQ(url_index_->CachedResultsIndexForTerm(uni_chars), 6U); 363 EXPECT_EQ(6U, url_index_->CachedResultsIndexForTerm(uni_chars));
218 } 364 }
219 365
220 { 366 {
221 // Use different string. 367 // Use different string.
222 InMemoryURLIndex::Char16Vector uni_chars = 368 InMemoryURLIndex::Char16Vector uni_chars =
223 InMemoryURLIndex::Char16VectorFromString16(ASCIIToUTF16("abcde")); 369 InMemoryURLIndex::Char16VectorFromString16(ASCIIToUTF16("abcde"));
224 EXPECT_EQ(uni_chars.size(), 5U); 370 EXPECT_EQ(5U, uni_chars.size());
225 EXPECT_EQ(url_index_->CachedResultsIndexForTerm(uni_chars), 371 EXPECT_EQ(static_cast<size_t>(-1),
226 static_cast<size_t>(-1)); 372 url_index_->CachedResultsIndexForTerm(uni_chars));
227 } 373 }
228 } 374 }
229 375
376 TEST_F(InMemoryURLIndexTest, Scoring) {
377 URLRow row_a(MakeURLRow("http://abcdef", "fedcba", 20, 0, 20));
378 // Test scores based on position.
379 ScoredHistoryMatch scored_a(
380 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("abc")));
381 ScoredHistoryMatch scored_b(
382 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("bcd")));
383 EXPECT_GT(scored_a.raw_score, scored_b.raw_score);
384 // Test scores based on length.
385 ScoredHistoryMatch scored_c(
386 InMemoryURLIndex::ScoredMatchForURL(row_a, Make1Term("abcd")));
387 EXPECT_LT(scored_a.raw_score, scored_c.raw_score);
388 // Test scores based on order.
389 ScoredHistoryMatch scored_d(
390 InMemoryURLIndex::ScoredMatchForURL(row_a, Make2Terms("abc", "def")));
391 ScoredHistoryMatch scored_e(
392 InMemoryURLIndex::ScoredMatchForURL(row_a, Make2Terms("def", "abc")));
393 EXPECT_GT(scored_d.raw_score, scored_e.raw_score);
394 // Test scores based on visit_count.
395 URLRow row_b(MakeURLRow("http://abcdef", "fedcba", 10, 0, 20));
396 ScoredHistoryMatch scored_f(
397 InMemoryURLIndex::ScoredMatchForURL(row_b, Make1Term("abc")));
398 EXPECT_GT(scored_a.raw_score, scored_f.raw_score);
399 // Test scores based on last_visit.
400 URLRow row_c(MakeURLRow("http://abcdef", "fedcba", 20, 2, 20));
401 ScoredHistoryMatch scored_g(
402 InMemoryURLIndex::ScoredMatchForURL(row_c, Make1Term("abc")));
403 EXPECT_GT(scored_a.raw_score, scored_g.raw_score);
404 // Test scores based on typed_count.
405 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 20, 0, 10));
406 ScoredHistoryMatch scored_h(
407 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc")));
408 EXPECT_GT(scored_a.raw_score, scored_h.raw_score);
409 }
410
230 TEST_F(InMemoryURLIndexTest, AddNewRows) { 411 TEST_F(InMemoryURLIndexTest, AddNewRows) {
231 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy")))); 412 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
232 url_index_->Init(this, "en,ja,hi,zh"); 413 url_index_->Init(this, "en,ja,hi,zh");
233 InMemoryURLIndex::String16Vector terms; 414 InMemoryURLIndex::String16Vector terms;
234 415
235 // Verify that the row we're going to add does not already exist. 416 // Verify that the row we're going to add does not already exist.
236 URLID new_row_id = 87654321; 417 URLID new_row_id = 87654321;
237 // Newly created URLRows get a last_visit time of 'right now' so it should 418 // Newly created URLRows get a last_visit time of 'right now' so it should
238 // qualify as a quick result candidate. 419 // qualify as a quick result candidate.
239 terms.push_back(ASCIIToUTF16("brokeandalone")); 420 terms.push_back(ASCIIToUTF16("brokeandalone"));
240 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty()); 421 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty());
241 422
242 // Add a new row. 423 // Add a new row.
243 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); 424 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id);
244 new_row.set_last_visit(base::Time::Now()); 425 new_row.set_last_visit(base::Time::Now());
245 url_index_->UpdateURL(new_row_id, new_row); 426 url_index_->UpdateURL(new_row_id, new_row);
246 427
247 // Verify that we can retrieve it. 428 // Verify that we can retrieve it.
248 EXPECT_EQ(url_index_->HistoryItemsForTerms(terms).size(), 1U); 429 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size());
249 430
250 // Add it again just to be sure that is harmless. 431 // Add it again just to be sure that is harmless.
251 url_index_->UpdateURL(new_row_id, new_row); 432 url_index_->UpdateURL(new_row_id, new_row);
252 EXPECT_EQ(url_index_->HistoryItemsForTerms(terms).size(), 1U); 433 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size());
253 } 434 }
254 435
255 TEST_F(InMemoryURLIndexTest, DeleteRows) { 436 TEST_F(InMemoryURLIndexTest, DeleteRows) {
256 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy")))); 437 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
257 url_index_->Init(this, "en,ja,hi,zh"); 438 url_index_->Init(this, "en,ja,hi,zh");
258 InMemoryURLIndex::String16Vector terms; 439 InMemoryURLIndex::String16Vector terms;
259 440
260 // Make sure we actually get an existing result. 441 // Make sure we actually get an existing result.
261 terms.push_back(ASCIIToUTF16("DrudgeReport")); 442 terms.push_back(ASCIIToUTF16("DrudgeReport"));
262 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms); 443 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms);
263 EXPECT_EQ(matches.size(), 1U); 444 ASSERT_EQ(1U, matches.size());
264 445
265 // Determine the row id for that result, delete that id, then search again. 446 // Determine the row id for that result, delete that id, then search again.
266 url_index_->DeleteURL(matches[0].url_info.id()); 447 url_index_->DeleteURL(matches[0].url_info.id());
267 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty()); 448 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty());
268 } 449 }
269 450
270 TEST_F(InMemoryURLIndexTest, CacheFilePath) { 451 TEST_F(InMemoryURLIndexTest, CacheFilePath) {
271 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL( 452 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL(
272 "/flammmy/frammy/")))); 453 "/flammmy/frammy/"))));
273 FilePath full_file_path; 454 FilePath full_file_path;
(...skipping 30 matching lines...) Expand all
304 // Prove that there is really something there. 485 // Prove that there is really something there.
305 EXPECT_GT(url_index.history_item_count_, 0); 486 EXPECT_GT(url_index.history_item_count_, 0);
306 EXPECT_FALSE(url_index.word_list_.empty()); 487 EXPECT_FALSE(url_index.word_list_.empty());
307 EXPECT_FALSE(url_index.word_map_.empty()); 488 EXPECT_FALSE(url_index.word_map_.empty());
308 EXPECT_FALSE(url_index.char_word_map_.empty()); 489 EXPECT_FALSE(url_index.char_word_map_.empty());
309 EXPECT_FALSE(url_index.word_id_history_map_.empty()); 490 EXPECT_FALSE(url_index.word_id_history_map_.empty());
310 EXPECT_FALSE(url_index.history_info_map_.empty()); 491 EXPECT_FALSE(url_index.history_info_map_.empty());
311 492
312 // Clear and then prove it's clear. 493 // Clear and then prove it's clear.
313 url_index.ClearPrivateData(); 494 url_index.ClearPrivateData();
314 EXPECT_EQ(url_index.history_item_count_, 0); 495 EXPECT_EQ(0, url_index.history_item_count_);
315 EXPECT_TRUE(url_index.word_list_.empty()); 496 EXPECT_TRUE(url_index.word_list_.empty());
316 EXPECT_TRUE(url_index.word_map_.empty()); 497 EXPECT_TRUE(url_index.word_map_.empty());
317 EXPECT_TRUE(url_index.char_word_map_.empty()); 498 EXPECT_TRUE(url_index.char_word_map_.empty());
318 EXPECT_TRUE(url_index.word_id_history_map_.empty()); 499 EXPECT_TRUE(url_index.word_id_history_map_.empty());
319 EXPECT_TRUE(url_index.history_info_map_.empty()); 500 EXPECT_TRUE(url_index.history_info_map_.empty());
320 501
321 // Restore the cache. 502 // Restore the cache.
322 EXPECT_TRUE(url_index.RestorePrivateData(index_cache)); 503 EXPECT_TRUE(url_index.RestorePrivateData(index_cache));
323 504
324 // Compare the restored and captured for equality. 505 // Compare the restored and captured for equality.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 const URLRow& expected_row(expected->second); 547 const URLRow& expected_row(expected->second);
367 const URLRow& actual_row(actual->second); 548 const URLRow& actual_row(actual->second);
368 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); 549 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count());
369 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); 550 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count());
370 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); 551 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit());
371 EXPECT_EQ(expected_row.url(), actual_row.url()); 552 EXPECT_EQ(expected_row.url(), actual_row.url());
372 } 553 }
373 } 554 }
374 555
375 } // namespace history 556 } // namespace history
OLDNEW
« no previous file with comments | « chrome/browser/history/in_memory_url_index.cc ('k') | chrome/test/data/History/url_history_provider_test.db.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698