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

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

Issue 1163963004: [Omnibox] Changing scheme from https:// to http:// results in DCHECK (Always). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Incorporates review. Created 5 years, 6 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
« no previous file with comments | « no previous file | chrome/browser/autocomplete/url_index_private_data.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 15 matching lines...) Expand all
37 // The only difference between this table and a live 'urls' table from a 38 // The only difference between this table and a live 'urls' table from a
38 // profile is that the last_visit_time column in the test table contains a 39 // profile is that the last_visit_time column in the test table contains a
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 {
48 const size_t kInvalid = base::string16::npos;
47 const size_t kMaxMatches = 3; 49 const size_t kMaxMatches = 3;
48 const char kTestLanguages[] = "en,ja,hi,zh"; 50 const char kTestLanguages[] = "en,ja,hi,zh";
51
52 // Helper function to set lower case |lower_string| and |lower_terms| (words
53 // list) based on supplied |search_string| and |cursor_position|. If
54 // |cursor_position| is set and useful (not at either end of the string), allow
55 // the |search_string| to be broken at |cursor_position|. We do this by
56 // pretending there's a space where the cursor is. |lower_terms| are obtained by
57 // splitting the |lower_string| on whitespace into tokens.
58 void StringToTerms(const char* search_string,
59 size_t cursor_position,
60 base::string16* lower_string,
61 String16Vector* lower_terms) {
62 *lower_string = base::i18n::ToLower(ASCIIToUTF16(search_string));
63 if ((cursor_position != kInvalid) &&
64 (cursor_position < lower_string->length()) && (cursor_position > 0)) {
65 lower_string->insert(cursor_position, base::ASCIIToUTF16(" "));
66 }
67
68 Tokenize(*lower_string, base::kWhitespaceUTF16, lower_terms);
69 }
70
49 } // namespace 71 } // namespace
50 72
51 // ----------------------------------------------------------------------------- 73 // -----------------------------------------------------------------------------
52 74
53 // Observer class so the unit tests can wait while the cache is being saved. 75 // Observer class so the unit tests can wait while the cache is being saved.
54 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver { 76 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver {
55 public: 77 public:
56 explicit CacheFileSaverObserver(const base::Closure& task); 78 explicit CacheFileSaverObserver(const base::Closure& task);
57 79
58 bool succeeded() { return succeeded_; } 80 bool succeeded() { return succeeded_; }
(...skipping 1127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 // Make sure the data we have was rebuilt from history. (Version 0 1208 // Make sure the data we have was rebuilt from history. (Version 0
1187 // means rebuilt from history; anything else means restored from 1209 // means rebuilt from history; anything else means restored from
1188 // a cache version.) 1210 // a cache version.)
1189 EXPECT_EQ(0, new_data.restored_cache_version_); 1211 EXPECT_EQ(0, new_data.restored_cache_version_);
1190 EXPECT_NE(fake_rebuild_time, new_data.last_time_rebuilt_from_history_); 1212 EXPECT_NE(fake_rebuild_time, new_data.last_time_rebuilt_from_history_);
1191 1213
1192 // Compare the captured and restored for equality. 1214 // Compare the captured and restored for equality.
1193 ExpectPrivateDataEqual(*old_data.get(), new_data); 1215 ExpectPrivateDataEqual(*old_data.get(), new_data);
1194 } 1216 }
1195 1217
1218 TEST_F(InMemoryURLIndexTest, AddHistoryMatch) {
1219 const struct {
1220 const char* search_string;
1221 size_t cursor_position;
1222 const size_t expected_word_starts_offsets_size;
1223 const size_t expected_word_starts_offsets[4];
Mark P 2015/06/12 20:02:26 FYI: this can be [3] if you want to bother touchin
Pritam Nikam 2015/06/13 10:48:16 Sure, done.
1224 } test_cases[] = {
1225 /* No punctuations, only cursor position change. */
1226 { "ABCD", kInvalid, 1, {0, kInvalid, kInvalid, kInvalid} },
1227 { "abcd", 0, 1, {0, kInvalid, kInvalid, kInvalid} },
1228 { "AbcD", 1, 2, {0, 0, kInvalid, kInvalid} },
1229 { "abcd", 4, 1, {0, kInvalid, kInvalid, kInvalid} },
1230
1231 /* Starting with punctuation. */
1232 { ".abcd", kInvalid, 1, {1, kInvalid, kInvalid, kInvalid} },
1233 { ".abcd", 0, 1, {1, kInvalid, kInvalid, kInvalid} },
1234 { "!abcd", 1, 2, {1, 0, kInvalid, kInvalid} },
1235 { "::abcd", 1, 2, {1, 1, kInvalid, kInvalid} },
1236 { ":abcd", 5, 1, {1, kInvalid, kInvalid, kInvalid} },
1237
1238 /* Ending with punctuation. */
1239 { "abcd://", kInvalid, 1, {0, kInvalid, kInvalid, kInvalid} },
1240 { "ABCD://", 0, 1, {0, kInvalid, kInvalid, kInvalid} },
1241 { "abcd://", 1, 2, {0, 0, kInvalid, kInvalid} },
1242 { "abcd://", 4, 2, {0, 3, kInvalid, kInvalid} },
1243 { "abcd://", 7, 1, {0, kInvalid, kInvalid, kInvalid} },
1244
1245 /* Punctuation in the middle. */
1246 { "ab.cd", kInvalid, 1, {0, kInvalid, kInvalid, kInvalid} },
1247 { "ab.cd", 0, 1, {0, kInvalid, kInvalid, kInvalid} },
1248 { "ab!cd", 1, 2, {0, 0, kInvalid, kInvalid} },
1249 { "AB.cd", 2, 2, {0, 1, kInvalid, kInvalid} },
1250 { "AB.cd", 3, 2, {0, 0, kInvalid, kInvalid} },
1251 { "ab:cd", 5, 1, {0, kInvalid, kInvalid, kInvalid} },
1252
1253 /* Hyphenation */
1254 { "Ab-cd", kInvalid, 1, {0, kInvalid, kInvalid, kInvalid} },
1255 { "ab-cd", 0, 1, {0, kInvalid, kInvalid, kInvalid} },
1256 { "-abcd", 0, 1, {1, kInvalid, kInvalid, kInvalid} },
1257 { "-abcd", 1, 2, {1, 0, kInvalid, kInvalid} },
1258 { "abcd-", 2, 2, {0, 0, kInvalid, kInvalid} },
1259 { "abcd-", 4, 2, {0, 1, kInvalid, kInvalid} },
1260 { "ab-cd", 5, 1, {0, kInvalid, kInvalid, kInvalid} },
1261
1262 /* Whitespace */
1263 { "Ab cd", kInvalid, 2, {0, 0, kInvalid, kInvalid} },
1264 { "ab cd", 0, 2, {0, 0, kInvalid, kInvalid} },
1265 { " abcd", 0, 1, {0, kInvalid, kInvalid, kInvalid} },
1266 { " abcd", 1, 1, {0, kInvalid, kInvalid, kInvalid} },
1267 { "abcd ", 2, 2, {0, 0, kInvalid, kInvalid} },
1268 { "abcd :", 4, 2, {0, 1, kInvalid, kInvalid} },
1269 { "abcd :", 5, 2, {0, 1, kInvalid, kInvalid} },
1270 { "abcd :", 2, 3, {0, 0, 1, kInvalid} }
1271 };
1272
1273 for (size_t i = 0; i < arraysize(test_cases); ++i) {
1274 SCOPED_TRACE(testing::Message()
1275 << "search_string = " << test_cases[i].search_string
1276 << ", cursor_position = " << test_cases[i].cursor_position);
1277
1278 base::string16 lower_string;
1279 String16Vector lower_terms;
1280 StringToTerms(test_cases[i].search_string, test_cases[i].cursor_position,
1281 &lower_string, &lower_terms);
1282 URLIndexPrivateData::AddHistoryMatch match(nullptr, *GetPrivateData(),
1283 kTestLanguages, lower_string,
1284 lower_terms, base::Time::Now());
1285
1286 // Verify against expectations.
1287 EXPECT_EQ(test_cases[i].expected_word_starts_offsets_size,
1288 match.lower_terms_to_word_starts_offsets_.size());
1289 for (size_t j = 0; j < test_cases[i].expected_word_starts_offsets_size;
1290 ++j) {
1291 EXPECT_EQ(test_cases[i].expected_word_starts_offsets[j],
1292 match.lower_terms_to_word_starts_offsets_[j]);
1293 }
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
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 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/autocomplete/url_index_private_data.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698