OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <fstream> | 6 #include <fstream> |
7 | 7 |
8 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/i18n/case_conversion.h" | |
12 #include "base/path_service.h" | 13 #include "base/path_service.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/autocomplete/in_memory_url_index.h" | 18 #include "chrome/browser/autocomplete/in_memory_url_index.h" |
18 #include "chrome/browser/autocomplete/in_memory_url_index_types.h" | 19 #include "chrome/browser/autocomplete/in_memory_url_index_types.h" |
19 #include "chrome/browser/autocomplete/url_index_private_data.h" | 20 #include "chrome/browser/autocomplete/url_index_private_data.h" |
20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 21 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
21 #include "chrome/browser/history/history_service_factory.h" | 22 #include "chrome/browser/history/history_service_factory.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
39 // number specifying the number of days relative to 'today' to which the | 40 // number specifying the number of days relative to 'today' to which the |
40 // absolute time should be set during the test setup stage. | 41 // absolute time should be set during the test setup stage. |
41 // | 42 // |
42 // The format of the test database text file is of a SQLite .dump file. | 43 // The format of the test database text file is of a SQLite .dump file. |
43 // Note that only lines whose first character is an upper-case letter are | 44 // Note that only lines whose first character is an upper-case letter are |
44 // processed when creating the test database. | 45 // processed when creating the test database. |
45 | 46 |
46 namespace { | 47 namespace { |
47 const size_t kMaxMatches = 3; | 48 const size_t kMaxMatches = 3; |
48 const char kTestLanguages[] = "en,ja,hi,zh"; | 49 const char kTestLanguages[] = "en,ja,hi,zh"; |
50 | |
51 // Helper function to set lower case |lower_string| and |lower_terms| (words | |
52 // list) based on supplied |search_string| and |cursor_position|. If | |
53 // |cursor_position| is set and useful (not at either end of the string), allow | |
54 // the |search_string| to be broken at |cursor_position|. We do this by | |
55 // pretending there's a space where the cursor is. |lower_terms| are obtained by | |
56 // splitting the |lower_string| on whitespace into tokens. | |
57 void StringToTerms(const char* search_string, | |
58 size_t cursor_position, | |
59 base::string16* lower_string, | |
60 String16Vector* lower_terms) { | |
61 *lower_string = base::i18n::ToLower(ASCIIToUTF16(search_string)); | |
62 if ((cursor_position != base::string16::npos) && | |
63 (cursor_position < lower_string->length()) && (cursor_position > 0)) { | |
64 lower_string->insert(cursor_position, base::ASCIIToUTF16(" ")); | |
65 } | |
66 | |
67 Tokenize(*lower_string, base::kWhitespaceUTF16, lower_terms); | |
68 } | |
69 | |
49 } // namespace | 70 } // namespace |
50 | 71 |
51 // ----------------------------------------------------------------------------- | 72 // ----------------------------------------------------------------------------- |
52 | 73 |
53 // Observer class so the unit tests can wait while the cache is being saved. | 74 // Observer class so the unit tests can wait while the cache is being saved. |
54 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver { | 75 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver { |
55 public: | 76 public: |
56 explicit CacheFileSaverObserver(const base::Closure& task); | 77 explicit CacheFileSaverObserver(const base::Closure& task); |
57 | 78 |
58 bool succeeded() { return succeeded_; } | 79 bool succeeded() { return succeeded_; } |
(...skipping 1127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1186 // Make sure the data we have was rebuilt from history. (Version 0 | 1207 // Make sure the data we have was rebuilt from history. (Version 0 |
1187 // means rebuilt from history; anything else means restored from | 1208 // means rebuilt from history; anything else means restored from |
1188 // a cache version.) | 1209 // a cache version.) |
1189 EXPECT_EQ(0, new_data.restored_cache_version_); | 1210 EXPECT_EQ(0, new_data.restored_cache_version_); |
1190 EXPECT_NE(fake_rebuild_time, new_data.last_time_rebuilt_from_history_); | 1211 EXPECT_NE(fake_rebuild_time, new_data.last_time_rebuilt_from_history_); |
1191 | 1212 |
1192 // Compare the captured and restored for equality. | 1213 // Compare the captured and restored for equality. |
1193 ExpectPrivateDataEqual(*old_data.get(), new_data); | 1214 ExpectPrivateDataEqual(*old_data.get(), new_data); |
1194 } | 1215 } |
1195 | 1216 |
1217 TEST_F(InMemoryURLIndexTest, AddHistoryMatch) { | |
1218 const struct { | |
1219 const char* search_string; | |
1220 size_t cursor_position; | |
1221 const size_t expected_word_starts_offsets[4]; | |
1222 } test_cases[] = { | |
1223 /* No punctuations, only cursor position change. */ | |
1224 { "ABCD", base::string16::npos, {0, -1, -1, -1} }, | |
1225 { "abcd", 0, {0, -1, -1, -1} }, | |
1226 { "AbcD", 1, {0, 0, -1, -1} }, | |
1227 { "abcd", 4, {0, -1, -1, -1} }, | |
1228 | |
1229 /* Starting with punctuation. */ | |
1230 { ".abcd", base::string16::npos, {1, -1, -1, -1} }, | |
1231 { ".abcd", 0, {1, -1, -1, -1} }, | |
1232 { "!abcd", 1, {1, 0, -1, -1} }, | |
1233 { "::abcd", 1, {1, 1, -1, -1} }, | |
1234 { ":abcd", 5, {1, -1, -1, -1} }, | |
1235 | |
1236 /* Ending with punctuation. */ | |
1237 { "abcd://", base::string16::npos, {0, -1, -1, -1} }, | |
1238 { "ABCD://", 0, {0, -1, -1, -1} }, | |
1239 { "abcd://", 1, {0, 0, -1, -1} }, | |
1240 { "abcd://", 4, {0, 3, -1, -1} }, | |
1241 { "abcd://", 7, {0, -1, -1, -1} }, | |
1242 | |
1243 /* Punctuation in the middle. */ | |
1244 { "ab.cd", base::string16::npos, {0, -1, -1, -1} }, | |
1245 { "ab.cd", 0, {0, -1, -1, -1} }, | |
1246 { "ab!cd", 1, {0, 0, -1, -1} }, | |
1247 { "AB.cd", 2, {0, 1, -1, -1} }, | |
1248 { "AB.cd", 3, {0, 0, -1, -1} }, | |
1249 { "ab:cd", 5, {0, -1, -1, -1} }, | |
1250 | |
1251 /* Hyphenation */ | |
1252 { "Ab-cd", base::string16::npos, {0, -1, -1, -1} }, | |
1253 { "ab-cd", 0, {0, -1, -1, -1} }, | |
1254 { "-abcd", 0, {1, -1, -1, -1} }, | |
1255 { "-abcd", 1, {1, 0, -1, -1} }, | |
1256 { "abcd-", 2, {0, 0, -1, -1} }, | |
1257 { "abcd-", 4, {0, 1, -1, -1} }, | |
1258 { "ab-cd", 5, {0, -1, -1, -1} }, | |
1259 | |
1260 /* Whitespace */ | |
1261 { "Ab cd", base::string16::npos, {0, 0, -1, -1} }, | |
1262 { "ab cd", 0, {0, 0, -1, -1} }, | |
1263 { " abcd", 0, {0, -1, -1, -1} }, | |
1264 { " abcd", 1, {0, 0, -1, -1} }, | |
1265 { "abcd ", 2, {0, 0, -1, -1} }, | |
1266 { "abcd :", 4, {0, 1, -1, -1} }, | |
1267 { "abcd :", 5, {0, 1, -1, -1} }, | |
1268 { "abcd :", 2, {0, 0, 1, -1} } | |
1269 }; | |
1270 | |
1271 for (size_t i = 0; i < arraysize(test_cases); ++i) { | |
1272 SCOPED_TRACE(testing::Message() | |
1273 << "search_string = " << test_cases[i].search_string | |
1274 << ", cursor_position = " << test_cases[i].cursor_position); | |
1275 | |
1276 base::string16 lower_string; | |
1277 String16Vector lower_terms; | |
1278 StringToTerms(test_cases[i].search_string, test_cases[i].cursor_position, | |
1279 &lower_string, &lower_terms); | |
1280 URLIndexPrivateData::AddHistoryMatch match(nullptr, *GetPrivateData(), | |
1281 kTestLanguages, lower_string, | |
1282 lower_terms, base::Time::Now()); | |
1283 | |
1284 // Verify against expectations. | |
1285 for (size_t j = 0; j < match.lower_terms_to_word_starts_offsets_.size(); | |
1286 ++j) { | |
1287 EXPECT_EQ(test_cases[i].expected_word_starts_offsets[j], | |
1288 match.lower_terms_to_word_starts_offsets_[j]); | |
1289 } | |
Mark P
2015/06/11 18:37:06
You should either check that match.lower_terms_to_
Pritam Nikam
2015/06/12 06:09:50
Done.
Now, I'm matching match.lower_terms_to_word
| |
1290 } | |
1291 } | |
1292 | |
1196 class InMemoryURLIndexCacheTest : public testing::Test { | 1293 class InMemoryURLIndexCacheTest : public testing::Test { |
1197 public: | 1294 public: |
1198 InMemoryURLIndexCacheTest() {} | 1295 InMemoryURLIndexCacheTest() {} |
1199 | 1296 |
1200 protected: | 1297 protected: |
1201 void SetUp() override; | 1298 void SetUp() override; |
1202 void TearDown() override; | 1299 void TearDown() override; |
1203 | 1300 |
1204 // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 1301 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
1205 void set_history_dir(const base::FilePath& dir_path); | 1302 void set_history_dir(const base::FilePath& dir_path); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1242 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); | 1339 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); |
1243 std::vector<base::FilePath::StringType> actual_parts; | 1340 std::vector<base::FilePath::StringType> actual_parts; |
1244 full_file_path.GetComponents(&actual_parts); | 1341 full_file_path.GetComponents(&actual_parts); |
1245 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1342 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
1246 size_t count = expected_parts.size(); | 1343 size_t count = expected_parts.size(); |
1247 for (size_t i = 0; i < count; ++i) | 1344 for (size_t i = 0; i < count; ++i) |
1248 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1345 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
1249 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1346 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
1250 set_history_dir(base::FilePath()); | 1347 set_history_dir(base::FilePath()); |
1251 } | 1348 } |
OLD | NEW |