Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2062)

Unified Diff: components/search_engines/template_url_unittest.cc

Issue 1968303002: Support inexact path matching when extracting terms from Template URL. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/search_engines/template_url.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/search_engines/template_url_unittest.cc
diff --git a/components/search_engines/template_url_unittest.cc b/components/search_engines/template_url_unittest.cc
index 4cecbd773ddb2145bf565277f3df9bd33357f0ef..7b493684a95247499d9774ba7372fa75e603e322 100644
--- a/components/search_engines/template_url_unittest.cc
+++ b/components/search_engines/template_url_unittest.cc
@@ -717,78 +717,102 @@ TEST_F(TemplateURLTest, HostAndSearchTermKey) {
struct TestData {
const std::string url;
const std::string host;
+ const url::Parsed::ComponentType location;
const std::string path;
+ const bool ignore_path_ending;
const std::string search_term_key;
+ const std::string search_term_value_prefix;
+ const std::string search_term_value_suffix;
} test_data[] = {
- { "http://blah/?foo=bar&q={searchTerms}&b=x", "blah", "/", "q"},
- { "http://blah/{searchTerms}", "blah", "/", ""},
+ { "http://blah/?foo=bar&q={searchTerms}&b=x",
+ "blah", url::Parsed::QUERY, "/", false, "q", "", ""},
- // No term should result in empty values.
- { "http://blah/", "", "", ""},
+ { "http://blah/?foo=bar#q={searchTerms}&b=x",
+ "blah", url::Parsed::REF, "/", false, "q", "", ""},
- // Multiple terms should result in empty values.
- { "http://blah/?q={searchTerms}&x={searchTerms}", "", "", ""},
+ { "http://blah/{searchTerms}",
+ "blah", url::Parsed::PATH, "/", false, "", "/", ""},
- // Term in the host shouldn't match.
- { "http://{searchTerms}", "", "", ""},
+ { "http://blah/{searchTerms}/",
+ "blah", url::Parsed::PATH, "//", false, "", "/", "/"},
- { "http://blah/?q={searchTerms}", "blah", "/", "q"},
- { "https://blah/?q={searchTerms}", "blah", "/", "q"},
+ { "http://blah/begin/{searchTerms}/end",
+ "blah", url::Parsed::PATH, "/begin//end", false, "", "/begin/", "/end"},
- // Single term with extra chars in value should match.
- { "http://blah/?q=stock:{searchTerms}", "blah", "/", "q"},
- };
+ // No term should result in empty values.
+ { "http://blah/", "", url::Parsed::QUERY, "", false, "", "", ""},
- for (size_t i = 0; i < arraysize(test_data); ++i) {
- TemplateURLData data;
- data.SetURL(test_data[i].url);
- TemplateURL url(data);
- EXPECT_EQ(test_data[i].host, url.url_ref().GetHost(search_terms_data_));
- EXPECT_EQ(test_data[i].path, url.url_ref().GetPath(search_terms_data_));
- EXPECT_EQ(test_data[i].search_term_key,
- url.url_ref().GetSearchTermKey(search_terms_data_));
- }
-}
+ // Term in the host shouldn't match.
+ { "http://{searchTerms}", "", url::Parsed::QUERY, "", false, "", "", "" },
-TEST_F(TemplateURLTest, SearchTermKeyLocation) {
- struct TestData {
- const std::string url;
- const url::Parsed::ComponentType location;
- const std::string path;
- size_t position_in_path;
- } test_data[] = {
- { "http://blah/{searchTerms}/", url::Parsed::PATH, "//", 1 },
- { "http://blah/{searchTerms}", url::Parsed::PATH, "/", 1 },
- { "http://blah/begin/{searchTerms}/end", url::Parsed::PATH, "/begin//end", 7 },
-
- { "http://blah/?foo=bar&q={searchTerms}&b=x", url::Parsed::QUERY,
- "/", std::string::npos },
- { "http://blah/?foo=bar#x={searchTerms}&b=x", url::Parsed::REF,
- "/", std::string::npos },
// searchTerms is a key, not a value, so this should result in an empty
// value.
{ "http://blah/?foo=bar#x=012345678901234&a=b&{searchTerms}=x",
- url::Parsed::QUERY, std::string(), std::string::npos },
-
- // Multiple search terms should result in empty values.
- { "http://blah/{searchTerms}?q={searchTerms}", url::Parsed::QUERY,
- "", std::string::npos },
- { "http://blah/{searchTerms}#x={searchTerms}", url::Parsed::QUERY,
- "", std::string::npos },
- { "http://blah/?q={searchTerms}#x={searchTerms}", url::Parsed::QUERY,
- "", std::string::npos },
+ "", url::Parsed::QUERY, "", false, "", "", "" },
+
+ // Multiple terms should result in empty values.
+ { "http://blah/?q={searchTerms}&x={searchTerms}",
+ "", url::Parsed::QUERY, "", false, "", "", "" },
+
+ { "http://blah/{searchTerms}?q={searchTerms}",
+ "", url::Parsed::QUERY, "", false, "", "", "" },
+
+ { "http://blah/{searchTerms}#x={searchTerms}",
+ "", url::Parsed::QUERY, "", false, "", "", "" },
+
+ // Fixed prefixes and suffixes in query or ref.
+ { "http://blah/?q=stock:{searchTerms}",
+ "blah", url::Parsed::QUERY, "/", false, "q", "stock:", ""},
+
+ { "http://www.example.com/?q=chromium-{searchTerms}@chromium.org",
+ "www.example.com", url::Parsed::QUERY, "/", false, "q", "chromium-",
+ "@chromium.org" },
+
+ { "http://www.example.com/#q=chromium-{searchTerms}@chromium.org",
+ "www.example.com", url::Parsed::REF, "/", false, "q", "chromium-",
+ "@chromium.org" },
+
+ { "http://www.example.com/chromium-{searchTerms}@chromium.org/info",
+ "www.example.com", url::Parsed::PATH, "/chromium-@chromium.org/info",
+ false, "", "/chromium-", "@chromium.org/info" },
+
+ // Ignoring ending of path.
+ { "http://blah/{google:ignorePathEnding}?q={searchTerms}",
+ "blah", url::Parsed::QUERY, "/", true, "q", "", "" },
+
+ { "http://blah/path/{google:ignorePathEnding}/end#q={searchTerms}",
+ "blah", url::Parsed::REF, "/path/", true, "q", "", "" },
+
+ { "http://blah/path/{searchTerms}/{google:ignorePathEnding}",
+ "blah", url::Parsed::PATH, "/path//", true, "", "/path/", "/" },
+
+ { "http://blah/path/{searchTerms}/data/{google:ignorePathEnding}",
+ "blah", url::Parsed::PATH, "/path//data/", true, "", "/path/", "/data/" },
+
+ { "http://blah/path/{searchTerms}{google:ignorePathEnding}",
+ "blah", url::Parsed::PATH, "/path/", false, "", "/path/", "" },
+
+ { "http://blah/path/{google:ignorePathEnding}{searchTerms}",
+ "", url::Parsed::QUERY, "", false, "", "", "" },
};
for (size_t i = 0; i < arraysize(test_data); ++i) {
TemplateURLData data;
data.SetURL(test_data[i].url);
TemplateURL url(data);
+ const TemplateURLRef& url_ref = url.url_ref();
+ EXPECT_EQ(test_data[i].host, url_ref.GetHost(search_terms_data_));
EXPECT_EQ(test_data[i].location,
- url.url_ref().GetSearchTermKeyLocation(search_terms_data_));
- EXPECT_EQ(test_data[i].path,
- url.url_ref().GetPath(search_terms_data_));
- EXPECT_EQ(test_data[i].position_in_path,
- url.url_ref().GetSearchTermPositionInPath(search_terms_data_));
+ url_ref.GetSearchTermKeyLocation(search_terms_data_));
+ EXPECT_EQ(test_data[i].path, url_ref.GetPath(search_terms_data_));
+ EXPECT_EQ(test_data[i].ignore_path_ending,
+ url_ref.GetIgnorePathEnding(search_terms_data_));
+ EXPECT_EQ(test_data[i].search_term_key,
+ url_ref.GetSearchTermKey(search_terms_data_));
+ EXPECT_EQ(test_data[i].search_term_value_prefix,
+ url_ref.GetSearchTermValuePrefix(search_terms_data_));
+ EXPECT_EQ(test_data[i].search_term_value_suffix,
+ url_ref.GetSearchTermValueSuffix(search_terms_data_));
}
}
@@ -1117,6 +1141,75 @@ TEST_F(TemplateURLTest, ExtractSearchTermsFromURLPath) {
EXPECT_EQ(base::string16(), result);
}
+// Test that extracting search terms from URL works correctly when
+// the "{google:ignorePathEnding}" marker is contained in a search template.
+TEST_F(TemplateURLTest, ExtractSearchTermsFromURLWithIgnorePathEnding) {
+ struct TestCase {
+ std::string search_template;
+ GURL url;
+ bool result;
+ base::string16 search_terms;
+ };
+
+ const std::string template1 =
+ "http://h.com/begin/{google:ignorePathEnding}?q={searchTerms}";
+ const std::string template2 =
+ "http://h.com/begin/{searchTerms}/{google:ignorePathEnding}";
+ const std::string template3 =
+ "http://h.com/begin/{searchTerms}/mid/{google:ignorePathEnding}";
+
+ const TestCase test_cases[] = {
+ // Cases if "{searchTerms}" are located in query.
+ { template1, GURL("http://h.com/begin/blablabla?q=something"),
+ true, ASCIIToUTF16("something") },
+
+ { template1, GURL("http://h.com/begin/bla/bla/bla?q=anotherthing"),
+ true, ASCIIToUTF16("anotherthing") },
+
+ { template1, GURL("http://h.com/begin/?q=thirdthing"),
+ true, ASCIIToUTF16("thirdthing") },
+
+ { template1, GURL("http://h.com/begin?q=abc"), false, base::string16() },
+ { template1, GURL("http://h.com/about?q=abc"), false, base::string16() },
+ { template1, GURL("http://h.com?q=abc"), false, base::string16() },
+
+ // Cases if "{searchTerms}" are located in path.
+ { template2, GURL("http://h.com/begin/something/blablabla"),
+ true, ASCIIToUTF16("something") },
+
+ { template2, GURL("http://h.com/begin/anotherthing/bla/bla/bla"),
+ true, ASCIIToUTF16("anotherthing") },
+
+ { template2, GURL("http://h.com/begin/thirdthing/"),
+ true, ASCIIToUTF16("thirdthing") },
+
+ { template2, GURL("http://h.com/begin//"), false, base::string16() },
+ { template2, GURL("http://h.com/begin/abc"), false, base::string16() },
+ { template2, GURL("http://h.com/begin/"), false, base::string16() },
+ { template2, GURL("http://h.com/begin"), false, base::string16() },
+ { template2, GURL("http://h.com/begin"), false, base::string16() },
+ { template2, GURL("http://h.com/about"), false, base::string16() },
+ { template2, GURL("http://h.com"), false, base::string16() },
+
+ { template3, GURL("http://h.com/begin/bla/bla/mid/bla"),
+ true, ASCIIToUTF16("bla/bla") },
+ { template3, GURL("http://h.com/begin/bla/bla/mid/"),
+ true, ASCIIToUTF16("bla/bla") },
+ };
+
+ for (size_t i = 0; i != arraysize(test_cases); ++i) {
+ const TestCase& test_case = test_cases[i];
+ TemplateURLData data;
+ data.SetURL(test_case.search_template);
+ TemplateURL template_url(data);
+ base::string16 search_terms;
+ const bool result = template_url.ExtractSearchTermsFromURL(
+ test_case.url, search_terms_data_, &search_terms);
+ EXPECT_EQ(test_case.result, result) << "i=" << i;
+ EXPECT_EQ(test_case.search_terms, search_terms) << "i=" << i;
+ }
+}
+
// Checks that the ExtractSearchTermsFromURL function works correctly
// for urls containing non-latin characters in UTF8 encoding.
TEST_F(TemplateURLTest, ExtractSearchTermsFromUTF8URL) {
« no previous file with comments | « components/search_engines/template_url.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698