| 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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1011 EXPECT_FALSE(private_data.history_id_word_map_.empty()); | 1011 EXPECT_FALSE(private_data.history_id_word_map_.empty()); |
| 1012 EXPECT_FALSE(private_data.history_info_map_.empty()); | 1012 EXPECT_FALSE(private_data.history_info_map_.empty()); |
| 1013 EXPECT_FALSE(private_data.word_starts_map_.empty()); | 1013 EXPECT_FALSE(private_data.word_starts_map_.empty()); |
| 1014 | 1014 |
| 1015 // Make sure the data we have was built from history. (Version 0 | 1015 // Make sure the data we have was built from history. (Version 0 |
| 1016 // means rebuilt from history.) | 1016 // means rebuilt from history.) |
| 1017 EXPECT_EQ(0, private_data.restored_cache_version_); | 1017 EXPECT_EQ(0, private_data.restored_cache_version_); |
| 1018 | 1018 |
| 1019 // Capture the current private data for later comparison to restored data. | 1019 // Capture the current private data for later comparison to restored data. |
| 1020 scoped_refptr<URLIndexPrivateData> old_data(private_data.Duplicate()); | 1020 scoped_refptr<URLIndexPrivateData> old_data(private_data.Duplicate()); |
| 1021 const base::Time rebuild_time = private_data.last_time_rebuilt_from_history_; |
| 1021 | 1022 |
| 1022 // Save then restore our private data. | 1023 // Save then restore our private data. |
| 1023 CacheFileSaverObserver save_observer(&message_loop_); | 1024 CacheFileSaverObserver save_observer(&message_loop_); |
| 1024 url_index_->set_save_cache_observer(&save_observer); | 1025 url_index_->set_save_cache_observer(&save_observer); |
| 1025 PostSaveToCacheFileTask(); | 1026 PostSaveToCacheFileTask(); |
| 1026 message_loop_.Run(); | 1027 message_loop_.Run(); |
| 1027 EXPECT_TRUE(save_observer.succeeded_); | 1028 EXPECT_TRUE(save_observer.succeeded_); |
| 1028 | 1029 |
| 1029 // Clear and then prove it's clear before restoring. | 1030 // Clear and then prove it's clear before restoring. |
| 1030 ClearPrivateData(); | 1031 ClearPrivateData(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1041 base::Bind(&base::MessageLoop::Quit, base::Unretained(&message_loop_))); | 1042 base::Bind(&base::MessageLoop::Quit, base::Unretained(&message_loop_))); |
| 1042 url_index_->set_restore_cache_observer(&restore_observer); | 1043 url_index_->set_restore_cache_observer(&restore_observer); |
| 1043 PostRestoreFromCacheFileTask(); | 1044 PostRestoreFromCacheFileTask(); |
| 1044 message_loop_.Run(); | 1045 message_loop_.Run(); |
| 1045 EXPECT_TRUE(restore_observer.succeeded()); | 1046 EXPECT_TRUE(restore_observer.succeeded()); |
| 1046 | 1047 |
| 1047 URLIndexPrivateData& new_data(*GetPrivateData()); | 1048 URLIndexPrivateData& new_data(*GetPrivateData()); |
| 1048 | 1049 |
| 1049 // Make sure the data we have was reloaded from cache. (Version 0 | 1050 // Make sure the data we have was reloaded from cache. (Version 0 |
| 1050 // means rebuilt from history; anything else means restored from | 1051 // means rebuilt from history; anything else means restored from |
| 1051 // a cache version.) | 1052 // a cache version.) Also, the rebuild time should not have changed. |
| 1052 EXPECT_GT(new_data.restored_cache_version_, 0); | 1053 EXPECT_GT(new_data.restored_cache_version_, 0); |
| 1054 EXPECT_EQ(rebuild_time, new_data.last_time_rebuilt_from_history_); |
| 1053 | 1055 |
| 1054 // Compare the captured and restored for equality. | 1056 // Compare the captured and restored for equality. |
| 1055 ExpectPrivateDataEqual(*old_data.get(), new_data); | 1057 ExpectPrivateDataEqual(*old_data.get(), new_data); |
| 1058 } |
| 1059 |
| 1060 TEST_F(InMemoryURLIndexTest, RebuildFromHistoryIfCacheOld) { |
| 1061 base::ScopedTempDir temp_directory; |
| 1062 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 1063 set_history_dir(temp_directory.path()); |
| 1064 |
| 1065 URLIndexPrivateData& private_data(*GetPrivateData()); |
| 1066 |
| 1067 // Ensure that there is really something there to be saved. |
| 1068 EXPECT_FALSE(private_data.word_list_.empty()); |
| 1069 // available_words_ will already be empty since we have freshly built the |
| 1070 // data set for this test. |
| 1071 EXPECT_TRUE(private_data.available_words_.empty()); |
| 1072 EXPECT_FALSE(private_data.word_map_.empty()); |
| 1073 EXPECT_FALSE(private_data.char_word_map_.empty()); |
| 1074 EXPECT_FALSE(private_data.word_id_history_map_.empty()); |
| 1075 EXPECT_FALSE(private_data.history_id_word_map_.empty()); |
| 1076 EXPECT_FALSE(private_data.history_info_map_.empty()); |
| 1077 EXPECT_FALSE(private_data.word_starts_map_.empty()); |
| 1078 |
| 1079 // Make sure the data we have was built from history. (Version 0 |
| 1080 // means rebuilt from history.) |
| 1081 EXPECT_EQ(0, private_data.restored_cache_version_); |
| 1082 |
| 1083 // Overwrite the build time so that we'll think the data is too old |
| 1084 // and rebuild the cache from history. |
| 1085 const base::Time fake_rebuild_time = |
| 1086 base::Time::Now() - base::TimeDelta::FromDays(30); |
| 1087 private_data.last_time_rebuilt_from_history_ = fake_rebuild_time; |
| 1088 |
| 1089 // Capture the current private data for later comparison to restored data. |
| 1090 scoped_refptr<URLIndexPrivateData> old_data(private_data.Duplicate()); |
| 1091 |
| 1092 // Save then restore our private data. |
| 1093 CacheFileSaverObserver save_observer(&message_loop_); |
| 1094 url_index_->set_save_cache_observer(&save_observer); |
| 1095 PostSaveToCacheFileTask(); |
| 1096 message_loop_.Run(); |
| 1097 EXPECT_TRUE(save_observer.succeeded_); |
| 1098 |
| 1099 // Clear and then prove it's clear before restoring. |
| 1100 ClearPrivateData(); |
| 1101 EXPECT_TRUE(private_data.word_list_.empty()); |
| 1102 EXPECT_TRUE(private_data.available_words_.empty()); |
| 1103 EXPECT_TRUE(private_data.word_map_.empty()); |
| 1104 EXPECT_TRUE(private_data.char_word_map_.empty()); |
| 1105 EXPECT_TRUE(private_data.word_id_history_map_.empty()); |
| 1106 EXPECT_TRUE(private_data.history_id_word_map_.empty()); |
| 1107 EXPECT_TRUE(private_data.history_info_map_.empty()); |
| 1108 EXPECT_TRUE(private_data.word_starts_map_.empty()); |
| 1109 |
| 1110 HistoryIndexRestoreObserver restore_observer( |
| 1111 base::Bind(&base::MessageLoop::Quit, base::Unretained(&message_loop_))); |
| 1112 url_index_->set_restore_cache_observer(&restore_observer); |
| 1113 PostRestoreFromCacheFileTask(); |
| 1114 message_loop_.Run(); |
| 1115 EXPECT_TRUE(restore_observer.succeeded()); |
| 1116 |
| 1117 URLIndexPrivateData& new_data(*GetPrivateData()); |
| 1118 |
| 1119 // Make sure the data we have was rebuilt from history. (Version 0 |
| 1120 // means rebuilt from history; anything else means restored from |
| 1121 // a cache version.) |
| 1122 EXPECT_EQ(0, new_data.restored_cache_version_); |
| 1123 EXPECT_NE(fake_rebuild_time, new_data.last_time_rebuilt_from_history_); |
| 1124 |
| 1125 // Compare the captured and restored for equality. |
| 1126 ExpectPrivateDataEqual(*old_data.get(), new_data); |
| 1056 } | 1127 } |
| 1057 | 1128 |
| 1058 class InMemoryURLIndexCacheTest : public testing::Test { | 1129 class InMemoryURLIndexCacheTest : public testing::Test { |
| 1059 public: | 1130 public: |
| 1060 InMemoryURLIndexCacheTest() {} | 1131 InMemoryURLIndexCacheTest() {} |
| 1061 | 1132 |
| 1062 protected: | 1133 protected: |
| 1063 virtual void SetUp() OVERRIDE; | 1134 virtual void SetUp() OVERRIDE; |
| 1064 | 1135 |
| 1065 // Pass-through functions to simplify our friendship with InMemoryURLIndex. | 1136 // Pass-through functions to simplify our friendship with InMemoryURLIndex. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1099 full_file_path.GetComponents(&actual_parts); | 1170 full_file_path.GetComponents(&actual_parts); |
| 1100 ASSERT_EQ(expected_parts.size(), actual_parts.size()); | 1171 ASSERT_EQ(expected_parts.size(), actual_parts.size()); |
| 1101 size_t count = expected_parts.size(); | 1172 size_t count = expected_parts.size(); |
| 1102 for (size_t i = 0; i < count; ++i) | 1173 for (size_t i = 0; i < count; ++i) |
| 1103 EXPECT_EQ(expected_parts[i], actual_parts[i]); | 1174 EXPECT_EQ(expected_parts[i], actual_parts[i]); |
| 1104 // Must clear the history_dir_ to satisfy the dtor's DCHECK. | 1175 // Must clear the history_dir_ to satisfy the dtor's DCHECK. |
| 1105 set_history_dir(base::FilePath()); | 1176 set_history_dir(base::FilePath()); |
| 1106 } | 1177 } |
| 1107 | 1178 |
| 1108 } // namespace history | 1179 } // namespace history |
| OLD | NEW |