Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / chrome / renderer / searchbox / searchbox_unittest.cc
blob24300554f9349f4b401927607daf15b5b223ec45
1 // Copyright 2013 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 "chrome/renderer/searchbox/searchbox.h"
7 #include <map>
8 #include <string>
10 #include "base/basictypes.h"
11 #include "chrome/common/instant_types.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
15 namespace {
17 const auto FAVICON = SearchBox::FAVICON;
18 const auto LARGE_ICON = SearchBox::LARGE_ICON;
19 const auto FALLBACK_ICON = SearchBox::FALLBACK_ICON;
20 const auto THUMB = SearchBox::THUMB;
22 const char* kUrlString1 = "http://www.google.com";
23 const char* kUrlString2 = "http://www.chromium.org/path/q=3#r=4";
24 const char* kUrlString3 = "http://www.youtube.com:8080/hosps";
26 // Mock helper to test internal::TranslateIconRestrictedUrl().
27 class MockIconURLHelper: public SearchBox::IconURLHelper {
28 public:
29 MockIconURLHelper();
30 ~MockIconURLHelper() override;
31 int GetViewID() const override;
32 std::string GetURLStringFromRestrictedID(InstantRestrictedID rid) const
33 override;
35 private:
36 std::map<InstantRestrictedID, std::string> rid_to_url_string_;
39 MockIconURLHelper::MockIconURLHelper() {
40 rid_to_url_string_[1] = kUrlString1;
41 rid_to_url_string_[2] = kUrlString2;
42 rid_to_url_string_[3] = kUrlString3;
45 MockIconURLHelper::~MockIconURLHelper() {
48 int MockIconURLHelper::GetViewID() const {
49 return 137;
52 std::string MockIconURLHelper::GetURLStringFromRestrictedID(
53 InstantRestrictedID rid) const {
54 auto it = rid_to_url_string_.find(rid);
55 return it == rid_to_url_string_.end() ? std::string() : it->second;
58 } // namespace
60 namespace internal {
62 // Defined in searchbox.cc
63 bool ParseViewIdAndRestrictedId(const std::string id_part,
64 int* view_id_out,
65 InstantRestrictedID* rid_out);
67 // Defined in searchbox.cc
68 bool ParseIconRestrictedUrl(const GURL& url,
69 SearchBox::ImageSourceType type,
70 std::string* param_part,
71 int* view_id,
72 InstantRestrictedID* rid);
74 // Defined in searchbox.cc
75 bool TranslateIconRestrictedUrl(const GURL& transient_url,
76 SearchBox::ImageSourceType type,
77 const SearchBox::IconURLHelper& helper,
78 GURL* url);
80 TEST(SearchBoxUtilTest, ParseViewIdAndRestrictedIdSuccess) {
81 int view_id = -1;
82 InstantRestrictedID rid = -1;
84 EXPECT_TRUE(ParseViewIdAndRestrictedId("2/3", &view_id, &rid));
85 EXPECT_EQ(2, view_id);
86 EXPECT_EQ(3, rid);
88 EXPECT_TRUE(ParseViewIdAndRestrictedId("0/0", &view_id, &rid));
89 EXPECT_EQ(0, view_id);
90 EXPECT_EQ(0, rid);
92 EXPECT_TRUE(ParseViewIdAndRestrictedId("1048576/314", &view_id, &rid));
93 EXPECT_EQ(1048576, view_id);
94 EXPECT_EQ(314, rid);
96 // Odd but not fatal.
97 EXPECT_TRUE(ParseViewIdAndRestrictedId("00/09", &view_id, &rid));
98 EXPECT_EQ(0, view_id);
99 EXPECT_EQ(9, rid);
101 // Tolerates multiple, leading, and trailing "/".
102 EXPECT_TRUE(ParseViewIdAndRestrictedId("2////3", &view_id, &rid));
103 EXPECT_EQ(2, view_id);
104 EXPECT_EQ(3, rid);
106 EXPECT_TRUE(ParseViewIdAndRestrictedId("5/6/", &view_id, &rid));
107 EXPECT_EQ(5, view_id);
108 EXPECT_EQ(6, rid);
110 EXPECT_TRUE(ParseViewIdAndRestrictedId("/7/8", &view_id, &rid));
111 EXPECT_EQ(7, view_id);
112 EXPECT_EQ(8, rid);
115 TEST(SearchBoxUtilTest, ParseViewIdAndRestrictedIdFailure) {
116 const char* test_cases[] = {
118 " ",
119 "/",
120 "2/",
121 "/3",
122 "2a/3",
123 "2/3a",
124 " 2/3",
125 "2/ 3",
126 "2 /3 ",
127 "23",
128 "2,3",
129 "-2/3",
130 "2/-3",
131 "2/3/1",
132 "blahblah",
133 "0xA/0x10",
135 for (size_t i = 0; i < arraysize(test_cases); ++i) {
136 int view_id = -1;
137 InstantRestrictedID rid = -1;
138 EXPECT_FALSE(ParseViewIdAndRestrictedId(test_cases[i], &view_id, &rid))
139 << " for test_cases[" << i << "]";
140 EXPECT_EQ(-1, view_id);
141 EXPECT_EQ(-1, rid);
145 TEST(SearchBoxUtilTest, ParseIconRestrictedUrlFaviconSuccess) {
146 struct {
147 SearchBox::ImageSourceType type;
148 const char* transient_url_str;
149 const char* expected_param_part;
150 int expected_view_id;
151 InstantRestrictedID expected_rid;
152 } test_cases[] = {
153 {FAVICON, "chrome-search://favicon/1/2", "", 1, 2},
154 {FAVICON, "chrome-search://favicon/size/16@2x/3/4", "size/16@2x/", 3, 4},
155 {FAVICON, "chrome-search://favicon/largest/5/6", "largest/", 5, 6},
156 {FAVICON, "chrome-search://favicon/origin/7/8", "origin/", 7, 8},
157 {FAVICON, "chrome-search://favicon/iconurl/9/10", "iconurl/", 9, 10},
158 {LARGE_ICON, "chrome-search://large-icon/96/1/2", "96/", 1, 2},
159 {LARGE_ICON, "chrome-search://large-icon/1/3/4", "1/", 3, 4},
160 // Size restriction is *not* enforced during parsing, but later.
161 {LARGE_ICON, "chrome-search://large-icon/1000000/5/6", "1000000/", 5, 6},
162 {FALLBACK_ICON, "chrome-search://fallback-icon/,,,,/1/2", ",,,,/", 1, 2},
163 {FALLBACK_ICON, "chrome-search://fallback-icon/1,,,,/3/4", "1,,,,/", 3, 4},
164 {FALLBACK_ICON, "chrome-search://fallback-icon/64,fff,black,0.4,0.6/5/6",
165 "64,fff,black,0.4,0.6/", 5, 6},
166 {THUMB, "chrome-search://thumb/1/2", "", 1, 2},
168 for (size_t i = 0; i < arraysize(test_cases); ++i) {
169 std::string param_part = "(unwritten)";
170 int view_id = -1;
171 InstantRestrictedID rid = -1;
172 EXPECT_TRUE(ParseIconRestrictedUrl(GURL(test_cases[i].transient_url_str),
173 test_cases[i].type, &param_part, &view_id, &rid))
174 << " for test_cases[" << i << "]";
175 EXPECT_EQ(test_cases[i].expected_param_part, param_part)
176 << " for test_cases[" << i << "]";
177 EXPECT_EQ(test_cases[i].expected_view_id, view_id)
178 << " for test_cases[" << i << "]";
179 EXPECT_EQ(test_cases[i].expected_rid, rid)
180 << " for test_cases[" << i << "]";
184 TEST(SearchBoxUtilTest, ParseIconRestrictedUrlFailure) {
185 struct {
186 SearchBox::ImageSourceType type;
187 const char* transient_url_str;
188 } test_cases[] = {
189 {FAVICON, "chrome-search://favicon/"},
190 {FAVICON, "chrome-search://favicon/3/"},
191 {FAVICON, "chrome-search://favicon/size/3/4"},
192 {FAVICON, "chrome-search://favicon/largest/http://www.google.com"},
193 {FAVICON, "chrome-search://favicon/size/16@2x/-1/10"},
194 {LARGE_ICON, "chrome-search://large-icon/"},
195 {LARGE_ICON, "chrome-search://large-icon/3"},
196 {LARGE_ICON, "chrome-search://large-icon/3/4"},
197 {LARGE_ICON, "chrome-search://large-icon/-1/3/4"},
198 {LARGE_ICON, "chrome-search://large-icon/0/3/4"},
199 {LARGE_ICON, "chrome-search://large-icon/64/http://www.google.com"},
200 {LARGE_ICON, "chrome-search://large-icon/bad-size/3/4"},
201 {FALLBACK_ICON, "chrome-search://fallback-icon/"},
202 {FALLBACK_ICON, "chrome-search://fallback-icon/3"},
203 {FALLBACK_ICON, "chrome-search://fallback-icon/3/4"},
204 {FALLBACK_ICON, "chrome-search://fallback-icon//3/4"},
205 {FALLBACK_ICON, "chrome-search://fallback-icon/,,/3/4"},
206 {FALLBACK_ICON, "chrome-search://fallback-icon/bad-spec/3/4"},
207 {FALLBACK_ICON, "chrome-search://fallback-icon/-1,,,,/3/4"},
208 {FALLBACK_ICON, "chrome-search://fallback-icon/,junk,,,/3/4"},
209 // This case is a bit stringent. Since we always render fallback icons,
210 // search privider can't use this to probe user history. We'll consider
211 // relaxing the check if the need arises.
212 {FALLBACK_ICON, "chrome-search://fallback-icon/,,,,/http://www.google.com"},
213 {THUMB, "chrome-search://thumb"},
214 {THUMB, "chrome-search://thumb/"},
215 {THUMB, "chrome-search://thumb/123"},
216 {THUMB, "chrome-search://thumb/xyz"},
217 {THUMB, "chrome-search://thumb/123/"},
218 {THUMB, "chrome-search://thumb/123/xyz"},
219 {THUMB, "chrome-search://thumb/http://www.google.com"},
221 for (size_t i = 0; i < arraysize(test_cases); ++i) {
222 std::string param_part = "(unwritten)";
223 int view_id = -1;
224 InstantRestrictedID rid = -1;
225 EXPECT_FALSE(ParseIconRestrictedUrl(GURL(test_cases[i].transient_url_str),
226 test_cases[i].type, &param_part, &view_id, &rid))
227 << " for test_cases[" << i << "]";
228 EXPECT_EQ("(unwritten)", param_part);
229 EXPECT_EQ(-1, view_id);
230 EXPECT_EQ(-1, rid);
234 TEST(SearchBoxUtilTest, TranslateIconRestrictedUrlSuccess) {
235 struct {
236 SearchBox::ImageSourceType type;
237 const char* transient_url_str;
238 std::string expected_url_str;
239 } test_cases[] = {
240 {FAVICON, "chrome-search://favicon/137/1",
241 std::string("chrome-search://favicon/") + kUrlString1},
242 // FAVICON is permission: invalid input just yields default endpoint.
243 {FAVICON, "chrome-search://favicon/", "chrome-search://favicon/"},
244 {FAVICON, "chrome-search://favicon/314", "chrome-search://favicon/"},
245 {FAVICON, "chrome-search://favicon/314/1", "chrome-search://favicon/"},
246 {FAVICON, "chrome-search://favicon/137/255", "chrome-search://favicon/"},
247 {FAVICON, "chrome-search://favicon/-3/-1", "chrome-search://favicon/"},
248 {FAVICON, "chrome-search://favicon/invalidstuff",
249 "chrome-search://favicon/"},
250 {FAVICON, "chrome-search://favicon/size/16@2x/http://www.google.com",
251 "chrome-search://favicon/"},
252 // Other types of icons.
253 {LARGE_ICON, "chrome-search://large-icon/64/137/2",
254 std::string("chrome-search://large-icon/64/") + kUrlString2},
255 {LARGE_ICON, "chrome-search://large-icon/1/137/1",
256 std::string("chrome-search://large-icon/1/") + kUrlString1},
257 {FALLBACK_ICON, "chrome-search://fallback-icon/,,,,/137/3",
258 std::string("chrome-search://fallback-icon/,,,,/") + kUrlString3},
259 {FALLBACK_ICON, "chrome-search://fallback-icon/64,fff,,,1/137/1",
260 std::string("chrome-search://fallback-icon/64,fff,,,1/") + kUrlString1},
261 {THUMB, "chrome-search://thumb/137/3",
262 std::string("chrome-search://thumb/") + kUrlString3},
265 MockIconURLHelper helper;
266 for (size_t i = 0; i < arraysize(test_cases); ++i) {
267 GURL url;
268 GURL transient_url(test_cases[i].transient_url_str);
269 EXPECT_TRUE(TranslateIconRestrictedUrl(transient_url, test_cases[i].type,
270 helper, &url))
271 << " for test_cases[" << i << "]";
272 EXPECT_EQ(GURL(test_cases[i].expected_url_str), url)
273 << " for test_cases[" << i << "]";
277 // For Non-FAVICON only.
278 TEST(SearchBoxUtilTest, TranslateIconRestrictedUrlFailure) {
279 struct {
280 SearchBox::ImageSourceType type;
281 const char* transient_url_str;
282 } test_cases[] = {
283 // Empty.
284 {LARGE_ICON, "chrome-search://large-icon/"},
285 {FALLBACK_ICON, "chrome-search://fallback-icon/"},
286 {THUMB, "chrome-search://thumb/"},
287 // Bad view_id.
288 {LARGE_ICON, "chrome-search://large-icon/64/314/2"},
289 {LARGE_ICON, "chrome-search://large-icon/1/314/1"},
290 {FALLBACK_ICON, "chrome-search://fallback-icon/,,,,/314/3"},
291 {FALLBACK_ICON, "chrome-search://fallback-icon/64,fff,,,1/314/1"},
292 {THUMB, "chrome-search://thumb/314/1"},
293 // Missing rid.
294 {LARGE_ICON, "chrome-search://large-icon/64/137/"},
295 {LARGE_ICON, "chrome-search://large-icon/64/137/blah"},
296 {FALLBACK_ICON, "chrome-search://fallback-icon/,,,,/137/"},
297 {THUMB, "chrome-search://thumb/314/"},
298 // Bad params.
299 {LARGE_ICON, "chrome-search://large-icon/137/2"},
300 {LARGE_ICON, "chrome-search://large-icon//137/2"},
301 {LARGE_ICON, "chrome-search://large-icon/96"},
302 {LARGE_ICON, "chrome-search://large-icon/-1/137/2"},
303 {LARGE_ICON, "chrome-search://large-icon/blah/137/2"},
304 {FALLBACK_ICON, "chrome-search://fallback-icon/137/3"},
305 {FALLBACK_ICON, "chrome-search://fallback-icon//137/3"},
306 {FALLBACK_ICON, "chrome-search://fallback-icon/-64,fff,,,1/137/3"},
307 {FALLBACK_ICON, "chrome-search://fallback-icon/springfront/137/3"},
308 // Use Page URL.
309 {LARGE_ICON, "chrome-search://large-icon/96/http://www.google.com"},
310 {FALLBACK_ICON, "chrome-search://fallback-icon/,,,,/http://www.google.com"},
311 {THUMB, "chrome-search://thumb/http://www.google.com"},
314 MockIconURLHelper helper;
315 for (size_t i = 0; i < arraysize(test_cases); ++i) {
316 GURL url;
317 GURL transient_url(test_cases[i].transient_url_str);
318 EXPECT_FALSE(TranslateIconRestrictedUrl(transient_url, test_cases[i].type,
319 helper, &url))
320 << " for test_cases[" << i << "]";
321 EXPECT_TRUE(url.is_empty()) << " for test_cases[" << i << "]";
325 } // namespace internal