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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "chrome/test/base/testing_browser_process.h" | 32 #include "chrome/test/base/testing_browser_process.h" |
33 #include "chrome/test/base/testing_profile.h" | 33 #include "chrome/test/base/testing_profile.h" |
34 #include "components/autocomplete/autocomplete_input.h" | 34 #include "components/autocomplete/autocomplete_input.h" |
35 #include "components/autocomplete/autocomplete_match.h" | 35 #include "components/autocomplete/autocomplete_match.h" |
36 #include "components/autocomplete/autocomplete_provider.h" | 36 #include "components/autocomplete/autocomplete_provider.h" |
37 #include "components/autocomplete/autocomplete_provider_listener.h" | 37 #include "components/autocomplete/autocomplete_provider_listener.h" |
38 #include "components/google/core/browser/google_switches.h" | 38 #include "components/google/core/browser/google_switches.h" |
39 #include "components/metrics/proto/omnibox_event.pb.h" | 39 #include "components/metrics/proto/omnibox_event.pb.h" |
40 #include "components/search_engines/search_engine_type.h" | 40 #include "components/search_engines/search_engine_type.h" |
41 #include "components/search_engines/search_engines_switches.h" | 41 #include "components/search_engines/search_engines_switches.h" |
| 42 #include "components/search_engines/search_terms_data.h" |
42 #include "components/search_engines/template_url.h" | 43 #include "components/search_engines/template_url.h" |
43 #include "components/search_engines/template_url_service.h" | 44 #include "components/search_engines/template_url_service.h" |
44 #include "components/signin/core/browser/signin_manager.h" | 45 #include "components/signin/core/browser/signin_manager.h" |
45 #include "components/sync_driver/pref_names.h" | 46 #include "components/sync_driver/pref_names.h" |
46 #include "components/variations/entropy_provider.h" | 47 #include "components/variations/entropy_provider.h" |
47 #include "components/variations/variations_associated_data.h" | 48 #include "components/variations/variations_associated_data.h" |
48 #include "content/public/test/test_browser_thread_bundle.h" | 49 #include "content/public/test/test_browser_thread_bundle.h" |
49 #include "net/url_request/test_url_fetcher_factory.h" | 50 #include "net/url_request/test_url_fetcher_factory.h" |
50 #include "net/url_request/url_request_status.h" | 51 #include "net/url_request/url_request_status.h" |
51 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
52 | 53 |
53 using base::ASCIIToUTF16; | 54 using base::ASCIIToUTF16; |
54 | 55 |
55 namespace { | 56 namespace { |
56 | 57 |
57 // Returns the first match in |matches| with |allowed_to_be_default_match| | 58 // Returns the first match in |matches| with |allowed_to_be_default_match| |
58 // set to true. | 59 // set to true. |
59 ACMatches::const_iterator FindDefaultMatch(const ACMatches& matches) { | 60 ACMatches::const_iterator FindDefaultMatch(const ACMatches& matches) { |
60 ACMatches::const_iterator it = matches.begin(); | 61 ACMatches::const_iterator it = matches.begin(); |
61 while ((it != matches.end()) && !it->allowed_to_be_default_match) | 62 while ((it != matches.end()) && !it->allowed_to_be_default_match) |
62 ++it; | 63 ++it; |
63 return it; | 64 return it; |
64 } | 65 } |
65 | 66 |
66 class SuggestionDeletionHandler; | 67 class SuggestionDeletionHandler; |
67 class SearchProviderForTest : public SearchProvider { | 68 class SearchProviderForTest : public SearchProvider { |
68 public: | 69 public: |
69 SearchProviderForTest( | 70 SearchProviderForTest(AutocompleteProviderListener* listener, |
70 AutocompleteProviderListener* listener, | 71 TemplateURLService* template_url_service, |
71 Profile* profile); | 72 Profile* profile); |
72 bool is_success() { return is_success_; }; | 73 bool is_success() { return is_success_; }; |
73 | 74 |
74 protected: | 75 protected: |
75 virtual ~SearchProviderForTest(); | 76 virtual ~SearchProviderForTest(); |
76 | 77 |
77 private: | 78 private: |
78 virtual void RecordDeletionResult(bool success) OVERRIDE; | 79 virtual void RecordDeletionResult(bool success) OVERRIDE; |
79 bool is_success_; | 80 bool is_success_; |
80 DISALLOW_COPY_AND_ASSIGN(SearchProviderForTest); | 81 DISALLOW_COPY_AND_ASSIGN(SearchProviderForTest); |
81 }; | 82 }; |
82 | 83 |
83 SearchProviderForTest::SearchProviderForTest( | 84 SearchProviderForTest::SearchProviderForTest( |
84 AutocompleteProviderListener* listener, | 85 AutocompleteProviderListener* listener, |
| 86 TemplateURLService* template_url_service, |
85 Profile* profile) | 87 Profile* profile) |
86 : SearchProvider(listener, profile), is_success_(false) { | 88 : SearchProvider(listener, template_url_service, profile), |
| 89 is_success_(false) { |
87 } | 90 } |
88 | 91 |
89 SearchProviderForTest::~SearchProviderForTest() { | 92 SearchProviderForTest::~SearchProviderForTest() { |
90 } | 93 } |
91 | 94 |
92 void SearchProviderForTest::RecordDeletionResult(bool success) { | 95 void SearchProviderForTest::RecordDeletionResult(bool success) { |
93 is_success_ = success; | 96 is_success_ = success; |
94 } | 97 } |
95 | 98 |
96 } // namespace | 99 } // namespace |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 ASSERT_NE(0, keyword_t_url_->id()); | 281 ASSERT_NE(0, keyword_t_url_->id()); |
279 | 282 |
280 // Add a page and search term for keyword_t_url_. | 283 // Add a page and search term for keyword_t_url_. |
281 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1); | 284 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1); |
282 | 285 |
283 // Keywords are updated by the InMemoryHistoryBackend only after the message | 286 // Keywords are updated by the InMemoryHistoryBackend only after the message |
284 // has been processed on the history thread. Block until history processes all | 287 // has been processed on the history thread. Block until history processes all |
285 // requests to ensure the InMemoryDatabase is the state we expect it. | 288 // requests to ensure the InMemoryDatabase is the state we expect it. |
286 profile_.BlockUntilHistoryProcessesPendingRequests(); | 289 profile_.BlockUntilHistoryProcessesPendingRequests(); |
287 | 290 |
288 provider_ = new SearchProviderForTest(this, &profile_); | 291 provider_ = new SearchProviderForTest(this, turl_model, &profile_); |
289 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; | 292 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; |
290 } | 293 } |
291 | 294 |
292 void SearchProviderTest::TearDown() { | 295 void SearchProviderTest::TearDown() { |
293 base::RunLoop().RunUntilIdle(); | 296 base::RunLoop().RunUntilIdle(); |
294 | 297 |
295 // Shutdown the provider before the profile. | 298 // Shutdown the provider before the profile. |
296 provider_ = NULL; | 299 provider_ = NULL; |
297 } | 300 } |
298 | 301 |
(...skipping 2639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2938 template_url_data.id = SEARCH_ENGINE_GOOGLE; | 2941 template_url_data.id = SEARCH_ENGINE_GOOGLE; |
2939 TemplateURL google_template_url(template_url_data); | 2942 TemplateURL google_template_url(template_url_data); |
2940 | 2943 |
2941 // Create field trial. | 2944 // Create field trial. |
2942 CreateZeroSuggestFieldTrial(true); | 2945 CreateZeroSuggestFieldTrial(true); |
2943 | 2946 |
2944 // Not signed in. | 2947 // Not signed in. |
2945 EXPECT_FALSE(SearchProvider::CanSendURL( | 2948 EXPECT_FALSE(SearchProvider::CanSendURL( |
2946 GURL("http://www.google.com/search"), | 2949 GURL("http://www.google.com/search"), |
2947 GURL("https://www.google.com/complete/search"), &google_template_url, | 2950 GURL("https://www.google.com/complete/search"), &google_template_url, |
2948 metrics::OmniboxEventProto::OTHER, &profile_)); | 2951 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
2949 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(&profile_); | 2952 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(&profile_); |
2950 signin->SetAuthenticatedUsername("test"); | 2953 signin->SetAuthenticatedUsername("test"); |
2951 | 2954 |
2952 // All conditions should be met. | 2955 // All conditions should be met. |
2953 EXPECT_TRUE(SearchProvider::CanSendURL( | 2956 EXPECT_TRUE(SearchProvider::CanSendURL( |
2954 GURL("http://www.google.com/search"), | 2957 GURL("http://www.google.com/search"), |
2955 GURL("https://www.google.com/complete/search"), &google_template_url, | 2958 GURL("https://www.google.com/complete/search"), &google_template_url, |
2956 metrics::OmniboxEventProto::OTHER, &profile_)); | 2959 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
2957 | 2960 |
2958 // Not in field trial. | 2961 // Not in field trial. |
2959 ResetFieldTrialList(); | 2962 ResetFieldTrialList(); |
2960 CreateZeroSuggestFieldTrial(false); | 2963 CreateZeroSuggestFieldTrial(false); |
2961 EXPECT_FALSE(SearchProvider::CanSendURL( | 2964 EXPECT_FALSE(SearchProvider::CanSendURL( |
2962 GURL("http://www.google.com/search"), | 2965 GURL("http://www.google.com/search"), |
2963 GURL("https://www.google.com/complete/search"), &google_template_url, | 2966 GURL("https://www.google.com/complete/search"), &google_template_url, |
2964 metrics::OmniboxEventProto::OTHER, &profile_)); | 2967 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
2965 ResetFieldTrialList(); | 2968 ResetFieldTrialList(); |
2966 CreateZeroSuggestFieldTrial(true); | 2969 CreateZeroSuggestFieldTrial(true); |
2967 | 2970 |
2968 // Invalid page URL. | 2971 // Invalid page URL. |
2969 EXPECT_FALSE(SearchProvider::CanSendURL( | 2972 EXPECT_FALSE(SearchProvider::CanSendURL( |
2970 GURL("badpageurl"), | 2973 GURL("badpageurl"), |
2971 GURL("https://www.google.com/complete/search"), &google_template_url, | 2974 GURL("https://www.google.com/complete/search"), &google_template_url, |
2972 metrics::OmniboxEventProto::OTHER, &profile_)); | 2975 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
2973 | 2976 |
2974 // Invalid page classification. | 2977 // Invalid page classification. |
2975 EXPECT_FALSE(SearchProvider::CanSendURL( | 2978 EXPECT_FALSE(SearchProvider::CanSendURL( |
2976 GURL("http://www.google.com/search"), | 2979 GURL("http://www.google.com/search"), |
2977 GURL("https://www.google.com/complete/search"), &google_template_url, | 2980 GURL("https://www.google.com/complete/search"), &google_template_url, |
2978 metrics::OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS, | 2981 metrics::OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS, |
2979 &profile_)); | 2982 SearchTermsData(), &profile_)); |
2980 | 2983 |
2981 // Invalid page classification. | 2984 // Invalid page classification. |
2982 EXPECT_FALSE(SearchProvider::CanSendURL( | 2985 EXPECT_FALSE(SearchProvider::CanSendURL( |
2983 GURL("http://www.google.com/search"), | 2986 GURL("http://www.google.com/search"), |
2984 GURL("https://www.google.com/complete/search"), &google_template_url, | 2987 GURL("https://www.google.com/complete/search"), &google_template_url, |
2985 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 2988 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
2986 &profile_)); | 2989 SearchTermsData(), &profile_)); |
2987 | 2990 |
2988 // HTTPS page URL on same domain as provider. | 2991 // HTTPS page URL on same domain as provider. |
2989 EXPECT_TRUE(SearchProvider::CanSendURL( | 2992 EXPECT_TRUE(SearchProvider::CanSendURL( |
2990 GURL("https://www.google.com/search"), | 2993 GURL("https://www.google.com/search"), |
2991 GURL("https://www.google.com/complete/search"), | 2994 GURL("https://www.google.com/complete/search"), |
2992 &google_template_url, metrics::OmniboxEventProto::OTHER, &profile_)); | 2995 &google_template_url, metrics::OmniboxEventProto::OTHER, |
| 2996 SearchTermsData(), &profile_)); |
2993 | 2997 |
2994 // Non-HTTP[S] page URL on same domain as provider. | 2998 // Non-HTTP[S] page URL on same domain as provider. |
2995 EXPECT_FALSE(SearchProvider::CanSendURL( | 2999 EXPECT_FALSE(SearchProvider::CanSendURL( |
2996 GURL("ftp://www.google.com/search"), | 3000 GURL("ftp://www.google.com/search"), |
2997 GURL("https://www.google.com/complete/search"), &google_template_url, | 3001 GURL("https://www.google.com/complete/search"), &google_template_url, |
2998 metrics::OmniboxEventProto::OTHER, &profile_)); | 3002 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
2999 | 3003 |
3000 // Non-HTTP page URL on different domain. | 3004 // Non-HTTP page URL on different domain. |
3001 EXPECT_FALSE(SearchProvider::CanSendURL( | 3005 EXPECT_FALSE(SearchProvider::CanSendURL( |
3002 GURL("https://www.notgoogle.com/search"), | 3006 GURL("https://www.notgoogle.com/search"), |
3003 GURL("https://www.google.com/complete/search"), &google_template_url, | 3007 GURL("https://www.google.com/complete/search"), &google_template_url, |
3004 metrics::OmniboxEventProto::OTHER, &profile_)); | 3008 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
3005 | 3009 |
3006 // Non-HTTPS provider. | 3010 // Non-HTTPS provider. |
3007 EXPECT_FALSE(SearchProvider::CanSendURL( | 3011 EXPECT_FALSE(SearchProvider::CanSendURL( |
3008 GURL("http://www.google.com/search"), | 3012 GURL("http://www.google.com/search"), |
3009 GURL("http://www.google.com/complete/search"), &google_template_url, | 3013 GURL("http://www.google.com/complete/search"), &google_template_url, |
3010 metrics::OmniboxEventProto::OTHER, &profile_)); | 3014 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
3011 | 3015 |
3012 // Suggest disabled. | 3016 // Suggest disabled. |
3013 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | 3017 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
3014 EXPECT_FALSE(SearchProvider::CanSendURL( | 3018 EXPECT_FALSE(SearchProvider::CanSendURL( |
3015 GURL("http://www.google.com/search"), | 3019 GURL("http://www.google.com/search"), |
3016 GURL("https://www.google.com/complete/search"), &google_template_url, | 3020 GURL("https://www.google.com/complete/search"), &google_template_url, |
3017 metrics::OmniboxEventProto::OTHER, &profile_)); | 3021 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
3018 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 3022 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
3019 | 3023 |
3020 // Incognito. | 3024 // Incognito. |
3021 EXPECT_FALSE(SearchProvider::CanSendURL( | 3025 EXPECT_FALSE(SearchProvider::CanSendURL( |
3022 GURL("http://www.google.com/search"), | 3026 GURL("http://www.google.com/search"), |
3023 GURL("https://www.google.com/complete/search"), &google_template_url, | 3027 GURL("https://www.google.com/complete/search"), &google_template_url, |
3024 metrics::OmniboxEventProto::OTHER, profile_.GetOffTheRecordProfile())); | 3028 metrics::OmniboxEventProto::OTHER, SearchTermsData(), |
| 3029 profile_.GetOffTheRecordProfile())); |
3025 | 3030 |
3026 // Tab sync not enabled. | 3031 // Tab sync not enabled. |
3027 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced, | 3032 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced, |
3028 false); | 3033 false); |
3029 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, false); | 3034 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, false); |
3030 EXPECT_FALSE(SearchProvider::CanSendURL( | 3035 EXPECT_FALSE(SearchProvider::CanSendURL( |
3031 GURL("http://www.google.com/search"), | 3036 GURL("http://www.google.com/search"), |
3032 GURL("https://www.google.com/complete/search"), &google_template_url, | 3037 GURL("https://www.google.com/complete/search"), &google_template_url, |
3033 metrics::OmniboxEventProto::OTHER, &profile_)); | 3038 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
3034 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, true); | 3039 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, true); |
3035 | 3040 |
3036 // Tab sync is encrypted. | 3041 // Tab sync is encrypted. |
3037 ProfileSyncService* service = | 3042 ProfileSyncService* service = |
3038 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_); | 3043 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_); |
3039 syncer::ModelTypeSet encrypted_types = service->GetEncryptedDataTypes(); | 3044 syncer::ModelTypeSet encrypted_types = service->GetEncryptedDataTypes(); |
3040 encrypted_types.Put(syncer::SESSIONS); | 3045 encrypted_types.Put(syncer::SESSIONS); |
3041 service->OnEncryptedTypesChanged(encrypted_types, false); | 3046 service->OnEncryptedTypesChanged(encrypted_types, false); |
3042 EXPECT_FALSE(SearchProvider::CanSendURL( | 3047 EXPECT_FALSE(SearchProvider::CanSendURL( |
3043 GURL("http://www.google.com/search"), | 3048 GURL("http://www.google.com/search"), |
3044 GURL("https://www.google.com/complete/search"), &google_template_url, | 3049 GURL("https://www.google.com/complete/search"), &google_template_url, |
3045 metrics::OmniboxEventProto::OTHER, &profile_)); | 3050 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
3046 encrypted_types.Remove(syncer::SESSIONS); | 3051 encrypted_types.Remove(syncer::SESSIONS); |
3047 service->OnEncryptedTypesChanged(encrypted_types, false); | 3052 service->OnEncryptedTypesChanged(encrypted_types, false); |
3048 | 3053 |
3049 // Check that there were no side effects from previous tests. | 3054 // Check that there were no side effects from previous tests. |
3050 EXPECT_TRUE(SearchProvider::CanSendURL( | 3055 EXPECT_TRUE(SearchProvider::CanSendURL( |
3051 GURL("http://www.google.com/search"), | 3056 GURL("http://www.google.com/search"), |
3052 GURL("https://www.google.com/complete/search"), &google_template_url, | 3057 GURL("https://www.google.com/complete/search"), &google_template_url, |
3053 metrics::OmniboxEventProto::OTHER, &profile_)); | 3058 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); |
3054 } | 3059 } |
3055 | 3060 |
3056 TEST_F(SearchProviderTest, TestDeleteMatch) { | 3061 TEST_F(SearchProviderTest, TestDeleteMatch) { |
3057 AutocompleteMatch match(provider_, 0, true, | 3062 AutocompleteMatch match(provider_, 0, true, |
3058 AutocompleteMatchType::SEARCH_SUGGEST); | 3063 AutocompleteMatchType::SEARCH_SUGGEST); |
3059 match.RecordAdditionalInfo( | 3064 match.RecordAdditionalInfo( |
3060 SearchProvider::kDeletionUrlKey, | 3065 SearchProvider::kDeletionUrlKey, |
3061 "https://www.google.com/complete/deleteitem?q=foo"); | 3066 "https://www.google.com/complete/deleteitem?q=foo"); |
3062 | 3067 |
3063 // Test a successful deletion request. | 3068 // Test a successful deletion request. |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3262 // Mismatching input will return empty prefetch data. | 3267 // Mismatching input will return empty prefetch data. |
3263 AutocompleteInput input2(base::ASCIIToUTF16("weather n"), | 3268 AutocompleteInput input2(base::ASCIIToUTF16("weather n"), |
3264 base::string16::npos, base::string16(), GURL(), | 3269 base::string16::npos, base::string16(), GURL(), |
3265 metrics::OmniboxEventProto::INVALID_SPEC, false, | 3270 metrics::OmniboxEventProto::INVALID_SPEC, false, |
3266 false, true, true, | 3271 false, true, true, |
3267 ChromeAutocompleteSchemeClassifier(&profile_)); | 3272 ChromeAutocompleteSchemeClassifier(&profile_)); |
3268 provider_->DoAnswersQuery(input2); | 3273 provider_->DoAnswersQuery(input2); |
3269 EXPECT_TRUE(provider_->prefetch_data_.full_query_text.empty()); | 3274 EXPECT_TRUE(provider_->prefetch_data_.full_query_text.empty()); |
3270 EXPECT_TRUE(provider_->prefetch_data_.query_type.empty()); | 3275 EXPECT_TRUE(provider_->prefetch_data_.query_type.empty()); |
3271 } | 3276 } |
OLD | NEW |