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