Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/autocomplete/search_provider_unittest.cc

Issue 1855423003: Interpret '?' and Ctrl-K or Ctrl-E as putting omnibox in keyword search mode for Default Search Pro… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed compilation on Mac and removed forced query unit tests Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/omnibox/browser/search_provider.h" 5 #include "components/omnibox/browser/search_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 10
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 const base::string16& text, 212 const base::string16& text,
213 const bool prefer_keyword, 213 const bool prefer_keyword,
214 const std::string& default_fetcher_response, 214 const std::string& default_fetcher_response,
215 const std::string& keyword_fetcher_response); 215 const std::string& keyword_fetcher_response);
216 216
217 // Notifies the URLFetcher for the suggest query corresponding to the default 217 // Notifies the URLFetcher for the suggest query corresponding to the default
218 // search provider that it's done. 218 // search provider that it's done.
219 // Be sure and wrap calls to this in ASSERT_NO_FATAL_FAILURE. 219 // Be sure and wrap calls to this in ASSERT_NO_FATAL_FAILURE.
220 void FinishDefaultSuggestQuery(); 220 void FinishDefaultSuggestQuery();
221 221
222 // Runs SearchProvider on |input|, for which the suggest server replies
223 // with |json|, and expects that the resulting matches' contents equals
224 // that in |matches|. An empty entry in |matches| means no match should
225 // be returned in that position. Reports any errors with a message that
226 // includes |error_description|.
227 void ForcedQueryTestHelper(const std::string& input,
228 const std::string& json,
229 const std::string matches[3],
230 const std::string& error_description);
231
232 // Verifies that |matches| and |expected_matches| agree on the first 222 // Verifies that |matches| and |expected_matches| agree on the first
233 // |num_expected_matches|, displaying an error message that includes 223 // |num_expected_matches|, displaying an error message that includes
234 // |description| for any disagreement. 224 // |description| for any disagreement.
235 void CheckMatches(const std::string& description, 225 void CheckMatches(const std::string& description,
236 const size_t num_expected_matches, 226 const size_t num_expected_matches,
237 const ExpectedMatch expected_matches[], 227 const ExpectedMatch expected_matches[],
238 const ACMatches& matches); 228 const ACMatches& matches);
239 229
240 void ResetFieldTrialList(); 230 void ResetFieldTrialList();
241 231
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 net::TestURLFetcher* default_fetcher = 475 net::TestURLFetcher* default_fetcher =
486 test_factory_.GetFetcherByID( 476 test_factory_.GetFetcherByID(
487 SearchProvider::kDefaultProviderURLFetcherID); 477 SearchProvider::kDefaultProviderURLFetcherID);
488 ASSERT_TRUE(default_fetcher); 478 ASSERT_TRUE(default_fetcher);
489 479
490 // Tell the SearchProvider the default suggest query is done. 480 // Tell the SearchProvider the default suggest query is done.
491 default_fetcher->set_response_code(200); 481 default_fetcher->set_response_code(200);
492 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); 482 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher);
493 } 483 }
494 484
495 void SearchProviderTest::ForcedQueryTestHelper(
496 const std::string& input,
497 const std::string& json,
498 const std::string expected_matches[3],
499 const std::string& error_description) {
500 // Send the query twice in order to have a synchronous pass after the first
501 // response is received. This is necessary because SearchProvider doesn't
502 // allow an asynchronous response to change the default match.
503 for (size_t i = 0; i < 2; ++i) {
504 QueryForInputAndWaitForFetcherResponses(
505 ASCIIToUTF16(input), false, json, std::string());
506 }
507
508 const ACMatches& matches = provider_->matches();
509 ASSERT_LE(matches.size(), 3u);
510 size_t i = 0;
511 // Ensure that the returned matches equal the expectations.
512 for (; i < matches.size(); ++i) {
513 EXPECT_EQ(ASCIIToUTF16(expected_matches[i]), matches[i].contents) <<
514 error_description;
515 }
516 // Ensure that no expected matches are missing.
517 for (; i < 3u; ++i) {
518 EXPECT_EQ(std::string(), expected_matches[i]) <<
519 "Case #" << i << ": " << error_description;
520 }
521 }
522
523 void SearchProviderTest::CheckMatches(const std::string& description, 485 void SearchProviderTest::CheckMatches(const std::string& description,
524 const size_t num_expected_matches, 486 const size_t num_expected_matches,
525 const ExpectedMatch expected_matches[], 487 const ExpectedMatch expected_matches[],
526 const ACMatches& matches) { 488 const ACMatches& matches) {
527 ASSERT_FALSE(matches.empty()); 489 ASSERT_FALSE(matches.empty());
528 ASSERT_LE(matches.size(), num_expected_matches); 490 ASSERT_LE(matches.size(), num_expected_matches);
529 size_t i = 0; 491 size_t i = 0;
530 SCOPED_TRACE(description); 492 SCOPED_TRACE(description);
531 // Ensure that the returned matches equal the expectations. 493 // Ensure that the returned matches equal the expectations.
532 for (; i < matches.size(); ++i) { 494 for (; i < matches.size(); ++i) {
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 // 'docs.google.com'. The search term should have a lower priority than the 762 // 'docs.google.com'. The search term should have a lower priority than the
801 // what you typed match. 763 // what you typed match.
802 ASSERT_EQ(2u, provider_->matches().size()); 764 ASSERT_EQ(2u, provider_->matches().size());
803 AutocompleteMatch term_match; 765 AutocompleteMatch term_match;
804 EXPECT_TRUE(FindMatchWithDestination(url, &term_match)); 766 EXPECT_TRUE(FindMatchWithDestination(url, &term_match));
805 EXPECT_GT(wyt_match.relevance, term_match.relevance); 767 EXPECT_GT(wyt_match.relevance, term_match.relevance);
806 EXPECT_TRUE(wyt_match.allowed_to_be_default_match); 768 EXPECT_TRUE(wyt_match.allowed_to_be_default_match);
807 EXPECT_TRUE(term_match.allowed_to_be_default_match); 769 EXPECT_TRUE(term_match.allowed_to_be_default_match);
808 } 770 }
809 771
810 TEST_F(SearchProviderTest, DontGiveNavsuggestionsInForcedQueryMode) {
811 const std::string kEmptyMatch;
812 struct {
813 const std::string json;
814 const std::string matches_in_default_mode[3];
815 const std::string matches_in_forced_query_mode[3];
816 } cases[] = {
817 // Without suggested relevance scores.
818 { "[\"a\",[\"http://a1.com\", \"a2\"],[],[],"
819 "{\"google:suggesttype\":[\"NAVIGATION\", \"QUERY\"]}]",
820 { "a", "a1.com", "a2" },
821 { "a", "a2", kEmptyMatch } },
822
823 // With suggested relevance scores in a situation where navsuggest would
824 // go second.
825 { "[\"a\",[\"http://a1.com\", \"a2\"],[],[],"
826 "{\"google:suggesttype\":[\"NAVIGATION\", \"QUERY\"],"
827 "\"google:suggestrelevance\":[1250, 1200]}]",
828 { "a", "a1.com", "a2" },
829 { "a", "a2", kEmptyMatch } },
830
831 // With suggested relevance scores in a situation where navsuggest
832 // would go first.
833 { "[\"a\",[\"http://a1.com\", \"a2\"],[],[],"
834 "{\"google:suggesttype\":[\"NAVIGATION\", \"QUERY\"],"
835 "\"google:suggestrelevance\":[1350, 1250]}]",
836 { "a1.com", "a", "a2" },
837 { "a", "a2", kEmptyMatch } },
838
839 // With suggested relevance scores in a situation where navsuggest
840 // would go first only because verbatim has been demoted.
841 { "[\"a\",[\"http://a1.com\", \"a2\"],[],[],"
842 "{\"google:suggesttype\":[\"NAVIGATION\", \"QUERY\"],"
843 "\"google:suggestrelevance\":[1450, 1400],"
844 "\"google:verbatimrelevance\":1350}]",
845 { "a1.com", "a2", "a" },
846 { "a2", "a", kEmptyMatch } },
847 };
848
849 for (size_t i = 0; i < arraysize(cases); ++i) {
850 ForcedQueryTestHelper("a", cases[i].json, cases[i].matches_in_default_mode,
851 "regular input with json=" + cases[i].json);
852 ForcedQueryTestHelper("?a", cases[i].json,
853 cases[i].matches_in_forced_query_mode,
854 "forced query input with json=" + cases[i].json);
855 }
856 }
857
858 // A multiword search with one visit should not autocomplete until multiple 772 // A multiword search with one visit should not autocomplete until multiple
859 // words are typed. 773 // words are typed.
860 TEST_F(SearchProviderTest, DontAutocompleteUntilMultipleWordsTyped) { 774 TEST_F(SearchProviderTest, DontAutocompleteUntilMultipleWordsTyped) {
861 GURL term_url(AddSearchToHistory(default_t_url_, ASCIIToUTF16("one search"), 775 GURL term_url(AddSearchToHistory(default_t_url_, ASCIIToUTF16("one search"),
862 1)); 776 1));
863 profile_.BlockUntilHistoryProcessesPendingRequests(); 777 profile_.BlockUntilHistoryProcessesPendingRequests();
864 778
865 AutocompleteMatch wyt_match; 779 AutocompleteMatch wyt_match;
866 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("on"), 780 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("on"),
867 &wyt_match)); 781 &wyt_match));
(...skipping 1857 matching lines...) Expand 10 before | Expand all | Expand 10 after
2725 { "www.ab", "https://www.abc.com/path/file.htm?q=x#foo", 2639 { "www.ab", "https://www.abc.com/path/file.htm?q=x#foo",
2726 "https://www.abc.com/path/file.htm?q=x#foo", 2640 "https://www.abc.com/path/file.htm?q=x#foo",
2727 "c.com/path/file.htm?q=x#foo", true, false }, 2641 "c.com/path/file.htm?q=x#foo", true, false },
2728 { "ab", "https://www.abc.com/path/file.htm?q=x#foo", 2642 { "ab", "https://www.abc.com/path/file.htm?q=x#foo",
2729 "https://www.abc.com/path/file.htm?q=x#foo", 2643 "https://www.abc.com/path/file.htm?q=x#foo",
2730 "c.com/path/file.htm?q=x#foo", true, false }, 2644 "c.com/path/file.htm?q=x#foo", true, false },
2731 { "ab", "https://abc.com/path/file.htm?q=x#foo", 2645 { "ab", "https://abc.com/path/file.htm?q=x#foo",
2732 "https://abc.com/path/file.htm?q=x#foo", 2646 "https://abc.com/path/file.htm?q=x#foo",
2733 "c.com/path/file.htm?q=x#foo", true, false }, 2647 "c.com/path/file.htm?q=x#foo", true, false },
2734 2648
2735 // Forced query input should inline and retain the "?" prefix.
2736 { "?http://www.ab", "http://www.abc.com",
2737 "?http://www.abc.com", "c.com", true, false },
2738 { "?www.ab", "http://www.abc.com",
2739 "?www.abc.com", "c.com", true, false },
2740 { "?ab", "http://www.abc.com",
2741 "?www.abc.com", "c.com", true, false },
2742 { "?abc.com", "http://www.abc.com",
2743 "?www.abc.com", std::string(), true, true },
2744 }; 2649 };
2745 2650
2746 for (size_t i = 0; i < arraysize(cases); ++i) { 2651 for (size_t i = 0; i < arraysize(cases); ++i) {
2747 // First test regular mode. 2652 // First test regular mode.
2748 QueryForInput(ASCIIToUTF16(cases[i].input), false, false); 2653 QueryForInput(ASCIIToUTF16(cases[i].input), false, false);
2749 SearchSuggestionParser::NavigationResult result( 2654 SearchSuggestionParser::NavigationResult result(
2750 ChromeAutocompleteSchemeClassifier(&profile_), GURL(cases[i].url), 2655 ChromeAutocompleteSchemeClassifier(&profile_), GURL(cases[i].url),
2751 AutocompleteMatchType::NAVSUGGEST, base::string16(), std::string(), 2656 AutocompleteMatchType::NAVSUGGEST, base::string16(), std::string(),
2752 false, 0, false, ASCIIToUTF16(cases[i].input), std::string()); 2657 false, 0, false, ASCIIToUTF16(cases[i].input), std::string());
2753 result.set_received_after_last_keystroke(false); 2658 result.set_received_after_last_keystroke(false);
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after
3598 } 3503 }
3599 3504
3600 TEST_F(SearchProviderTest, DoesNotProvideOnFocus) { 3505 TEST_F(SearchProviderTest, DoesNotProvideOnFocus) {
3601 AutocompleteInput input( 3506 AutocompleteInput input(
3602 base::ASCIIToUTF16("f"), base::string16::npos, std::string(), GURL(), 3507 base::ASCIIToUTF16("f"), base::string16::npos, std::string(), GURL(),
3603 metrics::OmniboxEventProto::INVALID_SPEC, false, true, true, true, true, 3508 metrics::OmniboxEventProto::INVALID_SPEC, false, true, true, true, true,
3604 ChromeAutocompleteSchemeClassifier(&profile_)); 3509 ChromeAutocompleteSchemeClassifier(&profile_));
3605 provider_->Start(input, false); 3510 provider_->Start(input, false);
3606 EXPECT_TRUE(provider_->matches().empty()); 3511 EXPECT_TRUE(provider_->matches().empty());
3607 } 3512 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698