Chromium Code Reviews| 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 "components/omnibox/browser/autocomplete_provider.h" | 5 #include "components/omnibox/browser/autocomplete_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 17 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" | 18 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" |
| 18 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | |
| 19 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/search_engines/template_url_service_factory.h" | 20 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 21 #include "chrome/test/base/testing_browser_process.h" | |
| 22 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/metrics/proto/omnibox_event.pb.h" | 22 #include "components/metrics/proto/omnibox_event.pb.h" |
| 24 #include "components/omnibox/browser/autocomplete_controller.h" | 23 #include "components/omnibox/browser/autocomplete_controller.h" |
| 25 #include "components/omnibox/browser/autocomplete_input.h" | 24 #include "components/omnibox/browser/autocomplete_input.h" |
| 26 #include "components/omnibox/browser/autocomplete_match.h" | 25 #include "components/omnibox/browser/autocomplete_match.h" |
| 27 #include "components/omnibox/browser/autocomplete_provider_listener.h" | 26 #include "components/omnibox/browser/autocomplete_provider_listener.h" |
| 28 #include "components/omnibox/browser/keyword_provider.h" | 27 #include "components/omnibox/browser/keyword_provider.h" |
| 28 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" | |
| 29 #include "components/omnibox/browser/search_provider.h" | 29 #include "components/omnibox/browser/search_provider.h" |
| 30 #include "components/search_engines/search_engines_switches.h" | 30 #include "components/search_engines/search_engines_switches.h" |
| 31 #include "components/search_engines/template_url.h" | 31 #include "components/search_engines/template_url.h" |
| 32 #include "components/search_engines/template_url_service.h" | 32 #include "components/search_engines/template_url_service.h" |
| 33 #include "components/search_engines/template_url_service_client.h" | |
| 33 #include "content/public/browser/notification_observer.h" | 34 #include "content/public/browser/notification_observer.h" |
| 34 #include "content/public/browser/notification_registrar.h" | 35 #include "content/public/browser/notification_registrar.h" |
| 35 #include "content/public/browser/notification_source.h" | 36 #include "content/public/browser/notification_source.h" |
| 36 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 38 | 39 |
| 40 using testing::NiceMock; | |
| 41 | |
| 39 static std::ostream& operator<<(std::ostream& os, | 42 static std::ostream& operator<<(std::ostream& os, |
| 40 const AutocompleteResult::const_iterator& it) { | 43 const AutocompleteResult::const_iterator& it) { |
| 41 return os << static_cast<const AutocompleteMatch*>(&(*it)); | 44 return os << static_cast<const AutocompleteMatch*>(&(*it)); |
| 42 } | 45 } |
| 43 | 46 |
| 44 namespace { | 47 namespace { |
| 45 const size_t kResultsPerProvider = 3; | 48 const size_t kResultsPerProvider = 3; |
| 46 const char kTestTemplateURLKeyword[] = "t"; | 49 const char kTestTemplateURLKeyword[] = "t"; |
| 47 } | 50 |
| 51 class TestingSchemeClassifier : public AutocompleteSchemeClassifier { | |
| 52 public: | |
| 53 metrics::OmniboxInputType::Type GetInputTypeForScheme( | |
| 54 const std::string& scheme) const override { | |
| 55 if (net::URLRequest::IsHandledProtocol(scheme)) | |
| 56 return metrics::OmniboxInputType::URL; | |
| 57 return metrics::OmniboxInputType::INVALID; | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 } // namespace | |
| 48 | 62 |
| 49 // Autocomplete provider that provides known results. Note that this is | 63 // Autocomplete provider that provides known results. Note that this is |
| 50 // refcounted so that it can also be a task on the message loop. | 64 // refcounted so that it can also be a task on the message loop. |
| 51 class TestProvider : public AutocompleteProvider { | 65 class TestProvider : public AutocompleteProvider { |
| 52 public: | 66 public: |
| 53 TestProvider(int relevance, const base::string16& prefix, | 67 TestProvider(int relevance, const base::string16& prefix, |
| 54 Profile* profile, | 68 Profile* profile, |
| 55 const base::string16 match_keyword) | 69 const base::string16 match_keyword) |
| 56 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH), | 70 : AutocompleteProvider(AutocompleteProvider::TYPE_SEARCH), |
| 57 listener_(NULL), | 71 listener_(NULL), |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 const base::string16 keyword; | 183 const base::string16 keyword; |
| 170 const base::string16 expected_associated_keyword; | 184 const base::string16 expected_associated_keyword; |
| 171 }; | 185 }; |
| 172 | 186 |
| 173 struct AssistedQueryStatsTestData { | 187 struct AssistedQueryStatsTestData { |
| 174 const AutocompleteMatch::Type match_type; | 188 const AutocompleteMatch::Type match_type; |
| 175 const std::string expected_aqs; | 189 const std::string expected_aqs; |
| 176 }; | 190 }; |
| 177 | 191 |
| 178 protected: | 192 protected: |
| 193 ~AutocompleteProviderTest() override {} | |
| 194 | |
| 179 // Registers a test TemplateURL under the given keyword. | 195 // Registers a test TemplateURL under the given keyword. |
| 180 void RegisterTemplateURL(const base::string16 keyword, | 196 void RegisterTemplateURL(const base::string16 keyword, |
| 181 const std::string& template_url); | 197 const std::string& template_url); |
| 182 | 198 |
| 183 // Resets |controller_| with two TestProviders. |provider1_ptr| and | 199 // Resets |controller_| with two TestProviders. |provider1_ptr| and |
| 184 // |provider2_ptr| are updated to point to the new providers if non-NULL. | 200 // |provider2_ptr| are updated to point to the new providers if non-NULL. |
| 185 void ResetControllerWithTestProviders(bool same_destinations, | 201 void ResetControllerWithTestProviders(bool same_destinations, |
| 186 TestProvider** provider1_ptr, | 202 TestProvider** provider1_ptr, |
| 187 TestProvider** provider2_ptr); | 203 TestProvider** provider2_ptr); |
| 188 | 204 |
| 205 void SetUp() override; | |
| 206 | |
| 189 // Runs a query on the input "a", and makes sure both providers' input is | 207 // Runs a query on the input "a", and makes sure both providers' input is |
| 190 // properly collected. | 208 // properly collected. |
| 191 void RunTest(); | 209 void RunTest(); |
| 192 | 210 |
| 193 // Constructs an AutocompleteResult from |match_data|, sets the |controller_| | 211 // Constructs an AutocompleteResult from |match_data|, sets the |controller_| |
| 194 // to pretend it was running against input |input|, calls the |controller_|'s | 212 // to pretend it was running against input |input|, calls the |controller_|'s |
| 195 // UpdateAssociatedKeywords, and checks that the matches have associated | 213 // UpdateAssociatedKeywords, and checks that the matches have associated |
| 196 // keywords as expected. | 214 // keywords as expected. |
| 197 void RunKeywordTest(const base::string16& input, | 215 void RunKeywordTest(const base::string16& input, |
| 198 const KeywordTestData* match_data, | 216 const KeywordTestData* match_data, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 private: | 249 private: |
| 232 // content::NotificationObserver: | 250 // content::NotificationObserver: |
| 233 void Observe(int type, | 251 void Observe(int type, |
| 234 const content::NotificationSource& source, | 252 const content::NotificationSource& source, |
| 235 const content::NotificationDetails& details) override; | 253 const content::NotificationDetails& details) override; |
| 236 | 254 |
| 237 content::TestBrowserThreadBundle thread_bundle_; | 255 content::TestBrowserThreadBundle thread_bundle_; |
| 238 content::NotificationRegistrar registrar_; | 256 content::NotificationRegistrar registrar_; |
| 239 TestingProfile profile_; | 257 TestingProfile profile_; |
| 240 scoped_ptr<AutocompleteController> controller_; | 258 scoped_ptr<AutocompleteController> controller_; |
| 259 scoped_ptr<MockAutocompleteProviderClient> client_; | |
| 241 }; | 260 }; |
| 242 | 261 |
| 262 void AutocompleteProviderTest::SetUp() { | |
| 263 scoped_ptr<TemplateURLService> template_url_service( | |
| 264 new TemplateURLService( | |
| 265 nullptr, scoped_ptr<SearchTermsData>(new SearchTermsData), | |
| 266 nullptr, scoped_ptr<TemplateURLServiceClient>(), nullptr, | |
| 267 nullptr, base::Closure())); | |
| 268 client_.reset(new NiceMock<MockAutocompleteProviderClient>()); | |
| 269 client_->set_template_url_service(template_url_service.Pass()); | |
| 270 } | |
| 271 | |
| 243 void AutocompleteProviderTest::RegisterTemplateURL( | 272 void AutocompleteProviderTest::RegisterTemplateURL( |
| 244 const base::string16 keyword, | 273 const base::string16 keyword, |
| 245 const std::string& template_url) { | 274 const std::string& template_url) { |
| 246 if (TemplateURLServiceFactory::GetForProfile(&profile_) == NULL) { | 275 if (TemplateURLServiceFactory::GetForProfile(&profile_) == NULL) { |
|
droger
2015/10/28 16:56:47
You have to remove this call to TemplateURLService
Abhishek
2015/10/30 14:15:55
Done.
| |
| 247 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 276 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 248 &profile_, &TemplateURLServiceFactory::BuildInstanceFor); | 277 &profile_, &TemplateURLServiceFactory::BuildInstanceFor); |
| 249 } | 278 } |
| 250 TemplateURLData data; | 279 TemplateURLData data; |
| 251 data.SetURL(template_url); | 280 data.SetURL(template_url); |
| 252 data.SetShortName(keyword); | 281 data.SetShortName(keyword); |
| 253 data.SetKeyword(keyword); | 282 data.SetKeyword(keyword); |
| 254 TemplateURL* default_t_url = new TemplateURL(data); | 283 TemplateURL* default_t_url = new TemplateURL(data); |
| 255 TemplateURLService* turl_model = | 284 TemplateURLService* turl_model = |
| 256 TemplateURLServiceFactory::GetForProfile(&profile_); | 285 TemplateURLServiceFactory::GetForProfile(&profile_); |
|
droger
2015/10/28 16:56:47
Here you need to use the TemplateURLService you cr
Abhishek
2015/10/30 14:15:55
Actually I'm able to get the TemplateURLService fr
droger
2015/11/02 10:21:46
I don't understand what you are asking here. If yo
| |
| 257 turl_model->Add(default_t_url); | 286 turl_model->Add(default_t_url); |
| 258 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url); | 287 turl_model->SetUserSelectedDefaultSearchProvider(default_t_url); |
| 259 turl_model->Load(); | 288 turl_model->Load(); |
| 260 TemplateURLID default_provider_id = default_t_url->id(); | 289 TemplateURLID default_provider_id = default_t_url->id(); |
| 261 ASSERT_NE(0, default_provider_id); | 290 ASSERT_NE(0, default_provider_id); |
| 262 } | 291 } |
| 263 | 292 |
| 264 void AutocompleteProviderTest::ResetControllerWithTestProviders( | 293 void AutocompleteProviderTest::ResetControllerWithTestProviders( |
| 265 bool same_destinations, | 294 bool same_destinations, |
| 266 TestProvider** provider1_ptr, | 295 TestProvider** provider1_ptr, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 match.fill_into_edit = match_data[i].fill_into_edit; | 435 match.fill_into_edit = match_data[i].fill_into_edit; |
| 407 match.transition = ui::PAGE_TRANSITION_KEYWORD; | 436 match.transition = ui::PAGE_TRANSITION_KEYWORD; |
| 408 match.keyword = match_data[i].keyword; | 437 match.keyword = match_data[i].keyword; |
| 409 matches.push_back(match); | 438 matches.push_back(match); |
| 410 } | 439 } |
| 411 | 440 |
| 412 controller_->input_ = AutocompleteInput( | 441 controller_->input_ = AutocompleteInput( |
| 413 input, base::string16::npos, std::string(), GURL(), | 442 input, base::string16::npos, std::string(), GURL(), |
| 414 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 443 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| 415 false, true, true, true, false, | 444 false, true, true, true, false, |
| 416 ChromeAutocompleteSchemeClassifier(&profile_)); | 445 TestingSchemeClassifier()); |
| 417 AutocompleteResult result; | 446 AutocompleteResult result; |
| 418 result.AppendMatches(controller_->input_, matches); | 447 result.AppendMatches(controller_->input_, matches); |
| 419 controller_->UpdateAssociatedKeywords(&result); | 448 controller_->UpdateAssociatedKeywords(&result); |
| 420 | 449 |
| 421 for (size_t j = 0; j < result.size(); ++j) { | 450 for (size_t j = 0; j < result.size(); ++j) { |
| 422 EXPECT_EQ(match_data[j].expected_associated_keyword, | 451 EXPECT_EQ(match_data[j].expected_associated_keyword, |
| 423 result.match_at(j)->associated_keyword.get() ? | 452 result.match_at(j)->associated_keyword.get() ? |
| 424 result.match_at(j)->associated_keyword->keyword : | 453 result.match_at(j)->associated_keyword->keyword : |
| 425 base::string16()); | 454 base::string16()); |
| 426 } | 455 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 452 EXPECT_EQ(aqs_test_data[i].expected_aqs, | 481 EXPECT_EQ(aqs_test_data[i].expected_aqs, |
| 453 result_.match_at(i)->search_terms_args->assisted_query_stats); | 482 result_.match_at(i)->search_terms_args->assisted_query_stats); |
| 454 } | 483 } |
| 455 } | 484 } |
| 456 | 485 |
| 457 void AutocompleteProviderTest::RunQuery(const base::string16 query) { | 486 void AutocompleteProviderTest::RunQuery(const base::string16 query) { |
| 458 result_.Reset(); | 487 result_.Reset(); |
| 459 controller_->Start(AutocompleteInput( | 488 controller_->Start(AutocompleteInput( |
| 460 query, base::string16::npos, std::string(), GURL(), | 489 query, base::string16::npos, std::string(), GURL(), |
| 461 metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, true, false, | 490 metrics::OmniboxEventProto::INVALID_SPEC, true, false, true, true, false, |
| 462 ChromeAutocompleteSchemeClassifier(&profile_))); | 491 TestingSchemeClassifier())); |
| 463 | 492 |
| 464 if (!controller_->done()) | 493 if (!controller_->done()) |
| 465 // The message loop will terminate when all autocomplete input has been | 494 // The message loop will terminate when all autocomplete input has been |
| 466 // collected. | 495 // collected. |
| 467 base::MessageLoop::current()->Run(); | 496 base::MessageLoop::current()->Run(); |
| 468 } | 497 } |
| 469 | 498 |
| 470 void AutocompleteProviderTest::RunExactKeymatchTest( | 499 void AutocompleteProviderTest::RunExactKeymatchTest( |
| 471 bool allow_exact_keyword_match) { | 500 bool allow_exact_keyword_match) { |
| 472 // Send the controller input which exactly matches the keyword provider we | 501 // Send the controller input which exactly matches the keyword provider we |
| 473 // created in ResetControllerWithKeywordAndSearchProviders(). The default | 502 // created in ResetControllerWithKeywordAndSearchProviders(). The default |
| 474 // match should thus be a search-other-engine match iff | 503 // match should thus be a search-other-engine match iff |
| 475 // |allow_exact_keyword_match| is true. Regardless, the match should | 504 // |allow_exact_keyword_match| is true. Regardless, the match should |
| 476 // be from SearchProvider. (It provides all verbatim search matches, | 505 // be from SearchProvider. (It provides all verbatim search matches, |
| 477 // keyword or not.) | 506 // keyword or not.) |
| 478 controller_->Start(AutocompleteInput( | 507 controller_->Start(AutocompleteInput( |
| 479 base::ASCIIToUTF16("k test"), base::string16::npos, std::string(), GURL(), | 508 base::ASCIIToUTF16("k test"), base::string16::npos, std::string(), GURL(), |
| 480 metrics::OmniboxEventProto::INVALID_SPEC, true, false, | 509 metrics::OmniboxEventProto::INVALID_SPEC, true, false, |
| 481 allow_exact_keyword_match, false, false, | 510 allow_exact_keyword_match, false, false, |
| 482 ChromeAutocompleteSchemeClassifier(&profile_))); | 511 TestingSchemeClassifier())); |
| 483 EXPECT_TRUE(controller_->done()); | 512 EXPECT_TRUE(controller_->done()); |
| 484 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH, | 513 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH, |
| 485 controller_->result().default_match()->provider->type()); | 514 controller_->result().default_match()->provider->type()); |
| 486 EXPECT_EQ(allow_exact_keyword_match ? | 515 EXPECT_EQ(allow_exact_keyword_match ? |
| 487 AutocompleteMatchType::SEARCH_OTHER_ENGINE : | 516 AutocompleteMatchType::SEARCH_OTHER_ENGINE : |
| 488 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 517 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 489 controller_->result().default_match()->type); | 518 controller_->result().default_match()->type); |
| 490 } | 519 } |
| 491 | 520 |
| 492 void AutocompleteProviderTest::CopyResults() { | 521 void AutocompleteProviderTest::CopyResults() { |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 EXPECT_FALSE(search_provider_field_trial_triggered_in_session()); | 776 EXPECT_FALSE(search_provider_field_trial_triggered_in_session()); |
| 748 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); | 777 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); |
| 749 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path()); | 778 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0j4&", url.path()); |
| 750 | 779 |
| 751 // Test page classification and field trial triggered set. | 780 // Test page classification and field trial triggered set. |
| 752 set_search_provider_field_trial_triggered_in_session(true); | 781 set_search_provider_field_trial_triggered_in_session(true); |
| 753 EXPECT_TRUE(search_provider_field_trial_triggered_in_session()); | 782 EXPECT_TRUE(search_provider_field_trial_triggered_in_session()); |
| 754 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); | 783 url = GetDestinationURL(match, base::TimeDelta::FromMilliseconds(2456)); |
| 755 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path()); | 784 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1j4&", url.path()); |
| 756 } | 785 } |
| OLD | NEW |