OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/file_util.h" | |
6 #include "base/path_service.h" | |
7 #include "base/scoped_temp_dir.h" | |
8 #include "base/time.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/history/in_memory_url_cache_database.h" | |
11 #include "chrome/browser/history/url_index_private_data.h" | |
12 #include "chrome/common/chrome_paths.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "googleurl/src/gurl.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace history { | |
18 | |
19 struct TestURLInfo { | |
20 std::string url; | |
21 std::string title; | |
22 int visit_count; | |
23 int typed_count; | |
24 int days_from_now; | |
25 } private_data_test_db[] = { | |
26 {"http://www.google.com/", "Google", 3, 3, 0}, | |
27 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0}, | |
28 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0}, | |
29 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 1, 0}, | |
30 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 0}, | |
31 {"http://news.google.com/", "Google News", 1, 1, 0}, | |
32 {"http://foo.com/", "Dir", 200, 100, 0}, | |
33 {"http://foo.com/dir/", "Dir", 2, 1, 10}, | |
34 {"http://foo.com/dir/another/", "Dir", 5, 10, 0}, | |
35 {"http://foo.com/dir/another/again/", "Dir", 5, 1, 0}, | |
36 {"http://foo.com/dir/another/again/myfile.html", "File", 3, 2, 0}, | |
37 {"http://visitedest.com/y/a", "VA", 10, 1, 20}, | |
38 {"http://visitedest.com/y/b", "VB", 9, 1, 20}, | |
39 {"http://visitedest.com/x/c", "VC", 8, 1, 20}, | |
40 {"http://visitedest.com/x/d", "VD", 7, 1, 20}, | |
41 {"http://visitedest.com/y/e", "VE", 6, 1, 20}, | |
42 {"http://typeredest.com/y/a", "TA", 3, 5, 0}, | |
43 {"http://typeredest.com/y/b", "TB", 3, 4, 0}, | |
44 {"http://typeredest.com/x/c", "TC", 3, 3, 0}, | |
45 {"http://typeredest.com/x/d", "TD", 3, 2, 0}, | |
46 {"http://typeredest.com/y/e", "TE", 3, 1, 0}, | |
47 {"http://daysagoest.com/y/a", "DA", 1, 1, 0}, | |
48 {"http://daysagoest.com/y/b", "DB", 1, 1, 1}, | |
49 {"http://daysagoest.com/x/c", "DC", 1, 1, 2}, | |
50 {"http://daysagoest.com/x/d", "DD", 1, 1, 3}, | |
51 {"http://daysagoest.com/y/e", "DE", 1, 1, 4}, | |
52 {"http://abcdefghixyzjklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
53 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 0}, | |
54 {"http://abcdefghijklxyzmnopqrstuvw.com/a", "", 3, 1, 0}, | |
55 {"http://abcdefxyzghijklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
56 {"http://abcxyzdefghijklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
57 {"http://xyzabcdefghijklmnopqrstuvw.com/a", "", 3, 1, 0}, | |
58 {"http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice", | |
59 "Dogs & Cats & Mice & Other Animals", 1, 1, 0}, | |
60 {"https://monkeytrap.org/", "", 3, 1, 0}, | |
61 }; | |
62 | |
63 class URLIndexPrivateDataTest : public testing::Test { | |
64 public: | |
65 URLIndexPrivateDataTest(); | |
66 virtual ~URLIndexPrivateDataTest(); | |
67 | |
68 virtual void SetUp() OVERRIDE; | |
69 | |
70 void GetTestData(size_t* data_count, TestURLInfo** test_data); | |
71 | |
72 // Fills test data into the history system. | |
73 void FillData(); | |
74 | |
75 // Data verification helper functions. | |
76 void ExpectPrivateDataNotEmpty(const URLIndexPrivateData& data); | |
77 void ExpectPrivateDataEqual(const URLIndexPrivateData& expected, | |
78 const URLIndexPrivateData& actual); | |
79 | |
80 protected: | |
81 scoped_refptr<URLIndexPrivateData> private_data_; | |
82 ScopedTempDir temp_cache_dir_; | |
83 }; | |
84 | |
85 URLIndexPrivateDataTest::URLIndexPrivateDataTest() {} | |
86 URLIndexPrivateDataTest::~URLIndexPrivateDataTest() {} | |
87 | |
88 void URLIndexPrivateDataTest::SetUp() { | |
89 ASSERT_TRUE(temp_cache_dir_.CreateUniqueTempDir()); | |
90 private_data_ = new URLIndexPrivateData(temp_cache_dir_.path(), "en"); | |
91 private_data_->Init( | |
92 content::BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
93 FillData(); | |
94 } | |
95 | |
96 void URLIndexPrivateDataTest::GetTestData(size_t* data_count, | |
97 TestURLInfo** test_data) { | |
98 DCHECK(data_count); | |
99 DCHECK(test_data); | |
100 *data_count = arraysize(private_data_test_db); | |
101 *test_data = &private_data_test_db[0]; | |
102 } | |
103 | |
104 void URLIndexPrivateDataTest::FillData() { | |
105 size_t data_count = 0; | |
106 TestURLInfo* test_data = NULL; | |
107 GetTestData(&data_count, &test_data); | |
108 for (size_t i = 0; i < data_count; ++i) { | |
109 const TestURLInfo& cur(test_data[i]); | |
110 const GURL current_url(cur.url); | |
111 base::Time visit_time = | |
112 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now); | |
113 history::URLRow url_info(current_url); | |
114 url_info.set_id(i); | |
115 url_info.set_title(UTF8ToUTF16(cur.title)); | |
116 url_info.set_visit_count(cur.visit_count); | |
117 url_info.set_typed_count(cur.typed_count); | |
118 url_info.set_last_visit(visit_time); | |
119 url_info.set_hidden(false); | |
120 private_data_->UpdateURL(url_info); | |
121 } | |
122 // Stall until the pending operations have completed. | |
123 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | |
124 } | |
125 | |
126 void URLIndexPrivateDataTest::ExpectPrivateDataNotEmpty( | |
127 const URLIndexPrivateData& data) { | |
128 EXPECT_FALSE(data.word_list_.empty()); | |
129 // available_words_ will be empty since we have freshly built the | |
130 // data set for these tests. | |
131 EXPECT_TRUE(data.available_words_.empty()); | |
132 EXPECT_FALSE(data.word_map_.empty()); | |
133 EXPECT_FALSE(data.char_word_map_.empty()); | |
134 EXPECT_FALSE(data.word_id_history_map_.empty()); | |
135 EXPECT_FALSE(data.history_id_word_map_.empty()); | |
136 EXPECT_FALSE(data.history_info_map_.empty()); | |
137 EXPECT_FALSE(data.word_starts_map_.empty()); | |
138 } | |
139 | |
140 // Helper function which compares two maps for equivalence. The maps' values | |
141 // are associative containers and their contents are compared as well. | |
142 template<typename T> | |
143 void ExpectMapOfContainersIdentical(const T& expected, const T& actual) { | |
144 ASSERT_EQ(expected.size(), actual.size()); | |
145 for (typename T::const_iterator expected_iter = expected.begin(); | |
146 expected_iter != expected.end(); ++expected_iter) { | |
147 typename T::const_iterator actual_iter = actual.find(expected_iter->first); | |
148 ASSERT_TRUE(actual_iter != actual.end()); | |
149 typename T::mapped_type const& expected_values(expected_iter->second); | |
150 typename T::mapped_type const& actual_values(actual_iter->second); | |
151 ASSERT_EQ(expected_values.size(), actual_values.size()); | |
152 for (typename T::mapped_type::const_iterator set_iter = | |
153 expected_values.begin(); set_iter != expected_values.end(); ++set_iter) | |
154 EXPECT_EQ(actual_values.count(*set_iter), | |
155 expected_values.count(*set_iter)); | |
156 } | |
157 } | |
158 | |
159 void URLIndexPrivateDataTest::ExpectPrivateDataEqual( | |
160 const URLIndexPrivateData& expected, | |
161 const URLIndexPrivateData& actual) { | |
162 EXPECT_EQ(expected.word_list_.size(), actual.word_list_.size()); | |
163 EXPECT_EQ(expected.word_map_.size(), actual.word_map_.size()); | |
164 EXPECT_EQ(expected.char_word_map_.size(), actual.char_word_map_.size()); | |
165 EXPECT_EQ(expected.word_id_history_map_.size(), | |
166 actual.word_id_history_map_.size()); | |
167 EXPECT_EQ(expected.history_id_word_map_.size(), | |
168 actual.history_id_word_map_.size()); | |
169 EXPECT_EQ(expected.history_info_map_.size(), actual.history_info_map_.size()); | |
170 EXPECT_EQ(expected.word_starts_map_.size(), actual.word_starts_map_.size()); | |
171 // WordList must be index-by-index equal. | |
172 size_t count = expected.word_list_.size(); | |
173 for (size_t i = 0; i < count; ++i) | |
174 EXPECT_EQ(expected.word_list_[i], actual.word_list_[i]); | |
175 | |
176 ExpectMapOfContainersIdentical(expected.char_word_map_, | |
177 actual.char_word_map_); | |
178 ExpectMapOfContainersIdentical(expected.word_id_history_map_, | |
179 actual.word_id_history_map_); | |
180 ExpectMapOfContainersIdentical(expected.history_id_word_map_, | |
181 actual.history_id_word_map_); | |
182 | |
183 for (HistoryInfoMap::const_iterator expected_info = | |
184 expected.history_info_map_.begin(); | |
185 expected_info != expected.history_info_map_.end(); ++expected_info) { | |
186 HistoryInfoMap::const_iterator actual_info = | |
187 actual.history_info_map_.find(expected_info->first); | |
188 // NOTE(yfriedman): ASSERT_NE can't be used due to incompatibility between | |
189 // gtest and STLPort in the Android build. See | |
190 // http://code.google.com/p/googletest/issues/detail?id=359 | |
191 ASSERT_TRUE(actual_info != actual.history_info_map_.end()); | |
192 const URLRow& expected_row(expected_info->second); | |
193 const URLRow& actual_row(actual_info->second); | |
194 EXPECT_EQ(expected_row.visit_count(), actual_row.visit_count()); | |
195 EXPECT_EQ(expected_row.typed_count(), actual_row.typed_count()); | |
196 EXPECT_EQ(expected_row.last_visit(), actual_row.last_visit()); | |
197 EXPECT_EQ(expected_row.url(), actual_row.url()); | |
198 } | |
199 | |
200 for (WordStartsMap::const_iterator expected_starts = | |
201 expected.word_starts_map_.begin(); | |
202 expected_starts != expected.word_starts_map_.end(); | |
203 ++expected_starts) { | |
204 WordStartsMap::const_iterator actual_starts = | |
205 actual.word_starts_map_.find(expected_starts->first); | |
206 // NOTE(yfriedman): ASSERT_NE can't be used due to incompatibility between | |
207 // gtest and STLPort in the Android build. See | |
208 // http://code.google.com/p/googletest/issues/detail?id=359 | |
209 ASSERT_TRUE(actual_starts != actual.word_starts_map_.end()); | |
210 const RowWordStarts& expected_word_starts(expected_starts->second); | |
211 const RowWordStarts& actual_word_starts(actual_starts->second); | |
212 EXPECT_EQ(expected_word_starts.url_word_starts_.size(), | |
213 actual_word_starts.url_word_starts_.size()); | |
214 EXPECT_TRUE(std::equal(expected_word_starts.url_word_starts_.begin(), | |
215 expected_word_starts.url_word_starts_.end(), | |
216 actual_word_starts.url_word_starts_.begin())); | |
217 EXPECT_EQ(expected_word_starts.title_word_starts_.size(), | |
218 actual_word_starts.title_word_starts_.size()); | |
219 EXPECT_TRUE(std::equal(expected_word_starts.title_word_starts_.begin(), | |
220 expected_word_starts.title_word_starts_.end(), | |
221 actual_word_starts.title_word_starts_.begin())); | |
222 } | |
223 } | |
224 | |
225 TEST_F(URLIndexPrivateDataTest, CacheFetch) { | |
226 // Compare the in-memory index data to the on-disk cached data. | |
227 const URLIndexPrivateData& expected_data(*(private_data_.get())); | |
228 ExpectPrivateDataNotEmpty(expected_data); | |
229 | |
230 // Grab the index data from the cache. | |
231 scoped_refptr<URLIndexPrivateData> cached_data = new URLIndexPrivateData; | |
232 URLIndexPrivateData* actual_data = cached_data.get(); | |
233 ASSERT_TRUE(private_data_->cache_db_->RestorePrivateData(actual_data)); | |
234 EXPECT_TRUE(actual_data->ValidateConsistency()); | |
235 ExpectPrivateDataEqual(expected_data, *actual_data); | |
236 } | |
237 | |
238 class URLIndexOldCacheTest : public testing::Test { | |
239 public: | |
240 URLIndexOldCacheTest(); | |
241 virtual ~URLIndexOldCacheTest(); | |
242 | |
243 virtual void SetUp() OVERRIDE; | |
244 | |
245 protected: | |
246 scoped_refptr<URLIndexPrivateData> private_data_; | |
247 ScopedTempDir temp_cache_dir_; | |
248 }; | |
249 | |
250 URLIndexOldCacheTest::URLIndexOldCacheTest() {} | |
251 URLIndexOldCacheTest::~URLIndexOldCacheTest() {} | |
252 | |
253 void URLIndexOldCacheTest::SetUp() { | |
254 // Create a file that looks like an old protobuf-based cache file. | |
255 ASSERT_TRUE(temp_cache_dir_.CreateUniqueTempDir()); | |
256 std::string dummy_data("DUMMY DATA"); | |
257 int size = dummy_data.size(); | |
258 FilePath path = temp_cache_dir_.path().Append( | |
259 FILE_PATH_LITERAL("History Provider Cache")); | |
260 ASSERT_EQ(size, file_util::WriteFile(path, dummy_data.c_str(), size)); | |
261 ASSERT_TRUE(file_util::PathExists(path)); | |
262 | |
263 // Continue initializing. This will attempt to restore from the SQLite cache | |
264 // but it doesn't exist so the old protobuf file should be automatically | |
265 // deleted. | |
266 private_data_ = new URLIndexPrivateData(temp_cache_dir_.path(), "en"); | |
267 private_data_->Init( | |
268 content::BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
269 EXPECT_FALSE(private_data_->RestoreFromCacheTask()); | |
270 } | |
271 | |
272 TEST_F(URLIndexOldCacheTest, CacheProtobufDelete) { | |
273 // If an old, protobuf-based cache file exists then it should be being deleted | |
274 // when an attempt is made to restore the index data from the SQLite cache but | |
275 // such SQLite cache does not exist. This will happen the first time a user | |
276 // runs Chrome after the SQLite-based cache implementation has been added. | |
277 // All we have to do here is verify that the file does not exist. | |
278 FilePath path = temp_cache_dir_.path().Append( | |
279 FILE_PATH_LITERAL("History Provider Cache")); | |
280 ASSERT_FALSE(file_util::PathExists(path)); | |
281 } | |
282 | |
283 } // namespace history | |
OLD | NEW |