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/i18n/case_conversion.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "chrome/browser/autocomplete/in_memory_url_index.h" | 19 #include "chrome/browser/autocomplete/in_memory_url_index.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" |
| 22 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/test/base/history_index_restore_observer.h" | 24 #include "chrome/test/base/history_index_restore_observer.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 25 #include "components/bookmarks/test/bookmark_test_helpers.h" | 26 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 26 #include "components/history/core/browser/history_backend.h" | 27 #include "components/history/core/browser/history_backend.h" |
| 27 #include "components/history/core/browser/history_database.h" | 28 #include "components/history/core/browser/history_database.h" |
| 28 #include "components/history/core/browser/history_service.h" | 29 #include "components/history/core/browser/history_service.h" |
| 29 #include "components/omnibox/in_memory_url_index_types.h" | 30 #include "components/omnibox/in_memory_url_index_types.h" |
| 31 #include "content/public/browser/browser_thread.h" | |
| 30 #include "content/public/test/test_browser_thread_bundle.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
| 31 #include "sql/transaction.h" | 33 #include "sql/transaction.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 35 |
| 34 using base::ASCIIToUTF16; | 36 using base::ASCIIToUTF16; |
| 35 | 37 |
| 36 // The test version of the history url database table ('url') is contained in | 38 // The test version of the history url database table ('url') is contained in |
| 37 // a database file created from a text file('url_history_provider_test.db.txt'). | 39 // a database file created from a text file('url_history_provider_test.db.txt'). |
| 38 // The only difference between this table and a live 'urls' table from a | 40 // The only difference between this table and a live 'urls' table from a |
| 39 // profile is that the last_visit_time column in the test table contains a | 41 // profile is that the last_visit_time column in the test table contains a |
| 40 // number specifying the number of days relative to 'today' to which the | 42 // number specifying the number of days relative to 'today' to which the |
| 41 // absolute time should be set during the test setup stage. | 43 // absolute time should be set during the test setup stage. |
| 42 // | 44 // |
| 43 // The format of the test database text file is of a SQLite .dump file. | 45 // The format of the test database text file is of a SQLite .dump file. |
| 44 // Note that only lines whose first character is an upper-case letter are | 46 // Note that only lines whose first character is an upper-case letter are |
| 45 // processed when creating the test database. | 47 // processed when creating the test database. |
| 46 | 48 |
| 47 namespace { | 49 namespace { |
| 48 const size_t kInvalid = base::string16::npos; | 50 const size_t kInvalid = base::string16::npos; |
| 49 const size_t kMaxMatches = 3; | 51 const size_t kMaxMatches = 3; |
| 50 const char kTestLanguages[] = "en,ja,hi,zh"; | 52 const char kTestLanguages[] = "en,ja,hi,zh"; |
| 53 const char kClientWhitelistedScheme[] = "chrome"; | |
|
Peter Kasting
2015/06/16 22:18:07
Nit: Let's use "xyz" here or something to make it
blundell
2015/06/17 07:55:44
Done.
| |
| 51 | 54 |
| 52 // Helper function to set lower case |lower_string| and |lower_terms| (words | 55 // Helper function to set lower case |lower_string| and |lower_terms| (words |
| 53 // list) based on supplied |search_string| and |cursor_position|. If | 56 // 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 | 57 // |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 | 58 // 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 | 59 // pretending there's a space where the cursor is. |lower_terms| are obtained by |
| 57 // splitting the |lower_string| on whitespace into tokens. | 60 // splitting the |lower_string| on whitespace into tokens. |
| 58 void StringToTerms(const char* search_string, | 61 void StringToTerms(const char* search_string, |
| 59 size_t cursor_position, | 62 size_t cursor_position, |
| 60 base::string16* lower_string, | 63 base::string16* lower_string, |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 base::FilePath::StringType InMemoryURLIndexTest::TestDBName() const { | 317 base::FilePath::StringType InMemoryURLIndexTest::TestDBName() const { |
| 315 return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); | 318 return FILE_PATH_LITERAL("url_history_provider_test.db.txt"); |
| 316 } | 319 } |
| 317 | 320 |
| 318 bool InMemoryURLIndexTest::InitializeInMemoryURLIndexInSetUp() const { | 321 bool InMemoryURLIndexTest::InitializeInMemoryURLIndexInSetUp() const { |
| 319 return true; | 322 return true; |
| 320 } | 323 } |
| 321 | 324 |
| 322 void InMemoryURLIndexTest::InitializeInMemoryURLIndex() { | 325 void InMemoryURLIndexTest::InitializeInMemoryURLIndex() { |
| 323 DCHECK(!url_index_); | 326 DCHECK(!url_index_); |
| 324 url_index_.reset(new InMemoryURLIndex(nullptr, history_service_, | 327 |
| 325 base::FilePath(), kTestLanguages)); | 328 std::set<std::string> client_schemes_to_whitelist; |
| 329 client_schemes_to_whitelist.insert(kClientWhitelistedScheme); | |
| 330 url_index_.reset(new InMemoryURLIndex( | |
| 331 nullptr, history_service_, content::BrowserThread::GetBlockingPool(), | |
| 332 base::FilePath(), kTestLanguages, client_schemes_to_whitelist)); | |
| 326 url_index_->Init(); | 333 url_index_->Init(); |
| 327 url_index_->RebuildFromHistory(history_database_); | 334 url_index_->RebuildFromHistory(history_database_); |
| 328 } | 335 } |
| 329 | 336 |
| 330 void InMemoryURLIndexTest::CheckTerm( | 337 void InMemoryURLIndexTest::CheckTerm( |
| 331 const URLIndexPrivateData::SearchTermCacheMap& cache, | 338 const URLIndexPrivateData::SearchTermCacheMap& cache, |
| 332 base::string16 term) const { | 339 base::string16 term) const { |
| 333 URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( | 340 URLIndexPrivateData::SearchTermCacheMap::const_iterator cache_iter( |
| 334 cache.find(term)); | 341 cache.find(term)); |
| 335 ASSERT_TRUE(cache.end() != cache_iter) | 342 ASSERT_TRUE(cache.end() != cache_iter) |
| (...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 935 history::URLRows deleted_rows; | 942 history::URLRows deleted_rows; |
| 936 deleted_rows.push_back(matches[0].url_info); | 943 deleted_rows.push_back(matches[0].url_info); |
| 937 url_index_->OnURLsDeleted(nullptr, false, false, deleted_rows, | 944 url_index_->OnURLsDeleted(nullptr, false, false, deleted_rows, |
| 938 std::set<GURL>()); | 945 std::set<GURL>()); |
| 939 EXPECT_TRUE(url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"), | 946 EXPECT_TRUE(url_index_->HistoryItemsForTerms(ASCIIToUTF16("DrudgeReport"), |
| 940 base::string16::npos, | 947 base::string16::npos, |
| 941 kMaxMatches).empty()); | 948 kMaxMatches).empty()); |
| 942 } | 949 } |
| 943 | 950 |
| 944 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { | 951 TEST_F(InMemoryURLIndexTest, WhitelistedURLs) { |
| 952 std::string client_whitelisted_url = | |
| 953 base::StringPrintf("%s://foo", kClientWhitelistedScheme); | |
| 945 struct TestData { | 954 struct TestData { |
| 946 const std::string url_spec; | 955 const std::string url_spec; |
| 947 const bool expected_is_whitelisted; | 956 const bool expected_is_whitelisted; |
| 948 } data[] = { | 957 } data[] = { |
| 949 // URLs with whitelisted schemes. | 958 // URLs with whitelisted schemes. |
| 950 { "about:histograms", true }, | 959 { "about:histograms", true }, |
| 951 { "chrome://settings", true }, | |
| 952 { "file://localhost/Users/joeschmoe/sekrets", true }, | 960 { "file://localhost/Users/joeschmoe/sekrets", true }, |
| 953 { "ftp://public.mycompany.com/myfile.txt", true }, | 961 { "ftp://public.mycompany.com/myfile.txt", true }, |
| 954 { "http://www.google.com/translate", true }, | 962 { "http://www.google.com/translate", true }, |
| 955 { "https://www.gmail.com/", true }, | 963 { "https://www.gmail.com/", true }, |
| 956 { "mailto:support@google.com", true }, | 964 { "mailto:support@google.com", true }, |
| 965 { client_whitelisted_url, true }, | |
| 957 // URLs with unacceptable schemes. | 966 // URLs with unacceptable schemes. |
| 958 { "aaa://www.dummyhost.com;frammy", false }, | 967 { "aaa://www.dummyhost.com;frammy", false }, |
| 959 { "aaas://www.dummyhost.com;frammy", false }, | 968 { "aaas://www.dummyhost.com;frammy", false }, |
| 960 { "acap://suzie@somebody.com", false }, | 969 { "acap://suzie@somebody.com", false }, |
| 961 { "cap://cal.example.com/Company/Holidays", false }, | 970 { "cap://cal.example.com/Company/Holidays", false }, |
| 962 { "cid:foo4*foo1@bar.net", false }, | 971 { "cid:foo4*foo1@bar.net", false }, |
| 963 { "crid://example.com/foobar", false }, | 972 { "crid://example.com/foobar", false }, |
| 964 { "data:image/png;base64,iVBORw0KGgoAAAANSUhE=", false }, | 973 { "data:image/png;base64,iVBORw0KGgoAAAANSUhE=", false }, |
| 965 { "dict://dict.org/d:shortcake:", false }, | 974 { "dict://dict.org/d:shortcake:", false }, |
| 966 { "dns://192.168.1.1/ftp.example.org?type=A", false }, | 975 { "dns://192.168.1.1/ftp.example.org?type=A", false }, |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1307 bool GetCacheFilePath(base::FilePath* file_path) const; | 1316 bool GetCacheFilePath(base::FilePath* file_path) const; |
| 1308 | 1317 |
| 1309 content::TestBrowserThreadBundle thread_bundle_; | 1318 content::TestBrowserThreadBundle thread_bundle_; |
| 1310 base::ScopedTempDir temp_dir_; | 1319 base::ScopedTempDir temp_dir_; |
| 1311 scoped_ptr<InMemoryURLIndex> url_index_; | 1320 scoped_ptr<InMemoryURLIndex> url_index_; |
| 1312 }; | 1321 }; |
| 1313 | 1322 |
| 1314 void InMemoryURLIndexCacheTest::SetUp() { | 1323 void InMemoryURLIndexCacheTest::SetUp() { |
| 1315 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 1324 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 1316 base::FilePath path(temp_dir_.path()); | 1325 base::FilePath path(temp_dir_.path()); |
| 1317 url_index_.reset( | 1326 url_index_.reset(new InMemoryURLIndex( |
| 1318 new InMemoryURLIndex(nullptr, nullptr, path, kTestLanguages)); | 1327 nullptr, nullptr, content::BrowserThread::GetBlockingPool(), path, |
| 1328 kTestLanguages, std::set<std::string>())); | |
| 1319 } | 1329 } |
| 1320 | 1330 |
| 1321 void InMemoryURLIndexCacheTest::TearDown() { | 1331 void InMemoryURLIndexCacheTest::TearDown() { |
| 1322 if (url_index_) | 1332 if (url_index_) |
| 1323 url_index_->Shutdown(); | 1333 url_index_->Shutdown(); |
| 1324 } | 1334 } |
| 1325 | 1335 |
| 1326 void InMemoryURLIndexCacheTest::set_history_dir( | 1336 void InMemoryURLIndexCacheTest::set_history_dir( |
| 1327 const base::FilePath& dir_path) { | 1337 const base::FilePath& dir_path) { |
| 1328 return url_index_->set_history_dir(dir_path); | 1338 return url_index_->set_history_dir(dir_path); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1343 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); | 1353 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); |
| 1344 std::vector<base::FilePath::StringType> actual_parts; | 1354 std::vector<base::FilePath::StringType> actual_parts; |
| 1345 full_file_path.GetComponents(&actual_parts); | 1355 full_file_path.GetComponents(&actual_parts); |
| 1346 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1356 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
| 1347 size_t count = expected_parts.size(); | 1357 size_t count = expected_parts.size(); |
| 1348 for (size_t i = 0; i < count; ++i) | 1358 for (size_t i = 0; i < count; ++i) |
| 1349 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1359 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
| 1350 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1360 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
| 1351 set_history_dir(base::FilePath()); | 1361 set_history_dir(base::FilePath()); |
| 1352 } | 1362 } |
| OLD | NEW |