| 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/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 12 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 13 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_input.h" | 14 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_match.h" | 15 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 16 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 18 #include "chrome/browser/history/history.h" | 18 #include "chrome/browser/history/history.h" |
| 19 #include "chrome/browser/history/history_service_factory.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
| 20 #include "chrome/browser/prefs/pref_service.h" | |
| 21 #include "chrome/browser/search_engines/template_url.h" | 20 #include "chrome/browser/search_engines/template_url.h" |
| 22 #include "chrome/browser/search_engines/template_url_service.h" | 21 #include "chrome/browser/search_engines/template_url_service.h" |
| 23 #include "chrome/browser/search_engines/template_url_service_factory.h" | 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 24 #include "chrome/common/pref_names.h" | |
| 25 #include "chrome/test/base/testing_browser_process.h" | 23 #include "chrome/test/base/testing_browser_process.h" |
| 26 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 27 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
| 28 #include "net/url_request/test_url_fetcher_factory.h" | 26 #include "net/url_request/test_url_fetcher_factory.h" |
| 29 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 29 |
| 32 using content::BrowserThread; | 30 using content::BrowserThread; |
| 33 | 31 |
| 34 // The following environment is configured for these tests: | 32 // The following environment is configured for these tests: |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 // URLFetchers runs. | 199 // URLFetchers runs. |
| 202 message_loop_.RunAllPending(); | 200 message_loop_.RunAllPending(); |
| 203 } | 201 } |
| 204 | 202 |
| 205 void SearchProviderTest::QueryForInputAndSetWYTMatch( | 203 void SearchProviderTest::QueryForInputAndSetWYTMatch( |
| 206 const string16& text, | 204 const string16& text, |
| 207 AutocompleteMatch* wyt_match) { | 205 AutocompleteMatch* wyt_match) { |
| 208 QueryForInput(text, string16(), false); | 206 QueryForInput(text, string16(), false); |
| 209 profile_.BlockUntilHistoryProcessesPendingRequests(); | 207 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 210 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 208 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
| 211 EXPECT_NE(profile_.GetPrefs()->GetBoolean(prefs::kInstantEnabled), | 209 EXPECT_NE(provider_->instant_suggest_enabled(), provider_->done()); |
| 212 provider_->done()); | |
| 213 if (!wyt_match) | 210 if (!wyt_match) |
| 214 return; | 211 return; |
| 215 ASSERT_GE(provider_->matches().size(), 1u); | 212 ASSERT_GE(provider_->matches().size(), 1u); |
| 216 EXPECT_TRUE(FindMatchWithDestination(GURL( | 213 EXPECT_TRUE(FindMatchWithDestination(GURL( |
| 217 default_t_url_->url_ref().ReplaceSearchTerms( | 214 default_t_url_->url_ref().ReplaceSearchTerms( |
| 218 TemplateURLRef::SearchTermsArgs(text))), | 215 TemplateURLRef::SearchTermsArgs(text))), |
| 219 wyt_match)); | 216 wyt_match)); |
| 220 } | 217 } |
| 221 | 218 |
| 222 void SearchProviderTest::TearDown() { | 219 void SearchProviderTest::TearDown() { |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 // Make sure the default providers suggest service was not queried. | 396 // Make sure the default providers suggest service was not queried. |
| 400 ASSERT_TRUE(test_factory_.GetFetcherByID( | 397 ASSERT_TRUE(test_factory_.GetFetcherByID( |
| 401 SearchProvider::kDefaultProviderURLFetcherID) == NULL); | 398 SearchProvider::kDefaultProviderURLFetcherID) == NULL); |
| 402 // Run till the history results complete. | 399 // Run till the history results complete. |
| 403 RunTillProviderDone(); | 400 RunTillProviderDone(); |
| 404 } | 401 } |
| 405 } | 402 } |
| 406 | 403 |
| 407 // Make sure FinalizeInstantQuery works. | 404 // Make sure FinalizeInstantQuery works. |
| 408 TEST_F(SearchProviderTest, FinalizeInstantQuery) { | 405 TEST_F(SearchProviderTest, FinalizeInstantQuery) { |
| 409 PrefService* service = profile_.GetPrefs(); | 406 provider_->set_instant_suggest_enabled(true); |
| 410 service->SetBoolean(prefs::kInstantEnabled, true); | |
| 411 | 407 |
| 412 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"), | 408 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"), |
| 413 NULL)); | 409 NULL)); |
| 414 | 410 |
| 415 // Tell the provider instant is done. | 411 // Tell the provider instant is done. |
| 416 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 412 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
| 417 | 413 |
| 418 // The provider should now be done. | 414 // The provider should now be done. |
| 419 EXPECT_TRUE(provider_->done()); | 415 EXPECT_TRUE(provider_->done()); |
| 420 | 416 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 437 &wyt_match)); | 433 &wyt_match)); |
| 438 EXPECT_TRUE(wyt_match.description.empty()); | 434 EXPECT_TRUE(wyt_match.description.empty()); |
| 439 | 435 |
| 440 // The instant search should be more relevant. | 436 // The instant search should be more relevant. |
| 441 EXPECT_GT(instant_match.relevance, wyt_match.relevance); | 437 EXPECT_GT(instant_match.relevance, wyt_match.relevance); |
| 442 } | 438 } |
| 443 | 439 |
| 444 // Make sure that if FinalizeInstantQuery is invoked before suggest results | 440 // Make sure that if FinalizeInstantQuery is invoked before suggest results |
| 445 // return, the suggest text from FinalizeInstantQuery is remembered. | 441 // return, the suggest text from FinalizeInstantQuery is remembered. |
| 446 TEST_F(SearchProviderTest, RememberInstantQuery) { | 442 TEST_F(SearchProviderTest, RememberInstantQuery) { |
| 447 PrefService* service = profile_.GetPrefs(); | 443 provider_->set_instant_suggest_enabled(true); |
| 448 service->SetBoolean(prefs::kInstantEnabled, true); | |
| 449 | 444 |
| 450 QueryForInput(ASCIIToUTF16("foo"), string16(), false); | 445 QueryForInput(ASCIIToUTF16("foo"), string16(), false); |
| 451 | 446 |
| 452 // Finalize the instant query immediately. | 447 // Finalize the instant query immediately. |
| 453 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 448 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
| 454 | 449 |
| 455 // There should be two matches, one for what you typed, the other for | 450 // There should be two matches, one for what you typed, the other for |
| 456 // 'foobar'. | 451 // 'foobar'. |
| 457 EXPECT_EQ(2u, provider_->matches().size()); | 452 EXPECT_EQ(2u, provider_->matches().size()); |
| 458 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( | 453 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 472 EXPECT_EQ(2u, provider_->matches().size()); | 467 EXPECT_EQ(2u, provider_->matches().size()); |
| 473 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | 468 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); |
| 474 | 469 |
| 475 // And the 'foobar' match should not have a description, it'll be set later. | 470 // And the 'foobar' match should not have a description, it'll be set later. |
| 476 EXPECT_TRUE(instant_match.description.empty()); | 471 EXPECT_TRUE(instant_match.description.empty()); |
| 477 } | 472 } |
| 478 | 473 |
| 479 // Make sure that if trailing whitespace is added to the text supplied to | 474 // Make sure that if trailing whitespace is added to the text supplied to |
| 480 // AutocompleteInput the default suggest text is cleared. | 475 // AutocompleteInput the default suggest text is cleared. |
| 481 TEST_F(SearchProviderTest, DifferingText) { | 476 TEST_F(SearchProviderTest, DifferingText) { |
| 482 PrefService* service = profile_.GetPrefs(); | 477 provider_->set_instant_suggest_enabled(true); |
| 483 service->SetBoolean(prefs::kInstantEnabled, true); | |
| 484 | 478 |
| 485 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"), | 479 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"), |
| 486 NULL)); | 480 NULL)); |
| 487 | 481 |
| 488 // Finalize the instant query immediately. | 482 // Finalize the instant query immediately. |
| 489 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 483 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
| 490 | 484 |
| 491 // Query with the same input text, but trailing whitespace. | 485 // Query with the same input text, but trailing whitespace. |
| 492 AutocompleteMatch instant_match; | 486 AutocompleteMatch instant_match; |
| 493 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo "), | 487 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo "), |
| (...skipping 736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1230 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1224 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
| 1231 match.contents_class[0].style); | 1225 match.contents_class[0].style); |
| 1232 EXPECT_EQ(4U, match.contents_class[1].offset); | 1226 EXPECT_EQ(4U, match.contents_class[1].offset); |
| 1233 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | | 1227 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | |
| 1234 AutocompleteMatch::ACMatchClassification::MATCH, | 1228 AutocompleteMatch::ACMatchClassification::MATCH, |
| 1235 match.contents_class[1].style); | 1229 match.contents_class[1].style); |
| 1236 EXPECT_EQ(5U, match.contents_class[2].offset); | 1230 EXPECT_EQ(5U, match.contents_class[2].offset); |
| 1237 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1231 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
| 1238 match.contents_class[2].style); | 1232 match.contents_class[2].style); |
| 1239 } | 1233 } |
| OLD | NEW |