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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "components/history/core/browser/history_database.h" | 28 #include "components/history/core/browser/history_database.h" |
29 #include "components/history/core/browser/in_memory_url_index_types.h" | 29 #include "components/history/core/browser/in_memory_url_index_types.h" |
30 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
31 #include "sql/transaction.h" | 31 #include "sql/transaction.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
33 | 33 |
34 using base::ASCIIToUTF16; | 34 using base::ASCIIToUTF16; |
35 | 35 |
36 namespace { | 36 namespace { |
37 const size_t kMaxMatches = 3; | 37 const size_t kMaxMatches = 3; |
| 38 const char kTestLanguages[] = "en,ja,hi,zh"; |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 // The test version of the history url database table ('url') is contained in | 41 // The test version of the history url database table ('url') is contained in |
41 // a database file created from a text file('url_history_provider_test.db.txt'). | 42 // a database file created from a text file('url_history_provider_test.db.txt'). |
42 // The only difference between this table and a live 'urls' table from a | 43 // The only difference between this table and a live 'urls' table from a |
43 // profile is that the last_visit_time column in the test table contains a | 44 // profile is that the last_visit_time column in the test table contains a |
44 // number specifying the number of days relative to 'today' to which the | 45 // number specifying the number of days relative to 'today' to which the |
45 // absolute time should be set during the test setup stage. | 46 // absolute time should be set during the test setup stage. |
46 // | 47 // |
47 // The format of the test database text file is of a SQLite .dump file. | 48 // The format of the test database text file is of a SQLite .dump file. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 82 |
82 // ----------------------------------------------------------------------------- | 83 // ----------------------------------------------------------------------------- |
83 | 84 |
84 class InMemoryURLIndexTest : public testing::Test { | 85 class InMemoryURLIndexTest : public testing::Test { |
85 public: | 86 public: |
86 InMemoryURLIndexTest(); | 87 InMemoryURLIndexTest(); |
87 | 88 |
88 protected: | 89 protected: |
89 // Test setup. | 90 // Test setup. |
90 void SetUp() override; | 91 void SetUp() override; |
| 92 void TearDown() override; |
91 | 93 |
92 // Allows the database containing the test data to be customized by | 94 // Allows the database containing the test data to be customized by |
93 // subclasses. | 95 // subclasses. |
94 virtual base::FilePath::StringType TestDBName() const; | 96 virtual base::FilePath::StringType TestDBName() const; |
95 | 97 |
| 98 // Allows the test to control when the InMemoryURLIndex is initialized. |
| 99 virtual bool InitializeInMemoryURLIndexInSetUp() const; |
| 100 |
| 101 // Initialize the InMemoryURLIndex for the tests. |
| 102 void InitializeInMemoryURLIndex(); |
| 103 |
96 // Validates that the given |term| is contained in |cache| and that it is | 104 // Validates that the given |term| is contained in |cache| and that it is |
97 // marked as in-use. | 105 // marked as in-use. |
98 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, | 106 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, |
99 base::string16 term) const; | 107 base::string16 term) const; |
100 | 108 |
101 // Pass-through function to simplify our friendship with HistoryService. | 109 // Pass-through function to simplify our friendship with HistoryService. |
102 sql::Connection& GetDB(); | 110 sql::Connection& GetDB(); |
103 | 111 |
104 // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 112 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
105 URLIndexPrivateData* GetPrivateData() const; | 113 URLIndexPrivateData* GetPrivateData() const; |
(...skipping 10 matching lines...) Expand all Loading... |
116 bool UpdateURL(const URLRow& row); | 124 bool UpdateURL(const URLRow& row); |
117 bool DeleteURL(const GURL& url); | 125 bool DeleteURL(const GURL& url); |
118 | 126 |
119 // Data verification helper functions. | 127 // Data verification helper functions. |
120 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); | 128 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); |
121 void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); | 129 void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); |
122 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, | 130 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, |
123 const URLIndexPrivateData& actual); | 131 const URLIndexPrivateData& actual); |
124 | 132 |
125 content::TestBrowserThreadBundle thread_bundle_; | 133 content::TestBrowserThreadBundle thread_bundle_; |
| 134 scoped_ptr<InMemoryURLIndex> url_index_; |
126 TestingProfile profile_; | 135 TestingProfile profile_; |
127 HistoryService* history_service_; | 136 HistoryService* history_service_; |
128 | |
129 scoped_ptr<InMemoryURLIndex> url_index_; | |
130 HistoryDatabase* history_database_; | 137 HistoryDatabase* history_database_; |
131 }; | 138 }; |
132 | 139 |
133 InMemoryURLIndexTest::InMemoryURLIndexTest() { | 140 InMemoryURLIndexTest::InMemoryURLIndexTest() { |
134 } | 141 } |
135 | 142 |
136 sql::Connection& InMemoryURLIndexTest::GetDB() { | 143 sql::Connection& InMemoryURLIndexTest::GetDB() { |
137 return history_database_->GetDB(); | 144 return history_database_->GetDB(); |
138 } | 145 } |
139 | 146 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 history_database_->FillVisitRow(statement, &row); | 273 history_database_->FillVisitRow(statement, &row); |
267 base::Time last_visit = time_right_now; | 274 base::Time last_visit = time_right_now; |
268 for (int64 i = row.visit_time.ToInternalValue(); i > 0; --i) | 275 for (int64 i = row.visit_time.ToInternalValue(); i > 0; --i) |
269 last_visit -= day_delta; | 276 last_visit -= day_delta; |
270 row.visit_time = last_visit; | 277 row.visit_time = last_visit; |
271 history_database_->UpdateVisitRow(row); | 278 history_database_->UpdateVisitRow(row); |
272 } | 279 } |
273 transaction.Commit(); | 280 transaction.Commit(); |
274 } | 281 } |
275 | 282 |
276 url_index_.reset(new InMemoryURLIndex(&profile_, | 283 if (InitializeInMemoryURLIndexInSetUp()) |
277 history_service_, | 284 InitializeInMemoryURLIndex(); |
278 base::FilePath(), | 285 } |
279 "en,ja,hi,zh", | 286 |
280 history_service_->history_client())); | 287 void InMemoryURLIndexTest::TearDown() { |
281 url_index_->Init(); | 288 // Ensure that the InMemoryURLIndex no longer observer HistoryService before |
282 url_index_->RebuildFromHistory(history_database_); | 289 // it is destroyed in order to prevent HistoryService calling dead observer. |
| 290 if (url_index_) |
| 291 url_index_->ShutDown(); |
283 } | 292 } |
284 | 293 |
285 base::FilePath::StringType InMemoryURLIndexTest::TestDBName() const { | 294 base::FilePath::StringType InMemoryURLIndexTest::TestDBName() const { |
286 return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); | 295 return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); |
287 } | 296 } |
288 | 297 |
| 298 bool InMemoryURLIndexTest::InitializeInMemoryURLIndexInSetUp() const { |
| 299 return true; |
| 300 } |
| 301 |
| 302 void InMemoryURLIndexTest::InitializeInMemoryURLIndex() { |
| 303 DCHECK(!url_index_); |
| 304 url_index_.reset(new InMemoryURLIndex(history_service_, base::FilePath(), |
| 305 kTestLanguages, |
| 306 history_service_->history_client())); |
| 307 url_index_->Init(); |
| 308 url_index_->RebuildFromHistory(history_database_); |
| 309 } |
| 310 |
289 void InMemoryURLIndexTest::CheckTerm( | 311 void InMemoryURLIndexTest::CheckTerm( |
290 const URLIndexPrivateData::SearchTermCacheMap& cache, | 312 const URLIndexPrivateData::SearchTermCacheMap& cache, |
291 base::string16 term) const { | 313 base::string16 term) const { |
292 URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( | 314 URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( |
293 cache.find(term)); | 315 cache.find(term)); |
294 ASSERT_TRUE(cache.end() != cache_iter) | 316 ASSERT_TRUE(cache.end() != cache_iter) |
295 << "Cache does not contain '" << term << "' but should."; | 317 << "Cache does not contain '" << term << "' but should."; |
296 URLIndexPrivateData::SearchTermCacheItem cache_item = cache_iter->second; | 318 URLIndexPrivateData::SearchTermCacheItem cache_item = cache_iter->second; |
297 EXPECT_TRUE(cache_item.used_) | 319 EXPECT_TRUE(cache_item.used_) |
298 << "Cache item '" << term << "' should be marked as being in use."; | 320 << "Cache item '" << term << "' should be marked as being in use."; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 expected_word_starts.title_word_starts_.end(), | 435 expected_word_starts.title_word_starts_.end(), |
414 actual_word_starts.title_word_starts_.begin())); | 436 actual_word_starts.title_word_starts_.begin())); |
415 } | 437 } |
416 } | 438 } |
417 | 439 |
418 //------------------------------------------------------------------------------ | 440 //------------------------------------------------------------------------------ |
419 | 441 |
420 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { | 442 class LimitedInMemoryURLIndexTest : public InMemoryURLIndexTest { |
421 protected: | 443 protected: |
422 base::FilePath::StringType TestDBName() const override; | 444 base::FilePath::StringType TestDBName() const override; |
| 445 bool InitializeInMemoryURLIndexInSetUp() const override; |
423 }; | 446 }; |
424 | 447 |
425 base::FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { | 448 base::FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { |
426 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); | 449 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); |
427 } | 450 } |
428 | 451 |
| 452 bool LimitedInMemoryURLIndexTest::InitializeInMemoryURLIndexInSetUp() const { |
| 453 return false; |
| 454 } |
| 455 |
429 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { | 456 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { |
430 // Verify that the database contains the expected number of items, which | 457 // Verify that the database contains the expected number of items, which |
431 // is the pre-filtered count, i.e. all of the items. | 458 // is the pre-filtered count, i.e. all of the items. |
432 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); | 459 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); |
433 ASSERT_TRUE(statement.is_valid()); | 460 ASSERT_TRUE(statement.is_valid()); |
434 uint64 row_count = 0; | 461 uint64 row_count = 0; |
435 while (statement.Step()) ++row_count; | 462 while (statement.Step()) ++row_count; |
436 EXPECT_EQ(1U, row_count); | 463 EXPECT_EQ(1U, row_count); |
437 url_index_.reset(new InMemoryURLIndex(&profile_, | 464 |
438 history_service_, | 465 InitializeInMemoryURLIndex(); |
439 base::FilePath(), | |
440 "en,ja,hi,zh", | |
441 history_service_->history_client())); | |
442 url_index_->Init(); | |
443 url_index_->RebuildFromHistory(history_database_); | |
444 URLIndexPrivateData& private_data(*GetPrivateData()); | 466 URLIndexPrivateData& private_data(*GetPrivateData()); |
445 | 467 |
446 // history_info_map_ should have the same number of items as were filtered. | 468 // history_info_map_ should have the same number of items as were filtered. |
447 EXPECT_EQ(1U, private_data.history_info_map_.size()); | 469 EXPECT_EQ(1U, private_data.history_info_map_.size()); |
448 EXPECT_EQ(35U, private_data.char_word_map_.size()); | 470 EXPECT_EQ(35U, private_data.char_word_map_.size()); |
449 EXPECT_EQ(17U, private_data.word_map_.size()); | 471 EXPECT_EQ(17U, private_data.word_map_.size()); |
450 } | 472 } |
451 | 473 |
452 #if defined(OS_WIN) | 474 #if defined(OS_WIN) |
453 // Flaky on windows trybots: http://crbug.com/351500 | 475 // Flaky on windows trybots: http://crbug.com/351500 |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1166 // Compare the captured and restored for equality. | 1188 // Compare the captured and restored for equality. |
1167 ExpectPrivateDataEqual(*old_data.get(), new_data); | 1189 ExpectPrivateDataEqual(*old_data.get(), new_data); |
1168 } | 1190 } |
1169 | 1191 |
1170 class InMemoryURLIndexCacheTest : public testing::Test { | 1192 class InMemoryURLIndexCacheTest : public testing::Test { |
1171 public: | 1193 public: |
1172 InMemoryURLIndexCacheTest() {} | 1194 InMemoryURLIndexCacheTest() {} |
1173 | 1195 |
1174 protected: | 1196 protected: |
1175 void SetUp() override; | 1197 void SetUp() override; |
| 1198 void TearDown() override; |
1176 | 1199 |
1177 // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 1200 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
1178 void set_history_dir(const base::FilePath& dir_path); | 1201 void set_history_dir(const base::FilePath& dir_path); |
1179 bool GetCacheFilePath(base::FilePath* file_path) const; | 1202 bool GetCacheFilePath(base::FilePath* file_path) const; |
1180 | 1203 |
1181 base::ScopedTempDir temp_dir_; | 1204 base::ScopedTempDir temp_dir_; |
1182 scoped_ptr<InMemoryURLIndex> url_index_; | 1205 scoped_ptr<InMemoryURLIndex> url_index_; |
1183 }; | 1206 }; |
1184 | 1207 |
1185 void InMemoryURLIndexCacheTest::SetUp() { | 1208 void InMemoryURLIndexCacheTest::SetUp() { |
1186 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 1209 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
1187 HistoryClient history_client; | |
1188 base::FilePath path(temp_dir_.path()); | 1210 base::FilePath path(temp_dir_.path()); |
1189 url_index_.reset(new InMemoryURLIndex( | 1211 url_index_.reset( |
1190 NULL, nullptr, path, "en,ja,hi,zh", &history_client)); | 1212 new InMemoryURLIndex(nullptr, path, kTestLanguages, nullptr)); |
| 1213 } |
| 1214 |
| 1215 void InMemoryURLIndexCacheTest::TearDown() { |
| 1216 if (url_index_) |
| 1217 url_index_->ShutDown(); |
1191 } | 1218 } |
1192 | 1219 |
1193 void InMemoryURLIndexCacheTest::set_history_dir( | 1220 void InMemoryURLIndexCacheTest::set_history_dir( |
1194 const base::FilePath& dir_path) { | 1221 const base::FilePath& dir_path) { |
1195 return url_index_->set_history_dir(dir_path); | 1222 return url_index_->set_history_dir(dir_path); |
1196 } | 1223 } |
1197 | 1224 |
1198 bool InMemoryURLIndexCacheTest::GetCacheFilePath( | 1225 bool InMemoryURLIndexCacheTest::GetCacheFilePath( |
1199 base::FilePath* file_path) const { | 1226 base::FilePath* file_path) const { |
1200 DCHECK(file_path); | 1227 DCHECK(file_path); |
(...skipping 11 matching lines...) Expand all Loading... |
1212 full_file_path.GetComponents(&actual_parts); | 1239 full_file_path.GetComponents(&actual_parts); |
1213 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1240 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
1214 size_t count = expected_parts.size(); | 1241 size_t count = expected_parts.size(); |
1215 for (size_t i = 0; i < count; ++i) | 1242 for (size_t i = 0; i < count; ++i) |
1216 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1243 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
1217 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1244 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
1218 set_history_dir(base::FilePath()); | 1245 set_history_dir(base::FilePath()); |
1219 } | 1246 } |
1220 | 1247 |
1221 } // namespace history | 1248 } // namespace history |
OLD | NEW |