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