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