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"
10 #include "base/basictypes.h"
11 #include "chrome/common/instant_types.h"
12 #include "testing/gtest/include/gtest/gtest.h"
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
{
30 ~MockIconURLHelper() override
;
31 int GetViewID() const override
;
32 std::string
GetURLStringFromRestrictedID(InstantRestrictedID rid
) const
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 {
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
;
62 // Defined in searchbox.cc
63 bool ParseViewIdAndRestrictedId(const std::string id_part
,
65 InstantRestrictedID
* rid_out
);
67 // Defined in searchbox.cc
68 bool ParseIconRestrictedUrl(const GURL
& url
,
69 SearchBox::ImageSourceType type
,
70 std::string
* param_part
,
72 InstantRestrictedID
* rid
);
74 // Defined in searchbox.cc
75 bool TranslateIconRestrictedUrl(const GURL
& transient_url
,
76 SearchBox::ImageSourceType type
,
77 const SearchBox::IconURLHelper
& helper
,
80 TEST(SearchBoxUtilTest
, ParseViewIdAndRestrictedIdSuccess
) {
82 InstantRestrictedID rid
= -1;
84 EXPECT_TRUE(ParseViewIdAndRestrictedId("2/3", &view_id
, &rid
));
85 EXPECT_EQ(2, view_id
);
88 EXPECT_TRUE(ParseViewIdAndRestrictedId("0/0", &view_id
, &rid
));
89 EXPECT_EQ(0, view_id
);
92 EXPECT_TRUE(ParseViewIdAndRestrictedId("1048576/314", &view_id
, &rid
));
93 EXPECT_EQ(1048576, view_id
);
97 EXPECT_TRUE(ParseViewIdAndRestrictedId("00/09", &view_id
, &rid
));
98 EXPECT_EQ(0, view_id
);
101 // Tolerates multiple, leading, and trailing "/".
102 EXPECT_TRUE(ParseViewIdAndRestrictedId("2////3", &view_id
, &rid
));
103 EXPECT_EQ(2, view_id
);
106 EXPECT_TRUE(ParseViewIdAndRestrictedId("5/6/", &view_id
, &rid
));
107 EXPECT_EQ(5, view_id
);
110 EXPECT_TRUE(ParseViewIdAndRestrictedId("/7/8", &view_id
, &rid
));
111 EXPECT_EQ(7, view_id
);
115 TEST(SearchBoxUtilTest
, ParseViewIdAndRestrictedIdFailure
) {
116 const char* test_cases
[] = {
135 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
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
);
145 TEST(SearchBoxUtilTest
, ParseIconRestrictedUrlFaviconSuccess
) {
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
;
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)";
171 InstantRestrictedID rid
= -1;
172 EXPECT_TRUE(ParseIconRestrictedUrl(GURL(test_cases
[i
].transient_url_str
),
173 test_cases
[i
].type
, ¶m_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
) {
186 SearchBox::ImageSourceType type
;
187 const char* transient_url_str
;
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)";
224 InstantRestrictedID rid
= -1;
225 EXPECT_FALSE(ParseIconRestrictedUrl(GURL(test_cases
[i
].transient_url_str
),
226 test_cases
[i
].type
, ¶m_part
, &view_id
, &rid
))
227 << " for test_cases[" << i
<< "]";
228 EXPECT_EQ("(unwritten)", param_part
);
229 EXPECT_EQ(-1, view_id
);
234 TEST(SearchBoxUtilTest
, TranslateIconRestrictedUrlSuccess
) {
236 SearchBox::ImageSourceType type
;
237 const char* transient_url_str
;
238 std::string expected_url_str
;
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
) {
268 GURL
transient_url(test_cases
[i
].transient_url_str
);
269 EXPECT_TRUE(TranslateIconRestrictedUrl(transient_url
, test_cases
[i
].type
,
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
) {
280 SearchBox::ImageSourceType type
;
281 const char* transient_url_str
;
284 {LARGE_ICON
, "chrome-search://large-icon/"},
285 {FALLBACK_ICON
, "chrome-search://fallback-icon/"},
286 {THUMB
, "chrome-search://thumb/"},
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"},
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/"},
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"},
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
) {
317 GURL
transient_url(test_cases
[i
].transient_url_str
);
318 EXPECT_FALSE(TranslateIconRestrictedUrl(transient_url
, test_cases
[i
].type
,
320 << " for test_cases[" << i
<< "]";
321 EXPECT_TRUE(url
.is_empty()) << " for test_cases[" << i
<< "]";
325 } // namespace internal