| 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; |
| 722 const bool ignore_path_ending; |
| 721 const std::string search_term_key; | 723 const std::string search_term_key; |
| 724 const std::string search_term_value_prefix; |
| 725 const std::string search_term_value_suffix; |
| 722 } test_data[] = { | 726 } test_data[] = { |
| 723 { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"}, | 727 { "http://blah/?foo=bar&q={searchTerms}&b=x", |
| 724 { "http://blah/{searchTerms}", "blah", "/", ""}, | 728 "blah", url::Parsed::QUERY, "/", false, "q", "", ""}, |
| 729 |
| 730 { "http://blah/?foo=bar#q={searchTerms}&b=x", |
| 731 "blah", url::Parsed::REF, "/", false, "q", "", ""}, |
| 732 |
| 733 { "http://blah/{searchTerms}", |
| 734 "blah", url::Parsed::PATH, "/", false, "", "/", ""}, |
| 735 |
| 736 { "http://blah/{searchTerms}/", |
| 737 "blah", url::Parsed::PATH, "//", false, "", "/", "/"}, |
| 738 |
| 739 { "http://blah/begin/{searchTerms}/end", |
| 740 "blah", url::Parsed::PATH, "/begin//end", false, "", "/begin/", "/end"}, |
| 725 | 741 |
| 726 // No term should result in empty values. | 742 // No term should result in empty values. |
| 727 { "http://blah/", "", "", ""}, | 743 { "http://blah/", "", url::Parsed::QUERY, "", false, "", "", ""}, |
| 744 |
| 745 // Term in the host shouldn't match. |
| 746 { "http://{searchTerms}", "", url::Parsed::QUERY, "", false, "", "", "" }, |
| 747 |
| 748 // searchTerms is a key, not a value, so this should result in an empty |
| 749 // value. |
| 750 { "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x", |
| 751 "", url::Parsed::QUERY, "", false, "", "", "" }, |
| 728 | 752 |
| 729 // Multiple terms should result in empty values. | 753 // Multiple terms should result in empty values. |
| 730 { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""}, | 754 { "http://blah/?q={searchTerms}&x={searchTerms}", |
| 755 "", url::Parsed::QUERY, "", false, "", "", "" }, |
| 731 | 756 |
| 732 // Term in the host shouldn't match. | 757 { "http://blah/{searchTerms}?q={searchTerms}", |
| 733 { "http://{searchTerms}", "", "", ""}, | 758 "", url::Parsed::QUERY, "", false, "", "", "" }, |
| 734 | 759 |
| 735 { "http://blah/?q={searchTerms}", "blah", "/", "q"}, | 760 { "http://blah/{searchTerms}#x={searchTerms}", |
| 736 { "https://blah/?q={searchTerms}", "blah", "/", "q"}, | 761 "", url::Parsed::QUERY, "", false, "", "", "" }, |
| 737 | 762 |
| 738 // Single term with extra chars in value should match. | 763 // Fixed prefixes and suffixes in query or ref. |
| 739 { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"}, | 764 { "http://blah/?q=stock:{searchTerms}", |
| 765 "blah", url::Parsed::QUERY, "/", false, "q", "stock:", ""}, |
| 766 |
| 767 { "http://www.example.com/?q=chromium-{searchTerms}@chromium.org", |
| 768 "www.example.com", url::Parsed::QUERY, "/", false, "q", "chromium-", |
| 769 "@chromium.org" }, |
| 770 |
| 771 { "http://www.example.com/#q=chromium-{searchTerms}@chromium.org", |
| 772 "www.example.com", url::Parsed::REF, "/", false, "q", "chromium-", |
| 773 "@chromium.org" }, |
| 774 |
| 775 { "http://www.example.com/chromium-{searchTerms}@chromium.org/info", |
| 776 "www.example.com", url::Parsed::PATH, "/chromium-@chromium.org/info", |
| 777 false, "", "/chromium-", "@chromium.org/info" }, |
| 778 |
| 779 // Ignoring ending of path. |
| 780 { "http://blah/{google:ignorePathEnding}?q={searchTerms}", |
| 781 "blah", url::Parsed::QUERY, "/", true, "q", "", "" }, |
| 782 |
| 783 { "http://blah/path/{google:ignorePathEnding}/end#q={searchTerms}", |
| 784 "blah", url::Parsed::REF, "/path/", true, "q", "", "" }, |
| 785 |
| 786 { "http://blah/path/{searchTerms}/{google:ignorePathEnding}", |
| 787 "blah", url::Parsed::PATH, "/path//", true, "", "/path/", "/" }, |
| 788 |
| 789 { "http://blah/path/{searchTerms}/data/{google:ignorePathEnding}", |
| 790 "blah", url::Parsed::PATH, "/path//data/", true, "", "/path/", "/data/" }, |
| 791 |
| 792 { "http://blah/path/{searchTerms}{google:ignorePathEnding}", |
| 793 "blah", url::Parsed::PATH, "/path/", false, "", "/path/", "" }, |
| 794 |
| 795 { "http://blah/path/{google:ignorePathEnding}{searchTerms}", |
| 796 "", url::Parsed::QUERY, "", false, "", "", "" }, |
| 740 }; | 797 }; |
| 741 | 798 |
| 742 for (size_t i = 0; i < arraysize(test_data); ++i) { | 799 for (size_t i = 0; i < arraysize(test_data); ++i) { |
| 743 TemplateURLData data; | 800 TemplateURLData data; |
| 744 data.SetURL(test_data[i].url); | 801 data.SetURL(test_data[i].url); |
| 745 TemplateURL url(data); | 802 TemplateURL url(data); |
| 746 EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_)); | 803 const TemplateURLRef& url_ref = url.url_ref(); |
| 747 EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_)); | 804 EXPECT_EQ(test_data[i].host, url_ref.GetHost(search_terms_data_)); |
| 805 EXPECT_EQ(test_data[i].location, |
| 806 url_ref.GetSearchTermKeyLocation(search_terms_data_)); |
| 807 EXPECT_EQ(test_data[i].path, url_ref.GetPath(search_terms_data_)); |
| 808 EXPECT_EQ(test_data[i].ignore_path_ending, |
| 809 url_ref.GetIgnorePathEnding(search_terms_data_)); |
| 748 EXPECT_EQ(test_data[i].search_term_key, | 810 EXPECT_EQ(test_data[i].search_term_key, |
| 749 url.url_ref().GetSearchTermKey(search_terms_data_)); | 811 url_ref.GetSearchTermKey(search_terms_data_)); |
| 812 EXPECT_EQ(test_data[i].search_term_value_prefix, |
| 813 url_ref.GetSearchTermValuePrefix(search_terms_data_)); |
| 814 EXPECT_EQ(test_data[i].search_term_value_suffix, |
| 815 url_ref.GetSearchTermValueSuffix(search_terms_data_)); |
| 750 } | 816 } |
| 751 } | 817 } |
| 752 | 818 |
| 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) { | 819 TEST_F(TemplateURLTest, GoogleBaseSuggestURL) { |
| 796 static const struct { | 820 static const struct { |
| 797 const char* const base_url; | 821 const char* const base_url; |
| 798 const char* const base_suggest_url; | 822 const char* const base_suggest_url; |
| 799 } data[] = { | 823 } data[] = { |
| 800 { "http://google.com/", "http://google.com/complete/", }, | 824 { "http://google.com/", "http://google.com/complete/", }, |
| 801 { "http://www.google.com/", "http://www.google.com/complete/", }, | 825 { "http://www.google.com/", "http://www.google.com/complete/", }, |
| 802 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, | 826 { "http://www.google.co.uk/", "http://www.google.co.uk/complete/", }, |
| 803 { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, | 827 { "http://www.google.com.by/", "http://www.google.com.by/complete/", }, |
| 804 { "http://google.com/intl/xx/", "http://google.com/complete/", }, | 828 { "http://google.com/intl/xx/", "http://google.com/complete/", }, |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1110 | 1134 |
| 1111 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | 1135 EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 1112 GURL("http://term-in-path.com/begin"), search_terms_data_, &result)); | 1136 GURL("http://term-in-path.com/begin"), search_terms_data_, &result)); |
| 1113 EXPECT_EQ(base::string16(), result); | 1137 EXPECT_EQ(base::string16(), result); |
| 1114 | 1138 |
| 1115 EXPECT_FALSE(url.ExtractSearchTermsFromURL( | 1139 EXPECT_FALSE(url.ExtractSearchTermsFromURL( |
| 1116 GURL("http://term-in-path.com/end"), search_terms_data_, &result)); | 1140 GURL("http://term-in-path.com/end"), search_terms_data_, &result)); |
| 1117 EXPECT_EQ(base::string16(), result); | 1141 EXPECT_EQ(base::string16(), result); |
| 1118 } | 1142 } |
| 1119 | 1143 |
| 1144 // Test that extracting search terms from URL works correctly when |
| 1145 // the "{google:ignorePathEnding}" marker is contained in a search template. |
| 1146 TEST_F(TemplateURLTest, ExtractSearchTermsFromURLWithIgnorePathEnding) { |
| 1147 struct TestCase { |
| 1148 std::string search_template; |
| 1149 GURL url; |
| 1150 bool result; |
| 1151 base::string16 search_terms; |
| 1152 }; |
| 1153 |
| 1154 const std::string template1 = |
| 1155 "http://h.com/begin/{google:ignorePathEnding}?q={searchTerms}"; |
| 1156 const std::string template2 = |
| 1157 "http://h.com/begin/{searchTerms}/{google:ignorePathEnding}"; |
| 1158 const std::string template3 = |
| 1159 "http://h.com/begin/{searchTerms}/mid/{google:ignorePathEnding}"; |
| 1160 |
| 1161 const TestCase test_cases[] = { |
| 1162 // Cases if "{searchTerms}" are located in query. |
| 1163 { template1, GURL("http://h.com/begin/blablabla?q=something"), |
| 1164 true, ASCIIToUTF16("something") }, |
| 1165 |
| 1166 { template1, GURL("http://h.com/begin/bla/bla/bla?q=anotherthing"), |
| 1167 true, ASCIIToUTF16("anotherthing") }, |
| 1168 |
| 1169 { template1, GURL("http://h.com/begin/?q=thirdthing"), |
| 1170 true, ASCIIToUTF16("thirdthing") }, |
| 1171 |
| 1172 { template1, GURL("http://h.com/begin?q=abc"), false, base::string16() }, |
| 1173 { template1, GURL("http://h.com/about?q=abc"), false, base::string16() }, |
| 1174 { template1, GURL("http://h.com?q=abc"), false, base::string16() }, |
| 1175 |
| 1176 // Cases if "{searchTerms}" are located in path. |
| 1177 { template2, GURL("http://h.com/begin/something/blablabla"), |
| 1178 true, ASCIIToUTF16("something") }, |
| 1179 |
| 1180 { template2, GURL("http://h.com/begin/anotherthing/bla/bla/bla"), |
| 1181 true, ASCIIToUTF16("anotherthing") }, |
| 1182 |
| 1183 { template2, GURL("http://h.com/begin/thirdthing/"), |
| 1184 true, ASCIIToUTF16("thirdthing") }, |
| 1185 |
| 1186 { template2, GURL("http://h.com/begin//"), false, base::string16() }, |
| 1187 { template2, GURL("http://h.com/begin/abc"), false, base::string16() }, |
| 1188 { template2, GURL("http://h.com/begin/"), false, base::string16() }, |
| 1189 { template2, GURL("http://h.com/begin"), false, base::string16() }, |
| 1190 { template2, GURL("http://h.com/begin"), false, base::string16() }, |
| 1191 { template2, GURL("http://h.com/about"), false, base::string16() }, |
| 1192 { template2, GURL("http://h.com"), false, base::string16() }, |
| 1193 |
| 1194 { template3, GURL("http://h.com/begin/bla/bla/mid/bla"), |
| 1195 true, ASCIIToUTF16("bla/bla") }, |
| 1196 { template3, GURL("http://h.com/begin/bla/bla/mid/"), |
| 1197 true, ASCIIToUTF16("bla/bla") }, |
| 1198 }; |
| 1199 |
| 1200 for (size_t i = 0; i != arraysize(test_cases); ++i) { |
| 1201 const TestCase& test_case = test_cases[i]; |
| 1202 TemplateURLData data; |
| 1203 data.SetURL(test_case.search_template); |
| 1204 TemplateURL template_url(data); |
| 1205 base::string16 search_terms; |
| 1206 const bool result = template_url.ExtractSearchTermsFromURL( |
| 1207 test_case.url, search_terms_data_, &search_terms); |
| 1208 EXPECT_EQ(test_case.result, result) << "i=" << i; |
| 1209 EXPECT_EQ(test_case.search_terms, search_terms) << "i=" << i; |
| 1210 } |
| 1211 } |
| 1212 |
| 1120 // Checks that the ExtractSearchTermsFromURL function works correctly | 1213 // Checks that the ExtractSearchTermsFromURL function works correctly |
| 1121 // for urls containing non-latin characters in UTF8 encoding. | 1214 // for urls containing non-latin characters in UTF8 encoding. |
| 1122 TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) { | 1215 TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) { |
| 1123 TemplateURLData data; | 1216 TemplateURLData data; |
| 1124 data.SetURL("http://utf-8.ru/?q={searchTerms}"); | 1217 data.SetURL("http://utf-8.ru/?q={searchTerms}"); |
| 1125 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); | 1218 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); |
| 1126 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); | 1219 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); |
| 1127 TemplateURL url(data); | 1220 TemplateURL url(data); |
| 1128 base::string16 result; | 1221 base::string16 result; |
| 1129 | 1222 |
| (...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1804 GURL("https://www.foo.org/search?q=Y+Z"), | 1897 GURL("https://www.foo.org/search?q=Y+Z"), |
| 1805 search_terms_data_, &search_terms)); | 1898 search_terms_data_, &search_terms)); |
| 1806 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms); | 1899 EXPECT_EQ(base::ASCIIToUTF16("Y Z"), search_terms); |
| 1807 EXPECT_TRUE(url.ExtractSearchTermsFromURL( | 1900 EXPECT_TRUE(url.ExtractSearchTermsFromURL( |
| 1808 GURL("https://www.foo.org/s#q=123"), | 1901 GURL("https://www.foo.org/s#q=123"), |
| 1809 search_terms_data_, &search_terms)); | 1902 search_terms_data_, &search_terms)); |
| 1810 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms); | 1903 EXPECT_EQ(base::ASCIIToUTF16("123"), search_terms); |
| 1811 | 1904 |
| 1812 search_terms_data_.set_google_base_url("http://www.google.com/"); | 1905 search_terms_data_.set_google_base_url("http://www.google.com/"); |
| 1813 } | 1906 } |
| OLD | NEW |