Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / components / search_engines / template_url_parser_unittest.cc
blobea94a51f2f2550c4ddcd73f64d3c4a5d5354a9a9
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
3 // found in the LICENSE file.
5 #include "base/files/file_util.h"
6 #include "base/logging.h"
7 #include "base/path_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/chrome_paths.h"
10 #include "components/search_engines/search_terms_data.h"
11 #include "components/search_engines/template_url.h"
12 #include "components/search_engines/template_url_parser.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16;
17 // ParamFilterImpl ------------------------------------------------------------
19 // Filters any param which as an occurrence of name_str_ in its name or an
20 // occurrence of value_str_ in its value.
21 class ParamFilterImpl : public TemplateURLParser::ParameterFilter {
22 public:
23 ParamFilterImpl(std::string name_str, std::string value_str);
24 ~ParamFilterImpl() override;
26 bool KeepParameter(const std::string& key, const std::string& value) override;
28 private:
29 std::string name_str_;
30 std::string value_str_;
32 DISALLOW_COPY_AND_ASSIGN(ParamFilterImpl);
35 ParamFilterImpl::ParamFilterImpl(std::string name_str, std::string value_str)
36 : name_str_(name_str),
37 value_str_(value_str) {
40 ParamFilterImpl::~ParamFilterImpl() {
43 bool ParamFilterImpl::KeepParameter(const std::string& key,
44 const std::string& value) {
45 return (name_str_.empty() || key.find(name_str_) == std::string::npos) &&
46 (value_str_.empty() || value.find(value_str_) == std::string::npos);
50 // TemplateURLParserTest ------------------------------------------------------
52 class TemplateURLParserTest : public testing::Test {
53 protected:
54 TemplateURLParserTest();
55 ~TemplateURLParserTest() override;
57 void SetUp() override;
59 bool is_disabled() const;
61 // Parses the OpenSearch description document at file_name (relative to the
62 // data dir). The TemplateURL is placed in |template_url_|.
63 void ParseFile(const std::string& file_name,
64 TemplateURLParser::ParameterFilter* filter);
66 // ParseFile parses the results into this template_url.
67 scoped_ptr<TemplateURL> template_url_;
69 private:
70 base::FilePath full_path_;
73 TemplateURLParserTest::TemplateURLParserTest() {
76 TemplateURLParserTest::~TemplateURLParserTest() {
79 void TemplateURLParserTest::SetUp() {
80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &full_path_));
81 full_path_ = full_path_.AppendASCII("osdd");
82 if (!base::PathExists(full_path_)) {
83 LOG(ERROR) <<
84 "This test can't be run without some non-redistributable data";
85 full_path_ = base::FilePath();
89 bool TemplateURLParserTest::is_disabled() const {
90 return full_path_.empty();
93 void TemplateURLParserTest::ParseFile(
94 const std::string& file_name,
95 TemplateURLParser::ParameterFilter* filter) {
96 base::FilePath full_path;
97 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &full_path));
98 full_path = full_path.AppendASCII("osdd");
99 full_path = full_path.AppendASCII(file_name);
100 ASSERT_TRUE(base::PathExists(full_path));
102 std::string contents;
103 ASSERT_TRUE(base::ReadFileToString(full_path, &contents));
104 template_url_.reset(TemplateURLParser::Parse(
105 SearchTermsData(), false, contents.data(), contents.length(), filter));
109 // Actual tests ---------------------------------------------------------------
111 TEST_F(TemplateURLParserTest, FailOnBogusURL) {
112 if (is_disabled())
113 return;
114 ASSERT_NO_FATAL_FAILURE(ParseFile("bogus.xml", NULL));
115 EXPECT_FALSE(template_url_.get());
118 TEST_F(TemplateURLParserTest, PassOnHTTPS) {
119 if (is_disabled())
120 return;
121 ASSERT_NO_FATAL_FAILURE(ParseFile("https.xml", NULL));
122 EXPECT_TRUE(template_url_.get());
125 TEST_F(TemplateURLParserTest, FailOnPost) {
126 if (is_disabled())
127 return;
128 ASSERT_NO_FATAL_FAILURE(ParseFile("post.xml", NULL));
129 EXPECT_FALSE(template_url_.get());
132 TEST_F(TemplateURLParserTest, TestDictionary) {
133 if (is_disabled())
134 return;
135 ASSERT_NO_FATAL_FAILURE(ParseFile("dictionary.xml", NULL));
136 ASSERT_TRUE(template_url_.get());
137 EXPECT_EQ(ASCIIToUTF16("Dictionary.com"), template_url_->short_name());
138 EXPECT_EQ(GURL("http://cache.lexico.com/g/d/favicon.ico"),
139 template_url_->favicon_url());
140 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
141 EXPECT_EQ("http://dictionary.reference.com/browse/{searchTerms}?r=75",
142 template_url_->url());
145 TEST_F(TemplateURLParserTest, TestMSDN) {
146 if (is_disabled())
147 return;
148 ASSERT_NO_FATAL_FAILURE(ParseFile("msdn.xml", NULL));
149 ASSERT_TRUE(template_url_.get());
150 EXPECT_EQ(ASCIIToUTF16("Search \" MSDN"), template_url_->short_name());
151 EXPECT_EQ(GURL("http://search.msdn.microsoft.com/search/favicon.ico"),
152 template_url_->favicon_url());
153 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
154 EXPECT_EQ("http://search.msdn.microsoft.com/search/default.aspx?"
155 "Query={searchTerms}&brand=msdn&locale=en-US",
156 template_url_->url());
159 TEST_F(TemplateURLParserTest, TestWikipedia) {
160 if (is_disabled())
161 return;
162 ASSERT_NO_FATAL_FAILURE(ParseFile("wikipedia.xml", NULL));
163 ASSERT_TRUE(template_url_.get());
164 EXPECT_EQ(ASCIIToUTF16("Wikipedia (English)"), template_url_->short_name());
165 EXPECT_EQ(GURL("http://en.wikipedia.org/favicon.ico"),
166 template_url_->favicon_url());
167 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
168 EXPECT_EQ("http://en.wikipedia.org/w/index.php?"
169 "title=Special:Search&search={searchTerms}",
170 template_url_->url());
171 EXPECT_TRUE(template_url_->suggestions_url_ref().SupportsReplacement(
172 SearchTermsData()));
173 EXPECT_EQ("http://en.wikipedia.org/w/api.php?"
174 "action=opensearch&search={searchTerms}",
175 template_url_->suggestions_url());
176 ASSERT_EQ(2U, template_url_->input_encodings().size());
177 EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
178 EXPECT_EQ("Shift_JIS", template_url_->input_encodings()[1]);
181 TEST_F(TemplateURLParserTest, NoCrashOnEmptyAttributes) {
182 if (is_disabled())
183 return;
184 ASSERT_NO_FATAL_FAILURE(ParseFile("url_with_no_attributes.xml", NULL));
187 TEST_F(TemplateURLParserTest, TestFirefoxEbay) {
188 if (is_disabled())
189 return;
190 // This file uses the Parameter extension
191 // (see http://www.opensearch.org/Specifications/OpenSearch/Extensions/Parameter/1.0)
192 ParamFilterImpl filter("ebay", "ebay");
193 ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_ebay.xml", &filter));
194 ASSERT_TRUE(template_url_.get());
195 EXPECT_EQ(ASCIIToUTF16("eBay"), template_url_->short_name());
196 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
197 EXPECT_EQ("http://search.ebay.com/search/search.dll?query={searchTerms}&"
198 "MfcISAPICommand=GetResult&ht=1&srchdesc=n&maxRecordsReturned=300&"
199 "maxRecordsPerPage=50&SortProperty=MetaEndSort",
200 template_url_->url());
201 ASSERT_EQ(1U, template_url_->input_encodings().size());
202 EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]);
203 EXPECT_EQ(GURL("http://search.ebay.com/favicon.ico"),
204 template_url_->favicon_url());
207 TEST_F(TemplateURLParserTest, TestFirefoxWebster) {
208 if (is_disabled())
209 return;
210 // This XML file uses a namespace.
211 ParamFilterImpl filter(std::string(), "Mozilla");
212 ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_webster.xml", &filter));
213 ASSERT_TRUE(template_url_.get());
214 EXPECT_EQ(ASCIIToUTF16("Webster"), template_url_->short_name());
215 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
216 EXPECT_EQ("http://www.webster.com/cgi-bin/dictionary?va={searchTerms}",
217 template_url_->url());
218 ASSERT_EQ(1U, template_url_->input_encodings().size());
219 EXPECT_EQ("ISO-8859-1", template_url_->input_encodings()[0]);
220 EXPECT_EQ(GURL("http://www.webster.com/favicon.ico"),
221 template_url_->favicon_url());
224 TEST_F(TemplateURLParserTest, TestFirefoxYahoo) {
225 if (is_disabled())
226 return;
227 // This XML file uses a namespace.
228 ParamFilterImpl filter(std::string(), "Mozilla");
229 ASSERT_NO_FATAL_FAILURE(ParseFile("firefox_yahoo.xml", &filter));
230 ASSERT_TRUE(template_url_.get());
231 EXPECT_EQ(ASCIIToUTF16("Yahoo"), template_url_->short_name());
232 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
233 EXPECT_EQ("http://ff.search.yahoo.com/gossip?"
234 "output=fxjson&command={searchTerms}",
235 template_url_->suggestions_url());
236 EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8",
237 template_url_->url());
238 ASSERT_EQ(1U, template_url_->input_encodings().size());
239 EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
240 EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"),
241 template_url_->favicon_url());
244 // Make sure we ignore POST suggestions (this is the same XML file as
245 // firefox_yahoo.xml, the suggestion method was just changed to POST).
246 TEST_F(TemplateURLParserTest, TestPostSuggestion) {
247 if (is_disabled())
248 return;
249 // This XML file uses a namespace.
250 ParamFilterImpl filter(std::string(), "Mozilla");
251 ASSERT_NO_FATAL_FAILURE(ParseFile("post_suggestion.xml", &filter));
252 ASSERT_TRUE(template_url_.get());
253 EXPECT_EQ(ASCIIToUTF16("Yahoo"), template_url_->short_name());
254 EXPECT_TRUE(template_url_->url_ref().SupportsReplacement(SearchTermsData()));
255 EXPECT_TRUE(template_url_->suggestions_url().empty());
256 EXPECT_EQ("http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8",
257 template_url_->url());
258 ASSERT_EQ(1U, template_url_->input_encodings().size());
259 EXPECT_EQ("UTF-8", template_url_->input_encodings()[0]);
260 EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"),
261 template_url_->favicon_url());