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/history_url_provider.h" | 5 #include "chrome/browser/autocomplete/history_url_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "chrome/browser/autocomplete/autocomplete_match.h" | 15 #include "chrome/browser/autocomplete/autocomplete_match.h" |
16 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 16 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 17 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
18 #include "chrome/browser/autocomplete/autocomplete_result.h" | 18 #include "chrome/browser/autocomplete/autocomplete_result.h" |
19 #include "chrome/browser/autocomplete/history_quick_provider.h" | 19 #include "chrome/browser/autocomplete/history_quick_provider.h" |
20 #include "chrome/browser/history/history_service.h" | 20 #include "chrome/browser/history/history_service.h" |
21 #include "chrome/browser/history/history_service_factory.h" | 21 #include "chrome/browser/history/history_service_factory.h" |
22 #include "chrome/browser/search_engines/template_url.h" | 22 #include "chrome/browser/search_engines/template_url.h" |
23 #include "chrome/browser/search_engines/template_url_service.h" | 23 #include "chrome/browser/search_engines/template_url_service.h" |
24 #include "chrome/browser/search_engines/template_url_service_factory.h" | 24 #include "chrome/browser/search_engines/template_url_service_factory.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/test/base/testing_browser_process.h" | 26 #include "chrome/test/base/testing_browser_process.h" |
27 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
| 28 #include "components/metrics/proto/omnibox_event.pb.h" |
| 29 #include "components/metrics/proto/omnibox_input_type.pb.h" |
28 #include "components/url_fixer/url_fixer.h" | 30 #include "components/url_fixer/url_fixer.h" |
29 #include "content/public/test/test_browser_thread_bundle.h" | 31 #include "content/public/test/test_browser_thread_bundle.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
31 | 33 |
32 using base::ASCIIToUTF16; | 34 using base::ASCIIToUTF16; |
33 using base::Time; | 35 using base::Time; |
34 using base::TimeDelta; | 36 using base::TimeDelta; |
35 | 37 |
36 using content::TestBrowserThreadBundle; | 38 using content::TestBrowserThreadBundle; |
37 | 39 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 void FillData(); | 184 void FillData(); |
183 | 185 |
184 // Runs an autocomplete query on |text| and checks to see that the returned | 186 // Runs an autocomplete query on |text| and checks to see that the returned |
185 // results' destination URLs match those provided. Also allows checking | 187 // results' destination URLs match those provided. Also allows checking |
186 // that the input type was identified correctly. | 188 // that the input type was identified correctly. |
187 void RunTest(const base::string16 text, | 189 void RunTest(const base::string16 text, |
188 const base::string16& desired_tld, | 190 const base::string16& desired_tld, |
189 bool prevent_inline_autocomplete, | 191 bool prevent_inline_autocomplete, |
190 const UrlAndLegalDefault* expected_urls, | 192 const UrlAndLegalDefault* expected_urls, |
191 size_t num_results, | 193 size_t num_results, |
192 AutocompleteInput::Type* identified_input_type); | 194 metrics::OmniboxInputType::Type* identified_input_type); |
193 | 195 |
194 // A version of the above without the final |type| output parameter. | 196 // A version of the above without the final |type| output parameter. |
195 void RunTest(const base::string16 text, | 197 void RunTest(const base::string16 text, |
196 const base::string16& desired_tld, | 198 const base::string16& desired_tld, |
197 bool prevent_inline_autocomplete, | 199 bool prevent_inline_autocomplete, |
198 const UrlAndLegalDefault* expected_urls, | 200 const UrlAndLegalDefault* expected_urls, |
199 size_t num_results) { | 201 size_t num_results) { |
200 AutocompleteInput::Type type; | 202 metrics::OmniboxInputType::Type type; |
201 return RunTest(text, desired_tld, prevent_inline_autocomplete, | 203 return RunTest(text, desired_tld, prevent_inline_autocomplete, |
202 expected_urls, num_results, &type); | 204 expected_urls, num_results, &type); |
203 } | 205 } |
204 | 206 |
205 content::TestBrowserThreadBundle thread_bundle_; | 207 content::TestBrowserThreadBundle thread_bundle_; |
206 ACMatches matches_; | 208 ACMatches matches_; |
207 scoped_ptr<TestingProfile> profile_; | 209 scoped_ptr<TestingProfile> profile_; |
208 HistoryService* history_service_; | 210 HistoryService* history_service_; |
209 scoped_refptr<HistoryURLProvider> autocomplete_; | 211 scoped_refptr<HistoryURLProvider> autocomplete_; |
210 // Should the matches be sorted and duplicates removed? | 212 // Should the matches be sorted and duplicates removed? |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1), | 273 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1), |
272 false, history::SOURCE_BROWSED); | 274 false, history::SOURCE_BROWSED); |
273 } | 275 } |
274 | 276 |
275 void HistoryURLProviderTest::RunTest( | 277 void HistoryURLProviderTest::RunTest( |
276 const base::string16 text, | 278 const base::string16 text, |
277 const base::string16& desired_tld, | 279 const base::string16& desired_tld, |
278 bool prevent_inline_autocomplete, | 280 bool prevent_inline_autocomplete, |
279 const UrlAndLegalDefault* expected_urls, | 281 const UrlAndLegalDefault* expected_urls, |
280 size_t num_results, | 282 size_t num_results, |
281 AutocompleteInput::Type* identified_input_type) { | 283 metrics::OmniboxInputType::Type* identified_input_type) { |
282 AutocompleteInput input(text, base::string16::npos, desired_tld, GURL(), | 284 AutocompleteInput input(text, base::string16::npos, desired_tld, GURL(), |
283 AutocompleteInput::INVALID_SPEC, | 285 metrics::OmniboxEventProto::INVALID_SPEC, |
284 prevent_inline_autocomplete, false, true, true); | 286 prevent_inline_autocomplete, false, true, true); |
285 *identified_input_type = input.type(); | 287 *identified_input_type = input.type(); |
286 autocomplete_->Start(input, false); | 288 autocomplete_->Start(input, false); |
287 if (!autocomplete_->done()) | 289 if (!autocomplete_->done()) |
288 base::MessageLoop::current()->Run(); | 290 base::MessageLoop::current()->Run(); |
289 | 291 |
290 matches_ = autocomplete_->matches(); | 292 matches_ = autocomplete_->matches(); |
291 if (sort_matches_) { | 293 if (sort_matches_) { |
292 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) | 294 for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i) |
293 i->ComputeStrippedDestinationURL(profile_.get()); | 295 i->ComputeStrippedDestinationURL(profile_.get()); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 } | 554 } |
553 | 555 |
554 // Make sure the results for the input 'p' don't change between the first and | 556 // Make sure the results for the input 'p' don't change between the first and |
555 // second passes. | 557 // second passes. |
556 TEST_F(HistoryURLProviderTest, EmptyVisits) { | 558 TEST_F(HistoryURLProviderTest, EmptyVisits) { |
557 // Wait for history to create the in memory DB. | 559 // Wait for history to create the in memory DB. |
558 profile_->BlockUntilHistoryProcessesPendingRequests(); | 560 profile_->BlockUntilHistoryProcessesPendingRequests(); |
559 | 561 |
560 AutocompleteInput input(ASCIIToUTF16("p"), base::string16::npos, | 562 AutocompleteInput input(ASCIIToUTF16("p"), base::string16::npos, |
561 base::string16(), GURL(), | 563 base::string16(), GURL(), |
562 AutocompleteInput::INVALID_SPEC, false, false, true, | 564 metrics::OmniboxEventProto::INVALID_SPEC, false, |
563 true); | 565 false, true, true); |
564 autocomplete_->Start(input, false); | 566 autocomplete_->Start(input, false); |
565 // HistoryURLProvider shouldn't be done (waiting on async results). | 567 // HistoryURLProvider shouldn't be done (waiting on async results). |
566 EXPECT_FALSE(autocomplete_->done()); | 568 EXPECT_FALSE(autocomplete_->done()); |
567 | 569 |
568 // We should get back an entry for pandora. | 570 // We should get back an entry for pandora. |
569 matches_ = autocomplete_->matches(); | 571 matches_ = autocomplete_->matches(); |
570 ASSERT_GT(matches_.size(), 0u); | 572 ASSERT_GT(matches_.size(), 0u); |
571 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); | 573 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url); |
572 int pandora_relevance = matches_[0].relevance; | 574 int pandora_relevance = matches_[0].relevance; |
573 | 575 |
(...skipping 20 matching lines...) Expand all Loading... |
594 }; | 596 }; |
595 RunTest(ASCIIToUTF16("slash"), base::string16(), false, navigation_2, | 597 RunTest(ASCIIToUTF16("slash"), base::string16(), false, navigation_2, |
596 arraysize(navigation_2)); | 598 arraysize(navigation_2)); |
597 | 599 |
598 RunTest(ASCIIToUTF16("this is a query"), base::string16(), false, NULL, 0); | 600 RunTest(ASCIIToUTF16("this is a query"), base::string16(), false, NULL, 0); |
599 } | 601 } |
600 | 602 |
601 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { | 603 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) { |
602 AutocompleteInput input(ASCIIToUTF16("slash "), base::string16::npos, | 604 AutocompleteInput input(ASCIIToUTF16("slash "), base::string16::npos, |
603 base::string16(), GURL(), | 605 base::string16(), GURL(), |
604 AutocompleteInput::INVALID_SPEC, false, false, | 606 metrics::OmniboxEventProto::INVALID_SPEC, false, |
605 true, true); | 607 false, true, true); |
606 autocomplete_->Start(input, false); | 608 autocomplete_->Start(input, false); |
607 if (!autocomplete_->done()) | 609 if (!autocomplete_->done()) |
608 base::MessageLoop::current()->Run(); | 610 base::MessageLoop::current()->Run(); |
609 | 611 |
610 // None of the matches should attempt to autocomplete. | 612 // None of the matches should attempt to autocomplete. |
611 matches_ = autocomplete_->matches(); | 613 matches_ = autocomplete_->matches(); |
612 for (size_t i = 0; i < matches_.size(); ++i) { | 614 for (size_t i = 0; i < matches_.size(); ++i) { |
613 EXPECT_TRUE(matches_[i].inline_autocompletion.empty()); | 615 EXPECT_TRUE(matches_[i].inline_autocompletion.empty()); |
614 EXPECT_FALSE(matches_[i].allowed_to_be_default_match); | 616 EXPECT_FALSE(matches_[i].allowed_to_be_default_match); |
615 } | 617 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); | 662 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); |
661 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); | 663 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); |
662 } | 664 } |
663 } | 665 } |
664 } | 666 } |
665 | 667 |
666 TEST_F(HistoryURLProviderTest, IntranetURLsWithRefs) { | 668 TEST_F(HistoryURLProviderTest, IntranetURLsWithRefs) { |
667 struct TestCase { | 669 struct TestCase { |
668 const char* input; | 670 const char* input; |
669 int relevance; | 671 int relevance; |
670 AutocompleteInput::Type type; | 672 metrics::OmniboxInputType::Type type; |
671 } test_cases[] = { | 673 } test_cases[] = { |
672 { "gooey", 1410, metrics::OmniboxInputType::UNKNOWN }, | 674 { "gooey", 1410, metrics::OmniboxInputType::UNKNOWN }, |
673 { "gooey/", 1410, metrics::OmniboxInputType::URL }, | 675 { "gooey/", 1410, metrics::OmniboxInputType::URL }, |
674 { "gooey#", 1200, metrics::OmniboxInputType::UNKNOWN }, | 676 { "gooey#", 1200, metrics::OmniboxInputType::UNKNOWN }, |
675 { "gooey/#", 1200, metrics::OmniboxInputType::URL }, | 677 { "gooey/#", 1200, metrics::OmniboxInputType::URL }, |
676 { "gooey#foo", 1200, metrics::OmniboxInputType::UNKNOWN }, | 678 { "gooey#foo", 1200, metrics::OmniboxInputType::UNKNOWN }, |
677 { "gooey/#foo", 1200, metrics::OmniboxInputType::URL }, | 679 { "gooey/#foo", 1200, metrics::OmniboxInputType::URL }, |
678 { "gooey# foo", 1200, metrics::OmniboxInputType::UNKNOWN }, | 680 { "gooey# foo", 1200, metrics::OmniboxInputType::UNKNOWN }, |
679 { "gooey/# foo", 1200, metrics::OmniboxInputType::URL }, | 681 { "gooey/# foo", 1200, metrics::OmniboxInputType::URL }, |
680 }; | 682 }; |
681 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 683 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
682 SCOPED_TRACE(test_cases[i].input); | 684 SCOPED_TRACE(test_cases[i].input); |
683 const UrlAndLegalDefault output[] = { | 685 const UrlAndLegalDefault output[] = { |
684 {url_fixer::FixupURL(test_cases[i].input, std::string()).spec(), true}}; | 686 {url_fixer::FixupURL(test_cases[i].input, std::string()).spec(), true}}; |
685 AutocompleteInput::Type type; | 687 metrics::OmniboxInputType::Type type; |
686 ASSERT_NO_FATAL_FAILURE( | 688 ASSERT_NO_FATAL_FAILURE( |
687 RunTest(ASCIIToUTF16(test_cases[i].input), | 689 RunTest(ASCIIToUTF16(test_cases[i].input), |
688 base::string16(), false, output, arraysize(output), &type)); | 690 base::string16(), false, output, arraysize(output), &type)); |
689 // Actual relevance should be at least what test_cases expects and | 691 // Actual relevance should be at least what test_cases expects and |
690 // and no more than 10 more. | 692 // and no more than 10 more. |
691 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); | 693 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance); |
692 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); | 694 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10); |
693 // Input type should be what we expect. This is important because | 695 // Input type should be what we expect. This is important because |
694 // this provider counts on SearchProvider to give queries a relevance | 696 // this provider counts on SearchProvider to give queries a relevance |
695 // score >1200 for UNKNOWN inputs and <1200 for URL inputs. (That's | 697 // score >1200 for UNKNOWN inputs and <1200 for URL inputs. (That's |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 TEST_F(HistoryURLProviderTest, CrashDueToFixup) { | 772 TEST_F(HistoryURLProviderTest, CrashDueToFixup) { |
771 // This test passes if we don't crash. The results don't matter. | 773 // This test passes if we don't crash. The results don't matter. |
772 const char* const test_cases[] = { | 774 const char* const test_cases[] = { |
773 "//c", | 775 "//c", |
774 "\\@st", | 776 "\\@st", |
775 "view-source:x", | 777 "view-source:x", |
776 }; | 778 }; |
777 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 779 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
778 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), base::string16::npos, | 780 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), base::string16::npos, |
779 base::string16(), GURL(), | 781 base::string16(), GURL(), |
780 AutocompleteInput::INVALID_SPEC, | 782 metrics::OmniboxEventProto::INVALID_SPEC, |
781 false, false, true, true); | 783 false, false, true, true); |
782 autocomplete_->Start(input, false); | 784 autocomplete_->Start(input, false); |
783 if (!autocomplete_->done()) | 785 if (!autocomplete_->done()) |
784 base::MessageLoop::current()->Run(); | 786 base::MessageLoop::current()->Run(); |
785 } | 787 } |
786 } | 788 } |
787 | 789 |
788 TEST_F(HistoryURLProviderTest, CullSearchResults) { | 790 TEST_F(HistoryURLProviderTest, CullSearchResults) { |
789 // Set up a default search engine. | 791 // Set up a default search engine. |
790 TemplateURLData data; | 792 TemplateURLData data; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 "mailto://a@b.com", {0, npos, npos}, 0 }, | 890 "mailto://a@b.com", {0, npos, npos}, 0 }, |
889 }; | 891 }; |
890 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 892 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
891 SCOPED_TRACE(testing::Message() << "Index " << i << " input: " | 893 SCOPED_TRACE(testing::Message() << "Index " << i << " input: " |
892 << test_cases[i].input << ", trim_http: " | 894 << test_cases[i].input << ", trim_http: " |
893 << test_cases[i].trim_http); | 895 << test_cases[i].trim_http); |
894 | 896 |
895 AutocompleteInput input(ASCIIToUTF16(test_cases[i].input), | 897 AutocompleteInput input(ASCIIToUTF16(test_cases[i].input), |
896 base::string16::npos, base::string16(), | 898 base::string16::npos, base::string16(), |
897 GURL("about:blank"), | 899 GURL("about:blank"), |
898 AutocompleteInput::INVALID_SPEC, false, false, true, | 900 metrics::OmniboxEventProto::INVALID_SPEC, false, |
899 true); | 901 false, true, true); |
900 AutocompleteMatch match(autocomplete_->SuggestExactInput( | 902 AutocompleteMatch match(autocomplete_->SuggestExactInput( |
901 input.text(), input.canonicalized_url(), test_cases[i].trim_http)); | 903 input.text(), input.canonicalized_url(), test_cases[i].trim_http)); |
902 EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents); | 904 EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents); |
903 for (size_t match_index = 0; match_index < match.contents_class.size(); | 905 for (size_t match_index = 0; match_index < match.contents_class.size(); |
904 ++match_index) { | 906 ++match_index) { |
905 EXPECT_EQ(test_cases[i].offsets[match_index], | 907 EXPECT_EQ(test_cases[i].offsets[match_index], |
906 match.contents_class[match_index].offset); | 908 match.contents_class[match_index].offset); |
907 EXPECT_EQ(ACMatchClassification::URL | | 909 EXPECT_EQ(ACMatchClassification::URL | |
908 (match_index == test_cases[i].match_classification_index ? | 910 (match_index == test_cases[i].match_classification_index ? |
909 ACMatchClassification::MATCH : 0), | 911 ACMatchClassification::MATCH : 0), |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1011 autocomplete_->scoring_params_.experimental_scoring_enabled = true; | 1013 autocomplete_->scoring_params_.experimental_scoring_enabled = true; |
1012 ASSERT_NO_FATAL_FAILURE( | 1014 ASSERT_NO_FATAL_FAILURE( |
1013 RunTest(ASCIIToUTF16(test_cases[i].input), | 1015 RunTest(ASCIIToUTF16(test_cases[i].input), |
1014 base::string16(), false, output, max_matches)); | 1016 base::string16(), false, output, max_matches)); |
1015 for (int j = 0; j < max_matches; ++j) { | 1017 for (int j = 0; j < max_matches; ++j) { |
1016 EXPECT_EQ(test_cases[i].matches[j].experiment_relevance, | 1018 EXPECT_EQ(test_cases[i].matches[j].experiment_relevance, |
1017 matches_[j].relevance); | 1019 matches_[j].relevance); |
1018 } | 1020 } |
1019 } | 1021 } |
1020 } | 1022 } |
OLD | NEW |