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

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

Powered by Google App Engine
This is Rietveld 408576698