| 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/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/scoped_temp_dir.h" |
| 12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 13 #include "base/string16.h" | 14 #include "base/string16.h" |
| 14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete.h" | 16 #include "chrome/browser/autocomplete/autocomplete.h" |
| 16 #include "chrome/browser/history/history.h" | 17 #include "chrome/browser/history/history.h" |
| 17 #include "chrome/browser/history/history_backend.h" | 18 #include "chrome/browser/history/history_backend.h" |
| 18 #include "chrome/browser/history/history_database.h" | 19 #include "chrome/browser/history/history_database.h" |
| 19 #include "chrome/browser/history/in_memory_url_index_types.h" | 20 #include "chrome/browser/history/in_memory_url_index_types.h" |
| 20 #include "chrome/browser/history/in_memory_url_index.h" | 21 #include "chrome/browser/history/in_memory_url_index.h" |
| 21 #include "chrome/browser/history/url_index_private_data.h" | 22 #include "chrome/browser/history/url_index_private_data.h" |
| 22 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 24 #include "content/test/test_browser_thread.h" | 25 #include "content/test/test_browser_thread.h" |
| 25 #include "sql/transaction.h" | 26 #include "sql/transaction.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 29 using content::BrowserThread; |
| 30 |
| 28 // The test version of the history url database table ('url') is contained in | 31 // The test version of the history url database table ('url') is contained in |
| 29 // a database file created from a text file('url_history_provider_test.db.txt'). | 32 // a database file created from a text file('url_history_provider_test.db.txt'). |
| 30 // The only difference between this table and a live 'urls' table from a | 33 // The only difference between this table and a live 'urls' table from a |
| 31 // profile is that the last_visit_time column in the test table contains a | 34 // profile is that the last_visit_time column in the test table contains a |
| 32 // number specifying the number of days relative to 'today' to which the | 35 // number specifying the number of days relative to 'today' to which the |
| 33 // absolute time should be set during the test setup stage. | 36 // absolute time should be set during the test setup stage. |
| 34 // | 37 // |
| 35 // The format of the test database text file is of a SQLite .dump file. | 38 // The format of the test database text file is of a SQLite .dump file. |
| 36 // Note that only lines whose first character is an upper-case letter are | 39 // Note that only lines whose first character is an upper-case letter are |
| 37 // processed when creating the test database. | 40 // processed when creating the test database. |
| 38 | 41 |
| 39 namespace history { | 42 namespace history { |
| 40 | 43 |
| 44 // ----------------------------------------------------------------------------- |
| 45 |
| 46 // Observer class so the unit tests can wait while the cache is being saved. |
| 47 class CacheFileSaverObserver : public InMemoryURLIndex::SaveCacheObserver { |
| 48 public: |
| 49 explicit CacheFileSaverObserver(MessageLoop* loop); |
| 50 virtual void OnCacheSaveFinished(bool succeeded) OVERRIDE; |
| 51 |
| 52 MessageLoop* loop_; |
| 53 bool succeeded_; |
| 54 DISALLOW_COPY_AND_ASSIGN(CacheFileSaverObserver); |
| 55 }; |
| 56 |
| 57 CacheFileSaverObserver::CacheFileSaverObserver(MessageLoop* loop) |
| 58 : loop_(loop), |
| 59 succeeded_(false) { |
| 60 DCHECK(loop); |
| 61 } |
| 62 |
| 63 void CacheFileSaverObserver::OnCacheSaveFinished(bool succeeded) { |
| 64 succeeded_ = succeeded; |
| 65 loop_->Quit(); |
| 66 } |
| 67 |
| 68 // Observer class so the unit tests can wait while the cache is being restored. |
| 69 class CacheFileReaderObserver : public InMemoryURLIndex::RestoreCacheObserver { |
| 70 public: |
| 71 explicit CacheFileReaderObserver(MessageLoop* loop); |
| 72 virtual void OnCacheRestoreFinished(bool succeeded) OVERRIDE; |
| 73 |
| 74 MessageLoop* loop_; |
| 75 bool succeeded_; |
| 76 DISALLOW_COPY_AND_ASSIGN(CacheFileReaderObserver); |
| 77 }; |
| 78 |
| 79 CacheFileReaderObserver::CacheFileReaderObserver(MessageLoop* loop) |
| 80 : loop_(loop), |
| 81 succeeded_(false) { |
| 82 DCHECK(loop); |
| 83 } |
| 84 |
| 85 void CacheFileReaderObserver::OnCacheRestoreFinished(bool succeeded) { |
| 86 succeeded_ = succeeded; |
| 87 loop_->Quit(); |
| 88 } |
| 89 |
| 90 // ----------------------------------------------------------------------------- |
| 91 |
| 41 class InMemoryURLIndexTest : public testing::Test { | 92 class InMemoryURLIndexTest : public testing::Test { |
| 42 public: | 93 public: |
| 43 InMemoryURLIndexTest(); | 94 InMemoryURLIndexTest(); |
| 44 | 95 |
| 45 protected: | 96 protected: |
| 46 // Test setup. | 97 // Test setup. |
| 47 virtual void SetUp(); | 98 virtual void SetUp(); |
| 48 | 99 |
| 49 // Allows the database containing the test data to be customized by | 100 // Allows the database containing the test data to be customized by |
| 50 // subclasses. | 101 // subclasses. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 66 // Validates that the given |term| is contained in |cache| and that it is | 117 // Validates that the given |term| is contained in |cache| and that it is |
| 67 // marked as in-use. | 118 // marked as in-use. |
| 68 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, | 119 void CheckTerm(const URLIndexPrivateData::SearchTermCacheMap& cache, |
| 69 string16 term) const; | 120 string16 term) const; |
| 70 | 121 |
| 71 // Pass-through function to simplify our friendship with HistoryService. | 122 // Pass-through function to simplify our friendship with HistoryService. |
| 72 sql::Connection& GetDB(); | 123 sql::Connection& GetDB(); |
| 73 | 124 |
| 74 // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 125 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
| 75 URLIndexPrivateData* GetPrivateData() const; | 126 URLIndexPrivateData* GetPrivateData() const; |
| 127 void ClearPrivateData(); |
| 128 void set_history_dir(const FilePath& dir_path); |
| 76 bool GetCacheFilePath(FilePath* file_path) const; | 129 bool GetCacheFilePath(FilePath* file_path) const; |
| 77 void ClearHistoryDir() const; | 130 void PostRestoreFromCacheFileTask(); |
| 131 void PostSaveToCacheFileTask(); |
| 132 const std::set<std::string>& scheme_whitelist(); |
| 133 |
| 78 | 134 |
| 79 // Pass-through functions to simplify our friendship with URLIndexPrivateData. | 135 // Pass-through functions to simplify our friendship with URLIndexPrivateData. |
| 80 bool UpdateURL(const URLRow& row); | 136 bool UpdateURL(const URLRow& row); |
| 81 bool DeleteURL(const GURL& url); | 137 bool DeleteURL(const GURL& url); |
| 82 | 138 |
| 83 // Data verification helper functions. | 139 // Data verification helper functions. |
| 84 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); | 140 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); |
| 85 void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); | 141 void ExpectPrivateDataEmpty(const URLIndexPrivateData& data); |
| 86 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, | 142 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, |
| 87 const URLIndexPrivateData& actual); | 143 const URLIndexPrivateData& actual); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 98 InMemoryURLIndexTest::InMemoryURLIndexTest() | 154 InMemoryURLIndexTest::InMemoryURLIndexTest() |
| 99 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 155 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 100 file_thread_(content::BrowserThread::FILE, &message_loop_) { | 156 file_thread_(content::BrowserThread::FILE, &message_loop_) { |
| 101 } | 157 } |
| 102 | 158 |
| 103 sql::Connection& InMemoryURLIndexTest::GetDB() { | 159 sql::Connection& InMemoryURLIndexTest::GetDB() { |
| 104 return history_database_->GetDB(); | 160 return history_database_->GetDB(); |
| 105 } | 161 } |
| 106 | 162 |
| 107 URLIndexPrivateData* InMemoryURLIndexTest::GetPrivateData() const { | 163 URLIndexPrivateData* InMemoryURLIndexTest::GetPrivateData() const { |
| 108 DCHECK(url_index_->private_data_.get()); | 164 DCHECK(url_index_->private_data()); |
| 109 return url_index_->private_data_.get(); | 165 return url_index_->private_data(); |
| 166 } |
| 167 |
| 168 void InMemoryURLIndexTest::ClearPrivateData() { |
| 169 return url_index_->ClearPrivateData(); |
| 170 } |
| 171 |
| 172 void InMemoryURLIndexTest::set_history_dir(const FilePath& dir_path) { |
| 173 return url_index_->set_history_dir(dir_path); |
| 110 } | 174 } |
| 111 | 175 |
| 112 bool InMemoryURLIndexTest::GetCacheFilePath(FilePath* file_path) const { | 176 bool InMemoryURLIndexTest::GetCacheFilePath(FilePath* file_path) const { |
| 113 DCHECK(file_path); | 177 DCHECK(file_path); |
| 114 return url_index_->GetCacheFilePath(file_path); | 178 return url_index_->GetCacheFilePath(file_path); |
| 115 } | 179 } |
| 116 | 180 |
| 117 void InMemoryURLIndexTest::ClearHistoryDir() const { | 181 void InMemoryURLIndexTest::PostRestoreFromCacheFileTask() { |
| 118 url_index_->history_dir_.clear(); | 182 url_index_->PostRestoreFromCacheFileTask(); |
| 183 } |
| 184 |
| 185 void InMemoryURLIndexTest::PostSaveToCacheFileTask() { |
| 186 url_index_->PostSaveToCacheFileTask(); |
| 187 } |
| 188 |
| 189 const std::set<std::string>& InMemoryURLIndexTest::scheme_whitelist() { |
| 190 return url_index_->scheme_whitelist(); |
| 119 } | 191 } |
| 120 | 192 |
| 121 bool InMemoryURLIndexTest::UpdateURL(const URLRow& row) { | 193 bool InMemoryURLIndexTest::UpdateURL(const URLRow& row) { |
| 122 return url_index_->private_data_->UpdateURL(row); | 194 return GetPrivateData()->UpdateURL(row, url_index_->languages_, |
| 195 url_index_->scheme_whitelist_); |
| 123 } | 196 } |
| 124 | 197 |
| 125 bool InMemoryURLIndexTest::DeleteURL(const GURL& url) { | 198 bool InMemoryURLIndexTest::DeleteURL(const GURL& url) { |
| 126 return url_index_->private_data_->DeleteURL(url); | 199 return GetPrivateData()->DeleteURL(url); |
| 127 } | 200 } |
| 128 | 201 |
| 129 void InMemoryURLIndexTest::SetUp() { | 202 void InMemoryURLIndexTest::SetUp() { |
| 130 // We cannot access the database until the backend has been loaded. | 203 // We cannot access the database until the backend has been loaded. |
| 131 profile_.CreateHistoryService(true, false); | 204 profile_.CreateHistoryService(true, false); |
| 132 profile_.CreateBookmarkModel(true); | 205 profile_.CreateBookmarkModel(true); |
| 133 profile_.BlockUntilBookmarkModelLoaded(); | 206 profile_.BlockUntilBookmarkModelLoaded(); |
| 134 profile_.BlockUntilHistoryProcessesPendingRequests(); | 207 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 135 HistoryService* history_service = | 208 HistoryService* history_service = |
| 136 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 209 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 FilePath::StringType TestDBName() const; | 425 FilePath::StringType TestDBName() const; |
| 353 }; | 426 }; |
| 354 | 427 |
| 355 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { | 428 FilePath::StringType LimitedInMemoryURLIndexTest::TestDBName() const { |
| 356 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); | 429 return FILE_PATH_LITERAL("url_history_provider_test_limited.db.txt"); |
| 357 } | 430 } |
| 358 | 431 |
| 359 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { | 432 TEST_F(LimitedInMemoryURLIndexTest, Initialization) { |
| 360 // Verify that the database contains the expected number of items, which | 433 // Verify that the database contains the expected number of items, which |
| 361 // is the pre-filtered count, i.e. all of the items. | 434 // is the pre-filtered count, i.e. all of the items. |
| 362 sql::Connection& db(GetDB()); | 435 sql::Statement statement(GetDB().GetUniqueStatement("SELECT * FROM urls;")); |
| 363 sql::Statement statement(db.GetUniqueStatement("SELECT * FROM urls;")); | |
| 364 ASSERT_TRUE(statement.is_valid()); | 436 ASSERT_TRUE(statement.is_valid()); |
| 365 uint64 row_count = 0; | 437 uint64 row_count = 0; |
| 366 while (statement.Step()) ++row_count; | 438 while (statement.Step()) ++row_count; |
| 367 EXPECT_EQ(1U, row_count); | 439 EXPECT_EQ(1U, row_count); |
| 368 url_index_.reset( | 440 url_index_.reset( |
| 369 new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh")); | 441 new InMemoryURLIndex(&profile_, FilePath(), "en,ja,hi,zh")); |
| 370 url_index_->Init(); | 442 url_index_->Init(); |
| 371 url_index_->RebuildFromHistory(history_database_); | 443 url_index_->RebuildFromHistory(history_database_); |
| 372 URLIndexPrivateData& private_data(*(url_index_->private_data_)); | 444 URLIndexPrivateData& private_data(*GetPrivateData()); |
| 373 | 445 |
| 374 // history_info_map_ should have the same number of items as were filtered. | 446 // history_info_map_ should have the same number of items as were filtered. |
| 375 EXPECT_EQ(1U, private_data.history_info_map_.size()); | 447 EXPECT_EQ(1U, private_data.history_info_map_.size()); |
| 376 EXPECT_EQ(35U, private_data.char_word_map_.size()); | 448 EXPECT_EQ(35U, private_data.char_word_map_.size()); |
| 377 EXPECT_EQ(17U, private_data.word_map_.size()); | 449 EXPECT_EQ(17U, private_data.word_map_.size()); |
| 378 } | 450 } |
| 379 | 451 |
| 380 TEST_F(InMemoryURLIndexTest, Retrieval) { | 452 TEST_F(InMemoryURLIndexTest, Retrieval) { |
| 381 // See if a very specific term gives a single result. | 453 // See if a very specific term gives a single result. |
| 382 ScoredHistoryMatches matches = | 454 ScoredHistoryMatches matches = |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 { "tip://123.123.123.123/?urn:xopen:xid", false }, | 903 { "tip://123.123.123.123/?urn:xopen:xid", false }, |
| 832 { "tv:nbc.com", false }, | 904 { "tv:nbc.com", false }, |
| 833 { "urn:foo:A123,456", false }, | 905 { "urn:foo:A123,456", false }, |
| 834 { "vemmi://zeus.mctel.fr/demo", false }, | 906 { "vemmi://zeus.mctel.fr/demo", false }, |
| 835 { "wais://www.mydomain.net:8765/mydatabase", false }, | 907 { "wais://www.mydomain.net:8765/mydatabase", false }, |
| 836 { "xmpp:node@example.com", false }, | 908 { "xmpp:node@example.com", false }, |
| 837 { "xmpp://guest@example.com", false }, | 909 { "xmpp://guest@example.com", false }, |
| 838 }; | 910 }; |
| 839 | 911 |
| 840 URLIndexPrivateData& private_data(*GetPrivateData()); | 912 URLIndexPrivateData& private_data(*GetPrivateData()); |
| 913 const std::set<std::string>& whitelist(scheme_whitelist()); |
| 841 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 914 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 842 GURL url(data[i].url_spec); | 915 GURL url(data[i].url_spec); |
| 843 EXPECT_EQ(data[i].expected_is_whitelisted, | 916 EXPECT_EQ(data[i].expected_is_whitelisted, |
| 844 private_data.URLSchemeIsWhitelisted(url)); | 917 private_data.URLSchemeIsWhitelisted(url, whitelist)); |
| 845 } | 918 } |
| 846 } | 919 } |
| 847 | 920 |
| 848 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { | 921 TEST_F(InMemoryURLIndexTest, CacheSaveRestore) { |
| 849 // Part 1: Save the cache to a protobuf, restore it, and compare the results. | 922 ScopedTempDir temp_directory; |
| 850 in_memory_url_index::InMemoryURLIndexCacheItem index_cache; | 923 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 851 URLIndexPrivateData& expected(*GetPrivateData()); | 924 set_history_dir(temp_directory.path()); |
| 852 | 925 |
| 853 // Capture our private data so we can later compare for equality. | 926 URLIndexPrivateData& private_data(*GetPrivateData()); |
| 854 URLIndexPrivateData actual(expected); | |
| 855 | 927 |
| 856 actual.SavePrivateData(&index_cache); | 928 // Ensure that there is really something there to be saved. |
| 929 EXPECT_FALSE(private_data.word_list_.empty()); |
| 930 // available_words_ will already be empty since we have freshly built the |
| 931 // data set for this test. |
| 932 EXPECT_TRUE(private_data.available_words_.empty()); |
| 933 EXPECT_FALSE(private_data.word_map_.empty()); |
| 934 EXPECT_FALSE(private_data.char_word_map_.empty()); |
| 935 EXPECT_FALSE(private_data.word_id_history_map_.empty()); |
| 936 EXPECT_FALSE(private_data.history_id_word_map_.empty()); |
| 937 EXPECT_FALSE(private_data.history_info_map_.empty()); |
| 857 | 938 |
| 858 // Version check: Make sure this version actually has the word starts. | 939 // Capture the current private data for later comparison to restored data. |
| 859 EXPECT_TRUE(index_cache.has_word_starts_map()); | 940 scoped_refptr<URLIndexPrivateData> old_data(private_data.Duplicate()); |
| 860 | 941 |
| 861 // Save the size of the resulting cache for later versioning comparison. | 942 // Save then restore our private data. |
| 862 std::string data; | 943 CacheFileSaverObserver save_observer(&message_loop_); |
| 863 EXPECT_TRUE(index_cache.SerializeToString(&data)); | 944 url_index_->set_save_cache_observer(&save_observer); |
| 864 size_t current_version_cache_size = data.size(); | 945 PostSaveToCacheFileTask(); |
| 946 message_loop_.Run(); |
| 947 EXPECT_TRUE(save_observer.succeeded_); |
| 865 | 948 |
| 866 // Prove that there is really something there. | 949 // Clear and then prove it's clear before restoring. |
| 867 ExpectPrivateDataNotEmpty(actual); | 950 ClearPrivateData(); |
| 951 EXPECT_TRUE(private_data.word_list_.empty()); |
| 952 EXPECT_TRUE(private_data.available_words_.empty()); |
| 953 EXPECT_TRUE(private_data.word_map_.empty()); |
| 954 EXPECT_TRUE(private_data.char_word_map_.empty()); |
| 955 EXPECT_TRUE(private_data.word_id_history_map_.empty()); |
| 956 EXPECT_TRUE(private_data.history_id_word_map_.empty()); |
| 957 EXPECT_TRUE(private_data.history_info_map_.empty()); |
| 868 | 958 |
| 869 // Clear and then prove it's clear. | 959 CacheFileReaderObserver read_observer(&message_loop_); |
| 870 actual.Clear(); | 960 url_index_->set_restore_cache_observer(&read_observer); |
| 871 ExpectPrivateDataEmpty(actual); | 961 PostRestoreFromCacheFileTask(); |
| 962 message_loop_.Run(); |
| 963 EXPECT_TRUE(read_observer.succeeded_); |
| 872 | 964 |
| 873 // Restore the cache. | 965 URLIndexPrivateData& new_data(*GetPrivateData()); |
| 874 EXPECT_TRUE(actual.RestorePrivateData(index_cache)); | |
| 875 EXPECT_EQ(kCurrentCacheFileVersion, actual.restored_cache_version_); | |
| 876 | 966 |
| 877 // Compare the restored and expected for equality. | 967 // Compare the captured and restored for equality. |
| 878 ExpectPrivateDataEqual(expected, actual); | 968 EXPECT_EQ(old_data->word_list_.size(), new_data.word_list_.size()); |
| 969 EXPECT_EQ(old_data->word_map_.size(), new_data.word_map_.size()); |
| 970 EXPECT_EQ(old_data->char_word_map_.size(), new_data.char_word_map_.size()); |
| 971 EXPECT_EQ(old_data->word_id_history_map_.size(), |
| 972 new_data.word_id_history_map_.size()); |
| 973 EXPECT_EQ(old_data->history_id_word_map_.size(), |
| 974 new_data.history_id_word_map_.size()); |
| 975 EXPECT_EQ(old_data->history_info_map_.size(), |
| 976 new_data.history_info_map_.size()); |
| 977 // WordList must be index-by-index equal. |
| 978 size_t count = old_data->word_list_.size(); |
| 979 for (size_t i = 0; i < count; ++i) |
| 980 EXPECT_EQ(old_data->word_list_[i], new_data.word_list_[i]); |
| 879 | 981 |
| 880 // Part 2: Save an older version of the cache, restore it, and verify that the | 982 ExpectMapOfContainersIdentical(old_data->char_word_map_, |
| 881 // reversioned portions are as expected. | 983 new_data.char_word_map_); |
| 882 URLIndexPrivateData older(expected); | 984 ExpectMapOfContainersIdentical(old_data->word_id_history_map_, |
| 883 in_memory_url_index::InMemoryURLIndexCacheItem older_cache; | 985 new_data.word_id_history_map_); |
| 884 older.set_saved_cache_version(0); | 986 ExpectMapOfContainersIdentical(old_data->history_id_word_map_, |
| 885 older.SavePrivateData(&older_cache); | 987 new_data.history_id_word_map_); |
| 886 | 988 |
| 887 // Version check: Make sure this version does not have the word starts. | 989 for (HistoryInfoMap::const_iterator expected = |
| 888 EXPECT_FALSE(older_cache.has_word_starts_map()); | 990 old_data->history_info_map_.begin(); |
| 889 | 991 expected != old_data->history_info_map_.end(); ++expected) { |
| 890 // Since we shouldn't have saved the word starts information for the version | 992 HistoryInfoMap::const_iterator actual = |
| 891 // 0 save immediately above, the cache should be a bit smaller. | 993 new_data.history_info_map_.find(expected->first); |
| 892 std::string older_data; | 994 ASSERT_FALSE(new_data.history_info_map_.end() == actual); |
| 893 EXPECT_TRUE(older_cache.SerializeToString(&older_data)); | 995 const URLRow& expected_row(expected->second); |
| 894 size_t old_version_file_size = older_data.size(); | 996 const URLRow& actual_row(actual->second); |
| 895 EXPECT_LT(old_version_file_size, current_version_cache_size); | 997 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); |
| 896 EXPECT_NE(data, older_data); | 998 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); |
| 897 | 999 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); |
| 898 // Clear and then prove it's clear. | 1000 EXPECT_EQ(expected_row.url(), actual_row.url()); |
| 899 older.Clear(); | 1001 } |
| 900 ExpectPrivateDataEmpty(older); | |
| 901 | |
| 902 // Restore the cache. | |
| 903 EXPECT_TRUE(older.RestorePrivateData(older_cache)); | |
| 904 EXPECT_EQ(0, older.restored_cache_version_); | |
| 905 | |
| 906 // Compare the restored and expected for equality. | |
| 907 ExpectPrivateDataEqual(expected, older); | |
| 908 } | 1002 } |
| 909 | 1003 |
| 910 class InMemoryURLIndexCacheTest : public testing::Test { | 1004 class InMemoryURLIndexCacheTest : public testing::Test { |
| 911 public: | 1005 public: |
| 912 InMemoryURLIndexCacheTest() {} | 1006 InMemoryURLIndexCacheTest() {} |
| 913 | 1007 |
| 914 protected: | 1008 protected: |
| 915 virtual void SetUp() OVERRIDE; | 1009 virtual void SetUp() OVERRIDE; |
| 916 | 1010 |
| 1011 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
| 1012 void set_history_dir(const FilePath& dir_path); |
| 1013 bool GetCacheFilePath(FilePath* file_path) const; |
| 1014 |
| 917 ScopedTempDir temp_dir_; | 1015 ScopedTempDir temp_dir_; |
| 918 scoped_ptr<InMemoryURLIndex> url_index_; | 1016 scoped_ptr<InMemoryURLIndex> url_index_; |
| 919 }; | 1017 }; |
| 920 | 1018 |
| 921 void InMemoryURLIndexCacheTest::SetUp() { | 1019 void InMemoryURLIndexCacheTest::SetUp() { |
| 922 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 1020 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 923 FilePath path(temp_dir_.path()); | 1021 FilePath path(temp_dir_.path()); |
| 924 url_index_.reset( | 1022 url_index_.reset( |
| 925 new InMemoryURLIndex(NULL, path, "en,ja,hi,zh")); | 1023 new InMemoryURLIndex(NULL, path, "en,ja,hi,zh")); |
| 926 } | 1024 } |
| 927 | 1025 |
| 1026 void InMemoryURLIndexCacheTest::set_history_dir(const FilePath& dir_path) { |
| 1027 return url_index_->set_history_dir(dir_path); |
| 1028 } |
| 1029 |
| 1030 bool InMemoryURLIndexCacheTest::GetCacheFilePath(FilePath* file_path) const { |
| 1031 DCHECK(file_path); |
| 1032 return url_index_->GetCacheFilePath(file_path); |
| 1033 } |
| 1034 |
| 928 TEST_F(InMemoryURLIndexCacheTest, CacheFilePath) { | 1035 TEST_F(InMemoryURLIndexCacheTest, CacheFilePath) { |
| 929 FilePath expectedPath = | 1036 FilePath expectedPath = |
| 930 temp_dir_.path().Append(FILE_PATH_LITERAL("History Provider Cache")); | 1037 temp_dir_.path().Append(FILE_PATH_LITERAL("History Provider Cache")); |
| 931 std::vector<FilePath::StringType> expected_parts; | 1038 std::vector<FilePath::StringType> expected_parts; |
| 932 expectedPath.GetComponents(&expected_parts); | 1039 expectedPath.GetComponents(&expected_parts); |
| 933 FilePath full_file_path; | 1040 FilePath full_file_path; |
| 934 ASSERT_TRUE(url_index_->GetCacheFilePath(&full_file_path)); | 1041 ASSERT_TRUE(GetCacheFilePath(&full_file_path)); |
| 935 std::vector<FilePath::StringType> actual_parts; | 1042 std::vector<FilePath::StringType> actual_parts; |
| 936 full_file_path.GetComponents(&actual_parts); | 1043 full_file_path.GetComponents(&actual_parts); |
| 937 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1044 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
| 938 size_t count = expected_parts.size(); | 1045 size_t count = expected_parts.size(); |
| 939 for (size_t i = 0; i < count; ++i) | 1046 for (size_t i = 0; i < count; ++i) |
| 940 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1047 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
| 941 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1048 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
| 942 url_index_->history_dir_.clear(); | 1049 set_history_dir(FilePath()); |
| 943 } | 1050 } |
| 944 | 1051 |
| 945 } // namespace history | 1052 } // namespace history |
| OLD | NEW |