| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/metrics/field_trial.h" | 6 #include "base/metrics/field_trial.h" |
| 7 #include "base/metrics/histogram_base.h" | 7 #include "base/metrics/histogram_base.h" |
| 8 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/common/renderer_preferences.h" | 32 #include "content/public/common/renderer_preferences.h" |
| 33 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 34 | 34 |
| 35 #if defined(ENABLE_SUPERVISED_USERS) | 35 #if defined(ENABLE_SUPERVISED_USERS) |
| 36 #include "chrome/browser/supervised_user/supervised_user_service.h" | 36 #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 37 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 37 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
| 38 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" | 38 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
| 39 #endif | 39 #endif |
| 40 | 40 |
| 41 namespace chrome { | 41 namespace search { |
| 42 | 42 |
| 43 class SearchTest : public BrowserWithTestWindowTest { | 43 class SearchTest : public BrowserWithTestWindowTest { |
| 44 protected: | 44 protected: |
| 45 void SetUp() override { | 45 void SetUp() override { |
| 46 BrowserWithTestWindowTest::SetUp(); | 46 BrowserWithTestWindowTest::SetUp(); |
| 47 field_trial_list_.reset(new base::FieldTrialList( | 47 field_trial_list_.reset(new base::FieldTrialList( |
| 48 new metrics::SHA1EntropyProvider("42"))); | 48 new metrics::SHA1EntropyProvider("42"))); |
| 49 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 49 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 50 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | 50 profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 51 TemplateURLService* template_url_service = | 51 TemplateURLService* template_url_service = |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 content::NavigationController& controller = contents->GetController(); | 382 content::NavigationController& controller = contents->GetController(); |
| 383 // Local NTP. | 383 // Local NTP. |
| 384 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); | 384 NavigateAndCommitActiveTab(GURL(chrome::kChromeSearchLocalNtpUrl)); |
| 385 EXPECT_TRUE(NavEntryIsInstantNTP(contents, | 385 EXPECT_TRUE(NavEntryIsInstantNTP(contents, |
| 386 controller.GetLastCommittedEntry())); | 386 controller.GetLastCommittedEntry())); |
| 387 // Instant page is not cacheable NTP. | 387 // Instant page is not cacheable NTP. |
| 388 NavigateAndCommitActiveTab(GetInstantURL(profile(), false)); | 388 NavigateAndCommitActiveTab(GetInstantURL(profile(), false)); |
| 389 EXPECT_FALSE(NavEntryIsInstantNTP(contents, | 389 EXPECT_FALSE(NavEntryIsInstantNTP(contents, |
| 390 controller.GetLastCommittedEntry())); | 390 controller.GetLastCommittedEntry())); |
| 391 // Test Cacheable NTP | 391 // Test Cacheable NTP |
| 392 NavigateAndCommitActiveTab(chrome::GetNewTabPageURL(profile())); | 392 NavigateAndCommitActiveTab(GetNewTabPageURL(profile())); |
| 393 EXPECT_TRUE(NavEntryIsInstantNTP(contents, | 393 EXPECT_TRUE(NavEntryIsInstantNTP(contents, |
| 394 controller.GetLastCommittedEntry())); | 394 controller.GetLastCommittedEntry())); |
| 395 } | 395 } |
| 396 | 396 |
| 397 TEST_F(SearchTest, InstantCacheableNTPNavigationEntryNewProfile) { | 397 TEST_F(SearchTest, InstantCacheableNTPNavigationEntryNewProfile) { |
| 398 SetSearchProvider(false, false); | 398 SetSearchProvider(false, false); |
| 399 AddTab(browser(), GURL(chrome::kChromeUINewTabURL)); | 399 AddTab(browser(), GURL(chrome::kChromeUINewTabURL)); |
| 400 content::WebContents* contents = | 400 content::WebContents* contents = |
| 401 browser()->tab_strip_model()->GetWebContentsAt(0); | 401 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 402 content::NavigationController& controller = contents->GetController(); | 402 content::NavigationController& controller = contents->GetController(); |
| 403 // Test virtual url chrome://newtab for first NTP of a new profile | 403 // Test virtual url chrome://newtab for first NTP of a new profile |
| 404 EXPECT_TRUE(NavEntryIsInstantNTP(contents, | 404 EXPECT_TRUE(NavEntryIsInstantNTP(contents, |
| 405 controller.GetLastCommittedEntry())); | 405 controller.GetLastCommittedEntry())); |
| 406 // The new_tab_url gets set after the first NTP is visible. | 406 // The new_tab_url gets set after the first NTP is visible. |
| 407 SetSearchProvider(true, false); | 407 SetSearchProvider(true, false); |
| 408 EXPECT_TRUE(NavEntryIsInstantNTP(contents, | 408 EXPECT_TRUE(NavEntryIsInstantNTP(contents, |
| 409 controller.GetLastCommittedEntry())); | 409 controller.GetLastCommittedEntry())); |
| 410 } | 410 } |
| 411 | 411 |
| 412 TEST_F(SearchTest, NoRewriteInIncognito) { | 412 TEST_F(SearchTest, NoRewriteInIncognito) { |
| 413 profile()->ForceIncognito(true); | 413 profile()->ForceIncognito(true); |
| 414 EXPECT_EQ(GURL(), chrome::GetNewTabPageURL(profile())); | 414 EXPECT_EQ(GURL(), GetNewTabPageURL(profile())); |
| 415 GURL new_tab_url(chrome::kChromeUINewTabURL); | 415 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 416 EXPECT_FALSE(HandleNewTabURLRewrite(&new_tab_url, profile())); | 416 EXPECT_FALSE(HandleNewTabURLRewrite(&new_tab_url, profile())); |
| 417 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), new_tab_url); | 417 EXPECT_EQ(GURL(chrome::kChromeUINewTabURL), new_tab_url); |
| 418 } | 418 } |
| 419 | 419 |
| 420 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsInsecure) { | 420 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsInsecure) { |
| 421 // Set an insecure new tab page URL and verify that it's ignored. | 421 // Set an insecure new tab page URL and verify that it's ignored. |
| 422 SetSearchProvider(true, true); | 422 SetSearchProvider(true, true); |
| 423 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), | 423 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 424 chrome::GetNewTabPageURL(profile())); | 424 GetNewTabPageURL(profile())); |
| 425 GURL new_tab_url(chrome::kChromeUINewTabURL); | 425 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 426 EXPECT_TRUE(HandleNewTabURLRewrite(&new_tab_url, profile())); | 426 EXPECT_TRUE(HandleNewTabURLRewrite(&new_tab_url, profile())); |
| 427 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), new_tab_url); | 427 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), new_tab_url); |
| 428 } | 428 } |
| 429 | 429 |
| 430 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsNotSet) { | 430 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsNotSet) { |
| 431 // Set an insecure new tab page URL and verify that it's ignored. | 431 // Set an insecure new tab page URL and verify that it's ignored. |
| 432 SetSearchProvider(false, true); | 432 SetSearchProvider(false, true); |
| 433 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), | 433 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 434 chrome::GetNewTabPageURL(profile())); | 434 GetNewTabPageURL(profile())); |
| 435 GURL new_tab_url(chrome::kChromeUINewTabURL); | 435 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 436 EXPECT_TRUE(HandleNewTabURLRewrite(&new_tab_url, profile())); | 436 EXPECT_TRUE(HandleNewTabURLRewrite(&new_tab_url, profile())); |
| 437 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), new_tab_url); | 437 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), new_tab_url); |
| 438 } | 438 } |
| 439 | 439 |
| 440 #if defined(ENABLE_SUPERVISED_USERS) | 440 #if defined(ENABLE_SUPERVISED_USERS) |
| 441 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsBlockedForSupervisedUser) { | 441 TEST_F(SearchTest, UseLocalNTPIfNTPURLIsBlockedForSupervisedUser) { |
| 442 // Block access to foo.com in the URL filter. | 442 // Block access to foo.com in the URL filter. |
| 443 SupervisedUserService* supervised_user_service = | 443 SupervisedUserService* supervised_user_service = |
| 444 SupervisedUserServiceFactory::GetForProfile(profile()); | 444 SupervisedUserServiceFactory::GetForProfile(profile()); |
| 445 SupervisedUserURLFilter* url_filter = | 445 SupervisedUserURLFilter* url_filter = |
| 446 supervised_user_service->GetURLFilterForUIThread(); | 446 supervised_user_service->GetURLFilterForUIThread(); |
| 447 std::map<std::string, bool> hosts; | 447 std::map<std::string, bool> hosts; |
| 448 hosts["foo.com"] = false; | 448 hosts["foo.com"] = false; |
| 449 url_filter->SetManualHosts(&hosts); | 449 url_filter->SetManualHosts(&hosts); |
| 450 | 450 |
| 451 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), | 451 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), |
| 452 chrome::GetNewTabPageURL(profile())); | 452 GetNewTabPageURL(profile())); |
| 453 GURL new_tab_url(chrome::kChromeUINewTabURL); | 453 GURL new_tab_url(chrome::kChromeUINewTabURL); |
| 454 EXPECT_TRUE(HandleNewTabURLRewrite(&new_tab_url, profile())); | 454 EXPECT_TRUE(HandleNewTabURLRewrite(&new_tab_url, profile())); |
| 455 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), new_tab_url); | 455 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), new_tab_url); |
| 456 EXPECT_EQ(GURL(), GetInstantURL(profile(), false)); | 456 EXPECT_EQ(GURL(), GetInstantURL(profile(), false)); |
| 457 } | 457 } |
| 458 #endif | 458 #endif |
| 459 | 459 |
| 460 TEST_F(SearchTest, GetInstantURL) { | 460 TEST_F(SearchTest, GetInstantURL) { |
| 461 // No Instant URL because "strk" is missing. | 461 // No Instant URL because "strk" is missing. |
| 462 SetDefaultInstantTemplateUrl(false); | 462 SetDefaultInstantTemplateUrl(false); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 | 547 |
| 548 // If we specify extra search query params, they should be inserted into the | 548 // If we specify extra search query params, they should be inserted into the |
| 549 // query portion of the instant URL. | 549 // query portion of the instant URL. |
| 550 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 550 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 551 switches::kExtraSearchQueryParams, "a=b"); | 551 switches::kExtraSearchQueryParams, "a=b"); |
| 552 instant_url = GetInstantURL(profile(), false); | 552 instant_url = GetInstantURL(profile(), false); |
| 553 ASSERT_TRUE(instant_url.is_valid()); | 553 ASSERT_TRUE(instant_url.is_valid()); |
| 554 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec()); | 554 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec()); |
| 555 } | 555 } |
| 556 | 556 |
| 557 TEST_F(SearchTest, ShouldPrefetchSearchResults_InstantExtendedAPIEnabled) { | |
| 558 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 559 "EmbeddedSearch", "Group1 espv:2")); | |
| 560 #if defined(OS_IOS) | |
| 561 EXPECT_EQ(1ul, EmbeddedSearchPageVersion()); | |
| 562 EXPECT_TRUE(ShouldPrefetchSearchResults()); | |
| 563 #else | |
| 564 EXPECT_EQ(2ul, EmbeddedSearchPageVersion()); | |
| 565 EXPECT_TRUE(ShouldPrefetchSearchResults()); | |
| 566 #endif | |
| 567 } | |
| 568 | |
| 569 TEST_F(SearchTest, ShouldPrefetchSearchResults_Default) { | |
| 570 #if defined(OS_IOS) | |
| 571 EXPECT_FALSE(ShouldPrefetchSearchResults()); | |
| 572 #else | |
| 573 EXPECT_TRUE(ShouldPrefetchSearchResults()); | |
| 574 #endif | |
| 575 } | |
| 576 | |
| 577 TEST_F(SearchTest, ShouldReuseInstantSearchBasePage_Default) { | |
| 578 #if defined(OS_IOS) | |
| 579 EXPECT_FALSE(ShouldReuseInstantSearchBasePage()); | |
| 580 #else | |
| 581 EXPECT_TRUE(ShouldReuseInstantSearchBasePage()); | |
| 582 #endif | |
| 583 } | |
| 584 | |
| 585 TEST_F(SearchTest, ShouldAllowPrefetchNonDefaultMatch_DisabledViaFieldTrial) { | |
| 586 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 587 "EmbeddedSearch", "Group1 espv:89 allow_prefetch_non_default_match:0")); | |
| 588 EXPECT_FALSE(ShouldAllowPrefetchNonDefaultMatch()); | |
| 589 EXPECT_EQ(89ul, EmbeddedSearchPageVersion()); | |
| 590 } | |
| 591 | |
| 592 TEST_F(SearchTest, ShouldAllowPrefetchNonDefaultMatch_EnabledViaFieldTrial) { | |
| 593 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 594 "EmbeddedSearch", "Group1 espv:80 allow_prefetch_non_default_match:1")); | |
| 595 EXPECT_TRUE(ShouldAllowPrefetchNonDefaultMatch()); | |
| 596 EXPECT_EQ(80ul, EmbeddedSearchPageVersion()); | |
| 597 } | |
| 598 | |
| 599 TEST_F(SearchTest, ShouldUseAltInstantURL_DisabledViaFieldTrial) { | 557 TEST_F(SearchTest, ShouldUseAltInstantURL_DisabledViaFieldTrial) { |
| 600 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 558 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 601 "EmbeddedSearch", "Group1 espv:8 use_alternate_instant_url:0")); | 559 "EmbeddedSearch", "Group1 espv:8 use_alternate_instant_url:0")); |
| 602 EXPECT_FALSE(ShouldUseAltInstantURL()); | 560 EXPECT_FALSE(ShouldUseAltInstantURL()); |
| 603 } | 561 } |
| 604 | 562 |
| 605 TEST_F(SearchTest, ShouldUseAltInstantURL_EnabledViaFieldTrial) { | 563 TEST_F(SearchTest, ShouldUseAltInstantURL_EnabledViaFieldTrial) { |
| 606 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | 564 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( |
| 607 "EmbeddedSearch", "Group1 espv:8 use_alternate_instant_url:1")); | 565 "EmbeddedSearch", "Group1 espv:8 use_alternate_instant_url:1")); |
| 608 EXPECT_TRUE(ShouldUseAltInstantURL()); | 566 EXPECT_TRUE(ShouldUseAltInstantURL()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 "EmbeddedSearch", "Group1 espv:2 google_local_ntp:0")); | 615 "EmbeddedSearch", "Group1 espv:2 google_local_ntp:0")); |
| 658 EXPECT_FALSE(ShouldShowGoogleLocalNTP()); | 616 EXPECT_FALSE(ShouldShowGoogleLocalNTP()); |
| 659 } | 617 } |
| 660 | 618 |
| 661 | 619 |
| 662 TEST_F(SearchTest, IsNTPURL) { | 620 TEST_F(SearchTest, IsNTPURL) { |
| 663 GURL invalid_url; | 621 GURL invalid_url; |
| 664 GURL ntp_url(chrome::kChromeUINewTabURL); | 622 GURL ntp_url(chrome::kChromeUINewTabURL); |
| 665 GURL local_ntp_url(GetLocalInstantURL(profile())); | 623 GURL local_ntp_url(GetLocalInstantURL(profile())); |
| 666 | 624 |
| 667 EXPECT_FALSE(chrome::IsNTPURL(invalid_url, profile())); | 625 EXPECT_FALSE(IsNTPURL(invalid_url, profile())); |
| 668 // No margin. | 626 // No margin. |
| 669 EnableQueryExtractionForTesting(); | 627 EnableQueryExtractionForTesting(); |
| 670 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 628 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
| 671 GURL remote_ntp_url(GetInstantURL(profile(), false)); | 629 GURL remote_ntp_url(GetInstantURL(profile(), false)); |
| 672 GURL search_url_with_search_terms("https://foo.com/url?strk&bar=abc"); | 630 GURL search_url_with_search_terms("https://foo.com/url?strk&bar=abc"); |
| 673 GURL search_url_without_search_terms("https://foo.com/url?strk&bar"); | 631 GURL search_url_without_search_terms("https://foo.com/url?strk&bar"); |
| 674 | 632 |
| 675 EXPECT_FALSE(chrome::IsNTPURL(ntp_url, profile())); | 633 EXPECT_FALSE(IsNTPURL(ntp_url, profile())); |
| 676 EXPECT_TRUE(chrome::IsNTPURL(local_ntp_url, profile())); | 634 EXPECT_TRUE(IsNTPURL(local_ntp_url, profile())); |
| 677 EXPECT_TRUE(chrome::IsNTPURL(remote_ntp_url, profile())); | 635 EXPECT_TRUE(IsNTPURL(remote_ntp_url, profile())); |
| 678 EXPECT_FALSE(chrome::IsNTPURL(search_url_with_search_terms, profile())); | 636 EXPECT_FALSE(IsNTPURL(search_url_with_search_terms, profile())); |
| 679 EXPECT_TRUE(chrome::IsNTPURL(search_url_without_search_terms, profile())); | 637 EXPECT_TRUE(IsNTPURL(search_url_without_search_terms, profile())); |
| 680 | 638 |
| 681 EXPECT_FALSE(chrome::IsNTPURL(ntp_url, NULL)); | 639 EXPECT_FALSE(IsNTPURL(ntp_url, NULL)); |
| 682 EXPECT_FALSE(chrome::IsNTPURL(local_ntp_url, NULL)); | 640 EXPECT_FALSE(IsNTPURL(local_ntp_url, NULL)); |
| 683 EXPECT_FALSE(chrome::IsNTPURL(remote_ntp_url, NULL)); | 641 EXPECT_FALSE(IsNTPURL(remote_ntp_url, NULL)); |
| 684 EXPECT_FALSE(chrome::IsNTPURL(search_url_with_search_terms, NULL)); | 642 EXPECT_FALSE(IsNTPURL(search_url_with_search_terms, NULL)); |
| 685 EXPECT_FALSE(chrome::IsNTPURL(search_url_without_search_terms, NULL)); | 643 EXPECT_FALSE(IsNTPURL(search_url_without_search_terms, NULL)); |
| 686 } | 644 } |
| 687 | 645 |
| 688 TEST_F(SearchTest, GetSearchURLs) { | 646 TEST_F(SearchTest, GetSearchURLs) { |
| 689 std::vector<GURL> search_urls = GetSearchURLs(profile()); | 647 std::vector<GURL> search_urls = GetSearchURLs(profile()); |
| 690 EXPECT_EQ(2U, search_urls.size()); | 648 EXPECT_EQ(2U, search_urls.size()); |
| 691 EXPECT_EQ("http://foo.com/alt#quux=", search_urls[0].spec()); | 649 EXPECT_EQ("http://foo.com/alt#quux=", search_urls[0].spec()); |
| 692 EXPECT_EQ("http://foo.com/url?bar=", search_urls[1].spec()); | 650 EXPECT_EQ("http://foo.com/url?bar=", search_urls[1].spec()); |
| 693 } | 651 } |
| 694 | 652 |
| 695 TEST_F(SearchTest, GetSearchResultPrefetchBaseURL) { | 653 TEST_F(SearchTest, GetSearchResultPrefetchBaseURL) { |
| 696 #if defined(OS_IOS) | 654 #if defined(OS_IOS) |
| 697 EXPECT_FALSE(ShouldPrefetchSearchResults()); | 655 EXPECT_FALSE(ShouldPrefetchSearchResults()); |
| 698 EXPECT_EQ(GURL(), GetSearchResultPrefetchBaseURL(profile())); | 656 EXPECT_EQ(GURL(), GetSearchResultPrefetchBaseURL(profile())); |
| 699 #else | 657 #else |
| 700 EXPECT_TRUE(ShouldPrefetchSearchResults()); | 658 EXPECT_TRUE(ShouldPrefetchSearchResults()); |
| 701 EXPECT_EQ(GURL("https://foo.com/instant?ion=1&foo=foo#foo=foo&strk"), | 659 EXPECT_EQ(GURL("https://foo.com/instant?ion=1&foo=foo#foo=foo&strk"), |
| 702 GetSearchResultPrefetchBaseURL(profile())); | 660 GetSearchResultPrefetchBaseURL(profile())); |
| 703 #endif | 661 #endif |
| 704 } | 662 } |
| 705 | 663 |
| 706 TEST_F(SearchTest, ForceInstantResultsParam) { | |
| 707 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", | |
| 708 "Group1 espv:2")); | |
| 709 EXPECT_TRUE(IsInstantExtendedAPIEnabled()); | |
| 710 EXPECT_EQ("ion=1&", ForceInstantResultsParam(true)); | |
| 711 EXPECT_EQ(std::string(), ForceInstantResultsParam(false)); | |
| 712 } | |
| 713 | |
| 714 struct ExtractSearchTermsTestCase { | 664 struct ExtractSearchTermsTestCase { |
| 715 const char* url; | 665 const char* url; |
| 716 const char* expected_result; | 666 const char* expected_result; |
| 717 const char* comment; | 667 const char* comment; |
| 718 }; | 668 }; |
| 719 | 669 |
| 720 TEST_F(SearchTest, ExtractSearchTermsFromURL) { | 670 TEST_F(SearchTest, ExtractSearchTermsFromURL) { |
| 721 const ExtractSearchTermsTestCase kTestCases[] = { | 671 const ExtractSearchTermsTestCase kTestCases[] = { |
| 722 {chrome::kChromeSearchLocalNtpUrl, "", "NTP url"}, | 672 {chrome::kChromeSearchLocalNtpUrl, "", "NTP url"}, |
| 723 {"https://foo.com/instant?strk", "", "Invalid search url"}, | 673 {"https://foo.com/instant?strk", "", "Invalid search url"}, |
| 724 {"https://foo.com/instant#strk", "", "Invalid search url"}, | 674 {"https://foo.com/instant#strk", "", "Invalid search url"}, |
| 725 {"https://foo.com/alt#quux=foo", "foo", "Valid search url"}, | 675 {"https://foo.com/alt#quux=foo", "foo", "Valid search url"}, |
| 726 {"https://foo.com/alt#quux=foo&strk", "foo", "Valid search url"} | 676 {"https://foo.com/alt#quux=foo&strk", "foo", "Valid search url"} |
| 727 }; | 677 }; |
| 728 | 678 |
| 729 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 679 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 730 const ExtractSearchTermsTestCase& test = kTestCases[i]; | 680 const ExtractSearchTermsTestCase& test = kTestCases[i]; |
| 731 EXPECT_EQ( | 681 EXPECT_EQ(test.expected_result, |
| 732 test.expected_result, | 682 base::UTF16ToASCII( |
| 733 base::UTF16ToASCII(chrome::ExtractSearchTermsFromURL(profile(), | 683 ExtractSearchTermsFromURL(profile(), GURL(test.url)))) |
| 734 GURL(test.url)))) | 684 << test.url << " " << test.comment; |
| 735 << test.url << " " << test.comment; | |
| 736 } | 685 } |
| 737 } | 686 } |
| 738 | 687 |
| 739 struct QueryExtractionAllowedTestCase { | 688 struct QueryExtractionAllowedTestCase { |
| 740 const char* url; | 689 const char* url; |
| 741 bool expected_result; | 690 bool expected_result; |
| 742 const char* comment; | 691 const char* comment; |
| 743 }; | 692 }; |
| 744 | 693 |
| 745 TEST_F(SearchTest, IsQueryExtractionAllowedForURL) { | 694 TEST_F(SearchTest, IsQueryExtractionAllowedForURL) { |
| 746 const QueryExtractionAllowedTestCase kTestCases[] = { | 695 const QueryExtractionAllowedTestCase kTestCases[] = { |
| 747 {"http://foo.com/instant?strk", false, "HTTP URL"}, | 696 {"http://foo.com/instant?strk", false, "HTTP URL"}, |
| 748 {"https://foo.com/instant?strk", true, "Valid URL"}, | 697 {"https://foo.com/instant?strk", true, "Valid URL"}, |
| 749 {"https://foo.com/instant?", false, | 698 {"https://foo.com/instant?", false, |
| 750 "No search terms replacement key"}, | 699 "No search terms replacement key"}, |
| 751 {"https://foo.com/alt#quux=foo", false, | 700 {"https://foo.com/alt#quux=foo", false, |
| 752 "No search terms replacement key"}, | 701 "No search terms replacement key"}, |
| 753 {"https://foo.com/alt#quux=foo&strk", true, "Valid search url"} | 702 {"https://foo.com/alt#quux=foo&strk", true, "Valid search url"} |
| 754 }; | 703 }; |
| 755 | 704 |
| 756 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 705 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 757 const QueryExtractionAllowedTestCase& test = kTestCases[i]; | 706 const QueryExtractionAllowedTestCase& test = kTestCases[i]; |
| 758 EXPECT_EQ(test.expected_result, | 707 EXPECT_EQ(test.expected_result, |
| 759 chrome::IsQueryExtractionAllowedForURL(profile(), GURL(test.url))) | 708 IsQueryExtractionAllowedForURL(profile(), GURL(test.url))) |
| 760 << test.url << " " << test.comment; | 709 << test.url << " " << test.comment; |
| 761 } | 710 } |
| 762 } | 711 } |
| 763 | 712 |
| 764 class SearchURLTest : public SearchTest { | 713 class SearchURLTest : public SearchTest { |
| 765 protected: | 714 protected: |
| 766 void SetSearchProvider(bool set_ntp_url, bool insecure_ntp_url) override { | 715 void SetSearchProvider(bool set_ntp_url, bool insecure_ntp_url) override { |
| 767 TemplateURLService* template_url_service = | 716 TemplateURLService* template_url_service = |
| 768 TemplateURLServiceFactory::GetForProfile(profile()); | 717 TemplateURLServiceFactory::GetForProfile(profile()); |
| 769 TemplateURLData data; | 718 TemplateURLData data; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 798 EXPECT_FALSE(IsQueryExtractionEnabled()); | 747 EXPECT_FALSE(IsQueryExtractionEnabled()); |
| 799 TemplateURLRef::SearchTermsArgs search_terms_args(base::ASCIIToUTF16("foo")); | 748 TemplateURLRef::SearchTermsArgs search_terms_args(base::ASCIIToUTF16("foo")); |
| 800 GURL result(template_url_->url_ref().ReplaceSearchTerms( | 749 GURL result(template_url_->url_ref().ReplaceSearchTerms( |
| 801 search_terms_args, UIThreadSearchTermsData(profile()))); | 750 search_terms_args, UIThreadSearchTermsData(profile()))); |
| 802 ASSERT_TRUE(result.is_valid()); | 751 ASSERT_TRUE(result.is_valid()); |
| 803 // Query extraction is disabled. Make sure | 752 // Query extraction is disabled. Make sure |
| 804 // {google:instantExtendedEnabledParameter} is not set in the search URL. | 753 // {google:instantExtendedEnabledParameter} is not set in the search URL. |
| 805 EXPECT_EQ("http://www.google.com/search?q=foo", result.spec()); | 754 EXPECT_EQ("http://www.google.com/search?q=foo", result.spec()); |
| 806 } | 755 } |
| 807 | 756 |
| 808 typedef SearchTest InstantExtendedEnabledParamTest; | 757 } // namespace search |
| 809 | |
| 810 TEST_F(InstantExtendedEnabledParamTest, QueryExtractionDisabled) { | |
| 811 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch", | |
| 812 "Group1 espv:12")); | |
| 813 // Make sure InstantExtendedEnabledParam() returns an empty string for search | |
| 814 // requests. | |
| 815 #if defined(OS_IOS) | |
| 816 // Query extraction is always enabled on mobile. | |
| 817 EXPECT_TRUE(IsQueryExtractionEnabled()); | |
| 818 EXPECT_EQ("espv=12&", InstantExtendedEnabledParam(true)); | |
| 819 #else | |
| 820 EXPECT_FALSE(IsQueryExtractionEnabled()); | |
| 821 EXPECT_EQ("", InstantExtendedEnabledParam(true)); | |
| 822 #endif | |
| 823 EXPECT_EQ("espv=12&", InstantExtendedEnabledParam(false)); | |
| 824 } | |
| 825 | |
| 826 TEST_F(InstantExtendedEnabledParamTest, QueryExtractionEnabled) { | |
| 827 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 828 "EmbeddedSearch", "Group1 espv:10 query_extraction:1")); | |
| 829 EXPECT_TRUE(IsQueryExtractionEnabled()); | |
| 830 // Make sure InstantExtendedEnabledParam() returns a non-empty param string | |
| 831 // for search requests. | |
| 832 EXPECT_EQ("espv=10&", InstantExtendedEnabledParam(true)); | |
| 833 EXPECT_EQ("espv=10&", InstantExtendedEnabledParam(false)); | |
| 834 } | |
| 835 | |
| 836 TEST_F(InstantExtendedEnabledParamTest, UseDefaultEmbeddedSearchPageVersion) { | |
| 837 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 838 "EmbeddedSearch", "Group1 espv:-1 query_extraction:1")); | |
| 839 EXPECT_TRUE(IsQueryExtractionEnabled()); | |
| 840 #if defined(OS_IOS) | |
| 841 EXPECT_EQ("espv=1&", InstantExtendedEnabledParam(true)); | |
| 842 EXPECT_EQ("espv=1&", InstantExtendedEnabledParam(false)); | |
| 843 #else | |
| 844 EXPECT_EQ("espv=2&", InstantExtendedEnabledParam(true)); | |
| 845 EXPECT_EQ("espv=2&", InstantExtendedEnabledParam(false)); | |
| 846 #endif | |
| 847 } | |
| 848 | |
| 849 typedef SearchTest IsQueryExtractionEnabledTest; | |
| 850 | |
| 851 TEST_F(IsQueryExtractionEnabledTest, NotSet) { | |
| 852 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 853 "EmbeddedSearch", "Group1 espv:2")); | |
| 854 EXPECT_TRUE(IsInstantExtendedAPIEnabled()); | |
| 855 EXPECT_FALSE(IsQueryExtractionEnabled()); | |
| 856 EXPECT_EQ(2ul, EmbeddedSearchPageVersion()); | |
| 857 } | |
| 858 | |
| 859 TEST_F(IsQueryExtractionEnabledTest, EnabledViaFieldTrial) { | |
| 860 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 861 "EmbeddedSearch", "Group1 espv:2 query_extraction:1")); | |
| 862 EXPECT_TRUE(IsInstantExtendedAPIEnabled()); | |
| 863 EXPECT_TRUE(IsQueryExtractionEnabled()); | |
| 864 EXPECT_EQ(2ul, EmbeddedSearchPageVersion()); | |
| 865 } | |
| 866 | |
| 867 TEST_F(IsQueryExtractionEnabledTest, DisabledViaFieldTrial) { | |
| 868 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 869 "EmbeddedSearch", "Group1 espv:2 query_extraction:0")); | |
| 870 EXPECT_TRUE(IsInstantExtendedAPIEnabled()); | |
| 871 EXPECT_FALSE(IsQueryExtractionEnabled()); | |
| 872 EXPECT_EQ(2ul, EmbeddedSearchPageVersion()); | |
| 873 } | |
| 874 | |
| 875 TEST_F(IsQueryExtractionEnabledTest, EnabledViaCommandLine) { | |
| 876 EnableQueryExtractionForTesting(); | |
| 877 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial( | |
| 878 "EmbeddedSearch", "Group1 espv:2 query_extraction:0")); | |
| 879 EXPECT_TRUE(IsInstantExtendedAPIEnabled()); | |
| 880 EXPECT_TRUE(IsQueryExtractionEnabled()); | |
| 881 EXPECT_EQ(2ul, EmbeddedSearchPageVersion()); | |
| 882 } | |
| 883 | |
| 884 } // namespace chrome | |
| OLD | NEW |