OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/string_util.h" | 5 #include "base/string_util.h" |
6 #include "base/time.h" | 6 #include "base/time.h" |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "chrome/browser/autocomplete/autocomplete_match.h" | 9 #include "chrome/browser/autocomplete/autocomplete_match.h" |
10 #include "chrome/browser/autocomplete/search_provider.h" | 10 #include "chrome/browser/autocomplete/search_provider.h" |
11 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 default_t_url_->SetSuggestionsURL("http://defaultturl2/{searchTerms}", 0, 0); | 109 default_t_url_->SetSuggestionsURL("http://defaultturl2/{searchTerms}", 0, 0); |
110 turl_model->Add(default_t_url_); | 110 turl_model->Add(default_t_url_); |
111 turl_model->SetDefaultSearchProvider(default_t_url_); | 111 turl_model->SetDefaultSearchProvider(default_t_url_); |
112 TemplateURLID default_provider_id = default_t_url_->id(); | 112 TemplateURLID default_provider_id = default_t_url_->id(); |
113 ASSERT_NE(0, default_provider_id); | 113 ASSERT_NE(0, default_provider_id); |
114 | 114 |
115 // Add url1, with search term term1_. | 115 // Add url1, with search term term1_. |
116 HistoryService* history = | 116 HistoryService* history = |
117 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 117 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
118 term1_url_ = GURL(default_t_url_->url()->ReplaceSearchTerms( | 118 term1_url_ = GURL(default_t_url_->url()->ReplaceSearchTerms( |
119 *default_t_url_, UTF16ToWide(term1_), 0, std::wstring())); | 119 *default_t_url_, term1_, 0, string16())); |
120 history->AddPageWithDetails(term1_url_, string16(), 1, 1, | 120 history->AddPageWithDetails(term1_url_, string16(), 1, 1, |
121 base::Time::Now(), false, | 121 base::Time::Now(), false, |
122 history::SOURCE_BROWSED); | 122 history::SOURCE_BROWSED); |
123 history->SetKeywordSearchTermsForURL(term1_url_, default_t_url_->id(), | 123 history->SetKeywordSearchTermsForURL(term1_url_, default_t_url_->id(), |
124 term1_); | 124 term1_); |
125 | 125 |
126 // Create another TemplateURL. | 126 // Create another TemplateURL. |
127 keyword_t_url_ = new TemplateURL(); | 127 keyword_t_url_ = new TemplateURL(); |
128 keyword_t_url_->set_keyword(L"k"); | 128 keyword_t_url_->set_keyword(ASCIIToUTF16("k")); |
129 keyword_t_url_->SetURL("http://keyword/{searchTerms}", 0, 0); | 129 keyword_t_url_->SetURL("http://keyword/{searchTerms}", 0, 0); |
130 keyword_t_url_->SetSuggestionsURL("http://suggest_keyword/{searchTerms}", 0, | 130 keyword_t_url_->SetSuggestionsURL("http://suggest_keyword/{searchTerms}", 0, |
131 0); | 131 0); |
132 profile_.GetTemplateURLModel()->Add(keyword_t_url_); | 132 profile_.GetTemplateURLModel()->Add(keyword_t_url_); |
133 ASSERT_NE(0, keyword_t_url_->id()); | 133 ASSERT_NE(0, keyword_t_url_->id()); |
134 | 134 |
135 // Add a page and search term for keyword_t_url_. | 135 // Add a page and search term for keyword_t_url_. |
136 keyword_url_ = GURL(keyword_t_url_->url()->ReplaceSearchTerms( | 136 keyword_url_ = GURL(keyword_t_url_->url()->ReplaceSearchTerms( |
137 *keyword_t_url_, UTF16ToWide(keyword_term_), 0, std::wstring())); | 137 *keyword_t_url_, keyword_term_, 0, string16())); |
138 history->AddPageWithDetails(keyword_url_, string16(), 1, 1, | 138 history->AddPageWithDetails(keyword_url_, string16(), 1, 1, |
139 base::Time::Now(), false, | 139 base::Time::Now(), false, |
140 history::SOURCE_BROWSED); | 140 history::SOURCE_BROWSED); |
141 history->SetKeywordSearchTermsForURL(keyword_url_, keyword_t_url_->id(), | 141 history->SetKeywordSearchTermsForURL(keyword_url_, keyword_t_url_->id(), |
142 keyword_term_); | 142 keyword_term_); |
143 | 143 |
144 // Keywords are updated by the InMemoryHistoryBackend only after the message | 144 // Keywords are updated by the InMemoryHistoryBackend only after the message |
145 // has been processed on the history thread. Block until history processes all | 145 // has been processed on the history thread. Block until history processes all |
146 // requests to ensure the InMemoryDatabase is the state we expect it. | 146 // requests to ensure the InMemoryDatabase is the state we expect it. |
147 profile_.BlockUntilHistoryProcessesPendingRequests(); | 147 profile_.BlockUntilHistoryProcessesPendingRequests(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 string16 term = term1_.substr(0, term1_.size() - 1); | 220 string16 term = term1_.substr(0, term1_.size() - 1); |
221 QueryForInput(term, false); | 221 QueryForInput(term, false); |
222 | 222 |
223 // Make sure the default providers suggest service was queried. | 223 // Make sure the default providers suggest service was queried. |
224 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 224 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
225 SearchProvider::kDefaultProviderURLFetcherID); | 225 SearchProvider::kDefaultProviderURLFetcherID); |
226 ASSERT_TRUE(fetcher); | 226 ASSERT_TRUE(fetcher); |
227 | 227 |
228 // And the URL matches what we expected. | 228 // And the URL matches what we expected. |
229 GURL expected_url = GURL(default_t_url_->suggestions_url()-> | 229 GURL expected_url = GURL(default_t_url_->suggestions_url()-> |
230 ReplaceSearchTerms(*default_t_url_, UTF16ToWide(term), | 230 ReplaceSearchTerms(*default_t_url_, term, 0, string16())); |
231 0, std::wstring())); | |
232 ASSERT_TRUE(fetcher->original_url() == expected_url); | 231 ASSERT_TRUE(fetcher->original_url() == expected_url); |
233 | 232 |
234 // Tell the SearchProvider the suggest query is done. | 233 // Tell the SearchProvider the suggest query is done. |
235 fetcher->delegate()->OnURLFetchComplete( | 234 fetcher->delegate()->OnURLFetchComplete( |
236 fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), | 235 fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), |
237 std::string()); | 236 std::string()); |
238 fetcher = NULL; | 237 fetcher = NULL; |
239 | 238 |
240 // Run till the history results complete. | 239 // Run till the history results complete. |
241 RunTillProviderDone(); | 240 RunTillProviderDone(); |
242 | 241 |
243 // The SearchProvider is done. Make sure it has a result for the history | 242 // The SearchProvider is done. Make sure it has a result for the history |
244 // term term1. | 243 // term term1. |
245 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_); | 244 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_); |
246 EXPECT_TRUE(!term1_match.destination_url.is_empty()); | 245 EXPECT_TRUE(!term1_match.destination_url.is_empty()); |
247 // Term1 should have a description. | 246 // Term1 should have a description. |
248 EXPECT_FALSE(term1_match.description.empty()); | 247 EXPECT_FALSE(term1_match.description.empty()); |
249 | 248 |
250 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 249 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
251 *default_t_url_, UTF16ToWide(term), 0, std::wstring())); | 250 *default_t_url_, term, 0, string16())); |
252 AutocompleteMatch what_you_typed_match = | 251 AutocompleteMatch what_you_typed_match = |
253 FindMatchWithDestination(what_you_typed_url); | 252 FindMatchWithDestination(what_you_typed_url); |
254 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); | 253 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); |
255 EXPECT_TRUE(what_you_typed_match.description.empty()); | 254 EXPECT_TRUE(what_you_typed_match.description.empty()); |
256 | 255 |
257 // The match for term1 should be more relevant than the what you typed result. | 256 // The match for term1 should be more relevant than the what you typed result. |
258 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance); | 257 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance); |
259 } | 258 } |
260 | 259 |
261 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { | 260 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { |
262 string16 term = term1_.substr(0, term1_.size() - 1); | 261 string16 term = term1_.substr(0, term1_.size() - 1); |
263 QueryForInput(term, true); | 262 QueryForInput(term, true); |
264 | 263 |
265 ASSERT_FALSE(provider_->matches().empty()); | 264 ASSERT_FALSE(provider_->matches().empty()); |
266 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | 265 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
267 provider_->matches()[0].type); | 266 provider_->matches()[0].type); |
268 } | 267 } |
269 | 268 |
270 // Issues a query that matches the registered keyword and makes sure history | 269 // Issues a query that matches the registered keyword and makes sure history |
271 // is queried as well as URLFetchers getting created. | 270 // is queried as well as URLFetchers getting created. |
272 TEST_F(SearchProviderTest, QueryKeywordProvider) { | 271 TEST_F(SearchProviderTest, QueryKeywordProvider) { |
273 string16 term = keyword_term_.substr(0, keyword_term_.size() - 1); | 272 string16 term = keyword_term_.substr(0, keyword_term_.size() - 1); |
274 QueryForInput(WideToUTF16(keyword_t_url_->keyword()) + | 273 QueryForInput(keyword_t_url_->keyword() + |
275 UTF8ToUTF16(" ") + term, false); | 274 UTF8ToUTF16(" ") + term, false); |
276 | 275 |
277 // Make sure the default providers suggest service was queried. | 276 // Make sure the default providers suggest service was queried. |
278 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( | 277 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( |
279 SearchProvider::kDefaultProviderURLFetcherID); | 278 SearchProvider::kDefaultProviderURLFetcherID); |
280 ASSERT_TRUE(default_fetcher); | 279 ASSERT_TRUE(default_fetcher); |
281 | 280 |
282 // Tell the SearchProvider the default suggest query is done. | 281 // Tell the SearchProvider the default suggest query is done. |
283 default_fetcher->delegate()->OnURLFetchComplete( | 282 default_fetcher->delegate()->OnURLFetchComplete( |
284 default_fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), | 283 default_fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), |
285 std::string()); | 284 std::string()); |
286 default_fetcher = NULL; | 285 default_fetcher = NULL; |
287 | 286 |
288 // Make sure the keyword providers suggest service was queried. | 287 // Make sure the keyword providers suggest service was queried. |
289 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( | 288 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( |
290 SearchProvider::kKeywordProviderURLFetcherID); | 289 SearchProvider::kKeywordProviderURLFetcherID); |
291 ASSERT_TRUE(keyword_fetcher); | 290 ASSERT_TRUE(keyword_fetcher); |
292 | 291 |
293 // And the URL matches what we expected. | 292 // And the URL matches what we expected. |
294 GURL expected_url = GURL(keyword_t_url_->suggestions_url()-> | 293 GURL expected_url = GURL(keyword_t_url_->suggestions_url()-> |
295 ReplaceSearchTerms(*keyword_t_url_, UTF16ToWide(term), 0, | 294 ReplaceSearchTerms(*keyword_t_url_, term, 0, string16())); |
296 std::wstring())); | |
297 ASSERT_TRUE(keyword_fetcher->original_url() == expected_url); | 295 ASSERT_TRUE(keyword_fetcher->original_url() == expected_url); |
298 | 296 |
299 // Tell the SearchProvider the keyword suggest query is done. | 297 // Tell the SearchProvider the keyword suggest query is done. |
300 keyword_fetcher->delegate()->OnURLFetchComplete( | 298 keyword_fetcher->delegate()->OnURLFetchComplete( |
301 keyword_fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), | 299 keyword_fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), |
302 std::string()); | 300 std::string()); |
303 keyword_fetcher = NULL; | 301 keyword_fetcher = NULL; |
304 | 302 |
305 // Run till the history results complete. | 303 // Run till the history results complete. |
306 RunTillProviderDone(); | 304 RunTillProviderDone(); |
307 | 305 |
308 // The SearchProvider is done. Make sure it has a result for the history | 306 // The SearchProvider is done. Make sure it has a result for the history |
309 // term keyword. | 307 // term keyword. |
310 AutocompleteMatch match = FindMatchWithDestination(keyword_url_); | 308 AutocompleteMatch match = FindMatchWithDestination(keyword_url_); |
311 ASSERT_TRUE(!match.destination_url.is_empty()); | 309 ASSERT_TRUE(!match.destination_url.is_empty()); |
312 | 310 |
313 // The match should have a TemplateURL. | 311 // The match should have a TemplateURL. |
314 EXPECT_TRUE(match.template_url); | 312 EXPECT_TRUE(match.template_url); |
315 | 313 |
316 // The fill into edit should contain the keyword. | 314 // The fill into edit should contain the keyword. |
317 EXPECT_EQ(keyword_t_url_->keyword() + L" " + UTF16ToWide(keyword_term_), | 315 EXPECT_EQ(UTF16ToWideHack(keyword_t_url_->keyword()) + |
| 316 L" " + UTF16ToWide(keyword_term_), |
318 match.fill_into_edit); | 317 match.fill_into_edit); |
319 } | 318 } |
320 | 319 |
321 TEST_F(SearchProviderTest, DontSendPrivateDataToSuggest) { | 320 TEST_F(SearchProviderTest, DontSendPrivateDataToSuggest) { |
322 // None of the following input strings should be sent to the suggest server, | 321 // None of the following input strings should be sent to the suggest server, |
323 // because they may contain private data. | 322 // because they may contain private data. |
324 const char* inputs[] = { | 323 const char* inputs[] = { |
325 "username:password", | 324 "username:password", |
326 "http://username:password", | 325 "http://username:password", |
327 "https://username:password", | 326 "https://username:password", |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 // Tell the provider instant is done. | 362 // Tell the provider instant is done. |
364 provider_->FinalizeInstantQuery(L"foo", L"bar"); | 363 provider_->FinalizeInstantQuery(L"foo", L"bar"); |
365 | 364 |
366 // The provider should now be done. | 365 // The provider should now be done. |
367 EXPECT_TRUE(provider_->done()); | 366 EXPECT_TRUE(provider_->done()); |
368 | 367 |
369 // There should be two matches, one for what you typed, the other for | 368 // There should be two matches, one for what you typed, the other for |
370 // 'foobar'. | 369 // 'foobar'. |
371 EXPECT_EQ(2u, provider_->matches().size()); | 370 EXPECT_EQ(2u, provider_->matches().size()); |
372 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 371 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
373 *default_t_url_, L"foobar", 0, std::wstring())); | 372 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); |
374 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); | 373 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); |
375 EXPECT_TRUE(!instant_match.destination_url.is_empty()); | 374 EXPECT_TRUE(!instant_match.destination_url.is_empty()); |
376 | 375 |
377 // And the 'foobar' match should have a description. | 376 // And the 'foobar' match should have a description. |
378 EXPECT_FALSE(instant_match.description.empty()); | 377 EXPECT_FALSE(instant_match.description.empty()); |
379 | 378 |
380 // Make sure the what you typed match has no description. | 379 // Make sure the what you typed match has no description. |
381 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 380 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
382 *default_t_url_, L"foo", 0, std::wstring())); | 381 *default_t_url_, ASCIIToUTF16("foo"), 0, string16())); |
383 AutocompleteMatch what_you_typed_match = | 382 AutocompleteMatch what_you_typed_match = |
384 FindMatchWithDestination(what_you_typed_url); | 383 FindMatchWithDestination(what_you_typed_url); |
385 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); | 384 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); |
386 EXPECT_TRUE(what_you_typed_match.description.empty()); | 385 EXPECT_TRUE(what_you_typed_match.description.empty()); |
387 | 386 |
388 // The instant search should be more relevant. | 387 // The instant search should be more relevant. |
389 EXPECT_GT(instant_match.relevance, what_you_typed_match.relevance); | 388 EXPECT_GT(instant_match.relevance, what_you_typed_match.relevance); |
390 } | 389 } |
391 | 390 |
392 // Make sure that if FinalizeInstantQuery is invoked before suggest results | 391 // Make sure that if FinalizeInstantQuery is invoked before suggest results |
393 // return, the suggest text from FinalizeInstantQuery is remembered. | 392 // return, the suggest text from FinalizeInstantQuery is remembered. |
394 TEST_F(SearchProviderTest, RememberInstantQuery) { | 393 TEST_F(SearchProviderTest, RememberInstantQuery) { |
395 PrefService* service = profile_.GetPrefs(); | 394 PrefService* service = profile_.GetPrefs(); |
396 service->SetBoolean(prefs::kInstantEnabled, true); | 395 service->SetBoolean(prefs::kInstantEnabled, true); |
397 | 396 |
398 QueryForInput(ASCIIToUTF16("foo"), false); | 397 QueryForInput(ASCIIToUTF16("foo"), false); |
399 | 398 |
400 // Finalize the instant query immediately. | 399 // Finalize the instant query immediately. |
401 provider_->FinalizeInstantQuery(L"foo", L"bar"); | 400 provider_->FinalizeInstantQuery(L"foo", L"bar"); |
402 | 401 |
403 // There should be two matches, one for what you typed, the other for | 402 // There should be two matches, one for what you typed, the other for |
404 // 'foobar'. | 403 // 'foobar'. |
405 EXPECT_EQ(2u, provider_->matches().size()); | 404 EXPECT_EQ(2u, provider_->matches().size()); |
406 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 405 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( |
407 *default_t_url_, L"foobar", 0, std::wstring())); | 406 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); |
408 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); | 407 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); |
409 EXPECT_FALSE(instant_match.destination_url.is_empty()); | 408 EXPECT_FALSE(instant_match.destination_url.is_empty()); |
410 | 409 |
411 // Wait until history and the suggest query complete. | 410 // Wait until history and the suggest query complete. |
412 profile_.BlockUntilHistoryProcessesPendingRequests(); | 411 profile_.BlockUntilHistoryProcessesPendingRequests(); |
413 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 412 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
414 | 413 |
415 // Provider should be done. | 414 // Provider should be done. |
416 EXPECT_TRUE(provider_->done()); | 415 EXPECT_TRUE(provider_->done()); |
417 | 416 |
418 // There should be two matches, one for what you typed, the other for | 417 // There should be two matches, one for what you typed, the other for |
419 // 'foobar'. | 418 // 'foobar'. |
420 EXPECT_EQ(2u, provider_->matches().size()); | 419 EXPECT_EQ(2u, provider_->matches().size()); |
421 instant_match = FindMatchWithDestination(instant_url); | 420 instant_match = FindMatchWithDestination(instant_url); |
422 EXPECT_FALSE(instant_match.destination_url.is_empty()); | 421 EXPECT_FALSE(instant_match.destination_url.is_empty()); |
423 | 422 |
424 // And the 'foobar' match should have a description. | 423 // And the 'foobar' match should have a description. |
425 EXPECT_FALSE(instant_match.description.empty()); | 424 EXPECT_FALSE(instant_match.description.empty()); |
426 } | 425 } |
OLD | NEW |