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