| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/base_paths.h" | 5 #include "base/base_paths.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/metrics/proto/omnibox_event.pb.h" | 10 #include "components/metrics/proto/omnibox_event.pb.h" |
| (...skipping 1295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1306 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); | 1306 data.alternate_urls.push_back("http://google.com/alt/#q={searchTerms}"); |
| 1307 data.alternate_urls.push_back( | 1307 data.alternate_urls.push_back( |
| 1308 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); | 1308 "http://google.com/alt/?ext=foo&q={searchTerms}#ref=bar"); |
| 1309 TemplateURL url(data); | 1309 TemplateURL url(data); |
| 1310 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); | 1310 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); |
| 1311 GURL result; | 1311 GURL result; |
| 1312 | 1312 |
| 1313 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1313 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1314 GURL("http://google.com/?q=something"), search_terms, | 1314 GURL("http://google.com/?q=something"), search_terms, |
| 1315 search_terms_data_, &result)); | 1315 search_terms_data_, &result)); |
| 1316 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane"), result); | 1316 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane"), result); |
| 1317 | 1317 |
| 1318 result = GURL("http://should.not.change.com"); | 1318 result = GURL("http://should.not.change.com"); |
| 1319 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1319 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1320 GURL("http://google.ca/?q=something"), search_terms, | 1320 GURL("http://google.ca/?q=something"), search_terms, |
| 1321 search_terms_data_, &result)); | 1321 search_terms_data_, &result)); |
| 1322 EXPECT_EQ(GURL("http://should.not.change.com"), result); | 1322 EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| 1323 | 1323 |
| 1324 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1324 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1325 GURL("http://google.com/foo/?q=foo"), search_terms, | 1325 GURL("http://google.com/foo/?q=foo"), search_terms, |
| 1326 search_terms_data_, &result)); | 1326 search_terms_data_, &result)); |
| 1327 | 1327 |
| 1328 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1328 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1329 GURL("https://google.com/?q=foo"), search_terms, | 1329 GURL("https://google.com/?q=foo"), search_terms, |
| 1330 search_terms_data_, &result)); | 1330 search_terms_data_, &result)); |
| 1331 EXPECT_EQ(GURL("https://google.com/?q=Bob%20Morane"), result); | 1331 EXPECT_EQ(GURL("https://google.com/?q=Bob+Morane"), result); |
| 1332 | 1332 |
| 1333 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1333 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1334 GURL("http://google.com:8080/?q=foo"), search_terms, | 1334 GURL("http://google.com:8080/?q=foo"), search_terms, |
| 1335 search_terms_data_, &result)); | 1335 search_terms_data_, &result)); |
| 1336 | 1336 |
| 1337 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1337 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1338 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, | 1338 GURL("http://google.com/?q=1+2+3&b=456"), search_terms, |
| 1339 search_terms_data_, &result)); | 1339 search_terms_data_, &result)); |
| 1340 EXPECT_EQ(GURL("http://google.com/?q=Bob%20Morane&b=456"), result); | 1340 EXPECT_EQ(GURL("http://google.com/?q=Bob+Morane&b=456"), result); |
| 1341 | 1341 |
| 1342 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in | 1342 // Note: Spaces in REF parameters are not escaped. See TryEncoding() in |
| 1343 // template_url.cc for details. | 1343 // template_url.cc for details. |
| 1344 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1344 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1345 GURL("http://google.com/alt/?q=123#q=456"), search_terms, | 1345 GURL("http://google.com/alt/?q=123#q=456"), search_terms, |
| 1346 search_terms_data_, &result)); | 1346 search_terms_data_, &result)); |
| 1347 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob Morane"), result); | 1347 EXPECT_EQ(GURL("http://google.com/alt/?q=123#q=Bob+Morane"), result); |
| 1348 | 1348 |
| 1349 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1349 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1350 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms, | 1350 GURL("http://google.com/alt/?a=012&q=123&b=456#f=789"), search_terms, |
| 1351 search_terms_data_, &result)); | 1351 search_terms_data_, &result)); |
| 1352 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob%20Morane&b=456#f=789"), | 1352 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=Bob+Morane&b=456#f=789"), |
| 1353 result); | 1353 result); |
| 1354 | 1354 |
| 1355 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1355 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1356 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), | 1356 GURL("http://google.com/alt/?a=012&q=123&b=456#j=abc&q=789&h=def9"), |
| 1357 search_terms, search_terms_data_, &result)); | 1357 search_terms, search_terms_data_, &result)); |
| 1358 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456" | 1358 EXPECT_EQ(GURL("http://google.com/alt/?a=012&q=123&b=456" |
| 1359 "#j=abc&q=Bob Morane&h=def9"), result); | 1359 "#j=abc&q=Bob+Morane&h=def9"), result); |
| 1360 | 1360 |
| 1361 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1361 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1362 GURL("http://google.com/alt/?q="), search_terms, | 1362 GURL("http://google.com/alt/?q="), search_terms, |
| 1363 search_terms_data_, &result)); | 1363 search_terms_data_, &result)); |
| 1364 | 1364 |
| 1365 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1365 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1366 GURL("http://google.com/alt/?#q="), search_terms, | 1366 GURL("http://google.com/alt/?#q="), search_terms, |
| 1367 search_terms_data_, &result)); | 1367 search_terms_data_, &result)); |
| 1368 | 1368 |
| 1369 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1369 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1370 GURL("http://google.com/alt/?q=#q="), search_terms, | 1370 GURL("http://google.com/alt/?q=#q="), search_terms, |
| 1371 search_terms_data_, &result)); | 1371 search_terms_data_, &result)); |
| 1372 | 1372 |
| 1373 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1373 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1374 GURL("http://google.com/alt/?q=123#q="), search_terms, | 1374 GURL("http://google.com/alt/?q=123#q="), search_terms, |
| 1375 search_terms_data_, &result)); | 1375 search_terms_data_, &result)); |
| 1376 | 1376 |
| 1377 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1377 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1378 GURL("http://google.com/alt/?q=#q=123"), search_terms, | 1378 GURL("http://google.com/alt/?q=#q=123"), search_terms, |
| 1379 search_terms_data_, &result)); | 1379 search_terms_data_, &result)); |
| 1380 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob Morane"), result); | 1380 EXPECT_EQ(GURL("http://google.com/alt/?q=#q=Bob+Morane"), result); |
| 1381 } | 1381 } |
| 1382 | 1382 |
| 1383 TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) { | 1383 TEST_F(TemplateURLTest, ReplaceSearchTermsInURLPath) { |
| 1384 TemplateURLData data; | 1384 TemplateURLData data; |
| 1385 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end"); | 1385 data.SetURL("http://term-in-path.com/begin/{searchTerms}/end"); |
| 1386 TemplateURL url(data); | 1386 TemplateURL url(data); |
| 1387 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); | 1387 TemplateURLRef::SearchTermsArgs search_terms(ASCIIToUTF16("Bob Morane")); |
| 1388 GURL result; | 1388 GURL result; |
| 1389 | 1389 |
| 1390 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1390 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1391 GURL("http://term-in-path.com/begin/something/end"), search_terms, | 1391 GURL("http://term-in-path.com/begin/something/end"), search_terms, |
| 1392 search_terms_data_, &result)); | 1392 search_terms_data_, &result)); |
| 1393 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); | 1393 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); |
| 1394 | 1394 |
| 1395 EXPECT_TRUE(url.ReplaceSearchTermsInURL( | 1395 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1396 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms, | 1396 GURL("http://term-in-path.com/begin/1%202%203/end"), search_terms, |
| 1397 search_terms_data_, &result)); | 1397 search_terms_data_, &result)); |
| 1398 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); | 1398 EXPECT_EQ(GURL("http://term-in-path.com/begin/Bob%20Morane/end"), result); |
| 1399 | 1399 |
| 1400 result = GURL("http://should.not.change.com"); | 1400 result = GURL("http://should.not.change.com"); |
| 1401 EXPECT_FALSE(url.ReplaceSearchTermsInURL( | 1401 EXPECT_FALSE(url.ReplaceSearchTermsInURL( |
| 1402 GURL("http://term-in-path.com/about"), search_terms, | 1402 GURL("http://term-in-path.com/about"), search_terms, |
| 1403 search_terms_data_, &result)); | 1403 search_terms_data_, &result)); |
| 1404 EXPECT_EQ(GURL("http://should.not.change.com"), result); | 1404 EXPECT_EQ(GURL("http://should.not.change.com"), result); |
| 1405 } | 1405 } |
| 1406 | 1406 |
| 1407 // Checks that the ReplaceSearchTermsInURL function works correctly |
| 1408 // for search terms containing non-latin characters for a search engine |
| 1409 // using UTF-8 input encoding. |
| 1410 TEST_F(TemplateURLTest, ReplaceSearchTermsInUTF8URL) { |
| 1411 TemplateURLData data; |
| 1412 data.SetURL("http://utf-8.ru/?q={searchTerms}"); |
| 1413 data.alternate_urls.push_back("http://utf-8.ru/#q={searchTerms}"); |
| 1414 data.alternate_urls.push_back("http://utf-8.ru/path/{searchTerms}"); |
| 1415 TemplateURL url(data); |
| 1416 |
| 1417 // Russian text which will be encoded with UTF-8. |
| 1418 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16( |
| 1419 L"\x0442\x0435\x043A\x0441\x0442")); |
| 1420 GURL result; |
| 1421 |
| 1422 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1423 GURL("http://utf-8.ru/?q=a+b"), search_terms, search_terms_data_, |
| 1424 &result)); |
| 1425 EXPECT_EQ(GURL("http://utf-8.ru/?q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| 1426 result); |
| 1427 |
| 1428 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1429 GURL("http://utf-8.ru/#q=a+b"), search_terms, search_terms_data_, |
| 1430 &result)); |
| 1431 EXPECT_EQ(GURL("http://utf-8.ru/#q=%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| 1432 result); |
| 1433 |
| 1434 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1435 GURL("http://utf-8.ru/path/a%20b"), search_terms, search_terms_data_, |
| 1436 &result)); |
| 1437 EXPECT_EQ(GURL("http://utf-8.ru/path/%D1%82%D0%B5%D0%BA%D1%81%D1%82"), |
| 1438 result); |
| 1439 } |
| 1440 |
| 1441 // Checks that the ReplaceSearchTermsInURL function works correctly |
| 1442 // for search terms containing non-latin characters for a search engine |
| 1443 // using non UTF-8 input encoding. |
| 1444 TEST_F(TemplateURLTest, ReplaceSearchTermsInNonUTF8URL) { |
| 1445 TemplateURLData data; |
| 1446 data.SetURL("http://windows-1251.ru/?q={searchTerms}"); |
| 1447 data.alternate_urls.push_back("http://windows-1251.ru/#q={searchTerms}"); |
| 1448 data.alternate_urls.push_back("http://windows-1251.ru/path/{searchTerms}"); |
| 1449 data.input_encodings.push_back("windows-1251"); |
| 1450 TemplateURL url(data); |
| 1451 |
| 1452 // Russian text which will be encoded with Windows-1251. |
| 1453 TemplateURLRef::SearchTermsArgs search_terms(base::WideToUTF16( |
| 1454 L"\x0442\x0435\x043A\x0441\x0442")); |
| 1455 GURL result; |
| 1456 |
| 1457 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1458 GURL("http://windows-1251.ru/?q=a+b"), search_terms, search_terms_data_, |
| 1459 &result)); |
| 1460 EXPECT_EQ(GURL("http://windows-1251.ru/?q=%F2%E5%EA%F1%F2"), |
| 1461 result); |
| 1462 |
| 1463 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1464 GURL("http://windows-1251.ru/#q=a+b"), search_terms, search_terms_data_, |
| 1465 &result)); |
| 1466 EXPECT_EQ(GURL("http://windows-1251.ru/#q=%F2%E5%EA%F1%F2"), |
| 1467 result); |
| 1468 |
| 1469 EXPECT_TRUE(url.ReplaceSearchTermsInURL( |
| 1470 GURL("http://windows-1251.ru/path/a%20b"), search_terms, |
| 1471 search_terms_data_, &result)); |
| 1472 EXPECT_EQ(GURL("http://windows-1251.ru/path/%F2%E5%EA%F1%F2"), |
| 1473 result); |
| 1474 } |
| 1475 |
| 1407 // Test the |suggest_query_params| field of SearchTermsArgs. | 1476 // Test the |suggest_query_params| field of SearchTermsArgs. |
| 1408 TEST_F(TemplateURLTest, SuggestQueryParams) { | 1477 TEST_F(TemplateURLTest, SuggestQueryParams) { |
| 1409 TemplateURLData data; | 1478 TemplateURLData data; |
| 1410 // Pick a URL with replacements before, during, and after the query, to ensure | 1479 // Pick a URL with replacements before, during, and after the query, to ensure |
| 1411 // we don't goof up any of them. | 1480 // we don't goof up any of them. |
| 1412 data.SetURL("{google:baseURL}search?q={searchTerms}" | 1481 data.SetURL("{google:baseURL}search?q={searchTerms}" |
| 1413 "#{google:originalQueryForSuggestion}x"); | 1482 "#{google:originalQueryForSuggestion}x"); |
| 1414 TemplateURL url(data); | 1483 TemplateURL url(data); |
| 1415 | 1484 |
| 1416 // Baseline: no |suggest_query_params| field. | 1485 // Baseline: no |suggest_query_params| field. |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1675 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t", | 1744 EXPECT_EQ("http://bar/search?q=foo&pfq=full%20query%20text&qha=2338&xssi=t", |
| 1676 result); | 1745 result); |
| 1677 | 1746 |
| 1678 TemplateURL url2(data); | 1747 TemplateURL url2(data); |
| 1679 search_terms_args.prefetch_query.clear(); | 1748 search_terms_args.prefetch_query.clear(); |
| 1680 search_terms_args.prefetch_query_type.clear(); | 1749 search_terms_args.prefetch_query_type.clear(); |
| 1681 result = | 1750 result = |
| 1682 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); | 1751 url2.url_ref().ReplaceSearchTerms(search_terms_args, search_terms_data_); |
| 1683 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); | 1752 EXPECT_EQ("http://bar/search?q=foo&xssi=t", result); |
| 1684 } | 1753 } |
| OLD | NEW |