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

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

Issue 7314018: Don't autocomplete searches of >1 word if they've only been visited once and the user has not yet... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 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 "chrome/browser/autocomplete/search_provider.h" 5 #include "chrome/browser/autocomplete/search_provider.h"
6 6
7 #include "base/string_util.h" 7 #include "base/string_util.h"
8 #include "base/time.h" 8 #include "base/time.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 quit_when_done_(false) { 45 quit_when_done_(false) {
46 io_thread_.Start(); 46 io_thread_.Start();
47 } 47 }
48 48
49 // See description above class for what this registers. 49 // See description above class for what this registers.
50 virtual void SetUp(); 50 virtual void SetUp();
51 51
52 virtual void TearDown(); 52 virtual void TearDown();
53 53
54 protected: 54 protected:
55 // Returns an AutocompleteMatch in provider_'s set of matches that matches 55 // Adds a search for |term|, using the engine |t_url| to the history, and
56 // |url|. If there is no matching URL, an empty match is returned. 56 // returns the URL for that search.
57 AutocompleteMatch FindMatchWithDestination(const GURL& url); 57 GURL AddSearchToHistory(TemplateURL* t_url, string16 term, int visit_count);
58
59 // Looks for a match in |provider_| with destination |url|. Sets |match| to
60 // it if found. Returns whether |match| was set.
61 bool FindMatchWithDestination(const GURL& url, AutocompleteMatch* match);
58 62
59 // ACProviderListener method. If we're waiting for the provider to finish, 63 // ACProviderListener method. If we're waiting for the provider to finish,
60 // this exits the message loop. 64 // this exits the message loop.
61 virtual void OnProviderUpdate(bool updated_matches); 65 virtual void OnProviderUpdate(bool updated_matches);
62 66
63 // Runs a nested message loop until provider_ is done. The message loop is 67 // Runs a nested message loop until provider_ is done. The message loop is
64 // exited by way of OnProviderUPdate. 68 // exited by way of OnProviderUPdate.
65 void RunTillProviderDone(); 69 void RunTillProviderDone();
66 70
67 // Invokes Start on provider_, then runs all pending tasks. 71 // Invokes Start on provider_, then runs all pending tasks.
68 void QueryForInput(const string16& text, 72 void QueryForInput(const string16& text, bool prevent_inline_autocomplete);
69 bool prevent_inline_autocomplete, 73
70 bool minimal_changes); 74 // Calls QueryForInput(), finishes any suggest query, then if |wyt_match| is
75 // non-NULL, sets it to the "what you typed" entry for |text|.
76 void QueryForInputAndSetWYTMatch(const string16& text,
77 AutocompleteMatch* wyt_match);
71 78
72 // Notifies the URLFetcher for the suggest query corresponding to the default 79 // Notifies the URLFetcher for the suggest query corresponding to the default
73 // search provider that it's done. 80 // search provider that it's done.
74 // Be sure and wrap calls to this in ASSERT_NO_FATAL_FAILURE. 81 // Be sure and wrap calls to this in ASSERT_NO_FATAL_FAILURE.
75 void FinishDefaultSuggestQuery(); 82 void FinishDefaultSuggestQuery();
76 83
77 // See description above class for details of these fields. 84 // See description above class for details of these fields.
78 TemplateURL* default_t_url_; 85 TemplateURL* default_t_url_;
79 const string16 term1_; 86 const string16 term1_;
80 GURL term1_url_; 87 GURL term1_url_;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 default_t_url_ = new TemplateURL(); 123 default_t_url_ = new TemplateURL();
117 default_t_url_->SetURL("http://defaultturl/{searchTerms}", 0, 0); 124 default_t_url_->SetURL("http://defaultturl/{searchTerms}", 0, 0);
118 default_t_url_->SetSuggestionsURL("http://defaultturl2/{searchTerms}", 0, 0); 125 default_t_url_->SetSuggestionsURL("http://defaultturl2/{searchTerms}", 0, 0);
119 default_t_url_->set_short_name(ASCIIToUTF16("t")); 126 default_t_url_->set_short_name(ASCIIToUTF16("t"));
120 turl_model->Add(default_t_url_); 127 turl_model->Add(default_t_url_);
121 turl_model->SetDefaultSearchProvider(default_t_url_); 128 turl_model->SetDefaultSearchProvider(default_t_url_);
122 TemplateURLID default_provider_id = default_t_url_->id(); 129 TemplateURLID default_provider_id = default_t_url_->id();
123 ASSERT_NE(0, default_provider_id); 130 ASSERT_NE(0, default_provider_id);
124 131
125 // Add url1, with search term term1_. 132 // Add url1, with search term term1_.
126 HistoryService* history = 133 term1_url_ = AddSearchToHistory(default_t_url_, term1_, 1);
127 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS);
128 term1_url_ = GURL(default_t_url_->url()->ReplaceSearchTerms(
129 *default_t_url_, term1_, 0, string16()));
130 history->AddPageWithDetails(term1_url_, string16(), 1, 1,
131 base::Time::Now(), false,
132 history::SOURCE_BROWSED);
133 history->SetKeywordSearchTermsForURL(term1_url_, default_t_url_->id(),
134 term1_);
135 134
136 // Create another TemplateURL. 135 // Create another TemplateURL.
137 keyword_t_url_ = new TemplateURL(); 136 keyword_t_url_ = new TemplateURL();
138 keyword_t_url_->set_keyword(ASCIIToUTF16("k")); 137 keyword_t_url_->set_keyword(ASCIIToUTF16("k"));
139 keyword_t_url_->set_short_name(ASCIIToUTF16("k")); 138 keyword_t_url_->set_short_name(ASCIIToUTF16("k"));
140 keyword_t_url_->SetURL("http://keyword/{searchTerms}", 0, 0); 139 keyword_t_url_->SetURL("http://keyword/{searchTerms}", 0, 0);
141 keyword_t_url_->SetSuggestionsURL("http://suggest_keyword/{searchTerms}", 0, 140 keyword_t_url_->SetSuggestionsURL("http://suggest_keyword/{searchTerms}", 0,
142 0); 141 0);
143 turl_model->Add(keyword_t_url_); 142 turl_model->Add(keyword_t_url_);
144 ASSERT_NE(0, keyword_t_url_->id()); 143 ASSERT_NE(0, keyword_t_url_->id());
145 144
146 // Add a page and search term for keyword_t_url_. 145 // Add a page and search term for keyword_t_url_.
147 keyword_url_ = GURL(keyword_t_url_->url()->ReplaceSearchTerms( 146 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1);
148 *keyword_t_url_, keyword_term_, 0, string16()));
149 history->AddPageWithDetails(keyword_url_, string16(), 1, 1,
150 base::Time::Now(), false,
151 history::SOURCE_BROWSED);
152 history->SetKeywordSearchTermsForURL(keyword_url_, keyword_t_url_->id(),
153 keyword_term_);
154 147
155 // Keywords are updated by the InMemoryHistoryBackend only after the message 148 // Keywords are updated by the InMemoryHistoryBackend only after the message
156 // has been processed on the history thread. Block until history processes all 149 // has been processed on the history thread. Block until history processes all
157 // requests to ensure the InMemoryDatabase is the state we expect it. 150 // requests to ensure the InMemoryDatabase is the state we expect it.
158 profile_.BlockUntilHistoryProcessesPendingRequests(); 151 profile_.BlockUntilHistoryProcessesPendingRequests();
159 152
160 provider_ = new SearchProvider(this, &profile_); 153 provider_ = new SearchProvider(this, &profile_);
161 154
162 URLFetcher::set_factory(&test_factory_); 155 URLFetcher::set_factory(&test_factory_);
163 } 156 }
(...skipping 11 matching lines...) Expand all
175 168
176 quit_when_done_ = true; 169 quit_when_done_ = true;
177 #if defined(OS_MACOSX) 170 #if defined(OS_MACOSX)
178 message_loop_.Run(); 171 message_loop_.Run();
179 #else 172 #else
180 message_loop_.Run(NULL); 173 message_loop_.Run(NULL);
181 #endif 174 #endif
182 } 175 }
183 176
184 void SearchProviderTest::QueryForInput(const string16& text, 177 void SearchProviderTest::QueryForInput(const string16& text,
185 bool prevent_inline_autocomplete, 178 bool prevent_inline_autocomplete) {
186 bool minimal_changes) {
187 // Start a query. 179 // Start a query.
188 AutocompleteInput input(text, string16(), prevent_inline_autocomplete, 180 AutocompleteInput input(text, string16(), prevent_inline_autocomplete,
189 false, true, AutocompleteInput::ALL_MATCHES); 181 false, true, AutocompleteInput::ALL_MATCHES);
190 provider_->Start(input, minimal_changes); 182 provider_->Start(input, false);
191 183
192 // RunAllPending so that the task scheduled by SearchProvider to create the 184 // RunAllPending so that the task scheduled by SearchProvider to create the
193 // URLFetchers runs. 185 // URLFetchers runs.
194 message_loop_.RunAllPending(); 186 message_loop_.RunAllPending();
195 } 187 }
196 188
189 void SearchProviderTest::QueryForInputAndSetWYTMatch(
190 const string16& text,
191 AutocompleteMatch* wyt_match) {
192 QueryForInput(text, false);
193 profile_.BlockUntilHistoryProcessesPendingRequests();
194 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
195 EXPECT_NE(profile_.GetPrefs()->GetBoolean(prefs::kInstantEnabled),
196 provider_->done());
197 if (!wyt_match)
198 return;
199 ASSERT_GE(provider_->matches().size(), 1u);
200 EXPECT_TRUE(FindMatchWithDestination(GURL(
201 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, text, 0,
202 string16())), wyt_match));
203 }
204
197 void SearchProviderTest::TearDown() { 205 void SearchProviderTest::TearDown() {
198 message_loop_.RunAllPending(); 206 message_loop_.RunAllPending();
199 207
200 URLFetcher::set_factory(NULL); 208 URLFetcher::set_factory(NULL);
201 209
202 // Shutdown the provider before the profile. 210 // Shutdown the provider before the profile.
203 provider_ = NULL; 211 provider_ = NULL;
204 } 212 }
205 213
206 AutocompleteMatch SearchProviderTest::FindMatchWithDestination( 214 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url,
207 const GURL& url) { 215 string16 term,
216 int visit_count) {
217 HistoryService* history =
218 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS);
219 GURL search(t_url->url()->ReplaceSearchTerms(*t_url, term, 0, string16()));
220 static base::Time last_added_time;
221 last_added_time = std::max(base::Time::Now(),
222 last_added_time + base::TimeDelta::FromMicroseconds(1));
223 history->AddPageWithDetails(search, string16(), visit_count, visit_count,
224 last_added_time, false, history::SOURCE_BROWSED);
225 history->SetKeywordSearchTermsForURL(search, t_url->id(), term);
226 return search;
227 }
228
229 bool SearchProviderTest::FindMatchWithDestination(const GURL& url,
230 AutocompleteMatch* match) {
208 for (ACMatches::const_iterator i = provider_->matches().begin(); 231 for (ACMatches::const_iterator i = provider_->matches().begin();
209 i != provider_->matches().end(); ++i) { 232 i != provider_->matches().end(); ++i) {
210 if (i->destination_url == url) 233 if (i->destination_url == url) {
211 return *i; 234 *match = *i;
235 return true;
236 }
212 } 237 }
213 return AutocompleteMatch(NULL, 1, false, AutocompleteMatch::HISTORY_URL); 238 return false;
214 } 239 }
215 240
216 void SearchProviderTest::FinishDefaultSuggestQuery() { 241 void SearchProviderTest::FinishDefaultSuggestQuery() {
217 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( 242 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID(
218 SearchProvider::kDefaultProviderURLFetcherID); 243 SearchProvider::kDefaultProviderURLFetcherID);
219 ASSERT_TRUE(default_fetcher); 244 ASSERT_TRUE(default_fetcher);
220 245
221 // Tell the SearchProvider the default suggest query is done. 246 // Tell the SearchProvider the default suggest query is done.
222 default_fetcher->delegate()->OnURLFetchComplete( 247 default_fetcher->delegate()->OnURLFetchComplete(
223 default_fetcher, GURL(), net::URLRequestStatus(), 200, 248 default_fetcher, GURL(), net::URLRequestStatus(), 200,
224 net::ResponseCookies(), std::string()); 249 net::ResponseCookies(), std::string());
225 } 250 }
226 251
227 // Tests ----------------------------------------------------------------------- 252 // Tests -----------------------------------------------------------------------
228 253
229 // Make sure we query history for the default provider and a URLFetcher is 254 // Make sure we query history for the default provider and a URLFetcher is
230 // created for the default provider suggest results. 255 // created for the default provider suggest results.
231 TEST_F(SearchProviderTest, QueryDefaultProvider) { 256 TEST_F(SearchProviderTest, QueryDefaultProvider) {
232 string16 term = term1_.substr(0, term1_.length() - 1); 257 string16 term = term1_.substr(0, term1_.length() - 1);
233 QueryForInput(term, false, false); 258 QueryForInput(term, false);
234 259
235 // Make sure the default providers suggest service was queried. 260 // Make sure the default providers suggest service was queried.
236 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( 261 TestURLFetcher* fetcher = test_factory_.GetFetcherByID(
237 SearchProvider::kDefaultProviderURLFetcherID); 262 SearchProvider::kDefaultProviderURLFetcherID);
238 ASSERT_TRUE(fetcher); 263 ASSERT_TRUE(fetcher);
239 264
240 // And the URL matches what we expected. 265 // And the URL matches what we expected.
241 GURL expected_url = GURL(default_t_url_->suggestions_url()-> 266 GURL expected_url = GURL(default_t_url_->suggestions_url()->
242 ReplaceSearchTerms(*default_t_url_, term, 0, string16())); 267 ReplaceSearchTerms(*default_t_url_, term, 0, string16()));
243 ASSERT_TRUE(fetcher->original_url() == expected_url); 268 ASSERT_TRUE(fetcher->original_url() == expected_url);
244 269
245 // Tell the SearchProvider the suggest query is done. 270 // Tell the SearchProvider the suggest query is done.
246 fetcher->delegate()->OnURLFetchComplete( 271 fetcher->delegate()->OnURLFetchComplete(
247 fetcher, GURL(), net::URLRequestStatus(), 200, net::ResponseCookies(), 272 fetcher, GURL(), net::URLRequestStatus(), 200, net::ResponseCookies(),
248 std::string()); 273 std::string());
249 fetcher = NULL; 274 fetcher = NULL;
250 275
251 // Run till the history results complete. 276 // Run till the history results complete.
252 RunTillProviderDone(); 277 RunTillProviderDone();
253 278
254 // The SearchProvider is done. Make sure it has a result for the history 279 // The SearchProvider is done. Make sure it has a result for the history
255 // term term1. 280 // term term1.
256 AutocompleteMatch term1_match = FindMatchWithDestination(term1_url_); 281 AutocompleteMatch term1_match;
257 EXPECT_TRUE(!term1_match.destination_url.is_empty()); 282 EXPECT_TRUE(FindMatchWithDestination(term1_url_, &term1_match));
258 // Term1 should not have a description, it's set later. 283 // Term1 should not have a description, it's set later.
259 EXPECT_TRUE(term1_match.description.empty()); 284 EXPECT_TRUE(term1_match.description.empty());
260 285
261 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 286 AutocompleteMatch wyt_match;
262 *default_t_url_, term, 0, string16())); 287 EXPECT_TRUE(FindMatchWithDestination(GURL(
263 AutocompleteMatch what_you_typed_match = 288 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, term, 0,
264 FindMatchWithDestination(what_you_typed_url); 289 string16())), &wyt_match));
265 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); 290 EXPECT_TRUE(wyt_match.description.empty());
266 EXPECT_TRUE(what_you_typed_match.description.empty());
267 291
268 // The match for term1 should be more relevant than the what you typed result. 292 // The match for term1 should be more relevant than the what you typed result.
269 EXPECT_GT(term1_match.relevance, what_you_typed_match.relevance); 293 EXPECT_GT(term1_match.relevance, wyt_match.relevance);
270 } 294 }
271 295
272 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { 296 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) {
273 string16 term = term1_.substr(0, term1_.length() - 1); 297 string16 term = term1_.substr(0, term1_.length() - 1);
274 QueryForInput(term, true, false); 298 QueryForInput(term, true);
275 299
276 ASSERT_FALSE(provider_->matches().empty()); 300 ASSERT_FALSE(provider_->matches().empty());
277 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, 301 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED,
278 provider_->matches()[0].type); 302 provider_->matches()[0].type);
279 } 303 }
280 304
281 // Issues a query that matches the registered keyword and makes sure history 305 // Issues a query that matches the registered keyword and makes sure history
282 // is queried as well as URLFetchers getting created. 306 // is queried as well as URLFetchers getting created.
283 TEST_F(SearchProviderTest, QueryKeywordProvider) { 307 TEST_F(SearchProviderTest, QueryKeywordProvider) {
284 string16 term = keyword_term_.substr(0, keyword_term_.length() - 1); 308 string16 term = keyword_term_.substr(0, keyword_term_.length() - 1);
285 QueryForInput(keyword_t_url_->keyword() + UTF8ToUTF16(" ") + term, false, 309 QueryForInput(keyword_t_url_->keyword() + UTF8ToUTF16(" ") + term, false);
286 false);
287 310
288 // Make sure the default providers suggest service was queried. 311 // Make sure the default providers suggest service was queried.
289 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( 312 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID(
290 SearchProvider::kDefaultProviderURLFetcherID); 313 SearchProvider::kDefaultProviderURLFetcherID);
291 ASSERT_TRUE(default_fetcher); 314 ASSERT_TRUE(default_fetcher);
292 315
293 // Tell the SearchProvider the default suggest query is done. 316 // Tell the SearchProvider the default suggest query is done.
294 default_fetcher->delegate()->OnURLFetchComplete( 317 default_fetcher->delegate()->OnURLFetchComplete(
295 default_fetcher, GURL(), net::URLRequestStatus(), 200, 318 default_fetcher, GURL(), net::URLRequestStatus(), 200,
296 net::ResponseCookies(), std::string()); 319 net::ResponseCookies(), std::string());
(...skipping 13 matching lines...) Expand all
310 keyword_fetcher->delegate()->OnURLFetchComplete( 333 keyword_fetcher->delegate()->OnURLFetchComplete(
311 keyword_fetcher, GURL(), net::URLRequestStatus(), 200, 334 keyword_fetcher, GURL(), net::URLRequestStatus(), 200,
312 net::ResponseCookies(), std::string()); 335 net::ResponseCookies(), std::string());
313 keyword_fetcher = NULL; 336 keyword_fetcher = NULL;
314 337
315 // Run till the history results complete. 338 // Run till the history results complete.
316 RunTillProviderDone(); 339 RunTillProviderDone();
317 340
318 // The SearchProvider is done. Make sure it has a result for the history 341 // The SearchProvider is done. Make sure it has a result for the history
319 // term keyword. 342 // term keyword.
320 AutocompleteMatch match = FindMatchWithDestination(keyword_url_); 343 AutocompleteMatch match;
321 ASSERT_TRUE(!match.destination_url.is_empty()); 344 EXPECT_TRUE(FindMatchWithDestination(keyword_url_, &match));
322 345
323 // The match should have a TemplateURL. 346 // The match should have a TemplateURL.
324 EXPECT_TRUE(match.template_url); 347 EXPECT_TRUE(match.template_url);
325 348
326 // The fill into edit should contain the keyword. 349 // The fill into edit should contain the keyword.
327 EXPECT_EQ(keyword_t_url_->keyword() + char16(' ') + keyword_term_, 350 EXPECT_EQ(keyword_t_url_->keyword() + char16(' ') + keyword_term_,
328 match.fill_into_edit); 351 match.fill_into_edit);
329 } 352 }
330 353
331 TEST_F(SearchProviderTest, DontSendPrivateDataToSuggest) { 354 TEST_F(SearchProviderTest, DontSendPrivateDataToSuggest) {
332 // None of the following input strings should be sent to the suggest server, 355 // None of the following input strings should be sent to the suggest server,
333 // because they may contain private data. 356 // because they may contain private data.
334 const char* inputs[] = { 357 const char* inputs[] = {
335 "username:password", 358 "username:password",
336 "http://username:password", 359 "http://username:password",
337 "https://username:password", 360 "https://username:password",
338 "username:password@hostname", 361 "username:password@hostname",
339 "http://username:password@hostname/", 362 "http://username:password@hostname/",
340 "file://filename", 363 "file://filename",
341 "data://data", 364 "data://data",
342 "unknownscheme:anything", 365 "unknownscheme:anything",
343 "http://hostname/?query=q", 366 "http://hostname/?query=q",
344 "http://hostname/path#ref", 367 "http://hostname/path#ref",
345 "https://hostname/path", 368 "https://hostname/path",
346 }; 369 };
347 370
348 for (size_t i = 0; i < arraysize(inputs); ++i) { 371 for (size_t i = 0; i < arraysize(inputs); ++i) {
349 QueryForInput(ASCIIToUTF16(inputs[i]), false, false); 372 QueryForInput(ASCIIToUTF16(inputs[i]), false);
350 // Make sure the default providers suggest service was not queried. 373 // Make sure the default providers suggest service was not queried.
351 ASSERT_TRUE(test_factory_.GetFetcherByID( 374 ASSERT_TRUE(test_factory_.GetFetcherByID(
352 SearchProvider::kDefaultProviderURLFetcherID) == NULL); 375 SearchProvider::kDefaultProviderURLFetcherID) == NULL);
353 // Run till the history results complete. 376 // Run till the history results complete.
354 RunTillProviderDone(); 377 RunTillProviderDone();
355 } 378 }
356 } 379 }
357 380
358 // Make sure FinalizeInstantQuery works. 381 // Make sure FinalizeInstantQuery works.
359 TEST_F(SearchProviderTest, FinalizeInstantQuery) { 382 TEST_F(SearchProviderTest, FinalizeInstantQuery) {
360 PrefService* service = profile_.GetPrefs(); 383 PrefService* service = profile_.GetPrefs();
361 service->SetBoolean(prefs::kInstantEnabled, true); 384 service->SetBoolean(prefs::kInstantEnabled, true);
362 385
363 QueryForInput(ASCIIToUTF16("foo"), false, false); 386 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"),
364 387 NULL));
365 // Wait until history and the suggest query complete.
366 profile_.BlockUntilHistoryProcessesPendingRequests();
367 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
368
369 // When instant is enabled the provider isn't done until it hears from
370 // instant.
371 EXPECT_FALSE(provider_->done());
372 388
373 // Tell the provider instant is done. 389 // Tell the provider instant is done.
374 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); 390 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"));
375 391
376 // The provider should now be done. 392 // The provider should now be done.
377 EXPECT_TRUE(provider_->done()); 393 EXPECT_TRUE(provider_->done());
378 394
379 // There should be two matches, one for what you typed, the other for 395 // There should be two matches, one for what you typed, the other for
380 // 'foobar'. 396 // 'foobar'.
381 EXPECT_EQ(2u, provider_->matches().size()); 397 EXPECT_EQ(2u, provider_->matches().size());
382 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 398 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms(
383 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); 399 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16()));
384 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); 400 AutocompleteMatch instant_match;
385 EXPECT_TRUE(!instant_match.destination_url.is_empty()); 401 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match));
386 402
387 // And the 'foobar' match should not have a description, it'll be set later. 403 // And the 'foobar' match should not have a description, it'll be set later.
388 EXPECT_TRUE(instant_match.description.empty()); 404 EXPECT_TRUE(instant_match.description.empty());
389 405
390 // Make sure the what you typed match has no description. 406 // Make sure the what you typed match has no description.
391 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 407 AutocompleteMatch wyt_match;
392 *default_t_url_, ASCIIToUTF16("foo"), 0, string16())); 408 EXPECT_TRUE(FindMatchWithDestination(GURL(
393 AutocompleteMatch what_you_typed_match = 409 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_,
394 FindMatchWithDestination(what_you_typed_url); 410 ASCIIToUTF16("foo"), 0, string16())), &wyt_match));
395 EXPECT_TRUE(!what_you_typed_match.destination_url.is_empty()); 411 EXPECT_TRUE(wyt_match.description.empty());
396 EXPECT_TRUE(what_you_typed_match.description.empty());
397 412
398 // The instant search should be more relevant. 413 // The instant search should be more relevant.
399 EXPECT_GT(instant_match.relevance, what_you_typed_match.relevance); 414 EXPECT_GT(instant_match.relevance, wyt_match.relevance);
400 } 415 }
401 416
402 // Make sure that if FinalizeInstantQuery is invoked before suggest results 417 // Make sure that if FinalizeInstantQuery is invoked before suggest results
403 // return, the suggest text from FinalizeInstantQuery is remembered. 418 // return, the suggest text from FinalizeInstantQuery is remembered.
404 TEST_F(SearchProviderTest, RememberInstantQuery) { 419 TEST_F(SearchProviderTest, RememberInstantQuery) {
405 PrefService* service = profile_.GetPrefs(); 420 PrefService* service = profile_.GetPrefs();
406 service->SetBoolean(prefs::kInstantEnabled, true); 421 service->SetBoolean(prefs::kInstantEnabled, true);
407 422
408 QueryForInput(ASCIIToUTF16("foo"), false, false); 423 QueryForInput(ASCIIToUTF16("foo"), false);
409 424
410 // Finalize the instant query immediately. 425 // Finalize the instant query immediately.
411 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); 426 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"));
412 427
413 // There should be two matches, one for what you typed, the other for 428 // There should be two matches, one for what you typed, the other for
414 // 'foobar'. 429 // 'foobar'.
415 EXPECT_EQ(2u, provider_->matches().size()); 430 EXPECT_EQ(2u, provider_->matches().size());
416 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 431 GURL instant_url(default_t_url_->url()->ReplaceSearchTerms(*default_t_url_,
417 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); 432 ASCIIToUTF16("foobar"), 0, string16()));
418 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url); 433 AutocompleteMatch instant_match;
419 EXPECT_FALSE(instant_match.destination_url.is_empty()); 434 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match));
420 435
421 // Wait until history and the suggest query complete. 436 // Wait until history and the suggest query complete.
422 profile_.BlockUntilHistoryProcessesPendingRequests(); 437 profile_.BlockUntilHistoryProcessesPendingRequests();
423 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); 438 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
424 439
425 // Provider should be done. 440 // Provider should be done.
426 EXPECT_TRUE(provider_->done()); 441 EXPECT_TRUE(provider_->done());
427 442
428 // There should be two matches, one for what you typed, the other for 443 // There should be two matches, one for what you typed, the other for
429 // 'foobar'. 444 // 'foobar'.
430 EXPECT_EQ(2u, provider_->matches().size()); 445 EXPECT_EQ(2u, provider_->matches().size());
431 instant_match = FindMatchWithDestination(instant_url); 446 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match));
432 EXPECT_FALSE(instant_match.destination_url.is_empty());
433 447
434 // And the 'foobar' match should not have a description, it'll be set later. 448 // And the 'foobar' match should not have a description, it'll be set later.
435 EXPECT_TRUE(instant_match.description.empty()); 449 EXPECT_TRUE(instant_match.description.empty());
436 } 450 }
437 451
438 // Make sure that if trailing whitespace is added to the text supplied to 452 // Make sure that if trailing whitespace is added to the text supplied to
439 // AutocompleteInput the default suggest text is cleared. 453 // AutocompleteInput the default suggest text is cleared.
440 TEST_F(SearchProviderTest, DifferingText) { 454 TEST_F(SearchProviderTest, DifferingText) {
441 PrefService* service = profile_.GetPrefs(); 455 PrefService* service = profile_.GetPrefs();
442 service->SetBoolean(prefs::kInstantEnabled, true); 456 service->SetBoolean(prefs::kInstantEnabled, true);
443 457
444 QueryForInput(ASCIIToUTF16("foo"), false, false); 458 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"),
445 459 NULL));
446 // Wait until history and the suggest query complete.
447 profile_.BlockUntilHistoryProcessesPendingRequests();
448 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
449 460
450 // Finalize the instant query immediately. 461 // Finalize the instant query immediately.
451 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); 462 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar"));
452 463
453 // Query with the same input text, but trailing whitespace. 464 // Query with the same input text, but trailing whitespace.
454 QueryForInput(ASCIIToUTF16("foo "), false, false); 465 AutocompleteMatch instant_match;
466 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo "),
467 &instant_match));
455 468
456 // There should only one match, for what you typed. 469 // There should only one match, for what you typed.
457 EXPECT_EQ(1u, provider_->matches().size()); 470 EXPECT_EQ(1u, provider_->matches().size());
458 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms(
459 *default_t_url_, ASCIIToUTF16("foo "), 0, string16()));
460 AutocompleteMatch instant_match = FindMatchWithDestination(instant_url);
461 EXPECT_FALSE(instant_match.destination_url.is_empty()); 471 EXPECT_FALSE(instant_match.destination_url.is_empty());
462 } 472 }
463 473
464 TEST_F(SearchProviderTest, DontAutocompleteURLLikeTerms) { 474 TEST_F(SearchProviderTest, DontAutocompleteURLLikeTerms) {
465 profile_.CreateAutocompleteClassifier(); 475 profile_.CreateAutocompleteClassifier();
466 string16 term(ASCIIToUTF16("docs.google.com")); 476 GURL url = AddSearchToHistory(default_t_url_,
467 HistoryService* history = 477 ASCIIToUTF16("docs.google.com"), 1);
468 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS);
469 GURL url = GURL(default_t_url_->url()->ReplaceSearchTerms(
470 *default_t_url_, term, 0, string16()));
471 history->AddPageWithDetails(
472 url, string16(), 1, 1, base::Time::Now(), false, history::SOURCE_BROWSED);
473 history->SetKeywordSearchTermsForURL(url, default_t_url_->id(), term);
474 478
475 // Add the term as a url. 479 // Add the term as a url.
476 history->AddPageWithDetails( 480 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPageWithDetails(
477 GURL("http://docs.google.com"), string16(), 1, 1, base::Time::Now(), 481 GURL("http://docs.google.com"), string16(), 1, 1, base::Time::Now(),
478 false, history::SOURCE_BROWSED); 482 false, history::SOURCE_BROWSED);
479
480 profile_.BlockUntilHistoryProcessesPendingRequests(); 483 profile_.BlockUntilHistoryProcessesPendingRequests();
481 484
482 QueryForInput(ASCIIToUTF16("docs"), false, false); 485 AutocompleteMatch wyt_match;
483 486 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("docs"),
484 // Wait until history and the suggest query complete. 487 &wyt_match));
485 profile_.BlockUntilHistoryProcessesPendingRequests();
486 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
487
488 // Provider should be done.
489 EXPECT_TRUE(provider_->done());
490 488
491 // There should be two matches, one for what you typed, the other for 489 // There should be two matches, one for what you typed, the other for
492 // 'docs.google.com'. The search term should have a lower priority than the 490 // 'docs.google.com'. The search term should have a lower priority than the
493 // what you typed match. 491 // what you typed match.
494 ASSERT_EQ(2u, provider_->matches().size()); 492 ASSERT_EQ(2u, provider_->matches().size());
495 AutocompleteMatch term_match = FindMatchWithDestination(url); 493 AutocompleteMatch term_match;
496 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 494 EXPECT_TRUE(FindMatchWithDestination(url, &term_match));
497 *default_t_url_, ASCIIToUTF16("docs"), 0, string16())); 495 EXPECT_GT(wyt_match.relevance, term_match.relevance);
498 AutocompleteMatch what_you_typed_match =
499 FindMatchWithDestination(what_you_typed_url);
500 EXPECT_GT(what_you_typed_match.relevance, term_match.relevance);
501 } 496 }
502 497
503 // Verifies autocomplete of previously typed words works on word boundaries. 498 // A multiword search with one visit should not autocomplete until multiple
504 TEST_F(SearchProviderTest, AutocompletePreviousSearchOnSpace) { 499 // words are typed.
505 // Add an entry that corresponds to a search with two words. 500 TEST_F(SearchProviderTest, DontAutocompleteUntilMultipleWordsTyped) {
506 string16 term(ASCIIToUTF16("two words")); 501 GURL term_url(AddSearchToHistory(default_t_url_, ASCIIToUTF16("one search"),
507 HistoryService* history = 502 1));
508 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS);
509 GURL term_url(default_t_url_->url()->ReplaceSearchTerms(
510 *default_t_url_, term, 0, string16()));
511 history->AddPageWithDetails(term_url, string16(), 1, 1,
512 base::Time::Now(), false,
513 history::SOURCE_BROWSED);
514 history->SetKeywordSearchTermsForURL(term_url, default_t_url_->id(), term);
515
516 profile_.BlockUntilHistoryProcessesPendingRequests(); 503 profile_.BlockUntilHistoryProcessesPendingRequests();
517 504
518 QueryForInput(ASCIIToUTF16("two "), false, false); 505 AutocompleteMatch wyt_match;
506 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("on"),
507 &wyt_match));
508 ASSERT_EQ(2u, provider_->matches().size());
509 AutocompleteMatch term_match;
510 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match));
511 EXPECT_GT(wyt_match.relevance, term_match.relevance);
519 512
520 // Wait until history and the suggest query complete. 513 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("one se"),
514 &wyt_match));
515 ASSERT_EQ(2u, provider_->matches().size());
516 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match));
517 EXPECT_GT(term_match.relevance, wyt_match.relevance);
518 }
519
520 // A multiword search with more than one visit should autocomplete immediately.
521 TEST_F(SearchProviderTest, AutocompleteMultipleVisitsImmediately) {
522 GURL term_url(AddSearchToHistory(default_t_url_, ASCIIToUTF16("two searches"),
523 2));
521 profile_.BlockUntilHistoryProcessesPendingRequests(); 524 profile_.BlockUntilHistoryProcessesPendingRequests();
522 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery());
523 525
524 // Provider should be done. 526 AutocompleteMatch wyt_match;
525 EXPECT_TRUE(provider_->done()); 527 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("tw"),
528 &wyt_match));
529 ASSERT_EQ(2u, provider_->matches().size());
530 AutocompleteMatch term_match;
531 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match));
532 EXPECT_GT(term_match.relevance, wyt_match.relevance);
533 }
526 534
527 // There should be two matches, one for what you typed, the other for 535 // Autocompletion should work at a word boundary after a space.
528 // 'two words'. 536 TEST_F(SearchProviderTest, AutocompleteAfterSpace) {
537 GURL term_url(AddSearchToHistory(default_t_url_, ASCIIToUTF16("two searches"),
538 2));
539 profile_.BlockUntilHistoryProcessesPendingRequests();
540
541 AutocompleteMatch wyt_match;
542 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("two "),
543 &wyt_match));
529 ASSERT_EQ(2u, provider_->matches().size()); 544 ASSERT_EQ(2u, provider_->matches().size());
530 AutocompleteMatch term_match = FindMatchWithDestination(term_url); 545 AutocompleteMatch term_match;
531 EXPECT_FALSE(term_match.destination_url.is_empty()); 546 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match));
532 GURL what_you_typed_url = GURL(default_t_url_->url()->ReplaceSearchTerms( 547 EXPECT_GT(term_match.relevance, wyt_match.relevance);
533 *default_t_url_, ASCIIToUTF16("two "), 0, string16())); 548 }
534 AutocompleteMatch what_you_typed_match = 549
535 FindMatchWithDestination(what_you_typed_url); 550 // Newer multiword searches should score more highly than older ones.
536 EXPECT_FALSE(what_you_typed_match.destination_url.is_empty()); 551 TEST_F(SearchProviderTest, ScoreNewerSearchesHigher) {
537 // term_match should be autocompleted. 552 GURL term_url_a(AddSearchToHistory(default_t_url_,
538 EXPECT_GT(term_match.relevance, what_you_typed_match.relevance); 553 ASCIIToUTF16("three searches aaa"), 1));
539 // And the offset should be at 4. 554 GURL term_url_b(AddSearchToHistory(default_t_url_,
540 EXPECT_EQ(4u, term_match.inline_autocomplete_offset); 555 ASCIIToUTF16("three searches bbb"), 1));
556 profile_.BlockUntilHistoryProcessesPendingRequests();
557
558 AutocompleteMatch wyt_match;
559 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("three se"),
560 &wyt_match));
561 ASSERT_EQ(3u, provider_->matches().size());
562 AutocompleteMatch term_match_a;
563 EXPECT_TRUE(FindMatchWithDestination(term_url_a, &term_match_a));
564 AutocompleteMatch term_match_b;
565 EXPECT_TRUE(FindMatchWithDestination(term_url_b, &term_match_b));
566 EXPECT_GT(term_match_b.relevance, term_match_a.relevance);
567 EXPECT_GT(term_match_a.relevance, wyt_match.relevance);
568 }
569
570 // An autocompleted multiword search should not be replaced by a different
571 // autocompletion while the user is still typing a valid prefix.
572 TEST_F(SearchProviderTest, DontReplacePreviousAutocompletion) {
573 GURL term_url_a(AddSearchToHistory(default_t_url_,
574 ASCIIToUTF16("four searches aaa"), 2));
575 GURL term_url_b(AddSearchToHistory(default_t_url_,
576 ASCIIToUTF16("four searches bbb"), 1));
577 profile_.BlockUntilHistoryProcessesPendingRequests();
578
579 AutocompleteMatch wyt_match;
580 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("fo"),
581 &wyt_match));
582 ASSERT_EQ(3u, provider_->matches().size());
583 AutocompleteMatch term_match_a;
584 EXPECT_TRUE(FindMatchWithDestination(term_url_a, &term_match_a));
585 AutocompleteMatch term_match_b;
586 EXPECT_TRUE(FindMatchWithDestination(term_url_b, &term_match_b));
587 EXPECT_GT(term_match_a.relevance, wyt_match.relevance);
588 EXPECT_GT(wyt_match.relevance, term_match_b.relevance);
589
590 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("four se"),
591 &wyt_match));
592 ASSERT_EQ(3u, provider_->matches().size());
593 EXPECT_TRUE(FindMatchWithDestination(term_url_a, &term_match_a));
594 EXPECT_TRUE(FindMatchWithDestination(term_url_b, &term_match_b));
595 EXPECT_GT(term_match_a.relevance, wyt_match.relevance);
596 EXPECT_GT(wyt_match.relevance, term_match_b.relevance);
597 }
598
599 // Non-completable multiword searches should not crowd out single-word searches.
600 TEST_F(SearchProviderTest, DontCrowdOutSingleWords) {
601 GURL term_url(AddSearchToHistory(default_t_url_, ASCIIToUTF16("five"), 1));
602 AddSearchToHistory(default_t_url_, ASCIIToUTF16("five searches bbb"), 1);
603 AddSearchToHistory(default_t_url_, ASCIIToUTF16("five searches ccc"), 1);
604 AddSearchToHistory(default_t_url_, ASCIIToUTF16("five searches ddd"), 1);
605 AddSearchToHistory(default_t_url_, ASCIIToUTF16("five searches eee"), 1);
606 profile_.BlockUntilHistoryProcessesPendingRequests();
607
608 AutocompleteMatch wyt_match;
609 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("fi"),
610 &wyt_match));
611 ASSERT_EQ(AutocompleteProvider::kMaxMatches + 1, provider_->matches().size());
612 AutocompleteMatch term_match;
613 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match));
614 EXPECT_GT(term_match.relevance, wyt_match.relevance);
541 } 615 }
542 616
543 // Verifies AutocompleteControllers sets descriptions for results correctly. 617 // Verifies AutocompleteControllers sets descriptions for results correctly.
544 TEST_F(SearchProviderTest, UpdateKeywordDescriptions) { 618 TEST_F(SearchProviderTest, UpdateKeywordDescriptions) {
545 // Add an entry that corresponds to a keyword search with 'term2'. 619 // Add an entry that corresponds to a keyword search with 'term2'.
546 string16 term(ASCIIToUTF16("term2")); 620 AddSearchToHistory(keyword_t_url_, ASCIIToUTF16("term2"), 1);
547 HistoryService* history =
548 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS);
549 GURL term_url(keyword_t_url_->url()->ReplaceSearchTerms(
550 *keyword_t_url_, term, 0, string16()));
551 history->AddPageWithDetails(term_url, string16(), 1, 1,
552 base::Time::Now(), false,
553 history::SOURCE_BROWSED);
554 history->SetKeywordSearchTermsForURL(term_url, keyword_t_url_->id(), term);
555
556 profile_.BlockUntilHistoryProcessesPendingRequests(); 621 profile_.BlockUntilHistoryProcessesPendingRequests();
557 622
558 ACProviders providers; 623 ACProviders providers;
559 SearchProvider* provider = provider_.release(); 624 SearchProvider* provider = provider_.release();
560 providers.push_back(provider); 625 providers.push_back(provider);
561 AutocompleteController controller(providers); 626 AutocompleteController controller(providers);
562 controller.set_search_provider(provider); 627 controller.set_search_provider(provider);
563 provider->set_listener(&controller); 628 provider->set_listener(&controller);
564 controller.Start(ASCIIToUTF16("k t"), string16(), false, false, true, 629 controller.Start(ASCIIToUTF16("k t"), string16(), false, false, true,
565 AutocompleteInput::ALL_MATCHES); 630 AutocompleteInput::ALL_MATCHES);
566 const AutocompleteResult& result = controller.result(); 631 const AutocompleteResult& result = controller.result();
567 632
568 // There should be two matches, one for the keyword one for what you typed. 633 // There should be two matches, one for the keyword one for what you typed.
569 ASSERT_EQ(2u, result.size()); 634 ASSERT_EQ(2u, result.size());
570 635
571 EXPECT_TRUE(result.match_at(0).template_url != NULL); 636 EXPECT_TRUE(result.match_at(0).template_url != NULL);
572 EXPECT_TRUE(result.match_at(1).template_url != NULL); 637 EXPECT_TRUE(result.match_at(1).template_url != NULL);
573 EXPECT_NE(result.match_at(0).template_url, 638 EXPECT_NE(result.match_at(0).template_url,
574 result.match_at(1).template_url); 639 result.match_at(1).template_url);
575 640
576 EXPECT_FALSE(result.match_at(0).description.empty()); 641 EXPECT_FALSE(result.match_at(0).description.empty());
577 EXPECT_FALSE(result.match_at(1).description.empty()); 642 EXPECT_FALSE(result.match_at(1).description.empty());
578 EXPECT_NE(result.match_at(0).description, 643 EXPECT_NE(result.match_at(0).description,
579 result.match_at(1).description); 644 result.match_at(1).description);
580 } 645 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698