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/autocomplete_provider.h" | 5 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 15 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
15 #include "chrome/browser/autocomplete/autocomplete_input.h" | 16 #include "chrome/browser/autocomplete/autocomplete_input.h" |
16 #include "chrome/browser/autocomplete/autocomplete_match.h" | 17 #include "chrome/browser/autocomplete/autocomplete_match.h" |
17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 18 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
18 #include "chrome/browser/autocomplete/keyword_provider.h" | 19 #include "chrome/browser/autocomplete/keyword_provider.h" |
19 #include "chrome/browser/autocomplete/search_provider.h" | 20 #include "chrome/browser/autocomplete/search_provider.h" |
20 #include "chrome/browser/search_engines/template_url.h" | 21 #include "chrome/browser/search_engines/template_url.h" |
21 #include "chrome/browser/search_engines/template_url_service.h" | 22 #include "chrome/browser/search_engines/template_url_service.h" |
22 #include "chrome/browser/search_engines/template_url_service_factory.h" | 23 #include "chrome/browser/search_engines/template_url_service_factory.h" |
23 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 25 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/test/base/testing_browser_process.h" | 26 #include "chrome/test/base/testing_browser_process.h" |
25 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
26 #include "content/public/browser/notification_observer.h" | 28 #include "content/public/browser/notification_observer.h" |
27 #include "content/public/browser/notification_registrar.h" | 29 #include "content/public/browser/notification_registrar.h" |
28 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 | 32 |
31 static std::ostream& operator<<(std::ostream& os, | 33 static std::ostream& operator<<(std::ostream& os, |
32 const AutocompleteResult::const_iterator& it) { | 34 const AutocompleteResult::const_iterator& it) { |
33 return os << static_cast<const AutocompleteMatch*>(&(*it)); | 35 return os << static_cast<const AutocompleteMatch*>(&(*it)); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 void RunAssistedQueryStatsTest( | 180 void RunAssistedQueryStatsTest( |
179 const AssistedQueryStatsTestData* aqs_test_data, | 181 const AssistedQueryStatsTestData* aqs_test_data, |
180 size_t size); | 182 size_t size); |
181 | 183 |
182 void RunQuery(const string16 query); | 184 void RunQuery(const string16 query); |
183 | 185 |
184 void ResetControllerWithKeywordAndSearchProviders(); | 186 void ResetControllerWithKeywordAndSearchProviders(); |
185 void ResetControllerWithKeywordProvider(); | 187 void ResetControllerWithKeywordProvider(); |
186 void RunExactKeymatchTest(bool allow_exact_keyword_match); | 188 void RunExactKeymatchTest(bool allow_exact_keyword_match); |
187 | 189 |
| 190 void CopyResults(); |
| 191 |
188 AutocompleteResult result_; | 192 AutocompleteResult result_; |
189 scoped_ptr<AutocompleteController> controller_; | 193 scoped_ptr<AutocompleteController> controller_; |
190 | 194 |
191 private: | 195 private: |
192 // content::NotificationObserver | 196 // content::NotificationObserver: |
193 virtual void Observe(int type, | 197 virtual void Observe(int type, |
194 const content::NotificationSource& source, | 198 const content::NotificationSource& source, |
195 const content::NotificationDetails& details) OVERRIDE; | 199 const content::NotificationDetails& details) OVERRIDE; |
196 | 200 |
197 base::MessageLoopForUI message_loop_; | 201 base::MessageLoopForUI message_loop_; |
198 content::NotificationRegistrar registrar_; | 202 content::NotificationRegistrar registrar_; |
199 TestingProfile profile_; | 203 TestingProfile profile_; |
200 }; | 204 }; |
201 | 205 |
202 void AutocompleteProviderTest::RegisterTemplateURL( | 206 void AutocompleteProviderTest::RegisterTemplateURL( |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 allow_exact_keyword_match, AutocompleteInput::SYNCHRONOUS_MATCHES)); | 413 allow_exact_keyword_match, AutocompleteInput::SYNCHRONOUS_MATCHES)); |
410 EXPECT_TRUE(controller_->done()); | 414 EXPECT_TRUE(controller_->done()); |
411 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH, | 415 EXPECT_EQ(AutocompleteProvider::TYPE_SEARCH, |
412 controller_->result().default_match()->provider->type()); | 416 controller_->result().default_match()->provider->type()); |
413 EXPECT_EQ(allow_exact_keyword_match ? | 417 EXPECT_EQ(allow_exact_keyword_match ? |
414 AutocompleteMatchType::SEARCH_OTHER_ENGINE : | 418 AutocompleteMatchType::SEARCH_OTHER_ENGINE : |
415 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 419 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
416 controller_->result().default_match()->type); | 420 controller_->result().default_match()->type); |
417 } | 421 } |
418 | 422 |
| 423 void AutocompleteProviderTest::CopyResults() { |
| 424 result_.CopyFrom(controller_->result()); |
| 425 } |
| 426 |
419 void AutocompleteProviderTest::Observe( | 427 void AutocompleteProviderTest::Observe( |
420 int type, | 428 int type, |
421 const content::NotificationSource& source, | 429 const content::NotificationSource& source, |
422 const content::NotificationDetails& details) { | 430 const content::NotificationDetails& details) { |
423 if (controller_->done()) { | 431 if (controller_->done()) { |
424 result_.CopyFrom(controller_->result()); | 432 CopyResults(); |
425 base::MessageLoop::current()->Quit(); | 433 base::MessageLoop::current()->Quit(); |
426 } | 434 } |
427 } | 435 } |
428 | 436 |
429 // Tests that the default selection is set properly when updating results. | 437 // Tests that the default selection is set properly when updating results. |
430 TEST_F(AutocompleteProviderTest, Query) { | 438 TEST_F(AutocompleteProviderTest, Query) { |
431 TestProvider* provider1 = NULL; | 439 TestProvider* provider1 = NULL; |
432 TestProvider* provider2 = NULL; | 440 TestProvider* provider2 = NULL; |
433 ResetControllerWithTestProviders(false, &provider1, &provider2); | 441 ResetControllerWithTestProviders(false, &provider1, &provider2); |
434 RunTest(); | 442 RunTest(); |
435 | 443 |
436 // Make sure the default match gets set to the highest relevance match. The | 444 // Make sure the default match gets set to the highest relevance match. The |
437 // highest relevance matches should come from the second provider. | 445 // highest relevance matches should come from the second provider. |
438 EXPECT_EQ(kResultsPerProvider * 2, result_.size()); // two providers | 446 EXPECT_EQ(kResultsPerProvider * 2, result_.size()); // two providers |
439 ASSERT_NE(result_.end(), result_.default_match()); | 447 ASSERT_NE(result_.end(), result_.default_match()); |
440 EXPECT_EQ(provider2, result_.default_match()->provider); | 448 EXPECT_EQ(provider2, result_.default_match()->provider); |
441 } | 449 } |
442 | 450 |
443 // Tests assisted query stats. | 451 // Tests assisted query stats. |
444 TEST_F(AutocompleteProviderTest, AssistedQueryStats) { | 452 TEST_F(AutocompleteProviderTest, AssistedQueryStats) { |
445 ResetControllerWithTestProviders(false, NULL, NULL); | 453 ResetControllerWithTestProviders(false, NULL, NULL); |
446 RunTest(); | 454 RunTest(); |
447 | 455 |
448 EXPECT_EQ(kResultsPerProvider * 2, result_.size()); // two providers | 456 ASSERT_EQ(kResultsPerProvider * 2, result_.size()); // two providers |
449 | 457 |
450 // Now, check the results from the second provider, as they should not have | 458 // Now, check the results from the second provider, as they should not have |
451 // assisted query stats set. | 459 // assisted query stats set. |
452 for (size_t i = 0; i < kResultsPerProvider; ++i) { | 460 for (size_t i = 0; i < kResultsPerProvider; ++i) { |
453 EXPECT_TRUE( | 461 EXPECT_TRUE( |
454 result_.match_at(i)->search_terms_args->assisted_query_stats.empty()); | 462 result_.match_at(i)->search_terms_args->assisted_query_stats.empty()); |
455 } | 463 } |
456 // The first provider has a test keyword, so AQS should be non-empty. | 464 // The first provider has a test keyword, so AQS should be non-empty. |
457 for (size_t i = kResultsPerProvider; i < kResultsPerProvider * 2; ++i) { | 465 for (size_t i = kResultsPerProvider; i < kResultsPerProvider * 2; ++i) { |
458 EXPECT_FALSE( | 466 EXPECT_FALSE( |
(...skipping 14 matching lines...) Expand all Loading... |
473 i != result_.end(); ++i) | 481 i != result_.end(); ++i) |
474 EXPECT_EQ(provider2, i->provider); | 482 EXPECT_EQ(provider2, i->provider); |
475 } | 483 } |
476 | 484 |
477 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { | 485 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { |
478 ResetControllerWithKeywordAndSearchProviders(); | 486 ResetControllerWithKeywordAndSearchProviders(); |
479 RunExactKeymatchTest(true); | 487 RunExactKeymatchTest(true); |
480 RunExactKeymatchTest(false); | 488 RunExactKeymatchTest(false); |
481 } | 489 } |
482 | 490 |
| 491 // Ensures matches from (only) the default search provider respect any extra |
| 492 // query params set on the command line. |
| 493 TEST_F(AutocompleteProviderTest, ExtraQueryParams) { |
| 494 ResetControllerWithKeywordAndSearchProviders(); |
| 495 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 496 switches::kExtraSearchQueryParams, "a=b"); |
| 497 RunExactKeymatchTest(true); |
| 498 CopyResults(); |
| 499 ASSERT_EQ(2, result_.size()); |
| 500 EXPECT_EQ("http://keyword/test", |
| 501 result_.match_at(0)->destination_url.possibly_invalid_spec()); |
| 502 EXPECT_EQ("http://defaultturl/k%20test?a=b", |
| 503 result_.match_at(1)->destination_url.possibly_invalid_spec()); |
| 504 } |
| 505 |
483 // Test that redundant associated keywords are removed. | 506 // Test that redundant associated keywords are removed. |
484 TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) { | 507 TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) { |
485 ResetControllerWithKeywordProvider(); | 508 ResetControllerWithKeywordProvider(); |
486 | 509 |
487 // Get the controller's internal members in the correct state. | 510 // Get the controller's internal members in the correct state. |
488 RunQuery(ASCIIToUTF16("fo")); | 511 RunQuery(ASCIIToUTF16("fo")); |
489 | 512 |
490 { | 513 { |
491 KeywordTestData duplicate_url[] = { | 514 KeywordTestData duplicate_url[] = { |
492 { ASCIIToUTF16("fo"), string16(), false }, | 515 { ASCIIToUTF16("fo"), string16(), false }, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0&", url.path()); | 629 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j0&", url.path()); |
607 | 630 |
608 // Test field trial triggered bit set. | 631 // Test field trial triggered bit set. |
609 controller_->search_provider_->field_trial_triggered_in_session_ = true; | 632 controller_->search_provider_->field_trial_triggered_in_session_ = true; |
610 EXPECT_TRUE( | 633 EXPECT_TRUE( |
611 controller_->search_provider_->field_trial_triggered_in_session()); | 634 controller_->search_provider_->field_trial_triggered_in_session()); |
612 url = controller_->GetDestinationURL(match, | 635 url = controller_->GetDestinationURL(match, |
613 base::TimeDelta::FromMilliseconds(2456)); | 636 base::TimeDelta::FromMilliseconds(2456)); |
614 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1&", url.path()); | 637 EXPECT_EQ("//aqs=chrome.0.69i57j69i58j5l2j0l3j69i59.2456j1&", url.path()); |
615 } | 638 } |
OLD | NEW |