Chromium Code Reviews| 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 "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 | 61 |
| 62 static void SetUpTestCase(); | 62 static void SetUpTestCase(); |
| 63 | 63 |
| 64 static void TearDownTestCase(); | 64 static void TearDownTestCase(); |
| 65 | 65 |
| 66 // See description above class for what this registers. | 66 // See description above class for what this registers. |
| 67 virtual void SetUp(); | 67 virtual void SetUp(); |
| 68 | 68 |
| 69 virtual void TearDown(); | 69 virtual void TearDown(); |
| 70 | 70 |
| 71 struct ResultInfo { | |
| 72 ResultInfo(): | |
|
msw
2013/02/05 01:35:19
nit: either combine this line and the line below w
Mark P
2013/02/05 19:50:55
Done.
| |
| 73 result_type(AutocompleteMatch::NUM_TYPES) { | |
| 74 } | |
| 75 ResultInfo(GURL gurl, | |
| 76 AutocompleteMatch::Type result_type, | |
| 77 string16 fill_into_edit): | |
| 78 gurl(gurl), | |
| 79 result_type(result_type), | |
| 80 fill_into_edit(fill_into_edit) { | |
| 81 } | |
| 82 const GURL gurl; | |
| 83 const AutocompleteMatch::Type result_type; | |
| 84 const string16 fill_into_edit; | |
| 85 }; | |
| 86 struct TestData { | |
| 87 const string16 input; | |
| 88 const size_t num_results; | |
| 89 const ResultInfo output[3]; | |
| 90 }; | |
| 91 | |
| 92 void RunTest(TestData* cases, int num_cases, bool prefer_keyword); | |
| 93 | |
| 71 protected: | 94 protected: |
| 72 // Adds a search for |term|, using the engine |t_url| to the history, and | 95 // Adds a search for |term|, using the engine |t_url| to the history, and |
| 73 // returns the URL for that search. | 96 // returns the URL for that search. |
| 74 GURL AddSearchToHistory(TemplateURL* t_url, string16 term, int visit_count); | 97 GURL AddSearchToHistory(TemplateURL* t_url, string16 term, int visit_count); |
| 75 | 98 |
| 76 // Looks for a match in |provider_| with |contents| equal to |contents|. | 99 // Looks for a match in |provider_| with |contents| equal to |contents|. |
| 77 // Sets |match| to it if found. Returns whether |match| was set. | 100 // Sets |match| to it if found. Returns whether |match| was set. |
| 78 bool FindMatchWithContents(const string16& contents, | 101 bool FindMatchWithContents(const string16& contents, |
| 79 AutocompleteMatch* match); | 102 AutocompleteMatch* match); |
| 80 | 103 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 wyt_match)); | 286 wyt_match)); |
| 264 } | 287 } |
| 265 | 288 |
| 266 void SearchProviderTest::TearDown() { | 289 void SearchProviderTest::TearDown() { |
| 267 message_loop_.RunUntilIdle(); | 290 message_loop_.RunUntilIdle(); |
| 268 | 291 |
| 269 // Shutdown the provider before the profile. | 292 // Shutdown the provider before the profile. |
| 270 provider_ = NULL; | 293 provider_ = NULL; |
| 271 } | 294 } |
| 272 | 295 |
| 296 void SearchProviderTest::RunTest(TestData* cases, | |
| 297 int num_cases, | |
| 298 bool prefer_keyword) { | |
| 299 ACMatches matches; | |
| 300 for (int i = 0; i < num_cases; ++i) { | |
| 301 AutocompleteInput input(cases[i].input, string16::npos, string16(), | |
| 302 false, prefer_keyword, true, | |
| 303 AutocompleteInput::ALL_MATCHES); | |
| 304 provider_->Start(input, false); | |
| 305 matches = provider_->matches(); | |
| 306 string16 diagnostic_details = ASCIIToUTF16("Input was: ") + cases[i].input + | |
| 307 ASCIIToUTF16("; prefer_keyword was: ") + | |
| 308 (prefer_keyword ? ASCIIToUTF16("true") : ASCIIToUTF16("false")); | |
| 309 EXPECT_EQ(cases[i].num_results, matches.size()) << diagnostic_details; | |
| 310 if (matches.size() == cases[i].num_results) { | |
| 311 for (size_t j = 0; j < cases[i].num_results; ++j) { | |
| 312 EXPECT_EQ(cases[i].output[j].gurl, matches[j].destination_url) << | |
| 313 diagnostic_details; | |
| 314 EXPECT_EQ(cases[i].output[j].result_type, matches[j].type) << | |
| 315 diagnostic_details; | |
| 316 EXPECT_EQ(cases[i].output[j].fill_into_edit, | |
| 317 matches[j].fill_into_edit) << | |
| 318 diagnostic_details; | |
| 319 } | |
| 320 } | |
| 321 } | |
| 322 } | |
| 323 | |
| 273 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, | 324 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, |
| 274 string16 term, | 325 string16 term, |
| 275 int visit_count) { | 326 int visit_count) { |
| 276 HistoryService* history = | 327 HistoryService* history = |
| 277 HistoryServiceFactory::GetForProfile(&profile_, | 328 HistoryServiceFactory::GetForProfile(&profile_, |
| 278 Profile::EXPLICIT_ACCESS); | 329 Profile::EXPLICIT_ACCESS); |
| 279 GURL search(t_url->url_ref().ReplaceSearchTerms( | 330 GURL search(t_url->url_ref().ReplaceSearchTerms( |
| 280 TemplateURLRef::SearchTermsArgs(term))); | 331 TemplateURLRef::SearchTermsArgs(term))); |
| 281 static base::Time last_added_time; | 332 static base::Time last_added_time; |
| 282 last_added_time = std::max(base::Time::Now(), | 333 last_added_time = std::max(base::Time::Now(), |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 780 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("f"), | 831 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("f"), |
| 781 &wyt_match)); | 832 &wyt_match)); |
| 782 ASSERT_EQ(2u, provider_->matches().size()); | 833 ASSERT_EQ(2u, provider_->matches().size()); |
| 783 AutocompleteMatch term_match; | 834 AutocompleteMatch term_match; |
| 784 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match)); | 835 EXPECT_TRUE(FindMatchWithDestination(term_url, &term_match)); |
| 785 EXPECT_GT(term_match.relevance, wyt_match.relevance); | 836 EXPECT_GT(term_match.relevance, wyt_match.relevance); |
| 786 EXPECT_EQ(1u, term_match.inline_autocomplete_offset); | 837 EXPECT_EQ(1u, term_match.inline_autocomplete_offset); |
| 787 EXPECT_EQ(ASCIIToUTF16("FOO"), term_match.fill_into_edit); | 838 EXPECT_EQ(ASCIIToUTF16("FOO"), term_match.fill_into_edit); |
| 788 } | 839 } |
| 789 | 840 |
| 790 // Verifies AutocompleteControllers sets descriptions for results correctly. | 841 // Verifies AutocompleteControllers return results (including keyword |
| 791 TEST_F(SearchProviderTest, UpdateKeywordDescriptions) { | 842 // results) in the right order and set descriptions for them correctly. |
| 843 TEST_F(SearchProviderTest, KeywordOrderingAndDescriptions) { | |
| 792 // Add an entry that corresponds to a keyword search with 'term2'. | 844 // Add an entry that corresponds to a keyword search with 'term2'. |
| 793 AddSearchToHistory(keyword_t_url_, ASCIIToUTF16("term2"), 1); | 845 AddSearchToHistory(keyword_t_url_, ASCIIToUTF16("term2"), 1); |
| 794 profile_.BlockUntilHistoryProcessesPendingRequests(); | 846 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 795 | 847 |
| 796 AutocompleteController controller(&profile_, NULL, | 848 AutocompleteController controller(&profile_, NULL, |
| 797 AutocompleteProvider::TYPE_SEARCH); | 849 AutocompleteProvider::TYPE_SEARCH); |
| 798 controller.Start(AutocompleteInput( | 850 controller.Start(AutocompleteInput( |
| 799 ASCIIToUTF16("k t"), string16::npos, string16(), false, false, true, | 851 ASCIIToUTF16("k t"), string16::npos, string16(), false, false, true, |
| 800 AutocompleteInput::ALL_MATCHES)); | 852 AutocompleteInput::ALL_MATCHES)); |
| 801 const AutocompleteResult& result = controller.result(); | 853 const AutocompleteResult& result = controller.result(); |
| 802 | 854 |
| 803 // There should be two matches, one for the keyword one for what you typed. | 855 // There should be three matches, one for the keyword history, one for |
| 804 ASSERT_EQ(2u, result.size()); | 856 // keyword provider's what-you-typed, and one for the default provider's |
| 857 // what you typed, in that order. | |
| 858 ASSERT_EQ(3u, result.size()); | |
| 859 EXPECT_EQ(AutocompleteMatch::SEARCH_HISTORY, result.match_at(0).type); | |
| 860 EXPECT_EQ(AutocompleteMatch::SEARCH_OTHER_ENGINE, result.match_at(1).type); | |
| 861 EXPECT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, result.match_at(2).type); | |
| 862 EXPECT_GT(result.match_at(0).relevance, result.match_at(1).relevance); | |
| 863 EXPECT_GT(result.match_at(1).relevance, result.match_at(2).relevance); | |
| 805 | 864 |
| 806 EXPECT_FALSE(result.match_at(0).keyword.empty()); | 865 // The two keyword results should come with the keyword we expect. |
| 807 EXPECT_FALSE(result.match_at(1).keyword.empty()); | 866 EXPECT_EQ(ASCIIToUTF16("k"), result.match_at(0).keyword); |
| 808 EXPECT_NE(result.match_at(0).keyword, result.match_at(1).keyword); | 867 EXPECT_EQ(ASCIIToUTF16("k"), result.match_at(1).keyword); |
| 868 // The default provider has a different keyword. (We don't explicitly | |
| 869 // set it during this test, so all we do is assert that it's different.) | |
| 870 EXPECT_NE(result.match_at(0).keyword, result.match_at(2).keyword); | |
| 809 | 871 |
| 872 // The top result will always have a description. The third result, | |
| 873 // coming from a different provider than the first two, should also. | |
| 874 // Whether the second result has one doesn't matter much. (If it was | |
| 875 // missing, people would infer that it's the same search provider as | |
| 876 // the one above it.) | |
| 810 EXPECT_FALSE(result.match_at(0).description.empty()); | 877 EXPECT_FALSE(result.match_at(0).description.empty()); |
| 811 EXPECT_FALSE(result.match_at(1).description.empty()); | 878 EXPECT_FALSE(result.match_at(2).description.empty()); |
| 812 EXPECT_NE(result.match_at(0).description, result.match_at(1).description); | 879 EXPECT_NE(result.match_at(0).description, result.match_at(2).description); |
| 880 } | |
| 881 | |
| 882 TEST_F(SearchProviderTest, KeywordVerbatim) { | |
| 883 TestData cases[] = { | |
| 884 // Test a simple keyword input. | |
| 885 { ASCIIToUTF16("k foo"), 2, | |
| 886 { ResultInfo(GURL("http://keyword/foo"), | |
| 887 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 888 ASCIIToUTF16("k foo")), | |
| 889 ResultInfo(GURL("http://defaultturl/k%20foo"), | |
| 890 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 891 ASCIIToUTF16("k foo") ) } }, | |
| 892 | |
| 893 // Make sure extra whitespace after the keyword doesn't change the | |
| 894 // keyword verbatim query. | |
| 895 { ASCIIToUTF16("k foo"), 2, | |
| 896 { ResultInfo(GURL("http://keyword/foo"), | |
| 897 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 898 ASCIIToUTF16("k foo")), | |
| 899 ResultInfo(GURL("http://defaultturl/k%20%20%20foo"), | |
| 900 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 901 ASCIIToUTF16("k foo")) } }, | |
| 902 // Leading whitespace should be stripped before SearchProvider gets the | |
| 903 // input; hence there are no tests here about how it handles those inputs. | |
| 904 | |
| 905 // But whitespace elsewhere in the query string should matter to both | |
| 906 // matches. | |
| 907 { ASCIIToUTF16("k foo bar"), 2, | |
| 908 { ResultInfo(GURL("http://keyword/foo%20%20bar"), | |
| 909 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 910 ASCIIToUTF16("k foo bar")), | |
| 911 ResultInfo(GURL("http://defaultturl/k%20%20foo%20%20bar"), | |
| 912 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 913 ASCIIToUTF16("k foo bar")) } }, | |
| 914 // Note in the above test case we don't test trailing whitespace because | |
| 915 // SearchProvider still doesn't handle this well. See related bugs: | |
| 916 // 102690, 99239, 164635. | |
| 917 | |
| 918 // Keywords can be prefixed by certain things that should get ignored | |
| 919 // when constructing the keyword match. | |
| 920 { ASCIIToUTF16("www.k foo"), 2, | |
| 921 { ResultInfo(GURL("http://keyword/foo"), | |
| 922 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 923 ASCIIToUTF16("k foo")), | |
| 924 ResultInfo(GURL("http://defaultturl/www.k%20foo"), | |
| 925 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 926 ASCIIToUTF16("www.k foo")) } }, | |
| 927 { ASCIIToUTF16("http://k foo"), 2, | |
| 928 { ResultInfo(GURL("http://keyword/foo"), | |
| 929 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 930 ASCIIToUTF16("k foo")), | |
| 931 ResultInfo(GURL("http://defaultturl/http%3A//k%20foo"), | |
| 932 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 933 ASCIIToUTF16("http://k foo")) } }, | |
| 934 { ASCIIToUTF16("http://www.k foo"), 2, | |
| 935 { ResultInfo(GURL("http://keyword/foo"), | |
| 936 AutocompleteMatch::SEARCH_OTHER_ENGINE, | |
| 937 ASCIIToUTF16("k foo")), | |
| 938 ResultInfo(GURL("http://defaultturl/http%3A//www.k%20foo"), | |
| 939 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 940 ASCIIToUTF16("http://www.k foo")) } }, | |
| 941 | |
| 942 // A keyword with no remaining input shouldn't get a keyword | |
| 943 // verbatim match. | |
| 944 { ASCIIToUTF16("k"), 1, | |
| 945 { ResultInfo(GURL("http://defaultturl/k"), | |
| 946 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 947 ASCIIToUTF16("k")) } }, | |
| 948 { ASCIIToUTF16("k "), 1, | |
| 949 { ResultInfo(GURL("http://defaultturl/k%20"), | |
| 950 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, | |
| 951 ASCIIToUTF16("k ")) } } | |
| 952 | |
| 953 // The fact that verbatim queries to keyword are handled by KeywordProvider | |
| 954 // not SearchProvider is tested in | |
| 955 // chrome/browser/extensions/api/omnibox/omnibox_apitest.cc. | |
| 956 }; | |
| 957 | |
| 958 // Test not in keyword mode. | |
| 959 RunTest(cases, arraysize(cases), false); | |
| 960 | |
| 961 // Test in keyword mode. (Both modes should give the same result.) | |
| 962 RunTest(cases, arraysize(cases), true); | |
| 813 } | 963 } |
| 814 | 964 |
| 815 // Verifies Navsuggest results don't set a TemplateURL, which instant relies on. | 965 // Verifies Navsuggest results don't set a TemplateURL, which instant relies on. |
| 816 // Also verifies that just the *first* navigational result is listed as a match | 966 // Also verifies that just the *first* navigational result is listed as a match |
| 817 // if suggested relevance scores were not sent. | 967 // if suggested relevance scores were not sent. |
| 818 TEST_F(SearchProviderTest, NavSuggestNoSuggestedRelevanceScores) { | 968 TEST_F(SearchProviderTest, NavSuggestNoSuggestedRelevanceScores) { |
| 819 QueryForInput(ASCIIToUTF16("a.c"), string16(), false); | 969 QueryForInput(ASCIIToUTF16("a.c"), string16(), false); |
| 820 | 970 |
| 821 // Make sure the default providers suggest service was queried. | 971 // Make sure the default providers suggest service was queried. |
| 822 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 972 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1417 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1567 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
| 1418 match.contents_class[0].style); | 1568 match.contents_class[0].style); |
| 1419 EXPECT_EQ(4U, match.contents_class[1].offset); | 1569 EXPECT_EQ(4U, match.contents_class[1].offset); |
| 1420 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | | 1570 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | |
| 1421 AutocompleteMatch::ACMatchClassification::MATCH, | 1571 AutocompleteMatch::ACMatchClassification::MATCH, |
| 1422 match.contents_class[1].style); | 1572 match.contents_class[1].style); |
| 1423 EXPECT_EQ(5U, match.contents_class[2].offset); | 1573 EXPECT_EQ(5U, match.contents_class[2].offset); |
| 1424 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1574 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
| 1425 match.contents_class[2].style); | 1575 match.contents_class[2].style); |
| 1426 } | 1576 } |
| OLD | NEW |