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/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "chrome/browser/search/search.h" | 11 #include "chrome/browser/search/search.h" |
| 12 #include "chrome/browser/search_engines/search_terms_data.h" |
12 #include "chrome/browser/search_engines/template_url_service.h" | 13 #include "chrome/browser/search_engines/template_url_service.h" |
13 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/metrics/entropy_provider.h" | 17 #include "chrome/common/metrics/entropy_provider.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
19 #include "chrome/test/base/browser_with_test_window_test.h" | 20 #include "chrome/test/base/browser_with_test_window_test.h" |
20 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 }; | 389 }; |
389 | 390 |
390 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 391 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
391 const SearchTestCase& test = kTestCases[i]; | 392 const SearchTestCase& test = kTestCases[i]; |
392 EXPECT_EQ(test.expected_result, | 393 EXPECT_EQ(test.expected_result, |
393 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) | 394 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) |
394 << test.url << " " << test.comment; | 395 << test.url << " " << test.comment; |
395 } | 396 } |
396 } | 397 } |
397 | 398 |
398 TEST_F(SearchTest, CoerceCommandLineURLToTemplateURL) { | |
399 TemplateURL* template_url = | |
400 TemplateURLServiceFactory::GetForProfile(profile())-> | |
401 GetDefaultSearchProvider(); | |
402 EXPECT_EQ( | |
403 GURL("https://foo.com/dev?bar=bar#bar=bar"), | |
404 CoerceCommandLineURLToTemplateURL( | |
405 GURL("http://myserver.com:9000/dev?bar=bar#bar=bar"), | |
406 template_url->instant_url_ref(), kDisableStartMargin)); | |
407 } | |
408 | |
409 const SearchTestCase kInstantNTPTestCases[] = { | 399 const SearchTestCase kInstantNTPTestCases[] = { |
410 {"https://foo.com/instant?strk", true, "Valid Instant URL"}, | 400 {"https://foo.com/instant?strk", true, "Valid Instant URL"}, |
411 {"https://foo.com/instant#strk", true, "Valid Instant URL"}, | 401 {"https://foo.com/instant#strk", true, "Valid Instant URL"}, |
412 {"https://foo.com/url?strk", true, "Valid search URL"}, | 402 {"https://foo.com/url?strk", true, "Valid search URL"}, |
413 {"https://foo.com/url#strk", true, "Valid search URL"}, | 403 {"https://foo.com/url#strk", true, "Valid search URL"}, |
414 {"https://foo.com/alt?strk", true, "Valid alternative URL"}, | 404 {"https://foo.com/alt?strk", true, "Valid alternative URL"}, |
415 {"https://foo.com/alt#strk", true, "Valid alternative URL"}, | 405 {"https://foo.com/alt#strk", true, "Valid alternative URL"}, |
416 {"https://foo.com/url?strk&bar=", true, "No query terms"}, | 406 {"https://foo.com/url?strk&bar=", true, "No query terms"}, |
417 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, | 407 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, |
418 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, | 408 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 // Enable suggest. No difference. | 487 // Enable suggest. No difference. |
498 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 488 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
499 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | 489 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), |
500 GetInstantURL(profile(), kDisableStartMargin)); | 490 GetInstantURL(profile(), kDisableStartMargin)); |
501 | 491 |
502 // Disable Instant. No difference, because suggest is still enabled. | 492 // Disable Instant. No difference, because suggest is still enabled. |
503 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); | 493 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); |
504 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | 494 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), |
505 GetInstantURL(profile(), kDisableStartMargin)); | 495 GetInstantURL(profile(), kDisableStartMargin)); |
506 | 496 |
507 // Override the Instant URL on the commandline. Oops, forgot "strk". | |
508 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
509 switches::kInstantURL, | |
510 "http://myserver.com:9000/dev?bar=bar#bar=bar"); | |
511 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | |
512 | |
513 // Override with "strk". For fun, put it in the query, instead of the ref. | |
514 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
515 switches::kInstantURL, | |
516 "http://myserver.com:9000/dev?bar=bar&strk#bar=bar"); | |
517 EXPECT_EQ(GURL("http://myserver.com:9000/dev?bar=bar&strk#bar=bar"), | |
518 GetInstantURL(profile(), kDisableStartMargin)); | |
519 | |
520 // Disable suggest. No Instant URL. | 497 // Disable suggest. No Instant URL. |
521 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | 498 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
522 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 499 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
523 } | 500 } |
524 | 501 |
525 TEST_F(SearchTest, StartMarginCGI) { | 502 TEST_F(SearchTest, StartMarginCGI) { |
526 // Instant is disabled, so no Instant URL. | 503 // Instant is disabled, so no Instant URL. |
527 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 504 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
528 | 505 |
529 // Enable Instant. No margin. | 506 // Enable Instant. No margin. |
(...skipping 23 matching lines...) Expand all Loading... |
553 // Default search provider should now support instant. | 530 // Default search provider should now support instant. |
554 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 531 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
555 // Enable suggest. No difference. | 532 // Enable suggest. No difference. |
556 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 533 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
557 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 534 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
558 | 535 |
559 // Disable Instant. No difference. | 536 // Disable Instant. No difference. |
560 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); | 537 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); |
561 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 538 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
562 | 539 |
563 // Override the Instant URL on the commandline. Oops, forgot "strk". | |
564 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
565 switches::kInstantURL, | |
566 "http://myserver.com:9000/dev?bar=bar#bar=bar"); | |
567 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | |
568 | |
569 // Check that command line overrides don't affect the default search provider. | |
570 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | |
571 | |
572 // Disable suggest. No Instant URL. | 540 // Disable suggest. No Instant URL. |
573 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | 541 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
574 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 542 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
575 // Even with suggest disabled, the default search provider still supports | 543 // Even with suggest disabled, the default search provider still supports |
576 // instant. | 544 // instant. |
577 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 545 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
578 | 546 |
579 // Set an Instant URL with no valid search terms replacement key. | 547 // Set an Instant URL with no valid search terms replacement key. |
580 SetDefaultInstantTemplateUrl(false); | 548 SetDefaultInstantTemplateUrl(false); |
581 | 549 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 | 639 |
672 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 640 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
673 | 641 |
674 // Now that suggest is back on and the instant url is good, the checkbox | 642 // Now that suggest is back on and the instant url is good, the checkbox |
675 // should be enabled and checked again, but still invisible. | 643 // should be enabled and checked again, but still invisible. |
676 EXPECT_FALSE(IsInstantCheckboxVisible()); | 644 EXPECT_FALSE(IsInstantCheckboxVisible()); |
677 EXPECT_TRUE(IsInstantCheckboxEnabled(profile())); | 645 EXPECT_TRUE(IsInstantCheckboxEnabled(profile())); |
678 EXPECT_FALSE(IsInstantCheckboxChecked(profile())); | 646 EXPECT_FALSE(IsInstantCheckboxChecked(profile())); |
679 } | 647 } |
680 | 648 |
| 649 TEST_F(SearchTest, CommandLineOverrides) { |
| 650 EnableInstantExtendedAPIForTesting(); |
| 651 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, true); |
| 652 |
| 653 // GetLocalInstantURL() should default to the non-Google local NTP. |
| 654 SetSearchProvider(false); |
| 655 GURL local_instant_url(GetLocalInstantURL(profile())); |
| 656 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), local_instant_url); |
| 657 |
| 658 TemplateURLService* template_url_service = |
| 659 TemplateURLServiceFactory::GetForProfile(profile()); |
| 660 TemplateURLData data; |
| 661 data.SetURL("{google:baseURL}search?q={searchTerms}"); |
| 662 data.instant_url = "{google:baseURL}webhp?strk"; |
| 663 data.search_terms_replacement_key = "strk"; |
| 664 TemplateURL* template_url = new TemplateURL(profile(), data); |
| 665 // Takes ownership of |template_url|. |
| 666 template_url_service->Add(template_url); |
| 667 template_url_service->SetDefaultSearchProvider(template_url); |
| 668 |
| 669 // By default, Instant Extended forces the instant URL to be HTTPS, so even if |
| 670 // we set a Google base URL that is HTTP, we should get an HTTPS URL. |
| 671 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/"); |
| 672 GURL instant_url(GetInstantURL(profile(), kDisableStartMargin)); |
| 673 ASSERT_TRUE(instant_url.is_valid()); |
| 674 EXPECT_EQ("https://www.foo.com/webhp?strk", instant_url.spec()); |
| 675 |
| 676 // However, if the Google base URL is specified on the command line, the |
| 677 // instant URL should just use it, even if it's HTTP. |
| 678 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); |
| 679 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, |
| 680 "http://www.bar.com/"); |
| 681 instant_url = GetInstantURL(profile(), kDisableStartMargin); |
| 682 ASSERT_TRUE(instant_url.is_valid()); |
| 683 EXPECT_EQ("http://www.bar.com/webhp?strk", instant_url.spec()); |
| 684 |
| 685 // Similarly, setting a Google base URL on the command line should allow you |
| 686 // to get the Google version of the local NTP, even though search provider's |
| 687 // URL doesn't contain "google". |
| 688 local_instant_url = GetLocalInstantURL(profile()); |
| 689 EXPECT_EQ(GURL(chrome::kChromeSearchLocalGoogleNtpUrl), local_instant_url); |
| 690 |
| 691 // If we specify extra search query params, they should be inserted into the |
| 692 // query portion of the instant URL. |
| 693 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 694 switches::kExtraSearchQueryParams, "a=b"); |
| 695 instant_url = GetInstantURL(profile(), kDisableStartMargin); |
| 696 ASSERT_TRUE(instant_url.is_valid()); |
| 697 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec()); |
| 698 } |
681 | 699 |
682 } // namespace chrome | 700 } // namespace chrome |
OLD | NEW |