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 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 }; | 390 }; |
390 | 391 |
391 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 392 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
392 const SearchTestCase& test = kTestCases[i]; | 393 const SearchTestCase& test = kTestCases[i]; |
393 EXPECT_EQ(test.expected_result, | 394 EXPECT_EQ(test.expected_result, |
394 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) | 395 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) |
395 << test.url << " " << test.comment; | 396 << test.url << " " << test.comment; |
396 } | 397 } |
397 } | 398 } |
398 | 399 |
399 TEST_F(SearchTest, CoerceCommandLineURLToTemplateURL) { | |
400 TemplateURL* template_url = | |
401 TemplateURLServiceFactory::GetForProfile(profile())-> | |
402 GetDefaultSearchProvider(); | |
403 EXPECT_EQ( | |
404 GURL("https://foo.com/dev?bar=bar#bar=bar"), | |
405 CoerceCommandLineURLToTemplateURL( | |
406 GURL("http://myserver.com:9000/dev?bar=bar#bar=bar"), | |
407 template_url->instant_url_ref(), kDisableStartMargin)); | |
408 } | |
409 | |
410 const SearchTestCase kInstantNTPTestCases[] = { | 400 const SearchTestCase kInstantNTPTestCases[] = { |
411 {"https://foo.com/instant?strk", true, "Valid Instant URL"}, | 401 {"https://foo.com/instant?strk", true, "Valid Instant URL"}, |
412 {"https://foo.com/instant#strk", true, "Valid Instant URL"}, | 402 {"https://foo.com/instant#strk", true, "Valid Instant 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/url#strk", true, "Valid search URL"}, | 404 {"https://foo.com/url#strk", true, "Valid search 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/alt#strk", true, "Valid alternative URL"}, | 406 {"https://foo.com/alt#strk", true, "Valid alternative URL"}, |
417 {"https://foo.com/url?strk&bar=", true, "No query terms"}, | 407 {"https://foo.com/url?strk&bar=", true, "No query terms"}, |
418 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, | 408 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, |
419 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, | 409 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 // Enable suggest. No difference. | 498 // Enable suggest. No difference. |
509 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 499 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
510 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | 500 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), |
511 GetInstantURL(profile(), kDisableStartMargin)); | 501 GetInstantURL(profile(), kDisableStartMargin)); |
512 | 502 |
513 // Disable Instant. No difference, because suggest is still enabled. | 503 // Disable Instant. No difference, because suggest is still enabled. |
514 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); | 504 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); |
515 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | 505 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), |
516 GetInstantURL(profile(), kDisableStartMargin)); | 506 GetInstantURL(profile(), kDisableStartMargin)); |
517 | 507 |
518 // Override the Instant URL on the commandline. Oops, forgot "strk". | |
519 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
520 switches::kInstantURL, | |
521 "http://myserver.com:9000/dev?bar=bar#bar=bar"); | |
522 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | |
523 | |
524 // Override with "strk". For fun, put it in the query, instead of the ref. | |
525 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
526 switches::kInstantURL, | |
527 "http://myserver.com:9000/dev?bar=bar&strk#bar=bar"); | |
528 EXPECT_EQ(GURL("http://myserver.com:9000/dev?bar=bar&strk#bar=bar"), | |
529 GetInstantURL(profile(), kDisableStartMargin)); | |
530 | |
531 // Disable suggest. No Instant URL. | 508 // Disable suggest. No Instant URL. |
532 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | 509 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
533 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 510 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
534 } | 511 } |
535 | 512 |
536 TEST_F(SearchTest, StartMarginCGI) { | 513 TEST_F(SearchTest, StartMarginCGI) { |
537 // Instant is disabled, so no Instant URL. | 514 // Instant is disabled, so no Instant URL. |
538 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 515 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
539 | 516 |
540 // Enable Instant. No margin. | 517 // Enable Instant. No margin. |
(...skipping 23 matching lines...) Expand all Loading... |
564 // Default search provider should now support instant. | 541 // Default search provider should now support instant. |
565 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 542 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
566 // Enable suggest. No difference. | 543 // Enable suggest. No difference. |
567 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 544 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
568 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 545 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
569 | 546 |
570 // Disable Instant. No difference. | 547 // Disable Instant. No difference. |
571 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); | 548 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, false); |
572 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 549 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
573 | 550 |
574 // Override the Instant URL on the commandline. Oops, forgot "strk". | |
575 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
576 switches::kInstantURL, | |
577 "http://myserver.com:9000/dev?bar=bar#bar=bar"); | |
578 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | |
579 | |
580 // Check that command line overrides don't affect the default search provider. | |
581 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | |
582 | |
583 // Disable suggest. No Instant URL. | 551 // Disable suggest. No Instant URL. |
584 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | 552 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
585 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 553 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
586 // Even with suggest disabled, the default search provider still supports | 554 // Even with suggest disabled, the default search provider still supports |
587 // instant. | 555 // instant. |
588 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); | 556 EXPECT_TRUE(DefaultSearchProviderSupportsInstant(profile())); |
589 | 557 |
590 // Set an Instant URL with no valid search terms replacement key. | 558 // Set an Instant URL with no valid search terms replacement key. |
591 SetDefaultInstantTemplateUrl(false); | 559 SetDefaultInstantTemplateUrl(false); |
592 | 560 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 | 650 |
683 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 651 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
684 | 652 |
685 // Now that suggest is back on and the instant url is good, the checkbox | 653 // Now that suggest is back on and the instant url is good, the checkbox |
686 // should be enabled and checked again, but still invisible. | 654 // should be enabled and checked again, but still invisible. |
687 EXPECT_FALSE(IsInstantCheckboxVisible()); | 655 EXPECT_FALSE(IsInstantCheckboxVisible()); |
688 EXPECT_TRUE(IsInstantCheckboxEnabled(profile())); | 656 EXPECT_TRUE(IsInstantCheckboxEnabled(profile())); |
689 EXPECT_FALSE(IsInstantCheckboxChecked(profile())); | 657 EXPECT_FALSE(IsInstantCheckboxChecked(profile())); |
690 } | 658 } |
691 | 659 |
| 660 TEST_F(SearchTest, CommandLineOverrides) { |
| 661 EnableInstantExtendedAPIForTesting(); |
| 662 profile()->GetPrefs()->SetBoolean(prefs::kSearchInstantEnabled, true); |
| 663 TemplateURLService* template_url_service = |
| 664 TemplateURLServiceFactory::GetForProfile(profile()); |
| 665 TemplateURLData data; |
| 666 data.SetURL("{google:baseURL}search?q={searchTerms}"); |
| 667 data.instant_url = "{google:baseURL}webhp?strk"; |
| 668 data.search_terms_replacement_key = "strk"; |
| 669 TemplateURL* template_url = new TemplateURL(profile(), data); |
| 670 // Takes ownership of |template_url|. |
| 671 template_url_service->Add(template_url); |
| 672 template_url_service->SetDefaultSearchProvider(template_url); |
| 673 |
| 674 // By default, Instant Extended forces the instant URL to be HTTPS, so even if |
| 675 // we set a Google base URL that is HTTP, we should get an HTTPS URL. |
| 676 UIThreadSearchTermsData::SetGoogleBaseURL("http://www.foo.com/"); |
| 677 GURL instant_url(GetInstantURL(profile(), kDisableStartMargin)); |
| 678 ASSERT_TRUE(instant_url.is_valid()); |
| 679 EXPECT_EQ("https://www.foo.com/webhp?strk", instant_url.spec()); |
| 680 |
| 681 // However, if the Google base URL is specified on the command line, the |
| 682 // instant URL should just use it, even if it's HTTP. |
| 683 UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); |
| 684 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, |
| 685 "http://www.bar.com/"); |
| 686 instant_url = GetInstantURL(profile(), kDisableStartMargin); |
| 687 ASSERT_TRUE(instant_url.is_valid()); |
| 688 EXPECT_EQ("http://www.bar.com/webhp?strk", instant_url.spec()); |
| 689 |
| 690 // If we specify extra search query params, they should be inserted into the |
| 691 // query portion of the instant URL. |
| 692 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 693 switches::kExtraSearchQueryParams, "a=b"); |
| 694 instant_url = GetInstantURL(profile(), kDisableStartMargin); |
| 695 ASSERT_TRUE(instant_url.is_valid()); |
| 696 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec()); |
| 697 } |
692 | 698 |
693 } // namespace chrome | 699 } // namespace chrome |
OLD | NEW |