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 "chrome/browser/autocomplete/history_contents_provider.h" | 5 #include "chrome/browser/autocomplete/history_contents_provider.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 scoped_ptr<TestingProfile> profile_; | 125 scoped_ptr<TestingProfile> profile_; |
126 scoped_refptr<HistoryContentsProvider> provider_; | 126 scoped_refptr<HistoryContentsProvider> provider_; |
127 }; | 127 }; |
128 | 128 |
129 class HistoryContentsProviderBodyOnlyTest : public HistoryContentsProviderTest { | 129 class HistoryContentsProviderBodyOnlyTest : public HistoryContentsProviderTest { |
130 protected: | 130 protected: |
131 virtual bool BodyOnly() OVERRIDE { return true; } | 131 virtual bool BodyOnly() OVERRIDE { return true; } |
132 }; | 132 }; |
133 | 133 |
134 TEST_F(HistoryContentsProviderTest, Body) { | 134 TEST_F(HistoryContentsProviderTest, Body) { |
135 AutocompleteInput input(ASCIIToUTF16("FOO"), string16::npos, string16(), true, | 135 AutocompleteInput input(ASCIIToUTF16("FOO"), string16::npos, string16(), |
136 false, true, AutocompleteInput::ALL_MATCHES); | 136 GURL(), true, false, true, |
| 137 AutocompleteInput::ALL_MATCHES); |
137 RunQuery(input, false); | 138 RunQuery(input, false); |
138 | 139 |
139 // The results should be the first two pages, in decreasing order. | 140 // The results should be the first two pages, in decreasing order. |
140 const ACMatches& m = matches(); | 141 const ACMatches& m = matches(); |
141 ASSERT_EQ(2U, m.size()); | 142 ASSERT_EQ(2U, m.size()); |
142 EXPECT_EQ(test_entries[0].url, m[0].destination_url.spec()); | 143 EXPECT_EQ(test_entries[0].url, m[0].destination_url.spec()); |
143 EXPECT_STREQ(test_entries[0].title, UTF16ToUTF8(m[0].description).c_str()); | 144 EXPECT_STREQ(test_entries[0].title, UTF16ToUTF8(m[0].description).c_str()); |
144 EXPECT_EQ(test_entries[1].url, m[1].destination_url.spec()); | 145 EXPECT_EQ(test_entries[1].url, m[1].destination_url.spec()); |
145 EXPECT_STREQ(test_entries[1].title, UTF16ToUTF8(m[1].description).c_str()); | 146 EXPECT_STREQ(test_entries[1].title, UTF16ToUTF8(m[1].description).c_str()); |
146 } | 147 } |
147 | 148 |
148 TEST_F(HistoryContentsProviderTest, Title) { | 149 TEST_F(HistoryContentsProviderTest, Title) { |
149 AutocompleteInput input(ASCIIToUTF16("PAGEONE"), string16::npos, string16(), | 150 AutocompleteInput input(ASCIIToUTF16("PAGEONE"), string16::npos, string16(), |
150 true, false, true, AutocompleteInput::ALL_MATCHES); | 151 GURL(), true, false, true, |
| 152 AutocompleteInput::ALL_MATCHES); |
151 RunQuery(input, false); | 153 RunQuery(input, false); |
152 | 154 |
153 // The results should be the first two pages. | 155 // The results should be the first two pages. |
154 const ACMatches& m = matches(); | 156 const ACMatches& m = matches(); |
155 ASSERT_EQ(2U, m.size()); | 157 ASSERT_EQ(2U, m.size()); |
156 EXPECT_EQ(test_entries[0].url, m[0].destination_url.spec()); | 158 EXPECT_EQ(test_entries[0].url, m[0].destination_url.spec()); |
157 EXPECT_STREQ(test_entries[0].title, UTF16ToUTF8(m[0].description).c_str()); | 159 EXPECT_STREQ(test_entries[0].title, UTF16ToUTF8(m[0].description).c_str()); |
158 EXPECT_EQ(test_entries[1].url, m[1].destination_url.spec()); | 160 EXPECT_EQ(test_entries[1].url, m[1].destination_url.spec()); |
159 EXPECT_STREQ(test_entries[1].title, UTF16ToUTF8(m[1].description).c_str()); | 161 EXPECT_STREQ(test_entries[1].title, UTF16ToUTF8(m[1].description).c_str()); |
160 } | 162 } |
161 | 163 |
162 // The "minimal changes" flag should mean that we don't re-query the DB. | 164 // The "minimal changes" flag should mean that we don't re-query the DB. |
163 TEST_F(HistoryContentsProviderTest, MinimalChanges) { | 165 TEST_F(HistoryContentsProviderTest, MinimalChanges) { |
164 // A minimal changes request when there have been no real queries should | 166 // A minimal changes request when there have been no real queries should |
165 // give us no results. | 167 // give us no results. |
166 AutocompleteInput sync_input(ASCIIToUTF16("PAGEONE"), string16::npos, | 168 AutocompleteInput sync_input(ASCIIToUTF16("PAGEONE"), string16::npos, |
167 string16(), true, false, true, | 169 string16(), GURL(), true, false, true, |
168 AutocompleteInput::SYNCHRONOUS_MATCHES); | 170 AutocompleteInput::SYNCHRONOUS_MATCHES); |
169 RunQuery(sync_input, true); | 171 RunQuery(sync_input, true); |
170 const ACMatches& m1 = matches(); | 172 const ACMatches& m1 = matches(); |
171 EXPECT_EQ(0U, m1.size()); | 173 EXPECT_EQ(0U, m1.size()); |
172 | 174 |
173 // Now do a "regular" query to get the results. | 175 // Now do a "regular" query to get the results. |
174 AutocompleteInput async_input(ASCIIToUTF16("PAGEONE"), string16::npos, | 176 AutocompleteInput async_input(ASCIIToUTF16("PAGEONE"), string16::npos, |
175 string16(), true, false, true, | 177 string16(), GURL(), true, false, true, |
176 AutocompleteInput::ALL_MATCHES); | 178 AutocompleteInput::ALL_MATCHES); |
177 RunQuery(async_input, false); | 179 RunQuery(async_input, false); |
178 const ACMatches& m2 = matches(); | 180 const ACMatches& m2 = matches(); |
179 EXPECT_EQ(2U, m2.size()); | 181 EXPECT_EQ(2U, m2.size()); |
180 | 182 |
181 // Now do a minimal one where we want synchronous results, and the results | 183 // Now do a minimal one where we want synchronous results, and the results |
182 // should still be there. | 184 // should still be there. |
183 RunQuery(sync_input, true); | 185 RunQuery(sync_input, true); |
184 const ACMatches& m3 = matches(); | 186 const ACMatches& m3 = matches(); |
185 EXPECT_EQ(2U, m3.size()); | 187 EXPECT_EQ(2U, m3.size()); |
186 } | 188 } |
187 | 189 |
188 TEST_F(HistoryContentsProviderBodyOnlyTest, MinimalChanges) { | 190 TEST_F(HistoryContentsProviderBodyOnlyTest, MinimalChanges) { |
189 // A minimal changes request when there have been no real queries should | 191 // A minimal changes request when there have been no real queries should |
190 // give us no results. | 192 // give us no results. |
191 AutocompleteInput sync_input(ASCIIToUTF16("PAGEONE"), string16::npos, | 193 AutocompleteInput sync_input(ASCIIToUTF16("PAGEONE"), string16::npos, |
192 string16(), true, false, true, | 194 string16(), GURL(), true, false, true, |
193 AutocompleteInput::SYNCHRONOUS_MATCHES); | 195 AutocompleteInput::SYNCHRONOUS_MATCHES); |
194 RunQuery(sync_input, true); | 196 RunQuery(sync_input, true); |
195 const ACMatches& m1 = matches(); | 197 const ACMatches& m1 = matches(); |
196 EXPECT_EQ(0U, m1.size()); | 198 EXPECT_EQ(0U, m1.size()); |
197 | 199 |
198 // Now do a "regular" query to get no results because we are body-only. | 200 // Now do a "regular" query to get no results because we are body-only. |
199 AutocompleteInput async_input(ASCIIToUTF16("PAGEONE"), string16::npos, | 201 AutocompleteInput async_input(ASCIIToUTF16("PAGEONE"), string16::npos, |
200 string16(), true, false, true, | 202 string16(), GURL(), true, false, true, |
201 AutocompleteInput::ALL_MATCHES); | 203 AutocompleteInput::ALL_MATCHES); |
202 RunQuery(async_input, false); | 204 RunQuery(async_input, false); |
203 const ACMatches& m2 = matches(); | 205 const ACMatches& m2 = matches(); |
204 EXPECT_EQ(0U, m2.size()); | 206 EXPECT_EQ(0U, m2.size()); |
205 | 207 |
206 // Now do a minimal one where we want synchronous results, and the results | 208 // Now do a minimal one where we want synchronous results, and the results |
207 // should still not be there. | 209 // should still not be there. |
208 RunQuery(sync_input, true); | 210 RunQuery(sync_input, true); |
209 const ACMatches& m3 = matches(); | 211 const ACMatches& m3 = matches(); |
210 EXPECT_EQ(0U, m3.size()); | 212 EXPECT_EQ(0U, m3.size()); |
211 } | 213 } |
212 | 214 |
213 // Tests that history is deleted properly. | 215 // Tests that history is deleted properly. |
214 TEST_F(HistoryContentsProviderTest, DeleteMatch) { | 216 TEST_F(HistoryContentsProviderTest, DeleteMatch) { |
215 AutocompleteInput input(ASCIIToUTF16("bar"), string16::npos, string16(), true, | 217 AutocompleteInput input(ASCIIToUTF16("bar"), string16::npos, string16(), |
216 false, true, AutocompleteInput::ALL_MATCHES); | 218 GURL(), true, false, true, |
| 219 AutocompleteInput::ALL_MATCHES); |
217 RunQuery(input, false); | 220 RunQuery(input, false); |
218 | 221 |
219 // Query; the result should be the third page. | 222 // Query; the result should be the third page. |
220 const ACMatches& m = matches(); | 223 const ACMatches& m = matches(); |
221 ASSERT_EQ(1U, m.size()); | 224 ASSERT_EQ(1U, m.size()); |
222 EXPECT_EQ(test_entries[2].url, m[0].destination_url.spec()); | 225 EXPECT_EQ(test_entries[2].url, m[0].destination_url.spec()); |
223 | 226 |
224 // Now delete the match and ensure it was removed. | 227 // Now delete the match and ensure it was removed. |
225 provider()->DeleteMatch(m[0]); | 228 provider()->DeleteMatch(m[0]); |
226 EXPECT_EQ(0U, matches().size()); | 229 EXPECT_EQ(0U, matches().size()); |
227 } | 230 } |
228 | 231 |
229 // Tests deleting starred results from history, not affecting bookmarks/matches. | 232 // Tests deleting starred results from history, not affecting bookmarks/matches. |
230 TEST_F(HistoryContentsProviderTest, DeleteStarredMatch) { | 233 TEST_F(HistoryContentsProviderTest, DeleteStarredMatch) { |
231 profile()->CreateBookmarkModel(false); | 234 profile()->CreateBookmarkModel(false); |
232 profile()->BlockUntilBookmarkModelLoaded(); | 235 profile()->BlockUntilBookmarkModelLoaded(); |
233 | 236 |
234 // Bookmark a history item. | 237 // Bookmark a history item. |
235 GURL bookmark_url(test_entries[2].url); | 238 GURL bookmark_url(test_entries[2].url); |
236 bookmark_utils::AddIfNotBookmarked( | 239 bookmark_utils::AddIfNotBookmarked( |
237 BookmarkModelFactory::GetForProfile(profile()), | 240 BookmarkModelFactory::GetForProfile(profile()), |
238 bookmark_url, | 241 bookmark_url, |
239 ASCIIToUTF16("bar")); | 242 ASCIIToUTF16("bar")); |
240 | 243 |
241 // Get the match to delete its history | 244 // Get the match to delete its history |
242 AutocompleteInput input(ASCIIToUTF16("bar"), string16::npos, string16(), true, | 245 AutocompleteInput input(ASCIIToUTF16("bar"), string16::npos, string16(), |
243 false, true, AutocompleteInput::ALL_MATCHES); | 246 GURL(), true, false, true, |
| 247 AutocompleteInput::ALL_MATCHES); |
244 RunQuery(input, false); | 248 RunQuery(input, false); |
245 const ACMatches& m = matches(); | 249 const ACMatches& m = matches(); |
246 ASSERT_EQ(1U, m.size()); | 250 ASSERT_EQ(1U, m.size()); |
247 | 251 |
248 // Now delete the match and ensure it was *not* removed. | 252 // Now delete the match and ensure it was *not* removed. |
249 provider()->DeleteMatch(m[0]); | 253 provider()->DeleteMatch(m[0]); |
250 EXPECT_EQ(1U, matches().size()); | 254 EXPECT_EQ(1U, matches().size()); |
251 | 255 |
252 // Run a query that would only match history (but the history is deleted) | 256 // Run a query that would only match history (but the history is deleted) |
253 AutocompleteInput you_input(ASCIIToUTF16("you"), string16::npos, string16(), | 257 AutocompleteInput you_input(ASCIIToUTF16("you"), string16::npos, string16(), |
254 true, false, true, | 258 GURL(), true, false, true, |
255 AutocompleteInput::ALL_MATCHES); | 259 AutocompleteInput::ALL_MATCHES); |
256 RunQuery(you_input, false); | 260 RunQuery(you_input, false); |
257 EXPECT_EQ(0U, matches().size()); | 261 EXPECT_EQ(0U, matches().size()); |
258 } | 262 } |
259 | 263 |
260 TEST_F(HistoryContentsProviderTest, CullSearchResults) { | 264 TEST_F(HistoryContentsProviderTest, CullSearchResults) { |
261 // Set up a default search engine. | 265 // Set up a default search engine. |
262 TemplateURLData data; | 266 TemplateURLData data; |
263 data.SetKeyword(ASCIIToUTF16("TestEngine")); | 267 data.SetKeyword(ASCIIToUTF16("TestEngine")); |
264 data.SetURL("http://testsearch.com/?q={searchTerms}"); | 268 data.SetURL("http://testsearch.com/?q={searchTerms}"); |
265 TemplateURLService* template_url_service = | 269 TemplateURLService* template_url_service = |
266 TemplateURLServiceFactory::GetForProfile(profile()); | 270 TemplateURLServiceFactory::GetForProfile(profile()); |
267 TemplateURL* template_url = new TemplateURL(profile(), data); | 271 TemplateURL* template_url = new TemplateURL(profile(), data); |
268 template_url_service->Add(template_url); | 272 template_url_service->Add(template_url); |
269 template_url_service->SetDefaultSearchProvider(template_url); | 273 template_url_service->SetDefaultSearchProvider(template_url); |
270 template_url_service->Load(); | 274 template_url_service->Load(); |
271 | 275 |
272 AutocompleteInput input(ASCIIToUTF16("moo"), string16::npos, string16(), true, | 276 AutocompleteInput input(ASCIIToUTF16("moo"), string16::npos, string16(), |
273 false, true, AutocompleteInput::ALL_MATCHES); | 277 GURL(), true, false, true, |
| 278 AutocompleteInput::ALL_MATCHES); |
274 RunQuery(input, false); | 279 RunQuery(input, false); |
275 | 280 |
276 // Run a query that is found on a search result page and on the main page | 281 // Run a query that is found on a search result page and on the main page |
277 // of the default search engine. The result should be the main page, the | 282 // of the default search engine. The result should be the main page, the |
278 // SRP should be culled. | 283 // SRP should be culled. |
279 const ACMatches& m = matches(); | 284 const ACMatches& m = matches(); |
280 ASSERT_EQ(1U, m.size()); | 285 ASSERT_EQ(1U, m.size()); |
281 EXPECT_EQ(test_entries[4].url, m[0].destination_url.spec()); | 286 EXPECT_EQ(test_entries[4].url, m[0].destination_url.spec()); |
282 } | 287 } |
283 | 288 |
284 } // namespace | 289 } // namespace |
OLD | NEW |