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

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

Issue 8275004: Revert 105300 - HQP Refactoring (in Preparation for SQLite Cache) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 2 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>
6
5 #include <fstream> 7 #include <fstream>
8 #include <string>
9 #include <vector>
6 10
7 #include "base/file_path.h" 11 #include "base/file_path.h"
8 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/memory/scoped_ptr.h"
9 #include "base/path_service.h" 14 #include "base/path_service.h"
10 #include "base/string16.h"
11 #include "base/string_util.h" 15 #include "base/string_util.h"
16 #include "base/time.h"
12 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/history/in_memory_database.h" 18 #include "chrome/browser/history/in_memory_database.h"
14 #include "chrome/browser/history/in_memory_url_index.h" 19 #include "chrome/browser/history/in_memory_url_index.h"
15 #include "chrome/browser/history/in_memory_url_index_types.h"
16 #include "chrome/common/chrome_paths.h" 20 #include "chrome/common/chrome_paths.h"
21 #include "sql/connection.h"
22 #include "sql/statement.h"
17 #include "sql/transaction.h" 23 #include "sql/transaction.h"
18 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
19 25
20 // The test version of the history url database table ('url') is contained in 26 // The test version of the history url database table ('url') is contained in
21 // a database file created from a text file('url_history_provider_test.db.txt'). 27 // a database file created from a text file('url_history_provider_test.db.txt').
22 // The only difference between this table and a live 'urls' table from a 28 // The only difference between this table and a live 'urls' table from a
23 // profile is that the last_visit_time column in the test table contains a 29 // profile is that the last_visit_time column in the test table contains a
24 // number specifying the number of days relative to 'today' to which the 30 // number specifying the number of days relative to 'today' to which the
25 // absolute time should be set during the test setup stage. 31 // absolute time should be set during the test setup stage.
26 // 32 //
(...skipping 19 matching lines...) Expand all
46 // 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|,
47 // |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
48 // days from now to set the URL's last_visit. 54 // days from now to set the URL's last_visit.
49 URLRow MakeURLRow(const char* url, 55 URLRow MakeURLRow(const char* url,
50 const char* title, 56 const char* title,
51 int visit_count, 57 int visit_count,
52 int last_visit_ago, 58 int last_visit_ago,
53 int typed_count); 59 int typed_count);
54 60
55 // Convenience functions for easily creating vectors of search terms. 61 // Convenience functions for easily creating vectors of search terms.
56 String16Vector Make1Term(const char* term) const; 62 InMemoryURLIndex::String16Vector Make1Term(const char* term) const;
57 String16Vector Make2Terms(const char* term_1, const char* term_2) const; 63 InMemoryURLIndex::String16Vector Make2Terms(const char* term_1,
64 const char* term_2) const;
58 65
59 // Validates that the given |term| is contained in |cache| and that it is 66 // Validates that the given |term| is contained in |cache| and that it is
60 // marked as in-use. 67 // marked as in-use.
61 void CheckTerm(const InMemoryURLIndex::SearchTermCacheMap& cache, 68 void CheckTerm(const InMemoryURLIndex::SearchTermCacheMap& cache,
62 string16 term) const; 69 string16 term) const;
63 70
64 scoped_ptr<InMemoryURLIndex> url_index_; 71 scoped_ptr<InMemoryURLIndex> url_index_;
65 }; 72 };
66 73
67 void InMemoryURLIndexTest::SetUp() { 74 void InMemoryURLIndexTest::SetUp() {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 int typed_count) { 138 int typed_count) {
132 URLRow row(GURL(url), 0); 139 URLRow row(GURL(url), 0);
133 row.set_title(UTF8ToUTF16(title)); 140 row.set_title(UTF8ToUTF16(title));
134 row.set_visit_count(visit_count); 141 row.set_visit_count(visit_count);
135 row.set_typed_count(typed_count); 142 row.set_typed_count(typed_count);
136 row.set_last_visit(base::Time::NowFromSystemTime() - 143 row.set_last_visit(base::Time::NowFromSystemTime() -
137 base::TimeDelta::FromDays(last_visit_ago)); 144 base::TimeDelta::FromDays(last_visit_ago));
138 return row; 145 return row;
139 } 146 }
140 147
141 String16Vector InMemoryURLIndexTest::Make1Term(const char* term) const { 148 InMemoryURLIndex::String16Vector InMemoryURLIndexTest::Make1Term(
142 String16Vector terms; 149 const char* term) const {
150 InMemoryURLIndex::String16Vector terms;
143 terms.push_back(UTF8ToUTF16(term)); 151 terms.push_back(UTF8ToUTF16(term));
144 return terms; 152 return terms;
145 } 153 }
146 154
147 String16Vector InMemoryURLIndexTest::Make2Terms(const char* term_1, 155 InMemoryURLIndex::String16Vector InMemoryURLIndexTest::Make2Terms(
148 const char* term_2) const { 156 const char* term_1,
149 String16Vector terms; 157 const char* term_2) const {
158 InMemoryURLIndex::String16Vector terms;
150 terms.push_back(UTF8ToUTF16(term_1)); 159 terms.push_back(UTF8ToUTF16(term_1));
151 terms.push_back(UTF8ToUTF16(term_2)); 160 terms.push_back(UTF8ToUTF16(term_2));
152 return terms; 161 return terms;
153 } 162 }
154 163
155 void InMemoryURLIndexTest::CheckTerm( 164 void InMemoryURLIndexTest::CheckTerm(
156 const InMemoryURLIndex::SearchTermCacheMap& cache, 165 const InMemoryURLIndex::SearchTermCacheMap& cache,
157 string16 term) const { 166 string16 term) const {
158 InMemoryURLIndex::SearchTermCacheMap::const_iterator cache_iter( 167 InMemoryURLIndex::SearchTermCacheMap::const_iterator cache_iter(
159 cache.find(term)); 168 cache.find(term));
160 ASSERT_NE(cache.end(), cache_iter) 169 ASSERT_NE(cache.end(), cache_iter)
161 << "Cache does not contain '" << term << "' but should."; 170 << "Cache does not contain '" << term << "' but should.";
162 InMemoryURLIndex::SearchTermCacheItem cache_item = cache_iter->second; 171 InMemoryURLIndex::SearchTermCacheItem cache_item = cache_iter->second;
163 EXPECT_TRUE(cache_item.used_) 172 EXPECT_TRUE(cache_item.used_)
164 << "Cache item '" << term << "' should be marked as being in use."; 173 << "Cache item '" << term << "' should be marked as being in use.";
165 } 174 }
166 175
167 // Helper function which compares two maps for equivalence. The maps' values
168 // are associative containers and their contents are compared as well.
169 template<typename T>
170 void ExpectMapOfContainersIdentical(const T& expected, const T& actual) {
171 ASSERT_EQ(expected.size(), actual.size());
172 for (typename T::const_iterator expected_iter = expected.begin();
173 expected_iter != expected.end(); ++expected_iter) {
174 typename T::const_iterator actual_iter = actual.find(expected_iter->first);
175 ASSERT_NE(actual.end(), actual_iter);
176 typename T::mapped_type const& expected_values(expected_iter->second);
177 typename T::mapped_type const& actual_values(actual_iter->second);
178 ASSERT_EQ(expected_values.size(), actual_values.size());
179 for (typename T::mapped_type::const_iterator set_iter =
180 expected_values.begin(); set_iter != expected_values.end(); ++set_iter)
181 EXPECT_EQ(actual_values.count(*set_iter),
182 expected_values.count(*set_iter));
183 }
184 }
185
186 //------------------------------------------------------------------------------
187
188 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { 176 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest {
189 protected: 177 protected:
190 FilePath::StringType TestDBName() const; 178 FilePath::StringType TestDBName() const;
191 }; 179 };
192 180
193 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { 181 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const {
194 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); 182 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt");
195 } 183 }
196 184
197 class ExpandedInMemoryURLIndexTest : public InMemoryURLIndexTest { 185 class ExpandedInMemoryURLIndexTest : public InMemoryURLIndexTest {
(...skipping 10 matching lines...) Expand all
208 const size_t kMaxLen = arraysize(url_format) + 5; 196 const size_t kMaxLen = arraysize(url_format) + 5;
209 char url_string[kMaxLen + 1]; 197 char url_string[kMaxLen + 1];
210 for (int i = 0; i < 600; ++i) { 198 for (int i = 0; i < 600; ++i) {
211 base::snprintf(url_string, kMaxLen, url_format, i); 199 base::snprintf(url_string, kMaxLen, url_format, i);
212 URLRow row(MakeURLRow(url_string, "Google Search", 20, 0, 20)); 200 URLRow row(MakeURLRow(url_string, "Google Search", 20, 0, 20));
213 AddURL(row); 201 AddURL(row);
214 } 202 }
215 } 203 }
216 204
217 TEST_F(InMemoryURLIndexTest, Construction) { 205 TEST_F(InMemoryURLIndexTest, Construction) {
218 url_index_.reset(new InMemoryURLIndex(NULL, 206 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
219 FilePath(FILE_PATH_LITERAL("/dummy"))));
220 EXPECT_TRUE(url_index_.get()); 207 EXPECT_TRUE(url_index_.get());
221 } 208 }
222 209
223 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { 210 TEST_F(LimitedInMemoryURLIndexTest, Initialization) {
224 // Verify that the database contains the expected number of items, which 211 // Verify that the database contains the expected number of items, which
225 // is the pre-filtered count, i.e. all of the items. 212 // is the pre-filtered count, i.e. all of the items.
226 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); 213 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;"));
227 EXPECT_TRUE(statement); 214 EXPECT_TRUE(statement);
228 uint64 row_count = 0; 215 uint64 row_count = 0;
229 while (statement.Step()) ++row_count; 216 while (statement.Step()) ++row_count;
230 EXPECT_EQ(1U, row_count); 217 EXPECT_EQ(1U, row_count);
231 url_index_.reset(new InMemoryURLIndex); 218 url_index_.reset(new InMemoryURLIndex);
232 url_index_->Init(this, "en,ja,hi,zh"); 219 url_index_->Init(this, "en,ja,hi,zh");
233 URLIndexPrivateData& private_data(*(url_index_->private_data_)); 220 EXPECT_EQ(1, url_index_->history_item_count_);
234 221
235 // history_info_map_ should have the same number of items as were filtered. 222 // history_info_map_ should have the same number of items as were filtered.
236 EXPECT_EQ(1U, private_data.history_info_map_.size()); 223 EXPECT_EQ(1U, url_index_->history_info_map_.size());
237 EXPECT_EQ(35U, private_data.char_word_map_.size()); 224 EXPECT_EQ(35U, url_index_->char_word_map_.size());
238 EXPECT_EQ(17U, private_data.word_map_.size()); 225 EXPECT_EQ(17U, url_index_->word_map_.size());
239 } 226 }
240 227
241 TEST_F(InMemoryURLIndexTest, Retrieval) { 228 TEST_F(InMemoryURLIndexTest, Retrieval) {
242 url_index_.reset(new InMemoryURLIndex(NULL, 229 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
243 FilePath(FILE_PATH_LITERAL("/dummy"))));
244 url_index_->Init(this, "en,ja,hi,zh"); 230 url_index_->Init(this, "en,ja,hi,zh");
245 // The term will be lowercased by the search. 231 // The term will be lowercased by the search.
246 232
247 // See if a very specific term gives a single result. 233 // See if a very specific term gives a single result.
248 ScoredHistoryMatches matches = 234 ScoredHistoryMatches matches =
249 url_index_->HistoryItemsForTerms(Make1Term("DrudgeReport")); 235 url_index_->HistoryItemsForTerms(Make1Term("DrudgeReport"));
250 ASSERT_EQ(1U, matches.size()); 236 ASSERT_EQ(1U, matches.size());
251 237
252 // Verify that we got back the result we expected. 238 // Verify that we got back the result we expected.
253 EXPECT_EQ(5, matches[0].url_info.id()); 239 EXPECT_EQ(5, matches[0].url_info.id());
(...skipping 12 matching lines...) Expand all
266 ASSERT_EQ(1U, matches.size()); 252 ASSERT_EQ(1U, matches.size());
267 // The results should have a very high score. 253 // The results should have a very high score.
268 EXPECT_GT(matches[0].raw_score, 900); 254 EXPECT_GT(matches[0].raw_score, 900);
269 EXPECT_EQ(32, matches[0].url_info.id()); 255 EXPECT_EQ(32, matches[0].url_info.id());
270 EXPECT_EQ("https://nearlyperfectresult.com/", 256 EXPECT_EQ("https://nearlyperfectresult.com/",
271 matches[0].url_info.url().spec()); // Note: URL gets lowercased. 257 matches[0].url_info.url().spec()); // Note: URL gets lowercased.
272 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"), 258 EXPECT_EQ(ASCIIToUTF16("Practically Perfect Search Result"),
273 matches[0].url_info.title()); 259 matches[0].url_info.title());
274 260
275 // Search which should result in very poor result. 261 // Search which should result in very poor result.
276 String16Vector terms; 262 InMemoryURLIndex::String16Vector terms;
277 terms.push_back(ASCIIToUTF16("z")); 263 terms.push_back(ASCIIToUTF16("z"));
278 terms.push_back(ASCIIToUTF16("y")); 264 terms.push_back(ASCIIToUTF16("y"));
279 terms.push_back(ASCIIToUTF16("x")); 265 terms.push_back(ASCIIToUTF16("x"));
280 matches = url_index_->HistoryItemsForTerms(terms); 266 matches = url_index_->HistoryItemsForTerms(terms);
281 ASSERT_EQ(1U, matches.size()); 267 ASSERT_EQ(1U, matches.size());
282 // The results should have a poor score. 268 // The results should have a poor score.
283 EXPECT_LT(matches[0].raw_score, 500); 269 EXPECT_LT(matches[0].raw_score, 500);
284 EXPECT_EQ(33, matches[0].url_info.id()); 270 EXPECT_EQ(33, matches[0].url_info.id());
285 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/", 271 EXPECT_EQ("http://quiteuselesssearchresultxyz.com/",
286 matches[0].url_info.url().spec()); // Note: URL gets lowercased. 272 matches[0].url_info.url().spec()); // Note: URL gets lowercased.
287 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"), 273 EXPECT_EQ(ASCIIToUTF16("Practically Useless Search Result"),
288 matches[0].url_info.title()); 274 matches[0].url_info.title());
289 275
290 // Search which will match at the end of an URL with encoded characters. 276 // Search which will match at the end of an URL with encoded characters.
291 matches = url_index_->HistoryItemsForTerms(Make1Term("ice")); 277 matches = url_index_->HistoryItemsForTerms(Make1Term("ice"));
292 ASSERT_EQ(1U, matches.size()); 278 ASSERT_EQ(1U, matches.size());
293 } 279 }
294 280
295 TEST_F(ExpandedInMemoryURLIndexTest, ShortCircuit) { 281 TEST_F(ExpandedInMemoryURLIndexTest, ShortCircuit) {
296 url_index_.reset(new InMemoryURLIndex(NULL, 282 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
297 FilePath(FILE_PATH_LITERAL("/dummy"))));
298 url_index_->Init(this, "en,ja,hi,zh"); 283 url_index_->Init(this, "en,ja,hi,zh");
299 284
300 // A search for 'w' should short-circuit and not return any matches. 285 // A search for 'w' should short-circuit and not return any matches.
301 ScoredHistoryMatches matches = 286 ScoredHistoryMatches matches =
302 url_index_->HistoryItemsForTerms(Make1Term("w")); 287 url_index_->HistoryItemsForTerms(Make1Term("w"));
303 EXPECT_TRUE(matches.empty()); 288 EXPECT_TRUE(matches.empty());
304 289
305 // A search for 'working' should not short-circuit. 290 // A search for 'working' should not short-circuit.
306 matches = url_index_->HistoryItemsForTerms(Make1Term("working")); 291 matches = url_index_->HistoryItemsForTerms(Make1Term("working"));
307 EXPECT_EQ(1U, matches.size()); 292 EXPECT_EQ(1U, matches.size());
308 } 293 }
309 294
310 TEST_F(InMemoryURLIndexTest, TitleSearch) { 295 TEST_F(InMemoryURLIndexTest, TitleSearch) {
311 url_index_.reset(new InMemoryURLIndex()); 296 url_index_.reset(new InMemoryURLIndex());
312 url_index_->Init(this, "en,ja,hi,zh"); 297 url_index_->Init(this, "en,ja,hi,zh");
313 // Signal if someone has changed the test DB. 298 // Signal if someone has changed the test DB.
314 EXPECT_EQ(27U, url_index_->private_data_->history_info_map_.size()); 299 EXPECT_EQ(27U, url_index_->history_info_map_.size());
315 String16Vector terms; 300 InMemoryURLIndex::String16Vector terms;
316 301
317 // Ensure title is being searched. 302 // Ensure title is being searched.
318 terms.push_back(ASCIIToUTF16("MORTGAGE")); 303 terms.push_back(ASCIIToUTF16("MORTGAGE"));
319 terms.push_back(ASCIIToUTF16("RATE")); 304 terms.push_back(ASCIIToUTF16("RATE"));
320 terms.push_back(ASCIIToUTF16("DROPS")); 305 terms.push_back(ASCIIToUTF16("DROPS"));
321 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms); 306 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms);
322 ASSERT_EQ(1U, matches.size()); 307 ASSERT_EQ(1U, matches.size());
323 308
324 // Verify that we got back the result we expected. 309 // Verify that we got back the result we expected.
325 EXPECT_EQ(1, matches[0].url_info.id()); 310 EXPECT_EQ(1, matches[0].url_info.id());
(...skipping 27 matching lines...) Expand all
353 338
354 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACADABR")); 339 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACADABR"));
355 ASSERT_EQ(1U, matches.size()); 340 ASSERT_EQ(1U, matches.size());
356 EXPECT_EQ(28, matches[0].url_info.id()); 341 EXPECT_EQ(28, matches[0].url_info.id());
357 342
358 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACA")); 343 matches = url_index_->HistoryItemsForTerms(Make1Term("ABRACA"));
359 ASSERT_EQ(1U, matches.size()); 344 ASSERT_EQ(1U, matches.size());
360 EXPECT_EQ(28, matches[0].url_info.id()); 345 EXPECT_EQ(28, matches[0].url_info.id());
361 } 346 }
362 347
348 TEST_F(InMemoryURLIndexTest, StaticFunctions) {
349 // Test WordVectorFromString16
350 string16 string_a(ASCIIToUTF16("http://www.google.com/ frammy the brammy"));
351 InMemoryURLIndex::String16Vector string_vec =
352 InMemoryURLIndex::WordVectorFromString16(string_a, false);
353 ASSERT_EQ(7U, string_vec.size());
354 // See if we got the words we expected.
355 EXPECT_EQ(UTF8ToUTF16("http"), string_vec[0]);
356 EXPECT_EQ(UTF8ToUTF16("www"), string_vec[1]);
357 EXPECT_EQ(UTF8ToUTF16("google"), string_vec[2]);
358 EXPECT_EQ(UTF8ToUTF16("com"), string_vec[3]);
359 EXPECT_EQ(UTF8ToUTF16("frammy"), string_vec[4]);
360 EXPECT_EQ(UTF8ToUTF16("the"), string_vec[5]);
361 EXPECT_EQ(UTF8ToUTF16("brammy"), string_vec[6]);
362
363 string_vec = InMemoryURLIndex::WordVectorFromString16(string_a, true);
364 ASSERT_EQ(5U, string_vec.size());
365 EXPECT_EQ(UTF8ToUTF16("http://"), string_vec[0]);
366 EXPECT_EQ(UTF8ToUTF16("www.google.com/"), string_vec[1]);
367 EXPECT_EQ(UTF8ToUTF16("frammy"), string_vec[2]);
368 EXPECT_EQ(UTF8ToUTF16("the"), string_vec[3]);
369 EXPECT_EQ(UTF8ToUTF16("brammy"), string_vec[4]);
370
371 // Test WordSetFromString16
372 string16 string_b(ASCIIToUTF16(
373 "http://web.google.com/search Google Web Search"));
374 InMemoryURLIndex::String16Set string_set =
375 InMemoryURLIndex::WordSetFromString16(string_b);
376 EXPECT_EQ(5U, string_set.size());
377 // See if we got the words we expected.
378 EXPECT_TRUE(string_set.find(UTF8ToUTF16("com")) != string_set.end());
379 EXPECT_TRUE(string_set.find(UTF8ToUTF16("google")) != string_set.end());
380 EXPECT_TRUE(string_set.find(UTF8ToUTF16("http")) != string_set.end());
381 EXPECT_TRUE(string_set.find(UTF8ToUTF16("search")) != string_set.end());
382 EXPECT_TRUE(string_set.find(UTF8ToUTF16("web")) != string_set.end());
383
384 // Test SortAndDeoverlap
385 TermMatches matches_a;
386 matches_a.push_back(TermMatch(1, 13, 10));
387 matches_a.push_back(TermMatch(2, 23, 10));
388 matches_a.push_back(TermMatch(3, 3, 10));
389 matches_a.push_back(TermMatch(4, 40, 5));
390 TermMatches matches_b = InMemoryURLIndex::SortAndDeoverlap(matches_a);
391 // Nothing should have been eliminated.
392 EXPECT_EQ(matches_a.size(), matches_b.size());
393 // The order should now be 3, 1, 2, 4.
394 EXPECT_EQ(3, matches_b[0].term_num);
395 EXPECT_EQ(1, matches_b[1].term_num);
396 EXPECT_EQ(2, matches_b[2].term_num);
397 EXPECT_EQ(4, matches_b[3].term_num);
398 matches_a.push_back(TermMatch(5, 18, 10));
399 matches_a.push_back(TermMatch(6, 38, 5));
400 matches_b = InMemoryURLIndex::SortAndDeoverlap(matches_a);
401 // Two matches should have been eliminated.
402 EXPECT_EQ(matches_a.size() - 2, matches_b.size());
403 // The order should now be 3, 1, 2, 6.
404 EXPECT_EQ(3, matches_b[0].term_num);
405 EXPECT_EQ(1, matches_b[1].term_num);
406 EXPECT_EQ(2, matches_b[2].term_num);
407 EXPECT_EQ(6, matches_b[3].term_num);
408
409 // Test MatchTermInString
410 TermMatches matches_c = InMemoryURLIndex::MatchTermInString(
411 UTF8ToUTF16("x"), UTF8ToUTF16("axbxcxdxex fxgx/hxixjx.kx"), 123);
412 ASSERT_EQ(11U, matches_c.size());
413 const size_t expected_offsets[] = { 1, 3, 5, 7, 9, 12, 14, 17, 19, 21, 24 };
414 for (int i = 0; i < 11; ++i)
415 EXPECT_EQ(expected_offsets[i], matches_c[i].offset);
416 }
417
418 TEST_F(InMemoryURLIndexTest, OffsetsAndTermMatches) {
419 // Test OffsetsFromTermMatches
420 history::TermMatches matches_a;
421 matches_a.push_back(history::TermMatch(1, 1, 2));
422 matches_a.push_back(history::TermMatch(2, 4, 3));
423 matches_a.push_back(history::TermMatch(3, 9, 1));
424 matches_a.push_back(history::TermMatch(3, 10, 1));
425 matches_a.push_back(history::TermMatch(4, 14, 5));
426 std::vector<size_t> offsets =
427 InMemoryURLIndex::OffsetsFromTermMatches(matches_a);
428 const size_t expected_offsets_a[] = {1, 4, 9, 10, 14};
429 ASSERT_EQ(offsets.size(), arraysize(expected_offsets_a));
430 for (size_t i = 0; i < offsets.size(); ++i)
431 EXPECT_EQ(expected_offsets_a[i], offsets[i]);
432
433 // Test ReplaceOffsetsInTermMatches
434 offsets[2] = string16::npos;
435 history::TermMatches matches_b =
436 InMemoryURLIndex::ReplaceOffsetsInTermMatches(matches_a, offsets);
437 const size_t expected_offsets_b[] = {1, 4, 10, 14};
438 ASSERT_EQ(arraysize(expected_offsets_b), matches_b.size());
439 for (size_t i = 0; i < matches_b.size(); ++i)
440 EXPECT_EQ(expected_offsets_b[i], matches_b[i].offset);
441 }
442
363 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) { 443 TEST_F(InMemoryURLIndexTest, TypedCharacterCaching) {
364 // Verify that match results for previously typed characters are retained 444 // Verify that match results for previously typed characters are retained
365 // (in the term_char_word_set_cache_) and reused, if possible, in future 445 // (in the term_char_word_set_cache_) and reused, if possible, in future
366 // autocompletes. 446 // autocompletes.
367 typedef InMemoryURLIndex::SearchTermCacheMap::iterator CacheIter; 447 typedef InMemoryURLIndex::SearchTermCacheMap::iterator CacheIter;
368 typedef InMemoryURLIndex::SearchTermCacheItem CacheItem; 448 typedef InMemoryURLIndex::SearchTermCacheItem CacheItem;
369 449
370 url_index_.reset(new InMemoryURLIndex(NULL, 450 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
371 FilePath(FILE_PATH_LITERAL("/dummy"))));
372 url_index_->Init(this, "en,ja,hi,zh"); 451 url_index_->Init(this, "en,ja,hi,zh");
373 452
374 InMemoryURLIndex::SearchTermCacheMap& cache(url_index_->search_term_cache_); 453 InMemoryURLIndex::SearchTermCacheMap& cache(url_index_->search_term_cache_);
375 454
376 // The cache should be empty at this point. 455 // The cache should be empty at this point.
377 EXPECT_EQ(0U, cache.size()); 456 EXPECT_EQ(0U, cache.size());
378 457
379 // Now simulate typing search terms into the omnibox and check the state of 458 // Now simulate typing search terms into the omnibox and check the state of
380 // the cache as each item is 'typed'. 459 // the cache as each item is 'typed'.
381 460
382 // Simulate typing "r" giving "r" in the simulated omnibox. The results for 461 // Simulate typing "r" giving "r" in the simulated omnibox. The results for
383 // 'r' will be not cached because it is only 1 character long. 462 // 'r' will be not cached because it is only 1 character long.
384 String16Vector terms; 463 InMemoryURLIndex::String16Vector terms;
385 string16 term_r = ASCIIToUTF16("r"); 464 string16 term_r = ASCIIToUTF16("r");
386 terms.push_back(term_r); 465 terms.push_back(term_r);
387 url_index_->HistoryItemsForTerms(terms); 466 url_index_->HistoryItemsForTerms(terms);
388 EXPECT_EQ(0U, cache.size()); 467 EXPECT_EQ(0U, cache.size());
389 468
390 // Simulate typing "re" giving "r re" in the simulated omnibox. 469 // Simulate typing "re" giving "r re" in the simulated omnibox.
391 string16 term_re = ASCIIToUTF16("re"); 470 string16 term_re = ASCIIToUTF16("re");
392 terms.push_back(term_re); 471 terms.push_back(term_re);
393 // 're' should be cached at this point but not 'r' as it is a single 472 // 're' should be cached at this point but not 'r' as it is a single
394 // character. 473 // character.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 InMemoryURLIndex::ScoredMatchForURL(row_c, Make1Term("abc"))); 543 InMemoryURLIndex::ScoredMatchForURL(row_c, Make1Term("abc")));
465 EXPECT_GT(scored_g.raw_score, scored_a.raw_score); 544 EXPECT_GT(scored_g.raw_score, scored_a.raw_score);
466 // Test scores based on typed_count. 545 // Test scores based on typed_count.
467 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10)); 546 URLRow row_d(MakeURLRow("http://abcdef", "fedcba", 3, 30, 10));
468 ScoredHistoryMatch scored_h( 547 ScoredHistoryMatch scored_h(
469 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc"))); 548 InMemoryURLIndex::ScoredMatchForURL(row_d, Make1Term("abc")));
470 EXPECT_GT(scored_h.raw_score, scored_a.raw_score); 549 EXPECT_GT(scored_h.raw_score, scored_a.raw_score);
471 } 550 }
472 551
473 TEST_F(InMemoryURLIndexTest, AddNewRows) { 552 TEST_F(InMemoryURLIndexTest, AddNewRows) {
474 url_index_.reset(new InMemoryURLIndex(NULL, 553 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
475 FilePath(FILE_PATH_LITERAL("/dummy"))));
476 url_index_->Init(this, "en,ja,hi,zh"); 554 url_index_->Init(this, "en,ja,hi,zh");
477 String16Vector terms; 555 InMemoryURLIndex::String16Vector terms;
478 556
479 // Verify that the row we're going to add does not already exist. 557 // Verify that the row we're going to add does not already exist.
480 URLID new_row_id = 87654321; 558 URLID new_row_id = 87654321;
481 // Newly created URLRows get a last_visit time of 'right now' so it should 559 // Newly created URLRows get a last_visit time of 'right now' so it should
482 // qualify as a quick result candidate. 560 // qualify as a quick result candidate.
483 terms.push_back(ASCIIToUTF16("brokeandalone")); 561 terms.push_back(ASCIIToUTF16("brokeandalone"));
484 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty()); 562 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty());
485 563
486 // Add a new row. 564 // Add a new row.
487 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id); 565 URLRow new_row(GURL("http://www.brokeandaloneinmanitoba.com/"), new_row_id);
488 new_row.set_last_visit(base::Time::Now()); 566 new_row.set_last_visit(base::Time::Now());
489 url_index_->UpdateURL(new_row); 567 url_index_->UpdateURL(new_row_id, new_row);
490 568
491 // Verify that we can retrieve it. 569 // Verify that we can retrieve it.
492 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size()); 570 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size());
493 571
494 // Add it again just to be sure that is harmless. 572 // Add it again just to be sure that is harmless.
495 url_index_->UpdateURL(new_row); 573 url_index_->UpdateURL(new_row_id, new_row);
496 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size()); 574 EXPECT_EQ(1U, url_index_->HistoryItemsForTerms(terms).size());
497 } 575 }
498 576
499 TEST_F(InMemoryURLIndexTest, DeleteRows) { 577 TEST_F(InMemoryURLIndexTest, DeleteRows) {
500 url_index_.reset(new InMemoryURLIndex(NULL, 578 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
501 FilePath(FILE_PATH_LITERAL("/dummy"))));
502 url_index_->Init(this, "en,ja,hi,zh"); 579 url_index_->Init(this, "en,ja,hi,zh");
503 String16Vector terms; 580 InMemoryURLIndex::String16Vector terms;
504 581
505 // Make sure we actually get an existing result. 582 // Make sure we actually get an existing result.
506 terms.push_back(ASCIIToUTF16("DrudgeReport")); 583 terms.push_back(ASCIIToUTF16("DrudgeReport"));
507 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms); 584 ScoredHistoryMatches matches = url_index_->HistoryItemsForTerms(terms);
508 ASSERT_EQ(1U, matches.size()); 585 ASSERT_EQ(1U, matches.size());
509 586
510 // Determine the row id for that result, delete that id, then search again. 587 // Determine the row id for that result, delete that id, then search again.
511 url_index_->DeleteURL(matches[0].url_info); 588 url_index_->DeleteURL(matches[0].url_info.id());
512 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty()); 589 EXPECT_TRUE(url_index_->HistoryItemsForTerms(terms).empty());
513 } 590 }
514 591
515 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { 592 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) {
516 struct TestData { 593 struct TestData {
517 const std::string url_spec; 594 const std::string url_spec;
518 const bool expected_is_whitelisted; 595 const bool expected_is_whitelisted;
519 } data[] = { 596 } data[] = {
520 // URLs with whitelisted schemes. 597 // URLs with whitelisted schemes.
521 { "about:histograms", true }, 598 { "about:histograms", true },
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false }, 655 { "telnet://mayor_margie:one2rule4All@www.mycity.com:6789/", false },
579 { "tftp://example.com/mystartupfile", false }, 656 { "tftp://example.com/mystartupfile", false },
580 { "tip://123.123.123.123/?urn:xopen:xid", false }, 657 { "tip://123.123.123.123/?urn:xopen:xid", false },
581 { "tv:nbc.com", false }, 658 { "tv:nbc.com", false },
582 { "urn:foo:A123,456", false }, 659 { "urn:foo:A123,456", false },
583 { "vemmi://zeus.mctel.fr/demo", false }, 660 { "vemmi://zeus.mctel.fr/demo", false },
584 { "wais://www.mydomain.net:8765/mydatabase", false }, 661 { "wais://www.mydomain.net:8765/mydatabase", false },
585 { "xmpp:node@example.com", false }, 662 { "xmpp:node@example.com", false },
586 { "xmpp://guest@example.com", false }, 663 { "xmpp://guest@example.com", false },
587 }; 664 };
588 url_index_.reset(new InMemoryURLIndex(NULL, 665 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL(
589 FilePath(FILE_PATH_LITERAL(
590 "/flammmy/frammy/")))); 666 "/flammmy/frammy/"))));
591 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { 667 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
592 GURL url(data[i].url_spec); 668 GURL url(data[i].url_spec);
593 EXPECT_EQ(data[i].expected_is_whitelisted, 669 EXPECT_EQ(data[i].expected_is_whitelisted,
594 url_index_->URLSchemeIsWhitelisted(url)); 670 url_index_->URLSchemeIsWhitelisted(url));
595 } 671 }
596 } 672 }
597 673
598 TEST_F(InMemoryURLIndexTest, CacheFilePath) { 674 TEST_F(InMemoryURLIndexTest, CacheFilePath) {
599 url_index_.reset(new InMemoryURLIndex( 675 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL(
600 NULL, FilePath(FILE_PATH_LITERAL("/flammmy/frammy/")))); 676 "/flammmy/frammy/"))));
601 FilePath full_file_path; 677 FilePath full_file_path;
602 url_index_->GetCacheFilePath(&full_file_path); 678 url_index_->GetCacheFilePath(&full_file_path);
603 std::vector<FilePath::StringType> expected_parts; 679 std::vector<FilePath::StringType> expected_parts;
604 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")). 680 FilePath(FILE_PATH_LITERAL("/flammmy/frammy/History Provider Cache")).
605 GetComponents(&expected_parts); 681 GetComponents(&expected_parts);
606 std::vector<FilePath::StringType> actual_parts; 682 std::vector<FilePath::StringType> actual_parts;
607 full_file_path.GetComponents(&actual_parts); 683 full_file_path.GetComponents(&actual_parts);
608 ASSERT_EQ(expected_parts.size(), actual_parts.size()); 684 ASSERT_EQ(expected_parts.size(), actual_parts.size());
609 size_t count = expected_parts.size(); 685 size_t count = expected_parts.size();
610 for (size_t i = 0; i < count; ++i) 686 for (size_t i = 0; i < count; ++i)
611 EXPECT_EQ(expected_parts[i], actual_parts[i]); 687 EXPECT_EQ(expected_parts[i], actual_parts[i]);
612 } 688 }
613 689
614 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { 690 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) {
615 // Save the cache to a protobuf, restore it, and compare the results. 691 // Save the cache to a protobuf, restore it, and compare the results.
616 url_index_.reset(new InMemoryURLIndex(NULL, 692 url_index_.reset(new InMemoryURLIndex(FilePath(FILE_PATH_LITERAL("/dummy"))));
617 FilePath(FILE_PATH_LITERAL("/dummy"))));
618 InMemoryURLIndex& url_index(*(url_index_.get())); 693 InMemoryURLIndex& url_index(*(url_index_.get()));
619 url_index.Init(this, "en,ja,hi,zh"); 694 url_index.Init(this, "en,ja,hi,zh");
620 in_memory_url_index::InMemoryURLIndexCacheItem index_cache; 695 in_memory_url_index::InMemoryURLIndexCacheItem index_cache;
621 url_index.SavePrivateData(&index_cache); 696 url_index.SavePrivateData(&index_cache);
622 697
623 // Capture our private data so we can later compare for equality. 698 // Capture our private data so we can later compare for equality.
624 URLIndexPrivateData& private_data(*(url_index_->private_data_)); 699 int history_item_count(url_index.history_item_count_);
625 String16Vector word_list(private_data.word_list_); 700 InMemoryURLIndex::String16Vector word_list(url_index.word_list_);
626 WordMap word_map(private_data.word_map_); 701 InMemoryURLIndex::WordMap word_map(url_index.word_map_);
627 CharWordIDMap char_word_map(private_data.char_word_map_); 702 InMemoryURLIndex::CharWordIDMap char_word_map(url_index.char_word_map_);
628 WordIDHistoryMap word_id_history_map(private_data.word_id_history_map_); 703 InMemoryURLIndex::WordIDHistoryMap word_id_history_map(
629 HistoryIDWordMap history_id_word_map(private_data.history_id_word_map_); 704 url_index.word_id_history_map_);
630 HistoryInfoMap history_info_map(private_data.history_info_map_); 705 InMemoryURLIndex::HistoryInfoMap history_info_map(
706 url_index.history_info_map_);
631 707
632 // Prove that there is really something there. 708 // Prove that there is really something there.
633 EXPECT_FALSE(private_data.word_list_.empty()); 709 EXPECT_GT(url_index.history_item_count_, 0);
634 // available_words_ will already be empty since we have freshly built the 710 EXPECT_FALSE(url_index.word_list_.empty());
635 // data set for this test. 711 EXPECT_FALSE(url_index.word_map_.empty());
636 EXPECT_TRUE(private_data.available_words_.empty()); 712 EXPECT_FALSE(url_index.char_word_map_.empty());
637 EXPECT_FALSE(private_data.word_map_.empty()); 713 EXPECT_FALSE(url_index.word_id_history_map_.empty());
638 EXPECT_FALSE(private_data.char_word_map_.empty()); 714 EXPECT_FALSE(url_index.history_info_map_.empty());
639 EXPECT_FALSE(private_data.word_id_history_map_.empty());
640 EXPECT_FALSE(private_data.history_id_word_map_.empty());
641 EXPECT_FALSE(private_data.history_info_map_.empty());
642 715
643 // Clear and then prove it's clear. 716 // Clear and then prove it's clear.
644 url_index.ClearPrivateData(); 717 url_index.ClearPrivateData();
645 EXPECT_TRUE(private_data.word_list_.empty()); 718 EXPECT_EQ(0, url_index.history_item_count_);
646 EXPECT_TRUE(private_data.available_words_.empty()); 719 EXPECT_TRUE(url_index.word_list_.empty());
647 EXPECT_TRUE(private_data.word_map_.empty()); 720 EXPECT_TRUE(url_index.word_map_.empty());
648 EXPECT_TRUE(private_data.char_word_map_.empty()); 721 EXPECT_TRUE(url_index.char_word_map_.empty());
649 EXPECT_TRUE(private_data.word_id_history_map_.empty()); 722 EXPECT_TRUE(url_index.word_id_history_map_.empty());
650 EXPECT_TRUE(private_data.history_id_word_map_.empty()); 723 EXPECT_TRUE(url_index.history_info_map_.empty());
651 EXPECT_TRUE(private_data.history_info_map_.empty());
652 724
653 // Restore the cache. 725 // Restore the cache.
654 EXPECT_TRUE(url_index.RestorePrivateData(index_cache)); 726 EXPECT_TRUE(url_index.RestorePrivateData(index_cache));
655 727
656 // Compare the restored and captured for equality. 728 // Compare the restored and captured for equality.
657 EXPECT_EQ(word_list.size(), private_data.word_list_.size()); 729 EXPECT_EQ(history_item_count, url_index.history_item_count_);
658 EXPECT_EQ(word_map.size(), private_data.word_map_.size()); 730 EXPECT_EQ(word_list.size(), url_index.word_list_.size());
659 EXPECT_EQ(char_word_map.size(), private_data.char_word_map_.size()); 731 EXPECT_EQ(word_map.size(), url_index.word_map_.size());
660 EXPECT_EQ(word_id_history_map.size(), 732 EXPECT_EQ(char_word_map.size(), url_index.char_word_map_.size());
661 private_data.word_id_history_map_.size()); 733 EXPECT_EQ(word_id_history_map.size(), url_index.word_id_history_map_.size());
662 EXPECT_EQ(history_id_word_map.size(), 734 EXPECT_EQ(history_info_map.size(), url_index.history_info_map_.size());
663 private_data.history_id_word_map_.size());
664 EXPECT_EQ(history_info_map.size(), private_data.history_info_map_.size());
665 // WordList must be index-by-index equal. 735 // WordList must be index-by-index equal.
666 size_t count = word_list.size(); 736 size_t count = word_list.size();
667 for (size_t i = 0; i < count; ++i) 737 for (size_t i = 0; i < count; ++i)
668 EXPECT_EQ(word_list[i], private_data.word_list_[i]); 738 EXPECT_EQ(word_list[i], url_index.word_list_[i]);
669 739 for (InMemoryURLIndex::CharWordIDMap::const_iterator expected =
670 ExpectMapOfContainersIdentical(char_word_map, 740 char_word_map.begin(); expected != char_word_map.end(); ++expected) {
671 private_data.char_word_map_); 741 InMemoryURLIndex::CharWordIDMap::const_iterator actual =
672 ExpectMapOfContainersIdentical(word_id_history_map, 742 url_index.char_word_map_.find(expected->first);
673 private_data.word_id_history_map_); 743 ASSERT_TRUE(url_index.char_word_map_.end() != actual);
674 ExpectMapOfContainersIdentical(history_id_word_map, 744 const InMemoryURLIndex::WordIDSet& expected_set(expected->second);
675 private_data.history_id_word_map_); 745 const InMemoryURLIndex::WordIDSet& actual_set(actual->second);
676 746 ASSERT_EQ(expected_set.size(), actual_set.size());
677 for (HistoryInfoMap::const_iterator expected = history_info_map.begin(); 747 for (InMemoryURLIndex::WordIDSet::const_iterator set_iter =
678 expected != history_info_map.end(); ++expected) { 748 expected_set.begin(); set_iter != expected_set.end(); ++set_iter)
679 HistoryInfoMap::const_iterator actual = 749 EXPECT_GT(actual_set.count(*set_iter), 0U);
680 private_data.history_info_map_.find(expected->first); 750 }
681 ASSERT_FALSE(private_data.history_info_map_.end() == actual); 751 for (InMemoryURLIndex::WordIDHistoryMap::const_iterator expected =
752 word_id_history_map.begin(); expected != word_id_history_map.end();
753 ++expected) {
754 InMemoryURLIndex::WordIDHistoryMap::const_iterator actual =
755 url_index.word_id_history_map_.find(expected->first);
756 ASSERT_TRUE(url_index.word_id_history_map_.end() != actual);
757 const InMemoryURLIndex::HistoryIDSet& expected_set(expected->second);
758 const InMemoryURLIndex::HistoryIDSet& actual_set(actual->second);
759 ASSERT_EQ(expected_set.size(), actual_set.size());
760 for (InMemoryURLIndex::HistoryIDSet::const_iterator set_iter =
761 expected_set.begin(); set_iter != expected_set.end(); ++set_iter)
762 EXPECT_GT(actual_set.count(*set_iter), 0U);
763 }
764 for (InMemoryURLIndex::HistoryInfoMap::const_iterator expected =
765 history_info_map.begin(); expected != history_info_map.end();
766 ++expected) {
767 InMemoryURLIndex::HistoryInfoMap::const_iterator actual =
768 url_index.history_info_map_.find(expected->first);
769 ASSERT_FALSE(url_index.history_info_map_.end() == actual);
682 const URLRow& expected_row(expected->second); 770 const URLRow& expected_row(expected->second);
683 const URLRow& actual_row(actual->second); 771 const URLRow& actual_row(actual->second);
684 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); 772 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count());
685 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); 773 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count());
686 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); 774 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit());
687 EXPECT_EQ(expected_row.url(), actual_row.url()); 775 EXPECT_EQ(expected_row.url(), actual_row.url());
688 } 776 }
689 } 777 }
690 778
691 } // namespace history 779 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698