Chromium Code Reviews| 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 #if defined(OS_MACOSX) | |
| 1218 // FIXME(pritam.nikam): Flaky on mac trybots: C++11 error about initializing | |
| 1219 // explicit constructor with {}. | |
| 1220 #define MAX_TERMS 2 | |
| 1221 typedef size_t SizeVector[MAX_TERMS]; | |
| 1222 #else | |
| 1223 typedef std::vector<size_t> SizeVector; | |
| 1224 #endif | |
| 1225 | |
| 1226 TEST_F(InMemoryURLIndexTest, AddHistoryMatch) { | |
| 1227 const struct { | |
| 1228 const char* search_string; | |
| 1229 size_t cursor_position; | |
| 1230 const SizeVector expected_word_starts_offsets; | |
|
Mark P
2015/06/10 16:53:01
Instead of this approach, just use a fixed size ar
Pritam Nikam
2015/06/11 03:06:09
Done.
| |
| 1231 } test_cases[] = { | |
| 1232 /* No punctuations, only cursor position change. */ | |
| 1233 { "ABCD", base::string16::npos, {0} }, | |
| 1234 { "abcd", 0, {0} }, | |
| 1235 { "AbcD", 1, {0, 0} }, | |
| 1236 { "abcd", 4, {0} }, | |
| 1237 | |
| 1238 /* Starting with punctuation. */ | |
| 1239 { ".abcd", base::string16::npos, {1} }, | |
| 1240 { ".abcd", 0, {1} }, | |
| 1241 { "!abcd", 1, {1, 0} }, | |
| 1242 { "::abcd", 1, {1, 1} }, | |
| 1243 { ":abcd", 5, {1} }, | |
| 1244 | |
| 1245 /* Ending with punctuation. */ | |
| 1246 { "abcd://", base::string16::npos, {0} }, | |
| 1247 { "ABCD://", 0, {0} }, | |
| 1248 { "abcd://", 1, {0, 0} }, | |
| 1249 { "abcd://", 4, {0, 3} }, | |
| 1250 { "abcd://", 7, {0} }, | |
| 1251 | |
| 1252 /* Punctuation in the middle. */ | |
| 1253 { "ab.cd", base::string16::npos, {0} }, | |
| 1254 { "ab.cd", 0, {0} }, | |
| 1255 { "ab!cd", 1, {0, 0} }, | |
| 1256 { "AB.cd", 2, {0, 1} }, | |
| 1257 { "AB.cd", 3, {0, 0} }, | |
| 1258 { "ab:cd", 5, {0} }, | |
| 1259 | |
| 1260 /* Hyphenation */ | |
| 1261 { "Ab-cd", base::string16::npos, {0} }, | |
| 1262 { "ab-cd", 0, {0} }, | |
| 1263 { "-abcd", 0, {1} }, | |
| 1264 { "-abcd", 1, {1, 0} }, | |
| 1265 { "abcd-", 2, {0, 0} }, | |
| 1266 { "abcd-", 4, {0, 1} }, | |
| 1267 { "ab-cd", 5, {0} } | |
| 1268 }; | |
| 1269 | |
| 1270 for (size_t i = 0; i < arraysize(test_cases); ++i) { | |
| 1271 SCOPED_TRACE(testing::Message() | |
| 1272 << "search_string = " << test_cases[i].search_string | |
| 1273 << ", cursor_position = " << test_cases[i].cursor_position); | |
| 1274 | |
| 1275 base::string16 lower_string; | |
| 1276 String16Vector lower_terms; | |
| 1277 StringToTerms(test_cases[i].search_string, test_cases[i].cursor_position, | |
| 1278 &lower_string, &lower_terms); | |
| 1279 URLIndexPrivateData::AddHistoryMatch match(nullptr, *GetPrivateData(), | |
| 1280 kTestLanguages, lower_string, | |
| 1281 lower_terms, base::Time::Now()); | |
| 1282 | |
| 1283 // Verify against expectations. | |
| 1284 #if defined(OS_MACOSX) | |
| 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 } | |
| 1290 #else | |
| 1291 EXPECT_EQ(test_cases[i].expected_word_starts_offsets, | |
| 1292 match.lower_terms_to_word_starts_offsets_); | |
| 1293 #endif | |
| 1294 } | |
| 1295 } | |
| 1296 | |
| 1196 class InMemoryURLIndexCacheTest : public testing::Test { | 1297 class InMemoryURLIndexCacheTest : public testing::Test { |
| 1197 public: | 1298 public: |
| 1198 InMemoryURLIndexCacheTest() {} | 1299 InMemoryURLIndexCacheTest() {} |
| 1199 | 1300 |
| 1200 protected: | 1301 protected: |
| 1201 void SetUp() override; | 1302 void SetUp() override; |
| 1202 void TearDown() override; | 1303 void TearDown() override; |
| 1203 | 1304 |
| 1204 // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 1305 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
| 1205 void set_history_dir(const base::FilePath& dir_path); | 1306 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)); | 1343 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); |
| 1243 std::vector<base::FilePath::StringType> actual_parts; | 1344 std::vector<base::FilePath::StringType> actual_parts; |
| 1244 full_file_path.GetComponents(&actual_parts); | 1345 full_file_path.GetComponents(&actual_parts); |
| 1245 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1346 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
| 1246 size_t count = expected_parts.size(); | 1347 size_t count = expected_parts.size(); |
| 1247 for (size_t i = 0; i < count; ++i) | 1348 for (size_t i = 0; i < count; ++i) |
| 1248 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1349 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
| 1249 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1350 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
| 1250 set_history_dir(base::FilePath()); | 1351 set_history_dir(base::FilePath()); |
| 1251 } | 1352 } |
| OLD | NEW |