OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "chrome/browser/history/history.h" | 8 #include "chrome/browser/history/history.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 file_util::CreateDirectory(history_dir_); | 89 file_util::CreateDirectory(history_dir_); |
90 | 90 |
91 history_ = new HistoryService; | 91 history_ = new HistoryService; |
92 if (!history_->Init(FilePath::FromWStringHack(history_dir_), NULL)) { | 92 if (!history_->Init(FilePath::FromWStringHack(history_dir_), NULL)) { |
93 history_ = NULL; // Tests should notice this NULL ptr & fail. | 93 history_ = NULL; // Tests should notice this NULL ptr & fail. |
94 return; | 94 return; |
95 } | 95 } |
96 | 96 |
97 // Fill the test data. | 97 // Fill the test data. |
98 Time now = Time::Now().LocalMidnight(); | 98 Time now = Time::Now().LocalMidnight(); |
99 for (int i = 0; i < arraysize(test_entries); i++) { | 99 for (size_t i = 0; i < arraysize(test_entries); i++) { |
100 test_entries[i].time = | 100 test_entries[i].time = |
101 now - (test_entries[i].days_ago * TimeDelta::FromDays(1)); | 101 now - (test_entries[i].days_ago * TimeDelta::FromDays(1)); |
102 | 102 |
103 // We need the ID scope and page ID so that the visit tracker can find it. | 103 // We need the ID scope and page ID so that the visit tracker can find it. |
104 const void* id_scope = reinterpret_cast<void*>(1); | 104 const void* id_scope = reinterpret_cast<void*>(1); |
105 int32 page_id = i; | 105 int32 page_id = i; |
106 GURL url(test_entries[i].url); | 106 GURL url(test_entries[i].url); |
107 | 107 |
108 history_->AddPage(url, test_entries[i].time, id_scope, page_id, GURL(), | 108 history_->AddPage(url, test_entries[i].time, id_scope, page_id, GURL(), |
109 PageTransition::LINK, HistoryService::RedirectList()); | 109 PageTransition::LINK, HistoryService::RedirectList()); |
(...skipping 11 matching lines...) Expand all Loading... |
121 } | 121 } |
122 file_util::Delete(history_dir_, true); | 122 file_util::Delete(history_dir_, true); |
123 } | 123 } |
124 | 124 |
125 void QueryHistoryComplete(HistoryService::Handle, QueryResults* results) { | 125 void QueryHistoryComplete(HistoryService::Handle, QueryResults* results) { |
126 results->Swap(&last_query_results_); | 126 results->Swap(&last_query_results_); |
127 MessageLoop::current()->Quit(); // Will return out to QueryHistory. | 127 MessageLoop::current()->Quit(); // Will return out to QueryHistory. |
128 } | 128 } |
129 | 129 |
130 MessageLoop message_loop_; | 130 MessageLoop message_loop_; |
131 | 131 |
132 std::wstring history_dir_; | 132 std::wstring history_dir_; |
133 | 133 |
134 CancelableRequestConsumer consumer_; | 134 CancelableRequestConsumer consumer_; |
135 | 135 |
136 // The QueryHistoryComplete callback will put the results here so QueryHistory | 136 // The QueryHistoryComplete callback will put the results here so QueryHistory |
137 // can return them. | 137 // can return them. |
138 QueryResults last_query_results_; | 138 QueryResults last_query_results_; |
139 | 139 |
140 DISALLOW_EVIL_CONSTRUCTORS(HistoryQueryTest); | 140 DISALLOW_EVIL_CONSTRUCTORS(HistoryQueryTest); |
141 }; | 141 }; |
142 | 142 |
143 TEST_F(HistoryQueryTest, Basic) { | 143 TEST_F(HistoryQueryTest, Basic) { |
144 ASSERT_TRUE(history_.get()); | 144 ASSERT_TRUE(history_.get()); |
145 | 145 |
146 QueryOptions options; | 146 QueryOptions options; |
147 QueryResults results; | 147 QueryResults results; |
148 | 148 |
149 // First query for all of them to make sure they are there and in | 149 // First query for all of them to make sure they are there and in |
150 // chronological order, most recent first. | 150 // chronological order, most recent first. |
151 QueryHistory(std::wstring(), options, &results); | 151 QueryHistory(std::wstring(), options, &results); |
152 ASSERT_EQ(5, results.size()); | 152 ASSERT_EQ(5U, results.size()); |
153 EXPECT_TRUE(NthResultIs(results, 0, 4)); | 153 EXPECT_TRUE(NthResultIs(results, 0, 4)); |
154 EXPECT_TRUE(NthResultIs(results, 1, 2)); | 154 EXPECT_TRUE(NthResultIs(results, 1, 2)); |
155 EXPECT_TRUE(NthResultIs(results, 2, 3)); | 155 EXPECT_TRUE(NthResultIs(results, 2, 3)); |
156 EXPECT_TRUE(NthResultIs(results, 3, 1)); | 156 EXPECT_TRUE(NthResultIs(results, 3, 1)); |
157 EXPECT_TRUE(NthResultIs(results, 4, 0)); | 157 EXPECT_TRUE(NthResultIs(results, 4, 0)); |
158 | 158 |
159 // Next query a time range. The beginning should be inclusive, the ending | 159 // Next query a time range. The beginning should be inclusive, the ending |
160 // should be exclusive. | 160 // should be exclusive. |
161 options.begin_time = test_entries[3].time; | 161 options.begin_time = test_entries[3].time; |
162 options.end_time = test_entries[2].time; | 162 options.end_time = test_entries[2].time; |
163 QueryHistory(std::wstring(), options, &results); | 163 QueryHistory(std::wstring(), options, &results); |
164 EXPECT_EQ(1, results.size()); | 164 EXPECT_EQ(1U, results.size()); |
165 EXPECT_TRUE(NthResultIs(results, 0, 3)); | 165 EXPECT_TRUE(NthResultIs(results, 0, 3)); |
166 } | 166 } |
167 | 167 |
168 // Tests max_count feature for basic (non-Full Text Search) queries. | 168 // Tests max_count feature for basic (non-Full Text Search) queries. |
169 TEST_F(HistoryQueryTest, BasicCount) { | 169 TEST_F(HistoryQueryTest, BasicCount) { |
170 ASSERT_TRUE(history_.get()); | 170 ASSERT_TRUE(history_.get()); |
171 | 171 |
172 QueryOptions options; | 172 QueryOptions options; |
173 QueryResults results; | 173 QueryResults results; |
174 | 174 |
175 // Query all time but with a limit on the number of entries. We should | 175 // Query all time but with a limit on the number of entries. We should |
176 // get the N most recent entries. | 176 // get the N most recent entries. |
177 options.max_count = 2; | 177 options.max_count = 2; |
178 QueryHistory(std::wstring(), options, &results); | 178 QueryHistory(std::wstring(), options, &results); |
179 EXPECT_EQ(2, results.size()); | 179 EXPECT_EQ(2U, results.size()); |
180 EXPECT_TRUE(NthResultIs(results, 0, 4)); | 180 EXPECT_TRUE(NthResultIs(results, 0, 4)); |
181 EXPECT_TRUE(NthResultIs(results, 1, 2)); | 181 EXPECT_TRUE(NthResultIs(results, 1, 2)); |
182 } | 182 } |
183 | 183 |
184 // Tests duplicate collapsing and not in non-Full Text Search situations. | 184 // Tests duplicate collapsing and not in non-Full Text Search situations. |
185 TEST_F(HistoryQueryTest, BasicDupes) { | 185 TEST_F(HistoryQueryTest, BasicDupes) { |
186 ASSERT_TRUE(history_.get()); | 186 ASSERT_TRUE(history_.get()); |
187 | 187 |
188 QueryOptions options; | 188 QueryOptions options; |
189 QueryResults results; | 189 QueryResults results; |
190 | 190 |
191 // We did the query for no collapsing in the "Basic" test above, so here we | 191 // We did the query for no collapsing in the "Basic" test above, so here we |
192 // only test collapsing. | 192 // only test collapsing. |
193 options.most_recent_visit_only = true; | 193 options.most_recent_visit_only = true; |
194 QueryHistory(std::wstring(), options, &results); | 194 QueryHistory(std::wstring(), options, &results); |
195 EXPECT_EQ(4, results.size()); | 195 EXPECT_EQ(4U, results.size()); |
196 EXPECT_TRUE(NthResultIs(results, 0, 4)); | 196 EXPECT_TRUE(NthResultIs(results, 0, 4)); |
197 EXPECT_TRUE(NthResultIs(results, 1, 2)); | 197 EXPECT_TRUE(NthResultIs(results, 1, 2)); |
198 EXPECT_TRUE(NthResultIs(results, 2, 3)); | 198 EXPECT_TRUE(NthResultIs(results, 2, 3)); |
199 EXPECT_TRUE(NthResultIs(results, 3, 1)); | 199 EXPECT_TRUE(NthResultIs(results, 3, 1)); |
200 } | 200 } |
201 | 201 |
202 // This does most of the same tests above, but searches for a FTS string that | 202 // This does most of the same tests above, but searches for a FTS string that |
203 // will match the pages in question. This will trigger a different code path. | 203 // will match the pages in question. This will trigger a different code path. |
204 TEST_F(HistoryQueryTest, FTS) { | 204 TEST_F(HistoryQueryTest, FTS) { |
205 ASSERT_TRUE(history_.get()); | 205 ASSERT_TRUE(history_.get()); |
206 | 206 |
207 QueryOptions options; | 207 QueryOptions options; |
208 QueryResults results; | 208 QueryResults results; |
209 | 209 |
210 // Query all of them to make sure they are there and in order. Note that | 210 // Query all of them to make sure they are there and in order. Note that |
211 // this query will return the starred item twice since we requested all | 211 // this query will return the starred item twice since we requested all |
212 // starred entries and no de-duping. | 212 // starred entries and no de-duping. |
213 QueryHistory(std::wstring(L"some"), options, &results); | 213 QueryHistory(std::wstring(L"some"), options, &results); |
214 EXPECT_EQ(3, results.size()); | 214 EXPECT_EQ(3U, results.size()); |
215 EXPECT_TRUE(NthResultIs(results, 0, 2)); | 215 EXPECT_TRUE(NthResultIs(results, 0, 2)); |
216 EXPECT_TRUE(NthResultIs(results, 1, 3)); | 216 EXPECT_TRUE(NthResultIs(results, 1, 3)); |
217 EXPECT_TRUE(NthResultIs(results, 2, 1)); | 217 EXPECT_TRUE(NthResultIs(results, 2, 1)); |
218 | 218 |
219 // Do a query that should only match one of them. | 219 // Do a query that should only match one of them. |
220 QueryHistory(std::wstring(L"PAGETWO"), options, &results); | 220 QueryHistory(std::wstring(L"PAGETWO"), options, &results); |
221 EXPECT_EQ(1, results.size()); | 221 EXPECT_EQ(1U, results.size()); |
222 EXPECT_TRUE(NthResultIs(results, 0, 3)); | 222 EXPECT_TRUE(NthResultIs(results, 0, 3)); |
223 | 223 |
224 // Next query a time range. The beginning should be inclusive, the ending | 224 // Next query a time range. The beginning should be inclusive, the ending |
225 // should be exclusive. | 225 // should be exclusive. |
226 options.begin_time = test_entries[1].time; | 226 options.begin_time = test_entries[1].time; |
227 options.end_time = test_entries[3].time; | 227 options.end_time = test_entries[3].time; |
228 QueryHistory(std::wstring(L"some"), options, &results); | 228 QueryHistory(std::wstring(L"some"), options, &results); |
229 EXPECT_EQ(1, results.size()); | 229 EXPECT_EQ(1U, results.size()); |
230 EXPECT_TRUE(NthResultIs(results, 0, 1)); | 230 EXPECT_TRUE(NthResultIs(results, 0, 1)); |
231 } | 231 } |
232 | 232 |
233 // Searches titles. | 233 // Searches titles. |
234 TEST_F(HistoryQueryTest, FTSTitle) { | 234 TEST_F(HistoryQueryTest, FTSTitle) { |
235 ASSERT_TRUE(history_.get()); | 235 ASSERT_TRUE(history_.get()); |
236 | 236 |
237 QueryOptions options; | 237 QueryOptions options; |
238 QueryResults results; | 238 QueryResults results; |
239 | 239 |
240 // Query all time but with a limit on the number of entries. We should | 240 // Query all time but with a limit on the number of entries. We should |
241 // get the N most recent entries. | 241 // get the N most recent entries. |
242 QueryHistory(std::wstring(L"title"), options, &results); | 242 QueryHistory(std::wstring(L"title"), options, &results); |
243 EXPECT_EQ(3, results.size()); | 243 EXPECT_EQ(3U, results.size()); |
244 EXPECT_TRUE(NthResultIs(results, 0, 2)); | 244 EXPECT_TRUE(NthResultIs(results, 0, 2)); |
245 EXPECT_TRUE(NthResultIs(results, 1, 3)); | 245 EXPECT_TRUE(NthResultIs(results, 1, 3)); |
246 EXPECT_TRUE(NthResultIs(results, 2, 1)); | 246 EXPECT_TRUE(NthResultIs(results, 2, 1)); |
247 } | 247 } |
248 | 248 |
249 // Tests prefix searching for Full Text Search queries. | 249 // Tests prefix searching for Full Text Search queries. |
250 TEST_F(HistoryQueryTest, FTSPrefix) { | 250 TEST_F(HistoryQueryTest, FTSPrefix) { |
251 ASSERT_TRUE(history_.get()); | 251 ASSERT_TRUE(history_.get()); |
252 | 252 |
253 QueryOptions options; | 253 QueryOptions options; |
254 QueryResults results; | 254 QueryResults results; |
255 | 255 |
256 // Query with a prefix search. Should return matches for "PAGETWO" and | 256 // Query with a prefix search. Should return matches for "PAGETWO" and |
257 // "PAGETHREE". | 257 // "PAGETHREE". |
258 QueryHistory(std::wstring(L"PAGET"), options, &results); | 258 QueryHistory(std::wstring(L"PAGET"), options, &results); |
259 EXPECT_EQ(2, results.size()); | 259 EXPECT_EQ(2U, results.size()); |
260 EXPECT_TRUE(NthResultIs(results, 0, 2)); | 260 EXPECT_TRUE(NthResultIs(results, 0, 2)); |
261 EXPECT_TRUE(NthResultIs(results, 1, 3)); | 261 EXPECT_TRUE(NthResultIs(results, 1, 3)); |
262 } | 262 } |
263 | 263 |
264 // Tests max_count feature for Full Text Search queries. | 264 // Tests max_count feature for Full Text Search queries. |
265 TEST_F(HistoryQueryTest, FTSCount) { | 265 TEST_F(HistoryQueryTest, FTSCount) { |
266 ASSERT_TRUE(history_.get()); | 266 ASSERT_TRUE(history_.get()); |
267 | 267 |
268 QueryOptions options; | 268 QueryOptions options; |
269 QueryResults results; | 269 QueryResults results; |
270 | 270 |
271 // Query all time but with a limit on the number of entries. We should | 271 // Query all time but with a limit on the number of entries. We should |
272 // get the N most recent entries. | 272 // get the N most recent entries. |
273 options.max_count = 2; | 273 options.max_count = 2; |
274 QueryHistory(std::wstring(L"some"), options, &results); | 274 QueryHistory(std::wstring(L"some"), options, &results); |
275 EXPECT_EQ(2, results.size()); | 275 EXPECT_EQ(2U, results.size()); |
276 EXPECT_TRUE(NthResultIs(results, 0, 2)); | 276 EXPECT_TRUE(NthResultIs(results, 0, 2)); |
277 EXPECT_TRUE(NthResultIs(results, 1, 3)); | 277 EXPECT_TRUE(NthResultIs(results, 1, 3)); |
278 | 278 |
279 // Now query a subset of the pages and limit by N items. "FOO" should match | 279 // Now query a subset of the pages and limit by N items. "FOO" should match |
280 // the 2nd & 3rd pages, but we should only get the 3rd one because of the one | 280 // the 2nd & 3rd pages, but we should only get the 3rd one because of the one |
281 // page max restriction. | 281 // page max restriction. |
282 options.max_count = 1; | 282 options.max_count = 1; |
283 QueryHistory(std::wstring(L"FOO"), options, &results); | 283 QueryHistory(std::wstring(L"FOO"), options, &results); |
284 EXPECT_EQ(1, results.size()); | 284 EXPECT_EQ(1U, results.size()); |
285 EXPECT_TRUE(NthResultIs(results, 0, 3)); | 285 EXPECT_TRUE(NthResultIs(results, 0, 3)); |
286 } | 286 } |
287 | 287 |
288 // Tests that FTS queries can find URLs when they exist only in the archived | 288 // Tests that FTS queries can find URLs when they exist only in the archived |
289 // database. This also tests that imported URLs can be found, since we use | 289 // database. This also tests that imported URLs can be found, since we use |
290 // AddPageWithDetails just like the importer. | 290 // AddPageWithDetails just like the importer. |
291 TEST_F(HistoryQueryTest, FTSArchived) { | 291 TEST_F(HistoryQueryTest, FTSArchived) { |
292 ASSERT_TRUE(history_.get()); | 292 ASSERT_TRUE(history_.get()); |
293 | 293 |
294 std::vector<URLRow> urls_to_add; | 294 std::vector<URLRow> urls_to_add; |
(...skipping 10 matching lines...) Expand all Loading... |
305 | 305 |
306 history_->AddPagesWithDetails(urls_to_add); | 306 history_->AddPagesWithDetails(urls_to_add); |
307 | 307 |
308 QueryOptions options; | 308 QueryOptions options; |
309 QueryResults results; | 309 QueryResults results; |
310 | 310 |
311 // Query all time. The title we get should be the one in the full text | 311 // Query all time. The title we get should be the one in the full text |
312 // database and not the most current title (since otherwise highlighting in | 312 // database and not the most current title (since otherwise highlighting in |
313 // the title might be wrong). | 313 // the title might be wrong). |
314 QueryHistory(std::wstring(L"archived"), options, &results); | 314 QueryHistory(std::wstring(L"archived"), options, &results); |
315 ASSERT_EQ(1, results.size()); | 315 ASSERT_EQ(1U, results.size()); |
316 EXPECT_TRUE(row1.url() == results[0].url()); | 316 EXPECT_TRUE(row1.url() == results[0].url()); |
317 EXPECT_TRUE(row1.title() == results[0].title()); | 317 EXPECT_TRUE(row1.title() == results[0].title()); |
318 } | 318 } |
319 | 319 |
320 /* TODO(brettw) re-enable this. It is commented out because the current history | 320 /* TODO(brettw) re-enable this. It is commented out because the current history |
321 code prohibits adding more than one indexed page with the same URL. When we | 321 code prohibits adding more than one indexed page with the same URL. When we |
322 have tiered history, there could be a dupe in the archived history which | 322 have tiered history, there could be a dupe in the archived history which |
323 won't get picked up by the deletor and it can happen again. When this is the | 323 won't get picked up by the deletor and it can happen again. When this is the |
324 case, we should fix this test to duplicate that situation. | 324 case, we should fix this test to duplicate that situation. |
325 | 325 |
(...skipping 12 matching lines...) Expand all Loading... |
338 | 338 |
339 // Now with collapsing. | 339 // Now with collapsing. |
340 options.most_recent_visit_only = true; | 340 options.most_recent_visit_only = true; |
341 QueryHistory(std::wstring(L"Other"), options, &results); | 341 QueryHistory(std::wstring(L"Other"), options, &results); |
342 EXPECT_EQ(1, results.urls().size()); | 342 EXPECT_EQ(1, results.urls().size()); |
343 EXPECT_TRUE(NthResultIs(results, 0, 4)); | 343 EXPECT_TRUE(NthResultIs(results, 0, 4)); |
344 } | 344 } |
345 */ | 345 */ |
346 | 346 |
347 } // namespace history | 347 } // namespace history |
OLD | NEW |