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 "base/base_paths.h" | 5 #include "base/base_paths.h" |
| 6 #include "base/command_line.h" |
6 #include "base/string_util.h" | 7 #include "base/string_util.h" |
7 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/rlz/rlz.h" | 10 #include "chrome/browser/rlz/rlz.h" |
10 #include "chrome/browser/search_engines/search_terms_data.h" | 11 #include "chrome/browser/search_engines/search_terms_data.h" |
11 #include "chrome/browser/search_engines/template_url.h" | 12 #include "chrome/browser/search_engines/template_url.h" |
| 13 #include "chrome/common/chrome_switches.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 #if defined(ENABLE_RLZ) | 16 #if defined(ENABLE_RLZ) |
15 #include "chrome/browser/google/google_util.h" | 17 #include "chrome/browser/google/google_util.h" |
16 #endif | 18 #endif |
17 | 19 |
18 // TestSearchTermsData -------------------------------------------------------- | 20 // TestSearchTermsData -------------------------------------------------------- |
19 | 21 |
20 // Simple implementation of SearchTermsData. | 22 // Simple implementation of SearchTermsData. |
21 class TestSearchTermsData : public SearchTermsData { | 23 class TestSearchTermsData : public SearchTermsData { |
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 TemplateURL url(NULL, data); | 636 TemplateURL url(NULL, data); |
635 TemplateURLRef::Replacements replacements; | 637 TemplateURLRef::Replacements replacements; |
636 bool valid = false; | 638 bool valid = false; |
637 EXPECT_EQ("{", | 639 EXPECT_EQ("{", |
638 url.url_ref().ParseURL("{{searchTerms}", &replacements, &valid)); | 640 url.url_ref().ParseURL("{{searchTerms}", &replacements, &valid)); |
639 ASSERT_EQ(1U, replacements.size()); | 641 ASSERT_EQ(1U, replacements.size()); |
640 EXPECT_EQ(1U, replacements[0].index); | 642 EXPECT_EQ(1U, replacements[0].index); |
641 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); | 643 EXPECT_EQ(TemplateURLRef::SEARCH_TERMS, replacements[0].type); |
642 EXPECT_TRUE(valid); | 644 EXPECT_TRUE(valid); |
643 } | 645 } |
| 646 |
| 647 TEST_F(TemplateURLTest, DeserializeAndSetAlternateURLs) { |
| 648 TemplateURLData data; |
| 649 EXPECT_TRUE(data.alternate_urls().empty()); |
| 650 |
| 651 data.DeserializeAndSetAlternateURLs("http://google.com?q={searchTerms}," |
| 652 "{google:baseURL}/#q={searchTerms}"); |
| 653 EXPECT_EQ(2U, data.alternate_urls().size()); |
| 654 EXPECT_EQ("http://google.com?q={searchTerms}", data.alternate_urls()[0]); |
| 655 EXPECT_EQ("{google:baseURL}/#q={searchTerms}", data.alternate_urls()[1]); |
| 656 } |
| 657 |
| 658 |
| 659 TEST_F(TemplateURLTest, DeserializeAndSetEmptyAlternateURLs) { |
| 660 TemplateURLData data; |
| 661 EXPECT_TRUE(data.alternate_urls().empty()); |
| 662 |
| 663 data.DeserializeAndSetAlternateURLs(""); |
| 664 EXPECT_TRUE(data.alternate_urls().empty()); |
| 665 } |
| 666 |
| 667 TEST_F(TemplateURLTest, SerializeAlternateURLs) { |
| 668 TemplateURLData data; |
| 669 EXPECT_TRUE(data.alternate_urls().empty()); |
| 670 data.alternate_urls_.push_back("{google:baseURL}/#q={searchTerms}"); |
| 671 data.alternate_urls_.push_back("http://google.com?q={searchTerms}"); |
| 672 |
| 673 std::string result = data.SerializeAlternateURLs(); |
| 674 EXPECT_EQ("{google:baseURL}/#q={searchTerms}," |
| 675 "http://google.com?q={searchTerms}", result); |
| 676 } |
| 677 |
| 678 TEST_F(TemplateURLTest, SerializeEmptyAlternateURLs) { |
| 679 TemplateURLData data; |
| 680 EXPECT_TRUE(data.alternate_urls().empty()); |
| 681 |
| 682 std::string result = data.SerializeAlternateURLs(); |
| 683 EXPECT_EQ("", result); |
| 684 } |
| 685 |
| 686 TEST_F(TemplateURLTest, GetURL) { |
| 687 TemplateURLData data; |
| 688 data.SetURL("http://google.com/?q={searchTerms}"); |
| 689 data.suggestions_url = "http://google.com/suggest?q={searchTerms}"; |
| 690 data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 691 data.DeserializeAndSetAlternateURLs("http://google.com/alt?q={searchTerms}," |
| 692 "{google:baseURL}/alt/#q={searchTerms}"); |
| 693 TemplateURL url(NULL, data); |
| 694 ASSERT_EQ(3U, url.URLCount()); |
| 695 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); |
| 696 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); |
| 697 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); |
| 698 } |
| 699 |
| 700 TEST_F(TemplateURLTest, GetURLNoInstantURL) { |
| 701 TemplateURLData data; |
| 702 data.SetURL("http://google.com/?q={searchTerms}"); |
| 703 data.suggestions_url = "http://google.com/suggest?q={searchTerms}"; |
| 704 data.DeserializeAndSetAlternateURLs("http://google.com/alt?q={searchTerms}," |
| 705 "{google:baseURL}/alt/#q={searchTerms}"); |
| 706 TemplateURL url(NULL, data); |
| 707 ASSERT_EQ(3U, url.URLCount()); |
| 708 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); |
| 709 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); |
| 710 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); |
| 711 } |
| 712 |
| 713 TEST_F(TemplateURLTest, GetURLNoSuggestionsURL) { |
| 714 TemplateURLData data; |
| 715 data.SetURL("http://google.com/?q={searchTerms}"); |
| 716 data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 717 data.DeserializeAndSetAlternateURLs("http://google.com/alt?q={searchTerms}," |
| 718 "{google:baseURL}/alt/#q={searchTerms}"); |
| 719 TemplateURL url(NULL, data); |
| 720 ASSERT_EQ(3U, url.URLCount()); |
| 721 EXPECT_EQ("http://google.com/alt?q={searchTerms}", url.GetURL(0)); |
| 722 EXPECT_EQ("{google:baseURL}/alt/#q={searchTerms}", url.GetURL(1)); |
| 723 EXPECT_EQ("http://google.com/?q={searchTerms}", url.GetURL(2)); |
| 724 } |
| 725 |
| 726 TEST_F(TemplateURLTest, GetURLOnlyOneURL) { |
| 727 TemplateURLData data; |
| 728 data.SetURL("http://www.google.co.uk/"); |
| 729 TemplateURL url(NULL, data); |
| 730 ASSERT_EQ(1U, url.URLCount()); |
| 731 EXPECT_EQ("http://www.google.co.uk/", url.GetURL(0)); |
| 732 } |
| 733 |
| 734 TEST_F(TemplateURLTest, ExtractSearchTermsFromURL) { |
| 735 TemplateURLData data; |
| 736 data.SetURL("http://google.com/?q={searchTerms}"); |
| 737 data.instant_url = "http://google.com/instant#q={searchTerms}"; |
| 738 data.DeserializeAndSetAlternateURLs( |
| 739 "http://google.com/alt/#q={searchTerms}," |
| 740 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| 741 TemplateURL url(NULL, data); |
| 742 string16 result; |
| 743 |
| 744 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 745 GURL("http://google.com/?q=something"), &result)); |
| 746 EXPECT_EQ(string16(), result); |
| 747 |
| 748 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 749 GURL("http://google.com/?q=something#espv=1"), &result)); |
| 750 EXPECT_EQ(string16(), result); |
| 751 |
| 752 EXPECT_TRUE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 753 GURL("http://google.com/?q=something&espv=1"), &result)); |
| 754 EXPECT_EQ(ASCIIToUTF16("something"), result); |
| 755 |
| 756 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 757 GURL("http://google.ca/?q=something&espv=1"), &result)); |
| 758 EXPECT_EQ(string16(), result); |
| 759 |
| 760 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 761 GURL("http://google.com/foo/?q=foo&espv=1"), &result)); |
| 762 EXPECT_EQ(string16(), result); |
| 763 |
| 764 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 765 GURL("https://google.com/?q=foo&espv=1"), &result)); |
| 766 EXPECT_EQ(string16(), result); |
| 767 |
| 768 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 769 GURL("http://google.com:8080/?q=foo&espv=1"), &result)); |
| 770 EXPECT_EQ(string16(), result); |
| 771 |
| 772 EXPECT_TRUE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 773 GURL("http://google.com/?q=1+2+3&b=456&espv=1"), &result)); |
| 774 EXPECT_EQ(ASCIIToUTF16("1 2 3"), result); |
| 775 |
| 776 EXPECT_TRUE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 777 GURL("http://google.com/alt/?espv=1&q=123#q=456"), &result)); |
| 778 EXPECT_EQ(ASCIIToUTF16("456"), result); |
| 779 |
| 780 EXPECT_TRUE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 781 GURL("http://google.com/alt/?espv=1&a=012&q=123&b=456#f=789"), &result)); |
| 782 EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 783 |
| 784 EXPECT_TRUE(url.ExtractSearchTermsFromInstantExtendedURL(GURL( |
| 785 "http://google.com/alt/?a=012&q=123&espv=1&b=456#j=abc&q=789&h=def9"), |
| 786 &result)); |
| 787 EXPECT_EQ(ASCIIToUTF16("789"), result); |
| 788 |
| 789 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 790 GURL("http://google.com/alt/?espv=1&q="), &result)); |
| 791 EXPECT_EQ(string16(), result); |
| 792 |
| 793 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 794 GURL("http://google.com/alt/?q=&espv=1"), &result)); |
| 795 EXPECT_EQ(string16(), result); |
| 796 |
| 797 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 798 GURL("http://google.com/alt/?espv=1#q="), &result)); |
| 799 EXPECT_EQ(string16(), result); |
| 800 |
| 801 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 802 GURL("http://google.com/alt/?espv=1&q=#q="), &result)); |
| 803 EXPECT_EQ(string16(), result); |
| 804 |
| 805 EXPECT_FALSE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 806 GURL("http://google.com/alt/?espv=1&q=123#q="), &result)); |
| 807 EXPECT_EQ(string16(), result); |
| 808 |
| 809 EXPECT_TRUE(url.ExtractSearchTermsFromInstantExtendedURL( |
| 810 GURL("http://google.com/alt/?espv=1&q=#q=123"), &result)); |
| 811 EXPECT_EQ(ASCIIToUTF16("123"), result); |
| 812 } |
OLD | NEW |