OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "chrome/browser/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
(...skipping 3365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3376 const std::string& suggestion = cases[i].results[j].suggestion; | 3376 const std::string& suggestion = cases[i].results[j].suggestion; |
3377 if (suggestion == kNotApplicable) | 3377 if (suggestion == kNotApplicable) |
3378 break; | 3378 break; |
3379 if (cases[i].results[j].is_navigation_result) { | 3379 if (cases[i].results[j].is_navigation_result) { |
3380 provider_->default_results_.navigation_results.push_back( | 3380 provider_->default_results_.navigation_results.push_back( |
3381 SearchProvider::NavigationResult( | 3381 SearchProvider::NavigationResult( |
3382 *provider_.get(), GURL(suggestion), base::string16(), false, | 3382 *provider_.get(), GURL(suggestion), base::string16(), false, |
3383 cases[i].results[j].relevance, false)); | 3383 cases[i].results[j].relevance, false)); |
3384 } else { | 3384 } else { |
3385 provider_->default_results_.suggest_results.push_back( | 3385 provider_->default_results_.suggest_results.push_back( |
3386 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), base::string
16(), | 3386 SearchProvider::SuggestResult( |
3387 base::string16(), std::string(), | 3387 ASCIIToUTF16(suggestion), AutocompleteMatchType::SEARCH_SUGGEST, |
3388 std::string(), false, | 3388 base::string16(), base::string16(), std::string(), |
3389 cases[i].results[j].relevance, | 3389 std::string(), false, cases[i].results[j].relevance, false, |
3390 false, false)); | 3390 false)); |
3391 } | 3391 } |
3392 } | 3392 } |
3393 | 3393 |
3394 provider_->input_ = AutocompleteInput( | 3394 provider_->input_ = AutocompleteInput( |
3395 ASCIIToUTF16(cases[i].omnibox_input), base::string16::npos, base::string
16(), | 3395 ASCIIToUTF16(cases[i].omnibox_input), base::string16::npos, base::string
16(), |
3396 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, | 3396 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, |
3397 AutocompleteInput::ALL_MATCHES); | 3397 AutocompleteInput::ALL_MATCHES); |
3398 provider_->RemoveAllStaleResults(); | 3398 provider_->RemoveAllStaleResults(); |
3399 | 3399 |
3400 // Check cached results. | 3400 // Check cached results. |
(...skipping 24 matching lines...) Expand all Loading... |
3425 EXPECT_EQ(sug_end, sug_it); | 3425 EXPECT_EQ(sug_end, sug_it); |
3426 EXPECT_EQ(nav_end, nav_it); | 3426 EXPECT_EQ(nav_end, nav_it); |
3427 } | 3427 } |
3428 } | 3428 } |
3429 | 3429 |
3430 #if !defined(OS_WIN) | 3430 #if !defined(OS_WIN) |
3431 // Verify entity suggestion parsing. | 3431 // Verify entity suggestion parsing. |
3432 TEST_F(SearchProviderTest, ParseEntitySuggestion) { | 3432 TEST_F(SearchProviderTest, ParseEntitySuggestion) { |
3433 struct Match { | 3433 struct Match { |
3434 std::string contents; | 3434 std::string contents; |
| 3435 std::string description; |
3435 std::string query_params; | 3436 std::string query_params; |
3436 std::string fill_into_edit; | 3437 std::string fill_into_edit; |
3437 AutocompleteMatchType::Type type; | 3438 AutocompleteMatchType::Type type; |
3438 size_t classification_offsets[3]; | |
3439 int classification_styles[3]; | |
3440 }; | 3439 }; |
3441 const size_t invalid_offset = 10; | |
3442 const int invalid_style = -1; | |
3443 const Match kEmptyMatch = { | 3440 const Match kEmptyMatch = { |
3444 kNotApplicable, kNotApplicable, kNotApplicable, | 3441 kNotApplicable, kNotApplicable, kNotApplicable, kNotApplicable, |
3445 AutocompleteMatchType::NUM_TYPES, | 3442 AutocompleteMatchType::NUM_TYPES}; |
3446 { invalid_offset, invalid_offset, invalid_offset }, | |
3447 { invalid_style, invalid_style, invalid_style } }; | |
3448 | 3443 |
3449 struct { | 3444 struct { |
3450 const std::string input_text; | 3445 const std::string input_text; |
3451 const std::string response_json; | 3446 const std::string response_json; |
3452 const Match matches[5]; | 3447 const Match matches[5]; |
3453 } cases[] = { | 3448 } cases[] = { |
3454 // A query and an entity suggestion with different search terms. | 3449 // A query and an entity suggestion with different search terms. |
3455 { "x", | 3450 { "x", |
3456 "[\"x\",[\"xy\", \"xy\"],[\"\",\"\"],[]," | 3451 "[\"x\",[\"xy\", \"yy\"],[\"\",\"\"],[]," |
3457 " {\"google:suggestdetail\":[{}," | 3452 " {\"google:suggestdetail\":[{}," |
3458 " {\"a\":\"A\",\"dq\":\"yy\",\"q\":\"p=v\"}]," | 3453 " {\"a\":\"A\",\"t\":\"xy\",\"q\":\"p=v\"}]," |
3459 "\"google:suggesttype\":[\"QUERY\",\"ENTITY\"]}]", | 3454 "\"google:suggesttype\":[\"QUERY\",\"ENTITY\"]}]", |
3460 { { "x", "", "x", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3455 { { "x", "", "", "x", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
3461 { 0, invalid_offset, invalid_offset }, | 3456 { "xy", "", "", "xy", AutocompleteMatchType::SEARCH_SUGGEST }, |
3462 { ACMatchClassification::NONE, invalid_style, invalid_style } }, | 3457 { "xy", "A", "p=v", "yy", |
3463 { "xy", "", "xy", AutocompleteMatchType::SEARCH_SUGGEST, | 3458 AutocompleteMatchType::SEARCH_SUGGEST_ENTITY }, |
3464 { 0, 1, invalid_offset }, | |
3465 { ACMatchClassification::NONE, ACMatchClassification::MATCH, | |
3466 invalid_style } }, | |
3467 { "xy - A", "p=v", "yy", AutocompleteMatchType::SEARCH_SUGGEST, | |
3468 { 0, 1, 2 }, | |
3469 { ACMatchClassification::NONE, ACMatchClassification::MATCH, | |
3470 ACMatchClassification::DIM } }, | |
3471 kEmptyMatch, | 3459 kEmptyMatch, |
3472 kEmptyMatch | 3460 kEmptyMatch |
3473 }, | 3461 }, |
3474 }, | 3462 }, |
3475 // A query and an entity suggestion with same search terms. | 3463 // A query and an entity suggestion with same search terms. |
3476 { "x", | 3464 { "x", |
3477 "[\"x\",[\"xy\", \"xy\"],[\"\",\"\"],[]," | 3465 "[\"x\",[\"xy\", \"xy\"],[\"\",\"\"],[]," |
3478 " {\"google:suggestdetail\":[{}," | 3466 " {\"google:suggestdetail\":[{}," |
3479 " {\"a\":\"A\",\"dq\":\"xy\",\"q\":\"p=v\"}]," | 3467 " {\"a\":\"A\",\"t\":\"xy\",\"q\":\"p=v\"}]," |
3480 "\"google:suggesttype\":[\"QUERY\",\"ENTITY\"]}]", | 3468 "\"google:suggesttype\":[\"QUERY\",\"ENTITY\"]}]", |
3481 { { "x", "", "x", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3469 { { "x", "", "", "x", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
3482 { 0, invalid_offset, invalid_offset }, | 3470 { "xy", "", "", "xy", AutocompleteMatchType::SEARCH_SUGGEST }, |
3483 { ACMatchClassification::NONE, invalid_style, invalid_style } }, | 3471 { "xy", "A", "p=v", "xy", |
3484 { "xy", "", "xy", AutocompleteMatchType::SEARCH_SUGGEST, | 3472 AutocompleteMatchType::SEARCH_SUGGEST_ENTITY }, |
3485 { 0, 1, invalid_offset }, | |
3486 { ACMatchClassification::NONE, ACMatchClassification::MATCH, | |
3487 invalid_style } }, | |
3488 { "xy - A", "p=v", "xy", AutocompleteMatchType::SEARCH_SUGGEST, | |
3489 { 0, 1, 2 }, | |
3490 { ACMatchClassification::NONE, ACMatchClassification::MATCH, | |
3491 ACMatchClassification::DIM } }, | |
3492 kEmptyMatch, | 3473 kEmptyMatch, |
3493 kEmptyMatch | 3474 kEmptyMatch |
3494 }, | 3475 }, |
3495 }, | 3476 }, |
3496 }; | 3477 }; |
3497 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | 3478 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
3498 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); | 3479 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); |
3499 | 3480 |
3500 // Set up a default fetcher with provided results. | 3481 // Set up a default fetcher with provided results. |
3501 net::TestURLFetcher* fetcher = | 3482 net::TestURLFetcher* fetcher = |
(...skipping 12 matching lines...) Expand all Loading... |
3514 SCOPED_TRACE("for input with json = " + cases[i].response_json); | 3495 SCOPED_TRACE("for input with json = " + cases[i].response_json); |
3515 | 3496 |
3516 ASSERT_LE(matches.size(), ARRAYSIZE_UNSAFE(cases[i].matches)); | 3497 ASSERT_LE(matches.size(), ARRAYSIZE_UNSAFE(cases[i].matches)); |
3517 size_t j = 0; | 3498 size_t j = 0; |
3518 // Ensure that the returned matches equal the expectations. | 3499 // Ensure that the returned matches equal the expectations. |
3519 for (; j < matches.size(); ++j) { | 3500 for (; j < matches.size(); ++j) { |
3520 const Match& match = cases[i].matches[j]; | 3501 const Match& match = cases[i].matches[j]; |
3521 SCOPED_TRACE(" and match index: " + base::IntToString(j)); | 3502 SCOPED_TRACE(" and match index: " + base::IntToString(j)); |
3522 EXPECT_EQ(match.contents, | 3503 EXPECT_EQ(match.contents, |
3523 UTF16ToUTF8(matches[j].contents)); | 3504 UTF16ToUTF8(matches[j].contents)); |
| 3505 EXPECT_EQ(match.description, |
| 3506 UTF16ToUTF8(matches[j].description)); |
3524 EXPECT_EQ(match.query_params, | 3507 EXPECT_EQ(match.query_params, |
3525 matches[j].search_terms_args->suggest_query_params); | 3508 matches[j].search_terms_args->suggest_query_params); |
3526 EXPECT_EQ(match.fill_into_edit, | 3509 EXPECT_EQ(match.fill_into_edit, |
3527 UTF16ToUTF8(matches[j].fill_into_edit)); | 3510 UTF16ToUTF8(matches[j].fill_into_edit)); |
3528 EXPECT_EQ(match.type, matches[j].type); | 3511 EXPECT_EQ(match.type, matches[j].type); |
3529 | |
3530 size_t k = 0; | |
3531 for (; k < matches[j].contents_class.size(); k++) { | |
3532 SCOPED_TRACE(" and contents class: " + base::IntToString(k)); | |
3533 EXPECT_EQ(match.classification_offsets[k], | |
3534 matches[j].contents_class[k].offset); | |
3535 EXPECT_EQ(match.classification_styles[k], | |
3536 matches[j].contents_class[k].style); | |
3537 } | |
3538 for (; k < ARRAYSIZE_UNSAFE(match.classification_offsets); k++) { | |
3539 SCOPED_TRACE(" and contents class: " + base::IntToString(k)); | |
3540 EXPECT_EQ(match.classification_offsets[k], invalid_offset); | |
3541 EXPECT_EQ(match.classification_styles[k], invalid_style); | |
3542 } | |
3543 } | 3512 } |
3544 // Ensure that no expected matches are missing. | 3513 // Ensure that no expected matches are missing. |
3545 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { | 3514 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { |
3546 SCOPED_TRACE(" and match index: " + base::IntToString(j)); | 3515 SCOPED_TRACE(" and match index: " + base::IntToString(j)); |
3547 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); | 3516 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); |
| 3517 EXPECT_EQ(cases[i].matches[j].description, kNotApplicable); |
3548 EXPECT_EQ(cases[i].matches[j].query_params, kNotApplicable); | 3518 EXPECT_EQ(cases[i].matches[j].query_params, kNotApplicable); |
3549 EXPECT_EQ(cases[i].matches[j].fill_into_edit, kNotApplicable); | 3519 EXPECT_EQ(cases[i].matches[j].fill_into_edit, kNotApplicable); |
3550 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); | 3520 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); |
3551 } | 3521 } |
3552 } | 3522 } |
3553 } | 3523 } |
3554 #endif // !defined(OS_WIN) | 3524 #endif // !defined(OS_WIN) |
3555 | 3525 |
3556 TEST_F(SearchProviderTest, SearchHistorySuppressesEntitySuggestion) { | |
3557 struct Match { | |
3558 std::string contents; | |
3559 std::string query_params; | |
3560 std::string fill_into_edit; | |
3561 AutocompleteMatchType::Type type; | |
3562 }; | |
3563 const Match kEmptyMatch = { kNotApplicable, kNotApplicable, kNotApplicable, | |
3564 AutocompleteMatchType::NUM_TYPES}; | |
3565 | |
3566 struct { | |
3567 const std::string input_text; | |
3568 const std::string history_search_term; | |
3569 const std::string response_json; | |
3570 const Match matches[5]; | |
3571 } cases[] = { | |
3572 // Search history suppresses both query and entity suggestions. | |
3573 { "x", "xy", | |
3574 "[\"x\",[\"xy\", \"xy\"],[\"\",\"\"],[]," | |
3575 " {\"google:suggestdetail\":[{}," | |
3576 " {\"a\":\"A\",\"dq\":\"xy\",\"q\":\"p=v\"}]," | |
3577 "\"google:suggesttype\":[\"QUERY\",\"ENTITY\"]}]", | |
3578 { | |
3579 {"xy", "", "xy", AutocompleteMatchType::SEARCH_HISTORY}, | |
3580 {"x", "", "x", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED}, | |
3581 {"xy - A", "p=v", "xy", AutocompleteMatchType::SEARCH_SUGGEST}, | |
3582 kEmptyMatch, | |
3583 kEmptyMatch, | |
3584 }, | |
3585 }, | |
3586 // Search history suppresses only query suggestion. | |
3587 { "x", "xyy", | |
3588 "[\"x\",[\"xy\", \"xy\"],[\"\",\"\"],[]," | |
3589 " {\"google:suggestdetail\":[{}," | |
3590 " {\"a\":\"A\",\"dq\":\"xyy\",\"q\":\"p=v\"}]," | |
3591 "\"google:suggesttype\":[\"QUERY\",\"ENTITY\"]}]", | |
3592 { | |
3593 {"xyy", "", "xyy", AutocompleteMatchType::SEARCH_HISTORY}, | |
3594 {"xy", "", "xy", AutocompleteMatchType::SEARCH_HISTORY}, | |
3595 {"x", "", "x", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED}, | |
3596 {"xy - A", "p=v", "xyy", AutocompleteMatchType::SEARCH_SUGGEST}, | |
3597 kEmptyMatch, | |
3598 }, | |
3599 } | |
3600 }; | |
3601 | |
3602 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
3603 GURL term_url(AddSearchToHistory( | |
3604 default_t_url_, ASCIIToUTF16(cases[i].history_search_term), 10)); | |
3605 profile_.BlockUntilHistoryProcessesPendingRequests(); | |
3606 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); | |
3607 | |
3608 // Set up a default fetcher with provided results. | |
3609 net::TestURLFetcher* fetcher = | |
3610 test_factory_.GetFetcherByID( | |
3611 SearchProvider::kDefaultProviderURLFetcherID); | |
3612 ASSERT_TRUE(fetcher); | |
3613 fetcher->set_response_code(200); | |
3614 fetcher->SetResponseString(cases[i].response_json); | |
3615 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
3616 | |
3617 RunTillProviderDone(); | |
3618 | |
3619 const ACMatches& matches = provider_->matches(); | |
3620 ASSERT_FALSE(matches.empty()); | |
3621 SCOPED_TRACE("for case: " + base::IntToString(i)); | |
3622 | |
3623 ASSERT_LE(matches.size(), ARRAYSIZE_UNSAFE(cases[i].matches)); | |
3624 size_t j = 0; | |
3625 // Ensure that the returned matches equal the expectations. | |
3626 for (; j < matches.size(); ++j) { | |
3627 SCOPED_TRACE(" and match index: " + base::IntToString(j)); | |
3628 EXPECT_EQ(cases[i].matches[j].contents, | |
3629 UTF16ToUTF8(matches[j].contents)); | |
3630 EXPECT_EQ(cases[i].matches[j].query_params, | |
3631 matches[j].search_terms_args->suggest_query_params); | |
3632 EXPECT_EQ(cases[i].matches[j].fill_into_edit, | |
3633 UTF16ToUTF8(matches[j].fill_into_edit)); | |
3634 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | |
3635 } | |
3636 // Ensure that no expected matches are missing. | |
3637 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { | |
3638 SCOPED_TRACE(" and match index: " + base::IntToString(j)); | |
3639 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); | |
3640 EXPECT_EQ(cases[i].matches[j].query_params, kNotApplicable); | |
3641 EXPECT_EQ(cases[i].matches[j].fill_into_edit, kNotApplicable); | |
3642 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); | |
3643 } | |
3644 } | |
3645 } | |
3646 | 3526 |
3647 // A basic test that verifies the prefetch metadata parsing logic. | 3527 // A basic test that verifies the prefetch metadata parsing logic. |
3648 TEST_F(SearchProviderTest, PrefetchMetadataParsing) { | 3528 TEST_F(SearchProviderTest, PrefetchMetadataParsing) { |
3649 struct Match { | 3529 struct Match { |
3650 std::string contents; | 3530 std::string contents; |
3651 bool allowed_to_be_prefetched; | 3531 bool allowed_to_be_prefetched; |
3652 AutocompleteMatchType::Type type; | 3532 AutocompleteMatchType::Type type; |
3653 bool from_keyword; | 3533 bool from_keyword; |
3654 }; | 3534 }; |
3655 const Match kEmptyMatch = { kNotApplicable, | 3535 const Match kEmptyMatch = { kNotApplicable, |
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4143 EXPECT_FALSE(provider_->deletion_handlers_.empty()); | 4023 EXPECT_FALSE(provider_->deletion_handlers_.empty()); |
4144 // Set up a default fetcher with provided results. | 4024 // Set up a default fetcher with provided results. |
4145 fetcher = test_factory_.GetFetcherByID( | 4025 fetcher = test_factory_.GetFetcherByID( |
4146 SearchProvider::kDeletionURLFetcherID); | 4026 SearchProvider::kDeletionURLFetcherID); |
4147 ASSERT_TRUE(fetcher); | 4027 ASSERT_TRUE(fetcher); |
4148 fetcher->set_response_code(500); | 4028 fetcher->set_response_code(500); |
4149 fetcher->delegate()->OnURLFetchComplete(fetcher); | 4029 fetcher->delegate()->OnURLFetchComplete(fetcher); |
4150 EXPECT_TRUE(provider_->deletion_handlers_.empty()); | 4030 EXPECT_TRUE(provider_->deletion_handlers_.empty()); |
4151 EXPECT_FALSE(provider_->is_success()); | 4031 EXPECT_FALSE(provider_->is_success()); |
4152 } | 4032 } |
OLD | NEW |