| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_)); | 710 GURL result(url.url_ref().ReplaceSearchTerms(args, search_terms_data_)); |
| 711 ASSERT_TRUE(result.is_valid()); | 711 ASSERT_TRUE(result.is_valid()); |
| 712 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", | 712 EXPECT_EQ("http://bar/?rlz=" + base::UTF16ToUTF8(rlz_string) + "&x", |
| 713 result.spec()); | 713 result.spec()); |
| 714 } | 714 } |
| 715 | 715 |
| 716 TEST_F(TemplateURLTest, HostAndSearchTermKey) { | 716 TEST_F(TemplateURLTest, HostAndSearchTermKey) { |
| 717 struct TestData { | 717 struct TestData { |
| 718 const std::string url; | 718 const std::string url; |
| 719 const std::string host; | 719 const std::string host; |
| 720 const url::Parsed::ComponentType location; |
| 720 const std::string path; | 721 const std::string path; |
| 721 const std::string search_term_key; | 722 const std::string search_term_key; |
| 723 const std::string search_term_value_prefix; |
| 724 const std::string search_term_value_suffix; |
| 722 } test_data[] = { | 725 } test_data[] = { |
| 723 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, | 726 { "http://blah/?foo=bar&q={searchTerms}&b=x", |
| 724 { "http://blah/{searchTerms}", "blah", "/", ""}, | 727 "blah", url::Parsed::QUERY, "/", "q", "", ""}, |
| 728 |
| 729 { "http://blah/?foo=bar#q={searchTerms}&b=x", |
| 730 "blah", url::Parsed::REF, "/", "q", "", ""}, |
| 731 |
| 732 { "http://blah/{searchTerms}", |
| 733 "blah", url::Parsed::PATH, "/", "", "/", ""}, |
| 734 |
| 735 { "http://blah/{searchTerms}/", |
| 736 "blah", url::Parsed::PATH, "//", "", "/", "/"}, |
| 737 |
| 738 { "http://blah/begin/{searchTerms}/end", |
| 739 "blah", url::Parsed::PATH, "/begin//end", "", "/begin/", "/end"}, |
| 725 | 740 |
| 726 // No term should result in empty values. | 741 // No term should result in empty values. |
| 727 { "http://blah/", "", "", ""}, | 742 { "http://blah/", "", url::Parsed::QUERY, "", "", "", ""}, |
| 743 |
| 744 // Term in the host shouldn't match. |
| 745 { "http://{searchTerms}", "", url::Parsed::QUERY, "", "", "", "" }, |
| 746 |
| 747 // searchTerms is a key, not a value, so this should result in an empty |
| 748 // value. |
| 749 { "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x", |
| 750 "", url::Parsed::QUERY, "", "", "", "" }, |
| 728 | 751 |
| 729 // Multiple terms should result in empty values. | 752 // Multiple terms should result in empty values. |
| 730 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, | 753 { "http://blah/?q={searchTerms}&x={searchTerms}", |
| 754 "", url::Parsed::QUERY, "", "", "", "" }, |
| 731 | 755 |
| 732 // Term in the host shouldn't match. | 756 { "http://blah/{searchTerms}?q={searchTerms}", |
| 733 { "http://{searchTerms}", "", "", ""}, | 757 "", url::Parsed::QUERY, "", "", "", "" }, |
| 734 | 758 |
| 735 { "http://blah/?q={searchTerms}", "blah", "/", "q"}, | 759 { "http://blah/{searchTerms}#x={searchTerms}", |
| 736 { "https://blah/?q={searchTerms}", "blah", "/", "q"}, | 760 "", url::Parsed::QUERY, "", "", "", "" }, |
| 737 | 761 |
| 738 // Single term with extra chars in value should match. | 762 // Fixed prefixes and suffixes in query or ref. |
| 739 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, | 763 { "http://blah/?q=stock:{searchTerms}", |
| 764 "blah", url::Parsed::QUERY, "/", "q", "stock:", ""}, |
| 765 |
| 766 { "http://www.example.com/?q=chromium-{searchTerms}@chromium.org", |
| 767 "www.example.com", url::Parsed::QUERY, "/", "q", "chromium-", |
| 768 "@chromium.org" }, |
| 769 |
| 770 { "http://www.example.com/#q=chromium-{searchTerms}@chromium.org", |
| 771 "www.example.com", url::Parsed::REF, "/", "q", "chromium-", |
| 772 "@chromium.org" }, |
| 773 |
| 774 { "http://www.example.com/chromium-{searchTerms}@chromium.org/info", |
| 775 "www.example.com", url::Parsed::PATH, "/chromium-@chromium.org/info", |
| 776 "", "/chromium-", "@chromium.org/info" }, |
| 740 }; | 777 }; |
| 741 | 778 |
| 742 for (size_t i = 0; i < arraysize(test_data); ++i) { | 779 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 743 TemplateURLData data; | 780 TemplateURLData data; |
| 744 data.SetURL(test_data[i].url); | 781 data.SetURL(test_data[i].url); |
| 745 TemplateURL url(data); | 782 TemplateURL url(data); |
| 746 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_)); | 783 const TemplateURLRef& url_ref = url.url_ref(); |
| 747 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_)); | 784 EXPECT_EQ(test_data[i].host, url_ref.GetHost(search_terms_data_)); |
| 785 EXPECT_EQ(test_data[i].location, |
| 786 url_ref.GetSearchTermKeyLocation(search_terms_data_)); |
| 787 EXPECT_EQ(test_data[i].path, url_ref.GetPath(search_terms_data_)); |
| 748 EXPECT_EQ(test_data[i].search_term_key, | 788 EXPECT_EQ(test_data[i].search_term_key, |
| 749 url.url_ref().GetSearchTermKey(search_terms_data_)); | 789 url_ref.GetSearchTermKey(search_terms_data_)); |
| 790 EXPECT_EQ(test_data[i].search_term_value_prefix, |
| 791 url_ref.GetSearchTermValuePrefix(search_terms_data_)); |
| 792 EXPECT_EQ(test_data[i].search_term_value_suffix, |
| 793 url_ref.GetSearchTermValueSuffix(search_terms_data_)); |
| 750 } | 794 } |
| 751 } | 795 } |
| 752 | 796 |
| 753 TEST_F(TemplateURLTest, SearchTermKeyLocation) { | |
| 754 struct TestData { | |
| 755 const std::string url; | |
| 756 const url::Parsed::ComponentType location; | |
| 757 const std::string path; | |
| 758 size_t position_in_path; | |
| 759 } test_data[] = { | |
| 760 { "http://blah/{searchTerms}/", url::Parsed::PATH, "//", 1 }, | |
| 761 { "http://blah/{searchTerms}", url::Parsed::PATH, "/", 1 }, | |
| 762 { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "/begin//end", 7
}, | |
| 763 | |
| 764 { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY, | |
| 765 "/", std::string::npos }, | |
| 766 { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF, | |
| 767 "/", std::string::npos }, | |
| 768 // searchTerms is a key, not a value, so this should result in an empty | |
| 769 // value. | |
| 770 { "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x", | |
| 771 url::Parsed::QUERY, std::string(), std::string::npos }, | |
| 772 | |
| 773 // Multiple search terms should result in empty values. | |
| 774 { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY, | |
| 775 "", std::string::npos }, | |
| 776 { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY, | |
| 777 "", std::string::npos }, | |
| 778 { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY, | |
| 779 "", std::string::npos }, | |
| 780 }; | |
| 781 | |
| 782 for (size_t i = 0; i < arraysize(test_data); ++i) { | |
| 783 TemplateURLData data; | |
| 784 data.SetURL(test_data[i].url); | |
| 785 TemplateURL url(data); | |
| 786 EXPECT_EQ(test_data[i].location, | |
| 787 url.url_ref().GetSearchTermKeyLocation(search_terms_data_)); | |
| 788 EXPECT_EQ(test_data[i].path, | |
| 789 url.url_ref().GetPath(search_terms_data_)); | |
| 790 EXPECT_EQ(test_data[i].position_in_path, | |
| 791 url.url_ref().GetSearchTermPositionInPath(search_terms_data_)); | |
| 792 } | |
| 793 } | |
| 794 | |
| 795 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { | 797 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 796 static const struct { | 798 static const struct { |
| 797 const char* const base_url; | 799 const char* const base_url; |
| 798 const char* const base_suggest_url; | 800 const char* const base_suggest_url; |
| 799 } data[] = { | 801 } data[] = { |
| 800 { "http://google.com/", "http://google.com/complete/", }, | 802 { "http://google.com/", "http://google.com/complete/", }, |
| 801 { "http://www.google.com/", "http://www.google.com/complete/", }, | 803 { "http://www.google.com/", "http://www.google.com/complete/", }, |
| 802 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, | 804 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| 803 { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, | 805 { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| 804 { "http://google.com/intl/xx/", "http://google.com/complete/", }, | 806 { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
| (...skipping 999 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1804 GURL("https://www.foo.org/search?q=Y+Z"), | 1806 GURL("https://www.foo.org/search?q=Y+Z"), |
| 1805 search_terms_data_, &search_terms)); | 1807 search_terms_data_, &search_terms)); |
| 1806 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms); | 1808 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms); |
| 1807 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | 1809 EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1808 GURL("https://www.foo.org/s#q=123"), | 1810 GURL("https://www.foo.org/s#q=123"), |
| 1809 search_terms_data_, &search_terms)); | 1811 search_terms_data_, &search_terms)); |
| 1810 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms); | 1812 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms); |
| 1811 | 1813 |
| 1812 search_terms_data_.set_google_base_url("http://www.google.com/"); | 1814 search_terms_data_.set_google_base_url("http://www.google.com/"); |
| 1813 } | 1815 } |
| OLD | NEW |