| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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. | |
| 4 | |
| 5 #include "base/file_util.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/path_service.h" | |
| 8 #include "base/scoped_ptr.h" | |
| 9 #include "chrome/browser/template_url.h" | |
| 10 #include "chrome/browser/template_url_parser.h" | |
| 11 #include "chrome/common/chrome_paths.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 class TemplateURLParserTest : public testing::Test { | |
| 15 public: | |
| 16 TemplateURLParserTest() : parse_result_(true) { | |
| 17 } | |
| 18 | |
| 19 virtual void SetUp() { | |
| 20 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &full_path_)); | |
| 21 file_util::AppendToPath(&full_path_, L"osdd"); | |
| 22 if (!file_util::PathExists(full_path_)) { | |
| 23 LOG(ERROR) << | |
| 24 L"This test can't be run without some non-redistributable data"; | |
| 25 full_path_.clear(); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 bool IsDisabled() { | |
| 30 return full_path_.empty(); | |
| 31 } | |
| 32 | |
| 33 // Parses the OpenSearch description document at file_name (relative to | |
| 34 // the data dir). The TemplateURL is placed in template_url_. | |
| 35 // The result of Parse is stored in the field parse_result_ (this doesn't | |
| 36 // use a return value due to internally using ASSERT_). | |
| 37 void ParseFile(const std::wstring& file_name, | |
| 38 TemplateURLParser::ParameterFilter* filter) { | |
| 39 std::wstring full_path(full_path_); | |
| 40 file_util::AppendToPath(&full_path, file_name); | |
| 41 parse_result_ = false; | |
| 42 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &full_path)); | |
| 43 file_util::AppendToPath(&full_path, L"osdd"); | |
| 44 file_util::AppendToPath(&full_path, file_name); | |
| 45 ASSERT_TRUE(file_util::PathExists(full_path)); | |
| 46 | |
| 47 std::string contents; | |
| 48 file_util::ReadFileToString(full_path, &contents); | |
| 49 parse_result_ = TemplateURLParser::Parse( | |
| 50 reinterpret_cast<const unsigned char*>(contents.c_str()), | |
| 51 contents.length(), filter, &template_url_); | |
| 52 } | |
| 53 | |
| 54 // ParseFile parses the results into this template_url. | |
| 55 TemplateURL template_url_; | |
| 56 | |
| 57 std::wstring full_path_; | |
| 58 | |
| 59 // Result of the parse. | |
| 60 bool parse_result_; | |
| 61 }; | |
| 62 | |
| 63 TEST_F(TemplateURLParserTest, FailOnBogusURL) { | |
| 64 if (IsDisabled()) | |
| 65 return; | |
| 66 ParseFile(L"bogus.xml", NULL); | |
| 67 EXPECT_FALSE(parse_result_); | |
| 68 } | |
| 69 | |
| 70 TEST_F(TemplateURLParserTest, PassOnHTTPS) { | |
| 71 if (IsDisabled()) | |
| 72 return; | |
| 73 ParseFile(L"https.xml", NULL); | |
| 74 EXPECT_TRUE(parse_result_); | |
| 75 } | |
| 76 | |
| 77 TEST_F(TemplateURLParserTest, FailOnPost) { | |
| 78 if (IsDisabled()) | |
| 79 return; | |
| 80 ParseFile(L"post.xml", NULL); | |
| 81 EXPECT_FALSE(parse_result_); | |
| 82 } | |
| 83 | |
| 84 TEST_F(TemplateURLParserTest, TestDictionary) { | |
| 85 if (IsDisabled()) | |
| 86 return; | |
| 87 ParseFile(L"dictionary.xml", NULL); | |
| 88 ASSERT_TRUE(parse_result_); | |
| 89 EXPECT_EQ(L"Dictionary.com", template_url_.short_name()); | |
| 90 EXPECT_TRUE(template_url_.GetFavIconURL() == | |
| 91 GURL("http://cache.lexico.com/g/d/favicon.ico")); | |
| 92 EXPECT_TRUE(template_url_.url() != NULL); | |
| 93 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 94 EXPECT_EQ(template_url_.url()->url(), | |
| 95 L"http://dictionary.reference.com/browse/{searchTerms}?r=75"); | |
| 96 } | |
| 97 | |
| 98 TEST_F(TemplateURLParserTest, TestMSDN) { | |
| 99 if (IsDisabled()) | |
| 100 return; | |
| 101 ParseFile(L"msdn.xml", NULL); | |
| 102 ASSERT_TRUE(parse_result_); | |
| 103 EXPECT_EQ(L"Search \" MSDN", template_url_.short_name()); | |
| 104 EXPECT_TRUE(template_url_.GetFavIconURL() == | |
| 105 GURL("http://search.msdn.microsoft.com/search/favicon.ico")); | |
| 106 EXPECT_TRUE(template_url_.url() != NULL); | |
| 107 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 108 EXPECT_EQ(template_url_.url()->url(), | |
| 109 L"http://search.msdn.microsoft.com/search/default.aspx?Query={search
Terms}&brand=msdn&locale=en-US"); | |
| 110 } | |
| 111 | |
| 112 TEST_F(TemplateURLParserTest, TestWikipedia) { | |
| 113 if (IsDisabled()) | |
| 114 return; | |
| 115 ParseFile(L"wikipedia.xml", NULL); | |
| 116 ASSERT_TRUE(parse_result_); | |
| 117 EXPECT_EQ(L"Wikipedia (English)", template_url_.short_name()); | |
| 118 EXPECT_TRUE(template_url_.GetFavIconURL() == | |
| 119 GURL("http://en.wikipedia.org/favicon.ico")); | |
| 120 EXPECT_TRUE(template_url_.url() != NULL); | |
| 121 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 122 EXPECT_EQ(template_url_.url()->url(), | |
| 123 L"http://en.wikipedia.org/w/index.php?title=Special:Search&search={searchT
erms}"); | |
| 124 EXPECT_TRUE(template_url_.suggestions_url() != NULL); | |
| 125 EXPECT_TRUE(template_url_.suggestions_url()->SupportsReplacement()); | |
| 126 EXPECT_EQ(template_url_.suggestions_url()->url(), | |
| 127 L"http://en.wikipedia.org/w/api.php?action=opensearch&search={searchTerms}
"); | |
| 128 ASSERT_EQ(2U, template_url_.input_encodings().size()); | |
| 129 EXPECT_EQ("UTF-8", template_url_.input_encodings()[0]); | |
| 130 EXPECT_EQ("Shift_JIS", template_url_.input_encodings()[1]); | |
| 131 } | |
| 132 | |
| 133 TEST_F(TemplateURLParserTest, NoCrashOnEmptyAttributes) { | |
| 134 if (IsDisabled()) | |
| 135 return; | |
| 136 ParseFile(L"url_with_no_attributes.xml", NULL); | |
| 137 } | |
| 138 | |
| 139 // Filters any param which as an occurrence of name_str_ in its name or an | |
| 140 // occurrence of value_str_ in its value. | |
| 141 class ParamFilterImpl : public TemplateURLParser::ParameterFilter { | |
| 142 public: | |
| 143 ParamFilterImpl(std::string name_str, std::string value_str) | |
| 144 : name_str_(name_str), | |
| 145 value_str_(value_str) { | |
| 146 } | |
| 147 | |
| 148 bool KeepParameter(const std::string& key, const std::string& value) { | |
| 149 return (name_str_.empty() || key.find(name_str_) == std::string::npos) && | |
| 150 (value_str_.empty() || value.find(value_str_) == std::string::npos); | |
| 151 } | |
| 152 | |
| 153 private: | |
| 154 std::string name_str_; | |
| 155 std::string value_str_; | |
| 156 | |
| 157 DISALLOW_EVIL_CONSTRUCTORS(ParamFilterImpl); | |
| 158 }; | |
| 159 | |
| 160 TEST_F(TemplateURLParserTest, TestFirefoxEbay) { | |
| 161 if (IsDisabled()) | |
| 162 return; | |
| 163 // This file uses the Parameter extension | |
| 164 // (see http://www.opensearch.org/Specifications/OpenSearch/Extensions/Paramet
er/1.0) | |
| 165 ParamFilterImpl filter("ebay", "ebay"); | |
| 166 ParseFile(L"firefox_ebay.xml", &filter); | |
| 167 ASSERT_TRUE(parse_result_); | |
| 168 EXPECT_EQ(L"eBay", template_url_.short_name()); | |
| 169 EXPECT_TRUE(template_url_.url() != NULL); | |
| 170 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 171 std::wstring exp_url = | |
| 172 L"http://search.ebay.com/search/search.dll?query={searchTerms}&" | |
| 173 L"MfcISAPICommand=GetResult&ht=1&srchdesc=n&maxRecordsReturned=300&" | |
| 174 L"maxRecordsPerPage=50&SortProperty=MetaEndSort"; | |
| 175 EXPECT_EQ(exp_url, template_url_.url()->url()); | |
| 176 ASSERT_EQ(1U, template_url_.input_encodings().size()); | |
| 177 EXPECT_EQ("ISO-8859-1", template_url_.input_encodings()[0]); | |
| 178 EXPECT_EQ(GURL("http://search.ebay.com/favicon.ico"), | |
| 179 template_url_.GetFavIconURL()); | |
| 180 } | |
| 181 | |
| 182 TEST_F(TemplateURLParserTest, TestFirefoxWebster) { | |
| 183 if (IsDisabled()) | |
| 184 return; | |
| 185 // This XML file uses a namespace. | |
| 186 ParamFilterImpl filter("", "Mozilla"); | |
| 187 ParseFile(L"firefox_webster.xml", &filter); | |
| 188 ASSERT_TRUE(parse_result_); | |
| 189 EXPECT_EQ(L"Webster", template_url_.short_name()); | |
| 190 EXPECT_TRUE(template_url_.url() != NULL); | |
| 191 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 192 EXPECT_EQ(L"http://www.webster.com/cgi-bin/dictionary?va={searchTerms}", | |
| 193 template_url_.url()->url()); | |
| 194 ASSERT_EQ(1U, template_url_.input_encodings().size()); | |
| 195 EXPECT_EQ("ISO-8859-1", template_url_.input_encodings()[0]); | |
| 196 EXPECT_EQ(GURL("http://www.webster.com/favicon.ico"), | |
| 197 template_url_.GetFavIconURL()); | |
| 198 } | |
| 199 | |
| 200 TEST_F(TemplateURLParserTest, TestFirefoxYahoo) { | |
| 201 if (IsDisabled()) | |
| 202 return; | |
| 203 // This XML file uses a namespace. | |
| 204 ParamFilterImpl filter("", "Mozilla"); | |
| 205 ParseFile(L"firefox_yahoo.xml", &filter); | |
| 206 ASSERT_TRUE(parse_result_); | |
| 207 EXPECT_EQ(L"Yahoo", template_url_.short_name()); | |
| 208 EXPECT_TRUE(template_url_.url() != NULL); | |
| 209 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 210 EXPECT_EQ(L"http://ff.search.yahoo.com/gossip?" | |
| 211 L"output=fxjson&command={searchTerms}", | |
| 212 template_url_.suggestions_url()->url()); | |
| 213 EXPECT_EQ(L"http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8", | |
| 214 template_url_.url()->url()); | |
| 215 ASSERT_EQ(1U, template_url_.input_encodings().size()); | |
| 216 EXPECT_EQ("UTF-8", template_url_.input_encodings()[0]); | |
| 217 EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"), | |
| 218 template_url_.GetFavIconURL()); | |
| 219 } | |
| 220 | |
| 221 // Make sure we ignore POST suggestions (this is the same XML file as | |
| 222 // firefox_yahoo.xml, the suggestion method was just changed to POST). | |
| 223 TEST_F(TemplateURLParserTest, TestPostSuggestion) { | |
| 224 if (IsDisabled()) | |
| 225 return; | |
| 226 // This XML file uses a namespace. | |
| 227 ParamFilterImpl filter("", "Mozilla"); | |
| 228 ParseFile(L"post_suggestion.xml", &filter); | |
| 229 ASSERT_TRUE(parse_result_); | |
| 230 EXPECT_EQ(L"Yahoo", template_url_.short_name()); | |
| 231 EXPECT_TRUE(template_url_.url() != NULL); | |
| 232 EXPECT_TRUE(template_url_.url()->SupportsReplacement()); | |
| 233 EXPECT_TRUE(template_url_.suggestions_url() == NULL); | |
| 234 EXPECT_EQ(L"http://search.yahoo.com/search?p={searchTerms}&ei=UTF-8", | |
| 235 template_url_.url()->url()); | |
| 236 ASSERT_EQ(1U, template_url_.input_encodings().size()); | |
| 237 EXPECT_EQ("UTF-8", template_url_.input_encodings()[0]); | |
| 238 EXPECT_EQ(GURL("http://search.yahoo.com/favicon.ico"), | |
| 239 template_url_.GetFavIconURL()); | |
| 240 } | |
| OLD | NEW |