| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a"), &verbatim)); | 709 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a"), &verbatim)); |
| 710 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a1"), &match_a1)); | 710 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a1"), &match_a1)); |
| 711 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a2"), &match_a2)); | 711 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a2"), &match_a2)); |
| 712 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a3"), &match_a3)); | 712 EXPECT_TRUE(FindMatchWithContents(ASCIIToUTF16("a3"), &match_a3)); |
| 713 EXPECT_FALSE(FindMatchWithContents(ASCIIToUTF16("a4"), &match_a4)); | 713 EXPECT_FALSE(FindMatchWithContents(ASCIIToUTF16("a4"), &match_a4)); |
| 714 EXPECT_GT(verbatim.relevance, match_a1.relevance); | 714 EXPECT_GT(verbatim.relevance, match_a1.relevance); |
| 715 EXPECT_GT(match_a1.relevance, match_a2.relevance); | 715 EXPECT_GT(match_a1.relevance, match_a2.relevance); |
| 716 EXPECT_GT(match_a2.relevance, match_a3.relevance); | 716 EXPECT_GT(match_a2.relevance, match_a3.relevance); |
| 717 } | 717 } |
| 718 | 718 |
| 719 // Verifies that suggest experiment results are added properly. |
| 720 TEST_F(SearchProviderTest, SuggestRelevanceExperiment) { |
| 721 const std::string kNotApplicable("Not Applicable"); |
| 722 struct { |
| 723 const std::string input; |
| 724 const std::string json; |
| 725 const std::string matches[4]; |
| 726 } cases[] = { |
| 727 // Ensure that suggestrelevance scores reorder matches. |
| 728 { "a", "[\"a\",[\"b\", \"c\"],[],[],{\"google:suggestrelevance\":[1, 2]}]", |
| 729 { "a", "c", "b", kNotApplicable } }, |
| 730 { "a", "[\"a\",[\"http://b.com\", \"http://c.com\"],[],[]," |
| 731 "{\"google:suggesttype\":[\"NAVIGATION\", \"NAVIGATION\"]," |
| 732 "\"google:suggestrelevance\":[1, 2]}]", |
| 733 { "a", "c.com", kNotApplicable, kNotApplicable } }, |
| 734 |
| 735 // Ensure that verbatimrelevance scores reorder or suppress what-you-typed. |
| 736 // Negative values will have no effect; the calculated value will be used. |
| 737 { "a", "[\"a\",[\"a1\"],[],[],{\"google:verbatimrelevance\":1}]", |
| 738 { "a1", "a", kNotApplicable, kNotApplicable } }, |
| 739 { "a", "[\"a\",[\"a1\"],[],[],{\"google:verbatimrelevance\":0}]", |
| 740 { "a1", kNotApplicable, kNotApplicable, kNotApplicable } }, |
| 741 { "a", "[\"a\",[\"a1\"],[],[],{\"google:verbatimrelevance\":-1}]", |
| 742 { "a", "a1", kNotApplicable, kNotApplicable } }, |
| 743 { "a", "[\"a\",[\"http://a.com\"],[],[]," |
| 744 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 745 "\"google:verbatimrelevance\":1}]", |
| 746 { "a.com", "a", kNotApplicable, kNotApplicable } }, |
| 747 { "a", "[\"a\",[\"http://a.com\"],[],[]," |
| 748 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 749 "\"google:verbatimrelevance\":0}]", |
| 750 { "a.com", kNotApplicable, kNotApplicable, kNotApplicable } }, |
| 751 { "a", "[\"a\",[\"http://a.com\"],[],[]," |
| 752 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 753 "\"google:verbatimrelevance\":-1}]", |
| 754 { "a", "a.com", kNotApplicable, kNotApplicable } }, |
| 755 |
| 756 // Ensure that both types of relevance scores reorder matches together. |
| 757 { "a", "[\"a\",[\"a1\", \"a2\"],[],[],{\"google:suggestrelevance\":[3, 1]," |
| 758 "\"google:verbatimrelevance\":2}]", |
| 759 { "a1", "a", "a2", kNotApplicable } }, |
| 760 |
| 761 // Ensure that only inlinable matches may be ranked as the highest result. |
| 762 // If the server suggests relevance scores that violate this constraint, |
| 763 // then all other matches are ranked lower than the what-you-typed result. |
| 764 { "a", "[\"a\",[\"b\"],[],[],{\"google:verbatimrelevance\":0}]", |
| 765 { "a", "b", kNotApplicable, kNotApplicable } }, |
| 766 { "a", "[\"a\",[\"b\"],[],[],{\"google:suggestrelevance\":[9999]}]", |
| 767 { "a", "b", kNotApplicable, kNotApplicable } }, |
| 768 { "a", "[\"a\",[\"b\"],[],[],{\"google:suggestrelevance\":[9999]," |
| 769 "\"google:verbatimrelevance\":0}]", |
| 770 { "a", "b", kNotApplicable, kNotApplicable } }, |
| 771 { "a", "[\"a\",[\"http://b.com\"],[],[]," |
| 772 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 773 "\"google:verbatimrelevance\":0}]", |
| 774 { "a", "b.com", kNotApplicable, kNotApplicable } }, |
| 775 { "a", "[\"a\",[\"http://b.com\"],[],[]," |
| 776 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 777 "\"google:suggestrelevance\":[9999]}]", |
| 778 { "a", "b.com", kNotApplicable, kNotApplicable } }, |
| 779 { "a", "[\"a\",[\"http://b.com\"],[],[]," |
| 780 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 781 "\"google:suggestrelevance\":[9999]," |
| 782 "\"google:verbatimrelevance\":0}]", |
| 783 { "a", "b.com", kNotApplicable, kNotApplicable } }, |
| 784 |
| 785 // Ensure that all suggestions are considered, regardless of order. |
| 786 { "a", "[\"a\",[\"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"],[],[]," |
| 787 "{\"google:suggestrelevance\":[1, 2, 3, 4, 5, 6, 7]}]", |
| 788 { "a", "h", "g", "f" } }, |
| 789 { "a", "[\"a\",[\"http://b.com\", \"http://c.com\", \"http://d.com\"," |
| 790 "\"http://e.com\", \"http://f.com\", \"http://g.com\"," |
| 791 "\"http://h.com\"],[],[]," |
| 792 "{\"google:suggesttype\":[\"NAVIGATION\", \"NAVIGATION\"," |
| 793 "\"NAVIGATION\", \"NAVIGATION\"," |
| 794 "\"NAVIGATION\", \"NAVIGATION\"," |
| 795 "\"NAVIGATION\"]," |
| 796 "\"google:suggestrelevance\":[1, 2, 3, 4, 5, 6, 7]}]", |
| 797 { "a", "h.com", kNotApplicable, kNotApplicable } }, |
| 798 |
| 799 // Ensure that incorrectly sized suggestion relevance lists are ignored. |
| 800 { "a", "[\"a\",[\"a1\", \"a2\"],[],[],{\"google:suggestrelevance\":[1]}]", |
| 801 { "a", "a1", "a2", kNotApplicable } }, |
| 802 { "a", "[\"a\",[\"a1\"],[],[],{\"google:suggestrelevance\":[9999, 1]}]", |
| 803 { "a", "a1", kNotApplicable, kNotApplicable } }, |
| 804 { "a", "[\"a\",[\"http://a1.com\", \"http://a2.com\"],[],[]," |
| 805 "{\"google:suggesttype\":[\"NAVIGATION\", \"NAVIGATION\"]," |
| 806 "\"google:suggestrelevance\":[1]}]", |
| 807 { "a", "a1.com", kNotApplicable, kNotApplicable } }, |
| 808 { "a", "[\"a\",[\"http://a1.com\"],[],[]," |
| 809 "{\"google:suggesttype\":[\"NAVIGATION\"]," |
| 810 "\"google:suggestrelevance\":[9999, 1]}]", |
| 811 { "a", "a1.com", kNotApplicable, kNotApplicable } }, |
| 812 }; |
| 813 |
| 814 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 815 QueryForInput(ASCIIToUTF16(cases[i].input), false); |
| 816 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 817 SearchProvider::kDefaultProviderURLFetcherID); |
| 818 fetcher->set_response_code(200); |
| 819 fetcher->SetResponseString(cases[i].json); |
| 820 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 821 RunTillProviderDone(); |
| 822 |
| 823 const ACMatches& matches = provider_->matches(); |
| 824 EXPECT_NE(string16::npos, matches[0].inline_autocomplete_offset); |
| 825 |
| 826 size_t j = 0; |
| 827 // Ensure that the supplied matches equal the expectations. |
| 828 for (; j < matches.size(); ++j) |
| 829 EXPECT_EQ(ASCIIToUTF16(cases[i].matches[j]), matches[j].contents); |
| 830 // Ensure that no expected matches are missing. |
| 831 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) |
| 832 EXPECT_EQ(kNotApplicable, cases[i].matches[j]); |
| 833 } |
| 834 } |
| 835 |
| 719 // Verifies inline autocompletion of navigational results. | 836 // Verifies inline autocompletion of navigational results. |
| 720 TEST_F(SearchProviderTest, NavigationInline) { | 837 TEST_F(SearchProviderTest, NavigationInline) { |
| 721 struct { | 838 struct { |
| 722 const std::string input; | 839 const std::string input; |
| 723 const std::string url; | 840 const std::string url; |
| 724 // Test the expected fill_into_edit, which may drop "http://". | 841 // Test the expected fill_into_edit, which may drop "http://". |
| 725 // Some cases will not trim "http://" because "www." is not a valid prefix. | 842 // Some cases do not trim "http://" to match from the start of the scheme. |
| 726 const std::string fill_into_edit; | 843 const std::string fill_into_edit; |
| 727 size_t inline_offset; | 844 size_t inline_offset; |
| 728 } cases[] = { | 845 } cases[] = { |
| 729 // Do not inline matches that do not contain the input; trim http as needed. | 846 // Do not inline matches that do not contain the input; trim http as needed. |
| 730 { "x", "http://www.abc.com", | 847 { "x", "http://www.abc.com", |
| 731 "www.abc.com", string16::npos }, | 848 "www.abc.com", string16::npos }, |
| 732 { "https:", "http://www.abc.com", | 849 { "https:", "http://www.abc.com", |
| 733 "www.abc.com", string16::npos }, | 850 "www.abc.com", string16::npos }, |
| 734 { "abc.com/", "http://www.abc.com", | 851 { "abc.com/", "http://www.abc.com", |
| 735 "www.abc.com", string16::npos }, | 852 "www.abc.com", string16::npos }, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1010 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
| 894 match.contents_class[0].style); | 1011 match.contents_class[0].style); |
| 895 EXPECT_EQ(4U, match.contents_class[1].offset); | 1012 EXPECT_EQ(4U, match.contents_class[1].offset); |
| 896 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | | 1013 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | |
| 897 AutocompleteMatch::ACMatchClassification::MATCH, | 1014 AutocompleteMatch::ACMatchClassification::MATCH, |
| 898 match.contents_class[1].style); | 1015 match.contents_class[1].style); |
| 899 EXPECT_EQ(5U, match.contents_class[2].offset); | 1016 EXPECT_EQ(5U, match.contents_class[2].offset); |
| 900 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1017 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
| 901 match.contents_class[2].style); | 1018 match.contents_class[2].style); |
| 902 } | 1019 } |
| OLD | NEW |