Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: chrome/browser/autocomplete/search_provider_unittest.cc

Issue 6291003: Revert 71485 - Remove wstring from TemplateURL and friends.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 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"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
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_, term1_, 0, string16())); 119 *default_t_url_, UTF16ToWide(term1_), 0, std::wstring()));
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(ASCIIToUTF16("k")); 128 keyword_t_url_->set_keyword(L"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_, keyword_term_, 0, string16())); 137 *keyword_t_url_, UTF16ToWide(keyword_term_), 0, std::wstring()));
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
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_, term, 0, string16())); 230 ReplaceSearchTerms(*default_t_url_, UTF16ToWide(term),
231 0, std::wstring()));
231 ASSERT_TRUE(fetcher->original_url() == expected_url); 232 ASSERT_TRUE(fetcher->original_url() == expected_url);
232 233
233 // Tell the SearchProvider the suggest query is done. 234 // Tell the SearchProvider the suggest query is done.
234 fetcher->delegate()->OnURLFetchComplete( 235 fetcher->delegate()->OnURLFetchComplete(
235 fetcher, GURL(), net::URLRequestStatus(), 200, ResponseCookies(), 236 fetcher, GURL(), net::URLRequestStatus(), 200, ResponseCookies(),
236 std::string()); 237 std::string());
237 fetcher = NULL; 238 fetcher = NULL;
238 239
239 // Run till the history results complete. 240 // Run till the history results complete.
240 RunTillProviderDone(); 241 RunTillProviderDone();
241 242
242 // The SearchProvider is done. Make sure it has a result for the history 243 // The SearchProvider is done. Make sure it has a result for the history
243 // term term1. 244 // term term1.
244 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_); 245 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_);
245 EXPECT_TRUE(!term1_match.destination_url.is_empty()); 246 EXPECT_TRUE(!term1_match.destination_url.is_empty());
246 // Term1 should have a description. 247 // Term1 should have a description.
247 EXPECT_FALSE(term1_match.description.empty()); 248 EXPECT_FALSE(term1_match.description.empty());
248 249
249 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 250 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms(
250 *default_t_url_, term, 0, string16())); 251 *default_t_url_, UTF16ToWide(term), 0, std::wstring()));
251 AutocompleteMatch what_you_typed_match = 252 AutocompleteMatch what_you_typed_match =
252 FindMatchWithDestination(what_you_typed_url); 253 FindMatchWithDestination(what_you_typed_url);
253 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); 254 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty());
254 EXPECT_TRUE(what_you_typed_match.description.empty()); 255 EXPECT_TRUE(what_you_typed_match.description.empty());
255 256
256 // The match for term1 should be more relevant than the what you typed result. 257 // The match for term1 should be more relevant than the what you typed result.
257 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance); 258 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance);
258 } 259 }
259 260
260 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { 261 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) {
261 string16 term = term1_.substr(0, term1_.size() - 1); 262 string16 term = term1_.substr(0, term1_.size() - 1);
262 QueryForInput(term, true); 263 QueryForInput(term, true);
263 264
264 ASSERT_FALSE(provider_->matches().empty()); 265 ASSERT_FALSE(provider_->matches().empty());
265 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, 266 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
266 provider_->matches()[0].type); 267 provider_->matches()[0].type);
267 } 268 }
268 269
269 // Issues a query that matches the registered keyword and makes sure history 270 // Issues a query that matches the registered keyword and makes sure history
270 // is queried as well as URLFetchers getting created. 271 // is queried as well as URLFetchers getting created.
271 TEST_F(SearchProviderTest, QueryKeywordProvider) { 272 TEST_F(SearchProviderTest, QueryKeywordProvider) {
272 string16 term = keyword_term_.substr(0, keyword_term_.size() - 1); 273 string16 term = keyword_term_.substr(0, keyword_term_.size() - 1);
273 QueryForInput(keyword_t_url_->keyword() + 274 QueryForInput(WideToUTF16(keyword_t_url_->keyword()) +
274 UTF8ToUTF16(" ") + term, false); 275 UTF8ToUTF16(" ") + term, false);
275 276
276 // Make sure the default providers suggest service was queried. 277 // Make sure the default providers suggest service was queried.
277 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( 278 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID(
278 SearchProvider::kDefaultProviderURLFetcherID); 279 SearchProvider::kDefaultProviderURLFetcherID);
279 ASSERT_TRUE(default_fetcher); 280 ASSERT_TRUE(default_fetcher);
280 281
281 // Tell the SearchProvider the default suggest query is done. 282 // Tell the SearchProvider the default suggest query is done.
282 default_fetcher->delegate()->OnURLFetchComplete( 283 default_fetcher->delegate()->OnURLFetchComplete(
283 default_fetcher, GURL(), net::URLRequestStatus(), 200, ResponseCookies(), 284 default_fetcher, GURL(), net::URLRequestStatus(), 200, ResponseCookies(),
284 std::string()); 285 std::string());
285 default_fetcher = NULL; 286 default_fetcher = NULL;
286 287
287 // Make sure the keyword providers suggest service was queried. 288 // Make sure the keyword providers suggest service was queried.
288 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( 289 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID(
289 SearchProvider::kKeywordProviderURLFetcherID); 290 SearchProvider::kKeywordProviderURLFetcherID);
290 ASSERT_TRUE(keyword_fetcher); 291 ASSERT_TRUE(keyword_fetcher);
291 292
292 // And the URL matches what we expected. 293 // And the URL matches what we expected.
293 GURL expected_url = GURL(keyword_t_url_->suggestions_url()-> 294 GURL expected_url = GURL(keyword_t_url_->suggestions_url()->
294 ReplaceSearchTerms(*keyword_t_url_, term, 0, string16())); 295 ReplaceSearchTerms(*keyword_t_url_, UTF16ToWide(term), 0,
296 std::wstring()));
295 ASSERT_TRUE(keyword_fetcher->original_url() == expected_url); 297 ASSERT_TRUE(keyword_fetcher->original_url() == expected_url);
296 298
297 // Tell the SearchProvider the keyword suggest query is done. 299 // Tell the SearchProvider the keyword suggest query is done.
298 keyword_fetcher->delegate()->OnURLFetchComplete( 300 keyword_fetcher->delegate()->OnURLFetchComplete(
299 keyword_fetcher, GURL(), net::URLRequestStatus(), 200, ResponseCookies(), 301 keyword_fetcher, GURL(), net::URLRequestStatus(), 200, ResponseCookies(),
300 std::string()); 302 std::string());
301 keyword_fetcher = NULL; 303 keyword_fetcher = NULL;
302 304
303 // Run till the history results complete. 305 // Run till the history results complete.
304 RunTillProviderDone(); 306 RunTillProviderDone();
305 307
306 // The SearchProvider is done. Make sure it has a result for the history 308 // The SearchProvider is done. Make sure it has a result for the history
307 // term keyword. 309 // term keyword.
308 AutocompleteMatch match = FindMatchWithDestination(keyword_url_); 310 AutocompleteMatch match = FindMatchWithDestination(keyword_url_);
309 ASSERT_TRUE(!match.destination_url.is_empty()); 311 ASSERT_TRUE(!match.destination_url.is_empty());
310 312
311 // The match should have a TemplateURL. 313 // The match should have a TemplateURL.
312 EXPECT_TRUE(match.template_url); 314 EXPECT_TRUE(match.template_url);
313 315
314 // The fill into edit should contain the keyword. 316 // The fill into edit should contain the keyword.
315 EXPECT_EQ(UTF16ToWideHack(keyword_t_url_->keyword()) + 317 EXPECT_EQ(keyword_t_url_->keyword() + L" " + UTF16ToWide(keyword_term_),
316 L" " + UTF16ToWide(keyword_term_),
317 match.fill_into_edit); 318 match.fill_into_edit);
318 } 319 }
319 320
320 TEST_F(SearchProviderTest, DontSendPrivateDataToSuggest) { 321 TEST_F(SearchProviderTest, DontSendPrivateDataToSuggest) {
321 // None of the following input strings should be sent to the suggest server, 322 // None of the following input strings should be sent to the suggest server,
322 // because they may contain private data. 323 // because they may contain private data.
323 const char* inputs[] = { 324 const char* inputs[] = {
324 "username:password", 325 "username:password",
325 "http://username:password", 326 "http://username:password",
326 "https://username:password", 327 "https://username:password",
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 // Tell the provider instant is done. 363 // Tell the provider instant is done.
363 provider_->FinalizeInstantQuery(L"foo", L"bar"); 364 provider_->FinalizeInstantQuery(L"foo", L"bar");
364 365
365 // The provider should now be done. 366 // The provider should now be done.
366 EXPECT_TRUE(provider_->done()); 367 EXPECT_TRUE(provider_->done());
367 368
368 // There should be two matches, one for what you typed, the other for 369 // There should be two matches, one for what you typed, the other for
369 // 'foobar'. 370 // 'foobar'.
370 EXPECT_EQ(2u, provider_->matches().size()); 371 EXPECT_EQ(2u, provider_->matches().size());
371 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 372 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms(
372 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); 373 *default_t_url_, L"foobar", 0, std::wstring()));
373 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); 374 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url);
374 EXPECT_TRUE(!instant_match.destination_url.is_empty()); 375 EXPECT_TRUE(!instant_match.destination_url.is_empty());
375 376
376 // And the 'foobar' match should have a description. 377 // And the 'foobar' match should have a description.
377 EXPECT_FALSE(instant_match.description.empty()); 378 EXPECT_FALSE(instant_match.description.empty());
378 379
379 // Make sure the what you typed match has no description. 380 // Make sure the what you typed match has no description.
380 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 381 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms(
381 *default_t_url_, ASCIIToUTF16("foo"), 0, string16())); 382 *default_t_url_, L"foo", 0, std::wstring()));
382 AutocompleteMatch what_you_typed_match = 383 AutocompleteMatch what_you_typed_match =
383 FindMatchWithDestination(what_you_typed_url); 384 FindMatchWithDestination(what_you_typed_url);
384 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); 385 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty());
385 EXPECT_TRUE(what_you_typed_match.description.empty()); 386 EXPECT_TRUE(what_you_typed_match.description.empty());
386 387
387 // The instant search should be more relevant. 388 // The instant search should be more relevant.
388 EXPECT_GT(instant_match.relevance, what_you_typed_match.relevance); 389 EXPECT_GT(instant_match.relevance, what_you_typed_match.relevance);
389 } 390 }
390 391
391 // Make sure that if FinalizeInstantQuery is invoked before suggest results 392 // Make sure that if FinalizeInstantQuery is invoked before suggest results
392 // return, the suggest text from FinalizeInstantQuery is remembered. 393 // return, the suggest text from FinalizeInstantQuery is remembered.
393 TEST_F(SearchProviderTest, RememberInstantQuery) { 394 TEST_F(SearchProviderTest, RememberInstantQuery) {
394 PrefService* service = profile_.GetPrefs(); 395 PrefService* service = profile_.GetPrefs();
395 service->SetBoolean(prefs::kInstantEnabled, true); 396 service->SetBoolean(prefs::kInstantEnabled, true);
396 397
397 QueryForInput(ASCIIToUTF16("foo"), false); 398 QueryForInput(ASCIIToUTF16("foo"), false);
398 399
399 // Finalize the instant query immediately. 400 // Finalize the instant query immediately.
400 provider_->FinalizeInstantQuery(L"foo", L"bar"); 401 provider_->FinalizeInstantQuery(L"foo", L"bar");
401 402
402 // There should be two matches, one for what you typed, the other for 403 // There should be two matches, one for what you typed, the other for
403 // 'foobar'. 404 // 'foobar'.
404 EXPECT_EQ(2u, provider_->matches().size()); 405 EXPECT_EQ(2u, provider_->matches().size());
405 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 406 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms(
406 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); 407 *default_t_url_, L"foobar", 0, std::wstring()));
407 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); 408 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url);
408 EXPECT_FALSE(instant_match.destination_url.is_empty()); 409 EXPECT_FALSE(instant_match.destination_url.is_empty());
409 410
410 // Wait until history and the suggest query complete. 411 // Wait until history and the suggest query complete.
411 profile_.BlockUntilHistoryProcessesPendingRequests(); 412 profile_.BlockUntilHistoryProcessesPendingRequests();
412 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); 413 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
413 414
414 // Provider should be done. 415 // Provider should be done.
415 EXPECT_TRUE(provider_->done()); 416 EXPECT_TRUE(provider_->done());
416 417
417 // There should be two matches, one for what you typed, the other for 418 // There should be two matches, one for what you typed, the other for
418 // 'foobar'. 419 // 'foobar'.
419 EXPECT_EQ(2u, provider_->matches().size()); 420 EXPECT_EQ(2u, provider_->matches().size());
420 instant_match = FindMatchWithDestination(instant_url); 421 instant_match = FindMatchWithDestination(instant_url);
421 EXPECT_FALSE(instant_match.destination_url.is_empty()); 422 EXPECT_FALSE(instant_match.destination_url.is_empty());
422 423
423 // And the 'foobar' match should have a description. 424 // And the 'foobar' match should have a description.
424 EXPECT_FALSE(instant_match.description.empty()); 425 EXPECT_FALSE(instant_match.description.empty());
425 } 426 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/search_provider.cc ('k') | chrome/browser/automation/testing_automation_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698